| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) |
| 2 |
// Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. |
| 3 |
// DTLS code by Eric Rescorla <[email protected]> |
| 4 |
// Copyright (C) 2006, Network Resonance, Inc. |
| 5 |
// Copyright (C) 2011, RTFM, Inc. |
| 6 |
// SPDX-License-Identifier: Apache-2.0 |
| 7 |
|
| 8 |
#include <openssl/ssl.h> |
| 9 |
|
| 10 |
#include <string.h> |
| 11 |
|
| 12 |
#include <openssl/bytestring.h> |
| 13 |
#include <openssl/err.h> |
| 14 |
|
| 15 |
#include "internal.h" |
| 16 |
|
| 17 |
|
| 18 |
using namespace bssl; |
| 19 |
|
| 20 |
static const SRTP_PROTECTION_PROFILE kSRTPProfiles[] = { |
| 21 |
{ |
| 22 |
"SRTP_AES128_CM_SHA1_80", SRTP_AES128_CM_SHA1_80, |
| 23 |
}, |
| 24 |
{ |
| 25 |
"SRTP_AES128_CM_SHA1_32", SRTP_AES128_CM_SHA1_32, |
| 26 |
}, |
| 27 |
{ |
| 28 |
"SRTP_AEAD_AES_128_GCM", SRTP_AEAD_AES_128_GCM, |
| 29 |
}, |
| 30 |
{ |
| 31 |
"SRTP_AEAD_AES_256_GCM", SRTP_AEAD_AES_256_GCM, |
| 32 |
}, |
| 33 |
{0, 0}, |
| 34 |
}; |
| 35 |
|
| 36 |
static int find_profile_by_name(const char *profile_name, |
| 37 |
const SRTP_PROTECTION_PROFILE **pptr, |
| 38 |
size_t len) { |
| 39 |
const SRTP_PROTECTION_PROFILE *p = kSRTPProfiles; |
| 40 |
while (p->name) { |
| 41 |
if (len == strlen(p->name) && !strncmp(p->name, profile_name, len)) { |
| 42 |
*pptr = p; |
| 43 |
return 1; |
| 44 |
} |
| 45 |
|
| 46 |
p++; |
| 47 |
} |
| 48 |
|
| 49 |
return 0; |
| 50 |
} |
| 51 |
|
| 52 |
static int ssl_ctx_make_profiles( |
| 53 |
const char *profiles_string, |
| 54 |
UniquePtr<STACK_OF(SRTP_PROTECTION_PROFILE)> *out) { |
| 55 |
UniquePtr<STACK_OF(SRTP_PROTECTION_PROFILE)> profiles( |
| 56 |
sk_SRTP_PROTECTION_PROFILE_new_null()); |
| 57 |
if (profiles == nullptr) { |
| 58 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SRTP_COULD_NOT_ALLOCATE_PROFILES); |
| 59 |
return 0; |
| 60 |
} |
| 61 |
|
| 62 |
const char *col; |
| 63 |
const char *ptr = profiles_string; |
| 64 |
do { |
| 65 |
col = strchr(ptr, ':'); |
| 66 |
|
| 67 |
const SRTP_PROTECTION_PROFILE *profile; |
| 68 |
if (!find_profile_by_name(ptr, &profile, |
| 69 |
col ? (size_t)(col - ptr) : strlen(ptr))) { |
| 70 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SRTP_UNKNOWN_PROTECTION_PROFILE); |
| 71 |
return 0; |
| 72 |
} |
| 73 |
|
| 74 |
if (!sk_SRTP_PROTECTION_PROFILE_push(profiles.get(), profile)) { |
| 75 |
return 0; |
| 76 |
} |
| 77 |
|
| 78 |
if (col) { |
| 79 |
ptr = col + 1; |
| 80 |
} |
| 81 |
} while (col); |
| 82 |
|
| 83 |
*out = std::move(profiles); |
| 84 |
return 1; |
| 85 |
} |
| 86 |
|
| 87 |
int SSL_CTX_set_srtp_profiles(SSL_CTX *ctx, const char *profiles) { |
| 88 |
return ssl_ctx_make_profiles(profiles, &ctx->srtp_profiles); |
| 89 |
} |
| 90 |
|
| 91 |
int SSL_set_srtp_profiles(SSL *ssl, const char *profiles) { |
| 92 |
return ssl->config != nullptr && |
| 93 |
ssl_ctx_make_profiles(profiles, &ssl->config->srtp_profiles); |
| 94 |
} |
| 95 |
|
| 96 |
const STACK_OF(SRTP_PROTECTION_PROFILE) *SSL_get_srtp_profiles(const SSL *ssl) { |
| 97 |
if (ssl == nullptr) { |
| 98 |
return nullptr; |
| 99 |
} |
| 100 |
|
| 101 |
if (ssl->config == nullptr) { |
| 102 |
assert(0); |
| 103 |
return nullptr; |
| 104 |
} |
| 105 |
|
| 106 |
return ssl->config->srtp_profiles != nullptr |
| 107 |
? ssl->config->srtp_profiles.get() |
| 108 |
: ssl->ctx->srtp_profiles.get(); |
| 109 |
} |
| 110 |
|
| 111 |
const SRTP_PROTECTION_PROFILE *SSL_get_selected_srtp_profile(SSL *ssl) { |
| 112 |
return ssl->s3->srtp_profile; |
| 113 |
} |
| 114 |
|
| 115 |
int SSL_CTX_set_tlsext_use_srtp(SSL_CTX *ctx, const char *profiles) { |
| 116 |
// This API inverts its return value. |
| 117 |
return !SSL_CTX_set_srtp_profiles(ctx, profiles); |
| 118 |
} |
| 119 |
|
| 120 |
int SSL_set_tlsext_use_srtp(SSL *ssl, const char *profiles) { |
| 121 |
// This API inverts its return value. |
| 122 |
return !SSL_set_srtp_profiles(ssl, profiles); |
| 123 |
} |