blob: a00302779d9ba654178d0f39d34b327e5c09b544 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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_STREAM_H_
6#define QUICHE_QUIC_CORE_QUIC_CRYPTO_SERVER_STREAM_H_
7
8#include <cstdint>
9#include <memory>
vasilvv872e7a32019-03-12 16:42:44 -070010#include <string>
QUICHE teama6ef0a62019-03-07 20:34:33 -050011
QUICHE teama6ef0a62019-03-07 20:34:33 -050012#include "net/third_party/quiche/src/quic/core/crypto/crypto_handshake.h"
13#include "net/third_party/quiche/src/quic/core/crypto/quic_compressed_certs_cache.h"
14#include "net/third_party/quiche/src/quic/core/crypto/quic_crypto_server_config.h"
15#include "net/third_party/quiche/src/quic/core/quic_config.h"
16#include "net/third_party/quiche/src/quic/core/quic_crypto_handshaker.h"
17#include "net/third_party/quiche/src/quic/core/quic_crypto_stream.h"
18#include "net/third_party/quiche/src/quic/core/quic_session.h"
19#include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050020
21namespace quic {
22
23class CachedNetworkParameters;
24class CryptoHandshakeMessage;
25class QuicCryptoServerConfig;
26class QuicCryptoServerStreamBase;
27
28// TODO(alyssar) see what can be moved out of QuicCryptoServerStream with
29// various code and test refactoring.
30class QUIC_EXPORT_PRIVATE QuicCryptoServerStreamBase : public QuicCryptoStream {
31 public:
32 explicit QuicCryptoServerStreamBase(QuicSession* session);
33
34 ~QuicCryptoServerStreamBase() override {}
35
36 // Cancel any outstanding callbacks, such as asynchronous validation of client
37 // hello.
38 virtual void CancelOutstandingCallbacks() = 0;
39
40 // GetBase64SHA256ClientChannelID sets |*output| to the base64 encoded,
41 // SHA-256 hash of the client's ChannelID key and returns true, if the client
42 // presented a ChannelID. Otherwise it returns false.
vasilvvc48c8712019-03-11 13:38:16 -070043 virtual bool GetBase64SHA256ClientChannelID(std::string* output) const = 0;
QUICHE teama6ef0a62019-03-07 20:34:33 -050044
45 virtual int NumServerConfigUpdateMessagesSent() const = 0;
46
47 // Sends the latest server config and source-address token to the client.
48 virtual void SendServerConfigUpdate(
49 const CachedNetworkParameters* cached_network_params) = 0;
50
51 // These are all accessors and setters to their respective counters.
52 virtual uint8_t NumHandshakeMessages() const = 0;
53 virtual uint8_t NumHandshakeMessagesWithServerNonces() const = 0;
54 virtual bool UseStatelessRejectsIfPeerSupported() const = 0;
55 virtual bool PeerSupportsStatelessRejects() const = 0;
56 virtual bool ZeroRttAttempted() const = 0;
57 virtual void SetPeerSupportsStatelessRejects(bool set) = 0;
58 virtual const CachedNetworkParameters* PreviousCachedNetworkParams()
59 const = 0;
60 virtual void SetPreviousCachedNetworkParams(
61 CachedNetworkParameters cached_network_params) = 0;
62
63 // Checks the options on the handshake-message to see whether the
64 // peer supports stateless-rejects.
65 static bool DoesPeerSupportStatelessRejects(
66 const CryptoHandshakeMessage& message);
67};
68
69class QUIC_EXPORT_PRIVATE QuicCryptoServerStream
70 : public QuicCryptoServerStreamBase {
71 public:
72 // QuicCryptoServerStream creates a HandshakerDelegate at construction time
73 // based on the QuicTransportVersion of the connection. Different
74 // HandshakerDelegates provide implementations of different crypto handshake
75 // protocols. Currently QUIC crypto is the only protocol implemented; a future
76 // HandshakerDelegate will use TLS as the handshake protocol.
77 // QuicCryptoServerStream delegates all of its public methods to its
78 // HandshakerDelegate.
79 //
80 // This setup of the crypto stream delegating its implementation to the
81 // handshaker results in the handshaker reading and writing bytes on the
82 // crypto stream, instead of the handshake rpassing the stream bytes to send.
83 class QUIC_EXPORT_PRIVATE HandshakerDelegate {
84 public:
85 virtual ~HandshakerDelegate() {}
86
87 // Cancel any outstanding callbacks, such as asynchronous validation of
88 // client hello.
89 virtual void CancelOutstandingCallbacks() = 0;
90
91 // GetBase64SHA256ClientChannelID sets |*output| to the base64 encoded,
92 // SHA-256 hash of the client's ChannelID key and returns true, if the
93 // client presented a ChannelID. Otherwise it returns false.
vasilvvc48c8712019-03-11 13:38:16 -070094 virtual bool GetBase64SHA256ClientChannelID(std::string* output) const = 0;
QUICHE teama6ef0a62019-03-07 20:34:33 -050095
96 // Sends the latest server config and source-address token to the client.
97 virtual void SendServerConfigUpdate(
98 const CachedNetworkParameters* cached_network_params) = 0;
99
100 // These are all accessors and setters to their respective counters.
101 virtual uint8_t NumHandshakeMessages() const = 0;
102 virtual uint8_t NumHandshakeMessagesWithServerNonces() const = 0;
103 virtual int NumServerConfigUpdateMessagesSent() const = 0;
104 virtual const CachedNetworkParameters* PreviousCachedNetworkParams()
105 const = 0;
106 virtual bool ZeroRttAttempted() const = 0;
107 virtual void SetPreviousCachedNetworkParams(
108 CachedNetworkParameters cached_network_params) = 0;
109
110 // NOTE: Indicating that the Expect-CT header should be sent here presents a
111 // layering violation to some extent. The Expect-CT header only applies to
112 // HTTP connections, while this class can be used for non-HTTP applications.
113 // However, it is exposed here because that is the only place where the
114 // configuration for the certificate used in the connection is accessible.
115 virtual bool ShouldSendExpectCTHeader() const = 0;
116
117 // Returns true once any encrypter (initial/0RTT or final/1RTT) has been set
118 // for the connection.
119 virtual bool encryption_established() const = 0;
120
121 // Returns true once the crypto handshake has completed.
122 virtual bool handshake_confirmed() const = 0;
123
124 // Returns the parameters negotiated in the crypto handshake.
125 virtual const QuicCryptoNegotiatedParameters& crypto_negotiated_params()
126 const = 0;
127
128 // Used by QuicCryptoStream to parse data received on this stream.
129 virtual CryptoMessageParser* crypto_message_parser() = 0;
130 };
131
132 class Helper {
133 public:
134 virtual ~Helper() {}
135
136 // Given the current connection_id, generates a new ConnectionId to
137 // be returned with a stateless reject.
138 virtual QuicConnectionId GenerateConnectionIdForReject(
139 QuicTransportVersion version,
140 QuicConnectionId connection_id) const = 0;
141
142 // Returns true if |message|, which was received on |self_address| is
143 // acceptable according to the visitor's policy. Otherwise, returns false
144 // and populates |error_details|.
145 virtual bool CanAcceptClientHello(const CryptoHandshakeMessage& message,
146 const QuicSocketAddress& client_address,
147 const QuicSocketAddress& peer_address,
148 const QuicSocketAddress& self_address,
vasilvvc48c8712019-03-11 13:38:16 -0700149 std::string* error_details) const = 0;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500150 };
151
152 // |crypto_config| must outlive the stream.
153 // |session| must outlive the stream.
154 // |helper| must outlive the stream.
155 QuicCryptoServerStream(const QuicCryptoServerConfig* crypto_config,
156 QuicCompressedCertsCache* compressed_certs_cache,
157 bool use_stateless_rejects_if_peer_supported,
158 QuicSession* session,
159 Helper* helper);
160 QuicCryptoServerStream(const QuicCryptoServerStream&) = delete;
161 QuicCryptoServerStream& operator=(const QuicCryptoServerStream&) = delete;
162
163 ~QuicCryptoServerStream() override;
164
165 // From QuicCryptoServerStreamBase
166 void CancelOutstandingCallbacks() override;
vasilvvc48c8712019-03-11 13:38:16 -0700167 bool GetBase64SHA256ClientChannelID(std::string* output) const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500168 void SendServerConfigUpdate(
169 const CachedNetworkParameters* cached_network_params) override;
170 uint8_t NumHandshakeMessages() const override;
171 uint8_t NumHandshakeMessagesWithServerNonces() const override;
172 int NumServerConfigUpdateMessagesSent() const override;
173 const CachedNetworkParameters* PreviousCachedNetworkParams() const override;
174 bool UseStatelessRejectsIfPeerSupported() const override;
175 bool PeerSupportsStatelessRejects() const override;
176 bool ZeroRttAttempted() const override;
177 void SetPeerSupportsStatelessRejects(
178 bool peer_supports_stateless_rejects) override;
179 void SetPreviousCachedNetworkParams(
180 CachedNetworkParameters cached_network_params) override;
181
182 // NOTE: Indicating that the Expect-CT header should be sent here presents
183 // a layering violation to some extent. The Expect-CT header only applies to
184 // HTTP connections, while this class can be used for non-HTTP applications.
185 // However, it is exposed here because that is the only place where the
186 // configuration for the certificate used in the connection is accessible.
187 bool ShouldSendExpectCTHeader() const;
188
189 bool encryption_established() const override;
190 bool handshake_confirmed() const override;
191 const QuicCryptoNegotiatedParameters& crypto_negotiated_params()
192 const override;
193 CryptoMessageParser* crypto_message_parser() override;
194 void OnSuccessfulVersionNegotiation(
195 const ParsedQuicVersion& version) override;
196
197 protected:
198 // Provided so that subclasses can provide their own handshaker.
199 virtual HandshakerDelegate* handshaker() const;
200
201 private:
202 std::unique_ptr<HandshakerDelegate> handshaker_;
203
204 // If true, the server should use stateless rejects, so long as the
205 // client supports them, as indicated by
206 // peer_supports_stateless_rejects_.
207 bool use_stateless_rejects_if_peer_supported_;
208
209 // Set to true, once the server has received information from the
210 // client that it supports stateless reject.
211 // TODO(jokulik): Remove once client stateless reject support
212 // becomes the default.
213 bool peer_supports_stateless_rejects_;
214
215 // Arguments from QuicCryptoServerStream constructor that might need to be
216 // passed to the HandshakerDelegate constructor in its late construction.
217 const QuicCryptoServerConfig* crypto_config_;
218 QuicCompressedCertsCache* compressed_certs_cache_;
219 Helper* helper_;
220};
221
222} // namespace quic
223
224#endif // QUICHE_QUIC_CORE_QUIC_CRYPTO_SERVER_STREAM_H_