blob: 9b6e28789b4cb43341b5bf07fec5d5c18c60b1ee [file] [log] [blame]
[email protected]62b23c22012-03-22 04:50:241// 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
5#include "net/base/cert_verify_proc_mac.h"
6
7#include <CommonCrypto/CommonDigest.h>
8#include <CoreServices/CoreServices.h>
9#include <Security/Security.h>
10
[email protected]ede03212012-09-07 12:52:2611#include <string>
12#include <vector>
13
[email protected]62b23c22012-03-22 04:50:2414#include "base/logging.h"
15#include "base/mac/mac_logging.h"
16#include "base/mac/scoped_cftyperef.h"
17#include "base/sha1.h"
18#include "base/string_piece.h"
[email protected]d6e8fe62012-10-03 05:46:4519#include "base/synchronization/lock.h"
20#include "crypto/mac_security_services_lock.h"
[email protected]62b23c22012-03-22 04:50:2421#include "crypto/nss_util.h"
22#include "crypto/sha2.h"
23#include "net/base/asn1_util.h"
24#include "net/base/cert_status_flags.h"
[email protected]8738e0d72012-08-23 02:00:4725#include "net/base/cert_verifier.h"
[email protected]62b23c22012-03-22 04:50:2426#include "net/base/cert_verify_result.h"
27#include "net/base/crl_set.h"
28#include "net/base/net_errors.h"
29#include "net/base/test_root_certs.h"
30#include "net/base/x509_certificate.h"
31#include "net/base/x509_certificate_known_roots_mac.h"
32#include "net/base/x509_util_mac.h"
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
40using base::mac::ScopedCFTypeRef;
41
42namespace net {
43
44namespace {
45
46typedef OSStatus (*SecTrustCopyExtendedResultFuncPtr)(SecTrustRef,
47 CFDictionaryRef*);
48
49int 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
66CertStatus 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]58484ca2012-05-29 21:56:34120 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]62b23c22012-03-22 04:50:24129 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|.
146OSStatus 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]8738e0d72012-08-23 02:00:47165 (flags & CertVerifier::VERIFY_REV_CHECKING_ENABLED),
166 (flags & CertVerifier::VERIFY_REV_CHECKING_ENABLED_EV_ONLY),
[email protected]62b23c22012-03-22 04:50:24167 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.
178void 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
240void AppendPublicKeyHashes(CFArrayRef chain,
[email protected]ede03212012-09-07 12:52:26241 HashValueVector* hashes) {
[email protected]62b23c22012-03-22 04:50:24242 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]ede03212012-09-07 12:52:26256 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]62b23c22012-03-22 04:50:24263 }
264}
265
266bool 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
333bool 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]ede03212012-09-07 12:52:26339 SHA1HashValue hash = X509Certificate::CalculateFingerprint(root_ref);
[email protected]62b23c22012-03-22 04:50:24340 return IsSHA1HashInSortedArray(
341 hash, &kKnownRootCertSHA1Hashes[0][0], sizeof(kKnownRootCertSHA1Hashes));
342}
343
344} // namespace
345
346CertVerifyProcMac::CertVerifyProcMac() {}
347
348CertVerifyProcMac::~CertVerifyProcMac() {}
349
350int CertVerifyProcMac::VerifyInternal(X509Certificate* cert,
351 const std::string& hostname,
352 int flags,
353 CRLSet* crl_set,
354 CertVerifyResult* verify_result) {
355 ScopedCFTypeRef<CFArrayRef> trust_policies;
356 OSStatus status = CreateTrustPolicies(hostname, flags, &trust_policies);
357 if (status)
358 return NetErrorFromOSStatus(status);
359
360 // Create and configure a SecTrustRef, which takes our certificate(s)
361 // and our SSL SecPolicyRef. SecTrustCreateWithCertificates() takes an
362 // array of certificates, the first of which is the certificate we're
363 // verifying, and the subsequent (optional) certificates are used for
364 // chain building.
365 ScopedCFTypeRef<CFArrayRef> cert_array(cert->CreateOSCertChainForCert());
366
[email protected]d6e8fe62012-10-03 05:46:45367 // Serialize all calls that may use the Keychain, to work around various
368 // issues in OS X 10.6+ with multi-threaded access to Security.framework.
369 base::AutoLock lock(crypto::GetMacSecurityServicesLock());
[email protected]62b23c22012-03-22 04:50:24370
371 SecTrustRef trust_ref = NULL;
372 status = SecTrustCreateWithCertificates(cert_array, trust_policies,
373 &trust_ref);
374 if (status)
375 return NetErrorFromOSStatus(status);
376 ScopedCFTypeRef<SecTrustRef> scoped_trust_ref(trust_ref);
377
378 if (TestRootCerts::HasInstance()) {
379 status = TestRootCerts::GetInstance()->FixupSecTrustRef(trust_ref);
380 if (status)
381 return NetErrorFromOSStatus(status);
382 }
383
384 CSSM_APPLE_TP_ACTION_DATA tp_action_data;
385 memset(&tp_action_data, 0, sizeof(tp_action_data));
386 tp_action_data.Version = CSSM_APPLE_TP_ACTION_VERSION;
387 // Allow CSSM to download any missing intermediate certificates if an
388 // authorityInfoAccess extension or issuerAltName extension is present.
389 tp_action_data.ActionFlags = CSSM_TP_ACTION_FETCH_CERT_FROM_NET |
390 CSSM_TP_ACTION_TRUST_SETTINGS;
391
[email protected]b6f2de32012-08-17 04:35:08392 // Note: For EV certificates, the Apple TP will handle setting these flags
393 // as part of EV evaluation.
[email protected]8738e0d72012-08-23 02:00:47394 if (flags & CertVerifier::VERIFY_REV_CHECKING_ENABLED) {
[email protected]62b23c22012-03-22 04:50:24395 // Require a positive result from an OCSP responder or a CRL (or both)
396 // for every certificate in the chain. The Apple TP automatically
397 // excludes the self-signed root from this requirement. If a certificate
398 // is missing both a crlDistributionPoints extension and an
399 // authorityInfoAccess extension with an OCSP responder URL, then we
400 // will get a kSecTrustResultRecoverableTrustFailure back from
401 // SecTrustEvaluate(), with a
402 // CSSMERR_APPLETP_INCOMPLETE_REVOCATION_CHECK error code. In that case,
403 // we'll set our own result to include
404 // CERT_STATUS_NO_REVOCATION_MECHANISM. If one or both extensions are
405 // present, and a check fails (server unavailable, OCSP retry later,
406 // signature mismatch), then we'll set our own result to include
407 // CERT_STATUS_UNABLE_TO_CHECK_REVOCATION.
408 tp_action_data.ActionFlags |= CSSM_TP_ACTION_REQUIRE_REV_PER_CERT;
409 verify_result->cert_status |= CERT_STATUS_REV_CHECKING_ENABLED;
410
411 // Note, even if revocation checking is disabled, SecTrustEvaluate() will
412 // modify the OCSP options so as to attempt OCSP checking if it believes a
413 // certificate may chain to an EV root. However, because network fetches
414 // are disabled in CreateTrustPolicies() when revocation checking is
415 // disabled, these will only go against the local cache.
416 }
417
418 CFDataRef action_data_ref =
419 CFDataCreateWithBytesNoCopy(kCFAllocatorDefault,
420 reinterpret_cast<UInt8*>(&tp_action_data),
421 sizeof(tp_action_data), kCFAllocatorNull);
422 if (!action_data_ref)
423 return ERR_OUT_OF_MEMORY;
424 ScopedCFTypeRef<CFDataRef> scoped_action_data_ref(action_data_ref);
425 status = SecTrustSetParameters(trust_ref, CSSM_TP_ACTION_DEFAULT,
426 action_data_ref);
427 if (status)
428 return NetErrorFromOSStatus(status);
429
430 // Verify the certificate. A non-zero result from SecTrustGetResult()
431 // indicates that some fatal error occurred and the chain couldn't be
432 // processed, not that the chain contains no errors. We need to examine the
433 // output of SecTrustGetResult() to determine that.
434 SecTrustResultType trust_result;
435 status = SecTrustEvaluate(trust_ref, &trust_result);
436 if (status)
437 return NetErrorFromOSStatus(status);
438 CFArrayRef completed_chain = NULL;
439 CSSM_TP_APPLE_EVIDENCE_INFO* chain_info;
440 status = SecTrustGetResult(trust_ref, &trust_result, &completed_chain,
441 &chain_info);
442 if (status)
443 return NetErrorFromOSStatus(status);
444 ScopedCFTypeRef<CFArrayRef> scoped_completed_chain(completed_chain);
445
446 if (crl_set && !CheckRevocationWithCRLSet(completed_chain, crl_set))
447 verify_result->cert_status |= CERT_STATUS_REVOKED;
448
449 GetCertChainInfo(scoped_completed_chain.get(), chain_info, verify_result);
450
[email protected]58484ca2012-05-29 21:56:34451 // As of Security Update 2012-002/OS X 10.7.4, when an RSA key < 1024 bits
452 // is encountered, CSSM returns CSSMERR_TP_VERIFY_ACTION_FAILED and adds
453 // CSSMERR_CSP_UNSUPPORTED_KEY_SIZE as a certificate status. Avoid mapping
454 // the CSSMERR_TP_VERIFY_ACTION_FAILED to CERT_STATUS_INVALID if the only
455 // error was due to an unsupported key size.
456 bool policy_failed = false;
457 bool weak_key = false;
458
[email protected]62b23c22012-03-22 04:50:24459 // Evaluate the results
460 OSStatus cssm_result;
461 switch (trust_result) {
462 case kSecTrustResultUnspecified:
463 case kSecTrustResultProceed:
464 // Certificate chain is valid and trusted ("unspecified" indicates that
465 // the user has not explicitly set a trust setting)
466 break;
467
468 case kSecTrustResultDeny:
469 case kSecTrustResultConfirm:
470 // Certificate chain is explicitly untrusted. For kSecTrustResultConfirm,
471 // we're following what Secure Transport does and treating it as
472 // "deny".
473 verify_result->cert_status |= CERT_STATUS_AUTHORITY_INVALID;
474 break;
475
476 case kSecTrustResultRecoverableTrustFailure:
477 // Certificate chain has a failure that can be overridden by the user.
478 status = SecTrustGetCssmResultCode(trust_ref, &cssm_result);
479 if (status)
480 return NetErrorFromOSStatus(status);
[email protected]58484ca2012-05-29 21:56:34481 if (cssm_result == CSSMERR_TP_VERIFY_ACTION_FAILED) {
482 policy_failed = true;
483 } else {
484 verify_result->cert_status |= CertStatusFromOSStatus(cssm_result);
485 }
[email protected]62b23c22012-03-22 04:50:24486 // Walk the chain of error codes in the CSSM_TP_APPLE_EVIDENCE_INFO
487 // structure which can catch multiple errors from each certificate.
488 for (CFIndex index = 0, chain_count = CFArrayGetCount(completed_chain);
489 index < chain_count; ++index) {
490 if (chain_info[index].StatusBits & CSSM_CERT_STATUS_EXPIRED ||
491 chain_info[index].StatusBits & CSSM_CERT_STATUS_NOT_VALID_YET)
492 verify_result->cert_status |= CERT_STATUS_DATE_INVALID;
493 if (!IsCertStatusError(verify_result->cert_status) &&
494 chain_info[index].NumStatusCodes == 0) {
495 LOG(WARNING) << "chain_info[" << index << "].NumStatusCodes is 0"
496 ", chain_info[" << index << "].StatusBits is "
497 << chain_info[index].StatusBits;
498 }
499 for (uint32 status_code_index = 0;
500 status_code_index < chain_info[index].NumStatusCodes;
501 ++status_code_index) {
[email protected]58484ca2012-05-29 21:56:34502 CertStatus mapped_status = CertStatusFromOSStatus(
[email protected]62b23c22012-03-22 04:50:24503 chain_info[index].StatusCodes[status_code_index]);
[email protected]58484ca2012-05-29 21:56:34504 if (mapped_status == CERT_STATUS_WEAK_KEY)
505 weak_key = true;
506 verify_result->cert_status |= mapped_status;
[email protected]62b23c22012-03-22 04:50:24507 }
508 }
[email protected]58484ca2012-05-29 21:56:34509 if (policy_failed && !weak_key) {
510 // If CSSMERR_TP_VERIFY_ACTION_FAILED wasn't returned due to a weak
511 // key, map it back to an appropriate error code.
512 verify_result->cert_status |= CertStatusFromOSStatus(cssm_result);
513 }
[email protected]62b23c22012-03-22 04:50:24514 if (!IsCertStatusError(verify_result->cert_status)) {
515 LOG(ERROR) << "cssm_result=" << cssm_result;
516 verify_result->cert_status |= CERT_STATUS_INVALID;
517 NOTREACHED();
518 }
519 break;
520
521 default:
522 status = SecTrustGetCssmResultCode(trust_ref, &cssm_result);
523 if (status)
524 return NetErrorFromOSStatus(status);
525 verify_result->cert_status |= CertStatusFromOSStatus(cssm_result);
526 if (!IsCertStatusError(verify_result->cert_status)) {
527 LOG(WARNING) << "trust_result=" << trust_result;
528 verify_result->cert_status |= CERT_STATUS_INVALID;
529 }
530 break;
531 }
532
533 // Perform hostname verification independent of SecTrustEvaluate. In order to
534 // do so, mask off any reported name errors first.
535 verify_result->cert_status &= ~CERT_STATUS_COMMON_NAME_INVALID;
536 if (!cert->VerifyNameMatch(hostname))
537 verify_result->cert_status |= CERT_STATUS_COMMON_NAME_INVALID;
538
539 // TODO(wtc): Suppress CERT_STATUS_NO_REVOCATION_MECHANISM for now to be
540 // compatible with Windows, which in turn implements this behavior to be
541 // compatible with WinHTTP, which doesn't report this error (bug 3004).
542 verify_result->cert_status &= ~CERT_STATUS_NO_REVOCATION_MECHANISM;
543
544 if (IsCertStatusError(verify_result->cert_status))
545 return MapCertStatusToNetError(verify_result->cert_status);
546
[email protected]8738e0d72012-08-23 02:00:47547 if (flags & CertVerifier::VERIFY_EV_CERT) {
[email protected]62b23c22012-03-22 04:50:24548 // Determine the certificate's EV status using SecTrustCopyExtendedResult(),
549 // which we need to look up because the function wasn't added until
550 // Mac OS X 10.5.7.
551 // Note: "ExtendedResult" means extended validation results.
552 CFBundleRef bundle =
553 CFBundleGetBundleWithIdentifier(CFSTR("com.apple.security"));
554 if (bundle) {
555 SecTrustCopyExtendedResultFuncPtr copy_extended_result =
556 reinterpret_cast<SecTrustCopyExtendedResultFuncPtr>(
557 CFBundleGetFunctionPointerForName(bundle,
558 CFSTR("SecTrustCopyExtendedResult")));
559 if (copy_extended_result) {
560 CFDictionaryRef ev_dict_temp = NULL;
561 status = copy_extended_result(trust_ref, &ev_dict_temp);
562 ScopedCFTypeRef<CFDictionaryRef> ev_dict(ev_dict_temp);
563 ev_dict_temp = NULL;
564 if (status == noErr && ev_dict) {
565 // In 10.7.3, SecTrustCopyExtendedResult returns noErr and populates
566 // ev_dict even for non-EV certificates, but only EV certificates
567 // will cause ev_dict to contain kSecEVOrganizationName. In previous
568 // releases, SecTrustCopyExtendedResult would only return noErr and
569 // populate ev_dict for EV certificates, but would always include
570 // kSecEVOrganizationName in that case, so checking for this key is
571 // appropriate for all known versions of SecTrustCopyExtendedResult.
572 // The actual organization name is unneeded here and can be accessed
573 // through other means. All that matters here is the OS' conception
574 // of whether or not the certificate is EV.
575 if (CFDictionaryContainsKey(ev_dict,
576 kSecEVOrganizationName)) {
577 verify_result->cert_status |= CERT_STATUS_IS_EV;
[email protected]8738e0d72012-08-23 02:00:47578 if (flags & CertVerifier::VERIFY_REV_CHECKING_ENABLED_EV_ONLY)
[email protected]b6f2de32012-08-17 04:35:08579 verify_result->cert_status |= CERT_STATUS_REV_CHECKING_ENABLED;
[email protected]62b23c22012-03-22 04:50:24580 }
581 }
582 }
583 }
584 }
585
586 AppendPublicKeyHashes(completed_chain, &verify_result->public_key_hashes);
587 verify_result->is_issued_by_known_root = IsIssuedByKnownRoot(completed_chain);
588
589 return OK;
590}
591
592} // namespace net