blob: 746ebd50063dca8bfae68985dbdb6eed7f532e0e [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/sslstreamadapter.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "rtc_base/opensslstreamadapter.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000014
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015///////////////////////////////////////////////////////////////////////////////
16
17namespace rtc {
18
Guo-wei Shieh456696a2015-09-30 21:48:54 -070019// TODO(guoweis): Move this to SDP layer and use int form internally.
20// webrtc:5043.
21const char CS_AES_CM_128_HMAC_SHA1_80[] = "AES_CM_128_HMAC_SHA1_80";
22const char CS_AES_CM_128_HMAC_SHA1_32[] = "AES_CM_128_HMAC_SHA1_32";
jbauchcb560652016-08-04 05:20:32 -070023const char CS_AEAD_AES_128_GCM[] = "AEAD_AES_128_GCM";
24const char CS_AEAD_AES_256_GCM[] = "AEAD_AES_256_GCM";
Guo-wei Shieh456696a2015-09-30 21:48:54 -070025
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080026std::string SrtpCryptoSuiteToName(int crypto_suite) {
jbauchcb560652016-08-04 05:20:32 -070027 switch (crypto_suite) {
Yves Gerey665174f2018-06-19 15:03:05 +020028 case SRTP_AES128_CM_SHA1_32:
29 return CS_AES_CM_128_HMAC_SHA1_32;
30 case SRTP_AES128_CM_SHA1_80:
31 return CS_AES_CM_128_HMAC_SHA1_80;
32 case SRTP_AEAD_AES_128_GCM:
33 return CS_AEAD_AES_128_GCM;
34 case SRTP_AEAD_AES_256_GCM:
35 return CS_AEAD_AES_256_GCM;
36 default:
37 return std::string();
jbauchcb560652016-08-04 05:20:32 -070038 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080039}
40
41int SrtpCryptoSuiteFromName(const std::string& crypto_suite) {
42 if (crypto_suite == CS_AES_CM_128_HMAC_SHA1_32)
Guo-wei Shieh456696a2015-09-30 21:48:54 -070043 return SRTP_AES128_CM_SHA1_32;
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080044 if (crypto_suite == CS_AES_CM_128_HMAC_SHA1_80)
Guo-wei Shieh456696a2015-09-30 21:48:54 -070045 return SRTP_AES128_CM_SHA1_80;
jbauchcb560652016-08-04 05:20:32 -070046 if (crypto_suite == CS_AEAD_AES_128_GCM)
47 return SRTP_AEAD_AES_128_GCM;
48 if (crypto_suite == CS_AEAD_AES_256_GCM)
49 return SRTP_AEAD_AES_256_GCM;
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080050 return SRTP_INVALID_CRYPTO_SUITE;
Guo-wei Shieh456696a2015-09-30 21:48:54 -070051}
52
Yves Gerey665174f2018-06-19 15:03:05 +020053bool GetSrtpKeyAndSaltLengths(int crypto_suite,
54 int* key_length,
55 int* salt_length) {
jbauchcb560652016-08-04 05:20:32 -070056 switch (crypto_suite) {
Yves Gerey665174f2018-06-19 15:03:05 +020057 case SRTP_AES128_CM_SHA1_32:
58 case SRTP_AES128_CM_SHA1_80:
59 // SRTP_AES128_CM_HMAC_SHA1_32 and SRTP_AES128_CM_HMAC_SHA1_80 are defined
60 // in RFC 5764 to use a 128 bits key and 112 bits salt for the cipher.
61 *key_length = 16;
62 *salt_length = 14;
63 break;
64 case SRTP_AEAD_AES_128_GCM:
65 // SRTP_AEAD_AES_128_GCM is defined in RFC 7714 to use a 128 bits key and
66 // a 96 bits salt for the cipher.
67 *key_length = 16;
68 *salt_length = 12;
69 break;
70 case SRTP_AEAD_AES_256_GCM:
71 // SRTP_AEAD_AES_256_GCM is defined in RFC 7714 to use a 256 bits key and
72 // a 96 bits salt for the cipher.
73 *key_length = 32;
74 *salt_length = 12;
75 break;
76 default:
77 return false;
jbauchcb560652016-08-04 05:20:32 -070078 }
79 return true;
80}
81
82bool IsGcmCryptoSuite(int crypto_suite) {
83 return (crypto_suite == SRTP_AEAD_AES_256_GCM ||
84 crypto_suite == SRTP_AEAD_AES_128_GCM);
85}
86
87bool IsGcmCryptoSuiteName(const std::string& crypto_suite) {
88 return (crypto_suite == CS_AEAD_AES_256_GCM ||
89 crypto_suite == CS_AEAD_AES_128_GCM);
90}
91
92// static
93CryptoOptions CryptoOptions::NoGcm() {
94 CryptoOptions options;
95 options.enable_gcm_crypto_suites = false;
96 return options;
97}
98
deadbeef7914b8c2017-04-21 03:23:33 -070099std::vector<int> GetSupportedDtlsSrtpCryptoSuites(
100 const rtc::CryptoOptions& crypto_options) {
101 std::vector<int> crypto_suites;
102 if (crypto_options.enable_gcm_crypto_suites) {
103 crypto_suites.push_back(rtc::SRTP_AEAD_AES_256_GCM);
104 crypto_suites.push_back(rtc::SRTP_AEAD_AES_128_GCM);
105 }
106 // Note: SRTP_AES128_CM_SHA1_80 is what is required to be supported (by
107 // draft-ietf-rtcweb-security-arch), but SRTP_AES128_CM_SHA1_32 is allowed as
108 // well, and saves a few bytes per packet if it ends up selected.
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -0700109 // As the cipher suite is potentially insecure, it will only be used if
110 // enabled by both peers.
111 if (crypto_options.enable_aes128_sha1_32_crypto_cipher) {
112 crypto_suites.push_back(rtc::SRTP_AES128_CM_SHA1_32);
113 }
deadbeef7914b8c2017-04-21 03:23:33 -0700114 crypto_suites.push_back(rtc::SRTP_AES128_CM_SHA1_80);
115 return crypto_suites;
116}
117
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000118SSLStreamAdapter* SSLStreamAdapter::Create(StreamInterface* stream) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000119 return new OpenSSLStreamAdapter(stream);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000120}
121
zhihuangd82eee02016-08-26 11:25:05 -0700122SSLStreamAdapter::SSLStreamAdapter(StreamInterface* stream)
123 : StreamAdapterInterface(stream),
124 ignore_bad_cert_(false),
125 client_auth_enabled_(true) {}
126
127SSLStreamAdapter::~SSLStreamAdapter() {}
128
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800129bool SSLStreamAdapter::GetSslCipherSuite(int* cipher_suite) {
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000130 return false;
131}
132
133bool SSLStreamAdapter::ExportKeyingMaterial(const std::string& label,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200134 const uint8_t* context,
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000135 size_t context_len,
136 bool use_context,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200137 uint8_t* result,
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000138 size_t result_len) {
139 return false; // Default is unsupported
140}
141
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800142bool SSLStreamAdapter::SetDtlsSrtpCryptoSuites(
143 const std::vector<int>& crypto_suites) {
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000144 return false;
145}
146
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800147bool SSLStreamAdapter::GetDtlsSrtpCryptoSuite(int* crypto_suite) {
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000148 return false;
149}
150
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -0700151bool SSLStreamAdapter::IsBoringSsl() {
152 return OpenSSLStreamAdapter::IsBoringSsl();
153}
torbjorng43166b82016-03-11 00:06:47 -0800154bool SSLStreamAdapter::IsAcceptableCipher(int cipher, KeyType key_type) {
155 return OpenSSLStreamAdapter::IsAcceptableCipher(cipher, key_type);
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700156}
torbjorng43166b82016-03-11 00:06:47 -0800157bool SSLStreamAdapter::IsAcceptableCipher(const std::string& cipher,
158 KeyType key_type) {
159 return OpenSSLStreamAdapter::IsAcceptableCipher(cipher, key_type);
160}
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800161std::string SSLStreamAdapter::SslCipherSuiteToName(int cipher_suite) {
162 return OpenSSLStreamAdapter::SslCipherSuiteToName(cipher_suite);
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000163}
deadbeef6cf94a02016-11-28 17:38:34 -0800164void SSLStreamAdapter::enable_time_callback_for_testing() {
165 OpenSSLStreamAdapter::enable_time_callback_for_testing();
166}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000167
168///////////////////////////////////////////////////////////////////////////////
169
170} // namespace rtc