QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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 QUICHE_QUIC_CORE_QUIC_CRYPTO_SERVER_HANDSHAKER_H_ |
| 6 | #define QUICHE_QUIC_CORE_QUIC_CRYPTO_SERVER_HANDSHAKER_H_ |
| 7 | |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 10 | #include "net/third_party/quiche/src/quic/core/proto/cached_network_parameters.pb.h" |
| 11 | #include "net/third_party/quiche/src/quic/core/proto/source_address_token.pb.h" |
| 12 | #include "net/third_party/quiche/src/quic/core/quic_crypto_handshaker.h" |
| 13 | #include "net/third_party/quiche/src/quic/core/quic_crypto_server_stream.h" |
| 14 | #include "net/third_party/quiche/src/quic/core/quic_session.h" |
| 15 | #include "net/third_party/quiche/src/quic/platform/api/quic_export.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 16 | |
| 17 | namespace quic { |
| 18 | |
| 19 | namespace test { |
| 20 | class QuicCryptoServerStreamPeer; |
| 21 | } // namespace test |
| 22 | |
| 23 | class QUIC_EXPORT_PRIVATE QuicCryptoServerHandshaker |
| 24 | : public QuicCryptoServerStream::HandshakerDelegate, |
| 25 | public QuicCryptoHandshaker { |
| 26 | public: |
| 27 | // |crypto_config| must outlive the stream. |
| 28 | // |session| must outlive the stream. |
| 29 | // |helper| must outlive the stream. |
| 30 | QuicCryptoServerHandshaker(const QuicCryptoServerConfig* crypto_config, |
| 31 | QuicCryptoServerStream* stream, |
| 32 | QuicCompressedCertsCache* compressed_certs_cache, |
| 33 | QuicSession* session, |
| 34 | QuicCryptoServerStream::Helper* helper); |
| 35 | QuicCryptoServerHandshaker(const QuicCryptoServerHandshaker&) = delete; |
| 36 | QuicCryptoServerHandshaker& operator=(const QuicCryptoServerHandshaker&) = |
| 37 | delete; |
| 38 | |
| 39 | ~QuicCryptoServerHandshaker() override; |
| 40 | |
| 41 | // From HandshakerDelegate |
| 42 | void CancelOutstandingCallbacks() override; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 43 | bool GetBase64SHA256ClientChannelID(std::string* output) const override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 44 | void SendServerConfigUpdate( |
| 45 | const CachedNetworkParameters* cached_network_params) override; |
| 46 | uint8_t NumHandshakeMessages() const override; |
| 47 | uint8_t NumHandshakeMessagesWithServerNonces() const override; |
| 48 | int NumServerConfigUpdateMessagesSent() const override; |
| 49 | const CachedNetworkParameters* PreviousCachedNetworkParams() const override; |
| 50 | bool ZeroRttAttempted() const override; |
| 51 | void SetPreviousCachedNetworkParams( |
| 52 | CachedNetworkParameters cached_network_params) override; |
| 53 | bool ShouldSendExpectCTHeader() const override; |
| 54 | |
| 55 | // From QuicCryptoStream |
| 56 | bool encryption_established() const override; |
| 57 | bool handshake_confirmed() const override; |
| 58 | const QuicCryptoNegotiatedParameters& crypto_negotiated_params() |
| 59 | const override; |
| 60 | CryptoMessageParser* crypto_message_parser() override; |
| 61 | |
| 62 | // From QuicCryptoHandshaker |
| 63 | void OnHandshakeMessage(const CryptoHandshakeMessage& message) override; |
| 64 | |
| 65 | protected: |
| 66 | virtual void ProcessClientHello( |
| 67 | QuicReferenceCountedPointer<ValidateClientHelloResultCallback::Result> |
| 68 | result, |
| 69 | std::unique_ptr<ProofSource::Details> proof_source_details, |
| 70 | std::unique_ptr<ProcessClientHelloResultCallback> done_cb); |
| 71 | |
| 72 | // Hook that allows the server to set QuicConfig defaults just |
| 73 | // before going through the parameter negotiation step. |
| 74 | virtual void OverrideQuicConfigDefaults(QuicConfig* config); |
| 75 | |
| 76 | // Returns client address used to generate and validate source address token. |
| 77 | virtual const QuicSocketAddress GetClientAddress(); |
| 78 | |
| 79 | // Returns the QuicSession that this stream belongs to. |
| 80 | QuicSession* session() const { return session_; } |
| 81 | |
| 82 | void set_encryption_established(bool encryption_established) { |
| 83 | encryption_established_ = encryption_established; |
| 84 | } |
| 85 | |
| 86 | void set_handshake_confirmed(bool handshake_confirmed) { |
| 87 | handshake_confirmed_ = handshake_confirmed; |
| 88 | } |
| 89 | |
| 90 | private: |
| 91 | friend class test::QuicCryptoServerStreamPeer; |
| 92 | |
| 93 | class ValidateCallback : public ValidateClientHelloResultCallback { |
| 94 | public: |
| 95 | explicit ValidateCallback(QuicCryptoServerHandshaker* parent); |
| 96 | ValidateCallback(const ValidateCallback&) = delete; |
| 97 | ValidateCallback& operator=(const ValidateCallback&) = delete; |
| 98 | // To allow the parent to detach itself from the callback before deletion. |
| 99 | void Cancel(); |
| 100 | |
| 101 | // From ValidateClientHelloResultCallback |
| 102 | void Run(QuicReferenceCountedPointer<Result> result, |
| 103 | std::unique_ptr<ProofSource::Details> details) override; |
| 104 | |
| 105 | private: |
| 106 | QuicCryptoServerHandshaker* parent_; |
| 107 | }; |
| 108 | |
| 109 | class SendServerConfigUpdateCallback |
| 110 | : public BuildServerConfigUpdateMessageResultCallback { |
| 111 | public: |
| 112 | explicit SendServerConfigUpdateCallback(QuicCryptoServerHandshaker* parent); |
| 113 | SendServerConfigUpdateCallback(const SendServerConfigUpdateCallback&) = |
| 114 | delete; |
| 115 | void operator=(const SendServerConfigUpdateCallback&) = delete; |
| 116 | |
| 117 | // To allow the parent to detach itself from the callback before deletion. |
| 118 | void Cancel(); |
| 119 | |
| 120 | // From BuildServerConfigUpdateMessageResultCallback |
| 121 | void Run(bool ok, const CryptoHandshakeMessage& message) override; |
| 122 | |
| 123 | private: |
| 124 | QuicCryptoServerHandshaker* parent_; |
| 125 | }; |
| 126 | |
| 127 | // Invoked by ValidateCallback::RunImpl once initial validation of |
| 128 | // the client hello is complete. Finishes processing of the client |
| 129 | // hello message and handles handshake success/failure. |
| 130 | void FinishProcessingHandshakeMessage( |
| 131 | QuicReferenceCountedPointer<ValidateClientHelloResultCallback::Result> |
| 132 | result, |
| 133 | std::unique_ptr<ProofSource::Details> details); |
| 134 | |
| 135 | class ProcessClientHelloCallback; |
| 136 | friend class ProcessClientHelloCallback; |
| 137 | |
| 138 | // Portion of FinishProcessingHandshakeMessage which executes after |
| 139 | // ProcessClientHello has been called. |
| 140 | void FinishProcessingHandshakeMessageAfterProcessClientHello( |
| 141 | const ValidateClientHelloResultCallback::Result& result, |
| 142 | QuicErrorCode error, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 143 | const std::string& error_details, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 144 | std::unique_ptr<CryptoHandshakeMessage> reply, |
| 145 | std::unique_ptr<DiversificationNonce> diversification_nonce, |
| 146 | std::unique_ptr<ProofSource::Details> proof_source_details); |
| 147 | |
| 148 | // Invoked by SendServerConfigUpdateCallback::RunImpl once the proof has been |
| 149 | // received. |ok| indicates whether or not the proof was successfully |
| 150 | // acquired, and |message| holds the partially-constructed message from |
| 151 | // SendServerConfigUpdate. |
| 152 | void FinishSendServerConfigUpdate(bool ok, |
| 153 | const CryptoHandshakeMessage& message); |
| 154 | |
| 155 | // Returns a new ConnectionId to be used for statelessly rejected connections |
| 156 | // if |use_stateless_rejects| is true. Returns 0 otherwise. |
| 157 | QuicConnectionId GenerateConnectionIdForReject(bool use_stateless_rejects); |
| 158 | |
| 159 | // Returns the QuicTransportVersion of the connection. |
| 160 | QuicTransportVersion transport_version() const { |
| 161 | return session_->connection()->transport_version(); |
| 162 | } |
| 163 | |
| 164 | QuicCryptoServerStream* stream_; |
| 165 | |
| 166 | QuicSession* session_; |
| 167 | |
| 168 | // crypto_config_ contains crypto parameters for the handshake. |
| 169 | const QuicCryptoServerConfig* crypto_config_; |
| 170 | |
| 171 | // compressed_certs_cache_ contains a set of most recently compressed certs. |
| 172 | // Owned by QuicDispatcher. |
| 173 | QuicCompressedCertsCache* compressed_certs_cache_; |
| 174 | |
| 175 | // Server's certificate chain and signature of the server config, as provided |
| 176 | // by ProofSource::GetProof. |
| 177 | QuicReferenceCountedPointer<QuicSignedServerConfig> signed_config_; |
| 178 | |
| 179 | // Hash of the last received CHLO message which can be used for generating |
| 180 | // server config update messages. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 181 | std::string chlo_hash_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 182 | |
| 183 | // Pointer to the helper for this crypto stream. Must outlive this stream. |
| 184 | QuicCryptoServerStream::Helper* helper_; |
| 185 | |
| 186 | // Number of handshake messages received by this stream. |
| 187 | uint8_t num_handshake_messages_; |
| 188 | |
| 189 | // Number of handshake messages received by this stream that contain |
| 190 | // server nonces (indicating that this is a non-zero-RTT handshake |
| 191 | // attempt). |
| 192 | uint8_t num_handshake_messages_with_server_nonces_; |
| 193 | |
| 194 | // Pointer to the active callback that will receive the result of |
| 195 | // BuildServerConfigUpdateMessage and forward it to |
| 196 | // FinishSendServerConfigUpdate. nullptr if no update message is currently |
| 197 | // being built. |
| 198 | SendServerConfigUpdateCallback* send_server_config_update_cb_; |
| 199 | |
| 200 | // Number of server config update (SCUP) messages sent by this stream. |
| 201 | int num_server_config_update_messages_sent_; |
| 202 | |
| 203 | // If the client provides CachedNetworkParameters in the STK in the CHLO, then |
| 204 | // store here, and send back in future STKs if we have no better bandwidth |
| 205 | // estimate to send. |
| 206 | std::unique_ptr<CachedNetworkParameters> previous_cached_network_params_; |
| 207 | |
| 208 | // Contains any source address tokens which were present in the CHLO. |
| 209 | SourceAddressTokens previous_source_address_tokens_; |
| 210 | |
| 211 | // True if client attempts 0-rtt handshake (which can succeed or fail). If |
| 212 | // stateless rejects are used, this variable will be false for the stateless |
| 213 | // rejected connection and true for subsequent connections. |
| 214 | bool zero_rtt_attempted_; |
| 215 | |
| 216 | // Size of the packet containing the most recently received CHLO. |
| 217 | QuicByteCount chlo_packet_size_; |
| 218 | |
| 219 | // Pointer to the active callback that will receive the result of the client |
| 220 | // hello validation request and forward it to FinishProcessingHandshakeMessage |
| 221 | // for processing. nullptr if no handshake message is being validated. Note |
| 222 | // that this field is mutually exclusive with process_client_hello_cb_. |
| 223 | ValidateCallback* validate_client_hello_cb_; |
| 224 | |
| 225 | // Pointer to the active callback which will receive the results of |
| 226 | // ProcessClientHello and forward it to |
| 227 | // FinishProcessingHandshakeMessageAfterProcessClientHello. Note that this |
| 228 | // field is mutually exclusive with validate_client_hello_cb_. |
| 229 | ProcessClientHelloCallback* process_client_hello_cb_; |
| 230 | |
| 231 | bool encryption_established_; |
| 232 | bool handshake_confirmed_; |
| 233 | QuicReferenceCountedPointer<QuicCryptoNegotiatedParameters> |
| 234 | crypto_negotiated_params_; |
| 235 | }; |
| 236 | |
| 237 | } // namespace quic |
| 238 | |
| 239 | #endif // QUICHE_QUIC_CORE_QUIC_CRYPTO_SERVER_HANDSHAKER_H_ |