Fix: PEM_read_bio_PrivateKey with no-ui / no-stdio
[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                 sk_SRTP_PROTECTION_PROFILE_free(profiles);
85                 return 1;
86             }
87
88             sk_SRTP_PROTECTION_PROFILE_push(profiles, p);
89         } else {
90             SSLerr(SSL_F_SSL_CTX_MAKE_PROFILES,
91                    SSL_R_SRTP_UNKNOWN_PROTECTION_PROFILE);
92             sk_SRTP_PROTECTION_PROFILE_free(profiles);
93             return 1;
94         }
95
96         if (col)
97             ptr = col + 1;
98     } while (col);
99
100     sk_SRTP_PROTECTION_PROFILE_free(*out);
101
102     *out = profiles;
103
104     return 0;
105 }
106
107 int SSL_CTX_set_tlsext_use_srtp(SSL_CTX *ctx, const char *profiles)
108 {
109     return ssl_ctx_make_profiles(profiles, &ctx->srtp_profiles);
110 }
111
112 int SSL_set_tlsext_use_srtp(SSL *s, const char *profiles)
113 {
114     return ssl_ctx_make_profiles(profiles, &s->srtp_profiles);
115 }
116
117 STACK_OF(SRTP_PROTECTION_PROFILE) *SSL_get_srtp_profiles(SSL *s)
118 {
119     if (s != NULL) {
120         if (s->srtp_profiles != NULL) {
121             return s->srtp_profiles;
122         } else if ((s->ctx != NULL) && (s->ctx->srtp_profiles != NULL)) {
123             return s->ctx->srtp_profiles;
124         }
125     }
126
127     return NULL;
128 }
129
130 SRTP_PROTECTION_PROFILE *SSL_get_selected_srtp_profile(SSL *s)
131 {
132     return s->srtp_profile;
133 }
134
135 /*
136  * Note: this function returns 0 length if there are no profiles specified
137  */
138 int ssl_add_clienthello_use_srtp_ext(SSL *s, unsigned char *p, int *len,
139                                      int maxlen)
140 {
141     int ct = 0;
142     int i;
143     STACK_OF(SRTP_PROTECTION_PROFILE) *clnt = 0;
144     SRTP_PROTECTION_PROFILE *prof;
145
146     clnt = SSL_get_srtp_profiles(s);
147     ct = sk_SRTP_PROTECTION_PROFILE_num(clnt); /* -1 if clnt == 0 */
148
149     if (p) {
150         if (ct == 0) {
151             SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_USE_SRTP_EXT,
152                    SSL_R_EMPTY_SRTP_PROTECTION_PROFILE_LIST);
153             return 1;
154         }
155
156         if ((2 + ct * 2 + 1) > maxlen) {
157             SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_USE_SRTP_EXT,
158                    SSL_R_SRTP_PROTECTION_PROFILE_LIST_TOO_LONG);
159             return 1;
160         }
161
162         /* Add the length */
163         s2n(ct * 2, p);
164         for (i = 0; i < ct; i++) {
165             prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i);
166             s2n(prof->id, p);
167         }
168
169         /* Add an empty use_mki value */
170         *p++ = 0;
171     }
172
173     *len = 2 + ct * 2 + 1;
174
175     return 0;
176 }
177
178 int ssl_parse_clienthello_use_srtp_ext(SSL *s, PACKET *pkt, int *al)
179 {
180     SRTP_PROTECTION_PROFILE *sprof;
181     STACK_OF(SRTP_PROTECTION_PROFILE) *srvr;
182     unsigned int ct, mki_len, id;
183     int i, srtp_pref;
184     PACKET subpkt;
185
186     /* Pull off the length of the cipher suite list  and check it is even */
187     if (!PACKET_get_net_2(pkt, &ct)
188             || (ct & 1) != 0
189             || !PACKET_get_sub_packet(pkt, &subpkt, ct)) {
190         SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_USE_SRTP_EXT,
191                SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
192         *al = SSL_AD_DECODE_ERROR;
193         return 1;
194     }
195
196     srvr = SSL_get_srtp_profiles(s);
197     s->srtp_profile = NULL;
198     /* Search all profiles for a match initially */
199     srtp_pref = sk_SRTP_PROTECTION_PROFILE_num(srvr);
200
201     while (PACKET_remaining(&subpkt)) {
202         if (!PACKET_get_net_2(&subpkt, &id)) {
203             SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_USE_SRTP_EXT,
204                    SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
205             *al = SSL_AD_DECODE_ERROR;
206             return 1;
207         }
208
209         /*
210          * Only look for match in profiles of higher preference than
211          * current match.
212          * If no profiles have been have been configured then this
213          * does nothing.
214          */
215         for (i = 0; i < srtp_pref; i++) {
216             sprof = sk_SRTP_PROTECTION_PROFILE_value(srvr, i);
217             if (sprof->id == id) {
218                 s->srtp_profile = sprof;
219                 srtp_pref = i;
220                 break;
221             }
222         }
223     }
224
225     /*
226      * Now extract the MKI value as a sanity check, but discard it for now
227      */
228     if (!PACKET_get_1(pkt, &mki_len)) {
229         SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_USE_SRTP_EXT,
230                SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
231         *al = SSL_AD_DECODE_ERROR;
232         return 1;
233     }
234
235     if (!PACKET_forward(pkt, mki_len)
236             || PACKET_remaining(pkt)) {
237         SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_USE_SRTP_EXT,
238                SSL_R_BAD_SRTP_MKI_VALUE);
239         *al = SSL_AD_DECODE_ERROR;
240         return 1;
241     }
242
243     return 0;
244 }
245
246 int ssl_add_serverhello_use_srtp_ext(SSL *s, unsigned char *p, int *len,
247                                      int maxlen)
248 {
249     if (p) {
250         if (maxlen < 5) {
251             SSLerr(SSL_F_SSL_ADD_SERVERHELLO_USE_SRTP_EXT,
252                    SSL_R_SRTP_PROTECTION_PROFILE_LIST_TOO_LONG);
253             return 1;
254         }
255
256         if (s->srtp_profile == 0) {
257             SSLerr(SSL_F_SSL_ADD_SERVERHELLO_USE_SRTP_EXT,
258                    SSL_R_USE_SRTP_NOT_NEGOTIATED);
259             return 1;
260         }
261         s2n(2, p);
262         s2n(s->srtp_profile->id, p);
263         *p++ = 0;
264     }
265     *len = 5;
266
267     return 0;
268 }
269
270 int ssl_parse_serverhello_use_srtp_ext(SSL *s, PACKET *pkt, int *al)
271 {
272     unsigned int id, ct, mki;
273     int i;
274
275     STACK_OF(SRTP_PROTECTION_PROFILE) *clnt;
276     SRTP_PROTECTION_PROFILE *prof;
277
278     if (!PACKET_get_net_2(pkt, &ct)
279             || ct != 2
280             || !PACKET_get_net_2(pkt, &id)
281             || !PACKET_get_1(pkt, &mki)
282             || PACKET_remaining(pkt) != 0) {
283         SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_USE_SRTP_EXT,
284                SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
285         *al = SSL_AD_DECODE_ERROR;
286         return 1;
287     }
288
289     if (mki != 0) {
290         /* Must be no MKI, since we never offer one */
291         SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_USE_SRTP_EXT,
292                SSL_R_BAD_SRTP_MKI_VALUE);
293         *al = SSL_AD_ILLEGAL_PARAMETER;
294         return 1;
295     }
296
297     clnt = SSL_get_srtp_profiles(s);
298
299     /* Throw an error if the server gave us an unsolicited extension */
300     if (clnt == NULL) {
301         SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_USE_SRTP_EXT,
302                SSL_R_NO_SRTP_PROFILES);
303         *al = SSL_AD_DECODE_ERROR;
304         return 1;
305     }
306
307     /*
308      * Check to see if the server gave us something we support (and
309      * presumably offered)
310      */
311     for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(clnt); i++) {
312         prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i);
313
314         if (prof->id == id) {
315             s->srtp_profile = prof;
316             *al = 0;
317             return 0;
318         }
319     }
320
321     SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_USE_SRTP_EXT,
322            SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
323     *al = SSL_AD_DECODE_ERROR;
324     return 1;
325 }
326
327 #endif