[email protected] | e54d0af | 2012-03-03 01:07:15 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | c81d9dcc | 2010-03-17 00:51:44 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 5 | #ifndef NET_TEST_CERT_TEST_UTIL_H_ |
| 6 | #define NET_TEST_CERT_TEST_UTIL_H_ |
[email protected] | c81d9dcc | 2010-03-17 00:51:44 | [diff] [blame] | 7 | |
[email protected] | 32765f8 | 2010-12-16 00:01:37 | [diff] [blame] | 8 | #include <string> |
| 9 | |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 10 | #include "base/memory/ref_counted.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 11 | #include "net/cert/x509_cert_types.h" |
| 12 | #include "net/cert/x509_certificate.h" |
eroman | ce65aff | 2017-02-04 00:05:32 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | c81d9dcc | 2010-03-17 00:51:44 | [diff] [blame] | 14 | |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame] | 15 | #if defined(USE_NSS_CERTS) |
Matt Mueller | 381b177 | 2017-08-18 06:22:58 | [diff] [blame] | 16 | #include "net/cert/scoped_nss_types.h" |
| 17 | |
[email protected] | 83e1ae3 | 2014-07-18 10:57:07 | [diff] [blame] | 18 | // From <pk11pub.h> |
| 19 | typedef struct PK11SlotInfoStr PK11SlotInfo; |
Matt Mueller | 947a0b8 | 2017-08-18 01:43:16 | [diff] [blame] | 20 | |
| 21 | #include "net/cert/scoped_nss_types.h" |
[email protected] | 83e1ae3 | 2014-07-18 10:57:07 | [diff] [blame] | 22 | #endif |
| 23 | |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 24 | namespace base { |
[email protected] | 864b136 | 2010-08-19 03:49:38 | [diff] [blame] | 25 | class FilePath; |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 26 | } |
[email protected] | 864b136 | 2010-08-19 03:49:38 | [diff] [blame] | 27 | |
[email protected] | c81d9dcc | 2010-03-17 00:51:44 | [diff] [blame] | 28 | namespace net { |
| 29 | |
[email protected] | 7d015e4 | 2012-03-14 16:15:12 | [diff] [blame] | 30 | class EVRootCAMetadata; |
| 31 | |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame] | 32 | #if defined(USE_NSS_CERTS) |
davidben | 85bad9e | 2015-05-11 20:20:10 | [diff] [blame] | 33 | // Imports a private key from file |key_filename| in |dir| into |slot|. The file |
| 34 | // must contain a PKCS#8 PrivateKeyInfo in DER encoding. Returns true on success |
| 35 | // and false on failure. |
| 36 | bool ImportSensitiveKeyFromFile(const base::FilePath& dir, |
| 37 | const std::string& key_filename, |
| 38 | PK11SlotInfo* slot); |
[email protected] | cc9b29fb | 2014-08-02 11:52:26 | [diff] [blame] | 39 | |
Matt Mueller | 6c8b07c6 | 2017-10-09 21:02:25 | [diff] [blame] | 40 | bool ImportClientCertToSlot(CERTCertificate* cert, PK11SlotInfo* slot); |
| 41 | |
Matt Mueller | 947a0b8 | 2017-08-18 01:43:16 | [diff] [blame] | 42 | ScopedCERTCertificate ImportClientCertToSlot( |
| 43 | const scoped_refptr<X509Certificate>& cert, |
| 44 | PK11SlotInfo* slot); |
[email protected] | cc9b29fb | 2014-08-02 11:52:26 | [diff] [blame] | 45 | |
| 46 | scoped_refptr<X509Certificate> ImportClientCertAndKeyFromFile( |
| 47 | const base::FilePath& dir, |
| 48 | const std::string& cert_filename, |
| 49 | const std::string& key_filename, |
Matt Mueller | 947a0b8 | 2017-08-18 01:43:16 | [diff] [blame] | 50 | PK11SlotInfo* slot, |
| 51 | ScopedCERTCertificate* nss_cert); |
| 52 | scoped_refptr<X509Certificate> ImportClientCertAndKeyFromFile( |
| 53 | const base::FilePath& dir, |
| 54 | const std::string& cert_filename, |
| 55 | const std::string& key_filename, |
[email protected] | cc9b29fb | 2014-08-02 11:52:26 | [diff] [blame] | 56 | PK11SlotInfo* slot); |
Matt Mueller | 381b177 | 2017-08-18 06:22:58 | [diff] [blame] | 57 | |
| 58 | ScopedCERTCertificate ImportCERTCertificateFromFile( |
| 59 | const base::FilePath& certs_dir, |
| 60 | const std::string& cert_file); |
| 61 | |
| 62 | ScopedCERTCertificateList CreateCERTCertificateListFromFile( |
| 63 | const base::FilePath& certs_dir, |
| 64 | const std::string& cert_file, |
| 65 | int format); |
[email protected] | 83e1ae3 | 2014-07-18 10:57:07 | [diff] [blame] | 66 | #endif |
| 67 | |
| 68 | // Imports all of the certificates in |cert_file|, a file in |certs_dir|, into a |
| 69 | // CertificateList. |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 70 | CertificateList CreateCertificateListFromFile(const base::FilePath& certs_dir, |
[email protected] | e54d0af | 2012-03-03 01:07:15 | [diff] [blame] | 71 | const std::string& cert_file, |
| 72 | int format); |
| 73 | |
eroman | ce65aff | 2017-02-04 00:05:32 | [diff] [blame] | 74 | // Imports all the certificates given a list of filenames, and assigns the |
| 75 | // result to |*certs|. The filenames are relative to the test certificates |
| 76 | // directory. |
| 77 | ::testing::AssertionResult LoadCertificateFiles( |
| 78 | const std::vector<std::string>& cert_filenames, |
| 79 | CertificateList* certs); |
| 80 | |
[email protected] | 1f11d6f | 2013-11-24 22:33:00 | [diff] [blame] | 81 | // Imports all of the certificates in |cert_file|, a file in |certs_dir|, into |
| 82 | // a new X509Certificate. The first certificate in the chain will be used for |
| 83 | // the returned cert, with any additional certificates configured as |
| 84 | // intermediate certificates. |
| 85 | scoped_refptr<X509Certificate> CreateCertificateChainFromFile( |
| 86 | const base::FilePath& certs_dir, |
| 87 | const std::string& cert_file, |
| 88 | int format); |
| 89 | |
| 90 | // Imports a single certificate from |cert_file|. |
[email protected] | 42fdb45 | 2012-11-01 12:44:40 | [diff] [blame] | 91 | // |certs_dir| represents the test certificates directory. |cert_file| is the |
[email protected] | 32765f8 | 2010-12-16 00:01:37 | [diff] [blame] | 92 | // name of the certificate file. If cert_file contains multiple certificates, |
| 93 | // the first certificate found will be returned. |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 94 | scoped_refptr<X509Certificate> ImportCertFromFile(const base::FilePath& certs_dir, |
[email protected] | 32765f8 | 2010-12-16 00:01:37 | [diff] [blame] | 95 | const std::string& cert_file); |
[email protected] | c81d9dcc | 2010-03-17 00:51:44 | [diff] [blame] | 96 | |
[email protected] | 7d015e4 | 2012-03-14 16:15:12 | [diff] [blame] | 97 | // ScopedTestEVPolicy causes certificates marked with |policy|, issued from a |
| 98 | // root with the given fingerprint, to be treated as EV. |policy| is expressed |
| 99 | // as a string of dotted numbers: i.e. "1.2.3.4". |
| 100 | // This should only be used in unittests as adding a CA twice causes a CHECK |
| 101 | // failure. |
| 102 | class ScopedTestEVPolicy { |
| 103 | public: |
| 104 | ScopedTestEVPolicy(EVRootCAMetadata* ev_root_ca_metadata, |
David Benjamin | 9cedc3a5 | 2017-08-20 21:30:58 | [diff] [blame] | 105 | const SHA256HashValue& fingerprint, |
[email protected] | 7d015e4 | 2012-03-14 16:15:12 | [diff] [blame] | 106 | const char* policy); |
| 107 | ~ScopedTestEVPolicy(); |
| 108 | |
| 109 | private: |
David Benjamin | 9cedc3a5 | 2017-08-20 21:30:58 | [diff] [blame] | 110 | SHA256HashValue fingerprint_; |
[email protected] | 7d015e4 | 2012-03-14 16:15:12 | [diff] [blame] | 111 | EVRootCAMetadata* const ev_root_ca_metadata_; |
| 112 | }; |
| 113 | |
[email protected] | c81d9dcc | 2010-03-17 00:51:44 | [diff] [blame] | 114 | } // namespace net |
| 115 | |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 116 | #endif // NET_TEST_CERT_TEST_UTIL_H_ |