| 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 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 sk_X509_push(stack.get(), x509.release()); | 133 sk_X509_push(stack.get(), x509.release()); |
| 134 } | 134 } |
| 135 return stack.Pass(); | 135 return stack.Pass(); |
| 136 } | 136 } |
| 137 | 137 |
| 138 int LogErrorCallback(const char* str, size_t len, void* context) { | 138 int LogErrorCallback(const char* str, size_t len, void* context) { |
| 139 LOG(ERROR) << base::StringPiece(str, len); | 139 LOG(ERROR) << base::StringPiece(str, len); |
| 140 return 1; | 140 return 1; |
| 141 } | 141 } |
| 142 | 142 |
| 143 bool IsOCSPStaplingSupported() { |
| 144 #if defined(OS_WIN) |
| 145 // CERT_OCSP_RESPONSE_PROP_ID is only implemented on Vista+, but it can be |
| 146 // set on Windows XP without error. There is some overhead from the server |
| 147 // sending the OCSP response if it supports the extension, for the subset of |
| 148 // XP clients who will request it but be unable to use it, but this is an |
| 149 // acceptable trade-off for simplicity of implementation. |
| 150 return true; |
| 151 #else |
| 152 return false; |
| 153 #endif |
| 154 } |
| 155 |
| 143 } // namespace | 156 } // namespace |
| 144 | 157 |
| 145 class SSLClientSocketOpenSSL::SSLContext { | 158 class SSLClientSocketOpenSSL::SSLContext { |
| 146 public: | 159 public: |
| 147 static SSLContext* GetInstance() { return Singleton<SSLContext>::get(); } | 160 static SSLContext* GetInstance() { return Singleton<SSLContext>::get(); } |
| 148 SSL_CTX* ssl_ctx() { return ssl_ctx_.get(); } | 161 SSL_CTX* ssl_ctx() { return ssl_ctx_.get(); } |
| 149 SSLSessionCacheOpenSSL* session_cache() { return &session_cache_; } | 162 SSLSessionCacheOpenSSL* session_cache() { return &session_cache_; } |
| 150 | 163 |
| 151 SSLClientSocketOpenSSL* GetClientSocketFromSSL(const SSL* ssl) { | 164 SSLClientSocketOpenSSL* GetClientSocketFromSSL(const SSL* ssl) { |
| 152 DCHECK(ssl); | 165 DCHECK(ssl); |
| (...skipping 669 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 822 SerializeNextProtos(ssl_config_.next_protos); | 835 SerializeNextProtos(ssl_config_.next_protos); |
| 823 SSL_set_alpn_protos(ssl_, wire_protos.empty() ? NULL : &wire_protos[0], | 836 SSL_set_alpn_protos(ssl_, wire_protos.empty() ? NULL : &wire_protos[0], |
| 824 wire_protos.size()); | 837 wire_protos.size()); |
| 825 } | 838 } |
| 826 | 839 |
| 827 if (ssl_config_.signed_cert_timestamps_enabled) { | 840 if (ssl_config_.signed_cert_timestamps_enabled) { |
| 828 SSL_enable_signed_cert_timestamps(ssl_); | 841 SSL_enable_signed_cert_timestamps(ssl_); |
| 829 SSL_enable_ocsp_stapling(ssl_); | 842 SSL_enable_ocsp_stapling(ssl_); |
| 830 } | 843 } |
| 831 | 844 |
| 832 // TODO(davidben): Enable OCSP stapling on platforms which support it and pass | 845 if (IsOCSPStaplingSupported()) |
| 833 // into the certificate verifier. https://crbug.com/398677 | 846 SSL_enable_ocsp_stapling(ssl_); |
| 834 | 847 |
| 835 return OK; | 848 return OK; |
| 836 } | 849 } |
| 837 | 850 |
| 838 void SSLClientSocketOpenSSL::DoReadCallback(int rv) { | 851 void SSLClientSocketOpenSSL::DoReadCallback(int rv) { |
| 839 // Since Run may result in Read being called, clear |user_read_callback_| | 852 // Since Run may result in Read being called, clear |user_read_callback_| |
| 840 // up front. | 853 // up front. |
| 841 if (rv > 0) | 854 if (rv > 0) |
| 842 was_ever_used_ = true; | 855 was_ever_used_ = true; |
| 843 user_read_buf_ = NULL; | 856 user_read_buf_ = NULL; |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 926 npn_status_ = kNextProtoNegotiated; | 939 npn_status_ = kNextProtoNegotiated; |
| 927 set_negotiation_extension(kExtensionALPN); | 940 set_negotiation_extension(kExtensionALPN); |
| 928 } | 941 } |
| 929 } | 942 } |
| 930 | 943 |
| 931 RecordChannelIDSupport(channel_id_service_, | 944 RecordChannelIDSupport(channel_id_service_, |
| 932 channel_id_xtn_negotiated_, | 945 channel_id_xtn_negotiated_, |
| 933 ssl_config_.channel_id_enabled, | 946 ssl_config_.channel_id_enabled, |
| 934 crypto::ECPrivateKey::IsSupported()); | 947 crypto::ECPrivateKey::IsSupported()); |
| 935 | 948 |
| 936 uint8_t* ocsp_response; | 949 // Only record OCSP histograms if OCSP was requested. |
| 937 size_t ocsp_response_len; | 950 if (ssl_config_.signed_cert_timestamps_enabled || |
| 938 SSL_get0_ocsp_response(ssl_, &ocsp_response, &ocsp_response_len); | 951 IsOCSPStaplingSupported()) { |
| 939 set_stapled_ocsp_response_received(ocsp_response_len != 0); | 952 uint8_t* ocsp_response; |
| 953 size_t ocsp_response_len; |
| 954 SSL_get0_ocsp_response(ssl_, &ocsp_response, &ocsp_response_len); |
| 955 |
| 956 set_stapled_ocsp_response_received(ocsp_response_len != 0); |
| 957 UMA_HISTOGRAM_BOOLEAN("Net.OCSPResponseStapled", ocsp_response_len != 0); |
| 958 } |
| 940 | 959 |
| 941 uint8_t* sct_list; | 960 uint8_t* sct_list; |
| 942 size_t sct_list_len; | 961 size_t sct_list_len; |
| 943 SSL_get0_signed_cert_timestamp_list(ssl_, &sct_list, &sct_list_len); | 962 SSL_get0_signed_cert_timestamp_list(ssl_, &sct_list, &sct_list_len); |
| 944 set_signed_cert_timestamps_received(sct_list_len != 0); | 963 set_signed_cert_timestamps_received(sct_list_len != 0); |
| 945 | 964 |
| 946 // Verify the certificate. | 965 // Verify the certificate. |
| 947 UpdateServerCert(); | 966 UpdateServerCert(); |
| 948 GotoState(STATE_VERIFY_CERT); | 967 GotoState(STATE_VERIFY_CERT); |
| 949 } else { | 968 } else { |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1159 | 1178 |
| 1160 void SSLClientSocketOpenSSL::UpdateServerCert() { | 1179 void SSLClientSocketOpenSSL::UpdateServerCert() { |
| 1161 server_cert_chain_->Reset(SSL_get_peer_cert_chain(ssl_)); | 1180 server_cert_chain_->Reset(SSL_get_peer_cert_chain(ssl_)); |
| 1162 server_cert_ = server_cert_chain_->AsOSChain(); | 1181 server_cert_ = server_cert_chain_->AsOSChain(); |
| 1163 | 1182 |
| 1164 if (server_cert_.get()) { | 1183 if (server_cert_.get()) { |
| 1165 net_log_.AddEvent( | 1184 net_log_.AddEvent( |
| 1166 NetLog::TYPE_SSL_CERTIFICATES_RECEIVED, | 1185 NetLog::TYPE_SSL_CERTIFICATES_RECEIVED, |
| 1167 base::Bind(&NetLogX509CertificateCallback, | 1186 base::Bind(&NetLogX509CertificateCallback, |
| 1168 base::Unretained(server_cert_.get()))); | 1187 base::Unretained(server_cert_.get()))); |
| 1188 |
| 1189 // TODO(rsleevi): Plumb an OCSP response into the Mac system library and |
| 1190 // update IsOCSPStaplingSupported for Mac. https://crbug.com/430714 |
| 1191 if (IsOCSPStaplingSupported()) { |
| 1192 #if defined(OS_WIN) |
| 1193 uint8_t* ocsp_response_raw; |
| 1194 size_t ocsp_response_len; |
| 1195 SSL_get0_ocsp_response(ssl_, &ocsp_response_raw, &ocsp_response_len); |
| 1196 |
| 1197 CRYPT_DATA_BLOB ocsp_response_blob; |
| 1198 ocsp_response_blob.cbData = ocsp_response_len; |
| 1199 ocsp_response_blob.pbData = ocsp_response_raw; |
| 1200 BOOL ok = CertSetCertificateContextProperty( |
| 1201 server_cert_->os_cert_handle(), |
| 1202 CERT_OCSP_RESPONSE_PROP_ID, |
| 1203 CERT_SET_PROPERTY_IGNORE_PERSIST_ERROR_FLAG, |
| 1204 &ocsp_response_blob); |
| 1205 if (!ok) { |
| 1206 VLOG(1) << "Failed to set OCSP response property: " |
| 1207 << GetLastError(); |
| 1208 } |
| 1209 #else |
| 1210 NOTREACHED(); |
| 1211 #endif |
| 1212 } |
| 1169 } | 1213 } |
| 1170 } | 1214 } |
| 1171 | 1215 |
| 1172 void SSLClientSocketOpenSSL::VerifyCT() { | 1216 void SSLClientSocketOpenSSL::VerifyCT() { |
| 1173 if (!cert_transparency_verifier_) | 1217 if (!cert_transparency_verifier_) |
| 1174 return; | 1218 return; |
| 1175 | 1219 |
| 1176 uint8_t* ocsp_response_raw; | 1220 uint8_t* ocsp_response_raw; |
| 1177 size_t ocsp_response_len; | 1221 size_t ocsp_response_len; |
| 1178 SSL_get0_ocsp_response(ssl_, &ocsp_response_raw, &ocsp_response_len); | 1222 SSL_get0_ocsp_response(ssl_, &ocsp_response_raw, &ocsp_response_len); |
| (...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1560 } | 1604 } |
| 1561 recv_buffer_ = NULL; | 1605 recv_buffer_ = NULL; |
| 1562 transport_recv_busy_ = false; | 1606 transport_recv_busy_ = false; |
| 1563 return result; | 1607 return result; |
| 1564 } | 1608 } |
| 1565 | 1609 |
| 1566 int SSLClientSocketOpenSSL::ClientCertRequestCallback(SSL* ssl) { | 1610 int SSLClientSocketOpenSSL::ClientCertRequestCallback(SSL* ssl) { |
| 1567 DVLOG(3) << "OpenSSL ClientCertRequestCallback called"; | 1611 DVLOG(3) << "OpenSSL ClientCertRequestCallback called"; |
| 1568 DCHECK(ssl == ssl_); | 1612 DCHECK(ssl == ssl_); |
| 1569 | 1613 |
| 1614 net_log_.AddEvent(NetLog::TYPE_SSL_CLIENT_CERT_REQUESTED); |
| 1615 |
| 1570 // Clear any currently configured certificates. | 1616 // Clear any currently configured certificates. |
| 1571 SSL_certs_clear(ssl_); | 1617 SSL_certs_clear(ssl_); |
| 1572 | 1618 |
| 1573 #if defined(OS_IOS) | 1619 #if defined(OS_IOS) |
| 1574 // TODO(droger): Support client auth on iOS. See http://crbug.com/145954). | 1620 // TODO(droger): Support client auth on iOS. See http://crbug.com/145954). |
| 1575 LOG(WARNING) << "Client auth is not supported"; | 1621 LOG(WARNING) << "Client auth is not supported"; |
| 1576 #else // !defined(OS_IOS) | 1622 #else // !defined(OS_IOS) |
| 1577 if (!ssl_config_.send_client_cert) { | 1623 if (!ssl_config_.send_client_cert) { |
| 1578 // First pass: we know that a client certificate is needed, but we do not | 1624 // First pass: we know that a client certificate is needed, but we do not |
| 1579 // have one at hand. | 1625 // have one at hand. |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1637 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY); | 1683 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY); |
| 1638 return -1; | 1684 return -1; |
| 1639 } | 1685 } |
| 1640 | 1686 |
| 1641 if (!SSL_use_certificate(ssl_, leaf_x509.get()) || | 1687 if (!SSL_use_certificate(ssl_, leaf_x509.get()) || |
| 1642 !SSL_use_PrivateKey(ssl_, privkey.get()) || | 1688 !SSL_use_PrivateKey(ssl_, privkey.get()) || |
| 1643 !SSL_set1_chain(ssl_, chain.get())) { | 1689 !SSL_set1_chain(ssl_, chain.get())) { |
| 1644 LOG(WARNING) << "Failed to set client certificate"; | 1690 LOG(WARNING) << "Failed to set client certificate"; |
| 1645 return -1; | 1691 return -1; |
| 1646 } | 1692 } |
| 1693 |
| 1694 int cert_count = 1 + sk_X509_num(chain.get()); |
| 1695 net_log_.AddEvent(NetLog::TYPE_SSL_CLIENT_CERT_PROVIDED, |
| 1696 NetLog::IntegerCallback("cert_count", cert_count)); |
| 1647 return 1; | 1697 return 1; |
| 1648 } | 1698 } |
| 1649 #endif // defined(OS_IOS) | 1699 #endif // defined(OS_IOS) |
| 1650 | 1700 |
| 1651 // Send no client certificate. | 1701 // Send no client certificate. |
| 1702 net_log_.AddEvent(NetLog::TYPE_SSL_CLIENT_CERT_PROVIDED, |
| 1703 NetLog::IntegerCallback("cert_count", 0)); |
| 1652 return 1; | 1704 return 1; |
| 1653 } | 1705 } |
| 1654 | 1706 |
| 1655 int SSLClientSocketOpenSSL::CertVerifyCallback(X509_STORE_CTX* store_ctx) { | 1707 int SSLClientSocketOpenSSL::CertVerifyCallback(X509_STORE_CTX* store_ctx) { |
| 1656 if (!completed_connect_) { | 1708 if (!completed_connect_) { |
| 1657 // If the first handshake hasn't completed then we accept any certificates | 1709 // If the first handshake hasn't completed then we accept any certificates |
| 1658 // because we verify after the handshake. | 1710 // because we verify after the handshake. |
| 1659 return 1; | 1711 return 1; |
| 1660 } | 1712 } |
| 1661 | 1713 |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1821 ct::SCT_STATUS_LOG_UNKNOWN)); | 1873 ct::SCT_STATUS_LOG_UNKNOWN)); |
| 1822 } | 1874 } |
| 1823 } | 1875 } |
| 1824 | 1876 |
| 1825 scoped_refptr<X509Certificate> | 1877 scoped_refptr<X509Certificate> |
| 1826 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { | 1878 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { |
| 1827 return server_cert_; | 1879 return server_cert_; |
| 1828 } | 1880 } |
| 1829 | 1881 |
| 1830 } // namespace net | 1882 } // namespace net |
| OLD | NEW |