/src/gnutls/lib/ext/srtp.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright (C) 2012 Free Software Foundation  | 
3  |  |  *   | 
4  |  |  * Author: Martin Storsjo  | 
5  |  |  *  | 
6  |  |  * This file is part of GnuTLS.  | 
7  |  |  *  | 
8  |  |  * The GnuTLS is free software; you can redistribute it and/or  | 
9  |  |  * modify it under the terms of the GNU Lesser General Public License  | 
10  |  |  * as published by the Free Software Foundation; either version 2.1 of  | 
11  |  |  * the License, or (at your option) any later version.  | 
12  |  |  *  | 
13  |  |  * This library is distributed in the hope that it will be useful, but  | 
14  |  |  * WITHOUT ANY WARRANTY; without even the implied warranty of  | 
15  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  | 
16  |  |  * Lesser General Public License for more details.  | 
17  |  |  *  | 
18  |  |  * You should have received a copy of the GNU Lesser General Public License  | 
19  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>  | 
20  |  |  *  | 
21  |  |  */  | 
22  |  |  | 
23  |  | #include "gnutls_int.h"  | 
24  |  | #include "auth.h"  | 
25  |  | #include "errors.h"  | 
26  |  | #include "num.h"  | 
27  |  | #include "ext/srtp.h"  | 
28  |  |  | 
29  |  | static int _gnutls_srtp_recv_params(gnutls_session_t session,  | 
30  |  |             const uint8_t *data, size_t data_size);  | 
31  |  | static int _gnutls_srtp_send_params(gnutls_session_t session,  | 
32  |  |             gnutls_buffer_st *extdata);  | 
33  |  |  | 
34  |  | static int _gnutls_srtp_unpack(gnutls_buffer_st *ps,  | 
35  |  |              gnutls_ext_priv_data_t *_priv);  | 
36  |  | static int _gnutls_srtp_pack(gnutls_ext_priv_data_t _priv,  | 
37  |  |            gnutls_buffer_st *ps);  | 
38  |  | static void _gnutls_srtp_deinit_data(gnutls_ext_priv_data_t priv);  | 
39  |  |  | 
40  |  | const hello_ext_entry_st ext_mod_srtp = { | 
41  |  |   .name = "SRTP",  | 
42  |  |   .tls_id = 14,  | 
43  |  |   .gid = GNUTLS_EXTENSION_SRTP,  | 
44  |  |   .validity = GNUTLS_EXT_FLAG_TLS | GNUTLS_EXT_FLAG_DTLS |  | 
45  |  |         GNUTLS_EXT_FLAG_CLIENT_HELLO | GNUTLS_EXT_FLAG_EE |  | 
46  |  |         GNUTLS_EXT_FLAG_TLS12_SERVER_HELLO,  | 
47  |  |   .client_parse_point = GNUTLS_EXT_APPLICATION,  | 
48  |  |   .server_parse_point = GNUTLS_EXT_APPLICATION,  | 
49  |  |   .recv_func = _gnutls_srtp_recv_params,  | 
50  |  |   .send_func = _gnutls_srtp_send_params,  | 
51  |  |   .pack_func = _gnutls_srtp_pack,  | 
52  |  |   .unpack_func = _gnutls_srtp_unpack,  | 
53  |  |   .deinit_func = _gnutls_srtp_deinit_data,  | 
54  |  |   .cannot_be_overriden = 1  | 
55  |  | };  | 
56  |  |  | 
57  |  | typedef struct { | 
58  |  |   const char *name;  | 
59  |  |   gnutls_srtp_profile_t id;  | 
60  |  |   unsigned int key_length;  | 
61  |  |   unsigned int salt_length;  | 
62  |  | } srtp_profile_st;  | 
63  |  |  | 
64  |  | static const srtp_profile_st profile_names[] = { | 
65  |  |   { "SRTP_AES128_CM_HMAC_SHA1_80", GNUTLS_SRTP_AES128_CM_HMAC_SHA1_80, 16, | 
66  |  |     14 },  | 
67  |  |   { "SRTP_AES128_CM_HMAC_SHA1_32", GNUTLS_SRTP_AES128_CM_HMAC_SHA1_32, 16, | 
68  |  |     14 },  | 
69  |  |   { "SRTP_NULL_HMAC_SHA1_80", GNUTLS_SRTP_NULL_HMAC_SHA1_80, 16, 14 }, | 
70  |  |   { "SRTP_NULL_SHA1_32", GNUTLS_SRTP_NULL_HMAC_SHA1_32, 16, 14 }, | 
71  |  |   { "SRTP_AEAD_AES_128_GCM", GNUTLS_SRTP_AEAD_AES_128_GCM, 16, 12 }, | 
72  |  |   { "SRTP_AEAD_AES_256_GCM", GNUTLS_SRTP_AEAD_AES_256_GCM, 32, 12 }, | 
73  |  |   { NULL, 0, 0, 0 } | 
74  |  | };  | 
75  |  |  | 
76  |  | static const srtp_profile_st *get_profile(gnutls_srtp_profile_t profile)  | 
77  | 0  | { | 
78  | 0  |   const srtp_profile_st *p = profile_names;  | 
79  | 0  |   while (p->name != NULL) { | 
80  | 0  |     if (p->id == profile)  | 
81  | 0  |       return p;  | 
82  | 0  |     p++;  | 
83  | 0  |   }  | 
84  | 0  |   return NULL;  | 
85  | 0  | }  | 
86  |  |  | 
87  |  | static gnutls_srtp_profile_t find_profile(const char *str, const char *end)  | 
88  | 0  | { | 
89  | 0  |   const srtp_profile_st *prof = profile_names;  | 
90  | 0  |   unsigned int len;  | 
91  | 0  |   if (end != NULL) { | 
92  | 0  |     len = end - str;  | 
93  | 0  |   } else { | 
94  | 0  |     len = strlen(str);  | 
95  | 0  |   }  | 
96  |  | 
  | 
97  | 0  |   while (prof->name != NULL) { | 
98  | 0  |     if (strlen(prof->name) == len &&  | 
99  | 0  |         !strncmp(str, prof->name, len)) { | 
100  | 0  |       return prof->id;  | 
101  | 0  |     }  | 
102  | 0  |     prof++;  | 
103  | 0  |   }  | 
104  | 0  |   return 0;  | 
105  | 0  | }  | 
106  |  |  | 
107  |  | /**  | 
108  |  |  * gnutls_srtp_get_profile_id  | 
109  |  |  * @name: The name of the profile to look up  | 
110  |  |  * @profile: Will hold the profile id  | 
111  |  |  *  | 
112  |  |  * This function allows you to look up a profile based on a string.  | 
113  |  |  *  | 
114  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned,  | 
115  |  |  *   otherwise a negative error code is returned.  | 
116  |  |  *  | 
117  |  |  * Since 3.1.4  | 
118  |  |  **/  | 
119  |  | int gnutls_srtp_get_profile_id(const char *name, gnutls_srtp_profile_t *profile)  | 
120  | 0  | { | 
121  | 0  |   *profile = find_profile(name, NULL);  | 
122  | 0  |   if (*profile == 0) { | 
123  | 0  |     return GNUTLS_E_ILLEGAL_PARAMETER;  | 
124  | 0  |   }  | 
125  | 0  |   return 0;  | 
126  | 0  | }  | 
127  |  |  | 
128  | 0  | #define MAX_PROFILES_IN_SRTP_EXTENSION 256  | 
129  |  |  | 
130  |  | /**  | 
131  |  |  * gnutls_srtp_get_profile_name  | 
132  |  |  * @profile: The profile to look up a string for  | 
133  |  |  *  | 
134  |  |  * This function allows you to get the corresponding name for a  | 
135  |  |  * SRTP protection profile.  | 
136  |  |  *  | 
137  |  |  * Returns: On success, the name of a SRTP profile as a string,  | 
138  |  |  *   otherwise NULL.  | 
139  |  |  *  | 
140  |  |  * Since 3.1.4  | 
141  |  |  **/  | 
142  |  | const char *gnutls_srtp_get_profile_name(gnutls_srtp_profile_t profile)  | 
143  | 0  | { | 
144  | 0  |   const srtp_profile_st *p = get_profile(profile);  | 
145  |  | 
  | 
146  | 0  |   if (p != NULL)  | 
147  | 0  |     return p->name;  | 
148  |  |  | 
149  | 0  |   return NULL;  | 
150  | 0  | }  | 
151  |  |  | 
152  |  | static int _gnutls_srtp_recv_params(gnutls_session_t session,  | 
153  |  |             const uint8_t *data, size_t data_size)  | 
154  | 0  | { | 
155  | 0  |   unsigned int i;  | 
156  | 0  |   int ret;  | 
157  | 0  |   const uint8_t *p = data;  | 
158  | 0  |   size_t len;  | 
159  | 0  |   srtp_ext_st *priv;  | 
160  | 0  |   gnutls_ext_priv_data_t epriv;  | 
161  | 0  |   uint16_t profile;  | 
162  |  | 
  | 
163  | 0  |   ret = _gnutls_hello_ext_get_priv(session, GNUTLS_EXTENSION_SRTP,  | 
164  | 0  |            &epriv);  | 
165  | 0  |   if (ret < 0)  | 
166  | 0  |     return 0;  | 
167  |  |  | 
168  | 0  |   priv = epriv;  | 
169  |  | 
  | 
170  | 0  |   DECR_LENGTH_RET(data_size, 2, 0);  | 
171  | 0  |   len = _gnutls_read_uint16(p);  | 
172  | 0  |   p += 2;  | 
173  |  | 
  | 
174  | 0  |   if (len + 1 > data_size)  | 
175  | 0  |     return gnutls_assert_val(GNUTLS_E_UNEXPECTED_PACKET_LENGTH);  | 
176  |  |  | 
177  | 0  |   if (session->security_parameters.entity == GNUTLS_SERVER) { | 
178  | 0  |     if (len > MAX_PROFILES_IN_SRTP_EXTENSION * 2)  | 
179  | 0  |       return 0;  | 
180  | 0  |   } else { | 
181  | 0  |     if (len != 2)  | 
182  | 0  |       return gnutls_assert_val(  | 
183  | 0  |         GNUTLS_E_UNEXPECTED_PACKET_LENGTH);  | 
184  | 0  |   }  | 
185  |  |  | 
186  | 0  |   priv->selected_profile = 0;  | 
187  |  | 
  | 
188  | 0  |   while (len > 0) { | 
189  | 0  |     DECR_LEN(data_size, 2);  | 
190  | 0  |     profile = _gnutls_read_uint16(p);  | 
191  |  | 
  | 
192  | 0  |     for (i = 0;  | 
193  | 0  |          i < priv->profiles_size && priv->selected_profile == 0;  | 
194  | 0  |          i++) { | 
195  | 0  |       if (priv->profiles[i] == profile) { | 
196  | 0  |         priv->selected_profile = profile;  | 
197  | 0  |         break;  | 
198  | 0  |       }  | 
199  | 0  |     }  | 
200  | 0  |     p += 2;  | 
201  | 0  |     len -= 2;  | 
202  | 0  |   }  | 
203  |  |  | 
204  | 0  |   DECR_LEN(data_size, 1);  | 
205  | 0  |   priv->mki_size = *p;  | 
206  | 0  |   p++;  | 
207  |  | 
  | 
208  | 0  |   if (priv->mki_size > 0) { | 
209  | 0  |     DECR_LEN(data_size, priv->mki_size);  | 
210  | 0  |     memcpy(priv->mki, p, priv->mki_size);  | 
211  | 0  |     priv->mki_received = 1;  | 
212  | 0  |   }  | 
213  |  |  | 
214  | 0  |   return 0;  | 
215  | 0  | }  | 
216  |  |  | 
217  |  | static int _gnutls_srtp_send_params(gnutls_session_t session,  | 
218  |  |             gnutls_buffer_st *extdata)  | 
219  | 0  | { | 
220  | 0  |   unsigned i;  | 
221  | 0  |   int total_size = 0, ret;  | 
222  | 0  |   srtp_ext_st *priv;  | 
223  | 0  |   gnutls_ext_priv_data_t epriv;  | 
224  |  | 
  | 
225  | 0  |   ret = _gnutls_hello_ext_get_priv(session, GNUTLS_EXTENSION_SRTP,  | 
226  | 0  |            &epriv);  | 
227  | 0  |   if (ret < 0)  | 
228  | 0  |     return 0;  | 
229  |  |  | 
230  | 0  |   priv = epriv;  | 
231  |  | 
  | 
232  | 0  |   if (priv->profiles_size == 0)  | 
233  | 0  |     return 0;  | 
234  |  |  | 
235  | 0  |   if (session->security_parameters.entity == GNUTLS_SERVER) { | 
236  |  |     /* Don't send anything if no matching profile was found */  | 
237  | 0  |     if (priv->selected_profile == 0)  | 
238  | 0  |       return 0;  | 
239  |  |  | 
240  | 0  |     ret = _gnutls_buffer_append_prefix(extdata, 16, 2);  | 
241  | 0  |     if (ret < 0)  | 
242  | 0  |       return gnutls_assert_val(ret);  | 
243  | 0  |     ret = _gnutls_buffer_append_prefix(extdata, 16,  | 
244  | 0  |                priv->selected_profile);  | 
245  | 0  |     if (ret < 0)  | 
246  | 0  |       return gnutls_assert_val(ret);  | 
247  | 0  |     total_size = 4;  | 
248  | 0  |   } else { | 
249  | 0  |     ret = _gnutls_buffer_append_prefix(extdata, 16,  | 
250  | 0  |                2 * priv->profiles_size);  | 
251  | 0  |     if (ret < 0)  | 
252  | 0  |       return gnutls_assert_val(ret);  | 
253  |  |  | 
254  | 0  |     for (i = 0; i < priv->profiles_size; i++) { | 
255  | 0  |       ret = _gnutls_buffer_append_prefix(extdata, 16,  | 
256  | 0  |                  priv->profiles[i]);  | 
257  | 0  |       if (ret < 0)  | 
258  | 0  |         return gnutls_assert_val(ret);  | 
259  | 0  |     }  | 
260  | 0  |     total_size = 2 + 2 * priv->profiles_size;  | 
261  | 0  |   }  | 
262  |  |  | 
263  |  |   /* use_mki */  | 
264  | 0  |   ret = _gnutls_buffer_append_data_prefix(extdata, 8, priv->mki,  | 
265  | 0  |             priv->mki_size);  | 
266  | 0  |   if (ret < 0)  | 
267  | 0  |     return gnutls_assert_val(ret);  | 
268  | 0  |   total_size += 1 + priv->mki_size;  | 
269  |  | 
  | 
270  | 0  |   return total_size;  | 
271  | 0  | }  | 
272  |  |  | 
273  |  | /**  | 
274  |  |  * gnutls_srtp_get_selected_profile:  | 
275  |  |  * @session: is a #gnutls_session_t type.  | 
276  |  |  * @profile: will hold the profile  | 
277  |  |  *  | 
278  |  |  * This function allows you to get the negotiated SRTP profile.  | 
279  |  |  *  | 
280  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned,  | 
281  |  |  *   otherwise a negative error code is returned.  | 
282  |  |  *  | 
283  |  |  * Since 3.1.4  | 
284  |  |  **/  | 
285  |  | int gnutls_srtp_get_selected_profile(gnutls_session_t session,  | 
286  |  |              gnutls_srtp_profile_t *profile)  | 
287  | 0  | { | 
288  | 0  |   srtp_ext_st *priv;  | 
289  | 0  |   int ret;  | 
290  | 0  |   gnutls_ext_priv_data_t epriv;  | 
291  |  | 
  | 
292  | 0  |   ret = _gnutls_hello_ext_get_priv(session, GNUTLS_EXTENSION_SRTP,  | 
293  | 0  |            &epriv);  | 
294  | 0  |   if (ret < 0) { | 
295  | 0  |     gnutls_assert();  | 
296  | 0  |     return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;  | 
297  | 0  |   }  | 
298  |  |  | 
299  | 0  |   priv = epriv;  | 
300  |  | 
  | 
301  | 0  |   if (priv->selected_profile == 0) { | 
302  | 0  |     return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;  | 
303  | 0  |   }  | 
304  |  |  | 
305  | 0  |   *profile = priv->selected_profile;  | 
306  |  | 
  | 
307  | 0  |   return 0;  | 
308  | 0  | }  | 
309  |  |  | 
310  |  | /**  | 
311  |  |  * gnutls_srtp_get_mki:  | 
312  |  |  * @session: is a #gnutls_session_t type.  | 
313  |  |  * @mki: will hold the MKI  | 
314  |  |  *  | 
315  |  |  * This function exports the negotiated Master Key Identifier,  | 
316  |  |  * received by the peer if any. The returned value in @mki should be   | 
317  |  |  * treated as constant and valid only during the session's lifetime.  | 
318  |  |  *  | 
319  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned,  | 
320  |  |  *   otherwise a negative error code is returned.  | 
321  |  |  *  | 
322  |  |  * Since 3.1.4  | 
323  |  |  **/  | 
324  |  | int gnutls_srtp_get_mki(gnutls_session_t session, gnutls_datum_t *mki)  | 
325  | 0  | { | 
326  | 0  |   srtp_ext_st *priv;  | 
327  | 0  |   int ret;  | 
328  | 0  |   gnutls_ext_priv_data_t epriv;  | 
329  |  | 
  | 
330  | 0  |   ret = _gnutls_hello_ext_get_priv(session, GNUTLS_EXTENSION_SRTP,  | 
331  | 0  |            &epriv);  | 
332  | 0  |   if (ret < 0)  | 
333  | 0  |     return gnutls_assert_val(GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE);  | 
334  |  |  | 
335  | 0  |   priv = epriv;  | 
336  |  | 
  | 
337  | 0  |   if (priv->mki_received == 0)  | 
338  | 0  |     return gnutls_assert_val(GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE);  | 
339  |  |  | 
340  | 0  |   mki->data = priv->mki;  | 
341  | 0  |   mki->size = priv->mki_size;  | 
342  |  | 
  | 
343  | 0  |   return 0;  | 
344  | 0  | }  | 
345  |  |  | 
346  |  | /**  | 
347  |  |  * gnutls_srtp_set_mki:  | 
348  |  |  * @session: is a #gnutls_session_t type.  | 
349  |  |  * @mki: holds the MKI  | 
350  |  |  *  | 
351  |  |  * This function sets the Master Key Identifier, to be  | 
352  |  |  * used by this session (if any).  | 
353  |  |  *  | 
354  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned,  | 
355  |  |  *   otherwise a negative error code is returned.  | 
356  |  |  *  | 
357  |  |  * Since 3.1.4  | 
358  |  |  **/  | 
359  |  | int gnutls_srtp_set_mki(gnutls_session_t session, const gnutls_datum_t *mki)  | 
360  | 0  | { | 
361  | 0  |   int ret;  | 
362  | 0  |   srtp_ext_st *priv;  | 
363  | 0  |   gnutls_ext_priv_data_t epriv;  | 
364  |  | 
  | 
365  | 0  |   ret = _gnutls_hello_ext_get_priv(session, GNUTLS_EXTENSION_SRTP,  | 
366  | 0  |            &epriv);  | 
367  | 0  |   if (ret < 0) { | 
368  | 0  |     priv = gnutls_calloc(1, sizeof(*priv));  | 
369  | 0  |     if (priv == NULL) { | 
370  | 0  |       gnutls_assert();  | 
371  | 0  |       return GNUTLS_E_MEMORY_ERROR;  | 
372  | 0  |     }  | 
373  | 0  |     epriv = priv;  | 
374  | 0  |     _gnutls_hello_ext_set_priv(session, GNUTLS_EXTENSION_SRTP,  | 
375  | 0  |              epriv);  | 
376  | 0  |   } else  | 
377  | 0  |     priv = epriv;  | 
378  |  |  | 
379  | 0  |   if (mki->size > 0 && mki->size <= sizeof(priv->mki)) { | 
380  | 0  |     priv->mki_size = mki->size;  | 
381  | 0  |     memcpy(priv->mki, mki->data, mki->size);  | 
382  | 0  |   } else  | 
383  | 0  |     return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);  | 
384  |  |  | 
385  | 0  |   return 0;  | 
386  | 0  | }  | 
387  |  |  | 
388  |  | /**  | 
389  |  |  * gnutls_srtp_set_profile:  | 
390  |  |  * @session: is a #gnutls_session_t type.  | 
391  |  |  * @profile: is the profile id to add.  | 
392  |  |  *  | 
393  |  |  * This function is to be used by both clients and servers, to declare  | 
394  |  |  * what SRTP profiles they support, to negotiate with the peer.  | 
395  |  |  *  | 
396  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned,  | 
397  |  |  *   otherwise a negative error code is returned.  | 
398  |  |  *  | 
399  |  |  * Since 3.1.4  | 
400  |  |  **/  | 
401  |  | int gnutls_srtp_set_profile(gnutls_session_t session,  | 
402  |  |           gnutls_srtp_profile_t profile)  | 
403  | 0  | { | 
404  | 0  |   int ret;  | 
405  | 0  |   srtp_ext_st *priv;  | 
406  | 0  |   gnutls_ext_priv_data_t epriv;  | 
407  |  | 
  | 
408  | 0  |   ret = _gnutls_hello_ext_get_priv(session, GNUTLS_EXTENSION_SRTP,  | 
409  | 0  |            &epriv);  | 
410  | 0  |   if (ret < 0) { | 
411  | 0  |     priv = gnutls_calloc(1, sizeof(*priv));  | 
412  | 0  |     if (priv == NULL) { | 
413  | 0  |       gnutls_assert();  | 
414  | 0  |       return GNUTLS_E_MEMORY_ERROR;  | 
415  | 0  |     }  | 
416  | 0  |     epriv = priv;  | 
417  | 0  |     _gnutls_hello_ext_set_priv(session, GNUTLS_EXTENSION_SRTP,  | 
418  | 0  |              epriv);  | 
419  | 0  |   } else  | 
420  | 0  |     priv = epriv;  | 
421  |  |  | 
422  | 0  |   if (priv->profiles_size < MAX_SRTP_PROFILES)  | 
423  | 0  |     priv->profiles_size++;  | 
424  | 0  |   priv->profiles[priv->profiles_size - 1] = profile;  | 
425  |  | 
  | 
426  | 0  |   return 0;  | 
427  | 0  | }  | 
428  |  |  | 
429  |  | /**  | 
430  |  |  * gnutls_srtp_set_profile_direct:  | 
431  |  |  * @session: is a #gnutls_session_t type.  | 
432  |  |  * @profiles: is a string that contains the supported SRTP profiles,  | 
433  |  |  *   separated by colons.  | 
434  |  |  * @err_pos: In case of an error this will have the position in the string the error occurred, may be NULL.  | 
435  |  |  *  | 
436  |  |  * This function is to be used by both clients and servers, to declare  | 
437  |  |  * what SRTP profiles they support, to negotiate with the peer.  | 
438  |  |  *  | 
439  |  |  * Returns: On syntax error %GNUTLS_E_INVALID_REQUEST is returned,  | 
440  |  |  * %GNUTLS_E_SUCCESS on success, or an error code.  | 
441  |  |  *  | 
442  |  |  * Since 3.1.4  | 
443  |  |  **/  | 
444  |  | int gnutls_srtp_set_profile_direct(gnutls_session_t session,  | 
445  |  |            const char *profiles, const char **err_pos)  | 
446  | 0  | { | 
447  | 0  |   int ret;  | 
448  | 0  |   srtp_ext_st *priv;  | 
449  | 0  |   gnutls_ext_priv_data_t epriv;  | 
450  | 0  |   int set = 0;  | 
451  | 0  |   const char *col;  | 
452  | 0  |   gnutls_srtp_profile_t id;  | 
453  |  | 
  | 
454  | 0  |   ret = _gnutls_hello_ext_get_priv(session, GNUTLS_EXTENSION_SRTP,  | 
455  | 0  |            &epriv);  | 
456  | 0  |   if (ret < 0) { | 
457  | 0  |     set = 1;  | 
458  | 0  |     priv = gnutls_calloc(1, sizeof(*priv));  | 
459  | 0  |     if (priv == NULL) { | 
460  | 0  |       if (err_pos != NULL)  | 
461  | 0  |         *err_pos = profiles;  | 
462  | 0  |       gnutls_assert();  | 
463  | 0  |       return GNUTLS_E_MEMORY_ERROR;  | 
464  | 0  |     }  | 
465  | 0  |     epriv = priv;  | 
466  | 0  |   } else  | 
467  | 0  |     priv = epriv;  | 
468  |  |  | 
469  | 0  |   do { | 
470  | 0  |     col = strchr(profiles, ':');  | 
471  | 0  |     id = find_profile(profiles, col);  | 
472  | 0  |     if (id == 0) { | 
473  | 0  |       if (set != 0)  | 
474  | 0  |         gnutls_free(priv);  | 
475  | 0  |       if (err_pos != NULL)  | 
476  | 0  |         *err_pos = profiles;  | 
477  | 0  |       return GNUTLS_E_INVALID_REQUEST;  | 
478  | 0  |     }  | 
479  |  |  | 
480  | 0  |     if (priv->profiles_size < MAX_SRTP_PROFILES) { | 
481  | 0  |       priv->profiles_size++;  | 
482  | 0  |     }  | 
483  | 0  |     priv->profiles[priv->profiles_size - 1] = id;  | 
484  | 0  |     profiles = col + 1;  | 
485  | 0  |   } while (col != NULL);  | 
486  |  |  | 
487  | 0  |   if (set != 0)  | 
488  | 0  |     _gnutls_hello_ext_set_priv(session, GNUTLS_EXTENSION_SRTP,  | 
489  | 0  |              epriv);  | 
490  |  | 
  | 
491  | 0  |   return 0;  | 
492  | 0  | }  | 
493  |  |  | 
494  |  | /**  | 
495  |  |  * gnutls_srtp_get_keys:  | 
496  |  |  * @session: is a #gnutls_session_t type.  | 
497  |  |  * @key_material: Space to hold the generated key material  | 
498  |  |  * @key_material_size: The maximum size of the key material  | 
499  |  |  * @client_key: The master client write key, pointing inside the key material  | 
500  |  |  * @server_key: The master server write key, pointing inside the key material  | 
501  |  |  * @client_salt: The master client write salt, pointing inside the key material  | 
502  |  |  * @server_salt: The master server write salt, pointing inside the key material  | 
503  |  |  *  | 
504  |  |  * This is a helper function to generate the keying material for SRTP.  | 
505  |  |  * It requires the space of the key material to be pre-allocated (should be at least  | 
506  |  |  * 2x the maximum key size and salt size). The @client_key, @client_salt, @server_key  | 
507  |  |  * and @server_salt are convenience datums that point inside the key material. They may  | 
508  |  |  * be %NULL.  | 
509  |  |  *  | 
510  |  |  * Returns: On success the size of the key material is returned,  | 
511  |  |  * otherwise, %GNUTLS_E_SHORT_MEMORY_BUFFER if the buffer given is not   | 
512  |  |  * sufficient, or a negative error code.  | 
513  |  |  *  | 
514  |  |  * Since 3.1.4  | 
515  |  |  **/  | 
516  |  | int gnutls_srtp_get_keys(gnutls_session_t session, void *key_material,  | 
517  |  |        unsigned int key_material_size,  | 
518  |  |        gnutls_datum_t *client_key,  | 
519  |  |        gnutls_datum_t *client_salt,  | 
520  |  |        gnutls_datum_t *server_key,  | 
521  |  |        gnutls_datum_t *server_salt)  | 
522  | 0  | { | 
523  | 0  |   int ret;  | 
524  | 0  |   const srtp_profile_st *p;  | 
525  | 0  |   gnutls_srtp_profile_t profile;  | 
526  | 0  |   unsigned int msize;  | 
527  | 0  |   uint8_t *km = key_material;  | 
528  |  | 
  | 
529  | 0  |   ret = gnutls_srtp_get_selected_profile(session, &profile);  | 
530  | 0  |   if (ret < 0)  | 
531  | 0  |     return gnutls_assert_val(ret);  | 
532  |  |  | 
533  | 0  |   p = get_profile(profile);  | 
534  | 0  |   if (p == NULL)  | 
535  | 0  |     return gnutls_assert_val(GNUTLS_E_UNKNOWN_ALGORITHM);  | 
536  |  |  | 
537  | 0  |   msize = 2 * (p->key_length + p->salt_length);  | 
538  | 0  |   if (msize > key_material_size)  | 
539  | 0  |     return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);  | 
540  |  |  | 
541  | 0  |   if (msize == 0)  | 
542  | 0  |     return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);  | 
543  |  |  | 
544  | 0  |   ret = gnutls_prf(session, sizeof("EXTRACTOR-dtls_srtp") - 1, | 
545  | 0  |        "EXTRACTOR-dtls_srtp", 0, 0, NULL, msize,  | 
546  | 0  |        key_material);  | 
547  | 0  |   if (ret < 0)  | 
548  | 0  |     return gnutls_assert_val(ret);  | 
549  |  |  | 
550  | 0  |   if (client_key) { | 
551  | 0  |     client_key->data = km;  | 
552  | 0  |     client_key->size = p->key_length;  | 
553  | 0  |   }  | 
554  |  | 
  | 
555  | 0  |   if (server_key) { | 
556  | 0  |     server_key->data = km + p->key_length;  | 
557  | 0  |     server_key->size = p->key_length;  | 
558  | 0  |   }  | 
559  |  | 
  | 
560  | 0  |   if (client_salt) { | 
561  | 0  |     client_salt->data = km + 2 * p->key_length;  | 
562  | 0  |     client_salt->size = p->salt_length;  | 
563  | 0  |   }  | 
564  |  | 
  | 
565  | 0  |   if (server_salt) { | 
566  | 0  |     server_salt->data = km + 2 * p->key_length + p->salt_length;  | 
567  | 0  |     server_salt->size = p->salt_length;  | 
568  | 0  |   }  | 
569  |  | 
  | 
570  | 0  |   return msize;  | 
571  | 0  | }  | 
572  |  |  | 
573  |  | static void _gnutls_srtp_deinit_data(gnutls_ext_priv_data_t priv)  | 
574  | 0  | { | 
575  | 0  |   gnutls_free(priv);  | 
576  | 0  | }  | 
577  |  |  | 
578  |  | static int _gnutls_srtp_pack(gnutls_ext_priv_data_t epriv, gnutls_buffer_st *ps)  | 
579  | 0  | { | 
580  | 0  |   srtp_ext_st *priv = epriv;  | 
581  | 0  |   unsigned int i;  | 
582  | 0  |   int ret;  | 
583  |  | 
  | 
584  | 0  |   BUFFER_APPEND_NUM(ps, priv->profiles_size);  | 
585  | 0  |   for (i = 0; i < priv->profiles_size; i++) { | 
586  | 0  |     BUFFER_APPEND_NUM(ps, priv->profiles[i]);  | 
587  | 0  |   }  | 
588  |  |  | 
589  | 0  |   BUFFER_APPEND_NUM(ps, priv->mki_received);  | 
590  | 0  |   if (priv->mki_received) { | 
591  | 0  |     BUFFER_APPEND_NUM(ps, priv->selected_profile);  | 
592  | 0  |     BUFFER_APPEND_PFX4(ps, priv->mki, priv->mki_size);  | 
593  | 0  |   }  | 
594  | 0  |   return 0;  | 
595  | 0  | }  | 
596  |  |  | 
597  |  | static int _gnutls_srtp_unpack(gnutls_buffer_st *ps,  | 
598  |  |              gnutls_ext_priv_data_t *_priv)  | 
599  | 0  | { | 
600  | 0  |   srtp_ext_st *priv;  | 
601  | 0  |   unsigned int i;  | 
602  | 0  |   int ret;  | 
603  | 0  |   gnutls_ext_priv_data_t epriv;  | 
604  |  | 
  | 
605  | 0  |   priv = gnutls_calloc(1, sizeof(*priv));  | 
606  | 0  |   if (priv == NULL) { | 
607  | 0  |     gnutls_assert();  | 
608  | 0  |     return GNUTLS_E_MEMORY_ERROR;  | 
609  | 0  |   }  | 
610  |  |  | 
611  | 0  |   BUFFER_POP_NUM(ps, priv->profiles_size);  | 
612  | 0  |   for (i = 0; i < priv->profiles_size; i++) { | 
613  | 0  |     BUFFER_POP_NUM(ps, priv->profiles[i]);  | 
614  | 0  |   }  | 
615  | 0  |   BUFFER_POP_NUM(ps, priv->selected_profile);  | 
616  |  | 
  | 
617  | 0  |   BUFFER_POP_NUM(ps, priv->mki_received);  | 
618  | 0  |   if (priv->mki_received) { | 
619  | 0  |     BUFFER_POP_NUM(ps, priv->mki_size);  | 
620  | 0  |     BUFFER_POP(ps, priv->mki, priv->mki_size);  | 
621  | 0  |   }  | 
622  |  |  | 
623  | 0  |   epriv = priv;  | 
624  | 0  |   *_priv = epriv;  | 
625  |  | 
  | 
626  | 0  |   return 0;  | 
627  |  |  | 
628  | 0  | error:  | 
629  | 0  |   gnutls_free(priv);  | 
630  | 0  |   return ret;  | 
631  | 0  | }  |