blob: 15a4f4f9a3a1e8b11c3b7ccc47cf56962a3da9d7 [file] [log] [blame]
nharper6ebe83b2019-06-13 17:43:52 -07001// Copyright (c) 2019 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_CRYPTO_TLS_CONNECTION_H_
6#define QUICHE_QUIC_CORE_CRYPTO_TLS_CONNECTION_H_
7
8#include <vector>
9
10#include "third_party/boringssl/src/include/openssl/ssl.h"
11#include "net/third_party/quiche/src/quic/core/quic_types.h"
dmcardle904ef182019-12-13 08:34:33 -080012#include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h"
nharper6ebe83b2019-06-13 17:43:52 -070013
14namespace quic {
15
16// TlsConnection wraps BoringSSL's SSL object which represents a single TLS
17// connection. Callbacks set in BoringSSL which are called with an SSL* argument
18// will get dispatched to the TlsConnection object owning that SSL. In turn, the
19// TlsConnection will delegate the implementation of that callback to its
20// Delegate.
21//
22// The owner of the TlsConnection is responsible for driving the TLS handshake
23// (and other interactions with the SSL*). This class only handles mapping
24// callbacks to the correct instance.
renjietang96c17672019-06-14 12:50:14 -070025class QUIC_EXPORT_PRIVATE TlsConnection {
nharper6ebe83b2019-06-13 17:43:52 -070026 public:
27 // A TlsConnection::Delegate implements the methods that are set as callbacks
28 // of TlsConnection.
dschinazif25169a2019-10-23 08:12:18 -070029 class QUIC_EXPORT_PRIVATE Delegate {
nharper6ebe83b2019-06-13 17:43:52 -070030 public:
31 virtual ~Delegate() {}
32
33 protected:
nharperd08b82c2020-02-26 12:10:25 -080034 // SetWriteSecret provides the encryption secret used to encrypt messages at
35 // encryption level |level|. The secret provided here is the one from the
36 // TLS 1.3 key schedule (RFC 8446 section 7.1), in particular the handshake
37 // traffic secrets and application traffic secrets. The provided write
38 // secret must be used with the provided cipher suite |cipher|.
39 virtual void SetWriteSecret(EncryptionLevel level,
40 const SSL_CIPHER* cipher,
41 const std::vector<uint8_t>& write_secret) = 0;
42
43 // SetReadSecret is similar to SetWriteSecret, except that it is used for
44 // decrypting messages. SetReadSecret at a particular level is always called
45 // after SetWriteSecret for that level, except for ENCRYPTION_ZERO_RTT,
46 // where the EncryptionLevel for SetWriteSecret is
47 // ENCRYPTION_FORWARD_SECURE.
48 virtual bool SetReadSecret(EncryptionLevel level,
49 const SSL_CIPHER* cipher,
50 const std::vector<uint8_t>& read_secret) = 0;
nharper6ebe83b2019-06-13 17:43:52 -070051
52 // WriteMessage is called when there is |data| from the TLS stack ready for
53 // the QUIC stack to write in a crypto frame. The data must be transmitted
54 // at encryption level |level|.
dmcardle904ef182019-12-13 08:34:33 -080055 virtual void WriteMessage(EncryptionLevel level,
56 quiche::QuicheStringPiece data) = 0;
nharper6ebe83b2019-06-13 17:43:52 -070057
58 // FlushFlight is called to signal that the current flight of messages have
59 // all been written (via calls to WriteMessage) and can be flushed to the
60 // underlying transport.
61 virtual void FlushFlight() = 0;
62
63 // SendAlert causes this TlsConnection to close the QUIC connection with an
64 // error code corersponding to the TLS alert description |desc| sent at
65 // level |level|.
66 virtual void SendAlert(EncryptionLevel level, uint8_t desc) = 0;
67
68 friend class TlsConnection;
69 };
70
71 TlsConnection(const TlsConnection&) = delete;
72 TlsConnection& operator=(const TlsConnection&) = delete;
73
74 // Functions to convert between BoringSSL's enum ssl_encryption_level_t and
75 // QUIC's EncryptionLevel.
76 static EncryptionLevel QuicEncryptionLevel(enum ssl_encryption_level_t level);
77 static enum ssl_encryption_level_t BoringEncryptionLevel(
78 EncryptionLevel level);
79
nharper486a8a92019-08-28 16:25:10 -070080 SSL* ssl() const { return ssl_.get(); }
nharper6ebe83b2019-06-13 17:43:52 -070081
82 protected:
83 // TlsConnection does not take ownership of any of its arguments; they must
84 // outlive the TlsConnection object.
85 TlsConnection(SSL_CTX* ssl_ctx, Delegate* delegate);
86
87 // Creates an SSL_CTX and configures it with the options that are appropriate
88 // for both client and server. The caller is responsible for ownership of the
89 // newly created struct.
90 static bssl::UniquePtr<SSL_CTX> CreateSslCtx();
91
92 // From a given SSL* |ssl|, returns a pointer to the TlsConnection that it
93 // belongs to. This helper method allows the callbacks set in BoringSSL to be
94 // dispatched to the correct TlsConnection from the SSL* passed into the
95 // callback.
96 static TlsConnection* ConnectionFromSsl(const SSL* ssl);
97
98 private:
99 // TlsConnection implements SSL_QUIC_METHOD, which provides the interface
100 // between BoringSSL's TLS stack and a QUIC implementation.
101 static const SSL_QUIC_METHOD kSslQuicMethod;
102
103 // The following static functions make up the members of kSslQuicMethod:
nharper74d96ae2020-02-27 14:11:52 -0800104 static int SetReadSecretCallback(SSL* ssl,
105 enum ssl_encryption_level_t level,
106 const SSL_CIPHER* cipher,
107 const uint8_t* secret,
108 size_t secret_len);
109 static int SetWriteSecretCallback(SSL* ssl,
110 enum ssl_encryption_level_t level,
111 const SSL_CIPHER* cipher,
112 const uint8_t* secret,
113 size_t secret_len);
nharper6ebe83b2019-06-13 17:43:52 -0700114 static int WriteMessageCallback(SSL* ssl,
115 enum ssl_encryption_level_t level,
116 const uint8_t* data,
117 size_t len);
118 static int FlushFlightCallback(SSL* ssl);
119 static int SendAlertCallback(SSL* ssl,
120 enum ssl_encryption_level_t level,
121 uint8_t desc);
122
123 Delegate* delegate_;
124 bssl::UniquePtr<SSL> ssl_;
125};
126
127} // namespace quic
128
129#endif // QUICHE_QUIC_CORE_CRYPTO_TLS_CONNECTION_H_