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 <errno.h> |
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
351 const SSLClientSocketContext& context) | 351 const SSLClientSocketContext& context) |
352 : transport_send_busy_(false), | 352 : transport_send_busy_(false), |
353 transport_recv_busy_(false), | 353 transport_recv_busy_(false), |
354 pending_read_error_(kNoPendingReadResult), | 354 pending_read_error_(kNoPendingReadResult), |
355 pending_read_ssl_error_(SSL_ERROR_NONE), | 355 pending_read_ssl_error_(SSL_ERROR_NONE), |
356 transport_read_error_(OK), | 356 transport_read_error_(OK), |
357 transport_write_error_(OK), | 357 transport_write_error_(OK), |
358 server_cert_chain_(new PeerCertificateChain(NULL)), | 358 server_cert_chain_(new PeerCertificateChain(NULL)), |
359 completed_connect_(false), | 359 completed_connect_(false), |
360 was_ever_used_(false), | 360 was_ever_used_(false), |
361 client_auth_cert_needed_(false), | |
362 cert_verifier_(context.cert_verifier), | 361 cert_verifier_(context.cert_verifier), |
363 cert_transparency_verifier_(context.cert_transparency_verifier), | 362 cert_transparency_verifier_(context.cert_transparency_verifier), |
364 channel_id_service_(context.channel_id_service), | 363 channel_id_service_(context.channel_id_service), |
365 ssl_(NULL), | 364 ssl_(NULL), |
366 transport_bio_(NULL), | 365 transport_bio_(NULL), |
367 transport_(transport_socket.Pass()), | 366 transport_(transport_socket.Pass()), |
368 host_and_port_(host_and_port), | 367 host_and_port_(host_and_port), |
369 ssl_config_(ssl_config), | 368 ssl_config_(ssl_config), |
370 ssl_session_cache_shard_(context.ssl_session_cache_shard), | 369 ssl_session_cache_shard_(context.ssl_session_cache_shard), |
371 next_handshake_state_(STATE_NONE), | 370 next_handshake_state_(STATE_NONE), |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
492 pending_read_error_info_ = OpenSSLErrorInfo(); | 491 pending_read_error_info_ = OpenSSLErrorInfo(); |
493 | 492 |
494 transport_read_error_ = OK; | 493 transport_read_error_ = OK; |
495 transport_write_error_ = OK; | 494 transport_write_error_ = OK; |
496 | 495 |
497 server_cert_verify_result_.Reset(); | 496 server_cert_verify_result_.Reset(); |
498 completed_connect_ = false; | 497 completed_connect_ = false; |
499 | 498 |
500 cert_authorities_.clear(); | 499 cert_authorities_.clear(); |
501 cert_key_types_.clear(); | 500 cert_key_types_.clear(); |
502 client_auth_cert_needed_ = false; | |
503 | 501 |
504 start_cert_verification_time_ = base::TimeTicks(); | 502 start_cert_verification_time_ = base::TimeTicks(); |
505 | 503 |
506 npn_status_ = kNextProtoUnsupported; | 504 npn_status_ = kNextProtoUnsupported; |
507 npn_proto_.clear(); | 505 npn_proto_.clear(); |
508 | 506 |
509 channel_id_sent_ = false; | 507 channel_id_sent_ = false; |
510 channel_id_request_handle_.Cancel(); | 508 channel_id_request_handle_.Cancel(); |
511 } | 509 } |
512 | 510 |
(...skipping 440 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
953 SSL_get0_signed_cert_timestamp_list(ssl_, &sct_list, &sct_list_len); | 951 SSL_get0_signed_cert_timestamp_list(ssl_, &sct_list, &sct_list_len); |
954 set_signed_cert_timestamps_received(sct_list_len != 0); | 952 set_signed_cert_timestamps_received(sct_list_len != 0); |
955 | 953 |
956 if (IsRenegotiationAllowed()) | 954 if (IsRenegotiationAllowed()) |
957 SSL_set_reject_peer_renegotiations(ssl_, 0); | 955 SSL_set_reject_peer_renegotiations(ssl_, 0); |
958 | 956 |
959 // Verify the certificate. | 957 // Verify the certificate. |
960 UpdateServerCert(); | 958 UpdateServerCert(); |
961 GotoState(STATE_VERIFY_CERT); | 959 GotoState(STATE_VERIFY_CERT); |
962 } else { | 960 } else { |
963 if (client_auth_cert_needed_) | |
964 return ERR_SSL_CLIENT_AUTH_CERT_NEEDED; | |
965 | |
966 int ssl_error = SSL_get_error(ssl_, rv); | 961 int ssl_error = SSL_get_error(ssl_, rv); |
967 | |
968 if (ssl_error == SSL_ERROR_WANT_CHANNEL_ID_LOOKUP) { | 962 if (ssl_error == SSL_ERROR_WANT_CHANNEL_ID_LOOKUP) { |
969 // The server supports channel ID. Stop to look one up before returning to | 963 // The server supports channel ID. Stop to look one up before returning to |
970 // the handshake. | 964 // the handshake. |
971 GotoState(STATE_CHANNEL_ID_LOOKUP); | 965 GotoState(STATE_CHANNEL_ID_LOOKUP); |
972 return OK; | 966 return OK; |
973 } | 967 } |
968 if (ssl_error == SSL_ERROR_WANT_X509_LOOKUP && | |
969 !ssl_config_.send_client_cert) { | |
970 return ERR_SSL_CLIENT_AUTH_CERT_NEEDED; | |
971 } | |
974 | 972 |
975 OpenSSLErrorInfo error_info; | 973 OpenSSLErrorInfo error_info; |
976 net_error = MapOpenSSLErrorWithDetails(ssl_error, err_tracer, &error_info); | 974 net_error = MapOpenSSLErrorWithDetails(ssl_error, err_tracer, &error_info); |
977 | 975 |
978 // If not done, stay in this state | 976 // If not done, stay in this state |
979 if (net_error == ERR_IO_PENDING) { | 977 if (net_error == ERR_IO_PENDING) { |
980 GotoState(STATE_HANDSHAKE); | 978 GotoState(STATE_HANDSHAKE); |
981 } else { | 979 } else { |
982 LOG(ERROR) << "handshake failed; returned " << rv | 980 LOG(ERROR) << "handshake failed; returned " << rv |
983 << ", SSL error code " << ssl_error | 981 << ", SSL error code " << ssl_error |
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1382 do { | 1380 do { |
1383 ssl_ret = SSL_read(ssl_, user_read_buf_->data() + total_bytes_read, | 1381 ssl_ret = SSL_read(ssl_, user_read_buf_->data() + total_bytes_read, |
1384 user_read_buf_len_ - total_bytes_read); | 1382 user_read_buf_len_ - total_bytes_read); |
1385 if (ssl_ret > 0) | 1383 if (ssl_ret > 0) |
1386 total_bytes_read += ssl_ret; | 1384 total_bytes_read += ssl_ret; |
1387 } while (total_bytes_read < user_read_buf_len_ && ssl_ret > 0); | 1385 } while (total_bytes_read < user_read_buf_len_ && ssl_ret > 0); |
1388 | 1386 |
1389 // Although only the final SSL_read call may have failed, the failure needs to | 1387 // Although only the final SSL_read call may have failed, the failure needs to |
1390 // processed immediately, while the information still available in OpenSSL's | 1388 // processed immediately, while the information still available in OpenSSL's |
1391 // error queue. | 1389 // error queue. |
1392 if (client_auth_cert_needed_) { | 1390 if (ssl_ret <= 0) { |
1393 pending_read_error_ = ERR_SSL_CLIENT_AUTH_CERT_NEEDED; | |
1394 } else if (ssl_ret <= 0) { | |
1395 // A zero return from SSL_read may mean any of: | 1391 // A zero return from SSL_read may mean any of: |
1396 // - The underlying BIO_read returned 0. | 1392 // - The underlying BIO_read returned 0. |
1397 // - The peer sent a close_notify. | 1393 // - The peer sent a close_notify. |
1398 // - Any arbitrary error. https://crbug.com/466303 | 1394 // - Any arbitrary error. https://crbug.com/466303 |
1399 // | 1395 // |
1400 // TransportReadComplete converts the first to an ERR_CONNECTION_CLOSED | 1396 // TransportReadComplete converts the first to an ERR_CONNECTION_CLOSED |
1401 // error, so it does not occur. The second and third are distinguished by | 1397 // error, so it does not occur. The second and third are distinguished by |
1402 // SSL_ERROR_ZERO_RETURN. | 1398 // SSL_ERROR_ZERO_RETURN. |
1403 pending_read_ssl_error_ = SSL_get_error(ssl_, ssl_ret); | 1399 pending_read_ssl_error_ = SSL_get_error(ssl_, ssl_ret); |
1404 if (pending_read_ssl_error_ == SSL_ERROR_ZERO_RETURN) { | 1400 if (pending_read_ssl_error_ == SSL_ERROR_ZERO_RETURN) { |
1405 pending_read_error_ = 0; | 1401 pending_read_error_ = 0; |
1402 } else if (pending_read_ssl_error_ == SSL_ERROR_WANT_X509_LOOKUP && | |
1403 !ssl_config_.send_client_cert) { | |
davidben
2015/05/08 22:15:52
This is the same as checking it before the ssl_ret
| |
1404 pending_read_error_ = ERR_SSL_CLIENT_AUTH_CERT_NEEDED; | |
1406 } else { | 1405 } else { |
1407 pending_read_error_ = MapOpenSSLErrorWithDetails( | 1406 pending_read_error_ = MapOpenSSLErrorWithDetails( |
1408 pending_read_ssl_error_, err_tracer, &pending_read_error_info_); | 1407 pending_read_ssl_error_, err_tracer, &pending_read_error_info_); |
1409 } | 1408 } |
1410 | 1409 |
1411 // Many servers do not reliably send a close_notify alert when shutting down | 1410 // Many servers do not reliably send a close_notify alert when shutting down |
1412 // a connection, and instead terminate the TCP connection. This is reported | 1411 // a connection, and instead terminate the TCP connection. This is reported |
1413 // as ERR_CONNECTION_CLOSED. Because of this, map the unclean shutdown to a | 1412 // as ERR_CONNECTION_CLOSED. Because of this, map the unclean shutdown to a |
1414 // graceful EOF, instead of treating it as an error as it should be. | 1413 // graceful EOF, instead of treating it as an error as it should be. |
1415 if (pending_read_error_ == ERR_CONNECTION_CLOSED) | 1414 if (pending_read_error_ == ERR_CONNECTION_CLOSED) |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1607 // Clear any currently configured certificates. | 1606 // Clear any currently configured certificates. |
1608 SSL_certs_clear(ssl_); | 1607 SSL_certs_clear(ssl_); |
1609 | 1608 |
1610 #if defined(OS_IOS) | 1609 #if defined(OS_IOS) |
1611 // TODO(droger): Support client auth on iOS. See http://crbug.com/145954). | 1610 // TODO(droger): Support client auth on iOS. See http://crbug.com/145954). |
1612 LOG(WARNING) << "Client auth is not supported"; | 1611 LOG(WARNING) << "Client auth is not supported"; |
1613 #else // !defined(OS_IOS) | 1612 #else // !defined(OS_IOS) |
1614 if (!ssl_config_.send_client_cert) { | 1613 if (!ssl_config_.send_client_cert) { |
1615 // First pass: we know that a client certificate is needed, but we do not | 1614 // First pass: we know that a client certificate is needed, but we do not |
1616 // have one at hand. | 1615 // have one at hand. |
1617 client_auth_cert_needed_ = true; | |
1618 STACK_OF(X509_NAME) *authorities = SSL_get_client_CA_list(ssl); | 1616 STACK_OF(X509_NAME) *authorities = SSL_get_client_CA_list(ssl); |
1619 for (size_t i = 0; i < sk_X509_NAME_num(authorities); i++) { | 1617 for (size_t i = 0; i < sk_X509_NAME_num(authorities); i++) { |
1620 X509_NAME *ca_name = (X509_NAME *)sk_X509_NAME_value(authorities, i); | 1618 X509_NAME *ca_name = (X509_NAME *)sk_X509_NAME_value(authorities, i); |
1621 unsigned char* str = NULL; | 1619 unsigned char* str = NULL; |
1622 int length = i2d_X509_NAME(ca_name, &str); | 1620 int length = i2d_X509_NAME(ca_name, &str); |
1623 cert_authorities_.push_back(std::string( | 1621 cert_authorities_.push_back(std::string( |
1624 reinterpret_cast<const char*>(str), | 1622 reinterpret_cast<const char*>(str), |
1625 static_cast<size_t>(length))); | 1623 static_cast<size_t>(length))); |
1626 OPENSSL_free(str); | 1624 OPENSSL_free(str); |
1627 } | 1625 } |
1628 | 1626 |
1629 const unsigned char* client_cert_types; | 1627 const unsigned char* client_cert_types; |
1630 size_t num_client_cert_types = | 1628 size_t num_client_cert_types = |
1631 SSL_get0_certificate_types(ssl, &client_cert_types); | 1629 SSL_get0_certificate_types(ssl, &client_cert_types); |
1632 for (size_t i = 0; i < num_client_cert_types; i++) { | 1630 for (size_t i = 0; i < num_client_cert_types; i++) { |
1633 cert_key_types_.push_back( | 1631 cert_key_types_.push_back( |
1634 static_cast<SSLClientCertType>(client_cert_types[i])); | 1632 static_cast<SSLClientCertType>(client_cert_types[i])); |
1635 } | 1633 } |
1636 | 1634 |
1637 return -1; // Suspends handshake. | 1635 // Suspends handshake. SSL_get_error will return SSL_ERROR_WANT_X509_LOOKUP. |
1636 return -1; | |
1638 } | 1637 } |
1639 | 1638 |
1640 // Second pass: a client certificate should have been selected. | 1639 // Second pass: a client certificate should have been selected. |
1641 if (ssl_config_.client_cert.get()) { | 1640 if (ssl_config_.client_cert.get()) { |
1642 ScopedX509 leaf_x509 = | 1641 ScopedX509 leaf_x509 = |
1643 OSCertHandleToOpenSSL(ssl_config_.client_cert->os_cert_handle()); | 1642 OSCertHandleToOpenSSL(ssl_config_.client_cert->os_cert_handle()); |
1644 if (!leaf_x509) { | 1643 if (!leaf_x509) { |
1645 LOG(WARNING) << "Failed to import certificate"; | 1644 LOG(WARNING) << "Failed to import certificate"; |
1646 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_BAD_FORMAT); | 1645 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_BAD_FORMAT); |
1647 return -1; | 1646 return -1; |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1904 } | 1903 } |
1905 return false; | 1904 return false; |
1906 } | 1905 } |
1907 | 1906 |
1908 scoped_refptr<X509Certificate> | 1907 scoped_refptr<X509Certificate> |
1909 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { | 1908 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { |
1910 return server_cert_; | 1909 return server_cert_; |
1911 } | 1910 } |
1912 | 1911 |
1913 } // namespace net | 1912 } // namespace net |
OLD | NEW |