-
Notifications
You must be signed in to change notification settings - Fork 30.7k
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
Crypto add opensslerror stack #15518
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f2c9e49
crypto: better crypto error messages
gla5001 dc2165a
Review updates and tests
gla5001 5c24f07
Additional review updates.
gla5001 d1296cf
Review update. Use the overload of Set()
gla5001 2b177cb
Nit fix: changing local variable to snake_case
gla5001 bbaea8b
PR review updates
gla5001 a5a2f64
changing openSSLErrorStack property name to opensslErrorStack
gla5001 2ad7440
fixing long doc line
gla5001 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 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 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 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 |
---|---|---|
|
@@ -255,13 +255,65 @@ void ThrowCryptoError(Environment* env, | |
unsigned long err, // NOLINT(runtime/int) | ||
const char* default_message = nullptr) { | ||
HandleScope scope(env->isolate()); | ||
Local<String> message; | ||
|
||
if (err != 0 || default_message == nullptr) { | ||
char errmsg[128] = { 0 }; | ||
ERR_error_string_n(err, errmsg, sizeof(errmsg)); | ||
env->ThrowError(errmsg); | ||
message = String::NewFromUtf8(env->isolate(), errmsg, | ||
v8::NewStringType::kNormal) | ||
.ToLocalChecked(); | ||
} else { | ||
env->ThrowError(default_message); | ||
message = String::NewFromUtf8(env->isolate(), default_message, | ||
v8::NewStringType::kNormal) | ||
.ToLocalChecked(); | ||
} | ||
|
||
Local<Value> exception_v = Exception::Error(message); | ||
CHECK(!exception_v.IsEmpty()); | ||
Local<Object> exception = exception_v.As<Object>(); | ||
ERR_STATE* es = ERR_get_state(); | ||
|
||
if (es->bottom != es->top) { | ||
Local<Array> error_stack = Array::New(env->isolate()); | ||
int top = es->top; | ||
|
||
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. Now, we wont add the property to the exception every time. |
||
// Build the error_stack array to be added to opensslErrorStack property. | ||
for (unsigned int i = 0; es->bottom != es->top;) { | ||
unsigned long err_buf = es->err_buffer[es->top]; // NOLINT(runtime/int) | ||
// Only add error string if there is valid err_buffer. | ||
if (err_buf) { | ||
char tmp_str[256]; | ||
ERR_error_string_n(err_buf, tmp_str, sizeof(tmp_str)); | ||
error_stack->Set(env->context(), i, | ||
String::NewFromUtf8(env->isolate(), tmp_str, | ||
v8::NewStringType::kNormal) | ||
.ToLocalChecked()).FromJust(); | ||
// Only increment if we added to error_stack. | ||
i++; | ||
} | ||
|
||
// Since the ERR_STATE is a ring buffer, we need to use modular | ||
// arithmetic to loop back around in the case where bottom is after top. | ||
// Using ERR_NUM_ERRORS macro defined in openssl. | ||
es->top = (((es->top - 1) % ERR_NUM_ERRORS) + ERR_NUM_ERRORS) % | ||
ERR_NUM_ERRORS; | ||
} | ||
|
||
// Restore top. | ||
es->top = top; | ||
|
||
// Add the opensslErrorStack property to the exception object. | ||
// The new property will look like the following: | ||
// opensslErrorStack: [ | ||
// 'error:0906700D:PEM routines:PEM_ASN1_read_bio:ASN1 lib', | ||
// 'error:0D07803A:asn1 encoding routines:ASN1_ITEM_EX_D2I:nested asn1 err' | ||
// ] | ||
exception->Set(env->context(), env->openssl_error_stack(), error_stack) | ||
.FromJust(); | ||
} | ||
|
||
env->isolate()->ThrowException(exception); | ||
} | ||
|
||
|
||
|
@@ -4278,8 +4330,6 @@ SignBase::Error Verify::VerifyFinal(const char* key_pem, | |
if (!initialised_) | ||
return kSignNotInitialised; | ||
|
||
ClearErrorOnReturn clear_error_on_return; | ||
|
||
EVP_PKEY* pkey = nullptr; | ||
BIO* bp = nullptr; | ||
X509* x509 = nullptr; | ||
|
@@ -4368,6 +4418,8 @@ SignBase::Error Verify::VerifyFinal(const char* key_pem, | |
void Verify::VerifyFinal(const FunctionCallbackInfo<Value>& args) { | ||
Environment* env = Environment::GetCurrent(args); | ||
|
||
ClearErrorOnReturn clear_error_on_return; | ||
|
||
Verify* verify; | ||
ASSIGN_OR_RETURN_UNWRAP(&verify, args.Holder()); | ||
|
||
|
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This commit has the latest review comment fixes.
@bnoordhuis, i believe i've addressed all of your comments. Thanks for the review.