Skip to content

Commit 4f177c4

Browse files
tniessencodebytere
authored andcommitted
tls: simplify errors using ThrowCryptoError
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]>
1 parent dbe2d85 commit 4f177c4

File tree

1 file changed

+6
-24
lines changed

1 file changed

+6
-24
lines changed

src/node_crypto.cc

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -733,19 +733,14 @@ void SecureContext::SetKey(const FunctionCallbackInfo<Value>& args) {
733733

734734
if (!key) {
735735
unsigned long err = ERR_get_error(); // NOLINT(runtime/int)
736-
if (!err) {
737-
return env->ThrowError("PEM_read_bio_PrivateKey");
738-
}
739-
return ThrowCryptoError(env, err);
736+
return ThrowCryptoError(env, err, "PEM_read_bio_PrivateKey");
740737
}
741738

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

744741
if (!rv) {
745742
unsigned long err = ERR_get_error(); // NOLINT(runtime/int)
746-
if (!err)
747-
return env->ThrowError("SSL_CTX_use_PrivateKey");
748-
return ThrowCryptoError(env, err);
743+
return ThrowCryptoError(env, err, "SSL_CTX_use_PrivateKey");
749744
}
750745
}
751746

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

972967
if (!rv) {
973968
unsigned long err = ERR_get_error(); // NOLINT(runtime/int)
974-
if (!err) {
975-
return env->ThrowError("SSL_CTX_use_certificate_chain");
976-
}
977-
return ThrowCryptoError(env, err);
969+
return ThrowCryptoError(env, err, "SSL_CTX_use_certificate_chain");
978970
}
979971
}
980972

@@ -1183,11 +1175,7 @@ void SecureContext::SetCipherSuites(const FunctionCallbackInfo<Value>& args) {
11831175
const node::Utf8Value ciphers(args.GetIsolate(), args[0]);
11841176
if (!SSL_CTX_set_ciphersuites(sc->ctx_.get(), *ciphers)) {
11851177
unsigned long err = ERR_get_error(); // NOLINT(runtime/int)
1186-
if (!err) {
1187-
// This would be an OpenSSL bug if it happened.
1188-
return env->ThrowError("Failed to set ciphers");
1189-
}
1190-
return ThrowCryptoError(env, err);
1178+
return ThrowCryptoError(env, err, "Failed to set ciphers");
11911179
}
11921180
#endif
11931181
}
@@ -1205,10 +1193,6 @@ void SecureContext::SetCiphers(const FunctionCallbackInfo<Value>& args) {
12051193
const node::Utf8Value ciphers(args.GetIsolate(), args[0]);
12061194
if (!SSL_CTX_set_cipher_list(sc->ctx_.get(), *ciphers)) {
12071195
unsigned long err = ERR_get_error(); // NOLINT(runtime/int)
1208-
if (!err) {
1209-
// This would be an OpenSSL bug if it happened.
1210-
return env->ThrowError("Failed to set ciphers");
1211-
}
12121196

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

@@ -3029,9 +3013,7 @@ void SSLWrap<Base>::CertCbDone(const FunctionCallbackInfo<Value>& args) {
30293013
// Not clear why sometimes we throw error, and sometimes we call
30303014
// onerror(). Both cause .destroy(), but onerror does a bit more.
30313015
unsigned long err = ERR_get_error(); // NOLINT(runtime/int)
3032-
if (!err)
3033-
return env->ThrowError("CertCbDone");
3034-
return ThrowCryptoError(env, err);
3016+
return ThrowCryptoError(env, err, "CertCbDone");
30353017
}
30363018
} else {
30373019
// Failure: incorrect SNI context object

0 commit comments

Comments
 (0)