Removed
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_platform_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_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); |
658 |
} |
659 |
|