cert_test_util.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #ifndef NET_TEST_CERT_TEST_UTIL_H_
  5. #define NET_TEST_CERT_TEST_UTIL_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "base/strings/string_piece.h"
  11. #include "crypto/crypto_buildflags.h"
  12. #include "net/base/hash_value.h"
  13. #include "net/cert/x509_certificate.h"
  14. #include "testing/gtest/include/gtest/gtest.h"
  15. #if BUILDFLAG(USE_NSS_CERTS)
  16. #include "net/cert/scoped_nss_types.h"
  17. // From <pk11pub.h>
  18. typedef struct PK11SlotInfoStr PK11SlotInfo;
  19. #include "net/cert/scoped_nss_types.h"
  20. #endif
  21. namespace base {
  22. class FilePath;
  23. }
  24. namespace net {
  25. class EVRootCAMetadata;
  26. #if BUILDFLAG(USE_NSS_CERTS)
  27. // Imports a private key from file |key_filename| in |dir| into |slot|. The file
  28. // must contain a PKCS#8 PrivateKeyInfo in DER encoding. Returns true on success
  29. // and false on failure.
  30. bool ImportSensitiveKeyFromFile(const base::FilePath& dir,
  31. base::StringPiece key_filename,
  32. PK11SlotInfo* slot);
  33. bool ImportClientCertToSlot(CERTCertificate* cert, PK11SlotInfo* slot);
  34. ScopedCERTCertificate ImportClientCertToSlot(
  35. const scoped_refptr<X509Certificate>& cert,
  36. PK11SlotInfo* slot);
  37. scoped_refptr<X509Certificate> ImportClientCertAndKeyFromFile(
  38. const base::FilePath& dir,
  39. base::StringPiece cert_filename,
  40. base::StringPiece key_filename,
  41. PK11SlotInfo* slot,
  42. ScopedCERTCertificate* nss_cert);
  43. scoped_refptr<X509Certificate> ImportClientCertAndKeyFromFile(
  44. const base::FilePath& dir,
  45. base::StringPiece cert_filename,
  46. base::StringPiece key_filename,
  47. PK11SlotInfo* slot);
  48. ScopedCERTCertificate ImportCERTCertificateFromFile(
  49. const base::FilePath& certs_dir,
  50. base::StringPiece cert_file);
  51. ScopedCERTCertificateList CreateCERTCertificateListFromFile(
  52. const base::FilePath& certs_dir,
  53. base::StringPiece cert_file,
  54. int format);
  55. #endif
  56. // Imports all of the certificates in |cert_file|, a file in |certs_dir|, into a
  57. // CertificateList.
  58. CertificateList CreateCertificateListFromFile(const base::FilePath& certs_dir,
  59. base::StringPiece cert_file,
  60. int format);
  61. // Imports all the certificates given a list of filenames, and assigns the
  62. // result to |*certs|. The filenames are relative to the test certificates
  63. // directory.
  64. ::testing::AssertionResult LoadCertificateFiles(
  65. const std::vector<std::string>& cert_filenames,
  66. CertificateList* certs);
  67. // Imports all of the certificates in |cert_file|, a file in |certs_dir|, into
  68. // a new X509Certificate. The first certificate in the chain will be used for
  69. // the returned cert, with any additional certificates configured as
  70. // intermediate certificates.
  71. scoped_refptr<X509Certificate> CreateCertificateChainFromFile(
  72. const base::FilePath& certs_dir,
  73. base::StringPiece cert_file,
  74. int format);
  75. // Imports a single certificate from |cert_path|.
  76. // If the file contains multiple certificates, the first certificate found
  77. // will be returned.
  78. scoped_refptr<X509Certificate> ImportCertFromFile(
  79. const base::FilePath& cert_path);
  80. // Imports a single certificate from |cert_file|.
  81. // |certs_dir| represents the test certificates directory. |cert_file| is the
  82. // name of the certificate file. If cert_file contains multiple certificates,
  83. // the first certificate found will be returned.
  84. scoped_refptr<X509Certificate> ImportCertFromFile(
  85. const base::FilePath& certs_dir,
  86. base::StringPiece cert_file);
  87. // Imports a private key from |key_path|, which should be a PEM file containing
  88. // a PRIVATE KEY block. Only the first private key found will be returned, if
  89. // the file contains multiple private keys or other PEM blocks, they will be
  90. // ignored.
  91. bssl::UniquePtr<EVP_PKEY> LoadPrivateKeyFromFile(
  92. const base::FilePath& key_path);
  93. // ScopedTestEVPolicy causes certificates marked with |policy|, issued from a
  94. // root with the given fingerprint, to be treated as EV. |policy| is expressed
  95. // as a string of dotted numbers: i.e. "1.2.3.4".
  96. // This should only be used in unittests as adding a CA twice causes a CHECK
  97. // failure.
  98. class ScopedTestEVPolicy {
  99. public:
  100. ScopedTestEVPolicy(EVRootCAMetadata* ev_root_ca_metadata,
  101. const SHA256HashValue& fingerprint,
  102. const char* policy);
  103. ~ScopedTestEVPolicy();
  104. private:
  105. SHA256HashValue fingerprint_;
  106. const raw_ptr<EVRootCAMetadata> ev_root_ca_metadata_;
  107. };
  108. } // namespace net
  109. #endif // NET_TEST_CERT_TEST_UTIL_H_