@@ -73,6 +73,20 @@ namespace crypto {
7373
7474namespace {
7575
76+ void InjectOpenSSLErrorForTesting (const FunctionCallbackInfo<Value>&) {
77+ #if NCRYPTO_USE_OPENSSL_PROVIDER
78+ ERR_raise (ERR_LIB_USER , ERR_R_INTERNAL_ERROR );
79+ #elif NCRYPTO_USE_BORINGSSL
80+ ERR_put_error (ERR_LIB_USER , 0 , ERR_R_INTERNAL_ERROR , __FILE__, __LINE__);
81+ #else
82+ #error "Unsupported crypto backend"
83+ #endif
84+ }
85+
86+ void HasOpenSSLErrorForTesting (const FunctionCallbackInfo<Value>& args) {
87+ args.GetReturnValue ().Set (ERR_peek_error () != 0 );
88+ }
89+
7690// Our custom implementation of the certificate verify callback
7791// used when establishing a TLS handshake. Because we cannot perform
7892// I/O quickly enough with X509_STORE_CTX_ APIs in this callback,
@@ -883,6 +897,7 @@ void TLSWrap::ClearOut() {
883897 for (;;) {
884898 {
885899 SSLLibraryCallScope ssl_library_call_scope (this );
900+ if (ERR_set_mark () != 0 ) ERR_clear_error ();
886901 read = SSL_read (ssl_.get (), out + length, kClearOutChunkSize - length);
887902 }
888903 Debug (this , " Read %d bytes of cleartext output" , read);
@@ -1016,6 +1031,7 @@ void TLSWrap::ClearIn() {
10161031 int written;
10171032 {
10181033 SSLLibraryCallScope ssl_library_call_scope (this );
1034+ if (ERR_set_mark () != 0 ) ERR_clear_error ();
10191035 written = SSL_write (ssl_.get (), bs->Data (), bs->ByteLength ());
10201036 }
10211037 Debug (this , " Writing %zu bytes, written = %d" , bs->ByteLength (), written);
@@ -1214,6 +1230,7 @@ int TLSWrap::DoWrite(WriteWrap* w,
12141230 NodeBIO::FromBIO (enc_out_)->set_allocate_tls_hint (length);
12151231 {
12161232 SSLLibraryCallScope ssl_library_call_scope (this );
1233+ if (ERR_set_mark () != 0 ) ERR_clear_error ();
12171234 written = SSL_write (ssl_.get (), bs->Data (), length);
12181235 }
12191236 } else {
@@ -1222,6 +1239,7 @@ int TLSWrap::DoWrite(WriteWrap* w,
12221239 NodeBIO::FromBIO (enc_out_)->set_allocate_tls_hint (buf->len );
12231240 {
12241241 SSLLibraryCallScope ssl_library_call_scope (this );
1242+ if (ERR_set_mark () != 0 ) ERR_clear_error ();
12251243 written = SSL_write (ssl_.get (), buf->base , buf->len );
12261244 }
12271245
@@ -2351,6 +2369,12 @@ void TLSWrap::Initialize(
23512369 Isolate* isolate = env->isolate ();
23522370
23532371 SetMethod (context, target, " wrap" , TLSWrap::Wrap);
2372+ SetMethod (context,
2373+ target,
2374+ " injectOpenSSLErrorForTesting" ,
2375+ InjectOpenSSLErrorForTesting);
2376+ SetMethodNoSideEffect (
2377+ context, target, " hasOpenSSLErrorForTesting" , HasOpenSSLErrorForTesting);
23542378
23552379 NODE_DEFINE_CONSTANT (target, HAVE_SSL_TRACE );
23562380
@@ -2445,6 +2469,8 @@ void TLSWrap::Initialize(
24452469void TLSWrap::RegisterExternalReferences (ExternalReferenceRegistry* registry) {
24462470 registry->Register (TLSWrap::Wrap);
24472471 registry->Register (GetWriteQueueSize);
2472+ registry->Register (InjectOpenSSLErrorForTesting);
2473+ registry->Register (HasOpenSSLErrorForTesting);
24482474
24492475 registry->Register (CertCbDone);
24502476 registry->Register (ClientHelloDone);
0 commit comments