| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 {0, NULL} | 140 {0, NULL} |
| 141 }; | 141 }; |
| 142 #endif // #ifndef OPENSSL_IS_BORINGSSL | 142 #endif // #ifndef OPENSSL_IS_BORINGSSL |
| 143 | 143 |
| 144 #if defined(_MSC_VER) | 144 #if defined(_MSC_VER) |
| 145 #pragma warning(push) | 145 #pragma warning(push) |
| 146 #pragma warning(disable : 4309) | 146 #pragma warning(disable : 4309) |
| 147 #pragma warning(disable : 4310) | 147 #pragma warning(disable : 4310) |
| 148 #endif // defined(_MSC_VER) | 148 #endif // defined(_MSC_VER) |
| 149 | 149 |
| 150 // Default cipher used between OpenSSL/BoringSSL stream adapters. | |
| 151 // This needs to be updated when the default of the SSL library changes. | |
| 152 // static_cast<uint16_t> causes build warnings on windows platform. | |
| 153 static int kDefaultSslCipher10 = | |
| 154 static_cast<uint16_t>(TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA); | |
| 155 static int kDefaultSslEcCipher10 = | |
| 156 static_cast<uint16_t>(TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA); | |
| 157 #ifdef OPENSSL_IS_BORINGSSL | |
| 158 static int kDefaultSslCipher12 = | |
| 159 static_cast<uint16_t>(TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256); | |
| 160 static int kDefaultSslEcCipher12 = | |
| 161 static_cast<uint16_t>(TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256); | |
| 162 // Fallback cipher for DTLS 1.2 if hardware-accelerated AES-GCM is unavailable. | |
| 163 | |
| 164 #ifdef TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 | |
| 165 // This ciphersuite was added in boringssl 13414b3a..., changing the fallback | |
| 166 // ciphersuite. For compatibility during a transitional period, support old | |
| 167 // boringssl versions. TODO(torbjorng): Remove this. | |
| 168 static int kDefaultSslCipher12NoAesGcm = | |
| 169 static_cast<uint16_t>(TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256); | |
| 170 #else | |
| 171 static int kDefaultSslCipher12NoAesGcm = | |
| 172 static_cast<uint16_t>(TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305_OLD); | |
| 173 #endif | |
| 174 | |
| 175 #ifdef TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 | |
| 176 // This ciphersuite was added in boringssl 13414b3a..., changing the fallback | |
| 177 // ciphersuite. For compatibility during a transitional period, support old | |
| 178 // boringssl versions. TODO(torbjorng): Remove this. | |
| 179 static int kDefaultSslEcCipher12NoAesGcm = | |
| 180 static_cast<uint16_t>(TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256); | |
| 181 #else | |
| 182 static int kDefaultSslEcCipher12NoAesGcm = | |
| 183 static_cast<uint16_t>(TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305_OLD); | |
| 184 #endif | |
| 185 | |
| 186 #else // !OPENSSL_IS_BORINGSSL | |
| 187 // OpenSSL sorts differently than BoringSSL, so the default cipher doesn't | |
| 188 // change between TLS 1.0 and TLS 1.2 with the current setup. | |
| 189 static int kDefaultSslCipher12 = | |
| 190 static_cast<uint16_t>(TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA); | |
| 191 static int kDefaultSslEcCipher12 = | |
| 192 static_cast<uint16_t>(TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA); | |
| 193 #endif // OPENSSL_IS_BORINGSSL | |
| 194 | |
| 195 #if defined(_MSC_VER) | 150 #if defined(_MSC_VER) |
| 196 #pragma warning(pop) | 151 #pragma warning(pop) |
| 197 #endif // defined(_MSC_VER) | 152 #endif // defined(_MSC_VER) |
| 198 | 153 |
| 199 ////////////////////////////////////////////////////////////////////// | 154 ////////////////////////////////////////////////////////////////////// |
| 200 // StreamBIO | 155 // StreamBIO |
| 201 ////////////////////////////////////////////////////////////////////// | 156 ////////////////////////////////////////////////////////////////////// |
| 202 | 157 |
| 203 static int stream_write(BIO* h, const char* buf, int num); | 158 static int stream_write(BIO* h, const char* buf, int num); |
| 204 static int stream_read(BIO* h, char* buf, int size); | 159 static int stream_read(BIO* h, char* buf, int size); |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 397 | 352 |
| 398 const SSL_CIPHER* current_cipher = SSL_get_current_cipher(ssl_); | 353 const SSL_CIPHER* current_cipher = SSL_get_current_cipher(ssl_); |
| 399 if (current_cipher == NULL) { | 354 if (current_cipher == NULL) { |
| 400 return false; | 355 return false; |
| 401 } | 356 } |
| 402 | 357 |
| 403 *cipher_suite = static_cast<uint16_t>(SSL_CIPHER_get_id(current_cipher)); | 358 *cipher_suite = static_cast<uint16_t>(SSL_CIPHER_get_id(current_cipher)); |
| 404 return true; | 359 return true; |
| 405 } | 360 } |
| 406 | 361 |
| 362 int OpenSSLStreamAdapter::GetSslVersion() const { |
| 363 if (state_ != SSL_CONNECTED) |
| 364 return -1; |
| 365 |
| 366 int ssl_version = SSL_version(ssl_); |
| 367 if (ssl_mode_ == SSL_MODE_DTLS) { |
| 368 if (ssl_version == DTLS1_VERSION) |
| 369 return SSL_PROTOCOL_DTLS_10; |
| 370 else if (ssl_version == DTLS1_2_VERSION) |
| 371 return SSL_PROTOCOL_DTLS_12; |
| 372 } else { |
| 373 if (ssl_version == TLS1_VERSION) |
| 374 return SSL_PROTOCOL_TLS_10; |
| 375 else if (ssl_version == TLS1_1_VERSION) |
| 376 return SSL_PROTOCOL_TLS_11; |
| 377 else if (ssl_version == TLS1_2_VERSION) |
| 378 return SSL_PROTOCOL_TLS_12; |
| 379 } |
| 380 |
| 381 return -1; |
| 382 } |
| 383 |
| 407 // Key Extractor interface | 384 // Key Extractor interface |
| 408 bool OpenSSLStreamAdapter::ExportKeyingMaterial(const std::string& label, | 385 bool OpenSSLStreamAdapter::ExportKeyingMaterial(const std::string& label, |
| 409 const uint8_t* context, | 386 const uint8_t* context, |
| 410 size_t context_len, | 387 size_t context_len, |
| 411 bool use_context, | 388 bool use_context, |
| 412 uint8_t* result, | 389 uint8_t* result, |
| 413 size_t result_len) { | 390 size_t result_len) { |
| 414 #ifdef HAVE_DTLS_SRTP | 391 #ifdef HAVE_DTLS_SRTP |
| 415 int i; | 392 int i; |
| 416 | 393 |
| (...skipping 723 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1140 } | 1117 } |
| 1141 | 1118 |
| 1142 bool OpenSSLStreamAdapter::HaveExporter() { | 1119 bool OpenSSLStreamAdapter::HaveExporter() { |
| 1143 #ifdef HAVE_DTLS_SRTP | 1120 #ifdef HAVE_DTLS_SRTP |
| 1144 return true; | 1121 return true; |
| 1145 #else | 1122 #else |
| 1146 return false; | 1123 return false; |
| 1147 #endif | 1124 #endif |
| 1148 } | 1125 } |
| 1149 | 1126 |
| 1150 int OpenSSLStreamAdapter::GetDefaultSslCipherForTest(SSLProtocolVersion version, | 1127 #define CDEF(X) \ |
| 1151 KeyType key_type) { | 1128 { static_cast<uint16_t>(TLS1_CK_##X & 0xffff), "TLS_" #X } |
| 1129 |
| 1130 struct cipher_list { |
| 1131 uint16_t cipher; |
| 1132 const char* cipher_str; |
| 1133 }; |
| 1134 |
| 1135 // TODO(torbjorng): Perhaps add more cipher suites to these lists. |
| 1136 static const cipher_list OK_RSA_ciphers[] = { |
| 1137 CDEF(ECDHE_RSA_WITH_AES_128_CBC_SHA), |
| 1138 CDEF(ECDHE_RSA_WITH_AES_256_CBC_SHA), |
| 1139 CDEF(ECDHE_RSA_WITH_AES_128_GCM_SHA256), |
| 1140 #ifdef TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA256 |
| 1141 CDEF(ECDHE_RSA_WITH_AES_256_GCM_SHA256), |
| 1142 #endif |
| 1143 CDEF(ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256), |
| 1144 }; |
| 1145 |
| 1146 static const cipher_list OK_ECDSA_ciphers[] = { |
| 1147 CDEF(ECDHE_ECDSA_WITH_AES_128_CBC_SHA), |
| 1148 CDEF(ECDHE_ECDSA_WITH_AES_256_CBC_SHA), |
| 1149 CDEF(ECDHE_ECDSA_WITH_AES_128_GCM_SHA256), |
| 1150 #ifdef TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA256 |
| 1151 CDEF(ECDHE_ECDSA_WITH_AES_256_GCM_SHA256), |
| 1152 #endif |
| 1153 CDEF(ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256), |
| 1154 }; |
| 1155 #undef CDEF |
| 1156 |
| 1157 bool OpenSSLStreamAdapter::IsAcceptableCipher(int cipher, KeyType key_type) { |
| 1152 if (key_type == KT_RSA) { | 1158 if (key_type == KT_RSA) { |
| 1153 switch (version) { | 1159 for (const cipher_list& c : OK_RSA_ciphers) { |
| 1154 case SSL_PROTOCOL_TLS_10: | 1160 if (cipher == c.cipher) |
| 1155 case SSL_PROTOCOL_TLS_11: | 1161 return true; |
| 1156 return kDefaultSslCipher10; | |
| 1157 case SSL_PROTOCOL_TLS_12: | |
| 1158 default: | |
| 1159 #ifdef OPENSSL_IS_BORINGSSL | |
| 1160 if (EVP_has_aes_hardware()) { | |
| 1161 return kDefaultSslCipher12; | |
| 1162 } else { | |
| 1163 return kDefaultSslCipher12NoAesGcm; | |
| 1164 } | |
| 1165 #else // !OPENSSL_IS_BORINGSSL | |
| 1166 return kDefaultSslCipher12; | |
| 1167 #endif | |
| 1168 } | 1162 } |
| 1169 } else if (key_type == KT_ECDSA) { | 1163 } |
| 1170 switch (version) { | 1164 |
| 1171 case SSL_PROTOCOL_TLS_10: | 1165 if (key_type == KT_ECDSA) { |
| 1172 case SSL_PROTOCOL_TLS_11: | 1166 for (const cipher_list& c : OK_ECDSA_ciphers) { |
| 1173 return kDefaultSslEcCipher10; | 1167 if (cipher == c.cipher) |
| 1174 case SSL_PROTOCOL_TLS_12: | 1168 return true; |
| 1175 default: | |
| 1176 #ifdef OPENSSL_IS_BORINGSSL | |
| 1177 if (EVP_has_aes_hardware()) { | |
| 1178 return kDefaultSslEcCipher12; | |
| 1179 } else { | |
| 1180 return kDefaultSslEcCipher12NoAesGcm; | |
| 1181 } | |
| 1182 #else // !OPENSSL_IS_BORINGSSL | |
| 1183 return kDefaultSslEcCipher12; | |
| 1184 #endif | |
| 1185 } | 1169 } |
| 1186 } else { | |
| 1187 RTC_NOTREACHED(); | |
| 1188 return kDefaultSslEcCipher12; | |
| 1189 } | 1170 } |
| 1171 |
| 1172 return false; |
| 1173 } |
| 1174 |
| 1175 bool OpenSSLStreamAdapter::IsAcceptableCipher(const std::string& cipher, |
| 1176 KeyType key_type) { |
| 1177 if (key_type == KT_RSA) { |
| 1178 for (const cipher_list& c : OK_RSA_ciphers) { |
| 1179 if (cipher == c.cipher_str) |
| 1180 return true; |
| 1181 } |
| 1182 } |
| 1183 |
| 1184 if (key_type == KT_ECDSA) { |
| 1185 for (const cipher_list& c : OK_ECDSA_ciphers) { |
| 1186 if (cipher == c.cipher_str) |
| 1187 return true; |
| 1188 } |
| 1189 } |
| 1190 |
| 1191 return false; |
| 1190 } | 1192 } |
| 1191 | 1193 |
| 1192 } // namespace rtc | 1194 } // namespace rtc |
| 1193 | 1195 |
| 1194 #endif // HAVE_OPENSSL_SSL_H | 1196 #endif // HAVE_OPENSSL_SSL_H |
| OLD | NEW |