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> |
11 #include <openssl/err.h> | 11 #include <openssl/err.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" |
22 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
23 #include "net/cert/cert_verifier.h" | 23 #include "net/cert/cert_verifier.h" |
| 24 #include "net/cert/ct_verifier.h" |
24 #include "net/cert/single_request_cert_verifier.h" | 25 #include "net/cert/single_request_cert_verifier.h" |
25 #include "net/cert/x509_certificate_net_log_param.h" | 26 #include "net/cert/x509_certificate_net_log_param.h" |
26 #include "net/http/transport_security_state.h" | 27 #include "net/http/transport_security_state.h" |
27 #include "net/socket/ssl_error_params.h" | 28 #include "net/socket/ssl_error_params.h" |
28 #include "net/socket/ssl_session_cache_openssl.h" | 29 #include "net/socket/ssl_session_cache_openssl.h" |
29 #include "net/ssl/openssl_ssl_util.h" | 30 #include "net/ssl/openssl_ssl_util.h" |
30 #include "net/ssl/ssl_cert_request_info.h" | 31 #include "net/ssl/ssl_cert_request_info.h" |
31 #include "net/ssl/ssl_connection_status_flags.h" | 32 #include "net/ssl/ssl_connection_status_flags.h" |
32 #include "net/ssl/ssl_info.h" | 33 #include "net/ssl/ssl_info.h" |
33 | 34 |
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
341 transport_recv_busy_(false), | 342 transport_recv_busy_(false), |
342 weak_factory_(this), | 343 weak_factory_(this), |
343 pending_read_error_(kNoPendingReadResult), | 344 pending_read_error_(kNoPendingReadResult), |
344 transport_read_error_(OK), | 345 transport_read_error_(OK), |
345 transport_write_error_(OK), | 346 transport_write_error_(OK), |
346 server_cert_chain_(new PeerCertificateChain(NULL)), | 347 server_cert_chain_(new PeerCertificateChain(NULL)), |
347 completed_connect_(false), | 348 completed_connect_(false), |
348 was_ever_used_(false), | 349 was_ever_used_(false), |
349 client_auth_cert_needed_(false), | 350 client_auth_cert_needed_(false), |
350 cert_verifier_(context.cert_verifier), | 351 cert_verifier_(context.cert_verifier), |
| 352 cert_transparency_verifier_(context.cert_transparency_verifier), |
351 channel_id_service_(context.channel_id_service), | 353 channel_id_service_(context.channel_id_service), |
352 ssl_(NULL), | 354 ssl_(NULL), |
353 transport_bio_(NULL), | 355 transport_bio_(NULL), |
354 transport_(transport_socket.Pass()), | 356 transport_(transport_socket.Pass()), |
355 host_and_port_(host_and_port), | 357 host_and_port_(host_and_port), |
356 ssl_config_(ssl_config), | 358 ssl_config_(ssl_config), |
357 ssl_session_cache_shard_(context.ssl_session_cache_shard), | 359 ssl_session_cache_shard_(context.ssl_session_cache_shard), |
358 trying_cached_session_(false), | 360 trying_cached_session_(false), |
359 next_handshake_state_(STATE_NONE), | 361 next_handshake_state_(STATE_NONE), |
360 npn_status_(kNextProtoUnsupported), | 362 npn_status_(kNextProtoUnsupported), |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
593 ssl_info->cert_status = server_cert_verify_result_.cert_status; | 595 ssl_info->cert_status = server_cert_verify_result_.cert_status; |
594 ssl_info->is_issued_by_known_root = | 596 ssl_info->is_issued_by_known_root = |
595 server_cert_verify_result_.is_issued_by_known_root; | 597 server_cert_verify_result_.is_issued_by_known_root; |
596 ssl_info->public_key_hashes = | 598 ssl_info->public_key_hashes = |
597 server_cert_verify_result_.public_key_hashes; | 599 server_cert_verify_result_.public_key_hashes; |
598 ssl_info->client_cert_sent = | 600 ssl_info->client_cert_sent = |
599 ssl_config_.send_client_cert && ssl_config_.client_cert.get(); | 601 ssl_config_.send_client_cert && ssl_config_.client_cert.get(); |
600 ssl_info->channel_id_sent = WasChannelIDSent(); | 602 ssl_info->channel_id_sent = WasChannelIDSent(); |
601 ssl_info->pinning_failure_log = pinning_failure_log_; | 603 ssl_info->pinning_failure_log = pinning_failure_log_; |
602 | 604 |
| 605 AddSCTInfoToSSLInfo(ssl_info); |
| 606 |
603 const SSL_CIPHER* cipher = SSL_get_current_cipher(ssl_); | 607 const SSL_CIPHER* cipher = SSL_get_current_cipher(ssl_); |
604 CHECK(cipher); | 608 CHECK(cipher); |
605 ssl_info->security_bits = SSL_CIPHER_get_bits(cipher, NULL); | 609 ssl_info->security_bits = SSL_CIPHER_get_bits(cipher, NULL); |
606 | 610 |
607 ssl_info->connection_status = EncodeSSLConnectionStatus( | 611 ssl_info->connection_status = EncodeSSLConnectionStatus( |
608 SSL_CIPHER_get_id(cipher), 0 /* no compression */, | 612 SSL_CIPHER_get_id(cipher), 0 /* no compression */, |
609 GetNetSSLVersion(ssl_)); | 613 GetNetSSLVersion(ssl_)); |
610 | 614 |
611 if (!SSL_get_secure_renegotiation_support(ssl_)) | 615 if (!SSL_get_secure_renegotiation_support(ssl_)) |
612 ssl_info->connection_status |= SSL_CONNECTION_NO_RENEGOTIATION_EXTENSION; | 616 ssl_info->connection_status |= SSL_CONNECTION_NO_RENEGOTIATION_EXTENSION; |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
801 SSL_enable_tls_channel_id(ssl_); | 805 SSL_enable_tls_channel_id(ssl_); |
802 } | 806 } |
803 | 807 |
804 if (!ssl_config_.next_protos.empty()) { | 808 if (!ssl_config_.next_protos.empty()) { |
805 std::vector<uint8_t> wire_protos = | 809 std::vector<uint8_t> wire_protos = |
806 SerializeNextProtos(ssl_config_.next_protos); | 810 SerializeNextProtos(ssl_config_.next_protos); |
807 SSL_set_alpn_protos(ssl_, wire_protos.empty() ? NULL : &wire_protos[0], | 811 SSL_set_alpn_protos(ssl_, wire_protos.empty() ? NULL : &wire_protos[0], |
808 wire_protos.size()); | 812 wire_protos.size()); |
809 } | 813 } |
810 | 814 |
| 815 if (ssl_config_.signed_cert_timestamps_enabled) { |
| 816 SSL_enable_signed_cert_timestamps(ssl_); |
| 817 SSL_enable_ocsp_stapling(ssl_); |
| 818 } |
| 819 |
| 820 // TODO(davidben): Enable OCSP stapling on platforms which support it and pass |
| 821 // into the certificate verifier. |
| 822 |
811 return OK; | 823 return OK; |
812 } | 824 } |
813 | 825 |
814 void SSLClientSocketOpenSSL::DoReadCallback(int rv) { | 826 void SSLClientSocketOpenSSL::DoReadCallback(int rv) { |
815 // Since Run may result in Read being called, clear |user_read_callback_| | 827 // Since Run may result in Read being called, clear |user_read_callback_| |
816 // up front. | 828 // up front. |
817 if (rv > 0) | 829 if (rv > 0) |
818 was_ever_used_ = true; | 830 was_ever_used_ = true; |
819 user_read_buf_ = NULL; | 831 user_read_buf_ = NULL; |
820 user_read_buf_len_ = 0; | 832 user_read_buf_len_ = 0; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
896 npn_proto_.assign(reinterpret_cast<const char*>(alpn_proto), alpn_len); | 908 npn_proto_.assign(reinterpret_cast<const char*>(alpn_proto), alpn_len); |
897 npn_status_ = kNextProtoNegotiated; | 909 npn_status_ = kNextProtoNegotiated; |
898 } | 910 } |
899 } | 911 } |
900 | 912 |
901 RecordChannelIDSupport(channel_id_service_, | 913 RecordChannelIDSupport(channel_id_service_, |
902 channel_id_xtn_negotiated_, | 914 channel_id_xtn_negotiated_, |
903 ssl_config_.channel_id_enabled, | 915 ssl_config_.channel_id_enabled, |
904 crypto::ECPrivateKey::IsSupported()); | 916 crypto::ECPrivateKey::IsSupported()); |
905 | 917 |
| 918 uint8_t* ocsp_response; |
| 919 size_t ocsp_response_len; |
| 920 SSL_get0_ocsp_response(ssl_, &ocsp_response, &ocsp_response_len); |
| 921 set_stapled_ocsp_response_received(ocsp_response_len != 0); |
| 922 |
| 923 uint8_t* sct_list; |
| 924 size_t sct_list_len; |
| 925 SSL_get0_signed_cert_timestamp_list(ssl_, &sct_list, &sct_list_len); |
| 926 set_signed_cert_timestamps_received(sct_list_len != 0); |
| 927 |
906 // Verify the certificate. | 928 // Verify the certificate. |
907 const bool got_cert = !!UpdateServerCert(); | 929 const bool got_cert = !!UpdateServerCert(); |
908 DCHECK(got_cert); | 930 DCHECK(got_cert); |
909 net_log_.AddEvent( | 931 net_log_.AddEvent( |
910 NetLog::TYPE_SSL_CERTIFICATES_RECEIVED, | 932 NetLog::TYPE_SSL_CERTIFICATES_RECEIVED, |
911 base::Bind(&NetLogX509CertificateCallback, | 933 base::Bind(&NetLogX509CertificateCallback, |
912 base::Unretained(server_cert_.get()))); | 934 base::Unretained(server_cert_.get()))); |
913 GotoState(STATE_VERIFY_CERT); | 935 GotoState(STATE_VERIFY_CERT); |
914 } else { | 936 } else { |
915 int ssl_error = SSL_get_error(ssl_, rv); | 937 int ssl_error = SSL_get_error(ssl_, rv); |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1051 !transport_security_state_->CheckPublicKeyPins( | 1073 !transport_security_state_->CheckPublicKeyPins( |
1052 host_and_port_.host(), | 1074 host_and_port_.host(), |
1053 sni_available, | 1075 sni_available, |
1054 server_cert_verify_result_.is_issued_by_known_root, | 1076 server_cert_verify_result_.is_issued_by_known_root, |
1055 server_cert_verify_result_.public_key_hashes, | 1077 server_cert_verify_result_.public_key_hashes, |
1056 &pinning_failure_log_)) { | 1078 &pinning_failure_log_)) { |
1057 result = ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN; | 1079 result = ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN; |
1058 } | 1080 } |
1059 | 1081 |
1060 if (result == OK) { | 1082 if (result == OK) { |
| 1083 // Only check Certificate Transparency if there were no other errors with |
| 1084 // the connection. |
| 1085 VerifyCT(); |
| 1086 |
1061 // TODO(joth): Work out if we need to remember the intermediate CA certs | 1087 // TODO(joth): Work out if we need to remember the intermediate CA certs |
1062 // when the server sends them to us, and do so here. | 1088 // when the server sends them to us, and do so here. |
1063 SSLContext::GetInstance()->session_cache()->MarkSSLSessionAsGood(ssl_); | 1089 SSLContext::GetInstance()->session_cache()->MarkSSLSessionAsGood(ssl_); |
1064 marked_session_as_good_ = true; | 1090 marked_session_as_good_ = true; |
1065 CheckIfHandshakeFinished(); | 1091 CheckIfHandshakeFinished(); |
1066 } else { | 1092 } else { |
1067 DVLOG(1) << "DoVerifyCertComplete error " << ErrorToString(result) | 1093 DVLOG(1) << "DoVerifyCertComplete error " << ErrorToString(result) |
1068 << " (" << result << ")"; | 1094 << " (" << result << ")"; |
1069 } | 1095 } |
1070 | 1096 |
(...skipping 17 matching lines...) Expand all Loading... |
1088 X509Certificate* SSLClientSocketOpenSSL::UpdateServerCert() { | 1114 X509Certificate* SSLClientSocketOpenSSL::UpdateServerCert() { |
1089 server_cert_chain_->Reset(SSL_get_peer_cert_chain(ssl_)); | 1115 server_cert_chain_->Reset(SSL_get_peer_cert_chain(ssl_)); |
1090 server_cert_ = server_cert_chain_->AsOSChain(); | 1116 server_cert_ = server_cert_chain_->AsOSChain(); |
1091 | 1117 |
1092 if (!server_cert_chain_->IsValid()) | 1118 if (!server_cert_chain_->IsValid()) |
1093 DVLOG(1) << "UpdateServerCert received invalid certificate chain from peer"; | 1119 DVLOG(1) << "UpdateServerCert received invalid certificate chain from peer"; |
1094 | 1120 |
1095 return server_cert_.get(); | 1121 return server_cert_.get(); |
1096 } | 1122 } |
1097 | 1123 |
| 1124 void SSLClientSocketOpenSSL::VerifyCT() { |
| 1125 if (!cert_transparency_verifier_) |
| 1126 return; |
| 1127 |
| 1128 uint8_t* ocsp_response; |
| 1129 size_t ocsp_response_len; |
| 1130 SSL_get0_ocsp_response(ssl_, &ocsp_response, &ocsp_response_len); |
| 1131 |
| 1132 uint8_t* sct_list; |
| 1133 size_t sct_list_len; |
| 1134 SSL_get0_signed_cert_timestamp_list(ssl_, &sct_list, &sct_list_len); |
| 1135 |
| 1136 // Note that this is a completely synchronous operation: The CT Log Verifier |
| 1137 // gets all the data it needs for SCT verification and does not do any |
| 1138 // external communication. |
| 1139 int result = cert_transparency_verifier_->Verify( |
| 1140 server_cert_verify_result_.verified_cert.get(), |
| 1141 std::string(reinterpret_cast<const char*>(ocsp_response), |
| 1142 ocsp_response_len), |
| 1143 std::string(reinterpret_cast<const char*>(sct_list), |
| 1144 sct_list_len), |
| 1145 &ct_verify_result_, |
| 1146 net_log_); |
| 1147 |
| 1148 VLOG(1) << "CT Verification complete: result " << result |
| 1149 << " Invalid scts: " << ct_verify_result_.invalid_scts.size() |
| 1150 << " Verified scts: " << ct_verify_result_.verified_scts.size() |
| 1151 << " scts from unknown logs: " |
| 1152 << ct_verify_result_.unknown_logs_scts.size(); |
| 1153 } |
| 1154 |
1098 void SSLClientSocketOpenSSL::OnHandshakeIOComplete(int result) { | 1155 void SSLClientSocketOpenSSL::OnHandshakeIOComplete(int result) { |
1099 int rv = DoHandshakeLoop(result); | 1156 int rv = DoHandshakeLoop(result); |
1100 if (rv != ERR_IO_PENDING) { | 1157 if (rv != ERR_IO_PENDING) { |
1101 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); | 1158 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); |
1102 DoConnectCallback(rv); | 1159 DoConnectCallback(rv); |
1103 } | 1160 } |
1104 } | 1161 } |
1105 | 1162 |
1106 void SSLClientSocketOpenSSL::OnSendComplete(int result) { | 1163 void SSLClientSocketOpenSSL::OnSendComplete(int result) { |
1107 if (next_handshake_state_ == STATE_HANDSHAKE) { | 1164 if (next_handshake_state_ == STATE_HANDSHAKE) { |
(...skipping 541 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1649 // CheckIfHandshakeFinished is called twice per connection: once after | 1706 // CheckIfHandshakeFinished is called twice per connection: once after |
1650 // MarkSSLSessionAsGood, when the certificate has been verified, and | 1707 // MarkSSLSessionAsGood, when the certificate has been verified, and |
1651 // once via an OpenSSL callback when the handshake has completed. On the | 1708 // once via an OpenSSL callback when the handshake has completed. On the |
1652 // second call, when the certificate has been verified and the handshake | 1709 // second call, when the certificate has been verified and the handshake |
1653 // has completed, the connection's handshake completion callback is run. | 1710 // has completed, the connection's handshake completion callback is run. |
1654 void SSLClientSocketOpenSSL::CheckIfHandshakeFinished() { | 1711 void SSLClientSocketOpenSSL::CheckIfHandshakeFinished() { |
1655 if (handshake_succeeded_ && marked_session_as_good_) | 1712 if (handshake_succeeded_ && marked_session_as_good_) |
1656 OnHandshakeCompletion(); | 1713 OnHandshakeCompletion(); |
1657 } | 1714 } |
1658 | 1715 |
| 1716 void SSLClientSocketOpenSSL::AddSCTInfoToSSLInfo(SSLInfo* ssl_info) const { |
| 1717 for (ct::SCTList::const_iterator iter = |
| 1718 ct_verify_result_.verified_scts.begin(); |
| 1719 iter != ct_verify_result_.verified_scts.end(); ++iter) { |
| 1720 ssl_info->signed_certificate_timestamps.push_back( |
| 1721 SignedCertificateTimestampAndStatus(*iter, ct::SCT_STATUS_OK)); |
| 1722 } |
| 1723 for (ct::SCTList::const_iterator iter = |
| 1724 ct_verify_result_.invalid_scts.begin(); |
| 1725 iter != ct_verify_result_.invalid_scts.end(); ++iter) { |
| 1726 ssl_info->signed_certificate_timestamps.push_back( |
| 1727 SignedCertificateTimestampAndStatus(*iter, ct::SCT_STATUS_INVALID)); |
| 1728 } |
| 1729 for (ct::SCTList::const_iterator iter = |
| 1730 ct_verify_result_.unknown_logs_scts.begin(); |
| 1731 iter != ct_verify_result_.unknown_logs_scts.end(); ++iter) { |
| 1732 ssl_info->signed_certificate_timestamps.push_back( |
| 1733 SignedCertificateTimestampAndStatus(*iter, |
| 1734 ct::SCT_STATUS_LOG_UNKNOWN)); |
| 1735 } |
| 1736 } |
| 1737 |
1659 scoped_refptr<X509Certificate> | 1738 scoped_refptr<X509Certificate> |
1660 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { | 1739 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { |
1661 return server_cert_; | 1740 return server_cert_; |
1662 } | 1741 } |
1663 | 1742 |
1664 } // namespace net | 1743 } // namespace net |
OLD | NEW |