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