blob: b082e3481fa70fa83fec429b8cff769d802d4d1f [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
[email protected]6e7845ae2013-03-29 21:48:115#include "net/cert/cert_verify_proc_mac.h"
[email protected]62b23c22012-03-22 04:50:246
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"
[email protected]d069c11a2013-04-13 00:01:5518#include "base/strings/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"
[email protected]62b23c22012-03-22 04:50:2423#include "net/base/net_errors.h"
[email protected]6e7845ae2013-03-29 21:48:1124#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]62b23c22012-03-22 04:50:2433
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
[email protected]e9b8ca82013-04-29 20:52:29116 case CSSMERR_APPLETP_SSL_BAD_EXT_KEY_USE:
117 // TODO(wtc): Should we add CERT_STATUS_WRONG_USAGE?
118 return CERT_STATUS_INVALID;
119
[email protected]62b23c22012-03-22 04:50:24120 case CSSMERR_APPLETP_CRL_BAD_URI:
121 case CSSMERR_APPLETP_IDP_FAIL:
122 return CERT_STATUS_INVALID;
123
[email protected]58484ca2012-05-29 21:56:34124 case CSSMERR_CSP_UNSUPPORTED_KEY_SIZE:
125 // Mapping UNSUPPORTED_KEY_SIZE to CERT_STATUS_WEAK_KEY is not strictly
126 // accurate, as the error may have been returned due to a key size
127 // that exceeded the maximum supported. However, within
128 // CertVerifyProcMac::VerifyInternal(), this code should only be
129 // encountered as a certificate status code, and only when the key size
130 // is smaller than the minimum required (1024 bits).
131 return CERT_STATUS_WEAK_KEY;
132
[email protected]62b23c22012-03-22 04:50:24133 default: {
134 // Failure was due to something Chromium doesn't define a
135 // specific status for (such as basic constraints violation, or
136 // unknown critical extension)
137 OSSTATUS_LOG(WARNING, status)
138 << "Unknown error mapped to CERT_STATUS_INVALID";
139 return CERT_STATUS_INVALID;
140 }
141 }
142}
143
144// Creates a series of SecPolicyRefs to be added to a SecTrustRef used to
145// validate a certificate for an SSL server. |hostname| contains the name of
146// the SSL server that the certificate should be verified against. |flags| is
147// a bitwise-OR of VerifyFlags that can further alter how trust is validated,
148// such as how revocation is checked. If successful, returns noErr, and
149// stores the resultant array of SecPolicyRefs in |policies|.
150OSStatus CreateTrustPolicies(const std::string& hostname,
151 int flags,
152 ScopedCFTypeRef<CFArrayRef>* policies) {
153 ScopedCFTypeRef<CFMutableArrayRef> local_policies(
154 CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks));
155 if (!local_policies)
156 return memFullErr;
157
158 SecPolicyRef ssl_policy;
159 OSStatus status = x509_util::CreateSSLServerPolicy(hostname, &ssl_policy);
160 if (status)
161 return status;
162 CFArrayAppendValue(local_policies, ssl_policy);
163 CFRelease(ssl_policy);
164
165 // Explicitly add revocation policies, in order to override system
166 // revocation checking policies and instead respect the application-level
167 // revocation preference.
168 status = x509_util::CreateRevocationPolicies(
[email protected]8738e0d72012-08-23 02:00:47169 (flags & CertVerifier::VERIFY_REV_CHECKING_ENABLED),
170 (flags & CertVerifier::VERIFY_REV_CHECKING_ENABLED_EV_ONLY),
[email protected]62b23c22012-03-22 04:50:24171 local_policies);
172 if (status)
173 return status;
174
175 policies->reset(local_policies.release());
176 return noErr;
177}
178
179// Saves some information about the certificate chain |cert_chain| in
180// |*verify_result|. The caller MUST initialize |*verify_result| before
181// calling this function.
182void GetCertChainInfo(CFArrayRef cert_chain,
183 CSSM_TP_APPLE_EVIDENCE_INFO* chain_info,
184 CertVerifyResult* verify_result) {
185 SecCertificateRef verified_cert = NULL;
186 std::vector<SecCertificateRef> verified_chain;
187 for (CFIndex i = 0, count = CFArrayGetCount(cert_chain); i < count; ++i) {
188 SecCertificateRef chain_cert = reinterpret_cast<SecCertificateRef>(
189 const_cast<void*>(CFArrayGetValueAtIndex(cert_chain, i)));
190 if (i == 0) {
191 verified_cert = chain_cert;
192 } else {
193 verified_chain.push_back(chain_cert);
194 }
195
196 if ((chain_info[i].StatusBits & CSSM_CERT_STATUS_IS_IN_ANCHORS) ||
197 (chain_info[i].StatusBits & CSSM_CERT_STATUS_IS_ROOT)) {
198 // The current certificate is either in the user's trusted store or is
199 // a root (self-signed) certificate. Ignore the signature algorithm for
200 // these certificates, as it is meaningless for security. We allow
201 // self-signed certificates (i == 0 & IS_ROOT), since we accept that
202 // any security assertions by such a cert are inherently meaningless.
203 continue;
204 }
205
206 x509_util::CSSMCachedCertificate cached_cert;
207 OSStatus status = cached_cert.Init(chain_cert);
208 if (status)
209 continue;
210 x509_util::CSSMFieldValue signature_field;
211 status = cached_cert.GetField(&CSSMOID_X509V1SignatureAlgorithm,
212 &signature_field);
213 if (status || !signature_field.field())
214 continue;
215 // Match the behaviour of OS X system tools and defensively check that
216 // sizes are appropriate. This would indicate a critical failure of the
217 // OS X certificate library, but based on history, it is best to play it
218 // safe.
219 const CSSM_X509_ALGORITHM_IDENTIFIER* sig_algorithm =
220 signature_field.GetAs<CSSM_X509_ALGORITHM_IDENTIFIER>();
221 if (!sig_algorithm)
222 continue;
223
224 const CSSM_OID* alg_oid = &sig_algorithm->algorithm;
225 if (CSSMOIDEqual(alg_oid, &CSSMOID_MD2WithRSA)) {
226 verify_result->has_md2 = true;
227 if (i != 0)
228 verify_result->has_md2_ca = true;
229 } else if (CSSMOIDEqual(alg_oid, &CSSMOID_MD4WithRSA)) {
230 verify_result->has_md4 = true;
231 } else if (CSSMOIDEqual(alg_oid, &CSSMOID_MD5WithRSA)) {
232 verify_result->has_md5 = true;
233 if (i != 0)
234 verify_result->has_md5_ca = true;
235 }
236 }
237 if (!verified_cert)
238 return;
239
240 verify_result->verified_cert =
241 X509Certificate::CreateFromHandle(verified_cert, verified_chain);
242}
243
244void AppendPublicKeyHashes(CFArrayRef chain,
[email protected]ede03212012-09-07 12:52:26245 HashValueVector* hashes) {
[email protected]62b23c22012-03-22 04:50:24246 const CFIndex n = CFArrayGetCount(chain);
247 for (CFIndex i = 0; i < n; i++) {
248 SecCertificateRef cert = reinterpret_cast<SecCertificateRef>(
249 const_cast<void*>(CFArrayGetValueAtIndex(chain, i)));
250
251 CSSM_DATA cert_data;
252 OSStatus err = SecCertificateGetData(cert, &cert_data);
253 DCHECK_EQ(err, noErr);
254 base::StringPiece der_bytes(reinterpret_cast<const char*>(cert_data.Data),
255 cert_data.Length);
256 base::StringPiece spki_bytes;
257 if (!asn1::ExtractSPKIFromDERCert(der_bytes, &spki_bytes))
258 continue;
259
[email protected]ede03212012-09-07 12:52:26260 HashValue sha1(HASH_VALUE_SHA1);
261 CC_SHA1(spki_bytes.data(), spki_bytes.size(), sha1.data());
262 hashes->push_back(sha1);
263
264 HashValue sha256(HASH_VALUE_SHA256);
265 CC_SHA256(spki_bytes.data(), spki_bytes.size(), sha256.data());
266 hashes->push_back(sha256);
[email protected]62b23c22012-03-22 04:50:24267 }
268}
269
270bool CheckRevocationWithCRLSet(CFArrayRef chain, CRLSet* crl_set) {
271 if (CFArrayGetCount(chain) == 0)
272 return true;
273
274 // We iterate from the root certificate down to the leaf, keeping track of
275 // the issuer's SPKI at each step.
276 std::string issuer_spki_hash;
277 for (CFIndex i = CFArrayGetCount(chain) - 1; i >= 0; i--) {
278 SecCertificateRef cert = reinterpret_cast<SecCertificateRef>(
279 const_cast<void*>(CFArrayGetValueAtIndex(chain, i)));
280
281 CSSM_DATA cert_data;
282 OSStatus err = SecCertificateGetData(cert, &cert_data);
283 if (err != noErr) {
284 NOTREACHED();
285 continue;
286 }
287 base::StringPiece der_bytes(reinterpret_cast<const char*>(cert_data.Data),
288 cert_data.Length);
289 base::StringPiece spki;
290 if (!asn1::ExtractSPKIFromDERCert(der_bytes, &spki)) {
291 NOTREACHED();
292 continue;
293 }
294
295 const std::string spki_hash = crypto::SHA256HashString(spki);
296 x509_util::CSSMCachedCertificate cached_cert;
297 if (cached_cert.Init(cert) != CSSM_OK) {
298 NOTREACHED();
299 continue;
300 }
301 x509_util::CSSMFieldValue serial_number;
302 err = cached_cert.GetField(&CSSMOID_X509V1SerialNumber, &serial_number);
303 if (err || !serial_number.field()) {
304 NOTREACHED();
305 continue;
306 }
307
308 base::StringPiece serial(
309 reinterpret_cast<const char*>(serial_number.field()->Data),
310 serial_number.field()->Length);
311
312 CRLSet::Result result = crl_set->CheckSPKI(spki_hash);
313
314 if (result != CRLSet::REVOKED && !issuer_spki_hash.empty())
315 result = crl_set->CheckSerial(serial, issuer_spki_hash);
316
317 issuer_spki_hash = spki_hash;
318
319 switch (result) {
320 case CRLSet::REVOKED:
321 return false;
322 case CRLSet::UNKNOWN:
323 case CRLSet::GOOD:
324 continue;
325 default:
326 NOTREACHED();
327 return false;
328 }
329 }
330
331 return true;
332}
333
334// IsIssuedByKnownRoot returns true if the given chain is rooted at a root CA
335// that we recognise as a standard root.
336// static
337bool IsIssuedByKnownRoot(CFArrayRef chain) {
338 int n = CFArrayGetCount(chain);
339 if (n < 1)
340 return false;
341 SecCertificateRef root_ref = reinterpret_cast<SecCertificateRef>(
342 const_cast<void*>(CFArrayGetValueAtIndex(chain, n - 1)));
[email protected]ede03212012-09-07 12:52:26343 SHA1HashValue hash = X509Certificate::CalculateFingerprint(root_ref);
[email protected]62b23c22012-03-22 04:50:24344 return IsSHA1HashInSortedArray(
345 hash, &kKnownRootCertSHA1Hashes[0][0], sizeof(kKnownRootCertSHA1Hashes));
346}
347
348} // namespace
349
350CertVerifyProcMac::CertVerifyProcMac() {}
351
352CertVerifyProcMac::~CertVerifyProcMac() {}
353
[email protected]ef155122013-03-23 19:11:24354bool CertVerifyProcMac::SupportsAdditionalTrustAnchors() const {
355 return false;
356}
357
358int CertVerifyProcMac::VerifyInternal(
359 X509Certificate* cert,
360 const std::string& hostname,
361 int flags,
362 CRLSet* crl_set,
363 const CertificateList& additional_trust_anchors,
364 CertVerifyResult* verify_result) {
[email protected]62b23c22012-03-22 04:50:24365 ScopedCFTypeRef<CFArrayRef> trust_policies;
366 OSStatus status = CreateTrustPolicies(hostname, flags, &trust_policies);
367 if (status)
368 return NetErrorFromOSStatus(status);
369
370 // Create and configure a SecTrustRef, which takes our certificate(s)
371 // and our SSL SecPolicyRef. SecTrustCreateWithCertificates() takes an
372 // array of certificates, the first of which is the certificate we're
373 // verifying, and the subsequent (optional) certificates are used for
374 // chain building.
375 ScopedCFTypeRef<CFArrayRef> cert_array(cert->CreateOSCertChainForCert());
376
[email protected]d6e8fe62012-10-03 05:46:45377 // Serialize all calls that may use the Keychain, to work around various
378 // issues in OS X 10.6+ with multi-threaded access to Security.framework.
379 base::AutoLock lock(crypto::GetMacSecurityServicesLock());
[email protected]62b23c22012-03-22 04:50:24380
381 SecTrustRef trust_ref = NULL;
382 status = SecTrustCreateWithCertificates(cert_array, trust_policies,
383 &trust_ref);
384 if (status)
385 return NetErrorFromOSStatus(status);
386 ScopedCFTypeRef<SecTrustRef> scoped_trust_ref(trust_ref);
387
388 if (TestRootCerts::HasInstance()) {
389 status = TestRootCerts::GetInstance()->FixupSecTrustRef(trust_ref);
390 if (status)
391 return NetErrorFromOSStatus(status);
392 }
393
394 CSSM_APPLE_TP_ACTION_DATA tp_action_data;
395 memset(&tp_action_data, 0, sizeof(tp_action_data));
396 tp_action_data.Version = CSSM_APPLE_TP_ACTION_VERSION;
397 // Allow CSSM to download any missing intermediate certificates if an
398 // authorityInfoAccess extension or issuerAltName extension is present.
399 tp_action_data.ActionFlags = CSSM_TP_ACTION_FETCH_CERT_FROM_NET |
400 CSSM_TP_ACTION_TRUST_SETTINGS;
401
[email protected]b6f2de32012-08-17 04:35:08402 // Note: For EV certificates, the Apple TP will handle setting these flags
403 // as part of EV evaluation.
[email protected]8738e0d72012-08-23 02:00:47404 if (flags & CertVerifier::VERIFY_REV_CHECKING_ENABLED) {
[email protected]62b23c22012-03-22 04:50:24405 // Require a positive result from an OCSP responder or a CRL (or both)
406 // for every certificate in the chain. The Apple TP automatically
407 // excludes the self-signed root from this requirement. If a certificate
408 // is missing both a crlDistributionPoints extension and an
409 // authorityInfoAccess extension with an OCSP responder URL, then we
410 // will get a kSecTrustResultRecoverableTrustFailure back from
411 // SecTrustEvaluate(), with a
412 // CSSMERR_APPLETP_INCOMPLETE_REVOCATION_CHECK error code. In that case,
413 // we'll set our own result to include
414 // CERT_STATUS_NO_REVOCATION_MECHANISM. If one or both extensions are
415 // present, and a check fails (server unavailable, OCSP retry later,
416 // signature mismatch), then we'll set our own result to include
417 // CERT_STATUS_UNABLE_TO_CHECK_REVOCATION.
418 tp_action_data.ActionFlags |= CSSM_TP_ACTION_REQUIRE_REV_PER_CERT;
419 verify_result->cert_status |= CERT_STATUS_REV_CHECKING_ENABLED;
420
421 // Note, even if revocation checking is disabled, SecTrustEvaluate() will
422 // modify the OCSP options so as to attempt OCSP checking if it believes a
423 // certificate may chain to an EV root. However, because network fetches
424 // are disabled in CreateTrustPolicies() when revocation checking is
425 // disabled, these will only go against the local cache.
426 }
427
428 CFDataRef action_data_ref =
429 CFDataCreateWithBytesNoCopy(kCFAllocatorDefault,
430 reinterpret_cast<UInt8*>(&tp_action_data),
431 sizeof(tp_action_data), kCFAllocatorNull);
432 if (!action_data_ref)
433 return ERR_OUT_OF_MEMORY;
434 ScopedCFTypeRef<CFDataRef> scoped_action_data_ref(action_data_ref);
435 status = SecTrustSetParameters(trust_ref, CSSM_TP_ACTION_DEFAULT,
436 action_data_ref);
437 if (status)
438 return NetErrorFromOSStatus(status);
439
440 // Verify the certificate. A non-zero result from SecTrustGetResult()
441 // indicates that some fatal error occurred and the chain couldn't be
442 // processed, not that the chain contains no errors. We need to examine the
443 // output of SecTrustGetResult() to determine that.
444 SecTrustResultType trust_result;
445 status = SecTrustEvaluate(trust_ref, &trust_result);
446 if (status)
447 return NetErrorFromOSStatus(status);
448 CFArrayRef completed_chain = NULL;
449 CSSM_TP_APPLE_EVIDENCE_INFO* chain_info;
450 status = SecTrustGetResult(trust_ref, &trust_result, &completed_chain,
451 &chain_info);
452 if (status)
453 return NetErrorFromOSStatus(status);
454 ScopedCFTypeRef<CFArrayRef> scoped_completed_chain(completed_chain);
455
456 if (crl_set && !CheckRevocationWithCRLSet(completed_chain, crl_set))
457 verify_result->cert_status |= CERT_STATUS_REVOKED;
458
459 GetCertChainInfo(scoped_completed_chain.get(), chain_info, verify_result);
460
[email protected]58484ca2012-05-29 21:56:34461 // As of Security Update 2012-002/OS X 10.7.4, when an RSA key < 1024 bits
462 // is encountered, CSSM returns CSSMERR_TP_VERIFY_ACTION_FAILED and adds
463 // CSSMERR_CSP_UNSUPPORTED_KEY_SIZE as a certificate status. Avoid mapping
464 // the CSSMERR_TP_VERIFY_ACTION_FAILED to CERT_STATUS_INVALID if the only
465 // error was due to an unsupported key size.
466 bool policy_failed = false;
467 bool weak_key = false;
468
[email protected]62b23c22012-03-22 04:50:24469 // Evaluate the results
470 OSStatus cssm_result;
471 switch (trust_result) {
472 case kSecTrustResultUnspecified:
473 case kSecTrustResultProceed:
474 // Certificate chain is valid and trusted ("unspecified" indicates that
475 // the user has not explicitly set a trust setting)
476 break;
477
478 case kSecTrustResultDeny:
479 case kSecTrustResultConfirm:
480 // Certificate chain is explicitly untrusted. For kSecTrustResultConfirm,
481 // we're following what Secure Transport does and treating it as
482 // "deny".
483 verify_result->cert_status |= CERT_STATUS_AUTHORITY_INVALID;
484 break;
485
486 case kSecTrustResultRecoverableTrustFailure:
487 // Certificate chain has a failure that can be overridden by the user.
488 status = SecTrustGetCssmResultCode(trust_ref, &cssm_result);
489 if (status)
490 return NetErrorFromOSStatus(status);
[email protected]58484ca2012-05-29 21:56:34491 if (cssm_result == CSSMERR_TP_VERIFY_ACTION_FAILED) {
492 policy_failed = true;
493 } else {
494 verify_result->cert_status |= CertStatusFromOSStatus(cssm_result);
495 }
[email protected]62b23c22012-03-22 04:50:24496 // Walk the chain of error codes in the CSSM_TP_APPLE_EVIDENCE_INFO
497 // structure which can catch multiple errors from each certificate.
498 for (CFIndex index = 0, chain_count = CFArrayGetCount(completed_chain);
499 index < chain_count; ++index) {
500 if (chain_info[index].StatusBits & CSSM_CERT_STATUS_EXPIRED ||
501 chain_info[index].StatusBits & CSSM_CERT_STATUS_NOT_VALID_YET)
502 verify_result->cert_status |= CERT_STATUS_DATE_INVALID;
503 if (!IsCertStatusError(verify_result->cert_status) &&
504 chain_info[index].NumStatusCodes == 0) {
505 LOG(WARNING) << "chain_info[" << index << "].NumStatusCodes is 0"
506 ", chain_info[" << index << "].StatusBits is "
507 << chain_info[index].StatusBits;
508 }
509 for (uint32 status_code_index = 0;
510 status_code_index < chain_info[index].NumStatusCodes;
511 ++status_code_index) {
[email protected]58484ca2012-05-29 21:56:34512 CertStatus mapped_status = CertStatusFromOSStatus(
[email protected]62b23c22012-03-22 04:50:24513 chain_info[index].StatusCodes[status_code_index]);
[email protected]58484ca2012-05-29 21:56:34514 if (mapped_status == CERT_STATUS_WEAK_KEY)
515 weak_key = true;
516 verify_result->cert_status |= mapped_status;
[email protected]62b23c22012-03-22 04:50:24517 }
518 }
[email protected]58484ca2012-05-29 21:56:34519 if (policy_failed && !weak_key) {
520 // If CSSMERR_TP_VERIFY_ACTION_FAILED wasn't returned due to a weak
521 // key, map it back to an appropriate error code.
522 verify_result->cert_status |= CertStatusFromOSStatus(cssm_result);
523 }
[email protected]62b23c22012-03-22 04:50:24524 if (!IsCertStatusError(verify_result->cert_status)) {
525 LOG(ERROR) << "cssm_result=" << cssm_result;
526 verify_result->cert_status |= CERT_STATUS_INVALID;
527 NOTREACHED();
528 }
529 break;
530
531 default:
532 status = SecTrustGetCssmResultCode(trust_ref, &cssm_result);
533 if (status)
534 return NetErrorFromOSStatus(status);
535 verify_result->cert_status |= CertStatusFromOSStatus(cssm_result);
536 if (!IsCertStatusError(verify_result->cert_status)) {
537 LOG(WARNING) << "trust_result=" << trust_result;
538 verify_result->cert_status |= CERT_STATUS_INVALID;
539 }
540 break;
541 }
542
543 // Perform hostname verification independent of SecTrustEvaluate. In order to
544 // do so, mask off any reported name errors first.
545 verify_result->cert_status &= ~CERT_STATUS_COMMON_NAME_INVALID;
546 if (!cert->VerifyNameMatch(hostname))
547 verify_result->cert_status |= CERT_STATUS_COMMON_NAME_INVALID;
548
549 // TODO(wtc): Suppress CERT_STATUS_NO_REVOCATION_MECHANISM for now to be
550 // compatible with Windows, which in turn implements this behavior to be
551 // compatible with WinHTTP, which doesn't report this error (bug 3004).
552 verify_result->cert_status &= ~CERT_STATUS_NO_REVOCATION_MECHANISM;
553
[email protected]8b4a61a2012-11-28 22:57:20554 AppendPublicKeyHashes(completed_chain, &verify_result->public_key_hashes);
555 verify_result->is_issued_by_known_root = IsIssuedByKnownRoot(completed_chain);
556
[email protected]62b23c22012-03-22 04:50:24557 if (IsCertStatusError(verify_result->cert_status))
558 return MapCertStatusToNetError(verify_result->cert_status);
559
[email protected]8738e0d72012-08-23 02:00:47560 if (flags & CertVerifier::VERIFY_EV_CERT) {
[email protected]62b23c22012-03-22 04:50:24561 // Determine the certificate's EV status using SecTrustCopyExtendedResult(),
[email protected]7c23be82013-04-29 20:51:19562 // which we need to look up because the function wasn't added until
563 // Mac OS X 10.5.7.
[email protected]62b23c22012-03-22 04:50:24564 // Note: "ExtendedResult" means extended validation results.
565 CFBundleRef bundle =
566 CFBundleGetBundleWithIdentifier(CFSTR("com.apple.security"));
567 if (bundle) {
568 SecTrustCopyExtendedResultFuncPtr copy_extended_result =
569 reinterpret_cast<SecTrustCopyExtendedResultFuncPtr>(
570 CFBundleGetFunctionPointerForName(bundle,
571 CFSTR("SecTrustCopyExtendedResult")));
572 if (copy_extended_result) {
573 CFDictionaryRef ev_dict_temp = NULL;
574 status = copy_extended_result(trust_ref, &ev_dict_temp);
575 ScopedCFTypeRef<CFDictionaryRef> ev_dict(ev_dict_temp);
576 ev_dict_temp = NULL;
577 if (status == noErr && ev_dict) {
578 // In 10.7.3, SecTrustCopyExtendedResult returns noErr and populates
579 // ev_dict even for non-EV certificates, but only EV certificates
580 // will cause ev_dict to contain kSecEVOrganizationName. In previous
581 // releases, SecTrustCopyExtendedResult would only return noErr and
582 // populate ev_dict for EV certificates, but would always include
583 // kSecEVOrganizationName in that case, so checking for this key is
584 // appropriate for all known versions of SecTrustCopyExtendedResult.
585 // The actual organization name is unneeded here and can be accessed
586 // through other means. All that matters here is the OS' conception
587 // of whether or not the certificate is EV.
588 if (CFDictionaryContainsKey(ev_dict,
589 kSecEVOrganizationName)) {
590 verify_result->cert_status |= CERT_STATUS_IS_EV;
[email protected]8738e0d72012-08-23 02:00:47591 if (flags & CertVerifier::VERIFY_REV_CHECKING_ENABLED_EV_ONLY)
[email protected]b6f2de32012-08-17 04:35:08592 verify_result->cert_status |= CERT_STATUS_REV_CHECKING_ENABLED;
[email protected]62b23c22012-03-22 04:50:24593 }
594 }
595 }
596 }
597 }
598
[email protected]62b23c22012-03-22 04:50:24599 return OK;
600}
601
602} // namespace net