@@ -613,22 +613,37 @@ public static Stream CreateOutboundCommandStream(Socket connectedSocket, string
613613 {
614614 if ( cert == null )
615615 {
616+ Log ( "### TLS validation: No certificate presented" ) ;
616617 return false ;
617618 }
618619
620+ var presented = new X509Certificate2 ( cert ) ;
621+ string presentedThumbprint = presented . Thumbprint ? . Replace ( " " , "" , StringComparison . OrdinalIgnoreCase ) ?? "" ;
622+
619623 if ( ! string . IsNullOrWhiteSpace ( pinnedThumbprint ) )
620624 {
621- var presented = new X509Certificate2 ( cert ) ;
622- string presentedThumbprint = presented . Thumbprint ? . Replace ( " " , "" , StringComparison . OrdinalIgnoreCase ) ?? "" ;
623625 string expected = pinnedThumbprint . Replace ( " " , "" , StringComparison . OrdinalIgnoreCase ) ;
624- return string . Equals ( presentedThumbprint , expected , StringComparison . OrdinalIgnoreCase ) ;
626+ bool matches = string . Equals ( presentedThumbprint , expected , StringComparison . OrdinalIgnoreCase ) ;
627+ Log ( $ "^^^ TLS pinned validation: expected={ expected } , presented={ presentedThumbprint } , match={ matches } ") ;
628+ return matches ;
625629 }
626630
631+ Log ( $ "^^^ TLS validation: sslPolicyErrors={ sslPolicyErrors } , subject={ presented . Subject } , thumbprint={ presentedThumbprint } ") ;
627632 return sslPolicyErrors == SslPolicyErrors . None ;
628633 } ;
629634
630635 var sslStream = new SslStream ( networkStream , leaveInnerStreamOpen : false , validator ) ;
631- sslStream . AuthenticateAsClient ( serverHost ) ;
636+ try
637+ {
638+ Log ( $ "^^^ TLS handshake starting with serverHost='{ serverHost } '") ;
639+ sslStream . AuthenticateAsClient ( serverHost ) ;
640+ Log ( $ "^^^ TLS handshake completed successfully") ;
641+ }
642+ catch ( Exception e )
643+ {
644+ Log ( $ "### TLS handshake failed: { e . GetType ( ) } : { e . Message } ") ;
645+ throw ;
646+ }
632647 return sslStream ;
633648 }
634649
@@ -815,32 +830,44 @@ public static bool TrySendRemoteCommandTcpWithAck(int port, string command, stri
815830 clientSocket . Connect ( serverHost , port , TimeSpan . FromSeconds ( TcpConnectionTimeoutSeconds ) ) ;
816831 if ( ! clientSocket . Connected )
817832 {
818- Log ( $ "### Socket not connected when trying to send '{ command } ', closing connection") ;
833+ Log ( $ "### Socket not connected after Connect() when trying to send '{ command } ', closing connection") ;
819834 return false ;
820835 }
821836
822- using Stream stream = CreateOutboundCommandStream ( clientSocket , serverHost ) ;
823- WriteRemoteCommand ( stream , command , includePassPhrase : true ) ;
824-
825- // Wait for server ACK to confirm delivery.
826- string ? responseRaw = ReadSingleLineResponse ( stream ) ;
827- string response = TrimPassPhrasePrefix ( ( responseRaw ?? "" ) . Trim ( ) ) ;
828- Log ( $ "^^^ TCP responseRaw='{ responseRaw } ', response(after trim)='{ response } '") ;
829- if ( string . Equals ( response , "ack" , StringComparison . OrdinalIgnoreCase ) )
837+ Stream stream ;
838+ try
830839 {
831- Log ( $ "^^^ TCP ACK received from { serverHost } :{ port } ") ;
832- return true ;
840+ stream = CreateOutboundCommandStream ( clientSocket , serverHost ) ;
833841 }
834-
835- if ( string . Equals ( response , "lol" , StringComparison . OrdinalIgnoreCase ) )
842+ catch ( Exception tlsEx )
836843 {
837- Log ( "### Remote returned 'lol' (likely bad passphrase)" ) ;
844+ Log ( $ "### Failed to create outbound stream (likely TLS issue): { tlsEx . GetType ( ) } : { tlsEx . Message } ") ;
845+ return false ;
838846 }
839- else
847+
848+ using ( stream )
840849 {
841- Log ( $ "### No ACK received (response='{ responseRaw ?? "" } ')") ;
842- }
850+ WriteRemoteCommand ( stream , command , includePassPhrase : true ) ;
851+
852+ // Wait for server ACK to confirm delivery.
853+ string ? responseRaw = ReadSingleLineResponse ( stream ) ;
854+ string response = TrimPassPhrasePrefix ( ( responseRaw ?? "" ) . Trim ( ) ) ;
855+ Log ( $ "^^^ TCP responseRaw='{ responseRaw } ', response(after trim)='{ response } '") ;
856+ if ( string . Equals ( response , "ack" , StringComparison . OrdinalIgnoreCase ) )
857+ {
858+ Log ( $ "^^^ TCP ACK received from { serverHost } :{ port } ") ;
859+ return true ;
860+ }
843861
862+ if ( string . Equals ( response , "lol" , StringComparison . OrdinalIgnoreCase ) )
863+ {
864+ Log ( "### Remote returned 'lol' (likely bad passphrase)" ) ;
865+ }
866+ else
867+ {
868+ Log ( $ "### No ACK received (response='{ responseRaw ?? "" } ')") ;
869+ }
870+ }
844871 try
845872 {
846873 clientSocket . Shutdown ( SocketShutdown . Both ) ;
0 commit comments