support mki value

Add support mki value in the DTLS-SRTP

Signed-off-by: Johan Pascal <johan.pascal@belledonne-communications.com>
diff --git a/library/ssl_cli.c b/library/ssl_cli.c
index 17cd482..8b9f2f0 100644
--- a/library/ssl_cli.c
+++ b/library/ssl_cli.c
@@ -762,6 +762,7 @@
 {
     unsigned char *p = buf;
     size_t protection_profiles_index = 0;
+    size_t mki_len = 0, i;
 
     *olen = 0;
 
@@ -785,12 +786,15 @@
 
      * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
      *
-     * Note: srtp_mki is not supported
      */
-
-    /* Extension length = 2bytes for profiles lenght, ssl->conf->dtls_srtp_profile_list_len*2 (each profile is 2 bytes length ) + 1 byte for the non implemented srtp_mki vector length (always 0) */
-    *p++ = (unsigned char)( ( ( 2 + 2*(ssl->conf->dtls_srtp_profile_list_len) + 1 ) >> 8 ) & 0xFF );
-    *p++ = (unsigned char)( ( ( 2 + 2*(ssl->conf->dtls_srtp_profile_list_len) + 1 )      ) & 0xFF );
+    if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED &&
+                ssl->dtls_srtp_info.mki_len != 0 )
+    {
+        mki_len = ssl->dtls_srtp_info.mki_len;
+    }
+    /* Extension length = 2bytes for profiles length, ssl->conf->dtls_srtp_profile_list_len*2 (each profile is 2 bytes length ) + 1 byte for srtp_mki vector length and the mki_len value */
+    *p++ = (unsigned char)( ( ( 2 + 2*(ssl->conf->dtls_srtp_profile_list_len) + 1 + mki_len ) >> 8 ) & 0xFF );
+    *p++ = (unsigned char)( ( ( 2 + 2*(ssl->conf->dtls_srtp_profile_list_len) + 1 + mki_len )      ) & 0xFF );
 
 
     /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
@@ -831,9 +835,18 @@
         }
     }
 
-    *p++ = 0x00;  /* non implemented srtp_mki vector length is always 0 */
+    *p++ = mki_len & 0xFF;
+
+    if( mki_len != 0 )
+    {
+        for( i=0; i < mki_len; i++ )
+        {
+            *p++ = ssl->dtls_srtp_info.mki_value[i];
+        }
+    }
+
     /* total extension length: extension type (2 bytes) + extension length (2 bytes) + protection profile length (2 bytes) + 2*nb protection profiles + srtp_mki vector length(1 byte)*/
-    *olen = 2 + 2 + 2 + 2*(ssl->conf->dtls_srtp_profile_list_len) + 1;
+    *olen = 2 + 2 + 2 + 2*( ssl->conf->dtls_srtp_profile_list_len ) + 1 + mki_len;
 }
 #endif /* MBEDTLS_SSL_DTLS_SRTP */
 
@@ -1804,7 +1817,7 @@
                                const unsigned char *buf, size_t len )
 {
     mbedtls_ssl_srtp_profile server_protection = MBEDTLS_SRTP_UNSET_PROFILE;
-    size_t i;
+    size_t i, mki_len = 0;
     uint16_t server_protection_profile_value = 0;
 
     /* If use_srtp is not configured, just ignore the extension */
@@ -1821,22 +1834,28 @@
 
      * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
      *
-     * Note: srtp_mki is not supported
      */
+    if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED &&
+                ssl->dtls_srtp_info.mki_len != 0 )
+    {
+        mki_len = ssl->dtls_srtp_info.mki_len;
+    }
 
-    /* Length is 5 : one protection profile(2 bytes) + length(2 bytes) and potential srtp_mki which won't be parsed */
-    if( len < 5 )
+    /* Length is 5 and optional mki_value : one protection profile(2 bytes) + length(2 bytes) and srtp_mki */
+    if( ( len != 5 ) && ( len != ( 5 + mki_len ) ) )
         return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
 
     /*
      * get the server protection profile
      */
-    if (((uint16_t)(buf[0]<<8 | buf[1])) != 0x0002) { /* protection profile length must be 0x0002 as we must have only one protection profile in server Hello */
+    if (((uint16_t)( ( buf[0]<<8 ) | buf[1] ) ) != 0x0002) { /* protection profile length must be 0x0002 as we must have only one protection profile in server Hello */
         return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
     } else {
-        server_protection_profile_value = buf[2]<<8 | buf[3];
+        server_protection_profile_value = ( buf[2]<<8 ) | buf[3];
     }
 
+    ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_SRTP_UNSET_PROFILE;
+
     /*
      * Check we have the server profile in our list
      */
@@ -1862,15 +1881,30 @@
 
         if (server_protection == ssl->conf->dtls_srtp_profile_list[i]) {
             ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
-            return 0;
+            break;
         }
     }
 
-    /* If we get there, no match was found : server problem, it shall never answer with incompatible profile */
-    ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_SRTP_UNSET_PROFILE;
-    mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
-                            MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
-    return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
+    /* If no match was found : server problem, it shall never answer with incompatible profile */
+    if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_SRTP_UNSET_PROFILE )
+    {
+        mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
+                                         MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
+        return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
+    }
+    /* RFC5764:
+     *  If the client detects a nonzero-length MKI in the server's response
+     *  that is different than the one the client offered, then the client
+     *  MUST abort the handshake and SHOULD send an invalid_parameter alert.
+     */
+    if( len > 5  &&
+        ( memcmp( ssl->dtls_srtp_info.mki_value, &buf[5], mki_len ) ) )
+    {
+        mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
+                                        MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
+        return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
+    }
+    return 0;
 }
 #endif /* MBEDTLS_SSL_DTLS_SRTP */
 
diff --git a/library/ssl_srv.c b/library/ssl_srv.c
index 68afcba..f5229ef 100644
--- a/library/ssl_srv.c
+++ b/library/ssl_srv.c
@@ -782,7 +782,7 @@
 {
     mbedtls_ssl_srtp_profile client_protection = MBEDTLS_SRTP_UNSET_PROFILE;
     size_t i,j;
-    uint16_t profile_length;
+    size_t profile_length;
 
     /* If use_srtp is not configured, just ignore the extension */
     if( ( ssl->conf->dtls_srtp_profile_list == NULL ) || ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
@@ -798,53 +798,71 @@
 
      * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
      *
-     * Note: srtp_mki is not supported
      */
 
     /* Min length is 5 : at least one protection profile(2 bytes) and length(2 bytes) + srtp_mki length(1 byte) */
     if( len < 5 )
         return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
 
-    /*
-     * Use our order of preference
-     */
-    profile_length = buf[0]<<8|buf[1]; /* first 2 bytes are protection profile length(in bytes) */
-    for( i=0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
+   ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_SRTP_UNSET_PROFILE;
+
+    profile_length = ( buf[0]<<8 ) | buf[1]; /* first 2 bytes are protection profile length(in bytes) */
+
+
+    /* parse the extension list values are defined in http://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml */
+    for( j=0; j < profile_length; j+=2 )
     {
-        /* parse the extension list values are defined in http://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml */
-        for (j=0; j<profile_length; j+=2) { /* parse only the protection profile, srtp_mki is not supported and ignored */
-            uint16_t protection_profile_value = buf[j+2]<<8 | buf[j+3]; /* +2 to skip the length field */
+        uint16_t protection_profile_value = buf[j+2]<<8 | buf[j+3]; /* +2 to skip the length field */
 
-            switch ( protection_profile_value ) {
-                case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80_IANA_VALUE:
-                    client_protection = MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80;
-                    break;
-                case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32_IANA_VALUE:
-                    client_protection = MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32;
-                    break;
-                case MBEDTLS_SRTP_NULL_HMAC_SHA1_80_IANA_VALUE:
-                    client_protection = MBEDTLS_SRTP_NULL_HMAC_SHA1_80;
-                    break;
-                case MBEDTLS_SRTP_NULL_HMAC_SHA1_32_IANA_VALUE:
-                    client_protection = MBEDTLS_SRTP_NULL_HMAC_SHA1_32;
-                    break;
-                default:
-                    client_protection = MBEDTLS_SRTP_UNSET_PROFILE;
-                    break;
-            }
-
-            if (client_protection == ssl->conf->dtls_srtp_profile_list[i]) {
+        switch ( protection_profile_value )
+        {
+            case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80_IANA_VALUE:
+                client_protection = MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80;
+                break;
+            case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32_IANA_VALUE:
+                client_protection = MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32;
+                break;
+            case MBEDTLS_SRTP_NULL_HMAC_SHA1_80_IANA_VALUE:
+                client_protection = MBEDTLS_SRTP_NULL_HMAC_SHA1_80;
+                break;
+            case MBEDTLS_SRTP_NULL_HMAC_SHA1_32_IANA_VALUE:
+                client_protection = MBEDTLS_SRTP_NULL_HMAC_SHA1_32;
+                break;
+            default:
+                client_protection = MBEDTLS_SRTP_UNSET_PROFILE;
+                break;
+        }
+        /* check if suggested profile is in our list */
+        for( i=0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
+        {
+            if( client_protection == ssl->conf->dtls_srtp_profile_list[i] )
+            {
                 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
-                return 0;
+                break;
             }
         }
+        if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile != MBEDTLS_SRTP_UNSET_PROFILE )
+            break;
+    }
+    if( ( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED ) &&
+          ( len > ( profile_length + 2 ) ) )
+    {
+        ssl->dtls_srtp_info.mki_len = buf[ profile_length + 2 ];
+        if( ssl->dtls_srtp_info.mki_len > MBEDTLS_DTLS_SRTP_MAX_MKI_LENGTH )
+        {
+            mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
+                                            MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
+            ssl->dtls_srtp_info.mki_len = 0;
+            return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
+        }
+
+        for( i=0; i < ssl->dtls_srtp_info.mki_len; i++ )
+        {
+            ssl->dtls_srtp_info.mki_value[i] = buf[ profile_length + 2 + i ];
+        }
     }
 
-    /* If we get there, no match was found */
-    ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_SRTP_UNSET_PROFILE;
-  //  mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
-  //                          MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
-    return( 0 );
+     return( 0 );
 }
 #endif /* MBEDTLS_SSL_DTLS_SRTP */
 
@@ -2585,6 +2603,8 @@
 static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
                                 unsigned char *buf, size_t *olen )
 {
+    size_t mki_len = 0, ext_len = 0, i;
+
     if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_SRTP_UNSET_PROFILE )
     {
         *olen = 0;
@@ -2593,12 +2613,19 @@
 
     MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding use_srtp extension" ) );
 
+    if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED &&
+                ssl->dtls_srtp_info.mki_len != 0 )
+    {
+        mki_len = ssl->dtls_srtp_info.mki_len;
+    }
+
     /* extension */
     buf[0] = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP >> 8 ) & 0xFF );
     buf[1] = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP      ) & 0xFF );
-    /* total length (5: only one profile(2 bytes) and length(2bytes) and srtp_mki not supported so zero length(1byte) ) */
-    buf[2] = 0x00;
-    buf[3] = 0x05;
+    /* total length 5 and mki value: only one profile(2 bytes) and length(2 bytes) and srtp_mki  ) */
+    ext_len = 5 + mki_len;
+    buf[2] = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
+    buf[3] = (unsigned char)( ext_len & 0xFF );
 
     /* protection profile length: 2 */
     buf[4] = 0x00;
@@ -2625,9 +2652,13 @@
             return;
     }
 
-    buf[8] = 0x00; /* unsupported srtp_mki variable length vector set to 0 */
+    buf[8] = mki_len & 0xFF;
+    for( i=0; i < mki_len; i++ )
+    {
+        buf[ 9 + i ] = ssl->dtls_srtp_info.mki_value[i];
+    }
 
-    *olen = 9;
+    *olen = 9 + mki_len;
 }
 #endif /* MBEDTLS_SSL_DTLS_SRTP */
 
diff --git a/library/ssl_tls.c b/library/ssl_tls.c
index 48ddd9a..ecdd4f6 100644
--- a/library/ssl_tls.c
+++ b/library/ssl_tls.c
@@ -4714,6 +4714,24 @@
 #endif /* MBEDTLS_SSL_ALPN */
 
 #if defined(MBEDTLS_SSL_DTLS_SRTP)
+void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf, int support_mki_value )
+{
+    conf->dtls_srtp_mki_support = support_mki_value;
+}
+
+int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl, unsigned char* mki_value, size_t mki_len )
+{
+    if ( mki_len > MBEDTLS_DTLS_SRTP_MAX_MKI_LENGTH )
+        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
+
+    if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
+        return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
+
+    memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
+    ssl->dtls_srtp_info.mki_len = mki_len;
+    return 0;
+}
+
 int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf, const mbedtls_ssl_srtp_profile *profiles, size_t profiles_number)
 {
     size_t i;