QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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 | |
bnc | 463f235 | 2019-10-10 04:49:34 -0700 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9 | #include "net/third_party/quiche/src/quic/platform/api/quic_logging.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 10 | #include "net/third_party/quiche/src/quic/test_tools/crypto_test_utils.h" |
QUICHE team | 6dcf6ab | 2019-12-11 10:10:51 -0800 | [diff] [blame] | 11 | #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 12 | |
| 13 | namespace quic { |
| 14 | namespace test { |
| 15 | |
| 16 | FakeProofSource::FakeProofSource() |
| 17 | : delegate_(crypto_test_utils::ProofSourceForTesting()) {} |
| 18 | |
| 19 | FakeProofSource::~FakeProofSource() {} |
| 20 | |
| 21 | FakeProofSource::PendingOp::~PendingOp() = default; |
| 22 | |
| 23 | FakeProofSource::GetProofOp::GetProofOp( |
| 24 | const QuicSocketAddress& server_addr, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 25 | std::string hostname, |
| 26 | std::string server_config, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 27 | QuicTransportVersion transport_version, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 28 | std::string chlo_hash, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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 | |
| 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 | |
| 47 | FakeProofSource::ComputeSignatureOp::ComputeSignatureOp( |
| 48 | const QuicSocketAddress& server_address, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 49 | std::string hostname, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 50 | uint16_t sig_alg, |
QUICHE team | 6dcf6ab | 2019-12-11 10:10:51 -0800 | [diff] [blame] | 51 | quiche::QuicheStringPiece in, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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 | |
| 63 | void FakeProofSource::ComputeSignatureOp::Run() { |
| 64 | delegate_->ComputeTlsSignature(server_address_, hostname_, sig_alg_, in_, |
| 65 | std::move(callback_)); |
| 66 | } |
| 67 | |
| 68 | void FakeProofSource::Activate() { |
| 69 | active_ = true; |
| 70 | } |
| 71 | |
| 72 | void FakeProofSource::GetProof( |
| 73 | const QuicSocketAddress& server_address, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 74 | const std::string& hostname, |
| 75 | const std::string& server_config, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 76 | QuicTransportVersion transport_version, |
QUICHE team | 6dcf6ab | 2019-12-11 10:10:51 -0800 | [diff] [blame] | 77 | quiche::QuicheStringPiece chlo_hash, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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 | |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 85 | pending_ops_.push_back(std::make_unique<GetProofOp>( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 86 | server_address, hostname, server_config, transport_version, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 87 | std::string(chlo_hash), std::move(callback), delegate_.get())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | QuicReferenceCountedPointer<ProofSource::Chain> FakeProofSource::GetCertChain( |
| 91 | const QuicSocketAddress& server_address, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 92 | const std::string& hostname) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 93 | return delegate_->GetCertChain(server_address, hostname); |
| 94 | } |
| 95 | |
| 96 | void FakeProofSource::ComputeTlsSignature( |
| 97 | const QuicSocketAddress& server_address, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 98 | const std::string& hostname, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 99 | uint16_t signature_algorithm, |
QUICHE team | 6dcf6ab | 2019-12-11 10:10:51 -0800 | [diff] [blame] | 100 | quiche::QuicheStringPiece in, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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"; |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 111 | pending_ops_.push_back(std::make_unique<ComputeSignatureOp>( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 112 | server_address, hostname, signature_algorithm, in, std::move(callback), |
| 113 | delegate_.get())); |
| 114 | } |
| 115 | |
nharper | 037c21b | 2020-04-23 14:41:35 -0700 | [diff] [blame] | 116 | ProofSource::TicketCrypter* FakeProofSource::SessionTicketCrypter() { |
| 117 | return delegate_->SessionTicketCrypter(); |
| 118 | } |
| 119 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 120 | int FakeProofSource::NumPendingCallbacks() const { |
| 121 | return pending_ops_.size(); |
| 122 | } |
| 123 | |
| 124 | void 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 |