blob: a2fa96a5f7137f05ac81dfd85df203002c936c21 [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;
QUICHE teama6ef0a62019-03-07 20:34:33 -050054
fayangd58736d2019-11-27 13:35:31 -080055 // Override to drop initial keys if trying to write ENCRYPTION_HANDSHAKE data.
dmcardlecf0bfcf2019-12-13 08:08:21 -080056 void WriteMessage(EncryptionLevel level,
57 quiche::QuicheStringPiece data) override;
fayangd58736d2019-11-27 13:35:31 -080058
vasilvv4724c9c2019-08-29 11:52:11 -070059 void AllowEmptyAlpnForTests() { allow_empty_alpn_for_tests_ = true; }
60
nharper6ebe83b2019-06-13 17:43:52 -070061 protected:
nharper486a8a92019-08-28 16:25:10 -070062 const TlsConnection* tls_connection() const override {
63 return &tls_connection_;
64 }
nharper6ebe83b2019-06-13 17:43:52 -070065
66 void AdvanceHandshake() override;
67 void CloseConnection(QuicErrorCode error,
68 const std::string& reason_phrase) override;
69
70 // TlsClientConnection::Delegate implementation:
71 enum ssl_verify_result_t VerifyCert(uint8_t* out_alert) override;
72 TlsConnection::Delegate* ConnectionDelegate() override { return this; }
73
QUICHE teama6ef0a62019-03-07 20:34:33 -050074 private:
75 // ProofVerifierCallbackImpl handles the result of an asynchronous certificate
76 // verification operation.
dschinazif25169a2019-10-23 08:12:18 -070077 class QUIC_EXPORT_PRIVATE ProofVerifierCallbackImpl
78 : public ProofVerifierCallback {
QUICHE teama6ef0a62019-03-07 20:34:33 -050079 public:
80 explicit ProofVerifierCallbackImpl(TlsClientHandshaker* parent);
81 ~ProofVerifierCallbackImpl() override;
82
83 // ProofVerifierCallback interface.
84 void Run(bool ok,
vasilvvc48c8712019-03-11 13:38:16 -070085 const std::string& error_details,
QUICHE teama6ef0a62019-03-07 20:34:33 -050086 std::unique_ptr<ProofVerifyDetails>* details) override;
87
88 // If called, Cancel causes the pending callback to be a no-op.
89 void Cancel();
90
91 private:
92 TlsClientHandshaker* parent_;
93 };
94
95 enum State {
96 STATE_IDLE,
97 STATE_HANDSHAKE_RUNNING,
98 STATE_CERT_VERIFY_PENDING,
fayangd58736d2019-11-27 13:35:31 -080099 STATE_ENCRYPTION_HANDSHAKE_DATA_SENT,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500100 STATE_HANDSHAKE_COMPLETE,
101 STATE_CONNECTION_CLOSED,
102 } state_ = STATE_IDLE;
103
vasilvv4724c9c2019-08-29 11:52:11 -0700104 bool SetAlpn();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500105 bool SetTransportParameters();
vasilvvc48c8712019-03-11 13:38:16 -0700106 bool ProcessTransportParameters(std::string* error_details);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500107 void FinishHandshake();
108
nharperdf7a77b2019-11-11 13:12:45 -0800109 void InsertSession(bssl::UniquePtr<SSL_SESSION> session) override;
110
QUICHE teama6ef0a62019-03-07 20:34:33 -0500111 QuicServerId server_id_;
112
113 // Objects used for verifying the server's certificate chain.
114 // |proof_verifier_| is owned by the caller of TlsClientHandshaker's
115 // constructor.
116 ProofVerifier* proof_verifier_;
117 std::unique_ptr<ProofVerifyContext> verify_context_;
nharper40bdf532019-10-03 11:16:22 -0700118 // Unowned pointer to the proof handler which has the
119 // OnProofVerifyDetailsAvailable callback to use for notifying the result of
120 // certificate verification.
121 QuicCryptoClientStream::ProofHandler* proof_handler_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500122
nharperdf7a77b2019-11-11 13:12:45 -0800123 // Used for session resumption. |session_cache_| is owned by the
124 // QuicCryptoClientConfig passed into TlsClientHandshaker's constructor.
125 SessionCache* session_cache_;
126
vasilvvc48c8712019-03-11 13:38:16 -0700127 std::string user_agent_id_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500128
129 // ProofVerifierCallback used for async certificate verification. This object
130 // is owned by |proof_verifier_|.
131 ProofVerifierCallbackImpl* proof_verify_callback_ = nullptr;
132 std::unique_ptr<ProofVerifyDetails> verify_details_;
133 enum ssl_verify_result_t verify_result_ = ssl_verify_retry;
vasilvvc48c8712019-03-11 13:38:16 -0700134 std::string cert_verify_error_details_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500135
136 bool encryption_established_ = false;
fayang685367a2020-01-14 10:40:15 -0800137 bool one_rtt_keys_available_ = false;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500138 QuicReferenceCountedPointer<QuicCryptoNegotiatedParameters>
139 crypto_negotiated_params_;
nharper6ebe83b2019-06-13 17:43:52 -0700140
vasilvv4724c9c2019-08-29 11:52:11 -0700141 bool allow_empty_alpn_for_tests_ = false;
142
nharper6ebe83b2019-06-13 17:43:52 -0700143 TlsClientConnection tls_connection_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500144};
145
QUICHE teama6ef0a62019-03-07 20:34:33 -0500146} // namespace quic
147
148#endif // QUICHE_QUIC_CORE_TLS_CLIENT_HANDSHAKER_H_