Merge remote-tracking branch 'upstream-public/pr/1449' into mbedtls-2.7-proposed
diff --git a/ChangeLog b/ChangeLog
index 2bc462f..b7428b1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -21,6 +21,10 @@
      overflow. #1179
    * Fix memory allocation corner cases in memory_buffer_alloc.c module. Found
      by Guido Vranken. #639
+   * Fix a possible arithmetic overflow in ssl_parse_server_key_exchange()
+     that could cause a key exchange to fail on valid data.
+   * Fix a possible arithmetic overflow in ssl_parse_server_psk_hint() that
+     could cause a key exchange to fail on valid data.
 
 Changes
    * Clarify the documentation of mbedtls_ssl_setup.
@@ -108,6 +112,8 @@
      manner.
    * Fix a buffer overread in ssl_parse_server_key_exchange() that could cause
      a crash on invalid input.
+   * Fix a buffer overread in ssl_parse_server_psk_hint() that could cause a
+     crash on invalid input.
 
 Features
    * Allow comments in test data files.
@@ -242,8 +248,6 @@
    * In mbedtls_entropy_free(), properly free the message digest context.
    * Fix status handshake status message in programs/ssl/dtls_client.c. Found
      and fixed by muddog.
-   * Fix a possible arithmetic overflow in ssl_parse_server_key_exchange()
-     that could cause a key exchange to fail on valid data.
 
 Changes
    * Extend cert_write example program by options to set the certificate version
diff --git a/library/ssl_cli.c b/library/ssl_cli.c
index 2eec766..eeb2fe2 100644
--- a/library/ssl_cli.c
+++ b/library/ssl_cli.c
@@ -2057,10 +2057,16 @@
      *
      * opaque psk_identity_hint<0..2^16-1>;
      */
+    if( (*p) > end - 2 )
+    {
+        MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message "
+                                    "(psk_identity_hint length)" ) );
+        return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
+    }
     len = (*p)[0] << 8 | (*p)[1];
     *p += 2;
 
-    if( (*p) + len > end )
+    if( (*p) > end - len )
     {
         MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message "
                                     "(psk_identity_hint length)" ) );