[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/quic/test_tools/crypto_test_utils.h" |
[email protected] | 72818ea | 2013-03-13 03:23:57 | [diff] [blame] | 6 | |
[email protected] | 38b3fd1 | 2013-06-18 08:19:01 | [diff] [blame] | 7 | #include "net/quic/crypto/channel_id.h" |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 8 | #include "net/quic/crypto/common_cert_set.h" |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 9 | #include "net/quic/crypto/crypto_handshake.h" |
[email protected] | 8e01c06 | 2013-10-31 07:35:31 | [diff] [blame] | 10 | #include "net/quic/crypto/quic_crypto_server_config.h" |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 11 | #include "net/quic/crypto/quic_decrypter.h" |
| 12 | #include "net/quic/crypto/quic_encrypter.h" |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 13 | #include "net/quic/crypto/quic_random.h" |
| 14 | #include "net/quic/quic_clock.h" |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 15 | #include "net/quic/quic_crypto_client_stream.h" |
| 16 | #include "net/quic/quic_crypto_server_stream.h" |
| 17 | #include "net/quic/quic_crypto_stream.h" |
[email protected] | 257f24f | 2014-04-01 09:15:37 | [diff] [blame] | 18 | #include "net/quic/quic_server_id.h" |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 19 | #include "net/quic/test_tools/quic_connection_peer.h" |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 20 | #include "net/quic/test_tools/quic_test_utils.h" |
| 21 | #include "net/quic/test_tools/simple_quic_framer.h" |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 22 | |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 23 | using base::StringPiece; |
[email protected] | 691f45a98 | 2013-11-19 10:52:04 | [diff] [blame] | 24 | using std::make_pair; |
| 25 | using std::pair; |
[email protected] | ccc66e8a | 2013-03-26 08:26:14 | [diff] [blame] | 26 | using std::string; |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 27 | using std::vector; |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 28 | |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 29 | namespace net { |
| 30 | namespace test { |
| 31 | |
| 32 | namespace { |
| 33 | |
[email protected] | e4c3ea6 | 2014-03-15 00:45:14 | [diff] [blame] | 34 | const char kServerHostname[] = "test.example.com"; |
| 35 | const uint16 kServerPort = 80; |
| 36 | |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 37 | // CryptoFramerVisitor is a framer visitor that records handshake messages. |
| 38 | class CryptoFramerVisitor : public CryptoFramerVisitorInterface { |
| 39 | public: |
| 40 | CryptoFramerVisitor() |
| 41 | : error_(false) { |
| 42 | } |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 43 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 44 | void OnError(CryptoFramer* framer) override { error_ = true; } |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 45 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 46 | void OnHandshakeMessage(const CryptoHandshakeMessage& message) override { |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 47 | messages_.push_back(message); |
| 48 | } |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 49 | |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 50 | bool error() const { |
| 51 | return error_; |
| 52 | } |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 53 | |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 54 | const vector<CryptoHandshakeMessage>& messages() const { |
| 55 | return messages_; |
| 56 | } |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 57 | |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 58 | private: |
| 59 | bool error_; |
| 60 | vector<CryptoHandshakeMessage> messages_; |
| 61 | }; |
| 62 | |
| 63 | // MovePackets parses crypto handshake messages from packet number |
[email protected] | 9693157b | 2014-08-08 11:13:49 | [diff] [blame] | 64 | // |*inout_packet_index| through to the last packet (or until a packet fails to |
| 65 | // decrypt) and has |dest_stream| process them. |*inout_packet_index| is updated |
| 66 | // with an index one greater than the last packet processed. |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 67 | void MovePackets(PacketSavingConnection* source_conn, |
| 68 | size_t *inout_packet_index, |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 69 | QuicCryptoStream* dest_stream, |
| 70 | PacketSavingConnection* dest_conn) { |
[email protected] | 4d64079 | 2013-12-18 22:21:08 | [diff] [blame] | 71 | SimpleQuicFramer framer(source_conn->supported_versions()); |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 72 | CryptoFramer crypto_framer; |
| 73 | CryptoFramerVisitor crypto_visitor; |
| 74 | |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 75 | // In order to properly test the code we need to perform encryption and |
| 76 | // decryption so that the crypters latch when expected. The crypters are in |
| 77 | // |dest_conn|, but we don't want to try and use them there. Instead we swap |
| 78 | // them into |framer|, perform the decryption with them, and then swap them |
| 79 | // back. |
| 80 | QuicConnectionPeer::SwapCrypters(dest_conn, framer.framer()); |
| 81 | |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 82 | crypto_framer.set_visitor(&crypto_visitor); |
| 83 | |
| 84 | size_t index = *inout_packet_index; |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 85 | for (; index < source_conn->encrypted_packets_.size(); index++) { |
[email protected] | 9693157b | 2014-08-08 11:13:49 | [diff] [blame] | 86 | if (!framer.ProcessPacket(*source_conn->encrypted_packets_[index])) { |
| 87 | // The framer will be unable to decrypt forward-secure packets sent after |
| 88 | // the handshake is complete. Don't treat them as handshake packets. |
| 89 | break; |
| 90 | } |
| 91 | |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 92 | for (vector<QuicStreamFrame>::const_iterator |
| 93 | i = framer.stream_frames().begin(); |
| 94 | i != framer.stream_frames().end(); ++i) { |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 95 | scoped_ptr<string> frame_data(i->GetDataAsString()); |
| 96 | ASSERT_TRUE(crypto_framer.ProcessInput(*frame_data)); |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 97 | ASSERT_FALSE(crypto_visitor.error()); |
| 98 | } |
| 99 | } |
| 100 | *inout_packet_index = index; |
| 101 | |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 102 | QuicConnectionPeer::SwapCrypters(dest_conn, framer.framer()); |
| 103 | |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 104 | ASSERT_EQ(0u, crypto_framer.InputBytesRemaining()); |
| 105 | |
| 106 | for (vector<CryptoHandshakeMessage>::const_iterator |
| 107 | i = crypto_visitor.messages().begin(); |
| 108 | i != crypto_visitor.messages().end(); ++i) { |
| 109 | dest_stream->OnHandshakeMessage(*i); |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
[email protected] | 0bbeb697 | 2013-05-23 04:10:21 | [diff] [blame] | 113 | // HexChar parses |c| as a hex character. If valid, it sets |*value| to the |
| 114 | // value of the hex character and returns true. Otherwise it returns false. |
| 115 | bool HexChar(char c, uint8* value) { |
| 116 | if (c >= '0' && c <= '9') { |
| 117 | *value = c - '0'; |
| 118 | return true; |
| 119 | } |
| 120 | if (c >= 'a' && c <= 'f') { |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 121 | *value = c - 'a' + 10; |
[email protected] | 0bbeb697 | 2013-05-23 04:10:21 | [diff] [blame] | 122 | return true; |
| 123 | } |
| 124 | if (c >= 'A' && c <= 'F') { |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 125 | *value = c - 'A' + 10; |
[email protected] | 0bbeb697 | 2013-05-23 04:10:21 | [diff] [blame] | 126 | return true; |
| 127 | } |
| 128 | return false; |
| 129 | } |
| 130 | |
[email protected] | 6fc79ea | 2014-07-10 04:30:23 | [diff] [blame] | 131 | // A ChannelIDSource that works in asynchronous mode unless the |callback| |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 132 | // argument to GetChannelIDKey is nullptr. |
[email protected] | 6fc79ea | 2014-07-10 04:30:23 | [diff] [blame] | 133 | class AsyncTestChannelIDSource : public ChannelIDSource, |
[email protected] | dc6094a | 2014-07-23 01:50:04 | [diff] [blame] | 134 | public CryptoTestUtils::CallbackSource { |
[email protected] | 6fc79ea | 2014-07-10 04:30:23 | [diff] [blame] | 135 | public: |
| 136 | // Takes ownership of |sync_source|, a synchronous ChannelIDSource. |
| 137 | explicit AsyncTestChannelIDSource(ChannelIDSource* sync_source) |
| 138 | : sync_source_(sync_source) {} |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 139 | ~AsyncTestChannelIDSource() override {} |
[email protected] | 6fc79ea | 2014-07-10 04:30:23 | [diff] [blame] | 140 | |
| 141 | // ChannelIDSource implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 142 | QuicAsyncStatus GetChannelIDKey(const string& hostname, |
| 143 | scoped_ptr<ChannelIDKey>* channel_id_key, |
| 144 | ChannelIDSourceCallback* callback) override { |
[email protected] | 6fc79ea | 2014-07-10 04:30:23 | [diff] [blame] | 145 | // Synchronous mode. |
| 146 | if (!callback) { |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 147 | return sync_source_->GetChannelIDKey(hostname, channel_id_key, nullptr); |
[email protected] | 6fc79ea | 2014-07-10 04:30:23 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | // Asynchronous mode. |
| 151 | QuicAsyncStatus status = |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 152 | sync_source_->GetChannelIDKey(hostname, &channel_id_key_, nullptr); |
[email protected] | 6fc79ea | 2014-07-10 04:30:23 | [diff] [blame] | 153 | if (status != QUIC_SUCCESS) { |
| 154 | return QUIC_FAILURE; |
| 155 | } |
| 156 | callback_.reset(callback); |
| 157 | return QUIC_PENDING; |
| 158 | } |
| 159 | |
[email protected] | dc6094a | 2014-07-23 01:50:04 | [diff] [blame] | 160 | // CallbackSource implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 161 | void RunPendingCallbacks() override { |
[email protected] | 6fc79ea | 2014-07-10 04:30:23 | [diff] [blame] | 162 | if (callback_.get()) { |
| 163 | callback_->Run(&channel_id_key_); |
| 164 | callback_.reset(); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | private: |
| 169 | scoped_ptr<ChannelIDSource> sync_source_; |
| 170 | scoped_ptr<ChannelIDSourceCallback> callback_; |
| 171 | scoped_ptr<ChannelIDKey> channel_id_key_; |
| 172 | }; |
| 173 | |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 174 | } // anonymous namespace |
| 175 | |
[email protected] | 89995165 | 2013-05-16 12:52:39 | [diff] [blame] | 176 | CryptoTestUtils::FakeClientOptions::FakeClientOptions() |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 177 | : dont_verify_certs(false), |
[email protected] | 6fc79ea | 2014-07-10 04:30:23 | [diff] [blame] | 178 | channel_id_enabled(false), |
| 179 | channel_id_source_async(false) { |
[email protected] | 89995165 | 2013-05-16 12:52:39 | [diff] [blame] | 180 | } |
| 181 | |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 182 | // static |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 183 | int CryptoTestUtils::HandshakeWithFakeServer( |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 184 | PacketSavingConnection* client_conn, |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 185 | QuicCryptoClientStream* client) { |
[email protected] | 4d64079 | 2013-12-18 22:21:08 | [diff] [blame] | 186 | PacketSavingConnection* server_conn = |
| 187 | new PacketSavingConnection(true, client_conn->supported_versions()); |
[email protected] | c05a6d22 | 2013-12-16 19:42:03 | [diff] [blame] | 188 | TestSession server_session(server_conn, DefaultQuicConfig()); |
[email protected] | ccb3421 | 2014-07-18 09:27:50 | [diff] [blame] | 189 | server_session.InitializeSession(); |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 190 | QuicCryptoServerConfig crypto_config(QuicCryptoServerConfig::TESTING, |
| 191 | QuicRandom::GetInstance()); |
[email protected] | ccb3421 | 2014-07-18 09:27:50 | [diff] [blame] | 192 | |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 193 | SetupCryptoServerConfigForTest( |
| 194 | server_session.connection()->clock(), |
| 195 | server_session.connection()->random_generator(), |
[email protected] | 89995165 | 2013-05-16 12:52:39 | [diff] [blame] | 196 | server_session.config(), &crypto_config); |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 197 | |
[email protected] | 89995165 | 2013-05-16 12:52:39 | [diff] [blame] | 198 | QuicCryptoServerStream server(crypto_config, &server_session); |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 199 | server_session.SetCryptoStream(&server); |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 200 | |
| 201 | // The client's handshake must have been started already. |
| 202 | CHECK_NE(0u, client_conn->packets_.size()); |
| 203 | |
| 204 | CommunicateHandshakeMessages(client_conn, client, server_conn, &server); |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 205 | |
| 206 | CompareClientAndServerKeys(client, &server); |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 207 | |
| 208 | return client->num_sent_client_hellos(); |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | // static |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 212 | int CryptoTestUtils::HandshakeWithFakeClient( |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 213 | PacketSavingConnection* server_conn, |
[email protected] | 89995165 | 2013-05-16 12:52:39 | [diff] [blame] | 214 | QuicCryptoServerStream* server, |
| 215 | const FakeClientOptions& options) { |
[email protected] | c05a6d22 | 2013-12-16 19:42:03 | [diff] [blame] | 216 | PacketSavingConnection* client_conn = new PacketSavingConnection(false); |
[email protected] | 90f62f09 | 2014-03-24 02:41:23 | [diff] [blame] | 217 | TestClientSession client_session(client_conn, DefaultQuicConfig()); |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 218 | QuicCryptoClientConfig crypto_config; |
| 219 | |
[email protected] | 0cceb92 | 2014-07-01 02:00:56 | [diff] [blame] | 220 | if (!options.dont_verify_certs) { |
| 221 | // TODO(wtc): replace this with ProofVerifierForTesting() when we have |
| 222 | // a working ProofSourceForTesting(). |
| 223 | crypto_config.SetProofVerifier(FakeProofVerifierForTesting()); |
| 224 | } |
| 225 | bool is_https = false; |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 226 | AsyncTestChannelIDSource* async_channel_id_source = nullptr; |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 227 | if (options.channel_id_enabled) { |
[email protected] | 0cceb92 | 2014-07-01 02:00:56 | [diff] [blame] | 228 | is_https = true; |
[email protected] | 6fc79ea | 2014-07-10 04:30:23 | [diff] [blame] | 229 | |
| 230 | ChannelIDSource* source = ChannelIDSourceForTesting(); |
| 231 | if (options.channel_id_source_async) { |
| 232 | async_channel_id_source = new AsyncTestChannelIDSource(source); |
| 233 | source = async_channel_id_source; |
| 234 | } |
| 235 | crypto_config.SetChannelIDSource(source); |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 236 | } |
[email protected] | 0cceb92 | 2014-07-01 02:00:56 | [diff] [blame] | 237 | QuicServerId server_id(kServerHostname, kServerPort, is_https, |
[email protected] | 257f24f | 2014-04-01 09:15:37 | [diff] [blame] | 238 | PRIVACY_MODE_DISABLED); |
[email protected] | 0cceb92 | 2014-07-01 02:00:56 | [diff] [blame] | 239 | QuicCryptoClientStream client(server_id, &client_session, |
| 240 | ProofVerifyContextForTesting(), |
[email protected] | b694e48c | 2014-03-18 17:10:13 | [diff] [blame] | 241 | &crypto_config); |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 242 | client_session.SetCryptoStream(&client); |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 243 | |
| 244 | CHECK(client.CryptoConnect()); |
| 245 | CHECK_EQ(1u, client_conn->packets_.size()); |
| 246 | |
[email protected] | dc6094a | 2014-07-23 01:50:04 | [diff] [blame] | 247 | CommunicateHandshakeMessagesAndRunCallbacks( |
[email protected] | 6fc79ea | 2014-07-10 04:30:23 | [diff] [blame] | 248 | client_conn, &client, server_conn, server, async_channel_id_source); |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 249 | |
| 250 | CompareClientAndServerKeys(&client, server); |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 251 | |
[email protected] | 38b3fd1 | 2013-06-18 08:19:01 | [diff] [blame] | 252 | if (options.channel_id_enabled) { |
[email protected] | 03dd3253 | 2014-05-30 07:11:25 | [diff] [blame] | 253 | scoped_ptr<ChannelIDKey> channel_id_key; |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 254 | QuicAsyncStatus status = crypto_config.channel_id_source()->GetChannelIDKey( |
| 255 | kServerHostname, &channel_id_key, nullptr); |
[email protected] | 05bfc260f | 2014-06-07 06:31:25 | [diff] [blame] | 256 | EXPECT_EQ(QUIC_SUCCESS, status); |
[email protected] | 03dd3253 | 2014-05-30 07:11:25 | [diff] [blame] | 257 | EXPECT_EQ(channel_id_key->SerializeKey(), |
| 258 | server->crypto_negotiated_params().channel_id); |
[email protected] | dc6094a | 2014-07-23 01:50:04 | [diff] [blame] | 259 | EXPECT_EQ(options.channel_id_source_async, |
| 260 | client.WasChannelIDSourceCallbackRun()); |
[email protected] | 38b3fd1 | 2013-06-18 08:19:01 | [diff] [blame] | 261 | } |
| 262 | |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 263 | return client.num_sent_client_hellos(); |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | // static |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 267 | void CryptoTestUtils::SetupCryptoServerConfigForTest( |
| 268 | const QuicClock* clock, |
| 269 | QuicRandom* rand, |
| 270 | QuicConfig* config, |
| 271 | QuicCryptoServerConfig* crypto_config) { |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 272 | QuicCryptoServerConfig::ConfigOptions options; |
| 273 | options.channel_id_enabled = true; |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 274 | scoped_ptr<CryptoHandshakeMessage> scfg( |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 275 | crypto_config->AddDefaultConfig(rand, clock, options)); |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | // static |
[email protected] | 0bbeb697 | 2013-05-23 04:10:21 | [diff] [blame] | 279 | void CryptoTestUtils::CommunicateHandshakeMessages( |
| 280 | PacketSavingConnection* a_conn, |
| 281 | QuicCryptoStream* a, |
| 282 | PacketSavingConnection* b_conn, |
| 283 | QuicCryptoStream* b) { |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 284 | CommunicateHandshakeMessagesAndRunCallbacks(a_conn, a, b_conn, b, nullptr); |
[email protected] | 6fc79ea | 2014-07-10 04:30:23 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | // static |
[email protected] | dc6094a | 2014-07-23 01:50:04 | [diff] [blame] | 288 | void CryptoTestUtils::CommunicateHandshakeMessagesAndRunCallbacks( |
[email protected] | 6fc79ea | 2014-07-10 04:30:23 | [diff] [blame] | 289 | PacketSavingConnection* a_conn, |
| 290 | QuicCryptoStream* a, |
| 291 | PacketSavingConnection* b_conn, |
| 292 | QuicCryptoStream* b, |
[email protected] | dc6094a | 2014-07-23 01:50:04 | [diff] [blame] | 293 | CallbackSource* callback_source) { |
[email protected] | 0bbeb697 | 2013-05-23 04:10:21 | [diff] [blame] | 294 | size_t a_i = 0, b_i = 0; |
| 295 | while (!a->handshake_confirmed()) { |
| 296 | ASSERT_GT(a_conn->packets_.size(), a_i); |
[email protected] | b46d699 | 2013-11-25 19:30:52 | [diff] [blame] | 297 | LOG(INFO) << "Processing " << a_conn->packets_.size() - a_i |
[email protected] | 0bbeb697 | 2013-05-23 04:10:21 | [diff] [blame] | 298 | << " packets a->b"; |
| 299 | MovePackets(a_conn, &a_i, b, b_conn); |
[email protected] | dc6094a | 2014-07-23 01:50:04 | [diff] [blame] | 300 | if (callback_source) { |
| 301 | callback_source->RunPendingCallbacks(); |
[email protected] | 6fc79ea | 2014-07-10 04:30:23 | [diff] [blame] | 302 | } |
[email protected] | 0bbeb697 | 2013-05-23 04:10:21 | [diff] [blame] | 303 | |
| 304 | ASSERT_GT(b_conn->packets_.size(), b_i); |
[email protected] | b46d699 | 2013-11-25 19:30:52 | [diff] [blame] | 305 | LOG(INFO) << "Processing " << b_conn->packets_.size() - b_i |
[email protected] | 0bbeb697 | 2013-05-23 04:10:21 | [diff] [blame] | 306 | << " packets b->a"; |
[email protected] | 0bbeb697 | 2013-05-23 04:10:21 | [diff] [blame] | 307 | MovePackets(b_conn, &b_i, a, a_conn); |
[email protected] | dc6094a | 2014-07-23 01:50:04 | [diff] [blame] | 308 | if (callback_source) { |
| 309 | callback_source->RunPendingCallbacks(); |
[email protected] | 6fc79ea | 2014-07-10 04:30:23 | [diff] [blame] | 310 | } |
[email protected] | 0bbeb697 | 2013-05-23 04:10:21 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
[email protected] | b694e48c | 2014-03-18 17:10:13 | [diff] [blame] | 314 | // static |
[email protected] | 691f45a98 | 2013-11-19 10:52:04 | [diff] [blame] | 315 | pair<size_t, size_t> CryptoTestUtils::AdvanceHandshake( |
| 316 | PacketSavingConnection* a_conn, |
| 317 | QuicCryptoStream* a, |
| 318 | size_t a_i, |
| 319 | PacketSavingConnection* b_conn, |
| 320 | QuicCryptoStream* b, |
| 321 | size_t b_i) { |
[email protected] | b46d699 | 2013-11-25 19:30:52 | [diff] [blame] | 322 | LOG(INFO) << "Processing " << a_conn->packets_.size() - a_i |
[email protected] | 691f45a98 | 2013-11-19 10:52:04 | [diff] [blame] | 323 | << " packets a->b"; |
| 324 | MovePackets(a_conn, &a_i, b, b_conn); |
| 325 | |
[email protected] | b46d699 | 2013-11-25 19:30:52 | [diff] [blame] | 326 | LOG(INFO) << "Processing " << b_conn->packets_.size() - b_i |
[email protected] | 691f45a98 | 2013-11-19 10:52:04 | [diff] [blame] | 327 | << " packets b->a"; |
| 328 | if (b_conn->packets_.size() - b_i == 2) { |
[email protected] | b46d699 | 2013-11-25 19:30:52 | [diff] [blame] | 329 | LOG(INFO) << "here"; |
[email protected] | 691f45a98 | 2013-11-19 10:52:04 | [diff] [blame] | 330 | } |
| 331 | MovePackets(b_conn, &b_i, a, a_conn); |
| 332 | |
| 333 | return make_pair(a_i, b_i); |
| 334 | } |
| 335 | |
[email protected] | 0bbeb697 | 2013-05-23 04:10:21 | [diff] [blame] | 336 | // static |
[email protected] | ccc66e8a | 2013-03-26 08:26:14 | [diff] [blame] | 337 | string CryptoTestUtils::GetValueForTag(const CryptoHandshakeMessage& message, |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 338 | QuicTag tag) { |
| 339 | QuicTagValueMap::const_iterator it = message.tag_value_map().find(tag); |
[email protected] | ccc66e8a | 2013-03-26 08:26:14 | [diff] [blame] | 340 | if (it == message.tag_value_map().end()) { |
| 341 | return string(); |
| 342 | } |
| 343 | return it->second; |
| 344 | } |
| 345 | |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 346 | class MockCommonCertSets : public CommonCertSets { |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 347 | public: |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 348 | MockCommonCertSets(StringPiece cert, uint64 hash, uint32 index) |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 349 | : cert_(cert.as_string()), |
| 350 | hash_(hash), |
| 351 | index_(index) { |
| 352 | } |
| 353 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 354 | StringPiece GetCommonHashes() const override { |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 355 | CHECK(false) << "not implemented"; |
| 356 | return StringPiece(); |
| 357 | } |
| 358 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 359 | StringPiece GetCert(uint64 hash, uint32 index) const override { |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 360 | if (hash == hash_ && index == index_) { |
| 361 | return cert_; |
| 362 | } |
| 363 | return StringPiece(); |
| 364 | } |
| 365 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 366 | bool MatchCert(StringPiece cert, |
| 367 | StringPiece common_set_hashes, |
| 368 | uint64* out_hash, |
| 369 | uint32* out_index) const override { |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 370 | if (cert != cert_) { |
| 371 | return false; |
| 372 | } |
| 373 | |
| 374 | if (common_set_hashes.size() % sizeof(uint64) != 0) { |
| 375 | return false; |
| 376 | } |
| 377 | bool client_has_set = false; |
| 378 | for (size_t i = 0; i < common_set_hashes.size(); i += sizeof(uint64)) { |
| 379 | uint64 hash; |
| 380 | memcpy(&hash, common_set_hashes.data() + i, sizeof(hash)); |
| 381 | if (hash == hash_) { |
| 382 | client_has_set = true; |
| 383 | break; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | if (!client_has_set) { |
| 388 | return false; |
| 389 | } |
| 390 | |
| 391 | *out_hash = hash_; |
| 392 | *out_index = index_; |
| 393 | return true; |
| 394 | } |
| 395 | |
| 396 | private: |
| 397 | const string cert_; |
| 398 | const uint64 hash_; |
| 399 | const uint32 index_; |
| 400 | }; |
| 401 | |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 402 | CommonCertSets* CryptoTestUtils::MockCommonCertSets(StringPiece cert, |
[email protected] | 89995165 | 2013-05-16 12:52:39 | [diff] [blame] | 403 | uint64 hash, |
| 404 | uint32 index) { |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 405 | return new class MockCommonCertSets(cert, hash, index); |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 406 | } |
| 407 | |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 408 | void CryptoTestUtils::CompareClientAndServerKeys( |
| 409 | QuicCryptoClientStream* client, |
| 410 | QuicCryptoServerStream* server) { |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 411 | const QuicEncrypter* client_encrypter( |
| 412 | client->session()->connection()->encrypter(ENCRYPTION_INITIAL)); |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 413 | const QuicDecrypter* client_decrypter( |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 414 | client->session()->connection()->decrypter()); |
| 415 | const QuicEncrypter* client_forward_secure_encrypter( |
| 416 | client->session()->connection()->encrypter(ENCRYPTION_FORWARD_SECURE)); |
| 417 | const QuicDecrypter* client_forward_secure_decrypter( |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 418 | client->session()->connection()->alternative_decrypter()); |
| 419 | const QuicEncrypter* server_encrypter( |
| 420 | server->session()->connection()->encrypter(ENCRYPTION_INITIAL)); |
| 421 | const QuicDecrypter* server_decrypter( |
| 422 | server->session()->connection()->decrypter()); |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 423 | const QuicEncrypter* server_forward_secure_encrypter( |
| 424 | server->session()->connection()->encrypter(ENCRYPTION_FORWARD_SECURE)); |
| 425 | const QuicDecrypter* server_forward_secure_decrypter( |
| 426 | server->session()->connection()->alternative_decrypter()); |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 427 | |
| 428 | StringPiece client_encrypter_key = client_encrypter->GetKey(); |
| 429 | StringPiece client_encrypter_iv = client_encrypter->GetNoncePrefix(); |
| 430 | StringPiece client_decrypter_key = client_decrypter->GetKey(); |
| 431 | StringPiece client_decrypter_iv = client_decrypter->GetNoncePrefix(); |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 432 | StringPiece client_forward_secure_encrypter_key = |
| 433 | client_forward_secure_encrypter->GetKey(); |
| 434 | StringPiece client_forward_secure_encrypter_iv = |
| 435 | client_forward_secure_encrypter->GetNoncePrefix(); |
| 436 | StringPiece client_forward_secure_decrypter_key = |
| 437 | client_forward_secure_decrypter->GetKey(); |
| 438 | StringPiece client_forward_secure_decrypter_iv = |
| 439 | client_forward_secure_decrypter->GetNoncePrefix(); |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 440 | StringPiece server_encrypter_key = server_encrypter->GetKey(); |
| 441 | StringPiece server_encrypter_iv = server_encrypter->GetNoncePrefix(); |
| 442 | StringPiece server_decrypter_key = server_decrypter->GetKey(); |
| 443 | StringPiece server_decrypter_iv = server_decrypter->GetNoncePrefix(); |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 444 | StringPiece server_forward_secure_encrypter_key = |
| 445 | server_forward_secure_encrypter->GetKey(); |
| 446 | StringPiece server_forward_secure_encrypter_iv = |
| 447 | server_forward_secure_encrypter->GetNoncePrefix(); |
| 448 | StringPiece server_forward_secure_decrypter_key = |
| 449 | server_forward_secure_decrypter->GetKey(); |
| 450 | StringPiece server_forward_secure_decrypter_iv = |
| 451 | server_forward_secure_decrypter->GetNoncePrefix(); |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 452 | |
[email protected] | 2fe8b63 | 2014-07-31 11:36:37 | [diff] [blame] | 453 | StringPiece client_subkey_secret = |
| 454 | client->crypto_negotiated_params().subkey_secret; |
| 455 | StringPiece server_subkey_secret = |
| 456 | server->crypto_negotiated_params().subkey_secret; |
| 457 | |
| 458 | |
| 459 | const char kSampleLabel[] = "label"; |
| 460 | const char kSampleContext[] = "context"; |
| 461 | const size_t kSampleOutputLength = 32; |
| 462 | string client_key_extraction; |
| 463 | string server_key_extraction; |
| 464 | EXPECT_TRUE(client->ExportKeyingMaterial(kSampleLabel, |
| 465 | kSampleContext, |
| 466 | kSampleOutputLength, |
| 467 | &client_key_extraction)); |
| 468 | EXPECT_TRUE(server->ExportKeyingMaterial(kSampleLabel, |
| 469 | kSampleContext, |
| 470 | kSampleOutputLength, |
| 471 | &server_key_extraction)); |
| 472 | |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 473 | CompareCharArraysWithHexError("client write key", |
| 474 | client_encrypter_key.data(), |
| 475 | client_encrypter_key.length(), |
| 476 | server_decrypter_key.data(), |
| 477 | server_decrypter_key.length()); |
| 478 | CompareCharArraysWithHexError("client write IV", |
| 479 | client_encrypter_iv.data(), |
| 480 | client_encrypter_iv.length(), |
| 481 | server_decrypter_iv.data(), |
| 482 | server_decrypter_iv.length()); |
| 483 | CompareCharArraysWithHexError("server write key", |
| 484 | server_encrypter_key.data(), |
| 485 | server_encrypter_key.length(), |
| 486 | client_decrypter_key.data(), |
| 487 | client_decrypter_key.length()); |
| 488 | CompareCharArraysWithHexError("server write IV", |
| 489 | server_encrypter_iv.data(), |
| 490 | server_encrypter_iv.length(), |
| 491 | client_decrypter_iv.data(), |
| 492 | client_decrypter_iv.length()); |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 493 | CompareCharArraysWithHexError("client forward secure write key", |
| 494 | client_forward_secure_encrypter_key.data(), |
| 495 | client_forward_secure_encrypter_key.length(), |
| 496 | server_forward_secure_decrypter_key.data(), |
| 497 | server_forward_secure_decrypter_key.length()); |
| 498 | CompareCharArraysWithHexError("client forward secure write IV", |
| 499 | client_forward_secure_encrypter_iv.data(), |
| 500 | client_forward_secure_encrypter_iv.length(), |
| 501 | server_forward_secure_decrypter_iv.data(), |
| 502 | server_forward_secure_decrypter_iv.length()); |
| 503 | CompareCharArraysWithHexError("server forward secure write key", |
| 504 | server_forward_secure_encrypter_key.data(), |
| 505 | server_forward_secure_encrypter_key.length(), |
| 506 | client_forward_secure_decrypter_key.data(), |
| 507 | client_forward_secure_decrypter_key.length()); |
| 508 | CompareCharArraysWithHexError("server forward secure write IV", |
| 509 | server_forward_secure_encrypter_iv.data(), |
| 510 | server_forward_secure_encrypter_iv.length(), |
| 511 | client_forward_secure_decrypter_iv.data(), |
| 512 | client_forward_secure_decrypter_iv.length()); |
[email protected] | 2fe8b63 | 2014-07-31 11:36:37 | [diff] [blame] | 513 | CompareCharArraysWithHexError("subkey secret", |
| 514 | client_subkey_secret.data(), |
| 515 | client_subkey_secret.length(), |
| 516 | server_subkey_secret.data(), |
| 517 | server_subkey_secret.length()); |
| 518 | CompareCharArraysWithHexError("sample key extraction", |
| 519 | client_key_extraction.data(), |
| 520 | client_key_extraction.length(), |
| 521 | server_key_extraction.data(), |
| 522 | server_key_extraction.length()); |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 523 | } |
[email protected] | 0bbeb697 | 2013-05-23 04:10:21 | [diff] [blame] | 524 | |
| 525 | // static |
| 526 | QuicTag CryptoTestUtils::ParseTag(const char* tagstr) { |
| 527 | const size_t len = strlen(tagstr); |
| 528 | CHECK_NE(0u, len); |
| 529 | |
| 530 | QuicTag tag = 0; |
| 531 | |
| 532 | if (tagstr[0] == '#') { |
| 533 | CHECK_EQ(static_cast<size_t>(1 + 2*4), len); |
| 534 | tagstr++; |
| 535 | |
| 536 | for (size_t i = 0; i < 8; i++) { |
| 537 | tag <<= 4; |
| 538 | |
| 539 | uint8 v = 0; |
| 540 | CHECK(HexChar(tagstr[i], &v)); |
| 541 | tag |= v; |
| 542 | } |
| 543 | |
| 544 | return tag; |
| 545 | } |
| 546 | |
| 547 | CHECK_LE(len, 4u); |
| 548 | for (size_t i = 0; i < 4; i++) { |
| 549 | tag >>= 8; |
| 550 | if (i < len) { |
| 551 | tag |= static_cast<uint32>(tagstr[i]) << 24; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | return tag; |
| 556 | } |
| 557 | |
| 558 | // static |
| 559 | CryptoHandshakeMessage CryptoTestUtils::Message(const char* message_tag, ...) { |
| 560 | va_list ap; |
| 561 | va_start(ap, message_tag); |
| 562 | |
| 563 | CryptoHandshakeMessage message = BuildMessage(message_tag, ap); |
| 564 | va_end(ap); |
| 565 | return message; |
| 566 | } |
| 567 | |
| 568 | // static |
| 569 | CryptoHandshakeMessage CryptoTestUtils::BuildMessage(const char* message_tag, |
| 570 | va_list ap) { |
| 571 | CryptoHandshakeMessage msg; |
| 572 | msg.set_tag(ParseTag(message_tag)); |
| 573 | |
| 574 | for (;;) { |
| 575 | const char* tagstr = va_arg(ap, const char*); |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 576 | if (tagstr == nullptr) { |
[email protected] | 0bbeb697 | 2013-05-23 04:10:21 | [diff] [blame] | 577 | break; |
| 578 | } |
| 579 | |
[email protected] | 4e49b6a | 2013-06-18 16:39:28 | [diff] [blame] | 580 | if (tagstr[0] == '$') { |
| 581 | // Special value. |
| 582 | const char* const special = tagstr + 1; |
| 583 | if (strcmp(special, "padding") == 0) { |
| 584 | const int min_bytes = va_arg(ap, int); |
| 585 | msg.set_minimum_size(min_bytes); |
| 586 | } else { |
| 587 | CHECK(false) << "Unknown special value: " << special; |
| 588 | } |
| 589 | |
| 590 | continue; |
| 591 | } |
| 592 | |
[email protected] | 0bbeb697 | 2013-05-23 04:10:21 | [diff] [blame] | 593 | const QuicTag tag = ParseTag(tagstr); |
| 594 | const char* valuestr = va_arg(ap, const char*); |
| 595 | |
| 596 | size_t len = strlen(valuestr); |
| 597 | if (len > 0 && valuestr[0] == '#') { |
| 598 | valuestr++; |
| 599 | len--; |
| 600 | |
[email protected] | 257f24f | 2014-04-01 09:15:37 | [diff] [blame] | 601 | CHECK_EQ(0u, len % 2); |
[email protected] | 0bbeb697 | 2013-05-23 04:10:21 | [diff] [blame] | 602 | scoped_ptr<uint8[]> buf(new uint8[len/2]); |
| 603 | |
| 604 | for (size_t i = 0; i < len/2; i++) { |
| 605 | uint8 v = 0; |
| 606 | CHECK(HexChar(valuestr[i*2], &v)); |
| 607 | buf[i] = v << 4; |
| 608 | CHECK(HexChar(valuestr[i*2 + 1], &v)); |
| 609 | buf[i] |= v; |
| 610 | } |
| 611 | |
| 612 | msg.SetStringPiece( |
| 613 | tag, StringPiece(reinterpret_cast<char*>(buf.get()), len/2)); |
| 614 | continue; |
| 615 | } |
| 616 | |
| 617 | msg.SetStringPiece(tag, valuestr); |
| 618 | } |
| 619 | |
[email protected] | 4e49b6a | 2013-06-18 16:39:28 | [diff] [blame] | 620 | // The CryptoHandshakeMessage needs to be serialized and parsed to ensure |
| 621 | // that any padding is included. |
| 622 | scoped_ptr<QuicData> bytes(CryptoFramer::ConstructHandshakeMessage(msg)); |
| 623 | scoped_ptr<CryptoHandshakeMessage> parsed( |
| 624 | CryptoFramer::ParseMessage(bytes->AsStringPiece())); |
| 625 | CHECK(parsed.get()); |
| 626 | |
| 627 | return *parsed; |
[email protected] | 0bbeb697 | 2013-05-23 04:10:21 | [diff] [blame] | 628 | } |
| 629 | |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 630 | } // namespace test |
| 631 | } // namespace net |