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 #include "net/socket/ssl_client_socket_impl.h" | 5 #include "net/socket/ssl_client_socket_impl.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <openssl/bio.h> | 8 #include <openssl/bio.h> |
9 #include <openssl/bytestring.h> | 9 #include <openssl/bytestring.h> |
10 #include <openssl/err.h> | 10 #include <openssl/err.h> |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
63 #endif | 63 #endif |
64 | 64 |
65 namespace net { | 65 namespace net { |
66 | 66 |
67 namespace { | 67 namespace { |
68 | 68 |
69 // This constant can be any non-negative/non-zero value (eg: it does not | 69 // This constant can be any non-negative/non-zero value (eg: it does not |
70 // overlap with any value of the net::Error range, including net::OK). | 70 // overlap with any value of the net::Error range, including net::OK). |
71 const int kNoPendingResult = 1; | 71 const int kNoPendingResult = 1; |
72 | 72 |
73 // If a client doesn't have a list of protocols that it supports, but | |
74 // the server supports NPN, choosing "http/1.1" is the best answer. | |
75 const char kDefaultSupportedNPNProtocol[] = "http/1.1"; | |
76 | |
77 // Default size of the internal BoringSSL buffers. | 73 // Default size of the internal BoringSSL buffers. |
78 const int kDefaultOpenSSLBufferSize = 17 * 1024; | 74 const int kDefaultOpenSSLBufferSize = 17 * 1024; |
79 | 75 |
80 // TLS extension number use for Token Binding. | 76 // TLS extension number use for Token Binding. |
81 const unsigned int kTbExtNum = 24; | 77 const unsigned int kTbExtNum = 24; |
82 | 78 |
83 // Token Binding ProtocolVersions supported. | 79 // Token Binding ProtocolVersions supported. |
84 const uint8_t kTbProtocolVersionMajor = 0; | 80 const uint8_t kTbProtocolVersionMajor = 0; |
85 const uint8_t kTbProtocolVersionMinor = 8; | 81 const uint8_t kTbProtocolVersionMinor = 8; |
86 const uint8_t kTbMinProtocolVersionMajor = 0; | 82 const uint8_t kTbMinProtocolVersionMajor = 0; |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
241 ssl_socket_data_index_ = SSL_get_ex_new_index(0, 0, 0, 0, 0); | 237 ssl_socket_data_index_ = SSL_get_ex_new_index(0, 0, 0, 0, 0); |
242 DCHECK_NE(ssl_socket_data_index_, -1); | 238 DCHECK_NE(ssl_socket_data_index_, -1); |
243 ssl_ctx_.reset(SSL_CTX_new(SSLv23_client_method())); | 239 ssl_ctx_.reset(SSL_CTX_new(SSLv23_client_method())); |
244 SSL_CTX_set_cert_verify_callback(ssl_ctx_.get(), CertVerifyCallback, NULL); | 240 SSL_CTX_set_cert_verify_callback(ssl_ctx_.get(), CertVerifyCallback, NULL); |
245 SSL_CTX_set_cert_cb(ssl_ctx_.get(), ClientCertRequestCallback, NULL); | 241 SSL_CTX_set_cert_cb(ssl_ctx_.get(), ClientCertRequestCallback, NULL); |
246 SSL_CTX_set_verify(ssl_ctx_.get(), SSL_VERIFY_PEER, NULL); | 242 SSL_CTX_set_verify(ssl_ctx_.get(), SSL_VERIFY_PEER, NULL); |
247 // This stops |SSL_shutdown| from generating the close_notify message, which | 243 // This stops |SSL_shutdown| from generating the close_notify message, which |
248 // is currently not sent on the network. | 244 // is currently not sent on the network. |
249 // TODO(haavardm): Remove setting quiet shutdown once 118366 is fixed. | 245 // TODO(haavardm): Remove setting quiet shutdown once 118366 is fixed. |
250 SSL_CTX_set_quiet_shutdown(ssl_ctx_.get(), 1); | 246 SSL_CTX_set_quiet_shutdown(ssl_ctx_.get(), 1); |
251 // Note that SSL_OP_DISABLE_NPN is used to disable NPN if | |
252 // ssl_config_.next_proto is empty. | |
253 SSL_CTX_set_next_proto_select_cb(ssl_ctx_.get(), SelectNextProtoCallback, | |
254 NULL); | |
255 | 247 |
256 // Disable the internal session cache. Session caching is handled | 248 // Disable the internal session cache. Session caching is handled |
257 // externally (i.e. by SSLClientSessionCache). | 249 // externally (i.e. by SSLClientSessionCache). |
258 SSL_CTX_set_session_cache_mode( | 250 SSL_CTX_set_session_cache_mode( |
259 ssl_ctx_.get(), SSL_SESS_CACHE_CLIENT | SSL_SESS_CACHE_NO_INTERNAL); | 251 ssl_ctx_.get(), SSL_SESS_CACHE_CLIENT | SSL_SESS_CACHE_NO_INTERNAL); |
260 SSL_CTX_sess_set_new_cb(ssl_ctx_.get(), NewSessionCallback); | 252 SSL_CTX_sess_set_new_cb(ssl_ctx_.get(), NewSessionCallback); |
261 | 253 |
262 if (!SSL_CTX_add_client_custom_ext(ssl_ctx_.get(), kTbExtNum, | 254 if (!SSL_CTX_add_client_custom_ext(ssl_ctx_.get(), kTbExtNum, |
263 &TokenBindingAddCallback, | 255 &TokenBindingAddCallback, |
264 &TokenBindingFreeCallback, nullptr, | 256 &TokenBindingFreeCallback, nullptr, |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
309 | 301 |
310 static int CertVerifyCallback(X509_STORE_CTX* store_ctx, void* arg) { | 302 static int CertVerifyCallback(X509_STORE_CTX* store_ctx, void* arg) { |
311 SSL* ssl = reinterpret_cast<SSL*>(X509_STORE_CTX_get_ex_data( | 303 SSL* ssl = reinterpret_cast<SSL*>(X509_STORE_CTX_get_ex_data( |
312 store_ctx, SSL_get_ex_data_X509_STORE_CTX_idx())); | 304 store_ctx, SSL_get_ex_data_X509_STORE_CTX_idx())); |
313 SSLClientSocketImpl* socket = GetInstance()->GetClientSocketFromSSL(ssl); | 305 SSLClientSocketImpl* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
314 CHECK(socket); | 306 CHECK(socket); |
315 | 307 |
316 return socket->CertVerifyCallback(store_ctx); | 308 return socket->CertVerifyCallback(store_ctx); |
317 } | 309 } |
318 | 310 |
319 static int SelectNextProtoCallback(SSL* ssl, | |
320 unsigned char** out, | |
321 unsigned char* outlen, | |
322 const unsigned char* in, | |
323 unsigned int inlen, | |
324 void* arg) { | |
325 SSLClientSocketImpl* socket = GetInstance()->GetClientSocketFromSSL(ssl); | |
326 return socket->SelectNextProtoCallback(out, outlen, in, inlen); | |
327 } | |
328 | |
329 static int NewSessionCallback(SSL* ssl, SSL_SESSION* session) { | 311 static int NewSessionCallback(SSL* ssl, SSL_SESSION* session) { |
330 SSLClientSocketImpl* socket = GetInstance()->GetClientSocketFromSSL(ssl); | 312 SSLClientSocketImpl* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
331 return socket->NewSessionCallback(session); | 313 return socket->NewSessionCallback(session); |
332 } | 314 } |
333 | 315 |
334 static int PrivateKeyTypeCallback(SSL* ssl) { | 316 static int PrivateKeyTypeCallback(SSL* ssl) { |
335 SSLClientSocketImpl* socket = GetInstance()->GetClientSocketFromSSL(ssl); | 317 SSLClientSocketImpl* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
336 return socket->PrivateKeyTypeCallback(); | 318 return socket->PrivateKeyTypeCallback(); |
337 } | 319 } |
338 | 320 |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
507 tb_negotiated_param_(TB_PARAM_ECDSAP256), | 489 tb_negotiated_param_(TB_PARAM_ECDSAP256), |
508 tb_signed_ekm_map_(10), | 490 tb_signed_ekm_map_(10), |
509 ssl_(NULL), | 491 ssl_(NULL), |
510 transport_bio_(NULL), | 492 transport_bio_(NULL), |
511 transport_(std::move(transport_socket)), | 493 transport_(std::move(transport_socket)), |
512 host_and_port_(host_and_port), | 494 host_and_port_(host_and_port), |
513 ssl_config_(ssl_config), | 495 ssl_config_(ssl_config), |
514 ssl_session_cache_shard_(context.ssl_session_cache_shard), | 496 ssl_session_cache_shard_(context.ssl_session_cache_shard), |
515 next_handshake_state_(STATE_NONE), | 497 next_handshake_state_(STATE_NONE), |
516 disconnected_(false), | 498 disconnected_(false), |
517 npn_status_(kNextProtoUnsupported), | |
518 negotiated_protocol_(kProtoUnknown), | 499 negotiated_protocol_(kProtoUnknown), |
519 negotiation_extension_(kExtensionUnknown), | |
520 channel_id_sent_(false), | 500 channel_id_sent_(false), |
521 certificate_verified_(false), | 501 certificate_verified_(false), |
522 signature_result_(kNoPendingResult), | 502 signature_result_(kNoPendingResult), |
523 transport_security_state_(context.transport_security_state), | 503 transport_security_state_(context.transport_security_state), |
524 policy_enforcer_(context.ct_policy_enforcer), | 504 policy_enforcer_(context.ct_policy_enforcer), |
525 pkp_bypassed_(false), | 505 pkp_bypassed_(false), |
526 net_log_(transport_->socket()->NetLog()), | 506 net_log_(transport_->socket()->NetLog()), |
527 weak_factory_(this) { | 507 weak_factory_(this) { |
528 CHECK(cert_verifier_); | 508 CHECK(cert_verifier_); |
529 CHECK(transport_security_state_); | 509 CHECK(transport_security_state_); |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
690 transport_write_error_ = OK; | 670 transport_write_error_ = OK; |
691 | 671 |
692 server_cert_verify_result_.Reset(); | 672 server_cert_verify_result_.Reset(); |
693 completed_connect_ = false; | 673 completed_connect_ = false; |
694 | 674 |
695 cert_authorities_.clear(); | 675 cert_authorities_.clear(); |
696 cert_key_types_.clear(); | 676 cert_key_types_.clear(); |
697 | 677 |
698 start_cert_verification_time_ = base::TimeTicks(); | 678 start_cert_verification_time_ = base::TimeTicks(); |
699 | 679 |
700 npn_status_ = kNextProtoUnsupported; | |
701 negotiated_protocol_ = kProtoUnknown; | 680 negotiated_protocol_ = kProtoUnknown; |
702 | 681 |
703 channel_id_sent_ = false; | 682 channel_id_sent_ = false; |
704 tb_was_negotiated_ = false; | 683 tb_was_negotiated_ = false; |
705 pending_session_ = nullptr; | 684 pending_session_ = nullptr; |
706 certificate_verified_ = false; | 685 certificate_verified_ = false; |
707 channel_id_request_.Cancel(); | 686 channel_id_request_.Cancel(); |
708 | 687 |
709 signature_result_ = kNoPendingResult; | 688 signature_result_ = kNoPendingResult; |
710 signature_.clear(); | 689 signature_.clear(); |
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1058 SSL_enable_tls_channel_id(ssl_); | 1037 SSL_enable_tls_channel_id(ssl_); |
1059 } | 1038 } |
1060 | 1039 |
1061 if (!ssl_config_.alpn_protos.empty()) { | 1040 if (!ssl_config_.alpn_protos.empty()) { |
1062 std::vector<uint8_t> wire_protos = | 1041 std::vector<uint8_t> wire_protos = |
1063 SerializeNextProtos(ssl_config_.alpn_protos); | 1042 SerializeNextProtos(ssl_config_.alpn_protos); |
1064 SSL_set_alpn_protos(ssl_, wire_protos.empty() ? NULL : &wire_protos[0], | 1043 SSL_set_alpn_protos(ssl_, wire_protos.empty() ? NULL : &wire_protos[0], |
1065 wire_protos.size()); | 1044 wire_protos.size()); |
1066 } | 1045 } |
1067 | 1046 |
1068 if (ssl_config_.npn_protos.empty()) | 1047 SSL_set_options(ssl_, SSL_OP_DISABLE_NPN); |
davidben
2016/09/15 18:57:00
This line can be removed. (If the callback isn't c
Bence
2016/09/15 19:01:31
Done.
| |
1069 SSL_set_options(ssl_, SSL_OP_DISABLE_NPN); | |
1070 | 1048 |
1071 if (ssl_config_.signed_cert_timestamps_enabled) { | 1049 if (ssl_config_.signed_cert_timestamps_enabled) { |
1072 SSL_enable_signed_cert_timestamps(ssl_); | 1050 SSL_enable_signed_cert_timestamps(ssl_); |
1073 SSL_enable_ocsp_stapling(ssl_); | 1051 SSL_enable_ocsp_stapling(ssl_); |
1074 } | 1052 } |
1075 | 1053 |
1076 if (cert_verifier_->SupportsOCSPStapling()) | 1054 if (cert_verifier_->SupportsOCSPStapling()) |
1077 SSL_enable_ocsp_stapling(ssl_); | 1055 SSL_enable_ocsp_stapling(ssl_); |
1078 | 1056 |
1079 return OK; | 1057 return OK; |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1198 if (!ssl_config_.dhe_enabled && | 1176 if (!ssl_config_.dhe_enabled && |
1199 SSL_CIPHER_is_DHE(SSL_get_current_cipher(ssl_))) { | 1177 SSL_CIPHER_is_DHE(SSL_get_current_cipher(ssl_))) { |
1200 return ERR_SSL_OBSOLETE_CIPHER; | 1178 return ERR_SSL_OBSOLETE_CIPHER; |
1201 } | 1179 } |
1202 | 1180 |
1203 // Check that if token binding was negotiated, then extended master secret | 1181 // Check that if token binding was negotiated, then extended master secret |
1204 // must also be negotiated. | 1182 // must also be negotiated. |
1205 if (tb_was_negotiated_ && !SSL_get_extms_support(ssl_)) | 1183 if (tb_was_negotiated_ && !SSL_get_extms_support(ssl_)) |
1206 return ERR_SSL_PROTOCOL_ERROR; | 1184 return ERR_SSL_PROTOCOL_ERROR; |
1207 | 1185 |
1208 // SSL handshake is completed. If NPN wasn't negotiated, see if ALPN was. | 1186 const uint8_t* alpn_proto = NULL; |
1209 if (npn_status_ == kNextProtoUnsupported) { | 1187 unsigned alpn_len = 0; |
1210 const uint8_t* alpn_proto = NULL; | 1188 SSL_get0_alpn_selected(ssl_, &alpn_proto, &alpn_len); |
1211 unsigned alpn_len = 0; | 1189 if (alpn_len > 0) { |
1212 SSL_get0_alpn_selected(ssl_, &alpn_proto, &alpn_len); | 1190 base::StringPiece proto(reinterpret_cast<const char*>(alpn_proto), |
1213 if (alpn_len > 0) { | 1191 alpn_len); |
1214 base::StringPiece proto(reinterpret_cast<const char*>(alpn_proto), | 1192 negotiated_protocol_ = NextProtoFromString(proto); |
1215 alpn_len); | |
1216 negotiated_protocol_ = NextProtoFromString(proto); | |
1217 npn_status_ = kNextProtoNegotiated; | |
1218 negotiation_extension_ = kExtensionALPN; | |
1219 } | |
1220 } | 1193 } |
1221 | 1194 |
1222 RecordNegotiatedProtocol(); | 1195 RecordNegotiatedProtocol(); |
1223 RecordChannelIDSupport(); | 1196 RecordChannelIDSupport(); |
1224 | 1197 |
1225 const uint8_t* ocsp_response_raw; | 1198 const uint8_t* ocsp_response_raw; |
1226 size_t ocsp_response_len; | 1199 size_t ocsp_response_len; |
1227 SSL_get0_ocsp_response(ssl_, &ocsp_response_raw, &ocsp_response_len); | 1200 SSL_get0_ocsp_response(ssl_, &ocsp_response_raw, &ocsp_response_len); |
1228 std::string ocsp_response; | 1201 std::string ocsp_response; |
1229 if (ocsp_response_len > 0) { | 1202 if (ocsp_response_len > 0) { |
(...skipping 750 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1980 return 0; | 1953 return 0; |
1981 } | 1954 } |
1982 if (old_der != new_der) { | 1955 if (old_der != new_der) { |
1983 LOG(ERROR) << "Server certificate changed between handshakes"; | 1956 LOG(ERROR) << "Server certificate changed between handshakes"; |
1984 return 0; | 1957 return 0; |
1985 } | 1958 } |
1986 | 1959 |
1987 return 1; | 1960 return 1; |
1988 } | 1961 } |
1989 | 1962 |
1990 // SelectNextProtoCallback is called by OpenSSL during the handshake. If the | |
1991 // server supports NPN, selects a protocol from the list that the server | |
1992 // provides. According to third_party/boringssl/src/ssl/ssl_lib.c, the | |
1993 // callback can assume that |in| is syntactically valid. | |
1994 int SSLClientSocketImpl::SelectNextProtoCallback(unsigned char** out, | |
1995 unsigned char* outlen, | |
1996 const unsigned char* in, | |
1997 unsigned int inlen) { | |
1998 if (ssl_config_.npn_protos.empty()) { | |
1999 *out = reinterpret_cast<uint8_t*>( | |
2000 const_cast<char*>(kDefaultSupportedNPNProtocol)); | |
2001 *outlen = arraysize(kDefaultSupportedNPNProtocol) - 1; | |
2002 npn_status_ = kNextProtoUnsupported; | |
2003 return SSL_TLSEXT_ERR_OK; | |
2004 } | |
2005 | |
2006 // Assume there's no overlap between our protocols and the server's list. | |
2007 npn_status_ = kNextProtoNoOverlap; | |
2008 | |
2009 // For each protocol in server preference order, see if we support it. | |
2010 for (unsigned int i = 0; i < inlen; i += in[i] + 1) { | |
2011 for (NextProto next_proto : ssl_config_.npn_protos) { | |
2012 const std::string proto = NextProtoToString(next_proto); | |
2013 if (in[i] == proto.size() && | |
2014 memcmp(&in[i + 1], proto.data(), in[i]) == 0) { | |
2015 // We found a match. | |
2016 negotiated_protocol_ = next_proto; | |
2017 *out = const_cast<unsigned char*>(in) + i + 1; | |
2018 *outlen = in[i]; | |
2019 npn_status_ = kNextProtoNegotiated; | |
2020 break; | |
2021 } | |
2022 } | |
2023 if (npn_status_ == kNextProtoNegotiated) | |
2024 break; | |
2025 } | |
2026 | |
2027 // If we didn't find a protocol, we select the last one from our list. | |
2028 if (npn_status_ == kNextProtoNoOverlap) { | |
2029 negotiated_protocol_ = ssl_config_.npn_protos.back(); | |
2030 // NextProtoToString returns a pointer to a static string. | |
2031 const char* proto = NextProtoToString(negotiated_protocol_); | |
2032 *out = reinterpret_cast<unsigned char*>(const_cast<char*>(proto)); | |
2033 *outlen = strlen(proto); | |
2034 } | |
2035 | |
2036 negotiation_extension_ = kExtensionNPN; | |
2037 return SSL_TLSEXT_ERR_OK; | |
2038 } | |
2039 | |
2040 long SSLClientSocketImpl::MaybeReplayTransportError(BIO* bio, | 1963 long SSLClientSocketImpl::MaybeReplayTransportError(BIO* bio, |
2041 int cmd, | 1964 int cmd, |
2042 const char* argp, | 1965 const char* argp, |
2043 int argi, | 1966 int argi, |
2044 long argl, | 1967 long argl, |
2045 long retvalue) { | 1968 long retvalue) { |
2046 if (cmd == (BIO_CB_READ | BIO_CB_RETURN) && retvalue <= 0) { | 1969 if (cmd == (BIO_CB_READ | BIO_CB_RETURN) && retvalue <= 0) { |
2047 // If there is no more data in the buffer, report any pending errors that | 1970 // If there is no more data in the buffer, report any pending errors that |
2048 // were observed. Note that both the readbuf and the writebuf are checked | 1971 // were observed. Note that both the readbuf and the writebuf are checked |
2049 // for errors, since the application may have encountered a socket error | 1972 // for errors, since the application may have encountered a socket error |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2140 if (ssl_config_.channel_id_enabled) | 2063 if (ssl_config_.channel_id_enabled) |
2141 result.append("channelid"); | 2064 result.append("channelid"); |
2142 | 2065 |
2143 return result; | 2066 return result; |
2144 } | 2067 } |
2145 | 2068 |
2146 bool SSLClientSocketImpl::IsRenegotiationAllowed() const { | 2069 bool SSLClientSocketImpl::IsRenegotiationAllowed() const { |
2147 if (tb_was_negotiated_) | 2070 if (tb_was_negotiated_) |
2148 return false; | 2071 return false; |
2149 | 2072 |
2150 if (npn_status_ == kNextProtoUnsupported) | 2073 if (negotiated_protocol_ == kProtoUnknown) |
2151 return ssl_config_.renego_allowed_default; | 2074 return ssl_config_.renego_allowed_default; |
2152 | 2075 |
2153 for (NextProto allowed : ssl_config_.renego_allowed_for_protos) { | 2076 for (NextProto allowed : ssl_config_.renego_allowed_for_protos) { |
2154 if (negotiated_protocol_ == allowed) | 2077 if (negotiated_protocol_ == allowed) |
2155 return true; | 2078 return true; |
2156 } | 2079 } |
2157 return false; | 2080 return false; |
2158 } | 2081 } |
2159 | 2082 |
2160 int SSLClientSocketImpl::PrivateKeyTypeCallback() { | 2083 int SSLClientSocketImpl::PrivateKeyTypeCallback() { |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2364 } | 2287 } |
2365 UMA_HISTOGRAM_ENUMERATION("DomainBoundCerts.Support", supported, | 2288 UMA_HISTOGRAM_ENUMERATION("DomainBoundCerts.Support", supported, |
2366 CHANNEL_ID_USAGE_MAX); | 2289 CHANNEL_ID_USAGE_MAX); |
2367 } | 2290 } |
2368 | 2291 |
2369 bool SSLClientSocketImpl::IsChannelIDEnabled() const { | 2292 bool SSLClientSocketImpl::IsChannelIDEnabled() const { |
2370 return ssl_config_.channel_id_enabled && channel_id_service_; | 2293 return ssl_config_.channel_id_enabled && channel_id_service_; |
2371 } | 2294 } |
2372 | 2295 |
2373 } // namespace net | 2296 } // namespace net |
OLD | NEW |