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

Side by Side Diff: net/cert/cert_verify_proc_mac.cc

Issue 903273002: Update from https://crrev.com/315085 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 10 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
« no previous file with comments | « net/cert/cert_verify_proc_android.h ('k') | net/cert/x509_certificate.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/cert/cert_verify_proc_mac.h" 5 #include "net/cert/cert_verify_proc_mac.h"
6 6
7 #include <CommonCrypto/CommonDigest.h> 7 #include <CommonCrypto/CommonDigest.h>
8 #include <CoreServices/CoreServices.h> 8 #include <CoreServices/CoreServices.h>
9 #include <Security/Security.h> 9 #include <Security/Security.h>
10 10
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 (flags & CertVerifier::VERIFY_REV_CHECKING_ENABLED), 168 (flags & CertVerifier::VERIFY_REV_CHECKING_ENABLED),
169 (flags & CertVerifier::VERIFY_REV_CHECKING_ENABLED_EV_ONLY), 169 (flags & CertVerifier::VERIFY_REV_CHECKING_ENABLED_EV_ONLY),
170 local_policies); 170 local_policies);
171 if (status) 171 if (status)
172 return status; 172 return status;
173 173
174 policies->reset(local_policies.release()); 174 policies->reset(local_policies.release());
175 return noErr; 175 return noErr;
176 } 176 }
177 177
178 // Saves some information about the certificate chain |cert_chain| in 178 // Stores the constructed certificate chain |cert_chain| and information about
179 // |*verify_result|. The caller MUST initialize |*verify_result| before 179 // the signature algorithms used into |*verify_result|. If the leaf cert in
180 // calling this function. 180 // |cert_chain| contains a weak (MD2, MD4, MD5, SHA-1) signature, stores that
181 // in |*leaf_is_weak|.
181 void GetCertChainInfo(CFArrayRef cert_chain, 182 void GetCertChainInfo(CFArrayRef cert_chain,
182 CSSM_TP_APPLE_EVIDENCE_INFO* chain_info, 183 CSSM_TP_APPLE_EVIDENCE_INFO* chain_info,
183 CertVerifyResult* verify_result) { 184 CertVerifyResult* verify_result,
185 bool* leaf_is_weak) {
186 *leaf_is_weak = false;
187 verify_result->verified_cert = nullptr;
188 verify_result->has_md2 = false;
189 verify_result->has_md4 = false;
190 verify_result->has_md5 = false;
191 verify_result->has_sha1 = false;
192
184 SecCertificateRef verified_cert = NULL; 193 SecCertificateRef verified_cert = NULL;
185 std::vector<SecCertificateRef> verified_chain; 194 std::vector<SecCertificateRef> verified_chain;
186 for (CFIndex i = 0, count = CFArrayGetCount(cert_chain); i < count; ++i) { 195 for (CFIndex i = 0, count = CFArrayGetCount(cert_chain); i < count; ++i) {
187 SecCertificateRef chain_cert = reinterpret_cast<SecCertificateRef>( 196 SecCertificateRef chain_cert = reinterpret_cast<SecCertificateRef>(
188 const_cast<void*>(CFArrayGetValueAtIndex(cert_chain, i))); 197 const_cast<void*>(CFArrayGetValueAtIndex(cert_chain, i)));
189 if (i == 0) { 198 if (i == 0) {
190 verified_cert = chain_cert; 199 verified_cert = chain_cert;
191 } else { 200 } else {
192 verified_chain.push_back(chain_cert); 201 verified_chain.push_back(chain_cert);
193 } 202 }
(...skipping 22 matching lines...) Expand all
216 // OS X certificate library, but based on history, it is best to play it 225 // OS X certificate library, but based on history, it is best to play it
217 // safe. 226 // safe.
218 const CSSM_X509_ALGORITHM_IDENTIFIER* sig_algorithm = 227 const CSSM_X509_ALGORITHM_IDENTIFIER* sig_algorithm =
219 signature_field.GetAs<CSSM_X509_ALGORITHM_IDENTIFIER>(); 228 signature_field.GetAs<CSSM_X509_ALGORITHM_IDENTIFIER>();
220 if (!sig_algorithm) 229 if (!sig_algorithm)
221 continue; 230 continue;
222 231
223 const CSSM_OID* alg_oid = &sig_algorithm->algorithm; 232 const CSSM_OID* alg_oid = &sig_algorithm->algorithm;
224 if (CSSMOIDEqual(alg_oid, &CSSMOID_MD2WithRSA)) { 233 if (CSSMOIDEqual(alg_oid, &CSSMOID_MD2WithRSA)) {
225 verify_result->has_md2 = true; 234 verify_result->has_md2 = true;
235 if (i == 0)
236 *leaf_is_weak = true;
226 } else if (CSSMOIDEqual(alg_oid, &CSSMOID_MD4WithRSA)) { 237 } else if (CSSMOIDEqual(alg_oid, &CSSMOID_MD4WithRSA)) {
227 verify_result->has_md4 = true; 238 verify_result->has_md4 = true;
239 if (i == 0)
240 *leaf_is_weak = true;
228 } else if (CSSMOIDEqual(alg_oid, &CSSMOID_MD5WithRSA)) { 241 } else if (CSSMOIDEqual(alg_oid, &CSSMOID_MD5WithRSA)) {
229 verify_result->has_md5 = true; 242 verify_result->has_md5 = true;
243 if (i == 0)
244 *leaf_is_weak = true;
230 } else if (CSSMOIDEqual(alg_oid, &CSSMOID_SHA1WithRSA) || 245 } else if (CSSMOIDEqual(alg_oid, &CSSMOID_SHA1WithRSA) ||
231 CSSMOIDEqual(alg_oid, &CSSMOID_SHA1WithRSA_OIW) || 246 CSSMOIDEqual(alg_oid, &CSSMOID_SHA1WithRSA_OIW) ||
232 CSSMOIDEqual(alg_oid, &CSSMOID_SHA1WithDSA) || 247 CSSMOIDEqual(alg_oid, &CSSMOID_SHA1WithDSA) ||
233 CSSMOIDEqual(alg_oid, &CSSMOID_SHA1WithDSA_CMS) || 248 CSSMOIDEqual(alg_oid, &CSSMOID_SHA1WithDSA_CMS) ||
234 CSSMOIDEqual(alg_oid, &CSSMOID_SHA1WithDSA_JDK) || 249 CSSMOIDEqual(alg_oid, &CSSMOID_SHA1WithDSA_JDK) ||
235 CSSMOIDEqual(alg_oid, &CSSMOID_ECDSA_WithSHA1)) { 250 CSSMOIDEqual(alg_oid, &CSSMOID_ECDSA_WithSHA1)) {
236 verify_result->has_sha1 = true; 251 verify_result->has_sha1 = true;
252 if (i == 0)
253 *leaf_is_weak = true;
237 } 254 }
238 } 255 }
239 if (!verified_cert) 256 if (!verified_cert)
240 return; 257 return;
241 258
242 verify_result->verified_cert = 259 verify_result->verified_cert =
243 X509Certificate::CreateFromHandle(verified_cert, verified_chain); 260 X509Certificate::CreateFromHandle(verified_cert, verified_chain);
244 } 261 }
245 262
246 void AppendPublicKeyHashes(CFArrayRef chain, 263 void AppendPublicKeyHashes(CFArrayRef chain,
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
439 return NetErrorFromOSStatus(status); 456 return NetErrorFromOSStatus(status);
440 457
441 trust_ref->swap(scoped_tmp_trust); 458 trust_ref->swap(scoped_tmp_trust);
442 *trust_result = tmp_trust_result; 459 *trust_result = tmp_trust_result;
443 verified_chain->reset(tmp_verified_chain); 460 verified_chain->reset(tmp_verified_chain);
444 *chain_info = tmp_chain_info; 461 *chain_info = tmp_chain_info;
445 462
446 return OK; 463 return OK;
447 } 464 }
448 465
449 // OS X ships with both "GTE CyberTrust Global Root" and "Baltimore CyberTrust
450 // Root" as part of its trusted root store. However, a cross-certified version
451 // of the "Baltimore CyberTrust Root" exists that chains to "GTE CyberTrust
452 // Global Root". When OS X/Security.framework attempts to evaluate such a
453 // certificate chain, it disregards the "Baltimore CyberTrust Root" that exists
454 // within Keychain and instead attempts to terminate the chain in the "GTE
455 // CyberTrust Global Root". However, the GTE root is scheduled to be removed in
456 // a future OS X update (for sunsetting purposes), and once removed, such
457 // chains will fail validation, even though a trust anchor still exists.
458 //
459 // Rather than over-generalizing a solution that may mask a number of TLS
460 // misconfigurations, attempt to specifically match the affected
461 // cross-certified certificate and remove it from certificate chain processing.
462 bool IsBadBaltimoreGTECertificate(SecCertificateRef cert) {
463 // Matches the GTE-signed Baltimore CyberTrust Root
464 // https://cacert.omniroot.com/Baltimore-to-GTE-04-12.pem
465 static const SHA1HashValue kBadBaltimoreHashNew =
466 { { 0x4D, 0x34, 0xEA, 0x92, 0x76, 0x4B, 0x3A, 0x31, 0x49, 0x11,
467 0x99, 0x52, 0xF4, 0x19, 0x30, 0xCA, 0x11, 0x34, 0x83, 0x61 } };
468 // Matches the legacy GTE-signed Baltimore CyberTrust Root
469 // https://cacert.omniroot.com/gte-2-2025.pem
470 static const SHA1HashValue kBadBaltimoreHashOld =
471 { { 0x54, 0xD8, 0xCB, 0x49, 0x1F, 0xA1, 0x6D, 0xF8, 0x87, 0xDC,
472 0x94, 0xA9, 0x34, 0xCC, 0x83, 0x6B, 0xDA, 0xA8, 0xA3, 0x69 } };
473
474 SHA1HashValue fingerprint = X509Certificate::CalculateFingerprint(cert);
475
476 return fingerprint.Equals(kBadBaltimoreHashNew) ||
477 fingerprint.Equals(kBadBaltimoreHashOld);
478 }
479
480 // Attempts to re-verify |cert_array| after adjusting the inputs to work around
481 // known issues in OS X. To be used if BuildAndEvaluateSecTrustRef fails to
482 // return a positive result for verification.
483 //
484 // This function should only be called while the Mac Security Services lock is
485 // held.
486 void RetrySecTrustEvaluateWithAdjustedChain(
487 CFArrayRef cert_array,
488 CFArrayRef trust_policies,
489 int flags,
490 ScopedCFTypeRef<SecTrustRef>* trust_ref,
491 SecTrustResultType* trust_result,
492 ScopedCFTypeRef<CFArrayRef>* verified_chain,
493 CSSM_TP_APPLE_EVIDENCE_INFO** chain_info) {
494 CFIndex count = CFArrayGetCount(*verified_chain);
495 CFIndex slice_point = 0;
496
497 for (CFIndex i = 1; i < count; ++i) {
498 SecCertificateRef cert = reinterpret_cast<SecCertificateRef>(
499 const_cast<void*>(CFArrayGetValueAtIndex(*verified_chain, i)));
500 if (cert == NULL)
501 return; // Strange times; can't fix things up.
502
503 if (IsBadBaltimoreGTECertificate(cert)) {
504 slice_point = i;
505 break;
506 }
507 }
508 if (slice_point == 0)
509 return; // Nothing to do.
510
511 ScopedCFTypeRef<CFMutableArrayRef> adjusted_cert_array(
512 CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks));
513 // Note: This excludes the certificate at |slice_point|.
514 CFArrayAppendArray(adjusted_cert_array, cert_array,
515 CFRangeMake(0, slice_point));
516
517 // Ignore the result; failure will preserve the old verification results.
518 BuildAndEvaluateSecTrustRef(
519 adjusted_cert_array, trust_policies, flags, trust_ref, trust_result,
520 verified_chain, chain_info);
521 }
522
523 } // namespace 466 } // namespace
524 467
525 CertVerifyProcMac::CertVerifyProcMac() {} 468 CertVerifyProcMac::CertVerifyProcMac() {}
526 469
527 CertVerifyProcMac::~CertVerifyProcMac() {} 470 CertVerifyProcMac::~CertVerifyProcMac() {}
528 471
529 bool CertVerifyProcMac::SupportsAdditionalTrustAnchors() const { 472 bool CertVerifyProcMac::SupportsAdditionalTrustAnchors() const {
530 return false; 473 return false;
531 } 474 }
532 475
533 int CertVerifyProcMac::VerifyInternal( 476 int CertVerifyProcMac::VerifyInternal(
534 X509Certificate* cert, 477 X509Certificate* cert,
535 const std::string& hostname, 478 const std::string& hostname,
536 int flags, 479 int flags,
537 CRLSet* crl_set, 480 CRLSet* crl_set,
538 const CertificateList& additional_trust_anchors, 481 const CertificateList& additional_trust_anchors,
539 CertVerifyResult* verify_result) { 482 CertVerifyResult* verify_result) {
540 ScopedCFTypeRef<CFArrayRef> trust_policies; 483 ScopedCFTypeRef<CFArrayRef> trust_policies;
541 OSStatus status = CreateTrustPolicies(hostname, flags, &trust_policies); 484 OSStatus status = CreateTrustPolicies(hostname, flags, &trust_policies);
542 if (status) 485 if (status)
543 return NetErrorFromOSStatus(status); 486 return NetErrorFromOSStatus(status);
544 487
545 // Create and configure a SecTrustRef, which takes our certificate(s) 488 // Create and configure a SecTrustRef, which takes our certificate(s)
546 // and our SSL SecPolicyRef. SecTrustCreateWithCertificates() takes an 489 // and our SSL SecPolicyRef. SecTrustCreateWithCertificates() takes an
547 // array of certificates, the first of which is the certificate we're 490 // array of certificates, the first of which is the certificate we're
548 // verifying, and the subsequent (optional) certificates are used for 491 // verifying, and the subsequent (optional) certificates are used for
549 // chain building. 492 // chain building.
550 ScopedCFTypeRef<CFArrayRef> cert_array(cert->CreateOSCertChainForCert()); 493 ScopedCFTypeRef<CFMutableArrayRef> cert_array(
494 cert->CreateOSCertChainForCert());
551 495
552 // Serialize all calls that may use the Keychain, to work around various 496 // Serialize all calls that may use the Keychain, to work around various
553 // issues in OS X 10.6+ with multi-threaded access to Security.framework. 497 // issues in OS X 10.6+ with multi-threaded access to Security.framework.
554 base::AutoLock lock(crypto::GetMacSecurityServicesLock()); 498 base::AutoLock lock(crypto::GetMacSecurityServicesLock());
555 499
556 ScopedCFTypeRef<SecTrustRef> trust_ref; 500 ScopedCFTypeRef<SecTrustRef> trust_ref;
557 SecTrustResultType trust_result = kSecTrustResultDeny; 501 SecTrustResultType trust_result = kSecTrustResultDeny;
558 ScopedCFTypeRef<CFArrayRef> completed_chain; 502 ScopedCFTypeRef<CFArrayRef> completed_chain;
559 CSSM_TP_APPLE_EVIDENCE_INFO* chain_info = NULL; 503 CSSM_TP_APPLE_EVIDENCE_INFO* chain_info = NULL;
504 bool candidate_untrusted = true;
505 bool candidate_weak = false;
560 506
561 int rv = BuildAndEvaluateSecTrustRef( 507 // OS X lacks proper path discovery; it will take the input certs and never
562 cert_array, trust_policies, flags, &trust_ref, &trust_result, 508 // backtrack the graph attempting to discover valid paths.
563 &completed_chain, &chain_info); 509 // This can create issues in some situations:
564 if (rv != OK) 510 // - When OS X changes the trust store, there may be a chain
565 return rv; 511 // A -> B -> C -> D
566 if (trust_result != kSecTrustResultUnspecified && 512 // where OS X trusts D (on some versions) and trusts C (on some versions).
567 trust_result != kSecTrustResultProceed) { 513 // If a server supplies a chain A, B, C (cross-signed by D), then this chain
568 RetrySecTrustEvaluateWithAdjustedChain( 514 // will successfully validate on systems that trust D, but fail for systems
569 cert_array, trust_policies, flags, &trust_ref, &trust_result, 515 // that trust C. If the server supplies a chain of A -> B, then it forces
570 &completed_chain, &chain_info); 516 // all clients to fetch C (via AIA) if they trust D, and not all clients
517 // (notably, Firefox and Android) will do this, thus breaking them.
518 // An example of this is the Verizon Business Services root - GTE CyberTrust
519 // and Baltimore CyberTrust roots represent old and new roots that cause
520 // issues depending on which version of OS X being used.
521 //
522 // - A server may be (misconfigured) to send an expired intermediate
523 // certificate. On platforms with path discovery, the graph traversal
524 // will back up to immediately before this intermediate, and then
525 // attempt an AIA fetch or retrieval from local store. However, OS X
526 // does not do this, and thus prevents access. While this is ostensibly
527 // a server misconfiguration issue, the fact that it works on other
528 // platforms is a jarring inconsistency for users.
529 //
530 // - When OS X trusts both C and D (simultaneously), it's possible that the
531 // version of C signed by D is signed using a weak algorithm (e.g. SHA-1),
532 // while the version of C in the trust store's signature doesn't matter.
533 // Since a 'strong' chain exists, it would be desirable to prefer this
534 // chain.
535 //
536 // - A variant of the above example, it may be that the version of B sent by
537 // the server is signed using a weak algorithm, but the version of B
538 // present in the AIA of A is signed using a strong algorithm. Since a
539 // 'strong' chain exists, it would be desirable to prefer this chain.
540 //
541 // Because of this, the code below first attempts to validate the peer's
542 // identity using the supplied chain. If it is not trusted (e.g. the OS only
543 // trusts C, but the version of C signed by D was sent, and D is not trusted),
544 // or if it contains a weak chain, it will begin lopping off certificates
545 // from the end of the chain and attempting to verify. If a stronger, trusted
546 // chain is found, it is used, otherwise, the algorithm continues until only
547 // the peer's certificate remains.
548 //
549 // This does cause a performance hit for these users, but only in cases where
550 // OS X is building weaker chains than desired, or when it would otherwise
551 // fail the connection.
552 while (CFArrayGetCount(cert_array) > 0) {
553 ScopedCFTypeRef<SecTrustRef> temp_ref;
554 SecTrustResultType temp_trust_result = kSecTrustResultDeny;
555 ScopedCFTypeRef<CFArrayRef> temp_chain;
556 CSSM_TP_APPLE_EVIDENCE_INFO* temp_chain_info = NULL;
557
558 int rv = BuildAndEvaluateSecTrustRef(cert_array, trust_policies, flags,
559 &temp_ref, &temp_trust_result,
560 &temp_chain, &temp_chain_info);
561 if (rv != OK)
562 return rv;
563
564 CertVerifyResult temp_verify_result;
565 bool leaf_is_weak = false;
566 GetCertChainInfo(temp_chain, temp_chain_info, &temp_verify_result,
567 &leaf_is_weak);
568
569 bool untrusted = (temp_trust_result != kSecTrustResultUnspecified &&
570 temp_trust_result != kSecTrustResultProceed);
571 bool weak_chain =
572 !leaf_is_weak &&
573 (temp_verify_result.has_md2 || temp_verify_result.has_md4 ||
574 temp_verify_result.has_md5 || temp_verify_result.has_sha1);
575 // Set the result to the current chain if:
576 // - This is the first verification attempt. This ensures that if
577 // everything is awful (e.g. it may just be an untrusted cert), that
578 // what is reported is exactly what was sent by the server
579 // - If the current chain is trusted, and the old chain was not trusted,
580 // then prefer this chain. This ensures that if there is at least a
581 // valid path to a trust anchor, it's preferred over reporting an error.
582 // - If the current chain is trusted, and the old chain is trusted, but
583 // the old chain contained weak algorithms while the current chain only
584 // contains strong algorithms, then prefer the current chain over the
585 // old chain.
586 //
587 // Note: If the leaf certificate itself is weak, then the only
588 // consideration is whether or not there is a trusted chain. That's
589 // because no amount of path discovery will fix a weak leaf.
590 if (!trust_ref || (!untrusted && (candidate_untrusted ||
591 (candidate_weak && !weak_chain)))) {
592 trust_ref = temp_ref;
593 trust_result = temp_trust_result;
594 completed_chain = temp_chain;
595 chain_info = temp_chain_info;
596
597 candidate_untrusted = untrusted;
598 candidate_weak = weak_chain;
599 }
600 // Short-circuit when a current, trusted chain is found.
601 if (!untrusted && !weak_chain)
602 break;
603 CFArrayRemoveValueAtIndex(cert_array, CFArrayGetCount(cert_array) - 1);
571 } 604 }
572 605
573 if (flags & CertVerifier::VERIFY_REV_CHECKING_ENABLED) 606 if (flags & CertVerifier::VERIFY_REV_CHECKING_ENABLED)
574 verify_result->cert_status |= CERT_STATUS_REV_CHECKING_ENABLED; 607 verify_result->cert_status |= CERT_STATUS_REV_CHECKING_ENABLED;
575 608
576 if (crl_set && !CheckRevocationWithCRLSet(completed_chain, crl_set)) 609 if (crl_set && !CheckRevocationWithCRLSet(completed_chain, crl_set))
577 verify_result->cert_status |= CERT_STATUS_REVOKED; 610 verify_result->cert_status |= CERT_STATUS_REVOKED;
578 611
579 GetCertChainInfo(completed_chain, chain_info, verify_result); 612 bool leaf_is_weak_unused = false;
613 GetCertChainInfo(completed_chain, chain_info, verify_result,
614 &leaf_is_weak_unused);
580 615
581 // As of Security Update 2012-002/OS X 10.7.4, when an RSA key < 1024 bits 616 // As of Security Update 2012-002/OS X 10.7.4, when an RSA key < 1024 bits
582 // is encountered, CSSM returns CSSMERR_TP_VERIFY_ACTION_FAILED and adds 617 // is encountered, CSSM returns CSSMERR_TP_VERIFY_ACTION_FAILED and adds
583 // CSSMERR_CSP_UNSUPPORTED_KEY_SIZE as a certificate status. Avoid mapping 618 // CSSMERR_CSP_UNSUPPORTED_KEY_SIZE as a certificate status. Avoid mapping
584 // the CSSMERR_TP_VERIFY_ACTION_FAILED to CERT_STATUS_INVALID if the only 619 // the CSSMERR_TP_VERIFY_ACTION_FAILED to CERT_STATUS_INVALID if the only
585 // error was due to an unsupported key size. 620 // error was due to an unsupported key size.
586 bool policy_failed = false; 621 bool policy_failed = false;
587 bool weak_key_or_signature_algorithm = false; 622 bool weak_key_or_signature_algorithm = false;
588 623
589 // Evaluate the results 624 // Evaluate the results
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
730 } 765 }
731 } 766 }
732 } 767 }
733 } 768 }
734 } 769 }
735 770
736 return OK; 771 return OK;
737 } 772 }
738 773
739 } // namespace net 774 } // namespace net
OLDNEW
« no previous file with comments | « net/cert/cert_verify_proc_android.h ('k') | net/cert/x509_certificate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698