OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // OpenSSL binding for SSLClientSocket. The class layout and general principle | 5 // OpenSSL binding for SSLClientSocket. The class layout and general principle |
6 // of operation is derived from SSLClientSocketNSS. | 6 // of operation is derived from SSLClientSocketNSS. |
7 | 7 |
8 #include "net/socket/ssl_client_socket_openssl.h" | 8 #include "net/socket/ssl_client_socket_openssl.h" |
9 | 9 |
| 10 #include <errno.h> |
10 #include <openssl/err.h> | 11 #include <openssl/err.h> |
11 #include <openssl/opensslv.h> | |
12 #include <openssl/ssl.h> | 12 #include <openssl/ssl.h> |
13 | 13 |
14 #include "base/bind.h" | 14 #include "base/bind.h" |
15 #include "base/callback_helpers.h" | 15 #include "base/callback_helpers.h" |
16 #include "base/memory/singleton.h" | 16 #include "base/memory/singleton.h" |
17 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
18 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
19 #include "crypto/ec_private_key.h" | 19 #include "crypto/ec_private_key.h" |
20 #include "crypto/openssl_util.h" | 20 #include "crypto/openssl_util.h" |
21 #include "crypto/scoped_openssl_types.h" | 21 #include "crypto/scoped_openssl_types.h" |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 SSLClientSocketOpenSSL::PeerCertificateChain::operator=( | 241 SSLClientSocketOpenSSL::PeerCertificateChain::operator=( |
242 const PeerCertificateChain& other) { | 242 const PeerCertificateChain& other) { |
243 if (this == &other) | 243 if (this == &other) |
244 return *this; | 244 return *this; |
245 | 245 |
246 // os_chain_ is reference counted by scoped_refptr; | 246 // os_chain_ is reference counted by scoped_refptr; |
247 os_chain_ = other.os_chain_; | 247 os_chain_ = other.os_chain_; |
248 | 248 |
249 // Must increase the reference count manually for sk_X509_dup | 249 // Must increase the reference count manually for sk_X509_dup |
250 openssl_chain_.reset(sk_X509_dup(other.openssl_chain_.get())); | 250 openssl_chain_.reset(sk_X509_dup(other.openssl_chain_.get())); |
251 for (int i = 0; i < sk_X509_num(openssl_chain_.get()); ++i) { | 251 for (size_t i = 0; i < sk_X509_num(openssl_chain_.get()); ++i) { |
252 X509* x = sk_X509_value(openssl_chain_.get(), i); | 252 X509* x = sk_X509_value(openssl_chain_.get(), i); |
253 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); | 253 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); |
254 } | 254 } |
255 return *this; | 255 return *this; |
256 } | 256 } |
257 | 257 |
258 #if defined(USE_OPENSSL_CERTS) | 258 #if defined(USE_OPENSSL_CERTS) |
259 // When OSCertHandle is typedef'ed to X509, this implementation does a short cut | 259 // When OSCertHandle is typedef'ed to X509, this implementation does a short cut |
260 // to avoid converting back and forth between der and X509 struct. | 260 // to avoid converting back and forth between der and X509 struct. |
261 void SSLClientSocketOpenSSL::PeerCertificateChain::Reset( | 261 void SSLClientSocketOpenSSL::PeerCertificateChain::Reset( |
262 STACK_OF(X509)* chain) { | 262 STACK_OF(X509)* chain) { |
263 openssl_chain_.reset(NULL); | 263 openssl_chain_.reset(NULL); |
264 os_chain_ = NULL; | 264 os_chain_ = NULL; |
265 | 265 |
266 if (!chain) | 266 if (!chain) |
267 return; | 267 return; |
268 | 268 |
269 X509Certificate::OSCertHandles intermediates; | 269 X509Certificate::OSCertHandles intermediates; |
270 for (int i = 1; i < sk_X509_num(chain); ++i) | 270 for (size_t i = 1; i < sk_X509_num(chain); ++i) |
271 intermediates.push_back(sk_X509_value(chain, i)); | 271 intermediates.push_back(sk_X509_value(chain, i)); |
272 | 272 |
273 os_chain_ = | 273 os_chain_ = |
274 X509Certificate::CreateFromHandle(sk_X509_value(chain, 0), intermediates); | 274 X509Certificate::CreateFromHandle(sk_X509_value(chain, 0), intermediates); |
275 | 275 |
276 // sk_X509_dup does not increase reference count on the certs in the stack. | 276 // sk_X509_dup does not increase reference count on the certs in the stack. |
277 openssl_chain_.reset(sk_X509_dup(chain)); | 277 openssl_chain_.reset(sk_X509_dup(chain)); |
278 | 278 |
279 std::vector<base::StringPiece> der_chain; | 279 std::vector<base::StringPiece> der_chain; |
280 for (int i = 0; i < sk_X509_num(openssl_chain_.get()); ++i) { | 280 for (size_t i = 0; i < sk_X509_num(openssl_chain_.get()); ++i) { |
281 X509* x = sk_X509_value(openssl_chain_.get(), i); | 281 X509* x = sk_X509_value(openssl_chain_.get(), i); |
282 // Increase the reference count for the certs in openssl_chain_. | 282 // Increase the reference count for the certs in openssl_chain_. |
283 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); | 283 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); |
284 } | 284 } |
285 } | 285 } |
286 #else // !defined(USE_OPENSSL_CERTS) | 286 #else // !defined(USE_OPENSSL_CERTS) |
287 void SSLClientSocketOpenSSL::PeerCertificateChain::Reset( | 287 void SSLClientSocketOpenSSL::PeerCertificateChain::Reset( |
288 STACK_OF(X509)* chain) { | 288 STACK_OF(X509)* chain) { |
289 openssl_chain_.reset(NULL); | 289 openssl_chain_.reset(NULL); |
290 os_chain_ = NULL; | 290 os_chain_ = NULL; |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
500 | 500 |
501 bool SSLClientSocketOpenSSL::IsConnectedAndIdle() const { | 501 bool SSLClientSocketOpenSSL::IsConnectedAndIdle() const { |
502 // If the handshake has not yet completed. | 502 // If the handshake has not yet completed. |
503 if (!completed_handshake_) | 503 if (!completed_handshake_) |
504 return false; | 504 return false; |
505 // If an asynchronous operation is still pending. | 505 // If an asynchronous operation is still pending. |
506 if (user_read_buf_.get() || user_write_buf_.get()) | 506 if (user_read_buf_.get() || user_write_buf_.get()) |
507 return false; | 507 return false; |
508 // If there is data waiting to be sent, or data read from the network that | 508 // If there is data waiting to be sent, or data read from the network that |
509 // has not yet been consumed. | 509 // has not yet been consumed. |
510 if (BIO_ctrl_pending(transport_bio_) > 0 || | 510 if (BIO_pending(transport_bio_) > 0 || |
511 BIO_ctrl_wpending(transport_bio_) > 0) { | 511 BIO_wpending(transport_bio_) > 0) { |
512 return false; | 512 return false; |
513 } | 513 } |
514 | 514 |
515 return transport_->socket()->IsConnectedAndIdle(); | 515 return transport_->socket()->IsConnectedAndIdle(); |
516 } | 516 } |
517 | 517 |
518 int SSLClientSocketOpenSSL::GetPeerAddress(IPEndPoint* addressList) const { | 518 int SSLClientSocketOpenSSL::GetPeerAddress(IPEndPoint* addressList) const { |
519 return transport_->socket()->GetPeerAddress(addressList); | 519 return transport_->socket()->GetPeerAddress(addressList); |
520 } | 520 } |
521 | 521 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
571 ssl_info->channel_id_sent = WasChannelIDSent(); | 571 ssl_info->channel_id_sent = WasChannelIDSent(); |
572 | 572 |
573 RecordChannelIDSupport(server_bound_cert_service_, | 573 RecordChannelIDSupport(server_bound_cert_service_, |
574 channel_id_xtn_negotiated_, | 574 channel_id_xtn_negotiated_, |
575 ssl_config_.channel_id_enabled, | 575 ssl_config_.channel_id_enabled, |
576 crypto::ECPrivateKey::IsSupported()); | 576 crypto::ECPrivateKey::IsSupported()); |
577 | 577 |
578 const SSL_CIPHER* cipher = SSL_get_current_cipher(ssl_); | 578 const SSL_CIPHER* cipher = SSL_get_current_cipher(ssl_); |
579 CHECK(cipher); | 579 CHECK(cipher); |
580 ssl_info->security_bits = SSL_CIPHER_get_bits(cipher, NULL); | 580 ssl_info->security_bits = SSL_CIPHER_get_bits(cipher, NULL); |
581 const COMP_METHOD* compression = SSL_get_current_compression(ssl_); | |
582 | 581 |
583 ssl_info->connection_status = EncodeSSLConnectionStatus( | 582 ssl_info->connection_status = EncodeSSLConnectionStatus( |
584 SSL_CIPHER_get_id(cipher), | 583 SSL_CIPHER_get_id(cipher), 0 /* no compression */, |
585 compression ? compression->type : 0, | |
586 GetNetSSLVersion(ssl_)); | 584 GetNetSSLVersion(ssl_)); |
587 | 585 |
588 bool peer_supports_renego_ext = !!SSL_get_secure_renegotiation_support(ssl_); | 586 bool peer_supports_renego_ext = !!SSL_get_secure_renegotiation_support(ssl_); |
589 if (!peer_supports_renego_ext) | 587 if (!peer_supports_renego_ext) |
590 ssl_info->connection_status |= SSL_CONNECTION_NO_RENEGOTIATION_EXTENSION; | 588 ssl_info->connection_status |= SSL_CONNECTION_NO_RENEGOTIATION_EXTENSION; |
591 UMA_HISTOGRAM_ENUMERATION("Net.RenegotiationExtensionSupported", | 589 UMA_HISTOGRAM_ENUMERATION("Net.RenegotiationExtensionSupported", |
592 implicit_cast<int>(peer_supports_renego_ext), 2); | 590 implicit_cast<int>(peer_supports_renego_ext), 2); |
593 | 591 |
594 if (ssl_config_.version_fallback) | 592 if (ssl_config_.version_fallback) |
595 ssl_info->connection_status |= SSL_CONNECTION_VERSION_FALLBACK; | 593 ssl_info->connection_status |= SSL_CONNECTION_VERSION_FALLBACK; |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
721 STACK_OF(SSL_CIPHER)* ciphers = SSL_get_ciphers(ssl_); | 719 STACK_OF(SSL_CIPHER)* ciphers = SSL_get_ciphers(ssl_); |
722 DCHECK(ciphers); | 720 DCHECK(ciphers); |
723 // See SSLConfig::disabled_cipher_suites for description of the suites | 721 // See SSLConfig::disabled_cipher_suites for description of the suites |
724 // disabled by default. Note that !SHA256 and !SHA384 only remove HMAC-SHA256 | 722 // disabled by default. Note that !SHA256 and !SHA384 only remove HMAC-SHA256 |
725 // and HMAC-SHA384 cipher suites, not GCM cipher suites with SHA256 or SHA384 | 723 // and HMAC-SHA384 cipher suites, not GCM cipher suites with SHA256 or SHA384 |
726 // as the handshake hash. | 724 // as the handshake hash. |
727 std::string command("DEFAULT:!NULL:!aNULL:!IDEA:!FZA:!SRP:!SHA256:!SHA384:" | 725 std::string command("DEFAULT:!NULL:!aNULL:!IDEA:!FZA:!SRP:!SHA256:!SHA384:" |
728 "!aECDH:!AESGCM+AES256"); | 726 "!aECDH:!AESGCM+AES256"); |
729 // Walk through all the installed ciphers, seeing if any need to be | 727 // Walk through all the installed ciphers, seeing if any need to be |
730 // appended to the cipher removal |command|. | 728 // appended to the cipher removal |command|. |
731 for (int i = 0; i < sk_SSL_CIPHER_num(ciphers); ++i) { | 729 for (size_t i = 0; i < sk_SSL_CIPHER_num(ciphers); ++i) { |
732 const SSL_CIPHER* cipher = sk_SSL_CIPHER_value(ciphers, i); | 730 const SSL_CIPHER* cipher = sk_SSL_CIPHER_value(ciphers, i); |
733 const uint16 id = SSL_CIPHER_get_id(cipher); | 731 const uint16 id = SSL_CIPHER_get_id(cipher); |
734 // Remove any ciphers with a strength of less than 80 bits. Note the NSS | 732 // Remove any ciphers with a strength of less than 80 bits. Note the NSS |
735 // implementation uses "effective" bits here but OpenSSL does not provide | 733 // implementation uses "effective" bits here but OpenSSL does not provide |
736 // this detail. This only impacts Triple DES: reports 112 vs. 168 bits, | 734 // this detail. This only impacts Triple DES: reports 112 vs. 168 bits, |
737 // both of which are greater than 80 anyway. | 735 // both of which are greater than 80 anyway. |
738 bool disable = SSL_CIPHER_get_bits(cipher, NULL) < 80; | 736 bool disable = SSL_CIPHER_get_bits(cipher, NULL) < 80; |
739 if (!disable) { | 737 if (!disable) { |
740 disable = std::find(ssl_config_.disabled_cipher_suites.begin(), | 738 disable = std::find(ssl_config_.disabled_cipher_suites.begin(), |
741 ssl_config_.disabled_cipher_suites.end(), id) != | 739 ssl_config_.disabled_cipher_suites.end(), id) != |
(...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1195 int err = SSL_get_error(ssl_, rv); | 1193 int err = SSL_get_error(ssl_, rv); |
1196 return MapOpenSSLError(err, err_tracer); | 1194 return MapOpenSSLError(err, err_tracer); |
1197 } | 1195 } |
1198 | 1196 |
1199 int SSLClientSocketOpenSSL::BufferSend(void) { | 1197 int SSLClientSocketOpenSSL::BufferSend(void) { |
1200 if (transport_send_busy_) | 1198 if (transport_send_busy_) |
1201 return ERR_IO_PENDING; | 1199 return ERR_IO_PENDING; |
1202 | 1200 |
1203 if (!send_buffer_.get()) { | 1201 if (!send_buffer_.get()) { |
1204 // Get a fresh send buffer out of the send BIO. | 1202 // Get a fresh send buffer out of the send BIO. |
1205 size_t max_read = BIO_ctrl_pending(transport_bio_); | 1203 size_t max_read = BIO_pending(transport_bio_); |
1206 if (!max_read) | 1204 if (!max_read) |
1207 return 0; // Nothing pending in the OpenSSL write BIO. | 1205 return 0; // Nothing pending in the OpenSSL write BIO. |
1208 send_buffer_ = new DrainableIOBuffer(new IOBuffer(max_read), max_read); | 1206 send_buffer_ = new DrainableIOBuffer(new IOBuffer(max_read), max_read); |
1209 int read_bytes = BIO_read(transport_bio_, send_buffer_->data(), max_read); | 1207 int read_bytes = BIO_read(transport_bio_, send_buffer_->data(), max_read); |
1210 DCHECK_GT(read_bytes, 0); | 1208 DCHECK_GT(read_bytes, 0); |
1211 CHECK_EQ(static_cast<int>(max_read), read_bytes); | 1209 CHECK_EQ(static_cast<int>(max_read), read_bytes); |
1212 } | 1210 } |
1213 | 1211 |
1214 int rv = transport_->socket()->Write( | 1212 int rv = transport_->socket()->Write( |
1215 send_buffer_.get(), | 1213 send_buffer_.get(), |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1333 EVP_PKEY** pkey) { | 1331 EVP_PKEY** pkey) { |
1334 DVLOG(3) << "OpenSSL ClientCertRequestCallback called"; | 1332 DVLOG(3) << "OpenSSL ClientCertRequestCallback called"; |
1335 DCHECK(ssl == ssl_); | 1333 DCHECK(ssl == ssl_); |
1336 DCHECK(*x509 == NULL); | 1334 DCHECK(*x509 == NULL); |
1337 DCHECK(*pkey == NULL); | 1335 DCHECK(*pkey == NULL); |
1338 if (!ssl_config_.send_client_cert) { | 1336 if (!ssl_config_.send_client_cert) { |
1339 // First pass: we know that a client certificate is needed, but we do not | 1337 // First pass: we know that a client certificate is needed, but we do not |
1340 // have one at hand. | 1338 // have one at hand. |
1341 client_auth_cert_needed_ = true; | 1339 client_auth_cert_needed_ = true; |
1342 STACK_OF(X509_NAME) *authorities = SSL_get_client_CA_list(ssl); | 1340 STACK_OF(X509_NAME) *authorities = SSL_get_client_CA_list(ssl); |
1343 for (int i = 0; i < sk_X509_NAME_num(authorities); i++) { | 1341 for (size_t i = 0; i < sk_X509_NAME_num(authorities); i++) { |
1344 X509_NAME *ca_name = (X509_NAME *)sk_X509_NAME_value(authorities, i); | 1342 X509_NAME *ca_name = (X509_NAME *)sk_X509_NAME_value(authorities, i); |
1345 unsigned char* str = NULL; | 1343 unsigned char* str = NULL; |
1346 int length = i2d_X509_NAME(ca_name, &str); | 1344 int length = i2d_X509_NAME(ca_name, &str); |
1347 cert_authorities_.push_back(std::string( | 1345 cert_authorities_.push_back(std::string( |
1348 reinterpret_cast<const char*>(str), | 1346 reinterpret_cast<const char*>(str), |
1349 static_cast<size_t>(length))); | 1347 static_cast<size_t>(length))); |
1350 OPENSSL_free(str); | 1348 OPENSSL_free(str); |
1351 } | 1349 } |
1352 | 1350 |
1353 const unsigned char* client_cert_types; | 1351 const unsigned char* client_cert_types; |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1473 DVLOG(2) << "next protocol: '" << npn_proto_ << "' status: " << npn_status_; | 1471 DVLOG(2) << "next protocol: '" << npn_proto_ << "' status: " << npn_status_; |
1474 return SSL_TLSEXT_ERR_OK; | 1472 return SSL_TLSEXT_ERR_OK; |
1475 } | 1473 } |
1476 | 1474 |
1477 scoped_refptr<X509Certificate> | 1475 scoped_refptr<X509Certificate> |
1478 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { | 1476 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { |
1479 return server_cert_; | 1477 return server_cert_; |
1480 } | 1478 } |
1481 | 1479 |
1482 } // namespace net | 1480 } // namespace net |
OLD | NEW |