blob: bc75678872fe4295b227985379c61fb3e6d9d925 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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
QUICHE team5be974e2020-12-29 18:35:24 -05005#include "quic/test_tools/fake_proof_source.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -05006
bnc463f2352019-10-10 04:49:34 -07007#include <utility>
8
vasilvv5f225b02020-10-08 11:49:09 -04009#include "absl/strings/string_view.h"
QUICHE team5be974e2020-12-29 18:35:24 -050010#include "quic/platform/api/quic_logging.h"
11#include "quic/test_tools/crypto_test_utils.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050012
13namespace quic {
14namespace test {
15
16FakeProofSource::FakeProofSource()
17 : delegate_(crypto_test_utils::ProofSourceForTesting()) {}
18
19FakeProofSource::~FakeProofSource() {}
20
21FakeProofSource::PendingOp::~PendingOp() = default;
22
23FakeProofSource::GetProofOp::GetProofOp(
24 const QuicSocketAddress& server_addr,
danzhd1fc5912020-05-01 15:29:04 -070025 const QuicSocketAddress& client_address,
vasilvvc48c8712019-03-11 13:38:16 -070026 std::string hostname,
27 std::string server_config,
QUICHE teama6ef0a62019-03-07 20:34:33 -050028 QuicTransportVersion transport_version,
vasilvvc48c8712019-03-11 13:38:16 -070029 std::string chlo_hash,
QUICHE teama6ef0a62019-03-07 20:34:33 -050030 std::unique_ptr<ProofSource::Callback> callback,
31 ProofSource* delegate)
32 : server_address_(server_addr),
danzhd1fc5912020-05-01 15:29:04 -070033 client_address_(client_address),
QUICHE teama6ef0a62019-03-07 20:34:33 -050034 hostname_(std::move(hostname)),
35 server_config_(std::move(server_config)),
36 transport_version_(transport_version),
37 chlo_hash_(std::move(chlo_hash)),
38 callback_(std::move(callback)),
39 delegate_(delegate) {}
40
41FakeProofSource::GetProofOp::~GetProofOp() = default;
42
43void FakeProofSource::GetProofOp::Run() {
44 // Note: relies on the callback being invoked synchronously
danzhd1fc5912020-05-01 15:29:04 -070045 delegate_->GetProof(server_address_, client_address_, hostname_,
46 server_config_, transport_version_, chlo_hash_,
47 std::move(callback_));
QUICHE teama6ef0a62019-03-07 20:34:33 -050048}
49
50FakeProofSource::ComputeSignatureOp::ComputeSignatureOp(
51 const QuicSocketAddress& server_address,
danzhd1fc5912020-05-01 15:29:04 -070052 const QuicSocketAddress& client_address,
vasilvvc48c8712019-03-11 13:38:16 -070053 std::string hostname,
QUICHE teama6ef0a62019-03-07 20:34:33 -050054 uint16_t sig_alg,
vasilvv5f225b02020-10-08 11:49:09 -040055 absl::string_view in,
QUICHE teama6ef0a62019-03-07 20:34:33 -050056 std::unique_ptr<ProofSource::SignatureCallback> callback,
57 ProofSource* delegate)
58 : server_address_(server_address),
danzhd1fc5912020-05-01 15:29:04 -070059 client_address_(client_address),
QUICHE teama6ef0a62019-03-07 20:34:33 -050060 hostname_(std::move(hostname)),
61 sig_alg_(sig_alg),
62 in_(in),
63 callback_(std::move(callback)),
64 delegate_(delegate) {}
65
66FakeProofSource::ComputeSignatureOp::~ComputeSignatureOp() = default;
67
68void FakeProofSource::ComputeSignatureOp::Run() {
danzhd1fc5912020-05-01 15:29:04 -070069 delegate_->ComputeTlsSignature(server_address_, client_address_, hostname_,
70 sig_alg_, in_, std::move(callback_));
QUICHE teama6ef0a62019-03-07 20:34:33 -050071}
72
73void FakeProofSource::Activate() {
74 active_ = true;
75}
76
77void FakeProofSource::GetProof(
78 const QuicSocketAddress& server_address,
danzhd1fc5912020-05-01 15:29:04 -070079 const QuicSocketAddress& client_address,
vasilvvc48c8712019-03-11 13:38:16 -070080 const std::string& hostname,
81 const std::string& server_config,
QUICHE teama6ef0a62019-03-07 20:34:33 -050082 QuicTransportVersion transport_version,
vasilvv5f225b02020-10-08 11:49:09 -040083 absl::string_view chlo_hash,
QUICHE teama6ef0a62019-03-07 20:34:33 -050084 std::unique_ptr<ProofSource::Callback> callback) {
85 if (!active_) {
danzhd1fc5912020-05-01 15:29:04 -070086 delegate_->GetProof(server_address, client_address, hostname, server_config,
QUICHE teama6ef0a62019-03-07 20:34:33 -050087 transport_version, chlo_hash, std::move(callback));
88 return;
89 }
90
vasilvv0fc587f2019-09-06 13:33:08 -070091 pending_ops_.push_back(std::make_unique<GetProofOp>(
danzhd1fc5912020-05-01 15:29:04 -070092 server_address, client_address, hostname, server_config,
93 transport_version, std::string(chlo_hash), std::move(callback),
94 delegate_.get()));
QUICHE teama6ef0a62019-03-07 20:34:33 -050095}
96
97QuicReferenceCountedPointer<ProofSource::Chain> FakeProofSource::GetCertChain(
98 const QuicSocketAddress& server_address,
danzhd1fc5912020-05-01 15:29:04 -070099 const QuicSocketAddress& client_address,
vasilvvc48c8712019-03-11 13:38:16 -0700100 const std::string& hostname) {
danzhd1fc5912020-05-01 15:29:04 -0700101 return delegate_->GetCertChain(server_address, client_address, hostname);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500102}
103
104void FakeProofSource::ComputeTlsSignature(
105 const QuicSocketAddress& server_address,
danzhd1fc5912020-05-01 15:29:04 -0700106 const QuicSocketAddress& client_address,
vasilvvc48c8712019-03-11 13:38:16 -0700107 const std::string& hostname,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500108 uint16_t signature_algorithm,
vasilvv5f225b02020-10-08 11:49:09 -0400109 absl::string_view in,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500110 std::unique_ptr<ProofSource::SignatureCallback> callback) {
111 QUIC_LOG(INFO) << "FakeProofSource::ComputeTlsSignature";
112 if (!active_) {
113 QUIC_LOG(INFO) << "Not active - directly calling delegate";
danzhd1fc5912020-05-01 15:29:04 -0700114 delegate_->ComputeTlsSignature(server_address, client_address, hostname,
115 signature_algorithm, in,
116 std::move(callback));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500117 return;
118 }
119
120 QUIC_LOG(INFO) << "Adding pending op";
vasilvv0fc587f2019-09-06 13:33:08 -0700121 pending_ops_.push_back(std::make_unique<ComputeSignatureOp>(
danzhd1fc5912020-05-01 15:29:04 -0700122 server_address, client_address, hostname, signature_algorithm, in,
123 std::move(callback), delegate_.get()));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500124}
125
nharper1f8289a2020-04-27 11:57:28 -0700126ProofSource::TicketCrypter* FakeProofSource::GetTicketCrypter() {
nharperfbaacc02020-04-24 17:30:22 -0700127 if (ticket_crypter_) {
128 return ticket_crypter_.get();
129 }
nharper1f8289a2020-04-27 11:57:28 -0700130 return delegate_->GetTicketCrypter();
nharper037c21b2020-04-23 14:41:35 -0700131}
132
nharperfbaacc02020-04-24 17:30:22 -0700133void FakeProofSource::SetTicketCrypter(
134 std::unique_ptr<TicketCrypter> ticket_crypter) {
135 ticket_crypter_ = std::move(ticket_crypter);
136}
137
QUICHE teama6ef0a62019-03-07 20:34:33 -0500138int FakeProofSource::NumPendingCallbacks() const {
139 return pending_ops_.size();
140}
141
142void FakeProofSource::InvokePendingCallback(int n) {
vasilvvf8035162021-02-01 14:49:14 -0800143 QUICHE_CHECK(NumPendingCallbacks() > n);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500144
145 pending_ops_[n]->Run();
146
147 auto it = pending_ops_.begin() + n;
148 pending_ops_.erase(it);
149}
150
151} // namespace test
152} // namespace quic