blob: d839c6c84d7a6c0b2322eac4bc6a32187788fe35 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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#ifndef QUICHE_QUIC_TEST_TOOLS_CRYPTO_TEST_UTILS_H_
6#define QUICHE_QUIC_TEST_TOOLS_CRYPTO_TEST_UTILS_H_
7
8#include <cstdarg>
9#include <cstddef>
10#include <cstdint>
11#include <utility>
12#include <vector>
13
vasilvv5f225b02020-10-08 11:49:09 -040014#include "absl/strings/string_view.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050015#include "third_party/boringssl/src/include/openssl/evp.h"
QUICHE team5be974e2020-12-29 18:35:24 -050016#include "quic/core/crypto/crypto_framer.h"
17#include "quic/core/quic_framer.h"
18#include "quic/core/quic_packets.h"
19#include "quic/test_tools/quic_test_utils.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050020
21namespace quic {
22
QUICHE teama6ef0a62019-03-07 20:34:33 -050023class CommonCertSets;
24class ProofSource;
25class ProofVerifier;
26class ProofVerifyContext;
27class QuicClock;
28class QuicConfig;
29class QuicCryptoClientStream;
30class QuicCryptoServerConfig;
nharperf579b5e2020-01-21 14:11:18 -080031class QuicCryptoServerStreamBase;
QUICHE teama6ef0a62019-03-07 20:34:33 -050032class QuicCryptoStream;
33class QuicRandom;
34class QuicServerId;
35
36namespace test {
37
38class PacketSavingConnection;
39
QUICHE teama6ef0a62019-03-07 20:34:33 -050040namespace crypto_test_utils {
41
42// An interface for a source of callbacks. This is used for invoking
43// callbacks asynchronously.
44//
45// Call the RunPendingCallbacks method regularly to run the callbacks from
46// this source.
47class CallbackSource {
48 public:
49 virtual ~CallbackSource() {}
50
51 // Runs pending callbacks from this source. If there is no pending
52 // callback, does nothing.
53 virtual void RunPendingCallbacks() = 0;
54};
55
QUICHE teama6ef0a62019-03-07 20:34:33 -050056// FakeClientOptions bundles together a number of options for configuring
57// HandshakeWithFakeClient.
58struct FakeClientOptions {
59 FakeClientOptions();
60 ~FakeClientOptions();
61
QUICHE teama6ef0a62019-03-07 20:34:33 -050062 // If only_tls_versions is set, then the client will only use TLS for the
63 // crypto handshake.
64 bool only_tls_versions = false;
dschinazidffd2982020-03-02 10:25:11 -080065
66 // If only_quic_crypto_versions is set, then the client will only use
67 // PROTOCOL_QUIC_CRYPTO for the crypto handshake.
68 bool only_quic_crypto_versions = false;
QUICHE teama6ef0a62019-03-07 20:34:33 -050069};
70
nharperdf7a77b2019-11-11 13:12:45 -080071// Returns a QuicCryptoServerConfig that is in a reasonable configuration to
72// pass into HandshakeWithFakeServer.
rch83f29bd2019-11-13 11:47:34 -080073std::unique_ptr<QuicCryptoServerConfig> CryptoServerConfigForTesting();
nharperdf7a77b2019-11-11 13:12:45 -080074
QUICHE teama6ef0a62019-03-07 20:34:33 -050075// returns: the number of client hellos that the client sent.
76int HandshakeWithFakeServer(QuicConfig* server_quic_config,
nharperdf7a77b2019-11-11 13:12:45 -080077 QuicCryptoServerConfig* crypto_config,
QUICHE teama6ef0a62019-03-07 20:34:33 -050078 MockQuicConnectionHelper* helper,
79 MockAlarmFactory* alarm_factory,
80 PacketSavingConnection* client_conn,
vasilvve6472f62019-10-02 06:50:56 -070081 QuicCryptoClientStream* client,
82 std::string alpn);
QUICHE teama6ef0a62019-03-07 20:34:33 -050083
84// returns: the number of client hellos that the client sent.
85int HandshakeWithFakeClient(MockQuicConnectionHelper* helper,
86 MockAlarmFactory* alarm_factory,
87 PacketSavingConnection* server_conn,
nharperf579b5e2020-01-21 14:11:18 -080088 QuicCryptoServerStreamBase* server,
QUICHE teama6ef0a62019-03-07 20:34:33 -050089 const QuicServerId& server_id,
vasilvvefc6af82019-10-11 12:46:56 -070090 const FakeClientOptions& options,
91 std::string alpn);
QUICHE teama6ef0a62019-03-07 20:34:33 -050092
93// SetupCryptoServerConfigForTest configures |crypto_config|
94// with sensible defaults for testing.
95void SetupCryptoServerConfigForTest(const QuicClock* clock,
96 QuicRandom* rand,
nharperba5f32c2019-05-13 12:21:31 -070097 QuicCryptoServerConfig* crypto_config);
QUICHE teama6ef0a62019-03-07 20:34:33 -050098
99// Sends the handshake message |message| to stream |stream| with the perspective
100// that the message is coming from |perspective|.
101void SendHandshakeMessageToStream(QuicCryptoStream* stream,
102 const CryptoHandshakeMessage& message,
103 Perspective perspective);
104
105// CommunicateHandshakeMessages moves messages from |client| to |server| and
106// back until |clients|'s handshake has completed.
107void CommunicateHandshakeMessages(PacketSavingConnection* client_conn,
108 QuicCryptoStream* client,
109 PacketSavingConnection* server_conn,
110 QuicCryptoStream* server);
111
QUICHE teama6ef0a62019-03-07 20:34:33 -0500112// AdvanceHandshake attempts to moves messages from |client| to |server| and
113// |server| to |client|. Returns the number of messages moved.
114std::pair<size_t, size_t> AdvanceHandshake(PacketSavingConnection* client_conn,
115 QuicCryptoStream* client,
116 size_t client_i,
117 PacketSavingConnection* server_conn,
118 QuicCryptoStream* server,
119 size_t server_i);
120
121// Returns the value for the tag |tag| in the tag value map of |message|.
vasilvvc48c8712019-03-11 13:38:16 -0700122std::string GetValueForTag(const CryptoHandshakeMessage& message, QuicTag tag);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500123
124// Returns a new |ProofSource| that serves up test certificates.
125std::unique_ptr<ProofSource> ProofSourceForTesting();
126
127// Returns a new |ProofVerifier| that uses the QUIC testing root CA.
128std::unique_ptr<ProofVerifier> ProofVerifierForTesting();
129
130// Returns a hash of the leaf test certificate.
131uint64_t LeafCertHashForTesting();
132
133// Returns a |ProofVerifyContext| that must be used with the verifier
134// returned by |ProofVerifierForTesting|.
135std::unique_ptr<ProofVerifyContext> ProofVerifyContextForTesting();
136
137// MockCommonCertSets returns a CommonCertSets that contains a single set with
138// hash |hash|, consisting of the certificate |cert| at index |index|.
vasilvv5f225b02020-10-08 11:49:09 -0400139CommonCertSets* MockCommonCertSets(absl::string_view cert,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500140 uint64_t hash,
141 uint32_t index);
142
143// Creates a minimal dummy reject message that will pass the client-config
144// validation tests. This will include a server config, but no certs, proof
145// source address token, or server nonce.
wub9b9cc812019-05-20 09:01:52 -0700146void FillInDummyReject(CryptoHandshakeMessage* rej);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500147
148// ParseTag returns a QuicTag from parsing |tagstr|. |tagstr| may either be
149// in the format "EXMP" (i.e. ASCII format), or "#11223344" (an explicit hex
vasilvv5cef78e2021-01-30 11:11:14 -0800150// format). It QUICHE_CHECK fails if there's a parse error.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500151QuicTag ParseTag(const char* tagstr);
152
153// Message constructs a CHLO message from a provided vector of tag/value pairs.
154// The first of each pair is the tag of a tag/value and is given as an argument
155// to |ParseTag|. The second is the value of the tag/value pair and is either a
156// hex dump, preceeded by a '#', or a raw value. If minimum_size_bytes is
157// provided then the message will be padded to this minimum size.
158//
159// CreateCHLO(
160// {{"NOCE", "#11223344"},
161// {"SNI", "www.example.com"}},
162// optional_minimum_size_bytes);
163CryptoHandshakeMessage CreateCHLO(
vasilvvc48c8712019-03-11 13:38:16 -0700164 std::vector<std::pair<std::string, std::string>> tags_and_values);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500165CryptoHandshakeMessage CreateCHLO(
vasilvvc48c8712019-03-11 13:38:16 -0700166 std::vector<std::pair<std::string, std::string>> tags_and_values,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500167 int minimum_size_bytes);
168
QUICHE teama6ef0a62019-03-07 20:34:33 -0500169// MovePackets parses crypto handshake messages from packet number
170// |*inout_packet_index| through to the last packet (or until a packet fails
171// to decrypt) and has |dest_stream| process them. |*inout_packet_index| is
172// updated with an index one greater than the last packet processed.
173void MovePackets(PacketSavingConnection* source_conn,
174 size_t* inout_packet_index,
175 QuicCryptoStream* dest_stream,
176 PacketSavingConnection* dest_conn,
177 Perspective dest_perspective);
178
179// Return an inchoate CHLO with some basic tag value pairs.
180CryptoHandshakeMessage GenerateDefaultInchoateCHLO(
181 const QuicClock* clock,
182 QuicTransportVersion version,
183 QuicCryptoServerConfig* crypto_config);
184
185// Takes a inchoate CHLO, returns a full CHLO in |out| which can pass
186// |crypto_config|'s validation.
187void GenerateFullCHLO(
188 const CryptoHandshakeMessage& inchoate_chlo,
189 QuicCryptoServerConfig* crypto_config,
190 QuicSocketAddress server_addr,
191 QuicSocketAddress client_addr,
dschinazi8d1c9d42020-02-18 13:12:20 -0800192 QuicTransportVersion transport_version,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500193 const QuicClock* clock,
194 QuicReferenceCountedPointer<QuicSignedServerConfig> signed_config,
195 QuicCompressedCertsCache* compressed_certs_cache,
196 CryptoHandshakeMessage* out);
197
198void CompareClientAndServerKeys(QuicCryptoClientStream* client,
nharperf579b5e2020-01-21 14:11:18 -0800199 QuicCryptoServerStreamBase* server);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500200
201// Return a CHLO nonce in hexadecimal.
vasilvvc48c8712019-03-11 13:38:16 -0700202std::string GenerateClientNonceHex(const QuicClock* clock,
203 QuicCryptoServerConfig* crypto_config);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500204
205// Return a CHLO PUBS in hexadecimal.
vasilvvc48c8712019-03-11 13:38:16 -0700206std::string GenerateClientPublicValuesHex();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500207
208} // namespace crypto_test_utils
209
210} // namespace test
211
212} // namespace quic
213
214#endif // QUICHE_QUIC_TEST_TOOLS_CRYPTO_TEST_UTILS_H_