[email protected] | 62b23c2 | 2012-03-22 04:50:24 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 5 | #include "net/cert/cert_verify_proc_mac.h" |
[email protected] | 62b23c2 | 2012-03-22 04:50:24 | [diff] [blame] | 6 | |
| 7 | #include <CommonCrypto/CommonDigest.h> |
| 8 | #include <CoreServices/CoreServices.h> |
| 9 | #include <Security/Security.h> |
| 10 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
[email protected] | 62b23c2 | 2012-03-22 04:50:24 | [diff] [blame] | 14 | #include "base/logging.h" |
| 15 | #include "base/mac/mac_logging.h" |
| 16 | #include "base/mac/scoped_cftyperef.h" |
| 17 | #include "base/sha1.h" |
[email protected] | d069c11a | 2013-04-13 00:01:55 | [diff] [blame] | 18 | #include "base/strings/string_piece.h" |
[email protected] | d6e8fe6 | 2012-10-03 05:46:45 | [diff] [blame] | 19 | #include "base/synchronization/lock.h" |
| 20 | #include "crypto/mac_security_services_lock.h" |
[email protected] | 62b23c2 | 2012-03-22 04:50:24 | [diff] [blame] | 21 | #include "crypto/nss_util.h" |
| 22 | #include "crypto/sha2.h" |
[email protected] | 62b23c2 | 2012-03-22 04:50:24 | [diff] [blame] | 23 | #include "net/base/net_errors.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 24 | #include "net/cert/asn1_util.h" |
| 25 | #include "net/cert/cert_status_flags.h" |
| 26 | #include "net/cert/cert_verifier.h" |
| 27 | #include "net/cert/cert_verify_result.h" |
| 28 | #include "net/cert/crl_set.h" |
| 29 | #include "net/cert/test_root_certs.h" |
| 30 | #include "net/cert/x509_certificate.h" |
| 31 | #include "net/cert/x509_certificate_known_roots_mac.h" |
| 32 | #include "net/cert/x509_util_mac.h" |
[email protected] | 62b23c2 | 2012-03-22 04:50:24 | [diff] [blame] | 33 | |
| 34 | // From 10.7.2 libsecurity_keychain-55035/lib/SecTrustPriv.h, for use with |
| 35 | // SecTrustCopyExtendedResult. |
| 36 | #ifndef kSecEVOrganizationName |
| 37 | #define kSecEVOrganizationName CFSTR("Organization") |
| 38 | #endif |
| 39 | |
| 40 | using base::mac::ScopedCFTypeRef; |
| 41 | |
| 42 | namespace net { |
| 43 | |
| 44 | namespace { |
| 45 | |
| 46 | typedef OSStatus (*SecTrustCopyExtendedResultFuncPtr)(SecTrustRef, |
| 47 | CFDictionaryRef*); |
| 48 | |
| 49 | int NetErrorFromOSStatus(OSStatus status) { |
| 50 | switch (status) { |
| 51 | case noErr: |
| 52 | return OK; |
| 53 | case errSecNotAvailable: |
| 54 | case errSecNoCertificateModule: |
| 55 | case errSecNoPolicyModule: |
| 56 | return ERR_NOT_IMPLEMENTED; |
| 57 | case errSecAuthFailed: |
| 58 | return ERR_ACCESS_DENIED; |
| 59 | default: { |
| 60 | OSSTATUS_LOG(ERROR, status) << "Unknown error mapped to ERR_FAILED"; |
| 61 | return ERR_FAILED; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | CertStatus CertStatusFromOSStatus(OSStatus status) { |
| 67 | switch (status) { |
| 68 | case noErr: |
| 69 | return 0; |
| 70 | |
| 71 | case CSSMERR_TP_INVALID_ANCHOR_CERT: |
| 72 | case CSSMERR_TP_NOT_TRUSTED: |
| 73 | case CSSMERR_TP_INVALID_CERT_AUTHORITY: |
| 74 | return CERT_STATUS_AUTHORITY_INVALID; |
| 75 | |
| 76 | case CSSMERR_TP_CERT_EXPIRED: |
| 77 | case CSSMERR_TP_CERT_NOT_VALID_YET: |
| 78 | // "Expired" and "not yet valid" collapse into a single status. |
| 79 | return CERT_STATUS_DATE_INVALID; |
| 80 | |
| 81 | case CSSMERR_TP_CERT_REVOKED: |
| 82 | case CSSMERR_TP_CERT_SUSPENDED: |
| 83 | return CERT_STATUS_REVOKED; |
| 84 | |
| 85 | case CSSMERR_APPLETP_HOSTNAME_MISMATCH: |
| 86 | return CERT_STATUS_COMMON_NAME_INVALID; |
| 87 | |
| 88 | case CSSMERR_APPLETP_CRL_NOT_FOUND: |
| 89 | case CSSMERR_APPLETP_OCSP_UNAVAILABLE: |
| 90 | case CSSMERR_APPLETP_INCOMPLETE_REVOCATION_CHECK: |
| 91 | return CERT_STATUS_NO_REVOCATION_MECHANISM; |
| 92 | |
| 93 | case CSSMERR_APPLETP_CRL_EXPIRED: |
| 94 | case CSSMERR_APPLETP_CRL_NOT_VALID_YET: |
| 95 | case CSSMERR_APPLETP_CRL_SERVER_DOWN: |
| 96 | case CSSMERR_APPLETP_CRL_NOT_TRUSTED: |
| 97 | case CSSMERR_APPLETP_CRL_INVALID_ANCHOR_CERT: |
| 98 | case CSSMERR_APPLETP_CRL_POLICY_FAIL: |
| 99 | case CSSMERR_APPLETP_OCSP_BAD_RESPONSE: |
| 100 | case CSSMERR_APPLETP_OCSP_BAD_REQUEST: |
| 101 | case CSSMERR_APPLETP_OCSP_STATUS_UNRECOGNIZED: |
| 102 | case CSSMERR_APPLETP_NETWORK_FAILURE: |
| 103 | case CSSMERR_APPLETP_OCSP_NOT_TRUSTED: |
| 104 | case CSSMERR_APPLETP_OCSP_INVALID_ANCHOR_CERT: |
| 105 | case CSSMERR_APPLETP_OCSP_SIG_ERROR: |
| 106 | case CSSMERR_APPLETP_OCSP_NO_SIGNER: |
| 107 | case CSSMERR_APPLETP_OCSP_RESP_MALFORMED_REQ: |
| 108 | case CSSMERR_APPLETP_OCSP_RESP_INTERNAL_ERR: |
| 109 | case CSSMERR_APPLETP_OCSP_RESP_TRY_LATER: |
| 110 | case CSSMERR_APPLETP_OCSP_RESP_SIG_REQUIRED: |
| 111 | case CSSMERR_APPLETP_OCSP_RESP_UNAUTHORIZED: |
| 112 | case CSSMERR_APPLETP_OCSP_NONCE_MISMATCH: |
| 113 | // We asked for a revocation check, but didn't get it. |
| 114 | return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION; |
| 115 | |
| 116 | case CSSMERR_APPLETP_CRL_BAD_URI: |
| 117 | case CSSMERR_APPLETP_IDP_FAIL: |
| 118 | return CERT_STATUS_INVALID; |
| 119 | |
[email protected] | 58484ca | 2012-05-29 21:56:34 | [diff] [blame] | 120 | case CSSMERR_CSP_UNSUPPORTED_KEY_SIZE: |
| 121 | // Mapping UNSUPPORTED_KEY_SIZE to CERT_STATUS_WEAK_KEY is not strictly |
| 122 | // accurate, as the error may have been returned due to a key size |
| 123 | // that exceeded the maximum supported. However, within |
| 124 | // CertVerifyProcMac::VerifyInternal(), this code should only be |
| 125 | // encountered as a certificate status code, and only when the key size |
| 126 | // is smaller than the minimum required (1024 bits). |
| 127 | return CERT_STATUS_WEAK_KEY; |
| 128 | |
[email protected] | 62b23c2 | 2012-03-22 04:50:24 | [diff] [blame] | 129 | default: { |
| 130 | // Failure was due to something Chromium doesn't define a |
| 131 | // specific status for (such as basic constraints violation, or |
| 132 | // unknown critical extension) |
| 133 | OSSTATUS_LOG(WARNING, status) |
| 134 | << "Unknown error mapped to CERT_STATUS_INVALID"; |
| 135 | return CERT_STATUS_INVALID; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // Creates a series of SecPolicyRefs to be added to a SecTrustRef used to |
| 141 | // validate a certificate for an SSL server. |hostname| contains the name of |
| 142 | // the SSL server that the certificate should be verified against. |flags| is |
| 143 | // a bitwise-OR of VerifyFlags that can further alter how trust is validated, |
| 144 | // such as how revocation is checked. If successful, returns noErr, and |
| 145 | // stores the resultant array of SecPolicyRefs in |policies|. |
| 146 | OSStatus CreateTrustPolicies(const std::string& hostname, |
| 147 | int flags, |
| 148 | ScopedCFTypeRef<CFArrayRef>* policies) { |
| 149 | ScopedCFTypeRef<CFMutableArrayRef> local_policies( |
| 150 | CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks)); |
| 151 | if (!local_policies) |
| 152 | return memFullErr; |
| 153 | |
| 154 | SecPolicyRef ssl_policy; |
| 155 | OSStatus status = x509_util::CreateSSLServerPolicy(hostname, &ssl_policy); |
| 156 | if (status) |
| 157 | return status; |
| 158 | CFArrayAppendValue(local_policies, ssl_policy); |
| 159 | CFRelease(ssl_policy); |
| 160 | |
| 161 | // Explicitly add revocation policies, in order to override system |
| 162 | // revocation checking policies and instead respect the application-level |
| 163 | // revocation preference. |
| 164 | status = x509_util::CreateRevocationPolicies( |
[email protected] | 8738e0d7 | 2012-08-23 02:00:47 | [diff] [blame] | 165 | (flags & CertVerifier::VERIFY_REV_CHECKING_ENABLED), |
| 166 | (flags & CertVerifier::VERIFY_REV_CHECKING_ENABLED_EV_ONLY), |
[email protected] | 62b23c2 | 2012-03-22 04:50:24 | [diff] [blame] | 167 | local_policies); |
| 168 | if (status) |
| 169 | return status; |
| 170 | |
| 171 | policies->reset(local_policies.release()); |
| 172 | return noErr; |
| 173 | } |
| 174 | |
| 175 | // Saves some information about the certificate chain |cert_chain| in |
| 176 | // |*verify_result|. The caller MUST initialize |*verify_result| before |
| 177 | // calling this function. |
| 178 | void GetCertChainInfo(CFArrayRef cert_chain, |
| 179 | CSSM_TP_APPLE_EVIDENCE_INFO* chain_info, |
| 180 | CertVerifyResult* verify_result) { |
| 181 | SecCertificateRef verified_cert = NULL; |
| 182 | std::vector<SecCertificateRef> verified_chain; |
| 183 | for (CFIndex i = 0, count = CFArrayGetCount(cert_chain); i < count; ++i) { |
| 184 | SecCertificateRef chain_cert = reinterpret_cast<SecCertificateRef>( |
| 185 | const_cast<void*>(CFArrayGetValueAtIndex(cert_chain, i))); |
| 186 | if (i == 0) { |
| 187 | verified_cert = chain_cert; |
| 188 | } else { |
| 189 | verified_chain.push_back(chain_cert); |
| 190 | } |
| 191 | |
| 192 | if ((chain_info[i].StatusBits & CSSM_CERT_STATUS_IS_IN_ANCHORS) || |
| 193 | (chain_info[i].StatusBits & CSSM_CERT_STATUS_IS_ROOT)) { |
| 194 | // The current certificate is either in the user's trusted store or is |
| 195 | // a root (self-signed) certificate. Ignore the signature algorithm for |
| 196 | // these certificates, as it is meaningless for security. We allow |
| 197 | // self-signed certificates (i == 0 & IS_ROOT), since we accept that |
| 198 | // any security assertions by such a cert are inherently meaningless. |
| 199 | continue; |
| 200 | } |
| 201 | |
| 202 | x509_util::CSSMCachedCertificate cached_cert; |
| 203 | OSStatus status = cached_cert.Init(chain_cert); |
| 204 | if (status) |
| 205 | continue; |
| 206 | x509_util::CSSMFieldValue signature_field; |
| 207 | status = cached_cert.GetField(&CSSMOID_X509V1SignatureAlgorithm, |
| 208 | &signature_field); |
| 209 | if (status || !signature_field.field()) |
| 210 | continue; |
| 211 | // Match the behaviour of OS X system tools and defensively check that |
| 212 | // sizes are appropriate. This would indicate a critical failure of the |
| 213 | // OS X certificate library, but based on history, it is best to play it |
| 214 | // safe. |
| 215 | const CSSM_X509_ALGORITHM_IDENTIFIER* sig_algorithm = |
| 216 | signature_field.GetAs<CSSM_X509_ALGORITHM_IDENTIFIER>(); |
| 217 | if (!sig_algorithm) |
| 218 | continue; |
| 219 | |
| 220 | const CSSM_OID* alg_oid = &sig_algorithm->algorithm; |
| 221 | if (CSSMOIDEqual(alg_oid, &CSSMOID_MD2WithRSA)) { |
| 222 | verify_result->has_md2 = true; |
| 223 | if (i != 0) |
| 224 | verify_result->has_md2_ca = true; |
| 225 | } else if (CSSMOIDEqual(alg_oid, &CSSMOID_MD4WithRSA)) { |
| 226 | verify_result->has_md4 = true; |
| 227 | } else if (CSSMOIDEqual(alg_oid, &CSSMOID_MD5WithRSA)) { |
| 228 | verify_result->has_md5 = true; |
| 229 | if (i != 0) |
| 230 | verify_result->has_md5_ca = true; |
| 231 | } |
| 232 | } |
| 233 | if (!verified_cert) |
| 234 | return; |
| 235 | |
| 236 | verify_result->verified_cert = |
| 237 | X509Certificate::CreateFromHandle(verified_cert, verified_chain); |
| 238 | } |
| 239 | |
| 240 | void AppendPublicKeyHashes(CFArrayRef chain, |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 241 | HashValueVector* hashes) { |
[email protected] | 62b23c2 | 2012-03-22 04:50:24 | [diff] [blame] | 242 | const CFIndex n = CFArrayGetCount(chain); |
| 243 | for (CFIndex i = 0; i < n; i++) { |
| 244 | SecCertificateRef cert = reinterpret_cast<SecCertificateRef>( |
| 245 | const_cast<void*>(CFArrayGetValueAtIndex(chain, i))); |
| 246 | |
| 247 | CSSM_DATA cert_data; |
| 248 | OSStatus err = SecCertificateGetData(cert, &cert_data); |
| 249 | DCHECK_EQ(err, noErr); |
| 250 | base::StringPiece der_bytes(reinterpret_cast<const char*>(cert_data.Data), |
| 251 | cert_data.Length); |
| 252 | base::StringPiece spki_bytes; |
| 253 | if (!asn1::ExtractSPKIFromDERCert(der_bytes, &spki_bytes)) |
| 254 | continue; |
| 255 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 256 | HashValue sha1(HASH_VALUE_SHA1); |
| 257 | CC_SHA1(spki_bytes.data(), spki_bytes.size(), sha1.data()); |
| 258 | hashes->push_back(sha1); |
| 259 | |
| 260 | HashValue sha256(HASH_VALUE_SHA256); |
| 261 | CC_SHA256(spki_bytes.data(), spki_bytes.size(), sha256.data()); |
| 262 | hashes->push_back(sha256); |
[email protected] | 62b23c2 | 2012-03-22 04:50:24 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
| 266 | bool CheckRevocationWithCRLSet(CFArrayRef chain, CRLSet* crl_set) { |
| 267 | if (CFArrayGetCount(chain) == 0) |
| 268 | return true; |
| 269 | |
| 270 | // We iterate from the root certificate down to the leaf, keeping track of |
| 271 | // the issuer's SPKI at each step. |
| 272 | std::string issuer_spki_hash; |
| 273 | for (CFIndex i = CFArrayGetCount(chain) - 1; i >= 0; i--) { |
| 274 | SecCertificateRef cert = reinterpret_cast<SecCertificateRef>( |
| 275 | const_cast<void*>(CFArrayGetValueAtIndex(chain, i))); |
| 276 | |
| 277 | CSSM_DATA cert_data; |
| 278 | OSStatus err = SecCertificateGetData(cert, &cert_data); |
| 279 | if (err != noErr) { |
| 280 | NOTREACHED(); |
| 281 | continue; |
| 282 | } |
| 283 | base::StringPiece der_bytes(reinterpret_cast<const char*>(cert_data.Data), |
| 284 | cert_data.Length); |
| 285 | base::StringPiece spki; |
| 286 | if (!asn1::ExtractSPKIFromDERCert(der_bytes, &spki)) { |
| 287 | NOTREACHED(); |
| 288 | continue; |
| 289 | } |
| 290 | |
| 291 | const std::string spki_hash = crypto::SHA256HashString(spki); |
| 292 | x509_util::CSSMCachedCertificate cached_cert; |
| 293 | if (cached_cert.Init(cert) != CSSM_OK) { |
| 294 | NOTREACHED(); |
| 295 | continue; |
| 296 | } |
| 297 | x509_util::CSSMFieldValue serial_number; |
| 298 | err = cached_cert.GetField(&CSSMOID_X509V1SerialNumber, &serial_number); |
| 299 | if (err || !serial_number.field()) { |
| 300 | NOTREACHED(); |
| 301 | continue; |
| 302 | } |
| 303 | |
| 304 | base::StringPiece serial( |
| 305 | reinterpret_cast<const char*>(serial_number.field()->Data), |
| 306 | serial_number.field()->Length); |
| 307 | |
| 308 | CRLSet::Result result = crl_set->CheckSPKI(spki_hash); |
| 309 | |
| 310 | if (result != CRLSet::REVOKED && !issuer_spki_hash.empty()) |
| 311 | result = crl_set->CheckSerial(serial, issuer_spki_hash); |
| 312 | |
| 313 | issuer_spki_hash = spki_hash; |
| 314 | |
| 315 | switch (result) { |
| 316 | case CRLSet::REVOKED: |
| 317 | return false; |
| 318 | case CRLSet::UNKNOWN: |
| 319 | case CRLSet::GOOD: |
| 320 | continue; |
| 321 | default: |
| 322 | NOTREACHED(); |
| 323 | return false; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | return true; |
| 328 | } |
| 329 | |
| 330 | // IsIssuedByKnownRoot returns true if the given chain is rooted at a root CA |
| 331 | // that we recognise as a standard root. |
| 332 | // static |
| 333 | bool IsIssuedByKnownRoot(CFArrayRef chain) { |
| 334 | int n = CFArrayGetCount(chain); |
| 335 | if (n < 1) |
| 336 | return false; |
| 337 | SecCertificateRef root_ref = reinterpret_cast<SecCertificateRef>( |
| 338 | const_cast<void*>(CFArrayGetValueAtIndex(chain, n - 1))); |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 339 | SHA1HashValue hash = X509Certificate::CalculateFingerprint(root_ref); |
[email protected] | 62b23c2 | 2012-03-22 04:50:24 | [diff] [blame] | 340 | return IsSHA1HashInSortedArray( |
| 341 | hash, &kKnownRootCertSHA1Hashes[0][0], sizeof(kKnownRootCertSHA1Hashes)); |
| 342 | } |
| 343 | |
| 344 | } // namespace |
| 345 | |
| 346 | CertVerifyProcMac::CertVerifyProcMac() {} |
| 347 | |
| 348 | CertVerifyProcMac::~CertVerifyProcMac() {} |
| 349 | |
[email protected] | ef15512 | 2013-03-23 19:11:24 | [diff] [blame] | 350 | bool CertVerifyProcMac::SupportsAdditionalTrustAnchors() const { |
| 351 | return false; |
| 352 | } |
| 353 | |
| 354 | int CertVerifyProcMac::VerifyInternal( |
| 355 | X509Certificate* cert, |
| 356 | const std::string& hostname, |
| 357 | int flags, |
| 358 | CRLSet* crl_set, |
| 359 | const CertificateList& additional_trust_anchors, |
| 360 | CertVerifyResult* verify_result) { |
[email protected] | 62b23c2 | 2012-03-22 04:50:24 | [diff] [blame] | 361 | ScopedCFTypeRef<CFArrayRef> trust_policies; |
| 362 | OSStatus status = CreateTrustPolicies(hostname, flags, &trust_policies); |
| 363 | if (status) |
| 364 | return NetErrorFromOSStatus(status); |
| 365 | |
| 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 | |
[email protected] | d6e8fe6 | 2012-10-03 05:46:45 | [diff] [blame] | 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()); |
[email protected] | 62b23c2 | 2012-03-22 04:50:24 | [diff] [blame] | 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 | |
| 384 | if (TestRootCerts::HasInstance()) { |
| 385 | status = TestRootCerts::GetInstance()->FixupSecTrustRef(trust_ref); |
| 386 | if (status) |
| 387 | return NetErrorFromOSStatus(status); |
| 388 | } |
| 389 | |
| 390 | CSSM_APPLE_TP_ACTION_DATA tp_action_data; |
| 391 | memset(&tp_action_data, 0, sizeof(tp_action_data)); |
| 392 | tp_action_data.Version = CSSM_APPLE_TP_ACTION_VERSION; |
| 393 | // Allow CSSM to download any missing intermediate certificates if an |
| 394 | // authorityInfoAccess extension or issuerAltName extension is present. |
| 395 | tp_action_data.ActionFlags = CSSM_TP_ACTION_FETCH_CERT_FROM_NET | |
| 396 | CSSM_TP_ACTION_TRUST_SETTINGS; |
| 397 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 398 | // Note: For EV certificates, the Apple TP will handle setting these flags |
| 399 | // as part of EV evaluation. |
[email protected] | 8738e0d7 | 2012-08-23 02:00:47 | [diff] [blame] | 400 | if (flags & CertVerifier::VERIFY_REV_CHECKING_ENABLED) { |
[email protected] | 62b23c2 | 2012-03-22 04:50:24 | [diff] [blame] | 401 | // Require a positive result from an OCSP responder or a CRL (or both) |
| 402 | // for every certificate in the chain. The Apple TP automatically |
| 403 | // excludes the self-signed root from this requirement. If a certificate |
| 404 | // is missing both a crlDistributionPoints extension and an |
| 405 | // authorityInfoAccess extension with an OCSP responder URL, then we |
| 406 | // will get a kSecTrustResultRecoverableTrustFailure back from |
| 407 | // SecTrustEvaluate(), with a |
| 408 | // CSSMERR_APPLETP_INCOMPLETE_REVOCATION_CHECK error code. In that case, |
| 409 | // we'll set our own result to include |
| 410 | // CERT_STATUS_NO_REVOCATION_MECHANISM. If one or both extensions are |
| 411 | // present, and a check fails (server unavailable, OCSP retry later, |
| 412 | // signature mismatch), then we'll set our own result to include |
| 413 | // CERT_STATUS_UNABLE_TO_CHECK_REVOCATION. |
| 414 | tp_action_data.ActionFlags |= CSSM_TP_ACTION_REQUIRE_REV_PER_CERT; |
| 415 | verify_result->cert_status |= CERT_STATUS_REV_CHECKING_ENABLED; |
| 416 | |
| 417 | // Note, even if revocation checking is disabled, SecTrustEvaluate() will |
| 418 | // 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 |
| 420 | // are disabled in CreateTrustPolicies() when revocation checking is |
| 421 | // disabled, these will only go against the local cache. |
| 422 | } |
| 423 | |
| 424 | CFDataRef action_data_ref = |
| 425 | CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, |
| 426 | reinterpret_cast<UInt8*>(&tp_action_data), |
| 427 | sizeof(tp_action_data), kCFAllocatorNull); |
| 428 | if (!action_data_ref) |
| 429 | return ERR_OUT_OF_MEMORY; |
| 430 | ScopedCFTypeRef<CFDataRef> scoped_action_data_ref(action_data_ref); |
| 431 | status = SecTrustSetParameters(trust_ref, CSSM_TP_ACTION_DEFAULT, |
| 432 | action_data_ref); |
| 433 | if (status) |
| 434 | return NetErrorFromOSStatus(status); |
| 435 | |
| 436 | // Verify the certificate. A non-zero result from SecTrustGetResult() |
| 437 | // 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 |
| 439 | // output of SecTrustGetResult() to determine that. |
| 440 | SecTrustResultType trust_result; |
| 441 | status = SecTrustEvaluate(trust_ref, &trust_result); |
| 442 | if (status) |
| 443 | return NetErrorFromOSStatus(status); |
| 444 | CFArrayRef completed_chain = NULL; |
| 445 | CSSM_TP_APPLE_EVIDENCE_INFO* chain_info; |
| 446 | status = SecTrustGetResult(trust_ref, &trust_result, &completed_chain, |
| 447 | &chain_info); |
| 448 | if (status) |
| 449 | return NetErrorFromOSStatus(status); |
| 450 | ScopedCFTypeRef<CFArrayRef> scoped_completed_chain(completed_chain); |
| 451 | |
| 452 | if (crl_set && !CheckRevocationWithCRLSet(completed_chain, crl_set)) |
| 453 | verify_result->cert_status |= CERT_STATUS_REVOKED; |
| 454 | |
| 455 | GetCertChainInfo(scoped_completed_chain.get(), chain_info, verify_result); |
| 456 | |
[email protected] | 58484ca | 2012-05-29 21:56:34 | [diff] [blame] | 457 | // 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 |
| 459 | // 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 |
| 461 | // error was due to an unsupported key size. |
| 462 | bool policy_failed = false; |
| 463 | bool weak_key = false; |
| 464 | |
[email protected] | 62b23c2 | 2012-03-22 04:50:24 | [diff] [blame] | 465 | // Evaluate the results |
| 466 | OSStatus cssm_result; |
| 467 | switch (trust_result) { |
| 468 | case kSecTrustResultUnspecified: |
| 469 | case kSecTrustResultProceed: |
| 470 | // Certificate chain is valid and trusted ("unspecified" indicates that |
| 471 | // the user has not explicitly set a trust setting) |
| 472 | break; |
| 473 | |
| 474 | case kSecTrustResultDeny: |
| 475 | case kSecTrustResultConfirm: |
| 476 | // Certificate chain is explicitly untrusted. For kSecTrustResultConfirm, |
| 477 | // we're following what Secure Transport does and treating it as |
| 478 | // "deny". |
| 479 | verify_result->cert_status |= CERT_STATUS_AUTHORITY_INVALID; |
| 480 | break; |
| 481 | |
| 482 | case kSecTrustResultRecoverableTrustFailure: |
| 483 | // Certificate chain has a failure that can be overridden by the user. |
| 484 | status = SecTrustGetCssmResultCode(trust_ref, &cssm_result); |
| 485 | if (status) |
| 486 | return NetErrorFromOSStatus(status); |
[email protected] | 58484ca | 2012-05-29 21:56:34 | [diff] [blame] | 487 | if (cssm_result == CSSMERR_TP_VERIFY_ACTION_FAILED) { |
| 488 | policy_failed = true; |
| 489 | } else { |
| 490 | verify_result->cert_status |= CertStatusFromOSStatus(cssm_result); |
| 491 | } |
[email protected] | 62b23c2 | 2012-03-22 04:50:24 | [diff] [blame] | 492 | // Walk the chain of error codes in the CSSM_TP_APPLE_EVIDENCE_INFO |
| 493 | // structure which can catch multiple errors from each certificate. |
| 494 | for (CFIndex index = 0, chain_count = CFArrayGetCount(completed_chain); |
| 495 | index < chain_count; ++index) { |
| 496 | if (chain_info[index].StatusBits & CSSM_CERT_STATUS_EXPIRED || |
| 497 | chain_info[index].StatusBits & CSSM_CERT_STATUS_NOT_VALID_YET) |
| 498 | verify_result->cert_status |= CERT_STATUS_DATE_INVALID; |
| 499 | if (!IsCertStatusError(verify_result->cert_status) && |
| 500 | chain_info[index].NumStatusCodes == 0) { |
| 501 | LOG(WARNING) << "chain_info[" << index << "].NumStatusCodes is 0" |
| 502 | ", chain_info[" << index << "].StatusBits is " |
| 503 | << chain_info[index].StatusBits; |
| 504 | } |
| 505 | for (uint32 status_code_index = 0; |
| 506 | status_code_index < chain_info[index].NumStatusCodes; |
| 507 | ++status_code_index) { |
[email protected] | 58484ca | 2012-05-29 21:56:34 | [diff] [blame] | 508 | CertStatus mapped_status = CertStatusFromOSStatus( |
[email protected] | 62b23c2 | 2012-03-22 04:50:24 | [diff] [blame] | 509 | chain_info[index].StatusCodes[status_code_index]); |
[email protected] | 58484ca | 2012-05-29 21:56:34 | [diff] [blame] | 510 | if (mapped_status == CERT_STATUS_WEAK_KEY) |
| 511 | weak_key = true; |
| 512 | verify_result->cert_status |= mapped_status; |
[email protected] | 62b23c2 | 2012-03-22 04:50:24 | [diff] [blame] | 513 | } |
| 514 | } |
[email protected] | 58484ca | 2012-05-29 21:56:34 | [diff] [blame] | 515 | if (policy_failed && !weak_key) { |
| 516 | // If CSSMERR_TP_VERIFY_ACTION_FAILED wasn't returned due to a weak |
| 517 | // key, map it back to an appropriate error code. |
| 518 | verify_result->cert_status |= CertStatusFromOSStatus(cssm_result); |
| 519 | } |
[email protected] | 62b23c2 | 2012-03-22 04:50:24 | [diff] [blame] | 520 | if (!IsCertStatusError(verify_result->cert_status)) { |
| 521 | LOG(ERROR) << "cssm_result=" << cssm_result; |
| 522 | verify_result->cert_status |= CERT_STATUS_INVALID; |
| 523 | NOTREACHED(); |
| 524 | } |
| 525 | break; |
| 526 | |
| 527 | default: |
| 528 | status = SecTrustGetCssmResultCode(trust_ref, &cssm_result); |
| 529 | if (status) |
| 530 | return NetErrorFromOSStatus(status); |
| 531 | verify_result->cert_status |= CertStatusFromOSStatus(cssm_result); |
| 532 | if (!IsCertStatusError(verify_result->cert_status)) { |
| 533 | LOG(WARNING) << "trust_result=" << trust_result; |
| 534 | verify_result->cert_status |= CERT_STATUS_INVALID; |
| 535 | } |
| 536 | break; |
| 537 | } |
| 538 | |
| 539 | // Perform hostname verification independent of SecTrustEvaluate. In order to |
| 540 | // do so, mask off any reported name errors first. |
| 541 | verify_result->cert_status &= ~CERT_STATUS_COMMON_NAME_INVALID; |
| 542 | if (!cert->VerifyNameMatch(hostname)) |
| 543 | verify_result->cert_status |= CERT_STATUS_COMMON_NAME_INVALID; |
| 544 | |
| 545 | // TODO(wtc): Suppress CERT_STATUS_NO_REVOCATION_MECHANISM for now to be |
| 546 | // compatible with Windows, which in turn implements this behavior to be |
| 547 | // compatible with WinHTTP, which doesn't report this error (bug 3004). |
| 548 | verify_result->cert_status &= ~CERT_STATUS_NO_REVOCATION_MECHANISM; |
| 549 | |
[email protected] | 8b4a61a | 2012-11-28 22:57:20 | [diff] [blame] | 550 | AppendPublicKeyHashes(completed_chain, &verify_result->public_key_hashes); |
| 551 | verify_result->is_issued_by_known_root = IsIssuedByKnownRoot(completed_chain); |
| 552 | |
[email protected] | 62b23c2 | 2012-03-22 04:50:24 | [diff] [blame] | 553 | if (IsCertStatusError(verify_result->cert_status)) |
| 554 | return MapCertStatusToNetError(verify_result->cert_status); |
| 555 | |
[email protected] | 8738e0d7 | 2012-08-23 02:00:47 | [diff] [blame] | 556 | if (flags & CertVerifier::VERIFY_EV_CERT) { |
[email protected] | 62b23c2 | 2012-03-22 04:50:24 | [diff] [blame] | 557 | // Determine the certificate's EV status using SecTrustCopyExtendedResult(), |
[email protected] | 7c23be8 | 2013-04-29 20:51:19 | [diff] [blame^] | 558 | // which we need to look up because the function wasn't added until |
| 559 | // Mac OS X 10.5.7. |
[email protected] | 62b23c2 | 2012-03-22 04:50:24 | [diff] [blame] | 560 | // Note: "ExtendedResult" means extended validation results. |
| 561 | CFBundleRef bundle = |
| 562 | CFBundleGetBundleWithIdentifier(CFSTR("com.apple.security")); |
| 563 | if (bundle) { |
| 564 | SecTrustCopyExtendedResultFuncPtr copy_extended_result = |
| 565 | reinterpret_cast<SecTrustCopyExtendedResultFuncPtr>( |
| 566 | CFBundleGetFunctionPointerForName(bundle, |
| 567 | CFSTR("SecTrustCopyExtendedResult"))); |
| 568 | if (copy_extended_result) { |
| 569 | CFDictionaryRef ev_dict_temp = NULL; |
| 570 | status = copy_extended_result(trust_ref, &ev_dict_temp); |
| 571 | ScopedCFTypeRef<CFDictionaryRef> ev_dict(ev_dict_temp); |
| 572 | ev_dict_temp = NULL; |
| 573 | if (status == noErr && ev_dict) { |
| 574 | // In 10.7.3, SecTrustCopyExtendedResult returns noErr and populates |
| 575 | // ev_dict even for non-EV certificates, but only EV certificates |
| 576 | // will cause ev_dict to contain kSecEVOrganizationName. In previous |
| 577 | // releases, SecTrustCopyExtendedResult would only return noErr and |
| 578 | // populate ev_dict for EV certificates, but would always include |
| 579 | // kSecEVOrganizationName in that case, so checking for this key is |
| 580 | // appropriate for all known versions of SecTrustCopyExtendedResult. |
| 581 | // The actual organization name is unneeded here and can be accessed |
| 582 | // through other means. All that matters here is the OS' conception |
| 583 | // of whether or not the certificate is EV. |
| 584 | if (CFDictionaryContainsKey(ev_dict, |
| 585 | kSecEVOrganizationName)) { |
| 586 | verify_result->cert_status |= CERT_STATUS_IS_EV; |
[email protected] | 8738e0d7 | 2012-08-23 02:00:47 | [diff] [blame] | 587 | if (flags & CertVerifier::VERIFY_REV_CHECKING_ENABLED_EV_ONLY) |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 588 | verify_result->cert_status |= CERT_STATUS_REV_CHECKING_ENABLED; |
[email protected] | 62b23c2 | 2012-03-22 04:50:24 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | |
[email protected] | 62b23c2 | 2012-03-22 04:50:24 | [diff] [blame] | 595 | return OK; |
| 596 | } |
| 597 | |
| 598 | } // namespace net |