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 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 sk_X509_push(stack.get(), x509.release()); | 138 sk_X509_push(stack.get(), x509.release()); |
139 } | 139 } |
140 return stack.Pass(); | 140 return stack.Pass(); |
141 } | 141 } |
142 | 142 |
143 int LogErrorCallback(const char* str, size_t len, void* context) { | 143 int LogErrorCallback(const char* str, size_t len, void* context) { |
144 LOG(ERROR) << base::StringPiece(str, len); | 144 LOG(ERROR) << base::StringPiece(str, len); |
145 return 1; | 145 return 1; |
146 } | 146 } |
147 | 147 |
148 bool IsOCSPStaplingSupported() { | |
149 #if defined(OS_WIN) | |
150 // CERT_OCSP_RESPONSE_PROP_ID is only implemented on Vista+, but it can be | |
151 // set on Windows XP without error. There is some overhead from the server | |
152 // sending the OCSP response if it supports the extension, for the subset of | |
153 // XP clients who will request it but be unable to use it, but this is an | |
154 // acceptable trade-off for simplicity of implementation. | |
155 return true; | |
156 #else | |
157 return false; | |
158 #endif | |
159 } | |
160 | |
161 } // namespace | 148 } // namespace |
162 | 149 |
163 class SSLClientSocketOpenSSL::SSLContext { | 150 class SSLClientSocketOpenSSL::SSLContext { |
164 public: | 151 public: |
165 static SSLContext* GetInstance() { return Singleton<SSLContext>::get(); } | 152 static SSLContext* GetInstance() { return Singleton<SSLContext>::get(); } |
166 SSL_CTX* ssl_ctx() { return ssl_ctx_.get(); } | 153 SSL_CTX* ssl_ctx() { return ssl_ctx_.get(); } |
167 SSLClientSessionCacheOpenSSL* session_cache() { return &session_cache_; } | 154 SSLClientSessionCacheOpenSSL* session_cache() { return &session_cache_; } |
168 | 155 |
169 SSLClientSocketOpenSSL* GetClientSocketFromSSL(const SSL* ssl) { | 156 SSLClientSocketOpenSSL* GetClientSocketFromSSL(const SSL* ssl) { |
170 DCHECK(ssl); | 157 DCHECK(ssl); |
(...skipping 668 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
839 IsTLSVersionAdequateForHTTP2(ssl_config_)); | 826 IsTLSVersionAdequateForHTTP2(ssl_config_)); |
840 SSL_set_alpn_protos(ssl_, wire_protos.empty() ? NULL : &wire_protos[0], | 827 SSL_set_alpn_protos(ssl_, wire_protos.empty() ? NULL : &wire_protos[0], |
841 wire_protos.size()); | 828 wire_protos.size()); |
842 } | 829 } |
843 | 830 |
844 if (ssl_config_.signed_cert_timestamps_enabled) { | 831 if (ssl_config_.signed_cert_timestamps_enabled) { |
845 SSL_enable_signed_cert_timestamps(ssl_); | 832 SSL_enable_signed_cert_timestamps(ssl_); |
846 SSL_enable_ocsp_stapling(ssl_); | 833 SSL_enable_ocsp_stapling(ssl_); |
847 } | 834 } |
848 | 835 |
849 if (IsOCSPStaplingSupported()) | 836 if (cert_verifier_->SupportsOCSPStapling()) |
850 SSL_enable_ocsp_stapling(ssl_); | 837 SSL_enable_ocsp_stapling(ssl_); |
851 | 838 |
852 // Enable fastradio padding. | 839 // Enable fastradio padding. |
853 SSL_enable_fastradio_padding(ssl_, | 840 SSL_enable_fastradio_padding(ssl_, |
854 ssl_config_.fastradio_padding_enabled && | 841 ssl_config_.fastradio_padding_enabled && |
855 ssl_config_.fastradio_padding_eligible); | 842 ssl_config_.fastradio_padding_eligible); |
856 | 843 |
857 return OK; | 844 return OK; |
858 } | 845 } |
859 | 846 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
939 } | 926 } |
940 } | 927 } |
941 | 928 |
942 RecordChannelIDSupport(channel_id_service_, | 929 RecordChannelIDSupport(channel_id_service_, |
943 channel_id_xtn_negotiated_, | 930 channel_id_xtn_negotiated_, |
944 ssl_config_.channel_id_enabled, | 931 ssl_config_.channel_id_enabled, |
945 crypto::ECPrivateKey::IsSupported()); | 932 crypto::ECPrivateKey::IsSupported()); |
946 | 933 |
947 // Only record OCSP histograms if OCSP was requested. | 934 // Only record OCSP histograms if OCSP was requested. |
948 if (ssl_config_.signed_cert_timestamps_enabled || | 935 if (ssl_config_.signed_cert_timestamps_enabled || |
949 IsOCSPStaplingSupported()) { | 936 cert_verifier_->SupportsOCSPStapling()) { |
950 const uint8_t* ocsp_response; | 937 const uint8_t* ocsp_response; |
951 size_t ocsp_response_len; | 938 size_t ocsp_response_len; |
952 SSL_get0_ocsp_response(ssl_, &ocsp_response, &ocsp_response_len); | 939 SSL_get0_ocsp_response(ssl_, &ocsp_response, &ocsp_response_len); |
953 | 940 |
954 set_stapled_ocsp_response_received(ocsp_response_len != 0); | 941 set_stapled_ocsp_response_received(ocsp_response_len != 0); |
955 UMA_HISTOGRAM_BOOLEAN("Net.OCSPResponseStapled", ocsp_response_len != 0); | 942 UMA_HISTOGRAM_BOOLEAN("Net.OCSPResponseStapled", ocsp_response_len != 0); |
956 } | 943 } |
957 | 944 |
958 const uint8_t* sct_list; | 945 const uint8_t* sct_list; |
959 size_t sct_list_len; | 946 size_t sct_list_len; |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1072 | 1059 |
1073 // When running in a sandbox, it may not be possible to create an | 1060 // When running in a sandbox, it may not be possible to create an |
1074 // X509Certificate*, as that may depend on OS functionality blocked | 1061 // X509Certificate*, as that may depend on OS functionality blocked |
1075 // in the sandbox. | 1062 // in the sandbox. |
1076 if (!server_cert_.get()) { | 1063 if (!server_cert_.get()) { |
1077 server_cert_verify_result_.Reset(); | 1064 server_cert_verify_result_.Reset(); |
1078 server_cert_verify_result_.cert_status = CERT_STATUS_INVALID; | 1065 server_cert_verify_result_.cert_status = CERT_STATUS_INVALID; |
1079 return ERR_CERT_INVALID; | 1066 return ERR_CERT_INVALID; |
1080 } | 1067 } |
1081 | 1068 |
| 1069 std::string ocsp_response; |
| 1070 if (cert_verifier_->SupportsOCSPStapling()) { |
| 1071 const uint8_t* ocsp_response_raw; |
| 1072 size_t ocsp_response_len; |
| 1073 SSL_get0_ocsp_response(ssl_, &ocsp_response_raw, &ocsp_response_len); |
| 1074 ocsp_response.assign(reinterpret_cast<const char*>(ocsp_response_raw), |
| 1075 ocsp_response_len); |
| 1076 } |
| 1077 |
1082 start_cert_verification_time_ = base::TimeTicks::Now(); | 1078 start_cert_verification_time_ = base::TimeTicks::Now(); |
1083 | 1079 |
1084 int flags = 0; | 1080 int flags = 0; |
1085 if (ssl_config_.rev_checking_enabled) | 1081 if (ssl_config_.rev_checking_enabled) |
1086 flags |= CertVerifier::VERIFY_REV_CHECKING_ENABLED; | 1082 flags |= CertVerifier::VERIFY_REV_CHECKING_ENABLED; |
1087 if (ssl_config_.verify_ev_cert) | 1083 if (ssl_config_.verify_ev_cert) |
1088 flags |= CertVerifier::VERIFY_EV_CERT; | 1084 flags |= CertVerifier::VERIFY_EV_CERT; |
1089 if (ssl_config_.cert_io_enabled) | 1085 if (ssl_config_.cert_io_enabled) |
1090 flags |= CertVerifier::VERIFY_CERT_IO_ENABLED; | 1086 flags |= CertVerifier::VERIFY_CERT_IO_ENABLED; |
1091 if (ssl_config_.rev_checking_required_local_anchors) | 1087 if (ssl_config_.rev_checking_required_local_anchors) |
1092 flags |= CertVerifier::VERIFY_REV_CHECKING_REQUIRED_LOCAL_ANCHORS; | 1088 flags |= CertVerifier::VERIFY_REV_CHECKING_REQUIRED_LOCAL_ANCHORS; |
1093 verifier_.reset(new SingleRequestCertVerifier(cert_verifier_)); | 1089 verifier_.reset(new SingleRequestCertVerifier(cert_verifier_)); |
1094 return verifier_->Verify( | 1090 return verifier_->Verify( |
1095 server_cert_.get(), | 1091 server_cert_.get(), host_and_port_.host(), ocsp_response, flags, |
1096 host_and_port_.host(), | |
1097 flags, | |
1098 // TODO(davidben): Route the CRLSet through SSLConfig so | 1092 // TODO(davidben): Route the CRLSet through SSLConfig so |
1099 // SSLClientSocket doesn't depend on SSLConfigService. | 1093 // SSLClientSocket doesn't depend on SSLConfigService. |
1100 SSLConfigService::GetCRLSet().get(), | 1094 SSLConfigService::GetCRLSet().get(), &server_cert_verify_result_, |
1101 &server_cert_verify_result_, | |
1102 base::Bind(&SSLClientSocketOpenSSL::OnHandshakeIOComplete, | 1095 base::Bind(&SSLClientSocketOpenSSL::OnHandshakeIOComplete, |
1103 base::Unretained(this)), | 1096 base::Unretained(this)), |
1104 net_log_); | 1097 net_log_); |
1105 } | 1098 } |
1106 | 1099 |
1107 int SSLClientSocketOpenSSL::DoVerifyCertComplete(int result) { | 1100 int SSLClientSocketOpenSSL::DoVerifyCertComplete(int result) { |
1108 verifier_.reset(); | 1101 verifier_.reset(); |
1109 | 1102 |
1110 if (!start_cert_verification_time_.is_null()) { | 1103 if (!start_cert_verification_time_.is_null()) { |
1111 base::TimeDelta verify_time = | 1104 base::TimeDelta verify_time = |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1167 } | 1160 } |
1168 | 1161 |
1169 void SSLClientSocketOpenSSL::UpdateServerCert() { | 1162 void SSLClientSocketOpenSSL::UpdateServerCert() { |
1170 server_cert_chain_->Reset(SSL_get_peer_cert_chain(ssl_)); | 1163 server_cert_chain_->Reset(SSL_get_peer_cert_chain(ssl_)); |
1171 server_cert_ = server_cert_chain_->AsOSChain(); | 1164 server_cert_ = server_cert_chain_->AsOSChain(); |
1172 if (server_cert_.get()) { | 1165 if (server_cert_.get()) { |
1173 net_log_.AddEvent( | 1166 net_log_.AddEvent( |
1174 NetLog::TYPE_SSL_CERTIFICATES_RECEIVED, | 1167 NetLog::TYPE_SSL_CERTIFICATES_RECEIVED, |
1175 base::Bind(&NetLogX509CertificateCallback, | 1168 base::Bind(&NetLogX509CertificateCallback, |
1176 base::Unretained(server_cert_.get()))); | 1169 base::Unretained(server_cert_.get()))); |
1177 | |
1178 // TODO(rsleevi): Plumb an OCSP response into the Mac system library and | |
1179 // update IsOCSPStaplingSupported for Mac. https://crbug.com/430714 | |
1180 if (IsOCSPStaplingSupported()) { | |
1181 #if defined(OS_WIN) | |
1182 const uint8_t* ocsp_response_raw; | |
1183 size_t ocsp_response_len; | |
1184 SSL_get0_ocsp_response(ssl_, &ocsp_response_raw, &ocsp_response_len); | |
1185 | |
1186 CRYPT_DATA_BLOB ocsp_response_blob; | |
1187 ocsp_response_blob.cbData = ocsp_response_len; | |
1188 ocsp_response_blob.pbData = const_cast<BYTE*>(ocsp_response_raw); | |
1189 BOOL ok = CertSetCertificateContextProperty( | |
1190 server_cert_->os_cert_handle(), | |
1191 CERT_OCSP_RESPONSE_PROP_ID, | |
1192 CERT_SET_PROPERTY_IGNORE_PERSIST_ERROR_FLAG, | |
1193 &ocsp_response_blob); | |
1194 if (!ok) { | |
1195 VLOG(1) << "Failed to set OCSP response property: " | |
1196 << GetLastError(); | |
1197 } | |
1198 #else | |
1199 // TODO(davidben): Support OCSP stapling when NSS is the system | |
1200 // certificate verifier. https://crbug.com/479034. | |
1201 NOTREACHED(); | |
1202 #endif | |
1203 } | |
1204 } | 1170 } |
1205 } | 1171 } |
1206 | 1172 |
1207 void SSLClientSocketOpenSSL::VerifyCT() { | 1173 void SSLClientSocketOpenSSL::VerifyCT() { |
1208 if (!cert_transparency_verifier_) | 1174 if (!cert_transparency_verifier_) |
1209 return; | 1175 return; |
1210 | 1176 |
1211 const uint8_t* ocsp_response_raw; | 1177 const uint8_t* ocsp_response_raw; |
1212 size_t ocsp_response_len; | 1178 size_t ocsp_response_len; |
1213 SSL_get0_ocsp_response(ssl_, &ocsp_response_raw, &ocsp_response_len); | 1179 SSL_get0_ocsp_response(ssl_, &ocsp_response_raw, &ocsp_response_len); |
(...skipping 704 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1918 | 1884 |
1919 return result; | 1885 return result; |
1920 } | 1886 } |
1921 | 1887 |
1922 scoped_refptr<X509Certificate> | 1888 scoped_refptr<X509Certificate> |
1923 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { | 1889 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { |
1924 return server_cert_; | 1890 return server_cert_; |
1925 } | 1891 } |
1926 | 1892 |
1927 } // namespace net | 1893 } // namespace net |
OLD | NEW |