1 // Copyright 2013 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.
5 #include "net/quic/test_tools/crypto_test_utils.h"
7 #include "base/memory/ref_counted.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "net/base/net_errors.h"
10 #include "net/base/test_data_directory.h"
11 #include "net/cert/cert_verifier.h"
12 #include "net/cert/mock_cert_verifier.h"
13 #include "net/cert/test_root_certs.h"
14 #include "net/cert/x509_certificate.h"
15 #include "net/http/transport_security_state.h"
16 #include "net/quic/crypto/proof_source_chromium.h"
17 #include "net/quic/crypto/proof_verifier_chromium.h"
18 #include "net/test/cert_test_util.h"
26 class TestProofVerifierChromium
: public ProofVerifierChromium
{
28 // TODO(rch): |transport_security_state| should be a scoped_ptr.
29 TestProofVerifierChromium(CertVerifier
* cert_verifier
,
30 TransportSecurityState
* transport_security_state
,
31 const std::string
& cert_file
)
32 : ProofVerifierChromium(cert_verifier
, nullptr, transport_security_state
),
33 cert_verifier_(cert_verifier
),
34 transport_security_state_(transport_security_state
) {
35 // Load and install the root for the validated chain.
36 scoped_refptr
<X509Certificate
> root_cert
=
37 ImportCertFromFile(GetTestCertsDirectory(), cert_file
);
38 scoped_root_
.Reset(root_cert
.get());
40 ~TestProofVerifierChromium() override
{}
43 ScopedTestRoot scoped_root_
;
44 scoped_ptr
<CertVerifier
> cert_verifier_
;
45 scoped_ptr
<TransportSecurityState
> transport_security_state_
;
48 const char kLeafCert
[] = "leaf";
49 const char kIntermediateCert
[] = "intermediate";
50 const char kSignature
[] = "signature";
52 class FakeProofSource
: public ProofSource
{
54 FakeProofSource() : certs_(2) {
55 certs_
[0] = kLeafCert
;
56 certs_
[1] = kIntermediateCert
;
58 ~FakeProofSource() override
{}
60 // ProofSource interface
61 bool GetProof(const IPAddressNumber
& server_ip
,
62 const std::string
& hostname
,
63 const std::string
& server_config
,
65 const std::vector
<std::string
>** out_certs
,
66 std::string
* out_signature
) override
{
68 *out_signature
= kSignature
;
73 std::vector
<std::string
> certs_
;
74 DISALLOW_COPY_AND_ASSIGN(FakeProofSource
);
77 class FakeProofVerifier
: public ProofVerifier
{
79 FakeProofVerifier() {}
80 ~FakeProofVerifier() override
{}
82 // ProofVerifier interface
83 QuicAsyncStatus
VerifyProof(const std::string
& hostname
,
84 const std::string
& server_config
,
85 const std::vector
<std::string
>& certs
,
86 const std::string
& signature
,
87 const ProofVerifyContext
* verify_context
,
88 std::string
* error_details
,
89 scoped_ptr
<ProofVerifyDetails
>* verify_details
,
90 ProofVerifierCallback
* callback
) override
{
91 error_details
->clear();
92 scoped_ptr
<ProofVerifyDetailsChromium
> verify_details_chromium(
93 new ProofVerifyDetailsChromium
);
94 if (certs
.size() != 2 || certs
[0] != kLeafCert
||
95 certs
[1] != kIntermediateCert
|| signature
!= kSignature
) {
96 *error_details
= "Invalid proof";
97 verify_details_chromium
->cert_verify_result
.cert_status
=
99 *verify_details
= verify_details_chromium
.Pass();
102 *verify_details
= verify_details_chromium
.Pass();
107 DISALLOW_COPY_AND_ASSIGN(FakeProofVerifier
);
113 ProofSource
* CryptoTestUtils::ProofSourceForTesting() {
114 ProofSourceChromium
* source
= new ProofSourceChromium();
115 base::FilePath certs_dir
= GetTestCertsDirectory();
116 CHECK(source
->Initialize(
117 certs_dir
.AppendASCII("quic_chain.crt"),
118 certs_dir
.AppendASCII("quic_test.example.com.key.pkcs8")));
123 ProofVerifier
* CryptoTestUtils::ProofVerifierForTesting() {
124 // TODO(rch): use a real cert verifier?
125 MockCertVerifier
* cert_verifier
= new MockCertVerifier();
126 net::CertVerifyResult verify_result
;
127 verify_result
.verified_cert
=
128 ImportCertFromFile(GetTestCertsDirectory(), "quic_test.example.com.crt");
129 cert_verifier
->AddResultForCertAndHost(verify_result
.verified_cert
.get(),
130 "test.example.com", verify_result
, OK
);
131 verify_result
.verified_cert
= ImportCertFromFile(
132 GetTestCertsDirectory(), "quic_test_ecc.example.com.crt");
133 cert_verifier
->AddResultForCertAndHost(verify_result
.verified_cert
.get(),
134 "test.example.com", verify_result
, OK
);
135 return new TestProofVerifierChromium(
136 cert_verifier
, new TransportSecurityState
, "quic_root.crt");
140 ProofVerifyContext
* CryptoTestUtils::ProofVerifyContextForTesting() {
141 return new ProofVerifyContextChromium(/*cert_verify_flags=*/0, BoundNetLog());
145 ProofSource
* CryptoTestUtils::FakeProofSourceForTesting() {
146 return new FakeProofSource();
150 ProofVerifier
* CryptoTestUtils::FakeProofVerifierForTesting() {
151 return new FakeProofVerifier();
155 ProofVerifyContext
* CryptoTestUtils::FakeProofVerifyContextForTesting() {