1 // Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved.
2 // Copyright (C) 2006, Network Resonance, Inc.
3 // Copyright (C) 2011, RTFM, Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // https://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16
17 // DTLS code by Eric Rescorla <ekr@rtfm.com>
18
19 #include <openssl/ssl.h>
20
21 #include <string.h>
22
23 #include <openssl/bytestring.h>
24 #include <openssl/err.h>
25
26 #include "internal.h"
27
28
29 using namespace bssl;
30
31 static const SRTP_PROTECTION_PROFILE kSRTPProfiles[] = {
32 {"SRTP_AES128_CM_SHA1_80", SRTP_AES128_CM_SHA1_80},
33 {"SRTP_AES128_CM_SHA1_32", SRTP_AES128_CM_SHA1_32},
34 {"SRTP_AEAD_AES_128_GCM", SRTP_AEAD_AES_128_GCM},
35 {"SRTP_AEAD_AES_256_GCM", SRTP_AEAD_AES_256_GCM},
36 {0, 0},
37 };
38
find_profile_by_name(const char * profile_name,const SRTP_PROTECTION_PROFILE ** pptr,size_t len)39 static int find_profile_by_name(const char *profile_name,
40 const SRTP_PROTECTION_PROFILE **pptr,
41 size_t len) {
42 const SRTP_PROTECTION_PROFILE *p = kSRTPProfiles;
43 while (p->name) {
44 if (len == strlen(p->name) && !strncmp(p->name, profile_name, len)) {
45 *pptr = p;
46 return 1;
47 }
48
49 p++;
50 }
51
52 return 0;
53 }
54
ssl_ctx_make_profiles(const char * profiles_string,UniquePtr<STACK_OF (SRTP_PROTECTION_PROFILE)> * out)55 static int ssl_ctx_make_profiles(
56 const char *profiles_string,
57 UniquePtr<STACK_OF(SRTP_PROTECTION_PROFILE)> *out) {
58 UniquePtr<STACK_OF(SRTP_PROTECTION_PROFILE)> profiles(
59 sk_SRTP_PROTECTION_PROFILE_new_null());
60 if (profiles == nullptr) {
61 OPENSSL_PUT_ERROR(SSL, SSL_R_SRTP_COULD_NOT_ALLOCATE_PROFILES);
62 return 0;
63 }
64
65 const char *col;
66 const char *ptr = profiles_string;
67 do {
68 col = strchr(ptr, ':');
69
70 const SRTP_PROTECTION_PROFILE *profile;
71 if (!find_profile_by_name(ptr, &profile,
72 col ? (size_t)(col - ptr) : strlen(ptr))) {
73 OPENSSL_PUT_ERROR(SSL, SSL_R_SRTP_UNKNOWN_PROTECTION_PROFILE);
74 return 0;
75 }
76
77 if (!sk_SRTP_PROTECTION_PROFILE_push(profiles.get(), profile)) {
78 return 0;
79 }
80
81 if (col) {
82 ptr = col + 1;
83 }
84 } while (col);
85
86 *out = std::move(profiles);
87 return 1;
88 }
89
SSL_CTX_set_srtp_profiles(SSL_CTX * ctx,const char * profiles)90 int SSL_CTX_set_srtp_profiles(SSL_CTX *ctx, const char *profiles) {
91 return ssl_ctx_make_profiles(profiles, &ctx->srtp_profiles);
92 }
93
SSL_set_srtp_profiles(SSL * ssl,const char * profiles)94 int SSL_set_srtp_profiles(SSL *ssl, const char *profiles) {
95 return ssl->config != nullptr &&
96 ssl_ctx_make_profiles(profiles, &ssl->config->srtp_profiles);
97 }
98
STACK_OF(SRTP_PROTECTION_PROFILE)99 const STACK_OF(SRTP_PROTECTION_PROFILE) *SSL_get_srtp_profiles(const SSL *ssl) {
100 if (ssl == nullptr) {
101 return nullptr;
102 }
103
104 if (ssl->config == nullptr) {
105 assert(0);
106 return nullptr;
107 }
108
109 return ssl->config->srtp_profiles != nullptr
110 ? ssl->config->srtp_profiles.get()
111 : ssl->ctx->srtp_profiles.get();
112 }
113
SSL_get_selected_srtp_profile(SSL * ssl)114 const SRTP_PROTECTION_PROFILE *SSL_get_selected_srtp_profile(SSL *ssl) {
115 return ssl->s3->srtp_profile;
116 }
117
SSL_CTX_set_tlsext_use_srtp(SSL_CTX * ctx,const char * profiles)118 int SSL_CTX_set_tlsext_use_srtp(SSL_CTX *ctx, const char *profiles) {
119 // This API inverts its return value.
120 return !SSL_CTX_set_srtp_profiles(ctx, profiles);
121 }
122
SSL_set_tlsext_use_srtp(SSL * ssl,const char * profiles)123 int SSL_set_tlsext_use_srtp(SSL *ssl, const char *profiles) {
124 // This API inverts its return value.
125 return !SSL_set_srtp_profiles(ssl, profiles);
126 }
127