Botan 3.6.1
Crypto and TLS for C&
tls_channel.h
Go to the documentation of this file.
1/*
2* TLS Channel
3* (C) 2011,2012,2014,2015 Jack Lloyd
4* 2016 Matthias Gierlings
5* 2021 Elektrobit Automotive GmbH
6* 2022 René Meusel, Hannes Rantzsch - neXenio GmbH
7*
8* Botan is released under the Simplified BSD License (see license.txt)
9*/
10
11#ifndef BOTAN_TLS_CHANNEL_H_
12#define BOTAN_TLS_CHANNEL_H_
13
14#include <botan/tls_alert.h>
15#include <botan/tls_callbacks.h>
16#include <botan/tls_session.h>
17#include <botan/tls_session_manager.h>
18#include <botan/x509cert.h>
19
20#include <span>
21#include <string>
22#include <string_view>
23#include <vector>
24
25namespace Botan::TLS {
26
27/**
28* Generic interface for TLS endpoint
29*/
31 public:
32 static constexpr size_t IO_BUF_DEFAULT_SIZE = 10 * 1024;
33
34 virtual ~Channel() = default;
35
36 protected:
37 virtual size_t from_peer(std::span<const uint8_t> data) = 0;
38 virtual void to_peer(std::span<const uint8_t> data) = 0;
39
40 public:
41 /**
42 * Inject TLS traffic received from counterparty
43 * @return a hint as to how many more bytes we need to process the
44 * current record (this may be 0 if on a record boundary)
45 */
46 size_t received_data(std::span<const uint8_t> data) { return this->from_peer(data); }
47
48 size_t received_data(const uint8_t buf[], size_t buf_size) { return this->from_peer(std::span(buf, buf_size)); }
49
50 /**
51 * Inject plaintext intended for counterparty
52 * Throws an exception if is_active() is false
53 */
54 void send(std::span<const uint8_t> data) { this->to_peer(data); }
55
56 void send(const uint8_t buf[], size_t buf_size) { this->to_peer(std::span(buf, buf_size)); }
57
58 /**
59 * Inject plaintext intended for counterparty
60 * Throws an exception if is_active() is false
61 */
62 void send(std::string_view val) { this->send(std::span(cast_char_ptr_to_uint8(val.data()), val.size())); }
63
64 /**
65 * Inject plaintext intended for counterparty
66 * Throws an exception if is_active() is false
67 */
68
69 /**
70 * Send a TLS alert message. If the alert is fatal, the internal
71 * state (keys, etc) will be reset.
72 * @param alert the Alert to send
73 */
74 virtual void send_alert(const Alert& alert) = 0;
75
76 /**
77 * Send a warning alert
78 */
79 virtual void send_warning_alert(Alert::Type type) = 0;
80
81 /**
82 * Send a fatal alert
83 */
84 virtual void send_fatal_alert(Alert::Type type) = 0;
85
86 /**
87 * Send a close notification alert
88 */
89 virtual void close() = 0;
90
91 /**
92 * Becomes true as soon as the TLS handshake is fully complete and all
93 * security assurances TLS provides can be guaranteed.
94 *
95 * @returns true once the TLS handshake has finished successfully
96 */
97 virtual bool is_handshake_complete() const = 0;
98
99 /**
100 * Check whether the connection is ready to send application data. Note
101 * that a TLS 1.3 server MAY send data _before_ receiving the client's
102 * Finished message. Only _after_ receiving the client's Finished, can the
103 * server be sure about the client's liveness and (optional) identity.
104 *
105 * Consider using is_handshake_complete() if you need to wait until the
106 * handshake if fully complete.
107 *
108 * @return true iff the connection is active for sending application data
109 */
110 virtual bool is_active() const = 0;
111
112 /**
113 * Note: For TLS 1.3 a connection is closed only after both peers have
114 * signaled a "close_notify". While TLS 1.2 automatically responded
115 * in suit once the peer had sent "close_notify", TLS 1.3 allows to
116 * continue transmitting data even if the peer closed their writing
117 * end.
118 *
119 * @return true iff the connection has been definitely closed
120 */
121 virtual bool is_closed() const = 0;
122
123 /**
124 * @return true iff the peer closed their channel
125 * (i.e. no more incoming data expected)
126 */
127 virtual bool is_closed_for_reading() const = 0;
128
129 /**
130 * @return true iff we closed our channel
131 * (i.e. no more outgoing data allowed)
132 */
133 virtual bool is_closed_for_writing() const = 0;
134
135 /**
136 * @return certificate chain of the peer (may be empty)
137 */
138 virtual std::vector<X509_Certificate> peer_cert_chain() const = 0;
139
140 /**
141 * @return raw public key of the peer (may be nullptr)
142 */
143 virtual std::shared_ptr<const Public_Key> peer_raw_public_key() const = 0;
144
145 /**
146 * @return identity of the PSK used for this connection
147 * or std::nullopt if no PSK was used.
148 */
149 virtual std::optional<std::string> external_psk_identity() const = 0;
150
151 /**
152 * Key material export (RFC 5705)
153 * @param label a disambiguating label string
154 * @param context a per-association context value
155 * @param length the length of the desired key in bytes
156 * @return key of length bytes
157 */
158 virtual SymmetricKey key_material_export(std::string_view label,
159 std::string_view context,
160 size_t length) const = 0;
161
162 /**
163 * Attempt to renegotiate the session
164 * @param force_full_renegotiation if true, require a full renegotiation,
165 * otherwise allow session resumption
166 */
167 virtual void renegotiate(bool force_full_renegotiation = false) = 0;
168
169 /**
170 * Attempt to update the session's traffic key material
171 * Note that this is possible with a TLS 1.3 channel, only.
172 *
173 * @param request_peer_update if true, require a reciprocal key update
174 */
175 virtual void update_traffic_keys(bool request_peer_update = false) = 0;
176
177 /**
178 * @return true iff the counterparty supports the secure
179 * renegotiation extensions.
180 */
181 virtual bool secure_renegotiation_supported() const = 0;
182
183 /**
184 * Perform a handshake timeout check.
185 *
186 * This function does nothing unless the channel represents a DTLS
187 * connection and a handshake is actively in progress. In this case it will
188 * check the current timeout state and potentially initiate retransmission
189 * of handshake packets.
190 *
191 * @returns true if a timeout condition occurred
192 */
193 virtual bool timeout_check() = 0;
194
195 virtual std::string application_protocol() const = 0;
196};
197} // namespace Botan::TLS
198
199#endif
virtual size_t from_peer(std::span< const uint8_t > data)=0
virtual bool secure_renegotiation_supported() const =0
virtual void update_traffic_keys(bool request_peer_update=false)=0
virtual void send_fatal_alert(Alert::Type type)=0
virtual ~Channel()=default
virtual bool timeout_check()=0
virtual bool is_handshake_complete() const =0
size_t received_data(std::span< const uint8_t > data)
Definition tls_channel.h:46
virtual SymmetricKey key_material_export(std::string_view label, std::string_view context, size_t length) const =0
void send(const uint8_t buf[], size_t buf_size)
Definition tls_channel.h:56
virtual std::string application_protocol() const =0
virtual std::vector< X509_Certificate > peer_cert_chain() const =0
virtual bool is_closed_for_writing() const =0
virtual bool is_closed_for_reading() const =0
virtual std::shared_ptr< const Public_Key > peer_raw_public_key() const =0
virtual void send_warning_alert(Alert::Type type)=0
virtual bool is_active() const =0
void send(std::string_view val)
Definition tls_channel.h:62
virtual void close()=0
size_t received_data(const uint8_t buf[], size_t buf_size)
Definition tls_channel.h:48
virtual void to_peer(std::span< const uint8_t > data)=0
void send(std::span< const uint8_t > data)
Definition tls_channel.h:54
virtual void send_alert(const Alert &alert)=0
virtual std::optional< std::string > external_psk_identity() const =0
virtual bool is_closed() const =0
virtual void renegotiate(bool force_full_renegotiation=false)=0
#define BOTAN_PUBLIC_API(maj, min)
Definition compiler.h:31
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition mem_ops.h:273