1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "rtc_base/ssl_stream_adapter.h"
12
13 #include "absl/memory/memory.h"
14 #include "absl/strings/string_view.h"
15 #include "rtc_base/openssl_stream_adapter.h"
16
17 ///////////////////////////////////////////////////////////////////////////////
18
19 namespace rtc {
20
21 // TODO(guoweis): Move this to SDP layer and use int form internally.
22 // webrtc:5043.
23 const char kCsAesCm128HmacSha1_80[] = "AES_CM_128_HMAC_SHA1_80";
24 const char kCsAesCm128HmacSha1_32[] = "AES_CM_128_HMAC_SHA1_32";
25 const char kCsAeadAes128Gcm[] = "AEAD_AES_128_GCM";
26 const char kCsAeadAes256Gcm[] = "AEAD_AES_256_GCM";
27
SrtpCryptoSuiteToName(int crypto_suite)28 std::string SrtpCryptoSuiteToName(int crypto_suite) {
29 switch (crypto_suite) {
30 case kSrtpAes128CmSha1_32:
31 return kCsAesCm128HmacSha1_32;
32 case kSrtpAes128CmSha1_80:
33 return kCsAesCm128HmacSha1_80;
34 case kSrtpAeadAes128Gcm:
35 return kCsAeadAes128Gcm;
36 case kSrtpAeadAes256Gcm:
37 return kCsAeadAes256Gcm;
38 default:
39 return std::string();
40 }
41 }
42
SrtpCryptoSuiteFromName(absl::string_view crypto_suite)43 int SrtpCryptoSuiteFromName(absl::string_view crypto_suite) {
44 if (crypto_suite == kCsAesCm128HmacSha1_32)
45 return kSrtpAes128CmSha1_32;
46 if (crypto_suite == kCsAesCm128HmacSha1_80)
47 return kSrtpAes128CmSha1_80;
48 if (crypto_suite == kCsAeadAes128Gcm)
49 return kSrtpAeadAes128Gcm;
50 if (crypto_suite == kCsAeadAes256Gcm)
51 return kSrtpAeadAes256Gcm;
52 return kSrtpInvalidCryptoSuite;
53 }
54
GetSrtpKeyAndSaltLengths(int crypto_suite,int * key_length,int * salt_length)55 bool GetSrtpKeyAndSaltLengths(int crypto_suite,
56 int* key_length,
57 int* salt_length) {
58 switch (crypto_suite) {
59 case kSrtpAes128CmSha1_32:
60 case kSrtpAes128CmSha1_80:
61 // SRTP_AES128_CM_HMAC_SHA1_32 and SRTP_AES128_CM_HMAC_SHA1_80 are defined
62 // in RFC 5764 to use a 128 bits key and 112 bits salt for the cipher.
63 *key_length = 16;
64 *salt_length = 14;
65 break;
66 case kSrtpAeadAes128Gcm:
67 // kSrtpAeadAes128Gcm is defined in RFC 7714 to use a 128 bits key and
68 // a 96 bits salt for the cipher.
69 *key_length = 16;
70 *salt_length = 12;
71 break;
72 case kSrtpAeadAes256Gcm:
73 // kSrtpAeadAes256Gcm is defined in RFC 7714 to use a 256 bits key and
74 // a 96 bits salt for the cipher.
75 *key_length = 32;
76 *salt_length = 12;
77 break;
78 default:
79 return false;
80 }
81 return true;
82 }
83
IsGcmCryptoSuite(int crypto_suite)84 bool IsGcmCryptoSuite(int crypto_suite) {
85 return (crypto_suite == kSrtpAeadAes256Gcm ||
86 crypto_suite == kSrtpAeadAes128Gcm);
87 }
88
IsGcmCryptoSuiteName(absl::string_view crypto_suite)89 bool IsGcmCryptoSuiteName(absl::string_view crypto_suite) {
90 return (crypto_suite == kCsAeadAes256Gcm || crypto_suite == kCsAeadAes128Gcm);
91 }
92
Create(std::unique_ptr<StreamInterface> stream)93 std::unique_ptr<SSLStreamAdapter> SSLStreamAdapter::Create(
94 std::unique_ptr<StreamInterface> stream) {
95 return std::make_unique<OpenSSLStreamAdapter>(std::move(stream));
96 }
97
GetSslCipherSuite(int * cipher_suite)98 bool SSLStreamAdapter::GetSslCipherSuite(int* cipher_suite) {
99 return false;
100 }
101
ExportKeyingMaterial(absl::string_view label,const uint8_t * context,size_t context_len,bool use_context,uint8_t * result,size_t result_len)102 bool SSLStreamAdapter::ExportKeyingMaterial(absl::string_view label,
103 const uint8_t* context,
104 size_t context_len,
105 bool use_context,
106 uint8_t* result,
107 size_t result_len) {
108 return false; // Default is unsupported
109 }
110
SetDtlsSrtpCryptoSuites(const std::vector<int> & crypto_suites)111 bool SSLStreamAdapter::SetDtlsSrtpCryptoSuites(
112 const std::vector<int>& crypto_suites) {
113 return false;
114 }
115
GetDtlsSrtpCryptoSuite(int * crypto_suite)116 bool SSLStreamAdapter::GetDtlsSrtpCryptoSuite(int* crypto_suite) {
117 return false;
118 }
119
IsBoringSsl()120 bool SSLStreamAdapter::IsBoringSsl() {
121 return OpenSSLStreamAdapter::IsBoringSsl();
122 }
IsAcceptableCipher(int cipher,KeyType key_type)123 bool SSLStreamAdapter::IsAcceptableCipher(int cipher, KeyType key_type) {
124 return OpenSSLStreamAdapter::IsAcceptableCipher(cipher, key_type);
125 }
IsAcceptableCipher(absl::string_view cipher,KeyType key_type)126 bool SSLStreamAdapter::IsAcceptableCipher(absl::string_view cipher,
127 KeyType key_type) {
128 return OpenSSLStreamAdapter::IsAcceptableCipher(cipher, key_type);
129 }
SslCipherSuiteToName(int cipher_suite)130 std::string SSLStreamAdapter::SslCipherSuiteToName(int cipher_suite) {
131 return OpenSSLStreamAdapter::SslCipherSuiteToName(cipher_suite);
132 }
133
134 ///////////////////////////////////////////////////////////////////////////////
135 // Test only settings
136 ///////////////////////////////////////////////////////////////////////////////
137
EnableTimeCallbackForTesting()138 void SSLStreamAdapter::EnableTimeCallbackForTesting() {
139 OpenSSLStreamAdapter::EnableTimeCallbackForTesting();
140 }
141
142 ///////////////////////////////////////////////////////////////////////////////
143
144 } // namespace rtc
145