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