| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_QUIC_TEST_TOOLS_CRYPTO_TEST_UTILS_H_ | 5 #ifndef NET_QUIC_TEST_TOOLS_CRYPTO_TEST_UTILS_H_ |
| 6 #define NET_QUIC_TEST_TOOLS_CRYPTO_TEST_UTILS_H_ | 6 #define NET_QUIC_TEST_TOOLS_CRYPTO_TEST_UTILS_H_ |
| 7 | 7 |
| 8 #include <cstdarg> | 8 #include <cstdarg> |
| 9 #include <cstddef> | 9 #include <cstddef> |
| 10 #include <cstdint> | 10 #include <cstdint> |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 class QuicCryptoServerConfig; | 31 class QuicCryptoServerConfig; |
| 32 class QuicCryptoServerStream; | 32 class QuicCryptoServerStream; |
| 33 class QuicCryptoStream; | 33 class QuicCryptoStream; |
| 34 class QuicRandom; | 34 class QuicRandom; |
| 35 class QuicServerId; | 35 class QuicServerId; |
| 36 | 36 |
| 37 namespace test { | 37 namespace test { |
| 38 | 38 |
| 39 class PacketSavingConnection; | 39 class PacketSavingConnection; |
| 40 | 40 |
| 41 class CryptoTestUtils { | 41 namespace crypto_test_utils { |
| 42 |
| 43 // An interface for a source of callbacks. This is used for invoking |
| 44 // callbacks asynchronously. |
| 45 // |
| 46 // Call the RunPendingCallbacks method regularly to run the callbacks from |
| 47 // this source. |
| 48 class CallbackSource { |
| 42 public: | 49 public: |
| 43 // An interface for a source of callbacks. This is used for invoking | 50 virtual ~CallbackSource() {} |
| 44 // callbacks asynchronously. | |
| 45 // | |
| 46 // Call the RunPendingCallbacks method regularly to run the callbacks from | |
| 47 // this source. | |
| 48 class CallbackSource { | |
| 49 public: | |
| 50 virtual ~CallbackSource() {} | |
| 51 | 51 |
| 52 // Runs pending callbacks from this source. If there is no pending | 52 // Runs pending callbacks from this source. If there is no pending |
| 53 // callback, does nothing. | 53 // callback, does nothing. |
| 54 virtual void RunPendingCallbacks() = 0; | 54 virtual void RunPendingCallbacks() = 0; |
| 55 }; | 55 }; |
| 56 | 56 |
| 57 // FakeServerOptions bundles together a number of options for configuring the | 57 // FakeServerOptions bundles together a number of options for configuring the |
| 58 // server in HandshakeWithFakeServer. | 58 // server in HandshakeWithFakeServer. |
| 59 struct FakeServerOptions { | 59 struct FakeServerOptions { |
| 60 FakeServerOptions(); | 60 FakeServerOptions(); |
| 61 ~FakeServerOptions(); | 61 ~FakeServerOptions(); |
| 62 | 62 |
| 63 // The Token Binding params that the server supports and will negotiate. | 63 // The Token Binding params that the server supports and will negotiate. |
| 64 QuicTagVector token_binding_params; | 64 QuicTagVector token_binding_params; |
| 65 }; | 65 }; |
| 66 | 66 |
| 67 // FakeClientOptions bundles together a number of options for configuring | 67 // FakeClientOptions bundles together a number of options for configuring |
| 68 // HandshakeWithFakeClient. | 68 // HandshakeWithFakeClient. |
| 69 struct FakeClientOptions { | 69 struct FakeClientOptions { |
| 70 FakeClientOptions(); | 70 FakeClientOptions(); |
| 71 ~FakeClientOptions(); | 71 ~FakeClientOptions(); |
| 72 | 72 |
| 73 // If channel_id_enabled is true then the client will attempt to send a | 73 // If channel_id_enabled is true then the client will attempt to send a |
| 74 // ChannelID. | 74 // ChannelID. |
| 75 bool channel_id_enabled; | 75 bool channel_id_enabled; |
| 76 | 76 |
| 77 // If channel_id_source_async is true then the client will use an async | 77 // If channel_id_source_async is true then the client will use an async |
| 78 // ChannelIDSource for testing. Ignored if channel_id_enabled is false. | 78 // ChannelIDSource for testing. Ignored if channel_id_enabled is false. |
| 79 bool channel_id_source_async; | 79 bool channel_id_source_async; |
| 80 | 80 |
| 81 // The Token Binding params that the client supports and will negotiate. | 81 // The Token Binding params that the client supports and will negotiate. |
| 82 QuicTagVector token_binding_params; | 82 QuicTagVector token_binding_params; |
| 83 }; | 83 }; |
| 84 | 84 |
| 85 // returns: the number of client hellos that the client sent. | 85 // returns: the number of client hellos that the client sent. |
| 86 static int HandshakeWithFakeServer(QuicConfig* server_quic_config, | 86 int HandshakeWithFakeServer(QuicConfig* server_quic_config, |
| 87 MockQuicConnectionHelper* helper, | 87 MockQuicConnectionHelper* helper, |
| 88 MockAlarmFactory* alarm_factory, | 88 MockAlarmFactory* alarm_factory, |
| 89 PacketSavingConnection* client_conn, | 89 PacketSavingConnection* client_conn, |
| 90 QuicCryptoClientStream* client, | 90 QuicCryptoClientStream* client, |
| 91 const FakeServerOptions& options); | 91 const FakeServerOptions& options); |
| 92 | 92 |
| 93 // returns: the number of client hellos that the client sent. | 93 // returns: the number of client hellos that the client sent. |
| 94 static int HandshakeWithFakeClient(MockQuicConnectionHelper* helper, | 94 int HandshakeWithFakeClient(MockQuicConnectionHelper* helper, |
| 95 MockAlarmFactory* alarm_factory, | 95 MockAlarmFactory* alarm_factory, |
| 96 PacketSavingConnection* server_conn, | 96 PacketSavingConnection* server_conn, |
| 97 QuicCryptoServerStream* server, | 97 QuicCryptoServerStream* server, |
| 98 const QuicServerId& server_id, | 98 const QuicServerId& server_id, |
| 99 const FakeClientOptions& options); | 99 const FakeClientOptions& options); |
| 100 | 100 |
| 101 // SetupCryptoServerConfigForTest configures |crypto_config| | 101 // SetupCryptoServerConfigForTest configures |crypto_config| |
| 102 // with sensible defaults for testing. | 102 // with sensible defaults for testing. |
| 103 static void SetupCryptoServerConfigForTest( | 103 void SetupCryptoServerConfigForTest(const QuicClock* clock, |
| 104 const QuicClock* clock, | 104 QuicRandom* rand, |
| 105 QuicRandom* rand, | 105 QuicCryptoServerConfig* crypto_config, |
| 106 QuicCryptoServerConfig* crypto_config, | 106 const FakeServerOptions& options); |
| 107 const FakeServerOptions& options); | |
| 108 | 107 |
| 109 // CommunicateHandshakeMessages moves messages from |client| to |server| and | 108 // CommunicateHandshakeMessages moves messages from |client| to |server| and |
| 110 // back until |clients|'s handshake has completed. | 109 // back until |clients|'s handshake has completed. |
| 111 static void CommunicateHandshakeMessages(PacketSavingConnection* client_conn, | 110 void CommunicateHandshakeMessages(PacketSavingConnection* client_conn, |
| 111 QuicCryptoStream* client, |
| 112 PacketSavingConnection* server_conn, |
| 113 QuicCryptoStream* server); |
| 114 |
| 115 // CommunicateHandshakeMessagesAndRunCallbacks moves messages from |client| |
| 116 // to |server| and back until |client|'s handshake has completed. If |
| 117 // |callback_source| is not nullptr, |
| 118 // CommunicateHandshakeMessagesAndRunCallbacks also runs callbacks from |
| 119 // |callback_source| between processing messages. |
| 120 void CommunicateHandshakeMessagesAndRunCallbacks( |
| 121 PacketSavingConnection* client_conn, |
| 122 QuicCryptoStream* client, |
| 123 PacketSavingConnection* server_conn, |
| 124 QuicCryptoStream* server, |
| 125 CallbackSource* callback_source); |
| 126 |
| 127 // AdvanceHandshake attempts to moves messages from |client| to |server| and |
| 128 // |server| to |client|. Returns the number of messages moved. |
| 129 std::pair<size_t, size_t> AdvanceHandshake(PacketSavingConnection* client_conn, |
| 112 QuicCryptoStream* client, | 130 QuicCryptoStream* client, |
| 131 size_t client_i, |
| 113 PacketSavingConnection* server_conn, | 132 PacketSavingConnection* server_conn, |
| 114 QuicCryptoStream* server); | 133 QuicCryptoStream* server, |
| 134 size_t server_i); |
| 115 | 135 |
| 116 // CommunicateHandshakeMessagesAndRunCallbacks moves messages from |client| | 136 // Returns the value for the tag |tag| in the tag value map of |message|. |
| 117 // to |server| and back until |client|'s handshake has completed. If | 137 std::string GetValueForTag(const CryptoHandshakeMessage& message, QuicTag tag); |
| 118 // |callback_source| is not nullptr, | |
| 119 // CommunicateHandshakeMessagesAndRunCallbacks also runs callbacks from | |
| 120 // |callback_source| between processing messages. | |
| 121 static void CommunicateHandshakeMessagesAndRunCallbacks( | |
| 122 PacketSavingConnection* client_conn, | |
| 123 QuicCryptoStream* client, | |
| 124 PacketSavingConnection* server_conn, | |
| 125 QuicCryptoStream* server, | |
| 126 CallbackSource* callback_source); | |
| 127 | 138 |
| 128 // AdvanceHandshake attempts to moves messages from |client| to |server| and | 139 // Returns a new |ProofSource| that serves up test certificates. |
| 129 // |server| to |client|. Returns the number of messages moved. | 140 std::unique_ptr<ProofSource> ProofSourceForTesting(); |
| 130 static std::pair<size_t, size_t> AdvanceHandshake( | |
| 131 PacketSavingConnection* client_conn, | |
| 132 QuicCryptoStream* client, | |
| 133 size_t client_i, | |
| 134 PacketSavingConnection* server_conn, | |
| 135 QuicCryptoStream* server, | |
| 136 size_t server_i); | |
| 137 | 141 |
| 138 // Returns the value for the tag |tag| in the tag value map of |message|. | 142 // Returns a new |ProofVerifier| that uses the QUIC testing root CA. |
| 139 static std::string GetValueForTag(const CryptoHandshakeMessage& message, | 143 std::unique_ptr<ProofVerifier> ProofVerifierForTesting(); |
| 140 QuicTag tag); | |
| 141 | 144 |
| 142 // Returns a new |ProofSource| that serves up test certificates. | 145 // Returns a hash of the leaf test certificate. |
| 143 static std::unique_ptr<ProofSource> ProofSourceForTesting(); | 146 uint64_t LeafCertHashForTesting(); |
| 144 | 147 |
| 145 // Identical to |ProofSourceForTesting|, with the addition of setting | 148 // Returns a |ProofVerifyContext| that must be used with the verifier |
| 146 // the |emit_expect_ct_header| field on the test certificates | 149 // returned by |ProofVerifierForTesting|. |
| 147 // to be the value of |send_expect_ct_header|. | 150 ProofVerifyContext* ProofVerifyContextForTesting(); |
| 148 static std::unique_ptr<ProofSource> ProofSourceForTesting( | |
| 149 bool send_expect_ct_header); | |
| 150 | 151 |
| 151 // Returns a new |ProofVerifier| that uses the QUIC testing root CA. | 152 // MockCommonCertSets returns a CommonCertSets that contains a single set with |
| 152 static std::unique_ptr<ProofVerifier> ProofVerifierForTesting(); | 153 // hash |hash|, consisting of the certificate |cert| at index |index|. |
| 154 CommonCertSets* MockCommonCertSets(base::StringPiece cert, |
| 155 uint64_t hash, |
| 156 uint32_t index); |
| 153 | 157 |
| 154 // Returns a hash of the leaf test certificate. | 158 // Creates a minimal dummy reject message that will pass the client-config |
| 155 static uint64_t LeafCertHashForTesting(); | 159 // validation tests. This will include a server config, but no certs, proof |
| 160 // source address token, or server nonce. |
| 161 void FillInDummyReject(CryptoHandshakeMessage* rej, bool reject_is_stateless); |
| 156 | 162 |
| 157 // Returns a |ProofVerifyContext| that must be used with the verifier | 163 // ParseTag returns a QuicTag from parsing |tagstr|. |tagstr| may either be |
| 158 // returned by |ProofVerifierForTesting|. | 164 // in the format "EXMP" (i.e. ASCII format), or "#11223344" (an explicit hex |
| 159 static ProofVerifyContext* ProofVerifyContextForTesting(); | 165 // format). It CHECK fails if there's a parse error. |
| 166 QuicTag ParseTag(const char* tagstr); |
| 160 | 167 |
| 161 // MockCommonCertSets returns a CommonCertSets that contains a single set with | 168 // Message constructs a CHLO message from a provided vector of tag/value pairs. |
| 162 // hash |hash|, consisting of the certificate |cert| at index |index|. | 169 // The first of each pair is the tag of a tag/value and is given as an argument |
| 163 static CommonCertSets* MockCommonCertSets(base::StringPiece cert, | 170 // to |ParseTag|. The second is the value of the tag/value pair and is either a |
| 164 uint64_t hash, | 171 // hex dump, preceeded by a '#', or a raw value. If minimum_size_bytes is |
| 165 uint32_t index); | 172 // provided then the message will be padded to this minimum size. |
| 173 // |
| 174 // CreateCHLO( |
| 175 // {{"NOCE", "#11223344"}, |
| 176 // {"SNI", "www.example.com"}}, |
| 177 // optional_minimum_size_bytes); |
| 178 CryptoHandshakeMessage CreateCHLO( |
| 179 std::vector<std::pair<std::string, std::string>> tags_and_values); |
| 180 CryptoHandshakeMessage CreateCHLO( |
| 181 std::vector<std::pair<std::string, std::string>> tags_and_values, |
| 182 int minimum_size_bytes); |
| 166 | 183 |
| 167 // Creates a minimal dummy reject message that will pass the client-config | 184 // ChannelIDSourceForTesting returns a ChannelIDSource that generates keys |
| 168 // validation tests. This will include a server config, but no certs, proof | 185 // deterministically based on the hostname given in the GetChannelIDKey call. |
| 169 // source address token, or server nonce. | 186 // This ChannelIDSource works in synchronous mode, i.e., its GetChannelIDKey |
| 170 static void FillInDummyReject(CryptoHandshakeMessage* rej, | 187 // method never returns QUIC_PENDING. |
| 171 bool reject_is_stateless); | 188 ChannelIDSource* ChannelIDSourceForTesting(); |
| 172 | 189 |
| 173 // ParseTag returns a QuicTag from parsing |tagstr|. |tagstr| may either be | 190 // MovePackets parses crypto handshake messages from packet number |
| 174 // in the format "EXMP" (i.e. ASCII format), or "#11223344" (an explicit hex | 191 // |*inout_packet_index| through to the last packet (or until a packet fails |
| 175 // format). It CHECK fails if there's a parse error. | 192 // to decrypt) and has |dest_stream| process them. |*inout_packet_index| is |
| 176 static QuicTag ParseTag(const char* tagstr); | 193 // updated with an index one greater than the last packet processed. |
| 194 void MovePackets(PacketSavingConnection* source_conn, |
| 195 size_t* inout_packet_index, |
| 196 QuicCryptoStream* dest_stream, |
| 197 PacketSavingConnection* dest_conn, |
| 198 Perspective dest_perspective); |
| 177 | 199 |
| 178 // Message constructs a handshake message from a variable number of | 200 // Return an inchoate CHLO with some basic tag value pairs. |
| 179 // arguments. |message_tag| is passed to |ParseTag| and used as the tag of | 201 CryptoHandshakeMessage GenerateDefaultInchoateCHLO( |
| 180 // the resulting message. The arguments are taken in pairs and nullptr | 202 const QuicClock* clock, |
| 181 // terminated. The first of each pair is the tag of a tag/value and is given | 203 QuicVersion version, |
| 182 // as an argument to |ParseTag|. The second is the value of the tag/value | 204 QuicCryptoServerConfig* crypto_config); |
| 183 // pair and is either a hex dump, preceeded by a '#', or a raw value. | |
| 184 // | |
| 185 // Message( | |
| 186 // "CHLO", | |
| 187 // "NOCE", "#11223344", | |
| 188 // "SNI", "www.example.com", | |
| 189 // nullptr); | |
| 190 static CryptoHandshakeMessage Message(const char* message_tag, ...); | |
| 191 | 205 |
| 192 // ChannelIDSourceForTesting returns a ChannelIDSource that generates keys | 206 // Takes a inchoate CHLO, returns a full CHLO in |out| which can pass |
| 193 // deterministically based on the hostname given in the GetChannelIDKey call. | 207 // |crypto_config|'s validation. |
| 194 // This ChannelIDSource works in synchronous mode, i.e., its GetChannelIDKey | 208 void GenerateFullCHLO( |
| 195 // method never returns QUIC_PENDING. | 209 const CryptoHandshakeMessage& inchoate_chlo, |
| 196 static ChannelIDSource* ChannelIDSourceForTesting(); | 210 QuicCryptoServerConfig* crypto_config, |
| 211 QuicSocketAddress server_addr, |
| 212 QuicSocketAddress client_addr, |
| 213 QuicVersion version, |
| 214 const QuicClock* clock, |
| 215 QuicReferenceCountedPointer<QuicSignedServerConfig> signed_config, |
| 216 QuicCompressedCertsCache* compressed_certs_cache, |
| 217 CryptoHandshakeMessage* out); |
| 197 | 218 |
| 198 // MovePackets parses crypto handshake messages from packet number | 219 void CompareClientAndServerKeys(QuicCryptoClientStream* client, |
| 199 // |*inout_packet_index| through to the last packet (or until a packet fails | 220 QuicCryptoServerStream* server); |
| 200 // to decrypt) and has |dest_stream| process them. |*inout_packet_index| is | |
| 201 // updated with an index one greater than the last packet processed. | |
| 202 static void MovePackets(PacketSavingConnection* source_conn, | |
| 203 size_t* inout_packet_index, | |
| 204 QuicCryptoStream* dest_stream, | |
| 205 PacketSavingConnection* dest_conn, | |
| 206 Perspective dest_perspective); | |
| 207 | 221 |
| 208 // Return an inchoate CHLO with some basic tag value pairs. | 222 // Return a CHLO nonce in hexadecimal. |
| 209 static CryptoHandshakeMessage GenerateDefaultInchoateCHLO( | 223 std::string GenerateClientNonceHex(const QuicClock* clock, |
| 210 const QuicClock* clock, | 224 QuicCryptoServerConfig* crypto_config); |
| 211 QuicVersion version, | |
| 212 QuicCryptoServerConfig* crypto_config); | |
| 213 | 225 |
| 214 // Takes a inchoate CHLO, returns a full CHLO in |out| which can pass | 226 // Return a CHLO PUBS in hexadecimal. |
| 215 // |crypto_config|'s validation. | 227 std::string GenerateClientPublicValuesHex(); |
| 216 static void GenerateFullCHLO( | |
| 217 const CryptoHandshakeMessage& inchoate_chlo, | |
| 218 QuicCryptoServerConfig* crypto_config, | |
| 219 QuicSocketAddress server_addr, | |
| 220 QuicSocketAddress client_addr, | |
| 221 QuicVersion version, | |
| 222 const QuicClock* clock, | |
| 223 QuicReferenceCountedPointer<QuicSignedServerConfig> signed_config, | |
| 224 QuicCompressedCertsCache* compressed_certs_cache, | |
| 225 CryptoHandshakeMessage* out); | |
| 226 | 228 |
| 227 private: | 229 } // namespace crypto_test_utils |
| 228 static void CompareClientAndServerKeys(QuicCryptoClientStream* client, | |
| 229 QuicCryptoServerStream* server); | |
| 230 | |
| 231 // Return a CHLO nonce in hexadecimal. | |
| 232 static std::string GenerateClientNonceHex( | |
| 233 const QuicClock* clock, | |
| 234 QuicCryptoServerConfig* crypto_config); | |
| 235 | |
| 236 // Return a CHLO PUBS in hexadecimal. | |
| 237 static std::string GenerateClientPublicValuesHex(); | |
| 238 | |
| 239 DISALLOW_COPY_AND_ASSIGN(CryptoTestUtils); | |
| 240 }; | |
| 241 | 230 |
| 242 } // namespace test | 231 } // namespace test |
| 243 | 232 |
| 244 } // namespace net | 233 } // namespace net |
| 245 | 234 |
| 246 #endif // NET_QUIC_TEST_TOOLS_CRYPTO_TEST_UTILS_H_ | 235 #endif // NET_QUIC_TEST_TOOLS_CRYPTO_TEST_UTILS_H_ |
| OLD | NEW |