1*de0e0e4dSAntonio Huete Jimenez /* $OpenBSD: d1_srtp.c,v 1.30 2022/01/28 13:11:56 inoguchi Exp $ */
2f5b1c8a1SJohn Marino /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3f5b1c8a1SJohn Marino * All rights reserved.
4f5b1c8a1SJohn Marino *
5f5b1c8a1SJohn Marino * This package is an SSL implementation written
6f5b1c8a1SJohn Marino * by Eric Young (eay@cryptsoft.com).
7f5b1c8a1SJohn Marino * The implementation was written so as to conform with Netscapes SSL.
8f5b1c8a1SJohn Marino *
9f5b1c8a1SJohn Marino * This library is free for commercial and non-commercial use as long as
10f5b1c8a1SJohn Marino * the following conditions are aheared to. The following conditions
11f5b1c8a1SJohn Marino * apply to all code found in this distribution, be it the RC4, RSA,
12f5b1c8a1SJohn Marino * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13f5b1c8a1SJohn Marino * included with this distribution is covered by the same copyright terms
14f5b1c8a1SJohn Marino * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15f5b1c8a1SJohn Marino *
16f5b1c8a1SJohn Marino * Copyright remains Eric Young's, and as such any Copyright notices in
17f5b1c8a1SJohn Marino * the code are not to be removed.
18f5b1c8a1SJohn Marino * If this package is used in a product, Eric Young should be given attribution
19f5b1c8a1SJohn Marino * as the author of the parts of the library used.
20f5b1c8a1SJohn Marino * This can be in the form of a textual message at program startup or
21f5b1c8a1SJohn Marino * in documentation (online or textual) provided with the package.
22f5b1c8a1SJohn Marino *
23f5b1c8a1SJohn Marino * Redistribution and use in source and binary forms, with or without
24f5b1c8a1SJohn Marino * modification, are permitted provided that the following conditions
25f5b1c8a1SJohn Marino * are met:
26f5b1c8a1SJohn Marino * 1. Redistributions of source code must retain the copyright
27f5b1c8a1SJohn Marino * notice, this list of conditions and the following disclaimer.
28f5b1c8a1SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
29f5b1c8a1SJohn Marino * notice, this list of conditions and the following disclaimer in the
30f5b1c8a1SJohn Marino * documentation and/or other materials provided with the distribution.
31f5b1c8a1SJohn Marino * 3. All advertising materials mentioning features or use of this software
32f5b1c8a1SJohn Marino * must display the following acknowledgement:
33f5b1c8a1SJohn Marino * "This product includes cryptographic software written by
34f5b1c8a1SJohn Marino * Eric Young (eay@cryptsoft.com)"
35f5b1c8a1SJohn Marino * The word 'cryptographic' can be left out if the rouines from the library
36f5b1c8a1SJohn Marino * being used are not cryptographic related :-).
37f5b1c8a1SJohn Marino * 4. If you include any Windows specific code (or a derivative thereof) from
38f5b1c8a1SJohn Marino * the apps directory (application code) you must include an acknowledgement:
39f5b1c8a1SJohn Marino * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40f5b1c8a1SJohn Marino *
41f5b1c8a1SJohn Marino * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42f5b1c8a1SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43f5b1c8a1SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44f5b1c8a1SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45f5b1c8a1SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46f5b1c8a1SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47f5b1c8a1SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48f5b1c8a1SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49f5b1c8a1SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50f5b1c8a1SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51f5b1c8a1SJohn Marino * SUCH DAMAGE.
52f5b1c8a1SJohn Marino *
53f5b1c8a1SJohn Marino * The licence and distribution terms for any publically available version or
54f5b1c8a1SJohn Marino * derivative of this code cannot be changed. i.e. this code cannot simply be
55f5b1c8a1SJohn Marino * copied and put under another distribution licence
56f5b1c8a1SJohn Marino * [including the GNU Public Licence.]
57f5b1c8a1SJohn Marino */
58f5b1c8a1SJohn Marino /* ====================================================================
59f5b1c8a1SJohn Marino * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
60f5b1c8a1SJohn Marino *
61f5b1c8a1SJohn Marino * Redistribution and use in source and binary forms, with or without
62f5b1c8a1SJohn Marino * modification, are permitted provided that the following conditions
63f5b1c8a1SJohn Marino * are met:
64f5b1c8a1SJohn Marino *
65f5b1c8a1SJohn Marino * 1. Redistributions of source code must retain the above copyright
66f5b1c8a1SJohn Marino * notice, this list of conditions and the following disclaimer.
67f5b1c8a1SJohn Marino *
68f5b1c8a1SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
69f5b1c8a1SJohn Marino * notice, this list of conditions and the following disclaimer in
70f5b1c8a1SJohn Marino * the documentation and/or other materials provided with the
71f5b1c8a1SJohn Marino * distribution.
72f5b1c8a1SJohn Marino *
73f5b1c8a1SJohn Marino * 3. All advertising materials mentioning features or use of this
74f5b1c8a1SJohn Marino * software must display the following acknowledgment:
75f5b1c8a1SJohn Marino * "This product includes software developed by the OpenSSL Project
76f5b1c8a1SJohn Marino * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77f5b1c8a1SJohn Marino *
78f5b1c8a1SJohn Marino * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79f5b1c8a1SJohn Marino * endorse or promote products derived from this software without
80f5b1c8a1SJohn Marino * prior written permission. For written permission, please contact
81f5b1c8a1SJohn Marino * openssl-core@openssl.org.
82f5b1c8a1SJohn Marino *
83f5b1c8a1SJohn Marino * 5. Products derived from this software may not be called "OpenSSL"
84f5b1c8a1SJohn Marino * nor may "OpenSSL" appear in their names without prior written
85f5b1c8a1SJohn Marino * permission of the OpenSSL Project.
86f5b1c8a1SJohn Marino *
87f5b1c8a1SJohn Marino * 6. Redistributions of any form whatsoever must retain the following
88f5b1c8a1SJohn Marino * acknowledgment:
89f5b1c8a1SJohn Marino * "This product includes software developed by the OpenSSL Project
90f5b1c8a1SJohn Marino * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91f5b1c8a1SJohn Marino *
92f5b1c8a1SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93f5b1c8a1SJohn Marino * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94f5b1c8a1SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95f5b1c8a1SJohn Marino * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96f5b1c8a1SJohn Marino * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97f5b1c8a1SJohn Marino * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98f5b1c8a1SJohn Marino * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99f5b1c8a1SJohn Marino * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100f5b1c8a1SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101f5b1c8a1SJohn Marino * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102f5b1c8a1SJohn Marino * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103f5b1c8a1SJohn Marino * OF THE POSSIBILITY OF SUCH DAMAGE.
104f5b1c8a1SJohn Marino * ====================================================================
105f5b1c8a1SJohn Marino *
106f5b1c8a1SJohn Marino * This product includes cryptographic software written by Eric Young
107f5b1c8a1SJohn Marino * (eay@cryptsoft.com). This product includes software written by Tim
108f5b1c8a1SJohn Marino * Hudson (tjh@cryptsoft.com).
109f5b1c8a1SJohn Marino *
110f5b1c8a1SJohn Marino */
111f5b1c8a1SJohn Marino /*
112f5b1c8a1SJohn Marino * DTLS code by Eric Rescorla <ekr@rtfm.com>
113f5b1c8a1SJohn Marino *
114f5b1c8a1SJohn Marino * Copyright (C) 2006, Network Resonance, Inc.
115f5b1c8a1SJohn Marino * Copyright (C) 2011, RTFM, Inc.
116f5b1c8a1SJohn Marino */
117f5b1c8a1SJohn Marino
118f5b1c8a1SJohn Marino #include <stdio.h>
119f5b1c8a1SJohn Marino
120f5b1c8a1SJohn Marino #include <openssl/objects.h>
121*de0e0e4dSAntonio Huete Jimenez #include <openssl/opensslconf.h>
122f5b1c8a1SJohn Marino
123f5b1c8a1SJohn Marino #ifndef OPENSSL_NO_SRTP
124f5b1c8a1SJohn Marino
125f5b1c8a1SJohn Marino #include "bytestring.h"
126*de0e0e4dSAntonio Huete Jimenez #include "dtls_locl.h"
127*de0e0e4dSAntonio Huete Jimenez #include "ssl_locl.h"
128f5b1c8a1SJohn Marino #include "srtp.h"
129f5b1c8a1SJohn Marino
130*de0e0e4dSAntonio Huete Jimenez static const SRTP_PROTECTION_PROFILE srtp_known_profiles[] = {
131f5b1c8a1SJohn Marino {
132f5b1c8a1SJohn Marino "SRTP_AES128_CM_SHA1_80",
133f5b1c8a1SJohn Marino SRTP_AES128_CM_SHA1_80,
134f5b1c8a1SJohn Marino },
135f5b1c8a1SJohn Marino {
136f5b1c8a1SJohn Marino "SRTP_AES128_CM_SHA1_32",
137f5b1c8a1SJohn Marino SRTP_AES128_CM_SHA1_32,
138f5b1c8a1SJohn Marino },
139*de0e0e4dSAntonio Huete Jimenez {
140*de0e0e4dSAntonio Huete Jimenez "SRTP_AEAD_AES_128_GCM",
141*de0e0e4dSAntonio Huete Jimenez SRTP_AEAD_AES_128_GCM,
142*de0e0e4dSAntonio Huete Jimenez },
143*de0e0e4dSAntonio Huete Jimenez {
144*de0e0e4dSAntonio Huete Jimenez "SRTP_AEAD_AES_256_GCM",
145*de0e0e4dSAntonio Huete Jimenez SRTP_AEAD_AES_256_GCM,
146*de0e0e4dSAntonio Huete Jimenez },
147f5b1c8a1SJohn Marino {0}
148f5b1c8a1SJohn Marino };
149f5b1c8a1SJohn Marino
15072c33676SMaxim Ag int
srtp_find_profile_by_name(const char * profile_name,const SRTP_PROTECTION_PROFILE ** pptr,unsigned int len)151*de0e0e4dSAntonio Huete Jimenez srtp_find_profile_by_name(const char *profile_name,
152*de0e0e4dSAntonio Huete Jimenez const SRTP_PROTECTION_PROFILE **pptr, unsigned int len)
153f5b1c8a1SJohn Marino {
154*de0e0e4dSAntonio Huete Jimenez const SRTP_PROTECTION_PROFILE *p;
155f5b1c8a1SJohn Marino
156f5b1c8a1SJohn Marino p = srtp_known_profiles;
157f5b1c8a1SJohn Marino while (p->name) {
158f5b1c8a1SJohn Marino if ((len == strlen(p->name)) &&
159f5b1c8a1SJohn Marino !strncmp(p->name, profile_name, len)) {
160f5b1c8a1SJohn Marino *pptr = p;
161f5b1c8a1SJohn Marino return 0;
162f5b1c8a1SJohn Marino }
163f5b1c8a1SJohn Marino
164f5b1c8a1SJohn Marino p++;
165f5b1c8a1SJohn Marino }
166f5b1c8a1SJohn Marino
167f5b1c8a1SJohn Marino return 1;
168f5b1c8a1SJohn Marino }
169f5b1c8a1SJohn Marino
17072c33676SMaxim Ag int
srtp_find_profile_by_num(unsigned int profile_num,const SRTP_PROTECTION_PROFILE ** pptr)171cca6fc52SDaniel Fojt srtp_find_profile_by_num(unsigned int profile_num,
172*de0e0e4dSAntonio Huete Jimenez const SRTP_PROTECTION_PROFILE **pptr)
173f5b1c8a1SJohn Marino {
174*de0e0e4dSAntonio Huete Jimenez const SRTP_PROTECTION_PROFILE *p;
175f5b1c8a1SJohn Marino
176f5b1c8a1SJohn Marino p = srtp_known_profiles;
177f5b1c8a1SJohn Marino while (p->name) {
178f5b1c8a1SJohn Marino if (p->id == profile_num) {
179f5b1c8a1SJohn Marino *pptr = p;
180f5b1c8a1SJohn Marino return 0;
181f5b1c8a1SJohn Marino }
182f5b1c8a1SJohn Marino p++;
183f5b1c8a1SJohn Marino }
184f5b1c8a1SJohn Marino
185f5b1c8a1SJohn Marino return 1;
186f5b1c8a1SJohn Marino }
187f5b1c8a1SJohn Marino
188f5b1c8a1SJohn Marino static int
ssl_ctx_make_profiles(const char * profiles_string,STACK_OF (SRTP_PROTECTION_PROFILE)** out)189f5b1c8a1SJohn Marino ssl_ctx_make_profiles(const char *profiles_string,
190f5b1c8a1SJohn Marino STACK_OF(SRTP_PROTECTION_PROFILE) **out)
191f5b1c8a1SJohn Marino {
192f5b1c8a1SJohn Marino STACK_OF(SRTP_PROTECTION_PROFILE) *profiles;
193f5b1c8a1SJohn Marino char *col;
194*de0e0e4dSAntonio Huete Jimenez const char *ptr = profiles_string;
195*de0e0e4dSAntonio Huete Jimenez const SRTP_PROTECTION_PROFILE *p;
196f5b1c8a1SJohn Marino
197f5b1c8a1SJohn Marino if (!(profiles = sk_SRTP_PROTECTION_PROFILE_new_null())) {
19872c33676SMaxim Ag SSLerrorx(SSL_R_SRTP_COULD_NOT_ALLOCATE_PROFILES);
199f5b1c8a1SJohn Marino return 1;
200f5b1c8a1SJohn Marino }
201f5b1c8a1SJohn Marino
202f5b1c8a1SJohn Marino do {
203f5b1c8a1SJohn Marino col = strchr(ptr, ':');
204f5b1c8a1SJohn Marino
20572c33676SMaxim Ag if (!srtp_find_profile_by_name(ptr, &p,
206f5b1c8a1SJohn Marino col ? col - ptr : (int)strlen(ptr))) {
207*de0e0e4dSAntonio Huete Jimenez if (!sk_SRTP_PROTECTION_PROFILE_push(profiles, p)) {
208*de0e0e4dSAntonio Huete Jimenez sk_SRTP_PROTECTION_PROFILE_free(profiles);
209*de0e0e4dSAntonio Huete Jimenez return 1;
210*de0e0e4dSAntonio Huete Jimenez }
211f5b1c8a1SJohn Marino } else {
21272c33676SMaxim Ag SSLerrorx(SSL_R_SRTP_UNKNOWN_PROTECTION_PROFILE);
213f5b1c8a1SJohn Marino sk_SRTP_PROTECTION_PROFILE_free(profiles);
214f5b1c8a1SJohn Marino return 1;
215f5b1c8a1SJohn Marino }
216f5b1c8a1SJohn Marino
217f5b1c8a1SJohn Marino if (col)
218f5b1c8a1SJohn Marino ptr = col + 1;
219f5b1c8a1SJohn Marino } while (col);
220f5b1c8a1SJohn Marino
22172c33676SMaxim Ag sk_SRTP_PROTECTION_PROFILE_free(*out);
222f5b1c8a1SJohn Marino *out = profiles;
223f5b1c8a1SJohn Marino
224f5b1c8a1SJohn Marino return 0;
225f5b1c8a1SJohn Marino }
226f5b1c8a1SJohn Marino
227f5b1c8a1SJohn Marino int
SSL_CTX_set_tlsext_use_srtp(SSL_CTX * ctx,const char * profiles)228f5b1c8a1SJohn Marino SSL_CTX_set_tlsext_use_srtp(SSL_CTX *ctx, const char *profiles)
229f5b1c8a1SJohn Marino {
23072c33676SMaxim Ag return ssl_ctx_make_profiles(profiles, &ctx->internal->srtp_profiles);
231f5b1c8a1SJohn Marino }
232f5b1c8a1SJohn Marino
233f5b1c8a1SJohn Marino int
SSL_set_tlsext_use_srtp(SSL * s,const char * profiles)234f5b1c8a1SJohn Marino SSL_set_tlsext_use_srtp(SSL *s, const char *profiles)
235f5b1c8a1SJohn Marino {
23672c33676SMaxim Ag return ssl_ctx_make_profiles(profiles, &s->internal->srtp_profiles);
237f5b1c8a1SJohn Marino }
238f5b1c8a1SJohn Marino
239f5b1c8a1SJohn Marino
STACK_OF(SRTP_PROTECTION_PROFILE)240f5b1c8a1SJohn Marino STACK_OF(SRTP_PROTECTION_PROFILE) *
241f5b1c8a1SJohn Marino SSL_get_srtp_profiles(SSL *s)
242f5b1c8a1SJohn Marino {
243f5b1c8a1SJohn Marino if (s != NULL) {
24472c33676SMaxim Ag if (s->internal->srtp_profiles != NULL) {
24572c33676SMaxim Ag return s->internal->srtp_profiles;
246f5b1c8a1SJohn Marino } else if ((s->ctx != NULL) &&
24772c33676SMaxim Ag (s->ctx->internal->srtp_profiles != NULL)) {
24872c33676SMaxim Ag return s->ctx->internal->srtp_profiles;
249f5b1c8a1SJohn Marino }
250f5b1c8a1SJohn Marino }
251f5b1c8a1SJohn Marino
252f5b1c8a1SJohn Marino return NULL;
253f5b1c8a1SJohn Marino }
254f5b1c8a1SJohn Marino
255f5b1c8a1SJohn Marino SRTP_PROTECTION_PROFILE *
SSL_get_selected_srtp_profile(SSL * s)256f5b1c8a1SJohn Marino SSL_get_selected_srtp_profile(SSL *s)
257f5b1c8a1SJohn Marino {
258*de0e0e4dSAntonio Huete Jimenez /* XXX cast away the const */
259*de0e0e4dSAntonio Huete Jimenez return (SRTP_PROTECTION_PROFILE *)s->internal->srtp_profile;
260f5b1c8a1SJohn Marino }
261f5b1c8a1SJohn Marino
262f5b1c8a1SJohn Marino #endif
263