Skip to content

Commit

Permalink
src: use Maybe<void> in SecureContext
Browse files Browse the repository at this point in the history
With recent versions of V8, it is not necessary to use Maybe<bool>
anymore. This changes member functions of SecureContext to use
Maybe<void> instead.

PR-URL: #53883
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
tniessen authored and targos committed Jul 28, 2024
1 parent 87bab76 commit 9ffaf76
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
28 changes: 15 additions & 13 deletions src/crypto/crypto_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ using v8::HandleScope;
using v8::Int32;
using v8::Integer;
using v8::Isolate;
using v8::Just;
using v8::JustVoid;
using v8::Local;
using v8::Maybe;
using v8::Nothing;
Expand Down Expand Up @@ -576,20 +576,20 @@ void SecureContext::SetKeylogCallback(KeylogCb cb) {
SSL_CTX_set_keylog_callback(ctx_.get(), cb);
}

Maybe<bool> SecureContext::UseKey(Environment* env,
Maybe<void> SecureContext::UseKey(Environment* env,
std::shared_ptr<KeyObjectData> key) {
if (key->GetKeyType() != KeyType::kKeyTypePrivate) {
THROW_ERR_CRYPTO_INVALID_KEYTYPE(env);
return Nothing<bool>();
return Nothing<void>();
}

ClearErrorOnReturn clear_error_on_return;
if (!SSL_CTX_use_PrivateKey(ctx_.get(), key->GetAsymmetricKey().get())) {
ThrowCryptoError(env, ERR_get_error(), "SSL_CTX_use_PrivateKey");
return Nothing<bool>();
return Nothing<void>();
}

return Just(true);
return JustVoid();
}

void SecureContext::SetKey(const FunctionCallbackInfo<Value>& args) {
Expand Down Expand Up @@ -689,9 +689,10 @@ void SecureContext::SetEngineKey(const FunctionCallbackInfo<Value>& args) {
}
#endif // !OPENSSL_NO_ENGINE

Maybe<bool> SecureContext::AddCert(Environment* env, BIOPointer&& bio) {
Maybe<void> SecureContext::AddCert(Environment* env, BIOPointer&& bio) {
ClearErrorOnReturn clear_error_on_return;
if (!bio) return Just(false);
// TODO(tniessen): this should be checked by the caller and not treated as ok
if (!bio) return JustVoid();
cert_.reset();
issuer_.reset();

Expand All @@ -701,9 +702,9 @@ Maybe<bool> SecureContext::AddCert(Environment* env, BIOPointer&& bio) {
if (SSL_CTX_use_certificate_chain(
ctx_.get(), std::move(bio), &cert_, &issuer_) == 0) {
ThrowCryptoError(env, ERR_get_error(), "SSL_CTX_use_certificate_chain");
return Nothing<bool>();
return Nothing<void>();
}
return Just(true);
return JustVoid();
}

void SecureContext::SetCert(const FunctionCallbackInfo<Value>& args) {
Expand Down Expand Up @@ -745,16 +746,17 @@ void SecureContext::AddCACert(const FunctionCallbackInfo<Value>& args) {
sc->SetCACert(bio);
}

Maybe<bool> SecureContext::SetCRL(Environment* env, const BIOPointer& bio) {
Maybe<void> SecureContext::SetCRL(Environment* env, const BIOPointer& bio) {
ClearErrorOnReturn clear_error_on_return;
if (!bio) return Just(false);
// TODO(tniessen): this should be checked by the caller and not treated as ok
if (!bio) return JustVoid();

DeleteFnPtr<X509_CRL, X509_CRL_free> crl(
PEM_read_bio_X509_CRL(bio.get(), nullptr, NoPasswordCallback, nullptr));

if (!crl) {
THROW_ERR_CRYPTO_OPERATION_FAILED(env, "Failed to parse CRL");
return Nothing<bool>();
return Nothing<void>();
}

X509_STORE* cert_store = SSL_CTX_get_cert_store(ctx_.get());
Expand All @@ -767,7 +769,7 @@ Maybe<bool> SecureContext::SetCRL(Environment* env, const BIOPointer& bio) {
CHECK_EQ(1,
X509_STORE_set_flags(
cert_store, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL));
return Just(true);
return JustVoid();
}

void SecureContext::AddCRL(const FunctionCallbackInfo<Value>& args) {
Expand Down
6 changes: 3 additions & 3 deletions src/crypto/crypto_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ class SecureContext final : public BaseObject {
inline const X509Pointer& issuer() const { return issuer_; }
inline const X509Pointer& cert() const { return cert_; }

v8::Maybe<bool> AddCert(Environment* env, BIOPointer&& bio);
v8::Maybe<bool> SetCRL(Environment* env, const BIOPointer& bio);
v8::Maybe<bool> UseKey(Environment* env, std::shared_ptr<KeyObjectData> key);
v8::Maybe<void> AddCert(Environment* env, BIOPointer&& bio);
v8::Maybe<void> SetCRL(Environment* env, const BIOPointer& bio);
v8::Maybe<void> UseKey(Environment* env, std::shared_ptr<KeyObjectData> key);

void SetCACert(const BIOPointer& bio);
void SetRootCerts();
Expand Down

0 comments on commit 9ffaf76

Please sign in to comment.