Botan 3.0.0-alpha0
Crypto and TLS for C&
tls_callbacks.h
Go to the documentation of this file.
1/*
2* TLS Callbacks
3* (C) 2016 Matthias Gierlings
4* 2016 Jack Lloyd
5* 2017 Harry Reimann, Rohde & Schwarz Cybersecurity
6*
7* Botan is released under the Simplified BSD License (see license.txt)
8*/
9
10#ifndef BOTAN_TLS_CALLBACKS_H_
11#define BOTAN_TLS_CALLBACKS_H_
12
13#include <botan/tls_session.h>
14#include <botan/tls_alert.h>
15#include <botan/pubkey.h>
16#include <botan/ocsp.h>
17#include <optional>
18#include <chrono>
19
20namespace Botan {
21
22class Certificate_Store;
23class X509_Certificate;
24
25namespace OCSP {
26
27class Response;
28
29}
30
31namespace TLS {
32
33class Handshake_Message;
34class Policy;
35class Extensions;
36class Certificate_Status_Request;
37
38/**
39* Encapsulates the callbacks that a TLS channel will make which are due to
40* channel specific operations.
41*/
43 {
44 public:
45 virtual ~Callbacks() = default;
46
47 /**
48 * Mandatory callback: output function
49 * The channel will call this with data which needs to be sent to the peer
50 * (eg, over a socket or some other form of IPC). The array will be overwritten
51 * when the function returns so a copy must be made if the data cannot be
52 * sent immediately.
53 *
54 * @param data the vector of data to send
55 *
56 * @param size the number of bytes to send
57 */
58 virtual void tls_emit_data(const uint8_t data[], size_t size) = 0;
59
60 /**
61 * Mandatory callback: process application data
62 * Called when application data record is received from the peer.
63 * Again the array is overwritten immediately after the function returns.
64 *
65 * @param seq_no the underlying TLS/DTLS record sequence number
66 *
67 * @param data the vector containing the received record
68 *
69 * @param size the length of the received record, in bytes
70 */
71 virtual void tls_record_received(uint64_t seq_no, const uint8_t data[], size_t size) = 0;
72
73 /**
74 * Mandatory callback: alert received
75 * Called when an alert is received from the peer
76 * If fatal, the connection is closing. If not fatal, the connection may
77 * still be closing (depending on the error and the peer).
78 *
79 * @param alert the source of the alert
80 */
81 virtual void tls_alert(Alert alert) = 0;
82
83 /**
84 * Mandatory callback: session established
85 * Called when a session is established. Throw an exception to abort
86 * the connection.
87 *
88 * @param session the session descriptor
89 *
90 * @return return false to prevent the session from being cached,
91 * return true to cache the session in the configured session manager
92 */
93 virtual bool tls_session_established(const Session& session) = 0;
94
95 /**
96 * Optional callback: session activated
97 * Called when a session is active and can be written to
98 */
99 virtual void tls_session_activated() {}
100
101 /**
102 * Optional callback: New session ticket received
103 * Called when we receive a session ticket from the server at any point
104 * after the initial handshake has finished. Clients may decide to keep or
105 * discard the session ticket in the configured session manager.
106 *
107 * Note: this is called for connections that negotiated TLS 1.3 only.
108 *
109 * @param session the session descriptor
110 *
111 * @return false to prevent the session from being cached, and true to
112 * cache the session in the configured session manager
113 */
114 virtual bool tls_session_ticket_received(const Session& session);
115
116 /**
117 * Optional callback with default impl: verify cert chain
118 *
119 * Default implementation performs a standard PKIX validation
120 * and initiates network OCSP request for end-entity cert.
121 * Override to provide different behavior.
122 *
123 * Check the certificate chain is valid up to a trusted root, and
124 * optionally (if hostname != "") that the hostname given is
125 * consistent with the leaf certificate.
126 *
127 * This function should throw an exception derived from
128 * std::exception with an informative what() result if the
129 * certificate chain cannot be verified.
130 *
131 * @param cert_chain specifies a certificate chain leading to a
132 * trusted root CA certificate.
133 * @param ocsp_responses the server may have provided some
134 * @param trusted_roots the list of trusted certificates
135 * @param usage what this cert chain is being used for
136 * Usage_Type::TLS_SERVER_AUTH for server chains,
137 * Usage_Type::TLS_CLIENT_AUTH for client chains,
138 * Usage_Type::UNSPECIFIED for other uses
139 * @param hostname when authenticating a server, this is the hostname
140 * the client requested (eg via SNI). When authenticating a client,
141 * this is the server name the client is authenticating *to*.
142 * Empty in other cases or if no hostname was used.
143 * @param policy the TLS policy associated with the session being authenticated
144 * using the certificate chain
145 */
146 virtual void tls_verify_cert_chain(
147 const std::vector<X509_Certificate>& cert_chain,
148 const std::vector<std::optional<OCSP::Response>>& ocsp_responses,
149 const std::vector<Certificate_Store*>& trusted_roots,
150 Usage_Type usage,
151 const std::string& hostname,
152 const TLS::Policy& policy);
153
154 /**
155 * Called by default `tls_verify_cert_chain` to get the timeout to use for OCSP
156 * requests. Return 0 to disable online OCSP checks.
157 *
158 * This function should not be "const" since the implementation might need
159 * to perform some side effecting operation to compute the result.
160 */
161 virtual std::chrono::milliseconds tls_verify_cert_chain_ocsp_timeout() const
162 {
163 return std::chrono::milliseconds(0);
164 }
165
166 /**
167 * Called by the TLS server whenever the client included the
168 * status_request extension (see RFC 6066, a.k.a OCSP stapling)
169 * in the ClientHello.
170 *
171 * @return the encoded OCSP response to be sent to the client which
172 * indicates the revocation status of the server certificate. Return an
173 * empty vector to indicate that no response is available, and thus
174 * suppress the Certificate_Status message.
175 */
176 virtual std::vector<uint8_t> tls_provide_cert_status(const std::vector<X509_Certificate>& chain,
178 {
179 BOTAN_UNUSED(chain);
180 BOTAN_UNUSED(csr);
181 return std::vector<uint8_t>();
182 }
183
184 /**
185 * Optional callback with default impl: sign a message
186 *
187 * Default implementation uses PK_Signer::sign_message().
188 * Override to provide a different approach, e.g. using an external device.
189 *
190 * @param key the private key of the signer
191 * @param rng a random number generator
192 * @param emsa the encoding method to be applied to the message
193 * @param format the signature format
194 * @param msg the input data for the signature
195 *
196 * @return the signature
197 */
198 virtual std::vector<uint8_t> tls_sign_message(
199 const Private_Key& key,
201 const std::string& emsa,
202 Signature_Format format,
203 const std::vector<uint8_t>& msg);
204
205 /**
206 * Optional callback with default impl: verify a message signature
207 *
208 * Default implementation uses PK_Verifier::verify_message().
209 * Override to provide a different approach, e.g. using an external device.
210 *
211 * @param key the public key of the signer
212 * @param emsa the encoding method to be applied to the message
213 * @param format the signature format
214 * @param msg the input data for the signature
215 * @param sig the signature to be checked
216 *
217 * @return true if the signature is valid, false otherwise
218 */
219 virtual bool tls_verify_message(
220 const Public_Key& key,
221 const std::string& emsa,
222 Signature_Format format,
223 const std::vector<uint8_t>& msg,
224 const std::vector<uint8_t>& sig);
225
226 /**
227 * Optional callback with default impl: client side DH agreement
228 *
229 * Default implementation uses PK_Key_Agreement::derive_key().
230 * Override to provide a different approach, e.g. using an external device.
231 *
232 * @param modulus the modulus p of the discrete logarithm group
233 * @param generator the generator of the DH subgroup
234 * @param peer_public_value the public value of the peer
235 * @param policy the TLS policy associated with the session being established
236 * @param rng a random number generator
237 *
238 * @return a pair consisting of the agreed raw secret and our public value
239 *
240 * TODO: Currently, this is called in TLS 1.2 only. The key agreement mechanics
241 * changed in TLS 1.3, so this callback would (at least) need to be aware
242 * of the negotiated protocol version.
243 * Suggestion: Lets think about a more generic interface for this and
244 * deprecate/remove this callback in Botan 3.0
245 */
246 virtual std::pair<secure_vector<uint8_t>, std::vector<uint8_t>> tls_dh_agree(
247 const std::vector<uint8_t>& modulus,
248 const std::vector<uint8_t>& generator,
249 const std::vector<uint8_t>& peer_public_value,
250 const Policy& policy,
252
253 /**
254 * Optional callback with default impl: client side ECDH agreement
255 *
256 * Default implementation uses PK_Key_Agreement::derive_key().
257 * Override to provide a different approach, e.g. using an external device.
258 *
259 * @param curve_name the name of the elliptic curve
260 * @param peer_public_value the public value of the peer
261 * @param policy the TLS policy associated with the session being established
262 * @param rng a random number generator
263 * @param compressed the compression preference for our public value
264 *
265 * @return a pair consisting of the agreed raw secret and our public value
266 *
267 * TODO: Currently, this is called in TLS 1.2 only. The key agreement mechanics
268 * changed in TLS 1.3, so this callback would (at least) need to be aware
269 * of the negotiated protocol version.
270 * Suggestion: Lets think about a more generic interface for this and
271 * deprecate/remove this callback in Botan 3.0
272 */
273 virtual std::pair<secure_vector<uint8_t>, std::vector<uint8_t>> tls_ecdh_agree(
274 const std::string& curve_name,
275 const std::vector<uint8_t>& peer_public_value,
276 const Policy& policy,
278 bool compressed);
279
280 /**
281 * Optional callback: inspect handshake message
282 * Throw an exception to abort the handshake.
283 * Default simply ignores the message.
284 *
285 * @param message the handshake message
286 */
287 virtual void tls_inspect_handshake_msg(const Handshake_Message& message);
288
289 /**
290 * Optional callback for server: choose ALPN protocol
291 *
292 * ALPN (RFC 7301) works by the client sending a list of application
293 * protocols it is willing to negotiate. The server then selects which
294 * protocol to use. RFC 7301 requires that if the server does not support
295 * any protocols offered by the client, then it should close the connection
296 * with an alert of no_application_protocol. Within this callback this would
297 * be done by throwing a TLS_Exception(Alert::NO_APPLICATION_PROTOCOL)
298 *
299 * @param client_protos the vector of protocols the client is willing to negotiate
300 *
301 * @return the protocol selected by the server; if the empty string is
302 * returned, the server does not reply to the client ALPN extension.
303 *
304 * The default implementation returns the empty string, causing client
305 * ALPN to be ignored.
306 *
307 * It is highly recommended to support ALPN whenever possible to avoid
308 * cross-protocol attacks.
309 */
310 virtual std::string tls_server_choose_app_protocol(const std::vector<std::string>& client_protos);
311
312 /**
313 * Optional callback: examine/modify Extensions before sending.
314 *
315 * Both client and server will call this callback on the Extensions object
316 * before serializing it in the client/server hellos. This allows an
317 * application to modify which extensions are sent during the
318 * handshake.
319 *
320 * Default implementation does nothing.
321 *
322 * @param extn the extensions
323 * @param which_side will be CLIENT or SERVER which is the current
324 * applications role in the exchange.
325 */
326 virtual void tls_modify_extensions(Extensions& extn, Connection_Side which_side);
327
328 /**
329 * Optional callback: examine peer extensions.
330 *
331 * Both client and server will call this callback with the Extensions
332 * object after receiving it from the peer. This allows examining the
333 * Extensions, for example to implement a custom extension. It also allows
334 * an application to require that a particular extension be implemented;
335 * throw an exception from this function to abort the handshake.
336 *
337 * Default implementation does nothing.
338 *
339 * @param extn the extensions
340 * @param which_side will be CLIENT if these are are the clients extensions (ie we are
341 * the server) or SERVER if these are the server extensions (we are the client).
342 */
343 virtual void tls_examine_extensions(const Extensions& extn, Connection_Side which_side);
344
345 /**
346 * Optional callback: decode TLS group ID
347 *
348 * TLS uses a 16-bit field to identify ECC and DH groups. This callback
349 * handles the decoding. You only need to implement this if you are using
350 * a custom ECC or DH group (this is extremely uncommon).
351 *
352 * Default implementation uses the standard (IETF-defined) mappings.
353 *
354 * TODO: reconsider this callback together with `tls_dh_agree` and `tls_ecdh_agree`.
355 */
356 virtual std::string tls_decode_group_param(Group_Params group_param);
357
358 /**
359 * Optional callback: parse a single OCSP Response
360 *
361 * Note: Typically a user of the library would not want to override this
362 * callback. We provide this callback to be able to support OCSP
363 * related tests from BoringSSL's BoGo tests that provide unparsable
364 * responses.
365 *
366 * Default implementation tries to parse the provided raw OCSP response.
367 *
368 * This function should not throw an exception but return a std::nullopt
369 * if the OCSP response cannot be parsed.
370 *
371 * @param raw_response raw OCSP response buffer
372 * @returns the parsed OCSP response or std::nullopt on error
373 */
374 virtual std::optional<OCSP::Response> tls_parse_ocsp_response(const std::vector<uint8_t>& raw_response);
375
376 /**
377 * Optional callback: return peer network identity
378 *
379 * There is no expected or specified format. The only expectation is this
380 * function will return a unique value. For example returning the peer
381 * host IP and port.
382 *
383 * This is used to bind the DTLS cookie to a particular network identity.
384 * It is only called if the dtls-cookie-secret PSK is also defined.
385 */
386 virtual std::string tls_peer_network_identity();
387
388 /**
389 * Optional callback: return a custom time stamp value
390 *
391 * This allows the library user to specify a custom "now" timestamp when
392 * needed. By default it will use the current system clock time.
393 *
394 * Note that typical usages will not need to override this callback but it
395 * is useful for testing purposes to allow for deterministic test outcomes.
396 */
397 virtual std::chrono::system_clock::time_point tls_current_timestamp();
398
399 /**
400 * Optional callback: error logging. (not currently called)
401 * @param err An error message related to this connection.
402 */
403 virtual void tls_log_error(const char* err)
404 {
405 BOTAN_UNUSED(err);
406 }
407
408 /**
409 * Optional callback: debug logging. (not currently called)
410 * @param what Some hopefully informative string
411 */
412 virtual void tls_log_debug(const char* what)
413 {
414 BOTAN_UNUSED(what);
415 }
416
417 /**
418 * Optional callback: debug logging taking a buffer. (not currently called)
419 * @param descr What this buffer is
420 * @param val the bytes
421 * @param val_len length of val
422 */
423 virtual void tls_log_debug_bin(const char* descr, const uint8_t val[], size_t val_len)
424 {
425 BOTAN_UNUSED(descr, val, val_len);
426 }
427 };
428
429}
430
431}
432
433#endif
#define BOTAN_UNUSED(...)
Definition: assert.h:141
virtual std::vector< uint8_t > tls_provide_cert_status(const std::vector< X509_Certificate > &chain, const Certificate_Status_Request &csr)
virtual std::chrono::milliseconds tls_verify_cert_chain_ocsp_timeout() const
virtual void tls_emit_data(const uint8_t data[], size_t size)=0
virtual ~Callbacks()=default
virtual void tls_log_debug(const char *what)
virtual void tls_session_activated()
Definition: tls_callbacks.h:99
virtual bool tls_session_established(const Session &session)=0
virtual void tls_log_debug_bin(const char *descr, const uint8_t val[], size_t val_len)
virtual void tls_record_received(uint64_t seq_no, const uint8_t data[], size_t size)=0
virtual void tls_log_error(const char *err)
virtual void tls_alert(Alert alert)=0
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
Definition: alg_id.cpp:13
Usage_Type
Definition: x509cert.h:23
Signature_Format
Definition: pk_keys.h:23