[email protected] | e54d0af | 2012-03-03 01:07:15 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | c81d9dcc | 2010-03-17 00:51:44 | [diff] [blame] | 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/test/cert_test_util.h" |
[email protected] | c81d9dcc | 2010-03-17 00:51:44 | [diff] [blame] | 6 | |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 7 | #include "base/files/file_path.h" |
thestig | d8df033 | 2014-09-04 06:33:29 | [diff] [blame] | 8 | #include "base/files/file_util.h" |
jam | 3f2d393 | 2017-04-26 20:28:51 | [diff] [blame] | 9 | #include "base/threading/thread_restrictions.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 10 | #include "net/cert/ev_root_ca_metadata.h" |
| 11 | #include "net/cert/x509_certificate.h" |
Matt Mueller | a419327 | 2017-12-07 00:23:34 | [diff] [blame] | 12 | #include "net/cert/x509_util.h" |
eroman | ce65aff | 2017-02-04 00:05:32 | [diff] [blame] | 13 | #include "net/test/test_data_directory.h" |
[email protected] | c81d9dcc | 2010-03-17 00:51:44 | [diff] [blame] | 14 | |
| 15 | namespace net { |
| 16 | |
[email protected] | e54d0af | 2012-03-03 01:07:15 | [diff] [blame] | 17 | CertificateList CreateCertificateListFromFile( |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 18 | const base::FilePath& certs_dir, |
[email protected] | e54d0af | 2012-03-03 01:07:15 | [diff] [blame] | 19 | const std::string& cert_file, |
| 20 | int format) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 21 | base::FilePath cert_path = certs_dir.AppendASCII(cert_file); |
[email protected] | e54d0af | 2012-03-03 01:07:15 | [diff] [blame] | 22 | std::string cert_data; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 23 | if (!base::ReadFileToString(cert_path, &cert_data)) |
[email protected] | e54d0af | 2012-03-03 01:07:15 | [diff] [blame] | 24 | return CertificateList(); |
| 25 | return X509Certificate::CreateCertificateListFromBytes(cert_data.data(), |
| 26 | cert_data.size(), |
| 27 | format); |
| 28 | } |
| 29 | |
eroman | ce65aff | 2017-02-04 00:05:32 | [diff] [blame] | 30 | ::testing::AssertionResult LoadCertificateFiles( |
| 31 | const std::vector<std::string>& cert_filenames, |
| 32 | CertificateList* certs) { |
| 33 | certs->clear(); |
| 34 | for (const std::string& filename : cert_filenames) { |
| 35 | scoped_refptr<X509Certificate> cert = CreateCertificateChainFromFile( |
| 36 | GetTestCertsDirectory(), filename, X509Certificate::FORMAT_AUTO); |
| 37 | if (!cert) |
| 38 | return ::testing::AssertionFailure() |
| 39 | << "Failed loading certificate from file: " << filename |
| 40 | << " (in directory: " << GetTestCertsDirectory().value() << ")"; |
| 41 | certs->push_back(cert); |
| 42 | } |
| 43 | |
| 44 | return ::testing::AssertionSuccess(); |
| 45 | } |
| 46 | |
[email protected] | 1f11d6f | 2013-11-24 22:33:00 | [diff] [blame] | 47 | scoped_refptr<X509Certificate> CreateCertificateChainFromFile( |
| 48 | const base::FilePath& certs_dir, |
| 49 | const std::string& cert_file, |
| 50 | int format) { |
| 51 | CertificateList certs = CreateCertificateListFromFile( |
| 52 | certs_dir, cert_file, format); |
| 53 | if (certs.empty()) |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 54 | return nullptr; |
[email protected] | 1f11d6f | 2013-11-24 22:33:00 | [diff] [blame] | 55 | |
Matt Mueller | a419327 | 2017-12-07 00:23:34 | [diff] [blame] | 56 | std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates; |
[email protected] | 1f11d6f | 2013-11-24 22:33:00 | [diff] [blame] | 57 | for (size_t i = 1; i < certs.size(); ++i) |
David Benjamin | 4db85cf | 2018-07-10 16:10:04 | [diff] [blame] | 58 | intermediates.push_back(bssl::UpRef(certs[i]->cert_buffer())); |
[email protected] | 1f11d6f | 2013-11-24 22:33:00 | [diff] [blame] | 59 | |
Matt Mueller | a419327 | 2017-12-07 00:23:34 | [diff] [blame] | 60 | scoped_refptr<X509Certificate> result(X509Certificate::CreateFromBuffer( |
David Benjamin | 4db85cf | 2018-07-10 16:10:04 | [diff] [blame] | 61 | bssl::UpRef(certs[0]->cert_buffer()), std::move(intermediates))); |
[email protected] | 1f11d6f | 2013-11-24 22:33:00 | [diff] [blame] | 62 | return result; |
| 63 | } |
| 64 | |
[email protected] | 32765f8 | 2010-12-16 00:01:37 | [diff] [blame] | 65 | scoped_refptr<X509Certificate> ImportCertFromFile( |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 66 | const base::FilePath& certs_dir, |
[email protected] | 32765f8 | 2010-12-16 00:01:37 | [diff] [blame] | 67 | const std::string& cert_file) { |
Francois Doray | e6fb2d0 | 2017-10-18 21:29:13 | [diff] [blame] | 68 | base::ScopedAllowBlockingForTesting allow_blocking; |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 69 | base::FilePath cert_path = certs_dir.AppendASCII(cert_file); |
[email protected] | 32765f8 | 2010-12-16 00:01:37 | [diff] [blame] | 70 | std::string cert_data; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 71 | if (!base::ReadFileToString(cert_path, &cert_data)) |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 72 | return nullptr; |
[email protected] | 1e5fead | 2010-10-08 14:33:11 | [diff] [blame] | 73 | |
[email protected] | 32765f8 | 2010-12-16 00:01:37 | [diff] [blame] | 74 | CertificateList certs_in_file = |
| 75 | X509Certificate::CreateCertificateListFromBytes( |
| 76 | cert_data.data(), cert_data.size(), X509Certificate::FORMAT_AUTO); |
| 77 | if (certs_in_file.empty()) |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 78 | return nullptr; |
[email protected] | 32765f8 | 2010-12-16 00:01:37 | [diff] [blame] | 79 | return certs_in_file[0]; |
[email protected] | 1e5fead | 2010-10-08 14:33:11 | [diff] [blame] | 80 | } |
[email protected] | c81d9dcc | 2010-03-17 00:51:44 | [diff] [blame] | 81 | |
[email protected] | 7d015e4 | 2012-03-14 16:15:12 | [diff] [blame] | 82 | ScopedTestEVPolicy::ScopedTestEVPolicy(EVRootCAMetadata* ev_root_ca_metadata, |
David Benjamin | 9cedc3a5 | 2017-08-20 21:30:58 | [diff] [blame] | 83 | const SHA256HashValue& fingerprint, |
[email protected] | 7d015e4 | 2012-03-14 16:15:12 | [diff] [blame] | 84 | const char* policy) |
David Benjamin | 9cedc3a5 | 2017-08-20 21:30:58 | [diff] [blame] | 85 | : fingerprint_(fingerprint), ev_root_ca_metadata_(ev_root_ca_metadata) { |
[email protected] | 7d015e4 | 2012-03-14 16:15:12 | [diff] [blame] | 86 | EXPECT_TRUE(ev_root_ca_metadata->AddEVCA(fingerprint, policy)); |
| 87 | } |
| 88 | |
| 89 | ScopedTestEVPolicy::~ScopedTestEVPolicy() { |
| 90 | EXPECT_TRUE(ev_root_ca_metadata_->RemoveEVCA(fingerprint_)); |
| 91 | } |
| 92 | |
[email protected] | c81d9dcc | 2010-03-17 00:51:44 | [diff] [blame] | 93 | } // namespace net |