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 | |
| 7 | #include "net/third_party/quiche/src/quic/platform/api/quic_logging.h" |
| 8 | #include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h" |
| 9 | #include "net/third_party/quiche/src/quic/test_tools/crypto_test_utils.h" |
| 10 | |
| 11 | namespace quic { |
| 12 | namespace test { |
| 13 | |
| 14 | FakeProofSource::FakeProofSource() |
| 15 | : delegate_(crypto_test_utils::ProofSourceForTesting()) {} |
| 16 | |
| 17 | FakeProofSource::~FakeProofSource() {} |
| 18 | |
| 19 | FakeProofSource::PendingOp::~PendingOp() = default; |
| 20 | |
| 21 | FakeProofSource::GetProofOp::GetProofOp( |
| 22 | const QuicSocketAddress& server_addr, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 23 | std::string hostname, |
| 24 | std::string server_config, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 25 | QuicTransportVersion transport_version, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 26 | std::string chlo_hash, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 27 | std::unique_ptr<ProofSource::Callback> callback, |
| 28 | ProofSource* delegate) |
| 29 | : server_address_(server_addr), |
| 30 | hostname_(std::move(hostname)), |
| 31 | server_config_(std::move(server_config)), |
| 32 | transport_version_(transport_version), |
| 33 | chlo_hash_(std::move(chlo_hash)), |
| 34 | callback_(std::move(callback)), |
| 35 | delegate_(delegate) {} |
| 36 | |
| 37 | FakeProofSource::GetProofOp::~GetProofOp() = default; |
| 38 | |
| 39 | void FakeProofSource::GetProofOp::Run() { |
| 40 | // Note: relies on the callback being invoked synchronously |
| 41 | delegate_->GetProof(server_address_, hostname_, server_config_, |
| 42 | transport_version_, chlo_hash_, std::move(callback_)); |
| 43 | } |
| 44 | |
| 45 | FakeProofSource::ComputeSignatureOp::ComputeSignatureOp( |
| 46 | const QuicSocketAddress& server_address, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 47 | std::string hostname, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 48 | uint16_t sig_alg, |
| 49 | QuicStringPiece in, |
| 50 | std::unique_ptr<ProofSource::SignatureCallback> callback, |
| 51 | ProofSource* delegate) |
| 52 | : server_address_(server_address), |
| 53 | hostname_(std::move(hostname)), |
| 54 | sig_alg_(sig_alg), |
| 55 | in_(in), |
| 56 | callback_(std::move(callback)), |
| 57 | delegate_(delegate) {} |
| 58 | |
| 59 | FakeProofSource::ComputeSignatureOp::~ComputeSignatureOp() = default; |
| 60 | |
| 61 | void FakeProofSource::ComputeSignatureOp::Run() { |
| 62 | delegate_->ComputeTlsSignature(server_address_, hostname_, sig_alg_, in_, |
| 63 | std::move(callback_)); |
| 64 | } |
| 65 | |
| 66 | void FakeProofSource::Activate() { |
| 67 | active_ = true; |
| 68 | } |
| 69 | |
| 70 | void FakeProofSource::GetProof( |
| 71 | const QuicSocketAddress& server_address, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 72 | const std::string& hostname, |
| 73 | const std::string& server_config, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 74 | QuicTransportVersion transport_version, |
| 75 | QuicStringPiece chlo_hash, |
| 76 | std::unique_ptr<ProofSource::Callback> callback) { |
| 77 | if (!active_) { |
| 78 | delegate_->GetProof(server_address, hostname, server_config, |
| 79 | transport_version, chlo_hash, std::move(callback)); |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | pending_ops_.push_back(QuicMakeUnique<GetProofOp>( |
| 84 | server_address, hostname, server_config, transport_version, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 85 | std::string(chlo_hash), std::move(callback), delegate_.get())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | QuicReferenceCountedPointer<ProofSource::Chain> FakeProofSource::GetCertChain( |
| 89 | const QuicSocketAddress& server_address, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 90 | const std::string& hostname) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 91 | return delegate_->GetCertChain(server_address, hostname); |
| 92 | } |
| 93 | |
| 94 | void FakeProofSource::ComputeTlsSignature( |
| 95 | const QuicSocketAddress& server_address, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 96 | const std::string& hostname, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 97 | uint16_t signature_algorithm, |
| 98 | QuicStringPiece in, |
| 99 | std::unique_ptr<ProofSource::SignatureCallback> callback) { |
| 100 | QUIC_LOG(INFO) << "FakeProofSource::ComputeTlsSignature"; |
| 101 | if (!active_) { |
| 102 | QUIC_LOG(INFO) << "Not active - directly calling delegate"; |
| 103 | delegate_->ComputeTlsSignature( |
| 104 | server_address, hostname, signature_algorithm, in, std::move(callback)); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | QUIC_LOG(INFO) << "Adding pending op"; |
| 109 | pending_ops_.push_back(QuicMakeUnique<ComputeSignatureOp>( |
| 110 | server_address, hostname, signature_algorithm, in, std::move(callback), |
| 111 | delegate_.get())); |
| 112 | } |
| 113 | |
| 114 | int FakeProofSource::NumPendingCallbacks() const { |
| 115 | return pending_ops_.size(); |
| 116 | } |
| 117 | |
| 118 | void FakeProofSource::InvokePendingCallback(int n) { |
| 119 | CHECK(NumPendingCallbacks() > n); |
| 120 | |
| 121 | pending_ops_[n]->Run(); |
| 122 | |
| 123 | auto it = pending_ops_.begin() + n; |
| 124 | pending_ops_.erase(it); |
| 125 | } |
| 126 | |
| 127 | } // namespace test |
| 128 | } // namespace quic |