blob: 718c62d20f853df50fb978a5b4f7e3ac5ef05b20 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
Steve Anton10542f22019-01-11 09:11:00 -080011#include "rtc_base/ssl_stream_adapter.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012
Harald Alvestrand8515d5a2020-03-20 22:51:32 +010013#include "absl/memory/memory.h"
Steve Anton10542f22019-01-11 09:11:00 -080014#include "rtc_base/openssl_stream_adapter.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016///////////////////////////////////////////////////////////////////////////////
17
18namespace rtc {
19
Guo-wei Shieh456696a2015-09-30 21:48:54 -070020// TODO(guoweis): Move this to SDP layer and use int form internally.
21// webrtc:5043.
Mirko Bonadei7750d802021-07-26 17:27:42 +020022const char kCsAesCm128HmacSha1_80[] = "AES_CM_128_HMAC_SHA1_80";
23const char kCsAesCm128HmacSha1_32[] = "AES_CM_128_HMAC_SHA1_32";
24const char kCsAeadAes128Gcm[] = "AEAD_AES_128_GCM";
25const char kCsAeadAes256Gcm[] = "AEAD_AES_256_GCM";
26
27// TODO(bugs.webrtc.org/12997): Start.
28// Remove the following constants which violate the style guide and can
29// potentially clash with OpenSSL macros.
Guo-wei Shieh456696a2015-09-30 21:48:54 -070030const char CS_AES_CM_128_HMAC_SHA1_80[] = "AES_CM_128_HMAC_SHA1_80";
31const char CS_AES_CM_128_HMAC_SHA1_32[] = "AES_CM_128_HMAC_SHA1_32";
jbauchcb560652016-08-04 05:20:32 -070032const char CS_AEAD_AES_128_GCM[] = "AEAD_AES_128_GCM";
33const char CS_AEAD_AES_256_GCM[] = "AEAD_AES_256_GCM";
Mirko Bonadei7750d802021-07-26 17:27:42 +020034// TODO(bugs.webrtc.org/12997): End.
Guo-wei Shieh456696a2015-09-30 21:48:54 -070035
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080036std::string SrtpCryptoSuiteToName(int crypto_suite) {
jbauchcb560652016-08-04 05:20:32 -070037 switch (crypto_suite) {
Mirko Bonadei7750d802021-07-26 17:27:42 +020038 case kSrtpAes128CmSha1_32:
39 return kCsAesCm128HmacSha1_32;
40 case kSrtpAes128CmSha1_80:
41 return kCsAesCm128HmacSha1_80;
42 case kSrtpAeadAes128Gcm:
43 return kCsAeadAes128Gcm;
44 case kSrtpAeadAes256Gcm:
45 return kCsAeadAes256Gcm;
Yves Gerey665174f2018-06-19 15:03:05 +020046 default:
47 return std::string();
jbauchcb560652016-08-04 05:20:32 -070048 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080049}
50
51int SrtpCryptoSuiteFromName(const std::string& crypto_suite) {
Mirko Bonadei7750d802021-07-26 17:27:42 +020052 if (crypto_suite == kCsAesCm128HmacSha1_32)
53 return kSrtpAes128CmSha1_32;
54 if (crypto_suite == kCsAesCm128HmacSha1_80)
55 return kSrtpAes128CmSha1_80;
56 if (crypto_suite == kCsAeadAes128Gcm)
57 return kSrtpAeadAes128Gcm;
58 if (crypto_suite == kCsAeadAes256Gcm)
59 return kSrtpAeadAes256Gcm;
60 return kSrtpInvalidCryptoSuite;
Guo-wei Shieh456696a2015-09-30 21:48:54 -070061}
62
Yves Gerey665174f2018-06-19 15:03:05 +020063bool GetSrtpKeyAndSaltLengths(int crypto_suite,
64 int* key_length,
65 int* salt_length) {
jbauchcb560652016-08-04 05:20:32 -070066 switch (crypto_suite) {
Mirko Bonadei7750d802021-07-26 17:27:42 +020067 case kSrtpAes128CmSha1_32:
68 case kSrtpAes128CmSha1_80:
Yves Gerey665174f2018-06-19 15:03:05 +020069 // SRTP_AES128_CM_HMAC_SHA1_32 and SRTP_AES128_CM_HMAC_SHA1_80 are defined
70 // in RFC 5764 to use a 128 bits key and 112 bits salt for the cipher.
71 *key_length = 16;
72 *salt_length = 14;
73 break;
Mirko Bonadei7750d802021-07-26 17:27:42 +020074 case kSrtpAeadAes128Gcm:
75 // kSrtpAeadAes128Gcm is defined in RFC 7714 to use a 128 bits key and
Yves Gerey665174f2018-06-19 15:03:05 +020076 // a 96 bits salt for the cipher.
77 *key_length = 16;
78 *salt_length = 12;
79 break;
Mirko Bonadei7750d802021-07-26 17:27:42 +020080 case kSrtpAeadAes256Gcm:
81 // kSrtpAeadAes256Gcm is defined in RFC 7714 to use a 256 bits key and
Yves Gerey665174f2018-06-19 15:03:05 +020082 // a 96 bits salt for the cipher.
83 *key_length = 32;
84 *salt_length = 12;
85 break;
86 default:
87 return false;
jbauchcb560652016-08-04 05:20:32 -070088 }
89 return true;
90}
91
92bool IsGcmCryptoSuite(int crypto_suite) {
Mirko Bonadei7750d802021-07-26 17:27:42 +020093 return (crypto_suite == kSrtpAeadAes256Gcm ||
94 crypto_suite == kSrtpAeadAes128Gcm);
jbauchcb560652016-08-04 05:20:32 -070095}
96
97bool IsGcmCryptoSuiteName(const std::string& crypto_suite) {
Mirko Bonadei7750d802021-07-26 17:27:42 +020098 return (crypto_suite == kCsAeadAes256Gcm || crypto_suite == kCsAeadAes128Gcm);
jbauchcb560652016-08-04 05:20:32 -070099}
100
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100101std::unique_ptr<SSLStreamAdapter> SSLStreamAdapter::Create(
102 std::unique_ptr<StreamInterface> stream) {
103 return std::make_unique<OpenSSLStreamAdapter>(std::move(stream));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000104}
105
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800106bool SSLStreamAdapter::GetSslCipherSuite(int* cipher_suite) {
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000107 return false;
108}
109
110bool SSLStreamAdapter::ExportKeyingMaterial(const std::string& label,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200111 const uint8_t* context,
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000112 size_t context_len,
113 bool use_context,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200114 uint8_t* result,
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000115 size_t result_len) {
116 return false; // Default is unsupported
117}
118
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800119bool SSLStreamAdapter::SetDtlsSrtpCryptoSuites(
120 const std::vector<int>& crypto_suites) {
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000121 return false;
122}
123
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800124bool SSLStreamAdapter::GetDtlsSrtpCryptoSuite(int* crypto_suite) {
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000125 return false;
126}
127
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -0700128bool SSLStreamAdapter::IsBoringSsl() {
129 return OpenSSLStreamAdapter::IsBoringSsl();
130}
torbjorng43166b82016-03-11 00:06:47 -0800131bool SSLStreamAdapter::IsAcceptableCipher(int cipher, KeyType key_type) {
132 return OpenSSLStreamAdapter::IsAcceptableCipher(cipher, key_type);
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700133}
torbjorng43166b82016-03-11 00:06:47 -0800134bool SSLStreamAdapter::IsAcceptableCipher(const std::string& cipher,
135 KeyType key_type) {
136 return OpenSSLStreamAdapter::IsAcceptableCipher(cipher, key_type);
137}
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800138std::string SSLStreamAdapter::SslCipherSuiteToName(int cipher_suite) {
139 return OpenSSLStreamAdapter::SslCipherSuiteToName(cipher_suite);
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000140}
Benjamin Wrightb19b4972018-10-25 10:46:49 -0700141
142///////////////////////////////////////////////////////////////////////////////
143// Test only settings
144///////////////////////////////////////////////////////////////////////////////
145
146void SSLStreamAdapter::EnableTimeCallbackForTesting() {
147 OpenSSLStreamAdapter::EnableTimeCallbackForTesting();
deadbeef6cf94a02016-11-28 17:38:34 -0800148}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000149
150///////////////////////////////////////////////////////////////////////////////
151
152} // namespace rtc