@@ -356,13 +356,12 @@ internal static SafeSslHandle AllocateSslHandle(SafeFreeSslCredentials credentia
356356
357357 internal static SecurityStatusPal SslRenegotiate ( SafeSslHandle sslContext , out byte [ ] ? outputBuffer )
358358 {
359- int ret = Interop . Ssl . SslRenegotiate ( sslContext ) ;
359+ int ret = Interop . Ssl . SslRenegotiate ( sslContext , out Ssl . SslErrorCode errorCode ) ;
360360
361361 outputBuffer = Array . Empty < byte > ( ) ;
362362 if ( ret != 1 )
363363 {
364- GetSslError ( sslContext , ret , out Exception ? exception ) ;
365- return new SecurityStatusPal ( SecurityStatusPalErrorCode . InternalError , exception ) ;
364+ return new SecurityStatusPal ( SecurityStatusPalErrorCode . InternalError , GetSslError ( ret , errorCode ) ) ;
366365 }
367366 return new SecurityStatusPal ( SecurityStatusPalErrorCode . OK ) ;
368367 }
@@ -382,23 +381,21 @@ internal static SecurityStatusPalErrorCode DoSslHandshake(SafeSslHandle context,
382381 }
383382 }
384383
385- int retVal = Ssl . SslDoHandshake ( context ) ;
384+ int retVal = Ssl . SslDoHandshake ( context , out Ssl . SslErrorCode errorCode ) ;
386385 if ( retVal != 1 )
387386 {
388- Exception ? innerError ;
389- Ssl . SslErrorCode error = GetSslError ( context , retVal , out innerError ) ;
390-
391- if ( error == Ssl . SslErrorCode . SSL_ERROR_WANT_X509_LOOKUP )
387+ if ( errorCode == Ssl . SslErrorCode . SSL_ERROR_WANT_X509_LOOKUP )
392388 {
393389 return SecurityStatusPalErrorCode . CredentialsNeeded ;
394390 }
395391
396- if ( ( retVal != - 1 ) || ( error != Ssl . SslErrorCode . SSL_ERROR_WANT_READ ) )
392+ if ( ( retVal != - 1 ) || ( errorCode != Ssl . SslErrorCode . SSL_ERROR_WANT_READ ) )
397393 {
394+ Exception ? innerError = GetSslError ( retVal , errorCode ) ;
395+
398396 // Handshake failed, but even if the handshake does not need to read, there may be an Alert going out.
399397 // To handle that we will fall-through the block below to pull it out, and we will fail after.
400- handshakeException = new SslException ( SR . Format ( SR . net_ssl_handshake_failed_error , error ) , innerError ) ;
401- Crypto . ErrClearError ( ) ;
398+ handshakeException = new SslException ( SR . Format ( SR . net_ssl_handshake_failed_error , errorCode ) , innerError ) ;
402399 }
403400 }
404401
@@ -447,17 +444,7 @@ internal static int Encrypt(SafeSslHandle context, ReadOnlySpan<byte> input, ref
447444 ulong assertNoError = Crypto . ErrPeekError ( ) ;
448445 Debug . Assert ( assertNoError == 0 , $ "OpenSsl error queue is not empty, run: 'openssl errstr { assertNoError : X} ' for original error.") ;
449446#endif
450- errorCode = Ssl . SslErrorCode . SSL_ERROR_NONE ;
451-
452- int retVal ;
453- Exception ? innerError = null ;
454-
455- retVal = Ssl . SslWrite ( context , ref MemoryMarshal . GetReference ( input ) , input . Length ) ;
456-
457- if ( retVal != input . Length )
458- {
459- errorCode = GetSslError ( context , retVal , out innerError ) ;
460- }
447+ int retVal = Ssl . SslWrite ( context , ref MemoryMarshal . GetReference ( input ) , input . Length , out errorCode ) ;
461448
462449 if ( retVal != input . Length )
463450 {
@@ -471,7 +458,7 @@ internal static int Encrypt(SafeSslHandle context, ReadOnlySpan<byte> input, ref
471458 break ;
472459
473460 default :
474- throw new SslException ( SR . Format ( SR . net_ssl_encrypt_failed , errorCode ) , innerError ) ;
461+ throw new SslException ( SR . Format ( SR . net_ssl_encrypt_failed , errorCode ) , GetSslError ( retVal , errorCode ) ) ;
475462 }
476463 }
477464 else
@@ -501,17 +488,14 @@ internal static int Decrypt(SafeSslHandle context, Span<byte> buffer, out Ssl.Ss
501488 ulong assertNoError = Crypto . ErrPeekError ( ) ;
502489 Debug . Assert ( assertNoError == 0 , $ "OpenSsl error queue is not empty, run: 'openssl errstr { assertNoError : X} ' for original error.") ;
503490#endif
504- errorCode = Ssl . SslErrorCode . SSL_ERROR_NONE ;
505-
506491 BioWrite ( context . InputBio ! , buffer ) ;
507492
508- int retVal = Ssl . SslRead ( context , ref MemoryMarshal . GetReference ( buffer ) , buffer . Length ) ;
493+ int retVal = Ssl . SslRead ( context , ref MemoryMarshal . GetReference ( buffer ) , buffer . Length , out errorCode ) ;
509494 if ( retVal > 0 )
510495 {
511496 return retVal ;
512497 }
513498
514- errorCode = GetSslError ( context , retVal , out Exception ? innerError ) ;
515499 switch ( errorCode )
516500 {
517501 // indicate end-of-file
@@ -526,7 +510,7 @@ internal static int Decrypt(SafeSslHandle context, Span<byte> buffer, out Ssl.Ss
526510 break ;
527511
528512 default :
529- throw new SslException ( SR . Format ( SR . net_ssl_decrypt_failed , errorCode ) , innerError ) ;
513+ throw new SslException ( SR . Format ( SR . net_ssl_decrypt_failed , errorCode ) , GetSslError ( retVal , errorCode ) ) ;
530514 }
531515
532516 return 0 ;
@@ -647,14 +631,13 @@ private static void BioWrite(SafeBioHandle bio, ReadOnlySpan<byte> buffer)
647631 }
648632 }
649633
650- private static Ssl . SslErrorCode GetSslError ( SafeSslHandle context , int result , out Exception ? innerError )
634+ private static Exception ? GetSslError ( int result , Ssl . SslErrorCode retVal )
651635 {
652- ErrorInfo lastErrno = Sys . GetLastErrorInfo ( ) ; // cache it before we make more P/Invoke calls, just in case we need it
653-
654- Ssl . SslErrorCode retVal = Ssl . SslGetError ( context , result ) ;
636+ Exception ? innerError ;
655637 switch ( retVal )
656638 {
657639 case Ssl . SslErrorCode . SSL_ERROR_SYSCALL :
640+ ErrorInfo lastErrno = Sys . GetLastErrorInfo ( ) ;
658641 // Some I/O error occurred
659642 innerError =
660643 Crypto . ErrPeekError ( ) != 0 ? Crypto . CreateOpenSslCryptographicException ( ) : // crypto error queue not empty
@@ -673,7 +656,8 @@ private static Ssl.SslErrorCode GetSslError(SafeSslHandle context, int result, o
673656 innerError = null ;
674657 break ;
675658 }
676- return retVal ;
659+
660+ return innerError ;
677661 }
678662
679663 private static void SetSslCertificate ( SafeSslContextHandle contextPtr , SafeX509Handle certPtr , SafeEvpPKeyHandle keyPtr )
0 commit comments