blob: c52b3c8cbb963de89e7992d9d573a2a2f54c005f [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"
17
18namespace quic {
19
20// TransportParameters contains parameters for QUIC's transport layer that are
dschinazi52127d72019-04-17 15:12:38 -070021// exchanged during the TLS handshake. This struct is a mirror of the struct in
22// the "Transport Parameter Encoding" section of draft-ietf-quic-transport.
23// This struct currently uses the values from draft 18.
QUICHE teama6ef0a62019-03-07 20:34:33 -050024struct QUIC_EXPORT_PRIVATE TransportParameters {
dschinazi52127d72019-04-17 15:12:38 -070025 // The identifier used to differentiate transport parameters.
26 enum TransportParameterId : uint16_t;
27 // Represents an individual QUIC transport parameter that only encodes a
28 // variable length integer. Can only be created inside the constructor for
29 // TransportParameters.
30 class QUIC_EXPORT_PRIVATE IntegerParameter {
31 public:
32 // Forbid constructing and copying apart from TransportParameters.
33 IntegerParameter() = delete;
34 IntegerParameter(const IntegerParameter&) = delete;
35 IntegerParameter& operator=(const IntegerParameter&) = delete;
36 // Sets the value of this transport parameter.
37 void set_value(uint64_t value);
38 // Gets the value of this transport parameter.
39 uint64_t value() const;
40 // Validates whether the current value is valid.
41 bool IsValid() const;
42 // Writes to a crypto byte buffer, used during serialization. Does not write
43 // anything if the value is equal to the parameter's default value.
44 // Returns whether the write was successful.
45 bool WriteToCbb(CBB* parent_cbb) const;
46 // Reads from a crypto byte string, used during parsing.
47 // Returns whether the read was successful.
48 bool ReadFromCbs(CBS* const value_cbs);
49 // operator<< allows easily logging integer transport parameters.
50 friend QUIC_EXPORT_PRIVATE std::ostream& operator<<(
51 std::ostream& os,
52 const IntegerParameter& param);
53
54 private:
dschinazicf6a3ee2019-04-17 17:03:28 -070055 friend struct TransportParameters;
dschinazi52127d72019-04-17 15:12:38 -070056 // Constructors for initial setup used by TransportParameters only.
57 // This constructor sets |default_value| and |min_value| to 0, and
58 // |max_value| to kVarInt62MaxValue.
59 explicit IntegerParameter(TransportParameterId param_id);
60 IntegerParameter(TransportParameterId param_id,
61 uint64_t default_value,
62 uint64_t min_value,
63 uint64_t max_value);
64 // Human-readable string representation.
65 std::string ToString(bool for_use_in_list) const;
66
67 // Number used to indicate this transport parameter.
68 TransportParameterId param_id_;
69 // Current value of the transport parameter.
70 uint64_t value_;
71 // Default value of this transport parameter, as per IETF specification.
72 const uint64_t default_value_;
73 // Minimum value of this transport parameter, as per IETF specification.
74 const uint64_t min_value_;
75 // Maximum value of this transport parameter, as per IETF specification.
76 const uint64_t max_value_;
77 // Ensures this parameter is not parsed twice in the same message.
78 bool has_been_read_from_cbs_;
79 };
80
81 // Represents the preferred_address transport parameter that a server can
82 // send to clients.
83 struct QUIC_EXPORT_PRIVATE PreferredAddress {
84 PreferredAddress();
85 ~PreferredAddress();
86
87 QuicSocketAddress ipv4_socket_address;
88 QuicSocketAddress ipv6_socket_address;
89 QuicConnectionId connection_id;
90 std::vector<uint8_t> stateless_reset_token;
91
92 // Allows easily logging.
93 std::string ToString() const;
94 friend QUIC_EXPORT_PRIVATE std::ostream& operator<<(
95 std::ostream& os,
96 const TransportParameters& params);
97 };
98
QUICHE teama6ef0a62019-03-07 20:34:33 -050099 TransportParameters();
100 ~TransportParameters();
101
dschinazi52127d72019-04-17 15:12:38 -0700102 // Represents the sender of the transport parameters. When |perspective| is
103 // Perspective::IS_CLIENT, this struct is being used in the client_hello
104 // handshake message; when it is Perspective::IS_SERVER, it is being used in
105 // the encrypted_extensions handshake message.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500106 Perspective perspective;
107
108 // When Perspective::IS_CLIENT, |version| is the initial version offered by
109 // the client (before any version negotiation packets) for this connection.
110 // When Perspective::IS_SERVER, |version| is the version that is in use.
dschinazi52127d72019-04-17 15:12:38 -0700111 QuicVersionLabel version;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500112
113 // |supported_versions| contains a list of all versions that the server would
114 // send in a version negotiation packet. It is not used if |perspective ==
115 // Perspective::IS_CLIENT|.
116 QuicVersionLabelVector supported_versions;
117
dschinazi52127d72019-04-17 15:12:38 -0700118 // The value of the Destination Connection ID field from the first
119 // Initial packet sent by the client.
120 QuicConnectionId original_connection_id;
121
122 // Idle timeout expressed in seconds.
123 IntegerParameter idle_timeout_seconds;
124
125 // Stateless reset token used in verifying stateless resets.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500126 std::vector<uint8_t> stateless_reset_token;
127
dschinazi52127d72019-04-17 15:12:38 -0700128 // Limits the size of packets that the endpoint is willing to receive.
129 // This indicates that packets larger than this limit will be dropped.
130 IntegerParameter max_packet_size;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500131
dschinazi52127d72019-04-17 15:12:38 -0700132 // Contains the initial value for the maximum amount of data that can
133 // be sent on the connection.
134 IntegerParameter initial_max_data;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500135
dschinazi52127d72019-04-17 15:12:38 -0700136 // Initial flow control limit for locally-initiated bidirectional streams.
137 IntegerParameter initial_max_stream_data_bidi_local;
138
139 // Initial flow control limit for peer-initiated bidirectional streams.
140 IntegerParameter initial_max_stream_data_bidi_remote;
141
142 // Initial flow control limit for unidirectional streams.
143 IntegerParameter initial_max_stream_data_uni;
144
145 // Initial maximum number of bidirectional streams the peer may initiate.
146 IntegerParameter initial_max_streams_bidi;
147
148 // Initial maximum number of unidirectional streams the peer may initiate.
149 IntegerParameter initial_max_streams_uni;
150
151 // Exponent used to decode the ACK Delay field in ACK frames.
152 IntegerParameter ack_delay_exponent;
153
154 // Maximum amount of time in milliseconds by which the endpoint will
155 // delay sending acknowledgments.
156 IntegerParameter max_ack_delay;
157
158 // Indicates lack of support for connection migration.
159 bool disable_migration;
160
161 // Used to effect a change in server address at the end of the handshake.
162 std::unique_ptr<PreferredAddress> preferred_address;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500163
164 // Transport parameters used by Google QUIC but not IETF QUIC. This is
165 // serialized into a TransportParameter struct with a TransportParameterId of
dschinazi52127d72019-04-17 15:12:38 -0700166 // kGoogleQuicParamId.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500167 std::unique_ptr<CryptoHandshakeMessage> google_quic_params;
168
dschinazi52127d72019-04-17 15:12:38 -0700169 // Validates whether transport parameters are valid according to
170 // the specification.
171 bool AreValid() const;
172
173 // Allows easily logging transport parameters.
174 std::string ToString() const;
175 friend QUIC_EXPORT_PRIVATE std::ostream& operator<<(
176 std::ostream& os,
177 const TransportParameters& params);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500178};
179
180// Serializes a TransportParameters struct into the format for sending it in a
dschinazi52127d72019-04-17 15:12:38 -0700181// TLS extension. The serialized bytes are written to |*out|. Returns if the
182// parameters are valid and serialization succeeded.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500183QUIC_EXPORT_PRIVATE bool SerializeTransportParameters(
184 const TransportParameters& in,
185 std::vector<uint8_t>* out);
186
187// Parses bytes from the quic_transport_parameters TLS extension and writes the
188// parsed parameters into |*out|. Input is read from |in| for |in_len| bytes.
189// |perspective| indicates whether the input came from a client or a server.
dschinazi52127d72019-04-17 15:12:38 -0700190// This method returns true if the input was successfully parsed.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500191// TODO(nharper): Write fuzz tests for this method.
192QUIC_EXPORT_PRIVATE bool ParseTransportParameters(const uint8_t* in,
193 size_t in_len,
194 Perspective perspective,
195 TransportParameters* out);
196
197} // namespace quic
198
199#endif // QUICHE_QUIC_CORE_CRYPTO_TRANSPORT_PARAMETERS_H_