OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "net/socket/ssl_client_socket_win.h" | |
6 | |
7 #include <schnlsp.h> | |
8 | |
9 #include <algorithm> | |
10 #include <map> | |
11 | |
12 #include "base/bind.h" | |
13 #include "base/compiler_specific.h" | |
14 #include "base/lazy_instance.h" | |
15 #include "base/stl_util.h" | |
16 #include "base/string_util.h" | |
17 #include "base/synchronization/lock.h" | |
18 #include "base/utf_string_conversions.h" | |
19 #include "net/base/cert_verifier.h" | |
20 #include "net/base/connection_type_histograms.h" | |
21 #include "net/base/host_port_pair.h" | |
22 #include "net/base/io_buffer.h" | |
23 #include "net/base/net_log.h" | |
24 #include "net/base/net_errors.h" | |
25 #include "net/base/single_request_cert_verifier.h" | |
26 #include "net/base/ssl_cert_request_info.h" | |
27 #include "net/base/ssl_connection_status_flags.h" | |
28 #include "net/base/ssl_info.h" | |
29 #include "net/base/x509_certificate_net_log_param.h" | |
30 #include "net/base/x509_util.h" | |
31 #include "net/socket/client_socket_handle.h" | |
32 | |
33 #pragma comment(lib, "secur32.lib") | |
34 | |
35 namespace net { | |
36 | |
37 //----------------------------------------------------------------------------- | |
38 | |
39 // TODO(wtc): See http://msdn.microsoft.com/en-us/library/aa377188(VS.85).aspx | |
40 // for the other error codes we may need to map. | |
41 static int MapSecurityError(SECURITY_STATUS err) { | |
42 // There are numerous security error codes, but these are the ones we thus | |
43 // far find interesting. | |
44 switch (err) { | |
45 case SEC_E_WRONG_PRINCIPAL: // Schannel - server certificate error. | |
46 case CERT_E_CN_NO_MATCH: // CryptoAPI | |
47 return ERR_CERT_COMMON_NAME_INVALID; | |
48 case SEC_E_UNTRUSTED_ROOT: // Schannel - server certificate error or | |
49 // unknown_ca alert. | |
50 case CERT_E_UNTRUSTEDROOT: // CryptoAPI | |
51 return ERR_CERT_AUTHORITY_INVALID; | |
52 case SEC_E_CERT_EXPIRED: // Schannel - server certificate error or | |
53 // certificate_expired alert. | |
54 case CERT_E_EXPIRED: // CryptoAPI | |
55 return ERR_CERT_DATE_INVALID; | |
56 case CRYPT_E_NO_REVOCATION_CHECK: | |
57 return ERR_CERT_NO_REVOCATION_MECHANISM; | |
58 case CRYPT_E_REVOCATION_OFFLINE: | |
59 return ERR_CERT_UNABLE_TO_CHECK_REVOCATION; | |
60 case CRYPT_E_REVOKED: // CryptoAPI and Schannel server certificate error, | |
61 // or certificate_revoked alert. | |
62 return ERR_CERT_REVOKED; | |
63 | |
64 // We received one of the following alert messages from the server: | |
65 // bad_certificate | |
66 // unsupported_certificate | |
67 // certificate_unknown | |
68 case SEC_E_CERT_UNKNOWN: | |
69 case CERT_E_ROLE: | |
70 return ERR_CERT_INVALID; | |
71 | |
72 // We received one of the following alert messages from the server: | |
73 // decode_error | |
74 // export_restriction | |
75 // handshake_failure | |
76 // illegal_parameter | |
77 // record_overflow | |
78 // unexpected_message | |
79 // and all other TLS alerts not explicitly specified elsewhere. | |
80 case SEC_E_ILLEGAL_MESSAGE: | |
81 // We received one of the following alert messages from the server: | |
82 // decrypt_error | |
83 // decryption_failed | |
84 case SEC_E_DECRYPT_FAILURE: | |
85 // We received one of the following alert messages from the server: | |
86 // bad_record_mac | |
87 // decompression_failure | |
88 case SEC_E_MESSAGE_ALTERED: | |
89 // TODO(rsleevi): Add SEC_E_INTERNAL_ERROR, which corresponds to an | |
90 // internal_error alert message being received. However, it is also used | |
91 // by Schannel for authentication errors that happen locally, so it has | |
92 // been omitted to prevent masking them as protocol errors. | |
93 return ERR_SSL_PROTOCOL_ERROR; | |
94 | |
95 case SEC_E_LOGON_DENIED: // Received a access_denied alert. | |
96 return ERR_BAD_SSL_CLIENT_AUTH_CERT; | |
97 | |
98 case SEC_E_UNSUPPORTED_FUNCTION: // Received a protocol_version alert. | |
99 case SEC_E_ALGORITHM_MISMATCH: // Received an insufficient_security alert. | |
100 return ERR_SSL_VERSION_OR_CIPHER_MISMATCH; | |
101 | |
102 case SEC_E_NO_CREDENTIALS: | |
103 return ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY; | |
104 case SEC_E_INVALID_HANDLE: | |
105 case SEC_E_INVALID_TOKEN: | |
106 LOG(ERROR) << "Unexpected error " << err; | |
107 return ERR_UNEXPECTED; | |
108 case SEC_E_OK: | |
109 return OK; | |
110 default: | |
111 LOG(WARNING) << "Unknown error " << err << " mapped to net::ERR_FAILED"; | |
112 return ERR_FAILED; | |
113 } | |
114 } | |
115 | |
116 //----------------------------------------------------------------------------- | |
117 | |
118 // A bitmask consisting of these bit flags encodes which versions of the SSL | |
119 // protocol (SSL 3.0 and TLS 1.0) are enabled. | |
120 // TODO(wtc): support TLS 1.1 and TLS 1.2 on Windows Vista and later. | |
121 enum { | |
122 SSL3 = 1 << 0, | |
123 TLS1 = 1 << 1, | |
124 SSL_VERSION_MASKS = 1 << 2 // The number of SSL version bitmasks. | |
125 }; | |
126 | |
127 // CredHandleClass simply gives a default constructor and a destructor to | |
128 // SSPI's CredHandle type (a C struct). | |
129 class CredHandleClass : public CredHandle { | |
130 public: | |
131 CredHandleClass() { | |
132 SecInvalidateHandle(this); | |
133 } | |
134 | |
135 ~CredHandleClass() { | |
136 if (SecIsValidHandle(this)) { | |
137 SECURITY_STATUS status = FreeCredentialsHandle(this); | |
138 DCHECK(status == SEC_E_OK); | |
139 } | |
140 } | |
141 }; | |
142 | |
143 // A table of CredHandles. | |
144 class CredHandleTable { | |
145 public: | |
146 CredHandleTable() {} | |
147 | |
148 ~CredHandleTable() { | |
149 STLDeleteContainerPairSecondPointers(client_cert_creds_.begin(), | |
150 client_cert_creds_.end()); | |
151 } | |
152 | |
153 int GetHandle(PCCERT_CONTEXT client_cert, | |
154 int ssl_version_mask, | |
155 CredHandle** handle_ptr) { | |
156 DCHECK(0 < ssl_version_mask && | |
157 ssl_version_mask < arraysize(anonymous_creds_)); | |
158 CredHandleClass* handle; | |
159 base::AutoLock lock(lock_); | |
160 if (client_cert) { | |
161 CredHandleMapKey key = std::make_pair(client_cert, ssl_version_mask); | |
162 CredHandleMap::const_iterator it = client_cert_creds_.find(key); | |
163 if (it == client_cert_creds_.end()) { | |
164 handle = new CredHandleClass; | |
165 client_cert_creds_[key] = handle; | |
166 } else { | |
167 handle = it->second; | |
168 } | |
169 } else { | |
170 handle = &anonymous_creds_[ssl_version_mask]; | |
171 } | |
172 if (!SecIsValidHandle(handle)) { | |
173 int result = InitializeHandle(handle, client_cert, ssl_version_mask); | |
174 if (result != OK) | |
175 return result; | |
176 } | |
177 *handle_ptr = handle; | |
178 return OK; | |
179 } | |
180 | |
181 private: | |
182 // CredHandleMapKey is a std::pair consisting of these two components: | |
183 // PCCERT_CONTEXT client_cert | |
184 // int ssl_version_mask | |
185 typedef std::pair<PCCERT_CONTEXT, int> CredHandleMapKey; | |
186 | |
187 typedef std::map<CredHandleMapKey, CredHandleClass*> CredHandleMap; | |
188 | |
189 // Returns OK on success or a network error code on failure. | |
190 static int InitializeHandle(CredHandle* handle, | |
191 PCCERT_CONTEXT client_cert, | |
192 int ssl_version_mask); | |
193 | |
194 base::Lock lock_; | |
195 | |
196 // Anonymous (no client certificate) CredHandles for all possible | |
197 // combinations of SSL versions. Defined as an array for fast lookup. | |
198 CredHandleClass anonymous_creds_[SSL_VERSION_MASKS]; | |
199 | |
200 // CredHandles that use a client certificate. | |
201 CredHandleMap client_cert_creds_; | |
202 }; | |
203 | |
204 static base::LazyInstance<CredHandleTable> g_cred_handle_table = | |
205 LAZY_INSTANCE_INITIALIZER; | |
206 | |
207 // static | |
208 int CredHandleTable::InitializeHandle(CredHandle* handle, | |
209 PCCERT_CONTEXT client_cert, | |
210 int ssl_version_mask) { | |
211 SCHANNEL_CRED schannel_cred = {0}; | |
212 schannel_cred.dwVersion = SCHANNEL_CRED_VERSION; | |
213 if (client_cert) { | |
214 schannel_cred.cCreds = 1; | |
215 schannel_cred.paCred = &client_cert; | |
216 // Schannel will make its own copy of client_cert. | |
217 } | |
218 | |
219 // The global system registry settings take precedence over the value of | |
220 // schannel_cred.grbitEnabledProtocols. | |
221 schannel_cred.grbitEnabledProtocols = 0; | |
222 if (ssl_version_mask & SSL3) | |
223 schannel_cred.grbitEnabledProtocols |= SP_PROT_SSL3; | |
224 if (ssl_version_mask & TLS1) | |
225 schannel_cred.grbitEnabledProtocols |= SP_PROT_TLS1; | |
226 | |
227 // The default session lifetime is 36000000 milliseconds (ten hours). Set | |
228 // schannel_cred.dwSessionLifespan to change the number of milliseconds that | |
229 // Schannel keeps the session in its session cache. | |
230 | |
231 // We can set the key exchange algorithms (RSA or DH) in | |
232 // schannel_cred.{cSupportedAlgs,palgSupportedAlgs}. | |
233 | |
234 // Although SCH_CRED_AUTO_CRED_VALIDATION is convenient, we have to use | |
235 // SCH_CRED_MANUAL_CRED_VALIDATION for three reasons. | |
236 // 1. SCH_CRED_AUTO_CRED_VALIDATION doesn't allow us to get the certificate | |
237 // context if the certificate validation fails. | |
238 // 2. SCH_CRED_AUTO_CRED_VALIDATION returns only one error even if the | |
239 // certificate has multiple errors. | |
240 // 3. SCH_CRED_AUTO_CRED_VALIDATION doesn't allow us to ignore untrusted CA | |
241 // and expired certificate errors. There are only flags to ignore the | |
242 // name mismatch and unable-to-check-revocation errors. | |
243 // | |
244 // We specify SCH_CRED_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT to cause the TLS | |
245 // certificate status request extension (commonly known as OCSP stapling) | |
246 // to be sent on Vista or later. This flag matches the | |
247 // CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT flag that we pass to the | |
248 // CertGetCertificateChain calls. Note: we specify this flag even when | |
249 // revocation checking is disabled to avoid doubling the number of | |
250 // credentials handles we need to acquire. | |
251 // | |
252 // TODO(wtc): Look into undocumented or poorly documented flags: | |
253 // SCH_CRED_RESTRICTED_ROOTS | |
254 // SCH_CRED_REVOCATION_CHECK_CACHE_ONLY | |
255 // SCH_CRED_CACHE_ONLY_URL_RETRIEVAL | |
256 // SCH_CRED_MEMORY_STORE_CERT | |
257 schannel_cred.dwFlags |= SCH_CRED_NO_DEFAULT_CREDS | | |
258 SCH_CRED_MANUAL_CRED_VALIDATION | | |
259 SCH_CRED_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT; | |
260 TimeStamp expiry; | |
261 SECURITY_STATUS status; | |
262 | |
263 status = AcquireCredentialsHandle( | |
264 NULL, // Not used | |
265 UNISP_NAME, // Microsoft Unified Security Protocol Provider | |
266 SECPKG_CRED_OUTBOUND, | |
267 NULL, // Not used | |
268 &schannel_cred, | |
269 NULL, // Not used | |
270 NULL, // Not used | |
271 handle, | |
272 &expiry); // Optional | |
273 if (status != SEC_E_OK) | |
274 LOG(ERROR) << "AcquireCredentialsHandle failed: " << status; | |
275 return MapSecurityError(status); | |
276 } | |
277 | |
278 // For the SSL sockets to share SSL sessions by session resumption handshakes, | |
279 // they need to use the same CredHandle. The GetCredHandle function creates | |
280 // and stores a shared CredHandle in *handle_ptr. On success, GetCredHandle | |
281 // returns OK. On failure, GetCredHandle returns a network error code and | |
282 // leaves *handle_ptr unchanged. | |
283 // | |
284 // The versions of the SSL protocol enabled are a property of the CredHandle. | |
285 // So we need a separate CredHandle for each combination of SSL versions. | |
286 // Most of the time Chromium will use only one or two combinations of SSL | |
287 // versions (for example, SSL3 | TLS1 for normal use, plus SSL3 when visiting | |
288 // TLS-intolerant servers). These CredHandles are initialized only when | |
289 // needed. | |
290 | |
291 static int GetCredHandle(PCCERT_CONTEXT client_cert, | |
292 int ssl_version_mask, | |
293 CredHandle** handle_ptr) { | |
294 if (ssl_version_mask <= 0 || ssl_version_mask >= SSL_VERSION_MASKS) { | |
295 NOTREACHED(); | |
296 return ERR_UNEXPECTED; | |
297 } | |
298 return g_cred_handle_table.Get().GetHandle(client_cert, | |
299 ssl_version_mask, | |
300 handle_ptr); | |
301 } | |
302 | |
303 //----------------------------------------------------------------------------- | |
304 | |
305 // This callback is intended to be used with CertFindChainInStore. In addition | |
306 // to filtering by extended/enhanced key usage, we do not show expired | |
307 // certificates and require digital signature usage in the key usage | |
308 // extension. | |
309 // | |
310 // This matches our behavior on Mac OS X and that of NSS. It also matches the | |
311 // default behavior of IE8. See http://support.microsoft.com/kb/890326 and | |
312 // http://blogs.msdn.com/b/askie/archive/2009/06/09/my-expired-client-certificat
es-no-longer-display-when-connecting-to-my-web-server-using-ie8.aspx | |
313 static BOOL WINAPI ClientCertFindCallback(PCCERT_CONTEXT cert_context, | |
314 void* find_arg) { | |
315 // Verify the certificate's KU is good. | |
316 BYTE key_usage; | |
317 if (CertGetIntendedKeyUsage(X509_ASN_ENCODING, cert_context->pCertInfo, | |
318 &key_usage, 1)) { | |
319 if (!(key_usage & CERT_DIGITAL_SIGNATURE_KEY_USAGE)) | |
320 return FALSE; | |
321 } else { | |
322 DWORD err = GetLastError(); | |
323 // If |err| is non-zero, it's an actual error. Otherwise the extension | |
324 // just isn't present, and we treat it as if everything was allowed. | |
325 if (err) { | |
326 DLOG(ERROR) << "CertGetIntendedKeyUsage failed: " << err; | |
327 return FALSE; | |
328 } | |
329 } | |
330 | |
331 // Verify the current time is within the certificate's validity period. | |
332 if (CertVerifyTimeValidity(NULL, cert_context->pCertInfo) != 0) | |
333 return FALSE; | |
334 | |
335 // Verify private key metadata is associated with this certificate. | |
336 DWORD size = 0; | |
337 if (!CertGetCertificateContextProperty( | |
338 cert_context, CERT_KEY_PROV_INFO_PROP_ID, NULL, &size)) { | |
339 return FALSE; | |
340 } | |
341 | |
342 return TRUE; | |
343 } | |
344 | |
345 static bool IsCertificateChainIdentical(const X509Certificate* a, | |
346 const X509Certificate* b) { | |
347 DCHECK(a); | |
348 DCHECK(b); | |
349 const X509Certificate::OSCertHandles& a_intermediates = | |
350 a->GetIntermediateCertificates(); | |
351 const X509Certificate::OSCertHandles& b_intermediates = | |
352 b->GetIntermediateCertificates(); | |
353 if (a_intermediates.size() != b_intermediates.size() || !a->Equals(b)) | |
354 return false; | |
355 for (size_t i = 0; i < a_intermediates.size(); ++i) { | |
356 if (!X509Certificate::IsSameOSCert(a_intermediates[i], b_intermediates[i])) | |
357 return false; | |
358 } | |
359 return true; | |
360 } | |
361 | |
362 //----------------------------------------------------------------------------- | |
363 | |
364 // Size of recv_buffer_ | |
365 // | |
366 // Ciphertext is decrypted one SSL record at a time, so recv_buffer_ needs to | |
367 // have room for a full SSL record, with the header and trailer. Here is the | |
368 // breakdown of the size: | |
369 // 5: SSL record header | |
370 // 16K: SSL record maximum size | |
371 // 64: >= SSL record trailer (16 or 20 have been observed) | |
372 static const int kRecvBufferSize = (5 + 16*1024 + 64); | |
373 | |
374 SSLClientSocketWin::SSLClientSocketWin(ClientSocketHandle* transport_socket, | |
375 const HostPortPair& host_and_port, | |
376 const SSLConfig& ssl_config, | |
377 const SSLClientSocketContext& context) | |
378 : transport_(transport_socket), | |
379 host_and_port_(host_and_port), | |
380 ssl_config_(ssl_config), | |
381 user_read_buf_len_(0), | |
382 user_write_buf_len_(0), | |
383 next_state_(STATE_NONE), | |
384 cert_verifier_(context.cert_verifier), | |
385 creds_(NULL), | |
386 isc_status_(SEC_E_OK), | |
387 payload_send_buffer_len_(0), | |
388 bytes_sent_(0), | |
389 decrypted_ptr_(NULL), | |
390 bytes_decrypted_(0), | |
391 received_ptr_(NULL), | |
392 bytes_received_(0), | |
393 writing_first_token_(false), | |
394 ignore_ok_result_(false), | |
395 renegotiating_(false), | |
396 need_more_data_(false), | |
397 net_log_(transport_socket->socket()->NetLog()) { | |
398 memset(&stream_sizes_, 0, sizeof(stream_sizes_)); | |
399 memset(in_buffers_, 0, sizeof(in_buffers_)); | |
400 memset(&send_buffer_, 0, sizeof(send_buffer_)); | |
401 memset(&ctxt_, 0, sizeof(ctxt_)); | |
402 } | |
403 | |
404 SSLClientSocketWin::~SSLClientSocketWin() { | |
405 Disconnect(); | |
406 } | |
407 | |
408 bool SSLClientSocketWin::GetSSLInfo(SSLInfo* ssl_info) { | |
409 ssl_info->Reset(); | |
410 if (!server_cert_) | |
411 return false; | |
412 | |
413 ssl_info->cert = server_cert_verify_result_.verified_cert; | |
414 ssl_info->cert_status = server_cert_verify_result_.cert_status; | |
415 ssl_info->public_key_hashes = server_cert_verify_result_.public_key_hashes; | |
416 ssl_info->is_issued_by_known_root = | |
417 server_cert_verify_result_.is_issued_by_known_root; | |
418 ssl_info->client_cert_sent = | |
419 ssl_config_.send_client_cert && ssl_config_.client_cert; | |
420 ssl_info->channel_id_sent = WasChannelIDSent(); | |
421 SecPkgContext_ConnectionInfo connection_info; | |
422 SECURITY_STATUS status = QueryContextAttributes( | |
423 &ctxt_, SECPKG_ATTR_CONNECTION_INFO, &connection_info); | |
424 if (status == SEC_E_OK) { | |
425 // TODO(wtc): compute the overall security strength, taking into account | |
426 // dwExchStrength and dwHashStrength. dwExchStrength needs to be | |
427 // normalized. | |
428 ssl_info->security_bits = connection_info.dwCipherStrength; | |
429 // TODO(wtc): connection_info.dwProtocol is the negotiated version. | |
430 // Save it in ssl_info->connection_status. | |
431 } | |
432 // SecPkgContext_CipherInfo comes from CNG and is available on Vista or | |
433 // later only. On XP, the next QueryContextAttributes call fails with | |
434 // SEC_E_UNSUPPORTED_FUNCTION (0x80090302), so ssl_info->connection_status | |
435 // won't contain the cipher suite. If this is a problem, we can build the | |
436 // cipher suite from the aiCipher, aiHash, and aiExch fields of | |
437 // SecPkgContext_ConnectionInfo based on Appendix C of RFC 5246. | |
438 SecPkgContext_CipherInfo cipher_info = { SECPKGCONTEXT_CIPHERINFO_V1 }; | |
439 status = QueryContextAttributes( | |
440 &ctxt_, SECPKG_ATTR_CIPHER_INFO, &cipher_info); | |
441 if (status == SEC_E_OK) { | |
442 // TODO(wtc): find out what the cipher_info.dwBaseCipherSuite field is. | |
443 ssl_info->connection_status |= | |
444 (cipher_info.dwCipherSuite & SSL_CONNECTION_CIPHERSUITE_MASK) << | |
445 SSL_CONNECTION_CIPHERSUITE_SHIFT; | |
446 // SChannel doesn't support TLS compression, so cipher_info doesn't have | |
447 // any field related to the compression method. | |
448 } | |
449 | |
450 if (ssl_config_.version_fallback) | |
451 ssl_info->connection_status |= SSL_CONNECTION_VERSION_FALLBACK; | |
452 | |
453 return true; | |
454 } | |
455 | |
456 void SSLClientSocketWin::GetSSLCertRequestInfo( | |
457 SSLCertRequestInfo* cert_request_info) { | |
458 cert_request_info->host_and_port = host_and_port_.ToString(); | |
459 cert_request_info->cert_authorities.clear(); | |
460 cert_request_info->cert_key_types.clear(); | |
461 cert_request_info->client_certs.clear(); | |
462 | |
463 // Get the server criteria for client certificates. Schannel doesn't return | |
464 // the certificate_types field of the CertificateRequest message to us, so we | |
465 // can't fill the |cert_key_types| field. | |
466 SecPkgContext_IssuerListInfoEx issuer_list; | |
467 SECURITY_STATUS status = QueryContextAttributes( | |
468 &ctxt_, SECPKG_ATTR_ISSUER_LIST_EX, &issuer_list); | |
469 if (status != SEC_E_OK) { | |
470 DLOG(ERROR) << "QueryContextAttributes (issuer list) failed: " << status; | |
471 return; | |
472 } | |
473 | |
474 for (size_t i = 0; i < issuer_list.cIssuers; i++) { | |
475 cert_request_info->cert_authorities.push_back(std::string( | |
476 reinterpret_cast<const char*>(issuer_list.aIssuers[i].pbData), | |
477 static_cast<size_t>(issuer_list.aIssuers[i].cbData))); | |
478 } | |
479 | |
480 // Retrieve the list of matching client certificates. This is to be moved out | |
481 // of here as a part of refactoring effort being tracked in | |
482 // http://crbug.com/166642. | |
483 | |
484 // Client certificates of the user are in the "MY" system certificate store. | |
485 HCERTSTORE my_cert_store = CertOpenSystemStore(NULL, L"MY"); | |
486 if (!my_cert_store) { | |
487 LOG(ERROR) << "Could not open the \"MY\" system certificate store: " | |
488 << GetLastError(); | |
489 FreeContextBuffer(issuer_list.aIssuers); | |
490 return; | |
491 } | |
492 | |
493 // Enumerate the client certificates. | |
494 CERT_CHAIN_FIND_BY_ISSUER_PARA find_by_issuer_para; | |
495 memset(&find_by_issuer_para, 0, sizeof(find_by_issuer_para)); | |
496 find_by_issuer_para.cbSize = sizeof(find_by_issuer_para); | |
497 find_by_issuer_para.pszUsageIdentifier = szOID_PKIX_KP_CLIENT_AUTH; | |
498 find_by_issuer_para.cIssuer = issuer_list.cIssuers; | |
499 find_by_issuer_para.rgIssuer = issuer_list.aIssuers; | |
500 find_by_issuer_para.pfnFindCallback = ClientCertFindCallback; | |
501 | |
502 PCCERT_CHAIN_CONTEXT chain_context = NULL; | |
503 DWORD find_flags = CERT_CHAIN_FIND_BY_ISSUER_CACHE_ONLY_FLAG | | |
504 CERT_CHAIN_FIND_BY_ISSUER_CACHE_ONLY_URL_FLAG; | |
505 | |
506 for (;;) { | |
507 // Find a certificate chain. | |
508 chain_context = CertFindChainInStore(my_cert_store, | |
509 X509_ASN_ENCODING, | |
510 find_flags, | |
511 CERT_CHAIN_FIND_BY_ISSUER, | |
512 &find_by_issuer_para, | |
513 chain_context); | |
514 if (!chain_context) { | |
515 DWORD err = GetLastError(); | |
516 if (err != CRYPT_E_NOT_FOUND) | |
517 DLOG(ERROR) << "CertFindChainInStore failed: " << err; | |
518 break; | |
519 } | |
520 | |
521 // Get the leaf certificate. | |
522 PCCERT_CONTEXT cert_context = | |
523 chain_context->rgpChain[0]->rgpElement[0]->pCertContext; | |
524 // Copy the certificate into a NULL store, so that the "MY" store can be | |
525 // closed before returning from this function. | |
526 PCCERT_CONTEXT cert_context2 = NULL; | |
527 BOOL ok = CertAddCertificateContextToStore(NULL, cert_context, | |
528 CERT_STORE_ADD_USE_EXISTING, | |
529 &cert_context2); | |
530 if (!ok) { | |
531 NOTREACHED(); | |
532 continue; | |
533 } | |
534 | |
535 // Grab the intermediates, if any. | |
536 X509Certificate::OSCertHandles intermediates; | |
537 for (DWORD i = 1; i < chain_context->rgpChain[0]->cElement; ++i) { | |
538 PCCERT_CONTEXT chain_intermediate = | |
539 chain_context->rgpChain[0]->rgpElement[i]->pCertContext; | |
540 PCCERT_CONTEXT copied_intermediate = NULL; | |
541 ok = CertAddCertificateContextToStore(NULL, chain_intermediate, | |
542 CERT_STORE_ADD_USE_EXISTING, | |
543 &copied_intermediate); | |
544 if (ok) | |
545 intermediates.push_back(copied_intermediate); | |
546 } | |
547 scoped_refptr<X509Certificate> cert = X509Certificate::CreateFromHandle( | |
548 cert_context2, intermediates); | |
549 cert_request_info->client_certs.push_back(cert); | |
550 CertFreeCertificateContext(cert_context2); | |
551 for (size_t i = 0; i < intermediates.size(); ++i) | |
552 CertFreeCertificateContext(intermediates[i]); | |
553 } | |
554 | |
555 std::sort(cert_request_info->client_certs.begin(), | |
556 cert_request_info->client_certs.end(), | |
557 x509_util::ClientCertSorter()); | |
558 | |
559 FreeContextBuffer(issuer_list.aIssuers); | |
560 | |
561 BOOL ok = CertCloseStore(my_cert_store, CERT_CLOSE_STORE_CHECK_FLAG); | |
562 DCHECK(ok); | |
563 } | |
564 | |
565 int SSLClientSocketWin::ExportKeyingMaterial(const base::StringPiece& label, | |
566 bool has_context, | |
567 const base::StringPiece& context, | |
568 unsigned char* out, | |
569 unsigned int outlen) { | |
570 return ERR_NOT_IMPLEMENTED; | |
571 } | |
572 | |
573 int SSLClientSocketWin::GetTLSUniqueChannelBinding(std::string* out) { | |
574 return ERR_NOT_IMPLEMENTED; | |
575 } | |
576 | |
577 SSLClientSocket::NextProtoStatus | |
578 SSLClientSocketWin::GetNextProto(std::string* proto, | |
579 std::string* server_protos) { | |
580 proto->clear(); | |
581 server_protos->clear(); | |
582 return kNextProtoUnsupported; | |
583 } | |
584 | |
585 ServerBoundCertService* SSLClientSocketWin::GetServerBoundCertService() const { | |
586 return NULL; | |
587 } | |
588 | |
589 int SSLClientSocketWin::Connect(const CompletionCallback& callback) { | |
590 DCHECK(transport_.get()); | |
591 DCHECK(next_state_ == STATE_NONE); | |
592 DCHECK(user_connect_callback_.is_null()); | |
593 | |
594 net_log_.BeginEvent(NetLog::TYPE_SSL_CONNECT); | |
595 | |
596 int rv = InitializeSSLContext(); | |
597 if (rv != OK) { | |
598 net_log_.EndEvent(NetLog::TYPE_SSL_CONNECT); | |
599 return rv; | |
600 } | |
601 | |
602 writing_first_token_ = true; | |
603 next_state_ = STATE_HANDSHAKE_WRITE; | |
604 rv = DoLoop(OK); | |
605 if (rv == ERR_IO_PENDING) { | |
606 user_connect_callback_ = callback; | |
607 } else { | |
608 net_log_.EndEvent(NetLog::TYPE_SSL_CONNECT); | |
609 } | |
610 return rv; | |
611 } | |
612 | |
613 int SSLClientSocketWin::InitializeSSLContext() { | |
614 // If ssl_config_.version_max > SSL_PROTOCOL_VERSION_TLS1, it means the | |
615 // SSLConfigService::SetDefaultVersionMax(SSL_PROTOCOL_VERSION_TLS1) call | |
616 // in ClientSocketFactory::UseSystemSSL() is not effective. | |
617 DCHECK_LE(ssl_config_.version_max, SSL_PROTOCOL_VERSION_TLS1); | |
618 int ssl_version_mask = 0; | |
619 if (ssl_config_.version_min == SSL_PROTOCOL_VERSION_SSL3) | |
620 ssl_version_mask |= SSL3; | |
621 if (ssl_config_.version_min <= SSL_PROTOCOL_VERSION_TLS1 && | |
622 ssl_config_.version_max >= SSL_PROTOCOL_VERSION_TLS1) { | |
623 ssl_version_mask |= TLS1; | |
624 } | |
625 // If we pass 0 to GetCredHandle, we will let Schannel select the protocols, | |
626 // rather than enabling no protocols. So we have to fail here. | |
627 if (ssl_version_mask == 0) | |
628 return ERR_NO_SSL_VERSIONS_ENABLED; | |
629 PCCERT_CONTEXT cert_context = NULL; | |
630 if (ssl_config_.client_cert) | |
631 cert_context = ssl_config_.client_cert->os_cert_handle(); | |
632 int result = GetCredHandle(cert_context, ssl_version_mask, &creds_); | |
633 if (result != OK) | |
634 return result; | |
635 | |
636 memset(&ctxt_, 0, sizeof(ctxt_)); | |
637 | |
638 SecBufferDesc buffer_desc; | |
639 DWORD out_flags; | |
640 DWORD flags = ISC_REQ_SEQUENCE_DETECT | | |
641 ISC_REQ_REPLAY_DETECT | | |
642 ISC_REQ_CONFIDENTIALITY | | |
643 ISC_RET_EXTENDED_ERROR | | |
644 ISC_REQ_ALLOCATE_MEMORY | | |
645 ISC_REQ_STREAM; | |
646 | |
647 send_buffer_.pvBuffer = NULL; | |
648 send_buffer_.BufferType = SECBUFFER_TOKEN; | |
649 send_buffer_.cbBuffer = 0; | |
650 | |
651 buffer_desc.cBuffers = 1; | |
652 buffer_desc.pBuffers = &send_buffer_; | |
653 buffer_desc.ulVersion = SECBUFFER_VERSION; | |
654 | |
655 TimeStamp expiry; | |
656 SECURITY_STATUS status; | |
657 | |
658 status = InitializeSecurityContext( | |
659 creds_, | |
660 NULL, // NULL on the first call | |
661 const_cast<wchar_t*>(ASCIIToWide(host_and_port_.host()).c_str()), | |
662 flags, | |
663 0, // Reserved | |
664 0, // Not used with Schannel. | |
665 NULL, // NULL on the first call | |
666 0, // Reserved | |
667 &ctxt_, // Receives the new context handle | |
668 &buffer_desc, | |
669 &out_flags, | |
670 &expiry); | |
671 if (status != SEC_I_CONTINUE_NEEDED) { | |
672 LOG(ERROR) << "InitializeSecurityContext failed: " << status; | |
673 if (status == SEC_E_INVALID_HANDLE) { | |
674 // The only handle we passed to this InitializeSecurityContext call is | |
675 // creds_, so print its contents to figure out why it's invalid. | |
676 if (creds_) { | |
677 LOG(ERROR) << "creds_->dwLower = " << creds_->dwLower | |
678 << "; creds_->dwUpper = " << creds_->dwUpper; | |
679 } else { | |
680 LOG(ERROR) << "creds_ is NULL"; | |
681 } | |
682 } | |
683 return MapSecurityError(status); | |
684 } | |
685 | |
686 return OK; | |
687 } | |
688 | |
689 | |
690 void SSLClientSocketWin::Disconnect() { | |
691 // TODO(wtc): Send SSL close_notify alert. | |
692 next_state_ = STATE_NONE; | |
693 | |
694 // Shut down anything that may call us back. | |
695 verifier_.reset(); | |
696 transport_->socket()->Disconnect(); | |
697 | |
698 if (send_buffer_.pvBuffer) | |
699 FreeSendBuffer(); | |
700 if (SecIsValidHandle(&ctxt_)) { | |
701 DeleteSecurityContext(&ctxt_); | |
702 SecInvalidateHandle(&ctxt_); | |
703 } | |
704 if (server_cert_) | |
705 server_cert_ = NULL; | |
706 | |
707 // TODO(wtc): reset more members? | |
708 bytes_decrypted_ = 0; | |
709 bytes_received_ = 0; | |
710 writing_first_token_ = false; | |
711 renegotiating_ = false; | |
712 need_more_data_ = false; | |
713 } | |
714 | |
715 bool SSLClientSocketWin::IsConnected() const { | |
716 // Ideally, we should also check if we have received the close_notify alert | |
717 // message from the server, and return false in that case. We're not doing | |
718 // that, so this function may return a false positive. Since the upper | |
719 // layer (HttpNetworkTransaction) needs to handle a persistent connection | |
720 // closed by the server when we send a request anyway, a false positive in | |
721 // exchange for simpler code is a good trade-off. | |
722 return completed_handshake() && transport_->socket()->IsConnected(); | |
723 } | |
724 | |
725 bool SSLClientSocketWin::IsConnectedAndIdle() const { | |
726 // Unlike IsConnected, this method doesn't return a false positive. | |
727 // | |
728 // Strictly speaking, we should check if we have received the close_notify | |
729 // alert message from the server, and return false in that case. Although | |
730 // the close_notify alert message means EOF in the SSL layer, it is just | |
731 // bytes to the transport layer below, so | |
732 // transport_->socket()->IsConnectedAndIdle() returns the desired false | |
733 // when we receive close_notify. | |
734 return completed_handshake() && transport_->socket()->IsConnectedAndIdle(); | |
735 } | |
736 | |
737 int SSLClientSocketWin::GetPeerAddress(IPEndPoint* address) const { | |
738 return transport_->socket()->GetPeerAddress(address); | |
739 } | |
740 | |
741 int SSLClientSocketWin::GetLocalAddress(IPEndPoint* address) const { | |
742 return transport_->socket()->GetLocalAddress(address); | |
743 } | |
744 | |
745 void SSLClientSocketWin::SetSubresourceSpeculation() { | |
746 if (transport_.get() && transport_->socket()) { | |
747 transport_->socket()->SetSubresourceSpeculation(); | |
748 } else { | |
749 NOTREACHED(); | |
750 } | |
751 } | |
752 | |
753 void SSLClientSocketWin::SetOmniboxSpeculation() { | |
754 if (transport_.get() && transport_->socket()) { | |
755 transport_->socket()->SetOmniboxSpeculation(); | |
756 } else { | |
757 NOTREACHED(); | |
758 } | |
759 } | |
760 | |
761 bool SSLClientSocketWin::WasEverUsed() const { | |
762 if (transport_.get() && transport_->socket()) { | |
763 return transport_->socket()->WasEverUsed(); | |
764 } | |
765 NOTREACHED(); | |
766 return false; | |
767 } | |
768 | |
769 bool SSLClientSocketWin::UsingTCPFastOpen() const { | |
770 if (transport_.get() && transport_->socket()) { | |
771 return transport_->socket()->UsingTCPFastOpen(); | |
772 } | |
773 NOTREACHED(); | |
774 return false; | |
775 } | |
776 | |
777 int64 SSLClientSocketWin::NumBytesRead() const { | |
778 if (transport_.get() && transport_->socket()) { | |
779 return transport_->socket()->NumBytesRead(); | |
780 } | |
781 NOTREACHED(); | |
782 return -1; | |
783 } | |
784 | |
785 base::TimeDelta SSLClientSocketWin::GetConnectTimeMicros() const { | |
786 if (transport_.get() && transport_->socket()) { | |
787 return transport_->socket()->GetConnectTimeMicros(); | |
788 } | |
789 NOTREACHED(); | |
790 return base::TimeDelta::FromMicroseconds(-1); | |
791 } | |
792 | |
793 int SSLClientSocketWin::Read(IOBuffer* buf, int buf_len, | |
794 const CompletionCallback& callback) { | |
795 DCHECK(completed_handshake()); | |
796 DCHECK(user_read_callback_.is_null()); | |
797 | |
798 // If we have surplus decrypted plaintext, satisfy the Read with it without | |
799 // reading more ciphertext from the transport socket. | |
800 if (bytes_decrypted_ != 0) { | |
801 int len = std::min(buf_len, bytes_decrypted_); | |
802 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, len, | |
803 decrypted_ptr_); | |
804 memcpy(buf->data(), decrypted_ptr_, len); | |
805 decrypted_ptr_ += len; | |
806 bytes_decrypted_ -= len; | |
807 if (bytes_decrypted_ == 0) { | |
808 decrypted_ptr_ = NULL; | |
809 if (bytes_received_ != 0) { | |
810 memmove(recv_buffer_.get(), received_ptr_, bytes_received_); | |
811 received_ptr_ = recv_buffer_.get(); | |
812 } | |
813 } | |
814 return len; | |
815 } | |
816 | |
817 DCHECK(!user_read_buf_); | |
818 // http://crbug.com/16371: We're seeing |buf->data()| return NULL. See if the | |
819 // user is passing in an IOBuffer with a NULL |data_|. | |
820 CHECK(buf); | |
821 CHECK(buf->data()); | |
822 user_read_buf_ = buf; | |
823 user_read_buf_len_ = buf_len; | |
824 | |
825 int rv = DoPayloadRead(); | |
826 if (rv == ERR_IO_PENDING) { | |
827 user_read_callback_ = callback; | |
828 } else { | |
829 user_read_buf_ = NULL; | |
830 user_read_buf_len_ = 0; | |
831 } | |
832 return rv; | |
833 } | |
834 | |
835 int SSLClientSocketWin::Write(IOBuffer* buf, int buf_len, | |
836 const CompletionCallback& callback) { | |
837 DCHECK(completed_handshake()); | |
838 DCHECK(user_write_callback_.is_null()); | |
839 | |
840 DCHECK(!user_write_buf_); | |
841 user_write_buf_ = buf; | |
842 user_write_buf_len_ = buf_len; | |
843 | |
844 int rv = DoPayloadEncrypt(); | |
845 if (rv != OK) | |
846 return rv; | |
847 | |
848 rv = DoPayloadWrite(); | |
849 if (rv == ERR_IO_PENDING) { | |
850 user_write_callback_ = callback; | |
851 } else { | |
852 user_write_buf_ = NULL; | |
853 user_write_buf_len_ = 0; | |
854 } | |
855 return rv; | |
856 } | |
857 | |
858 bool SSLClientSocketWin::SetReceiveBufferSize(int32 size) { | |
859 return transport_->socket()->SetReceiveBufferSize(size); | |
860 } | |
861 | |
862 bool SSLClientSocketWin::SetSendBufferSize(int32 size) { | |
863 return transport_->socket()->SetSendBufferSize(size); | |
864 } | |
865 | |
866 void SSLClientSocketWin::OnHandshakeIOComplete(int result) { | |
867 int rv = DoLoop(result); | |
868 | |
869 // The SSL handshake has some round trips. We need to notify the caller of | |
870 // success or any error, other than waiting for IO. | |
871 if (rv != ERR_IO_PENDING) { | |
872 // If there is no connect callback available to call, we are renegotiating | |
873 // (which occurs because we are in the middle of a Read when the | |
874 // renegotiation process starts). So we complete the Read here. | |
875 if (user_connect_callback_.is_null()) { | |
876 CompletionCallback c = user_read_callback_; | |
877 user_read_callback_.Reset(); | |
878 user_read_buf_ = NULL; | |
879 user_read_buf_len_ = 0; | |
880 c.Run(rv); | |
881 return; | |
882 } | |
883 net_log_.EndEvent(NetLog::TYPE_SSL_CONNECT); | |
884 CompletionCallback c = user_connect_callback_; | |
885 user_connect_callback_.Reset(); | |
886 c.Run(rv); | |
887 } | |
888 } | |
889 | |
890 void SSLClientSocketWin::OnReadComplete(int result) { | |
891 DCHECK(completed_handshake()); | |
892 | |
893 result = DoPayloadReadComplete(result); | |
894 if (result > 0) | |
895 result = DoPayloadDecrypt(); | |
896 if (result != ERR_IO_PENDING) { | |
897 DCHECK(!user_read_callback_.is_null()); | |
898 CompletionCallback c = user_read_callback_; | |
899 user_read_callback_.Reset(); | |
900 user_read_buf_ = NULL; | |
901 user_read_buf_len_ = 0; | |
902 c.Run(result); | |
903 } | |
904 } | |
905 | |
906 void SSLClientSocketWin::OnWriteComplete(int result) { | |
907 DCHECK(completed_handshake()); | |
908 | |
909 int rv = DoPayloadWriteComplete(result); | |
910 if (rv != ERR_IO_PENDING) { | |
911 DCHECK(!user_write_callback_.is_null()); | |
912 CompletionCallback c = user_write_callback_; | |
913 user_write_callback_.Reset(); | |
914 user_write_buf_ = NULL; | |
915 user_write_buf_len_ = 0; | |
916 c.Run(rv); | |
917 } | |
918 } | |
919 | |
920 | |
921 int SSLClientSocketWin::DoLoop(int last_io_result) { | |
922 DCHECK(next_state_ != STATE_NONE); | |
923 int rv = last_io_result; | |
924 do { | |
925 State state = next_state_; | |
926 next_state_ = STATE_NONE; | |
927 switch (state) { | |
928 case STATE_HANDSHAKE_READ: | |
929 rv = DoHandshakeRead(); | |
930 break; | |
931 case STATE_HANDSHAKE_READ_COMPLETE: | |
932 rv = DoHandshakeReadComplete(rv); | |
933 break; | |
934 case STATE_HANDSHAKE_WRITE: | |
935 rv = DoHandshakeWrite(); | |
936 break; | |
937 case STATE_HANDSHAKE_WRITE_COMPLETE: | |
938 rv = DoHandshakeWriteComplete(rv); | |
939 break; | |
940 case STATE_VERIFY_CERT: | |
941 rv = DoVerifyCert(); | |
942 break; | |
943 case STATE_VERIFY_CERT_COMPLETE: | |
944 rv = DoVerifyCertComplete(rv); | |
945 break; | |
946 case STATE_COMPLETED_RENEGOTIATION: | |
947 rv = DoCompletedRenegotiation(rv); | |
948 break; | |
949 case STATE_COMPLETED_HANDSHAKE: | |
950 next_state_ = STATE_COMPLETED_HANDSHAKE; | |
951 // This is the end of our state machine, so return. | |
952 return rv; | |
953 default: | |
954 rv = ERR_UNEXPECTED; | |
955 LOG(DFATAL) << "unexpected state " << state; | |
956 break; | |
957 } | |
958 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); | |
959 return rv; | |
960 } | |
961 | |
962 int SSLClientSocketWin::DoHandshakeRead() { | |
963 next_state_ = STATE_HANDSHAKE_READ_COMPLETE; | |
964 | |
965 if (!recv_buffer_.get()) | |
966 recv_buffer_.reset(new char[kRecvBufferSize]); | |
967 | |
968 int buf_len = kRecvBufferSize - bytes_received_; | |
969 | |
970 if (buf_len <= 0) { | |
971 LOG(DFATAL) << "Receive buffer is too small!"; | |
972 return ERR_UNEXPECTED; | |
973 } | |
974 | |
975 DCHECK(!transport_read_buf_); | |
976 transport_read_buf_ = new IOBuffer(buf_len); | |
977 | |
978 return transport_->socket()->Read( | |
979 transport_read_buf_, buf_len, | |
980 base::Bind(&SSLClientSocketWin::OnHandshakeIOComplete, | |
981 base::Unretained(this))); | |
982 } | |
983 | |
984 int SSLClientSocketWin::DoHandshakeReadComplete(int result) { | |
985 if (result < 0) { | |
986 transport_read_buf_ = NULL; | |
987 return result; | |
988 } | |
989 | |
990 if (transport_read_buf_) { | |
991 // A transition to STATE_HANDSHAKE_READ_COMPLETE is set in multiple places, | |
992 // not only in DoHandshakeRead(), so we may not have a transport_read_buf_. | |
993 DCHECK_LE(result, kRecvBufferSize - bytes_received_); | |
994 char* buf = recv_buffer_.get() + bytes_received_; | |
995 memcpy(buf, transport_read_buf_->data(), result); | |
996 transport_read_buf_ = NULL; | |
997 } | |
998 | |
999 if (result == 0 && !ignore_ok_result_) | |
1000 return ERR_SSL_PROTOCOL_ERROR; // Incomplete response :( | |
1001 | |
1002 ignore_ok_result_ = false; | |
1003 | |
1004 bytes_received_ += result; | |
1005 | |
1006 // Process the contents of recv_buffer_. | |
1007 TimeStamp expiry; | |
1008 DWORD out_flags; | |
1009 | |
1010 DWORD flags = ISC_REQ_SEQUENCE_DETECT | | |
1011 ISC_REQ_REPLAY_DETECT | | |
1012 ISC_REQ_CONFIDENTIALITY | | |
1013 ISC_RET_EXTENDED_ERROR | | |
1014 ISC_REQ_ALLOCATE_MEMORY | | |
1015 ISC_REQ_STREAM; | |
1016 | |
1017 if (ssl_config_.send_client_cert) | |
1018 flags |= ISC_REQ_USE_SUPPLIED_CREDS; | |
1019 | |
1020 SecBufferDesc in_buffer_desc, out_buffer_desc; | |
1021 | |
1022 in_buffer_desc.cBuffers = 2; | |
1023 in_buffer_desc.pBuffers = in_buffers_; | |
1024 in_buffer_desc.ulVersion = SECBUFFER_VERSION; | |
1025 | |
1026 in_buffers_[0].pvBuffer = recv_buffer_.get(); | |
1027 in_buffers_[0].cbBuffer = bytes_received_; | |
1028 in_buffers_[0].BufferType = SECBUFFER_TOKEN; | |
1029 | |
1030 in_buffers_[1].pvBuffer = NULL; | |
1031 in_buffers_[1].cbBuffer = 0; | |
1032 in_buffers_[1].BufferType = SECBUFFER_EMPTY; | |
1033 | |
1034 out_buffer_desc.cBuffers = 1; | |
1035 out_buffer_desc.pBuffers = &send_buffer_; | |
1036 out_buffer_desc.ulVersion = SECBUFFER_VERSION; | |
1037 | |
1038 send_buffer_.pvBuffer = NULL; | |
1039 send_buffer_.BufferType = SECBUFFER_TOKEN; | |
1040 send_buffer_.cbBuffer = 0; | |
1041 | |
1042 isc_status_ = InitializeSecurityContext( | |
1043 creds_, | |
1044 &ctxt_, | |
1045 NULL, | |
1046 flags, | |
1047 0, | |
1048 0, | |
1049 &in_buffer_desc, | |
1050 0, | |
1051 NULL, | |
1052 &out_buffer_desc, | |
1053 &out_flags, | |
1054 &expiry); | |
1055 | |
1056 if (isc_status_ == SEC_E_INVALID_TOKEN) { | |
1057 // Peer sent us an SSL record type that's invalid during SSL handshake. | |
1058 // TODO(wtc): move this to MapSecurityError after sufficient testing. | |
1059 LOG(ERROR) << "InitializeSecurityContext failed: " << isc_status_; | |
1060 return ERR_SSL_PROTOCOL_ERROR; | |
1061 } | |
1062 | |
1063 if (send_buffer_.cbBuffer != 0 && | |
1064 (isc_status_ == SEC_E_OK || | |
1065 isc_status_ == SEC_I_CONTINUE_NEEDED || | |
1066 (FAILED(isc_status_) && (out_flags & ISC_RET_EXTENDED_ERROR)))) { | |
1067 next_state_ = STATE_HANDSHAKE_WRITE; | |
1068 return OK; | |
1069 } | |
1070 return DidCallInitializeSecurityContext(); | |
1071 } | |
1072 | |
1073 int SSLClientSocketWin::DidCallInitializeSecurityContext() { | |
1074 if (isc_status_ == SEC_E_INCOMPLETE_MESSAGE) { | |
1075 next_state_ = STATE_HANDSHAKE_READ; | |
1076 return OK; | |
1077 } | |
1078 | |
1079 if (isc_status_ == SEC_E_OK) { | |
1080 if (in_buffers_[1].BufferType == SECBUFFER_EXTRA) { | |
1081 // Save this data for later. | |
1082 memmove(recv_buffer_.get(), | |
1083 recv_buffer_.get() + (bytes_received_ - in_buffers_[1].cbBuffer), | |
1084 in_buffers_[1].cbBuffer); | |
1085 bytes_received_ = in_buffers_[1].cbBuffer; | |
1086 } else { | |
1087 bytes_received_ = 0; | |
1088 } | |
1089 return DidCompleteHandshake(); | |
1090 } | |
1091 | |
1092 if (FAILED(isc_status_)) { | |
1093 LOG(ERROR) << "InitializeSecurityContext failed: " << isc_status_; | |
1094 if (isc_status_ == SEC_E_INTERNAL_ERROR) { | |
1095 // "The Local Security Authority cannot be contacted". This happens | |
1096 // when the user denies access to the private key for SSL client auth. | |
1097 return ERR_SSL_CLIENT_AUTH_PRIVATE_KEY_ACCESS_DENIED; | |
1098 } | |
1099 int result = MapSecurityError(isc_status_); | |
1100 // We told Schannel to not verify the server certificate | |
1101 // (SCH_CRED_MANUAL_CRED_VALIDATION), so any certificate error returned by | |
1102 // InitializeSecurityContext must be referring to the bad or missing | |
1103 // client certificate. | |
1104 if (IsCertificateError(result)) { | |
1105 // TODO(wtc): Add fine-grained error codes for client certificate errors | |
1106 // reported by the server using the following SSL/TLS alert messages: | |
1107 // access_denied | |
1108 // bad_certificate | |
1109 // unsupported_certificate | |
1110 // certificate_expired | |
1111 // certificate_revoked | |
1112 // certificate_unknown | |
1113 // unknown_ca | |
1114 return ERR_BAD_SSL_CLIENT_AUTH_CERT; | |
1115 } | |
1116 return result; | |
1117 } | |
1118 | |
1119 if (isc_status_ == SEC_I_INCOMPLETE_CREDENTIALS) | |
1120 return ERR_SSL_CLIENT_AUTH_CERT_NEEDED; | |
1121 | |
1122 if (isc_status_ == SEC_I_NO_RENEGOTIATION) { | |
1123 // Received a no_renegotiation alert message. Although this is just a | |
1124 // warning, SChannel doesn't seem to allow us to continue after this | |
1125 // point, so we have to return an error. See http://crbug.com/36835. | |
1126 return ERR_SSL_NO_RENEGOTIATION; | |
1127 } | |
1128 | |
1129 DCHECK(isc_status_ == SEC_I_CONTINUE_NEEDED); | |
1130 if (in_buffers_[1].BufferType == SECBUFFER_EXTRA) { | |
1131 memmove(recv_buffer_.get(), | |
1132 recv_buffer_.get() + (bytes_received_ - in_buffers_[1].cbBuffer), | |
1133 in_buffers_[1].cbBuffer); | |
1134 bytes_received_ = in_buffers_[1].cbBuffer; | |
1135 next_state_ = STATE_HANDSHAKE_READ_COMPLETE; | |
1136 ignore_ok_result_ = true; // OK doesn't mean EOF. | |
1137 return OK; | |
1138 } | |
1139 | |
1140 bytes_received_ = 0; | |
1141 next_state_ = STATE_HANDSHAKE_READ; | |
1142 return OK; | |
1143 } | |
1144 | |
1145 int SSLClientSocketWin::DoHandshakeWrite() { | |
1146 next_state_ = STATE_HANDSHAKE_WRITE_COMPLETE; | |
1147 | |
1148 // We should have something to send. | |
1149 DCHECK(send_buffer_.pvBuffer); | |
1150 DCHECK(send_buffer_.cbBuffer > 0); | |
1151 DCHECK(!transport_write_buf_); | |
1152 | |
1153 const char* buf = static_cast<char*>(send_buffer_.pvBuffer) + bytes_sent_; | |
1154 int buf_len = send_buffer_.cbBuffer - bytes_sent_; | |
1155 transport_write_buf_ = new IOBuffer(buf_len); | |
1156 memcpy(transport_write_buf_->data(), buf, buf_len); | |
1157 | |
1158 return transport_->socket()->Write( | |
1159 transport_write_buf_, buf_len, | |
1160 base::Bind(&SSLClientSocketWin::OnHandshakeIOComplete, | |
1161 base::Unretained(this))); | |
1162 } | |
1163 | |
1164 int SSLClientSocketWin::DoHandshakeWriteComplete(int result) { | |
1165 DCHECK(transport_write_buf_); | |
1166 transport_write_buf_ = NULL; | |
1167 if (result < 0) | |
1168 return result; | |
1169 | |
1170 DCHECK(result != 0); | |
1171 | |
1172 bytes_sent_ += result; | |
1173 DCHECK(bytes_sent_ <= static_cast<int>(send_buffer_.cbBuffer)); | |
1174 | |
1175 if (bytes_sent_ >= static_cast<int>(send_buffer_.cbBuffer)) { | |
1176 bool overflow = (bytes_sent_ > static_cast<int>(send_buffer_.cbBuffer)); | |
1177 FreeSendBuffer(); | |
1178 bytes_sent_ = 0; | |
1179 if (overflow) { // Bug! | |
1180 LOG(DFATAL) << "overflow"; | |
1181 return ERR_UNEXPECTED; | |
1182 } | |
1183 if (writing_first_token_) { | |
1184 writing_first_token_ = false; | |
1185 DCHECK(bytes_received_ == 0); | |
1186 next_state_ = STATE_HANDSHAKE_READ; | |
1187 return OK; | |
1188 } | |
1189 return DidCallInitializeSecurityContext(); | |
1190 } | |
1191 | |
1192 // Send the remaining bytes. | |
1193 next_state_ = STATE_HANDSHAKE_WRITE; | |
1194 return OK; | |
1195 } | |
1196 | |
1197 // Set server_cert_status_ and return OK or a network error. | |
1198 int SSLClientSocketWin::DoVerifyCert() { | |
1199 next_state_ = STATE_VERIFY_CERT_COMPLETE; | |
1200 | |
1201 DCHECK(server_cert_); | |
1202 CertStatus cert_status; | |
1203 if (ssl_config_.IsAllowedBadCert(server_cert_, &cert_status)) { | |
1204 VLOG(1) << "Received an expected bad cert with status: " << cert_status; | |
1205 server_cert_verify_result_.Reset(); | |
1206 server_cert_verify_result_.cert_status = cert_status; | |
1207 server_cert_verify_result_.verified_cert = server_cert_; | |
1208 return OK; | |
1209 } | |
1210 | |
1211 int flags = 0; | |
1212 if (ssl_config_.rev_checking_enabled) | |
1213 flags |= CertVerifier::VERIFY_REV_CHECKING_ENABLED; | |
1214 if (ssl_config_.verify_ev_cert) | |
1215 flags |= CertVerifier::VERIFY_EV_CERT; | |
1216 if (ssl_config_.cert_io_enabled) | |
1217 flags |= CertVerifier::VERIFY_CERT_IO_ENABLED; | |
1218 verifier_.reset(new SingleRequestCertVerifier(cert_verifier_)); | |
1219 return verifier_->Verify( | |
1220 server_cert_, host_and_port_.host(), flags, | |
1221 NULL /* no CRL set */, | |
1222 &server_cert_verify_result_, | |
1223 base::Bind(&SSLClientSocketWin::OnHandshakeIOComplete, | |
1224 base::Unretained(this)), | |
1225 net_log_); | |
1226 } | |
1227 | |
1228 int SSLClientSocketWin::DoVerifyCertComplete(int result) { | |
1229 DCHECK(verifier_.get()); | |
1230 verifier_.reset(); | |
1231 | |
1232 if (result == OK) | |
1233 LogConnectionTypeMetrics(); | |
1234 if (renegotiating_) { | |
1235 DidCompleteRenegotiation(); | |
1236 return result; | |
1237 } | |
1238 | |
1239 // The initial handshake has completed. | |
1240 next_state_ = STATE_COMPLETED_HANDSHAKE; | |
1241 return result; | |
1242 } | |
1243 | |
1244 int SSLClientSocketWin::DoPayloadRead() { | |
1245 DCHECK(recv_buffer_.get()); | |
1246 | |
1247 int buf_len = kRecvBufferSize - bytes_received_; | |
1248 | |
1249 if (buf_len <= 0) { | |
1250 NOTREACHED() << "Receive buffer is too small!"; | |
1251 return ERR_FAILED; | |
1252 } | |
1253 | |
1254 int rv; | |
1255 // If bytes_received_, we have some data from a previous read still ready | |
1256 // for decoding. Otherwise, we need to issue a real read. | |
1257 if (!bytes_received_ || need_more_data_) { | |
1258 DCHECK(!transport_read_buf_); | |
1259 transport_read_buf_ = new IOBuffer(buf_len); | |
1260 | |
1261 rv = transport_->socket()->Read( | |
1262 transport_read_buf_, buf_len, | |
1263 base::Bind(&SSLClientSocketWin::OnReadComplete, | |
1264 base::Unretained(this))); | |
1265 if (rv != ERR_IO_PENDING) | |
1266 rv = DoPayloadReadComplete(rv); | |
1267 if (rv <= 0) | |
1268 return rv; | |
1269 } | |
1270 | |
1271 // Decode what we've read. If there is not enough data to decode yet, | |
1272 // this may return ERR_IO_PENDING still. | |
1273 return DoPayloadDecrypt(); | |
1274 } | |
1275 | |
1276 // result is the number of bytes that have been read; it should not be | |
1277 // less than zero; a value of zero means that no additional bytes have | |
1278 // been read. | |
1279 int SSLClientSocketWin::DoPayloadReadComplete(int result) { | |
1280 DCHECK(completed_handshake()); | |
1281 | |
1282 // If IO Pending, there is nothing to do here. | |
1283 if (result == ERR_IO_PENDING) | |
1284 return result; | |
1285 | |
1286 // We completed a Read, so reset the need_more_data_ flag. | |
1287 need_more_data_ = false; | |
1288 | |
1289 // Check for error | |
1290 if (result <= 0) { | |
1291 transport_read_buf_ = NULL; | |
1292 if (result == 0 && bytes_received_ != 0) { | |
1293 // TODO(wtc): Unless we have received the close_notify alert, we need | |
1294 // to return an error code indicating that the SSL connection ended | |
1295 // uncleanly, a potential truncation attack. See | |
1296 // http://crbug.com/18586. | |
1297 return ERR_SSL_PROTOCOL_ERROR; | |
1298 } | |
1299 return result; | |
1300 } | |
1301 | |
1302 // Transfer the data from transport_read_buf_ to recv_buffer_. | |
1303 if (transport_read_buf_) { | |
1304 DCHECK_LE(result, kRecvBufferSize - bytes_received_); | |
1305 char* buf = recv_buffer_.get() + bytes_received_; | |
1306 memcpy(buf, transport_read_buf_->data(), result); | |
1307 transport_read_buf_ = NULL; | |
1308 } | |
1309 | |
1310 bytes_received_ += result; | |
1311 | |
1312 return result; | |
1313 } | |
1314 | |
1315 int SSLClientSocketWin::DoPayloadDecrypt() { | |
1316 // Process the contents of recv_buffer_. | |
1317 int len = 0; // the number of bytes we've copied to the user buffer. | |
1318 while (bytes_received_) { | |
1319 SecBuffer buffers[4]; | |
1320 buffers[0].pvBuffer = recv_buffer_.get(); | |
1321 buffers[0].cbBuffer = bytes_received_; | |
1322 buffers[0].BufferType = SECBUFFER_DATA; | |
1323 | |
1324 buffers[1].BufferType = SECBUFFER_EMPTY; | |
1325 buffers[2].BufferType = SECBUFFER_EMPTY; | |
1326 buffers[3].BufferType = SECBUFFER_EMPTY; | |
1327 | |
1328 SecBufferDesc buffer_desc; | |
1329 buffer_desc.cBuffers = 4; | |
1330 buffer_desc.pBuffers = buffers; | |
1331 buffer_desc.ulVersion = SECBUFFER_VERSION; | |
1332 | |
1333 SECURITY_STATUS status; | |
1334 status = DecryptMessage(&ctxt_, &buffer_desc, 0, NULL); | |
1335 | |
1336 if (status == SEC_E_INCOMPLETE_MESSAGE) { | |
1337 need_more_data_ = true; | |
1338 return DoPayloadRead(); | |
1339 } | |
1340 | |
1341 if (status == SEC_I_CONTEXT_EXPIRED) { | |
1342 // Received the close_notify alert. | |
1343 bytes_received_ = 0; | |
1344 return OK; | |
1345 } | |
1346 | |
1347 if (status != SEC_E_OK && status != SEC_I_RENEGOTIATE) { | |
1348 DCHECK(status != SEC_E_MESSAGE_ALTERED); | |
1349 LOG(ERROR) << "DecryptMessage failed: " << status; | |
1350 return MapSecurityError(status); | |
1351 } | |
1352 | |
1353 // The received ciphertext was decrypted in place in recv_buffer_. Remember | |
1354 // the location and length of the decrypted plaintext and any unused | |
1355 // ciphertext. | |
1356 decrypted_ptr_ = NULL; | |
1357 bytes_decrypted_ = 0; | |
1358 received_ptr_ = NULL; | |
1359 bytes_received_ = 0; | |
1360 for (int i = 1; i < 4; i++) { | |
1361 switch (buffers[i].BufferType) { | |
1362 case SECBUFFER_DATA: | |
1363 DCHECK(!decrypted_ptr_ && bytes_decrypted_ == 0); | |
1364 decrypted_ptr_ = static_cast<char*>(buffers[i].pvBuffer); | |
1365 bytes_decrypted_ = buffers[i].cbBuffer; | |
1366 break; | |
1367 case SECBUFFER_EXTRA: | |
1368 DCHECK(!received_ptr_ && bytes_received_ == 0); | |
1369 received_ptr_ = static_cast<char*>(buffers[i].pvBuffer); | |
1370 bytes_received_ = buffers[i].cbBuffer; | |
1371 break; | |
1372 default: | |
1373 break; | |
1374 } | |
1375 } | |
1376 | |
1377 DCHECK(len == 0); | |
1378 if (bytes_decrypted_ != 0) { | |
1379 len = std::min(user_read_buf_len_, bytes_decrypted_); | |
1380 memcpy(user_read_buf_->data(), decrypted_ptr_, len); | |
1381 decrypted_ptr_ += len; | |
1382 bytes_decrypted_ -= len; | |
1383 } | |
1384 if (bytes_decrypted_ == 0) { | |
1385 decrypted_ptr_ = NULL; | |
1386 if (bytes_received_ != 0) { | |
1387 memmove(recv_buffer_.get(), received_ptr_, bytes_received_); | |
1388 received_ptr_ = recv_buffer_.get(); | |
1389 } | |
1390 } | |
1391 | |
1392 if (status == SEC_I_RENEGOTIATE) { | |
1393 if (bytes_received_ != 0) { | |
1394 // The server requested renegotiation, but there are some data yet to | |
1395 // be decrypted. The Platform SDK WebClient.c sample doesn't handle | |
1396 // this, so we don't know how to handle this. Assume this cannot | |
1397 // happen. | |
1398 LOG(ERROR) << "DecryptMessage returned SEC_I_RENEGOTIATE with a buffer " | |
1399 << "of type SECBUFFER_EXTRA."; | |
1400 return ERR_SSL_RENEGOTIATION_REQUESTED; | |
1401 } | |
1402 if (len != 0) { | |
1403 // The server requested renegotiation, but there are some decrypted | |
1404 // data. We can't start renegotiation until we have returned all | |
1405 // decrypted data to the caller. | |
1406 // | |
1407 // This hasn't happened during testing. Assume this cannot happen even | |
1408 // though we know how to handle this. | |
1409 LOG(ERROR) << "DecryptMessage returned SEC_I_RENEGOTIATE with a buffer " | |
1410 << "of type SECBUFFER_DATA."; | |
1411 return ERR_SSL_RENEGOTIATION_REQUESTED; | |
1412 } | |
1413 // Jump to the handshake sequence. Will come back when the rehandshake is | |
1414 // done. | |
1415 renegotiating_ = true; | |
1416 ignore_ok_result_ = true; // OK doesn't mean EOF. | |
1417 // If renegotiation handshake occurred, we need to go back into the | |
1418 // handshake state machine. | |
1419 next_state_ = STATE_HANDSHAKE_READ_COMPLETE; | |
1420 return DoLoop(OK); | |
1421 } | |
1422 | |
1423 // We've already copied data into the user buffer, so quit now. | |
1424 // TODO(mbelshe): We really should keep decoding as long as we can. This | |
1425 // break out is causing us to return pretty small chunks of data up to the | |
1426 // application, even though more is already buffered and ready to be | |
1427 // decoded. | |
1428 if (len) | |
1429 break; | |
1430 } | |
1431 | |
1432 // If we decrypted 0 bytes, don't report 0 bytes read, which would be | |
1433 // mistaken for EOF. Continue decrypting or read more. | |
1434 if (len == 0) | |
1435 return DoPayloadRead(); | |
1436 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, len, | |
1437 user_read_buf_->data()); | |
1438 return len; | |
1439 } | |
1440 | |
1441 int SSLClientSocketWin::DoPayloadEncrypt() { | |
1442 DCHECK(completed_handshake()); | |
1443 DCHECK(user_write_buf_); | |
1444 DCHECK(user_write_buf_len_ > 0); | |
1445 | |
1446 ULONG message_len = std::min( | |
1447 stream_sizes_.cbMaximumMessage, static_cast<ULONG>(user_write_buf_len_)); | |
1448 ULONG alloc_len = | |
1449 message_len + stream_sizes_.cbHeader + stream_sizes_.cbTrailer; | |
1450 user_write_buf_len_ = message_len; | |
1451 | |
1452 payload_send_buffer_.reset(new char[alloc_len]); | |
1453 memcpy(&payload_send_buffer_[stream_sizes_.cbHeader], | |
1454 user_write_buf_->data(), message_len); | |
1455 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_SENT, message_len, | |
1456 user_write_buf_->data()); | |
1457 | |
1458 SecBuffer buffers[4]; | |
1459 buffers[0].pvBuffer = payload_send_buffer_.get(); | |
1460 buffers[0].cbBuffer = stream_sizes_.cbHeader; | |
1461 buffers[0].BufferType = SECBUFFER_STREAM_HEADER; | |
1462 | |
1463 buffers[1].pvBuffer = &payload_send_buffer_[stream_sizes_.cbHeader]; | |
1464 buffers[1].cbBuffer = message_len; | |
1465 buffers[1].BufferType = SECBUFFER_DATA; | |
1466 | |
1467 buffers[2].pvBuffer = &payload_send_buffer_[stream_sizes_.cbHeader + | |
1468 message_len]; | |
1469 buffers[2].cbBuffer = stream_sizes_.cbTrailer; | |
1470 buffers[2].BufferType = SECBUFFER_STREAM_TRAILER; | |
1471 | |
1472 buffers[3].BufferType = SECBUFFER_EMPTY; | |
1473 | |
1474 SecBufferDesc buffer_desc; | |
1475 buffer_desc.cBuffers = 4; | |
1476 buffer_desc.pBuffers = buffers; | |
1477 buffer_desc.ulVersion = SECBUFFER_VERSION; | |
1478 | |
1479 SECURITY_STATUS status = EncryptMessage(&ctxt_, 0, &buffer_desc, 0); | |
1480 | |
1481 if (FAILED(status)) { | |
1482 LOG(ERROR) << "EncryptMessage failed: " << status; | |
1483 return MapSecurityError(status); | |
1484 } | |
1485 | |
1486 payload_send_buffer_len_ = buffers[0].cbBuffer + | |
1487 buffers[1].cbBuffer + | |
1488 buffers[2].cbBuffer; | |
1489 DCHECK(bytes_sent_ == 0); | |
1490 return OK; | |
1491 } | |
1492 | |
1493 int SSLClientSocketWin::DoPayloadWrite() { | |
1494 DCHECK(completed_handshake()); | |
1495 | |
1496 // We should have something to send. | |
1497 DCHECK(payload_send_buffer_.get()); | |
1498 DCHECK(payload_send_buffer_len_ > 0); | |
1499 DCHECK(!transport_write_buf_); | |
1500 | |
1501 const char* buf = payload_send_buffer_.get() + bytes_sent_; | |
1502 int buf_len = payload_send_buffer_len_ - bytes_sent_; | |
1503 transport_write_buf_ = new IOBuffer(buf_len); | |
1504 memcpy(transport_write_buf_->data(), buf, buf_len); | |
1505 | |
1506 int rv = transport_->socket()->Write( | |
1507 transport_write_buf_, buf_len, | |
1508 base::Bind(&SSLClientSocketWin::OnWriteComplete, | |
1509 base::Unretained(this))); | |
1510 if (rv != ERR_IO_PENDING) | |
1511 rv = DoPayloadWriteComplete(rv); | |
1512 return rv; | |
1513 } | |
1514 | |
1515 int SSLClientSocketWin::DoPayloadWriteComplete(int result) { | |
1516 DCHECK(transport_write_buf_); | |
1517 transport_write_buf_ = NULL; | |
1518 if (result < 0) | |
1519 return result; | |
1520 | |
1521 DCHECK(result != 0); | |
1522 | |
1523 bytes_sent_ += result; | |
1524 DCHECK(bytes_sent_ <= payload_send_buffer_len_); | |
1525 | |
1526 if (bytes_sent_ >= payload_send_buffer_len_) { | |
1527 bool overflow = (bytes_sent_ > payload_send_buffer_len_); | |
1528 payload_send_buffer_.reset(); | |
1529 payload_send_buffer_len_ = 0; | |
1530 bytes_sent_ = 0; | |
1531 if (overflow) { // Bug! | |
1532 LOG(DFATAL) << "overflow"; | |
1533 return ERR_UNEXPECTED; | |
1534 } | |
1535 // Done | |
1536 return user_write_buf_len_; | |
1537 } | |
1538 | |
1539 // Send the remaining bytes. | |
1540 return DoPayloadWrite(); | |
1541 } | |
1542 | |
1543 int SSLClientSocketWin::DoCompletedRenegotiation(int result) { | |
1544 // The user had a read in progress, which was usurped by the renegotiation. | |
1545 // Restart the read sequence. | |
1546 next_state_ = STATE_COMPLETED_HANDSHAKE; | |
1547 if (result != OK) | |
1548 return result; | |
1549 return DoPayloadRead(); | |
1550 } | |
1551 | |
1552 int SSLClientSocketWin::DidCompleteHandshake() { | |
1553 SECURITY_STATUS status = QueryContextAttributes( | |
1554 &ctxt_, SECPKG_ATTR_STREAM_SIZES, &stream_sizes_); | |
1555 if (status != SEC_E_OK) { | |
1556 LOG(ERROR) << "QueryContextAttributes (stream sizes) failed: " << status; | |
1557 return MapSecurityError(status); | |
1558 } | |
1559 DCHECK(!server_cert_ || renegotiating_); | |
1560 PCCERT_CONTEXT server_cert_handle = NULL; | |
1561 status = QueryContextAttributes( | |
1562 &ctxt_, SECPKG_ATTR_REMOTE_CERT_CONTEXT, &server_cert_handle); | |
1563 if (status != SEC_E_OK) { | |
1564 LOG(ERROR) << "QueryContextAttributes (remote cert) failed: " << status; | |
1565 return MapSecurityError(status); | |
1566 } | |
1567 | |
1568 X509Certificate::OSCertHandles intermediates; | |
1569 PCCERT_CONTEXT intermediate = NULL; | |
1570 // In testing, enumerating the store returned from SChannel appears to | |
1571 // enumerate certificates in reverse of the order they were added, meaning | |
1572 // that issuer certificates appear before the subject certificates. This is | |
1573 // likely because the default Windows memory store is implemented as a | |
1574 // linked-list. Reverse the list, so that intermediates are ordered from | |
1575 // subject to issuer. | |
1576 // Note that the store also includes the end-entity (server) certificate, | |
1577 // so exclude this certificate from the set of |intermediates|. | |
1578 while ((intermediate = CertEnumCertificatesInStore( | |
1579 server_cert_handle->hCertStore, intermediate))) { | |
1580 if (!X509Certificate::IsSameOSCert(server_cert_handle, intermediate)) | |
1581 intermediates.push_back(CertDuplicateCertificateContext(intermediate)); | |
1582 } | |
1583 std::reverse(intermediates.begin(), intermediates.end()); | |
1584 | |
1585 scoped_refptr<X509Certificate> new_server_cert( | |
1586 X509Certificate::CreateFromHandle(server_cert_handle, intermediates)); | |
1587 net_log_.AddEvent( | |
1588 NetLog::TYPE_SSL_CERTIFICATES_RECEIVED, | |
1589 base::Bind(&NetLogX509CertificateCallback, | |
1590 base::Unretained(new_server_cert.get()))); | |
1591 if (renegotiating_ && IsCertificateChainIdentical(server_cert_, | |
1592 new_server_cert)) { | |
1593 // We already verified the server certificate. Either it is good or the | |
1594 // user has accepted the certificate error. | |
1595 DidCompleteRenegotiation(); | |
1596 } else { | |
1597 server_cert_ = new_server_cert; | |
1598 next_state_ = STATE_VERIFY_CERT; | |
1599 } | |
1600 CertFreeCertificateContext(server_cert_handle); | |
1601 for (size_t i = 0; i < intermediates.size(); ++i) | |
1602 CertFreeCertificateContext(intermediates[i]); | |
1603 return OK; | |
1604 } | |
1605 | |
1606 // Called when a renegotiation is completed. |result| is the verification | |
1607 // result of the server certificate received during renegotiation. | |
1608 void SSLClientSocketWin::DidCompleteRenegotiation() { | |
1609 DCHECK(user_connect_callback_.is_null()); | |
1610 DCHECK(!user_read_callback_.is_null()); | |
1611 renegotiating_ = false; | |
1612 next_state_ = STATE_COMPLETED_RENEGOTIATION; | |
1613 } | |
1614 | |
1615 void SSLClientSocketWin::LogConnectionTypeMetrics() const { | |
1616 UpdateConnectionTypeHistograms(CONNECTION_SSL); | |
1617 if (server_cert_verify_result_.has_md5) | |
1618 UpdateConnectionTypeHistograms(CONNECTION_SSL_MD5); | |
1619 if (server_cert_verify_result_.has_md2) | |
1620 UpdateConnectionTypeHistograms(CONNECTION_SSL_MD2); | |
1621 if (server_cert_verify_result_.has_md4) | |
1622 UpdateConnectionTypeHistograms(CONNECTION_SSL_MD4); | |
1623 if (server_cert_verify_result_.has_md5_ca) | |
1624 UpdateConnectionTypeHistograms(CONNECTION_SSL_MD5_CA); | |
1625 if (server_cert_verify_result_.has_md2_ca) | |
1626 UpdateConnectionTypeHistograms(CONNECTION_SSL_MD2_CA); | |
1627 } | |
1628 | |
1629 void SSLClientSocketWin::FreeSendBuffer() { | |
1630 SECURITY_STATUS status = FreeContextBuffer(send_buffer_.pvBuffer); | |
1631 DCHECK(status == SEC_E_OK); | |
1632 memset(&send_buffer_, 0, sizeof(send_buffer_)); | |
1633 } | |
1634 | |
1635 } // namespace net | |
OLD | NEW |