blob: 5c5390f442409b319c588077e919c3500d621a99 [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_CLIENT_HANDSHAKER_H_
6#define QUICHE_QUIC_CORE_TLS_CLIENT_HANDSHAKER_H_
7
vasilvv872e7a32019-03-12 16:42:44 -07008#include <string>
9
QUICHE teama6ef0a62019-03-07 20:34:33 -050010#include "third_party/boringssl/src/include/openssl/ssl.h"
11#include "net/third_party/quiche/src/quic/core/crypto/proof_verifier.h"
nharper6ebe83b2019-06-13 17:43:52 -070012#include "net/third_party/quiche/src/quic/core/crypto/tls_client_connection.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050013#include "net/third_party/quiche/src/quic/core/quic_crypto_client_stream.h"
14#include "net/third_party/quiche/src/quic/core/quic_crypto_stream.h"
15#include "net/third_party/quiche/src/quic/core/tls_handshaker.h"
16#include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
dmcardlecf0bfcf2019-12-13 08:08:21 -080017#include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050018
19namespace quic {
20
rch85240a12019-12-23 11:51:59 -080021// An implementation of QuicCryptoClientStream::HandshakerInterface which uses
QUICHE teama6ef0a62019-03-07 20:34:33 -050022// TLS 1.3 for the crypto handshake protocol.
23class QUIC_EXPORT_PRIVATE TlsClientHandshaker
nharper6ebe83b2019-06-13 17:43:52 -070024 : public TlsHandshaker,
rch85240a12019-12-23 11:51:59 -080025 public QuicCryptoClientStream::HandshakerInterface,
nharper6ebe83b2019-06-13 17:43:52 -070026 public TlsClientConnection::Delegate {
QUICHE teama6ef0a62019-03-07 20:34:33 -050027 public:
nharperdf7a77b2019-11-11 13:12:45 -080028 TlsClientHandshaker(const QuicServerId& server_id,
29 QuicCryptoStream* stream,
QUICHE teama6ef0a62019-03-07 20:34:33 -050030 QuicSession* session,
QUICHE teama6ef0a62019-03-07 20:34:33 -050031 std::unique_ptr<ProofVerifyContext> verify_context,
nharperdf7a77b2019-11-11 13:12:45 -080032 QuicCryptoClientConfig* crypto_config,
33 QuicCryptoClientStream::ProofHandler* proof_handler);
QUICHE teama6ef0a62019-03-07 20:34:33 -050034 TlsClientHandshaker(const TlsClientHandshaker&) = delete;
35 TlsClientHandshaker& operator=(const TlsClientHandshaker&) = delete;
36
37 ~TlsClientHandshaker() override;
38
rch85240a12019-12-23 11:51:59 -080039 // From QuicCryptoClientStream::HandshakerInterface
QUICHE teama6ef0a62019-03-07 20:34:33 -050040 bool CryptoConnect() override;
41 int num_sent_client_hellos() const override;
nharper02703962019-11-07 12:23:13 -080042 bool IsResumption() const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050043 int num_scup_messages_received() const override;
vasilvvc48c8712019-03-11 13:38:16 -070044 std::string chlo_hash() const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050045
rch85240a12019-12-23 11:51:59 -080046 // From QuicCryptoClientStream::HandshakerInterface and TlsHandshaker
QUICHE teama6ef0a62019-03-07 20:34:33 -050047 bool encryption_established() const override;
fayang685367a2020-01-14 10:40:15 -080048 bool one_rtt_keys_available() const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050049 const QuicCryptoNegotiatedParameters& crypto_negotiated_params()
50 const override;
51 CryptoMessageParser* crypto_message_parser() override;
fayang9a863cf2020-01-16 14:12:11 -080052 HandshakeState GetHandshakeState() const override;
nharper486a8a92019-08-28 16:25:10 -070053 size_t BufferSizeLimitForLevel(EncryptionLevel level) const override;
fayang2f2915d2020-01-24 06:47:15 -080054 void OnOneRttPacketAcknowledged() override;
fayang01062942020-01-22 07:23:23 -080055 void OnHandshakeDoneReceived() override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050056
fayangd58736d2019-11-27 13:35:31 -080057 // Override to drop initial keys if trying to write ENCRYPTION_HANDSHAKE data.
dmcardlecf0bfcf2019-12-13 08:08:21 -080058 void WriteMessage(EncryptionLevel level,
59 quiche::QuicheStringPiece data) override;
fayangd58736d2019-11-27 13:35:31 -080060
vasilvv4724c9c2019-08-29 11:52:11 -070061 void AllowEmptyAlpnForTests() { allow_empty_alpn_for_tests_ = true; }
62
nharper6ebe83b2019-06-13 17:43:52 -070063 protected:
nharper486a8a92019-08-28 16:25:10 -070064 const TlsConnection* tls_connection() const override {
65 return &tls_connection_;
66 }
nharper6ebe83b2019-06-13 17:43:52 -070067
68 void AdvanceHandshake() override;
69 void CloseConnection(QuicErrorCode error,
70 const std::string& reason_phrase) override;
71
72 // TlsClientConnection::Delegate implementation:
73 enum ssl_verify_result_t VerifyCert(uint8_t* out_alert) override;
74 TlsConnection::Delegate* ConnectionDelegate() override { return this; }
75
QUICHE teama6ef0a62019-03-07 20:34:33 -050076 private:
77 // ProofVerifierCallbackImpl handles the result of an asynchronous certificate
78 // verification operation.
dschinazif25169a2019-10-23 08:12:18 -070079 class QUIC_EXPORT_PRIVATE ProofVerifierCallbackImpl
80 : public ProofVerifierCallback {
QUICHE teama6ef0a62019-03-07 20:34:33 -050081 public:
82 explicit ProofVerifierCallbackImpl(TlsClientHandshaker* parent);
83 ~ProofVerifierCallbackImpl() override;
84
85 // ProofVerifierCallback interface.
86 void Run(bool ok,
vasilvvc48c8712019-03-11 13:38:16 -070087 const std::string& error_details,
QUICHE teama6ef0a62019-03-07 20:34:33 -050088 std::unique_ptr<ProofVerifyDetails>* details) override;
89
90 // If called, Cancel causes the pending callback to be a no-op.
91 void Cancel();
92
93 private:
94 TlsClientHandshaker* parent_;
95 };
96
97 enum State {
98 STATE_IDLE,
99 STATE_HANDSHAKE_RUNNING,
100 STATE_CERT_VERIFY_PENDING,
fayangd58736d2019-11-27 13:35:31 -0800101 STATE_ENCRYPTION_HANDSHAKE_DATA_SENT,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500102 STATE_HANDSHAKE_COMPLETE,
103 STATE_CONNECTION_CLOSED,
104 } state_ = STATE_IDLE;
105
vasilvv4724c9c2019-08-29 11:52:11 -0700106 bool SetAlpn();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500107 bool SetTransportParameters();
vasilvvc48c8712019-03-11 13:38:16 -0700108 bool ProcessTransportParameters(std::string* error_details);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500109 void FinishHandshake();
110
fayang2f2915d2020-01-24 06:47:15 -0800111 // Called when server completes handshake (i.e., either handshake done is
112 // received or 1-RTT packet gets acknowledged).
113 void OnHandshakeConfirmed();
114
nharperdf7a77b2019-11-11 13:12:45 -0800115 void InsertSession(bssl::UniquePtr<SSL_SESSION> session) override;
116
nharperf579b5e2020-01-21 14:11:18 -0800117 QuicSession* session() { return session_; }
118 QuicSession* session_;
119
QUICHE teama6ef0a62019-03-07 20:34:33 -0500120 QuicServerId server_id_;
121
122 // Objects used for verifying the server's certificate chain.
123 // |proof_verifier_| is owned by the caller of TlsClientHandshaker's
124 // constructor.
125 ProofVerifier* proof_verifier_;
126 std::unique_ptr<ProofVerifyContext> verify_context_;
nharper40bdf532019-10-03 11:16:22 -0700127 // Unowned pointer to the proof handler which has the
128 // OnProofVerifyDetailsAvailable callback to use for notifying the result of
129 // certificate verification.
130 QuicCryptoClientStream::ProofHandler* proof_handler_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500131
nharperdf7a77b2019-11-11 13:12:45 -0800132 // Used for session resumption. |session_cache_| is owned by the
133 // QuicCryptoClientConfig passed into TlsClientHandshaker's constructor.
134 SessionCache* session_cache_;
135
vasilvvc48c8712019-03-11 13:38:16 -0700136 std::string user_agent_id_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500137
138 // ProofVerifierCallback used for async certificate verification. This object
139 // is owned by |proof_verifier_|.
140 ProofVerifierCallbackImpl* proof_verify_callback_ = nullptr;
141 std::unique_ptr<ProofVerifyDetails> verify_details_;
142 enum ssl_verify_result_t verify_result_ = ssl_verify_retry;
vasilvvc48c8712019-03-11 13:38:16 -0700143 std::string cert_verify_error_details_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500144
145 bool encryption_established_ = false;
fayang685367a2020-01-14 10:40:15 -0800146 bool one_rtt_keys_available_ = false;
fayang01062942020-01-22 07:23:23 -0800147 bool handshake_confirmed_ = false;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500148 QuicReferenceCountedPointer<QuicCryptoNegotiatedParameters>
149 crypto_negotiated_params_;
nharper6ebe83b2019-06-13 17:43:52 -0700150
vasilvv4724c9c2019-08-29 11:52:11 -0700151 bool allow_empty_alpn_for_tests_ = false;
152
nharper6ebe83b2019-06-13 17:43:52 -0700153 TlsClientConnection tls_connection_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500154};
155
QUICHE teama6ef0a62019-03-07 20:34:33 -0500156} // namespace quic
157
158#endif // QUICHE_QUIC_CORE_TLS_CLIENT_HANDSHAKER_H_