blob: e47892970f034f18bc3486e0a6a92bd11b8a142b [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"
19#include "crypto/nss_util.h"
20#include "crypto/sha2.h"
21#include "net/base/asn1_util.h"
22#include "net/base/cert_status_flags.h"
[email protected]8738e0d72012-08-23 02:00:4723#include "net/base/cert_verifier.h"
[email protected]62b23c22012-03-22 04:50:2424#include "net/base/cert_verify_result.h"
25#include "net/base/crl_set.h"
26#include "net/base/net_errors.h"
27#include "net/base/test_root_certs.h"
28#include "net/base/x509_certificate.h"
29#include "net/base/x509_certificate_known_roots_mac.h"
30#include "net/base/x509_util_mac.h"
31
32// From 10.7.2 libsecurity_keychain-55035/lib/SecTrustPriv.h, for use with
33// SecTrustCopyExtendedResult.
34#ifndef kSecEVOrganizationName
35#define kSecEVOrganizationName CFSTR("Organization")
36#endif
37
38using base::mac::ScopedCFTypeRef;
39
40namespace net {
41
42namespace {
43
44typedef OSStatus (*SecTrustCopyExtendedResultFuncPtr)(SecTrustRef,
45 CFDictionaryRef*);
46
47int NetErrorFromOSStatus(OSStatus status) {
48 switch (status) {
49 case noErr:
50 return OK;
51 case errSecNotAvailable:
52 case errSecNoCertificateModule:
53 case errSecNoPolicyModule:
54 return ERR_NOT_IMPLEMENTED;
55 case errSecAuthFailed:
56 return ERR_ACCESS_DENIED;
57 default: {
58 OSSTATUS_LOG(ERROR, status) << "Unknown error mapped to ERR_FAILED";
59 return ERR_FAILED;
60 }
61 }
62}
63
64CertStatus CertStatusFromOSStatus(OSStatus status) {
65 switch (status) {
66 case noErr:
67 return 0;
68
69 case CSSMERR_TP_INVALID_ANCHOR_CERT:
70 case CSSMERR_TP_NOT_TRUSTED:
71 case CSSMERR_TP_INVALID_CERT_AUTHORITY:
72 return CERT_STATUS_AUTHORITY_INVALID;
73
74 case CSSMERR_TP_CERT_EXPIRED:
75 case CSSMERR_TP_CERT_NOT_VALID_YET:
76 // "Expired" and "not yet valid" collapse into a single status.
77 return CERT_STATUS_DATE_INVALID;
78
79 case CSSMERR_TP_CERT_REVOKED:
80 case CSSMERR_TP_CERT_SUSPENDED:
81 return CERT_STATUS_REVOKED;
82
83 case CSSMERR_APPLETP_HOSTNAME_MISMATCH:
84 return CERT_STATUS_COMMON_NAME_INVALID;
85
86 case CSSMERR_APPLETP_CRL_NOT_FOUND:
87 case CSSMERR_APPLETP_OCSP_UNAVAILABLE:
88 case CSSMERR_APPLETP_INCOMPLETE_REVOCATION_CHECK:
89 return CERT_STATUS_NO_REVOCATION_MECHANISM;
90
91 case CSSMERR_APPLETP_CRL_EXPIRED:
92 case CSSMERR_APPLETP_CRL_NOT_VALID_YET:
93 case CSSMERR_APPLETP_CRL_SERVER_DOWN:
94 case CSSMERR_APPLETP_CRL_NOT_TRUSTED:
95 case CSSMERR_APPLETP_CRL_INVALID_ANCHOR_CERT:
96 case CSSMERR_APPLETP_CRL_POLICY_FAIL:
97 case CSSMERR_APPLETP_OCSP_BAD_RESPONSE:
98 case CSSMERR_APPLETP_OCSP_BAD_REQUEST:
99 case CSSMERR_APPLETP_OCSP_STATUS_UNRECOGNIZED:
100 case CSSMERR_APPLETP_NETWORK_FAILURE:
101 case CSSMERR_APPLETP_OCSP_NOT_TRUSTED:
102 case CSSMERR_APPLETP_OCSP_INVALID_ANCHOR_CERT:
103 case CSSMERR_APPLETP_OCSP_SIG_ERROR:
104 case CSSMERR_APPLETP_OCSP_NO_SIGNER:
105 case CSSMERR_APPLETP_OCSP_RESP_MALFORMED_REQ:
106 case CSSMERR_APPLETP_OCSP_RESP_INTERNAL_ERR:
107 case CSSMERR_APPLETP_OCSP_RESP_TRY_LATER:
108 case CSSMERR_APPLETP_OCSP_RESP_SIG_REQUIRED:
109 case CSSMERR_APPLETP_OCSP_RESP_UNAUTHORIZED:
110 case CSSMERR_APPLETP_OCSP_NONCE_MISMATCH:
111 // We asked for a revocation check, but didn't get it.
112 return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION;
113
114 case CSSMERR_APPLETP_CRL_BAD_URI:
115 case CSSMERR_APPLETP_IDP_FAIL:
116 return CERT_STATUS_INVALID;
117
[email protected]58484ca2012-05-29 21:56:34118 case CSSMERR_CSP_UNSUPPORTED_KEY_SIZE:
119 // Mapping UNSUPPORTED_KEY_SIZE to CERT_STATUS_WEAK_KEY is not strictly
120 // accurate, as the error may have been returned due to a key size
121 // that exceeded the maximum supported. However, within
122 // CertVerifyProcMac::VerifyInternal(), this code should only be
123 // encountered as a certificate status code, and only when the key size
124 // is smaller than the minimum required (1024 bits).
125 return CERT_STATUS_WEAK_KEY;
126
[email protected]62b23c22012-03-22 04:50:24127 default: {
128 // Failure was due to something Chromium doesn't define a
129 // specific status for (such as basic constraints violation, or
130 // unknown critical extension)
131 OSSTATUS_LOG(WARNING, status)
132 << "Unknown error mapped to CERT_STATUS_INVALID";
133 return CERT_STATUS_INVALID;
134 }
135 }
136}
137
138// Creates a series of SecPolicyRefs to be added to a SecTrustRef used to
139// validate a certificate for an SSL server. |hostname| contains the name of
140// the SSL server that the certificate should be verified against. |flags| is
141// a bitwise-OR of VerifyFlags that can further alter how trust is validated,
142// such as how revocation is checked. If successful, returns noErr, and
143// stores the resultant array of SecPolicyRefs in |policies|.
144OSStatus CreateTrustPolicies(const std::string& hostname,
145 int flags,
146 ScopedCFTypeRef<CFArrayRef>* policies) {
147 ScopedCFTypeRef<CFMutableArrayRef> local_policies(
148 CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks));
149 if (!local_policies)
150 return memFullErr;
151
152 SecPolicyRef ssl_policy;
153 OSStatus status = x509_util::CreateSSLServerPolicy(hostname, &ssl_policy);
154 if (status)
155 return status;
156 CFArrayAppendValue(local_policies, ssl_policy);
157 CFRelease(ssl_policy);
158
159 // Explicitly add revocation policies, in order to override system
160 // revocation checking policies and instead respect the application-level
161 // revocation preference.
162 status = x509_util::CreateRevocationPolicies(
[email protected]8738e0d72012-08-23 02:00:47163 (flags & CertVerifier::VERIFY_REV_CHECKING_ENABLED),
164 (flags & CertVerifier::VERIFY_REV_CHECKING_ENABLED_EV_ONLY),
[email protected]62b23c22012-03-22 04:50:24165 local_policies);
166 if (status)
167 return status;
168
169 policies->reset(local_policies.release());
170 return noErr;
171}
172
173// Saves some information about the certificate chain |cert_chain| in
174// |*verify_result|. The caller MUST initialize |*verify_result| before
175// calling this function.
176void GetCertChainInfo(CFArrayRef cert_chain,
177 CSSM_TP_APPLE_EVIDENCE_INFO* chain_info,
178 CertVerifyResult* verify_result) {
179 SecCertificateRef verified_cert = NULL;
180 std::vector<SecCertificateRef> verified_chain;
181 for (CFIndex i = 0, count = CFArrayGetCount(cert_chain); i < count; ++i) {
182 SecCertificateRef chain_cert = reinterpret_cast<SecCertificateRef>(
183 const_cast<void*>(CFArrayGetValueAtIndex(cert_chain, i)));
184 if (i == 0) {
185 verified_cert = chain_cert;
186 } else {
187 verified_chain.push_back(chain_cert);
188 }
189
190 if ((chain_info[i].StatusBits & CSSM_CERT_STATUS_IS_IN_ANCHORS) ||
191 (chain_info[i].StatusBits & CSSM_CERT_STATUS_IS_ROOT)) {
192 // The current certificate is either in the user's trusted store or is
193 // a root (self-signed) certificate. Ignore the signature algorithm for
194 // these certificates, as it is meaningless for security. We allow
195 // self-signed certificates (i == 0 & IS_ROOT), since we accept that
196 // any security assertions by such a cert are inherently meaningless.
197 continue;
198 }
199
200 x509_util::CSSMCachedCertificate cached_cert;
201 OSStatus status = cached_cert.Init(chain_cert);
202 if (status)
203 continue;
204 x509_util::CSSMFieldValue signature_field;
205 status = cached_cert.GetField(&CSSMOID_X509V1SignatureAlgorithm,
206 &signature_field);
207 if (status || !signature_field.field())
208 continue;
209 // Match the behaviour of OS X system tools and defensively check that
210 // sizes are appropriate. This would indicate a critical failure of the
211 // OS X certificate library, but based on history, it is best to play it
212 // safe.
213 const CSSM_X509_ALGORITHM_IDENTIFIER* sig_algorithm =
214 signature_field.GetAs<CSSM_X509_ALGORITHM_IDENTIFIER>();
215 if (!sig_algorithm)
216 continue;
217
218 const CSSM_OID* alg_oid = &sig_algorithm->algorithm;
219 if (CSSMOIDEqual(alg_oid, &CSSMOID_MD2WithRSA)) {
220 verify_result->has_md2 = true;
221 if (i != 0)
222 verify_result->has_md2_ca = true;
223 } else if (CSSMOIDEqual(alg_oid, &CSSMOID_MD4WithRSA)) {
224 verify_result->has_md4 = true;
225 } else if (CSSMOIDEqual(alg_oid, &CSSMOID_MD5WithRSA)) {
226 verify_result->has_md5 = true;
227 if (i != 0)
228 verify_result->has_md5_ca = true;
229 }
230 }
231 if (!verified_cert)
232 return;
233
234 verify_result->verified_cert =
235 X509Certificate::CreateFromHandle(verified_cert, verified_chain);
236}
237
238void AppendPublicKeyHashes(CFArrayRef chain,
[email protected]ede03212012-09-07 12:52:26239 HashValueVector* hashes) {
[email protected]62b23c22012-03-22 04:50:24240 const CFIndex n = CFArrayGetCount(chain);
241 for (CFIndex i = 0; i < n; i++) {
242 SecCertificateRef cert = reinterpret_cast<SecCertificateRef>(
243 const_cast<void*>(CFArrayGetValueAtIndex(chain, i)));
244
245 CSSM_DATA cert_data;
246 OSStatus err = SecCertificateGetData(cert, &cert_data);
247 DCHECK_EQ(err, noErr);
248 base::StringPiece der_bytes(reinterpret_cast<const char*>(cert_data.Data),
249 cert_data.Length);
250 base::StringPiece spki_bytes;
251 if (!asn1::ExtractSPKIFromDERCert(der_bytes, &spki_bytes))
252 continue;
253
[email protected]ede03212012-09-07 12:52:26254 HashValue sha1(HASH_VALUE_SHA1);
255 CC_SHA1(spki_bytes.data(), spki_bytes.size(), sha1.data());
256 hashes->push_back(sha1);
257
258 HashValue sha256(HASH_VALUE_SHA256);
259 CC_SHA256(spki_bytes.data(), spki_bytes.size(), sha256.data());
260 hashes->push_back(sha256);
[email protected]62b23c22012-03-22 04:50:24261 }
262}
263
264bool CheckRevocationWithCRLSet(CFArrayRef chain, CRLSet* crl_set) {
265 if (CFArrayGetCount(chain) == 0)
266 return true;
267
268 // We iterate from the root certificate down to the leaf, keeping track of
269 // the issuer's SPKI at each step.
270 std::string issuer_spki_hash;
271 for (CFIndex i = CFArrayGetCount(chain) - 1; i >= 0; i--) {
272 SecCertificateRef cert = reinterpret_cast<SecCertificateRef>(
273 const_cast<void*>(CFArrayGetValueAtIndex(chain, i)));
274
275 CSSM_DATA cert_data;
276 OSStatus err = SecCertificateGetData(cert, &cert_data);
277 if (err != noErr) {
278 NOTREACHED();
279 continue;
280 }
281 base::StringPiece der_bytes(reinterpret_cast<const char*>(cert_data.Data),
282 cert_data.Length);
283 base::StringPiece spki;
284 if (!asn1::ExtractSPKIFromDERCert(der_bytes, &spki)) {
285 NOTREACHED();
286 continue;
287 }
288
289 const std::string spki_hash = crypto::SHA256HashString(spki);
290 x509_util::CSSMCachedCertificate cached_cert;
291 if (cached_cert.Init(cert) != CSSM_OK) {
292 NOTREACHED();
293 continue;
294 }
295 x509_util::CSSMFieldValue serial_number;
296 err = cached_cert.GetField(&CSSMOID_X509V1SerialNumber, &serial_number);
297 if (err || !serial_number.field()) {
298 NOTREACHED();
299 continue;
300 }
301
302 base::StringPiece serial(
303 reinterpret_cast<const char*>(serial_number.field()->Data),
304 serial_number.field()->Length);
305
306 CRLSet::Result result = crl_set->CheckSPKI(spki_hash);
307
308 if (result != CRLSet::REVOKED && !issuer_spki_hash.empty())
309 result = crl_set->CheckSerial(serial, issuer_spki_hash);
310
311 issuer_spki_hash = spki_hash;
312
313 switch (result) {
314 case CRLSet::REVOKED:
315 return false;
316 case CRLSet::UNKNOWN:
317 case CRLSet::GOOD:
318 continue;
319 default:
320 NOTREACHED();
321 return false;
322 }
323 }
324
325 return true;
326}
327
328// IsIssuedByKnownRoot returns true if the given chain is rooted at a root CA
329// that we recognise as a standard root.
330// static
331bool IsIssuedByKnownRoot(CFArrayRef chain) {
332 int n = CFArrayGetCount(chain);
333 if (n < 1)
334 return false;
335 SecCertificateRef root_ref = reinterpret_cast<SecCertificateRef>(
336 const_cast<void*>(CFArrayGetValueAtIndex(chain, n - 1)));
[email protected]ede03212012-09-07 12:52:26337 SHA1HashValue hash = X509Certificate::CalculateFingerprint(root_ref);
[email protected]62b23c22012-03-22 04:50:24338 return IsSHA1HashInSortedArray(
339 hash, &kKnownRootCertSHA1Hashes[0][0], sizeof(kKnownRootCertSHA1Hashes));
340}
341
342} // namespace
343
344CertVerifyProcMac::CertVerifyProcMac() {}
345
346CertVerifyProcMac::~CertVerifyProcMac() {}
347
348int CertVerifyProcMac::VerifyInternal(X509Certificate* cert,
349 const std::string& hostname,
350 int flags,
351 CRLSet* crl_set,
352 CertVerifyResult* verify_result) {
353 ScopedCFTypeRef<CFArrayRef> trust_policies;
354 OSStatus status = CreateTrustPolicies(hostname, flags, &trust_policies);
355 if (status)
356 return NetErrorFromOSStatus(status);
357
358 // Create and configure a SecTrustRef, which takes our certificate(s)
359 // and our SSL SecPolicyRef. SecTrustCreateWithCertificates() takes an
360 // array of certificates, the first of which is the certificate we're
361 // verifying, and the subsequent (optional) certificates are used for
362 // chain building.
363 ScopedCFTypeRef<CFArrayRef> cert_array(cert->CreateOSCertChainForCert());
364
365 // From here on, only one thread can be active at a time. We have had a number
366 // of sporadic crashes in the SecTrustEvaluate call below, way down inside
367 // Apple's cert code, which we suspect are caused by a thread-safety issue.
368 // So as a speculative fix allow only one thread to use SecTrust on this cert.
369 base::AutoLock lock(verification_lock_);
370
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