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 #ifndef NET_QUIC_TEST_TOOLS_CRYPTO_TEST_UTILS_H_ 6 #define NET_QUIC_TEST_TOOLS_CRYPTO_TEST_UTILS_H_ 7 8 #include <stdarg.h> 9 10 #include <vector> 11 12 #include "base/logging.h" 13 #include "base/strings/string_piece.h" 14 #include "net/quic/crypto/crypto_framer.h" 15 #include "net/quic/quic_framer.h" 16 #include "net/quic/quic_protocol.h" 17 18 namespace net { 19 20 class ChannelIDSigner; 21 class CommonCertSets; 22 class ProofSource; 23 class ProofVerifier; 24 class QuicClock; 25 class QuicConfig; 26 class QuicCryptoClientStream; 27 class QuicCryptoServerConfig; 28 class QuicCryptoServerStream; 29 class QuicCryptoStream; 30 class QuicRandom; 31 32 namespace test { 33 34 class PacketSavingConnection; 35 36 class CryptoTestUtils { 37 public: 38 // FakeClientOptions bundles together a number of options for configuring 39 // HandshakeWithFakeClient. 40 struct FakeClientOptions { 41 FakeClientOptions(); 42 43 // If dont_verify_certs is true then no ProofVerifier is set on the client. 44 // Thus no certificates will be requested or checked. 45 bool dont_verify_certs; 46 47 // If channel_id_enabled is true then the client will attempt to send a 48 // ChannelID. The key will be the same as is returned by 49 // ChannelIDSigner's |GetKeyForHostname|. 50 bool channel_id_enabled; 51 }; 52 53 // returns: the number of client hellos that the client sent. 54 static int HandshakeWithFakeServer(PacketSavingConnection* client_conn, 55 QuicCryptoClientStream* client); 56 57 // returns: the number of client hellos that the client sent. 58 static int HandshakeWithFakeClient(PacketSavingConnection* server_conn, 59 QuicCryptoServerStream* server, 60 const FakeClientOptions& options); 61 62 // SetupCryptoServerConfigForTest configures |config| and |crypto_config| 63 // with sensible defaults for testing. 64 static void SetupCryptoServerConfigForTest( 65 const QuicClock* clock, 66 QuicRandom* rand, 67 QuicConfig* config, 68 QuicCryptoServerConfig* crypto_config); 69 70 // CommunicateHandshakeMessages moves messages from |a| to |b| and back until 71 // |a|'s handshake has completed. 72 static void CommunicateHandshakeMessages(PacketSavingConnection* a_conn, 73 QuicCryptoStream* a, 74 PacketSavingConnection* b_conn, 75 QuicCryptoStream* b); 76 77 // Returns the value for the tag |tag| in the tag value map of |message|. 78 static std::string GetValueForTag(const CryptoHandshakeMessage& message, 79 QuicTag tag); 80 81 // Returns a |ProofSource| that serves up test certificates. 82 static ProofSource* ProofSourceForTesting(); 83 84 // Returns a |ProofVerifier| that uses the QUIC testing root CA. 85 static ProofVerifier* ProofVerifierForTesting(); 86 87 // MockCommonCertSets returns a CommonCertSets that contains a single set with 88 // hash |hash|, consisting of the certificate |cert| at index |index|. 89 static CommonCertSets* MockCommonCertSets(base::StringPiece cert, 90 uint64 hash, 91 uint32 index); 92 93 // ParseTag returns a QuicTag from parsing |tagstr|. |tagstr| may either be 94 // in the format "EXMP" (i.e. ASCII format), or "#11223344" (an explicit hex 95 // format). It CHECK fails if there's a parse error. 96 static QuicTag ParseTag(const char* tagstr); 97 98 // Message constructs a handshake message from a variable number of 99 // arguments. |message_tag| is passed to |ParseTag| and used as the tag of 100 // the resulting message. The arguments are taken in pairs and NULL 101 // terminated. The first of each pair is the tag of a tag/value and is given 102 // as an argument to |ParseTag|. The second is the value of the tag/value 103 // pair and is either a hex dump, preceeded by a '#', or a raw value. 104 // 105 // Message( 106 // "CHLO", 107 // "NOCE", "#11223344", 108 // "SNI", "www.example.com", 109 // NULL); 110 static CryptoHandshakeMessage Message(const char* message_tag, ...); 111 112 // BuildMessage is the same as |Message|, but takes the variable arguments 113 // explicitly. TODO(rtenneti): Investigate whether it'd be better for 114 // Message() and BuildMessage() to return a CryptoHandshakeMessage* pointer 115 // instead, to avoid copying the return value. 116 static CryptoHandshakeMessage BuildMessage(const char* message_tag, 117 va_list ap); 118 119 // ChannelIDSignerForTesting returns a ChannelIDSigner that generates keys 120 // deterministically based on the hostname given in the Sign call. 121 static ChannelIDSigner* ChannelIDSignerForTesting(); 122 123 private: 124 static void CompareClientAndServerKeys(QuicCryptoClientStream* client, 125 QuicCryptoServerStream* server); 126 }; 127 128 } // namespace test 129 130 } // namespace net 131 132 #endif // NET_QUIC_TEST_TOOLS_CRYPTO_TEST_UTILS_H_ 133