blob: 5023363e48c7b135e74c02374d69b186018f6e2f [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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 17:11:0011#include "rtc_base/ssl_stream_adapter.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2612
Harald Alvestrand53c424e2024-08-01 06:31:0213#include <cstddef>
14#include <cstdint>
15#include <memory>
16#include <string>
17#include <utility>
18#include <vector>
19
20#include "absl/functional/any_invocable.h"
Ali Tofigh7fa90572022-03-17 14:47:4921#include "absl/strings/string_view.h"
Steve Anton10542f22019-01-11 17:11:0022#include "rtc_base/openssl_stream_adapter.h"
Harald Alvestrand53c424e2024-08-01 06:31:0223#include "rtc_base/ssl_identity.h"
24#include "rtc_base/stream.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2625
henrike@webrtc.orgf0488722014-05-13 18:00:2626namespace rtc {
27
Philipp Hanckedb519e72024-06-25 16:26:4528// Deprecated, prefer SrtpCryptoSuiteToName.
Björn Tereliuse71fa4e2024-06-25 09:55:1229const char kCsAesCm128HmacSha1_80[] = "AES_CM_128_HMAC_SHA1_80";
30const char kCsAesCm128HmacSha1_32[] = "AES_CM_128_HMAC_SHA1_32";
31const char kCsAeadAes128Gcm[] = "AEAD_AES_128_GCM";
32const char kCsAeadAes256Gcm[] = "AEAD_AES_256_GCM";
33
Guo-wei Shieh521ed7b2015-11-19 03:41:5334std::string SrtpCryptoSuiteToName(int crypto_suite) {
jbauchcb560652016-08-04 12:20:3235 switch (crypto_suite) {
Björn Tereliuse71fa4e2024-06-25 09:55:1236 case kSrtpAes128CmSha1_80:
Philipp Hanckedb519e72024-06-25 16:26:4537 return "AES_CM_128_HMAC_SHA1_80";
38 case kSrtpAes128CmSha1_32:
39 return "AES_CM_128_HMAC_SHA1_32";
Mirko Bonadei7750d802021-07-26 15:27:4240 case kSrtpAeadAes128Gcm:
Philipp Hanckedb519e72024-06-25 16:26:4541 return "AEAD_AES_128_GCM";
Mirko Bonadei7750d802021-07-26 15:27:4242 case kSrtpAeadAes256Gcm:
Philipp Hanckedb519e72024-06-25 16:26:4543 return "AEAD_AES_256_GCM";
Yves Gerey665174f2018-06-19 13:03:0544 default:
45 return std::string();
jbauchcb560652016-08-04 12:20:3246 }
Guo-wei Shieh521ed7b2015-11-19 03:41:5347}
48
Yves Gerey665174f2018-06-19 13:03:0549bool GetSrtpKeyAndSaltLengths(int crypto_suite,
50 int* key_length,
51 int* salt_length) {
jbauchcb560652016-08-04 12:20:3252 switch (crypto_suite) {
Mirko Bonadei7750d802021-07-26 15:27:4253 case kSrtpAes128CmSha1_32:
54 case kSrtpAes128CmSha1_80:
Yves Gerey665174f2018-06-19 13:03:0555 // SRTP_AES128_CM_HMAC_SHA1_32 and SRTP_AES128_CM_HMAC_SHA1_80 are defined
56 // in RFC 5764 to use a 128 bits key and 112 bits salt for the cipher.
57 *key_length = 16;
58 *salt_length = 14;
59 break;
Mirko Bonadei7750d802021-07-26 15:27:4260 case kSrtpAeadAes128Gcm:
61 // kSrtpAeadAes128Gcm is defined in RFC 7714 to use a 128 bits key and
Yves Gerey665174f2018-06-19 13:03:0562 // a 96 bits salt for the cipher.
63 *key_length = 16;
64 *salt_length = 12;
65 break;
Mirko Bonadei7750d802021-07-26 15:27:4266 case kSrtpAeadAes256Gcm:
67 // kSrtpAeadAes256Gcm is defined in RFC 7714 to use a 256 bits key and
Yves Gerey665174f2018-06-19 13:03:0568 // a 96 bits salt for the cipher.
69 *key_length = 32;
70 *salt_length = 12;
71 break;
72 default:
73 return false;
jbauchcb560652016-08-04 12:20:3274 }
75 return true;
76}
77
78bool IsGcmCryptoSuite(int crypto_suite) {
Mirko Bonadei7750d802021-07-26 15:27:4279 return (crypto_suite == kSrtpAeadAes256Gcm ||
80 crypto_suite == kSrtpAeadAes128Gcm);
jbauchcb560652016-08-04 12:20:3281}
82
Harald Alvestrand8515d5a2020-03-20 21:51:3283std::unique_ptr<SSLStreamAdapter> SSLStreamAdapter::Create(
Tommi59574ca2023-09-05 07:21:5784 std::unique_ptr<StreamInterface> stream,
85 absl::AnyInvocable<void(SSLHandshakeError)> handshake_error) {
86 return std::make_unique<OpenSSLStreamAdapter>(std::move(stream),
87 std::move(handshake_error));
henrike@webrtc.orgf0488722014-05-13 18:00:2688}
89
Guo-wei Shieh521ed7b2015-11-19 03:41:5390bool SSLStreamAdapter::GetSslCipherSuite(int* cipher_suite) {
kwiberg@webrtc.org67186fe2015-03-09 22:21:5391 return false;
92}
93
Ali Tofigh7fa90572022-03-17 14:47:4994bool SSLStreamAdapter::ExportKeyingMaterial(absl::string_view label,
Peter Boström0c4e06b2015-10-07 10:23:2195 const uint8_t* context,
kwiberg@webrtc.org67186fe2015-03-09 22:21:5396 size_t context_len,
97 bool use_context,
Peter Boström0c4e06b2015-10-07 10:23:2198 uint8_t* result,
kwiberg@webrtc.org67186fe2015-03-09 22:21:5399 size_t result_len) {
100 return false; // Default is unsupported
101}
102
Guo-wei Shieh521ed7b2015-11-19 03:41:53103bool SSLStreamAdapter::SetDtlsSrtpCryptoSuites(
104 const std::vector<int>& crypto_suites) {
kwiberg@webrtc.org67186fe2015-03-09 22:21:53105 return false;
106}
107
Guo-wei Shieh521ed7b2015-11-19 03:41:53108bool SSLStreamAdapter::GetDtlsSrtpCryptoSuite(int* crypto_suite) {
kwiberg@webrtc.org67186fe2015-03-09 22:21:53109 return false;
110}
111
Taylor Brandstetter4f0dfbd2016-06-16 00:15:23112bool SSLStreamAdapter::IsBoringSsl() {
113 return OpenSSLStreamAdapter::IsBoringSsl();
114}
torbjorng43166b82016-03-11 08:06:47115bool SSLStreamAdapter::IsAcceptableCipher(int cipher, KeyType key_type) {
116 return OpenSSLStreamAdapter::IsAcceptableCipher(cipher, key_type);
Guo-wei Shieh456696a2015-10-01 04:48:54117}
Ali Tofigh7fa90572022-03-17 14:47:49118bool SSLStreamAdapter::IsAcceptableCipher(absl::string_view cipher,
torbjorng43166b82016-03-11 08:06:47119 KeyType key_type) {
120 return OpenSSLStreamAdapter::IsAcceptableCipher(cipher, key_type);
121}
Guo-wei Shieh521ed7b2015-11-19 03:41:53122std::string SSLStreamAdapter::SslCipherSuiteToName(int cipher_suite) {
123 return OpenSSLStreamAdapter::SslCipherSuiteToName(cipher_suite);
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36124}
Benjamin Wrightb19b4972018-10-25 17:46:49125
126///////////////////////////////////////////////////////////////////////////////
127// Test only settings
128///////////////////////////////////////////////////////////////////////////////
129
130void SSLStreamAdapter::EnableTimeCallbackForTesting() {
131 OpenSSLStreamAdapter::EnableTimeCallbackForTesting();
deadbeef6cf94a02016-11-29 01:38:34132}
henrike@webrtc.orgf0488722014-05-13 18:00:26133
134///////////////////////////////////////////////////////////////////////////////
135
136} // namespace rtc