henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 1 | /* |
| 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 Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #include "rtc_base/ssl_stream_adapter.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 12 | |
Harald Alvestrand | 53c424e | 2024-08-01 06:31:02 | [diff] [blame] | 13 | #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 Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 21 | #include "absl/strings/string_view.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 22 | #include "rtc_base/openssl_stream_adapter.h" |
Harald Alvestrand | 53c424e | 2024-08-01 06:31:02 | [diff] [blame] | 23 | #include "rtc_base/ssl_identity.h" |
| 24 | #include "rtc_base/stream.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 25 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 26 | namespace rtc { |
| 27 | |
Philipp Hancke | db519e7 | 2024-06-25 16:26:45 | [diff] [blame] | 28 | // Deprecated, prefer SrtpCryptoSuiteToName. |
Björn Terelius | e71fa4e | 2024-06-25 09:55:12 | [diff] [blame] | 29 | const char kCsAesCm128HmacSha1_80[] = "AES_CM_128_HMAC_SHA1_80"; |
| 30 | const char kCsAesCm128HmacSha1_32[] = "AES_CM_128_HMAC_SHA1_32"; |
| 31 | const char kCsAeadAes128Gcm[] = "AEAD_AES_128_GCM"; |
| 32 | const char kCsAeadAes256Gcm[] = "AEAD_AES_256_GCM"; |
| 33 | |
Guo-wei Shieh | 521ed7b | 2015-11-19 03:41:53 | [diff] [blame] | 34 | std::string SrtpCryptoSuiteToName(int crypto_suite) { |
jbauch | cb56065 | 2016-08-04 12:20:32 | [diff] [blame] | 35 | switch (crypto_suite) { |
Björn Terelius | e71fa4e | 2024-06-25 09:55:12 | [diff] [blame] | 36 | case kSrtpAes128CmSha1_80: |
Philipp Hancke | db519e7 | 2024-06-25 16:26:45 | [diff] [blame] | 37 | return "AES_CM_128_HMAC_SHA1_80"; |
| 38 | case kSrtpAes128CmSha1_32: |
| 39 | return "AES_CM_128_HMAC_SHA1_32"; |
Mirko Bonadei | 7750d80 | 2021-07-26 15:27:42 | [diff] [blame] | 40 | case kSrtpAeadAes128Gcm: |
Philipp Hancke | db519e7 | 2024-06-25 16:26:45 | [diff] [blame] | 41 | return "AEAD_AES_128_GCM"; |
Mirko Bonadei | 7750d80 | 2021-07-26 15:27:42 | [diff] [blame] | 42 | case kSrtpAeadAes256Gcm: |
Philipp Hancke | db519e7 | 2024-06-25 16:26:45 | [diff] [blame] | 43 | return "AEAD_AES_256_GCM"; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 44 | default: |
| 45 | return std::string(); |
jbauch | cb56065 | 2016-08-04 12:20:32 | [diff] [blame] | 46 | } |
Guo-wei Shieh | 521ed7b | 2015-11-19 03:41:53 | [diff] [blame] | 47 | } |
| 48 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 49 | bool GetSrtpKeyAndSaltLengths(int crypto_suite, |
| 50 | int* key_length, |
| 51 | int* salt_length) { |
jbauch | cb56065 | 2016-08-04 12:20:32 | [diff] [blame] | 52 | switch (crypto_suite) { |
Mirko Bonadei | 7750d80 | 2021-07-26 15:27:42 | [diff] [blame] | 53 | case kSrtpAes128CmSha1_32: |
| 54 | case kSrtpAes128CmSha1_80: |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 55 | // 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 Bonadei | 7750d80 | 2021-07-26 15:27:42 | [diff] [blame] | 60 | case kSrtpAeadAes128Gcm: |
| 61 | // kSrtpAeadAes128Gcm is defined in RFC 7714 to use a 128 bits key and |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 62 | // a 96 bits salt for the cipher. |
| 63 | *key_length = 16; |
| 64 | *salt_length = 12; |
| 65 | break; |
Mirko Bonadei | 7750d80 | 2021-07-26 15:27:42 | [diff] [blame] | 66 | case kSrtpAeadAes256Gcm: |
| 67 | // kSrtpAeadAes256Gcm is defined in RFC 7714 to use a 256 bits key and |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 68 | // a 96 bits salt for the cipher. |
| 69 | *key_length = 32; |
| 70 | *salt_length = 12; |
| 71 | break; |
| 72 | default: |
| 73 | return false; |
jbauch | cb56065 | 2016-08-04 12:20:32 | [diff] [blame] | 74 | } |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | bool IsGcmCryptoSuite(int crypto_suite) { |
Mirko Bonadei | 7750d80 | 2021-07-26 15:27:42 | [diff] [blame] | 79 | return (crypto_suite == kSrtpAeadAes256Gcm || |
| 80 | crypto_suite == kSrtpAeadAes128Gcm); |
jbauch | cb56065 | 2016-08-04 12:20:32 | [diff] [blame] | 81 | } |
| 82 | |
Harald Alvestrand | 8515d5a | 2020-03-20 21:51:32 | [diff] [blame] | 83 | std::unique_ptr<SSLStreamAdapter> SSLStreamAdapter::Create( |
Tommi | 59574ca | 2023-09-05 07:21:57 | [diff] [blame] | 84 | 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.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 88 | } |
| 89 | |
Guo-wei Shieh | 521ed7b | 2015-11-19 03:41:53 | [diff] [blame] | 90 | bool SSLStreamAdapter::GetSslCipherSuite(int* cipher_suite) { |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 | [diff] [blame] | 91 | return false; |
| 92 | } |
| 93 | |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 94 | bool SSLStreamAdapter::ExportKeyingMaterial(absl::string_view label, |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 95 | const uint8_t* context, |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 | [diff] [blame] | 96 | size_t context_len, |
| 97 | bool use_context, |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 98 | uint8_t* result, |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 | [diff] [blame] | 99 | size_t result_len) { |
| 100 | return false; // Default is unsupported |
| 101 | } |
| 102 | |
Guo-wei Shieh | 521ed7b | 2015-11-19 03:41:53 | [diff] [blame] | 103 | bool SSLStreamAdapter::SetDtlsSrtpCryptoSuites( |
| 104 | const std::vector<int>& crypto_suites) { |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 | [diff] [blame] | 105 | return false; |
| 106 | } |
| 107 | |
Guo-wei Shieh | 521ed7b | 2015-11-19 03:41:53 | [diff] [blame] | 108 | bool SSLStreamAdapter::GetDtlsSrtpCryptoSuite(int* crypto_suite) { |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 | [diff] [blame] | 109 | return false; |
| 110 | } |
| 111 | |
Taylor Brandstetter | 4f0dfbd | 2016-06-16 00:15:23 | [diff] [blame] | 112 | bool SSLStreamAdapter::IsBoringSsl() { |
| 113 | return OpenSSLStreamAdapter::IsBoringSsl(); |
| 114 | } |
torbjorng | 43166b8 | 2016-03-11 08:06:47 | [diff] [blame] | 115 | bool SSLStreamAdapter::IsAcceptableCipher(int cipher, KeyType key_type) { |
| 116 | return OpenSSLStreamAdapter::IsAcceptableCipher(cipher, key_type); |
Guo-wei Shieh | 456696a | 2015-10-01 04:48:54 | [diff] [blame] | 117 | } |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 118 | bool SSLStreamAdapter::IsAcceptableCipher(absl::string_view cipher, |
torbjorng | 43166b8 | 2016-03-11 08:06:47 | [diff] [blame] | 119 | KeyType key_type) { |
| 120 | return OpenSSLStreamAdapter::IsAcceptableCipher(cipher, key_type); |
| 121 | } |
Guo-wei Shieh | 521ed7b | 2015-11-19 03:41:53 | [diff] [blame] | 122 | std::string SSLStreamAdapter::SslCipherSuiteToName(int cipher_suite) { |
| 123 | return OpenSSLStreamAdapter::SslCipherSuiteToName(cipher_suite); |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 | [diff] [blame] | 124 | } |
Benjamin Wright | b19b497 | 2018-10-25 17:46:49 | [diff] [blame] | 125 | |
| 126 | /////////////////////////////////////////////////////////////////////////////// |
| 127 | // Test only settings |
| 128 | /////////////////////////////////////////////////////////////////////////////// |
| 129 | |
| 130 | void SSLStreamAdapter::EnableTimeCallbackForTesting() { |
| 131 | OpenSSLStreamAdapter::EnableTimeCallbackForTesting(); |
deadbeef | 6cf94a0 | 2016-11-29 01:38:34 | [diff] [blame] | 132 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 133 | |
| 134 | /////////////////////////////////////////////////////////////////////////////// |
| 135 | |
| 136 | } // namespace rtc |