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/test_data_directory.h"
10 #include "net/cert/cert_verifier.h"
11 #include "net/cert/test_root_certs.h"
12 #include "net/cert/x509_certificate.h"
13 #include "net/http/transport_security_state.h"
14 #include "net/quic/crypto/proof_source_chromium.h"
15 #include "net/quic/crypto/proof_verifier_chromium.h"
16 #include "net/test/cert_test_util.h"
24 class TestProofVerifierChromium
: public ProofVerifierChromium
{
26 TestProofVerifierChromium(CertVerifier
* cert_verifier
,
27 TransportSecurityState
* transport_security_state
,
28 const std::string
& cert_file
)
29 : ProofVerifierChromium(cert_verifier
, transport_security_state
),
30 cert_verifier_(cert_verifier
),
31 transport_security_state_(transport_security_state
) {
32 // Load and install the root for the validated chain.
33 scoped_refptr
<X509Certificate
> root_cert
=
34 ImportCertFromFile(GetTestCertsDirectory(), cert_file
);
35 scoped_root_
.Reset(root_cert
.get());
37 ~TestProofVerifierChromium() override
{}
40 ScopedTestRoot scoped_root_
;
41 scoped_ptr
<CertVerifier
> cert_verifier_
;
42 scoped_ptr
<TransportSecurityState
> transport_security_state_
;
45 const char kLeafCert
[] = "leaf";
46 const char kIntermediateCert
[] = "intermediate";
47 const char kSignature
[] = "signature";
49 class FakeProofSource
: public ProofSource
{
51 FakeProofSource() : certs_(2) {
52 certs_
[0] = kLeafCert
;
53 certs_
[1] = kIntermediateCert
;
55 ~FakeProofSource() override
{}
57 // ProofSource interface
58 bool GetProof(const IPAddressNumber
& server_ip
,
59 const std::string
& hostname
,
60 const std::string
& server_config
,
62 const std::vector
<std::string
>** out_certs
,
63 std::string
* out_signature
) override
{
65 *out_signature
= kSignature
;
70 std::vector
<std::string
> certs_
;
71 DISALLOW_COPY_AND_ASSIGN(FakeProofSource
);
74 class FakeProofVerifier
: public ProofVerifier
{
76 FakeProofVerifier() {}
77 ~FakeProofVerifier() override
{}
79 // ProofVerifier interface
80 QuicAsyncStatus
VerifyProof(const std::string
& hostname
,
81 const std::string
& server_config
,
82 const std::vector
<std::string
>& certs
,
83 const std::string
& signature
,
84 const ProofVerifyContext
* verify_context
,
85 std::string
* error_details
,
86 scoped_ptr
<ProofVerifyDetails
>* verify_details
,
87 ProofVerifierCallback
* callback
) override
{
88 error_details
->clear();
89 scoped_ptr
<ProofVerifyDetailsChromium
> verify_details_chromium(
90 new ProofVerifyDetailsChromium
);
91 if (certs
.size() != 2 || certs
[0] != kLeafCert
||
92 certs
[1] != kIntermediateCert
|| signature
!= kSignature
) {
93 *error_details
= "Invalid proof";
94 verify_details_chromium
->cert_verify_result
.cert_status
=
96 *verify_details
= verify_details_chromium
.Pass();
99 *verify_details
= verify_details_chromium
.Pass();
104 DISALLOW_COPY_AND_ASSIGN(FakeProofVerifier
);
110 ProofSource
* CryptoTestUtils::ProofSourceForTesting() {
111 return new ProofSourceChromium();
115 ProofVerifier
* CryptoTestUtils::ProofVerifierForTesting() {
116 TestProofVerifierChromium
* proof_verifier
=
117 new TestProofVerifierChromium(CertVerifier::CreateDefault(),
118 new TransportSecurityState
,
120 return proof_verifier
;
124 ProofVerifyContext
* CryptoTestUtils::ProofVerifyContextForTesting() {
125 return new ProofVerifyContextChromium(BoundNetLog());
129 ProofSource
* CryptoTestUtils::FakeProofSourceForTesting() {
130 return new FakeProofSource();
134 ProofVerifier
* CryptoTestUtils::FakeProofVerifierForTesting() {
135 return new FakeProofVerifier();
139 ProofVerifyContext
* CryptoTestUtils::FakeProofVerifyContextForTesting() {