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_openssl_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" | |
| 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" | |
| 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*); | |
| 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; | |
|
Ryan Sleevi
2016/03/21 20:49:55
This doesn't seem right, because it means mapping
svaldez
2016/03/21 21:36:26
This is the same thing we do for cert_verify_proc
| |
| 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(const std::string& hostname, | |
| 50 ScopedCFTypeRef<CFArrayRef>* policies) { | |
| 51 ScopedCFTypeRef<CFMutableArrayRef> local_policies( | |
| 52 CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks)); | |
| 53 if (!local_policies) | |
| 54 return errSecAllocate; | |
| 55 | |
| 56 SecPolicyRef ssl_policy = SecPolicyCreateBasicX509(); | |
| 57 CFArrayAppendValue(local_policies, ssl_policy); | |
| 58 CFRelease(ssl_policy); | |
| 59 ssl_policy = SecPolicyCreateSSL(true, base::SysUTF8ToCFStringRef(hostname)); | |
|
Ryan Sleevi
2016/03/21 20:49:55
I'm torn as to whether or not we want to do this,
svaldez
2016/03/21 21:36:26
Will drop the hostname verification for now.
| |
| 60 CFArrayAppendValue(local_policies, ssl_policy); | |
| 61 CFRelease(ssl_policy); | |
| 62 | |
| 63 policies->reset(local_policies.release()); | |
| 64 return noErr; | |
| 65 } | |
| 66 | |
| 67 // Builds and evaluates a SecTrustRef for the certificate chain contained | |
| 68 // in |cert_array|, using the verification policies in |trust_policies|. On | |
| 69 // success, returns OK, and updates |trust_ref| and |trust_result|. On failure, | |
| 70 // no output parameters are modified. | |
| 71 // | |
| 72 // Note: An OK return does not mean that |cert_array| is trusted, merely that | |
| 73 // verification was performed successfully. | |
| 74 int BuildAndEvaluateSecTrustRef(CFArrayRef cert_array, | |
| 75 CFArrayRef trust_policies, | |
| 76 ScopedCFTypeRef<SecTrustRef>* trust_ref, | |
| 77 ScopedCFTypeRef<CFArrayRef>* verified_chain, | |
| 78 SecTrustResultType* trust_result) { | |
| 79 SecTrustRef tmp_trust = NULL; | |
| 80 OSStatus status = | |
| 81 SecTrustCreateWithCertificates(cert_array, trust_policies, &tmp_trust); | |
| 82 if (status) | |
| 83 return NetErrorFromOSStatus(status); | |
| 84 ScopedCFTypeRef<SecTrustRef> scoped_tmp_trust(tmp_trust); | |
| 85 | |
| 86 if (TestRootCerts::HasInstance()) { | |
| 87 status = TestRootCerts::GetInstance()->FixupSecTrustRef(tmp_trust); | |
| 88 if (status) | |
| 89 return NetErrorFromOSStatus(status); | |
| 90 } | |
| 91 | |
| 92 SecTrustResultType tmp_trust_result; | |
| 93 status = SecTrustEvaluate(tmp_trust, &tmp_trust_result); | |
| 94 if (status) | |
| 95 return NetErrorFromOSStatus(status); | |
| 96 | |
| 97 ScopedCFTypeRef<CFMutableArrayRef> tmp_verified_chain( | |
| 98 CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks)); | |
| 99 const CFIndex chain_length = SecTrustGetCertificateCount(tmp_trust); | |
| 100 for (CFIndex i = 0; i < chain_length; ++i) { | |
| 101 SecCertificateRef chain_cert = SecTrustGetCertificateAtIndex(tmp_trust, i); | |
| 102 CFArrayAppendValue(tmp_verified_chain, chain_cert); | |
| 103 CFRelease(chain_cert); | |
| 104 } | |
| 105 | |
| 106 trust_ref->swap(scoped_tmp_trust); | |
| 107 *trust_result = tmp_trust_result; | |
| 108 verified_chain->reset(tmp_verified_chain.release()); | |
| 109 return OK; | |
| 110 } | |
| 111 | |
| 112 void GetCertChainInfo(CFArrayRef cert_chain, | |
| 113 CertVerifyResult* verify_result, | |
| 114 bool* leaf_is_weak) { | |
| 115 DCHECK_LT(0, CFArrayGetCount(cert_chain)); | |
| 116 | |
| 117 *leaf_is_weak = false; | |
| 118 verify_result->has_md2 = false; | |
| 119 verify_result->has_md4 = false; | |
| 120 verify_result->has_md5 = false; | |
| 121 verify_result->has_sha1 = false; | |
| 122 verify_result->has_sha1_leaf = false; | |
| 123 | |
| 124 SecCertificateRef verified_cert = NULL; | |
| 125 std::vector<SecCertificateRef> verified_chain; | |
| 126 for (CFIndex i = 0, count = CFArrayGetCount(cert_chain); i < count; ++i) { | |
| 127 SecCertificateRef chain_cert = | |
| 128 X509Certificate::DupOSCertHandle(reinterpret_cast<SecCertificateRef>( | |
|
Ryan Sleevi
2016/03/21 20:49:55
BUG: You end up leaking all of these certs. You sh
svaldez
2016/03/21 21:36:26
Is that the case, I had errors without this. The R
Ryan Sleevi
2016/03/21 21:44:14
You mean the member |verified_chain|? If so, that
svaldez
2016/03/22 15:05:56
Fixed. There was an extra CFRelease I was doing up
| |
| 129 const_cast<void*>(CFArrayGetValueAtIndex(cert_chain, i)))); | |
| 130 if (i == 0) { | |
| 131 verified_cert = chain_cert; | |
| 132 } else { | |
| 133 verified_chain.push_back(chain_cert); | |
| 134 } | |
| 135 | |
| 136 ScopedX509 x509_cert = OSCertHandleToOpenSSL(chain_cert); | |
| 137 int sig_alg = OBJ_obj2nid(x509_cert->sig_alg->algorithm); | |
| 138 if (sig_alg == NID_md2WithRSAEncryption) { | |
| 139 verify_result->has_md2 = true; | |
| 140 if (i == 0) | |
| 141 *leaf_is_weak = true; | |
| 142 } else if (sig_alg == NID_md4WithRSAEncryption) { | |
| 143 verify_result->has_md4 = true; | |
| 144 if (i == 0) | |
| 145 *leaf_is_weak = true; | |
| 146 } else if (sig_alg == NID_md5WithRSAEncryption || | |
| 147 sig_alg == NID_md5WithRSA) { | |
| 148 verify_result->has_md5 = true; | |
| 149 if (i == 0) | |
| 150 *leaf_is_weak = true; | |
| 151 } else if (sig_alg == NID_sha1WithRSAEncryption || | |
| 152 sig_alg == NID_dsaWithSHA || sig_alg == NID_dsaWithSHA1 || | |
| 153 sig_alg == NID_dsaWithSHA1_2 || sig_alg == NID_sha1WithRSA || | |
| 154 sig_alg == NID_ecdsa_with_SHA1) { | |
| 155 verify_result->has_sha1 = true; | |
| 156 if (i == 0) { | |
| 157 verify_result->has_sha1_leaf = true; | |
| 158 *leaf_is_weak = true; | |
| 159 } | |
| 160 } | |
| 161 } | |
| 162 if (!verified_cert) { | |
| 163 NOTREACHED(); | |
| 164 return; | |
| 165 } | |
| 166 | |
| 167 verify_result->verified_cert = | |
| 168 X509Certificate::CreateFromHandle(verified_cert, verified_chain); | |
| 169 } | |
| 170 | |
| 171 void AppendPublicKeyHashes(CFArrayRef chain, HashValueVector* hashes) { | |
| 172 const CFIndex n = CFArrayGetCount(chain); | |
| 173 for (CFIndex i = 0; i < n; i++) { | |
| 174 SecCertificateRef cert = reinterpret_cast<SecCertificateRef>( | |
| 175 const_cast<void*>(CFArrayGetValueAtIndex(chain, i))); | |
| 176 | |
| 177 std::string der_bytes; | |
| 178 if (!X509Certificate::GetDEREncoded(cert, &der_bytes)) | |
| 179 return; | |
| 180 base::StringPiece spki_bytes; | |
| 181 if (!asn1::ExtractSPKIFromDERCert(der_bytes, &spki_bytes)) | |
| 182 continue; | |
| 183 | |
| 184 HashValue sha1(HASH_VALUE_SHA1); | |
| 185 CC_SHA1(spki_bytes.data(), spki_bytes.size(), sha1.data()); | |
| 186 hashes->push_back(sha1); | |
| 187 | |
| 188 HashValue sha256(HASH_VALUE_SHA256); | |
| 189 CC_SHA256(spki_bytes.data(), spki_bytes.size(), sha256.data()); | |
| 190 hashes->push_back(sha256); | |
| 191 } | |
| 192 } | |
| 193 | |
| 194 bool CheckRevocationWithCRLSet(CFArrayRef chain, CRLSet* crl_set) { | |
|
Ryan Sleevi
2016/03/21 20:49:55
I don't think we want/need this behaviour, given t
svaldez
2016/03/21 21:36:26
Done.
| |
| 195 if (CFArrayGetCount(chain) == 0) | |
| 196 return true; | |
| 197 | |
| 198 // We iterate from the root certificate down to the leaf, keeping track of | |
| 199 // the issuer's SPKI at each step. | |
| 200 std::string issuer_spki_hash; | |
| 201 for (CFIndex i = CFArrayGetCount(chain) - 1; i >= 0; i--) { | |
| 202 SecCertificateRef cert = reinterpret_cast<SecCertificateRef>( | |
| 203 const_cast<void*>(CFArrayGetValueAtIndex(chain, i))); | |
| 204 | |
| 205 std::string der_bytes; | |
| 206 if (!X509Certificate::GetDEREncoded(cert, &der_bytes)) | |
| 207 return false; | |
| 208 base::StringPiece spki; | |
| 209 if (!asn1::ExtractSPKIFromDERCert(der_bytes, &spki)) { | |
| 210 NOTREACHED(); | |
| 211 continue; | |
| 212 } | |
| 213 | |
| 214 const std::string spki_hash = crypto::SHA256HashString(spki); | |
| 215 scoped_refptr<X509Certificate::X509Certificate> x509_cert = | |
| 216 X509Certificate::CreateFromHandle(cert, | |
| 217 X509Certificate::OSCertHandles()); | |
| 218 | |
| 219 CRLSet::Result result = crl_set->CheckSPKI(spki_hash); | |
| 220 | |
| 221 if (result != CRLSet::REVOKED && !issuer_spki_hash.empty()) { | |
| 222 result = | |
| 223 crl_set->CheckSerial(x509_cert->serial_number(), issuer_spki_hash); | |
| 224 } | |
| 225 | |
| 226 issuer_spki_hash = spki_hash; | |
| 227 | |
| 228 switch (result) { | |
| 229 case CRLSet::REVOKED: | |
| 230 return false; | |
| 231 case CRLSet::UNKNOWN: | |
| 232 case CRLSet::GOOD: | |
| 233 continue; | |
| 234 default: | |
| 235 NOTREACHED(); | |
| 236 return false; | |
| 237 } | |
| 238 } | |
| 239 | |
| 240 return true; | |
| 241 } | |
| 242 | |
| 243 } // namespace | |
| 244 | |
| 245 CertVerifyProcIOS::CertVerifyProcIOS() {} | |
| 246 | |
| 247 CertVerifyProcIOS::~CertVerifyProcIOS() {} | |
| 248 | |
| 249 bool CertVerifyProcIOS::SupportsAdditionalTrustAnchors() const { | |
| 250 return false; | |
| 251 } | |
| 252 | |
| 253 bool CertVerifyProcIOS::SupportsOCSPStapling() const { | |
| 254 return false; | |
| 255 } | |
| 256 | |
| 257 int CertVerifyProcIOS::VerifyInternal( | |
| 258 X509Certificate* cert, | |
| 259 const std::string& hostname, | |
| 260 const std::string& ocsp_response, | |
| 261 int flags, | |
| 262 CRLSet* crl_set, | |
| 263 const CertificateList& additional_trust_anchors, | |
| 264 CertVerifyResult* verify_result) { | |
| 265 ScopedCFTypeRef<CFArrayRef> trust_policies; | |
| 266 OSStatus status = CreateTrustPolicies(hostname, &trust_policies); | |
| 267 if (status) | |
| 268 return NetErrorFromOSStatus(status); | |
| 269 | |
| 270 ScopedCFTypeRef<CFMutableArrayRef> cert_array( | |
| 271 cert->CreateOSCertChainForCert()); | |
| 272 ScopedCFTypeRef<SecTrustRef> trust_ref; | |
| 273 SecTrustResultType trust_result = kSecTrustResultDeny; | |
| 274 ScopedCFTypeRef<CFArrayRef> final_chain; | |
| 275 | |
| 276 status = BuildAndEvaluateSecTrustRef(cert_array, trust_policies, &trust_ref, | |
| 277 &final_chain, &trust_result); | |
| 278 if (status) | |
| 279 return NetErrorFromOSStatus(status); | |
| 280 | |
| 281 if (CFArrayGetCount(final_chain) > 0) { | |
| 282 bool unused_leaf_weak; | |
| 283 GetCertChainInfo(final_chain, verify_result, &unused_leaf_weak); | |
| 284 } | |
| 285 | |
| 286 if (crl_set && !CheckRevocationWithCRLSet(final_chain, crl_set)) | |
| 287 verify_result->cert_status |= CERT_STATUS_REVOKED; | |
| 288 | |
| 289 switch (trust_result) { | |
|
Ryan Sleevi
2016/03/21 20:49:55
BUG: No expiration handling is done here, which wa
svaldez
2016/03/21 21:36:26
Added a TODO. As it stands, this is mostly focused
Ryan Sleevi
2016/03/21 21:44:14
Yeah, I debated a comment to that effect, but iOS
| |
| 290 case kSecTrustResultUnspecified: | |
| 291 case kSecTrustResultProceed: | |
| 292 break; | |
| 293 case kSecTrustResultDeny: | |
| 294 verify_result->cert_status |= CERT_STATUS_AUTHORITY_INVALID; | |
|
Ryan Sleevi
2016/03/21 20:49:55
I don't think this this the right mapping code; a
svaldez
2016/03/21 21:36:26
For Mac we've been using AUTHORITY_INVALID in this
Ryan Sleevi
2016/03/21 21:44:14
OK, fair enough.
| |
| 295 default: | |
| 296 verify_result->cert_status |= CERT_STATUS_INVALID; | |
| 297 } | |
| 298 | |
| 299 // Perform hostname verification independent of SecTrustEvaluate. In order to | |
| 300 // do so, mask off any reported name errors first. | |
| 301 verify_result->cert_status &= ~CERT_STATUS_COMMON_NAME_INVALID; | |
|
Ryan Sleevi
2016/03/21 20:49:55
This is never set (per above), so it's impossible
svaldez
2016/03/21 21:36:26
Done.
| |
| 302 if (!cert->VerifyNameMatch(hostname, | |
| 303 &verify_result->common_name_fallback_used)) { | |
| 304 verify_result->cert_status |= CERT_STATUS_COMMON_NAME_INVALID; | |
| 305 } | |
| 306 | |
| 307 verify_result->cert_status &= ~CERT_STATUS_NO_REVOCATION_MECHANISM; | |
|
Ryan Sleevi
2016/03/21 20:49:55
This is never set.
svaldez
2016/03/21 21:36:26
Done.
| |
| 308 AppendPublicKeyHashes(final_chain, &verify_result->public_key_hashes); | |
| 309 verify_result->is_issued_by_known_root = true; | |
|
Ryan Sleevi
2016/03/21 20:49:55
BUG: This isn't correct. We default to false, not
svaldez
2016/03/21 21:36:26
Done.
| |
| 310 | |
| 311 if (IsCertStatusError(verify_result->cert_status)) | |
| 312 return MapCertStatusToNetError(verify_result->cert_status); | |
| 313 | |
| 314 return OK; | |
| 315 } | |
| 316 | |
| 317 } // namespace net | |
| OLD | NEW |