OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/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 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 int n = CFArrayGetCount(chain); | 334 int n = CFArrayGetCount(chain); |
335 if (n < 1) | 335 if (n < 1) |
336 return false; | 336 return false; |
337 SecCertificateRef root_ref = reinterpret_cast<SecCertificateRef>( | 337 SecCertificateRef root_ref = reinterpret_cast<SecCertificateRef>( |
338 const_cast<void*>(CFArrayGetValueAtIndex(chain, n - 1))); | 338 const_cast<void*>(CFArrayGetValueAtIndex(chain, n - 1))); |
339 SHA1HashValue hash = X509Certificate::CalculateFingerprint(root_ref); | 339 SHA1HashValue hash = X509Certificate::CalculateFingerprint(root_ref); |
340 return IsSHA1HashInSortedArray( | 340 return IsSHA1HashInSortedArray( |
341 hash, &kKnownRootCertSHA1Hashes[0][0], sizeof(kKnownRootCertSHA1Hashes)); | 341 hash, &kKnownRootCertSHA1Hashes[0][0], sizeof(kKnownRootCertSHA1Hashes)); |
342 } | 342 } |
343 | 343 |
344 } // namespace | 344 // Builds and evaluates a SecTrustRef for the certificate chain contained |
345 | 345 // in |cert_array|, using the verification policies in |trust_policies|. On |
346 CertVerifyProcMac::CertVerifyProcMac() {} | 346 // success, returns OK, and updates |trust_ref|, |trust_result|, |
347 | 347 // |verified_chain|, and |chain_info| with the verification results. On |
348 CertVerifyProcMac::~CertVerifyProcMac() {} | 348 // failure, no output parameters are modified. |
349 | 349 // |
350 bool CertVerifyProcMac::SupportsAdditionalTrustAnchors() const { | 350 // Note: An OK return does not mean that |cert_array| is trusted, merely that |
351 return false; | 351 // verification was performed successfully. |
352 } | 352 // |
353 | 353 // This function should only be called while the Mac Security Services lock is |
354 int CertVerifyProcMac::VerifyInternal( | 354 // held. |
355 X509Certificate* cert, | 355 int BuildAndEvaluateSecTrustRef(CFArrayRef cert_array, |
356 const std::string& hostname, | 356 CFArrayRef trust_policies, |
357 int flags, | 357 int flags, |
358 CRLSet* crl_set, | 358 ScopedCFTypeRef<SecTrustRef>* trust_ref, |
359 const CertificateList& additional_trust_anchors, | 359 SecTrustResultType* trust_result, |
360 CertVerifyResult* verify_result) { | 360 ScopedCFTypeRef<CFArrayRef>* verified_chain, |
361 ScopedCFTypeRef<CFArrayRef> trust_policies; | 361 CSSM_TP_APPLE_EVIDENCE_INFO** chain_info) { |
362 OSStatus status = CreateTrustPolicies(hostname, flags, &trust_policies); | 362 SecTrustRef tmp_trust = NULL; |
| 363 OSStatus status = SecTrustCreateWithCertificates(cert_array, trust_policies, |
| 364 &tmp_trust); |
363 if (status) | 365 if (status) |
364 return NetErrorFromOSStatus(status); | 366 return NetErrorFromOSStatus(status); |
365 | 367 ScopedCFTypeRef<SecTrustRef> scoped_tmp_trust(tmp_trust); |
366 // Create and configure a SecTrustRef, which takes our certificate(s) | |
367 // and our SSL SecPolicyRef. SecTrustCreateWithCertificates() takes an | |
368 // array of certificates, the first of which is the certificate we're | |
369 // verifying, and the subsequent (optional) certificates are used for | |
370 // chain building. | |
371 ScopedCFTypeRef<CFArrayRef> cert_array(cert->CreateOSCertChainForCert()); | |
372 | |
373 // Serialize all calls that may use the Keychain, to work around various | |
374 // issues in OS X 10.6+ with multi-threaded access to Security.framework. | |
375 base::AutoLock lock(crypto::GetMacSecurityServicesLock()); | |
376 | |
377 SecTrustRef trust_ref = NULL; | |
378 status = SecTrustCreateWithCertificates(cert_array, trust_policies, | |
379 &trust_ref); | |
380 if (status) | |
381 return NetErrorFromOSStatus(status); | |
382 ScopedCFTypeRef<SecTrustRef> scoped_trust_ref(trust_ref); | |
383 | 368 |
384 if (TestRootCerts::HasInstance()) { | 369 if (TestRootCerts::HasInstance()) { |
385 status = TestRootCerts::GetInstance()->FixupSecTrustRef(trust_ref); | 370 status = TestRootCerts::GetInstance()->FixupSecTrustRef(tmp_trust); |
386 if (status) | 371 if (status) |
387 return NetErrorFromOSStatus(status); | 372 return NetErrorFromOSStatus(status); |
388 } | 373 } |
389 | 374 |
390 CSSM_APPLE_TP_ACTION_DATA tp_action_data; | 375 CSSM_APPLE_TP_ACTION_DATA tp_action_data; |
391 memset(&tp_action_data, 0, sizeof(tp_action_data)); | 376 memset(&tp_action_data, 0, sizeof(tp_action_data)); |
392 tp_action_data.Version = CSSM_APPLE_TP_ACTION_VERSION; | 377 tp_action_data.Version = CSSM_APPLE_TP_ACTION_VERSION; |
393 // Allow CSSM to download any missing intermediate certificates if an | 378 // Allow CSSM to download any missing intermediate certificates if an |
394 // authorityInfoAccess extension or issuerAltName extension is present. | 379 // authorityInfoAccess extension or issuerAltName extension is present. |
395 tp_action_data.ActionFlags = CSSM_TP_ACTION_FETCH_CERT_FROM_NET | | 380 tp_action_data.ActionFlags = CSSM_TP_ACTION_FETCH_CERT_FROM_NET | |
396 CSSM_TP_ACTION_TRUST_SETTINGS; | 381 CSSM_TP_ACTION_TRUST_SETTINGS; |
397 | 382 |
398 // Note: For EV certificates, the Apple TP will handle setting these flags | 383 // Note: For EV certificates, the Apple TP will handle setting these flags |
399 // as part of EV evaluation. | 384 // as part of EV evaluation. |
400 if (flags & CertVerifier::VERIFY_REV_CHECKING_ENABLED) { | 385 if (flags & CertVerifier::VERIFY_REV_CHECKING_ENABLED) { |
401 // Require a positive result from an OCSP responder or a CRL (or both) | 386 // Require a positive result from an OCSP responder or a CRL (or both) |
402 // for every certificate in the chain. The Apple TP automatically | 387 // for every certificate in the chain. The Apple TP automatically |
403 // excludes the self-signed root from this requirement. If a certificate | 388 // excludes the self-signed root from this requirement. If a certificate |
404 // is missing both a crlDistributionPoints extension and an | 389 // is missing both a crlDistributionPoints extension and an |
405 // authorityInfoAccess extension with an OCSP responder URL, then we | 390 // authorityInfoAccess extension with an OCSP responder URL, then we |
406 // will get a kSecTrustResultRecoverableTrustFailure back from | 391 // will get a kSecTrustResultRecoverableTrustFailure back from |
407 // SecTrustEvaluate(), with a | 392 // SecTrustEvaluate(), with a |
408 // CSSMERR_APPLETP_INCOMPLETE_REVOCATION_CHECK error code. In that case, | 393 // CSSMERR_APPLETP_INCOMPLETE_REVOCATION_CHECK error code. In that case, |
409 // we'll set our own result to include | 394 // we'll set our own result to include |
410 // CERT_STATUS_NO_REVOCATION_MECHANISM. If one or both extensions are | 395 // CERT_STATUS_NO_REVOCATION_MECHANISM. If one or both extensions are |
411 // present, and a check fails (server unavailable, OCSP retry later, | 396 // present, and a check fails (server unavailable, OCSP retry later, |
412 // signature mismatch), then we'll set our own result to include | 397 // signature mismatch), then we'll set our own result to include |
413 // CERT_STATUS_UNABLE_TO_CHECK_REVOCATION. | 398 // CERT_STATUS_UNABLE_TO_CHECK_REVOCATION. |
414 tp_action_data.ActionFlags |= CSSM_TP_ACTION_REQUIRE_REV_PER_CERT; | 399 tp_action_data.ActionFlags |= CSSM_TP_ACTION_REQUIRE_REV_PER_CERT; |
415 verify_result->cert_status |= CERT_STATUS_REV_CHECKING_ENABLED; | |
416 | 400 |
417 // Note, even if revocation checking is disabled, SecTrustEvaluate() will | 401 // Note, even if revocation checking is disabled, SecTrustEvaluate() will |
418 // modify the OCSP options so as to attempt OCSP checking if it believes a | 402 // modify the OCSP options so as to attempt OCSP checking if it believes a |
419 // certificate may chain to an EV root. However, because network fetches | 403 // certificate may chain to an EV root. However, because network fetches |
420 // are disabled in CreateTrustPolicies() when revocation checking is | 404 // are disabled in CreateTrustPolicies() when revocation checking is |
421 // disabled, these will only go against the local cache. | 405 // disabled, these will only go against the local cache. |
422 } | 406 } |
423 | 407 |
424 CFDataRef action_data_ref = | 408 CFDataRef action_data_ref = |
425 CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, | 409 CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, |
426 reinterpret_cast<UInt8*>(&tp_action_data), | 410 reinterpret_cast<UInt8*>(&tp_action_data), |
427 sizeof(tp_action_data), kCFAllocatorNull); | 411 sizeof(tp_action_data), kCFAllocatorNull); |
428 if (!action_data_ref) | 412 if (!action_data_ref) |
429 return ERR_OUT_OF_MEMORY; | 413 return ERR_OUT_OF_MEMORY; |
430 ScopedCFTypeRef<CFDataRef> scoped_action_data_ref(action_data_ref); | 414 ScopedCFTypeRef<CFDataRef> scoped_action_data_ref(action_data_ref); |
431 status = SecTrustSetParameters(trust_ref, CSSM_TP_ACTION_DEFAULT, | 415 status = SecTrustSetParameters(tmp_trust, CSSM_TP_ACTION_DEFAULT, |
432 action_data_ref); | 416 action_data_ref); |
433 if (status) | 417 if (status) |
434 return NetErrorFromOSStatus(status); | 418 return NetErrorFromOSStatus(status); |
435 | 419 |
436 // Verify the certificate. A non-zero result from SecTrustGetResult() | 420 // Verify the certificate. A non-zero result from SecTrustGetResult() |
437 // indicates that some fatal error occurred and the chain couldn't be | 421 // indicates that some fatal error occurred and the chain couldn't be |
438 // processed, not that the chain contains no errors. We need to examine the | 422 // processed, not that the chain contains no errors. We need to examine the |
439 // output of SecTrustGetResult() to determine that. | 423 // output of SecTrustGetResult() to determine that. |
440 SecTrustResultType trust_result; | 424 SecTrustResultType tmp_trust_result; |
441 status = SecTrustEvaluate(trust_ref, &trust_result); | 425 status = SecTrustEvaluate(tmp_trust, &tmp_trust_result); |
442 if (status) | 426 if (status) |
443 return NetErrorFromOSStatus(status); | 427 return NetErrorFromOSStatus(status); |
444 CFArrayRef completed_chain = NULL; | 428 CFArrayRef tmp_verified_chain = NULL; |
445 CSSM_TP_APPLE_EVIDENCE_INFO* chain_info; | 429 CSSM_TP_APPLE_EVIDENCE_INFO* tmp_chain_info; |
446 status = SecTrustGetResult(trust_ref, &trust_result, &completed_chain, | 430 status = SecTrustGetResult(tmp_trust, &tmp_trust_result, &tmp_verified_chain, |
447 &chain_info); | 431 &tmp_chain_info); |
448 if (status) | 432 if (status) |
449 return NetErrorFromOSStatus(status); | 433 return NetErrorFromOSStatus(status); |
450 ScopedCFTypeRef<CFArrayRef> scoped_completed_chain(completed_chain); | 434 |
| 435 trust_ref->swap(scoped_tmp_trust); |
| 436 *trust_result = tmp_trust_result; |
| 437 verified_chain->reset(tmp_verified_chain); |
| 438 *chain_info = tmp_chain_info; |
| 439 |
| 440 return OK; |
| 441 } |
| 442 |
| 443 // OS X ships with both "GTE CyberTrust Global Root" and "Baltimore CyberTrust |
| 444 // Root" as part of its trusted root store. However, a cross-certified version |
| 445 // of the "Baltimore CyberTrust Root" exists that chains to "GTE CyberTrust |
| 446 // Global Root". When OS X/Security.framework attempts to evaluate such a |
| 447 // certificate chain, it disregards the "Baltimore CyberTrust Root" that exists |
| 448 // within Keychain and instead attempts to terminate the chain in the "GTE |
| 449 // CyberTrust Global Root". However, the GTE root is scheduled to be removed in |
| 450 // a future OS X update (for sunsetting purposes), and once removed, such |
| 451 // chains will fail validation, even though a trust anchor still exists. |
| 452 // |
| 453 // Rather than over-generalizing a solution that may mask a number of TLS |
| 454 // misconfigurations, attempt to specifically match the affected |
| 455 // cross-certified certificate and remove it from certificate chain processing. |
| 456 bool IsBadBaltimoreGTECertificate(SecCertificateRef cert) { |
| 457 // Matches the GTE-signed Baltimore CyberTrust Root |
| 458 // https://cacert.omniroot.com/Baltimore-to-GTE-04-12.pem |
| 459 static const SHA1HashValue kBadBaltimoreHashNew = |
| 460 { { 0x4D, 0x34, 0xEA, 0x92, 0x76, 0x4B, 0x3A, 0x31, 0x49, 0x11, |
| 461 0x99, 0x52, 0xF4, 0x19, 0x30, 0xCA, 0x11, 0x34, 0x83, 0x61 } }; |
| 462 // Matches the legacy GTE-signed Baltimore CyberTrust Root |
| 463 // https://cacert.omniroot.com/gte-2-2025.pem |
| 464 static const SHA1HashValue kBadBaltimoreHashOld = |
| 465 { { 0x54, 0xD8, 0xCB, 0x49, 0x1F, 0xA1, 0x6D, 0xF8, 0x87, 0xDC, |
| 466 0x94, 0xA9, 0x34, 0xCC, 0x83, 0x6B, 0xDA, 0xA8, 0xA3, 0x69 } }; |
| 467 |
| 468 SHA1HashValue fingerprint = X509Certificate::CalculateFingerprint(cert); |
| 469 |
| 470 return fingerprint.Equals(kBadBaltimoreHashNew) || |
| 471 fingerprint.Equals(kBadBaltimoreHashOld); |
| 472 } |
| 473 |
| 474 // Attempts to re-verify |cert_array| after adjusting the inputs to work around |
| 475 // known issues in OS X. To be used if BuildAndEvaluateSecTrustRef fails to |
| 476 // return a positive result for verification. |
| 477 // |
| 478 // This function should only be called while the Mac Security Services lock is |
| 479 // held. |
| 480 void RetrySecTrustEvaluateWithAdjustedChain( |
| 481 CFArrayRef cert_array, |
| 482 CFArrayRef trust_policies, |
| 483 int flags, |
| 484 ScopedCFTypeRef<SecTrustRef>* trust_ref, |
| 485 SecTrustResultType* trust_result, |
| 486 ScopedCFTypeRef<CFArrayRef>* verified_chain, |
| 487 CSSM_TP_APPLE_EVIDENCE_INFO** chain_info) { |
| 488 CFIndex count = CFArrayGetCount(*verified_chain); |
| 489 CFIndex slice_point = 0; |
| 490 |
| 491 for (CFIndex i = 1; i < count; ++i) { |
| 492 SecCertificateRef cert = reinterpret_cast<SecCertificateRef>( |
| 493 const_cast<void*>(CFArrayGetValueAtIndex(*verified_chain, i))); |
| 494 if (cert == NULL) |
| 495 return; // Strange times; can't fix things up. |
| 496 |
| 497 if (IsBadBaltimoreGTECertificate(cert)) { |
| 498 slice_point = i; |
| 499 break; |
| 500 } |
| 501 } |
| 502 if (slice_point == 0) |
| 503 return; // Nothing to do. |
| 504 |
| 505 ScopedCFTypeRef<CFMutableArrayRef> adjusted_cert_array( |
| 506 CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks)); |
| 507 // Note: This excludes the certificate at |slice_point|. |
| 508 CFArrayAppendArray(adjusted_cert_array, cert_array, |
| 509 CFRangeMake(0, slice_point)); |
| 510 |
| 511 // Ignore the result; failure will preserve the old verification results. |
| 512 BuildAndEvaluateSecTrustRef( |
| 513 adjusted_cert_array, trust_policies, flags, trust_ref, trust_result, |
| 514 verified_chain, chain_info); |
| 515 } |
| 516 |
| 517 } // namespace |
| 518 |
| 519 CertVerifyProcMac::CertVerifyProcMac() {} |
| 520 |
| 521 CertVerifyProcMac::~CertVerifyProcMac() {} |
| 522 |
| 523 bool CertVerifyProcMac::SupportsAdditionalTrustAnchors() const { |
| 524 return false; |
| 525 } |
| 526 |
| 527 int CertVerifyProcMac::VerifyInternal( |
| 528 X509Certificate* cert, |
| 529 const std::string& hostname, |
| 530 int flags, |
| 531 CRLSet* crl_set, |
| 532 const CertificateList& additional_trust_anchors, |
| 533 CertVerifyResult* verify_result) { |
| 534 ScopedCFTypeRef<CFArrayRef> trust_policies; |
| 535 OSStatus status = CreateTrustPolicies(hostname, flags, &trust_policies); |
| 536 if (status) |
| 537 return NetErrorFromOSStatus(status); |
| 538 |
| 539 // Create and configure a SecTrustRef, which takes our certificate(s) |
| 540 // and our SSL SecPolicyRef. SecTrustCreateWithCertificates() takes an |
| 541 // array of certificates, the first of which is the certificate we're |
| 542 // verifying, and the subsequent (optional) certificates are used for |
| 543 // chain building. |
| 544 ScopedCFTypeRef<CFArrayRef> cert_array(cert->CreateOSCertChainForCert()); |
| 545 |
| 546 // Serialize all calls that may use the Keychain, to work around various |
| 547 // issues in OS X 10.6+ with multi-threaded access to Security.framework. |
| 548 base::AutoLock lock(crypto::GetMacSecurityServicesLock()); |
| 549 |
| 550 ScopedCFTypeRef<SecTrustRef> trust_ref; |
| 551 SecTrustResultType trust_result = kSecTrustResultDeny; |
| 552 ScopedCFTypeRef<CFArrayRef> completed_chain; |
| 553 CSSM_TP_APPLE_EVIDENCE_INFO* chain_info = NULL; |
| 554 |
| 555 int rv = BuildAndEvaluateSecTrustRef( |
| 556 cert_array, trust_policies, flags, &trust_ref, &trust_result, |
| 557 &completed_chain, &chain_info); |
| 558 if (rv != OK) |
| 559 return rv; |
| 560 if (trust_result != kSecTrustResultUnspecified && |
| 561 trust_result != kSecTrustResultProceed) { |
| 562 RetrySecTrustEvaluateWithAdjustedChain( |
| 563 cert_array, trust_policies, flags, &trust_ref, &trust_result, |
| 564 &completed_chain, &chain_info); |
| 565 } |
| 566 |
| 567 if (flags & CertVerifier::VERIFY_REV_CHECKING_ENABLED) |
| 568 verify_result->cert_status |= CERT_STATUS_REV_CHECKING_ENABLED; |
451 | 569 |
452 if (crl_set && !CheckRevocationWithCRLSet(completed_chain, crl_set)) | 570 if (crl_set && !CheckRevocationWithCRLSet(completed_chain, crl_set)) |
453 verify_result->cert_status |= CERT_STATUS_REVOKED; | 571 verify_result->cert_status |= CERT_STATUS_REVOKED; |
454 | 572 |
455 GetCertChainInfo(scoped_completed_chain.get(), chain_info, verify_result); | 573 GetCertChainInfo(completed_chain, chain_info, verify_result); |
456 | 574 |
457 // As of Security Update 2012-002/OS X 10.7.4, when an RSA key < 1024 bits | 575 // As of Security Update 2012-002/OS X 10.7.4, when an RSA key < 1024 bits |
458 // is encountered, CSSM returns CSSMERR_TP_VERIFY_ACTION_FAILED and adds | 576 // is encountered, CSSM returns CSSMERR_TP_VERIFY_ACTION_FAILED and adds |
459 // CSSMERR_CSP_UNSUPPORTED_KEY_SIZE as a certificate status. Avoid mapping | 577 // CSSMERR_CSP_UNSUPPORTED_KEY_SIZE as a certificate status. Avoid mapping |
460 // the CSSMERR_TP_VERIFY_ACTION_FAILED to CERT_STATUS_INVALID if the only | 578 // the CSSMERR_TP_VERIFY_ACTION_FAILED to CERT_STATUS_INVALID if the only |
461 // error was due to an unsupported key size. | 579 // error was due to an unsupported key size. |
462 bool policy_failed = false; | 580 bool policy_failed = false; |
463 bool weak_key = false; | 581 bool weak_key = false; |
464 | 582 |
465 // Evaluate the results | 583 // Evaluate the results |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
588 } | 706 } |
589 } | 707 } |
590 } | 708 } |
591 } | 709 } |
592 } | 710 } |
593 | 711 |
594 return OK; | 712 return OK; |
595 } | 713 } |
596 | 714 |
597 } // namespace net | 715 } // namespace net |
OLD | NEW |