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 "net/third_party/quiche/src/quic/test_tools/fake_proof_source.h"
6 
7 #include <utility>
8 
9 #include "net/third_party/quiche/src/quic/platform/api/quic_logging.h"
10 #include "net/third_party/quiche/src/quic/test_tools/crypto_test_utils.h"
11 #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.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,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     std::string hostname,
26     std::string server_config,
27     QuicTransportVersion transport_version,
28     std::string chlo_hash,
29     std::unique_ptr<ProofSource::Callback> callback,
30     ProofSource* delegate)
31     : server_address_(server_addr),
32       hostname_(std::move(hostname)),
33       server_config_(std::move(server_config)),
34       transport_version_(transport_version),
35       chlo_hash_(std::move(chlo_hash)),
36       callback_(std::move(callback)),
37       delegate_(delegate) {}
38 
39 FakeProofSource::GetProofOp::~GetProofOp() = default;
40 
Run()41 void FakeProofSource::GetProofOp::Run() {
42   // Note: relies on the callback being invoked synchronously
43   delegate_->GetProof(server_address_, hostname_, server_config_,
44                       transport_version_, chlo_hash_, std::move(callback_));
45 }
46 
ComputeSignatureOp(const QuicSocketAddress & server_address,std::string hostname,uint16_t sig_alg,quiche::QuicheStringPiece in,std::unique_ptr<ProofSource::SignatureCallback> callback,ProofSource * delegate)47 FakeProofSource::ComputeSignatureOp::ComputeSignatureOp(
48     const QuicSocketAddress& server_address,
49     std::string hostname,
50     uint16_t sig_alg,
51     quiche::QuicheStringPiece in,
52     std::unique_ptr<ProofSource::SignatureCallback> callback,
53     ProofSource* delegate)
54     : server_address_(server_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_, hostname_, sig_alg_, in_,
65                                  std::move(callback_));
66 }
67 
Activate()68 void FakeProofSource::Activate() {
69   active_ = true;
70 }
71 
GetProof(const QuicSocketAddress & server_address,const std::string & hostname,const std::string & server_config,QuicTransportVersion transport_version,quiche::QuicheStringPiece chlo_hash,std::unique_ptr<ProofSource::Callback> callback)72 void FakeProofSource::GetProof(
73     const QuicSocketAddress& server_address,
74     const std::string& hostname,
75     const std::string& server_config,
76     QuicTransportVersion transport_version,
77     quiche::QuicheStringPiece chlo_hash,
78     std::unique_ptr<ProofSource::Callback> callback) {
79   if (!active_) {
80     delegate_->GetProof(server_address, hostname, server_config,
81                         transport_version, chlo_hash, std::move(callback));
82     return;
83   }
84 
85   pending_ops_.push_back(std::make_unique<GetProofOp>(
86       server_address, hostname, server_config, transport_version,
87       std::string(chlo_hash), std::move(callback), delegate_.get()));
88 }
89 
GetCertChain(const QuicSocketAddress & server_address,const std::string & hostname)90 QuicReferenceCountedPointer<ProofSource::Chain> FakeProofSource::GetCertChain(
91     const QuicSocketAddress& server_address,
92     const std::string& hostname) {
93   return delegate_->GetCertChain(server_address, hostname);
94 }
95 
ComputeTlsSignature(const QuicSocketAddress & server_address,const std::string & hostname,uint16_t signature_algorithm,quiche::QuicheStringPiece in,std::unique_ptr<ProofSource::SignatureCallback> callback)96 void FakeProofSource::ComputeTlsSignature(
97     const QuicSocketAddress& server_address,
98     const std::string& hostname,
99     uint16_t signature_algorithm,
100     quiche::QuicheStringPiece 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(
106         server_address, hostname, signature_algorithm, in, std::move(callback));
107     return;
108   }
109 
110   QUIC_LOG(INFO) << "Adding pending op";
111   pending_ops_.push_back(std::make_unique<ComputeSignatureOp>(
112       server_address, hostname, signature_algorithm, in, std::move(callback),
113       delegate_.get()));
114 }
115 
NumPendingCallbacks() const116 int FakeProofSource::NumPendingCallbacks() const {
117   return pending_ops_.size();
118 }
119 
InvokePendingCallback(int n)120 void FakeProofSource::InvokePendingCallback(int n) {
121   CHECK(NumPendingCallbacks() > n);
122 
123   pending_ops_[n]->Run();
124 
125   auto it = pending_ops_.begin() + n;
126   pending_ops_.erase(it);
127 }
128 
129 }  // namespace test
130 }  // namespace quic
131