1 // Copyright (c) 2016 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
5 #include "quiche/quic/test_tools/fake_proof_source.h"
6
7 #include <utility>
8
9 #include "absl/strings/string_view.h"
10 #include "quiche/quic/platform/api/quic_logging.h"
11 #include "quiche/quic/test_tools/crypto_test_utils.h"
12
13 namespace quic {
14 namespace test {
15
FakeProofSource()16 FakeProofSource::FakeProofSource()
17 : delegate_(crypto_test_utils::ProofSourceForTesting()) {}
18
~FakeProofSource()19 FakeProofSource::~FakeProofSource() {}
20
21 FakeProofSource::PendingOp::~PendingOp() = default;
22
GetProofOp(const QuicSocketAddress & server_addr,const QuicSocketAddress & client_address,std::string hostname,std::string server_config,QuicTransportVersion transport_version,std::string chlo_hash,std::unique_ptr<ProofSource::Callback> callback,ProofSource * delegate)23 FakeProofSource::GetProofOp::GetProofOp(
24 const QuicSocketAddress& server_addr,
25 const QuicSocketAddress& client_address, std::string hostname,
26 std::string server_config, QuicTransportVersion transport_version,
27 std::string chlo_hash, std::unique_ptr<ProofSource::Callback> callback,
28 ProofSource* delegate)
29 : server_address_(server_addr),
30 client_address_(client_address),
31 hostname_(std::move(hostname)),
32 server_config_(std::move(server_config)),
33 transport_version_(transport_version),
34 chlo_hash_(std::move(chlo_hash)),
35 callback_(std::move(callback)),
36 delegate_(delegate) {}
37
38 FakeProofSource::GetProofOp::~GetProofOp() = default;
39
Run()40 void FakeProofSource::GetProofOp::Run() {
41 // Note: relies on the callback being invoked synchronously
42 delegate_->GetProof(server_address_, client_address_, hostname_,
43 server_config_, transport_version_, chlo_hash_,
44 std::move(callback_));
45 }
46
ComputeSignatureOp(const QuicSocketAddress & server_address,const QuicSocketAddress & client_address,std::string hostname,uint16_t sig_alg,absl::string_view in,std::unique_ptr<ProofSource::SignatureCallback> callback,ProofSource * delegate)47 FakeProofSource::ComputeSignatureOp::ComputeSignatureOp(
48 const QuicSocketAddress& server_address,
49 const QuicSocketAddress& client_address, std::string hostname,
50 uint16_t sig_alg, absl::string_view in,
51 std::unique_ptr<ProofSource::SignatureCallback> callback,
52 ProofSource* delegate)
53 : server_address_(server_address),
54 client_address_(client_address),
55 hostname_(std::move(hostname)),
56 sig_alg_(sig_alg),
57 in_(in),
58 callback_(std::move(callback)),
59 delegate_(delegate) {}
60
61 FakeProofSource::ComputeSignatureOp::~ComputeSignatureOp() = default;
62
Run()63 void FakeProofSource::ComputeSignatureOp::Run() {
64 delegate_->ComputeTlsSignature(server_address_, client_address_, hostname_,
65 sig_alg_, in_, std::move(callback_));
66 }
67
Activate()68 void FakeProofSource::Activate() { active_ = true; }
69
GetProof(const QuicSocketAddress & server_address,const QuicSocketAddress & client_address,const std::string & hostname,const std::string & server_config,QuicTransportVersion transport_version,absl::string_view chlo_hash,std::unique_ptr<ProofSource::Callback> callback)70 void FakeProofSource::GetProof(
71 const QuicSocketAddress& server_address,
72 const QuicSocketAddress& client_address, const std::string& hostname,
73 const std::string& server_config, QuicTransportVersion transport_version,
74 absl::string_view chlo_hash,
75 std::unique_ptr<ProofSource::Callback> callback) {
76 if (!active_) {
77 delegate_->GetProof(server_address, client_address, hostname, server_config,
78 transport_version, chlo_hash, std::move(callback));
79 return;
80 }
81
82 pending_ops_.push_back(std::make_unique<GetProofOp>(
83 server_address, client_address, hostname, server_config,
84 transport_version, std::string(chlo_hash), std::move(callback),
85 delegate_.get()));
86 }
87
88 quiche::QuicheReferenceCountedPointer<ProofSource::Chain>
GetCertChain(const QuicSocketAddress & server_address,const QuicSocketAddress & client_address,const std::string & hostname,bool * cert_matched_sni)89 FakeProofSource::GetCertChain(const QuicSocketAddress& server_address,
90 const QuicSocketAddress& client_address,
91 const std::string& hostname,
92 bool* cert_matched_sni) {
93 return delegate_->GetCertChain(server_address, client_address, hostname,
94 cert_matched_sni);
95 }
96
ComputeTlsSignature(const QuicSocketAddress & server_address,const QuicSocketAddress & client_address,const std::string & hostname,uint16_t signature_algorithm,absl::string_view in,std::unique_ptr<ProofSource::SignatureCallback> callback)97 void FakeProofSource::ComputeTlsSignature(
98 const QuicSocketAddress& server_address,
99 const QuicSocketAddress& client_address, const std::string& hostname,
100 uint16_t signature_algorithm, absl::string_view in,
101 std::unique_ptr<ProofSource::SignatureCallback> callback) {
102 QUIC_LOG(INFO) << "FakeProofSource::ComputeTlsSignature";
103 if (!active_) {
104 QUIC_LOG(INFO) << "Not active - directly calling delegate";
105 delegate_->ComputeTlsSignature(server_address, client_address, hostname,
106 signature_algorithm, in,
107 std::move(callback));
108 return;
109 }
110
111 QUIC_LOG(INFO) << "Adding pending op";
112 pending_ops_.push_back(std::make_unique<ComputeSignatureOp>(
113 server_address, client_address, hostname, signature_algorithm, in,
114 std::move(callback), delegate_.get()));
115 }
116
117 absl::InlinedVector<uint16_t, 8>
SupportedTlsSignatureAlgorithms() const118 FakeProofSource::SupportedTlsSignatureAlgorithms() const {
119 return delegate_->SupportedTlsSignatureAlgorithms();
120 }
121
GetTicketCrypter()122 ProofSource::TicketCrypter* FakeProofSource::GetTicketCrypter() {
123 if (ticket_crypter_) {
124 return ticket_crypter_.get();
125 }
126 return delegate_->GetTicketCrypter();
127 }
128
SetTicketCrypter(std::unique_ptr<TicketCrypter> ticket_crypter)129 void FakeProofSource::SetTicketCrypter(
130 std::unique_ptr<TicketCrypter> ticket_crypter) {
131 ticket_crypter_ = std::move(ticket_crypter);
132 }
133
NumPendingCallbacks() const134 int FakeProofSource::NumPendingCallbacks() const { return pending_ops_.size(); }
135
InvokePendingCallback(int n)136 void FakeProofSource::InvokePendingCallback(int n) {
137 QUICHE_CHECK(NumPendingCallbacks() > n);
138
139 pending_ops_[n]->Run();
140
141 auto it = pending_ops_.begin() + n;
142 pending_ops_.erase(it);
143 }
144
145 } // namespace test
146 } // namespace quic
147