Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: use Maybe<void> in SecureContext #53883

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions src/crypto/crypto_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,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 @@ -575,20 +575,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 @@ -686,9 +686,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 @@ -698,9 +699,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 @@ -742,16 +743,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 @@ -764,7 +766,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
Loading