blob: 14ef352a12d882aa62bb67914fa39d079befae05 [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;
nharper486a8a92019-08-28 16:25:10 -070052 size_t BufferSizeLimitForLevel(EncryptionLevel level) const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050053
fayangd58736d2019-11-27 13:35:31 -080054 // Override to drop initial keys if trying to write ENCRYPTION_HANDSHAKE data.
dmcardlecf0bfcf2019-12-13 08:08:21 -080055 void WriteMessage(EncryptionLevel level,
56 quiche::QuicheStringPiece data) override;
fayangd58736d2019-11-27 13:35:31 -080057
vasilvv4724c9c2019-08-29 11:52:11 -070058 void AllowEmptyAlpnForTests() { allow_empty_alpn_for_tests_ = true; }
59
nharper6ebe83b2019-06-13 17:43:52 -070060 protected:
nharper486a8a92019-08-28 16:25:10 -070061 const TlsConnection* tls_connection() const override {
62 return &tls_connection_;
63 }
nharper6ebe83b2019-06-13 17:43:52 -070064
65 void AdvanceHandshake() override;
66 void CloseConnection(QuicErrorCode error,
67 const std::string& reason_phrase) override;
68
69 // TlsClientConnection::Delegate implementation:
70 enum ssl_verify_result_t VerifyCert(uint8_t* out_alert) override;
71 TlsConnection::Delegate* ConnectionDelegate() override { return this; }
72
QUICHE teama6ef0a62019-03-07 20:34:33 -050073 private:
74 // ProofVerifierCallbackImpl handles the result of an asynchronous certificate
75 // verification operation.
dschinazif25169a2019-10-23 08:12:18 -070076 class QUIC_EXPORT_PRIVATE ProofVerifierCallbackImpl
77 : public ProofVerifierCallback {
QUICHE teama6ef0a62019-03-07 20:34:33 -050078 public:
79 explicit ProofVerifierCallbackImpl(TlsClientHandshaker* parent);
80 ~ProofVerifierCallbackImpl() override;
81
82 // ProofVerifierCallback interface.
83 void Run(bool ok,
vasilvvc48c8712019-03-11 13:38:16 -070084 const std::string& error_details,
QUICHE teama6ef0a62019-03-07 20:34:33 -050085 std::unique_ptr<ProofVerifyDetails>* details) override;
86
87 // If called, Cancel causes the pending callback to be a no-op.
88 void Cancel();
89
90 private:
91 TlsClientHandshaker* parent_;
92 };
93
94 enum State {
95 STATE_IDLE,
96 STATE_HANDSHAKE_RUNNING,
97 STATE_CERT_VERIFY_PENDING,
fayangd58736d2019-11-27 13:35:31 -080098 STATE_ENCRYPTION_HANDSHAKE_DATA_SENT,
QUICHE teama6ef0a62019-03-07 20:34:33 -050099 STATE_HANDSHAKE_COMPLETE,
100 STATE_CONNECTION_CLOSED,
101 } state_ = STATE_IDLE;
102
vasilvv4724c9c2019-08-29 11:52:11 -0700103 bool SetAlpn();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500104 bool SetTransportParameters();
vasilvvc48c8712019-03-11 13:38:16 -0700105 bool ProcessTransportParameters(std::string* error_details);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500106 void FinishHandshake();
107
nharperdf7a77b2019-11-11 13:12:45 -0800108 void InsertSession(bssl::UniquePtr<SSL_SESSION> session) override;
109
QUICHE teama6ef0a62019-03-07 20:34:33 -0500110 QuicServerId server_id_;
111
112 // Objects used for verifying the server's certificate chain.
113 // |proof_verifier_| is owned by the caller of TlsClientHandshaker's
114 // constructor.
115 ProofVerifier* proof_verifier_;
116 std::unique_ptr<ProofVerifyContext> verify_context_;
nharper40bdf532019-10-03 11:16:22 -0700117 // Unowned pointer to the proof handler which has the
118 // OnProofVerifyDetailsAvailable callback to use for notifying the result of
119 // certificate verification.
120 QuicCryptoClientStream::ProofHandler* proof_handler_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500121
nharperdf7a77b2019-11-11 13:12:45 -0800122 // Used for session resumption. |session_cache_| is owned by the
123 // QuicCryptoClientConfig passed into TlsClientHandshaker's constructor.
124 SessionCache* session_cache_;
125
vasilvvc48c8712019-03-11 13:38:16 -0700126 std::string user_agent_id_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500127
128 // ProofVerifierCallback used for async certificate verification. This object
129 // is owned by |proof_verifier_|.
130 ProofVerifierCallbackImpl* proof_verify_callback_ = nullptr;
131 std::unique_ptr<ProofVerifyDetails> verify_details_;
132 enum ssl_verify_result_t verify_result_ = ssl_verify_retry;
vasilvvc48c8712019-03-11 13:38:16 -0700133 std::string cert_verify_error_details_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500134
135 bool encryption_established_ = false;
fayang685367a2020-01-14 10:40:15 -0800136 bool one_rtt_keys_available_ = false;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500137 QuicReferenceCountedPointer<QuicCryptoNegotiatedParameters>
138 crypto_negotiated_params_;
nharper6ebe83b2019-06-13 17:43:52 -0700139
vasilvv4724c9c2019-08-29 11:52:11 -0700140 bool allow_empty_alpn_for_tests_ = false;
141
nharper6ebe83b2019-06-13 17:43:52 -0700142 TlsClientConnection tls_connection_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500143};
144
QUICHE teama6ef0a62019-03-07 20:34:33 -0500145} // namespace quic
146
147#endif // QUICHE_QUIC_CORE_TLS_CLIENT_HANDSHAKER_H_