blob: 3d5f1e2d26a652148ed5697a894c0913c712b8ed [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_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"
nharper6ebe83b2019-06-13 17:43:52 -070014#include "net/third_party/quiche/src/quic/core/crypto/tls_connection.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050015#include "net/third_party/quiche/src/quic/core/quic_session.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
21class 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.
nharper6ebe83b2019-06-13 17:43:52 -070027class QUIC_EXPORT_PRIVATE TlsHandshaker : public TlsConnection::Delegate,
28 public CryptoMessageParser {
QUICHE teama6ef0a62019-03-07 20:34:33 -050029 public:
30 // TlsHandshaker does not take ownership of any of its arguments; they must
31 // outlive the TlsHandshaker.
nharper80d3b172020-01-17 11:43:39 -080032 TlsHandshaker(QuicCryptoStream* stream, QuicSession* session);
QUICHE teama6ef0a62019-03-07 20:34:33 -050033 TlsHandshaker(const TlsHandshaker&) = delete;
34 TlsHandshaker& operator=(const TlsHandshaker&) = delete;
35
36 ~TlsHandshaker() override;
37
38 // From CryptoMessageParser
dmcardlecf0bfcf2019-12-13 08:08:21 -080039 bool ProcessInput(quiche::QuicheStringPiece input,
40 EncryptionLevel level) override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050041 size_t InputBytesRemaining() const override { return 0; }
42 QuicErrorCode error() const override { return parser_error_; }
vasilvvc48c8712019-03-11 13:38:16 -070043 const std::string& error_detail() const override {
QUICHE teama6ef0a62019-03-07 20:34:33 -050044 return parser_error_detail_;
45 }
46
47 // From QuicCryptoStream
48 virtual bool encryption_established() const = 0;
fayang685367a2020-01-14 10:40:15 -080049 virtual bool one_rtt_keys_available() const = 0;
QUICHE teama6ef0a62019-03-07 20:34:33 -050050 virtual const QuicCryptoNegotiatedParameters& crypto_negotiated_params()
51 const = 0;
52 virtual CryptoMessageParser* crypto_message_parser() { return this; }
fayang9a863cf2020-01-16 14:12:11 -080053 virtual HandshakeState GetHandshakeState() const = 0;
nharper486a8a92019-08-28 16:25:10 -070054 size_t BufferSizeLimitForLevel(EncryptionLevel level) const;
QUICHE teama6ef0a62019-03-07 20:34:33 -050055
56 protected:
57 virtual void AdvanceHandshake() = 0;
58
59 virtual void CloseConnection(QuicErrorCode error,
vasilvvc48c8712019-03-11 13:38:16 -070060 const std::string& reason_phrase) = 0;
QUICHE teama6ef0a62019-03-07 20:34:33 -050061
QUICHE teama6ef0a62019-03-07 20:34:33 -050062 // Returns the PRF used by the cipher suite negotiated in the TLS handshake.
nharper8d4ff5c2020-02-27 11:43:44 -080063 const EVP_MD* Prf(const SSL_CIPHER* cipher);
QUICHE teama6ef0a62019-03-07 20:34:33 -050064
nharper486a8a92019-08-28 16:25:10 -070065 virtual const TlsConnection* tls_connection() const = 0;
nharper6ebe83b2019-06-13 17:43:52 -070066
nharper486a8a92019-08-28 16:25:10 -070067 SSL* ssl() const { return tls_connection()->ssl(); }
nharper6ebe83b2019-06-13 17:43:52 -070068
QUICHE teama6ef0a62019-03-07 20:34:33 -050069 QuicCryptoStream* stream() { return stream_; }
renjietangbd33b622020-02-12 16:52:30 -080070 HandshakerDelegateInterface* handshaker_delegate() {
71 return handshaker_delegate_;
72 }
QUICHE teama6ef0a62019-03-07 20:34:33 -050073
nharperd08b82c2020-02-26 12:10:25 -080074 // SetWriteSecret provides the encryption secret used to encrypt messages at
75 // encryption level |level|. The secret provided here is the one from the TLS
76 // 1.3 key schedule (RFC 8446 section 7.1), in particular the handshake
77 // traffic secrets and application traffic secrets. The provided write secret
78 // must be used with the provided cipher suite |cipher|.
79 void SetWriteSecret(EncryptionLevel level,
80 const SSL_CIPHER* cipher,
81 const std::vector<uint8_t>& write_secret) override;
82
83 // SetReadSecret is similar to SetWriteSecret, except that it is used for
84 // decrypting messages. SetReadSecret at a particular level is always called
85 // after SetWriteSecret for that level, except for ENCRYPTION_ZERO_RTT, where
86 // the EncryptionLevel for SetWriteSecret is ENCRYPTION_FORWARD_SECURE.
87 bool SetReadSecret(EncryptionLevel level,
88 const SSL_CIPHER* cipher,
89 const std::vector<uint8_t>& read_secret) override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050090
91 // WriteMessage is called when there is |data| from the TLS stack ready for
92 // the QUIC stack to write in a crypto frame. The data must be transmitted at
93 // encryption level |level|.
dmcardlecf0bfcf2019-12-13 08:08:21 -080094 void WriteMessage(EncryptionLevel level,
95 quiche::QuicheStringPiece data) override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050096
97 // FlushFlight is called to signal that the current flight of
98 // messages have all been written (via calls to WriteMessage) and can be
99 // flushed to the underlying transport.
nharper6ebe83b2019-06-13 17:43:52 -0700100 void FlushFlight() override;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500101
102 // SendAlert causes this TlsHandshaker to close the QUIC connection with an
103 // error code corresponding to the TLS alert description |desc|.
nharper6ebe83b2019-06-13 17:43:52 -0700104 void SendAlert(EncryptionLevel level, uint8_t desc) override;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500105
nharper6ebe83b2019-06-13 17:43:52 -0700106 private:
QUICHE teama6ef0a62019-03-07 20:34:33 -0500107 QuicCryptoStream* stream_;
renjietangbd33b622020-02-12 16:52:30 -0800108 HandshakerDelegateInterface* handshaker_delegate_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500109
110 QuicErrorCode parser_error_ = QUIC_NO_ERROR;
vasilvvc48c8712019-03-11 13:38:16 -0700111 std::string parser_error_detail_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500112};
113
114} // namespace quic
115
116#endif // QUICHE_QUIC_CORE_TLS_HANDSHAKER_H_