blob: b805fdc6c3c4109f880945627a71613eecbd6146 [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
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080027std::string SrtpCryptoSuiteToName(int crypto_suite) {
jbauchcb560652016-08-04 05:20:32 -070028 switch (crypto_suite) {
Mirko Bonadei7750d802021-07-26 17:27:42 +020029 case kSrtpAes128CmSha1_32:
30 return kCsAesCm128HmacSha1_32;
31 case kSrtpAes128CmSha1_80:
32 return kCsAesCm128HmacSha1_80;
33 case kSrtpAeadAes128Gcm:
34 return kCsAeadAes128Gcm;
35 case kSrtpAeadAes256Gcm:
36 return kCsAeadAes256Gcm;
Yves Gerey665174f2018-06-19 15:03:05 +020037 default:
38 return std::string();
jbauchcb560652016-08-04 05:20:32 -070039 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080040}
41
42int SrtpCryptoSuiteFromName(const std::string& crypto_suite) {
Mirko Bonadei7750d802021-07-26 17:27:42 +020043 if (crypto_suite == kCsAesCm128HmacSha1_32)
44 return kSrtpAes128CmSha1_32;
45 if (crypto_suite == kCsAesCm128HmacSha1_80)
46 return kSrtpAes128CmSha1_80;
47 if (crypto_suite == kCsAeadAes128Gcm)
48 return kSrtpAeadAes128Gcm;
49 if (crypto_suite == kCsAeadAes256Gcm)
50 return kSrtpAeadAes256Gcm;
51 return kSrtpInvalidCryptoSuite;
Guo-wei Shieh456696a2015-09-30 21:48:54 -070052}
53
Yves Gerey665174f2018-06-19 15:03:05 +020054bool GetSrtpKeyAndSaltLengths(int crypto_suite,
55 int* key_length,
56 int* salt_length) {
jbauchcb560652016-08-04 05:20:32 -070057 switch (crypto_suite) {
Mirko Bonadei7750d802021-07-26 17:27:42 +020058 case kSrtpAes128CmSha1_32:
59 case kSrtpAes128CmSha1_80:
Yves Gerey665174f2018-06-19 15:03:05 +020060 // SRTP_AES128_CM_HMAC_SHA1_32 and SRTP_AES128_CM_HMAC_SHA1_80 are defined
61 // in RFC 5764 to use a 128 bits key and 112 bits salt for the cipher.
62 *key_length = 16;
63 *salt_length = 14;
64 break;
Mirko Bonadei7750d802021-07-26 17:27:42 +020065 case kSrtpAeadAes128Gcm:
66 // kSrtpAeadAes128Gcm is defined in RFC 7714 to use a 128 bits key and
Yves Gerey665174f2018-06-19 15:03:05 +020067 // a 96 bits salt for the cipher.
68 *key_length = 16;
69 *salt_length = 12;
70 break;
Mirko Bonadei7750d802021-07-26 17:27:42 +020071 case kSrtpAeadAes256Gcm:
72 // kSrtpAeadAes256Gcm is defined in RFC 7714 to use a 256 bits key and
Yves Gerey665174f2018-06-19 15:03:05 +020073 // a 96 bits salt for the cipher.
74 *key_length = 32;
75 *salt_length = 12;
76 break;
77 default:
78 return false;
jbauchcb560652016-08-04 05:20:32 -070079 }
80 return true;
81}
82
83bool IsGcmCryptoSuite(int crypto_suite) {
Mirko Bonadei7750d802021-07-26 17:27:42 +020084 return (crypto_suite == kSrtpAeadAes256Gcm ||
85 crypto_suite == kSrtpAeadAes128Gcm);
jbauchcb560652016-08-04 05:20:32 -070086}
87
88bool IsGcmCryptoSuiteName(const std::string& crypto_suite) {
Mirko Bonadei7750d802021-07-26 17:27:42 +020089 return (crypto_suite == kCsAeadAes256Gcm || crypto_suite == kCsAeadAes128Gcm);
jbauchcb560652016-08-04 05:20:32 -070090}
91
Harald Alvestrand8515d5a2020-03-20 22:51:32 +010092std::unique_ptr<SSLStreamAdapter> SSLStreamAdapter::Create(
93 std::unique_ptr<StreamInterface> stream) {
94 return std::make_unique<OpenSSLStreamAdapter>(std::move(stream));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000095}
96
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080097bool SSLStreamAdapter::GetSslCipherSuite(int* cipher_suite) {
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000098 return false;
99}
100
101bool SSLStreamAdapter::ExportKeyingMaterial(const std::string& label,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200102 const uint8_t* context,
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000103 size_t context_len,
104 bool use_context,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200105 uint8_t* result,
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000106 size_t result_len) {
107 return false; // Default is unsupported
108}
109
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800110bool SSLStreamAdapter::SetDtlsSrtpCryptoSuites(
111 const std::vector<int>& crypto_suites) {
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000112 return false;
113}
114
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800115bool SSLStreamAdapter::GetDtlsSrtpCryptoSuite(int* crypto_suite) {
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000116 return false;
117}
118
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -0700119bool SSLStreamAdapter::IsBoringSsl() {
120 return OpenSSLStreamAdapter::IsBoringSsl();
121}
torbjorng43166b82016-03-11 00:06:47 -0800122bool SSLStreamAdapter::IsAcceptableCipher(int cipher, KeyType key_type) {
123 return OpenSSLStreamAdapter::IsAcceptableCipher(cipher, key_type);
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700124}
torbjorng43166b82016-03-11 00:06:47 -0800125bool SSLStreamAdapter::IsAcceptableCipher(const std::string& cipher,
126 KeyType key_type) {
127 return OpenSSLStreamAdapter::IsAcceptableCipher(cipher, key_type);
128}
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800129std::string SSLStreamAdapter::SslCipherSuiteToName(int cipher_suite) {
130 return OpenSSLStreamAdapter::SslCipherSuiteToName(cipher_suite);
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000131}
Benjamin Wrightb19b4972018-10-25 10:46:49 -0700132
133///////////////////////////////////////////////////////////////////////////////
134// Test only settings
135///////////////////////////////////////////////////////////////////////////////
136
137void SSLStreamAdapter::EnableTimeCallbackForTesting() {
138 OpenSSLStreamAdapter::EnableTimeCallbackForTesting();
deadbeef6cf94a02016-11-28 17:38:34 -0800139}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000140
141///////////////////////////////////////////////////////////////////////////////
142
143} // namespace rtc