Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2010 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/base/x509_chain.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/utf_string_conversions.h" | |
| 9 #include "net/base/cert_status_flags.h" | |
| 10 #include "net/base/cert_verify_result.h" | |
| 11 #include "net/base/ev_root_ca_metadata.h" | |
| 12 #include "net/base/net_errors.h" | |
| 13 #include "net/base/scoped_cert_chain_context.h" | |
| 14 #include "net/base/x509_certificate.h" | |
| 15 | |
| 16 #pragma comment(lib, "crypt32.lib") | |
| 17 | |
| 18 namespace net { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 //----------------------------------------------------------------------------- | |
| 23 | |
| 24 // TODO(wtc): This is a copy of the MapSecurityError function in | |
| 25 // ssl_client_socket_win.cc. Another function that maps Windows error codes | |
| 26 // to our network error codes is WinInetUtil::OSErrorToNetError. We should | |
| 27 // eliminate the code duplication. | |
| 28 int MapSecurityError(SECURITY_STATUS err) { | |
| 29 // There are numerous security error codes, but these are the ones we thus | |
| 30 // far find interesting. | |
| 31 switch (err) { | |
| 32 case SEC_E_WRONG_PRINCIPAL: // Schannel | |
| 33 case CERT_E_CN_NO_MATCH: // CryptoAPI | |
| 34 return ERR_CERT_COMMON_NAME_INVALID; | |
| 35 case SEC_E_UNTRUSTED_ROOT: // Schannel | |
| 36 case CERT_E_UNTRUSTEDROOT: // CryptoAPI | |
| 37 return ERR_CERT_AUTHORITY_INVALID; | |
| 38 case SEC_E_CERT_EXPIRED: // Schannel | |
| 39 case CERT_E_EXPIRED: // CryptoAPI | |
| 40 return ERR_CERT_DATE_INVALID; | |
| 41 case CRYPT_E_NO_REVOCATION_CHECK: | |
| 42 return ERR_CERT_NO_REVOCATION_MECHANISM; | |
| 43 case CRYPT_E_REVOCATION_OFFLINE: | |
| 44 return ERR_CERT_UNABLE_TO_CHECK_REVOCATION; | |
| 45 case CRYPT_E_REVOKED: // Schannel and CryptoAPI | |
| 46 return ERR_CERT_REVOKED; | |
| 47 case SEC_E_CERT_UNKNOWN: | |
| 48 case CERT_E_ROLE: | |
| 49 return ERR_CERT_INVALID; | |
| 50 case CERT_E_WRONG_USAGE: | |
| 51 // TODO(wtc): Should we add ERR_CERT_WRONG_USAGE? | |
| 52 return ERR_CERT_INVALID; | |
| 53 // We received an unexpected_message or illegal_parameter alert message | |
| 54 // from the server. | |
| 55 case SEC_E_ILLEGAL_MESSAGE: | |
| 56 return ERR_SSL_PROTOCOL_ERROR; | |
| 57 case SEC_E_ALGORITHM_MISMATCH: | |
| 58 return ERR_SSL_VERSION_OR_CIPHER_MISMATCH; | |
| 59 case SEC_E_INVALID_HANDLE: | |
| 60 return ERR_UNEXPECTED; | |
| 61 case SEC_E_OK: | |
| 62 return OK; | |
| 63 default: | |
| 64 LOG(WARNING) << "Unknown error " << err << " mapped to net::ERR_FAILED"; | |
| 65 return ERR_FAILED; | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 // Map the errors in the chain_context->TrustStatus.dwErrorStatus returned by | |
| 70 // CertGetCertificateChain to our certificate status flags. | |
| 71 int MapCertChainErrorStatusToCertStatus(DWORD error_status) { | |
| 72 int cert_status = 0; | |
| 73 | |
| 74 // CERT_TRUST_IS_NOT_TIME_NESTED means a subject certificate's time validity | |
| 75 // does not nest correctly within its issuer's time validity. | |
| 76 const DWORD kDateInvalidErrors = CERT_TRUST_IS_NOT_TIME_VALID | | |
| 77 CERT_TRUST_IS_NOT_TIME_NESTED | | |
| 78 CERT_TRUST_CTL_IS_NOT_TIME_VALID; | |
| 79 if (error_status & kDateInvalidErrors) | |
| 80 cert_status |= CERT_STATUS_DATE_INVALID; | |
| 81 | |
| 82 const DWORD kAuthorityInvalidErrors = CERT_TRUST_IS_UNTRUSTED_ROOT | | |
| 83 CERT_TRUST_IS_EXPLICIT_DISTRUST | | |
| 84 CERT_TRUST_IS_PARTIAL_CHAIN; | |
| 85 if (error_status & kAuthorityInvalidErrors) | |
| 86 cert_status |= CERT_STATUS_AUTHORITY_INVALID; | |
| 87 | |
| 88 if ((error_status & CERT_TRUST_REVOCATION_STATUS_UNKNOWN) && | |
| 89 !(error_status & CERT_TRUST_IS_OFFLINE_REVOCATION)) | |
| 90 cert_status |= CERT_STATUS_NO_REVOCATION_MECHANISM; | |
| 91 | |
| 92 if (error_status & CERT_TRUST_IS_OFFLINE_REVOCATION) | |
| 93 cert_status |= CERT_STATUS_UNABLE_TO_CHECK_REVOCATION; | |
| 94 | |
| 95 if (error_status & CERT_TRUST_IS_REVOKED) | |
| 96 cert_status |= CERT_STATUS_REVOKED; | |
| 97 | |
| 98 const DWORD kWrongUsageErrors = CERT_TRUST_IS_NOT_VALID_FOR_USAGE | | |
| 99 CERT_TRUST_CTL_IS_NOT_VALID_FOR_USAGE; | |
| 100 if (error_status & kWrongUsageErrors) { | |
| 101 // TODO(wtc): Should we add CERT_STATUS_WRONG_USAGE? | |
| 102 cert_status |= CERT_STATUS_INVALID; | |
| 103 } | |
| 104 | |
| 105 // The rest of the errors. | |
| 106 const DWORD kCertInvalidErrors = | |
| 107 CERT_TRUST_IS_NOT_SIGNATURE_VALID | | |
| 108 CERT_TRUST_IS_CYCLIC | | |
| 109 CERT_TRUST_INVALID_EXTENSION | | |
| 110 CERT_TRUST_INVALID_POLICY_CONSTRAINTS | | |
| 111 CERT_TRUST_INVALID_BASIC_CONSTRAINTS | | |
| 112 CERT_TRUST_INVALID_NAME_CONSTRAINTS | | |
| 113 CERT_TRUST_CTL_IS_NOT_SIGNATURE_VALID | | |
| 114 CERT_TRUST_HAS_NOT_SUPPORTED_NAME_CONSTRAINT | | |
| 115 CERT_TRUST_HAS_NOT_DEFINED_NAME_CONSTRAINT | | |
| 116 CERT_TRUST_HAS_NOT_PERMITTED_NAME_CONSTRAINT | | |
| 117 CERT_TRUST_HAS_EXCLUDED_NAME_CONSTRAINT | | |
| 118 CERT_TRUST_NO_ISSUANCE_CHAIN_POLICY | | |
| 119 CERT_TRUST_HAS_NOT_SUPPORTED_CRITICAL_EXT; | |
| 120 if (error_status & kCertInvalidErrors) | |
| 121 cert_status |= CERT_STATUS_INVALID; | |
| 122 | |
| 123 return cert_status; | |
| 124 } | |
| 125 | |
| 126 //----------------------------------------------------------------------------- | |
| 127 | |
| 128 // Wrappers of malloc and free for CRYPT_DECODE_PARA, which requires the | |
| 129 // WINAPI calling convention. | |
| 130 void* WINAPI MyCryptAlloc(size_t size) { | |
| 131 return malloc(size); | |
| 132 } | |
| 133 | |
| 134 void WINAPI MyCryptFree(void* p) { | |
| 135 free(p); | |
| 136 } | |
| 137 | |
| 138 // Decodes the cert's subjectAltName extension into a CERT_ALT_NAME_INFO | |
| 139 // structure and stores it in *output. | |
| 140 void GetCertSubjectAltName(PCCERT_CONTEXT cert, | |
| 141 scoped_ptr_malloc<CERT_ALT_NAME_INFO>* output) { | |
| 142 PCERT_EXTENSION extension = CertFindExtension(szOID_SUBJECT_ALT_NAME2, | |
| 143 cert->pCertInfo->cExtension, | |
| 144 cert->pCertInfo->rgExtension); | |
| 145 if (!extension) | |
| 146 return; | |
| 147 | |
| 148 CRYPT_DECODE_PARA decode_para; | |
| 149 decode_para.cbSize = sizeof(decode_para); | |
| 150 decode_para.pfnAlloc = MyCryptAlloc; | |
| 151 decode_para.pfnFree = MyCryptFree; | |
| 152 CERT_ALT_NAME_INFO* alt_name_info = NULL; | |
| 153 DWORD alt_name_info_size = 0; | |
| 154 BOOL rv; | |
| 155 rv = CryptDecodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, | |
| 156 szOID_SUBJECT_ALT_NAME2, | |
| 157 extension->Value.pbData, | |
| 158 extension->Value.cbData, | |
| 159 CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, | |
| 160 &decode_para, | |
| 161 &alt_name_info, | |
| 162 &alt_name_info_size); | |
| 163 if (rv) | |
| 164 output->reset(alt_name_info); | |
| 165 } | |
| 166 | |
| 167 // Returns true if any common name in the certificate's Subject field contains | |
| 168 // a NULL character. | |
| 169 bool CertSubjectCommonNameHasNull(PCCERT_CONTEXT cert) { | |
| 170 CRYPT_DECODE_PARA decode_para; | |
| 171 decode_para.cbSize = sizeof(decode_para); | |
| 172 decode_para.pfnAlloc = MyCryptAlloc; | |
| 173 decode_para.pfnFree = MyCryptFree; | |
| 174 CERT_NAME_INFO* name_info = NULL; | |
| 175 DWORD name_info_size = 0; | |
| 176 BOOL rv; | |
| 177 rv = CryptDecodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, | |
| 178 X509_NAME, | |
| 179 cert->pCertInfo->Subject.pbData, | |
| 180 cert->pCertInfo->Subject.cbData, | |
| 181 CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, | |
| 182 &decode_para, | |
| 183 &name_info, | |
| 184 &name_info_size); | |
| 185 if (rv) { | |
| 186 scoped_ptr_malloc<CERT_NAME_INFO> scoped_name_info(name_info); | |
| 187 | |
| 188 // The Subject field may have multiple common names. According to the | |
| 189 // "PKI Layer Cake" paper, CryptoAPI uses every common name in the | |
| 190 // Subject field, so we inspect every common name. | |
| 191 // | |
| 192 // From RFC 5280: | |
| 193 // X520CommonName ::= CHOICE { | |
| 194 // teletexString TeletexString (SIZE (1..ub-common-name)), | |
| 195 // printableString PrintableString (SIZE (1..ub-common-name)), | |
| 196 // universalString UniversalString (SIZE (1..ub-common-name)), | |
| 197 // utf8String UTF8String (SIZE (1..ub-common-name)), | |
| 198 // bmpString BMPString (SIZE (1..ub-common-name)) } | |
| 199 // | |
| 200 // We also check IA5String and VisibleString. | |
| 201 for (DWORD i = 0; i < name_info->cRDN; ++i) { | |
| 202 PCERT_RDN rdn = &name_info->rgRDN[i]; | |
| 203 for (DWORD j = 0; j < rdn->cRDNAttr; ++j) { | |
| 204 PCERT_RDN_ATTR rdn_attr = &rdn->rgRDNAttr[j]; | |
| 205 if (strcmp(rdn_attr->pszObjId, szOID_COMMON_NAME) == 0) { | |
| 206 switch (rdn_attr->dwValueType) { | |
| 207 // After the CryptoAPI ASN.1 security vulnerabilities described in | |
| 208 // http://www.microsoft.com/technet/security/Bulletin/MS09-056.mspx | |
| 209 // were patched, we get CERT_RDN_ENCODED_BLOB for a common name | |
| 210 // that contains a NULL character. | |
| 211 case CERT_RDN_ENCODED_BLOB: | |
| 212 break; | |
| 213 // Array of 8-bit characters. | |
| 214 case CERT_RDN_PRINTABLE_STRING: | |
| 215 case CERT_RDN_TELETEX_STRING: | |
| 216 case CERT_RDN_IA5_STRING: | |
| 217 case CERT_RDN_VISIBLE_STRING: | |
| 218 for (DWORD k = 0; k < rdn_attr->Value.cbData; ++k) { | |
| 219 if (rdn_attr->Value.pbData[k] == '\0') | |
| 220 return true; | |
| 221 } | |
| 222 break; | |
| 223 // Array of 16-bit characters. | |
| 224 case CERT_RDN_BMP_STRING: | |
| 225 case CERT_RDN_UTF8_STRING: { | |
| 226 DWORD num_wchars = rdn_attr->Value.cbData / 2; | |
|
bulach
2010/10/21 10:21:33
nit: const DWORD kNumWChars
| |
| 227 wchar_t* common_name = | |
| 228 reinterpret_cast<wchar_t*>(rdn_attr->Value.pbData); | |
| 229 for (DWORD k = 0; k < num_wchars; ++k) { | |
| 230 if (common_name[k] == L'\0') | |
| 231 return true; | |
| 232 } | |
| 233 break; | |
| 234 } | |
| 235 // Array of ints (32-bit). | |
| 236 case CERT_RDN_UNIVERSAL_STRING: { | |
| 237 DWORD num_ints = rdn_attr->Value.cbData / 4; | |
|
bulach
2010/10/21 10:21:33
nit: const DWORD kNumInts
| |
| 238 int* common_name = | |
| 239 reinterpret_cast<int*>(rdn_attr->Value.pbData); | |
| 240 for (DWORD k = 0; k < num_ints; ++k) { | |
| 241 if (common_name[k] == 0) | |
| 242 return true; | |
| 243 } | |
| 244 break; | |
| 245 } | |
| 246 default: | |
| 247 NOTREACHED(); | |
| 248 break; | |
| 249 } | |
| 250 } | |
| 251 } | |
| 252 } | |
| 253 } | |
| 254 return false; | |
| 255 } | |
| 256 | |
| 257 // Saves some information about the certificate chain chain_context in | |
| 258 // *verify_result. The caller MUST initialize *verify_result before calling | |
| 259 // this function. | |
| 260 void GetCertChainInfo(PCCERT_CHAIN_CONTEXT chain_context, | |
| 261 CertVerifyResult* verify_result) { | |
| 262 PCERT_SIMPLE_CHAIN first_chain = chain_context->rgpChain[0]; | |
| 263 int num_elements = first_chain->cElement; | |
|
bulach
2010/10/21 10:21:33
nit: const kNumElements
| |
| 264 PCERT_CHAIN_ELEMENT* element = first_chain->rgpElement; | |
| 265 | |
| 266 // Each chain starts with the end entity certificate (i = 0) and ends with | |
| 267 // the root CA certificate (i = num_elements - 1). Do not inspect the | |
| 268 // signature algorithm of the root CA certificate because the signature on | |
| 269 // the trust anchor is not important. | |
| 270 for (int i = 0; i < num_elements - 1; ++i) { | |
| 271 PCCERT_CONTEXT cert = element[i]->pCertContext; | |
| 272 const char* algorithm = cert->pCertInfo->SignatureAlgorithm.pszObjId; | |
| 273 if (strcmp(algorithm, szOID_RSA_MD5RSA) == 0) { | |
| 274 // md5WithRSAEncryption: 1.2.840.113549.1.1.4 | |
| 275 verify_result->has_md5 = true; | |
| 276 if (i != 0) | |
| 277 verify_result->has_md5_ca = true; | |
| 278 } else if (strcmp(algorithm, szOID_RSA_MD2RSA) == 0) { | |
| 279 // md2WithRSAEncryption: 1.2.840.113549.1.1.2 | |
| 280 verify_result->has_md2 = true; | |
| 281 if (i != 0) | |
| 282 verify_result->has_md2_ca = true; | |
| 283 } else if (strcmp(algorithm, szOID_RSA_MD4RSA) == 0) { | |
| 284 // md4WithRSAEncryption: 1.2.840.113549.1.1.3 | |
| 285 verify_result->has_md4 = true; | |
| 286 } | |
| 287 } | |
| 288 } | |
| 289 | |
| 290 /////////////////////////////////////////////////////////////////////////// | |
| 291 // | |
| 292 // Functions used by X509Certificate::IsEV | |
| 293 // | |
| 294 /////////////////////////////////////////////////////////////////////////// | |
| 295 | |
| 296 // Constructs a certificate chain starting from the end certificate | |
| 297 // 'cert_context', matching any of the certificate policies. | |
| 298 // | |
| 299 // Returns the certificate chain context on success, or NULL on failure. | |
| 300 // The caller is responsible for freeing the certificate chain context with | |
| 301 // CertFreeCertificateChain. | |
| 302 PCCERT_CHAIN_CONTEXT ConstructCertChain( | |
| 303 PCCERT_CONTEXT cert_context, | |
| 304 const char* const* policies, | |
| 305 int num_policies) { | |
| 306 CERT_CHAIN_PARA chain_para; | |
|
bulach
2010/10/21 10:21:33
can remove the memset with:
CERT_CHAIN_PARA chain
| |
| 307 memset(&chain_para, 0, sizeof(chain_para)); | |
| 308 chain_para.cbSize = sizeof(chain_para); | |
| 309 chain_para.RequestedUsage.dwType = USAGE_MATCH_TYPE_AND; | |
| 310 chain_para.RequestedUsage.Usage.cUsageIdentifier = 0; | |
| 311 chain_para.RequestedUsage.Usage.rgpszUsageIdentifier = NULL; // LPSTR* | |
| 312 chain_para.RequestedIssuancePolicy.dwType = USAGE_MATCH_TYPE_OR; | |
| 313 chain_para.RequestedIssuancePolicy.Usage.cUsageIdentifier = num_policies; | |
| 314 chain_para.RequestedIssuancePolicy.Usage.rgpszUsageIdentifier = | |
| 315 const_cast<char**>(policies); | |
| 316 PCCERT_CHAIN_CONTEXT chain_context; | |
| 317 if (!CertGetCertificateChain( | |
| 318 NULL, // default chain engine, HCCE_CURRENT_USER | |
| 319 cert_context, | |
| 320 NULL, // current system time | |
| 321 cert_context->hCertStore, // search this store | |
| 322 &chain_para, | |
| 323 CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT | | |
| 324 CERT_CHAIN_CACHE_END_CERT, | |
| 325 NULL, // reserved | |
| 326 &chain_context)) { | |
| 327 return NULL; | |
| 328 } | |
| 329 return chain_context; | |
| 330 } | |
| 331 | |
| 332 // Decodes the cert's certificatePolicies extension into a CERT_POLICIES_INFO | |
| 333 // structure and stores it in *output. | |
| 334 void GetCertPoliciesInfo(PCCERT_CONTEXT cert, | |
| 335 scoped_ptr_malloc<CERT_POLICIES_INFO>* output) { | |
| 336 PCERT_EXTENSION extension = CertFindExtension(szOID_CERT_POLICIES, | |
| 337 cert->pCertInfo->cExtension, | |
| 338 cert->pCertInfo->rgExtension); | |
| 339 if (!extension) | |
| 340 return; | |
| 341 | |
| 342 CRYPT_DECODE_PARA decode_para; | |
| 343 decode_para.cbSize = sizeof(decode_para); | |
| 344 decode_para.pfnAlloc = MyCryptAlloc; | |
| 345 decode_para.pfnFree = MyCryptFree; | |
| 346 CERT_POLICIES_INFO* policies_info = NULL; | |
| 347 DWORD policies_info_size = 0; | |
| 348 BOOL rv; | |
| 349 rv = CryptDecodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, | |
| 350 szOID_CERT_POLICIES, | |
| 351 extension->Value.pbData, | |
| 352 extension->Value.cbData, | |
| 353 CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, | |
| 354 &decode_para, | |
| 355 &policies_info, | |
| 356 &policies_info_size); | |
| 357 if (rv) | |
| 358 output->reset(policies_info); | |
| 359 } | |
| 360 | |
| 361 // Returns true if the policy is in the array of CERT_POLICY_INFO in | |
| 362 // the CERT_POLICIES_INFO structure. | |
| 363 bool ContainsPolicy(const CERT_POLICIES_INFO* policies_info, | |
| 364 const char* policy) { | |
| 365 int num_policies = policies_info->cPolicyInfo; | |
| 366 for (int i = 0; i < num_policies; i++) { | |
|
bulach
2010/10/21 10:21:33
nit: ++i
| |
| 367 if (!strcmp(policies_info->rgPolicyInfo[i].pszPolicyIdentifier, policy)) | |
| 368 return true; | |
| 369 } | |
| 370 return false; | |
| 371 } | |
| 372 | |
| 373 | |
| 374 // Returns true if the certificate is an extended-validation certificate. | |
| 375 // | |
| 376 // This function checks the certificatePolicies extensions of the | |
| 377 // certificates in the certificate chain according to Section 7 (pp. 11-12) | |
| 378 // of the EV Certificate Guidelines Version 1.0 at | |
| 379 // http://cabforum.org/EV_Certificate_Guidelines.pdf. | |
| 380 bool VerifyEV(X509Certificate* certificate) { | |
| 381 DCHECK(certificate); | |
| 382 net::EVRootCAMetadata* metadata = net::EVRootCAMetadata::GetInstance(); | |
| 383 | |
| 384 X509Certificate::OSCertListHandle cert_list = | |
| 385 certificate->CreateOSCertListHandle(); | |
| 386 PCCERT_CHAIN_CONTEXT chain_context = ConstructCertChain(cert_list, | |
| 387 metadata->GetPolicyOIDs(), metadata->NumPolicyOIDs()); | |
| 388 X509Certificate::FreeOSCertListHandle(cert_list); | |
| 389 if (!chain_context) | |
| 390 return false; | |
| 391 ScopedCertChainContext scoped_chain_context(chain_context); | |
| 392 | |
| 393 DCHECK(chain_context->cChain != 0); | |
| 394 // If the cert doesn't match any of the policies, the | |
| 395 // CERT_TRUST_IS_NOT_VALID_FOR_USAGE bit (0x10) in | |
| 396 // chain_context->TrustStatus.dwErrorStatus is set. | |
| 397 DWORD error_status = chain_context->TrustStatus.dwErrorStatus; | |
| 398 DWORD info_status = chain_context->TrustStatus.dwInfoStatus; | |
| 399 if (!chain_context->cChain || error_status != CERT_TRUST_NO_ERROR) | |
| 400 return false; | |
| 401 | |
| 402 // Check the end certificate simple chain (chain_context->rgpChain[0]). | |
| 403 // If the end certificate's certificatePolicies extension contains the | |
| 404 // EV policy OID of the root CA, return true. | |
| 405 PCERT_CHAIN_ELEMENT* element = chain_context->rgpChain[0]->rgpElement; | |
| 406 int num_elements = chain_context->rgpChain[0]->cElement; | |
| 407 if (num_elements < 2) | |
| 408 return false; | |
| 409 | |
| 410 // Look up the EV policy OID of the root CA. | |
| 411 PCCERT_CONTEXT root_cert = element[num_elements - 1]->pCertContext; | |
| 412 SHA1Fingerprint fingerprint = | |
| 413 X509Certificate::CalculateFingerprint(root_cert); | |
| 414 const char* ev_policy_oid = NULL; | |
| 415 if (!metadata->GetPolicyOID(fingerprint, &ev_policy_oid)) | |
| 416 return false; | |
| 417 DCHECK(ev_policy_oid); | |
| 418 | |
| 419 // Get the certificatePolicies extension of the end certificate. | |
| 420 PCCERT_CONTEXT end_cert = element[0]->pCertContext; | |
| 421 scoped_ptr_malloc<CERT_POLICIES_INFO> policies_info; | |
| 422 GetCertPoliciesInfo(end_cert, &policies_info); | |
| 423 if (!policies_info.get()) | |
| 424 return false; | |
| 425 | |
| 426 return ContainsPolicy(policies_info.get(), ev_policy_oid); | |
| 427 } | |
| 428 | |
| 429 } // namespace | |
| 430 | |
| 431 namespace x509_chain { | |
| 432 | |
| 433 int VerifySSLServer(X509Certificate* certificate, const std::string& hostname, | |
| 434 int flags, CertVerifyResult* verify_result) { | |
| 435 verify_result->Reset(); | |
| 436 if (!certificate || !certificate->os_cert_handle()) | |
| 437 return ERR_UNEXPECTED; | |
| 438 | |
| 439 // Build and validate certificate chain. | |
| 440 | |
| 441 CERT_CHAIN_PARA chain_para; | |
| 442 memset(&chain_para, 0, sizeof(chain_para)); | |
|
bulach
2010/10/21 10:21:33
ditto, = {0};
| |
| 443 chain_para.cbSize = sizeof(chain_para); | |
| 444 // ExtendedKeyUsage. | |
| 445 // We still need to request szOID_SERVER_GATED_CRYPTO and szOID_SGC_NETSCAPE | |
| 446 // today because some certificate chains need them. IE also requests these | |
| 447 // two usages. | |
| 448 static const LPSTR usage[] = { | |
| 449 szOID_PKIX_KP_SERVER_AUTH, | |
| 450 szOID_SERVER_GATED_CRYPTO, | |
| 451 szOID_SGC_NETSCAPE | |
| 452 }; | |
| 453 chain_para.RequestedUsage.dwType = USAGE_MATCH_TYPE_OR; | |
| 454 chain_para.RequestedUsage.Usage.cUsageIdentifier = arraysize(usage); | |
| 455 chain_para.RequestedUsage.Usage.rgpszUsageIdentifier = | |
| 456 const_cast<LPSTR*>(usage); | |
| 457 // We can set CERT_CHAIN_RETURN_LOWER_QUALITY_CONTEXTS to get more chains. | |
| 458 DWORD chain_flags = CERT_CHAIN_CACHE_END_CERT; | |
| 459 if (flags & VERIFY_REV_CHECKING_ENABLED) { | |
| 460 verify_result->cert_status |= CERT_STATUS_REV_CHECKING_ENABLED; | |
| 461 chain_flags |= CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT; | |
| 462 } else { | |
| 463 chain_flags |= CERT_CHAIN_REVOCATION_CHECK_CACHE_ONLY; | |
| 464 // EV requires revocation checking. | |
| 465 flags &= ~VERIFY_EV_CERT; | |
| 466 } | |
| 467 | |
| 468 X509Certificate::OSCertListHandle cert_list = | |
| 469 certificate->CreateOSCertListHandle(); | |
|
bulach
2010/10/21 10:21:33
there's a bunch of places that CreateOSCertListHan
| |
| 470 PCCERT_CHAIN_CONTEXT chain_context; | |
| 471 // IE passes a non-NULL pTime argument that specifies the current system | |
| 472 // time. IE passes CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT as the | |
| 473 // chain_flags argument. | |
| 474 if (!CertGetCertificateChain( | |
| 475 NULL, // default chain engine, HCCE_CURRENT_USER | |
| 476 cert_list, | |
| 477 NULL, // current system time | |
| 478 cert_list->hCertStore, // search this store | |
| 479 &chain_para, | |
| 480 chain_flags, | |
| 481 NULL, // reserved | |
| 482 &chain_context)) { | |
| 483 X509Certificate::FreeOSCertListHandle(cert_list); | |
| 484 return MapSecurityError(GetLastError()); | |
| 485 } | |
| 486 X509Certificate::FreeOSCertListHandle(cert_list); | |
| 487 ScopedCertChainContext scoped_chain_context(chain_context); | |
| 488 | |
| 489 GetCertChainInfo(chain_context, verify_result); | |
| 490 | |
| 491 verify_result->cert_status |= MapCertChainErrorStatusToCertStatus( | |
| 492 chain_context->TrustStatus.dwErrorStatus); | |
| 493 | |
| 494 // Treat certificates signed using broken signature algorithms as invalid. | |
| 495 if (verify_result->has_md4) | |
| 496 verify_result->cert_status |= CERT_STATUS_INVALID; | |
| 497 | |
| 498 // Flag certificates signed using weak signature algorithms. | |
| 499 if (verify_result->has_md2) | |
| 500 verify_result->cert_status |= CERT_STATUS_WEAK_SIGNATURE_ALGORITHM; | |
| 501 | |
| 502 // Flag certificates that have a Subject common name with a NULL character. | |
| 503 if (CertSubjectCommonNameHasNull(cert_list)) | |
| 504 verify_result->cert_status |= CERT_STATUS_INVALID; | |
| 505 | |
| 506 std::wstring wstr_hostname = ASCIIToWide(hostname); | |
| 507 | |
| 508 SSL_EXTRA_CERT_CHAIN_POLICY_PARA extra_policy_para; | |
| 509 memset(&extra_policy_para, 0, sizeof(extra_policy_para)); | |
| 510 extra_policy_para.cbSize = sizeof(extra_policy_para); | |
| 511 extra_policy_para.dwAuthType = AUTHTYPE_SERVER; | |
| 512 extra_policy_para.fdwChecks = 0; | |
| 513 extra_policy_para.pwszServerName = | |
| 514 const_cast<wchar_t*>(wstr_hostname.c_str()); | |
| 515 | |
| 516 CERT_CHAIN_POLICY_PARA policy_para; | |
| 517 memset(&policy_para, 0, sizeof(policy_para)); | |
| 518 policy_para.cbSize = sizeof(policy_para); | |
| 519 policy_para.dwFlags = 0; | |
| 520 policy_para.pvExtraPolicyPara = &extra_policy_para; | |
| 521 | |
| 522 CERT_CHAIN_POLICY_STATUS policy_status; | |
| 523 memset(&policy_status, 0, sizeof(policy_status)); | |
| 524 policy_status.cbSize = sizeof(policy_status); | |
| 525 | |
| 526 if (!CertVerifyCertificateChainPolicy( | |
| 527 CERT_CHAIN_POLICY_SSL, | |
| 528 chain_context, | |
| 529 &policy_para, | |
| 530 &policy_status)) { | |
| 531 return MapSecurityError(GetLastError()); | |
| 532 } | |
| 533 | |
| 534 if (policy_status.dwError) { | |
| 535 verify_result->cert_status |= MapNetErrorToCertStatus( | |
| 536 MapSecurityError(policy_status.dwError)); | |
| 537 | |
| 538 // CertVerifyCertificateChainPolicy reports only one error (in | |
| 539 // policy_status.dwError) if the certificate has multiple errors. | |
| 540 // CertGetCertificateChain doesn't report certificate name mismatch, so | |
| 541 // CertVerifyCertificateChainPolicy is the only function that can report | |
| 542 // certificate name mismatch. | |
| 543 // | |
| 544 // To prevent a potential certificate name mismatch from being hidden by | |
| 545 // some other certificate error, if we get any other certificate error, | |
| 546 // we call CertVerifyCertificateChainPolicy again, ignoring all other | |
| 547 // certificate errors. Both extra_policy_para.fdwChecks and | |
| 548 // policy_para.dwFlags allow us to ignore certificate errors, so we set | |
| 549 // them both. | |
| 550 if (policy_status.dwError != CERT_E_CN_NO_MATCH) { | |
| 551 const DWORD extra_ignore_flags = | |
| 552 0x00000080 | // SECURITY_FLAG_IGNORE_REVOCATION | |
| 553 0x00000100 | // SECURITY_FLAG_IGNORE_UNKNOWN_CA | |
| 554 0x00002000 | // SECURITY_FLAG_IGNORE_CERT_DATE_INVALID | |
| 555 0x00000200; // SECURITY_FLAG_IGNORE_WRONG_USAGE | |
| 556 extra_policy_para.fdwChecks = extra_ignore_flags; | |
| 557 const DWORD ignore_flags = | |
| 558 CERT_CHAIN_POLICY_IGNORE_ALL_NOT_TIME_VALID_FLAGS | | |
| 559 CERT_CHAIN_POLICY_IGNORE_INVALID_BASIC_CONSTRAINTS_FLAG | | |
| 560 CERT_CHAIN_POLICY_ALLOW_UNKNOWN_CA_FLAG | | |
| 561 CERT_CHAIN_POLICY_IGNORE_WRONG_USAGE_FLAG | | |
| 562 CERT_CHAIN_POLICY_IGNORE_INVALID_NAME_FLAG | | |
| 563 CERT_CHAIN_POLICY_IGNORE_INVALID_POLICY_FLAG | | |
| 564 CERT_CHAIN_POLICY_IGNORE_ALL_REV_UNKNOWN_FLAGS | | |
| 565 CERT_CHAIN_POLICY_ALLOW_TESTROOT_FLAG | | |
| 566 CERT_CHAIN_POLICY_TRUST_TESTROOT_FLAG | | |
| 567 CERT_CHAIN_POLICY_IGNORE_NOT_SUPPORTED_CRITICAL_EXT_FLAG | | |
| 568 CERT_CHAIN_POLICY_IGNORE_PEER_TRUST_FLAG; | |
| 569 policy_para.dwFlags = ignore_flags; | |
| 570 if (!CertVerifyCertificateChainPolicy( | |
| 571 CERT_CHAIN_POLICY_SSL, | |
| 572 chain_context, | |
| 573 &policy_para, | |
| 574 &policy_status)) { | |
| 575 return MapSecurityError(GetLastError()); | |
| 576 } | |
| 577 if (policy_status.dwError) { | |
| 578 verify_result->cert_status |= MapNetErrorToCertStatus( | |
| 579 MapSecurityError(policy_status.dwError)); | |
| 580 } | |
| 581 } | |
| 582 } | |
| 583 | |
| 584 // TODO(wtc): Suppress CERT_STATUS_NO_REVOCATION_MECHANISM for now to be | |
| 585 // compatible with WinHTTP, which doesn't report this error (bug 3004). | |
| 586 verify_result->cert_status &= ~CERT_STATUS_NO_REVOCATION_MECHANISM; | |
| 587 | |
| 588 if (IsCertStatusError(verify_result->cert_status)) | |
| 589 return MapCertStatusToNetError(verify_result->cert_status); | |
| 590 | |
| 591 // TODO(ukai): combine regular cert verification and EV cert verification. | |
| 592 if ((flags & VERIFY_EV_CERT) && VerifyEV(certificate)) | |
| 593 verify_result->cert_status |= CERT_STATUS_IS_EV; | |
| 594 return OK; | |
| 595 } | |
| 596 | |
| 597 } // namespace x509_chain | |
| 598 | |
| 599 } // namespace net | |
| OLD | NEW |