Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2016 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/cert/cert_verify_proc_ios.h" | |
| 6 | |
| 7 #include <CommonCrypto/CommonDigest.h> | |
| 8 #include <Security/Security.h> | |
| 9 | |
| 10 #include "base/logging.h" | |
| 11 #include "base/mac/scoped_cftyperef.h" | |
| 12 #include "base/strings/sys_string_conversions.h" | |
|
Ryan Sleevi
2016/03/24 20:17:42
Unused?
svaldez
2016/03/25 16:54:11
Done.
| |
| 13 #include "crypto/sha2.h" | |
| 14 #include "net/base/net_errors.h" | |
| 15 #include "net/cert/asn1_util.h" | |
| 16 #include "net/cert/cert_verify_result.h" | |
| 17 #include "net/cert/crl_set.h" | |
|
Ryan Sleevi
2016/03/24 20:17:42
Unused/unneeded.
svaldez
2016/03/25 16:54:11
Done.
| |
| 18 #include "net/cert/test_root_certs.h" | |
| 19 #include "net/cert/x509_certificate.h" | |
| 20 #include "net/ssl/openssl_ssl_util.h" | |
| 21 | |
| 22 using base::ScopedCFTypeRef; | |
| 23 | |
| 24 namespace net { | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 typedef OSStatus (*SecTrustCopyExtendedResultFuncPtr)(SecTrustRef, | |
| 29 CFDictionaryRef*); | |
|
Ryan Sleevi
2016/03/24 20:17:42
Unused (it's for EV, no need to muck with that her
svaldez
2016/03/25 16:54:11
Done.
| |
| 30 | |
| 31 int NetErrorFromOSStatus(OSStatus status) { | |
| 32 switch (status) { | |
| 33 case noErr: | |
| 34 return OK; | |
| 35 case errSecNotAvailable: | |
| 36 return ERR_NOT_IMPLEMENTED; | |
| 37 case errSecAuthFailed: | |
| 38 return ERR_ACCESS_DENIED; | |
| 39 default: | |
| 40 return ERR_FAILED; | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 // Creates a series of SecPolicyRefs to be added to a SecTrustRef used to | |
| 45 // validate a certificate for an SSL server. |hostname| contains the name of | |
| 46 // the SSL server that the certificate should be verified against. If | |
| 47 // successful, returns noErr, and stores the resultant array of SecPolicyRefs | |
| 48 // in |policies|. | |
| 49 OSStatus CreateTrustPolicies(ScopedCFTypeRef<CFArrayRef>* policies) { | |
| 50 ScopedCFTypeRef<CFMutableArrayRef> local_policies( | |
| 51 CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks)); | |
| 52 if (!local_policies) | |
| 53 return errSecAllocate; | |
| 54 | |
| 55 SecPolicyRef ssl_policy = SecPolicyCreateBasicX509(); | |
| 56 CFArrayAppendValue(local_policies, ssl_policy); | |
| 57 CFRelease(ssl_policy); | |
| 58 ssl_policy = SecPolicyCreateSSL(true, nullptr); | |
| 59 CFArrayAppendValue(local_policies, ssl_policy); | |
| 60 CFRelease(ssl_policy); | |
| 61 | |
| 62 policies->reset(local_policies.release()); | |
| 63 return noErr; | |
| 64 } | |
| 65 | |
| 66 // Builds and evaluates a SecTrustRef for the certificate chain contained | |
| 67 // in |cert_array|, using the verification policies in |trust_policies|. On | |
| 68 // success, returns OK, and updates |trust_ref| and |trust_result|. On failure, | |
| 69 // no output parameters are modified. | |
| 70 // | |
| 71 // Note: An OK return does not mean that |cert_array| is trusted, merely that | |
| 72 // verification was performed successfully. | |
| 73 int BuildAndEvaluateSecTrustRef(CFArrayRef cert_array, | |
| 74 CFArrayRef trust_policies, | |
| 75 ScopedCFTypeRef<SecTrustRef>* trust_ref, | |
| 76 ScopedCFTypeRef<CFArrayRef>* verified_chain, | |
| 77 SecTrustResultType* trust_result) { | |
| 78 SecTrustRef tmp_trust = nullptr; | |
| 79 OSStatus status = | |
| 80 SecTrustCreateWithCertificates(cert_array, trust_policies, &tmp_trust); | |
| 81 if (status) | |
| 82 return NetErrorFromOSStatus(status); | |
| 83 ScopedCFTypeRef<SecTrustRef> scoped_tmp_trust(tmp_trust); | |
| 84 | |
| 85 if (TestRootCerts::HasInstance()) { | |
| 86 status = TestRootCerts::GetInstance()->FixupSecTrustRef(tmp_trust); | |
| 87 if (status) | |
| 88 return NetErrorFromOSStatus(status); | |
| 89 } | |
| 90 | |
| 91 SecTrustResultType tmp_trust_result; | |
| 92 status = SecTrustEvaluate(tmp_trust, &tmp_trust_result); | |
| 93 if (status) | |
| 94 return NetErrorFromOSStatus(status); | |
| 95 | |
| 96 ScopedCFTypeRef<CFMutableArrayRef> tmp_verified_chain( | |
| 97 CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks)); | |
| 98 const CFIndex chain_length = SecTrustGetCertificateCount(tmp_trust); | |
| 99 for (CFIndex i = 0; i < chain_length; ++i) { | |
| 100 SecCertificateRef chain_cert = SecTrustGetCertificateAtIndex(tmp_trust, i); | |
| 101 CFArrayAppendValue(tmp_verified_chain, chain_cert); | |
| 102 } | |
| 103 | |
| 104 trust_ref->swap(scoped_tmp_trust); | |
| 105 *trust_result = tmp_trust_result; | |
| 106 verified_chain->reset(tmp_verified_chain.release()); | |
| 107 return OK; | |
| 108 } | |
| 109 | |
| 110 void GetCertChainInfo(CFArrayRef cert_chain, CertVerifyResult* verify_result) { | |
| 111 DCHECK_LT(0, CFArrayGetCount(cert_chain)); | |
| 112 | |
| 113 verify_result->has_md2 = false; | |
| 114 verify_result->has_md4 = false; | |
| 115 verify_result->has_md5 = false; | |
| 116 verify_result->has_sha1 = false; | |
| 117 verify_result->has_sha1_leaf = false; | |
| 118 | |
| 119 SecCertificateRef verified_cert = nullptr; | |
| 120 std::vector<SecCertificateRef> verified_chain; | |
| 121 for (CFIndex i = 0, count = CFArrayGetCount(cert_chain); i < count; ++i) { | |
| 122 SecCertificateRef chain_cert = reinterpret_cast<SecCertificateRef>( | |
| 123 const_cast<void*>(CFArrayGetValueAtIndex(cert_chain, i))); | |
| 124 if (i == 0) { | |
| 125 verified_cert = chain_cert; | |
| 126 } else { | |
| 127 verified_chain.push_back(chain_cert); | |
| 128 } | |
| 129 | |
| 130 std::string der_bytes; | |
| 131 if (!X509Certificate::GetDEREncoded(chain_cert, &der_bytes)) | |
| 132 return; | |
| 133 const uint8_t* bytes = reinterpret_cast<const uint8_t*>(der_bytes.data()); | |
| 134 ScopedX509 x509_cert(d2i_X509(NULL, &bytes, der_bytes.size())); | |
| 135 | |
| 136 base::StringPiece spki_bytes; | |
| 137 if (!asn1::ExtractSPKIFromDERCert(der_bytes, &spki_bytes)) | |
| 138 continue; | |
| 139 | |
| 140 HashValue sha1(HASH_VALUE_SHA1); | |
| 141 CC_SHA1(spki_bytes.data(), spki_bytes.size(), sha1.data()); | |
| 142 verify_result->public_key_hashes.push_back(sha1); | |
| 143 | |
| 144 HashValue sha256(HASH_VALUE_SHA256); | |
| 145 CC_SHA256(spki_bytes.data(), spki_bytes.size(), sha256.data()); | |
| 146 verify_result->public_key_hashes.push_back(sha256); | |
| 147 | |
| 148 int sig_alg = OBJ_obj2nid(x509_cert->sig_alg->algorithm); | |
| 149 if (sig_alg == NID_md2WithRSAEncryption) { | |
| 150 verify_result->has_md2 = true; | |
| 151 } else if (sig_alg == NID_md4WithRSAEncryption) { | |
| 152 verify_result->has_md4 = true; | |
| 153 } else if (sig_alg == NID_md5WithRSAEncryption || | |
| 154 sig_alg == NID_md5WithRSA) { | |
| 155 verify_result->has_md5 = true; | |
| 156 } else if (sig_alg == NID_sha1WithRSAEncryption || | |
| 157 sig_alg == NID_dsaWithSHA || sig_alg == NID_dsaWithSHA1 || | |
| 158 sig_alg == NID_dsaWithSHA1_2 || sig_alg == NID_sha1WithRSA || | |
| 159 sig_alg == NID_ecdsa_with_SHA1) { | |
| 160 verify_result->has_sha1 = true; | |
| 161 if (i == 0) | |
| 162 verify_result->has_sha1_leaf = true; | |
| 163 } | |
| 164 } | |
| 165 if (!verified_cert) { | |
| 166 NOTREACHED(); | |
| 167 return; | |
| 168 } | |
| 169 | |
| 170 verify_result->verified_cert = | |
| 171 X509Certificate::CreateFromHandle(verified_cert, verified_chain); | |
| 172 } | |
| 173 | |
| 174 } // namespace | |
| 175 | |
| 176 CertVerifyProcIOS::CertVerifyProcIOS() {} | |
| 177 | |
| 178 CertVerifyProcIOS::~CertVerifyProcIOS() {} | |
| 179 | |
| 180 bool CertVerifyProcIOS::SupportsAdditionalTrustAnchors() const { | |
| 181 return false; | |
| 182 } | |
| 183 | |
| 184 bool CertVerifyProcIOS::SupportsOCSPStapling() const { | |
| 185 return false; | |
| 186 } | |
| 187 | |
| 188 int CertVerifyProcIOS::VerifyInternal( | |
| 189 X509Certificate* cert, | |
| 190 const std::string& hostname, | |
| 191 const std::string& ocsp_response, | |
| 192 int flags, | |
| 193 CRLSet* crl_set, | |
| 194 const CertificateList& additional_trust_anchors, | |
| 195 CertVerifyResult* verify_result) { | |
| 196 ScopedCFTypeRef<CFArrayRef> trust_policies; | |
| 197 OSStatus status = CreateTrustPolicies(&trust_policies); | |
| 198 if (status) | |
| 199 return NetErrorFromOSStatus(status); | |
| 200 | |
| 201 ScopedCFTypeRef<CFMutableArrayRef> cert_array( | |
| 202 cert->CreateOSCertChainForCert()); | |
| 203 ScopedCFTypeRef<SecTrustRef> trust_ref; | |
| 204 SecTrustResultType trust_result = kSecTrustResultDeny; | |
| 205 ScopedCFTypeRef<CFArrayRef> final_chain; | |
| 206 | |
| 207 status = BuildAndEvaluateSecTrustRef(cert_array, trust_policies, &trust_ref, | |
| 208 &final_chain, &trust_result); | |
| 209 if (status) | |
| 210 return NetErrorFromOSStatus(status); | |
| 211 | |
| 212 if (CFArrayGetCount(final_chain) == 0) | |
| 213 return ERR_ACCESS_DENIED; | |
|
Ryan Sleevi
2016/03/24 20:17:42
ERR_FAILED or ERR_CERT_INVALID seem more applicabl
svaldez
2016/03/25 16:54:11
Done.
| |
| 214 | |
| 215 GetCertChainInfo(final_chain, verify_result); | |
| 216 | |
| 217 // TODO(sleevi): Support CRLSet revocation. | |
| 218 // TODO(svaldez): Add expiration handling. | |
|
Ryan Sleevi
2016/03/24 20:17:42
May be worth clarifying:
// TODO(svaldez): Add sp
svaldez
2016/03/25 16:54:11
Done.
| |
| 219 switch (trust_result) { | |
| 220 case kSecTrustResultUnspecified: | |
| 221 case kSecTrustResultProceed: | |
| 222 break; | |
| 223 case kSecTrustResultDeny: | |
| 224 verify_result->cert_status |= CERT_STATUS_AUTHORITY_INVALID; | |
| 225 default: | |
| 226 verify_result->cert_status |= CERT_STATUS_INVALID; | |
| 227 } | |
| 228 | |
| 229 // Perform hostname verification independent of SecTrustEvaluate. | |
| 230 if (!verify_result->verified_cert->VerifyNameMatch( | |
| 231 hostname, &verify_result->common_name_fallback_used)) { | |
| 232 verify_result->cert_status |= CERT_STATUS_COMMON_NAME_INVALID; | |
| 233 } | |
| 234 | |
| 235 verify_result->is_issued_by_known_root = false; | |
| 236 | |
| 237 if (IsCertStatusError(verify_result->cert_status)) | |
| 238 return MapCertStatusToNetError(verify_result->cert_status); | |
| 239 | |
| 240 return OK; | |
| 241 } | |
| 242 | |
| 243 } // namespace net | |
| OLD | NEW |