2 * Copyright 2011-2024 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
11 * DTLS code by Eric Rescorla <ekr@rtfm.com>
13 * Copyright (C) 2006, Network Resonance, Inc. Copyright (C) 2011, RTFM, Inc.
17 #include <openssl/objects.h>
18 #include "ssl_local.h"
19 #include "quic/quic_local.h"
21 #ifndef OPENSSL_NO_SRTP
23 static const SRTP_PROTECTION_PROFILE srtp_known_profiles[] = {
25 "SRTP_AES128_CM_SHA1_80",
26 SRTP_AES128_CM_SHA1_80,
29 "SRTP_AES128_CM_SHA1_32",
30 SRTP_AES128_CM_SHA1_32,
33 "SRTP_AEAD_AES_128_GCM",
34 SRTP_AEAD_AES_128_GCM,
37 "SRTP_AEAD_AES_256_GCM",
38 SRTP_AEAD_AES_256_GCM,
41 "SRTP_DOUBLE_AEAD_AES_128_GCM_AEAD_AES_128_GCM",
42 SRTP_DOUBLE_AEAD_AES_128_GCM_AEAD_AES_128_GCM,
45 "SRTP_DOUBLE_AEAD_AES_256_GCM_AEAD_AES_256_GCM",
46 SRTP_DOUBLE_AEAD_AES_256_GCM_AEAD_AES_256_GCM,
49 "SRTP_ARIA_128_CTR_HMAC_SHA1_80",
50 SRTP_ARIA_128_CTR_HMAC_SHA1_80,
53 "SRTP_ARIA_128_CTR_HMAC_SHA1_32",
54 SRTP_ARIA_128_CTR_HMAC_SHA1_32,
57 "SRTP_ARIA_256_CTR_HMAC_SHA1_80",
58 SRTP_ARIA_256_CTR_HMAC_SHA1_80,
61 "SRTP_ARIA_256_CTR_HMAC_SHA1_32",
62 SRTP_ARIA_256_CTR_HMAC_SHA1_32,
65 "SRTP_AEAD_ARIA_128_GCM",
66 SRTP_AEAD_ARIA_128_GCM,
69 "SRTP_AEAD_ARIA_256_GCM",
70 SRTP_AEAD_ARIA_256_GCM,
75 static int find_profile_by_name(char *profile_name,
76 const SRTP_PROTECTION_PROFILE **pptr, size_t len)
78 const SRTP_PROTECTION_PROFILE *p;
80 p = srtp_known_profiles;
82 if ((len == strlen(p->name))
83 && strncmp(p->name, profile_name, len) == 0) {
94 static int ssl_ctx_make_profiles(const char *profiles_string,
95 STACK_OF(SRTP_PROTECTION_PROFILE) **out)
97 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles;
100 char *ptr = (char *)profiles_string;
101 const SRTP_PROTECTION_PROFILE *p;
103 if ((profiles = sk_SRTP_PROTECTION_PROFILE_new_null()) == NULL) {
104 ERR_raise(ERR_LIB_SSL, SSL_R_SRTP_COULD_NOT_ALLOCATE_PROFILES);
109 col = strchr(ptr, ':');
111 if (!find_profile_by_name(ptr, &p, col ? (size_t)(col - ptr)
113 if (sk_SRTP_PROTECTION_PROFILE_find(profiles,
114 (SRTP_PROTECTION_PROFILE *)p) >= 0) {
115 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
119 if (!sk_SRTP_PROTECTION_PROFILE_push(profiles,
120 (SRTP_PROTECTION_PROFILE *)p)) {
121 ERR_raise(ERR_LIB_SSL, SSL_R_SRTP_COULD_NOT_ALLOCATE_PROFILES);
125 ERR_raise(ERR_LIB_SSL, SSL_R_SRTP_UNKNOWN_PROTECTION_PROFILE);
133 sk_SRTP_PROTECTION_PROFILE_free(*out);
139 sk_SRTP_PROTECTION_PROFILE_free(profiles);
143 int SSL_CTX_set_tlsext_use_srtp(SSL_CTX *ctx, const char *profiles)
145 if (IS_QUIC_METHOD(ctx->method))
148 return ssl_ctx_make_profiles(profiles, &ctx->srtp_profiles);
151 int SSL_set_tlsext_use_srtp(SSL *s, const char *profiles)
153 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
158 return ssl_ctx_make_profiles(profiles, &sc->srtp_profiles);
161 STACK_OF(SRTP_PROTECTION_PROFILE) *SSL_get_srtp_profiles(SSL *s)
163 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
166 if (sc->srtp_profiles != NULL) {
167 return sc->srtp_profiles;
168 } else if ((s->ctx != NULL) && (s->ctx->srtp_profiles != NULL)) {
169 return s->ctx->srtp_profiles;
176 SRTP_PROTECTION_PROFILE *SSL_get_selected_srtp_profile(SSL *s)
178 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
183 return sc->srtp_profile;