• 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/x509_certificate.h"
12 #include "net/cert/x509_util.h"
13 #include "net/test/test_data_directory.h"
14 #include "third_party/boringssl/src/include/openssl/bytestring.h"
15 #include "third_party/boringssl/src/include/openssl/evp.h"
16 
17 namespace net {
18 
CreateCertificateListFromFile(const base::FilePath & certs_dir,base::StringPiece cert_file,int format)19 CertificateList CreateCertificateListFromFile(const base::FilePath& certs_dir,
20                                               base::StringPiece cert_file,
21                                               int format) {
22   base::FilePath cert_path = certs_dir.AppendASCII(cert_file);
23   std::string cert_data;
24   if (!base::ReadFileToString(cert_path, &cert_data))
25     return CertificateList();
26   return X509Certificate::CreateCertificateListFromBytes(
27       base::as_bytes(base::make_span(cert_data)), format);
28 }
29 
LoadCertificateFiles(const std::vector<std::string> & cert_filenames,CertificateList * certs)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 
CreateCertificateChainFromFile(const base::FilePath & certs_dir,base::StringPiece cert_file,int format)47 scoped_refptr<X509Certificate> CreateCertificateChainFromFile(
48     const base::FilePath& certs_dir,
49     base::StringPiece cert_file,
50     int format) {
51   CertificateList certs = CreateCertificateListFromFile(
52       certs_dir, cert_file, format);
53   if (certs.empty())
54     return nullptr;
55 
56   std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates;
57   for (size_t i = 1; i < certs.size(); ++i)
58     intermediates.push_back(bssl::UpRef(certs[i]->cert_buffer()));
59 
60   scoped_refptr<X509Certificate> result(X509Certificate::CreateFromBuffer(
61       bssl::UpRef(certs[0]->cert_buffer()), std::move(intermediates)));
62   return result;
63 }
64 
ImportCertFromFile(const base::FilePath & cert_path)65 scoped_refptr<X509Certificate> ImportCertFromFile(
66     const base::FilePath& cert_path) {
67   base::ScopedAllowBlockingForTesting allow_blocking;
68   std::string cert_data;
69   if (!base::ReadFileToString(cert_path, &cert_data))
70     return nullptr;
71 
72   CertificateList certs_in_file =
73       X509Certificate::CreateCertificateListFromBytes(
74           base::as_bytes(base::make_span(cert_data)),
75           X509Certificate::FORMAT_AUTO);
76   if (certs_in_file.empty())
77     return nullptr;
78   return certs_in_file[0];
79 }
80 
ImportCertFromFile(const base::FilePath & certs_dir,base::StringPiece cert_file)81 scoped_refptr<X509Certificate> ImportCertFromFile(
82     const base::FilePath& certs_dir,
83     base::StringPiece cert_file) {
84   return ImportCertFromFile(certs_dir.AppendASCII(cert_file));
85 }
86 
ScopedTestEVPolicy(EVRootCAMetadata * ev_root_ca_metadata,const SHA256HashValue & fingerprint,const char * policy)87 ScopedTestEVPolicy::ScopedTestEVPolicy(EVRootCAMetadata* ev_root_ca_metadata,
88                                        const SHA256HashValue& fingerprint,
89                                        const char* policy)
90     : fingerprint_(fingerprint), ev_root_ca_metadata_(ev_root_ca_metadata) {
91   EXPECT_TRUE(ev_root_ca_metadata->AddEVCA(fingerprint, policy));
92 }
93 
~ScopedTestEVPolicy()94 ScopedTestEVPolicy::~ScopedTestEVPolicy() {
95   EXPECT_TRUE(ev_root_ca_metadata_->RemoveEVCA(fingerprint_));
96 }
97 
98 }  // namespace net
99