blob: 814be7b41f58bf75b2a8abc61b1920b27739910b [file] [log] [blame]
[email protected]ed3fc15d2013-03-08 18:37:441// 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]72818ea2013-03-13 03:23:576
[email protected]38b3fd12013-06-18 08:19:017#include "net/quic/crypto/channel_id.h"
[email protected]c244c5a12013-05-07 20:55:048#include "net/quic/crypto/common_cert_set.h"
[email protected]ef95114d2013-04-17 17:57:019#include "net/quic/crypto/crypto_handshake.h"
[email protected]8e01c062013-10-31 07:35:3110#include "net/quic/crypto/quic_crypto_server_config.h"
[email protected]14e8106c2013-03-14 16:25:3311#include "net/quic/crypto/quic_decrypter.h"
12#include "net/quic/crypto/quic_encrypter.h"
[email protected]ef95114d2013-04-17 17:57:0113#include "net/quic/crypto/quic_random.h"
14#include "net/quic/quic_clock.h"
[email protected]ed3fc15d2013-03-08 18:37:4415#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]257f24f2014-04-01 09:15:3718#include "net/quic/quic_server_id.h"
[email protected]2532de12013-05-09 12:29:3319#include "net/quic/test_tools/quic_connection_peer.h"
[email protected]ed3fc15d2013-03-08 18:37:4420#include "net/quic/test_tools/quic_test_utils.h"
21#include "net/quic/test_tools/simple_quic_framer.h"
[email protected]ed3fc15d2013-03-08 18:37:4422
[email protected]14e8106c2013-03-14 16:25:3323using base::StringPiece;
[email protected]691f45a982013-11-19 10:52:0424using std::make_pair;
25using std::pair;
[email protected]ccc66e8a2013-03-26 08:26:1426using std::string;
[email protected]fe053f92013-04-23 20:18:5527using std::vector;
[email protected]14e8106c2013-03-14 16:25:3328
[email protected]ed3fc15d2013-03-08 18:37:4429namespace net {
30namespace test {
31
32namespace {
33
[email protected]e4c3ea62014-03-15 00:45:1434const char kServerHostname[] = "test.example.com";
35const uint16 kServerPort = 80;
36
[email protected]fe053f92013-04-23 20:18:5537// CryptoFramerVisitor is a framer visitor that records handshake messages.
38class CryptoFramerVisitor : public CryptoFramerVisitorInterface {
39 public:
40 CryptoFramerVisitor()
41 : error_(false) {
42 }
[email protected]ed3fc15d2013-03-08 18:37:4443
dchengb03027d2014-10-21 12:00:2044 void OnError(CryptoFramer* framer) override { error_ = true; }
[email protected]ed3fc15d2013-03-08 18:37:4445
dchengb03027d2014-10-21 12:00:2046 void OnHandshakeMessage(const CryptoHandshakeMessage& message) override {
[email protected]fe053f92013-04-23 20:18:5547 messages_.push_back(message);
48 }
[email protected]ed3fc15d2013-03-08 18:37:4449
[email protected]fe053f92013-04-23 20:18:5550 bool error() const {
51 return error_;
52 }
[email protected]ed3fc15d2013-03-08 18:37:4453
[email protected]fe053f92013-04-23 20:18:5554 const vector<CryptoHandshakeMessage>& messages() const {
55 return messages_;
56 }
[email protected]ed3fc15d2013-03-08 18:37:4457
[email protected]fe053f92013-04-23 20:18:5558 private:
59 bool error_;
60 vector<CryptoHandshakeMessage> messages_;
61};
62
63// MovePackets parses crypto handshake messages from packet number
[email protected]9693157b2014-08-08 11:13:4964// |*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]fe053f92013-04-23 20:18:5567void MovePackets(PacketSavingConnection* source_conn,
68 size_t *inout_packet_index,
[email protected]2532de12013-05-09 12:29:3369 QuicCryptoStream* dest_stream,
70 PacketSavingConnection* dest_conn) {
[email protected]4d640792013-12-18 22:21:0871 SimpleQuicFramer framer(source_conn->supported_versions());
[email protected]fe053f92013-04-23 20:18:5572 CryptoFramer crypto_framer;
73 CryptoFramerVisitor crypto_visitor;
74
[email protected]2532de12013-05-09 12:29:3375 // 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]fe053f92013-04-23 20:18:5582 crypto_framer.set_visitor(&crypto_visitor);
83
84 size_t index = *inout_packet_index;
[email protected]2532de12013-05-09 12:29:3385 for (; index < source_conn->encrypted_packets_.size(); index++) {
[email protected]9693157b2014-08-08 11:13:4986 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]fe053f92013-04-23 20:18:5592 for (vector<QuicStreamFrame>::const_iterator
93 i = framer.stream_frames().begin();
94 i != framer.stream_frames().end(); ++i) {
[email protected]5dafdb62013-11-14 01:24:2695 scoped_ptr<string> frame_data(i->GetDataAsString());
96 ASSERT_TRUE(crypto_framer.ProcessInput(*frame_data));
[email protected]fe053f92013-04-23 20:18:5597 ASSERT_FALSE(crypto_visitor.error());
98 }
99 }
100 *inout_packet_index = index;
101
[email protected]2532de12013-05-09 12:29:33102 QuicConnectionPeer::SwapCrypters(dest_conn, framer.framer());
103
[email protected]fe053f92013-04-23 20:18:55104 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]ed3fc15d2013-03-08 18:37:44110 }
111}
112
[email protected]0bbeb6972013-05-23 04:10:21113// 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.
115bool 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]b064310782013-05-30 21:12:17121 *value = c - 'a' + 10;
[email protected]0bbeb6972013-05-23 04:10:21122 return true;
123 }
124 if (c >= 'A' && c <= 'F') {
[email protected]b064310782013-05-30 21:12:17125 *value = c - 'A' + 10;
[email protected]0bbeb6972013-05-23 04:10:21126 return true;
127 }
128 return false;
129}
130
[email protected]6fc79ea2014-07-10 04:30:23131// A ChannelIDSource that works in asynchronous mode unless the |callback|
rtennetibe635732014-10-02 22:51:42132// argument to GetChannelIDKey is nullptr.
[email protected]6fc79ea2014-07-10 04:30:23133class AsyncTestChannelIDSource : public ChannelIDSource,
[email protected]dc6094a2014-07-23 01:50:04134 public CryptoTestUtils::CallbackSource {
[email protected]6fc79ea2014-07-10 04:30:23135 public:
136 // Takes ownership of |sync_source|, a synchronous ChannelIDSource.
137 explicit AsyncTestChannelIDSource(ChannelIDSource* sync_source)
138 : sync_source_(sync_source) {}
dchengb03027d2014-10-21 12:00:20139 ~AsyncTestChannelIDSource() override {}
[email protected]6fc79ea2014-07-10 04:30:23140
141 // ChannelIDSource implementation.
dchengb03027d2014-10-21 12:00:20142 QuicAsyncStatus GetChannelIDKey(const string& hostname,
143 scoped_ptr<ChannelIDKey>* channel_id_key,
144 ChannelIDSourceCallback* callback) override {
[email protected]6fc79ea2014-07-10 04:30:23145 // Synchronous mode.
146 if (!callback) {
rtennetibe635732014-10-02 22:51:42147 return sync_source_->GetChannelIDKey(hostname, channel_id_key, nullptr);
[email protected]6fc79ea2014-07-10 04:30:23148 }
149
150 // Asynchronous mode.
151 QuicAsyncStatus status =
rtennetibe635732014-10-02 22:51:42152 sync_source_->GetChannelIDKey(hostname, &channel_id_key_, nullptr);
[email protected]6fc79ea2014-07-10 04:30:23153 if (status != QUIC_SUCCESS) {
154 return QUIC_FAILURE;
155 }
156 callback_.reset(callback);
157 return QUIC_PENDING;
158 }
159
[email protected]dc6094a2014-07-23 01:50:04160 // CallbackSource implementation.
dchengb03027d2014-10-21 12:00:20161 void RunPendingCallbacks() override {
[email protected]6fc79ea2014-07-10 04:30:23162 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]ed3fc15d2013-03-08 18:37:44174} // anonymous namespace
175
[email protected]899951652013-05-16 12:52:39176CryptoTestUtils::FakeClientOptions::FakeClientOptions()
[email protected]b064310782013-05-30 21:12:17177 : dont_verify_certs(false),
[email protected]6fc79ea2014-07-10 04:30:23178 channel_id_enabled(false),
179 channel_id_source_async(false) {
[email protected]899951652013-05-16 12:52:39180}
181
[email protected]ed3fc15d2013-03-08 18:37:44182// static
[email protected]fe053f92013-04-23 20:18:55183int CryptoTestUtils::HandshakeWithFakeServer(
[email protected]ed3fc15d2013-03-08 18:37:44184 PacketSavingConnection* client_conn,
[email protected]14e8106c2013-03-14 16:25:33185 QuicCryptoClientStream* client) {
[email protected]4d640792013-12-18 22:21:08186 PacketSavingConnection* server_conn =
187 new PacketSavingConnection(true, client_conn->supported_versions());
[email protected]c05a6d222013-12-16 19:42:03188 TestSession server_session(server_conn, DefaultQuicConfig());
[email protected]ccb34212014-07-18 09:27:50189 server_session.InitializeSession();
[email protected]b064310782013-05-30 21:12:17190 QuicCryptoServerConfig crypto_config(QuicCryptoServerConfig::TESTING,
191 QuicRandom::GetInstance());
[email protected]ccb34212014-07-18 09:27:50192
[email protected]ef95114d2013-04-17 17:57:01193 SetupCryptoServerConfigForTest(
194 server_session.connection()->clock(),
195 server_session.connection()->random_generator(),
[email protected]899951652013-05-16 12:52:39196 server_session.config(), &crypto_config);
[email protected]ef95114d2013-04-17 17:57:01197
[email protected]899951652013-05-16 12:52:39198 QuicCryptoServerStream server(crypto_config, &server_session);
[email protected]2532de12013-05-09 12:29:33199 server_session.SetCryptoStream(&server);
[email protected]ed3fc15d2013-03-08 18:37:44200
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]14e8106c2013-03-14 16:25:33205
206 CompareClientAndServerKeys(client, &server);
[email protected]fe053f92013-04-23 20:18:55207
208 return client->num_sent_client_hellos();
[email protected]ed3fc15d2013-03-08 18:37:44209}
210
211// static
[email protected]fe053f92013-04-23 20:18:55212int CryptoTestUtils::HandshakeWithFakeClient(
[email protected]ed3fc15d2013-03-08 18:37:44213 PacketSavingConnection* server_conn,
[email protected]899951652013-05-16 12:52:39214 QuicCryptoServerStream* server,
215 const FakeClientOptions& options) {
[email protected]c05a6d222013-12-16 19:42:03216 PacketSavingConnection* client_conn = new PacketSavingConnection(false);
[email protected]90f62f092014-03-24 02:41:23217 TestClientSession client_session(client_conn, DefaultQuicConfig());
[email protected]ef95114d2013-04-17 17:57:01218 QuicCryptoClientConfig crypto_config;
219
[email protected]0cceb922014-07-01 02:00:56220 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;
rtennetibe635732014-10-02 22:51:42226 AsyncTestChannelIDSource* async_channel_id_source = nullptr;
[email protected]b064310782013-05-30 21:12:17227 if (options.channel_id_enabled) {
[email protected]0cceb922014-07-01 02:00:56228 is_https = true;
[email protected]6fc79ea2014-07-10 04:30:23229
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]b064310782013-05-30 21:12:17236 }
[email protected]0cceb922014-07-01 02:00:56237 QuicServerId server_id(kServerHostname, kServerPort, is_https,
[email protected]257f24f2014-04-01 09:15:37238 PRIVACY_MODE_DISABLED);
[email protected]0cceb922014-07-01 02:00:56239 QuicCryptoClientStream client(server_id, &client_session,
240 ProofVerifyContextForTesting(),
[email protected]b694e48c2014-03-18 17:10:13241 &crypto_config);
[email protected]2532de12013-05-09 12:29:33242 client_session.SetCryptoStream(&client);
[email protected]ed3fc15d2013-03-08 18:37:44243
244 CHECK(client.CryptoConnect());
245 CHECK_EQ(1u, client_conn->packets_.size());
246
[email protected]dc6094a2014-07-23 01:50:04247 CommunicateHandshakeMessagesAndRunCallbacks(
[email protected]6fc79ea2014-07-10 04:30:23248 client_conn, &client, server_conn, server, async_channel_id_source);
[email protected]14e8106c2013-03-14 16:25:33249
250 CompareClientAndServerKeys(&client, server);
[email protected]fe053f92013-04-23 20:18:55251
[email protected]38b3fd12013-06-18 08:19:01252 if (options.channel_id_enabled) {
[email protected]03dd32532014-05-30 07:11:25253 scoped_ptr<ChannelIDKey> channel_id_key;
rtennetibe635732014-10-02 22:51:42254 QuicAsyncStatus status = crypto_config.channel_id_source()->GetChannelIDKey(
255 kServerHostname, &channel_id_key, nullptr);
[email protected]05bfc260f2014-06-07 06:31:25256 EXPECT_EQ(QUIC_SUCCESS, status);
[email protected]03dd32532014-05-30 07:11:25257 EXPECT_EQ(channel_id_key->SerializeKey(),
258 server->crypto_negotiated_params().channel_id);
[email protected]dc6094a2014-07-23 01:50:04259 EXPECT_EQ(options.channel_id_source_async,
260 client.WasChannelIDSourceCallbackRun());
[email protected]38b3fd12013-06-18 08:19:01261 }
262
[email protected]fe053f92013-04-23 20:18:55263 return client.num_sent_client_hellos();
[email protected]14e8106c2013-03-14 16:25:33264}
265
266// static
[email protected]ef95114d2013-04-17 17:57:01267void CryptoTestUtils::SetupCryptoServerConfigForTest(
268 const QuicClock* clock,
269 QuicRandom* rand,
270 QuicConfig* config,
271 QuicCryptoServerConfig* crypto_config) {
[email protected]b064310782013-05-30 21:12:17272 QuicCryptoServerConfig::ConfigOptions options;
273 options.channel_id_enabled = true;
[email protected]ef95114d2013-04-17 17:57:01274 scoped_ptr<CryptoHandshakeMessage> scfg(
[email protected]b064310782013-05-30 21:12:17275 crypto_config->AddDefaultConfig(rand, clock, options));
[email protected]ef95114d2013-04-17 17:57:01276}
277
278// static
[email protected]0bbeb6972013-05-23 04:10:21279void CryptoTestUtils::CommunicateHandshakeMessages(
280 PacketSavingConnection* a_conn,
281 QuicCryptoStream* a,
282 PacketSavingConnection* b_conn,
283 QuicCryptoStream* b) {
rtennetibe635732014-10-02 22:51:42284 CommunicateHandshakeMessagesAndRunCallbacks(a_conn, a, b_conn, b, nullptr);
[email protected]6fc79ea2014-07-10 04:30:23285}
286
287// static
[email protected]dc6094a2014-07-23 01:50:04288void CryptoTestUtils::CommunicateHandshakeMessagesAndRunCallbacks(
[email protected]6fc79ea2014-07-10 04:30:23289 PacketSavingConnection* a_conn,
290 QuicCryptoStream* a,
291 PacketSavingConnection* b_conn,
292 QuicCryptoStream* b,
[email protected]dc6094a2014-07-23 01:50:04293 CallbackSource* callback_source) {
[email protected]0bbeb6972013-05-23 04:10:21294 size_t a_i = 0, b_i = 0;
295 while (!a->handshake_confirmed()) {
296 ASSERT_GT(a_conn->packets_.size(), a_i);
[email protected]b46d6992013-11-25 19:30:52297 LOG(INFO) << "Processing " << a_conn->packets_.size() - a_i
[email protected]0bbeb6972013-05-23 04:10:21298 << " packets a->b";
299 MovePackets(a_conn, &a_i, b, b_conn);
[email protected]dc6094a2014-07-23 01:50:04300 if (callback_source) {
301 callback_source->RunPendingCallbacks();
[email protected]6fc79ea2014-07-10 04:30:23302 }
[email protected]0bbeb6972013-05-23 04:10:21303
304 ASSERT_GT(b_conn->packets_.size(), b_i);
[email protected]b46d6992013-11-25 19:30:52305 LOG(INFO) << "Processing " << b_conn->packets_.size() - b_i
[email protected]0bbeb6972013-05-23 04:10:21306 << " packets b->a";
[email protected]0bbeb6972013-05-23 04:10:21307 MovePackets(b_conn, &b_i, a, a_conn);
[email protected]dc6094a2014-07-23 01:50:04308 if (callback_source) {
309 callback_source->RunPendingCallbacks();
[email protected]6fc79ea2014-07-10 04:30:23310 }
[email protected]0bbeb6972013-05-23 04:10:21311 }
312}
313
[email protected]b694e48c2014-03-18 17:10:13314// static
[email protected]691f45a982013-11-19 10:52:04315pair<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]b46d6992013-11-25 19:30:52322 LOG(INFO) << "Processing " << a_conn->packets_.size() - a_i
[email protected]691f45a982013-11-19 10:52:04323 << " packets a->b";
324 MovePackets(a_conn, &a_i, b, b_conn);
325
[email protected]b46d6992013-11-25 19:30:52326 LOG(INFO) << "Processing " << b_conn->packets_.size() - b_i
[email protected]691f45a982013-11-19 10:52:04327 << " packets b->a";
328 if (b_conn->packets_.size() - b_i == 2) {
[email protected]b46d6992013-11-25 19:30:52329 LOG(INFO) << "here";
[email protected]691f45a982013-11-19 10:52:04330 }
331 MovePackets(b_conn, &b_i, a, a_conn);
332
333 return make_pair(a_i, b_i);
334}
335
[email protected]0bbeb6972013-05-23 04:10:21336// static
[email protected]ccc66e8a2013-03-26 08:26:14337string CryptoTestUtils::GetValueForTag(const CryptoHandshakeMessage& message,
[email protected]2532de12013-05-09 12:29:33338 QuicTag tag) {
339 QuicTagValueMap::const_iterator it = message.tag_value_map().find(tag);
[email protected]ccc66e8a2013-03-26 08:26:14340 if (it == message.tag_value_map().end()) {
341 return string();
342 }
343 return it->second;
344}
345
[email protected]2532de12013-05-09 12:29:33346class MockCommonCertSets : public CommonCertSets {
[email protected]c244c5a12013-05-07 20:55:04347 public:
[email protected]2532de12013-05-09 12:29:33348 MockCommonCertSets(StringPiece cert, uint64 hash, uint32 index)
[email protected]c244c5a12013-05-07 20:55:04349 : cert_(cert.as_string()),
350 hash_(hash),
351 index_(index) {
352 }
353
dchengb03027d2014-10-21 12:00:20354 StringPiece GetCommonHashes() const override {
[email protected]c244c5a12013-05-07 20:55:04355 CHECK(false) << "not implemented";
356 return StringPiece();
357 }
358
dchengb03027d2014-10-21 12:00:20359 StringPiece GetCert(uint64 hash, uint32 index) const override {
[email protected]c244c5a12013-05-07 20:55:04360 if (hash == hash_ && index == index_) {
361 return cert_;
362 }
363 return StringPiece();
364 }
365
dchengb03027d2014-10-21 12:00:20366 bool MatchCert(StringPiece cert,
367 StringPiece common_set_hashes,
368 uint64* out_hash,
369 uint32* out_index) const override {
[email protected]c244c5a12013-05-07 20:55:04370 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]2532de12013-05-09 12:29:33402CommonCertSets* CryptoTestUtils::MockCommonCertSets(StringPiece cert,
[email protected]899951652013-05-16 12:52:39403 uint64 hash,
404 uint32 index) {
[email protected]2532de12013-05-09 12:29:33405 return new class MockCommonCertSets(cert, hash, index);
[email protected]c244c5a12013-05-07 20:55:04406}
407
[email protected]14e8106c2013-03-14 16:25:33408void CryptoTestUtils::CompareClientAndServerKeys(
409 QuicCryptoClientStream* client,
410 QuicCryptoServerStream* server) {
[email protected]8ba81212013-05-03 13:11:48411 const QuicEncrypter* client_encrypter(
412 client->session()->connection()->encrypter(ENCRYPTION_INITIAL));
[email protected]8ba81212013-05-03 13:11:48413 const QuicDecrypter* client_decrypter(
[email protected]2532de12013-05-09 12:29:33414 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]8ba81212013-05-03 13:11:48418 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]2532de12013-05-09 12:29:33423 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]8ba81212013-05-03 13:11:48427
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]2532de12013-05-09 12:29:33432 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]8ba81212013-05-03 13:11:48440 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]2532de12013-05-09 12:29:33444 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]8ba81212013-05-03 13:11:48452
[email protected]2fe8b632014-07-31 11:36:37453 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]14e8106c2013-03-14 16:25:33473 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]2532de12013-05-09 12:29:33493 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]2fe8b632014-07-31 11:36:37513 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]ed3fc15d2013-03-08 18:37:44523}
[email protected]0bbeb6972013-05-23 04:10:21524
525// static
526QuicTag 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
559CryptoHandshakeMessage 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
569CryptoHandshakeMessage 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*);
rtennetibe635732014-10-02 22:51:42576 if (tagstr == nullptr) {
[email protected]0bbeb6972013-05-23 04:10:21577 break;
578 }
579
[email protected]4e49b6a2013-06-18 16:39:28580 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]0bbeb6972013-05-23 04:10:21593 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]257f24f2014-04-01 09:15:37601 CHECK_EQ(0u, len % 2);
[email protected]0bbeb6972013-05-23 04:10:21602 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]4e49b6a2013-06-18 16:39:28620 // 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]0bbeb6972013-05-23 04:10:21628}
629
[email protected]ed3fc15d2013-03-08 18:37:44630} // namespace test
631} // namespace net