QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // 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 | |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 10 | #include "third_party/boringssl/src/include/openssl/ssl.h" |
| 11 | #include "net/third_party/quiche/src/quic/core/crypto/proof_verifier.h" |
nharper | 6ebe83b | 2019-06-13 17:43:52 -0700 | [diff] [blame] | 12 | #include "net/third_party/quiche/src/quic/core/crypto/tls_client_connection.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 13 | #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" |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 17 | #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 18 | |
| 19 | namespace quic { |
| 20 | |
rch | 85240a1 | 2019-12-23 11:51:59 -0800 | [diff] [blame] | 21 | // An implementation of QuicCryptoClientStream::HandshakerInterface which uses |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 22 | // TLS 1.3 for the crypto handshake protocol. |
| 23 | class QUIC_EXPORT_PRIVATE TlsClientHandshaker |
nharper | 6ebe83b | 2019-06-13 17:43:52 -0700 | [diff] [blame] | 24 | : public TlsHandshaker, |
rch | 85240a1 | 2019-12-23 11:51:59 -0800 | [diff] [blame] | 25 | public QuicCryptoClientStream::HandshakerInterface, |
nharper | 6ebe83b | 2019-06-13 17:43:52 -0700 | [diff] [blame] | 26 | public TlsClientConnection::Delegate { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 27 | public: |
nharper | df7a77b | 2019-11-11 13:12:45 -0800 | [diff] [blame] | 28 | TlsClientHandshaker(const QuicServerId& server_id, |
| 29 | QuicCryptoStream* stream, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 30 | QuicSession* session, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 31 | std::unique_ptr<ProofVerifyContext> verify_context, |
nharper | df7a77b | 2019-11-11 13:12:45 -0800 | [diff] [blame] | 32 | QuicCryptoClientConfig* crypto_config, |
| 33 | QuicCryptoClientStream::ProofHandler* proof_handler); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 34 | TlsClientHandshaker(const TlsClientHandshaker&) = delete; |
| 35 | TlsClientHandshaker& operator=(const TlsClientHandshaker&) = delete; |
| 36 | |
| 37 | ~TlsClientHandshaker() override; |
| 38 | |
rch | 85240a1 | 2019-12-23 11:51:59 -0800 | [diff] [blame] | 39 | // From QuicCryptoClientStream::HandshakerInterface |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 40 | bool CryptoConnect() override; |
| 41 | int num_sent_client_hellos() const override; |
nharper | 0270396 | 2019-11-07 12:23:13 -0800 | [diff] [blame] | 42 | bool IsResumption() const override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 43 | int num_scup_messages_received() const override; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 44 | std::string chlo_hash() const override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 45 | |
rch | 85240a1 | 2019-12-23 11:51:59 -0800 | [diff] [blame] | 46 | // From QuicCryptoClientStream::HandshakerInterface and TlsHandshaker |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 47 | bool encryption_established() const override; |
fayang | 685367a | 2020-01-14 10:40:15 -0800 | [diff] [blame] | 48 | bool one_rtt_keys_available() const override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 49 | const QuicCryptoNegotiatedParameters& crypto_negotiated_params() |
| 50 | const override; |
| 51 | CryptoMessageParser* crypto_message_parser() override; |
fayang | 9a863cf | 2020-01-16 14:12:11 -0800 | [diff] [blame] | 52 | HandshakeState GetHandshakeState() const override; |
nharper | 486a8a9 | 2019-08-28 16:25:10 -0700 | [diff] [blame] | 53 | size_t BufferSizeLimitForLevel(EncryptionLevel level) const override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 54 | |
fayang | d58736d | 2019-11-27 13:35:31 -0800 | [diff] [blame] | 55 | // Override to drop initial keys if trying to write ENCRYPTION_HANDSHAKE data. |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 56 | void WriteMessage(EncryptionLevel level, |
| 57 | quiche::QuicheStringPiece data) override; |
fayang | d58736d | 2019-11-27 13:35:31 -0800 | [diff] [blame] | 58 | |
vasilvv | 4724c9c | 2019-08-29 11:52:11 -0700 | [diff] [blame] | 59 | void AllowEmptyAlpnForTests() { allow_empty_alpn_for_tests_ = true; } |
| 60 | |
nharper | 6ebe83b | 2019-06-13 17:43:52 -0700 | [diff] [blame] | 61 | protected: |
nharper | 486a8a9 | 2019-08-28 16:25:10 -0700 | [diff] [blame] | 62 | const TlsConnection* tls_connection() const override { |
| 63 | return &tls_connection_; |
| 64 | } |
nharper | 6ebe83b | 2019-06-13 17:43:52 -0700 | [diff] [blame] | 65 | |
| 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 74 | private: |
| 75 | // ProofVerifierCallbackImpl handles the result of an asynchronous certificate |
| 76 | // verification operation. |
dschinazi | f25169a | 2019-10-23 08:12:18 -0700 | [diff] [blame] | 77 | class QUIC_EXPORT_PRIVATE ProofVerifierCallbackImpl |
| 78 | : public ProofVerifierCallback { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 79 | public: |
| 80 | explicit ProofVerifierCallbackImpl(TlsClientHandshaker* parent); |
| 81 | ~ProofVerifierCallbackImpl() override; |
| 82 | |
| 83 | // ProofVerifierCallback interface. |
| 84 | void Run(bool ok, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 85 | const std::string& error_details, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 86 | 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, |
fayang | d58736d | 2019-11-27 13:35:31 -0800 | [diff] [blame] | 99 | STATE_ENCRYPTION_HANDSHAKE_DATA_SENT, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 100 | STATE_HANDSHAKE_COMPLETE, |
| 101 | STATE_CONNECTION_CLOSED, |
| 102 | } state_ = STATE_IDLE; |
| 103 | |
vasilvv | 4724c9c | 2019-08-29 11:52:11 -0700 | [diff] [blame] | 104 | bool SetAlpn(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 105 | bool SetTransportParameters(); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 106 | bool ProcessTransportParameters(std::string* error_details); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 107 | void FinishHandshake(); |
| 108 | |
nharper | df7a77b | 2019-11-11 13:12:45 -0800 | [diff] [blame] | 109 | void InsertSession(bssl::UniquePtr<SSL_SESSION> session) override; |
| 110 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 111 | 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_; |
nharper | 40bdf53 | 2019-10-03 11:16:22 -0700 | [diff] [blame] | 118 | // 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 122 | |
nharper | df7a77b | 2019-11-11 13:12:45 -0800 | [diff] [blame] | 123 | // Used for session resumption. |session_cache_| is owned by the |
| 124 | // QuicCryptoClientConfig passed into TlsClientHandshaker's constructor. |
| 125 | SessionCache* session_cache_; |
| 126 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 127 | std::string user_agent_id_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 128 | |
| 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; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 134 | std::string cert_verify_error_details_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 135 | |
| 136 | bool encryption_established_ = false; |
fayang | 685367a | 2020-01-14 10:40:15 -0800 | [diff] [blame] | 137 | bool one_rtt_keys_available_ = false; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 138 | QuicReferenceCountedPointer<QuicCryptoNegotiatedParameters> |
| 139 | crypto_negotiated_params_; |
nharper | 6ebe83b | 2019-06-13 17:43:52 -0700 | [diff] [blame] | 140 | |
vasilvv | 4724c9c | 2019-08-29 11:52:11 -0700 | [diff] [blame] | 141 | bool allow_empty_alpn_for_tests_ = false; |
| 142 | |
nharper | 6ebe83b | 2019-06-13 17:43:52 -0700 | [diff] [blame] | 143 | TlsClientConnection tls_connection_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 144 | }; |
| 145 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 146 | } // namespace quic |
| 147 | |
| 148 | #endif // QUICHE_QUIC_CORE_TLS_CLIENT_HANDSHAKER_H_ |