Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / quic / quic_crypto_server_stream.h
blob0a1a61a4f7a1f48f8063bfd8efcc97e1abc002e7
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.
5 #ifndef NET_QUIC_QUIC_CRYPTO_SERVER_STREAM_H_
6 #define NET_QUIC_QUIC_CRYPTO_SERVER_STREAM_H_
8 #include <string>
10 #include "net/quic/crypto/crypto_handshake.h"
11 #include "net/quic/crypto/quic_crypto_server_config.h"
12 #include "net/quic/proto/source_address_token.pb.h"
13 #include "net/quic/quic_config.h"
14 #include "net/quic/quic_crypto_stream.h"
16 namespace net {
18 class CachedNetworkParameters;
19 class CryptoHandshakeMessage;
20 class QuicCryptoServerConfig;
21 class QuicCryptoServerStream;
22 class QuicSession;
24 namespace test {
25 class CryptoTestUtils;
26 class QuicCryptoServerStreamPeer;
27 } // namespace test
29 // Receives a notification when the server hello (SHLO) has been ACKed by the
30 // peer. At this point we disable HANDSHAKE_MODE in the sent packet manager.
31 class NET_EXPORT_PRIVATE ServerHelloNotifier : public
32 QuicAckNotifier::DelegateInterface {
33 public:
34 explicit ServerHelloNotifier(QuicCryptoServerStream* stream)
35 : server_stream_(stream) {}
37 // QuicAckNotifier::DelegateInterface implementation
38 void OnAckNotification(int num_retransmitted_packets,
39 int num_retransmitted_bytes,
40 QuicTime::Delta delta_largest_observed) override;
42 private:
43 ~ServerHelloNotifier() override {}
45 QuicCryptoServerStream* server_stream_;
47 DISALLOW_COPY_AND_ASSIGN(ServerHelloNotifier);
50 class NET_EXPORT_PRIVATE QuicCryptoServerStream : public QuicCryptoStream {
51 public:
52 // |crypto_config| must outlive the stream.
53 QuicCryptoServerStream(const QuicCryptoServerConfig* crypto_config,
54 QuicSession* session);
55 ~QuicCryptoServerStream() override;
57 // Cancel any outstanding callbacks, such as asynchronous validation of client
58 // hello.
59 void CancelOutstandingCallbacks();
61 // CryptoFramerVisitorInterface implementation
62 void OnHandshakeMessage(const CryptoHandshakeMessage& message) override;
64 // GetBase64SHA256ClientChannelID sets |*output| to the base64 encoded,
65 // SHA-256 hash of the client's ChannelID key and returns true, if the client
66 // presented a ChannelID. Otherwise it returns false.
67 bool GetBase64SHA256ClientChannelID(std::string* output) const;
69 uint8 num_handshake_messages() const { return num_handshake_messages_; }
71 uint8 num_handshake_messages_with_server_nonces() const {
72 return num_handshake_messages_with_server_nonces_;
75 int num_server_config_update_messages_sent() const {
76 return num_server_config_update_messages_sent_;
79 // Sends the latest server config and source-address token to the client.
80 virtual void SendServerConfigUpdate(
81 const CachedNetworkParameters* cached_network_params);
83 // Called by the ServerHello AckNotifier once the SHLO has been ACKed by the
84 // client.
85 void OnServerHelloAcked();
87 void set_previous_cached_network_params(
88 CachedNetworkParameters cached_network_params);
90 const CachedNetworkParameters* previous_cached_network_params() const;
92 bool use_stateless_rejects_if_peer_supported() const {
93 return use_stateless_rejects_if_peer_supported_;
96 // Used by the quic dispatcher to indicate that this crypto server
97 // stream should use stateless rejects, so long as stateless rejects
98 // are supported by the client.
99 void set_use_stateless_rejects_if_peer_supported(
100 bool use_stateless_rejects_if_peer_supported) {
101 use_stateless_rejects_if_peer_supported_ =
102 use_stateless_rejects_if_peer_supported;
105 bool peer_supports_stateless_rejects() const {
106 return peer_supports_stateless_rejects_;
109 void set_peer_supports_stateless_rejects(
110 bool peer_supports_stateless_rejects) {
111 peer_supports_stateless_rejects_ = peer_supports_stateless_rejects;
114 protected:
115 virtual QuicErrorCode ProcessClientHello(
116 const CryptoHandshakeMessage& message,
117 const ValidateClientHelloResultCallback::Result& result,
118 CryptoHandshakeMessage* reply,
119 std::string* error_details);
121 // Hook that allows the server to set QuicConfig defaults just
122 // before going through the parameter negotiation step.
123 virtual void OverrideQuicConfigDefaults(QuicConfig* config);
125 // Given the current connection_id, generates a new ConnectionId to
126 // be returned with a stateless reject.
127 virtual QuicConnectionId GenerateConnectionIdForReject(
128 QuicConnectionId connection_id);
130 private:
131 friend class test::CryptoTestUtils;
132 friend class test::QuicCryptoServerStreamPeer;
134 class ValidateCallback : public ValidateClientHelloResultCallback {
135 public:
136 explicit ValidateCallback(QuicCryptoServerStream* parent);
137 // To allow the parent to detach itself from the callback before deletion.
138 void Cancel();
140 // From ValidateClientHelloResultCallback
141 void RunImpl(const CryptoHandshakeMessage& client_hello,
142 const Result& result) override;
144 private:
145 QuicCryptoServerStream* parent_;
147 DISALLOW_COPY_AND_ASSIGN(ValidateCallback);
150 // Invoked by ValidateCallback::RunImpl once initial validation of
151 // the client hello is complete. Finishes processing of the client
152 // hello message and handles handshake success/failure.
153 void FinishProcessingHandshakeMessage(
154 const CryptoHandshakeMessage& message,
155 const ValidateClientHelloResultCallback::Result& result);
157 // Checks the options on the handshake-message to see whether the
158 // peer supports stateless-rejects.
159 static bool DoesPeerSupportStatelessRejects(
160 const CryptoHandshakeMessage& message);
162 // crypto_config_ contains crypto parameters for the handshake.
163 const QuicCryptoServerConfig* crypto_config_;
165 // Pointer to the active callback that will receive the result of
166 // the client hello validation request and forward it to
167 // FinishProcessingHandshakeMessage for processing. nullptr if no
168 // handshake message is being validated.
169 ValidateCallback* validate_client_hello_cb_;
171 // Number of handshake messages received by this stream.
172 uint8 num_handshake_messages_;
174 // Number of handshake messages received by this stream that contain
175 // server nonces (indicating that this is a non-zero-RTT handshake
176 // attempt).
177 uint8 num_handshake_messages_with_server_nonces_;
179 // Number of server config update (SCUP) messages sent by this stream.
180 int num_server_config_update_messages_sent_;
182 // If the client provides CachedNetworkParameters in the STK in the CHLO, then
183 // store here, and send back in future STKs if we have no better bandwidth
184 // estimate to send.
185 scoped_ptr<CachedNetworkParameters> previous_cached_network_params_;
187 // Contains any source address tokens which were present in the CHLO.
188 SourceAddressTokens previous_source_address_tokens_;
190 // If true, the server should use stateless rejects, so long as the
191 // client supports them, as indicated by
192 // peer_supports_stateless_rejects_.
193 bool use_stateless_rejects_if_peer_supported_;
195 // Set to true, once the server has received information from the
196 // client that it supports stateless reject.
197 // TODO(jokulik): Remove once client stateless reject support
198 // becomes the default.
199 bool peer_supports_stateless_rejects_;
201 DISALLOW_COPY_AND_ASSIGN(QuicCryptoServerStream);
204 } // namespace net
206 #endif // NET_QUIC_QUIC_CRYPTO_SERVER_STREAM_H_