Forbid sequence number wrapping
diff --git a/library/ssl_tls.c b/library/ssl_tls.c
index d6e9dd3..c42eccc 100644
--- a/library/ssl_tls.c
+++ b/library/ssl_tls.c
@@ -1191,6 +1191,13 @@
if( ++ssl->out_ctr[i - 1] != 0 )
break;
+ /* The loops goes to its end iff the counter is wrapping */
+ if( i == 0 )
+ {
+ SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
+ return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
+ }
+
SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
return( 0 );
@@ -1589,6 +1596,13 @@
if( ++ssl->in_ctr[i - 1] != 0 )
break;
+ /* The loops goes to its end iff the counter is wrapping */
+ if( i == 0 )
+ {
+ SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
+ return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
+ }
+
SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
return( 0 );