Skip to content

Commit 7844116

Browse files
committed
crypto: don't reach into OpenSSL internals for ThrowCryptoError
There is a perfectly serviceable ERR_get_error function which avoids having to sniff through the OpenSSL ring buffer like that. It does return the errors in the opposite order, but that's easily fixed with std::reverse. Note this behavior is slightly different in that an ERR_get_error loop will ultimately clear the error queue, but this is desireable. Leaving the error queue uncleared means errors in subsequent operations may get mixed up and cause issues.
1 parent 3a977fc commit 7844116

File tree

1 file changed

+27
-36
lines changed

1 file changed

+27
-36
lines changed

src/node_crypto.cc

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@
4242
// StartComAndWoSignData.inc
4343
#include "StartComAndWoSignData.inc"
4444

45+
#include <algorithm>
4546
#include <errno.h>
4647
#include <limits.h> // INT_MAX
4748
#include <math.h>
4849
#include <stdlib.h>
4950
#include <string.h>
51+
#include <vector>
5052

5153
#define THROW_AND_RETURN_IF_NOT_BUFFER(val, prefix) \
5254
do { \
@@ -262,44 +264,33 @@ void ThrowCryptoError(Environment* env,
262264
Local<Value> exception_v = Exception::Error(message);
263265
CHECK(!exception_v.IsEmpty());
264266
Local<Object> exception = exception_v.As<Object>();
265-
ERR_STATE* es = ERR_get_state();
266-
267-
if (es->bottom != es->top) {
268-
Local<Array> error_stack = Array::New(env->isolate());
269-
int top = es->top;
270-
271-
// Build the error_stack array to be added to opensslErrorStack property.
272-
for (unsigned int i = 0; es->bottom != es->top;) {
273-
unsigned long err_buf = es->err_buffer[es->top]; // NOLINT(runtime/int)
274-
// Only add error string if there is valid err_buffer.
275-
if (err_buf) {
276-
char tmp_str[256];
277-
ERR_error_string_n(err_buf, tmp_str, sizeof(tmp_str));
278-
error_stack->Set(env->context(), i,
279-
String::NewFromUtf8(env->isolate(), tmp_str,
280-
v8::NewStringType::kNormal)
281-
.ToLocalChecked()).FromJust();
282-
// Only increment if we added to error_stack.
283-
i++;
284-
}
285267

286-
// Since the ERR_STATE is a ring buffer, we need to use modular
287-
// arithmetic to loop back around in the case where bottom is after top.
288-
// Using ERR_NUM_ERRORS macro defined in openssl.
289-
es->top = (((es->top - 1) % ERR_NUM_ERRORS) + ERR_NUM_ERRORS) %
290-
ERR_NUM_ERRORS;
268+
std::vector<Local<String>> errors;
269+
for (;;) {
270+
unsigned long err = ERR_get_error(); // NOLINT(runtime/int)
271+
if (err == 0) {
272+
break;
291273
}
292-
293-
// Restore top.
294-
es->top = top;
295-
296-
// Add the opensslErrorStack property to the exception object.
297-
// The new property will look like the following:
298-
// opensslErrorStack: [
299-
// 'error:0906700D:PEM routines:PEM_ASN1_read_bio:ASN1 lib',
300-
// 'error:0D07803A:asn1 encoding routines:ASN1_ITEM_EX_D2I:nested asn1 err'
301-
// ]
302-
exception->Set(env->context(), env->openssl_error_stack(), error_stack)
274+
char tmp_str[256];
275+
ERR_error_string_n(err, tmp_str, sizeof(tmp_str));
276+
errors.push_back(String::NewFromUtf8(env->isolate(), tmp_str,
277+
v8::NewStringType::kNormal)
278+
.ToLocalChecked());
279+
}
280+
281+
// ERR_get_error returns errors in order of most specific to least
282+
// specific. We wish to have the reverse ordering:
283+
// opensslErrorStack: [
284+
// 'error:0906700D:PEM routines:PEM_ASN1_read_bio:ASN1 lib',
285+
// 'error:0D07803A:asn1 encoding routines:ASN1_ITEM_EX_D2I:nested asn1 err'
286+
// ]
287+
if (!errors.empty()) {
288+
std::reverse(errors.begin(), errors.end());
289+
Local<Array> errors_array = Array::New(env->isolate(), errors.size());
290+
for (size_t i = 0; i < errors.size(); i++) {
291+
errors_array->Set(env->context(), i, errors[i]).FromJust();
292+
}
293+
exception->Set(env->context(), env->openssl_error_stack(), errors_array)
303294
.FromJust();
304295
}
305296

0 commit comments

Comments
 (0)