/src/boringssl/ssl/d1_srtp.cc
Line | Count | Source (jump to first uncovered line) |
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 | | |
39 | | static int find_profile_by_name(const char *profile_name, |
40 | | const SRTP_PROTECTION_PROFILE **pptr, |
41 | 0 | size_t len) { |
42 | 0 | const SRTP_PROTECTION_PROFILE *p = kSRTPProfiles; |
43 | 0 | while (p->name) { |
44 | 0 | if (len == strlen(p->name) && !strncmp(p->name, profile_name, len)) { |
45 | 0 | *pptr = p; |
46 | 0 | return 1; |
47 | 0 | } |
48 | | |
49 | 0 | p++; |
50 | 0 | } |
51 | | |
52 | 0 | return 0; |
53 | 0 | } |
54 | | |
55 | | static int ssl_ctx_make_profiles( |
56 | | const char *profiles_string, |
57 | 0 | UniquePtr<STACK_OF(SRTP_PROTECTION_PROFILE)> *out) { |
58 | 0 | UniquePtr<STACK_OF(SRTP_PROTECTION_PROFILE)> profiles( |
59 | 0 | sk_SRTP_PROTECTION_PROFILE_new_null()); |
60 | 0 | if (profiles == nullptr) { |
61 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_SRTP_COULD_NOT_ALLOCATE_PROFILES); |
62 | 0 | return 0; |
63 | 0 | } |
64 | | |
65 | 0 | const char *col; |
66 | 0 | const char *ptr = profiles_string; |
67 | 0 | do { |
68 | 0 | col = strchr(ptr, ':'); |
69 | |
|
70 | 0 | const SRTP_PROTECTION_PROFILE *profile; |
71 | 0 | if (!find_profile_by_name(ptr, &profile, |
72 | 0 | col ? (size_t)(col - ptr) : strlen(ptr))) { |
73 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_SRTP_UNKNOWN_PROTECTION_PROFILE); |
74 | 0 | return 0; |
75 | 0 | } |
76 | | |
77 | 0 | if (!sk_SRTP_PROTECTION_PROFILE_push(profiles.get(), profile)) { |
78 | 0 | return 0; |
79 | 0 | } |
80 | | |
81 | 0 | if (col) { |
82 | 0 | ptr = col + 1; |
83 | 0 | } |
84 | 0 | } while (col); |
85 | | |
86 | 0 | *out = std::move(profiles); |
87 | 0 | return 1; |
88 | 0 | } |
89 | | |
90 | 0 | int SSL_CTX_set_srtp_profiles(SSL_CTX *ctx, const char *profiles) { |
91 | 0 | return ssl_ctx_make_profiles(profiles, &ctx->srtp_profiles); |
92 | 0 | } |
93 | | |
94 | 0 | int SSL_set_srtp_profiles(SSL *ssl, const char *profiles) { |
95 | 0 | return ssl->config != nullptr && |
96 | 0 | ssl_ctx_make_profiles(profiles, &ssl->config->srtp_profiles); |
97 | 0 | } |
98 | | |
99 | 0 | const STACK_OF(SRTP_PROTECTION_PROFILE) *SSL_get_srtp_profiles(const SSL *ssl) { |
100 | 0 | if (ssl == nullptr) { |
101 | 0 | return nullptr; |
102 | 0 | } |
103 | | |
104 | 0 | if (ssl->config == nullptr) { |
105 | 0 | assert(0); |
106 | 0 | return nullptr; |
107 | 0 | } |
108 | | |
109 | 0 | return ssl->config->srtp_profiles != nullptr |
110 | 0 | ? ssl->config->srtp_profiles.get() |
111 | 0 | : ssl->ctx->srtp_profiles.get(); |
112 | 0 | } |
113 | | |
114 | 0 | const SRTP_PROTECTION_PROFILE *SSL_get_selected_srtp_profile(SSL *ssl) { |
115 | 0 | return ssl->s3->srtp_profile; |
116 | 0 | } |
117 | | |
118 | 0 | int SSL_CTX_set_tlsext_use_srtp(SSL_CTX *ctx, const char *profiles) { |
119 | | // This API inverts its return value. |
120 | 0 | return !SSL_CTX_set_srtp_profiles(ctx, profiles); |
121 | 0 | } |
122 | | |
123 | 0 | int SSL_set_tlsext_use_srtp(SSL *ssl, const char *profiles) { |
124 | | // This API inverts its return value. |
125 | 0 | return !SSL_set_srtp_profiles(ssl, profiles); |
126 | 0 | } |