xref: /openbsd-src/lib/libssl/d1_srtp.c (revision 71e048498e9e93d86d440a9a930c1c1566ea505d)
1*71e04849Sbeck /* $OpenBSD: d1_srtp.c,v 1.33 2023/07/08 16:40:13 beck Exp $ */
2ec07fdf1Sdjm /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3ec07fdf1Sdjm  * All rights reserved.
4ec07fdf1Sdjm  *
5ec07fdf1Sdjm  * This package is an SSL implementation written
6ec07fdf1Sdjm  * by Eric Young (eay@cryptsoft.com).
7ec07fdf1Sdjm  * The implementation was written so as to conform with Netscapes SSL.
8ec07fdf1Sdjm  *
9ec07fdf1Sdjm  * This library is free for commercial and non-commercial use as long as
10ec07fdf1Sdjm  * the following conditions are aheared to.  The following conditions
11ec07fdf1Sdjm  * apply to all code found in this distribution, be it the RC4, RSA,
12ec07fdf1Sdjm  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13ec07fdf1Sdjm  * included with this distribution is covered by the same copyright terms
14ec07fdf1Sdjm  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15ec07fdf1Sdjm  *
16ec07fdf1Sdjm  * Copyright remains Eric Young's, and as such any Copyright notices in
17ec07fdf1Sdjm  * the code are not to be removed.
18ec07fdf1Sdjm  * If this package is used in a product, Eric Young should be given attribution
19ec07fdf1Sdjm  * as the author of the parts of the library used.
20ec07fdf1Sdjm  * This can be in the form of a textual message at program startup or
21ec07fdf1Sdjm  * in documentation (online or textual) provided with the package.
22ec07fdf1Sdjm  *
23ec07fdf1Sdjm  * Redistribution and use in source and binary forms, with or without
24ec07fdf1Sdjm  * modification, are permitted provided that the following conditions
25ec07fdf1Sdjm  * are met:
26ec07fdf1Sdjm  * 1. Redistributions of source code must retain the copyright
27ec07fdf1Sdjm  *    notice, this list of conditions and the following disclaimer.
28ec07fdf1Sdjm  * 2. Redistributions in binary form must reproduce the above copyright
29ec07fdf1Sdjm  *    notice, this list of conditions and the following disclaimer in the
30ec07fdf1Sdjm  *    documentation and/or other materials provided with the distribution.
31ec07fdf1Sdjm  * 3. All advertising materials mentioning features or use of this software
32ec07fdf1Sdjm  *    must display the following acknowledgement:
33ec07fdf1Sdjm  *    "This product includes cryptographic software written by
34ec07fdf1Sdjm  *     Eric Young (eay@cryptsoft.com)"
35ec07fdf1Sdjm  *    The word 'cryptographic' can be left out if the rouines from the library
36ec07fdf1Sdjm  *    being used are not cryptographic related :-).
37ec07fdf1Sdjm  * 4. If you include any Windows specific code (or a derivative thereof) from
38ec07fdf1Sdjm  *    the apps directory (application code) you must include an acknowledgement:
39ec07fdf1Sdjm  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40ec07fdf1Sdjm  *
41ec07fdf1Sdjm  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42ec07fdf1Sdjm  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ec07fdf1Sdjm  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44ec07fdf1Sdjm  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45ec07fdf1Sdjm  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46ec07fdf1Sdjm  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47ec07fdf1Sdjm  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ec07fdf1Sdjm  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49ec07fdf1Sdjm  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50ec07fdf1Sdjm  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51ec07fdf1Sdjm  * SUCH DAMAGE.
52ec07fdf1Sdjm  *
53ec07fdf1Sdjm  * The licence and distribution terms for any publically available version or
54ec07fdf1Sdjm  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55ec07fdf1Sdjm  * copied and put under another distribution licence
56ec07fdf1Sdjm  * [including the GNU Public Licence.]
57ec07fdf1Sdjm  */
58ec07fdf1Sdjm /* ====================================================================
59ec07fdf1Sdjm  * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
60ec07fdf1Sdjm  *
61ec07fdf1Sdjm  * Redistribution and use in source and binary forms, with or without
62ec07fdf1Sdjm  * modification, are permitted provided that the following conditions
63ec07fdf1Sdjm  * are met:
64ec07fdf1Sdjm  *
65ec07fdf1Sdjm  * 1. Redistributions of source code must retain the above copyright
66ec07fdf1Sdjm  *    notice, this list of conditions and the following disclaimer.
67ec07fdf1Sdjm  *
68ec07fdf1Sdjm  * 2. Redistributions in binary form must reproduce the above copyright
69ec07fdf1Sdjm  *    notice, this list of conditions and the following disclaimer in
70ec07fdf1Sdjm  *    the documentation and/or other materials provided with the
71ec07fdf1Sdjm  *    distribution.
72ec07fdf1Sdjm  *
73ec07fdf1Sdjm  * 3. All advertising materials mentioning features or use of this
74ec07fdf1Sdjm  *    software must display the following acknowledgment:
75ec07fdf1Sdjm  *    "This product includes software developed by the OpenSSL Project
76ec07fdf1Sdjm  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77ec07fdf1Sdjm  *
78ec07fdf1Sdjm  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79ec07fdf1Sdjm  *    endorse or promote products derived from this software without
80ec07fdf1Sdjm  *    prior written permission. For written permission, please contact
81ec07fdf1Sdjm  *    openssl-core@openssl.org.
82ec07fdf1Sdjm  *
83ec07fdf1Sdjm  * 5. Products derived from this software may not be called "OpenSSL"
84ec07fdf1Sdjm  *    nor may "OpenSSL" appear in their names without prior written
85ec07fdf1Sdjm  *    permission of the OpenSSL Project.
86ec07fdf1Sdjm  *
87ec07fdf1Sdjm  * 6. Redistributions of any form whatsoever must retain the following
88ec07fdf1Sdjm  *    acknowledgment:
89ec07fdf1Sdjm  *    "This product includes software developed by the OpenSSL Project
90ec07fdf1Sdjm  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91ec07fdf1Sdjm  *
92ec07fdf1Sdjm  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93ec07fdf1Sdjm  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94ec07fdf1Sdjm  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95ec07fdf1Sdjm  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96ec07fdf1Sdjm  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97ec07fdf1Sdjm  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98ec07fdf1Sdjm  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99ec07fdf1Sdjm  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100ec07fdf1Sdjm  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101ec07fdf1Sdjm  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102ec07fdf1Sdjm  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103ec07fdf1Sdjm  * OF THE POSSIBILITY OF SUCH DAMAGE.
104ec07fdf1Sdjm  * ====================================================================
105ec07fdf1Sdjm  *
106ec07fdf1Sdjm  * This product includes cryptographic software written by Eric Young
107ec07fdf1Sdjm  * (eay@cryptsoft.com).  This product includes software written by Tim
108ec07fdf1Sdjm  * Hudson (tjh@cryptsoft.com).
109ec07fdf1Sdjm  *
110ec07fdf1Sdjm  */
111ec07fdf1Sdjm /*
112dbea66cdSguenther  * DTLS code by Eric Rescorla <ekr@rtfm.com>
113dbea66cdSguenther  *
114dbea66cdSguenther  * Copyright (C) 2006, Network Resonance, Inc.
115dbea66cdSguenther  * Copyright (C) 2011, RTFM, Inc.
116ec07fdf1Sdjm  */
117ec07fdf1Sdjm 
118ec07fdf1Sdjm #include <stdio.h>
119c5899dbcSjsing 
120ec07fdf1Sdjm #include <openssl/objects.h>
121f4dd87b5Sjsing #include <openssl/opensslconf.h>
122c5899dbcSjsing 
1239eac5592Smiod #ifndef OPENSSL_NO_SRTP
1249eac5592Smiod 
125e5a389e3Sdoug #include "bytestring.h"
126c9675a23Stb #include "dtls_local.h"
127c9675a23Stb #include "ssl_local.h"
128ec07fdf1Sdjm #include "srtp.h"
129ec07fdf1Sdjm 
13006ea081eSguenther static const SRTP_PROTECTION_PROFILE srtp_known_profiles[] = {
131ec07fdf1Sdjm 	{
132ec07fdf1Sdjm 		"SRTP_AES128_CM_SHA1_80",
133ec07fdf1Sdjm 		SRTP_AES128_CM_SHA1_80,
134ec07fdf1Sdjm 	},
135ec07fdf1Sdjm 	{
136ec07fdf1Sdjm 		"SRTP_AES128_CM_SHA1_32",
137ec07fdf1Sdjm 		SRTP_AES128_CM_SHA1_32,
138ec07fdf1Sdjm 	},
139d10f9851Slandry 	{
140d10f9851Slandry 		"SRTP_AEAD_AES_128_GCM",
141d10f9851Slandry 		SRTP_AEAD_AES_128_GCM,
142d10f9851Slandry 	},
143d10f9851Slandry 	{
144d10f9851Slandry 		"SRTP_AEAD_AES_256_GCM",
145d10f9851Slandry 		SRTP_AEAD_AES_256_GCM,
146d10f9851Slandry 	},
147ec07fdf1Sdjm 	{0}
148ec07fdf1Sdjm };
149ec07fdf1Sdjm 
150e7660786Sdoug int
srtp_find_profile_by_name(const char * profile_name,const SRTP_PROTECTION_PROFILE ** pptr,unsigned int len)151a522ba32Stb srtp_find_profile_by_name(const char *profile_name,
15206ea081eSguenther     const SRTP_PROTECTION_PROFILE **pptr, unsigned int len)
153ec07fdf1Sdjm {
15406ea081eSguenther 	const SRTP_PROTECTION_PROFILE *p;
155ec07fdf1Sdjm 
156ec07fdf1Sdjm 	p = srtp_known_profiles;
1574a47b859Sjsing 	while (p->name) {
1584a47b859Sjsing 		if ((len == strlen(p->name)) &&
1594a47b859Sjsing 		    !strncmp(p->name, profile_name, len)) {
160ec07fdf1Sdjm 			*pptr = p;
161ec07fdf1Sdjm 			return 0;
162ec07fdf1Sdjm 		}
163ec07fdf1Sdjm 
164ec07fdf1Sdjm 		p++;
165ec07fdf1Sdjm 	}
166ec07fdf1Sdjm 
167ec07fdf1Sdjm 	return 1;
168ec07fdf1Sdjm }
169ec07fdf1Sdjm 
170e7660786Sdoug int
srtp_find_profile_by_num(unsigned int profile_num,const SRTP_PROTECTION_PROFILE ** pptr)171ab91a68dStb srtp_find_profile_by_num(unsigned int profile_num,
17206ea081eSguenther     const SRTP_PROTECTION_PROFILE **pptr)
173ec07fdf1Sdjm {
17406ea081eSguenther 	const SRTP_PROTECTION_PROFILE *p;
175ec07fdf1Sdjm 
176ec07fdf1Sdjm 	p = srtp_known_profiles;
1774a47b859Sjsing 	while (p->name) {
1784a47b859Sjsing 		if (p->id == profile_num) {
179ec07fdf1Sdjm 			*pptr = p;
180ec07fdf1Sdjm 			return 0;
181ec07fdf1Sdjm 		}
182ec07fdf1Sdjm 		p++;
183ec07fdf1Sdjm 	}
184ec07fdf1Sdjm 
185ec07fdf1Sdjm 	return 1;
186ec07fdf1Sdjm }
187ec07fdf1Sdjm 
1884a47b859Sjsing static int
ssl_ctx_make_profiles(const char * profiles_string,STACK_OF (SRTP_PROTECTION_PROFILE)** out)1894a47b859Sjsing ssl_ctx_make_profiles(const char *profiles_string,
1904a47b859Sjsing     STACK_OF(SRTP_PROTECTION_PROFILE) **out)
191ec07fdf1Sdjm {
192ec07fdf1Sdjm 	STACK_OF(SRTP_PROTECTION_PROFILE) *profiles;
193ec07fdf1Sdjm 	char *col;
194a522ba32Stb 	const char *ptr = profiles_string;
19506ea081eSguenther 	const SRTP_PROTECTION_PROFILE *p;
196ec07fdf1Sdjm 
1974a47b859Sjsing 	if (!(profiles = sk_SRTP_PROTECTION_PROFILE_new_null())) {
198c9d7abb7Sbeck 		SSLerrorx(SSL_R_SRTP_COULD_NOT_ALLOCATE_PROFILES);
199ec07fdf1Sdjm 		return 1;
200ec07fdf1Sdjm 	}
201ec07fdf1Sdjm 
2024a47b859Sjsing 	do {
203ec07fdf1Sdjm 		col = strchr(ptr, ':');
204ec07fdf1Sdjm 
205e7660786Sdoug 		if (!srtp_find_profile_by_name(ptr, &p,
2064a47b859Sjsing 		    col ? col - ptr : (int)strlen(ptr))) {
20710134b5aSinoguchi 			if (!sk_SRTP_PROTECTION_PROFILE_push(profiles, p)) {
20810134b5aSinoguchi 				sk_SRTP_PROTECTION_PROFILE_free(profiles);
20910134b5aSinoguchi 				return 1;
21010134b5aSinoguchi 			}
2114a47b859Sjsing 		} else {
212c9d7abb7Sbeck 			SSLerrorx(SSL_R_SRTP_UNKNOWN_PROTECTION_PROFILE);
21334397580Slogan 			sk_SRTP_PROTECTION_PROFILE_free(profiles);
214ec07fdf1Sdjm 			return 1;
215ec07fdf1Sdjm 		}
216ec07fdf1Sdjm 
2174a47b859Sjsing 		if (col)
2184a47b859Sjsing 			ptr = col + 1;
219ec07fdf1Sdjm 	} while (col);
220ec07fdf1Sdjm 
2210471029aStb 	sk_SRTP_PROTECTION_PROFILE_free(*out);
222ec07fdf1Sdjm 	*out = profiles;
223ec07fdf1Sdjm 
224ec07fdf1Sdjm 	return 0;
225ec07fdf1Sdjm }
226ec07fdf1Sdjm 
2274a47b859Sjsing int
SSL_CTX_set_tlsext_use_srtp(SSL_CTX * ctx,const char * profiles)2284a47b859Sjsing SSL_CTX_set_tlsext_use_srtp(SSL_CTX *ctx, const char *profiles)
229ec07fdf1Sdjm {
2306f7f653bSjsing 	return ssl_ctx_make_profiles(profiles, &ctx->srtp_profiles);
231ec07fdf1Sdjm }
232*71e04849Sbeck LSSL_ALIAS(SSL_CTX_set_tlsext_use_srtp);
233ec07fdf1Sdjm 
2344a47b859Sjsing int
SSL_set_tlsext_use_srtp(SSL * s,const char * profiles)2354a47b859Sjsing SSL_set_tlsext_use_srtp(SSL *s, const char *profiles)
236ec07fdf1Sdjm {
2376f7f653bSjsing 	return ssl_ctx_make_profiles(profiles, &s->srtp_profiles);
238ec07fdf1Sdjm }
239*71e04849Sbeck LSSL_ALIAS(SSL_set_tlsext_use_srtp);
240ec07fdf1Sdjm 
241ec07fdf1Sdjm 
STACK_OF(SRTP_PROTECTION_PROFILE)242dbea66cdSguenther STACK_OF(SRTP_PROTECTION_PROFILE) *
243dbea66cdSguenther SSL_get_srtp_profiles(SSL *s)
244ec07fdf1Sdjm {
2454a47b859Sjsing 	if (s != NULL) {
2466f7f653bSjsing 		if (s->srtp_profiles != NULL) {
2476f7f653bSjsing 			return s->srtp_profiles;
2484a47b859Sjsing 		} else if ((s->ctx != NULL) &&
2496f7f653bSjsing 		    (s->ctx->srtp_profiles != NULL)) {
2506f7f653bSjsing 			return s->ctx->srtp_profiles;
251ec07fdf1Sdjm 		}
252ec07fdf1Sdjm 	}
253ec07fdf1Sdjm 
254ec07fdf1Sdjm 	return NULL;
255ec07fdf1Sdjm }
256*71e04849Sbeck LSSL_ALIAS(SSL_get_srtp_profiles);
257ec07fdf1Sdjm 
258dbea66cdSguenther SRTP_PROTECTION_PROFILE *
SSL_get_selected_srtp_profile(SSL * s)259dbea66cdSguenther SSL_get_selected_srtp_profile(SSL *s)
260ec07fdf1Sdjm {
26106ea081eSguenther 	/* XXX cast away the const */
2626f7f653bSjsing 	return (SRTP_PROTECTION_PROFILE *)s->srtp_profile;
263ec07fdf1Sdjm }
264*71e04849Sbeck LSSL_ALIAS(SSL_get_selected_srtp_profile);
265ec07fdf1Sdjm 
266ec07fdf1Sdjm #endif
267