@@ -73,6 +73,20 @@ namespace crypto {
7373
7474namespace {
7575
76+ void InjectOpenSSLErrorForTesting (const FunctionCallbackInfo<Value>&) {
77+ #if NCRYPTO_USE_OPENSSL3_PROVIDER
78+ ERR_raise (ERR_LIB_USER , ERR_R_INTERNAL_ERROR );
79+ #elif NCRYPTO_USE_BORINGSSL || NCRYPTO_USE_LEGACY_OPENSSL
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,
@@ -902,6 +916,7 @@ void TLSWrap::ClearOut() {
902916 for (;;) {
903917 {
904918 SSLLibraryCallScope ssl_library_call_scope (this );
919+ if (ERR_set_mark () != 0 ) ERR_clear_error ();
905920 read = SSL_read (ssl_.get (), out + length, kClearOutChunkSize - length);
906921 }
907922 Debug (this , " Read %d bytes of cleartext output" , read);
@@ -1033,6 +1048,7 @@ void TLSWrap::ClearIn() {
10331048 int written;
10341049 {
10351050 SSLLibraryCallScope ssl_library_call_scope (this );
1051+ if (ERR_set_mark () != 0 ) ERR_clear_error ();
10361052 written = SSL_write (ssl_.get (), bs->Data (), bs->ByteLength ());
10371053 }
10381054 Debug (this , " Writing %zu bytes, written = %d" , bs->ByteLength (), written);
@@ -1235,6 +1251,7 @@ int TLSWrap::DoWrite(WriteWrap* w,
12351251 NodeBIO::FromBIO (enc_out_)->set_allocate_tls_hint (length);
12361252 {
12371253 SSLLibraryCallScope ssl_library_call_scope (this );
1254+ if (ERR_set_mark () != 0 ) ERR_clear_error ();
12381255 written = SSL_write (ssl_.get (), bs->Data (), length);
12391256 }
12401257 } else {
@@ -1243,6 +1260,7 @@ int TLSWrap::DoWrite(WriteWrap* w,
12431260 NodeBIO::FromBIO (enc_out_)->set_allocate_tls_hint (buf->len );
12441261 {
12451262 SSLLibraryCallScope ssl_library_call_scope (this );
1263+ if (ERR_set_mark () != 0 ) ERR_clear_error ();
12461264 written = SSL_write (ssl_.get (), buf->base , buf->len );
12471265 }
12481266
@@ -2402,6 +2420,12 @@ void TLSWrap::Initialize(
24022420 Isolate* isolate = env->isolate ();
24032421
24042422 SetMethod (context, target, " wrap" , TLSWrap::Wrap);
2423+ SetMethod (context,
2424+ target,
2425+ " injectOpenSSLErrorForTesting" ,
2426+ InjectOpenSSLErrorForTesting);
2427+ SetMethodNoSideEffect (
2428+ context, target, " hasOpenSSLErrorForTesting" , HasOpenSSLErrorForTesting);
24052429
24062430 NODE_DEFINE_CONSTANT (target, HAVE_SSL_TRACE );
24072431
@@ -2496,6 +2520,8 @@ void TLSWrap::Initialize(
24962520void TLSWrap::RegisterExternalReferences (ExternalReferenceRegistry* registry) {
24972521 registry->Register (TLSWrap::Wrap);
24982522 registry->Register (GetWriteQueueSize);
2523+ registry->Register (InjectOpenSSLErrorForTesting);
2524+ registry->Register (HasOpenSSLErrorForTesting);
24992525
25002526 registry->Register (CertCbDone);
25012527 registry->Register (ClientHelloDone);
0 commit comments