blob: 415d685b4140015e7d78f4bc62f8e1c6b75fe996 [file] [log] [blame]
[email protected]e54d0af2012-03-03 01:07:151// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]c81d9dcc2010-03-17 00:51:442// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]6e7845ae2013-03-29 21:48:115#include "net/test/cert_test_util.h"
[email protected]c81d9dcc2010-03-17 00:51:446
[email protected]57999812013-02-24 05:40:527#include "base/files/file_path.h"
thestigd8df0332014-09-04 06:33:298#include "base/files/file_util.h"
jam3f2d3932017-04-26 20:28:519#include "base/threading/thread_restrictions.h"
[email protected]6e7845ae2013-03-29 21:48:1110#include "net/cert/ev_root_ca_metadata.h"
11#include "net/cert/x509_certificate.h"
Matt Muellera4193272017-12-07 00:23:3412#include "net/cert/x509_util.h"
eromance65aff2017-02-04 00:05:3213#include "net/test/test_data_directory.h"
[email protected]c81d9dcc2010-03-17 00:51:4414
15namespace net {
16
[email protected]e54d0af2012-03-03 01:07:1517CertificateList CreateCertificateListFromFile(
[email protected]6cdfd7f2013-02-08 20:40:1518 const base::FilePath& certs_dir,
[email protected]e54d0af2012-03-03 01:07:1519 const std::string& cert_file,
20 int format) {
[email protected]6cdfd7f2013-02-08 20:40:1521 base::FilePath cert_path = certs_dir.AppendASCII(cert_file);
[email protected]e54d0af2012-03-03 01:07:1522 std::string cert_data;
[email protected]82f84b92013-08-30 18:23:5023 if (!base::ReadFileToString(cert_path, &cert_data))
[email protected]e54d0af2012-03-03 01:07:1524 return CertificateList();
25 return X509Certificate::CreateCertificateListFromBytes(cert_data.data(),
26 cert_data.size(),
27 format);
28}
29
eromance65aff2017-02-04 00:05:3230::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]1f11d6f2013-11-24 22:33:0047scoped_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 Tambre94493c652019-03-11 17:18:3554 return nullptr;
[email protected]1f11d6f2013-11-24 22:33:0055
Matt Muellera4193272017-12-07 00:23:3456 std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates;
[email protected]1f11d6f2013-11-24 22:33:0057 for (size_t i = 1; i < certs.size(); ++i)
David Benjamin4db85cf2018-07-10 16:10:0458 intermediates.push_back(bssl::UpRef(certs[i]->cert_buffer()));
[email protected]1f11d6f2013-11-24 22:33:0059
Matt Muellera4193272017-12-07 00:23:3460 scoped_refptr<X509Certificate> result(X509Certificate::CreateFromBuffer(
David Benjamin4db85cf2018-07-10 16:10:0461 bssl::UpRef(certs[0]->cert_buffer()), std::move(intermediates)));
[email protected]1f11d6f2013-11-24 22:33:0062 return result;
63}
64
[email protected]32765f82010-12-16 00:01:3765scoped_refptr<X509Certificate> ImportCertFromFile(
[email protected]6cdfd7f2013-02-08 20:40:1566 const base::FilePath& certs_dir,
[email protected]32765f82010-12-16 00:01:3767 const std::string& cert_file) {
Francois Doraye6fb2d02017-10-18 21:29:1368 base::ScopedAllowBlockingForTesting allow_blocking;
[email protected]6cdfd7f2013-02-08 20:40:1569 base::FilePath cert_path = certs_dir.AppendASCII(cert_file);
[email protected]32765f82010-12-16 00:01:3770 std::string cert_data;
[email protected]82f84b92013-08-30 18:23:5071 if (!base::ReadFileToString(cert_path, &cert_data))
Raul Tambre94493c652019-03-11 17:18:3572 return nullptr;
[email protected]1e5fead2010-10-08 14:33:1173
[email protected]32765f82010-12-16 00:01:3774 CertificateList certs_in_file =
75 X509Certificate::CreateCertificateListFromBytes(
76 cert_data.data(), cert_data.size(), X509Certificate::FORMAT_AUTO);
77 if (certs_in_file.empty())
Raul Tambre94493c652019-03-11 17:18:3578 return nullptr;
[email protected]32765f82010-12-16 00:01:3779 return certs_in_file[0];
[email protected]1e5fead2010-10-08 14:33:1180}
[email protected]c81d9dcc2010-03-17 00:51:4481
[email protected]7d015e42012-03-14 16:15:1282ScopedTestEVPolicy::ScopedTestEVPolicy(EVRootCAMetadata* ev_root_ca_metadata,
David Benjamin9cedc3a52017-08-20 21:30:5883 const SHA256HashValue& fingerprint,
[email protected]7d015e42012-03-14 16:15:1284 const char* policy)
David Benjamin9cedc3a52017-08-20 21:30:5885 : fingerprint_(fingerprint), ev_root_ca_metadata_(ev_root_ca_metadata) {
[email protected]7d015e42012-03-14 16:15:1286 EXPECT_TRUE(ev_root_ca_metadata->AddEVCA(fingerprint, policy));
87}
88
89ScopedTestEVPolicy::~ScopedTestEVPolicy() {
90 EXPECT_TRUE(ev_root_ca_metadata_->RemoveEVCA(fingerprint_));
91}
92
[email protected]c81d9dcc2010-03-17 00:51:4493} // namespace net