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 669 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
840 IsTLSVersionAdequateForHTTP2(ssl_config_)); | 827 IsTLSVersionAdequateForHTTP2(ssl_config_)); |
841 SSL_set_alpn_protos(ssl_, wire_protos.empty() ? NULL : &wire_protos[0], | 828 SSL_set_alpn_protos(ssl_, wire_protos.empty() ? NULL : &wire_protos[0], |
842 wire_protos.size()); | 829 wire_protos.size()); |
843 } | 830 } |
844 | 831 |
845 if (ssl_config_.signed_cert_timestamps_enabled) { | 832 if (ssl_config_.signed_cert_timestamps_enabled) { |
846 SSL_enable_signed_cert_timestamps(ssl_); | 833 SSL_enable_signed_cert_timestamps(ssl_); |
847 SSL_enable_ocsp_stapling(ssl_); | 834 SSL_enable_ocsp_stapling(ssl_); |
848 } | 835 } |
849 | 836 |
850 if (IsOCSPStaplingSupported()) | 837 if (cert_verifier_->SupportsOCSPStapling()) |
851 SSL_enable_ocsp_stapling(ssl_); | 838 SSL_enable_ocsp_stapling(ssl_); |
852 | 839 |
853 // Enable fastradio padding. | 840 // Enable fastradio padding. |
854 SSL_enable_fastradio_padding(ssl_, | 841 SSL_enable_fastradio_padding(ssl_, |
855 ssl_config_.fastradio_padding_enabled && | 842 ssl_config_.fastradio_padding_enabled && |
856 ssl_config_.fastradio_padding_eligible); | 843 ssl_config_.fastradio_padding_eligible); |
857 | 844 |
858 return OK; | 845 return OK; |
859 } | 846 } |
860 | 847 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
940 } | 927 } |
941 } | 928 } |
942 | 929 |
943 RecordChannelIDSupport(channel_id_service_, | 930 RecordChannelIDSupport(channel_id_service_, |
944 channel_id_xtn_negotiated_, | 931 channel_id_xtn_negotiated_, |
945 ssl_config_.channel_id_enabled, | 932 ssl_config_.channel_id_enabled, |
946 crypto::ECPrivateKey::IsSupported()); | 933 crypto::ECPrivateKey::IsSupported()); |
947 | 934 |
948 // Only record OCSP histograms if OCSP was requested. | 935 // Only record OCSP histograms if OCSP was requested. |
949 if (ssl_config_.signed_cert_timestamps_enabled || | 936 if (ssl_config_.signed_cert_timestamps_enabled || |
950 IsOCSPStaplingSupported()) { | 937 cert_verifier_->SupportsOCSPStapling()) { |
951 const uint8_t* ocsp_response; | 938 const uint8_t* ocsp_response; |
952 size_t ocsp_response_len; | 939 size_t ocsp_response_len; |
953 SSL_get0_ocsp_response(ssl_, &ocsp_response, &ocsp_response_len); | 940 SSL_get0_ocsp_response(ssl_, &ocsp_response, &ocsp_response_len); |
954 | 941 |
955 set_stapled_ocsp_response_received(ocsp_response_len != 0); | 942 set_stapled_ocsp_response_received(ocsp_response_len != 0); |
956 UMA_HISTOGRAM_BOOLEAN("Net.OCSPResponseStapled", ocsp_response_len != 0); | 943 UMA_HISTOGRAM_BOOLEAN("Net.OCSPResponseStapled", ocsp_response_len != 0); |
957 } | 944 } |
958 | 945 |
959 const uint8_t* sct_list; | 946 const uint8_t* sct_list; |
960 size_t sct_list_len; | 947 size_t sct_list_len; |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1073 | 1060 |
1074 // When running in a sandbox, it may not be possible to create an | 1061 // When running in a sandbox, it may not be possible to create an |
1075 // X509Certificate*, as that may depend on OS functionality blocked | 1062 // X509Certificate*, as that may depend on OS functionality blocked |
1076 // in the sandbox. | 1063 // in the sandbox. |
1077 if (!server_cert_.get()) { | 1064 if (!server_cert_.get()) { |
1078 server_cert_verify_result_.Reset(); | 1065 server_cert_verify_result_.Reset(); |
1079 server_cert_verify_result_.cert_status = CERT_STATUS_INVALID; | 1066 server_cert_verify_result_.cert_status = CERT_STATUS_INVALID; |
1080 return ERR_CERT_INVALID; | 1067 return ERR_CERT_INVALID; |
1081 } | 1068 } |
1082 | 1069 |
| 1070 std::string ocsp_response; |
| 1071 if (cert_verifier_->SupportsOCSPStapling()) { |
| 1072 const uint8_t* ocsp_response_raw; |
| 1073 size_t ocsp_response_len; |
| 1074 SSL_get0_ocsp_response(ssl_, &ocsp_response_raw, &ocsp_response_len); |
| 1075 ocsp_response.assign(reinterpret_cast<const char*>(ocsp_response_raw), |
| 1076 ocsp_response_len); |
| 1077 } |
| 1078 |
1083 start_cert_verification_time_ = base::TimeTicks::Now(); | 1079 start_cert_verification_time_ = base::TimeTicks::Now(); |
1084 | 1080 |
1085 int flags = 0; | 1081 int flags = 0; |
1086 if (ssl_config_.rev_checking_enabled) | 1082 if (ssl_config_.rev_checking_enabled) |
1087 flags |= CertVerifier::VERIFY_REV_CHECKING_ENABLED; | 1083 flags |= CertVerifier::VERIFY_REV_CHECKING_ENABLED; |
1088 if (ssl_config_.verify_ev_cert) | 1084 if (ssl_config_.verify_ev_cert) |
1089 flags |= CertVerifier::VERIFY_EV_CERT; | 1085 flags |= CertVerifier::VERIFY_EV_CERT; |
1090 if (ssl_config_.cert_io_enabled) | 1086 if (ssl_config_.cert_io_enabled) |
1091 flags |= CertVerifier::VERIFY_CERT_IO_ENABLED; | 1087 flags |= CertVerifier::VERIFY_CERT_IO_ENABLED; |
1092 if (ssl_config_.rev_checking_required_local_anchors) | 1088 if (ssl_config_.rev_checking_required_local_anchors) |
1093 flags |= CertVerifier::VERIFY_REV_CHECKING_REQUIRED_LOCAL_ANCHORS; | 1089 flags |= CertVerifier::VERIFY_REV_CHECKING_REQUIRED_LOCAL_ANCHORS; |
1094 verifier_.reset(new SingleRequestCertVerifier(cert_verifier_)); | 1090 verifier_.reset(new SingleRequestCertVerifier(cert_verifier_)); |
1095 return verifier_->Verify( | 1091 return verifier_->Verify( |
1096 server_cert_.get(), | 1092 server_cert_.get(), host_and_port_.host(), ocsp_response, flags, |
1097 host_and_port_.host(), | |
1098 flags, | |
1099 // TODO(davidben): Route the CRLSet through SSLConfig so | 1093 // TODO(davidben): Route the CRLSet through SSLConfig so |
1100 // SSLClientSocket doesn't depend on SSLConfigService. | 1094 // SSLClientSocket doesn't depend on SSLConfigService. |
1101 SSLConfigService::GetCRLSet().get(), | 1095 SSLConfigService::GetCRLSet().get(), &server_cert_verify_result_, |
1102 &server_cert_verify_result_, | |
1103 base::Bind(&SSLClientSocketOpenSSL::OnHandshakeIOComplete, | 1096 base::Bind(&SSLClientSocketOpenSSL::OnHandshakeIOComplete, |
1104 base::Unretained(this)), | 1097 base::Unretained(this)), |
1105 net_log_); | 1098 net_log_); |
1106 } | 1099 } |
1107 | 1100 |
1108 int SSLClientSocketOpenSSL::DoVerifyCertComplete(int result) { | 1101 int SSLClientSocketOpenSSL::DoVerifyCertComplete(int result) { |
1109 verifier_.reset(); | 1102 verifier_.reset(); |
1110 | 1103 |
1111 if (!start_cert_verification_time_.is_null()) { | 1104 if (!start_cert_verification_time_.is_null()) { |
1112 base::TimeDelta verify_time = | 1105 base::TimeDelta verify_time = |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1168 } | 1161 } |
1169 | 1162 |
1170 void SSLClientSocketOpenSSL::UpdateServerCert() { | 1163 void SSLClientSocketOpenSSL::UpdateServerCert() { |
1171 server_cert_chain_->Reset(SSL_get_peer_cert_chain(ssl_)); | 1164 server_cert_chain_->Reset(SSL_get_peer_cert_chain(ssl_)); |
1172 server_cert_ = server_cert_chain_->AsOSChain(); | 1165 server_cert_ = server_cert_chain_->AsOSChain(); |
1173 if (server_cert_.get()) { | 1166 if (server_cert_.get()) { |
1174 net_log_.AddEvent( | 1167 net_log_.AddEvent( |
1175 NetLog::TYPE_SSL_CERTIFICATES_RECEIVED, | 1168 NetLog::TYPE_SSL_CERTIFICATES_RECEIVED, |
1176 base::Bind(&NetLogX509CertificateCallback, | 1169 base::Bind(&NetLogX509CertificateCallback, |
1177 base::Unretained(server_cert_.get()))); | 1170 base::Unretained(server_cert_.get()))); |
1178 | |
1179 // TODO(rsleevi): Plumb an OCSP response into the Mac system library and | |
1180 // update IsOCSPStaplingSupported for Mac. https://crbug.com/430714 | |
1181 if (IsOCSPStaplingSupported()) { | |
1182 #if defined(OS_WIN) | |
1183 const uint8_t* ocsp_response_raw; | |
1184 size_t ocsp_response_len; | |
1185 SSL_get0_ocsp_response(ssl_, &ocsp_response_raw, &ocsp_response_len); | |
1186 | |
1187 CRYPT_DATA_BLOB ocsp_response_blob; | |
1188 ocsp_response_blob.cbData = ocsp_response_len; | |
1189 ocsp_response_blob.pbData = const_cast<BYTE*>(ocsp_response_raw); | |
1190 BOOL ok = CertSetCertificateContextProperty( | |
1191 server_cert_->os_cert_handle(), | |
1192 CERT_OCSP_RESPONSE_PROP_ID, | |
1193 CERT_SET_PROPERTY_IGNORE_PERSIST_ERROR_FLAG, | |
1194 &ocsp_response_blob); | |
1195 if (!ok) { | |
1196 VLOG(1) << "Failed to set OCSP response property: " | |
1197 << GetLastError(); | |
1198 } | |
1199 #else | |
1200 // TODO(davidben): Support OCSP stapling when NSS is the system | |
1201 // certificate verifier. https://crbug.com/479034. | |
1202 NOTREACHED(); | |
1203 #endif | |
1204 } | |
1205 } | 1171 } |
1206 } | 1172 } |
1207 | 1173 |
1208 void SSLClientSocketOpenSSL::VerifyCT() { | 1174 void SSLClientSocketOpenSSL::VerifyCT() { |
1209 if (!cert_transparency_verifier_) | 1175 if (!cert_transparency_verifier_) |
1210 return; | 1176 return; |
1211 | 1177 |
1212 const uint8_t* ocsp_response_raw; | 1178 const uint8_t* ocsp_response_raw; |
1213 size_t ocsp_response_len; | 1179 size_t ocsp_response_len; |
1214 SSL_get0_ocsp_response(ssl_, &ocsp_response_raw, &ocsp_response_len); | 1180 SSL_get0_ocsp_response(ssl_, &ocsp_response_raw, &ocsp_response_len); |
(...skipping 704 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1919 | 1885 |
1920 return result; | 1886 return result; |
1921 } | 1887 } |
1922 | 1888 |
1923 scoped_refptr<X509Certificate> | 1889 scoped_refptr<X509Certificate> |
1924 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { | 1890 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { |
1925 return server_cert_; | 1891 return server_cert_; |
1926 } | 1892 } |
1927 | 1893 |
1928 } // namespace net | 1894 } // namespace net |
OLD | NEW |