QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // 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 | |
dschinazi | 52127d7 | 2019-04-17 15:12:38 -0700 | [diff] [blame] | 11 | #include "third_party/boringssl/src/include/openssl/bytestring.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 12 | #include "net/third_party/quiche/src/quic/core/crypto/crypto_handshake_message.h" |
dschinazi | 52127d7 | 2019-04-17 15:12:38 -0700 | [diff] [blame] | 13 | #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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 15 | #include "net/third_party/quiche/src/quic/core/quic_types.h" |
| 16 | #include "net/third_party/quiche/src/quic/core/quic_versions.h" |
vasilvv | a2ef301 | 2019-09-12 18:32:14 -0700 | [diff] [blame] | 17 | #include "net/third_party/quiche/src/quic/platform/api/quic_containers.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 18 | |
| 19 | namespace quic { |
| 20 | |
| 21 | // TransportParameters contains parameters for QUIC's transport layer that are |
dschinazi | 52127d7 | 2019-04-17 15:12:38 -0700 | [diff] [blame] | 22 | // 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. |
dschinazi | 6cf4d2a | 2019-04-30 16:20:23 -0700 | [diff] [blame] | 24 | // This struct currently uses the values from draft 20. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 25 | struct QUIC_EXPORT_PRIVATE TransportParameters { |
dschinazi | 52127d7 | 2019-04-17 15:12:38 -0700 | [diff] [blame] | 26 | // The identifier used to differentiate transport parameters. |
| 27 | enum TransportParameterId : uint16_t; |
vasilvv | a2ef301 | 2019-09-12 18:32:14 -0700 | [diff] [blame] | 28 | // A map used to specify custom parameters. |
| 29 | using ParameterMap = QuicUnorderedMap<TransportParameterId, std::string>; |
dschinazi | 52127d7 | 2019-04-17 15:12:38 -0700 | [diff] [blame] | 30 | // 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: |
dschinazi | cf6a3ee | 2019-04-17 17:03:28 -0700 | [diff] [blame] | 58 | friend struct TransportParameters; |
dschinazi | 52127d7 | 2019-04-17 15:12:38 -0700 | [diff] [blame] | 59 | // 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 102 | TransportParameters(); |
| 103 | ~TransportParameters(); |
| 104 | |
dschinazi | 52127d7 | 2019-04-17 15:12:38 -0700 | [diff] [blame] | 105 | // 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 109 | 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. |
dschinazi | 52127d7 | 2019-04-17 15:12:38 -0700 | [diff] [blame] | 114 | QuicVersionLabel version; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 115 | |
| 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 | |
dschinazi | 52127d7 | 2019-04-17 15:12:38 -0700 | [diff] [blame] | 121 | // The value of the Destination Connection ID field from the first |
| 122 | // Initial packet sent by the client. |
| 123 | QuicConnectionId original_connection_id; |
| 124 | |
dschinazi | 6cf4d2a | 2019-04-30 16:20:23 -0700 | [diff] [blame] | 125 | // Idle timeout expressed in milliseconds. |
| 126 | IntegerParameter idle_timeout_milliseconds; |
dschinazi | 52127d7 | 2019-04-17 15:12:38 -0700 | [diff] [blame] | 127 | |
| 128 | // Stateless reset token used in verifying stateless resets. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 129 | std::vector<uint8_t> stateless_reset_token; |
| 130 | |
dschinazi | 52127d7 | 2019-04-17 15:12:38 -0700 | [diff] [blame] | 131 | // 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 134 | |
dschinazi | 52127d7 | 2019-04-17 15:12:38 -0700 | [diff] [blame] | 135 | // Contains the initial value for the maximum amount of data that can |
| 136 | // be sent on the connection. |
| 137 | IntegerParameter initial_max_data; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 138 | |
dschinazi | 52127d7 | 2019-04-17 15:12:38 -0700 | [diff] [blame] | 139 | // 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 166 | |
dschinazi | e9db63c | 2019-07-17 16:19:42 -0700 | [diff] [blame] | 167 | // Maximum number of connection IDs from the peer that an endpoint is willing |
| 168 | // to store. |
| 169 | IntegerParameter active_connection_id_limit; |
| 170 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 171 | // Transport parameters used by Google QUIC but not IETF QUIC. This is |
| 172 | // serialized into a TransportParameter struct with a TransportParameterId of |
dschinazi | 52127d7 | 2019-04-17 15:12:38 -0700 | [diff] [blame] | 173 | // kGoogleQuicParamId. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 174 | std::unique_ptr<CryptoHandshakeMessage> google_quic_params; |
| 175 | |
dschinazi | 52127d7 | 2019-04-17 15:12:38 -0700 | [diff] [blame] | 176 | // Validates whether transport parameters are valid according to |
| 177 | // the specification. |
| 178 | bool AreValid() const; |
| 179 | |
vasilvv | a2ef301 | 2019-09-12 18:32:14 -0700 | [diff] [blame] | 180 | // Custom parameters that may be specific to application protocol. |
| 181 | ParameterMap custom_parameters; |
| 182 | |
dschinazi | 52127d7 | 2019-04-17 15:12:38 -0700 | [diff] [blame] | 183 | // 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | // Serializes a TransportParameters struct into the format for sending it in a |
dschinazi | 52127d7 | 2019-04-17 15:12:38 -0700 | [diff] [blame] | 191 | // TLS extension. The serialized bytes are written to |*out|. Returns if the |
| 192 | // parameters are valid and serialization succeeded. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 193 | QUIC_EXPORT_PRIVATE bool SerializeTransportParameters( |
dschinazi | 6c84c14 | 2019-07-31 09:11:49 -0700 | [diff] [blame] | 194 | ParsedQuicVersion version, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 195 | 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. |
dschinazi | 52127d7 | 2019-04-17 15:12:38 -0700 | [diff] [blame] | 201 | // This method returns true if the input was successfully parsed. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 202 | // TODO(nharper): Write fuzz tests for this method. |
dschinazi | 6c84c14 | 2019-07-31 09:11:49 -0700 | [diff] [blame] | 203 | QUIC_EXPORT_PRIVATE bool ParseTransportParameters(ParsedQuicVersion version, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 204 | Perspective perspective, |
dschinazi | 6c84c14 | 2019-07-31 09:11:49 -0700 | [diff] [blame] | 205 | const uint8_t* in, |
| 206 | size_t in_len, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 207 | TransportParameters* out); |
| 208 | |
| 209 | } // namespace quic |
| 210 | |
| 211 | #endif // QUICHE_QUIC_CORE_CRYPTO_TRANSPORT_PARAMETERS_H_ |