henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/sslstreamadapter.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "rtc_base/opensslstreamadapter.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 14 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 15 | /////////////////////////////////////////////////////////////////////////////// |
| 16 | |
| 17 | namespace rtc { |
| 18 | |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 19 | // TODO(guoweis): Move this to SDP layer and use int form internally. |
| 20 | // webrtc:5043. |
| 21 | const char CS_AES_CM_128_HMAC_SHA1_80[] = "AES_CM_128_HMAC_SHA1_80"; |
| 22 | const char CS_AES_CM_128_HMAC_SHA1_32[] = "AES_CM_128_HMAC_SHA1_32"; |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 23 | const char CS_AEAD_AES_128_GCM[] = "AEAD_AES_128_GCM"; |
| 24 | const char CS_AEAD_AES_256_GCM[] = "AEAD_AES_256_GCM"; |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 25 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 26 | std::string SrtpCryptoSuiteToName(int crypto_suite) { |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 27 | switch (crypto_suite) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 28 | 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(); |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 38 | } |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | int SrtpCryptoSuiteFromName(const std::string& crypto_suite) { |
| 42 | if (crypto_suite == CS_AES_CM_128_HMAC_SHA1_32) |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 43 | return SRTP_AES128_CM_SHA1_32; |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 44 | if (crypto_suite == CS_AES_CM_128_HMAC_SHA1_80) |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 45 | return SRTP_AES128_CM_SHA1_80; |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 46 | 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 Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 50 | return SRTP_INVALID_CRYPTO_SUITE; |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 53 | bool GetSrtpKeyAndSaltLengths(int crypto_suite, |
| 54 | int* key_length, |
| 55 | int* salt_length) { |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 56 | switch (crypto_suite) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 57 | 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; |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 78 | } |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | bool IsGcmCryptoSuite(int crypto_suite) { |
| 83 | return (crypto_suite == SRTP_AEAD_AES_256_GCM || |
| 84 | crypto_suite == SRTP_AEAD_AES_128_GCM); |
| 85 | } |
| 86 | |
| 87 | bool 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 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 92 | SSLStreamAdapter* SSLStreamAdapter::Create(StreamInterface* stream) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 93 | return new OpenSSLStreamAdapter(stream); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 94 | } |
| 95 | |
zhihuang | d82eee0 | 2016-08-26 11:25:05 -0700 | [diff] [blame] | 96 | SSLStreamAdapter::SSLStreamAdapter(StreamInterface* stream) |
| 97 | : StreamAdapterInterface(stream), |
| 98 | ignore_bad_cert_(false), |
| 99 | client_auth_enabled_(true) {} |
| 100 | |
| 101 | SSLStreamAdapter::~SSLStreamAdapter() {} |
| 102 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 103 | bool SSLStreamAdapter::GetSslCipherSuite(int* cipher_suite) { |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 104 | return false; |
| 105 | } |
| 106 | |
| 107 | bool SSLStreamAdapter::ExportKeyingMaterial(const std::string& label, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 108 | const uint8_t* context, |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 109 | size_t context_len, |
| 110 | bool use_context, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 111 | uint8_t* result, |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 112 | size_t result_len) { |
| 113 | return false; // Default is unsupported |
| 114 | } |
| 115 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 116 | bool SSLStreamAdapter::SetDtlsSrtpCryptoSuites( |
| 117 | const std::vector<int>& crypto_suites) { |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 118 | return false; |
| 119 | } |
| 120 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 121 | bool SSLStreamAdapter::GetDtlsSrtpCryptoSuite(int* crypto_suite) { |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 122 | return false; |
| 123 | } |
| 124 | |
Taylor Brandstetter | 4f0dfbd | 2016-06-15 17:15:23 -0700 | [diff] [blame] | 125 | bool SSLStreamAdapter::IsBoringSsl() { |
| 126 | return OpenSSLStreamAdapter::IsBoringSsl(); |
| 127 | } |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 128 | bool SSLStreamAdapter::IsAcceptableCipher(int cipher, KeyType key_type) { |
| 129 | return OpenSSLStreamAdapter::IsAcceptableCipher(cipher, key_type); |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 130 | } |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 131 | bool SSLStreamAdapter::IsAcceptableCipher(const std::string& cipher, |
| 132 | KeyType key_type) { |
| 133 | return OpenSSLStreamAdapter::IsAcceptableCipher(cipher, key_type); |
| 134 | } |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 135 | std::string SSLStreamAdapter::SslCipherSuiteToName(int cipher_suite) { |
| 136 | return OpenSSLStreamAdapter::SslCipherSuiteToName(cipher_suite); |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 137 | } |
deadbeef | 6cf94a0 | 2016-11-28 17:38:34 -0800 | [diff] [blame] | 138 | void SSLStreamAdapter::enable_time_callback_for_testing() { |
| 139 | OpenSSLStreamAdapter::enable_time_callback_for_testing(); |
| 140 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 141 | |
| 142 | /////////////////////////////////////////////////////////////////////////////// |
| 143 | |
| 144 | } // namespace rtc |