[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 5 | #include "net/cert/cert_verify_proc.h" |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 6 | |
| 7 | #include <vector> |
| 8 | |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 9 | #include "base/files/file_path.h" |
[email protected] | ef15512 | 2013-03-23 19:11:24 | [diff] [blame] | 10 | #include "base/logging.h" |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 11 | #include "base/sha1.h" |
[email protected] | 4b35521 | 2013-06-11 10:35:19 | [diff] [blame^] | 12 | #include "base/strings/string_number_conversions.h" |
[email protected] | bfea55d | 2013-04-04 11:06:04 | [diff] [blame] | 13 | #include "crypto/sha2.h" |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 14 | #include "net/base/net_errors.h" |
[email protected] | 42fdb45 | 2012-11-01 12:44:40 | [diff] [blame] | 15 | #include "net/base/test_data_directory.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 16 | #include "net/cert/asn1_util.h" |
| 17 | #include "net/cert/cert_status_flags.h" |
| 18 | #include "net/cert/cert_verifier.h" |
| 19 | #include "net/cert/cert_verify_result.h" |
| 20 | #include "net/cert/crl_set.h" |
| 21 | #include "net/cert/test_root_certs.h" |
| 22 | #include "net/cert/x509_certificate.h" |
| 23 | #include "net/test/cert_test_util.h" |
| 24 | #include "net/test/test_certificate_data.h" |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 25 | #include "testing/gtest/include/gtest/gtest.h" |
| 26 | |
| 27 | #if defined(OS_WIN) |
| 28 | #include "base/win/windows_version.h" |
[email protected] | 12b3c882 | 2012-09-26 11:51:18 | [diff] [blame] | 29 | #elif defined(OS_MACOSX) && !defined(OS_IOS) |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 30 | #include "base/mac/mac_util.h" |
| 31 | #endif |
| 32 | |
| 33 | using base::HexEncode; |
| 34 | |
| 35 | namespace net { |
| 36 | |
| 37 | namespace { |
| 38 | |
| 39 | // A certificate for www.paypal.com with a NULL byte in the common name. |
| 40 | // From http://www.gossamer-threads.com/lists/fulldisc/full-disclosure/70363 |
| 41 | unsigned char paypal_null_fingerprint[] = { |
| 42 | 0x4c, 0x88, 0x9e, 0x28, 0xd7, 0x7a, 0x44, 0x1e, 0x13, 0xf2, 0x6a, 0xba, |
| 43 | 0x1f, 0xe8, 0x1b, 0xd6, 0xab, 0x7b, 0xe8, 0xd7 |
| 44 | }; |
| 45 | |
[email protected] | ff35321 | 2013-05-17 02:09:08 | [diff] [blame] | 46 | // Mock CertVerifyProc that will set |verify_result->is_issued_by_known_root| |
| 47 | // for all certificates that are Verified. |
| 48 | class WellKnownCaCertVerifyProc : public CertVerifyProc { |
| 49 | public: |
| 50 | // Initialize a CertVerifyProc that will set |
| 51 | // |verify_result->is_issued_by_known_root| to |is_well_known|. |
| 52 | explicit WellKnownCaCertVerifyProc(bool is_well_known) |
| 53 | : is_well_known_(is_well_known) {} |
| 54 | |
| 55 | // CertVerifyProc implementation: |
| 56 | virtual bool SupportsAdditionalTrustAnchors() const OVERRIDE { return false; } |
| 57 | |
| 58 | protected: |
| 59 | virtual ~WellKnownCaCertVerifyProc() {} |
| 60 | |
| 61 | private: |
| 62 | virtual int VerifyInternal(X509Certificate* cert, |
| 63 | const std::string& hostname, |
| 64 | int flags, |
| 65 | CRLSet* crl_set, |
| 66 | const CertificateList& additional_trust_anchors, |
| 67 | CertVerifyResult* verify_result) OVERRIDE; |
| 68 | |
| 69 | const bool is_well_known_; |
| 70 | |
| 71 | DISALLOW_COPY_AND_ASSIGN(WellKnownCaCertVerifyProc); |
| 72 | }; |
| 73 | |
| 74 | int WellKnownCaCertVerifyProc::VerifyInternal( |
| 75 | X509Certificate* cert, |
| 76 | const std::string& hostname, |
| 77 | int flags, |
| 78 | CRLSet* crl_set, |
| 79 | const CertificateList& additional_trust_anchors, |
| 80 | CertVerifyResult* verify_result) { |
| 81 | verify_result->is_issued_by_known_root = is_well_known_; |
| 82 | return OK; |
| 83 | } |
| 84 | |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 85 | } // namespace |
| 86 | |
| 87 | class CertVerifyProcTest : public testing::Test { |
| 88 | public: |
| 89 | CertVerifyProcTest() |
| 90 | : verify_proc_(CertVerifyProc::CreateDefault()) { |
| 91 | } |
| 92 | virtual ~CertVerifyProcTest() {} |
| 93 | |
| 94 | protected: |
[email protected] | ef15512 | 2013-03-23 19:11:24 | [diff] [blame] | 95 | bool SupportsAdditionalTrustAnchors() { |
| 96 | return verify_proc_->SupportsAdditionalTrustAnchors(); |
| 97 | } |
| 98 | |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 99 | int Verify(X509Certificate* cert, |
| 100 | const std::string& hostname, |
| 101 | int flags, |
| 102 | CRLSet* crl_set, |
[email protected] | ef15512 | 2013-03-23 19:11:24 | [diff] [blame] | 103 | const CertificateList& additional_trust_anchors, |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 104 | CertVerifyResult* verify_result) { |
| 105 | return verify_proc_->Verify(cert, hostname, flags, crl_set, |
[email protected] | ef15512 | 2013-03-23 19:11:24 | [diff] [blame] | 106 | additional_trust_anchors, verify_result); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 107 | } |
| 108 | |
[email protected] | ef15512 | 2013-03-23 19:11:24 | [diff] [blame] | 109 | const CertificateList empty_cert_list_; |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 110 | scoped_refptr<CertVerifyProc> verify_proc_; |
| 111 | }; |
| 112 | |
| 113 | TEST_F(CertVerifyProcTest, WithoutRevocationChecking) { |
| 114 | // Check that verification without revocation checking works. |
| 115 | CertificateList certs = CreateCertificateListFromFile( |
| 116 | GetTestCertsDirectory(), |
| 117 | "googlenew.chain.pem", |
| 118 | X509Certificate::FORMAT_PEM_CERT_SEQUENCE); |
| 119 | |
| 120 | X509Certificate::OSCertHandles intermediates; |
| 121 | intermediates.push_back(certs[1]->os_cert_handle()); |
| 122 | |
| 123 | scoped_refptr<X509Certificate> google_full_chain = |
| 124 | X509Certificate::CreateFromHandle(certs[0]->os_cert_handle(), |
| 125 | intermediates); |
| 126 | |
| 127 | CertVerifyResult verify_result; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 128 | EXPECT_EQ(OK, |
| 129 | Verify(google_full_chain.get(), |
| 130 | "www.google.com", |
| 131 | 0 /* flags */, |
| 132 | NULL, |
| 133 | empty_cert_list_, |
| 134 | &verify_result)); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | #if defined(OS_ANDROID) || defined(USE_OPENSSL) |
| 138 | // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported. |
| 139 | #define MAYBE_EVVerification DISABLED_EVVerification |
| 140 | #else |
| 141 | #define MAYBE_EVVerification EVVerification |
| 142 | #endif |
| 143 | TEST_F(CertVerifyProcTest, MAYBE_EVVerification) { |
| 144 | // This certificate will expire Jun 21, 2013. |
| 145 | CertificateList certs = CreateCertificateListFromFile( |
| 146 | GetTestCertsDirectory(), |
| 147 | "comodo.chain.pem", |
| 148 | X509Certificate::FORMAT_PEM_CERT_SEQUENCE); |
| 149 | ASSERT_EQ(3U, certs.size()); |
| 150 | |
| 151 | X509Certificate::OSCertHandles intermediates; |
| 152 | intermediates.push_back(certs[1]->os_cert_handle()); |
| 153 | intermediates.push_back(certs[2]->os_cert_handle()); |
| 154 | |
| 155 | scoped_refptr<X509Certificate> comodo_chain = |
| 156 | X509Certificate::CreateFromHandle(certs[0]->os_cert_handle(), |
| 157 | intermediates); |
| 158 | |
| 159 | scoped_refptr<CRLSet> crl_set(CRLSet::EmptyCRLSetForTesting()); |
| 160 | CertVerifyResult verify_result; |
[email protected] | 8738e0d7 | 2012-08-23 02:00:47 | [diff] [blame] | 161 | int flags = CertVerifier::VERIFY_EV_CERT; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 162 | int error = Verify(comodo_chain.get(), |
| 163 | "comodo.com", |
| 164 | flags, |
| 165 | crl_set.get(), |
| 166 | empty_cert_list_, |
| 167 | &verify_result); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 168 | EXPECT_EQ(OK, error); |
| 169 | EXPECT_TRUE(verify_result.cert_status & CERT_STATUS_IS_EV); |
| 170 | } |
| 171 | |
| 172 | TEST_F(CertVerifyProcTest, PaypalNullCertParsing) { |
| 173 | scoped_refptr<X509Certificate> paypal_null_cert( |
| 174 | X509Certificate::CreateFromBytes( |
| 175 | reinterpret_cast<const char*>(paypal_null_der), |
| 176 | sizeof(paypal_null_der))); |
| 177 | |
| 178 | ASSERT_NE(static_cast<X509Certificate*>(NULL), paypal_null_cert); |
| 179 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 180 | const SHA1HashValue& fingerprint = |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 181 | paypal_null_cert->fingerprint(); |
| 182 | for (size_t i = 0; i < 20; ++i) |
| 183 | EXPECT_EQ(paypal_null_fingerprint[i], fingerprint.data[i]); |
| 184 | |
| 185 | int flags = 0; |
| 186 | CertVerifyResult verify_result; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 187 | int error = Verify(paypal_null_cert.get(), |
| 188 | "www.paypal.com", |
| 189 | flags, |
| 190 | NULL, |
| 191 | empty_cert_list_, |
| 192 | &verify_result); |
[email protected] | 71f4b27 | 2013-02-13 19:13:49 | [diff] [blame] | 193 | #if defined(USE_NSS) || defined(OS_IOS) || defined(OS_ANDROID) |
[email protected] | 317dbc7 | 2012-04-03 18:45:14 | [diff] [blame] | 194 | EXPECT_EQ(ERR_CERT_COMMON_NAME_INVALID, error); |
| 195 | #else |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 196 | // TOOD(bulach): investigate why macosx and win aren't returning |
| 197 | // ERR_CERT_INVALID or ERR_CERT_COMMON_NAME_INVALID. |
| 198 | EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, error); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 199 | #endif |
| 200 | // Either the system crypto library should correctly report a certificate |
| 201 | // name mismatch, or our certificate blacklist should cause us to report an |
| 202 | // invalid certificate. |
[email protected] | 12b3c882 | 2012-09-26 11:51:18 | [diff] [blame] | 203 | #if defined(USE_NSS) || defined(OS_WIN) || defined(OS_IOS) |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 204 | EXPECT_TRUE(verify_result.cert_status & |
| 205 | (CERT_STATUS_COMMON_NAME_INVALID | CERT_STATUS_INVALID)); |
| 206 | #endif |
| 207 | } |
| 208 | |
| 209 | // A regression test for http://crbug.com/31497. |
| 210 | // This certificate will expire on 2012-04-08. The test will still |
| 211 | // pass if error == ERR_CERT_DATE_INVALID. TODO(wtc): generate test |
| 212 | // certificates for this unit test. http://crbug.com/111742 |
| 213 | TEST_F(CertVerifyProcTest, IntermediateCARequireExplicitPolicy) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 214 | base::FilePath certs_dir = GetTestCertsDirectory(); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 215 | |
| 216 | scoped_refptr<X509Certificate> server_cert = |
| 217 | ImportCertFromFile(certs_dir, "www_us_army_mil_cert.der"); |
| 218 | ASSERT_NE(static_cast<X509Certificate*>(NULL), server_cert); |
| 219 | |
| 220 | // The intermediate CA certificate's policyConstraints extension has a |
| 221 | // requireExplicitPolicy field with SkipCerts=0. |
| 222 | scoped_refptr<X509Certificate> intermediate_cert = |
| 223 | ImportCertFromFile(certs_dir, "dod_ca_17_cert.der"); |
| 224 | ASSERT_NE(static_cast<X509Certificate*>(NULL), intermediate_cert); |
| 225 | |
| 226 | scoped_refptr<X509Certificate> root_cert = |
| 227 | ImportCertFromFile(certs_dir, "dod_root_ca_2_cert.der"); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 228 | ScopedTestRoot scoped_root(root_cert.get()); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 229 | |
| 230 | X509Certificate::OSCertHandles intermediates; |
| 231 | intermediates.push_back(intermediate_cert->os_cert_handle()); |
| 232 | scoped_refptr<X509Certificate> cert_chain = |
| 233 | X509Certificate::CreateFromHandle(server_cert->os_cert_handle(), |
| 234 | intermediates); |
| 235 | |
| 236 | int flags = 0; |
| 237 | CertVerifyResult verify_result; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 238 | int error = Verify(cert_chain.get(), |
| 239 | "www.us.army.mil", |
| 240 | flags, |
| 241 | NULL, |
| 242 | empty_cert_list_, |
| 243 | &verify_result); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 244 | if (error == OK) { |
| 245 | EXPECT_EQ(0U, verify_result.cert_status); |
| 246 | } else { |
| 247 | EXPECT_EQ(ERR_CERT_DATE_INVALID, error); |
| 248 | EXPECT_EQ(CERT_STATUS_DATE_INVALID, verify_result.cert_status); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | |
| 253 | // Test for bug 58437. |
| 254 | // This certificate will expire on 2011-12-21. The test will still |
| 255 | // pass if error == ERR_CERT_DATE_INVALID. |
| 256 | // This test is DISABLED because it appears that we cannot do |
| 257 | // certificate revocation checking when running all of the net unit tests. |
| 258 | // This test passes when run individually, but when run with all of the net |
| 259 | // unit tests, the call to PKIXVerifyCert returns the NSS error -8180, which is |
| 260 | // SEC_ERROR_REVOKED_CERTIFICATE. This indicates a lack of revocation |
| 261 | // status, i.e. that the revocation check is failing for some reason. |
| 262 | TEST_F(CertVerifyProcTest, DISABLED_GlobalSignR3EVTest) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 263 | base::FilePath certs_dir = GetTestCertsDirectory(); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 264 | |
| 265 | scoped_refptr<X509Certificate> server_cert = |
| 266 | ImportCertFromFile(certs_dir, "2029_globalsign_com_cert.pem"); |
| 267 | ASSERT_NE(static_cast<X509Certificate*>(NULL), server_cert); |
| 268 | |
| 269 | scoped_refptr<X509Certificate> intermediate_cert = |
| 270 | ImportCertFromFile(certs_dir, "globalsign_ev_sha256_ca_cert.pem"); |
| 271 | ASSERT_NE(static_cast<X509Certificate*>(NULL), intermediate_cert); |
| 272 | |
| 273 | X509Certificate::OSCertHandles intermediates; |
| 274 | intermediates.push_back(intermediate_cert->os_cert_handle()); |
| 275 | scoped_refptr<X509Certificate> cert_chain = |
| 276 | X509Certificate::CreateFromHandle(server_cert->os_cert_handle(), |
| 277 | intermediates); |
| 278 | |
| 279 | CertVerifyResult verify_result; |
[email protected] | 8738e0d7 | 2012-08-23 02:00:47 | [diff] [blame] | 280 | int flags = CertVerifier::VERIFY_REV_CHECKING_ENABLED | |
| 281 | CertVerifier::VERIFY_EV_CERT; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 282 | int error = Verify(cert_chain.get(), |
| 283 | "2029.globalsign.com", |
| 284 | flags, |
| 285 | NULL, |
| 286 | empty_cert_list_, |
| 287 | &verify_result); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 288 | if (error == OK) |
| 289 | EXPECT_TRUE(verify_result.cert_status & CERT_STATUS_IS_EV); |
| 290 | else |
| 291 | EXPECT_EQ(ERR_CERT_DATE_INVALID, error); |
| 292 | } |
| 293 | |
[email protected] | fe845ab6 | 2012-08-24 16:03:29 | [diff] [blame] | 294 | // Test that verifying an ECDSA certificate doesn't crash on XP. (See |
| 295 | // crbug.com/144466). |
| 296 | TEST_F(CertVerifyProcTest, ECDSA_RSA) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 297 | base::FilePath certs_dir = GetTestCertsDirectory(); |
[email protected] | fe845ab6 | 2012-08-24 16:03:29 | [diff] [blame] | 298 | |
| 299 | scoped_refptr<X509Certificate> cert = |
| 300 | ImportCertFromFile(certs_dir, |
| 301 | "prime256v1-ecdsa-ee-by-1024-rsa-intermediate.pem"); |
| 302 | |
| 303 | CertVerifyResult verify_result; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 304 | Verify(cert.get(), "127.0.0.1", 0, NULL, empty_cert_list_, &verify_result); |
[email protected] | fe845ab6 | 2012-08-24 16:03:29 | [diff] [blame] | 305 | |
| 306 | // We don't check verify_result because the certificate is signed by an |
| 307 | // unknown CA and will be considered invalid on XP because of the ECDSA |
| 308 | // public key. |
| 309 | } |
| 310 | |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 311 | // Currently, only RSA and DSA keys are checked for weakness, and our example |
| 312 | // weak size is 768. These could change in the future. |
| 313 | // |
| 314 | // Note that this means there may be false negatives: keys for other |
| 315 | // algorithms and which are weak will pass this test. |
| 316 | static bool IsWeakKeyType(const std::string& key_type) { |
| 317 | size_t pos = key_type.find("-"); |
| 318 | std::string size = key_type.substr(0, pos); |
| 319 | std::string type = key_type.substr(pos + 1); |
| 320 | |
| 321 | if (type == "rsa" || type == "dsa") |
| 322 | return size == "768"; |
| 323 | |
| 324 | return false; |
| 325 | } |
| 326 | |
| 327 | TEST_F(CertVerifyProcTest, RejectWeakKeys) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 328 | base::FilePath certs_dir = GetTestCertsDirectory(); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 329 | typedef std::vector<std::string> Strings; |
| 330 | Strings key_types; |
| 331 | |
| 332 | // generate-weak-test-chains.sh currently has: |
| 333 | // key_types="768-rsa 1024-rsa 2048-rsa prime256v1-ecdsa" |
| 334 | // We must use the same key types here. The filenames generated look like: |
| 335 | // 2048-rsa-ee-by-768-rsa-intermediate.pem |
| 336 | key_types.push_back("768-rsa"); |
| 337 | key_types.push_back("1024-rsa"); |
| 338 | key_types.push_back("2048-rsa"); |
| 339 | |
| 340 | bool use_ecdsa = true; |
| 341 | #if defined(OS_WIN) |
| 342 | use_ecdsa = base::win::GetVersion() > base::win::VERSION_XP; |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 343 | #endif |
| 344 | |
| 345 | if (use_ecdsa) |
| 346 | key_types.push_back("prime256v1-ecdsa"); |
| 347 | |
| 348 | // Add the root that signed the intermediates for this test. |
| 349 | scoped_refptr<X509Certificate> root_cert = |
| 350 | ImportCertFromFile(certs_dir, "2048-rsa-root.pem"); |
| 351 | ASSERT_NE(static_cast<X509Certificate*>(NULL), root_cert); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 352 | ScopedTestRoot scoped_root(root_cert.get()); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 353 | |
| 354 | // Now test each chain. |
| 355 | for (Strings::const_iterator ee_type = key_types.begin(); |
| 356 | ee_type != key_types.end(); ++ee_type) { |
| 357 | for (Strings::const_iterator signer_type = key_types.begin(); |
| 358 | signer_type != key_types.end(); ++signer_type) { |
| 359 | std::string basename = *ee_type + "-ee-by-" + *signer_type + |
| 360 | "-intermediate.pem"; |
| 361 | SCOPED_TRACE(basename); |
| 362 | scoped_refptr<X509Certificate> ee_cert = |
| 363 | ImportCertFromFile(certs_dir, basename); |
| 364 | ASSERT_NE(static_cast<X509Certificate*>(NULL), ee_cert); |
| 365 | |
| 366 | basename = *signer_type + "-intermediate.pem"; |
| 367 | scoped_refptr<X509Certificate> intermediate = |
| 368 | ImportCertFromFile(certs_dir, basename); |
| 369 | ASSERT_NE(static_cast<X509Certificate*>(NULL), intermediate); |
| 370 | |
| 371 | X509Certificate::OSCertHandles intermediates; |
| 372 | intermediates.push_back(intermediate->os_cert_handle()); |
| 373 | scoped_refptr<X509Certificate> cert_chain = |
| 374 | X509Certificate::CreateFromHandle(ee_cert->os_cert_handle(), |
| 375 | intermediates); |
| 376 | |
| 377 | CertVerifyResult verify_result; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 378 | int error = Verify(cert_chain.get(), |
| 379 | "127.0.0.1", |
| 380 | 0, |
| 381 | NULL, |
| 382 | empty_cert_list_, |
| 383 | &verify_result); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 384 | |
| 385 | if (IsWeakKeyType(*ee_type) || IsWeakKeyType(*signer_type)) { |
| 386 | EXPECT_NE(OK, error); |
| 387 | EXPECT_EQ(CERT_STATUS_WEAK_KEY, |
| 388 | verify_result.cert_status & CERT_STATUS_WEAK_KEY); |
[email protected] | 58484ca | 2012-05-29 21:56:34 | [diff] [blame] | 389 | EXPECT_NE(CERT_STATUS_INVALID, |
| 390 | verify_result.cert_status & CERT_STATUS_INVALID); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 391 | } else { |
| 392 | EXPECT_EQ(OK, error); |
| 393 | EXPECT_EQ(0U, verify_result.cert_status & CERT_STATUS_WEAK_KEY); |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | // Test for bug 108514. |
| 400 | // The certificate will expire on 2012-07-20. The test will still |
| 401 | // pass if error == ERR_CERT_DATE_INVALID. TODO(rsleevi): generate test |
| 402 | // certificates for this unit test. http://crbug.com/111730 |
| 403 | TEST_F(CertVerifyProcTest, ExtraneousMD5RootCert) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 404 | base::FilePath certs_dir = GetTestCertsDirectory(); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 405 | |
| 406 | scoped_refptr<X509Certificate> server_cert = |
| 407 | ImportCertFromFile(certs_dir, "images_etrade_wallst_com.pem"); |
| 408 | ASSERT_NE(static_cast<X509Certificate*>(NULL), server_cert); |
| 409 | |
| 410 | scoped_refptr<X509Certificate> intermediate_cert = |
| 411 | ImportCertFromFile(certs_dir, "globalsign_orgv1_ca.pem"); |
| 412 | ASSERT_NE(static_cast<X509Certificate*>(NULL), intermediate_cert); |
| 413 | |
| 414 | scoped_refptr<X509Certificate> md5_root_cert = |
| 415 | ImportCertFromFile(certs_dir, "globalsign_root_ca_md5.pem"); |
| 416 | ASSERT_NE(static_cast<X509Certificate*>(NULL), md5_root_cert); |
| 417 | |
| 418 | X509Certificate::OSCertHandles intermediates; |
| 419 | intermediates.push_back(intermediate_cert->os_cert_handle()); |
| 420 | intermediates.push_back(md5_root_cert->os_cert_handle()); |
| 421 | scoped_refptr<X509Certificate> cert_chain = |
| 422 | X509Certificate::CreateFromHandle(server_cert->os_cert_handle(), |
| 423 | intermediates); |
| 424 | |
| 425 | CertVerifyResult verify_result; |
| 426 | int flags = 0; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 427 | int error = Verify(cert_chain.get(), |
| 428 | "images.etrade.wallst.com", |
| 429 | flags, |
| 430 | NULL, |
| 431 | empty_cert_list_, |
| 432 | &verify_result); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 433 | if (error != OK) |
| 434 | EXPECT_EQ(ERR_CERT_DATE_INVALID, error); |
| 435 | |
| 436 | EXPECT_FALSE(verify_result.has_md5); |
| 437 | EXPECT_FALSE(verify_result.has_md5_ca); |
| 438 | } |
| 439 | |
| 440 | // Test for bug 94673. |
| 441 | TEST_F(CertVerifyProcTest, GoogleDigiNotarTest) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 442 | base::FilePath certs_dir = GetTestCertsDirectory(); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 443 | |
| 444 | scoped_refptr<X509Certificate> server_cert = |
| 445 | ImportCertFromFile(certs_dir, "google_diginotar.pem"); |
| 446 | ASSERT_NE(static_cast<X509Certificate*>(NULL), server_cert); |
| 447 | |
| 448 | scoped_refptr<X509Certificate> intermediate_cert = |
| 449 | ImportCertFromFile(certs_dir, "diginotar_public_ca_2025.pem"); |
| 450 | ASSERT_NE(static_cast<X509Certificate*>(NULL), intermediate_cert); |
| 451 | |
| 452 | X509Certificate::OSCertHandles intermediates; |
| 453 | intermediates.push_back(intermediate_cert->os_cert_handle()); |
| 454 | scoped_refptr<X509Certificate> cert_chain = |
| 455 | X509Certificate::CreateFromHandle(server_cert->os_cert_handle(), |
| 456 | intermediates); |
| 457 | |
| 458 | CertVerifyResult verify_result; |
[email protected] | 8738e0d7 | 2012-08-23 02:00:47 | [diff] [blame] | 459 | int flags = CertVerifier::VERIFY_REV_CHECKING_ENABLED; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 460 | int error = Verify(cert_chain.get(), |
| 461 | "mail.google.com", |
| 462 | flags, |
| 463 | NULL, |
| 464 | empty_cert_list_, |
| 465 | &verify_result); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 466 | EXPECT_NE(OK, error); |
| 467 | |
| 468 | // Now turn off revocation checking. Certificate verification should still |
| 469 | // fail. |
| 470 | flags = 0; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 471 | error = Verify(cert_chain.get(), |
| 472 | "mail.google.com", |
| 473 | flags, |
| 474 | NULL, |
| 475 | empty_cert_list_, |
| 476 | &verify_result); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 477 | EXPECT_NE(OK, error); |
| 478 | } |
| 479 | |
[email protected] | 62b23c2 | 2012-03-22 04:50:24 | [diff] [blame] | 480 | TEST_F(CertVerifyProcTest, DigiNotarCerts) { |
| 481 | static const char* const kDigiNotarFilenames[] = { |
| 482 | "diginotar_root_ca.pem", |
| 483 | "diginotar_cyber_ca.pem", |
| 484 | "diginotar_services_1024_ca.pem", |
| 485 | "diginotar_pkioverheid.pem", |
| 486 | "diginotar_pkioverheid_g2.pem", |
| 487 | NULL, |
| 488 | }; |
| 489 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 490 | base::FilePath certs_dir = GetTestCertsDirectory(); |
[email protected] | 62b23c2 | 2012-03-22 04:50:24 | [diff] [blame] | 491 | |
| 492 | for (size_t i = 0; kDigiNotarFilenames[i]; i++) { |
| 493 | scoped_refptr<X509Certificate> diginotar_cert = |
| 494 | ImportCertFromFile(certs_dir, kDigiNotarFilenames[i]); |
| 495 | std::string der_bytes; |
| 496 | ASSERT_TRUE(X509Certificate::GetDEREncoded( |
| 497 | diginotar_cert->os_cert_handle(), &der_bytes)); |
| 498 | |
| 499 | base::StringPiece spki; |
| 500 | ASSERT_TRUE(asn1::ExtractSPKIFromDERCert(der_bytes, &spki)); |
| 501 | |
| 502 | std::string spki_sha1 = base::SHA1HashString(spki.as_string()); |
| 503 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 504 | HashValueVector public_keys; |
| 505 | HashValue hash(HASH_VALUE_SHA1); |
| 506 | ASSERT_EQ(hash.size(), spki_sha1.size()); |
| 507 | memcpy(hash.data(), spki_sha1.data(), spki_sha1.size()); |
| 508 | public_keys.push_back(hash); |
[email protected] | 62b23c2 | 2012-03-22 04:50:24 | [diff] [blame] | 509 | |
| 510 | EXPECT_TRUE(CertVerifyProc::IsPublicKeyBlacklisted(public_keys)) << |
| 511 | "Public key not blocked for " << kDigiNotarFilenames[i]; |
| 512 | } |
| 513 | } |
| 514 | |
[email protected] | a6b4ee04 | 2012-07-24 20:52:46 | [diff] [blame] | 515 | TEST_F(CertVerifyProcTest, TestKnownRoot) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 516 | base::FilePath certs_dir = GetTestCertsDirectory(); |
[email protected] | a6b4ee04 | 2012-07-24 20:52:46 | [diff] [blame] | 517 | CertificateList certs = CreateCertificateListFromFile( |
| 518 | certs_dir, "certse.pem", X509Certificate::FORMAT_AUTO); |
| 519 | ASSERT_EQ(3U, certs.size()); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 520 | |
| 521 | X509Certificate::OSCertHandles intermediates; |
[email protected] | a6b4ee04 | 2012-07-24 20:52:46 | [diff] [blame] | 522 | intermediates.push_back(certs[1]->os_cert_handle()); |
| 523 | intermediates.push_back(certs[2]->os_cert_handle()); |
| 524 | |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 525 | scoped_refptr<X509Certificate> cert_chain = |
[email protected] | a6b4ee04 | 2012-07-24 20:52:46 | [diff] [blame] | 526 | X509Certificate::CreateFromHandle(certs[0]->os_cert_handle(), |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 527 | intermediates); |
| 528 | |
| 529 | int flags = 0; |
| 530 | CertVerifyResult verify_result; |
[email protected] | a6b4ee04 | 2012-07-24 20:52:46 | [diff] [blame] | 531 | // This will blow up, June 8th, 2014. Sorry! Please disable and file a bug |
| 532 | // against agl. See also PublicKeyHashes. |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 533 | int error = Verify(cert_chain.get(), |
| 534 | "cert.se", |
| 535 | flags, |
| 536 | NULL, |
| 537 | empty_cert_list_, |
| 538 | &verify_result); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 539 | EXPECT_EQ(OK, error); |
| 540 | EXPECT_EQ(0U, verify_result.cert_status); |
| 541 | EXPECT_TRUE(verify_result.is_issued_by_known_root); |
| 542 | } |
| 543 | |
[email protected] | a6b4ee04 | 2012-07-24 20:52:46 | [diff] [blame] | 544 | TEST_F(CertVerifyProcTest, PublicKeyHashes) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 545 | base::FilePath certs_dir = GetTestCertsDirectory(); |
[email protected] | a6b4ee04 | 2012-07-24 20:52:46 | [diff] [blame] | 546 | CertificateList certs = CreateCertificateListFromFile( |
| 547 | certs_dir, "certse.pem", X509Certificate::FORMAT_AUTO); |
| 548 | ASSERT_EQ(3U, certs.size()); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 549 | |
| 550 | X509Certificate::OSCertHandles intermediates; |
[email protected] | a6b4ee04 | 2012-07-24 20:52:46 | [diff] [blame] | 551 | intermediates.push_back(certs[1]->os_cert_handle()); |
| 552 | intermediates.push_back(certs[2]->os_cert_handle()); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 553 | |
[email protected] | a6b4ee04 | 2012-07-24 20:52:46 | [diff] [blame] | 554 | scoped_refptr<X509Certificate> cert_chain = |
| 555 | X509Certificate::CreateFromHandle(certs[0]->os_cert_handle(), |
| 556 | intermediates); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 557 | int flags = 0; |
| 558 | CertVerifyResult verify_result; |
| 559 | |
[email protected] | a6b4ee04 | 2012-07-24 20:52:46 | [diff] [blame] | 560 | // This will blow up, June 8th, 2014. Sorry! Please disable and file a bug |
| 561 | // against agl. See also TestKnownRoot. |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 562 | int error = Verify(cert_chain.get(), |
| 563 | "cert.se", |
| 564 | flags, |
| 565 | NULL, |
| 566 | empty_cert_list_, |
| 567 | &verify_result); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 568 | EXPECT_EQ(OK, error); |
| 569 | EXPECT_EQ(0U, verify_result.cert_status); |
[email protected] | bc0d7b8 | 2012-08-08 06:32:23 | [diff] [blame] | 570 | ASSERT_LE(3u, verify_result.public_key_hashes.size()); |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 571 | |
| 572 | HashValueVector sha1_hashes; |
[email protected] | bfea55d | 2013-04-04 11:06:04 | [diff] [blame] | 573 | for (size_t i = 0; i < verify_result.public_key_hashes.size(); ++i) { |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 574 | if (verify_result.public_key_hashes[i].tag != HASH_VALUE_SHA1) |
| 575 | continue; |
| 576 | sha1_hashes.push_back(verify_result.public_key_hashes[i]); |
| 577 | } |
| 578 | ASSERT_LE(3u, sha1_hashes.size()); |
| 579 | |
[email protected] | bfea55d | 2013-04-04 11:06:04 | [diff] [blame] | 580 | for (size_t i = 0; i < 3; ++i) { |
[email protected] | a6b4ee04 | 2012-07-24 20:52:46 | [diff] [blame] | 581 | EXPECT_EQ(HexEncode(kCertSESPKIs[i], base::kSHA1Length), |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 582 | HexEncode(sha1_hashes[i].data(), base::kSHA1Length)); |
[email protected] | a6b4ee04 | 2012-07-24 20:52:46 | [diff] [blame] | 583 | } |
[email protected] | bfea55d | 2013-04-04 11:06:04 | [diff] [blame] | 584 | |
| 585 | HashValueVector sha256_hashes; |
| 586 | for (size_t i = 0; i < verify_result.public_key_hashes.size(); ++i) { |
| 587 | if (verify_result.public_key_hashes[i].tag != HASH_VALUE_SHA256) |
| 588 | continue; |
| 589 | sha256_hashes.push_back(verify_result.public_key_hashes[i]); |
| 590 | } |
| 591 | ASSERT_LE(3u, sha256_hashes.size()); |
| 592 | |
| 593 | for (size_t i = 0; i < 3; ++i) { |
| 594 | EXPECT_EQ(HexEncode(kCertSESPKIsSHA256[i], crypto::kSHA256Length), |
| 595 | HexEncode(sha256_hashes[i].data(), crypto::kSHA256Length)); |
| 596 | } |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | // A regression test for http://crbug.com/70293. |
| 600 | // The Key Usage extension in this RSA SSL server certificate does not have |
| 601 | // the keyEncipherment bit. |
| 602 | TEST_F(CertVerifyProcTest, InvalidKeyUsage) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 603 | base::FilePath certs_dir = GetTestCertsDirectory(); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 604 | |
| 605 | scoped_refptr<X509Certificate> server_cert = |
| 606 | ImportCertFromFile(certs_dir, "invalid_key_usage_cert.der"); |
| 607 | ASSERT_NE(static_cast<X509Certificate*>(NULL), server_cert); |
| 608 | |
| 609 | int flags = 0; |
| 610 | CertVerifyResult verify_result; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 611 | int error = Verify(server_cert.get(), |
| 612 | "jira.aquameta.com", |
| 613 | flags, |
| 614 | NULL, |
| 615 | empty_cert_list_, |
| 616 | &verify_result); |
[email protected] | c97dc55 | 2013-04-25 21:29:56 | [diff] [blame] | 617 | #if defined(USE_OPENSSL) && !defined(OS_ANDROID) |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 618 | // This certificate has two errors: "invalid key usage" and "untrusted CA". |
| 619 | // However, OpenSSL returns only one (the latter), and we can't detect |
| 620 | // the other errors. |
| 621 | EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, error); |
| 622 | #else |
| 623 | EXPECT_EQ(ERR_CERT_INVALID, error); |
| 624 | EXPECT_TRUE(verify_result.cert_status & CERT_STATUS_INVALID); |
| 625 | #endif |
| 626 | // TODO(wtc): fix http://crbug.com/75520 to get all the certificate errors |
| 627 | // from NSS. |
[email protected] | c97dc55 | 2013-04-25 21:29:56 | [diff] [blame] | 628 | #if !defined(USE_NSS) && !defined(OS_IOS) && !defined(OS_ANDROID) |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 629 | // The certificate is issued by an unknown CA. |
| 630 | EXPECT_TRUE(verify_result.cert_status & CERT_STATUS_AUTHORITY_INVALID); |
| 631 | #endif |
| 632 | } |
| 633 | |
| 634 | // Basic test for returning the chain in CertVerifyResult. Note that the |
| 635 | // returned chain may just be a reflection of the originally supplied chain; |
| 636 | // that is, if any errors occur, the default chain returned is an exact copy |
| 637 | // of the certificate to be verified. The remaining VerifyReturn* tests are |
| 638 | // used to ensure that the actual, verified chain is being returned by |
| 639 | // Verify(). |
| 640 | TEST_F(CertVerifyProcTest, VerifyReturnChainBasic) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 641 | base::FilePath certs_dir = GetTestCertsDirectory(); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 642 | CertificateList certs = CreateCertificateListFromFile( |
| 643 | certs_dir, "x509_verify_results.chain.pem", |
| 644 | X509Certificate::FORMAT_AUTO); |
| 645 | ASSERT_EQ(3U, certs.size()); |
| 646 | |
| 647 | X509Certificate::OSCertHandles intermediates; |
| 648 | intermediates.push_back(certs[1]->os_cert_handle()); |
| 649 | intermediates.push_back(certs[2]->os_cert_handle()); |
| 650 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 651 | ScopedTestRoot scoped_root(certs[2].get()); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 652 | |
| 653 | scoped_refptr<X509Certificate> google_full_chain = |
| 654 | X509Certificate::CreateFromHandle(certs[0]->os_cert_handle(), |
| 655 | intermediates); |
| 656 | ASSERT_NE(static_cast<X509Certificate*>(NULL), google_full_chain); |
| 657 | ASSERT_EQ(2U, google_full_chain->GetIntermediateCertificates().size()); |
| 658 | |
| 659 | CertVerifyResult verify_result; |
| 660 | EXPECT_EQ(static_cast<X509Certificate*>(NULL), verify_result.verified_cert); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 661 | int error = Verify(google_full_chain.get(), |
| 662 | "127.0.0.1", |
| 663 | 0, |
| 664 | NULL, |
| 665 | empty_cert_list_, |
| 666 | &verify_result); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 667 | EXPECT_EQ(OK, error); |
| 668 | ASSERT_NE(static_cast<X509Certificate*>(NULL), verify_result.verified_cert); |
| 669 | |
| 670 | EXPECT_NE(google_full_chain, verify_result.verified_cert); |
| 671 | EXPECT_TRUE(X509Certificate::IsSameOSCert( |
| 672 | google_full_chain->os_cert_handle(), |
| 673 | verify_result.verified_cert->os_cert_handle())); |
| 674 | const X509Certificate::OSCertHandles& return_intermediates = |
| 675 | verify_result.verified_cert->GetIntermediateCertificates(); |
| 676 | ASSERT_EQ(2U, return_intermediates.size()); |
| 677 | EXPECT_TRUE(X509Certificate::IsSameOSCert(return_intermediates[0], |
| 678 | certs[1]->os_cert_handle())); |
| 679 | EXPECT_TRUE(X509Certificate::IsSameOSCert(return_intermediates[1], |
| 680 | certs[2]->os_cert_handle())); |
| 681 | } |
| 682 | |
[email protected] | ff35321 | 2013-05-17 02:09:08 | [diff] [blame] | 683 | // Test that certificates issued for 'intranet' names (that is, containing no |
| 684 | // known public registry controlled domain information) issued by well-known |
| 685 | // CAs are flagged appropriately, while certificates that are issued by |
| 686 | // internal CAs are not flagged. |
| 687 | TEST_F(CertVerifyProcTest, IntranetHostsRejected) { |
| 688 | CertificateList cert_list = CreateCertificateListFromFile( |
| 689 | GetTestCertsDirectory(), "ok_cert.pem", |
| 690 | X509Certificate::FORMAT_AUTO); |
| 691 | ASSERT_EQ(1U, cert_list.size()); |
| 692 | scoped_refptr<X509Certificate> cert(cert_list[0]); |
| 693 | |
| 694 | CertVerifyResult verify_result; |
| 695 | int error = 0; |
| 696 | |
| 697 | // Intranet names for public CAs should be flagged: |
| 698 | verify_proc_ = new WellKnownCaCertVerifyProc(true); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 699 | error = |
| 700 | Verify(cert.get(), "intranet", 0, NULL, empty_cert_list_, &verify_result); |
[email protected] | ff35321 | 2013-05-17 02:09:08 | [diff] [blame] | 701 | EXPECT_EQ(OK, error); |
| 702 | EXPECT_TRUE(verify_result.cert_status & CERT_STATUS_NON_UNIQUE_NAME); |
| 703 | |
| 704 | // However, if the CA is not well known, these should not be flagged: |
| 705 | verify_proc_ = new WellKnownCaCertVerifyProc(false); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 706 | error = |
| 707 | Verify(cert.get(), "intranet", 0, NULL, empty_cert_list_, &verify_result); |
[email protected] | ff35321 | 2013-05-17 02:09:08 | [diff] [blame] | 708 | EXPECT_EQ(OK, error); |
| 709 | EXPECT_FALSE(verify_result.cert_status & CERT_STATUS_NON_UNIQUE_NAME); |
| 710 | } |
| 711 | |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 712 | // Test that the certificate returned in CertVerifyResult is able to reorder |
| 713 | // certificates that are not ordered from end-entity to root. While this is |
| 714 | // a protocol violation if sent during a TLS handshake, if multiple sources |
| 715 | // of intermediate certificates are combined, it's possible that order may |
| 716 | // not be maintained. |
| 717 | TEST_F(CertVerifyProcTest, VerifyReturnChainProperlyOrdered) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 718 | base::FilePath certs_dir = GetTestCertsDirectory(); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 719 | CertificateList certs = CreateCertificateListFromFile( |
| 720 | certs_dir, "x509_verify_results.chain.pem", |
| 721 | X509Certificate::FORMAT_AUTO); |
| 722 | ASSERT_EQ(3U, certs.size()); |
| 723 | |
| 724 | // Construct the chain out of order. |
| 725 | X509Certificate::OSCertHandles intermediates; |
| 726 | intermediates.push_back(certs[2]->os_cert_handle()); |
| 727 | intermediates.push_back(certs[1]->os_cert_handle()); |
| 728 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 729 | ScopedTestRoot scoped_root(certs[2].get()); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 730 | |
| 731 | scoped_refptr<X509Certificate> google_full_chain = |
| 732 | X509Certificate::CreateFromHandle(certs[0]->os_cert_handle(), |
| 733 | intermediates); |
| 734 | ASSERT_NE(static_cast<X509Certificate*>(NULL), google_full_chain); |
| 735 | ASSERT_EQ(2U, google_full_chain->GetIntermediateCertificates().size()); |
| 736 | |
| 737 | CertVerifyResult verify_result; |
| 738 | EXPECT_EQ(static_cast<X509Certificate*>(NULL), verify_result.verified_cert); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 739 | int error = Verify(google_full_chain.get(), |
| 740 | "127.0.0.1", |
| 741 | 0, |
| 742 | NULL, |
| 743 | empty_cert_list_, |
| 744 | &verify_result); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 745 | EXPECT_EQ(OK, error); |
| 746 | ASSERT_NE(static_cast<X509Certificate*>(NULL), verify_result.verified_cert); |
| 747 | |
| 748 | EXPECT_NE(google_full_chain, verify_result.verified_cert); |
| 749 | EXPECT_TRUE(X509Certificate::IsSameOSCert( |
| 750 | google_full_chain->os_cert_handle(), |
| 751 | verify_result.verified_cert->os_cert_handle())); |
| 752 | const X509Certificate::OSCertHandles& return_intermediates = |
| 753 | verify_result.verified_cert->GetIntermediateCertificates(); |
| 754 | ASSERT_EQ(2U, return_intermediates.size()); |
| 755 | EXPECT_TRUE(X509Certificate::IsSameOSCert(return_intermediates[0], |
| 756 | certs[1]->os_cert_handle())); |
| 757 | EXPECT_TRUE(X509Certificate::IsSameOSCert(return_intermediates[1], |
| 758 | certs[2]->os_cert_handle())); |
| 759 | } |
| 760 | |
| 761 | // Test that Verify() filters out certificates which are not related to |
| 762 | // or part of the certificate chain being verified. |
| 763 | TEST_F(CertVerifyProcTest, VerifyReturnChainFiltersUnrelatedCerts) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 764 | base::FilePath certs_dir = GetTestCertsDirectory(); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 765 | CertificateList certs = CreateCertificateListFromFile( |
| 766 | certs_dir, "x509_verify_results.chain.pem", |
| 767 | X509Certificate::FORMAT_AUTO); |
| 768 | ASSERT_EQ(3U, certs.size()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 769 | ScopedTestRoot scoped_root(certs[2].get()); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 770 | |
| 771 | scoped_refptr<X509Certificate> unrelated_dod_certificate = |
| 772 | ImportCertFromFile(certs_dir, "dod_ca_17_cert.der"); |
| 773 | scoped_refptr<X509Certificate> unrelated_dod_certificate2 = |
| 774 | ImportCertFromFile(certs_dir, "dod_root_ca_2_cert.der"); |
| 775 | ASSERT_NE(static_cast<X509Certificate*>(NULL), unrelated_dod_certificate); |
| 776 | ASSERT_NE(static_cast<X509Certificate*>(NULL), unrelated_dod_certificate2); |
| 777 | |
| 778 | // Interject unrelated certificates into the list of intermediates. |
| 779 | X509Certificate::OSCertHandles intermediates; |
| 780 | intermediates.push_back(unrelated_dod_certificate->os_cert_handle()); |
| 781 | intermediates.push_back(certs[1]->os_cert_handle()); |
| 782 | intermediates.push_back(unrelated_dod_certificate2->os_cert_handle()); |
| 783 | intermediates.push_back(certs[2]->os_cert_handle()); |
| 784 | |
| 785 | scoped_refptr<X509Certificate> google_full_chain = |
| 786 | X509Certificate::CreateFromHandle(certs[0]->os_cert_handle(), |
| 787 | intermediates); |
| 788 | ASSERT_NE(static_cast<X509Certificate*>(NULL), google_full_chain); |
| 789 | ASSERT_EQ(4U, google_full_chain->GetIntermediateCertificates().size()); |
| 790 | |
| 791 | CertVerifyResult verify_result; |
| 792 | EXPECT_EQ(static_cast<X509Certificate*>(NULL), verify_result.verified_cert); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 793 | int error = Verify(google_full_chain.get(), |
| 794 | "127.0.0.1", |
| 795 | 0, |
| 796 | NULL, |
| 797 | empty_cert_list_, |
| 798 | &verify_result); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 799 | EXPECT_EQ(OK, error); |
| 800 | ASSERT_NE(static_cast<X509Certificate*>(NULL), verify_result.verified_cert); |
| 801 | |
| 802 | EXPECT_NE(google_full_chain, verify_result.verified_cert); |
| 803 | EXPECT_TRUE(X509Certificate::IsSameOSCert( |
| 804 | google_full_chain->os_cert_handle(), |
| 805 | verify_result.verified_cert->os_cert_handle())); |
| 806 | const X509Certificate::OSCertHandles& return_intermediates = |
| 807 | verify_result.verified_cert->GetIntermediateCertificates(); |
| 808 | ASSERT_EQ(2U, return_intermediates.size()); |
| 809 | EXPECT_TRUE(X509Certificate::IsSameOSCert(return_intermediates[0], |
| 810 | certs[1]->os_cert_handle())); |
| 811 | EXPECT_TRUE(X509Certificate::IsSameOSCert(return_intermediates[1], |
| 812 | certs[2]->os_cert_handle())); |
| 813 | } |
| 814 | |
[email protected] | ef15512 | 2013-03-23 19:11:24 | [diff] [blame] | 815 | TEST_F(CertVerifyProcTest, AdditionalTrustAnchors) { |
| 816 | if (!SupportsAdditionalTrustAnchors()) { |
| 817 | LOG(INFO) << "Skipping this test in this platform."; |
| 818 | return; |
| 819 | } |
| 820 | |
| 821 | // |ca_cert| is the issuer of |cert|. |
| 822 | CertificateList ca_cert_list = CreateCertificateListFromFile( |
| 823 | GetTestCertsDirectory(), "root_ca_cert.crt", |
| 824 | X509Certificate::FORMAT_AUTO); |
| 825 | ASSERT_EQ(1U, ca_cert_list.size()); |
| 826 | scoped_refptr<X509Certificate> ca_cert(ca_cert_list[0]); |
| 827 | |
| 828 | CertificateList cert_list = CreateCertificateListFromFile( |
| 829 | GetTestCertsDirectory(), "ok_cert.pem", |
| 830 | X509Certificate::FORMAT_AUTO); |
| 831 | ASSERT_EQ(1U, cert_list.size()); |
| 832 | scoped_refptr<X509Certificate> cert(cert_list[0]); |
| 833 | |
| 834 | // Verification of |cert| fails when |ca_cert| is not in the trust anchors |
| 835 | // list. |
| 836 | int flags = 0; |
| 837 | CertVerifyResult verify_result; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 838 | int error = Verify( |
| 839 | cert.get(), "127.0.0.1", flags, NULL, empty_cert_list_, &verify_result); |
[email protected] | ef15512 | 2013-03-23 19:11:24 | [diff] [blame] | 840 | EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, error); |
| 841 | EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, verify_result.cert_status); |
[email protected] | 0d20bad | 2013-03-24 04:56:48 | [diff] [blame] | 842 | EXPECT_FALSE(verify_result.is_issued_by_additional_trust_anchor); |
[email protected] | ef15512 | 2013-03-23 19:11:24 | [diff] [blame] | 843 | |
| 844 | // Now add the |ca_cert| to the |trust_anchors|, and verification should pass. |
| 845 | CertificateList trust_anchors; |
| 846 | trust_anchors.push_back(ca_cert); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 847 | error = Verify( |
| 848 | cert.get(), "127.0.0.1", flags, NULL, trust_anchors, &verify_result); |
[email protected] | ef15512 | 2013-03-23 19:11:24 | [diff] [blame] | 849 | EXPECT_EQ(OK, error); |
| 850 | EXPECT_EQ(0U, verify_result.cert_status); |
[email protected] | 0d20bad | 2013-03-24 04:56:48 | [diff] [blame] | 851 | EXPECT_TRUE(verify_result.is_issued_by_additional_trust_anchor); |
[email protected] | ef15512 | 2013-03-23 19:11:24 | [diff] [blame] | 852 | |
| 853 | // Clearing the |trust_anchors| makes verification fail again (the cache |
| 854 | // should be skipped). |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 855 | error = Verify( |
| 856 | cert.get(), "127.0.0.1", flags, NULL, empty_cert_list_, &verify_result); |
[email protected] | ef15512 | 2013-03-23 19:11:24 | [diff] [blame] | 857 | EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, error); |
| 858 | EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, verify_result.cert_status); |
[email protected] | 0d20bad | 2013-03-24 04:56:48 | [diff] [blame] | 859 | EXPECT_FALSE(verify_result.is_issued_by_additional_trust_anchor); |
[email protected] | ef15512 | 2013-03-23 19:11:24 | [diff] [blame] | 860 | } |
| 861 | |
[email protected] | 12b3c882 | 2012-09-26 11:51:18 | [diff] [blame] | 862 | #if defined(USE_NSS) || defined(OS_IOS) || defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 863 | static const uint8 kCRLSetThawteSPKIBlocked[] = { |
| 864 | 0x8e, 0x00, 0x7b, 0x22, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x22, 0x3a, |
| 865 | 0x30, 0x2c, 0x22, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x54, 0x79, 0x70, |
| 866 | 0x65, 0x22, 0x3a, 0x22, 0x43, 0x52, 0x4c, 0x53, 0x65, 0x74, 0x22, 0x2c, 0x22, |
| 867 | 0x53, 0x65, 0x71, 0x75, 0x65, 0x6e, 0x63, 0x65, 0x22, 0x3a, 0x30, 0x2c, 0x22, |
| 868 | 0x44, 0x65, 0x6c, 0x74, 0x61, 0x46, 0x72, 0x6f, 0x6d, 0x22, 0x3a, 0x30, 0x2c, |
| 869 | 0x22, 0x4e, 0x75, 0x6d, 0x50, 0x61, 0x72, 0x65, 0x6e, 0x74, 0x73, 0x22, 0x3a, |
| 870 | 0x30, 0x2c, 0x22, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x65, 0x64, 0x53, 0x50, 0x4b, |
| 871 | 0x49, 0x73, 0x22, 0x3a, 0x5b, 0x22, 0x36, 0x58, 0x36, 0x4d, 0x78, 0x52, 0x37, |
| 872 | 0x58, 0x70, 0x4d, 0x51, 0x4b, 0x78, 0x49, 0x41, 0x39, 0x50, 0x6a, 0x36, 0x37, |
| 873 | 0x36, 0x38, 0x76, 0x74, 0x55, 0x6b, 0x6b, 0x7a, 0x48, 0x79, 0x7a, 0x41, 0x6f, |
| 874 | 0x6d, 0x6f, 0x4f, 0x68, 0x4b, 0x55, 0x6e, 0x7a, 0x73, 0x55, 0x3d, 0x22, 0x5d, |
| 875 | 0x7d, |
| 876 | }; |
| 877 | |
| 878 | static const uint8 kCRLSetThawteSerialBlocked[] = { |
| 879 | 0x60, 0x00, 0x7b, 0x22, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x22, 0x3a, |
| 880 | 0x30, 0x2c, 0x22, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x54, 0x79, 0x70, |
| 881 | 0x65, 0x22, 0x3a, 0x22, 0x43, 0x52, 0x4c, 0x53, 0x65, 0x74, 0x22, 0x2c, 0x22, |
| 882 | 0x53, 0x65, 0x71, 0x75, 0x65, 0x6e, 0x63, 0x65, 0x22, 0x3a, 0x30, 0x2c, 0x22, |
| 883 | 0x44, 0x65, 0x6c, 0x74, 0x61, 0x46, 0x72, 0x6f, 0x6d, 0x22, 0x3a, 0x30, 0x2c, |
| 884 | 0x22, 0x4e, 0x75, 0x6d, 0x50, 0x61, 0x72, 0x65, 0x6e, 0x74, 0x73, 0x22, 0x3a, |
| 885 | 0x31, 0x2c, 0x22, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x65, 0x64, 0x53, 0x50, 0x4b, |
| 886 | 0x49, 0x73, 0x22, 0x3a, 0x5b, 0x5d, 0x7d, 0xb1, 0x12, 0x41, 0x42, 0xa5, 0xa1, |
| 887 | 0xa5, 0xa2, 0x88, 0x19, 0xc7, 0x35, 0x34, 0x0e, 0xff, 0x8c, 0x9e, 0x2f, 0x81, |
| 888 | 0x68, 0xfe, 0xe3, 0xba, 0x18, 0x7f, 0x25, 0x3b, 0xc1, 0xa3, 0x92, 0xd7, 0xe2, |
| 889 | // Note that this is actually blocking two serial numbers because on XP and |
| 890 | // Vista, CryptoAPI finds a different Thawte certificate. |
| 891 | 0x02, 0x00, 0x00, 0x00, |
| 892 | 0x04, 0x30, 0x00, 0x00, 0x02, |
| 893 | 0x04, 0x30, 0x00, 0x00, 0x06, |
| 894 | }; |
| 895 | |
| 896 | static const uint8 kCRLSetGoogleSerialBlocked[] = { |
| 897 | 0x60, 0x00, 0x7b, 0x22, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x22, 0x3a, |
| 898 | 0x30, 0x2c, 0x22, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x54, 0x79, 0x70, |
| 899 | 0x65, 0x22, 0x3a, 0x22, 0x43, 0x52, 0x4c, 0x53, 0x65, 0x74, 0x22, 0x2c, 0x22, |
| 900 | 0x53, 0x65, 0x71, 0x75, 0x65, 0x6e, 0x63, 0x65, 0x22, 0x3a, 0x30, 0x2c, 0x22, |
| 901 | 0x44, 0x65, 0x6c, 0x74, 0x61, 0x46, 0x72, 0x6f, 0x6d, 0x22, 0x3a, 0x30, 0x2c, |
| 902 | 0x22, 0x4e, 0x75, 0x6d, 0x50, 0x61, 0x72, 0x65, 0x6e, 0x74, 0x73, 0x22, 0x3a, |
| 903 | 0x31, 0x2c, 0x22, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x65, 0x64, 0x53, 0x50, 0x4b, |
| 904 | 0x49, 0x73, 0x22, 0x3a, 0x5b, 0x5d, 0x7d, 0xe9, 0x7e, 0x8c, 0xc5, 0x1e, 0xd7, |
| 905 | 0xa4, 0xc4, 0x0a, 0xc4, 0x80, 0x3d, 0x3e, 0x3e, 0xbb, 0xeb, 0xcb, 0xed, 0x52, |
| 906 | 0x49, 0x33, 0x1f, 0x2c, 0xc0, 0xa2, 0x6a, 0x0e, 0x84, 0xa5, 0x27, 0xce, 0xc5, |
| 907 | 0x01, 0x00, 0x00, 0x00, 0x10, 0x4f, 0x9d, 0x96, 0xd9, 0x66, 0xb0, 0x99, 0x2b, |
| 908 | 0x54, 0xc2, 0x95, 0x7c, 0xb4, 0x15, 0x7d, 0x4d, |
| 909 | }; |
| 910 | |
| 911 | // Test that CRLSets are effective in making a certificate appear to be |
| 912 | // revoked. |
| 913 | TEST_F(CertVerifyProcTest, CRLSet) { |
| 914 | CertificateList certs = CreateCertificateListFromFile( |
| 915 | GetTestCertsDirectory(), |
| 916 | "googlenew.chain.pem", |
| 917 | X509Certificate::FORMAT_PEM_CERT_SEQUENCE); |
| 918 | |
| 919 | X509Certificate::OSCertHandles intermediates; |
| 920 | intermediates.push_back(certs[1]->os_cert_handle()); |
| 921 | |
| 922 | scoped_refptr<X509Certificate> google_full_chain = |
| 923 | X509Certificate::CreateFromHandle(certs[0]->os_cert_handle(), |
| 924 | intermediates); |
| 925 | |
| 926 | CertVerifyResult verify_result; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 927 | int error = Verify(google_full_chain.get(), |
| 928 | "www.google.com", |
| 929 | 0, |
| 930 | NULL, |
| 931 | empty_cert_list_, |
| 932 | &verify_result); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 933 | EXPECT_EQ(OK, error); |
| 934 | |
| 935 | // First test blocking by SPKI. |
| 936 | base::StringPiece crl_set_bytes( |
| 937 | reinterpret_cast<const char*>(kCRLSetThawteSPKIBlocked), |
| 938 | sizeof(kCRLSetThawteSPKIBlocked)); |
| 939 | scoped_refptr<CRLSet> crl_set; |
| 940 | ASSERT_TRUE(CRLSet::Parse(crl_set_bytes, &crl_set)); |
| 941 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 942 | error = Verify(google_full_chain.get(), |
| 943 | "www.google.com", |
| 944 | 0, |
| 945 | crl_set.get(), |
| 946 | empty_cert_list_, |
| 947 | &verify_result); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 948 | EXPECT_EQ(ERR_CERT_REVOKED, error); |
| 949 | |
| 950 | // Second, test revocation by serial number of a cert directly under the |
| 951 | // root. |
| 952 | crl_set_bytes = base::StringPiece( |
| 953 | reinterpret_cast<const char*>(kCRLSetThawteSerialBlocked), |
| 954 | sizeof(kCRLSetThawteSerialBlocked)); |
| 955 | ASSERT_TRUE(CRLSet::Parse(crl_set_bytes, &crl_set)); |
| 956 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 957 | error = Verify(google_full_chain.get(), |
| 958 | "www.google.com", |
| 959 | 0, |
| 960 | crl_set.get(), |
| 961 | empty_cert_list_, |
| 962 | &verify_result); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 963 | EXPECT_EQ(ERR_CERT_REVOKED, error); |
| 964 | |
| 965 | // Lastly, test revocation by serial number of a certificate not under the |
| 966 | // root. |
| 967 | crl_set_bytes = base::StringPiece( |
| 968 | reinterpret_cast<const char*>(kCRLSetGoogleSerialBlocked), |
| 969 | sizeof(kCRLSetGoogleSerialBlocked)); |
| 970 | ASSERT_TRUE(CRLSet::Parse(crl_set_bytes, &crl_set)); |
| 971 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 972 | error = Verify(google_full_chain.get(), |
| 973 | "www.google.com", |
| 974 | 0, |
| 975 | crl_set.get(), |
| 976 | empty_cert_list_, |
| 977 | &verify_result); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 978 | EXPECT_EQ(ERR_CERT_REVOKED, error); |
| 979 | } |
| 980 | #endif |
| 981 | |
| 982 | struct WeakDigestTestData { |
| 983 | const char* root_cert_filename; |
| 984 | const char* intermediate_cert_filename; |
| 985 | const char* ee_cert_filename; |
| 986 | bool expected_has_md5; |
| 987 | bool expected_has_md4; |
| 988 | bool expected_has_md2; |
| 989 | bool expected_has_md5_ca; |
| 990 | bool expected_has_md2_ca; |
| 991 | }; |
| 992 | |
| 993 | // GTest 'magic' pretty-printer, so that if/when a test fails, it knows how |
| 994 | // to output the parameter that was passed. Without this, it will simply |
| 995 | // attempt to print out the first twenty bytes of the object, which depending |
| 996 | // on platform and alignment, may result in an invalid read. |
| 997 | void PrintTo(const WeakDigestTestData& data, std::ostream* os) { |
| 998 | *os << "root: " |
| 999 | << (data.root_cert_filename ? data.root_cert_filename : "none") |
| 1000 | << "; intermediate: " << data.intermediate_cert_filename |
| 1001 | << "; end-entity: " << data.ee_cert_filename; |
| 1002 | } |
| 1003 | |
| 1004 | class CertVerifyProcWeakDigestTest |
| 1005 | : public CertVerifyProcTest, |
| 1006 | public testing::WithParamInterface<WeakDigestTestData> { |
| 1007 | public: |
| 1008 | CertVerifyProcWeakDigestTest() {} |
| 1009 | virtual ~CertVerifyProcWeakDigestTest() {} |
| 1010 | }; |
| 1011 | |
| 1012 | TEST_P(CertVerifyProcWeakDigestTest, Verify) { |
| 1013 | WeakDigestTestData data = GetParam(); |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1014 | base::FilePath certs_dir = GetTestCertsDirectory(); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 1015 | |
| 1016 | ScopedTestRoot test_root; |
| 1017 | if (data.root_cert_filename) { |
| 1018 | scoped_refptr<X509Certificate> root_cert = |
| 1019 | ImportCertFromFile(certs_dir, data.root_cert_filename); |
| 1020 | ASSERT_NE(static_cast<X509Certificate*>(NULL), root_cert); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1021 | test_root.Reset(root_cert.get()); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | scoped_refptr<X509Certificate> intermediate_cert = |
| 1025 | ImportCertFromFile(certs_dir, data.intermediate_cert_filename); |
| 1026 | ASSERT_NE(static_cast<X509Certificate*>(NULL), intermediate_cert); |
| 1027 | scoped_refptr<X509Certificate> ee_cert = |
| 1028 | ImportCertFromFile(certs_dir, data.ee_cert_filename); |
| 1029 | ASSERT_NE(static_cast<X509Certificate*>(NULL), ee_cert); |
| 1030 | |
| 1031 | X509Certificate::OSCertHandles intermediates; |
| 1032 | intermediates.push_back(intermediate_cert->os_cert_handle()); |
| 1033 | |
| 1034 | scoped_refptr<X509Certificate> ee_chain = |
| 1035 | X509Certificate::CreateFromHandle(ee_cert->os_cert_handle(), |
| 1036 | intermediates); |
| 1037 | ASSERT_NE(static_cast<X509Certificate*>(NULL), ee_chain); |
| 1038 | |
| 1039 | int flags = 0; |
| 1040 | CertVerifyResult verify_result; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1041 | int rv = Verify(ee_chain.get(), |
| 1042 | "127.0.0.1", |
| 1043 | flags, |
| 1044 | NULL, |
| 1045 | empty_cert_list_, |
| 1046 | &verify_result); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 1047 | EXPECT_EQ(data.expected_has_md5, verify_result.has_md5); |
| 1048 | EXPECT_EQ(data.expected_has_md4, verify_result.has_md4); |
| 1049 | EXPECT_EQ(data.expected_has_md2, verify_result.has_md2); |
| 1050 | EXPECT_EQ(data.expected_has_md5_ca, verify_result.has_md5_ca); |
| 1051 | EXPECT_EQ(data.expected_has_md2_ca, verify_result.has_md2_ca); |
[email protected] | 0d20bad | 2013-03-24 04:56:48 | [diff] [blame] | 1052 | EXPECT_FALSE(verify_result.is_issued_by_additional_trust_anchor); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 1053 | |
| 1054 | // Ensure that MD4 and MD2 are tagged as invalid. |
| 1055 | if (data.expected_has_md4 || data.expected_has_md2) { |
| 1056 | EXPECT_EQ(CERT_STATUS_INVALID, |
| 1057 | verify_result.cert_status & CERT_STATUS_INVALID); |
| 1058 | } |
| 1059 | |
| 1060 | // Ensure that MD5 is flagged as weak. |
| 1061 | if (data.expected_has_md5) { |
| 1062 | EXPECT_EQ( |
| 1063 | CERT_STATUS_WEAK_SIGNATURE_ALGORITHM, |
| 1064 | verify_result.cert_status & CERT_STATUS_WEAK_SIGNATURE_ALGORITHM); |
| 1065 | } |
| 1066 | |
| 1067 | // If a root cert is present, then check that the chain was rejected if any |
| 1068 | // weak algorithms are present. This is only checked when a root cert is |
| 1069 | // present because the error reported for incomplete chains with weak |
| 1070 | // algorithms depends on which implementation was used to validate (NSS, |
| 1071 | // OpenSSL, CryptoAPI, Security.framework) and upon which weak algorithm |
| 1072 | // present (MD2, MD4, MD5). |
| 1073 | if (data.root_cert_filename) { |
| 1074 | if (data.expected_has_md4 || data.expected_has_md2) { |
| 1075 | EXPECT_EQ(ERR_CERT_INVALID, rv); |
| 1076 | } else if (data.expected_has_md5) { |
| 1077 | EXPECT_EQ(ERR_CERT_WEAK_SIGNATURE_ALGORITHM, rv); |
| 1078 | } else { |
| 1079 | EXPECT_EQ(OK, rv); |
| 1080 | } |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | // Unlike TEST/TEST_F, which are macros that expand to further macros, |
| 1085 | // INSTANTIATE_TEST_CASE_P is a macro that expands directly to code that |
| 1086 | // stringizes the arguments. As a result, macros passed as parameters (such as |
| 1087 | // prefix or test_case_name) will not be expanded by the preprocessor. To work |
| 1088 | // around this, indirect the macro for INSTANTIATE_TEST_CASE_P, so that the |
| 1089 | // pre-processor will expand macros such as MAYBE_test_name before |
| 1090 | // instantiating the test. |
| 1091 | #define WRAPPED_INSTANTIATE_TEST_CASE_P(prefix, test_case_name, generator) \ |
| 1092 | INSTANTIATE_TEST_CASE_P(prefix, test_case_name, generator) |
| 1093 | |
| 1094 | // The signature algorithm of the root CA should not matter. |
| 1095 | const WeakDigestTestData kVerifyRootCATestData[] = { |
| 1096 | { "weak_digest_md5_root.pem", "weak_digest_sha1_intermediate.pem", |
| 1097 | "weak_digest_sha1_ee.pem", false, false, false, false, false }, |
[email protected] | 317dbc7 | 2012-04-03 18:45:14 | [diff] [blame] | 1098 | #if defined(USE_OPENSSL) || defined(OS_WIN) |
| 1099 | // MD4 is not supported by OS X / NSS |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 1100 | { "weak_digest_md4_root.pem", "weak_digest_sha1_intermediate.pem", |
| 1101 | "weak_digest_sha1_ee.pem", false, false, false, false, false }, |
| 1102 | #endif |
| 1103 | { "weak_digest_md2_root.pem", "weak_digest_sha1_intermediate.pem", |
| 1104 | "weak_digest_sha1_ee.pem", false, false, false, false, false }, |
| 1105 | }; |
| 1106 | INSTANTIATE_TEST_CASE_P(VerifyRoot, CertVerifyProcWeakDigestTest, |
| 1107 | testing::ValuesIn(kVerifyRootCATestData)); |
| 1108 | |
| 1109 | // The signature algorithm of intermediates should be properly detected. |
| 1110 | const WeakDigestTestData kVerifyIntermediateCATestData[] = { |
| 1111 | { "weak_digest_sha1_root.pem", "weak_digest_md5_intermediate.pem", |
| 1112 | "weak_digest_sha1_ee.pem", true, false, false, true, false }, |
[email protected] | 317dbc7 | 2012-04-03 18:45:14 | [diff] [blame] | 1113 | #if defined(USE_OPENSSL) || defined(OS_WIN) |
| 1114 | // MD4 is not supported by OS X / NSS |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 1115 | { "weak_digest_sha1_root.pem", "weak_digest_md4_intermediate.pem", |
| 1116 | "weak_digest_sha1_ee.pem", false, true, false, false, false }, |
| 1117 | #endif |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 1118 | { "weak_digest_sha1_root.pem", "weak_digest_md2_intermediate.pem", |
| 1119 | "weak_digest_sha1_ee.pem", false, false, true, false, true }, |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 1120 | }; |
[email protected] | fa2d3dc | 2012-11-20 07:58:44 | [diff] [blame] | 1121 | // Disabled on NSS - MD4 is not supported, and MD2 and MD5 are disabled. |
| 1122 | #if defined(USE_NSS) || defined(OS_IOS) |
| 1123 | #define MAYBE_VerifyIntermediate DISABLED_VerifyIntermediate |
| 1124 | #else |
| 1125 | #define MAYBE_VerifyIntermediate VerifyIntermediate |
| 1126 | #endif |
| 1127 | WRAPPED_INSTANTIATE_TEST_CASE_P( |
| 1128 | MAYBE_VerifyIntermediate, |
| 1129 | CertVerifyProcWeakDigestTest, |
| 1130 | testing::ValuesIn(kVerifyIntermediateCATestData)); |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 1131 | |
| 1132 | // The signature algorithm of end-entity should be properly detected. |
| 1133 | const WeakDigestTestData kVerifyEndEntityTestData[] = { |
| 1134 | { "weak_digest_sha1_root.pem", "weak_digest_sha1_intermediate.pem", |
| 1135 | "weak_digest_md5_ee.pem", true, false, false, false, false }, |
[email protected] | 317dbc7 | 2012-04-03 18:45:14 | [diff] [blame] | 1136 | #if defined(USE_OPENSSL) || defined(OS_WIN) |
| 1137 | // MD4 is not supported by OS X / NSS |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 1138 | { "weak_digest_sha1_root.pem", "weak_digest_sha1_intermediate.pem", |
| 1139 | "weak_digest_md4_ee.pem", false, true, false, false, false }, |
| 1140 | #endif |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 1141 | { "weak_digest_sha1_root.pem", "weak_digest_sha1_intermediate.pem", |
| 1142 | "weak_digest_md2_ee.pem", false, false, true, false, false }, |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 1143 | }; |
| 1144 | // Disabled on NSS - NSS caches chains/signatures in such a way that cannot |
| 1145 | // be cleared until NSS is cleanly shutdown, which is not presently supported |
| 1146 | // in Chromium. |
[email protected] | 12b3c882 | 2012-09-26 11:51:18 | [diff] [blame] | 1147 | #if defined(USE_NSS) || defined(OS_IOS) |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 1148 | #define MAYBE_VerifyEndEntity DISABLED_VerifyEndEntity |
| 1149 | #else |
| 1150 | #define MAYBE_VerifyEndEntity VerifyEndEntity |
| 1151 | #endif |
| 1152 | WRAPPED_INSTANTIATE_TEST_CASE_P(MAYBE_VerifyEndEntity, |
| 1153 | CertVerifyProcWeakDigestTest, |
| 1154 | testing::ValuesIn(kVerifyEndEntityTestData)); |
| 1155 | |
| 1156 | // Incomplete chains should still report the status of the intermediate. |
| 1157 | const WeakDigestTestData kVerifyIncompleteIntermediateTestData[] = { |
| 1158 | { NULL, "weak_digest_md5_intermediate.pem", "weak_digest_sha1_ee.pem", |
| 1159 | true, false, false, true, false }, |
[email protected] | 317dbc7 | 2012-04-03 18:45:14 | [diff] [blame] | 1160 | #if defined(USE_OPENSSL) || defined(OS_WIN) |
| 1161 | // MD4 is not supported by OS X / NSS |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 1162 | { NULL, "weak_digest_md4_intermediate.pem", "weak_digest_sha1_ee.pem", |
| 1163 | false, true, false, false, false }, |
| 1164 | #endif |
| 1165 | { NULL, "weak_digest_md2_intermediate.pem", "weak_digest_sha1_ee.pem", |
| 1166 | false, false, true, false, true }, |
| 1167 | }; |
| 1168 | // Disabled on NSS - libpkix does not return constructed chains on error, |
| 1169 | // preventing us from detecting/inspecting the verified chain. |
[email protected] | 12b3c882 | 2012-09-26 11:51:18 | [diff] [blame] | 1170 | #if defined(USE_NSS) || defined(OS_IOS) |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 1171 | #define MAYBE_VerifyIncompleteIntermediate \ |
| 1172 | DISABLED_VerifyIncompleteIntermediate |
| 1173 | #else |
| 1174 | #define MAYBE_VerifyIncompleteIntermediate VerifyIncompleteIntermediate |
| 1175 | #endif |
| 1176 | WRAPPED_INSTANTIATE_TEST_CASE_P( |
| 1177 | MAYBE_VerifyIncompleteIntermediate, |
| 1178 | CertVerifyProcWeakDigestTest, |
| 1179 | testing::ValuesIn(kVerifyIncompleteIntermediateTestData)); |
| 1180 | |
| 1181 | // Incomplete chains should still report the status of the end-entity. |
| 1182 | const WeakDigestTestData kVerifyIncompleteEETestData[] = { |
| 1183 | { NULL, "weak_digest_sha1_intermediate.pem", "weak_digest_md5_ee.pem", |
| 1184 | true, false, false, false, false }, |
[email protected] | 317dbc7 | 2012-04-03 18:45:14 | [diff] [blame] | 1185 | #if defined(USE_OPENSSL) || defined(OS_WIN) |
| 1186 | // MD4 is not supported by OS X / NSS |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 1187 | { NULL, "weak_digest_sha1_intermediate.pem", "weak_digest_md4_ee.pem", |
| 1188 | false, true, false, false, false }, |
| 1189 | #endif |
| 1190 | { NULL, "weak_digest_sha1_intermediate.pem", "weak_digest_md2_ee.pem", |
| 1191 | false, false, true, false, false }, |
| 1192 | }; |
| 1193 | // Disabled on NSS - libpkix does not return constructed chains on error, |
| 1194 | // preventing us from detecting/inspecting the verified chain. |
[email protected] | 12b3c882 | 2012-09-26 11:51:18 | [diff] [blame] | 1195 | #if defined(USE_NSS) || defined(OS_IOS) |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 1196 | #define MAYBE_VerifyIncompleteEndEntity DISABLED_VerifyIncompleteEndEntity |
| 1197 | #else |
| 1198 | #define MAYBE_VerifyIncompleteEndEntity VerifyIncompleteEndEntity |
| 1199 | #endif |
| 1200 | WRAPPED_INSTANTIATE_TEST_CASE_P( |
| 1201 | MAYBE_VerifyIncompleteEndEntity, |
| 1202 | CertVerifyProcWeakDigestTest, |
| 1203 | testing::ValuesIn(kVerifyIncompleteEETestData)); |
| 1204 | |
| 1205 | // Differing algorithms between the intermediate and the EE should still be |
| 1206 | // reported. |
| 1207 | const WeakDigestTestData kVerifyMixedTestData[] = { |
| 1208 | { "weak_digest_sha1_root.pem", "weak_digest_md5_intermediate.pem", |
| 1209 | "weak_digest_md2_ee.pem", true, false, true, true, false }, |
| 1210 | { "weak_digest_sha1_root.pem", "weak_digest_md2_intermediate.pem", |
| 1211 | "weak_digest_md5_ee.pem", true, false, true, false, true }, |
[email protected] | 317dbc7 | 2012-04-03 18:45:14 | [diff] [blame] | 1212 | #if defined(USE_OPENSSL) || defined(OS_WIN) |
| 1213 | // MD4 is not supported by OS X / NSS |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 1214 | { "weak_digest_sha1_root.pem", "weak_digest_md4_intermediate.pem", |
| 1215 | "weak_digest_md2_ee.pem", false, true, true, false, false }, |
| 1216 | #endif |
| 1217 | }; |
| 1218 | // NSS does not support MD4 and does not enable MD2 by default, making all |
| 1219 | // permutations invalid. |
[email protected] | 12b3c882 | 2012-09-26 11:51:18 | [diff] [blame] | 1220 | #if defined(USE_NSS) || defined(OS_IOS) |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 1221 | #define MAYBE_VerifyMixed DISABLED_VerifyMixed |
| 1222 | #else |
| 1223 | #define MAYBE_VerifyMixed VerifyMixed |
| 1224 | #endif |
| 1225 | WRAPPED_INSTANTIATE_TEST_CASE_P( |
| 1226 | MAYBE_VerifyMixed, |
| 1227 | CertVerifyProcWeakDigestTest, |
| 1228 | testing::ValuesIn(kVerifyMixedTestData)); |
| 1229 | |
[email protected] | ff35321 | 2013-05-17 02:09:08 | [diff] [blame] | 1230 | struct NonUniqueNameTestData { |
| 1231 | bool is_unique; |
| 1232 | const char* hostname; |
| 1233 | }; |
| 1234 | |
| 1235 | // Google Test pretty-printer. |
| 1236 | void PrintTo(const NonUniqueNameTestData& data, std::ostream* os) { |
| 1237 | ASSERT_TRUE(data.hostname); |
| 1238 | *os << " hostname: " << testing::PrintToString(data.hostname) |
| 1239 | << "; is_unique: " << testing::PrintToString(data.is_unique); |
| 1240 | } |
| 1241 | |
| 1242 | const NonUniqueNameTestData kNonUniqueNameTestData[] = { |
| 1243 | // Domains under ICANN-assigned domains. |
| 1244 | { true, "google.com" }, |
| 1245 | { true, "google.co.uk" }, |
| 1246 | // Domains under private registries. |
| 1247 | { true, "appspot.com" }, |
| 1248 | { true, "test.appspot.com" }, |
| 1249 | // IPv4 addresses (in various forms). |
| 1250 | { true, "8.8.8.8" }, |
| 1251 | { true, "1.2.3" }, |
| 1252 | { true, "14.15" }, |
| 1253 | { true, "676768" }, |
| 1254 | // IPv6 addresses. |
| 1255 | { true, "FEDC:ba98:7654:3210:FEDC:BA98:7654:3210" }, |
| 1256 | { true, "::192.9.5.5" }, |
| 1257 | { true, "FEED::BEEF" }, |
| 1258 | // 'internal'/non-IANA assigned domains. |
| 1259 | { false, "intranet" }, |
| 1260 | { false, "intranet." }, |
| 1261 | { false, "intranet.example" }, |
| 1262 | { false, "host.intranet.example" }, |
| 1263 | // gTLDs under discussion, but not yet assigned. |
| 1264 | { false, "intranet.corp" }, |
| 1265 | { false, "example.tech" }, |
| 1266 | { false, "intranet.internal" }, |
| 1267 | // Invalid host names are treated as unique - but expected to be |
| 1268 | // filtered out before then. |
| 1269 | { true, "junk)(£)$*!@~#" }, |
| 1270 | { true, "w$w.example.com" }, |
| 1271 | { true, "nocolonsallowed:example" }, |
| 1272 | { true, "[::4.5.6.9]" }, |
| 1273 | }; |
| 1274 | |
| 1275 | class CertVerifyProcNonUniqueNameTest |
| 1276 | : public testing::TestWithParam<NonUniqueNameTestData> { |
| 1277 | public: |
| 1278 | virtual ~CertVerifyProcNonUniqueNameTest() {} |
| 1279 | |
| 1280 | protected: |
| 1281 | bool IsUnique(const std::string& hostname) { |
| 1282 | return !CertVerifyProc::IsHostnameNonUnique(hostname); |
| 1283 | } |
| 1284 | }; |
| 1285 | |
| 1286 | // Test that internal/non-unique names are properly identified as such, but |
| 1287 | // that IP addresses and hosts beneath registry-controlled domains are flagged |
| 1288 | // as unique names. |
| 1289 | TEST_P(CertVerifyProcNonUniqueNameTest, IsHostnameNonUnique) { |
| 1290 | const NonUniqueNameTestData& test_data = GetParam(); |
| 1291 | |
| 1292 | EXPECT_EQ(test_data.is_unique, IsUnique(test_data.hostname)); |
| 1293 | } |
| 1294 | |
| 1295 | INSTANTIATE_TEST_CASE_P(, CertVerifyProcNonUniqueNameTest, |
| 1296 | testing::ValuesIn(kNonUniqueNameTestData)); |
| 1297 | |
[email protected] | 66d9b6e3 | 2012-03-22 03:04:32 | [diff] [blame] | 1298 | } // namespace net |