-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Crypto add opensslerror stack #15518
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
Changes from 1 commit
f2c9e49
dc2165a
5c24f07
d1296cf
2b177cb
bbaea8b
a5a2f64
2ad7440
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -272,32 +272,47 @@ void ThrowCryptoError(Environment* env, | |
| Local<Value> exception_v = Exception::Error(message); | ||
| CHECK(!exception_v.IsEmpty()); | ||
| Local<Object> exception = exception_v.As<Object>(); | ||
| Local<Array> error_stack = Array::New(env->isolate()); | ||
|
|
||
| ERR_STATE *es = ERR_get_state(); | ||
| // Build the error_stack array to be added to openSSLErrorStack property. | ||
| for (unsigned int i = 0; es->bottom != es->top | ||
| && (es->err_flags[es->top] & ERR_FLAG_MARK) == 0; i++) { | ||
| 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 tmpStr[256] = { }; | ||
| ERR_error_string_n(err_buf, tmpStr, sizeof(tmpStr)); | ||
| error_stack->Set(env->context(), i, | ||
| String::NewFromUtf8(env->isolate(), tmpStr, | ||
| v8::NewStringType::kNormal) | ||
| .ToLocalChecked()).FromJust(); | ||
| ERR_STATE* es = ERR_get_state(); | ||
|
|
||
| if (es->bottom != es->top) { | ||
| Local<Array> error_stack = Array::New(env->isolate()); | ||
| int top = es->top; | ||
|
|
||
|
Contributor
Author
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; | ||
| } | ||
|
Contributor
Author
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. handle the ring buffer with modular arithmetic |
||
| es->top -= 1; | ||
|
|
||
| // 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(); | ||
| } | ||
|
|
||
| // 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 error' | ||
| // ] | ||
| exception->Set(env->openssl_error_stack(), error_stack); | ||
| env->isolate()->ThrowException(exception); | ||
| } | ||
|
|
||
|
|
@@ -4315,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; | ||
|
|
@@ -4326,8 +4339,6 @@ SignBase::Error Verify::VerifyFinal(const char* key_pem, | |
| int r = 0; | ||
| EVP_PKEY_CTX* pkctx = nullptr; | ||
|
|
||
| ERR_set_mark(); | ||
|
|
||
| bp = BIO_new_mem_buf(const_cast<char*>(key_pem), key_pem_len); | ||
|
Contributor
Author
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. this is not needed |
||
| if (bp == nullptr) | ||
| goto exit; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -235,13 +235,9 @@ assert.throws(function() { | |
| ].join('\n'); | ||
| crypto.createSign('SHA256').update('test').sign(priv); | ||
| }, (err) => { | ||
| // Throws crypto error, so there is an openSSLErrorStack property. | ||
| // The openSSL stack is empty. | ||
| if ((err instanceof Error) && | ||
| /digest too big for rsa key$/.test(err) && | ||
| err.openSSLErrorStack !== undefined && | ||
| Array.isArray(err.openSSLErrorStack) && | ||
| err.openSSLErrorStack.length === 0) { | ||
| err.openSSLErrorStack === undefined) { | ||
| return true; | ||
|
Contributor
Author
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. update test since the openSSLErrorStack is not always added |
||
| } | ||
| }); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.