Skip to content

Commit

Permalink
tls: simplify errors using ThrowCryptoError
Browse files Browse the repository at this point in the history
PR-URL: #31436
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: David Carlier <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
tniessen authored and codebytere committed Feb 17, 2020
1 parent 38ea536 commit 03aac4e
Showing 1 changed file with 6 additions and 24 deletions.
30 changes: 6 additions & 24 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -733,19 +733,14 @@ void SecureContext::SetKey(const FunctionCallbackInfo<Value>& args) {

if (!key) {
unsigned long err = ERR_get_error(); // NOLINT(runtime/int)
if (!err) {
return env->ThrowError("PEM_read_bio_PrivateKey");
}
return ThrowCryptoError(env, err);
return ThrowCryptoError(env, err, "PEM_read_bio_PrivateKey");
}

int rv = SSL_CTX_use_PrivateKey(sc->ctx_.get(), key.get());

if (!rv) {
unsigned long err = ERR_get_error(); // NOLINT(runtime/int)
if (!err)
return env->ThrowError("SSL_CTX_use_PrivateKey");
return ThrowCryptoError(env, err);
return ThrowCryptoError(env, err, "SSL_CTX_use_PrivateKey");
}
}

Expand Down Expand Up @@ -971,10 +966,7 @@ void SecureContext::SetCert(const FunctionCallbackInfo<Value>& args) {

if (!rv) {
unsigned long err = ERR_get_error(); // NOLINT(runtime/int)
if (!err) {
return env->ThrowError("SSL_CTX_use_certificate_chain");
}
return ThrowCryptoError(env, err);
return ThrowCryptoError(env, err, "SSL_CTX_use_certificate_chain");
}
}

Expand Down Expand Up @@ -1183,11 +1175,7 @@ void SecureContext::SetCipherSuites(const FunctionCallbackInfo<Value>& args) {
const node::Utf8Value ciphers(args.GetIsolate(), args[0]);
if (!SSL_CTX_set_ciphersuites(sc->ctx_.get(), *ciphers)) {
unsigned long err = ERR_get_error(); // NOLINT(runtime/int)
if (!err) {
// This would be an OpenSSL bug if it happened.
return env->ThrowError("Failed to set ciphers");
}
return ThrowCryptoError(env, err);
return ThrowCryptoError(env, err, "Failed to set ciphers");
}
#endif
}
Expand All @@ -1205,10 +1193,6 @@ void SecureContext::SetCiphers(const FunctionCallbackInfo<Value>& args) {
const node::Utf8Value ciphers(args.GetIsolate(), args[0]);
if (!SSL_CTX_set_cipher_list(sc->ctx_.get(), *ciphers)) {
unsigned long err = ERR_get_error(); // NOLINT(runtime/int)
if (!err) {
// This would be an OpenSSL bug if it happened.
return env->ThrowError("Failed to set ciphers");
}

if (strlen(*ciphers) == 0 && ERR_GET_REASON(err) == SSL_R_NO_CIPHER_MATCH) {
// TLS1.2 ciphers were deliberately cleared, so don't consider
Expand All @@ -1217,7 +1201,7 @@ void SecureContext::SetCiphers(const FunctionCallbackInfo<Value>& args) {
// that's actually an error.
return;
}
return ThrowCryptoError(env, err);
return ThrowCryptoError(env, err, "Failed to set ciphers");
}
}

Expand Down Expand Up @@ -3030,9 +3014,7 @@ void SSLWrap<Base>::CertCbDone(const FunctionCallbackInfo<Value>& args) {
// Not clear why sometimes we throw error, and sometimes we call
// onerror(). Both cause .destroy(), but onerror does a bit more.
unsigned long err = ERR_get_error(); // NOLINT(runtime/int)
if (!err)
return env->ThrowError("CertCbDone");
return ThrowCryptoError(env, err);
return ThrowCryptoError(env, err, "CertCbDone");
}
} else {
// Failure: incorrect SNI context object
Expand Down

0 comments on commit 03aac4e

Please sign in to comment.