blob: e98b512598ead916effc733b84788d47e61cd971 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/*
111 DTLS code by Eric Rescorla <ekr@rtfm.com>
112
113 Copyright (C) 2006, Network Resonance, Inc.
114 Copyright (C) 2011, RTFM, Inc.
115*/
116
117#ifndef OPENSSL_NO_SRTP
118
119#include <stdio.h>
120
121#include <openssl/obj.h>
122#include <openssl/err.h>
123
124#include "ssl_locl.h"
125#include "srtp.h"
126
127
128static SRTP_PROTECTION_PROFILE srtp_known_profiles[]=
129 {
130 {
131 "SRTP_AES128_CM_SHA1_80",
132 SRTP_AES128_CM_SHA1_80,
133 },
134 {
135 "SRTP_AES128_CM_SHA1_32",
136 SRTP_AES128_CM_SHA1_32,
137 },
138#if 0
139 {
140 "SRTP_NULL_SHA1_80",
141 SRTP_NULL_SHA1_80,
142 },
143 {
144 "SRTP_NULL_SHA1_32",
145 SRTP_NULL_SHA1_32,
146 },
147#endif
148 {0}
149 };
150
151static int find_profile_by_name(char *profile_name,
152 SRTP_PROTECTION_PROFILE **pptr,unsigned len)
153 {
154 SRTP_PROTECTION_PROFILE *p;
155
156 p=srtp_known_profiles;
157 while(p->name)
158 {
159 if((len == strlen(p->name)) && !strncmp(p->name,profile_name,
160 len))
161 {
162 *pptr=p;
163 return 0;
164 }
165
166 p++;
167 }
168
169 return 1;
170 }
171
172static int find_profile_by_num(unsigned profile_num,
173 SRTP_PROTECTION_PROFILE **pptr)
174 {
175 SRTP_PROTECTION_PROFILE *p;
176
177 p=srtp_known_profiles;
178 while(p->name)
179 {
180 if(p->id == profile_num)
181 {
182 *pptr=p;
183 return 0;
184 }
185 p++;
186 }
187
188 return 1;
189 }
190
191static int ssl_ctx_make_profiles(const char *profiles_string,STACK_OF(SRTP_PROTECTION_PROFILE) **out)
192 {
193 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles;
194
195 char *col;
196 char *ptr=(char *)profiles_string;
197
198 SRTP_PROTECTION_PROFILE *p;
199
200 if(!(profiles=sk_SRTP_PROTECTION_PROFILE_new_null()))
201 {
202 OPENSSL_PUT_ERROR(SSL, ssl_ctx_make_profiles, SSL_R_SRTP_COULD_NOT_ALLOCATE_PROFILES);
203 return 1;
204 }
205
206 do
207 {
208 col=strchr(ptr,':');
209
210 if(!find_profile_by_name(ptr,&p,
211 col ? col-ptr : (int)strlen(ptr)))
212 {
213 sk_SRTP_PROTECTION_PROFILE_push(profiles,p);
214 }
215 else
216 {
217 OPENSSL_PUT_ERROR(SSL, ssl_ctx_make_profiles, SSL_R_SRTP_UNKNOWN_PROTECTION_PROFILE);
218 return 1;
219 }
220
221 if(col) ptr=col+1;
222 } while (col);
223
224 *out=profiles;
225
226 return 0;
227 }
228
229int SSL_CTX_set_tlsext_use_srtp(SSL_CTX *ctx,const char *profiles)
230 {
231 return ssl_ctx_make_profiles(profiles,&ctx->srtp_profiles);
232 }
233
234int SSL_set_tlsext_use_srtp(SSL *s,const char *profiles)
235 {
236 return ssl_ctx_make_profiles(profiles,&s->srtp_profiles);
237 }
238
239
240STACK_OF(SRTP_PROTECTION_PROFILE) *SSL_get_srtp_profiles(SSL *s)
241 {
242 if(s != NULL)
243 {
244 if(s->srtp_profiles != NULL)
245 {
246 return s->srtp_profiles;
247 }
248 else if((s->ctx != NULL) &&
249 (s->ctx->srtp_profiles != NULL))
250 {
251 return s->ctx->srtp_profiles;
252 }
253 }
254
255 return NULL;
256 }
257
258SRTP_PROTECTION_PROFILE *SSL_get_selected_srtp_profile(SSL *s)
259 {
260 return s->srtp_profile;
261 }
262
263/* Note: this function returns 0 length if there are no
264 profiles specified */
265int ssl_add_clienthello_use_srtp_ext(SSL *s, unsigned char *p, int *len, int maxlen)
266 {
267 int ct=0;
268 int i;
269 STACK_OF(SRTP_PROTECTION_PROFILE) *clnt=0;
270 SRTP_PROTECTION_PROFILE *prof;
271
272 clnt=SSL_get_srtp_profiles(s);
273 ct=sk_SRTP_PROTECTION_PROFILE_num(clnt); /* -1 if clnt == 0 */
274
275 if(p)
276 {
277 if(ct==0)
278 {
279 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_use_srtp_ext, SSL_R_EMPTY_SRTP_PROTECTION_PROFILE_LIST);
280 return 1;
281 }
282
283 if((2 + ct*2 + 1) > maxlen)
284 {
285 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_use_srtp_ext, SSL_R_SRTP_PROTECTION_PROFILE_LIST_TOO_LONG);
286 return 1;
287 }
288
289 /* Add the length */
290 s2n(ct * 2, p);
291 for(i=0;i<ct;i++)
292 {
293 prof=sk_SRTP_PROTECTION_PROFILE_value(clnt,i);
294 s2n(prof->id,p);
295 }
296
297 /* Add an empty use_mki value */
298 *p++ = 0;
299 }
300
301 *len=2 + ct*2 + 1;
302
303 return 0;
304 }
305
306
307int ssl_parse_clienthello_use_srtp_ext(SSL *s, unsigned char *d, int len,int *al)
308 {
309 SRTP_PROTECTION_PROFILE *cprof,*sprof;
310 STACK_OF(SRTP_PROTECTION_PROFILE) *clnt=0,*srvr;
311 int ct;
312 int mki_len;
313 int i,j;
314 int id;
315 int ret;
316
317 /* Length value + the MKI length */
318 if(len < 3)
319 {
320 OPENSSL_PUT_ERROR(SSL, ssl_parse_clienthello_use_srtp_ext, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
321 *al=SSL_AD_DECODE_ERROR;
322 return 1;
323 }
324
325 /* Pull off the length of the cipher suite list */
326 n2s(d, ct);
327 len -= 2;
328
329 /* Check that it is even */
330 if(ct%2)
331 {
332 OPENSSL_PUT_ERROR(SSL, ssl_parse_clienthello_use_srtp_ext, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
333 *al=SSL_AD_DECODE_ERROR;
334 return 1;
335 }
336
337 /* Check that lengths are consistent */
338 if(len < (ct + 1))
339 {
340 OPENSSL_PUT_ERROR(SSL, ssl_parse_clienthello_use_srtp_ext, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
341 *al=SSL_AD_DECODE_ERROR;
342 return 1;
343 }
344
345
346 clnt=sk_SRTP_PROTECTION_PROFILE_new_null();
347
348 while(ct)
349 {
350 n2s(d,id);
351 ct-=2;
352 len-=2;
353
354 if(!find_profile_by_num(id,&cprof))
355 {
356 sk_SRTP_PROTECTION_PROFILE_push(clnt,cprof);
357 }
358 else
359 {
360 ; /* Ignore */
361 }
362 }
363
364 /* Now extract the MKI value as a sanity check, but discard it for now */
365 mki_len = *d;
366 d++; len--;
367
368 if (mki_len != len)
369 {
370 OPENSSL_PUT_ERROR(SSL, ssl_parse_clienthello_use_srtp_ext, SSL_R_BAD_SRTP_MKI_VALUE);
371 *al=SSL_AD_DECODE_ERROR;
372 return 1;
373 }
374
375 srvr=SSL_get_srtp_profiles(s);
376
377 /* Pick our most preferred profile. If no profiles have been
378 configured then the outer loop doesn't run
379 (sk_SRTP_PROTECTION_PROFILE_num() = -1)
380 and so we just return without doing anything */
381 for(i=0;i<sk_SRTP_PROTECTION_PROFILE_num(srvr);i++)
382 {
383 sprof=sk_SRTP_PROTECTION_PROFILE_value(srvr,i);
384
385 for(j=0;j<sk_SRTP_PROTECTION_PROFILE_num(clnt);j++)
386 {
387 cprof=sk_SRTP_PROTECTION_PROFILE_value(clnt,j);
388
389 if(cprof->id==sprof->id)
390 {
391 s->srtp_profile=sprof;
392 *al=0;
393 ret=0;
394 goto done;
395 }
396 }
397 }
398
399 ret=0;
400
401done:
402 if(clnt) sk_SRTP_PROTECTION_PROFILE_free(clnt);
403
404 return ret;
405 }
406
407int ssl_add_serverhello_use_srtp_ext(SSL *s, unsigned char *p, int *len, int maxlen)
408 {
409 if(p)
410 {
411 if(maxlen < 5)
412 {
413 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_use_srtp_ext, SSL_R_SRTP_PROTECTION_PROFILE_LIST_TOO_LONG);
414 return 1;
415 }
416
417 if(s->srtp_profile==0)
418 {
419 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_use_srtp_ext, SSL_R_USE_SRTP_NOT_NEGOTIATED);
420 return 1;
421 }
422 s2n(2, p);
423 s2n(s->srtp_profile->id,p);
424 *p++ = 0;
425 }
426 *len=5;
427
428 return 0;
429 }
430
431
432int ssl_parse_serverhello_use_srtp_ext(SSL *s, unsigned char *d, int len,int *al)
433 {
434 unsigned id;
435 int i;
436 int ct;
437
438 STACK_OF(SRTP_PROTECTION_PROFILE) *clnt;
439 SRTP_PROTECTION_PROFILE *prof;
440
441 if(len!=5)
442 {
443 OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_use_srtp_ext, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
444 *al=SSL_AD_DECODE_ERROR;
445 return 1;
446 }
447
448 n2s(d, ct);
449 if(ct!=2)
450 {
451 OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_use_srtp_ext, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
452 *al=SSL_AD_DECODE_ERROR;
453 return 1;
454 }
455
456 n2s(d,id);
457 if (*d) /* Must be no MKI, since we never offer one */
458 {
459 OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_use_srtp_ext, SSL_R_BAD_SRTP_MKI_VALUE);
460 *al=SSL_AD_ILLEGAL_PARAMETER;
461 return 1;
462 }
463
464 clnt=SSL_get_srtp_profiles(s);
465
466 /* Throw an error if the server gave us an unsolicited extension */
467 if (clnt == NULL)
468 {
469 OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_use_srtp_ext, SSL_R_NO_SRTP_PROFILES);
470 *al=SSL_AD_DECODE_ERROR;
471 return 1;
472 }
473
474 /* Check to see if the server gave us something we support
475 (and presumably offered)
476 */
477 for(i=0;i<sk_SRTP_PROTECTION_PROFILE_num(clnt);i++)
478 {
479 prof=sk_SRTP_PROTECTION_PROFILE_value(clnt,i);
480
481 if(prof->id == id)
482 {
483 s->srtp_profile=prof;
484 *al=0;
485 return 0;
486 }
487 }
488
489 OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_use_srtp_ext, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
490 *al=SSL_AD_DECODE_ERROR;
491 return 1;
492 }
493
494
495#endif