[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] | d069c11a | 2013-04-13 00:01:55 | [diff] [blame] | 7 | #include "base/strings/string_piece.h" |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 8 | #include "net/quic/crypto/crypto_handshake.h" |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 9 | #include "net/quic/crypto/crypto_server_config.h" |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 10 | #include "net/quic/crypto/quic_decrypter.h" |
| 11 | #include "net/quic/crypto/quic_encrypter.h" |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 12 | #include "net/quic/crypto/quic_random.h" |
| 13 | #include "net/quic/quic_clock.h" |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 14 | #include "net/quic/quic_crypto_client_stream.h" |
| 15 | #include "net/quic/quic_crypto_server_stream.h" |
| 16 | #include "net/quic/quic_crypto_stream.h" |
| 17 | #include "net/quic/test_tools/quic_test_utils.h" |
| 18 | #include "net/quic/test_tools/simple_quic_framer.h" |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 19 | |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 20 | using base::StringPiece; |
[email protected] | ccc66e8a | 2013-03-26 08:26:14 | [diff] [blame] | 21 | using std::string; |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 22 | using std::vector; |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 23 | |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 24 | namespace net { |
| 25 | namespace test { |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | class TestSession : public QuicSession { |
| 30 | public: |
| 31 | TestSession(QuicConnection* connection, bool is_server) |
| 32 | : QuicSession(connection, is_server) { |
| 33 | } |
| 34 | |
| 35 | MOCK_METHOD1(CreateIncomingReliableStream, |
| 36 | ReliableQuicStream*(QuicStreamId id)); |
| 37 | MOCK_METHOD0(GetCryptoStream, QuicCryptoStream*()); |
| 38 | MOCK_METHOD0(CreateOutgoingReliableStream, ReliableQuicStream*()); |
| 39 | }; |
| 40 | |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 41 | // CryptoFramerVisitor is a framer visitor that records handshake messages. |
| 42 | class CryptoFramerVisitor : public CryptoFramerVisitorInterface { |
| 43 | public: |
| 44 | CryptoFramerVisitor() |
| 45 | : error_(false) { |
| 46 | } |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 47 | |
[email protected] | 4209190 | 2013-05-02 02:24:12 | [diff] [blame^] | 48 | virtual void OnError(CryptoFramer* framer) OVERRIDE { |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 49 | error_ = true; |
| 50 | } |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 51 | |
[email protected] | 4209190 | 2013-05-02 02:24:12 | [diff] [blame^] | 52 | virtual void OnHandshakeMessage( |
| 53 | const CryptoHandshakeMessage& message) OVERRIDE { |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 54 | messages_.push_back(message); |
| 55 | } |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 56 | |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 57 | bool error() const { |
| 58 | return error_; |
| 59 | } |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 60 | |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 61 | const vector<CryptoHandshakeMessage>& messages() const { |
| 62 | return messages_; |
| 63 | } |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 64 | |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 65 | private: |
| 66 | bool error_; |
| 67 | vector<CryptoHandshakeMessage> messages_; |
| 68 | }; |
| 69 | |
| 70 | // MovePackets parses crypto handshake messages from packet number |
| 71 | // |*inout_packet_index| through to the last packet and has |dest_stream| |
| 72 | // process them. |*inout_packet_index| is updated with an index one greater |
| 73 | // than the last packet processed. |
| 74 | void MovePackets(PacketSavingConnection* source_conn, |
| 75 | size_t *inout_packet_index, |
| 76 | QuicCryptoStream* dest_stream) { |
| 77 | SimpleQuicFramer framer; |
| 78 | CryptoFramer crypto_framer; |
| 79 | CryptoFramerVisitor crypto_visitor; |
| 80 | |
| 81 | crypto_framer.set_visitor(&crypto_visitor); |
| 82 | |
| 83 | size_t index = *inout_packet_index; |
| 84 | for (; index < source_conn->packets_.size(); index++) { |
| 85 | ASSERT_TRUE(framer.ProcessPacket(*source_conn->packets_[index])); |
| 86 | for (vector<QuicStreamFrame>::const_iterator |
| 87 | i = framer.stream_frames().begin(); |
| 88 | i != framer.stream_frames().end(); ++i) { |
| 89 | ASSERT_TRUE(crypto_framer.ProcessInput(i->data)); |
| 90 | ASSERT_FALSE(crypto_visitor.error()); |
| 91 | } |
| 92 | } |
| 93 | *inout_packet_index = index; |
| 94 | |
| 95 | ASSERT_EQ(0u, crypto_framer.InputBytesRemaining()); |
| 96 | |
| 97 | for (vector<CryptoHandshakeMessage>::const_iterator |
| 98 | i = crypto_visitor.messages().begin(); |
| 99 | i != crypto_visitor.messages().end(); ++i) { |
| 100 | dest_stream->OnHandshakeMessage(*i); |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
| 104 | } // anonymous namespace |
| 105 | |
| 106 | // static |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 107 | void CryptoTestUtils::CommunicateHandshakeMessages( |
| 108 | PacketSavingConnection* a_conn, |
| 109 | QuicCryptoStream* a, |
| 110 | PacketSavingConnection* b_conn, |
| 111 | QuicCryptoStream* b) { |
| 112 | size_t a_i = 0, b_i = 0; |
| 113 | while (!a->handshake_complete()) { |
| 114 | ASSERT_GT(a_conn->packets_.size(), a_i); |
| 115 | LOG(INFO) << "Processing " << a_conn->packets_.size() - a_i |
| 116 | << " packets a->b"; |
| 117 | MovePackets(a_conn, &a_i, b); |
| 118 | |
| 119 | ASSERT_GT(b_conn->packets_.size(), b_i); |
| 120 | LOG(INFO) << "Processing " << b_conn->packets_.size() - b_i |
| 121 | << " packets b->a"; |
| 122 | if (b_conn->packets_.size() - b_i == 2) { |
| 123 | LOG(INFO) << "here"; |
| 124 | } |
| 125 | MovePackets(b_conn, &b_i, a); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // static |
| 130 | int CryptoTestUtils::HandshakeWithFakeServer( |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 131 | PacketSavingConnection* client_conn, |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 132 | QuicCryptoClientStream* client) { |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 133 | QuicGuid guid(1); |
[email protected] | 72818ea | 2013-03-13 03:23:57 | [diff] [blame] | 134 | IPAddressNumber ip; |
| 135 | CHECK(ParseIPLiteralToNumber("192.0.2.33", &ip)); |
| 136 | IPEndPoint addr = IPEndPoint(ip, 1); |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 137 | PacketSavingConnection* server_conn = |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 138 | new PacketSavingConnection(guid, addr, true); |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 139 | TestSession server_session(server_conn, true); |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 140 | |
| 141 | QuicConfig config; |
| 142 | QuicCryptoServerConfig crypto_config(QuicCryptoServerConfig::TESTING); |
| 143 | SetupCryptoServerConfigForTest( |
| 144 | server_session.connection()->clock(), |
| 145 | server_session.connection()->random_generator(), |
| 146 | &config, &crypto_config); |
| 147 | |
| 148 | QuicCryptoServerStream server(config, crypto_config, &server_session); |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 149 | |
| 150 | // The client's handshake must have been started already. |
| 151 | CHECK_NE(0u, client_conn->packets_.size()); |
| 152 | |
| 153 | CommunicateHandshakeMessages(client_conn, client, server_conn, &server); |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 154 | |
| 155 | CompareClientAndServerKeys(client, &server); |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 156 | |
| 157 | return client->num_sent_client_hellos(); |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // static |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 161 | int CryptoTestUtils::HandshakeWithFakeClient( |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 162 | PacketSavingConnection* server_conn, |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 163 | QuicCryptoServerStream* server) { |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 164 | QuicGuid guid(1); |
[email protected] | 72818ea | 2013-03-13 03:23:57 | [diff] [blame] | 165 | IPAddressNumber ip; |
| 166 | CHECK(ParseIPLiteralToNumber("192.0.2.33", &ip)); |
| 167 | IPEndPoint addr = IPEndPoint(ip, 1); |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 168 | PacketSavingConnection* client_conn = |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 169 | new PacketSavingConnection(guid, addr, false); |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 170 | TestSession client_session(client_conn, true); |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 171 | QuicConfig config; |
| 172 | QuicCryptoClientConfig crypto_config; |
| 173 | |
| 174 | config.SetDefaults(); |
| 175 | crypto_config.SetDefaults(); |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 176 | // TODO(rtenneti): Enable testing of ProofVerifier. |
| 177 | // crypto_config.SetProofVerifier(ProofVerifierForTesting()); |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 178 | QuicCryptoClientStream client("test.example.com", config, &client_session, |
| 179 | &crypto_config); |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 180 | |
| 181 | CHECK(client.CryptoConnect()); |
| 182 | CHECK_EQ(1u, client_conn->packets_.size()); |
| 183 | |
| 184 | CommunicateHandshakeMessages(client_conn, &client, server_conn, server); |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 185 | |
| 186 | CompareClientAndServerKeys(&client, server); |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 187 | |
| 188 | return client.num_sent_client_hellos(); |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | // static |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 192 | void CryptoTestUtils::SetupCryptoServerConfigForTest( |
| 193 | const QuicClock* clock, |
| 194 | QuicRandom* rand, |
| 195 | QuicConfig* config, |
| 196 | QuicCryptoServerConfig* crypto_config) { |
| 197 | config->SetDefaults(); |
| 198 | CryptoHandshakeMessage extra_tags; |
| 199 | config->ToHandshakeMessage(&extra_tags); |
| 200 | |
| 201 | scoped_ptr<CryptoHandshakeMessage> scfg( |
| 202 | crypto_config->AddDefaultConfig(rand, clock, extra_tags)); |
| 203 | if (!config->SetFromHandshakeMessage(*scfg)) { |
| 204 | CHECK(false) << "Crypto config could not be parsed by QuicConfig."; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | // static |
[email protected] | ccc66e8a | 2013-03-26 08:26:14 | [diff] [blame] | 209 | string CryptoTestUtils::GetValueForTag(const CryptoHandshakeMessage& message, |
| 210 | CryptoTag tag) { |
| 211 | CryptoTagValueMap::const_iterator it = message.tag_value_map().find(tag); |
| 212 | if (it == message.tag_value_map().end()) { |
| 213 | return string(); |
| 214 | } |
| 215 | return it->second; |
| 216 | } |
| 217 | |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 218 | void CryptoTestUtils::CompareClientAndServerKeys( |
| 219 | QuicCryptoClientStream* client, |
| 220 | QuicCryptoServerStream* server) { |
| 221 | StringPiece client_encrypter_key = |
[email protected] | 74bda14 | 2013-03-31 02:49:11 | [diff] [blame] | 222 | client->session()->connection()->encrypter()->GetKey(); |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 223 | StringPiece client_encrypter_iv = |
[email protected] | 74bda14 | 2013-03-31 02:49:11 | [diff] [blame] | 224 | client->session()->connection()->encrypter()->GetNoncePrefix(); |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 225 | StringPiece client_decrypter_key = |
[email protected] | 74bda14 | 2013-03-31 02:49:11 | [diff] [blame] | 226 | client->session()->connection()->decrypter()->GetKey(); |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 227 | StringPiece client_decrypter_iv = |
[email protected] | 74bda14 | 2013-03-31 02:49:11 | [diff] [blame] | 228 | client->session()->connection()->decrypter()->GetNoncePrefix(); |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 229 | StringPiece server_encrypter_key = |
[email protected] | 74bda14 | 2013-03-31 02:49:11 | [diff] [blame] | 230 | server->session()->connection()->encrypter()->GetKey(); |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 231 | StringPiece server_encrypter_iv = |
[email protected] | 74bda14 | 2013-03-31 02:49:11 | [diff] [blame] | 232 | server->session()->connection()->encrypter()->GetNoncePrefix(); |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 233 | StringPiece server_decrypter_key = |
[email protected] | 74bda14 | 2013-03-31 02:49:11 | [diff] [blame] | 234 | server->session()->connection()->decrypter()->GetKey(); |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 235 | StringPiece server_decrypter_iv = |
[email protected] | 74bda14 | 2013-03-31 02:49:11 | [diff] [blame] | 236 | server->session()->connection()->decrypter()->GetNoncePrefix(); |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 237 | CompareCharArraysWithHexError("client write key", |
| 238 | client_encrypter_key.data(), |
| 239 | client_encrypter_key.length(), |
| 240 | server_decrypter_key.data(), |
| 241 | server_decrypter_key.length()); |
| 242 | CompareCharArraysWithHexError("client write IV", |
| 243 | client_encrypter_iv.data(), |
| 244 | client_encrypter_iv.length(), |
| 245 | server_decrypter_iv.data(), |
| 246 | server_decrypter_iv.length()); |
| 247 | CompareCharArraysWithHexError("server write key", |
| 248 | server_encrypter_key.data(), |
| 249 | server_encrypter_key.length(), |
| 250 | client_decrypter_key.data(), |
| 251 | client_decrypter_key.length()); |
| 252 | CompareCharArraysWithHexError("server write IV", |
| 253 | server_encrypter_iv.data(), |
| 254 | server_encrypter_iv.length(), |
| 255 | client_decrypter_iv.data(), |
| 256 | client_decrypter_iv.length()); |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 257 | } |
| 258 | } // namespace test |
| 259 | } // namespace net |