-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
Decorate crypto openssl errors with .code, .reason, ... #26868
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -212,6 +212,113 @@ static int NoPasswordCallback(char* buf, int size, int rwflag, void* u) { | |||||||
} | ||||||||
|
||||||||
|
||||||||
// namespace node::crypto::error | ||||||||
namespace error { | ||||||||
sam-github marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
void Decorate(Environment* env, Local<Object> obj, | ||||||||
unsigned long err) { // NOLINT(runtime/int) | ||||||||
if (err == 0) return; // No decoration possible. | ||||||||
|
||||||||
const char* ls = ERR_lib_error_string(err); | ||||||||
const char* fs = ERR_func_error_string(err); | ||||||||
const char* rs = ERR_reason_error_string(err); | ||||||||
|
||||||||
Isolate* isolate = env->isolate(); | ||||||||
Local<Context> context = isolate->GetCurrentContext(); | ||||||||
|
||||||||
if (ls != nullptr) { | ||||||||
if (obj->Set(context, env->library_string(), | ||||||||
OneByteString(isolate, ls)).IsNothing()) { | ||||||||
return; | ||||||||
} | ||||||||
} | ||||||||
if (fs != nullptr) { | ||||||||
if (obj->Set(context, env->function_string(), | ||||||||
OneByteString(isolate, fs)).IsNothing()) { | ||||||||
return; | ||||||||
} | ||||||||
} | ||||||||
if (rs != nullptr) { | ||||||||
if (obj->Set(context, env->reason_string(), | ||||||||
OneByteString(isolate, rs)).IsNothing()) { | ||||||||
return; | ||||||||
} | ||||||||
|
||||||||
// SSL has no API to recover the error name from the number, so we | ||||||||
// transform reason strings like "this error" to "ERR_SSL_THIS_ERROR", | ||||||||
// which ends up being close to the original error macro name. | ||||||||
std::string reason(rs); | ||||||||
|
||||||||
for (auto& c : reason) { | ||||||||
if (c == ' ') | ||||||||
c = '_'; | ||||||||
else | ||||||||
c = ToUpper(c); | ||||||||
} | ||||||||
|
||||||||
#define OSSL_ERROR_CODES_MAP(V) \ | ||||||||
V(SYS) \ | ||||||||
V(BN) \ | ||||||||
V(RSA) \ | ||||||||
V(DH) \ | ||||||||
V(EVP) \ | ||||||||
V(BUF) \ | ||||||||
V(OBJ) \ | ||||||||
V(PEM) \ | ||||||||
V(DSA) \ | ||||||||
V(X509) \ | ||||||||
V(ASN1) \ | ||||||||
V(CONF) \ | ||||||||
V(CRYPTO) \ | ||||||||
V(EC) \ | ||||||||
V(SSL) \ | ||||||||
V(BIO) \ | ||||||||
V(PKCS7) \ | ||||||||
V(X509V3) \ | ||||||||
V(PKCS12) \ | ||||||||
V(RAND) \ | ||||||||
V(DSO) \ | ||||||||
V(ENGINE) \ | ||||||||
V(OCSP) \ | ||||||||
V(UI) \ | ||||||||
V(COMP) \ | ||||||||
V(ECDSA) \ | ||||||||
V(ECDH) \ | ||||||||
V(OSSL_STORE) \ | ||||||||
V(FIPS) \ | ||||||||
V(CMS) \ | ||||||||
V(TS) \ | ||||||||
V(HMAC) \ | ||||||||
V(CT) \ | ||||||||
V(ASYNC) \ | ||||||||
V(KDF) \ | ||||||||
V(SM2) \ | ||||||||
V(USER) \ | ||||||||
|
||||||||
#define V(name) case ERR_LIB_##name: lib = #name "_"; break; | ||||||||
const char* lib = ""; | ||||||||
const char* prefix = "OSSL_"; | ||||||||
switch (ERR_GET_LIB(err)) { OSSL_ERROR_CODES_MAP(V) } | ||||||||
#undef V | ||||||||
#undef OSSL_ERROR_CODES_MAP | ||||||||
// Don't generate codes like "ERR_OSSL_SSL_". | ||||||||
if (lib && strcmp(lib, "SSL_") == 0) | ||||||||
prefix = ""; | ||||||||
|
||||||||
// All OpenSSL reason strings fit in a single 80-column macro definition, | ||||||||
// all prefix lengths are <= 10, and ERR_OSSL_ is 9, so 128 is more than | ||||||||
// sufficient. | ||||||||
char code[128]; | ||||||||
snprintf(code, sizeof(code), "ERR_%s%s%s", prefix, lib, reason.c_str()); | ||||||||
|
||||||||
if (obj->Set(env->isolate()->GetCurrentContext(), | ||||||||
env->code_string(), | ||||||||
OneByteString(env->isolate(), code)).IsNothing()) | ||||||||
return; | ||||||||
} | ||||||||
} | ||||||||
} // namespace error | ||||||||
|
||||||||
|
||||||||
struct CryptoErrorVector : public std::vector<std::string> { | ||||||||
inline void Capture() { | ||||||||
clear(); | ||||||||
|
@@ -258,6 +365,8 @@ struct CryptoErrorVector : public std::vector<std::string> { | |||||||
|
||||||||
void ThrowCryptoError(Environment* env, | ||||||||
unsigned long err, // NOLINT(runtime/int) | ||||||||
// Default, only used if there is no SSL `err` which can | ||||||||
// be used to create a long-style message string. | ||||||||
const char* message = nullptr) { | ||||||||
char message_buffer[128] = {0}; | ||||||||
if (err != 0 || message == nullptr) { | ||||||||
|
@@ -270,7 +379,11 @@ void ThrowCryptoError(Environment* env, | |||||||
.ToLocalChecked(); | ||||||||
CryptoErrorVector errors; | ||||||||
errors.Capture(); | ||||||||
auto exception = errors.ToException(env, exception_string); | ||||||||
Local<Value> exception = errors.ToException(env, exception_string); | ||||||||
Local<Object> obj; | ||||||||
if (!exception->ToObject(env->context()).ToLocal(&obj)) | ||||||||
return; | ||||||||
error::Decorate(env, obj, err); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
env->isolate()->ThrowException(exception); | ||||||||
} | ||||||||
|
||||||||
|
@@ -2226,9 +2339,9 @@ void SSLWrap<Base>::Renegotiate(const FunctionCallbackInfo<Value>& args) { | |||||||
|
||||||||
ClearErrorOnReturn clear_error_on_return; | ||||||||
|
||||||||
// TODO(@sam-github) Return/throw an error, don't discard the SSL error info. | ||||||||
bool yes = SSL_renegotiate(w->ssl_.get()) == 1; | ||||||||
args.GetReturnValue().Set(yes); | ||||||||
if (SSL_renegotiate(w->ssl_.get()) != 1) { | ||||||||
return ThrowCryptoError(w->ssl_env(), ERR_get_error()); | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
|
||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.