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 69 matching lines...) Loading... |
80 | 80 |
81 #define DEFINE_CIPHER_ENTRY_SSL3(name) {SSL3_CK_##name, "TLS_"#name} | 81 #define DEFINE_CIPHER_ENTRY_SSL3(name) {SSL3_CK_##name, "TLS_"#name} |
82 #define DEFINE_CIPHER_ENTRY_TLS1(name) {TLS1_CK_##name, "TLS_"#name} | 82 #define DEFINE_CIPHER_ENTRY_TLS1(name) {TLS1_CK_##name, "TLS_"#name} |
83 | 83 |
84 // There currently is no method available to get a RFC-compliant name for a | 84 // There currently is no method available to get a RFC-compliant name for a |
85 // cipher suite from BoringSSL, so we need to define the mapping manually here. | 85 // cipher suite from BoringSSL, so we need to define the mapping manually here. |
86 // This should go away once BoringSSL supports "SSL_CIPHER_standard_name" | 86 // This should go away once BoringSSL supports "SSL_CIPHER_standard_name" |
87 // (as available in OpenSSL if compiled with tracing enabled) or a similar | 87 // (as available in OpenSSL if compiled with tracing enabled) or a similar |
88 // method. | 88 // method. |
89 static const SslCipherMapEntry kSslCipherMap[] = { | 89 static const SslCipherMapEntry kSslCipherMap[] = { |
90 // TLS v1.0 ciphersuites from RFC2246. | 90 // TLS v1.0 ciphersuites from RFC2246. |
91 DEFINE_CIPHER_ENTRY_SSL3(RSA_RC4_128_SHA), | 91 DEFINE_CIPHER_ENTRY_SSL3(RSA_RC4_128_SHA), |
92 {SSL3_CK_RSA_DES_192_CBC3_SHA, | 92 {SSL3_CK_RSA_DES_192_CBC3_SHA, "TLS_RSA_WITH_3DES_EDE_CBC_SHA"}, |
93 "TLS_RSA_WITH_3DES_EDE_CBC_SHA"}, | |
94 | 93 |
95 // AES ciphersuites from RFC3268. | 94 // AES ciphersuites from RFC3268. |
96 {TLS1_CK_RSA_WITH_AES_128_SHA, | 95 {TLS1_CK_RSA_WITH_AES_128_SHA, "TLS_RSA_WITH_AES_128_CBC_SHA"}, |
97 "TLS_RSA_WITH_AES_128_CBC_SHA"}, | 96 {TLS1_CK_DHE_RSA_WITH_AES_128_SHA, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA"}, |
98 {TLS1_CK_DHE_RSA_WITH_AES_128_SHA, | 97 {TLS1_CK_RSA_WITH_AES_256_SHA, "TLS_RSA_WITH_AES_256_CBC_SHA"}, |
99 "TLS_DHE_RSA_WITH_AES_128_CBC_SHA"}, | 98 {TLS1_CK_DHE_RSA_WITH_AES_256_SHA, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA"}, |
100 {TLS1_CK_RSA_WITH_AES_256_SHA, | |
101 "TLS_RSA_WITH_AES_256_CBC_SHA"}, | |
102 {TLS1_CK_DHE_RSA_WITH_AES_256_SHA, | |
103 "TLS_DHE_RSA_WITH_AES_256_CBC_SHA"}, | |
104 | 99 |
105 // ECC ciphersuites from RFC4492. | 100 // ECC ciphersuites from RFC4492. |
106 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_RC4_128_SHA), | 101 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_RC4_128_SHA), |
107 {TLS1_CK_ECDHE_ECDSA_WITH_DES_192_CBC3_SHA, | 102 {TLS1_CK_ECDHE_ECDSA_WITH_DES_192_CBC3_SHA, |
108 "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA"}, | 103 "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA"}, |
109 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_AES_128_CBC_SHA), | 104 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_AES_128_CBC_SHA), |
110 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_AES_256_CBC_SHA), | 105 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_AES_256_CBC_SHA), |
111 | 106 |
112 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_RC4_128_SHA), | 107 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_RC4_128_SHA), |
113 {TLS1_CK_ECDHE_RSA_WITH_DES_192_CBC3_SHA, | 108 {TLS1_CK_ECDHE_RSA_WITH_DES_192_CBC3_SHA, |
114 "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA"}, | 109 "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA"}, |
115 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_AES_128_CBC_SHA), | 110 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_AES_128_CBC_SHA), |
116 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_AES_256_CBC_SHA), | 111 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_AES_256_CBC_SHA), |
117 | 112 |
118 // TLS v1.2 ciphersuites. | 113 // TLS v1.2 ciphersuites. |
119 {TLS1_CK_RSA_WITH_AES_128_SHA256, | 114 {TLS1_CK_RSA_WITH_AES_128_SHA256, "TLS_RSA_WITH_AES_128_CBC_SHA256"}, |
120 "TLS_RSA_WITH_AES_128_CBC_SHA256"}, | 115 {TLS1_CK_RSA_WITH_AES_256_SHA256, "TLS_RSA_WITH_AES_256_CBC_SHA256"}, |
121 {TLS1_CK_RSA_WITH_AES_256_SHA256, | 116 {TLS1_CK_DHE_RSA_WITH_AES_128_SHA256, |
122 "TLS_RSA_WITH_AES_256_CBC_SHA256"}, | 117 "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256"}, |
123 {TLS1_CK_DHE_RSA_WITH_AES_128_SHA256, | 118 {TLS1_CK_DHE_RSA_WITH_AES_256_SHA256, |
124 "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256"}, | 119 "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256"}, |
125 {TLS1_CK_DHE_RSA_WITH_AES_256_SHA256, | |
126 "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256"}, | |
127 | 120 |
128 // TLS v1.2 GCM ciphersuites from RFC5288. | 121 // TLS v1.2 GCM ciphersuites from RFC5288. |
129 DEFINE_CIPHER_ENTRY_TLS1(RSA_WITH_AES_128_GCM_SHA256), | 122 DEFINE_CIPHER_ENTRY_TLS1(RSA_WITH_AES_128_GCM_SHA256), |
130 DEFINE_CIPHER_ENTRY_TLS1(RSA_WITH_AES_256_GCM_SHA384), | 123 DEFINE_CIPHER_ENTRY_TLS1(RSA_WITH_AES_256_GCM_SHA384), |
131 DEFINE_CIPHER_ENTRY_TLS1(DHE_RSA_WITH_AES_128_GCM_SHA256), | 124 DEFINE_CIPHER_ENTRY_TLS1(DHE_RSA_WITH_AES_128_GCM_SHA256), |
132 DEFINE_CIPHER_ENTRY_TLS1(DHE_RSA_WITH_AES_256_GCM_SHA384), | 125 DEFINE_CIPHER_ENTRY_TLS1(DHE_RSA_WITH_AES_256_GCM_SHA384), |
133 DEFINE_CIPHER_ENTRY_TLS1(DH_RSA_WITH_AES_128_GCM_SHA256), | 126 DEFINE_CIPHER_ENTRY_TLS1(DH_RSA_WITH_AES_128_GCM_SHA256), |
134 DEFINE_CIPHER_ENTRY_TLS1(DH_RSA_WITH_AES_256_GCM_SHA384), | 127 DEFINE_CIPHER_ENTRY_TLS1(DH_RSA_WITH_AES_256_GCM_SHA384), |
135 | 128 |
136 // ECDH HMAC based ciphersuites from RFC5289. | 129 // ECDH HMAC based ciphersuites from RFC5289. |
137 {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256, | 130 {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256, |
138 "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256"}, | 131 "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256"}, |
139 {TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384, | 132 {TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384, |
140 "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384"}, | 133 "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384"}, |
141 {TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256, | 134 {TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256, |
142 "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256"}, | 135 "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256"}, |
143 {TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384, | 136 {TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384, |
144 "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384"}, | 137 "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384"}, |
145 | 138 |
146 // ECDH GCM based ciphersuites from RFC5289. | 139 // ECDH GCM based ciphersuites from RFC5289. |
147 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_AES_128_GCM_SHA256), | 140 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_AES_128_GCM_SHA256), |
148 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_AES_256_GCM_SHA384), | 141 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_AES_256_GCM_SHA384), |
149 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_AES_128_GCM_SHA256), | 142 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_AES_128_GCM_SHA256), |
150 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_AES_256_GCM_SHA384), | 143 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_AES_256_GCM_SHA384), |
151 | 144 |
152 {0, NULL} | 145 {0, nullptr}}; |
153 }; | |
154 #endif // #ifndef OPENSSL_IS_BORINGSSL | 146 #endif // #ifndef OPENSSL_IS_BORINGSSL |
155 | 147 |
156 #if defined(_MSC_VER) | 148 #if defined(_MSC_VER) |
157 #pragma warning(push) | 149 #pragma warning(push) |
158 #pragma warning(disable : 4309) | 150 #pragma warning(disable : 4309) |
159 #pragma warning(disable : 4310) | 151 #pragma warning(disable : 4310) |
160 #endif // defined(_MSC_VER) | 152 #endif // defined(_MSC_VER) |
161 | 153 |
162 #if defined(_MSC_VER) | 154 #if defined(_MSC_VER) |
163 #pragma warning(pop) | 155 #pragma warning(pop) |
164 #endif // defined(_MSC_VER) | 156 #endif // defined(_MSC_VER) |
165 | 157 |
166 ////////////////////////////////////////////////////////////////////// | 158 ////////////////////////////////////////////////////////////////////// |
167 // StreamBIO | 159 // StreamBIO |
168 ////////////////////////////////////////////////////////////////////// | 160 ////////////////////////////////////////////////////////////////////// |
169 | 161 |
170 static int stream_write(BIO* h, const char* buf, int num); | 162 static int stream_write(BIO* h, const char* buf, int num); |
171 static int stream_read(BIO* h, char* buf, int size); | 163 static int stream_read(BIO* h, char* buf, int size); |
172 static int stream_puts(BIO* h, const char* str); | 164 static int stream_puts(BIO* h, const char* str); |
173 static long stream_ctrl(BIO* h, int cmd, long arg1, void* arg2); | 165 static long stream_ctrl(BIO* h, int cmd, long arg1, void* arg2); |
174 static int stream_new(BIO* h); | 166 static int stream_new(BIO* h); |
175 static int stream_free(BIO* data); | 167 static int stream_free(BIO* data); |
176 | 168 |
177 // TODO(davidben): This should be const once BoringSSL is assumed. | 169 // TODO(davidben): This should be const once BoringSSL is assumed. |
178 static BIO_METHOD methods_stream = { | 170 static BIO_METHOD methods_stream = { |
179 BIO_TYPE_BIO, | 171 BIO_TYPE_BIO, "stream", stream_write, stream_read, stream_puts, 0, |
180 "stream", | 172 stream_ctrl, stream_new, stream_free, nullptr, |
181 stream_write, | |
182 stream_read, | |
183 stream_puts, | |
184 0, | |
185 stream_ctrl, | |
186 stream_new, | |
187 stream_free, | |
188 NULL, | |
189 }; | 173 }; |
190 | 174 |
191 static BIO_METHOD* BIO_s_stream() { return(&methods_stream); } | 175 static BIO_METHOD* BIO_s_stream() { return(&methods_stream); } |
192 | 176 |
193 static BIO* BIO_new_stream(StreamInterface* stream) { | 177 static BIO* BIO_new_stream(StreamInterface* stream) { |
194 BIO* ret = BIO_new(BIO_s_stream()); | 178 BIO* ret = BIO_new(BIO_s_stream()); |
195 if (ret == NULL) | 179 if (ret == nullptr) |
196 return NULL; | 180 return nullptr; |
197 ret->ptr = stream; | 181 ret->ptr = stream; |
198 return ret; | 182 return ret; |
199 } | 183 } |
200 | 184 |
201 // bio methods return 1 (or at least non-zero) on success and 0 on failure. | 185 // bio methods return 1 (or at least non-zero) on success and 0 on failure. |
202 | 186 |
203 static int stream_new(BIO* b) { | 187 static int stream_new(BIO* b) { |
204 b->shutdown = 0; | 188 b->shutdown = 0; |
205 b->init = 1; | 189 b->init = 1; |
206 b->num = 0; // 1 means end-of-stream | 190 b->num = 0; // 1 means end-of-stream |
207 b->ptr = 0; | 191 b->ptr = 0; |
208 return 1; | 192 return 1; |
209 } | 193 } |
210 | 194 |
211 static int stream_free(BIO* b) { | 195 static int stream_free(BIO* b) { |
212 if (b == NULL) | 196 if (b == nullptr) |
213 return 0; | 197 return 0; |
214 return 1; | 198 return 1; |
215 } | 199 } |
216 | 200 |
217 static int stream_read(BIO* b, char* out, int outl) { | 201 static int stream_read(BIO* b, char* out, int outl) { |
218 if (!out) | 202 if (!out) |
219 return -1; | 203 return -1; |
220 StreamInterface* stream = static_cast<StreamInterface*>(b->ptr); | 204 StreamInterface* stream = static_cast<StreamInterface*>(b->ptr); |
221 BIO_clear_retry_flags(b); | 205 BIO_clear_retry_flags(b); |
222 size_t read; | 206 size_t read; |
(...skipping 57 matching lines...) Loading... |
280 ///////////////////////////////////////////////////////////////////////////// | 264 ///////////////////////////////////////////////////////////////////////////// |
281 // OpenSSLStreamAdapter | 265 // OpenSSLStreamAdapter |
282 ///////////////////////////////////////////////////////////////////////////// | 266 ///////////////////////////////////////////////////////////////////////////// |
283 | 267 |
284 OpenSSLStreamAdapter::OpenSSLStreamAdapter(StreamInterface* stream) | 268 OpenSSLStreamAdapter::OpenSSLStreamAdapter(StreamInterface* stream) |
285 : SSLStreamAdapter(stream), | 269 : SSLStreamAdapter(stream), |
286 state_(SSL_NONE), | 270 state_(SSL_NONE), |
287 role_(SSL_CLIENT), | 271 role_(SSL_CLIENT), |
288 ssl_read_needs_write_(false), | 272 ssl_read_needs_write_(false), |
289 ssl_write_needs_read_(false), | 273 ssl_write_needs_read_(false), |
290 ssl_(NULL), | 274 ssl_(nullptr), |
291 ssl_ctx_(NULL), | 275 ssl_ctx_(nullptr), |
292 ssl_mode_(SSL_MODE_TLS), | 276 ssl_mode_(SSL_MODE_TLS), |
293 ssl_max_version_(SSL_PROTOCOL_TLS_12) {} | 277 ssl_max_version_(SSL_PROTOCOL_TLS_12) {} |
294 | 278 |
295 OpenSSLStreamAdapter::~OpenSSLStreamAdapter() { | 279 OpenSSLStreamAdapter::~OpenSSLStreamAdapter() { |
296 Cleanup(0); | 280 Cleanup(0); |
297 } | 281 } |
298 | 282 |
299 void OpenSSLStreamAdapter::SetIdentity(SSLIdentity* identity) { | 283 void OpenSSLStreamAdapter::SetIdentity(SSLIdentity* identity) { |
300 RTC_DCHECK(!identity_); | 284 RTC_DCHECK(!identity_); |
301 identity_.reset(static_cast<OpenSSLIdentity*>(identity)); | 285 identity_.reset(static_cast<OpenSSLIdentity*>(identity)); |
(...skipping 82 matching lines...) Loading... |
384 } | 368 } |
385 return std::string(); | 369 return std::string(); |
386 #endif | 370 #endif |
387 } | 371 } |
388 | 372 |
389 bool OpenSSLStreamAdapter::GetSslCipherSuite(int* cipher_suite) { | 373 bool OpenSSLStreamAdapter::GetSslCipherSuite(int* cipher_suite) { |
390 if (state_ != SSL_CONNECTED) | 374 if (state_ != SSL_CONNECTED) |
391 return false; | 375 return false; |
392 | 376 |
393 const SSL_CIPHER* current_cipher = SSL_get_current_cipher(ssl_); | 377 const SSL_CIPHER* current_cipher = SSL_get_current_cipher(ssl_); |
394 if (current_cipher == NULL) { | 378 if (current_cipher == nullptr) { |
395 return false; | 379 return false; |
396 } | 380 } |
397 | 381 |
398 *cipher_suite = static_cast<uint16_t>(SSL_CIPHER_get_id(current_cipher)); | 382 *cipher_suite = static_cast<uint16_t>(SSL_CIPHER_get_id(current_cipher)); |
399 return true; | 383 return true; |
400 } | 384 } |
401 | 385 |
402 int OpenSSLStreamAdapter::GetSslVersion() const { | 386 int OpenSSLStreamAdapter::GetSslVersion() const { |
403 if (state_ != SSL_CONNECTED) | 387 if (state_ != SSL_CONNECTED) |
404 return -1; | 388 return -1; |
(...skipping 108 matching lines...) Loading... |
513 | 497 |
514 return 0; | 498 return 0; |
515 } | 499 } |
516 | 500 |
517 void OpenSSLStreamAdapter::SetMode(SSLMode mode) { | 501 void OpenSSLStreamAdapter::SetMode(SSLMode mode) { |
518 RTC_DCHECK(state_ == SSL_NONE); | 502 RTC_DCHECK(state_ == SSL_NONE); |
519 ssl_mode_ = mode; | 503 ssl_mode_ = mode; |
520 } | 504 } |
521 | 505 |
522 void OpenSSLStreamAdapter::SetMaxProtocolVersion(SSLProtocolVersion version) { | 506 void OpenSSLStreamAdapter::SetMaxProtocolVersion(SSLProtocolVersion version) { |
523 RTC_DCHECK(ssl_ctx_ == NULL); | 507 RTC_DCHECK(ssl_ctx_ == nullptr); |
524 ssl_max_version_ = version; | 508 ssl_max_version_ = version; |
525 } | 509 } |
526 | 510 |
527 void OpenSSLStreamAdapter::SetInitialRetransmissionTimeout( | 511 void OpenSSLStreamAdapter::SetInitialRetransmissionTimeout( |
528 int timeout_ms) { | 512 int timeout_ms) { |
529 RTC_DCHECK(ssl_ctx_ == NULL); | 513 RTC_DCHECK(ssl_ctx_ == nullptr); |
530 dtls_handshake_timeout_ms_ = timeout_ms; | 514 dtls_handshake_timeout_ms_ = timeout_ms; |
531 } | 515 } |
532 | 516 |
533 // | 517 // |
534 // StreamInterface Implementation | 518 // StreamInterface Implementation |
535 // | 519 // |
536 | 520 |
537 StreamResult OpenSSLStreamAdapter::Write(const void* data, size_t data_len, | 521 StreamResult OpenSSLStreamAdapter::Write(const void* data, size_t data_len, |
538 size_t* written, int* error) { | 522 size_t* written, int* error) { |
539 LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::Write(" << data_len << ")"; | 523 LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::Write(" << data_len << ")"; |
(...skipping 237 matching lines...) Loading... |
777 } | 761 } |
778 if (events_to_signal) | 762 if (events_to_signal) |
779 StreamAdapterInterface::OnEvent(stream, events_to_signal, signal_error); | 763 StreamAdapterInterface::OnEvent(stream, events_to_signal, signal_error); |
780 } | 764 } |
781 | 765 |
782 int OpenSSLStreamAdapter::BeginSSL() { | 766 int OpenSSLStreamAdapter::BeginSSL() { |
783 RTC_DCHECK(state_ == SSL_CONNECTING); | 767 RTC_DCHECK(state_ == SSL_CONNECTING); |
784 // The underlying stream has opened. | 768 // The underlying stream has opened. |
785 LOG(LS_INFO) << "BeginSSL with peer."; | 769 LOG(LS_INFO) << "BeginSSL with peer."; |
786 | 770 |
787 BIO* bio = NULL; | 771 BIO* bio = nullptr; |
788 | 772 |
789 // First set up the context. | 773 // First set up the context. |
790 RTC_DCHECK(ssl_ctx_ == NULL); | 774 RTC_DCHECK(ssl_ctx_ == nullptr); |
791 ssl_ctx_ = SetupSSLContext(); | 775 ssl_ctx_ = SetupSSLContext(); |
792 if (!ssl_ctx_) | 776 if (!ssl_ctx_) |
793 return -1; | 777 return -1; |
794 | 778 |
795 bio = BIO_new_stream(static_cast<StreamInterface*>(stream())); | 779 bio = BIO_new_stream(static_cast<StreamInterface*>(stream())); |
796 if (!bio) | 780 if (!bio) |
797 return -1; | 781 return -1; |
798 | 782 |
799 ssl_ = SSL_new(ssl_ctx_); | 783 ssl_ = SSL_new(ssl_ctx_); |
800 if (!ssl_) { | 784 if (!ssl_) { |
(...skipping 16 matching lines...) Loading... |
817 | 801 |
818 SSL_set_mode(ssl_, SSL_MODE_ENABLE_PARTIAL_WRITE | | 802 SSL_set_mode(ssl_, SSL_MODE_ENABLE_PARTIAL_WRITE | |
819 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER); | 803 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER); |
820 | 804 |
821 #if !defined(OPENSSL_IS_BORINGSSL) | 805 #if !defined(OPENSSL_IS_BORINGSSL) |
822 // Specify an ECDH group for ECDHE ciphers, otherwise OpenSSL cannot | 806 // Specify an ECDH group for ECDHE ciphers, otherwise OpenSSL cannot |
823 // negotiate them when acting as the server. Use NIST's P-256 which is | 807 // negotiate them when acting as the server. Use NIST's P-256 which is |
824 // commonly supported. BoringSSL doesn't need explicit configuration and has | 808 // commonly supported. BoringSSL doesn't need explicit configuration and has |
825 // a reasonable default set. | 809 // a reasonable default set. |
826 EC_KEY* ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); | 810 EC_KEY* ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); |
827 if (ecdh == NULL) | 811 if (ecdh == nullptr) |
828 return -1; | 812 return -1; |
829 SSL_set_options(ssl_, SSL_OP_SINGLE_ECDH_USE); | 813 SSL_set_options(ssl_, SSL_OP_SINGLE_ECDH_USE); |
830 SSL_set_tmp_ecdh(ssl_, ecdh); | 814 SSL_set_tmp_ecdh(ssl_, ecdh); |
831 EC_KEY_free(ecdh); | 815 EC_KEY_free(ecdh); |
832 #endif | 816 #endif |
833 | 817 |
834 // Do the connect | 818 // Do the connect |
835 return ContinueSSL(); | 819 return ContinueSSL(); |
836 } | 820 } |
837 | 821 |
(...skipping 94 matching lines...) Loading... |
932 #endif | 916 #endif |
933 ret = SSL_shutdown(ssl_); | 917 ret = SSL_shutdown(ssl_); |
934 if (ret < 0) { | 918 if (ret < 0) { |
935 LOG(LS_WARNING) << "SSL_shutdown failed, error = " | 919 LOG(LS_WARNING) << "SSL_shutdown failed, error = " |
936 << SSL_get_error(ssl_, ret); | 920 << SSL_get_error(ssl_, ret); |
937 } | 921 } |
938 #ifdef OPENSSL_IS_BORINGSSL | 922 #ifdef OPENSSL_IS_BORINGSSL |
939 } | 923 } |
940 #endif | 924 #endif |
941 SSL_free(ssl_); | 925 SSL_free(ssl_); |
942 ssl_ = NULL; | 926 ssl_ = nullptr; |
943 } | 927 } |
944 if (ssl_ctx_) { | 928 if (ssl_ctx_) { |
945 SSL_CTX_free(ssl_ctx_); | 929 SSL_CTX_free(ssl_ctx_); |
946 ssl_ctx_ = NULL; | 930 ssl_ctx_ = nullptr; |
947 } | 931 } |
948 identity_.reset(); | 932 identity_.reset(); |
949 peer_certificate_.reset(); | 933 peer_certificate_.reset(); |
950 | 934 |
951 // Clear the DTLS timer | 935 // Clear the DTLS timer |
952 Thread::Current()->Clear(this, MSG_TIMEOUT); | 936 Thread::Current()->Clear(this, MSG_TIMEOUT); |
953 } | 937 } |
954 | 938 |
955 | 939 |
956 void OpenSSLStreamAdapter::OnMessage(Message* msg) { | 940 void OpenSSLStreamAdapter::OnMessage(Message* msg) { |
957 // Process our own messages and then pass others to the superclass | 941 // Process our own messages and then pass others to the superclass |
958 if (MSG_TIMEOUT == msg->message_id) { | 942 if (MSG_TIMEOUT == msg->message_id) { |
959 LOG(LS_INFO) << "DTLS timeout expired"; | 943 LOG(LS_INFO) << "DTLS timeout expired"; |
960 DTLSv1_handle_timeout(ssl_); | 944 DTLSv1_handle_timeout(ssl_); |
961 ContinueSSL(); | 945 ContinueSSL(); |
962 } else { | 946 } else { |
963 StreamInterface::OnMessage(msg); | 947 StreamInterface::OnMessage(msg); |
964 } | 948 } |
965 } | 949 } |
966 | 950 |
967 SSL_CTX* OpenSSLStreamAdapter::SetupSSLContext() { | 951 SSL_CTX* OpenSSLStreamAdapter::SetupSSLContext() { |
968 SSL_CTX *ctx = NULL; | 952 SSL_CTX* ctx = nullptr; |
969 | 953 |
970 #ifdef OPENSSL_IS_BORINGSSL | 954 #ifdef OPENSSL_IS_BORINGSSL |
971 ctx = SSL_CTX_new(ssl_mode_ == SSL_MODE_DTLS ? | 955 ctx = SSL_CTX_new(ssl_mode_ == SSL_MODE_DTLS ? |
972 DTLS_method() : TLS_method()); | 956 DTLS_method() : TLS_method()); |
973 // Version limiting for BoringSSL will be done below. | 957 // Version limiting for BoringSSL will be done below. |
974 #else | 958 #else |
975 const SSL_METHOD* method; | 959 const SSL_METHOD* method; |
976 switch (ssl_max_version_) { | 960 switch (ssl_max_version_) { |
977 case SSL_PROTOCOL_TLS_10: | 961 case SSL_PROTOCOL_TLS_10: |
978 case SSL_PROTOCOL_TLS_11: | 962 case SSL_PROTOCOL_TLS_11: |
(...skipping 44 matching lines...) Loading... |
1023 } else { | 1007 } else { |
1024 method = SSLv23_server_method(); | 1008 method = SSLv23_server_method(); |
1025 } | 1009 } |
1026 #endif | 1010 #endif |
1027 } | 1011 } |
1028 break; | 1012 break; |
1029 } | 1013 } |
1030 ctx = SSL_CTX_new(method); | 1014 ctx = SSL_CTX_new(method); |
1031 #endif // OPENSSL_IS_BORINGSSL | 1015 #endif // OPENSSL_IS_BORINGSSL |
1032 | 1016 |
1033 if (ctx == NULL) | 1017 if (ctx == nullptr) |
1034 return NULL; | 1018 return nullptr; |
1035 | 1019 |
1036 #ifdef OPENSSL_IS_BORINGSSL | 1020 #ifdef OPENSSL_IS_BORINGSSL |
1037 SSL_CTX_set_min_proto_version(ctx, ssl_mode_ == SSL_MODE_DTLS ? | 1021 SSL_CTX_set_min_proto_version(ctx, ssl_mode_ == SSL_MODE_DTLS ? |
1038 DTLS1_VERSION : TLS1_VERSION); | 1022 DTLS1_VERSION : TLS1_VERSION); |
1039 switch (ssl_max_version_) { | 1023 switch (ssl_max_version_) { |
1040 case SSL_PROTOCOL_TLS_10: | 1024 case SSL_PROTOCOL_TLS_10: |
1041 SSL_CTX_set_max_proto_version(ctx, ssl_mode_ == SSL_MODE_DTLS ? | 1025 SSL_CTX_set_max_proto_version(ctx, ssl_mode_ == SSL_MODE_DTLS ? |
1042 DTLS1_VERSION : TLS1_VERSION); | 1026 DTLS1_VERSION : TLS1_VERSION); |
1043 break; | 1027 break; |
1044 case SSL_PROTOCOL_TLS_11: | 1028 case SSL_PROTOCOL_TLS_11: |
1045 SSL_CTX_set_max_proto_version(ctx, ssl_mode_ == SSL_MODE_DTLS ? | 1029 SSL_CTX_set_max_proto_version(ctx, ssl_mode_ == SSL_MODE_DTLS ? |
1046 DTLS1_VERSION : TLS1_1_VERSION); | 1030 DTLS1_VERSION : TLS1_1_VERSION); |
1047 break; | 1031 break; |
1048 case SSL_PROTOCOL_TLS_12: | 1032 case SSL_PROTOCOL_TLS_12: |
1049 default: | 1033 default: |
1050 SSL_CTX_set_max_proto_version(ctx, ssl_mode_ == SSL_MODE_DTLS ? | 1034 SSL_CTX_set_max_proto_version(ctx, ssl_mode_ == SSL_MODE_DTLS ? |
1051 DTLS1_2_VERSION : TLS1_2_VERSION); | 1035 DTLS1_2_VERSION : TLS1_2_VERSION); |
1052 break; | 1036 break; |
1053 } | 1037 } |
1054 if (g_use_time_callback_for_testing) { | 1038 if (g_use_time_callback_for_testing) { |
1055 SSL_CTX_set_current_time_cb(ctx, &TimeCallbackForTesting); | 1039 SSL_CTX_set_current_time_cb(ctx, &TimeCallbackForTesting); |
1056 } | 1040 } |
1057 #endif | 1041 #endif |
1058 | 1042 |
1059 if (identity_ && !identity_->ConfigureIdentity(ctx)) { | 1043 if (identity_ && !identity_->ConfigureIdentity(ctx)) { |
1060 SSL_CTX_free(ctx); | 1044 SSL_CTX_free(ctx); |
1061 return NULL; | 1045 return nullptr; |
1062 } | 1046 } |
1063 | 1047 |
1064 #if !defined(NDEBUG) | 1048 #if !defined(NDEBUG) |
1065 SSL_CTX_set_info_callback(ctx, OpenSSLAdapter::SSLInfoCallback); | 1049 SSL_CTX_set_info_callback(ctx, OpenSSLAdapter::SSLInfoCallback); |
1066 #endif | 1050 #endif |
1067 | 1051 |
1068 int mode = SSL_VERIFY_PEER; | 1052 int mode = SSL_VERIFY_PEER; |
1069 if (client_auth_enabled()) { | 1053 if (client_auth_enabled()) { |
1070 // Require a certificate from the client. | 1054 // Require a certificate from the client. |
1071 // Note: Normally this is always true in production, but it may be disabled | 1055 // Note: Normally this is always true in production, but it may be disabled |
1072 // for testing purposes (e.g. SSLAdapter unit tests). | 1056 // for testing purposes (e.g. SSLAdapter unit tests). |
1073 mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT; | 1057 mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
1074 } | 1058 } |
1075 | 1059 |
1076 SSL_CTX_set_verify(ctx, mode, SSLVerifyCallback); | 1060 SSL_CTX_set_verify(ctx, mode, SSLVerifyCallback); |
1077 SSL_CTX_set_verify_depth(ctx, 4); | 1061 SSL_CTX_set_verify_depth(ctx, 4); |
1078 // Select list of available ciphers. Note that !SHA256 and !SHA384 only | 1062 // Select list of available ciphers. Note that !SHA256 and !SHA384 only |
1079 // remove HMAC-SHA256 and HMAC-SHA384 cipher suites, not GCM cipher suites | 1063 // remove HMAC-SHA256 and HMAC-SHA384 cipher suites, not GCM cipher suites |
1080 // with SHA256 or SHA384 as the handshake hash. | 1064 // with SHA256 or SHA384 as the handshake hash. |
1081 // This matches the list of SSLClientSocketOpenSSL in Chromium. | 1065 // This matches the list of SSLClientSocketOpenSSL in Chromium. |
1082 SSL_CTX_set_cipher_list(ctx, | 1066 SSL_CTX_set_cipher_list( |
1083 "DEFAULT:!NULL:!aNULL:!SHA256:!SHA384:!aECDH:!AESGCM+AES256:!aPSK"); | 1067 ctx, "DEFAULT:!NULL:!aNULL:!SHA256:!SHA384:!aECDH:!AESGCM+AES256:!aPSK"); |
1084 | 1068 |
1085 if (!srtp_ciphers_.empty()) { | 1069 if (!srtp_ciphers_.empty()) { |
1086 if (SSL_CTX_set_tlsext_use_srtp(ctx, srtp_ciphers_.c_str())) { | 1070 if (SSL_CTX_set_tlsext_use_srtp(ctx, srtp_ciphers_.c_str())) { |
1087 SSL_CTX_free(ctx); | 1071 SSL_CTX_free(ctx); |
1088 return NULL; | 1072 return nullptr; |
1089 } | 1073 } |
1090 } | 1074 } |
1091 | 1075 |
1092 return ctx; | 1076 return ctx; |
1093 } | 1077 } |
1094 | 1078 |
1095 bool OpenSSLStreamAdapter::VerifyPeerCertificate() { | 1079 bool OpenSSLStreamAdapter::VerifyPeerCertificate() { |
1096 if (!has_peer_certificate_digest() || !peer_certificate_) { | 1080 if (!has_peer_certificate_digest() || !peer_certificate_) { |
1097 LOG(LS_WARNING) << "Missing digest or peer certificate."; | 1081 LOG(LS_WARNING) << "Missing digest or peer certificate."; |
1098 return false; | 1082 return false; |
(...skipping 131 matching lines...) Loading... |
1230 } | 1214 } |
1231 | 1215 |
1232 return false; | 1216 return false; |
1233 } | 1217 } |
1234 | 1218 |
1235 void OpenSSLStreamAdapter::enable_time_callback_for_testing() { | 1219 void OpenSSLStreamAdapter::enable_time_callback_for_testing() { |
1236 g_use_time_callback_for_testing = true; | 1220 g_use_time_callback_for_testing = true; |
1237 } | 1221 } |
1238 | 1222 |
1239 } // namespace rtc | 1223 } // namespace rtc |
OLD | NEW |