• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/test/cert_test_util.h"
6 
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/threading/thread_restrictions.h"
10 #include "net/cert/ev_root_ca_metadata.h"
11 #include "net/cert/pem.h"
12 #include "net/cert/x509_certificate.h"
13 #include "net/cert/x509_util.h"
14 #include "net/test/test_data_directory.h"
15 #include "third_party/boringssl/src/include/openssl/bytestring.h"
16 #include "third_party/boringssl/src/include/openssl/evp.h"
17 
18 namespace net {
19 
CreateCertificateListFromFile(const base::FilePath & certs_dir,base::StringPiece cert_file,int format)20 CertificateList CreateCertificateListFromFile(const base::FilePath& certs_dir,
21                                               base::StringPiece cert_file,
22                                               int format) {
23   base::FilePath cert_path = certs_dir.AppendASCII(cert_file);
24   std::string cert_data;
25   if (!base::ReadFileToString(cert_path, &cert_data))
26     return CertificateList();
27   return X509Certificate::CreateCertificateListFromBytes(
28       base::as_bytes(base::make_span(cert_data)), format);
29 }
30 
LoadCertificateFiles(const std::vector<std::string> & cert_filenames,CertificateList * certs)31 ::testing::AssertionResult LoadCertificateFiles(
32     const std::vector<std::string>& cert_filenames,
33     CertificateList* certs) {
34   certs->clear();
35   for (const std::string& filename : cert_filenames) {
36     scoped_refptr<X509Certificate> cert = CreateCertificateChainFromFile(
37         GetTestCertsDirectory(), filename, X509Certificate::FORMAT_AUTO);
38     if (!cert)
39       return ::testing::AssertionFailure()
40              << "Failed loading certificate from file: " << filename
41              << " (in directory: " << GetTestCertsDirectory().value() << ")";
42     certs->push_back(cert);
43   }
44 
45   return ::testing::AssertionSuccess();
46 }
47 
CreateCertificateChainFromFile(const base::FilePath & certs_dir,base::StringPiece cert_file,int format)48 scoped_refptr<X509Certificate> CreateCertificateChainFromFile(
49     const base::FilePath& certs_dir,
50     base::StringPiece cert_file,
51     int format) {
52   CertificateList certs = CreateCertificateListFromFile(
53       certs_dir, cert_file, format);
54   if (certs.empty())
55     return nullptr;
56 
57   std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates;
58   for (size_t i = 1; i < certs.size(); ++i)
59     intermediates.push_back(bssl::UpRef(certs[i]->cert_buffer()));
60 
61   scoped_refptr<X509Certificate> result(X509Certificate::CreateFromBuffer(
62       bssl::UpRef(certs[0]->cert_buffer()), std::move(intermediates)));
63   return result;
64 }
65 
ImportCertFromFile(const base::FilePath & cert_path)66 scoped_refptr<X509Certificate> ImportCertFromFile(
67     const base::FilePath& cert_path) {
68   base::ScopedAllowBlockingForTesting allow_blocking;
69   std::string cert_data;
70   if (!base::ReadFileToString(cert_path, &cert_data))
71     return nullptr;
72 
73   CertificateList certs_in_file =
74       X509Certificate::CreateCertificateListFromBytes(
75           base::as_bytes(base::make_span(cert_data)),
76           X509Certificate::FORMAT_AUTO);
77   if (certs_in_file.empty())
78     return nullptr;
79   return certs_in_file[0];
80 }
81 
ImportCertFromFile(const base::FilePath & certs_dir,base::StringPiece cert_file)82 scoped_refptr<X509Certificate> ImportCertFromFile(
83     const base::FilePath& certs_dir,
84     base::StringPiece cert_file) {
85   return ImportCertFromFile(certs_dir.AppendASCII(cert_file));
86 }
87 
ScopedTestEVPolicy(EVRootCAMetadata * ev_root_ca_metadata,const SHA256HashValue & fingerprint,const char * policy)88 ScopedTestEVPolicy::ScopedTestEVPolicy(EVRootCAMetadata* ev_root_ca_metadata,
89                                        const SHA256HashValue& fingerprint,
90                                        const char* policy)
91     : fingerprint_(fingerprint), ev_root_ca_metadata_(ev_root_ca_metadata) {
92   EXPECT_TRUE(ev_root_ca_metadata->AddEVCA(fingerprint, policy));
93 }
94 
~ScopedTestEVPolicy()95 ScopedTestEVPolicy::~ScopedTestEVPolicy() {
96   EXPECT_TRUE(ev_root_ca_metadata_->RemoveEVCA(fingerprint_));
97 }
98 
99 }  // namespace net
100