Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // 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 | 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.h" | 5 #include "net/cert/cert_verify_proc.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 11 #include "base/sha1.h" | 11 #include "base/sha1.h" |
| 12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 15 #include "crypto/sha2.h" | |
| 15 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 16 #include "net/base/net_util.h" | 17 #include "net/base/net_util.h" |
| 17 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 18 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 18 #include "net/cert/cert_status_flags.h" | 19 #include "net/cert/cert_status_flags.h" |
| 19 #include "net/cert/cert_verifier.h" | 20 #include "net/cert/cert_verifier.h" |
| 20 #include "net/cert/cert_verify_result.h" | 21 #include "net/cert/cert_verify_result.h" |
| 21 #include "net/cert/crl_set.h" | 22 #include "net/cert/crl_set.h" |
| 22 #include "net/cert/x509_certificate.h" | 23 #include "net/cert/x509_certificate.h" |
| 23 #include "url/url_canon.h" | 24 #include "url/url_canon.h" |
| 24 | 25 |
| 25 #if defined(USE_NSS) || defined(OS_IOS) | 26 #if defined(USE_NSS) || defined(OS_IOS) |
| 26 #include "net/cert/cert_verify_proc_nss.h" | 27 #include "net/cert/cert_verify_proc_nss.h" |
| 27 #elif defined(USE_OPENSSL_CERTS) && !defined(OS_ANDROID) | 28 #elif defined(USE_OPENSSL_CERTS) && !defined(OS_ANDROID) |
| 28 #include "net/cert/cert_verify_proc_openssl.h" | 29 #include "net/cert/cert_verify_proc_openssl.h" |
| 29 #elif defined(OS_ANDROID) | 30 #elif defined(OS_ANDROID) |
| 30 #include "net/cert/cert_verify_proc_android.h" | 31 #include "net/cert/cert_verify_proc_android.h" |
| 31 #elif defined(OS_MACOSX) | 32 #elif defined(OS_MACOSX) |
| 32 #include "net/cert/cert_verify_proc_mac.h" | 33 #include "net/cert/cert_verify_proc_mac.h" |
| 33 #elif defined(OS_WIN) | 34 #elif defined(OS_WIN) |
| 34 #include "net/cert/cert_verify_proc_win.h" | 35 #include "net/cert/cert_verify_proc_win.h" |
| 35 #else | 36 #else |
| 36 #error Implement certificate verification. | 37 #error Implement certificate verification. |
| 37 #endif | 38 #endif |
| 38 | 39 |
| 39 namespace net { | 40 namespace net { |
| 40 | 41 |
| 41 namespace { | 42 namespace { |
| 43 #include "net/cert/cert_verify_proc_whitelist-inc.cc" | |
|
davidben
2015/03/30 21:59:14
Maybe cert_verify_proc_whitelist.h or cert_verify_
Ryan Sleevi
2015/03/30 22:27:05
net_error_list makes some sense because it's inclu
| |
| 42 | 44 |
| 43 // Constants used to build histogram names | 45 // Constants used to build histogram names |
| 44 const char kLeafCert[] = "Leaf"; | 46 const char kLeafCert[] = "Leaf"; |
| 45 const char kIntermediateCert[] = "Intermediate"; | 47 const char kIntermediateCert[] = "Intermediate"; |
| 46 const char kRootCert[] = "Root"; | 48 const char kRootCert[] = "Root"; |
| 47 // Matches the order of X509Certificate::PublicKeyType | 49 // Matches the order of X509Certificate::PublicKeyType |
| 48 const char* const kCertTypeStrings[] = { | 50 const char* const kCertTypeStrings[] = { |
| 49 "Unknown", | 51 "Unknown", |
| 50 "RSA", | 52 "RSA", |
| 51 "DSA", | 53 "DSA", |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 157 size_bits, | 159 size_bits, |
| 158 type); | 160 type); |
| 159 } | 161 } |
| 160 if (!weak_key && IsWeakKey(type, size_bits)) | 162 if (!weak_key && IsWeakKey(type, size_bits)) |
| 161 weak_key = true; | 163 weak_key = true; |
| 162 } | 164 } |
| 163 | 165 |
| 164 return weak_key; | 166 return weak_key; |
| 165 } | 167 } |
| 166 | 168 |
| 169 int CompareHashValueToRawHash(const void* key, const void* element) { | |
| 170 const SHA256HashValue* search_key = | |
| 171 reinterpret_cast<const SHA256HashValue*>(key); | |
| 172 return memcmp(search_key->data, element, sizeof(search_key->data)); | |
| 173 } | |
| 174 | |
| 167 } // namespace | 175 } // namespace |
| 168 | 176 |
| 169 // static | 177 // static |
| 170 CertVerifyProc* CertVerifyProc::CreateDefault() { | 178 CertVerifyProc* CertVerifyProc::CreateDefault() { |
| 171 #if defined(USE_NSS) || defined(OS_IOS) | 179 #if defined(USE_NSS) || defined(OS_IOS) |
| 172 return new CertVerifyProcNSS(); | 180 return new CertVerifyProcNSS(); |
| 173 #elif defined(USE_OPENSSL_CERTS) && !defined(OS_ANDROID) | 181 #elif defined(USE_OPENSSL_CERTS) && !defined(OS_ANDROID) |
| 174 return new CertVerifyProcOpenSSL(); | 182 return new CertVerifyProcOpenSSL(); |
| 175 #elif defined(OS_ANDROID) | 183 #elif defined(OS_ANDROID) |
| 176 return new CertVerifyProcAndroid(); | 184 return new CertVerifyProcAndroid(); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 228 std::vector<std::string> dns_names, ip_addrs; | 236 std::vector<std::string> dns_names, ip_addrs; |
| 229 cert->GetSubjectAltName(&dns_names, &ip_addrs); | 237 cert->GetSubjectAltName(&dns_names, &ip_addrs); |
| 230 if (HasNameConstraintsViolation(verify_result->public_key_hashes, | 238 if (HasNameConstraintsViolation(verify_result->public_key_hashes, |
| 231 cert->subject().common_name, | 239 cert->subject().common_name, |
| 232 dns_names, | 240 dns_names, |
| 233 ip_addrs)) { | 241 ip_addrs)) { |
| 234 verify_result->cert_status |= CERT_STATUS_NAME_CONSTRAINT_VIOLATION; | 242 verify_result->cert_status |= CERT_STATUS_NAME_CONSTRAINT_VIOLATION; |
| 235 rv = MapCertStatusToNetError(verify_result->cert_status); | 243 rv = MapCertStatusToNetError(verify_result->cert_status); |
| 236 } | 244 } |
| 237 | 245 |
| 246 if (IsNonWhitelistedCert(*verify_result->verified_cert, | |
| 247 verify_result->public_key_hashes)) { | |
| 248 verify_result->cert_status |= CERT_STATUS_AUTHORITY_INVALID; | |
| 249 rv = MapCertStatusToNetError(verify_result->cert_status); | |
| 250 } | |
|
davidben
2015/03/30 21:59:14
Just to confirm this is fine: were the underlying
Ryan Sleevi
2015/03/30 22:27:05
Yup
| |
| 251 | |
| 238 // Check for weak keys in the entire verified chain. | 252 // Check for weak keys in the entire verified chain. |
| 239 bool weak_key = ExaminePublicKeys(verify_result->verified_cert, | 253 bool weak_key = ExaminePublicKeys(verify_result->verified_cert, |
| 240 verify_result->is_issued_by_known_root); | 254 verify_result->is_issued_by_known_root); |
| 241 | 255 |
| 242 if (weak_key) { | 256 if (weak_key) { |
| 243 verify_result->cert_status |= CERT_STATUS_WEAK_KEY; | 257 verify_result->cert_status |= CERT_STATUS_WEAK_KEY; |
| 244 // Avoid replacing a more serious error, such as an OS/library failure, | 258 // Avoid replacing a more serious error, such as an OS/library failure, |
| 245 // by ensuring that if verification failed, it failed with a certificate | 259 // by ensuring that if verification failed, it failed with a certificate |
| 246 // error. | 260 // error. |
| 247 if (rv == OK || IsCertificateError(rv)) | 261 if (rv == OK || IsCertificateError(rv)) |
| (...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 662 if (start >= time_2012_07_01 && month_diff > 60) | 676 if (start >= time_2012_07_01 && month_diff > 60) |
| 663 return true; | 677 return true; |
| 664 | 678 |
| 665 // For certificates issued after 1 April 2015: 39 months. | 679 // For certificates issued after 1 April 2015: 39 months. |
| 666 if (start >= time_2015_04_01 && month_diff > 39) | 680 if (start >= time_2015_04_01 && month_diff > 39) |
| 667 return true; | 681 return true; |
| 668 | 682 |
| 669 return false; | 683 return false; |
| 670 } | 684 } |
| 671 | 685 |
| 686 // static | |
| 687 bool CertVerifyProc::IsNonWhitelistedCert(const X509Certificate& cert, | |
| 688 const HashValueVector& hashes) { | |
| 689 for (size_t i = 0; i < arraysize(kWhitelistedIssuers); ++i) { | |
| 690 for (const auto& hash : hashes) { | |
| 691 if (hash.tag == HASH_VALUE_SHA256 && | |
| 692 memcmp(hash.data(), kWhitelistedIssuers[i].public_key, | |
| 693 crypto::kSHA256Length) == 0) { | |
| 694 const SHA256HashValue leaf_hash = | |
| 695 X509Certificate::CalculateFingerprint256(cert.os_cert_handle()); | |
| 696 void* result = | |
| 697 bsearch(&leaf_hash, kWhitelistedIssuers[i].whitelist, | |
|
davidben
2015/03/30 21:59:14
Optional: std::lower_bound might save you some cas
Ryan Sleevi
2015/03/30 22:27:05
Adds an extra compare and the extra check. Also en
| |
| 698 kWhitelistedIssuers[i].whitelist_size, | |
| 699 sizeof(leaf_hash.data), CompareHashValueToRawHash); | |
|
davidben
2015/03/30 21:59:14
Nit: Probably clearer to do sizeof(leaf_data.data)
Ryan Sleevi
2015/03/30 22:27:05
I don't believe that's guaranteed to optimize the
davidben
2015/03/30 22:29:22
Oh, I just meant if you were to switch to a differ
| |
| 700 if (result == nullptr) | |
| 701 return true; | |
| 702 return false; | |
| 703 } | |
| 704 } | |
| 705 } | |
| 706 | |
| 707 return false; | |
| 708 } | |
| 709 | |
| 672 } // namespace net | 710 } // namespace net |
| OLD | NEW |