cert_test_util.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. #include "net/test/cert_test_util.h"
  5. #include "base/files/file_path.h"
  6. #include "base/files/file_util.h"
  7. #include "base/threading/thread_restrictions.h"
  8. #include "net/cert/ev_root_ca_metadata.h"
  9. #include "net/cert/pem.h"
  10. #include "net/cert/x509_certificate.h"
  11. #include "net/cert/x509_util.h"
  12. #include "net/test/test_data_directory.h"
  13. #include "third_party/boringssl/src/include/openssl/bytestring.h"
  14. #include "third_party/boringssl/src/include/openssl/evp.h"
  15. namespace net {
  16. CertificateList CreateCertificateListFromFile(const base::FilePath& certs_dir,
  17. base::StringPiece cert_file,
  18. int format) {
  19. base::FilePath cert_path = certs_dir.AppendASCII(cert_file);
  20. std::string cert_data;
  21. if (!base::ReadFileToString(cert_path, &cert_data))
  22. return CertificateList();
  23. return X509Certificate::CreateCertificateListFromBytes(
  24. base::as_bytes(base::make_span(cert_data)), format);
  25. }
  26. ::testing::AssertionResult LoadCertificateFiles(
  27. const std::vector<std::string>& cert_filenames,
  28. CertificateList* certs) {
  29. certs->clear();
  30. for (const std::string& filename : cert_filenames) {
  31. scoped_refptr<X509Certificate> cert = CreateCertificateChainFromFile(
  32. GetTestCertsDirectory(), filename, X509Certificate::FORMAT_AUTO);
  33. if (!cert)
  34. return ::testing::AssertionFailure()
  35. << "Failed loading certificate from file: " << filename
  36. << " (in directory: " << GetTestCertsDirectory().value() << ")";
  37. certs->push_back(cert);
  38. }
  39. return ::testing::AssertionSuccess();
  40. }
  41. scoped_refptr<X509Certificate> CreateCertificateChainFromFile(
  42. const base::FilePath& certs_dir,
  43. base::StringPiece cert_file,
  44. int format) {
  45. CertificateList certs = CreateCertificateListFromFile(
  46. certs_dir, cert_file, format);
  47. if (certs.empty())
  48. return nullptr;
  49. std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates;
  50. for (size_t i = 1; i < certs.size(); ++i)
  51. intermediates.push_back(bssl::UpRef(certs[i]->cert_buffer()));
  52. scoped_refptr<X509Certificate> result(X509Certificate::CreateFromBuffer(
  53. bssl::UpRef(certs[0]->cert_buffer()), std::move(intermediates)));
  54. return result;
  55. }
  56. scoped_refptr<X509Certificate> ImportCertFromFile(
  57. const base::FilePath& cert_path) {
  58. base::ScopedAllowBlockingForTesting allow_blocking;
  59. std::string cert_data;
  60. if (!base::ReadFileToString(cert_path, &cert_data))
  61. return nullptr;
  62. CertificateList certs_in_file =
  63. X509Certificate::CreateCertificateListFromBytes(
  64. base::as_bytes(base::make_span(cert_data)),
  65. X509Certificate::FORMAT_AUTO);
  66. if (certs_in_file.empty())
  67. return nullptr;
  68. return certs_in_file[0];
  69. }
  70. scoped_refptr<X509Certificate> ImportCertFromFile(
  71. const base::FilePath& certs_dir,
  72. base::StringPiece cert_file) {
  73. return ImportCertFromFile(certs_dir.AppendASCII(cert_file));
  74. }
  75. bssl::UniquePtr<EVP_PKEY> LoadPrivateKeyFromFile(
  76. const base::FilePath& key_path) {
  77. std::string key_string;
  78. if (!base::ReadFileToString(key_path, &key_string))
  79. return nullptr;
  80. std::vector<std::string> headers;
  81. headers.push_back("PRIVATE KEY");
  82. PEMTokenizer pem_tokenizer(key_string, headers);
  83. if (!pem_tokenizer.GetNext())
  84. return nullptr;
  85. CBS cbs;
  86. CBS_init(&cbs, reinterpret_cast<const uint8_t*>(pem_tokenizer.data().data()),
  87. pem_tokenizer.data().size());
  88. return bssl::UniquePtr<EVP_PKEY>(EVP_parse_private_key(&cbs));
  89. }
  90. ScopedTestEVPolicy::ScopedTestEVPolicy(EVRootCAMetadata* ev_root_ca_metadata,
  91. const SHA256HashValue& fingerprint,
  92. const char* policy)
  93. : fingerprint_(fingerprint), ev_root_ca_metadata_(ev_root_ca_metadata) {
  94. EXPECT_TRUE(ev_root_ca_metadata->AddEVCA(fingerprint, policy));
  95. }
  96. ScopedTestEVPolicy::~ScopedTestEVPolicy() {
  97. EXPECT_TRUE(ev_root_ca_metadata_->RemoveEVCA(fingerprint_));
  98. }
  99. } // namespace net