blob: 4b60d6d7b121a98c14df1090cd9f72dbea6e3616 [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"
Ali Tofigh7fa90572022-03-17 15:47:49 +010014#include "absl/strings/string_view.h"
Steve Anton10542f22019-01-11 09:11:00 -080015#include "rtc_base/openssl_stream_adapter.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017///////////////////////////////////////////////////////////////////////////////
18
19namespace rtc {
20
Guo-wei Shieh456696a2015-09-30 21:48:54 -070021// TODO(guoweis): Move this to SDP layer and use int form internally.
22// webrtc:5043.
Mirko Bonadei7750d802021-07-26 17:27:42 +020023const char kCsAesCm128HmacSha1_80[] = "AES_CM_128_HMAC_SHA1_80";
24const char kCsAesCm128HmacSha1_32[] = "AES_CM_128_HMAC_SHA1_32";
25const char kCsAeadAes128Gcm[] = "AEAD_AES_128_GCM";
26const char kCsAeadAes256Gcm[] = "AEAD_AES_256_GCM";
27
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080028std::string SrtpCryptoSuiteToName(int crypto_suite) {
jbauchcb560652016-08-04 05:20:32 -070029 switch (crypto_suite) {
Mirko Bonadei7750d802021-07-26 17:27:42 +020030 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;
Yves Gerey665174f2018-06-19 15:03:05 +020038 default:
39 return std::string();
jbauchcb560652016-08-04 05:20:32 -070040 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080041}
42
Ali Tofigh7fa90572022-03-17 15:47:49 +010043int SrtpCryptoSuiteFromName(absl::string_view crypto_suite) {
Mirko Bonadei7750d802021-07-26 17:27:42 +020044 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;
Guo-wei Shieh456696a2015-09-30 21:48:54 -070053}
54
Yves Gerey665174f2018-06-19 15:03:05 +020055bool GetSrtpKeyAndSaltLengths(int crypto_suite,
56 int* key_length,
57 int* salt_length) {
jbauchcb560652016-08-04 05:20:32 -070058 switch (crypto_suite) {
Mirko Bonadei7750d802021-07-26 17:27:42 +020059 case kSrtpAes128CmSha1_32:
60 case kSrtpAes128CmSha1_80:
Yves Gerey665174f2018-06-19 15:03:05 +020061 // 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;
Mirko Bonadei7750d802021-07-26 17:27:42 +020066 case kSrtpAeadAes128Gcm:
67 // kSrtpAeadAes128Gcm is defined in RFC 7714 to use a 128 bits key and
Yves Gerey665174f2018-06-19 15:03:05 +020068 // a 96 bits salt for the cipher.
69 *key_length = 16;
70 *salt_length = 12;
71 break;
Mirko Bonadei7750d802021-07-26 17:27:42 +020072 case kSrtpAeadAes256Gcm:
73 // kSrtpAeadAes256Gcm is defined in RFC 7714 to use a 256 bits key and
Yves Gerey665174f2018-06-19 15:03:05 +020074 // a 96 bits salt for the cipher.
75 *key_length = 32;
76 *salt_length = 12;
77 break;
78 default:
79 return false;
jbauchcb560652016-08-04 05:20:32 -070080 }
81 return true;
82}
83
84bool IsGcmCryptoSuite(int crypto_suite) {
Mirko Bonadei7750d802021-07-26 17:27:42 +020085 return (crypto_suite == kSrtpAeadAes256Gcm ||
86 crypto_suite == kSrtpAeadAes128Gcm);
jbauchcb560652016-08-04 05:20:32 -070087}
88
Ali Tofigh7fa90572022-03-17 15:47:49 +010089bool IsGcmCryptoSuiteName(absl::string_view crypto_suite) {
Mirko Bonadei7750d802021-07-26 17:27:42 +020090 return (crypto_suite == kCsAeadAes256Gcm || crypto_suite == kCsAeadAes128Gcm);
jbauchcb560652016-08-04 05:20:32 -070091}
92
Harald Alvestrand8515d5a2020-03-20 22:51:32 +010093std::unique_ptr<SSLStreamAdapter> SSLStreamAdapter::Create(
94 std::unique_ptr<StreamInterface> stream) {
95 return std::make_unique<OpenSSLStreamAdapter>(std::move(stream));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000096}
97
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080098bool SSLStreamAdapter::GetSslCipherSuite(int* cipher_suite) {
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000099 return false;
100}
101
Ali Tofigh7fa90572022-03-17 15:47:49 +0100102bool SSLStreamAdapter::ExportKeyingMaterial(absl::string_view label,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200103 const uint8_t* context,
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000104 size_t context_len,
105 bool use_context,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200106 uint8_t* result,
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000107 size_t result_len) {
108 return false; // Default is unsupported
109}
110
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800111bool SSLStreamAdapter::SetDtlsSrtpCryptoSuites(
112 const std::vector<int>& crypto_suites) {
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000113 return false;
114}
115
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800116bool SSLStreamAdapter::GetDtlsSrtpCryptoSuite(int* crypto_suite) {
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000117 return false;
118}
119
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -0700120bool SSLStreamAdapter::IsBoringSsl() {
121 return OpenSSLStreamAdapter::IsBoringSsl();
122}
torbjorng43166b82016-03-11 00:06:47 -0800123bool SSLStreamAdapter::IsAcceptableCipher(int cipher, KeyType key_type) {
124 return OpenSSLStreamAdapter::IsAcceptableCipher(cipher, key_type);
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700125}
Ali Tofigh7fa90572022-03-17 15:47:49 +0100126bool SSLStreamAdapter::IsAcceptableCipher(absl::string_view cipher,
torbjorng43166b82016-03-11 00:06:47 -0800127 KeyType key_type) {
128 return OpenSSLStreamAdapter::IsAcceptableCipher(cipher, key_type);
129}
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800130std::string SSLStreamAdapter::SslCipherSuiteToName(int cipher_suite) {
131 return OpenSSLStreamAdapter::SslCipherSuiteToName(cipher_suite);
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000132}
Benjamin Wrightb19b4972018-10-25 10:46:49 -0700133
134///////////////////////////////////////////////////////////////////////////////
135// Test only settings
136///////////////////////////////////////////////////////////////////////////////
137
138void SSLStreamAdapter::EnableTimeCallbackForTesting() {
139 OpenSSLStreamAdapter::EnableTimeCallbackForTesting();
deadbeef6cf94a02016-11-28 17:38:34 -0800140}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000141
142///////////////////////////////////////////////////////////////////////////////
143
144} // namespace rtc