proxygen
Types.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 #pragma once
10 
11 #include <boost/variant.hpp>
12 #include <folly/Optional.h>
13 #include <folly/io/Cursor.h>
14 #include <folly/io/IOBuf.h>
15 
16 #include <fizz/protocol/Events.h>
17 
18 namespace fizz {
19 
20 constexpr folly::StringPiece kHkdfLabelPrefix = "tls13 ";
21 
22 using Buf = std::unique_ptr<folly::IOBuf>;
23 
24 enum class ProtocolVersion : uint16_t {
25  tls_1_0 = 0x0301,
26  tls_1_1 = 0x0302,
27  tls_1_2 = 0x0303,
28  tls_1_3 = 0x0304,
29  tls_1_3_20 = 0x7f14,
30  tls_1_3_20_fb = 0xfb14,
31  tls_1_3_21 = 0x7f15,
32  tls_1_3_21_fb = 0xfb15,
33  tls_1_3_22 = 0x7f16,
34  tls_1_3_22_fb = 0xfb16,
35  tls_1_3_23 = 0x7f17,
36  tls_1_3_23_fb = 0xfb17,
37  tls_1_3_26 = 0x7f1a,
38  tls_1_3_26_fb = 0xfb1a,
39  tls_1_3_28 = 0x7f1c,
40 };
41 
43 
45 
46 enum class ContentType : uint8_t {
47  alert = 21,
48  handshake = 22,
49  application_data = 23,
50 
51  change_cipher_spec = 20,
52 };
53 
54 struct TLSMessage {
57 };
58 
59 constexpr folly::StringPiece FakeChangeCipherSpec{"\x14\x03\x03\x00\x01\x01",
60  6};
61 
62 enum class HandshakeType : uint8_t {
63  client_hello = 1,
64  server_hello = 2,
69  certificate = 11,
71  certificate_verify = 15,
72  finished = 20,
73  key_update = 24,
75  message_hash = 254
76 };
77 
78 constexpr size_t kMaxHandshakeSize = 0x20000; // 128k
79 
80 struct message_hash {
81  static constexpr HandshakeType handshake_type = HandshakeType::message_hash;
82  std::unique_ptr<folly::IOBuf> hash;
83 };
84 
85 template <Event e, HandshakeType t>
87  static constexpr HandshakeType handshake_type = t;
88 
89  /*
90  * Original encoding of the message, populated on received handshake messages.
91  */
93 };
94 
95 enum class ExtensionType : uint16_t {
96  server_name = 0,
97  supported_groups = 10,
100  token_binding = 24,
102  key_share_old = 40,
103  pre_shared_key = 41,
104  early_data = 42,
105  supported_versions = 43,
106  cookie = 44,
109  post_handshake_auth = 49,
111  key_share = 51,
112 
113  // alternate_server_name = 0xfb00,
114  compress_certificate = 0xff02,
115 };
116 
118 
119 enum class AlertDescription : uint8_t {
120  close_notify = 0,
121  end_of_early_data = 1,
122  unexpected_message = 10,
123  bad_record_mac = 20,
124  record_overflow = 22,
125  handshake_failure = 40,
126  bad_certificate = 42,
128  certificate_revoked = 44,
129  certificate_expired = 45,
130  certificate_unknown = 46,
131  illegal_parameter = 47,
132  unknown_ca = 48,
133  access_denied = 49,
134  decode_error = 50,
135  decrypt_error = 51,
136  protocol_version = 70,
138  internal_error = 80,
140  user_canceled = 90,
141  missing_extension = 109,
142  unsupported_extension = 110,
144  unrecognized_name = 112,
147  unknown_psk_identity = 115,
149 };
150 
152 
153 enum class CipherSuite : uint16_t {
154  TLS_AES_128_GCM_SHA256 = 0x1301,
155  TLS_AES_256_GCM_SHA384 = 0x1302,
157  // experimental cipher suites
159 };
160 
162 
163 enum class PskKeyExchangeMode : uint8_t { psk_ke = 0, psk_dhe_ke = 1 };
164 
166 
168  zlib = 1,
169 };
170 
172 
173 struct Extension {
175  Buf extension_data; // Limited to 2^16-1 bytes.
176 };
177 
178 struct HkdfLabel {
182 };
183 
184 using Random = std::array<uint8_t, 32>;
185 
187  : HandshakeStruct<Event::ClientHello, HandshakeType::client_hello> {
191  std::vector<CipherSuite> cipher_suites;
192  std::vector<uint8_t> legacy_compression_methods;
193  std::vector<Extension> extensions;
194 };
195 
197  : HandshakeStruct<Event::ServerHello, HandshakeType::server_hello> {
200  // If legacy_session_id_echo is non-null the ServerHello will be encoded with
201  // it and legacy_compression_method.
204  uint8_t legacy_compression_method{0};
205  std::vector<Extension> extensions;
206 };
207 
209  : HandshakeStruct<Event::HelloRetryRequest, HandshakeType::server_hello> {
211  static constexpr Random HrrRandom{
212  {0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11, 0xBE, 0x1D, 0x8C,
213  0x02, 0x1E, 0x65, 0xB8, 0x91, 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB,
214  0x8C, 0x5E, 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C}};
217  uint8_t legacy_compression_method{0};
218  std::vector<Extension> extensions;
219 };
220 
222  : HandshakeStruct<Event::EndOfEarlyData, HandshakeType::end_of_early_data> {
223 };
224 
226  Event::EncryptedExtensions,
227  HandshakeType::encrypted_extensions> {
228  std::vector<Extension> extensions;
229 };
230 
233  std::vector<Extension> extensions;
234 };
235 
237  : HandshakeStruct<Event::Certificate, HandshakeType::certificate> {
239  std::vector<CertificateEntry> certificate_list;
240 };
241 
243  Event::CompressedCertificate,
244  HandshakeType::compressed_certificate> {
248 };
249 
251  Event::CertificateRequest,
252  HandshakeType::certificate_request> {
254  std::vector<Extension> extensions;
255 };
256 
257 enum class SignatureScheme : uint16_t {
258  ecdsa_secp256r1_sha256 = 0x0403,
259  ecdsa_secp384r1_sha384 = 0x0503,
260  ecdsa_secp521r1_sha512 = 0x0603,
261  rsa_pss_sha256 = 0x0804,
262  rsa_pss_sha384 = 0x0805,
263  rsa_pss_sha512 = 0x0806,
264  ed25519 = 0x0807,
265  ed448 = 0x0808,
266 };
267 
269 
271  Event::CertificateVerify,
272  HandshakeType::certificate_verify> {
275 };
276 
277 struct Finished : HandshakeStruct<Event::Finished, HandshakeType::finished> {
279 };
280 
282  Event::NewSessionTicket,
283  HandshakeType::new_session_ticket> {
286  // Ticket nonce is set to null iff pre-draft 21.
289  std::vector<Extension> extensions;
290 };
291 
292 enum class KeyUpdateRequest : uint8_t {
294  update_requested = 1
295 };
296 
297 struct KeyUpdate
298  : HandshakeStruct<Event::KeyUpdate, HandshakeType::key_update> {
300 };
301 
302 enum class NamedGroup : uint16_t {
303  secp256r1 = 23,
304  secp384r1 = 24,
305  secp521r1 = 25,
306  x25519 = 29
307 };
308 
310 
311 struct Alert : EventType<Event::Alert> {
312  uint8_t level = 0x02;
314 
315  Alert() = default;
316  explicit Alert(AlertDescription desc) : description(desc) {}
317 };
318 
319 class FizzException : public std::runtime_error {
320  public:
322  : std::runtime_error(msg), alert_(alert) {}
323 
325  return alert_;
326  }
327 
328  private:
330 };
331 
332 template <class T>
333 Buf encode(T&& t);
334 template <class T>
336 template <class T>
337 T decode(std::unique_ptr<folly::IOBuf>&& buf);
338 template <class T>
339 T decode(folly::io::Cursor& cursor);
340 template <typename T>
341 std::string enumToHex(T enumValue);
342 
343 Buf encodeHkdfLabel(HkdfLabel&& label, const std::string& hkdfLabelPrefix);
344 } // namespace fizz
345 
346 #include <fizz/record/Types-inl.h>
std::vector< Extension > extensions
Definition: Types.h:218
std::vector< Extension > extensions
Definition: Types.h:228
Buf fragment
Definition: Types.h:56
#define T(v)
Definition: http_parser.c:233
Buf encodeHandshake(T &&handshakeMsg)
Definition: Types-inl.h:515
KeyUpdateRequest request_update
Definition: Types.h:299
folly::StringPiece toString(StateEnum state)
Definition: State.cpp:16
CertificateCompressionAlgorithm algorithm
Definition: Types.h:245
std::vector< Extension > extensions
Definition: Types.h:233
CertificateCompressionAlgorithm
Definition: Types.h:167
AlertDescription description
Definition: Types.h:313
Buf encodeHkdfLabel(HkdfLabel &&label, const std::string &hkdfLabelPrefix)
Definition: Types-inl.h:488
std::unique_ptr< folly::IOBuf > hash
Definition: Types.h:82
TokenBindingMessage decode(folly::io::Cursor &cursor)
Definition: Types.cpp:132
NamedGroup
Definition: Types.h:302
std::string enumToHex(T enumValue)
Definition: Types-inl.h:646
SignatureScheme
Definition: Types.h:257
CipherSuite
Definition: Types.h:153
const std::string label
Definition: Types.h:180
STL namespace.
uint32_t uncompressed_length
Definition: Types.h:246
Buf extension_data
Definition: Types.h:175
Buf legacy_session_id_echo
Definition: Types.h:202
constexpr folly::StringPiece FakeChangeCipherSpec
Definition: Types.h:59
std::vector< Extension > extensions
Definition: Types.h:289
Buf certificate_request_context
Definition: Types.h:238
constexpr size_t kMaxHandshakeSize
Definition: Types.h:78
Random random
Definition: Types.h:189
ProtocolVersion
Definition: Types.h:24
Buf certificate_request_context
Definition: Types.h:253
AlertDescription
Definition: Types.h:119
std::vector< CertificateEntry > certificate_list
Definition: Types.h:239
Buf legacy_session_id
Definition: Types.h:190
folly::Optional< AlertDescription > getAlert() const
Definition: Types.h:324
std::vector< Extension > extensions
Definition: Types.h:254
Random random
Definition: Types.h:199
Definition: Actions.h:16
std::vector< Extension > extensions
Definition: Types.h:193
std::array< uint8_t, 32 > Random
Definition: Types.h:184
Alert(AlertDescription desc)
Definition: Types.h:316
std::vector< CipherSuite > cipher_suites
Definition: Types.h:191
Buf verify_data
Definition: Types.h:278
Buf hash_value
Definition: Types.h:181
ExtensionType
Definition: Types.h:95
FizzException(const std::string &msg, folly::Optional< AlertDescription > alert)
Definition: Types.h:321
SignatureScheme algorithm
Definition: Types.h:273
KeyUpdateRequest
Definition: Types.h:292
std::vector< Extension > extensions
Definition: Types.h:205
Buf encode(TokenBindingMessage &&message)
Definition: Types.cpp:124
folly::Optional< Buf > originalEncoding
Definition: Types.h:92
const char * string
Definition: Conv.cpp:212
ExtensionType extension_type
Definition: Types.h:174
folly::Optional< AlertDescription > alert_
Definition: Types.h:329
std::unique_ptr< folly::IOBuf > Buf
Definition: Types.h:22
CipherSuite cipher_suite
Definition: Types.h:216
uint32_t ticket_lifetime
Definition: Types.h:284
HandshakeType
Definition: Types.h:62
ProtocolVersion getRealDraftVersion(ProtocolVersion version)
Definition: Types.cpp:16
std::vector< uint8_t > legacy_compression_methods
Definition: Types.h:192
ContentType type
Definition: Types.h:55
constexpr folly::StringPiece kHkdfLabelPrefix
Definition: Types.h:20
PskKeyExchangeMode
Definition: Types.h:163
CipherSuite cipher_suite
Definition: Types.h:203
ContentType
Definition: Types.h:46
uint16_t length
Definition: Types.h:179
uint32_t ticket_age_add
Definition: Types.h:285
StringPiece label