| OLD | NEW |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/cert/cert_verify_proc_ios.h" | 5 #include "net/cert/cert_verify_proc_ios.h" |
| 6 | 6 |
| 7 #include <CommonCrypto/CommonDigest.h> | 7 #include <CommonCrypto/CommonDigest.h> |
| 8 #include <Security/Security.h> | |
| 9 | 8 |
| 10 #include "base/logging.h" | 9 #include "base/logging.h" |
| 11 #include "base/mac/scoped_cftyperef.h" | 10 #include "base/mac/scoped_cftyperef.h" |
| 12 #include "crypto/sha2.h" | 11 #include "crypto/sha2.h" |
| 13 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 14 #include "net/cert/asn1_util.h" | 13 #include "net/cert/asn1_util.h" |
| 15 #include "net/cert/cert_verify_result.h" | 14 #include "net/cert/cert_verify_result.h" |
| 16 #include "net/cert/test_root_certs.h" | 15 #include "net/cert/test_root_certs.h" |
| 17 #include "net/cert/x509_certificate.h" | 16 #include "net/cert/x509_certificate.h" |
| 18 #include "net/ssl/openssl_ssl_util.h" | 17 #include "net/ssl/openssl_ssl_util.h" |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 } | 164 } |
| 166 if (!verified_cert) { | 165 if (!verified_cert) { |
| 167 NOTREACHED(); | 166 NOTREACHED(); |
| 168 return; | 167 return; |
| 169 } | 168 } |
| 170 | 169 |
| 171 verify_result->verified_cert = | 170 verify_result->verified_cert = |
| 172 X509Certificate::CreateFromHandle(verified_cert, verified_chain); | 171 X509Certificate::CreateFromHandle(verified_cert, verified_chain); |
| 173 } | 172 } |
| 174 | 173 |
| 174 } // namespace |
| 175 |
| 176 CertVerifyProcIOS::CertVerifyProcIOS() {} |
| 177 |
| 175 // The iOS APIs don't expose an API-stable set of reasons for certificate | 178 // The iOS APIs don't expose an API-stable set of reasons for certificate |
| 176 // validation failures. However, internally, the reason is tracked, and it's | 179 // validation failures. However, internally, the reason is tracked, and it's |
| 177 // converted to user-facing localized strings. | 180 // converted to user-facing localized strings. |
| 178 // | 181 // |
| 179 // In the absence of a consistent API, convert the English strings to their | 182 // In the absence of a consistent API, convert the English strings to their |
| 180 // localized counterpart, and then compare that with the error properties. If | 183 // localized counterpart, and then compare that with the error properties. If |
| 181 // they're equal, it's a strong sign that this was the cause for the error. | 184 // they're equal, it's a strong sign that this was the cause for the error. |
| 182 // While this will break if/when iOS changes the contents of these strings, | 185 // While this will break if/when iOS changes the contents of these strings, |
| 183 // it's sufficient enough for now. | 186 // it's sufficient enough for now. |
| 184 // | 187 // |
| 185 // TODO(rsleevi): https://crbug.com/601915 - Use a less brittle solution when | 188 // TODO(rsleevi): https://crbug.com/601915 - Use a less brittle solution when |
| 186 // possible. | 189 // possible. |
| 187 CertStatus GetFailureFromTrustProperties(CFArrayRef properties) { | 190 // static |
| 191 CertStatus CertVerifyProcIOS::GetCertFailureStatusFromTrust(SecTrustRef trust) { |
| 188 CertStatus reason = 0; | 192 CertStatus reason = 0; |
| 189 | 193 |
| 194 base::ScopedCFTypeRef<CFArrayRef> properties(SecTrustCopyProperties(trust)); |
| 190 if (!properties) | 195 if (!properties) |
| 191 return CERT_STATUS_INVALID; | 196 return CERT_STATUS_INVALID; |
| 192 | 197 |
| 193 const CFIndex properties_length = CFArrayGetCount(properties); | 198 const CFIndex properties_length = CFArrayGetCount(properties); |
| 194 if (properties_length == 0) | 199 if (properties_length == 0) |
| 195 return CERT_STATUS_INVALID; | 200 return CERT_STATUS_INVALID; |
| 196 | 201 |
| 197 CFBundleRef bundle = | 202 CFBundleRef bundle = |
| 198 CFBundleGetBundleWithIdentifier(CFSTR("com.apple.Security")); | 203 CFBundleGetBundleWithIdentifier(CFSTR("com.apple.Security")); |
| 199 CFStringRef date_string = | 204 CFStringRef date_string = |
| 200 CFSTR("One or more certificates have expired or are not valid yet."); | 205 CFSTR("One or more certificates have expired or are not valid yet."); |
| 201 ScopedCFTypeRef<CFStringRef> date_error(CFBundleCopyLocalizedString( | 206 ScopedCFTypeRef<CFStringRef> date_error(CFBundleCopyLocalizedString( |
| 202 bundle, date_string, date_string, CFSTR("SecCertificate"))); | 207 bundle, date_string, date_string, CFSTR("SecCertificate"))); |
| 203 CFStringRef trust_string = CFSTR("Root certificate is not trusted."); | 208 CFStringRef trust_string = CFSTR("Root certificate is not trusted."); |
| 204 ScopedCFTypeRef<CFStringRef> trust_error(CFBundleCopyLocalizedString( | 209 ScopedCFTypeRef<CFStringRef> trust_error(CFBundleCopyLocalizedString( |
| 205 bundle, trust_string, trust_string, CFSTR("SecCertificate"))); | 210 bundle, trust_string, trust_string, CFSTR("SecCertificate"))); |
| 206 CFStringRef weak_string = | 211 CFStringRef weak_string = |
| 207 CFSTR("One or more certificates is using a weak key size."); | 212 CFSTR("One or more certificates is using a weak key size."); |
| 208 ScopedCFTypeRef<CFStringRef> weak_error(CFBundleCopyLocalizedString( | 213 ScopedCFTypeRef<CFStringRef> weak_error(CFBundleCopyLocalizedString( |
| 209 bundle, weak_string, weak_string, CFSTR("SecCertificate"))); | 214 bundle, weak_string, weak_string, CFSTR("SecCertificate"))); |
| 215 CFStringRef hostname_mismatch_string = CFSTR("Hostname mismatch."); |
| 216 ScopedCFTypeRef<CFStringRef> hostname_mismatch_error( |
| 217 CFBundleCopyLocalizedString(bundle, hostname_mismatch_string, |
| 218 hostname_mismatch_string, |
| 219 CFSTR("SecCertificate"))); |
| 210 | 220 |
| 211 for (CFIndex i = 0; i < properties_length; ++i) { | 221 for (CFIndex i = 0; i < properties_length; ++i) { |
| 212 CFDictionaryRef dict = reinterpret_cast<CFDictionaryRef>( | 222 CFDictionaryRef dict = reinterpret_cast<CFDictionaryRef>( |
| 213 const_cast<void*>(CFArrayGetValueAtIndex(properties, i))); | 223 const_cast<void*>(CFArrayGetValueAtIndex(properties, i))); |
| 214 CFStringRef error = reinterpret_cast<CFStringRef>( | 224 CFStringRef error = reinterpret_cast<CFStringRef>( |
| 215 const_cast<void*>(CFDictionaryGetValue(dict, CFSTR("value")))); | 225 const_cast<void*>(CFDictionaryGetValue(dict, CFSTR("value")))); |
| 216 | 226 |
| 217 if (CFEqual(error, date_error)) { | 227 if (CFEqual(error, date_error)) { |
| 218 reason |= CERT_STATUS_DATE_INVALID; | 228 reason |= CERT_STATUS_DATE_INVALID; |
| 219 } else if (CFEqual(error, trust_error)) { | 229 } else if (CFEqual(error, trust_error)) { |
| 220 reason |= CERT_STATUS_AUTHORITY_INVALID; | 230 reason |= CERT_STATUS_AUTHORITY_INVALID; |
| 221 } else if (CFEqual(error, weak_error)) { | 231 } else if (CFEqual(error, weak_error)) { |
| 222 reason |= CERT_STATUS_WEAK_KEY; | 232 reason |= CERT_STATUS_WEAK_KEY; |
| 233 } else if (CFEqual(error, hostname_mismatch_error)) { |
| 234 reason |= CERT_STATUS_COMMON_NAME_INVALID; |
| 223 } else { | 235 } else { |
| 224 reason |= CERT_STATUS_INVALID; | 236 reason |= CERT_STATUS_INVALID; |
| 225 } | 237 } |
| 226 } | 238 } |
| 227 | 239 |
| 228 return reason; | 240 return reason; |
| 229 } | 241 } |
| 230 | 242 |
| 231 } // namespace | |
| 232 | |
| 233 CertVerifyProcIOS::CertVerifyProcIOS() {} | |
| 234 | |
| 235 CertVerifyProcIOS::~CertVerifyProcIOS() {} | |
| 236 | |
| 237 bool CertVerifyProcIOS::SupportsAdditionalTrustAnchors() const { | 243 bool CertVerifyProcIOS::SupportsAdditionalTrustAnchors() const { |
| 238 return false; | 244 return false; |
| 239 } | 245 } |
| 240 | 246 |
| 241 bool CertVerifyProcIOS::SupportsOCSPStapling() const { | 247 bool CertVerifyProcIOS::SupportsOCSPStapling() const { |
| 242 return false; | 248 return false; |
| 243 } | 249 } |
| 244 | 250 |
| 251 CertVerifyProcIOS::~CertVerifyProcIOS() = default; |
| 252 |
| 245 int CertVerifyProcIOS::VerifyInternal( | 253 int CertVerifyProcIOS::VerifyInternal( |
| 246 X509Certificate* cert, | 254 X509Certificate* cert, |
| 247 const std::string& hostname, | 255 const std::string& hostname, |
| 248 const std::string& ocsp_response, | 256 const std::string& ocsp_response, |
| 249 int flags, | 257 int flags, |
| 250 CRLSet* crl_set, | 258 CRLSet* crl_set, |
| 251 const CertificateList& additional_trust_anchors, | 259 const CertificateList& additional_trust_anchors, |
| 252 CertVerifyResult* verify_result) { | 260 CertVerifyResult* verify_result) { |
| 253 ScopedCFTypeRef<CFArrayRef> trust_policies; | 261 ScopedCFTypeRef<CFArrayRef> trust_policies; |
| 254 OSStatus status = CreateTrustPolicies(&trust_policies); | 262 OSStatus status = CreateTrustPolicies(&trust_policies); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 271 | 279 |
| 272 // TODO(sleevi): Support CRLSet revocation. | 280 // TODO(sleevi): Support CRLSet revocation. |
| 273 switch (trust_result) { | 281 switch (trust_result) { |
| 274 case kSecTrustResultUnspecified: | 282 case kSecTrustResultUnspecified: |
| 275 case kSecTrustResultProceed: | 283 case kSecTrustResultProceed: |
| 276 break; | 284 break; |
| 277 case kSecTrustResultDeny: | 285 case kSecTrustResultDeny: |
| 278 verify_result->cert_status |= CERT_STATUS_AUTHORITY_INVALID; | 286 verify_result->cert_status |= CERT_STATUS_AUTHORITY_INVALID; |
| 279 break; | 287 break; |
| 280 default: | 288 default: |
| 281 ScopedCFTypeRef<CFArrayRef> properties(SecTrustCopyProperties(trust_ref)); | 289 verify_result->cert_status |= GetCertFailureStatusFromTrust(trust_ref); |
| 282 verify_result->cert_status |= GetFailureFromTrustProperties(properties); | |
| 283 } | 290 } |
| 284 | 291 |
| 285 GetCertChainInfo(final_chain, verify_result); | 292 GetCertChainInfo(final_chain, verify_result); |
| 286 | 293 |
| 287 // Perform hostname verification independent of SecTrustEvaluate. | 294 // Perform hostname verification independent of SecTrustEvaluate. |
| 288 if (!verify_result->verified_cert->VerifyNameMatch( | 295 if (!verify_result->verified_cert->VerifyNameMatch( |
| 289 hostname, &verify_result->common_name_fallback_used)) { | 296 hostname, &verify_result->common_name_fallback_used)) { |
| 290 verify_result->cert_status |= CERT_STATUS_COMMON_NAME_INVALID; | 297 verify_result->cert_status |= CERT_STATUS_COMMON_NAME_INVALID; |
| 291 } | 298 } |
| 292 | 299 |
| 293 verify_result->is_issued_by_known_root = false; | 300 verify_result->is_issued_by_known_root = false; |
| 294 | 301 |
| 295 if (IsCertStatusError(verify_result->cert_status)) | 302 if (IsCertStatusError(verify_result->cert_status)) |
| 296 return MapCertStatusToNetError(verify_result->cert_status); | 303 return MapCertStatusToNetError(verify_result->cert_status); |
| 297 | 304 |
| 298 return OK; | 305 return OK; |
| 299 } | 306 } |
| 300 | 307 |
| 301 } // namespace net | 308 } // namespace net |
| OLD | NEW |