QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright 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/quic_crypto_server_config_peer.h" |
| 6 | |
| 7 | #include "net/third_party/quiche/src/quic/test_tools/mock_clock.h" |
| 8 | #include "net/third_party/quiche/src/quic/test_tools/mock_random.h" |
| 9 | #include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h" |
QUICHE team | 6dcf6ab | 2019-12-11 10:10:51 -0800 | [diff] [blame] | 10 | #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] | 11 | |
| 12 | namespace quic { |
| 13 | namespace test { |
| 14 | |
| 15 | QuicReferenceCountedPointer<QuicCryptoServerConfig::Config> |
| 16 | QuicCryptoServerConfigPeer::GetPrimaryConfig() { |
| 17 | QuicReaderMutexLock locked(&server_config_->configs_lock_); |
| 18 | return QuicReferenceCountedPointer<QuicCryptoServerConfig::Config>( |
| 19 | server_config_->primary_config_); |
| 20 | } |
| 21 | |
| 22 | QuicReferenceCountedPointer<QuicCryptoServerConfig::Config> |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 23 | QuicCryptoServerConfigPeer::GetConfig(std::string config_id) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 24 | QuicReaderMutexLock locked(&server_config_->configs_lock_); |
| 25 | if (config_id == "<primary>") { |
| 26 | return QuicReferenceCountedPointer<QuicCryptoServerConfig::Config>( |
| 27 | server_config_->primary_config_); |
| 28 | } else { |
| 29 | return server_config_->GetConfigWithScid(config_id); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | ProofSource* QuicCryptoServerConfigPeer::GetProofSource() const { |
| 34 | return server_config_->proof_source_.get(); |
| 35 | } |
| 36 | |
| 37 | void QuicCryptoServerConfigPeer::ResetProofSource( |
| 38 | std::unique_ptr<ProofSource> proof_source) { |
| 39 | server_config_->proof_source_ = std::move(proof_source); |
| 40 | } |
| 41 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 42 | std::string QuicCryptoServerConfigPeer::NewSourceAddressToken( |
| 43 | std::string config_id, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 44 | SourceAddressTokens previous_tokens, |
| 45 | const QuicIpAddress& ip, |
| 46 | QuicRandom* rand, |
| 47 | QuicWallTime now, |
| 48 | CachedNetworkParameters* cached_network_params) { |
| 49 | return server_config_->NewSourceAddressToken(*GetConfig(config_id), |
| 50 | previous_tokens, ip, rand, now, |
| 51 | cached_network_params); |
| 52 | } |
| 53 | |
| 54 | HandshakeFailureReason QuicCryptoServerConfigPeer::ValidateSourceAddressTokens( |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 55 | std::string config_id, |
QUICHE team | 6dcf6ab | 2019-12-11 10:10:51 -0800 | [diff] [blame] | 56 | quiche::QuicheStringPiece srct, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 57 | const QuicIpAddress& ip, |
| 58 | QuicWallTime now, |
| 59 | CachedNetworkParameters* cached_network_params) { |
| 60 | SourceAddressTokens tokens; |
| 61 | HandshakeFailureReason reason = server_config_->ParseSourceAddressToken( |
| 62 | *GetConfig(config_id), srct, &tokens); |
| 63 | if (reason != HANDSHAKE_OK) { |
| 64 | return reason; |
| 65 | } |
| 66 | |
| 67 | return server_config_->ValidateSourceAddressTokens(tokens, ip, now, |
| 68 | cached_network_params); |
| 69 | } |
| 70 | |
| 71 | HandshakeFailureReason |
| 72 | QuicCryptoServerConfigPeer::ValidateSingleSourceAddressToken( |
QUICHE team | 6dcf6ab | 2019-12-11 10:10:51 -0800 | [diff] [blame] | 73 | quiche::QuicheStringPiece token, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 74 | const QuicIpAddress& ip, |
| 75 | QuicWallTime now) { |
| 76 | SourceAddressTokens tokens; |
| 77 | HandshakeFailureReason parse_status = server_config_->ParseSourceAddressToken( |
| 78 | *GetPrimaryConfig(), token, &tokens); |
| 79 | if (HANDSHAKE_OK != parse_status) { |
| 80 | return parse_status; |
| 81 | } |
| 82 | EXPECT_EQ(1, tokens.tokens_size()); |
| 83 | return server_config_->ValidateSingleSourceAddressToken(tokens.tokens(0), ip, |
| 84 | now); |
| 85 | } |
| 86 | |
| 87 | void QuicCryptoServerConfigPeer::CheckConfigs( |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 88 | std::vector<std::pair<std::string, bool>> expected_ids_and_status) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 89 | QuicReaderMutexLock locked(&server_config_->configs_lock_); |
| 90 | |
| 91 | ASSERT_EQ(expected_ids_and_status.size(), server_config_->configs_.size()) |
| 92 | << ConfigsDebug(); |
| 93 | |
| 94 | for (const std::pair< |
| 95 | const ServerConfigID, |
| 96 | QuicReferenceCountedPointer<QuicCryptoServerConfig::Config>>& i : |
| 97 | server_config_->configs_) { |
| 98 | bool found = false; |
| 99 | for (std::pair<ServerConfigID, bool>& j : expected_ids_and_status) { |
| 100 | if (i.first == j.first && i.second->is_primary == j.second) { |
| 101 | found = true; |
| 102 | j.first.clear(); |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | ASSERT_TRUE(found) << "Failed to find match for " << i.first |
| 108 | << " in configs:\n" |
| 109 | << ConfigsDebug(); |
| 110 | } |
| 111 | } |
| 112 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 113 | // ConfigsDebug returns a std::string that contains debugging information about |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 114 | // the set of Configs loaded in |server_config_| and their status. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 115 | std::string QuicCryptoServerConfigPeer::ConfigsDebug() { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 116 | if (server_config_->configs_.empty()) { |
| 117 | return "No Configs in QuicCryptoServerConfig"; |
| 118 | } |
| 119 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 120 | std::string s; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 121 | |
| 122 | for (const auto& i : server_config_->configs_) { |
| 123 | const QuicReferenceCountedPointer<QuicCryptoServerConfig::Config> config = |
| 124 | i.second; |
| 125 | if (config->is_primary) { |
| 126 | s += "(primary) "; |
| 127 | } else { |
| 128 | s += " "; |
| 129 | } |
| 130 | s += config->id; |
| 131 | s += "\n"; |
| 132 | } |
| 133 | |
| 134 | return s; |
| 135 | } |
| 136 | |
| 137 | void QuicCryptoServerConfigPeer::SelectNewPrimaryConfig(int seconds) { |
| 138 | QuicWriterMutexLock locked(&server_config_->configs_lock_); |
| 139 | server_config_->SelectNewPrimaryConfig( |
| 140 | QuicWallTime::FromUNIXSeconds(seconds)); |
| 141 | } |
| 142 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 143 | std::string QuicCryptoServerConfigPeer::CompressChain( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 144 | QuicCompressedCertsCache* compressed_certs_cache, |
| 145 | const QuicReferenceCountedPointer<ProofSource::Chain>& chain, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 146 | const std::string& client_common_set_hashes, |
| 147 | const std::string& client_cached_cert_hashes, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 148 | const CommonCertSets* common_sets) { |
| 149 | return QuicCryptoServerConfig::CompressChain( |
| 150 | compressed_certs_cache, chain, client_common_set_hashes, |
| 151 | client_cached_cert_hashes, common_sets); |
| 152 | } |
| 153 | |
| 154 | uint32_t QuicCryptoServerConfigPeer::source_address_token_future_secs() { |
| 155 | return server_config_->source_address_token_future_secs_; |
| 156 | } |
| 157 | |
| 158 | uint32_t QuicCryptoServerConfigPeer::source_address_token_lifetime_secs() { |
| 159 | return server_config_->source_address_token_lifetime_secs_; |
| 160 | } |
| 161 | |
| 162 | } // namespace test |
| 163 | } // namespace quic |