@@ -122,10 +122,11 @@ public void run()
122122 int port ;
123123 Socket sock ;
124124
125- Object connectionSemaphore = new Object ();
125+ private final Object connectionSemaphore = new Object ();
126126
127127 boolean flagKexOngoing = false ;
128128 boolean connectionClosed = false ;
129+ boolean firstKexFinished = false ;
129130
130131 Throwable reasonClosedCause = null ;
131132
@@ -404,6 +405,8 @@ public void sendKexMessage(byte[] msg) throws IOException
404405 }
405406
406407 public void kexFinished () {
408+ firstKexFinished = true ;
409+
407410 synchronized (connectionSemaphore )
408411 {
409412 flagKexOngoing = false ;
@@ -419,11 +422,15 @@ public void forceKeyExchange(CryptoWishList cwl, DHGexParameters dhgex) throws I
419422 public void changeRecvCipher (BlockCipher bc , MAC mac )
420423 {
421424 tc .changeRecvCipher (bc , mac );
425+ if (km .isStrictKex ())
426+ tc .resetReceiveSequenceNumber ();
422427 }
423428
424429 public void changeSendCipher (BlockCipher bc , MAC mac )
425430 {
426431 tc .changeSendCipher (bc , mac );
432+ if (km .isStrictKex ())
433+ tc .resetSendSequenceNumber ();
427434 }
428435
429436 /**
@@ -531,38 +538,6 @@ public void receiveLoop() throws IOException
531538
532539 int type = msg [0 ] & 0xff ;
533540
534- if (type == Packets .SSH_MSG_IGNORE )
535- continue ;
536-
537- if (type == Packets .SSH_MSG_DEBUG )
538- {
539- if (log .isEnabled ())
540- {
541- TypesReader tr = new TypesReader (msg , 0 , msglen );
542- tr .readByte ();
543- tr .readBoolean ();
544- StringBuffer debugMessageBuffer = new StringBuffer ();
545- debugMessageBuffer .append (tr .readString ("UTF-8" ));
546-
547- for (int i = 0 ; i < debugMessageBuffer .length (); i ++)
548- {
549- char c = debugMessageBuffer .charAt (i );
550-
551- if ((c >= 32 ) && (c <= 126 ))
552- continue ;
553- debugMessageBuffer .setCharAt (i , '\uFFFD' );
554- }
555-
556- log .log (50 , "DEBUG Message from remote: '" + debugMessageBuffer .toString () + "'" );
557- }
558- continue ;
559- }
560-
561- if (type == Packets .SSH_MSG_UNIMPLEMENTED )
562- {
563- throw new IOException ("Peer sent UNIMPLEMENTED message, that should not happen." );
564- }
565-
566541 if (type == Packets .SSH_MSG_DISCONNECT )
567542 {
568543 TypesReader tr = new TypesReader (msg , 0 , msglen );
@@ -615,6 +590,46 @@ public void receiveLoop() throws IOException
615590 continue ;
616591 }
617592
593+ /*
594+ * Any other packet should not be used when kex-strict is enabled.
595+ */
596+ if (!firstKexFinished && km .isStrictKex ())
597+ {
598+ throw new IOException ("Unexpected packet received when kex-strict enabled" );
599+ }
600+
601+ if (type == Packets .SSH_MSG_IGNORE )
602+ continue ;
603+
604+ if (type == Packets .SSH_MSG_DEBUG )
605+ {
606+ if (log .isEnabled ())
607+ {
608+ TypesReader tr = new TypesReader (msg , 0 , msglen );
609+ tr .readByte ();
610+ tr .readBoolean ();
611+ StringBuffer debugMessageBuffer = new StringBuffer ();
612+ debugMessageBuffer .append (tr .readString ("UTF-8" ));
613+
614+ for (int i = 0 ; i < debugMessageBuffer .length (); i ++)
615+ {
616+ char c = debugMessageBuffer .charAt (i );
617+
618+ if ((c >= 32 ) && (c <= 126 ))
619+ continue ;
620+ debugMessageBuffer .setCharAt (i , '\uFFFD' );
621+ }
622+
623+ log .log (50 , "DEBUG Message from remote: '" + debugMessageBuffer .toString () + "'" );
624+ }
625+ continue ;
626+ }
627+
628+ if (type == Packets .SSH_MSG_UNIMPLEMENTED )
629+ {
630+ throw new IOException ("Peer sent UNIMPLEMENTED message, that should not happen." );
631+ }
632+
618633 if (type == Packets .SSH_MSG_USERAUTH_SUCCESS ) {
619634 tc .startCompression ();
620635 }
0 commit comments