94c0127312c2054c5e5e7ece91b204e746d9940c
[openssl.git] / ssl / d1_srtp.c
1 /*
2  * Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /*
11  * DTLS code by Eric Rescorla <ekr@rtfm.com>
12  *
13  * Copyright (C) 2006, Network Resonance, Inc. Copyright (C) 2011, RTFM, Inc.
14  */
15
16 #include <stdio.h>
17 #include <openssl/objects.h>
18 #include "ssl_locl.h"
19
20 #ifndef OPENSSL_NO_SRTP
21
22 static SRTP_PROTECTION_PROFILE srtp_known_profiles[] = {
23     {
24      "SRTP_AES128_CM_SHA1_80",
25      SRTP_AES128_CM_SHA1_80,
26      },
27     {
28      "SRTP_AES128_CM_SHA1_32",
29      SRTP_AES128_CM_SHA1_32,
30      },
31     {
32      "SRTP_AEAD_AES_128_GCM",
33      SRTP_AEAD_AES_128_GCM
34      },
35     {
36      "SRTP_AEAD_AES_256_GCM",
37      SRTP_AEAD_AES_256_GCM
38      },
39     {0}
40 };
41
42 static int find_profile_by_name(char *profile_name,
43                                 SRTP_PROTECTION_PROFILE **pptr, unsigned len)
44 {
45     SRTP_PROTECTION_PROFILE *p;
46
47     p = srtp_known_profiles;
48     while (p->name) {
49         if ((len == strlen(p->name))
50             && strncmp(p->name, profile_name, len) == 0) {
51             *pptr = p;
52             return 0;
53         }
54
55         p++;
56     }
57
58     return 1;
59 }
60
61 static int ssl_ctx_make_profiles(const char *profiles_string,
62                                  STACK_OF(SRTP_PROTECTION_PROFILE) **out)
63 {
64     STACK_OF(SRTP_PROTECTION_PROFILE) *profiles;
65
66     char *col;
67     char *ptr = (char *)profiles_string;
68     SRTP_PROTECTION_PROFILE *p;
69
70     if ((profiles = sk_SRTP_PROTECTION_PROFILE_new_null()) == NULL) {
71         SSLerr(SSL_F_SSL_CTX_MAKE_PROFILES,
72                SSL_R_SRTP_COULD_NOT_ALLOCATE_PROFILES);
73         return 1;
74     }
75
76     do {
77         col = strchr(ptr, ':');
78
79         if (!find_profile_by_name(ptr, &p,
80                                   col ? col - ptr : (int)strlen(ptr))) {
81             if (sk_SRTP_PROTECTION_PROFILE_find(profiles, p) >= 0) {
82                 SSLerr(SSL_F_SSL_CTX_MAKE_PROFILES,
83                        SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
84                 goto err;
85             }
86
87             if (!sk_SRTP_PROTECTION_PROFILE_push(profiles, p)) {
88                 SSLerr(SSL_F_SSL_CTX_MAKE_PROFILES,
89                        SSL_R_SRTP_COULD_NOT_ALLOCATE_PROFILES);
90                 goto err;
91             }
92         } else {
93             SSLerr(SSL_F_SSL_CTX_MAKE_PROFILES,
94                    SSL_R_SRTP_UNKNOWN_PROTECTION_PROFILE);
95             goto err;
96         }
97
98         if (col)
99             ptr = col + 1;
100     } while (col);
101
102     sk_SRTP_PROTECTION_PROFILE_free(*out);
103
104     *out = profiles;
105
106     return 0;
107 err:
108     sk_SRTP_PROTECTION_PROFILE_free(profiles);
109     return 1;
110 }
111
112 int SSL_CTX_set_tlsext_use_srtp(SSL_CTX *ctx, const char *profiles)
113 {
114     return ssl_ctx_make_profiles(profiles, &ctx->srtp_profiles);
115 }
116
117 int SSL_set_tlsext_use_srtp(SSL *s, const char *profiles)
118 {
119     return ssl_ctx_make_profiles(profiles, &s->srtp_profiles);
120 }
121
122 STACK_OF(SRTP_PROTECTION_PROFILE) *SSL_get_srtp_profiles(SSL *s)
123 {
124     if (s != NULL) {
125         if (s->srtp_profiles != NULL) {
126             return s->srtp_profiles;
127         } else if ((s->ctx != NULL) && (s->ctx->srtp_profiles != NULL)) {
128             return s->ctx->srtp_profiles;
129         }
130     }
131
132     return NULL;
133 }
134
135 SRTP_PROTECTION_PROFILE *SSL_get_selected_srtp_profile(SSL *s)
136 {
137     return s->srtp_profile;
138 }
139
140 /*
141  * Note: this function returns 0 length if there are no profiles specified
142  */
143 int ssl_add_clienthello_use_srtp_ext(SSL *s, unsigned char *p, int *len,
144                                      int maxlen)
145 {
146     int ct = 0;
147     int i;
148     STACK_OF(SRTP_PROTECTION_PROFILE) *clnt = 0;
149     SRTP_PROTECTION_PROFILE *prof;
150
151     clnt = SSL_get_srtp_profiles(s);
152     ct = sk_SRTP_PROTECTION_PROFILE_num(clnt); /* -1 if clnt == 0 */
153
154     if (p) {
155         if (ct == 0) {
156             SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_USE_SRTP_EXT,
157                    SSL_R_EMPTY_SRTP_PROTECTION_PROFILE_LIST);
158             return 1;
159         }
160
161         if ((2 + ct * 2 + 1) > maxlen) {
162             SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_USE_SRTP_EXT,
163                    SSL_R_SRTP_PROTECTION_PROFILE_LIST_TOO_LONG);
164             return 1;
165         }
166
167         /* Add the length */
168         s2n(ct * 2, p);
169         for (i = 0; i < ct; i++) {
170             prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i);
171             s2n(prof->id, p);
172         }
173
174         /* Add an empty use_mki value */
175         *p++ = 0;
176     }
177
178     *len = 2 + ct * 2 + 1;
179
180     return 0;
181 }
182
183 int ssl_parse_clienthello_use_srtp_ext(SSL *s, PACKET *pkt, int *al)
184 {
185     SRTP_PROTECTION_PROFILE *sprof;
186     STACK_OF(SRTP_PROTECTION_PROFILE) *srvr;
187     unsigned int ct, mki_len, id;
188     int i, srtp_pref;
189     PACKET subpkt;
190
191     /* Pull off the length of the cipher suite list  and check it is even */
192     if (!PACKET_get_net_2(pkt, &ct)
193             || (ct & 1) != 0
194             || !PACKET_get_sub_packet(pkt, &subpkt, ct)) {
195         SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_USE_SRTP_EXT,
196                SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
197         *al = SSL_AD_DECODE_ERROR;
198         return 1;
199     }
200
201     srvr = SSL_get_srtp_profiles(s);
202     s->srtp_profile = NULL;
203     /* Search all profiles for a match initially */
204     srtp_pref = sk_SRTP_PROTECTION_PROFILE_num(srvr);
205
206     while (PACKET_remaining(&subpkt)) {
207         if (!PACKET_get_net_2(&subpkt, &id)) {
208             SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_USE_SRTP_EXT,
209                    SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
210             *al = SSL_AD_DECODE_ERROR;
211             return 1;
212         }
213
214         /*
215          * Only look for match in profiles of higher preference than
216          * current match.
217          * If no profiles have been have been configured then this
218          * does nothing.
219          */
220         for (i = 0; i < srtp_pref; i++) {
221             sprof = sk_SRTP_PROTECTION_PROFILE_value(srvr, i);
222             if (sprof->id == id) {
223                 s->srtp_profile = sprof;
224                 srtp_pref = i;
225                 break;
226             }
227         }
228     }
229
230     /*
231      * Now extract the MKI value as a sanity check, but discard it for now
232      */
233     if (!PACKET_get_1(pkt, &mki_len)) {
234         SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_USE_SRTP_EXT,
235                SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
236         *al = SSL_AD_DECODE_ERROR;
237         return 1;
238     }
239
240     if (!PACKET_forward(pkt, mki_len)
241             || PACKET_remaining(pkt)) {
242         SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_USE_SRTP_EXT,
243                SSL_R_BAD_SRTP_MKI_VALUE);
244         *al = SSL_AD_DECODE_ERROR;
245         return 1;
246     }
247
248     return 0;
249 }
250
251 int ssl_add_serverhello_use_srtp_ext(SSL *s, unsigned char *p, int *len,
252                                      int maxlen)
253 {
254     if (p) {
255         if (maxlen < 5) {
256             SSLerr(SSL_F_SSL_ADD_SERVERHELLO_USE_SRTP_EXT,
257                    SSL_R_SRTP_PROTECTION_PROFILE_LIST_TOO_LONG);
258             return 1;
259         }
260
261         if (s->srtp_profile == 0) {
262             SSLerr(SSL_F_SSL_ADD_SERVERHELLO_USE_SRTP_EXT,
263                    SSL_R_USE_SRTP_NOT_NEGOTIATED);
264             return 1;
265         }
266         s2n(2, p);
267         s2n(s->srtp_profile->id, p);
268         *p++ = 0;
269     }
270     *len = 5;
271
272     return 0;
273 }
274
275 int ssl_parse_serverhello_use_srtp_ext(SSL *s, PACKET *pkt, int *al)
276 {
277     unsigned int id, ct, mki;
278     int i;
279
280     STACK_OF(SRTP_PROTECTION_PROFILE) *clnt;
281     SRTP_PROTECTION_PROFILE *prof;
282
283     if (!PACKET_get_net_2(pkt, &ct)
284             || ct != 2
285             || !PACKET_get_net_2(pkt, &id)
286             || !PACKET_get_1(pkt, &mki)
287             || PACKET_remaining(pkt) != 0) {
288         SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_USE_SRTP_EXT,
289                SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
290         *al = SSL_AD_DECODE_ERROR;
291         return 1;
292     }
293
294     if (mki != 0) {
295         /* Must be no MKI, since we never offer one */
296         SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_USE_SRTP_EXT,
297                SSL_R_BAD_SRTP_MKI_VALUE);
298         *al = SSL_AD_ILLEGAL_PARAMETER;
299         return 1;
300     }
301
302     clnt = SSL_get_srtp_profiles(s);
303
304     /* Throw an error if the server gave us an unsolicited extension */
305     if (clnt == NULL) {
306         SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_USE_SRTP_EXT,
307                SSL_R_NO_SRTP_PROFILES);
308         *al = SSL_AD_DECODE_ERROR;
309         return 1;
310     }
311
312     /*
313      * Check to see if the server gave us something we support (and
314      * presumably offered)
315      */
316     for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(clnt); i++) {
317         prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i);
318
319         if (prof->id == id) {
320             s->srtp_profile = prof;
321             *al = 0;
322             return 0;
323         }
324     }
325
326     SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_USE_SRTP_EXT,
327            SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
328     *al = SSL_AD_DECODE_ERROR;
329     return 1;
330 }
331
332 #endif