Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(252)

Side by Side Diff: net/base/x509_certificate_win.cc

Issue 3112013: Move chain building/verification out of X509Certificate (Closed)
Patch Set: Rebase to trunk - Without OpenSSL fixes Created 10 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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/base/x509_certificate.h" 5 #include "net/base/x509_certificate.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/pickle.h" 8 #include "base/pickle.h"
9 #include "base/scoped_ptr.h"
9 #include "base/string_tokenizer.h" 10 #include "base/string_tokenizer.h"
10 #include "base/string_util.h" 11 #include "base/string_util.h"
11 #include "base/utf_string_conversions.h" 12 #include "base/utf_string_conversions.h"
12 #include "net/base/cert_status_flags.h"
13 #include "net/base/cert_verify_result.h"
14 #include "net/base/ev_root_ca_metadata.h"
15 #include "net/base/net_errors.h"
16 #include "net/base/scoped_cert_chain_context.h"
17 13
18 #pragma comment(lib, "crypt32.lib") 14 #pragma comment(lib, "crypt32.lib")
19 15
20 using base::Time; 16 using base::Time;
21 17
22 namespace net { 18 namespace net {
23 19
24 namespace { 20 namespace {
25 21
26 //-----------------------------------------------------------------------------
27
28 // TODO(wtc): This is a copy of the MapSecurityError function in
29 // ssl_client_socket_win.cc. Another function that maps Windows error codes
30 // to our network error codes is WinInetUtil::OSErrorToNetError. We should
31 // eliminate the code duplication.
32 int MapSecurityError(SECURITY_STATUS err) {
33 // There are numerous security error codes, but these are the ones we thus
34 // far find interesting.
35 switch (err) {
36 case SEC_E_WRONG_PRINCIPAL: // Schannel
37 case CERT_E_CN_NO_MATCH: // CryptoAPI
38 return ERR_CERT_COMMON_NAME_INVALID;
39 case SEC_E_UNTRUSTED_ROOT: // Schannel
40 case CERT_E_UNTRUSTEDROOT: // CryptoAPI
41 return ERR_CERT_AUTHORITY_INVALID;
42 case SEC_E_CERT_EXPIRED: // Schannel
43 case CERT_E_EXPIRED: // CryptoAPI
44 return ERR_CERT_DATE_INVALID;
45 case CRYPT_E_NO_REVOCATION_CHECK:
46 return ERR_CERT_NO_REVOCATION_MECHANISM;
47 case CRYPT_E_REVOCATION_OFFLINE:
48 return ERR_CERT_UNABLE_TO_CHECK_REVOCATION;
49 case CRYPT_E_REVOKED: // Schannel and CryptoAPI
50 return ERR_CERT_REVOKED;
51 case SEC_E_CERT_UNKNOWN:
52 case CERT_E_ROLE:
53 return ERR_CERT_INVALID;
54 case CERT_E_WRONG_USAGE:
55 // TODO(wtc): Should we add ERR_CERT_WRONG_USAGE?
56 return ERR_CERT_INVALID;
57 // We received an unexpected_message or illegal_parameter alert message
58 // from the server.
59 case SEC_E_ILLEGAL_MESSAGE:
60 return ERR_SSL_PROTOCOL_ERROR;
61 case SEC_E_ALGORITHM_MISMATCH:
62 return ERR_SSL_VERSION_OR_CIPHER_MISMATCH;
63 case SEC_E_INVALID_HANDLE:
64 return ERR_UNEXPECTED;
65 case SEC_E_OK:
66 return OK;
67 default:
68 LOG(WARNING) << "Unknown error " << err << " mapped to net::ERR_FAILED";
69 return ERR_FAILED;
70 }
71 }
72
73 // Map the errors in the chain_context->TrustStatus.dwErrorStatus returned by
74 // CertGetCertificateChain to our certificate status flags.
75 int MapCertChainErrorStatusToCertStatus(DWORD error_status) {
76 int cert_status = 0;
77
78 // CERT_TRUST_IS_NOT_TIME_NESTED means a subject certificate's time validity
79 // does not nest correctly within its issuer's time validity.
80 const DWORD kDateInvalidErrors = CERT_TRUST_IS_NOT_TIME_VALID |
81 CERT_TRUST_IS_NOT_TIME_NESTED |
82 CERT_TRUST_CTL_IS_NOT_TIME_VALID;
83 if (error_status & kDateInvalidErrors)
84 cert_status |= CERT_STATUS_DATE_INVALID;
85
86 const DWORD kAuthorityInvalidErrors = CERT_TRUST_IS_UNTRUSTED_ROOT |
87 CERT_TRUST_IS_EXPLICIT_DISTRUST |
88 CERT_TRUST_IS_PARTIAL_CHAIN;
89 if (error_status & kAuthorityInvalidErrors)
90 cert_status |= CERT_STATUS_AUTHORITY_INVALID;
91
92 if ((error_status & CERT_TRUST_REVOCATION_STATUS_UNKNOWN) &&
93 !(error_status & CERT_TRUST_IS_OFFLINE_REVOCATION))
94 cert_status |= CERT_STATUS_NO_REVOCATION_MECHANISM;
95
96 if (error_status & CERT_TRUST_IS_OFFLINE_REVOCATION)
97 cert_status |= CERT_STATUS_UNABLE_TO_CHECK_REVOCATION;
98
99 if (error_status & CERT_TRUST_IS_REVOKED)
100 cert_status |= CERT_STATUS_REVOKED;
101
102 const DWORD kWrongUsageErrors = CERT_TRUST_IS_NOT_VALID_FOR_USAGE |
103 CERT_TRUST_CTL_IS_NOT_VALID_FOR_USAGE;
104 if (error_status & kWrongUsageErrors) {
105 // TODO(wtc): Should we add CERT_STATUS_WRONG_USAGE?
106 cert_status |= CERT_STATUS_INVALID;
107 }
108
109 // The rest of the errors.
110 const DWORD kCertInvalidErrors =
111 CERT_TRUST_IS_NOT_SIGNATURE_VALID |
112 CERT_TRUST_IS_CYCLIC |
113 CERT_TRUST_INVALID_EXTENSION |
114 CERT_TRUST_INVALID_POLICY_CONSTRAINTS |
115 CERT_TRUST_INVALID_BASIC_CONSTRAINTS |
116 CERT_TRUST_INVALID_NAME_CONSTRAINTS |
117 CERT_TRUST_CTL_IS_NOT_SIGNATURE_VALID |
118 CERT_TRUST_HAS_NOT_SUPPORTED_NAME_CONSTRAINT |
119 CERT_TRUST_HAS_NOT_DEFINED_NAME_CONSTRAINT |
120 CERT_TRUST_HAS_NOT_PERMITTED_NAME_CONSTRAINT |
121 CERT_TRUST_HAS_EXCLUDED_NAME_CONSTRAINT |
122 CERT_TRUST_NO_ISSUANCE_CHAIN_POLICY |
123 CERT_TRUST_HAS_NOT_SUPPORTED_CRITICAL_EXT;
124 if (error_status & kCertInvalidErrors)
125 cert_status |= CERT_STATUS_INVALID;
126
127 return cert_status;
128 }
129
130 //-----------------------------------------------------------------------------
131
132 // Wrappers of malloc and free for CRYPT_DECODE_PARA, which requires the 22 // Wrappers of malloc and free for CRYPT_DECODE_PARA, which requires the
133 // WINAPI calling convention. 23 // WINAPI calling convention.
134 void* WINAPI MyCryptAlloc(size_t size) { 24 void* WINAPI MyCryptAlloc(size_t size) {
135 return malloc(size); 25 return malloc(size);
136 } 26 }
137 27
138 void WINAPI MyCryptFree(void* p) { 28 void WINAPI MyCryptFree(void* p) {
139 free(p); 29 free(p);
140 } 30 }
141 31
(...skipping 19 matching lines...) Expand all
161 extension->Value.pbData, 51 extension->Value.pbData,
162 extension->Value.cbData, 52 extension->Value.cbData,
163 CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, 53 CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG,
164 &decode_para, 54 &decode_para,
165 &alt_name_info, 55 &alt_name_info,
166 &alt_name_info_size); 56 &alt_name_info_size);
167 if (rv) 57 if (rv)
168 output->reset(alt_name_info); 58 output->reset(alt_name_info);
169 } 59 }
170 60
171 // Returns true if any common name in the certificate's Subject field contains
172 // a NULL character.
173 bool CertSubjectCommonNameHasNull(PCCERT_CONTEXT cert) {
174 CRYPT_DECODE_PARA decode_para;
175 decode_para.cbSize = sizeof(decode_para);
176 decode_para.pfnAlloc = MyCryptAlloc;
177 decode_para.pfnFree = MyCryptFree;
178 CERT_NAME_INFO* name_info = NULL;
179 DWORD name_info_size = 0;
180 BOOL rv;
181 rv = CryptDecodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
182 X509_NAME,
183 cert->pCertInfo->Subject.pbData,
184 cert->pCertInfo->Subject.cbData,
185 CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG,
186 &decode_para,
187 &name_info,
188 &name_info_size);
189 if (rv) {
190 scoped_ptr_malloc<CERT_NAME_INFO> scoped_name_info(name_info);
191
192 // The Subject field may have multiple common names. According to the
193 // "PKI Layer Cake" paper, CryptoAPI uses every common name in the
194 // Subject field, so we inspect every common name.
195 //
196 // From RFC 5280:
197 // X520CommonName ::= CHOICE {
198 // teletexString TeletexString (SIZE (1..ub-common-name)),
199 // printableString PrintableString (SIZE (1..ub-common-name)),
200 // universalString UniversalString (SIZE (1..ub-common-name)),
201 // utf8String UTF8String (SIZE (1..ub-common-name)),
202 // bmpString BMPString (SIZE (1..ub-common-name)) }
203 //
204 // We also check IA5String and VisibleString.
205 for (DWORD i = 0; i < name_info->cRDN; ++i) {
206 PCERT_RDN rdn = &name_info->rgRDN[i];
207 for (DWORD j = 0; j < rdn->cRDNAttr; ++j) {
208 PCERT_RDN_ATTR rdn_attr = &rdn->rgRDNAttr[j];
209 if (strcmp(rdn_attr->pszObjId, szOID_COMMON_NAME) == 0) {
210 switch (rdn_attr->dwValueType) {
211 // After the CryptoAPI ASN.1 security vulnerabilities described in
212 // http://www.microsoft.com/technet/security/Bulletin/MS09-056.mspx
213 // were patched, we get CERT_RDN_ENCODED_BLOB for a common name
214 // that contains a NULL character.
215 case CERT_RDN_ENCODED_BLOB:
216 break;
217 // Array of 8-bit characters.
218 case CERT_RDN_PRINTABLE_STRING:
219 case CERT_RDN_TELETEX_STRING:
220 case CERT_RDN_IA5_STRING:
221 case CERT_RDN_VISIBLE_STRING:
222 for (DWORD k = 0; k < rdn_attr->Value.cbData; ++k) {
223 if (rdn_attr->Value.pbData[k] == '\0')
224 return true;
225 }
226 break;
227 // Array of 16-bit characters.
228 case CERT_RDN_BMP_STRING:
229 case CERT_RDN_UTF8_STRING: {
230 DWORD num_wchars = rdn_attr->Value.cbData / 2;
231 wchar_t* common_name =
232 reinterpret_cast<wchar_t*>(rdn_attr->Value.pbData);
233 for (DWORD k = 0; k < num_wchars; ++k) {
234 if (common_name[k] == L'\0')
235 return true;
236 }
237 break;
238 }
239 // Array of ints (32-bit).
240 case CERT_RDN_UNIVERSAL_STRING: {
241 DWORD num_ints = rdn_attr->Value.cbData / 4;
242 int* common_name =
243 reinterpret_cast<int*>(rdn_attr->Value.pbData);
244 for (DWORD k = 0; k < num_ints; ++k) {
245 if (common_name[k] == 0)
246 return true;
247 }
248 break;
249 }
250 default:
251 NOTREACHED();
252 break;
253 }
254 }
255 }
256 }
257 }
258 return false;
259 }
260
261 // Saves some information about the certificate chain chain_context in
262 // *verify_result. The caller MUST initialize *verify_result before calling
263 // this function.
264 void GetCertChainInfo(PCCERT_CHAIN_CONTEXT chain_context,
265 CertVerifyResult* verify_result) {
266 PCERT_SIMPLE_CHAIN first_chain = chain_context->rgpChain[0];
267 int num_elements = first_chain->cElement;
268 PCERT_CHAIN_ELEMENT* element = first_chain->rgpElement;
269
270 // Each chain starts with the end entity certificate (i = 0) and ends with
271 // the root CA certificate (i = num_elements - 1). Do not inspect the
272 // signature algorithm of the root CA certificate because the signature on
273 // the trust anchor is not important.
274 for (int i = 0; i < num_elements - 1; ++i) {
275 PCCERT_CONTEXT cert = element[i]->pCertContext;
276 const char* algorithm = cert->pCertInfo->SignatureAlgorithm.pszObjId;
277 if (strcmp(algorithm, szOID_RSA_MD5RSA) == 0) {
278 // md5WithRSAEncryption: 1.2.840.113549.1.1.4
279 verify_result->has_md5 = true;
280 if (i != 0)
281 verify_result->has_md5_ca = true;
282 } else if (strcmp(algorithm, szOID_RSA_MD2RSA) == 0) {
283 // md2WithRSAEncryption: 1.2.840.113549.1.1.2
284 verify_result->has_md2 = true;
285 if (i != 0)
286 verify_result->has_md2_ca = true;
287 } else if (strcmp(algorithm, szOID_RSA_MD4RSA) == 0) {
288 // md4WithRSAEncryption: 1.2.840.113549.1.1.3
289 verify_result->has_md4 = true;
290 }
291 }
292 }
293
294 ///////////////////////////////////////////////////////////////////////////
295 //
296 // Functions used by X509Certificate::IsEV
297 //
298 ///////////////////////////////////////////////////////////////////////////
299
300 // Constructs a certificate chain starting from the end certificate
301 // 'cert_context', matching any of the certificate policies.
302 //
303 // Returns the certificate chain context on success, or NULL on failure.
304 // The caller is responsible for freeing the certificate chain context with
305 // CertFreeCertificateChain.
306 PCCERT_CHAIN_CONTEXT ConstructCertChain(
307 PCCERT_CONTEXT cert_context,
308 const char* const* policies,
309 int num_policies) {
310 CERT_CHAIN_PARA chain_para;
311 memset(&chain_para, 0, sizeof(chain_para));
312 chain_para.cbSize = sizeof(chain_para);
313 chain_para.RequestedUsage.dwType = USAGE_MATCH_TYPE_AND;
314 chain_para.RequestedUsage.Usage.cUsageIdentifier = 0;
315 chain_para.RequestedUsage.Usage.rgpszUsageIdentifier = NULL; // LPSTR*
316 chain_para.RequestedIssuancePolicy.dwType = USAGE_MATCH_TYPE_OR;
317 chain_para.RequestedIssuancePolicy.Usage.cUsageIdentifier = num_policies;
318 chain_para.RequestedIssuancePolicy.Usage.rgpszUsageIdentifier =
319 const_cast<char**>(policies);
320 PCCERT_CHAIN_CONTEXT chain_context;
321 if (!CertGetCertificateChain(
322 NULL, // default chain engine, HCCE_CURRENT_USER
323 cert_context,
324 NULL, // current system time
325 cert_context->hCertStore, // search this store
326 &chain_para,
327 CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT |
328 CERT_CHAIN_CACHE_END_CERT,
329 NULL, // reserved
330 &chain_context)) {
331 return NULL;
332 }
333 return chain_context;
334 }
335
336 // Decodes the cert's certificatePolicies extension into a CERT_POLICIES_INFO
337 // structure and stores it in *output.
338 void GetCertPoliciesInfo(PCCERT_CONTEXT cert,
339 scoped_ptr_malloc<CERT_POLICIES_INFO>* output) {
340 PCERT_EXTENSION extension = CertFindExtension(szOID_CERT_POLICIES,
341 cert->pCertInfo->cExtension,
342 cert->pCertInfo->rgExtension);
343 if (!extension)
344 return;
345
346 CRYPT_DECODE_PARA decode_para;
347 decode_para.cbSize = sizeof(decode_para);
348 decode_para.pfnAlloc = MyCryptAlloc;
349 decode_para.pfnFree = MyCryptFree;
350 CERT_POLICIES_INFO* policies_info = NULL;
351 DWORD policies_info_size = 0;
352 BOOL rv;
353 rv = CryptDecodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
354 szOID_CERT_POLICIES,
355 extension->Value.pbData,
356 extension->Value.cbData,
357 CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG,
358 &decode_para,
359 &policies_info,
360 &policies_info_size);
361 if (rv)
362 output->reset(policies_info);
363 }
364
365 // Returns true if the policy is in the array of CERT_POLICY_INFO in
366 // the CERT_POLICIES_INFO structure.
367 bool ContainsPolicy(const CERT_POLICIES_INFO* policies_info,
368 const char* policy) {
369 int num_policies = policies_info->cPolicyInfo;
370 for (int i = 0; i < num_policies; i++) {
371 if (!strcmp(policies_info->rgPolicyInfo[i].pszPolicyIdentifier, policy))
372 return true;
373 }
374 return false;
375 }
376
377 // Helper function to parse a principal from a WinInet description of that 61 // Helper function to parse a principal from a WinInet description of that
378 // principal. 62 // principal.
379 void ParsePrincipal(const std::string& description, 63 void ParsePrincipal(const std::string& description,
380 CertPrincipal* principal) { 64 CertPrincipal* principal) {
381 // The description of the principal is a string with each LDAP value on 65 // The description of the principal is a string with each LDAP value on
382 // a separate line. 66 // a separate line.
383 const std::string kDelimiters("\r\n"); 67 const std::string kDelimiters("\r\n");
384 68
385 std::vector<std::string> common_names, locality_names, state_names, 69 std::vector<std::string> common_names, locality_names, state_names,
386 country_names; 70 country_names;
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
565 } 249 }
566 } 250 }
567 251
568 // Note: This does not actually close the store if |identity_cert| is 252 // Note: This does not actually close the store if |identity_cert| is
569 // valid, otherwise it will cleanly close it. 253 // valid, otherwise it will cleanly close it.
570 CertCloseStore(store, 0); 254 CertCloseStore(store, 0);
571 } 255 }
572 256
573 return cert_list; 257 return cert_list;
574 } 258 }
575
576 int X509Certificate::Verify(const std::string& hostname,
577 int flags,
578 CertVerifyResult* verify_result) const {
579 verify_result->Reset();
580 if (!cert_handle_)
581 return ERR_UNEXPECTED;
582
583 // Build and validate certificate chain.
584
585 CERT_CHAIN_PARA chain_para;
586 memset(&chain_para, 0, sizeof(chain_para));
587 chain_para.cbSize = sizeof(chain_para);
588 // ExtendedKeyUsage.
589 // We still need to request szOID_SERVER_GATED_CRYPTO and szOID_SGC_NETSCAPE
590 // today because some certificate chains need them. IE also requests these
591 // two usages.
592 static const LPSTR usage[] = {
593 szOID_PKIX_KP_SERVER_AUTH,
594 szOID_SERVER_GATED_CRYPTO,
595 szOID_SGC_NETSCAPE
596 };
597 chain_para.RequestedUsage.dwType = USAGE_MATCH_TYPE_OR;
598 chain_para.RequestedUsage.Usage.cUsageIdentifier = arraysize(usage);
599 chain_para.RequestedUsage.Usage.rgpszUsageIdentifier =
600 const_cast<LPSTR*>(usage);
601 // We can set CERT_CHAIN_RETURN_LOWER_QUALITY_CONTEXTS to get more chains.
602 DWORD chain_flags = CERT_CHAIN_CACHE_END_CERT;
603 if (flags & VERIFY_REV_CHECKING_ENABLED) {
604 verify_result->cert_status |= CERT_STATUS_REV_CHECKING_ENABLED;
605 chain_flags |= CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT;
606 } else {
607 chain_flags |= CERT_CHAIN_REVOCATION_CHECK_CACHE_ONLY;
608 // EV requires revocation checking.
609 flags &= ~VERIFY_EV_CERT;
610 }
611
612 OSCertListHandle cert_list = CreateOSCertListHandle();
613 PCCERT_CHAIN_CONTEXT chain_context;
614 // IE passes a non-NULL pTime argument that specifies the current system
615 // time. IE passes CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT as the
616 // chain_flags argument.
617 if (!CertGetCertificateChain(
618 NULL, // default chain engine, HCCE_CURRENT_USER
619 cert_list,
620 NULL, // current system time
621 cert_list->hCertStore, // search this store
622 &chain_para,
623 chain_flags,
624 NULL, // reserved
625 &chain_context)) {
626 FreeOSCertListHandle(cert_list);
627 return MapSecurityError(GetLastError());
628 }
629 FreeOSCertListHandle(cert_list);
630 ScopedCertChainContext scoped_chain_context(chain_context);
631
632 GetCertChainInfo(chain_context, verify_result);
633
634 verify_result->cert_status |= MapCertChainErrorStatusToCertStatus(
635 chain_context->TrustStatus.dwErrorStatus);
636
637 // Treat certificates signed using broken signature algorithms as invalid.
638 if (verify_result->has_md4)
639 verify_result->cert_status |= CERT_STATUS_INVALID;
640
641 // Flag certificates signed using weak signature algorithms.
642 if (verify_result->has_md2)
643 verify_result->cert_status |= CERT_STATUS_WEAK_SIGNATURE_ALGORITHM;
644
645 // Flag certificates that have a Subject common name with a NULL character.
646 if (CertSubjectCommonNameHasNull(cert_handle_))
647 verify_result->cert_status |= CERT_STATUS_INVALID;
648
649 std::wstring wstr_hostname = ASCIIToWide(hostname);
650
651 SSL_EXTRA_CERT_CHAIN_POLICY_PARA extra_policy_para;
652 memset(&extra_policy_para, 0, sizeof(extra_policy_para));
653 extra_policy_para.cbSize = sizeof(extra_policy_para);
654 extra_policy_para.dwAuthType = AUTHTYPE_SERVER;
655 extra_policy_para.fdwChecks = 0;
656 extra_policy_para.pwszServerName =
657 const_cast<wchar_t*>(wstr_hostname.c_str());
658
659 CERT_CHAIN_POLICY_PARA policy_para;
660 memset(&policy_para, 0, sizeof(policy_para));
661 policy_para.cbSize = sizeof(policy_para);
662 policy_para.dwFlags = 0;
663 policy_para.pvExtraPolicyPara = &extra_policy_para;
664
665 CERT_CHAIN_POLICY_STATUS policy_status;
666 memset(&policy_status, 0, sizeof(policy_status));
667 policy_status.cbSize = sizeof(policy_status);
668
669 if (!CertVerifyCertificateChainPolicy(
670 CERT_CHAIN_POLICY_SSL,
671 chain_context,
672 &policy_para,
673 &policy_status)) {
674 return MapSecurityError(GetLastError());
675 }
676
677 if (policy_status.dwError) {
678 verify_result->cert_status |= MapNetErrorToCertStatus(
679 MapSecurityError(policy_status.dwError));
680
681 // CertVerifyCertificateChainPolicy reports only one error (in
682 // policy_status.dwError) if the certificate has multiple errors.
683 // CertGetCertificateChain doesn't report certificate name mismatch, so
684 // CertVerifyCertificateChainPolicy is the only function that can report
685 // certificate name mismatch.
686 //
687 // To prevent a potential certificate name mismatch from being hidden by
688 // some other certificate error, if we get any other certificate error,
689 // we call CertVerifyCertificateChainPolicy again, ignoring all other
690 // certificate errors. Both extra_policy_para.fdwChecks and
691 // policy_para.dwFlags allow us to ignore certificate errors, so we set
692 // them both.
693 if (policy_status.dwError != CERT_E_CN_NO_MATCH) {
694 const DWORD extra_ignore_flags =
695 0x00000080 | // SECURITY_FLAG_IGNORE_REVOCATION
696 0x00000100 | // SECURITY_FLAG_IGNORE_UNKNOWN_CA
697 0x00002000 | // SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
698 0x00000200; // SECURITY_FLAG_IGNORE_WRONG_USAGE
699 extra_policy_para.fdwChecks = extra_ignore_flags;
700 const DWORD ignore_flags =
701 CERT_CHAIN_POLICY_IGNORE_ALL_NOT_TIME_VALID_FLAGS |
702 CERT_CHAIN_POLICY_IGNORE_INVALID_BASIC_CONSTRAINTS_FLAG |
703 CERT_CHAIN_POLICY_ALLOW_UNKNOWN_CA_FLAG |
704 CERT_CHAIN_POLICY_IGNORE_WRONG_USAGE_FLAG |
705 CERT_CHAIN_POLICY_IGNORE_INVALID_NAME_FLAG |
706 CERT_CHAIN_POLICY_IGNORE_INVALID_POLICY_FLAG |
707 CERT_CHAIN_POLICY_IGNORE_ALL_REV_UNKNOWN_FLAGS |
708 CERT_CHAIN_POLICY_ALLOW_TESTROOT_FLAG |
709 CERT_CHAIN_POLICY_TRUST_TESTROOT_FLAG |
710 CERT_CHAIN_POLICY_IGNORE_NOT_SUPPORTED_CRITICAL_EXT_FLAG |
711 CERT_CHAIN_POLICY_IGNORE_PEER_TRUST_FLAG;
712 policy_para.dwFlags = ignore_flags;
713 if (!CertVerifyCertificateChainPolicy(
714 CERT_CHAIN_POLICY_SSL,
715 chain_context,
716 &policy_para,
717 &policy_status)) {
718 return MapSecurityError(GetLastError());
719 }
720 if (policy_status.dwError) {
721 verify_result->cert_status |= MapNetErrorToCertStatus(
722 MapSecurityError(policy_status.dwError));
723 }
724 }
725 }
726
727 // TODO(wtc): Suppress CERT_STATUS_NO_REVOCATION_MECHANISM for now to be
728 // compatible with WinHTTP, which doesn't report this error (bug 3004).
729 verify_result->cert_status &= ~CERT_STATUS_NO_REVOCATION_MECHANISM;
730
731 if (IsCertStatusError(verify_result->cert_status))
732 return MapCertStatusToNetError(verify_result->cert_status);
733
734 // TODO(ukai): combine regular cert verification and EV cert verification.
735 if ((flags & VERIFY_EV_CERT) && VerifyEV())
736 verify_result->cert_status |= CERT_STATUS_IS_EV;
737 return OK;
738 }
739
740 // Returns true if the certificate is an extended-validation certificate.
741 //
742 // This function checks the certificatePolicies extensions of the
743 // certificates in the certificate chain according to Section 7 (pp. 11-12)
744 // of the EV Certificate Guidelines Version 1.0 at
745 // http://cabforum.org/EV_Certificate_Guidelines.pdf.
746 bool X509Certificate::VerifyEV() const {
747 DCHECK(cert_handle_);
748 net::EVRootCAMetadata* metadata = net::EVRootCAMetadata::GetInstance();
749
750 OSCertListHandle cert_list = CreateOSCertListHandle();
751 PCCERT_CHAIN_CONTEXT chain_context = ConstructCertChain(cert_list,
752 metadata->GetPolicyOIDs(), metadata->NumPolicyOIDs());
753 FreeOSCertListHandle(cert_list);
754 if (!chain_context)
755 return false;
756 ScopedCertChainContext scoped_chain_context(chain_context);
757
758 DCHECK(chain_context->cChain != 0);
759 // If the cert doesn't match any of the policies, the
760 // CERT_TRUST_IS_NOT_VALID_FOR_USAGE bit (0x10) in
761 // chain_context->TrustStatus.dwErrorStatus is set.
762 DWORD error_status = chain_context->TrustStatus.dwErrorStatus;
763 DWORD info_status = chain_context->TrustStatus.dwInfoStatus;
764 if (!chain_context->cChain || error_status != CERT_TRUST_NO_ERROR)
765 return false;
766
767 // Check the end certificate simple chain (chain_context->rgpChain[0]).
768 // If the end certificate's certificatePolicies extension contains the
769 // EV policy OID of the root CA, return true.
770 PCERT_CHAIN_ELEMENT* element = chain_context->rgpChain[0]->rgpElement;
771 int num_elements = chain_context->rgpChain[0]->cElement;
772 if (num_elements < 2)
773 return false;
774
775 // Look up the EV policy OID of the root CA.
776 PCCERT_CONTEXT root_cert = element[num_elements - 1]->pCertContext;
777 SHA1Fingerprint fingerprint = CalculateFingerprint(root_cert);
778 const char* ev_policy_oid = NULL;
779 if (!metadata->GetPolicyOID(fingerprint, &ev_policy_oid))
780 return false;
781 DCHECK(ev_policy_oid);
782
783 // Get the certificatePolicies extension of the end certificate.
784 PCCERT_CONTEXT end_cert = element[0]->pCertContext;
785 scoped_ptr_malloc<CERT_POLICIES_INFO> policies_info;
786 GetCertPoliciesInfo(end_cert, &policies_info);
787 if (!policies_info.get())
788 return false;
789
790 return ContainsPolicy(policies_info.get(), ev_policy_oid);
791 }
792
793 // static 259 // static
794 bool X509Certificate::IsSameOSCert(X509Certificate::OSCertHandle a, 260 bool X509Certificate::IsSameOSCert(X509Certificate::OSCertHandle a,
795 X509Certificate::OSCertHandle b) { 261 X509Certificate::OSCertHandle b) {
796 DCHECK(a && b); 262 DCHECK(a && b);
797 if (a == b) 263 if (a == b)
798 return true; 264 return true;
799 return a->cbCertEncoded == b->cbCertEncoded && 265 return a->cbCertEncoded == b->cbCertEncoded &&
800 memcmp(a->pbCertEncoded, b->pbCertEncoded, a->cbCertEncoded) == 0; 266 memcmp(a->pbCertEncoded, b->pbCertEncoded, a->cbCertEncoded) == 0;
801 } 267 }
802 268
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
909 buffer.resize(length); 375 buffer.resize(length);
910 if (!CertSerializeCertificateStoreElement(cert_handle, 0, &buffer[0], 376 if (!CertSerializeCertificateStoreElement(cert_handle, 0, &buffer[0],
911 &length)) 377 &length))
912 return false; 378 return false;
913 379
914 return pickle->WriteData(reinterpret_cast<const char*>(&buffer[0]), 380 return pickle->WriteData(reinterpret_cast<const char*>(&buffer[0]),
915 length); 381 length);
916 } 382 }
917 383
918 } // namespace net 384 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698