blob: 354622e6f04cafd57b9d01cc0fe3781574f775e6 [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.
22const char CS_AES_CM_128_HMAC_SHA1_80[] = "AES_CM_128_HMAC_SHA1_80";
23const char CS_AES_CM_128_HMAC_SHA1_32[] = "AES_CM_128_HMAC_SHA1_32";
jbauchcb560652016-08-04 05:20:32 -070024const char CS_AEAD_AES_128_GCM[] = "AEAD_AES_128_GCM";
25const char CS_AEAD_AES_256_GCM[] = "AEAD_AES_256_GCM";
Guo-wei Shieh456696a2015-09-30 21:48:54 -070026
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080027std::string SrtpCryptoSuiteToName(int crypto_suite) {
jbauchcb560652016-08-04 05:20:32 -070028 switch (crypto_suite) {
Yves Gerey665174f2018-06-19 15:03:05 +020029 case SRTP_AES128_CM_SHA1_32:
30 return CS_AES_CM_128_HMAC_SHA1_32;
31 case SRTP_AES128_CM_SHA1_80:
32 return CS_AES_CM_128_HMAC_SHA1_80;
33 case SRTP_AEAD_AES_128_GCM:
34 return CS_AEAD_AES_128_GCM;
35 case SRTP_AEAD_AES_256_GCM:
36 return CS_AEAD_AES_256_GCM;
37 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) {
43 if (crypto_suite == CS_AES_CM_128_HMAC_SHA1_32)
Guo-wei Shieh456696a2015-09-30 21:48:54 -070044 return SRTP_AES128_CM_SHA1_32;
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080045 if (crypto_suite == CS_AES_CM_128_HMAC_SHA1_80)
Guo-wei Shieh456696a2015-09-30 21:48:54 -070046 return SRTP_AES128_CM_SHA1_80;
jbauchcb560652016-08-04 05:20:32 -070047 if (crypto_suite == CS_AEAD_AES_128_GCM)
48 return SRTP_AEAD_AES_128_GCM;
49 if (crypto_suite == CS_AEAD_AES_256_GCM)
50 return SRTP_AEAD_AES_256_GCM;
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080051 return SRTP_INVALID_CRYPTO_SUITE;
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) {
Yves Gerey665174f2018-06-19 15:03:05 +020058 case SRTP_AES128_CM_SHA1_32:
59 case SRTP_AES128_CM_SHA1_80:
60 // 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;
65 case SRTP_AEAD_AES_128_GCM:
66 // SRTP_AEAD_AES_128_GCM is defined in RFC 7714 to use a 128 bits key and
67 // a 96 bits salt for the cipher.
68 *key_length = 16;
69 *salt_length = 12;
70 break;
71 case SRTP_AEAD_AES_256_GCM:
72 // SRTP_AEAD_AES_256_GCM is defined in RFC 7714 to use a 256 bits key and
73 // 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) {
84 return (crypto_suite == SRTP_AEAD_AES_256_GCM ||
85 crypto_suite == SRTP_AEAD_AES_128_GCM);
86}
87
88bool IsGcmCryptoSuiteName(const std::string& crypto_suite) {
89 return (crypto_suite == CS_AEAD_AES_256_GCM ||
90 crypto_suite == CS_AEAD_AES_128_GCM);
91}
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
Harald Alvestrand8515d5a2020-03-20 22:51:32 +010098SSLStreamAdapter::SSLStreamAdapter(std::unique_ptr<StreamInterface> stream)
99 : StreamAdapterInterface(stream.release()) {}
zhihuangd82eee02016-08-26 11:25:05 -0700100
101SSLStreamAdapter::~SSLStreamAdapter() {}
102
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800103bool SSLStreamAdapter::GetSslCipherSuite(int* cipher_suite) {
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000104 return false;
105}
106
107bool SSLStreamAdapter::ExportKeyingMaterial(const std::string& label,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200108 const uint8_t* context,
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000109 size_t context_len,
110 bool use_context,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200111 uint8_t* result,
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000112 size_t result_len) {
113 return false; // Default is unsupported
114}
115
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800116bool SSLStreamAdapter::SetDtlsSrtpCryptoSuites(
117 const std::vector<int>& crypto_suites) {
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000118 return false;
119}
120
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800121bool SSLStreamAdapter::GetDtlsSrtpCryptoSuite(int* crypto_suite) {
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000122 return false;
123}
124
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -0700125bool SSLStreamAdapter::IsBoringSsl() {
126 return OpenSSLStreamAdapter::IsBoringSsl();
127}
torbjorng43166b82016-03-11 00:06:47 -0800128bool SSLStreamAdapter::IsAcceptableCipher(int cipher, KeyType key_type) {
129 return OpenSSLStreamAdapter::IsAcceptableCipher(cipher, key_type);
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700130}
torbjorng43166b82016-03-11 00:06:47 -0800131bool SSLStreamAdapter::IsAcceptableCipher(const std::string& cipher,
132 KeyType key_type) {
133 return OpenSSLStreamAdapter::IsAcceptableCipher(cipher, key_type);
134}
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800135std::string SSLStreamAdapter::SslCipherSuiteToName(int cipher_suite) {
136 return OpenSSLStreamAdapter::SslCipherSuiteToName(cipher_suite);
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000137}
Benjamin Wrightb19b4972018-10-25 10:46:49 -0700138
139///////////////////////////////////////////////////////////////////////////////
140// Test only settings
141///////////////////////////////////////////////////////////////////////////////
142
143void SSLStreamAdapter::EnableTimeCallbackForTesting() {
144 OpenSSLStreamAdapter::EnableTimeCallbackForTesting();
deadbeef6cf94a02016-11-28 17:38:34 -0800145}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000146
147///////////////////////////////////////////////////////////////////////////////
148
149} // namespace rtc