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_HANDSHAKER_H_ |
| 6 | #define QUICHE_QUIC_CORE_TLS_HANDSHAKER_H_ |
| 7 | |
| 8 | #include "third_party/boringssl/src/include/openssl/base.h" |
| 9 | #include "third_party/boringssl/src/include/openssl/ssl.h" |
| 10 | #include "net/third_party/quiche/src/quic/core/crypto/crypto_handshake.h" |
| 11 | #include "net/third_party/quiche/src/quic/core/crypto/crypto_message_parser.h" |
| 12 | #include "net/third_party/quiche/src/quic/core/crypto/quic_decrypter.h" |
| 13 | #include "net/third_party/quiche/src/quic/core/crypto/quic_encrypter.h" |
nharper | 6ebe83b | 2019-06-13 17:43:52 -0700 | [diff] [blame] | 14 | #include "net/third_party/quiche/src/quic/core/crypto/tls_connection.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 15 | #include "net/third_party/quiche/src/quic/core/quic_session.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 | |
| 21 | class QuicCryptoStream; |
| 22 | |
| 23 | // Base class for TlsClientHandshaker and TlsServerHandshaker. TlsHandshaker |
| 24 | // provides functionality common to both the client and server, such as moving |
| 25 | // messages between the TLS stack and the QUIC crypto stream, and handling |
| 26 | // derivation of secrets. |
nharper | 6ebe83b | 2019-06-13 17:43:52 -0700 | [diff] [blame] | 27 | class QUIC_EXPORT_PRIVATE TlsHandshaker : public TlsConnection::Delegate, |
| 28 | public CryptoMessageParser { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 29 | public: |
| 30 | // TlsHandshaker does not take ownership of any of its arguments; they must |
| 31 | // outlive the TlsHandshaker. |
nharper | 80d3b17 | 2020-01-17 11:43:39 -0800 | [diff] [blame] | 32 | TlsHandshaker(QuicCryptoStream* stream, QuicSession* session); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 33 | TlsHandshaker(const TlsHandshaker&) = delete; |
| 34 | TlsHandshaker& operator=(const TlsHandshaker&) = delete; |
| 35 | |
| 36 | ~TlsHandshaker() override; |
| 37 | |
| 38 | // From CryptoMessageParser |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 39 | bool ProcessInput(quiche::QuicheStringPiece input, |
| 40 | EncryptionLevel level) override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 41 | size_t InputBytesRemaining() const override { return 0; } |
| 42 | QuicErrorCode error() const override { return parser_error_; } |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 43 | const std::string& error_detail() const override { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 44 | return parser_error_detail_; |
| 45 | } |
| 46 | |
| 47 | // From QuicCryptoStream |
| 48 | virtual bool encryption_established() const = 0; |
fayang | 685367a | 2020-01-14 10:40:15 -0800 | [diff] [blame] | 49 | virtual bool one_rtt_keys_available() const = 0; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 50 | virtual const QuicCryptoNegotiatedParameters& crypto_negotiated_params() |
| 51 | const = 0; |
| 52 | virtual CryptoMessageParser* crypto_message_parser() { return this; } |
fayang | 9a863cf | 2020-01-16 14:12:11 -0800 | [diff] [blame] | 53 | virtual HandshakeState GetHandshakeState() const = 0; |
nharper | 486a8a9 | 2019-08-28 16:25:10 -0700 | [diff] [blame] | 54 | size_t BufferSizeLimitForLevel(EncryptionLevel level) const; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 55 | |
| 56 | protected: |
| 57 | virtual void AdvanceHandshake() = 0; |
| 58 | |
| 59 | virtual void CloseConnection(QuicErrorCode error, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 60 | const std::string& reason_phrase) = 0; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 61 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 62 | // Returns the PRF used by the cipher suite negotiated in the TLS handshake. |
| 63 | const EVP_MD* Prf(); |
| 64 | |
nharper | 486a8a9 | 2019-08-28 16:25:10 -0700 | [diff] [blame] | 65 | virtual const TlsConnection* tls_connection() const = 0; |
nharper | 6ebe83b | 2019-06-13 17:43:52 -0700 | [diff] [blame] | 66 | |
nharper | 486a8a9 | 2019-08-28 16:25:10 -0700 | [diff] [blame] | 67 | SSL* ssl() const { return tls_connection()->ssl(); } |
nharper | 6ebe83b | 2019-06-13 17:43:52 -0700 | [diff] [blame] | 68 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 69 | QuicCryptoStream* stream() { return stream_; } |
renjietang | bd33b62 | 2020-02-12 16:52:30 -0800 | [diff] [blame] | 70 | HandshakerDelegateInterface* handshaker_delegate() { |
| 71 | return handshaker_delegate_; |
| 72 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 73 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 74 | // SetEncryptionSecret provides the encryption secret to use at a particular |
| 75 | // encryption level. The secrets provided here are the ones from the TLS 1.3 |
| 76 | // key schedule (RFC 8446 section 7.1), in particular the handshake traffic |
| 77 | // secrets and application traffic secrets. For a given secret |secret|, |
| 78 | // |level| indicates which EncryptionLevel it is to be used at, and |is_write| |
| 79 | // indicates whether it is used for encryption or decryption. |
fayang | d286652 | 2020-02-12 11:15:27 -0800 | [diff] [blame] | 80 | // Returns true if secret is sucessfully set, otherwise, returns false. |
| 81 | bool SetEncryptionSecret(EncryptionLevel level, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 82 | const std::vector<uint8_t>& read_secret, |
nharper | 6ebe83b | 2019-06-13 17:43:52 -0700 | [diff] [blame] | 83 | const std::vector<uint8_t>& write_secret) override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 84 | |
| 85 | // WriteMessage is called when there is |data| from the TLS stack ready for |
| 86 | // the QUIC stack to write in a crypto frame. The data must be transmitted at |
| 87 | // encryption level |level|. |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 88 | void WriteMessage(EncryptionLevel level, |
| 89 | quiche::QuicheStringPiece data) override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 90 | |
| 91 | // FlushFlight is called to signal that the current flight of |
| 92 | // messages have all been written (via calls to WriteMessage) and can be |
| 93 | // flushed to the underlying transport. |
nharper | 6ebe83b | 2019-06-13 17:43:52 -0700 | [diff] [blame] | 94 | void FlushFlight() override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 95 | |
| 96 | // SendAlert causes this TlsHandshaker to close the QUIC connection with an |
| 97 | // error code corresponding to the TLS alert description |desc|. |
nharper | 6ebe83b | 2019-06-13 17:43:52 -0700 | [diff] [blame] | 98 | void SendAlert(EncryptionLevel level, uint8_t desc) override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 99 | |
nharper | 6ebe83b | 2019-06-13 17:43:52 -0700 | [diff] [blame] | 100 | private: |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 101 | QuicCryptoStream* stream_; |
renjietang | bd33b62 | 2020-02-12 16:52:30 -0800 | [diff] [blame] | 102 | HandshakerDelegateInterface* handshaker_delegate_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 103 | |
| 104 | QuicErrorCode parser_error_ = QUIC_NO_ERROR; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 105 | std::string parser_error_detail_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | } // namespace quic |
| 109 | |
| 110 | #endif // QUICHE_QUIC_CORE_TLS_HANDSHAKER_H_ |