blob: 0b5ddc367694b1bc8a4cf0e92480a2f976753bec [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// Copyright (c) 2017 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_TLS_HANDSHAKER_H_
6#define QUICHE_QUIC_CORE_TLS_HANDSHAKER_H_
7
vasilvvc872ee42020-10-07 19:50:22 -07008#include "absl/strings/string_view.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -05009#include "third_party/boringssl/src/include/openssl/base.h"
10#include "third_party/boringssl/src/include/openssl/ssl.h"
QUICHE team5be974e2020-12-29 18:35:24 -050011#include "quic/core/crypto/crypto_handshake.h"
12#include "quic/core/crypto/crypto_message_parser.h"
13#include "quic/core/crypto/proof_verifier.h"
14#include "quic/core/crypto/quic_decrypter.h"
15#include "quic/core/crypto/quic_encrypter.h"
16#include "quic/core/crypto/tls_connection.h"
17#include "quic/core/quic_session.h"
18#include "quic/platform/api/quic_export.h"
wubfa47a6d2021-04-16 08:56:26 -070019#include "quic/platform/api/quic_flags.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050020
21namespace quic {
22
23class QuicCryptoStream;
24
25// Base class for TlsClientHandshaker and TlsServerHandshaker. TlsHandshaker
26// provides functionality common to both the client and server, such as moving
27// messages between the TLS stack and the QUIC crypto stream, and handling
28// derivation of secrets.
nharper6ebe83b2019-06-13 17:43:52 -070029class QUIC_EXPORT_PRIVATE TlsHandshaker : public TlsConnection::Delegate,
30 public CryptoMessageParser {
QUICHE teama6ef0a62019-03-07 20:34:33 -050031 public:
32 // TlsHandshaker does not take ownership of any of its arguments; they must
33 // outlive the TlsHandshaker.
nharper80d3b172020-01-17 11:43:39 -080034 TlsHandshaker(QuicCryptoStream* stream, QuicSession* session);
QUICHE teama6ef0a62019-03-07 20:34:33 -050035 TlsHandshaker(const TlsHandshaker&) = delete;
36 TlsHandshaker& operator=(const TlsHandshaker&) = delete;
37
38 ~TlsHandshaker() override;
39
40 // From CryptoMessageParser
vasilvvc872ee42020-10-07 19:50:22 -070041 bool ProcessInput(absl::string_view input, EncryptionLevel level) override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050042 size_t InputBytesRemaining() const override { return 0; }
43 QuicErrorCode error() const override { return parser_error_; }
vasilvvc48c8712019-03-11 13:38:16 -070044 const std::string& error_detail() const override {
QUICHE teama6ef0a62019-03-07 20:34:33 -050045 return parser_error_detail_;
46 }
47
nharper82bea252020-09-16 16:18:49 -070048 // The following methods provide implementations to subclasses of
49 // TlsHandshaker which use them to implement methods of QuicCryptoStream.
50 CryptoMessageParser* crypto_message_parser() { return this; }
nharper486a8a92019-08-28 16:25:10 -070051 size_t BufferSizeLimitForLevel(EncryptionLevel level) const;
nharper6a6bd312020-09-17 13:20:53 -070052 ssl_early_data_reason_t EarlyDataReason() const;
mattm072a7e32020-10-09 16:16:56 -070053 std::unique_ptr<QuicDecrypter> AdvanceKeysAndCreateCurrentOneRttDecrypter();
54 std::unique_ptr<QuicEncrypter> CreateCurrentOneRttEncrypter();
nharper0ab1c662020-11-02 17:05:32 -080055 virtual HandshakeState GetHandshakeState() const = 0;
QUICHE teama6ef0a62019-03-07 20:34:33 -050056
57 protected:
nharper0ab1c662020-11-02 17:05:32 -080058 // Called when a new message is received on the crypto stream and is available
59 // for the TLS stack to read.
60 void AdvanceHandshake();
QUICHE teama6ef0a62019-03-07 20:34:33 -050061
nharper0ab1c662020-11-02 17:05:32 -080062 void CloseConnection(QuicErrorCode error, const std::string& reason_phrase);
mattm55006b02021-01-14 15:09:54 -080063 // Closes the connection, specifying the wire error code |ietf_error|
64 // explicitly.
65 void CloseConnection(QuicErrorCode error,
66 QuicIetfTransportErrorCodes ietf_error,
67 const std::string& reason_phrase);
nharper0ab1c662020-11-02 17:05:32 -080068
69 void OnConnectionClosed(QuicErrorCode error, ConnectionCloseSource source);
70
71 bool is_connection_closed() const { return is_connection_closed_; }
72
73 // Called when |SSL_do_handshake| returns 1, indicating that the handshake has
74 // finished. Note that due to 0-RTT, the handshake may "finish" twice;
75 // |SSL_in_early_data| can be used to determine whether the handshake is truly
76 // done.
wub48b16812021-04-30 10:22:22 -070077 // TODO(wub): When --quic_tls_retry_handshake_on_early_data is true, this
78 // function will only be called once when the handshake actually finishes.
79 // Update comment when deprecating the flag.
nharper0ab1c662020-11-02 17:05:32 -080080 virtual void FinishHandshake() = 0;
81
wub48b16812021-04-30 10:22:22 -070082 // Called when |SSL_do_handshake| returns 1 and the connection is in early
83 // data. In that case, |AdvanceHandshake| will call |OnEnterEarlyData| and
84 // retry |SSL_do_handshake| once.
85 virtual void OnEnterEarlyData() {
86 // By default, do nothing but check the preconditions.
87 QUICHE_DCHECK(retry_handshake_on_early_data_);
88 QUICHE_DCHECK(SSL_in_early_data(ssl()));
89 }
90
nharper0ab1c662020-11-02 17:05:32 -080091 // Called when a handshake message is received after the handshake is
92 // complete.
93 virtual void ProcessPostHandshakeMessage() = 0;
94
95 // Called when an unexpected error code is received from |SSL_get_error|. If a
96 // subclass can expect more than just a single error (as provided by
97 // |set_expected_ssl_error|), it can override this method to handle that case.
98 virtual bool ShouldCloseConnectionOnUnexpectedError(int ssl_error);
99
100 void set_expected_ssl_error(int ssl_error) {
101 expected_ssl_error_ = ssl_error;
102 }
103 int expected_ssl_error() const { return expected_ssl_error_; }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500104
nharpere62aab72020-11-04 16:32:09 -0800105 // Called to verify a cert chain. This is a simple wrapper around
106 // ProofVerifier or ServerProofVerifier, which optionally gathers additional
107 // arguments to pass into their VerifyCertChain method. This class retains a
108 // non-owning pointer to |callback|; the callback must live until this
109 // function returns QUIC_SUCCESS or QUIC_FAILURE, or until the callback is
110 // run.
nharper54fc9ab2020-11-12 11:07:39 -0800111 //
112 // If certificate verification fails, |*out_alert| may be set to a TLS alert
113 // that will be sent when closing the connection; it defaults to
114 // certificate_unknown. Implementations of VerifyCertChain may retain the
115 // |out_alert| pointer while performing an async operation.
nharpere62aab72020-11-04 16:32:09 -0800116 virtual QuicAsyncStatus VerifyCertChain(
117 const std::vector<std::string>& certs,
118 std::string* error_details,
119 std::unique_ptr<ProofVerifyDetails>* details,
nharper54fc9ab2020-11-12 11:07:39 -0800120 uint8_t* out_alert,
nharpere62aab72020-11-04 16:32:09 -0800121 std::unique_ptr<ProofVerifierCallback> callback) = 0;
122 // Called when certificate verification is completed.
123 virtual void OnProofVerifyDetailsAvailable(
124 const ProofVerifyDetails& verify_details) = 0;
125
QUICHE teama6ef0a62019-03-07 20:34:33 -0500126 // Returns the PRF used by the cipher suite negotiated in the TLS handshake.
nharper8d4ff5c2020-02-27 11:43:44 -0800127 const EVP_MD* Prf(const SSL_CIPHER* cipher);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500128
nharper486a8a92019-08-28 16:25:10 -0700129 virtual const TlsConnection* tls_connection() const = 0;
nharper6ebe83b2019-06-13 17:43:52 -0700130
nharper486a8a92019-08-28 16:25:10 -0700131 SSL* ssl() const { return tls_connection()->ssl(); }
nharper6ebe83b2019-06-13 17:43:52 -0700132
QUICHE teama6ef0a62019-03-07 20:34:33 -0500133 QuicCryptoStream* stream() { return stream_; }
renjietangbd33b622020-02-12 16:52:30 -0800134 HandshakerDelegateInterface* handshaker_delegate() {
135 return handshaker_delegate_;
136 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500137
nharpere62aab72020-11-04 16:32:09 -0800138 enum ssl_verify_result_t VerifyCert(uint8_t* out_alert) override;
139
nharperd08b82c2020-02-26 12:10:25 -0800140 // SetWriteSecret provides the encryption secret used to encrypt messages at
141 // encryption level |level|. The secret provided here is the one from the TLS
142 // 1.3 key schedule (RFC 8446 section 7.1), in particular the handshake
143 // traffic secrets and application traffic secrets. The provided write secret
144 // must be used with the provided cipher suite |cipher|.
145 void SetWriteSecret(EncryptionLevel level,
146 const SSL_CIPHER* cipher,
147 const std::vector<uint8_t>& write_secret) override;
148
149 // SetReadSecret is similar to SetWriteSecret, except that it is used for
150 // decrypting messages. SetReadSecret at a particular level is always called
151 // after SetWriteSecret for that level, except for ENCRYPTION_ZERO_RTT, where
152 // the EncryptionLevel for SetWriteSecret is ENCRYPTION_FORWARD_SECURE.
153 bool SetReadSecret(EncryptionLevel level,
154 const SSL_CIPHER* cipher,
155 const std::vector<uint8_t>& read_secret) override;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500156
157 // WriteMessage is called when there is |data| from the TLS stack ready for
158 // the QUIC stack to write in a crypto frame. The data must be transmitted at
159 // encryption level |level|.
vasilvvc872ee42020-10-07 19:50:22 -0700160 void WriteMessage(EncryptionLevel level, absl::string_view data) override;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500161
162 // FlushFlight is called to signal that the current flight of
163 // messages have all been written (via calls to WriteMessage) and can be
164 // flushed to the underlying transport.
nharper6ebe83b2019-06-13 17:43:52 -0700165 void FlushFlight() override;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500166
167 // SendAlert causes this TlsHandshaker to close the QUIC connection with an
168 // error code corresponding to the TLS alert description |desc|.
nharper6ebe83b2019-06-13 17:43:52 -0700169 void SendAlert(EncryptionLevel level, uint8_t desc) override;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500170
wubfa47a6d2021-04-16 08:56:26 -0700171 const bool add_packet_flusher_on_async_op_done_ =
172 GetQuicReloadableFlag(quic_add_packet_flusher_on_async_op_done);
173
wub48b16812021-04-30 10:22:22 -0700174 const bool retry_handshake_on_early_data_ =
175 GetQuicReloadableFlag(quic_tls_retry_handshake_on_early_data);
176
nharper6ebe83b2019-06-13 17:43:52 -0700177 private:
nharpere62aab72020-11-04 16:32:09 -0800178 // ProofVerifierCallbackImpl handles the result of an asynchronous certificate
179 // verification operation.
180 class QUIC_EXPORT_PRIVATE ProofVerifierCallbackImpl
181 : public ProofVerifierCallback {
182 public:
183 explicit ProofVerifierCallbackImpl(TlsHandshaker* parent);
184 ~ProofVerifierCallbackImpl() override;
185
186 // ProofVerifierCallback interface.
187 void Run(bool ok,
188 const std::string& error_details,
189 std::unique_ptr<ProofVerifyDetails>* details) override;
190
191 // If called, Cancel causes the pending callback to be a no-op.
192 void Cancel();
193
194 private:
195 // Non-owning pointer to the TlsHandshaker responsible for this callback.
196 // |parent_| must be valid for the life of this callback or until |Cancel|
197 // is called.
198 TlsHandshaker* parent_;
199 };
200
201 // ProofVerifierCallback used for async certificate verification. Ownership of
202 // this object is transferred to |VerifyCertChain|;
203 ProofVerifierCallbackImpl* proof_verify_callback_ = nullptr;
204 std::unique_ptr<ProofVerifyDetails> verify_details_;
205 enum ssl_verify_result_t verify_result_ = ssl_verify_retry;
nharper54fc9ab2020-11-12 11:07:39 -0800206 uint8_t cert_verify_tls_alert_ = SSL_AD_CERTIFICATE_UNKNOWN;
nharpere62aab72020-11-04 16:32:09 -0800207 std::string cert_verify_error_details_;
208
nharper0ab1c662020-11-02 17:05:32 -0800209 int expected_ssl_error_ = SSL_ERROR_WANT_READ;
210 bool is_connection_closed_ = false;
211
QUICHE teama6ef0a62019-03-07 20:34:33 -0500212 QuicCryptoStream* stream_;
renjietangbd33b622020-02-12 16:52:30 -0800213 HandshakerDelegateInterface* handshaker_delegate_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500214
215 QuicErrorCode parser_error_ = QUIC_NO_ERROR;
vasilvvc48c8712019-03-11 13:38:16 -0700216 std::string parser_error_detail_;
mattm072a7e32020-10-09 16:16:56 -0700217
218 // The most recently derived 1-RTT read and write secrets, which are updated
219 // on each key update.
220 std::vector<uint8_t> latest_read_secret_;
221 std::vector<uint8_t> latest_write_secret_;
222 // 1-RTT header protection keys, which are not changed during key update.
223 std::vector<uint8_t> one_rtt_read_header_protection_key_;
224 std::vector<uint8_t> one_rtt_write_header_protection_key_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500225};
226
227} // namespace quic
228
229#endif // QUICHE_QUIC_CORE_TLS_HANDSHAKER_H_