View | Details | Raw Unified | Return to bug 222485
Collapse All | Expand All

(-)./Makefile (-1 / +1 lines)
Lines 1-7 Link Here
1
# $FreeBSD: head/security/mbedtls/Makefile 444627 2017-06-29 08:25:23Z tijl $
1
# $FreeBSD: head/security/mbedtls/Makefile 444627 2017-06-29 08:25:23Z tijl $
2
2
3
PORTNAME=	mbedtls
3
PORTNAME=	mbedtls
4
PORTVERSION=	2.5.1
4
PORTVERSION=	2.6.0
5
DISTVERSIONSUFFIX=	-apache
5
DISTVERSIONSUFFIX=	-apache
6
CATEGORIES=	security devel
6
CATEGORIES=	security devel
7
MASTER_SITES=	https://tls.mbed.org/download/
7
MASTER_SITES=	https://tls.mbed.org/download/
(-)./distinfo (-3 / +3 lines)
Lines 1-3 Link Here
1
TIMESTAMP = 1498696001
1
TIMESTAMP = 1505971872
2
SHA256 (mbedtls-2.5.1-apache.tgz) = 559aeb8c8941262d6aad96a0286a230e7ff988ba53efbf609230ca1f81cc81f9
2
SHA256 (mbedtls-2.6.0-apache.tgz) = 99bc9d4212d3d885eeb96273bcde8ecc649a481404b8d7ea7bb26397c9909687
3
SIZE (mbedtls-2.5.1-apache.tgz) = 1955461
3
SIZE (mbedtls-2.6.0-apache.tgz) = 1973397
(-)./files/patch-config.h (-20 lines)
Lines 1-20 Link Here
1
--- include/mbedtls/config.h.orig	2017-03-08 15:33:04 UTC
2
+++ include/mbedtls/config.h
3
@@ -1285,7 +1285,7 @@
4
  *
5
  * Uncomment this to enable pthread mutexes.
6
  */
7
-//#define MBEDTLS_THREADING_PTHREAD
8
+#define MBEDTLS_THREADING_PTHREAD
9
 
10
 /**
11
  * \def MBEDTLS_VERSION_FEATURES
12
@@ -2322,7 +2322,7 @@
13
  *
14
  * Enable this layer to allow use of mutexes within mbed TLS
15
  */
16
-//#define MBEDTLS_THREADING_C
17
+#define MBEDTLS_THREADING_C
18
 
19
 /**
20
  * \def MBEDTLS_TIMING_C
(-)./files/patch-dtls-srtp (-659 lines)
Lines 1-659 Link Here
1
Add DTLS-SRTP (RFC 5764) support.
2
3
Obtained from:	git://git.linphone.org/linphone-desktop.git
4
5
diff --git a/include/mbedtls/config.h b/include/mbedtls/config.h
6
index d1db0d8..72e2d04 100644
7
--- include/mbedtls/config.h
8
+++ include/mbedtls/config.h
9
@@ -1154,6 +1154,17 @@
10
 #define MBEDTLS_SSL_DTLS_HELLO_VERIFY
11
 
12
 /**
13
+ * \def MBEDTLS_SSL_DTLS_SRTP
14
+ *
15
+ * Enable support for DTLS-SRTP, RFC5764
16
+ *
17
+ * Requires: MBEDTLS_SSL_PROTO_DTLS
18
+ *
19
+ * Comment this to disable support for DTLS-SRTP.
20
+ */
21
+#define MBEDTLS_SSL_DTLS_SRTP
22
+
23
+/**
24
  * \def MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE
25
  *
26
  * Enable server-side support for clients that reconnect from the same port.
27
diff --git a/include/mbedtls/ssl.h b/include/mbedtls/ssl.h
28
index 00e1c6c..729e95f 100644
29
--- include/mbedtls/ssl.h
30
+++ include/mbedtls/ssl.h
31
@@ -326,6 +326,8 @@
32
 
33
 #define MBEDTLS_TLS_EXT_SIG_ALG                     13
34
 
35
+#define MBEDTLS_TLS_EXT_USE_SRTP                    14
36
+
37
 #define MBEDTLS_TLS_EXT_ALPN                        16
38
 
39
 #define MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC            22 /* 0x16 */
40
@@ -338,6 +340,14 @@
41
 #define MBEDTLS_TLS_EXT_RENEGOTIATION_INFO      0xFF01
42
 
43
 /*
44
+ * use_srtp extension protection profiles values as defined in http://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml
45
+ */
46
+#define MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80_IANA_VALUE     0x0001
47
+#define MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32_IANA_VALUE     0x0002
48
+#define MBEDTLS_SRTP_NULL_HMAC_SHA1_80_IANA_VALUE          0x0005
49
+#define MBEDTLS_SRTP_NULL_HMAC_SHA1_32_IANA_VALUE          0x0006
50
+
51
+/*
52
  * Size defines
53
  */
54
 #if !defined(MBEDTLS_PSK_MAX_LEN)
55
@@ -426,6 +436,19 @@ typedef struct mbedtls_ssl_key_cert mbedtls_ssl_key_cert;
56
 typedef struct mbedtls_ssl_flight_item mbedtls_ssl_flight_item;
57
 #endif
58
 
59
+#if defined(MBEDTLS_SSL_DTLS_SRTP)
60
+/*
61
+ * List of SRTP profiles for DTLS-SRTP
62
+ */
63
+enum mbedtls_DTLS_SRTP_protection_profiles {
64
+    MBEDTLS_SRTP_UNSET_PROFILE,
65
+    MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80,
66
+    MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32,
67
+    MBEDTLS_SRTP_NULL_HMAC_SHA1_80,
68
+    MBEDTLS_SRTP_NULL_HMAC_SHA1_32,
69
+};
70
+#endif /* MBEDTLS_SSL_DTLS_SRTP */
71
+
72
 /*
73
  * This structure is used for storing current session data.
74
  */
75
@@ -566,6 +589,14 @@ struct mbedtls_ssl_config
76
     const char **alpn_list;         /*!< ordered list of protocols          */
77
 #endif
78
 
79
+#if defined(MBEDTLS_SSL_DTLS_SRTP)
80
+    /*
81
+     * use_srtp extension
82
+     */
83
+    enum mbedtls_DTLS_SRTP_protection_profiles *dtls_srtp_profiles_list; /*!< ordered list of supported srtp profile */
84
+    size_t dtls_srtp_profiles_list_len; /*!< number of supported profiles */
85
+#endif /* MBEDTLS_SSL_DTLS_SRTP */
86
+
87
     /*
88
      * Numerical settings (int then char)
89
      */
90
@@ -765,6 +796,15 @@ struct mbedtls_ssl_context
91
     const char *alpn_chosen;    /*!<  negotiated protocol                   */
92
 #endif
93
 
94
+#if defined(MBEDTLS_SSL_DTLS_SRTP)
95
+    /*
96
+     * use_srtp extension
97
+     */
98
+    enum mbedtls_DTLS_SRTP_protection_profiles chosen_dtls_srtp_profile; /*!< negotiated SRTP profile */
99
+    unsigned char *dtls_srtp_keys; /*<! master keys and master salt for SRTP generated during handshake */
100
+    size_t dtls_srtp_keys_len; /*<! length in bytes of master keys and master salt for SRTP generated during handshake */
101
+#endif /* MBEDTLS_SSL_DTLS_SRTP */
102
+
103
     /*
104
      * Information for DTLS hello verify
105
      */
106
@@ -1781,6 +1821,45 @@ int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **prot
107
 const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl );
108
 #endif /* MBEDTLS_SSL_ALPN */
109
 
110
+#if defined(MBEDTLS_SSL_DTLS_SRTP)
111
+/**
112
+ * \brief                   Set the supported DTLS-SRTP protection profiles.
113
+ *
114
+ * \param ssl               SSL configuration
115
+ * \param protos            List of supported protection profiles,
116
+ *                          in decreasing preference order.
117
+ * \param profiles_number   Number of supported profiles.
118
+ *
119
+ * \return         0 on success, or MBEDTLS_ERR_SSL_BAD_INPUT_DATA.
120
+ */
121
+int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf, const enum mbedtls_DTLS_SRTP_protection_profiles *profiles, size_t profiles_number);
122
+
123
+/**
124
+ * \brief          Get the negotiated DTLS-SRTP Protection Profile.
125
+ *                 This function should be called after the handshake is
126
+ *                 completed.
127
+ *
128
+ * \param ssl      SSL context
129
+ *
130
+ * \return         Protection Profile enum member, MBEDTLS_SRTP_UNSET_PROFILE if no protocol was negotiated.
131
+ */
132
+enum mbedtls_DTLS_SRTP_protection_profiles mbedtls_ssl_get_dtls_srtp_protection_profile( const mbedtls_ssl_context *ssl);
133
+
134
+/**
135
+ * \brief                  Get the generated DTLS-SRTP key material.
136
+ *                         This function should be called after the handshake is
137
+ *                         completed. It shall returns 80 bytes of key material generated according to RFC5764
138
+ *
139
+ * \param ssl              SSL context
140
+ * \param key              Buffer to hold the generated key material
141
+ * \param key_buffer_len   Length in bytes of the key buffer
142
+ * \param key_len          Actual length of data written in the key buffer
143
+ *
144
+ * \return         0 on succes, MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL if the key buffer is too small to hold the generated key
145
+ */
146
+int mbedtls_ssl_get_dtls_srtp_key_material( const mbedtls_ssl_context *ssl, unsigned char *key, const size_t key_buffer_len, size_t *key_len );
147
+#endif /* MBEDTLS_SSL_DTLS_SRTP */
148
+
149
 /**
150
  * \brief          Set the maximum supported version sent from the client side
151
  *                 and/or accepted at the server side
152
diff --git a/library/ssl_cli.c b/library/ssl_cli.c
153
index 4452169..91569ed 100644
154
--- library/ssl_cli.c
155
+++ library/ssl_cli.c
156
@@ -656,6 +656,79 @@ static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
157
 }
158
 #endif /* MBEDTLS_SSL_ALPN */
159
 
160
+#if defined (MBEDTLS_SSL_DTLS_SRTP)
161
+static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
162
+                                unsigned char *buf, size_t *olen )
163
+{
164
+    unsigned char *p = buf;
165
+    size_t protection_profiles_index = 0;
166
+
167
+    *olen = 0;
168
+
169
+    if( (ssl->conf->dtls_srtp_profiles_list == NULL)  || (ssl->conf->dtls_srtp_profiles_list_len == 0) )
170
+    {
171
+        return;
172
+    }
173
+
174
+    MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding use_srtp extension" ) );
175
+
176
+    *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP >> 8 ) & 0xFF );
177
+    *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP      ) & 0xFF );
178
+
179
+    /* RFC5764 section 4.1.1
180
+     * uint8 SRTPProtectionProfile[2];
181
+     *
182
+     * struct {
183
+     *   SRTPProtectionProfiles SRTPProtectionProfiles;
184
+     *   opaque srtp_mki<0..255>;
185
+     * } UseSRTPData;
186
+
187
+     * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
188
+     *
189
+     * Note: srtp_mki is not supported
190
+     */
191
+
192
+    /* Extension length = 2bytes for profiles lenght, ssl->conf->dtls_srtp_profiles_list_len*2 (each profile is 2 bytes length ) + 1 byte for the non implemented srtp_mki vector length (always 0) */
193
+    *p++ = (unsigned char)( ( ( 2 + 2*(ssl->conf->dtls_srtp_profiles_list_len) + 1 ) >> 8 ) & 0xFF );
194
+    *p++ = (unsigned char)( ( ( 2 + 2*(ssl->conf->dtls_srtp_profiles_list_len) + 1 )      ) & 0xFF );
195
+
196
+
197
+    /* protection profile length: 2*(ssl->conf->dtls_srtp_profiles_list_len) */
198
+    *p++ = (unsigned char)( ( ( 2*(ssl->conf->dtls_srtp_profiles_list_len) ) >> 8 ) & 0xFF );
199
+    *p++ = (unsigned char)( ( 2*(ssl->conf->dtls_srtp_profiles_list_len) ) & 0xFF );
200
+
201
+    for( protection_profiles_index=0; protection_profiles_index < ssl->conf->dtls_srtp_profiles_list_len; protection_profiles_index++ )
202
+    {
203
+        switch (ssl->conf->dtls_srtp_profiles_list[protection_profiles_index]) {
204
+            case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80:
205
+                *p++ = ( ( ( MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80_IANA_VALUE ) >> 8 ) & 0xFF);
206
+                *p++ = ( ( MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80_IANA_VALUE ) & 0xFF);
207
+                break;
208
+            case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32:
209
+                *p++ = ( ( ( MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32_IANA_VALUE ) >> 8 ) & 0xFF);
210
+                *p++ = ( ( MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32_IANA_VALUE ) & 0xFF);
211
+                break;
212
+            case MBEDTLS_SRTP_NULL_HMAC_SHA1_80:
213
+                *p++ = ( ( ( MBEDTLS_SRTP_NULL_HMAC_SHA1_80_IANA_VALUE ) >> 8 ) & 0xFF);
214
+                *p++ = ( ( MBEDTLS_SRTP_NULL_HMAC_SHA1_80_IANA_VALUE ) & 0xFF);
215
+                break;
216
+            case MBEDTLS_SRTP_NULL_HMAC_SHA1_32:
217
+                *p++ = ( ( ( MBEDTLS_SRTP_NULL_HMAC_SHA1_32_IANA_VALUE ) >> 8 ) & 0xFF);
218
+                *p++ = ( ( MBEDTLS_SRTP_NULL_HMAC_SHA1_32_IANA_VALUE ) & 0xFF);
219
+                break;
220
+            default:
221
+                /* Note: we shall never arrive here as protection profiles is checked by ssl_set_dtls_srtp_protection_profiles function */
222
+                MBEDTLS_SSL_DEBUG_MSG( 1, ( "client hello, ignore illegal DTLS-SRTP protection profile %d",  ssl->conf->dtls_srtp_profiles_list[protection_profiles_index]) );
223
+                break;
224
+        }
225
+    }
226
+
227
+    *p++ = 0x00;  /* non implemented srtp_mki vector length is always 0 */
228
+    /* 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)*/
229
+    *olen = 2 + 2 + 2 + 2*(ssl->conf->dtls_srtp_profiles_list_len) + 1;
230
+}
231
+#endif /* MBEDTLS_SSL_DTLS_SRTP */
232
+
233
 /*
234
  * Generate random bytes for ClientHello
235
  */
236
@@ -1006,6 +1079,11 @@ static int ssl_write_client_hello( mbedtls_ssl_context *ssl )
237
     ext_len += olen;
238
 #endif
239
 
240
+#if defined(MBEDTLS_SSL_DTLS_SRTP)
241
+    ssl_write_use_srtp_ext( ssl, p + 2 + ext_len, &olen );
242
+    ext_len += olen;
243
+#endif
244
+
245
 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
246
     ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
247
     ext_len += olen;
248
@@ -1308,6 +1386,81 @@ static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
249
 }
250
 #endif /* MBEDTLS_SSL_ALPN */
251
 
252
+#if defined(MBEDTLS_SSL_DTLS_SRTP)
253
+static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
254
+                               const unsigned char *buf, size_t len )
255
+{
256
+    enum mbedtls_DTLS_SRTP_protection_profiles server_protection = MBEDTLS_SRTP_UNSET_PROFILE;
257
+    size_t i;
258
+    uint16_t server_protection_profile_value = 0;
259
+
260
+    /* If use_srtp is not configured, just ignore the extension */
261
+    if( ( ssl->conf->dtls_srtp_profiles_list == NULL ) || ( ssl->conf->dtls_srtp_profiles_list_len == 0 ) )
262
+        return( 0 );
263
+
264
+    /* RFC5764 section 4.1.1
265
+     * uint8 SRTPProtectionProfile[2];
266
+     *
267
+     * struct {
268
+     *   SRTPProtectionProfiles SRTPProtectionProfiles;
269
+     *   opaque srtp_mki<0..255>;
270
+     * } UseSRTPData;
271
+
272
+     * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
273
+     *
274
+     * Note: srtp_mki is not supported
275
+     */
276
+
277
+    /* Length is 5 : one protection profile(2 bytes) + length(2 bytes) and potential srtp_mki which won't be parsed */
278
+    if( len < 5 )
279
+        return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
280
+
281
+    /*
282
+     * get the server protection profile
283
+     */
284
+    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 */
285
+        return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
286
+    } else {
287
+        server_protection_profile_value = buf[2]<<8 | buf[3];
288
+    }
289
+
290
+    /*
291
+     * Check we have the server profile in our list
292
+     */
293
+    for( i=0; i < ssl->conf->dtls_srtp_profiles_list_len; i++)
294
+    {
295
+        switch ( server_protection_profile_value ) {
296
+            case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80_IANA_VALUE:
297
+                server_protection = MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80;
298
+                break;
299
+            case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32_IANA_VALUE:
300
+                server_protection = MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32;
301
+                break;
302
+            case MBEDTLS_SRTP_NULL_HMAC_SHA1_80_IANA_VALUE:
303
+                server_protection = MBEDTLS_SRTP_NULL_HMAC_SHA1_80;
304
+                break;
305
+            case MBEDTLS_SRTP_NULL_HMAC_SHA1_32_IANA_VALUE:
306
+                server_protection = MBEDTLS_SRTP_NULL_HMAC_SHA1_32;
307
+                break;
308
+            default:
309
+                server_protection = MBEDTLS_SRTP_UNSET_PROFILE;
310
+                break;
311
+        }
312
+
313
+        if (server_protection == ssl->conf->dtls_srtp_profiles_list[i]) {
314
+            ssl->chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profiles_list[i];
315
+            return 0;
316
+        }
317
+    }
318
+
319
+    /* If we get there, no match was found : server problem, it shall never answer with incompatible profile */
320
+    ssl->chosen_dtls_srtp_profile = MBEDTLS_SRTP_UNSET_PROFILE;
321
+    mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
322
+                            MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
323
+    return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
324
+}
325
+#endif /* MBEDTLS_SSL_DTLS_SRTP */
326
+
327
 /*
328
  * Parse HelloVerifyRequest.  Only called after verifying the HS type.
329
  */
330
@@ -1786,6 +1939,16 @@ static int ssl_parse_server_hello( mbedtls_ssl_context *ssl )
331
             break;
332
 #endif /* MBEDTLS_SSL_ALPN */
333
 
334
+#if defined(MBEDTLS_SSL_DTLS_SRTP)
335
+        case MBEDTLS_TLS_EXT_USE_SRTP:
336
+            MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
337
+
338
+            if( ( ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size ) ) != 0 )
339
+                return( ret );
340
+
341
+            break;
342
+#endif /* MBEDTLS_SSL_DTLS_SRTP */
343
+
344
         default:
345
             MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
346
                            ext_id ) );
347
diff --git a/library/ssl_srv.c b/library/ssl_srv.c
348
index 6b5b461..72b50ab 100644
349
--- library/ssl_srv.c
350
+++ library/ssl_srv.c
351
@@ -573,6 +573,78 @@ static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
352
 }
353
 #endif /* MBEDTLS_SSL_ALPN */
354
 
355
+#if defined(MBEDTLS_SSL_DTLS_SRTP)
356
+static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
357
+                               const unsigned char *buf, size_t len )
358
+{
359
+    enum mbedtls_DTLS_SRTP_protection_profiles client_protection = MBEDTLS_SRTP_UNSET_PROFILE;
360
+    size_t i,j;
361
+    uint16_t profile_length;
362
+
363
+    /* If use_srtp is not configured, just ignore the extension */
364
+    if( ( ssl->conf->dtls_srtp_profiles_list == NULL ) || ( ssl->conf->dtls_srtp_profiles_list_len == 0 ) )
365
+        return( 0 );
366
+
367
+    /* RFC5764 section 4.1.1
368
+     * uint8 SRTPProtectionProfile[2];
369
+     *
370
+     * struct {
371
+     *   SRTPProtectionProfiles SRTPProtectionProfiles;
372
+     *   opaque srtp_mki<0..255>;
373
+     * } UseSRTPData;
374
+
375
+     * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
376
+     *
377
+     * Note: srtp_mki is not supported
378
+     */
379
+
380
+    /* Min length is 5 : at least one protection profile(2 bytes) and length(2 bytes) + srtp_mki length(1 byte) */
381
+    if( len < 5 )
382
+        return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
383
+
384
+    /*
385
+     * Use our order of preference
386
+     */
387
+    profile_length = buf[0]<<8|buf[1]; /* first 2 bytes are protection profile length(in bytes) */
388
+    for( i=0; i < ssl->conf->dtls_srtp_profiles_list_len; i++)
389
+    {
390
+        /* parse the extension list values are defined in http://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml */
391
+        for (j=0; j<profile_length; j+=2) { /* parse only the protection profile, srtp_mki is not supported and ignored */
392
+            uint16_t protection_profile_value = buf[j+2]<<8 | buf[j+3]; /* +2 to skip the length field */
393
+
394
+            switch ( protection_profile_value ) {
395
+                case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80_IANA_VALUE:
396
+                    client_protection = MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80;
397
+                    break;
398
+                case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32_IANA_VALUE:
399
+                    client_protection = MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32;
400
+                    break;
401
+                case MBEDTLS_SRTP_NULL_HMAC_SHA1_80_IANA_VALUE:
402
+                    client_protection = MBEDTLS_SRTP_NULL_HMAC_SHA1_80;
403
+                    break;
404
+                case MBEDTLS_SRTP_NULL_HMAC_SHA1_32_IANA_VALUE:
405
+                    client_protection = MBEDTLS_SRTP_NULL_HMAC_SHA1_32;
406
+                    break;
407
+                default:
408
+                    client_protection = MBEDTLS_SRTP_UNSET_PROFILE;
409
+                    break;
410
+            }
411
+
412
+            if (client_protection == ssl->conf->dtls_srtp_profiles_list[i]) {
413
+                ssl->chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profiles_list[i];
414
+                return 0;
415
+            }
416
+        }
417
+    }
418
+
419
+    /* If we get there, no match was found */
420
+    ssl->chosen_dtls_srtp_profile = MBEDTLS_SRTP_UNSET_PROFILE;
421
+    mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
422
+                            MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
423
+    return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
424
+}
425
+#endif /* MBEDTLS_SSL_DTLS_SRTP */
426
+
427
 /*
428
  * Auxiliary functions for ServerHello parsing and related actions
429
  */
430
@@ -1675,7 +1747,16 @@ read_record_header:
431
                 if( ret != 0 )
432
                     return( ret );
433
                 break;
434
-#endif /* MBEDTLS_SSL_SESSION_TICKETS */
435
+#endif /* MBEDTLS_SSL_ALPN */
436
+
437
+#if defined(MBEDTLS_SSL_DTLS_SRTP)
438
+            case MBEDTLS_TLS_EXT_USE_SRTP:
439
+                MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
440
+                ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size );
441
+                if ( ret != 0)
442
+                    return( ret );
443
+                break;
444
+#endif /* MBEDTLS_SSL_DTLS_SRTP */
445
 
446
             default:
447
                 MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
448
@@ -2144,6 +2225,57 @@ static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
449
 }
450
 #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
451
 
452
+#if defined(MBEDTLS_SSL_DTLS_SRTP )
453
+static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
454
+                                unsigned char *buf, size_t *olen )
455
+{
456
+    if( ssl->chosen_dtls_srtp_profile == MBEDTLS_SRTP_UNSET_PROFILE )
457
+    {
458
+        *olen = 0;
459
+        return;
460
+    }
461
+
462
+    MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding use_srtp extension" ) );
463
+
464
+    /* extension */
465
+    buf[0] = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP >> 8 ) & 0xFF );
466
+    buf[1] = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP      ) & 0xFF );
467
+    /* total length (5: only one profile(2 bytes) and length(2bytes) and srtp_mki not supported so zero length(1byte) ) */
468
+    buf[2] = 0x00;
469
+    buf[3] = 0x05;
470
+
471
+    /* protection profile length: 2 */
472
+    buf[4] = 0x00;
473
+    buf[5] = 0x02;
474
+    switch (ssl->chosen_dtls_srtp_profile) {
475
+        case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80:
476
+            buf[6] = (unsigned char)( ( MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80_IANA_VALUE >> 8) & 0xFF );
477
+            buf[7] = (unsigned char)( ( MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80_IANA_VALUE     ) & 0xFF );
478
+            break;
479
+        case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32:
480
+            buf[6] = (unsigned char)( ( MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32_IANA_VALUE >> 8) & 0xFF );
481
+            buf[7] = (unsigned char)( ( MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32_IANA_VALUE     ) & 0xFF );
482
+            break;
483
+        case MBEDTLS_SRTP_NULL_HMAC_SHA1_80:
484
+            buf[6] = (unsigned char)( ( MBEDTLS_SRTP_NULL_HMAC_SHA1_80_IANA_VALUE >> 8) & 0xFF );
485
+            buf[7] = (unsigned char)( ( MBEDTLS_SRTP_NULL_HMAC_SHA1_80_IANA_VALUE     ) & 0xFF );
486
+            break;
487
+        case MBEDTLS_SRTP_NULL_HMAC_SHA1_32:
488
+            buf[6] = (unsigned char)( ( MBEDTLS_SRTP_NULL_HMAC_SHA1_32_IANA_VALUE >> 8) & 0xFF );
489
+            buf[7] = (unsigned char)( ( MBEDTLS_SRTP_NULL_HMAC_SHA1_32_IANA_VALUE     ) & 0xFF );
490
+            break;
491
+        default:
492
+            *olen = 0;
493
+            return;
494
+            break;
495
+    }
496
+
497
+    buf[8] = 0x00; /* unsupported srtp_mki variable length vector set to 0 */
498
+
499
+    *olen = 9;
500
+}
501
+#endif /* MBEDTLS_SSL_DTLS_SRTP */
502
+
503
 #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
504
 static int ssl_write_hello_verify_request( mbedtls_ssl_context *ssl )
505
 {
506
@@ -2408,6 +2540,11 @@ static int ssl_write_server_hello( mbedtls_ssl_context *ssl )
507
     ext_len += olen;
508
 #endif
509
 
510
+#if defined(MBEDTLS_SSL_DTLS_SRTP)
511
+    ssl_write_use_srtp_ext( ssl, p + 2 + ext_len, &olen);
512
+    ext_len += olen;
513
+#endif
514
+
515
     MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
516
 
517
     if( ext_len > 0 )
518
diff --git a/library/ssl_tls.c b/library/ssl_tls.c
519
index 4424f56..940f8aa 100644
520
--- library/ssl_tls.c
521
+++ library/ssl_tls.c
522
@@ -373,7 +373,7 @@ static int tls_prf_generic( mbedtls_md_type_t md_type,
523
 {
524
     size_t nb;
525
     size_t i, j, k, md_len;
526
-    unsigned char tmp[128];
527
+    unsigned char tmp[144];
528
     unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
529
     const mbedtls_md_info_t *md_info;
530
     mbedtls_md_context_t md_ctx;
531
@@ -636,6 +636,30 @@ int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
532
     else
533
         MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
534
 
535
+#if defined(MBEDTLS_SSL_DTLS_SRTP)
536
+    /* check if we have a chosen srtp protection profile */
537
+    if (ssl->chosen_dtls_srtp_profile != MBEDTLS_SRTP_UNSET_PROFILE) {
538
+        /* derive key material for srtp session RFC5764 section 4.2 */
539
+        /* master key and master salt are respectively 128 bits and 112 bits for all currently available modes :
540
+         * SRTP_AES128_CM_HMAC_SHA1_80, SRTP_AES128_CM_HMAC_SHA1_32
541
+         * SRTP_NULL_HMAC_SHA1_80, SRTP_NULL_HMAC_SHA1_32
542
+         * So we must export 2*(128 + 112) = 480 bits
543
+         */
544
+        ssl->dtls_srtp_keys_len = 60;
545
+
546
+        ssl->dtls_srtp_keys = (unsigned char *)mbedtls_calloc(1, ssl->dtls_srtp_keys_len);
547
+
548
+        ret = handshake->tls_prf( session->master, 48, "EXTRACTOR-dtls_srtp",
549
+                        handshake->randbytes, 64, ssl->dtls_srtp_keys, ssl->dtls_srtp_keys_len );
550
+
551
+        if( ret != 0 )
552
+        {
553
+            MBEDTLS_SSL_DEBUG_RET( 1, "dtls srtp prf", ret );
554
+            return( ret );
555
+        }
556
+    }
557
+#endif /* MBEDTLS_SSL_DTLS_SRTP */
558
+
559
     /*
560
      * Swap the client and server random values.
561
      */
562
@@ -5408,6 +5432,12 @@ int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
563
         ssl->in_msg = ssl->in_buf + 13;
564
     }
565
 
566
+#if defined(MBEDTLS_SSL_DTLS_SRTP)
567
+    ssl->chosen_dtls_srtp_profile = MBEDTLS_SRTP_UNSET_PROFILE;
568
+    ssl->dtls_srtp_keys = NULL;
569
+    ssl->dtls_srtp_keys_len = 0;
570
+#endif
571
+
572
     if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
573
         return( ret );
574
 
575
@@ -5997,6 +6027,61 @@ const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
576
 }
577
 #endif /* MBEDTLS_SSL_ALPN */
578
 
579
+#if defined(MBEDTLS_SSL_DTLS_SRTP)
580
+int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf, const enum mbedtls_DTLS_SRTP_protection_profiles *profiles, size_t profiles_number)
581
+{
582
+    size_t i;
583
+    /* check in put validity : must be a list of profiles from enumeration */
584
+    /* maximum length is 4 as only 4 protection profiles are defined */
585
+    if (profiles_number>4) {
586
+            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
587
+    }
588
+
589
+    mbedtls_free(conf->dtls_srtp_profiles_list);
590
+    conf->dtls_srtp_profiles_list = (enum mbedtls_DTLS_SRTP_protection_profiles *)mbedtls_calloc(1, profiles_number*sizeof(enum mbedtls_DTLS_SRTP_protection_profiles));
591
+
592
+    for (i=0; i<profiles_number; i++) {
593
+        switch (profiles[i]) {
594
+            case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80:
595
+            case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32:
596
+            case MBEDTLS_SRTP_NULL_HMAC_SHA1_80:
597
+            case MBEDTLS_SRTP_NULL_HMAC_SHA1_32:
598
+                conf->dtls_srtp_profiles_list[i] = profiles[i];
599
+                break;
600
+            default:
601
+                mbedtls_free(conf->dtls_srtp_profiles_list);
602
+                conf->dtls_srtp_profiles_list = NULL;
603
+                conf->dtls_srtp_profiles_list_len = 0;
604
+                return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
605
+        }
606
+    }
607
+
608
+    /* assign array length */
609
+    conf->dtls_srtp_profiles_list_len = profiles_number;
610
+
611
+    return( 0 );
612
+}
613
+
614
+enum mbedtls_DTLS_SRTP_protection_profiles mbedtls_ssl_get_dtls_srtp_protection_profile( const mbedtls_ssl_context *ssl)
615
+{
616
+    return( ssl->chosen_dtls_srtp_profile);
617
+}
618
+
619
+int mbedtls_ssl_get_dtls_srtp_key_material( const mbedtls_ssl_context *ssl, unsigned char *key, const size_t key_buffer_len, size_t *key_len ) {
620
+    *key_len = 0;
621
+
622
+    /* check output buffer size */
623
+    if ( key_buffer_len < ssl->dtls_srtp_keys_len) {
624
+        return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
625
+    }
626
+
627
+    memcpy( key, ssl->dtls_srtp_keys, ssl->dtls_srtp_keys_len);
628
+    *key_len = ssl->dtls_srtp_keys_len;
629
+
630
+    return 0;
631
+}
632
+#endif /* MBEDTLS_SSL_DTLS_SRTP */
633
+
634
 void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
635
 {
636
     conf->max_major_ver = major;
637
@@ -7083,6 +7169,11 @@ void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
638
     mbedtls_free( ssl->cli_id );
639
 #endif
640
 
641
+#if defined (MBEDTLS_SSL_DTLS_SRTP)
642
+    mbedtls_zeroize( ssl->dtls_srtp_keys, ssl->dtls_srtp_keys_len );
643
+    mbedtls_free( ssl->dtls_srtp_keys );
644
+#endif /* MBEDTLS_SSL_DTLS_SRTP */
645
+
646
     MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
647
 
648
     /* Actually clear after last debug message */
649
@@ -7311,6 +7402,10 @@ void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
650
     ssl_key_cert_free( conf->key_cert );
651
 #endif
652
 
653
+#if defined (MBEDTLS_SSL_DTLS_SRTP)
654
+    mbedtls_free( conf->dtls_srtp_profiles_list );
655
+#endif
656
+
657
     mbedtls_zeroize( conf, sizeof( mbedtls_ssl_config ) );
658
 }
659
 
(-)./files/patch-include__mbedtls__config.h (+20 lines)
Line 0 Link Here
1
--- include/mbedtls/config.h.orig	2017-08-10 10:51:16 UTC
2
+++ include/mbedtls/config.h
3
@@ -1383,7 +1383,7 @@
4
  *
5
  * Uncomment this to enable pthread mutexes.
6
  */
7
-//#define MBEDTLS_THREADING_PTHREAD
8
+#define MBEDTLS_THREADING_PTHREAD
9
 
10
 /**
11
  * \def MBEDTLS_VERSION_FEATURES
12
@@ -2421,7 +2421,7 @@
13
  *
14
  * Enable this layer to allow use of mutexes within mbed TLS
15
  */
16
-//#define MBEDTLS_THREADING_C
17
+#define MBEDTLS_THREADING_C
18
 
19
 /**
20
  * \def MBEDTLS_TIMING_C
(-)./files/patch-library__ssl_cli.c (+193 lines)
Line 0 Link Here
1
--- library/ssl_cli.c.orig	2017-08-10 10:51:16 UTC
2
+++ library/ssl_cli.c
3
@@ -663,6 +663,79 @@ static void ssl_write_alpn_ext( mbedtls_
4
 }
5
 #endif /* MBEDTLS_SSL_ALPN */
6
 
7
+#if defined (MBEDTLS_SSL_DTLS_SRTP)
8
+static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
9
+                                unsigned char *buf, size_t *olen )
10
+{
11
+    unsigned char *p = buf;
12
+    size_t protection_profiles_index = 0;
13
+
14
+    *olen = 0;
15
+
16
+    if( (ssl->conf->dtls_srtp_profiles_list == NULL)  || (ssl->conf->dtls_srtp_profiles_list_len == 0) )
17
+    {
18
+        return;
19
+    }
20
+
21
+    MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding use_srtp extension" ) );
22
+
23
+    *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP >> 8 ) & 0xFF );
24
+    *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP      ) & 0xFF );
25
+
26
+    /* RFC5764 section 4.1.1
27
+     * uint8 SRTPProtectionProfile[2];
28
+     *
29
+     * struct {
30
+     *   SRTPProtectionProfiles SRTPProtectionProfiles;
31
+     *   opaque srtp_mki<0..255>;
32
+     * } UseSRTPData;
33
+
34
+     * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
35
+     *
36
+     * Note: srtp_mki is not supported
37
+     */
38
+
39
+    /* Extension length = 2bytes for profiles lenght, ssl->conf->dtls_srtp_profiles_list_len*2 (each profile is 2 bytes length ) + 1 byte for the non implemented srtp_mki vector length (always 0) */
40
+    *p++ = (unsigned char)( ( ( 2 + 2*(ssl->conf->dtls_srtp_profiles_list_len) + 1 ) >> 8 ) & 0xFF );
41
+    *p++ = (unsigned char)( ( ( 2 + 2*(ssl->conf->dtls_srtp_profiles_list_len) + 1 )      ) & 0xFF );
42
+
43
+
44
+    /* protection profile length: 2*(ssl->conf->dtls_srtp_profiles_list_len) */
45
+    *p++ = (unsigned char)( ( ( 2*(ssl->conf->dtls_srtp_profiles_list_len) ) >> 8 ) & 0xFF );
46
+    *p++ = (unsigned char)( ( 2*(ssl->conf->dtls_srtp_profiles_list_len) ) & 0xFF );
47
+
48
+    for( protection_profiles_index=0; protection_profiles_index < ssl->conf->dtls_srtp_profiles_list_len; protection_profiles_index++ )
49
+    {
50
+        switch (ssl->conf->dtls_srtp_profiles_list[protection_profiles_index]) {
51
+            case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80:
52
+                *p++ = ( ( ( MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80_IANA_VALUE ) >> 8 ) & 0xFF);
53
+                *p++ = ( ( MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80_IANA_VALUE ) & 0xFF);
54
+                break;
55
+            case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32:
56
+                *p++ = ( ( ( MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32_IANA_VALUE ) >> 8 ) & 0xFF);
57
+                *p++ = ( ( MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32_IANA_VALUE ) & 0xFF);
58
+                break;
59
+            case MBEDTLS_SRTP_NULL_HMAC_SHA1_80:
60
+                *p++ = ( ( ( MBEDTLS_SRTP_NULL_HMAC_SHA1_80_IANA_VALUE ) >> 8 ) & 0xFF);
61
+                *p++ = ( ( MBEDTLS_SRTP_NULL_HMAC_SHA1_80_IANA_VALUE ) & 0xFF);
62
+                break;
63
+            case MBEDTLS_SRTP_NULL_HMAC_SHA1_32:
64
+                *p++ = ( ( ( MBEDTLS_SRTP_NULL_HMAC_SHA1_32_IANA_VALUE ) >> 8 ) & 0xFF);
65
+                *p++ = ( ( MBEDTLS_SRTP_NULL_HMAC_SHA1_32_IANA_VALUE ) & 0xFF);
66
+                break;
67
+            default:
68
+                /* Note: we shall never arrive here as protection profiles is checked by ssl_set_dtls_srtp_protection_profiles function */
69
+                MBEDTLS_SSL_DEBUG_MSG( 1, ( "client hello, ignore illegal DTLS-SRTP protection profile %d",  ssl->conf->dtls_srtp_profiles_list[protection_profiles_index]) );
70
+                break;
71
+        }
72
+    }
73
+
74
+    *p++ = 0x00;  /* non implemented srtp_mki vector length is always 0 */
75
+    /* 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)*/
76
+    *olen = 2 + 2 + 2 + 2*(ssl->conf->dtls_srtp_profiles_list_len) + 1;
77
+}
78
+#endif /* MBEDTLS_SSL_DTLS_SRTP */
79
+
80
 /*
81
  * Generate random bytes for ClientHello
82
  */
83
@@ -1013,6 +1086,11 @@ static int ssl_write_client_hello( mbedt
84
     ext_len += olen;
85
 #endif
86
 
87
+#if defined(MBEDTLS_SSL_DTLS_SRTP)
88
+    ssl_write_use_srtp_ext( ssl, p + 2 + ext_len, &olen );
89
+    ext_len += olen;
90
+#endif
91
+
92
 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
93
     ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
94
     ext_len += olen;
95
@@ -1350,6 +1428,81 @@ static int ssl_parse_alpn_ext( mbedtls_s
96
 }
97
 #endif /* MBEDTLS_SSL_ALPN */
98
 
99
+#if defined(MBEDTLS_SSL_DTLS_SRTP)
100
+static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
101
+                               const unsigned char *buf, size_t len )
102
+{
103
+    enum mbedtls_DTLS_SRTP_protection_profiles server_protection = MBEDTLS_SRTP_UNSET_PROFILE;
104
+    size_t i;
105
+    uint16_t server_protection_profile_value = 0;
106
+
107
+    /* If use_srtp is not configured, just ignore the extension */
108
+    if( ( ssl->conf->dtls_srtp_profiles_list == NULL ) || ( ssl->conf->dtls_srtp_profiles_list_len == 0 ) )
109
+        return( 0 );
110
+
111
+    /* RFC5764 section 4.1.1
112
+     * uint8 SRTPProtectionProfile[2];
113
+     *
114
+     * struct {
115
+     *   SRTPProtectionProfiles SRTPProtectionProfiles;
116
+     *   opaque srtp_mki<0..255>;
117
+     * } UseSRTPData;
118
+
119
+     * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
120
+     *
121
+     * Note: srtp_mki is not supported
122
+     */
123
+
124
+    /* Length is 5 : one protection profile(2 bytes) + length(2 bytes) and potential srtp_mki which won't be parsed */
125
+    if( len < 5 )
126
+        return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
127
+
128
+    /*
129
+     * get the server protection profile
130
+     */
131
+    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 */
132
+        return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
133
+    } else {
134
+        server_protection_profile_value = buf[2]<<8 | buf[3];
135
+    }
136
+
137
+    /*
138
+     * Check we have the server profile in our list
139
+     */
140
+    for( i=0; i < ssl->conf->dtls_srtp_profiles_list_len; i++)
141
+    {
142
+        switch ( server_protection_profile_value ) {
143
+            case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80_IANA_VALUE:
144
+                server_protection = MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80;
145
+                break;
146
+            case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32_IANA_VALUE:
147
+                server_protection = MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32;
148
+                break;
149
+            case MBEDTLS_SRTP_NULL_HMAC_SHA1_80_IANA_VALUE:
150
+                server_protection = MBEDTLS_SRTP_NULL_HMAC_SHA1_80;
151
+                break;
152
+            case MBEDTLS_SRTP_NULL_HMAC_SHA1_32_IANA_VALUE:
153
+                server_protection = MBEDTLS_SRTP_NULL_HMAC_SHA1_32;
154
+                break;
155
+            default:
156
+                server_protection = MBEDTLS_SRTP_UNSET_PROFILE;
157
+                break;
158
+        }
159
+
160
+        if (server_protection == ssl->conf->dtls_srtp_profiles_list[i]) {
161
+            ssl->chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profiles_list[i];
162
+            return 0;
163
+        }
164
+    }
165
+
166
+    /* If we get there, no match was found : server problem, it shall never answer with incompatible profile */
167
+    ssl->chosen_dtls_srtp_profile = MBEDTLS_SRTP_UNSET_PROFILE;
168
+    mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
169
+                            MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
170
+    return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
171
+}
172
+#endif /* MBEDTLS_SSL_DTLS_SRTP */
173
+
174
 /*
175
  * Parse HelloVerifyRequest.  Only called after verifying the HS type.
176
  */
177
@@ -1864,6 +2017,16 @@ static int ssl_parse_server_hello( mbedt
178
             break;
179
 #endif /* MBEDTLS_SSL_ALPN */
180
 
181
+#if defined(MBEDTLS_SSL_DTLS_SRTP)
182
+        case MBEDTLS_TLS_EXT_USE_SRTP:
183
+            MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
184
+
185
+            if( ( ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size ) ) != 0 )
186
+                return( ret );
187
+
188
+            break;
189
+#endif /* MBEDTLS_SSL_DTLS_SRTP */
190
+
191
         default:
192
             MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
193
                            ext_id ) );
(-)./files/patch-library__ssl_srv.c (+169 lines)
Line 0 Link Here
1
--- library/ssl_srv.c.orig	2017-08-10 10:51:16 UTC
2
+++ library/ssl_srv.c
3
@@ -646,6 +646,78 @@ static int ssl_parse_alpn_ext( mbedtls_s
4
 }
5
 #endif /* MBEDTLS_SSL_ALPN */
6
 
7
+#if defined(MBEDTLS_SSL_DTLS_SRTP)
8
+static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
9
+                               const unsigned char *buf, size_t len )
10
+{
11
+    enum mbedtls_DTLS_SRTP_protection_profiles client_protection = MBEDTLS_SRTP_UNSET_PROFILE;
12
+    size_t i,j;
13
+    uint16_t profile_length;
14
+
15
+    /* If use_srtp is not configured, just ignore the extension */
16
+    if( ( ssl->conf->dtls_srtp_profiles_list == NULL ) || ( ssl->conf->dtls_srtp_profiles_list_len == 0 ) )
17
+        return( 0 );
18
+
19
+    /* RFC5764 section 4.1.1
20
+     * uint8 SRTPProtectionProfile[2];
21
+     *
22
+     * struct {
23
+     *   SRTPProtectionProfiles SRTPProtectionProfiles;
24
+     *   opaque srtp_mki<0..255>;
25
+     * } UseSRTPData;
26
+
27
+     * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
28
+     *
29
+     * Note: srtp_mki is not supported
30
+     */
31
+
32
+    /* Min length is 5 : at least one protection profile(2 bytes) and length(2 bytes) + srtp_mki length(1 byte) */
33
+    if( len < 5 )
34
+        return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
35
+
36
+    /*
37
+     * Use our order of preference
38
+     */
39
+    profile_length = buf[0]<<8|buf[1]; /* first 2 bytes are protection profile length(in bytes) */
40
+    for( i=0; i < ssl->conf->dtls_srtp_profiles_list_len; i++)
41
+    {
42
+        /* parse the extension list values are defined in http://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml */
43
+        for (j=0; j<profile_length; j+=2) { /* parse only the protection profile, srtp_mki is not supported and ignored */
44
+            uint16_t protection_profile_value = buf[j+2]<<8 | buf[j+3]; /* +2 to skip the length field */
45
+
46
+            switch ( protection_profile_value ) {
47
+                case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80_IANA_VALUE:
48
+                    client_protection = MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80;
49
+                    break;
50
+                case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32_IANA_VALUE:
51
+                    client_protection = MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32;
52
+                    break;
53
+                case MBEDTLS_SRTP_NULL_HMAC_SHA1_80_IANA_VALUE:
54
+                    client_protection = MBEDTLS_SRTP_NULL_HMAC_SHA1_80;
55
+                    break;
56
+                case MBEDTLS_SRTP_NULL_HMAC_SHA1_32_IANA_VALUE:
57
+                    client_protection = MBEDTLS_SRTP_NULL_HMAC_SHA1_32;
58
+                    break;
59
+                default:
60
+                    client_protection = MBEDTLS_SRTP_UNSET_PROFILE;
61
+                    break;
62
+            }
63
+
64
+            if (client_protection == ssl->conf->dtls_srtp_profiles_list[i]) {
65
+                ssl->chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profiles_list[i];
66
+                return 0;
67
+            }
68
+        }
69
+    }
70
+
71
+    /* If we get there, no match was found */
72
+    ssl->chosen_dtls_srtp_profile = MBEDTLS_SRTP_UNSET_PROFILE;
73
+    mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
74
+                            MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
75
+    return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
76
+}
77
+#endif /* MBEDTLS_SSL_DTLS_SRTP */
78
+
79
 /*
80
  * Auxiliary functions for ServerHello parsing and related actions
81
  */
82
@@ -1797,7 +1869,16 @@ read_record_header:
83
                 if( ret != 0 )
84
                     return( ret );
85
                 break;
86
-#endif /* MBEDTLS_SSL_SESSION_TICKETS */
87
+#endif /* MBEDTLS_SSL_ALPN */
88
+
89
+#if defined(MBEDTLS_SSL_DTLS_SRTP)
90
+            case MBEDTLS_TLS_EXT_USE_SRTP:
91
+                MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
92
+                ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size );
93
+                if ( ret != 0)
94
+                    return( ret );
95
+                break;
96
+#endif /* MBEDTLS_SSL_DTLS_SRTP */
97
 
98
             default:
99
                 MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
100
@@ -2309,6 +2390,57 @@ static void ssl_write_alpn_ext( mbedtls_
101
 }
102
 #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
103
 
104
+#if defined(MBEDTLS_SSL_DTLS_SRTP )
105
+static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
106
+                                unsigned char *buf, size_t *olen )
107
+{
108
+    if( ssl->chosen_dtls_srtp_profile == MBEDTLS_SRTP_UNSET_PROFILE )
109
+    {
110
+        *olen = 0;
111
+        return;
112
+    }
113
+
114
+    MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding use_srtp extension" ) );
115
+
116
+    /* extension */
117
+    buf[0] = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP >> 8 ) & 0xFF );
118
+    buf[1] = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP      ) & 0xFF );
119
+    /* total length (5: only one profile(2 bytes) and length(2bytes) and srtp_mki not supported so zero length(1byte) ) */
120
+    buf[2] = 0x00;
121
+    buf[3] = 0x05;
122
+
123
+    /* protection profile length: 2 */
124
+    buf[4] = 0x00;
125
+    buf[5] = 0x02;
126
+    switch (ssl->chosen_dtls_srtp_profile) {
127
+        case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80:
128
+            buf[6] = (unsigned char)( ( MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80_IANA_VALUE >> 8) & 0xFF );
129
+            buf[7] = (unsigned char)( ( MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80_IANA_VALUE     ) & 0xFF );
130
+            break;
131
+        case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32:
132
+            buf[6] = (unsigned char)( ( MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32_IANA_VALUE >> 8) & 0xFF );
133
+            buf[7] = (unsigned char)( ( MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32_IANA_VALUE     ) & 0xFF );
134
+            break;
135
+        case MBEDTLS_SRTP_NULL_HMAC_SHA1_80:
136
+            buf[6] = (unsigned char)( ( MBEDTLS_SRTP_NULL_HMAC_SHA1_80_IANA_VALUE >> 8) & 0xFF );
137
+            buf[7] = (unsigned char)( ( MBEDTLS_SRTP_NULL_HMAC_SHA1_80_IANA_VALUE     ) & 0xFF );
138
+            break;
139
+        case MBEDTLS_SRTP_NULL_HMAC_SHA1_32:
140
+            buf[6] = (unsigned char)( ( MBEDTLS_SRTP_NULL_HMAC_SHA1_32_IANA_VALUE >> 8) & 0xFF );
141
+            buf[7] = (unsigned char)( ( MBEDTLS_SRTP_NULL_HMAC_SHA1_32_IANA_VALUE     ) & 0xFF );
142
+            break;
143
+        default:
144
+            *olen = 0;
145
+            return;
146
+            break;
147
+    }
148
+
149
+    buf[8] = 0x00; /* unsupported srtp_mki variable length vector set to 0 */
150
+
151
+    *olen = 9;
152
+}
153
+#endif /* MBEDTLS_SSL_DTLS_SRTP */
154
+
155
 #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
156
 static int ssl_write_hello_verify_request( mbedtls_ssl_context *ssl )
157
 {
158
@@ -2579,6 +2711,11 @@ static int ssl_write_server_hello( mbedt
159
     ext_len += olen;
160
 #endif
161
 
162
+#if defined(MBEDTLS_SSL_DTLS_SRTP)
163
+    ssl_write_use_srtp_ext( ssl, p + 2 + ext_len, &olen);
164
+    ext_len += olen;
165
+#endif
166
+
167
     MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
168
 
169
     if( ext_len > 0 )
(-)./files/patch-library__ssl_tls.c (+140 lines)
Line 0 Link Here
1
--- library/ssl_tls.c.orig	2017-08-10 10:51:16 UTC
2
+++ library/ssl_tls.c
3
@@ -372,7 +372,7 @@ static int tls_prf_generic( mbedtls_md_t
4
 {
5
     size_t nb;
6
     size_t i, j, k, md_len;
7
-    unsigned char tmp[128];
8
+    unsigned char tmp[144];
9
     unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
10
     const mbedtls_md_info_t *md_info;
11
     mbedtls_md_context_t md_ctx;
12
@@ -635,6 +635,30 @@ int mbedtls_ssl_derive_keys( mbedtls_ssl
13
     else
14
         MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
15
 
16
+#if defined(MBEDTLS_SSL_DTLS_SRTP)
17
+    /* check if we have a chosen srtp protection profile */
18
+    if (ssl->chosen_dtls_srtp_profile != MBEDTLS_SRTP_UNSET_PROFILE) {
19
+        /* derive key material for srtp session RFC5764 section 4.2 */
20
+        /* master key and master salt are respectively 128 bits and 112 bits for all currently available modes :
21
+         * SRTP_AES128_CM_HMAC_SHA1_80, SRTP_AES128_CM_HMAC_SHA1_32
22
+         * SRTP_NULL_HMAC_SHA1_80, SRTP_NULL_HMAC_SHA1_32
23
+         * So we must export 2*(128 + 112) = 480 bits
24
+         */
25
+        ssl->dtls_srtp_keys_len = 60;
26
+
27
+        ssl->dtls_srtp_keys = (unsigned char *)mbedtls_calloc(1, ssl->dtls_srtp_keys_len);
28
+
29
+        ret = handshake->tls_prf( session->master, 48, "EXTRACTOR-dtls_srtp",
30
+                        handshake->randbytes, 64, ssl->dtls_srtp_keys, ssl->dtls_srtp_keys_len );
31
+
32
+        if( ret != 0 )
33
+        {
34
+            MBEDTLS_SSL_DEBUG_RET( 1, "dtls srtp prf", ret );
35
+            return( ret );
36
+        }
37
+    }
38
+#endif /* MBEDTLS_SSL_DTLS_SRTP */
39
+
40
     /*
41
      * Swap the client and server random values.
42
      */
43
@@ -5653,6 +5677,12 @@ int mbedtls_ssl_setup( mbedtls_ssl_conte
44
         ssl->in_msg = ssl->in_buf + 13;
45
     }
46
 
47
+#if defined(MBEDTLS_SSL_DTLS_SRTP)
48
+    ssl->chosen_dtls_srtp_profile = MBEDTLS_SRTP_UNSET_PROFILE;
49
+    ssl->dtls_srtp_keys = NULL;
50
+    ssl->dtls_srtp_keys_len = 0;
51
+#endif
52
+
53
     if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
54
         return( ret );
55
 
56
@@ -6244,6 +6274,61 @@ const char *mbedtls_ssl_get_alpn_protoco
57
 }
58
 #endif /* MBEDTLS_SSL_ALPN */
59
 
60
+#if defined(MBEDTLS_SSL_DTLS_SRTP)
61
+int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf, const enum mbedtls_DTLS_SRTP_protection_profiles *profiles, size_t profiles_number)
62
+{
63
+    size_t i;
64
+    /* check in put validity : must be a list of profiles from enumeration */
65
+    /* maximum length is 4 as only 4 protection profiles are defined */
66
+    if (profiles_number>4) {
67
+            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
68
+    }
69
+
70
+    mbedtls_free(conf->dtls_srtp_profiles_list);
71
+    conf->dtls_srtp_profiles_list = (enum mbedtls_DTLS_SRTP_protection_profiles *)mbedtls_calloc(1, profiles_number*sizeof(enum mbedtls_DTLS_SRTP_protection_profiles));
72
+
73
+    for (i=0; i<profiles_number; i++) {
74
+        switch (profiles[i]) {
75
+            case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80:
76
+            case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32:
77
+            case MBEDTLS_SRTP_NULL_HMAC_SHA1_80:
78
+            case MBEDTLS_SRTP_NULL_HMAC_SHA1_32:
79
+                conf->dtls_srtp_profiles_list[i] = profiles[i];
80
+                break;
81
+            default:
82
+                mbedtls_free(conf->dtls_srtp_profiles_list);
83
+                conf->dtls_srtp_profiles_list = NULL;
84
+                conf->dtls_srtp_profiles_list_len = 0;
85
+                return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
86
+        }
87
+    }
88
+
89
+    /* assign array length */
90
+    conf->dtls_srtp_profiles_list_len = profiles_number;
91
+
92
+    return( 0 );
93
+}
94
+
95
+enum mbedtls_DTLS_SRTP_protection_profiles mbedtls_ssl_get_dtls_srtp_protection_profile( const mbedtls_ssl_context *ssl)
96
+{
97
+    return( ssl->chosen_dtls_srtp_profile);
98
+}
99
+
100
+int mbedtls_ssl_get_dtls_srtp_key_material( const mbedtls_ssl_context *ssl, unsigned char *key, const size_t key_buffer_len, size_t *key_len ) {
101
+    *key_len = 0;
102
+
103
+    /* check output buffer size */
104
+    if ( key_buffer_len < ssl->dtls_srtp_keys_len) {
105
+        return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
106
+    }
107
+
108
+    memcpy( key, ssl->dtls_srtp_keys, ssl->dtls_srtp_keys_len);
109
+    *key_len = ssl->dtls_srtp_keys_len;
110
+
111
+    return 0;
112
+}
113
+#endif /* MBEDTLS_SSL_DTLS_SRTP */
114
+
115
 void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
116
 {
117
     conf->max_major_ver = major;
118
@@ -7406,6 +7491,11 @@ void mbedtls_ssl_free( mbedtls_ssl_conte
119
     mbedtls_free( ssl->cli_id );
120
 #endif
121
 
122
+#if defined (MBEDTLS_SSL_DTLS_SRTP)
123
+    mbedtls_zeroize( ssl->dtls_srtp_keys, ssl->dtls_srtp_keys_len );
124
+    mbedtls_free( ssl->dtls_srtp_keys );
125
+#endif /* MBEDTLS_SSL_DTLS_SRTP */
126
+
127
     MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
128
 
129
     /* Actually clear after last debug message */
130
@@ -7638,6 +7728,10 @@ void mbedtls_ssl_config_free( mbedtls_ss
131
     ssl_key_cert_free( conf->key_cert );
132
 #endif
133
 
134
+#if defined (MBEDTLS_SSL_DTLS_SRTP)
135
+    mbedtls_free( conf->dtls_srtp_profiles_list );
136
+#endif
137
+
138
     mbedtls_zeroize( conf, sizeof( mbedtls_ssl_config ) );
139
 }
140
 

Return to bug 222485