blob: 22dc252405c2a51445a610e6f735791059cda21b [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// Copyright (c) 2018 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_CORE_CRYPTO_TRANSPORT_PARAMETERS_H_
6#define QUICHE_QUIC_CORE_CRYPTO_TRANSPORT_PARAMETERS_H_
7
8#include <memory>
9#include <vector>
10
dschinazi52127d72019-04-17 15:12:38 -070011#include "third_party/boringssl/src/include/openssl/bytestring.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050012#include "net/third_party/quiche/src/quic/core/crypto/crypto_handshake_message.h"
dschinazi52127d72019-04-17 15:12:38 -070013#include "net/third_party/quiche/src/quic/core/quic_connection_id.h"
14#include "net/third_party/quiche/src/quic/core/quic_data_writer.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050015#include "net/third_party/quiche/src/quic/core/quic_types.h"
16#include "net/third_party/quiche/src/quic/core/quic_versions.h"
vasilvva2ef3012019-09-12 18:32:14 -070017#include "net/third_party/quiche/src/quic/platform/api/quic_containers.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050018
19namespace quic {
20
21// TransportParameters contains parameters for QUIC's transport layer that are
dschinazi52127d72019-04-17 15:12:38 -070022// exchanged during the TLS handshake. This struct is a mirror of the struct in
23// the "Transport Parameter Encoding" section of draft-ietf-quic-transport.
dschinazi6cf4d2a2019-04-30 16:20:23 -070024// This struct currently uses the values from draft 20.
QUICHE teama6ef0a62019-03-07 20:34:33 -050025struct QUIC_EXPORT_PRIVATE TransportParameters {
dschinazi52127d72019-04-17 15:12:38 -070026 // The identifier used to differentiate transport parameters.
27 enum TransportParameterId : uint16_t;
vasilvva2ef3012019-09-12 18:32:14 -070028 // A map used to specify custom parameters.
29 using ParameterMap = QuicUnorderedMap<TransportParameterId, std::string>;
dschinazi52127d72019-04-17 15:12:38 -070030 // Represents an individual QUIC transport parameter that only encodes a
31 // variable length integer. Can only be created inside the constructor for
32 // TransportParameters.
33 class QUIC_EXPORT_PRIVATE IntegerParameter {
34 public:
35 // Forbid constructing and copying apart from TransportParameters.
36 IntegerParameter() = delete;
37 IntegerParameter(const IntegerParameter&) = delete;
38 IntegerParameter& operator=(const IntegerParameter&) = delete;
39 // Sets the value of this transport parameter.
40 void set_value(uint64_t value);
41 // Gets the value of this transport parameter.
42 uint64_t value() const;
43 // Validates whether the current value is valid.
44 bool IsValid() const;
45 // Writes to a crypto byte buffer, used during serialization. Does not write
46 // anything if the value is equal to the parameter's default value.
47 // Returns whether the write was successful.
48 bool WriteToCbb(CBB* parent_cbb) const;
49 // Reads from a crypto byte string, used during parsing.
50 // Returns whether the read was successful.
51 bool ReadFromCbs(CBS* const value_cbs);
52 // operator<< allows easily logging integer transport parameters.
53 friend QUIC_EXPORT_PRIVATE std::ostream& operator<<(
54 std::ostream& os,
55 const IntegerParameter& param);
56
57 private:
dschinazicf6a3ee2019-04-17 17:03:28 -070058 friend struct TransportParameters;
dschinazi52127d72019-04-17 15:12:38 -070059 // Constructors for initial setup used by TransportParameters only.
60 // This constructor sets |default_value| and |min_value| to 0, and
61 // |max_value| to kVarInt62MaxValue.
62 explicit IntegerParameter(TransportParameterId param_id);
63 IntegerParameter(TransportParameterId param_id,
64 uint64_t default_value,
65 uint64_t min_value,
66 uint64_t max_value);
67 // Human-readable string representation.
68 std::string ToString(bool for_use_in_list) const;
69
70 // Number used to indicate this transport parameter.
71 TransportParameterId param_id_;
72 // Current value of the transport parameter.
73 uint64_t value_;
74 // Default value of this transport parameter, as per IETF specification.
75 const uint64_t default_value_;
76 // Minimum value of this transport parameter, as per IETF specification.
77 const uint64_t min_value_;
78 // Maximum value of this transport parameter, as per IETF specification.
79 const uint64_t max_value_;
80 // Ensures this parameter is not parsed twice in the same message.
81 bool has_been_read_from_cbs_;
82 };
83
84 // Represents the preferred_address transport parameter that a server can
85 // send to clients.
86 struct QUIC_EXPORT_PRIVATE PreferredAddress {
87 PreferredAddress();
88 ~PreferredAddress();
89
90 QuicSocketAddress ipv4_socket_address;
91 QuicSocketAddress ipv6_socket_address;
92 QuicConnectionId connection_id;
93 std::vector<uint8_t> stateless_reset_token;
94
95 // Allows easily logging.
96 std::string ToString() const;
97 friend QUIC_EXPORT_PRIVATE std::ostream& operator<<(
98 std::ostream& os,
99 const TransportParameters& params);
100 };
101
QUICHE teama6ef0a62019-03-07 20:34:33 -0500102 TransportParameters();
103 ~TransportParameters();
104
dschinazi52127d72019-04-17 15:12:38 -0700105 // Represents the sender of the transport parameters. When |perspective| is
106 // Perspective::IS_CLIENT, this struct is being used in the client_hello
107 // handshake message; when it is Perspective::IS_SERVER, it is being used in
108 // the encrypted_extensions handshake message.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500109 Perspective perspective;
110
111 // When Perspective::IS_CLIENT, |version| is the initial version offered by
112 // the client (before any version negotiation packets) for this connection.
113 // When Perspective::IS_SERVER, |version| is the version that is in use.
dschinazi52127d72019-04-17 15:12:38 -0700114 QuicVersionLabel version;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500115
116 // |supported_versions| contains a list of all versions that the server would
117 // send in a version negotiation packet. It is not used if |perspective ==
118 // Perspective::IS_CLIENT|.
119 QuicVersionLabelVector supported_versions;
120
dschinazi52127d72019-04-17 15:12:38 -0700121 // The value of the Destination Connection ID field from the first
122 // Initial packet sent by the client.
123 QuicConnectionId original_connection_id;
124
dschinazi6cf4d2a2019-04-30 16:20:23 -0700125 // Idle timeout expressed in milliseconds.
126 IntegerParameter idle_timeout_milliseconds;
dschinazi52127d72019-04-17 15:12:38 -0700127
128 // Stateless reset token used in verifying stateless resets.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500129 std::vector<uint8_t> stateless_reset_token;
130
dschinazi52127d72019-04-17 15:12:38 -0700131 // Limits the size of packets that the endpoint is willing to receive.
132 // This indicates that packets larger than this limit will be dropped.
133 IntegerParameter max_packet_size;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500134
dschinazi52127d72019-04-17 15:12:38 -0700135 // Contains the initial value for the maximum amount of data that can
136 // be sent on the connection.
137 IntegerParameter initial_max_data;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500138
dschinazi52127d72019-04-17 15:12:38 -0700139 // Initial flow control limit for locally-initiated bidirectional streams.
140 IntegerParameter initial_max_stream_data_bidi_local;
141
142 // Initial flow control limit for peer-initiated bidirectional streams.
143 IntegerParameter initial_max_stream_data_bidi_remote;
144
145 // Initial flow control limit for unidirectional streams.
146 IntegerParameter initial_max_stream_data_uni;
147
148 // Initial maximum number of bidirectional streams the peer may initiate.
149 IntegerParameter initial_max_streams_bidi;
150
151 // Initial maximum number of unidirectional streams the peer may initiate.
152 IntegerParameter initial_max_streams_uni;
153
154 // Exponent used to decode the ACK Delay field in ACK frames.
155 IntegerParameter ack_delay_exponent;
156
157 // Maximum amount of time in milliseconds by which the endpoint will
158 // delay sending acknowledgments.
159 IntegerParameter max_ack_delay;
160
161 // Indicates lack of support for connection migration.
162 bool disable_migration;
163
164 // Used to effect a change in server address at the end of the handshake.
165 std::unique_ptr<PreferredAddress> preferred_address;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500166
dschinazie9db63c2019-07-17 16:19:42 -0700167 // Maximum number of connection IDs from the peer that an endpoint is willing
168 // to store.
169 IntegerParameter active_connection_id_limit;
170
QUICHE teama6ef0a62019-03-07 20:34:33 -0500171 // Transport parameters used by Google QUIC but not IETF QUIC. This is
172 // serialized into a TransportParameter struct with a TransportParameterId of
dschinazi52127d72019-04-17 15:12:38 -0700173 // kGoogleQuicParamId.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500174 std::unique_ptr<CryptoHandshakeMessage> google_quic_params;
175
dschinazi52127d72019-04-17 15:12:38 -0700176 // Validates whether transport parameters are valid according to
177 // the specification.
178 bool AreValid() const;
179
vasilvva2ef3012019-09-12 18:32:14 -0700180 // Custom parameters that may be specific to application protocol.
181 ParameterMap custom_parameters;
182
dschinazi52127d72019-04-17 15:12:38 -0700183 // Allows easily logging transport parameters.
184 std::string ToString() const;
185 friend QUIC_EXPORT_PRIVATE std::ostream& operator<<(
186 std::ostream& os,
187 const TransportParameters& params);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500188};
189
190// Serializes a TransportParameters struct into the format for sending it in a
dschinazi52127d72019-04-17 15:12:38 -0700191// TLS extension. The serialized bytes are written to |*out|. Returns if the
192// parameters are valid and serialization succeeded.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500193QUIC_EXPORT_PRIVATE bool SerializeTransportParameters(
dschinazi6c84c142019-07-31 09:11:49 -0700194 ParsedQuicVersion version,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500195 const TransportParameters& in,
196 std::vector<uint8_t>* out);
197
198// Parses bytes from the quic_transport_parameters TLS extension and writes the
199// parsed parameters into |*out|. Input is read from |in| for |in_len| bytes.
200// |perspective| indicates whether the input came from a client or a server.
dschinazi52127d72019-04-17 15:12:38 -0700201// This method returns true if the input was successfully parsed.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500202// TODO(nharper): Write fuzz tests for this method.
dschinazi6c84c142019-07-31 09:11:49 -0700203QUIC_EXPORT_PRIVATE bool ParseTransportParameters(ParsedQuicVersion version,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500204 Perspective perspective,
dschinazi6c84c142019-07-31 09:11:49 -0700205 const uint8_t* in,
206 size_t in_len,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500207 TransportParameters* out);
208
209} // namespace quic
210
211#endif // QUICHE_QUIC_CORE_CRYPTO_TRANSPORT_PARAMETERS_H_