blob: 032560e957791ef0042683983f2a8e45854305e1 [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
5#include "net/third_party/quiche/src/quic/test_tools/fake_proof_source.h"
6
bnc463f2352019-10-10 04:49:34 -07007#include <utility>
8
QUICHE teama6ef0a62019-03-07 20:34:33 -05009#include "net/third_party/quiche/src/quic/platform/api/quic_logging.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050010#include "net/third_party/quiche/src/quic/test_tools/crypto_test_utils.h"
QUICHE team6dcf6ab2019-12-11 10:10:51 -080011#include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.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,
vasilvvc48c8712019-03-11 13:38:16 -070025 std::string hostname,
26 std::string server_config,
QUICHE teama6ef0a62019-03-07 20:34:33 -050027 QuicTransportVersion transport_version,
vasilvvc48c8712019-03-11 13:38:16 -070028 std::string chlo_hash,
QUICHE teama6ef0a62019-03-07 20:34:33 -050029 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
39FakeProofSource::GetProofOp::~GetProofOp() = default;
40
41void 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
47FakeProofSource::ComputeSignatureOp::ComputeSignatureOp(
48 const QuicSocketAddress& server_address,
vasilvvc48c8712019-03-11 13:38:16 -070049 std::string hostname,
QUICHE teama6ef0a62019-03-07 20:34:33 -050050 uint16_t sig_alg,
QUICHE team6dcf6ab2019-12-11 10:10:51 -080051 quiche::QuicheStringPiece in,
QUICHE teama6ef0a62019-03-07 20:34:33 -050052 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
61FakeProofSource::ComputeSignatureOp::~ComputeSignatureOp() = default;
62
63void FakeProofSource::ComputeSignatureOp::Run() {
64 delegate_->ComputeTlsSignature(server_address_, hostname_, sig_alg_, in_,
65 std::move(callback_));
66}
67
68void FakeProofSource::Activate() {
69 active_ = true;
70}
71
72void FakeProofSource::GetProof(
73 const QuicSocketAddress& server_address,
vasilvvc48c8712019-03-11 13:38:16 -070074 const std::string& hostname,
75 const std::string& server_config,
QUICHE teama6ef0a62019-03-07 20:34:33 -050076 QuicTransportVersion transport_version,
QUICHE team6dcf6ab2019-12-11 10:10:51 -080077 quiche::QuicheStringPiece chlo_hash,
QUICHE teama6ef0a62019-03-07 20:34:33 -050078 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
vasilvv0fc587f2019-09-06 13:33:08 -070085 pending_ops_.push_back(std::make_unique<GetProofOp>(
QUICHE teama6ef0a62019-03-07 20:34:33 -050086 server_address, hostname, server_config, transport_version,
vasilvvc48c8712019-03-11 13:38:16 -070087 std::string(chlo_hash), std::move(callback), delegate_.get()));
QUICHE teama6ef0a62019-03-07 20:34:33 -050088}
89
90QuicReferenceCountedPointer<ProofSource::Chain> FakeProofSource::GetCertChain(
91 const QuicSocketAddress& server_address,
vasilvvc48c8712019-03-11 13:38:16 -070092 const std::string& hostname) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050093 return delegate_->GetCertChain(server_address, hostname);
94}
95
96void FakeProofSource::ComputeTlsSignature(
97 const QuicSocketAddress& server_address,
vasilvvc48c8712019-03-11 13:38:16 -070098 const std::string& hostname,
QUICHE teama6ef0a62019-03-07 20:34:33 -050099 uint16_t signature_algorithm,
QUICHE team6dcf6ab2019-12-11 10:10:51 -0800100 quiche::QuicheStringPiece in,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500101 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";
vasilvv0fc587f2019-09-06 13:33:08 -0700111 pending_ops_.push_back(std::make_unique<ComputeSignatureOp>(
QUICHE teama6ef0a62019-03-07 20:34:33 -0500112 server_address, hostname, signature_algorithm, in, std::move(callback),
113 delegate_.get()));
114}
115
nharper037c21b2020-04-23 14:41:35 -0700116ProofSource::TicketCrypter* FakeProofSource::SessionTicketCrypter() {
117 return delegate_->SessionTicketCrypter();
118}
119
QUICHE teama6ef0a62019-03-07 20:34:33 -0500120int FakeProofSource::NumPendingCallbacks() const {
121 return pending_ops_.size();
122}
123
124void FakeProofSource::InvokePendingCallback(int n) {
125 CHECK(NumPendingCallbacks() > n);
126
127 pending_ops_[n]->Run();
128
129 auto it = pending_ops_.begin() + n;
130 pending_ops_.erase(it);
131}
132
133} // namespace test
134} // namespace quic