blob: cba474735eb287c9857c08e35606347dac2da930 [file] [log] [blame]
[email protected]e54d0af2012-03-03 01:07:151// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]c81d9dcc2010-03-17 00:51:442// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]6e7845ae2013-03-29 21:48:115#ifndef NET_TEST_CERT_TEST_UTIL_H_
6#define NET_TEST_CERT_TEST_UTIL_H_
[email protected]c81d9dcc2010-03-17 00:51:447
[email protected]32765f82010-12-16 00:01:378#include <string>
9
[email protected]3b63f8f42011-03-28 01:54:1510#include "base/memory/ref_counted.h"
[email protected]6e7845ae2013-03-29 21:48:1111#include "net/cert/x509_cert_types.h"
12#include "net/cert/x509_certificate.h"
eromance65aff2017-02-04 00:05:3213#include "testing/gtest/include/gtest/gtest.h"
[email protected]c81d9dcc2010-03-17 00:51:4414
davidben71f35ff2015-04-17 20:54:4815#if defined(USE_NSS_CERTS)
Matt Mueller381b1772017-08-18 06:22:5816#include "net/cert/scoped_nss_types.h"
17
[email protected]83e1ae32014-07-18 10:57:0718// From <pk11pub.h>
19typedef struct PK11SlotInfoStr PK11SlotInfo;
Matt Mueller947a0b82017-08-18 01:43:1620
21#include "net/cert/scoped_nss_types.h"
[email protected]83e1ae32014-07-18 10:57:0722#endif
23
[email protected]a3ef4832013-02-02 05:12:3324namespace base {
[email protected]864b1362010-08-19 03:49:3825class FilePath;
[email protected]a3ef4832013-02-02 05:12:3326}
[email protected]864b1362010-08-19 03:49:3827
[email protected]c81d9dcc2010-03-17 00:51:4428namespace net {
29
[email protected]7d015e42012-03-14 16:15:1230class EVRootCAMetadata;
31
davidben71f35ff2015-04-17 20:54:4832#if defined(USE_NSS_CERTS)
davidben85bad9e2015-05-11 20:20:1033// 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.
36bool ImportSensitiveKeyFromFile(const base::FilePath& dir,
37 const std::string& key_filename,
38 PK11SlotInfo* slot);
[email protected]cc9b29fb2014-08-02 11:52:2639
Matt Mueller947a0b82017-08-18 01:43:1640ScopedCERTCertificate ImportClientCertToSlot(
41 const scoped_refptr<X509Certificate>& cert,
42 PK11SlotInfo* slot);
[email protected]cc9b29fb2014-08-02 11:52:2643
44scoped_refptr<X509Certificate> ImportClientCertAndKeyFromFile(
45 const base::FilePath& dir,
46 const std::string& cert_filename,
47 const std::string& key_filename,
Matt Mueller947a0b82017-08-18 01:43:1648 PK11SlotInfo* slot,
49 ScopedCERTCertificate* nss_cert);
50scoped_refptr<X509Certificate> ImportClientCertAndKeyFromFile(
51 const base::FilePath& dir,
52 const std::string& cert_filename,
53 const std::string& key_filename,
[email protected]cc9b29fb2014-08-02 11:52:2654 PK11SlotInfo* slot);
Matt Mueller381b1772017-08-18 06:22:5855
56ScopedCERTCertificate ImportCERTCertificateFromFile(
57 const base::FilePath& certs_dir,
58 const std::string& cert_file);
59
60ScopedCERTCertificateList CreateCERTCertificateListFromFile(
61 const base::FilePath& certs_dir,
62 const std::string& cert_file,
63 int format);
[email protected]83e1ae32014-07-18 10:57:0764#endif
65
66// Imports all of the certificates in |cert_file|, a file in |certs_dir|, into a
67// CertificateList.
[email protected]a3ef4832013-02-02 05:12:3368CertificateList CreateCertificateListFromFile(const base::FilePath& certs_dir,
[email protected]e54d0af2012-03-03 01:07:1569 const std::string& cert_file,
70 int format);
71
eromance65aff2017-02-04 00:05:3272// Imports all the certificates given a list of filenames, and assigns the
73// result to |*certs|. The filenames are relative to the test certificates
74// directory.
75::testing::AssertionResult LoadCertificateFiles(
76 const std::vector<std::string>& cert_filenames,
77 CertificateList* certs);
78
[email protected]1f11d6f2013-11-24 22:33:0079// Imports all of the certificates in |cert_file|, a file in |certs_dir|, into
80// a new X509Certificate. The first certificate in the chain will be used for
81// the returned cert, with any additional certificates configured as
82// intermediate certificates.
83scoped_refptr<X509Certificate> CreateCertificateChainFromFile(
84 const base::FilePath& certs_dir,
85 const std::string& cert_file,
86 int format);
87
88// Imports a single certificate from |cert_file|.
[email protected]42fdb452012-11-01 12:44:4089// |certs_dir| represents the test certificates directory. |cert_file| is the
[email protected]32765f82010-12-16 00:01:3790// name of the certificate file. If cert_file contains multiple certificates,
91// the first certificate found will be returned.
[email protected]a3ef4832013-02-02 05:12:3392scoped_refptr<X509Certificate> ImportCertFromFile(const base::FilePath& certs_dir,
[email protected]32765f82010-12-16 00:01:3793 const std::string& cert_file);
[email protected]c81d9dcc2010-03-17 00:51:4494
[email protected]7d015e42012-03-14 16:15:1295// ScopedTestEVPolicy causes certificates marked with |policy|, issued from a
96// root with the given fingerprint, to be treated as EV. |policy| is expressed
97// as a string of dotted numbers: i.e. "1.2.3.4".
98// This should only be used in unittests as adding a CA twice causes a CHECK
99// failure.
100class ScopedTestEVPolicy {
101 public:
102 ScopedTestEVPolicy(EVRootCAMetadata* ev_root_ca_metadata,
David Benjamin9cedc3a52017-08-20 21:30:58103 const SHA256HashValue& fingerprint,
[email protected]7d015e42012-03-14 16:15:12104 const char* policy);
105 ~ScopedTestEVPolicy();
106
107 private:
David Benjamin9cedc3a52017-08-20 21:30:58108 SHA256HashValue fingerprint_;
[email protected]7d015e42012-03-14 16:15:12109 EVRootCAMetadata* const ev_root_ca_metadata_;
110};
111
[email protected]c81d9dcc2010-03-17 00:51:44112} // namespace net
113
[email protected]6e7845ae2013-03-29 21:48:11114#endif // NET_TEST_CERT_TEST_UTIL_H_