2 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #ifndef BR_BEARSSL_SSL_H__
26 #define BR_BEARSSL_SSL_H__
31 #include "bearssl_block.h"
32 #include "bearssl_hash.h"
33 #include "bearssl_hmac.h"
34 #include "bearssl_prf.h"
35 #include "bearssl_rand.h"
36 #include "bearssl_x509.h"
42 /** \file bearssl_ssl.h
46 * For an overview of the SSL/TLS API, see [the BearSSL Web
47 * site](https://www.bearssl.org/api1.html).
49 * The `BR_TLS_*` constants correspond to the standard cipher suites and
50 * their values in the [IANA
51 * registry](http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-4).
53 * The `BR_ALERT_*` constants are for standard TLS alert messages. When
54 * a fatal alert message is sent of received, then the SSL engine context
55 * status is set to the sum of that alert value (an integer in the 0..255
56 * range) and a fixed offset (`BR_ERR_SEND_FATAL_ALERT` for a sent alert,
57 * `BR_ERR_RECV_FATAL_ALERT` for a received alert).
60 /** \brief Optimal input buffer size. */
61 #define BR_SSL_BUFSIZE_INPUT (16384 + 325)
63 /** \brief Optimal output buffer size. */
64 #define BR_SSL_BUFSIZE_OUTPUT (16384 + 85)
66 /** \brief Optimal buffer size for monodirectional engine
67 (shared input/output buffer). */
68 #define BR_SSL_BUFSIZE_MONO BR_SSL_BUFSIZE_INPUT
70 /** \brief Optimal buffer size for bidirectional engine
71 (single buffer split into two separate input/output buffers). */
72 #define BR_SSL_BUFSIZE_BIDI (BR_SSL_BUFSIZE_INPUT + BR_SSL_BUFSIZE_OUTPUT)
75 * Constants for known SSL/TLS protocol versions (SSL 3.0, TLS 1.0, TLS 1.1
76 * and TLS 1.2). Note that though there is a constant for SSL 3.0, that
77 * protocol version is not actually supported.
80 /** \brief Protocol version: SSL 3.0 (unsupported). */
81 #define BR_SSL30 0x0300
82 /** \brief Protocol version: TLS 1.0. */
83 #define BR_TLS10 0x0301
84 /** \brief Protocol version: TLS 1.1. */
85 #define BR_TLS11 0x0302
86 /** \brief Protocol version: TLS 1.2. */
87 #define BR_TLS12 0x0303
90 * Error constants. They are used to report the reason why a context has
91 * been marked as failed.
93 * Implementation note: SSL-level error codes should be in the 1..31
94 * range. The 32..63 range is for certificate decoding and validation
95 * errors. Received fatal alerts imply an error code in the 256..511 range.
98 /** \brief SSL status: no error so far (0). */
101 /** \brief SSL status: caller-provided parameter is incorrect. */
102 #define BR_ERR_BAD_PARAM 1
104 /** \brief SSL status: operation requested by the caller cannot be applied
105 with the current context state (e.g. reading data while outgoing data
106 is waiting to be sent). */
107 #define BR_ERR_BAD_STATE 2
109 /** \brief SSL status: incoming protocol or record version is unsupported. */
110 #define BR_ERR_UNSUPPORTED_VERSION 3
112 /** \brief SSL status: incoming record version does not match the expected
114 #define BR_ERR_BAD_VERSION 4
116 /** \brief SSL status: incoming record length is invalid. */
117 #define BR_ERR_BAD_LENGTH 5
119 /** \brief SSL status: incoming record is too large to be processed, or
120 buffer is too small for the handshake message to send. */
121 #define BR_ERR_TOO_LARGE 6
123 /** \brief SSL status: decryption found an invalid padding, or the record
124 MAC is not correct. */
125 #define BR_ERR_BAD_MAC 7
127 /** \brief SSL status: no initial entropy was provided, and none can be
128 obtained from the OS. */
129 #define BR_ERR_NO_RANDOM 8
131 /** \brief SSL status: incoming record type is unknown. */
132 #define BR_ERR_UNKNOWN_TYPE 9
134 /** \brief SSL status: incoming record or message has wrong type with
135 regards to the current engine state. */
136 #define BR_ERR_UNEXPECTED 10
138 /** \brief SSL status: ChangeCipherSpec message from the peer has invalid
140 #define BR_ERR_BAD_CCS 12
142 /** \brief SSL status: alert message from the peer has invalid contents
144 #define BR_ERR_BAD_ALERT 13
146 /** \brief SSL status: incoming handshake message decoding failed. */
147 #define BR_ERR_BAD_HANDSHAKE 14
149 /** \brief SSL status: ServerHello contains a session ID which is larger
151 #define BR_ERR_OVERSIZED_ID 15
153 /** \brief SSL status: server wants to use a cipher suite that we did
154 not claim to support. This is also reported if we tried to advertise
155 a cipher suite that we do not support. */
156 #define BR_ERR_BAD_CIPHER_SUITE 16
158 /** \brief SSL status: server wants to use a compression that we did not
160 #define BR_ERR_BAD_COMPRESSION 17
162 /** \brief SSL status: server's max fragment length does not match
164 #define BR_ERR_BAD_FRAGLEN 18
166 /** \brief SSL status: secure renegotiation failed. */
167 #define BR_ERR_BAD_SECRENEG 19
169 /** \brief SSL status: server sent an extension type that we did not
170 announce, or used the same extension type several times in a single
172 #define BR_ERR_EXTRA_EXTENSION 20
174 /** \brief SSL status: invalid Server Name Indication contents (when
175 used by the server, this extension shall be empty). */
176 #define BR_ERR_BAD_SNI 21
178 /** \brief SSL status: invalid ServerHelloDone from the server (length
180 #define BR_ERR_BAD_HELLO_DONE 22
182 /** \brief SSL status: internal limit exceeded (e.g. server's public key
184 #define BR_ERR_LIMIT_EXCEEDED 23
186 /** \brief SSL status: Finished message from peer does not match the
188 #define BR_ERR_BAD_FINISHED 24
190 /** \brief SSL status: session resumption attempt with distinct version
192 #define BR_ERR_RESUME_MISMATCH 25
194 /** \brief SSL status: unsupported or invalid algorithm (ECDHE curve,
195 signature algorithm, hash function). */
196 #define BR_ERR_INVALID_ALGORITHM 26
198 /** \brief SSL status: invalid signature (on ServerKeyExchange from
199 server, or in CertificateVerify from client). */
200 #define BR_ERR_BAD_SIGNATURE 27
202 /** \brief SSL status: peer's public key does not have the proper type
203 or is not allowed for requested operation. */
204 #define BR_ERR_WRONG_KEY_USAGE 28
206 /** \brief SSL status: client did not send a certificate upon request,
207 or the client certificate could not be validated. */
208 #define BR_ERR_NO_CLIENT_AUTH 29
210 /** \brief SSL status: I/O error or premature close on underlying
211 transport stream. This error code is set only by the simplified
212 I/O API ("br_sslio_*"). */
215 /** \brief SSL status: base value for a received fatal alert.
217 When a fatal alert is received from the peer, the alert value
218 is added to this constant. */
219 #define BR_ERR_RECV_FATAL_ALERT 256
221 /** \brief SSL status: base value for a sent fatal alert.
223 When a fatal alert is sent to the peer, the alert value is added
225 #define BR_ERR_SEND_FATAL_ALERT 512
227 /* ===================================================================== */
230 * \brief Decryption engine for SSL.
232 * When processing incoming records, the SSL engine will use a decryption
233 * engine that uses a specific context structure, and has a set of
234 * methods (a vtable) that follows this template.
236 * The decryption engine is responsible for applying decryption, verifying
237 * MAC, and keeping track of the record sequence number.
239 typedef struct br_sslrec_in_class_ br_sslrec_in_class
;
240 struct br_sslrec_in_class_
{
242 * \brief Context size (in bytes).
247 * \brief Test validity of the incoming record length.
249 * This function returns 1 if the announced length for an
250 * incoming record is valid, 0 otherwise,
252 * \param ctx decryption engine context.
253 * \param record_len incoming record length.
254 * \return 1 of a valid length, 0 otherwise.
256 int (*check_length
)(const br_sslrec_in_class
*const *ctx
,
260 * \brief Decrypt the incoming record.
262 * This function may assume that the record length is valid
263 * (it has been previously tested with `check_length()`).
264 * Decryption is done in place; `*len` is updated with the
265 * cleartext length, and the address of the first plaintext
266 * byte is returned. If the record is correct but empty, then
267 * `*len` is set to 0 and a non-`NULL` pointer is returned.
269 * On decryption/MAC error, `NULL` is returned.
271 * \param ctx decryption engine context.
272 * \param record_type record type (23 for application data, etc).
273 * \param version record version.
274 * \param payload address of encrypted payload.
275 * \param len pointer to payload length (updated).
276 * \return pointer to plaintext, or `NULL` on error.
278 unsigned char *(*decrypt
)(const br_sslrec_in_class
**ctx
,
279 int record_type
, unsigned version
,
280 void *payload
, size_t *len
);
284 * \brief Encryption engine for SSL.
286 * When building outgoing records, the SSL engine will use an encryption
287 * engine that uses a specific context structure, and has a set of
288 * methods (a vtable) that follows this template.
290 * The encryption engine is responsible for applying encryption and MAC,
291 * and keeping track of the record sequence number.
293 typedef struct br_sslrec_out_class_ br_sslrec_out_class
;
294 struct br_sslrec_out_class_
{
296 * \brief Context size (in bytes).
301 * \brief Compute maximum plaintext sizes and offsets.
303 * When this function is called, the `*start` and `*end`
304 * values contain offsets designating the free area in the
305 * outgoing buffer for plaintext data; that free area is
306 * preceded by a 5-byte space which will receive the record
309 * The `max_plaintext()` function is responsible for adjusting
310 * both `*start` and `*end` to make room for any record-specific
311 * header, MAC, padding, and possible split.
313 * \param ctx encryption engine context.
314 * \param start pointer to start of plaintext offset (updated).
315 * \param end pointer to start of plaintext offset (updated).
317 void (*max_plaintext
)(const br_sslrec_out_class
*const *ctx
,
318 size_t *start
, size_t *end
);
321 * \brief Perform record encryption.
323 * This function encrypts the record. The plaintext address and
324 * length are provided. Returned value is the start of the
325 * encrypted record (or sequence of records, if a split was
326 * performed), _including_ the 5-byte header, and `*len` is
327 * adjusted to the total size of the record(s), there again
328 * including the header(s).
330 * \param ctx decryption engine context.
331 * \param record_type record type (23 for application data, etc).
332 * \param version record version.
333 * \param plaintext address of plaintext.
334 * \param len pointer to plaintext length (updated).
335 * \return pointer to start of built record.
337 unsigned char *(*encrypt
)(const br_sslrec_out_class
**ctx
,
338 int record_type
, unsigned version
,
339 void *plaintext
, size_t *len
);
343 * \brief Context for a no-encryption engine.
345 * The no-encryption engine processes outgoing records during the initial
346 * handshake, before encryption is applied.
349 /** \brief No-encryption engine vtable. */
350 const br_sslrec_out_class
*vtable
;
351 } br_sslrec_out_clear_context
;
353 /** \brief Static, constant vtable for the no-encryption engine. */
354 extern const br_sslrec_out_class br_sslrec_out_clear_vtable
;
356 /* ===================================================================== */
359 * \brief Record decryption engine class, for CBC mode.
361 * This class type extends the decryption engine class with an
362 * initialisation method that receives the parameters needed
363 * for CBC processing: block cipher implementation, block cipher key,
364 * HMAC parameters (hash function, key, MAC length), and IV. If the
365 * IV is `NULL`, then a per-record IV will be used (TLS 1.1+).
367 typedef struct br_sslrec_in_cbc_class_ br_sslrec_in_cbc_class
;
368 struct br_sslrec_in_cbc_class_
{
370 * \brief Superclass, as first vtable field.
372 br_sslrec_in_class inner
;
375 * \brief Engine initialisation method.
377 * This method sets the vtable field in the context.
379 * \param ctx context to initialise.
380 * \param bc_impl block cipher implementation (CBC decryption).
381 * \param bc_key block cipher key.
382 * \param bc_key_len block cipher key length (in bytes).
383 * \param dig_impl hash function for HMAC.
384 * \param mac_key HMAC key.
385 * \param mac_key_len HMAC key length (in bytes).
386 * \param mac_out_len HMAC output length (in bytes).
387 * \param iv initial IV (or `NULL`).
389 void (*init
)(const br_sslrec_in_cbc_class
**ctx
,
390 const br_block_cbcdec_class
*bc_impl
,
391 const void *bc_key
, size_t bc_key_len
,
392 const br_hash_class
*dig_impl
,
393 const void *mac_key
, size_t mac_key_len
, size_t mac_out_len
,
398 * \brief Record encryption engine class, for CBC mode.
400 * This class type extends the encryption engine class with an
401 * initialisation method that receives the parameters needed
402 * for CBC processing: block cipher implementation, block cipher key,
403 * HMAC parameters (hash function, key, MAC length), and IV. If the
404 * IV is `NULL`, then a per-record IV will be used (TLS 1.1+).
406 typedef struct br_sslrec_out_cbc_class_ br_sslrec_out_cbc_class
;
407 struct br_sslrec_out_cbc_class_
{
409 * \brief Superclass, as first vtable field.
411 br_sslrec_out_class inner
;
414 * \brief Engine initialisation method.
416 * This method sets the vtable field in the context.
418 * \param ctx context to initialise.
419 * \param bc_impl block cipher implementation (CBC encryption).
420 * \param bc_key block cipher key.
421 * \param bc_key_len block cipher key length (in bytes).
422 * \param dig_impl hash function for HMAC.
423 * \param mac_key HMAC key.
424 * \param mac_key_len HMAC key length (in bytes).
425 * \param mac_out_len HMAC output length (in bytes).
426 * \param iv initial IV (or `NULL`).
428 void (*init
)(const br_sslrec_out_cbc_class
**ctx
,
429 const br_block_cbcenc_class
*bc_impl
,
430 const void *bc_key
, size_t bc_key_len
,
431 const br_hash_class
*dig_impl
,
432 const void *mac_key
, size_t mac_key_len
, size_t mac_out_len
,
437 * \brief Context structure for decrypting incoming records with
440 * The first field points to the vtable. The other fields are opaque
441 * and shall not be accessed directly.
444 /** \brief Pointer to vtable. */
445 const br_sslrec_in_cbc_class
*vtable
;
446 #ifndef BR_DOXYGEN_IGNORE
449 const br_block_cbcdec_class
*vtable
;
450 br_aes_gen_cbcdec_keys aes
;
451 br_des_gen_cbcdec_keys des
;
453 br_hmac_key_context mac
;
455 unsigned char iv
[16];
458 } br_sslrec_in_cbc_context
;
461 * \brief Static, constant vtable for record decryption with CBC.
463 extern const br_sslrec_in_cbc_class br_sslrec_in_cbc_vtable
;
466 * \brief Context structure for encrypting outgoing records with
469 * The first field points to the vtable. The other fields are opaque
470 * and shall not be accessed directly.
473 /** \brief Pointer to vtable. */
474 const br_sslrec_out_cbc_class
*vtable
;
475 #ifndef BR_DOXYGEN_IGNORE
478 const br_block_cbcenc_class
*vtable
;
479 br_aes_gen_cbcenc_keys aes
;
480 br_des_gen_cbcenc_keys des
;
482 br_hmac_key_context mac
;
484 unsigned char iv
[16];
487 } br_sslrec_out_cbc_context
;
490 * \brief Static, constant vtable for record encryption with CBC.
492 extern const br_sslrec_out_cbc_class br_sslrec_out_cbc_vtable
;
494 /* ===================================================================== */
497 * \brief Record decryption engine class, for GCM mode.
499 * This class type extends the decryption engine class with an
500 * initialisation method that receives the parameters needed
501 * for GCM processing: block cipher implementation, block cipher key,
502 * GHASH implementation, and 4-byte IV.
504 typedef struct br_sslrec_in_gcm_class_ br_sslrec_in_gcm_class
;
505 struct br_sslrec_in_gcm_class_
{
507 * \brief Superclass, as first vtable field.
509 br_sslrec_in_class inner
;
512 * \brief Engine initialisation method.
514 * This method sets the vtable field in the context.
516 * \param ctx context to initialise.
517 * \param bc_impl block cipher implementation (CTR).
518 * \param key block cipher key.
519 * \param key_len block cipher key length (in bytes).
520 * \param gh_impl GHASH implementation.
521 * \param iv static IV (4 bytes).
523 void (*init
)(const br_sslrec_in_gcm_class
**ctx
,
524 const br_block_ctr_class
*bc_impl
,
525 const void *key
, size_t key_len
,
531 * \brief Record encryption engine class, for GCM mode.
533 * This class type extends the encryption engine class with an
534 * initialisation method that receives the parameters needed
535 * for GCM processing: block cipher implementation, block cipher key,
536 * GHASH implementation, and 4-byte IV.
538 typedef struct br_sslrec_out_gcm_class_ br_sslrec_out_gcm_class
;
539 struct br_sslrec_out_gcm_class_
{
541 * \brief Superclass, as first vtable field.
543 br_sslrec_out_class inner
;
546 * \brief Engine initialisation method.
548 * This method sets the vtable field in the context.
550 * \param ctx context to initialise.
551 * \param bc_impl block cipher implementation (CTR).
552 * \param key block cipher key.
553 * \param key_len block cipher key length (in bytes).
554 * \param gh_impl GHASH implementation.
555 * \param iv static IV (4 bytes).
557 void (*init
)(const br_sslrec_out_gcm_class
**ctx
,
558 const br_block_ctr_class
*bc_impl
,
559 const void *key
, size_t key_len
,
565 * \brief Context structure for processing records with GCM.
567 * The same context structure is used for encrypting and decrypting.
569 * The first field points to the vtable. The other fields are opaque
570 * and shall not be accessed directly.
573 /** \brief Pointer to vtable. */
576 const br_sslrec_in_gcm_class
*in
;
577 const br_sslrec_out_gcm_class
*out
;
579 #ifndef BR_DOXYGEN_IGNORE
582 const br_block_ctr_class
*vtable
;
583 br_aes_gen_ctr_keys aes
;
589 } br_sslrec_gcm_context
;
592 * \brief Static, constant vtable for record decryption with GCM.
594 extern const br_sslrec_in_gcm_class br_sslrec_in_gcm_vtable
;
597 * \brief Static, constant vtable for record encryption with GCM.
599 extern const br_sslrec_out_gcm_class br_sslrec_out_gcm_vtable
;
601 /* ===================================================================== */
604 * \brief Record decryption engine class, for ChaCha20+Poly1305.
606 * This class type extends the decryption engine class with an
607 * initialisation method that receives the parameters needed
608 * for ChaCha20+Poly1305 processing: ChaCha20 implementation,
609 * Poly1305 implementation, key, and 12-byte IV.
611 typedef struct br_sslrec_in_chapol_class_ br_sslrec_in_chapol_class
;
612 struct br_sslrec_in_chapol_class_
{
614 * \brief Superclass, as first vtable field.
616 br_sslrec_in_class inner
;
619 * \brief Engine initialisation method.
621 * This method sets the vtable field in the context.
623 * \param ctx context to initialise.
624 * \param ichacha ChaCha20 implementation.
625 * \param ipoly Poly1305 implementation.
626 * \param key secret key (32 bytes).
627 * \param iv static IV (12 bytes).
629 void (*init
)(const br_sslrec_in_chapol_class
**ctx
,
630 br_chacha20_run ichacha
,
631 br_poly1305_run ipoly
,
632 const void *key
, const void *iv
);
636 * \brief Record encryption engine class, for ChaCha20+Poly1305.
638 * This class type extends the encryption engine class with an
639 * initialisation method that receives the parameters needed
640 * for ChaCha20+Poly1305 processing: ChaCha20 implementation,
641 * Poly1305 implementation, key, and 12-byte IV.
643 typedef struct br_sslrec_out_chapol_class_ br_sslrec_out_chapol_class
;
644 struct br_sslrec_out_chapol_class_
{
646 * \brief Superclass, as first vtable field.
648 br_sslrec_out_class inner
;
651 * \brief Engine initialisation method.
653 * This method sets the vtable field in the context.
655 * \param ctx context to initialise.
656 * \param ichacha ChaCha20 implementation.
657 * \param ipoly Poly1305 implementation.
658 * \param key secret key (32 bytes).
659 * \param iv static IV (12 bytes).
661 void (*init
)(const br_sslrec_out_chapol_class
**ctx
,
662 br_chacha20_run ichacha
,
663 br_poly1305_run ipoly
,
664 const void *key
, const void *iv
);
668 * \brief Context structure for processing records with ChaCha20+Poly1305.
670 * The same context structure is used for encrypting and decrypting.
672 * The first field points to the vtable. The other fields are opaque
673 * and shall not be accessed directly.
676 /** \brief Pointer to vtable. */
679 const br_sslrec_in_chapol_class
*in
;
680 const br_sslrec_out_chapol_class
*out
;
682 #ifndef BR_DOXYGEN_IGNORE
684 unsigned char key
[32];
685 unsigned char iv
[12];
686 br_chacha20_run ichacha
;
687 br_poly1305_run ipoly
;
689 } br_sslrec_chapol_context
;
692 * \brief Static, constant vtable for record decryption with ChaCha20+Poly1305.
694 extern const br_sslrec_in_chapol_class br_sslrec_in_chapol_vtable
;
697 * \brief Static, constant vtable for record encryption with ChaCha20+Poly1305.
699 extern const br_sslrec_out_chapol_class br_sslrec_out_chapol_vtable
;
701 /* ===================================================================== */
704 * \brief Type for session parameters, to be saved for session resumption.
707 /** \brief Session ID buffer. */
708 unsigned char session_id
[32];
709 /** \brief Session ID length (in bytes, at most 32). */
710 unsigned char session_id_len
;
711 /** \brief Protocol version. */
713 /** \brief Cipher suite. */
714 uint16_t cipher_suite
;
715 /** \brief Master secret. */
716 unsigned char master_secret
[48];
717 } br_ssl_session_parameters
;
719 #ifndef BR_DOXYGEN_IGNORE
721 * Maximum numnber of cipher suites supported by a client or server.
723 #define BR_MAX_CIPHER_SUITES 40
727 * \brief Context structure for SSL engine.
729 * This strucuture is common to the client and server; both the client
730 * context (`br_ssl_client_context`) and the server context
731 * (`br_ssl_server_context`) include a `br_ssl_engine_context` as their
734 * The engine context manages records, including alerts, closures, and
735 * transitions to new encryption/MAC algorithms. Processing of handshake
736 * records is delegated to externally provided code. This structure
737 * should not be used directly.
739 * Structure contents are opaque and shall not be accessed directly.
742 #ifndef BR_DOXYGEN_IGNORE
744 * The error code. When non-zero, then the state is "failed" and
745 * no I/O may occur until reset.
750 * Configured I/O buffers. They are either disjoint, or identical.
752 unsigned char *ibuf
, *obuf
;
753 size_t ibuf_len
, obuf_len
;
756 * Maximum fragment length applies to outgoing records; incoming
757 * records can be processed as long as they fit in the input
758 * buffer. It is guaranteed that incoming records at least as big
759 * as max_frag_len can be processed.
761 uint16_t max_frag_len
;
762 unsigned char log_max_frag_len
;
763 unsigned char peer_log_max_frag_len
;
766 * Buffering management registers.
768 size_t ixa
, ixb
, ixc
;
769 size_t oxa
, oxb
, oxc
;
770 unsigned char iomode
;
771 unsigned char incrypt
;
774 * Shutdown flag: when set to non-zero, incoming record bytes
775 * will not be accepted anymore. This is used after a close_notify
776 * has been received: afterwards, the engine no longer claims that
777 * it could receive bytes from the transport medium.
779 unsigned char shutdown_recv
;
782 * 'record_type_in' is set to the incoming record type when the
783 * record header has been received.
784 * 'record_type_out' is used to make the next outgoing record
785 * header when it is ready to go.
787 unsigned char record_type_in
, record_type_out
;
790 * When a record is received, its version is extracted:
791 * -- if 'version_in' is 0, then it is set to the received version;
792 * -- otherwise, if the received version is not identical to
793 * the 'version_in' contents, then a failure is reported.
795 * This implements the SSL requirement that all records shall
796 * use the negotiated protocol version, once decided (in the
797 * ServerHello). It is up to the handshake handler to adjust this
798 * field when necessary.
803 * 'version_out' is used when the next outgoing record is ready
806 uint16_t version_out
;
809 * Record handler contexts.
812 const br_sslrec_in_class
*vtable
;
813 br_sslrec_in_cbc_context cbc
;
814 br_sslrec_gcm_context gcm
;
815 br_sslrec_chapol_context chapol
;
818 const br_sslrec_out_class
*vtable
;
819 br_sslrec_out_clear_context clear
;
820 br_sslrec_out_cbc_context cbc
;
821 br_sslrec_gcm_context gcm
;
822 br_sslrec_chapol_context chapol
;
826 * The "application data" flag. It is set when application data
827 * can be exchanged, cleared otherwise.
829 unsigned char application_data
;
834 br_hmac_drbg_context rng
;
836 int rng_os_rand_done
;
839 * Supported minimum and maximum versions, and cipher suites.
841 uint16_t version_min
;
842 uint16_t version_max
;
843 uint16_t suites_buf
[BR_MAX_CIPHER_SUITES
];
844 unsigned char suites_num
;
847 * For clients, the server name to send as a SNI extension. For
848 * servers, the name received in the SNI extension (if any).
850 char server_name
[256];
853 * "Security parameters". These are filled by the handshake
854 * handler, and used when switching encryption state.
856 unsigned char client_random
[32];
857 unsigned char server_random
[32];
858 br_ssl_session_parameters session
;
861 * ECDHE elements: curve and point from the peer. The server also
862 * uses that buffer for the point to send to the client.
864 unsigned char ecdhe_curve
;
865 unsigned char ecdhe_point
[133];
866 unsigned char ecdhe_point_len
;
869 * Secure renegotiation (RFC 5746): 'reneg' can be:
870 * 0 first handshake (server support is not known)
871 * 1 server does not support secure renegotiation
872 * 2 server supports secure renegotiation
874 * The saved_finished buffer contains the client and the
875 * server "Finished" values from the last handshake, in
876 * that order (12 bytes each).
879 unsigned char saved_finished
[24];
887 * Context variables for the handshake processor. The 'pad' must
888 * be large enough to accommodate an RSA-encrypted pre-master
889 * secret, or an RSA signature; since we want to support up to
890 * RSA-4096, this means at least 512 bytes. (Other pad usages
891 * require its length to be at least 256.)
896 const unsigned char *ip
;
898 uint32_t dp_stack
[32];
899 uint32_t rp_stack
[32];
900 unsigned char pad
[512];
901 unsigned char *hbuf_in
, *hbuf_out
, *saved_hbuf_out
;
902 size_t hlen_in
, hlen_out
;
903 void (*hsrun
)(void *ctx
);
906 * The 'action' value communicates OOB information between the
907 * engine and the handshake processor.
910 * 0 invocation triggered by I/O
911 * 1 invocation triggered by explicit close
912 * 2 invocation triggered by explicit renegotiation
914 unsigned char action
;
917 * State for alert messages. Value is either 0, or the value of
918 * the alert level byte (level is either 1 for warning, or 2 for
919 * fatal; we convert all other values to 'fatal').
924 * Closure flags. This flag is set when a close_notify has been
925 * received from the peer.
927 unsigned char close_received
;
930 * Multi-hasher for the handshake messages. The handshake handler
931 * is responsible for resetting it when appropriate.
933 br_multihash_context mhash
;
936 * Pointer to the X.509 engine. The engine is supposed to be
937 * already initialized. It is used to validate the peer's
940 const br_x509_class
**x509ctx
;
943 * Certificate chain to send. This is used by both client and
944 * server, when they send their respective Certificate messages.
945 * If chain_len is 0, then chain may be NULL.
947 const br_x509_certificate
*chain
;
949 const unsigned char *cert_cur
;
953 * List of supported protocol names (ALPN extension). If unset,
954 * (number of names is 0), then:
955 * - the client sends no ALPN extension;
956 * - the server ignores any incoming ALPN extension.
959 * - the client sends an ALPN extension with all the names;
960 * - the server selects the first protocol in its list that
961 * the client also supports, or fails (fatal alert 120)
962 * if the client sends an ALPN extension and there is no
965 * The 'selected_protocol' field contains 1+n if the matching
966 * name has index n in the list (the value is 0 if no match was
967 * performed, e.g. the peer did not send an ALPN extension).
969 const char **protocol_names
;
970 uint16_t protocol_names_num
;
971 uint16_t selected_protocol
;
974 * Pointers to implementations; left to NULL for unsupported
975 * functions. For the raw hash functions, implementations are
976 * referenced from the multihasher (mhash field).
978 br_tls_prf_impl prf10
;
979 br_tls_prf_impl prf_sha256
;
980 br_tls_prf_impl prf_sha384
;
981 const br_block_cbcenc_class
*iaes_cbcenc
;
982 const br_block_cbcdec_class
*iaes_cbcdec
;
983 const br_block_ctr_class
*iaes_ctr
;
984 const br_block_cbcenc_class
*ides_cbcenc
;
985 const br_block_cbcdec_class
*ides_cbcdec
;
987 br_chacha20_run ichacha
;
988 br_poly1305_run ipoly
;
989 const br_sslrec_in_cbc_class
*icbc_in
;
990 const br_sslrec_out_cbc_class
*icbc_out
;
991 const br_sslrec_in_gcm_class
*igcm_in
;
992 const br_sslrec_out_gcm_class
*igcm_out
;
993 const br_sslrec_in_chapol_class
*ichapol_in
;
994 const br_sslrec_out_chapol_class
*ichapol_out
;
995 const br_ec_impl
*iec
;
996 br_rsa_pkcs1_vrfy irsavrfy
;
997 br_ecdsa_vrfy iecdsa
;
999 } br_ssl_engine_context
;
1002 * \brief Get currently defined engine behavioural flags.
1004 * \param cc SSL engine context.
1005 * \return the flags.
1007 static inline uint32_t
1008 br_ssl_engine_get_flags(br_ssl_engine_context
*cc
)
1014 * \brief Set all engine behavioural flags.
1016 * \param cc SSL engine context.
1017 * \param flags new value for all flags.
1020 br_ssl_engine_set_all_flags(br_ssl_engine_context
*cc
, uint32_t flags
)
1026 * \brief Set some engine behavioural flags.
1028 * The flags set in the `flags` parameter are set in the context; other
1029 * flags are untouched.
1031 * \param cc SSL engine context.
1032 * \param flags additional set flags.
1035 br_ssl_engine_add_flags(br_ssl_engine_context
*cc
, uint32_t flags
)
1041 * \brief Clear some engine behavioural flags.
1043 * The flags set in the `flags` parameter are cleared from the context; other
1044 * flags are untouched.
1046 * \param cc SSL engine context.
1047 * \param flags flags to remove.
1050 br_ssl_engine_remove_flags(br_ssl_engine_context
*cc
, uint32_t flags
)
1052 cc
->flags
&= ~flags
;
1056 * \brief Behavioural flag: enforce server preferences.
1058 * If this flag is set, then the server will enforce its own cipher suite
1059 * preference order; otherwise, it follows the client preferences.
1061 #define BR_OPT_ENFORCE_SERVER_PREFERENCES ((uint32_t)1 << 0)
1064 * \brief Behavioural flag: disable renegotiation.
1066 * If this flag is set, then renegotiations are rejected unconditionally:
1067 * they won't be honoured if asked for programmatically, and requests from
1068 * the peer are rejected.
1070 #define BR_OPT_NO_RENEGOTIATION ((uint32_t)1 << 1)
1073 * \brief Behavioural flag: tolerate lack of client authentication.
1075 * If this flag is set in a server and the server requests a client
1076 * certificate, but the authentication fails (the client does not send
1077 * a certificate, or the client's certificate chain cannot be validated),
1078 * then the connection keeps on. Without this flag, a failed client
1079 * authentication terminates the connection.
1083 * - If the client's certificate can be validated and its public key is
1084 * supported, then a wrong signature value terminates the connection
1085 * regardless of that flag.
1087 * - If using full-static ECDH, then a failure to validate the client's
1088 * certificate prevents the handshake from succeeding.
1090 #define BR_OPT_TOLERATE_NO_CLIENT_AUTH ((uint32_t)1 << 2)
1093 * \brief Behavioural flag: fail on application protocol mismatch.
1095 * The ALPN extension ([RFC 7301](https://tools.ietf.org/html/rfc7301))
1096 * allows the client to send a list of application protocol names, and
1097 * the server to select one. A mismatch is one of the following occurrences:
1099 * - On the client: the client sends a list of names, the server
1100 * responds with a protocol name which is _not_ part of the list of
1101 * names sent by the client.
1103 * - On the server: the client sends a list of names, and the server
1104 * is also configured with a list of names, but there is no common
1105 * protocol name between the two lists.
1107 * Normal behaviour in case of mismatch is to report no matching name
1108 * (`br_ssl_engine_get_selected_protocol()` returns `NULL`) and carry on.
1109 * If the flag is set, then a mismatch implies a protocol failure (if
1110 * the mismatch is detected by the server, it will send a fatal alert).
1112 * Note: even with this flag, `br_ssl_engine_get_selected_protocol()`
1113 * may still return `NULL` if the client or the server does not send an
1114 * ALPN extension at all.
1116 #define BR_OPT_FAIL_ON_ALPN_MISMATCH ((uint32_t)1 << 3)
1119 * \brief Set the minimum and maximum supported protocol versions.
1121 * The two provided versions MUST be supported by the implementation
1122 * (i.e. TLS 1.0, 1.1 and 1.2), and `version_max` MUST NOT be lower
1123 * than `version_min`.
1125 * \param cc SSL engine context.
1126 * \param version_min minimum supported TLS version.
1127 * \param version_max maximum supported TLS version.
1130 br_ssl_engine_set_versions(br_ssl_engine_context
*cc
,
1131 unsigned version_min
, unsigned version_max
)
1133 cc
->version_min
= version_min
;
1134 cc
->version_max
= version_max
;
1138 * \brief Set the list of cipher suites advertised by this context.
1140 * The provided array is copied into the context. It is the caller
1141 * responsibility to ensure that all provided suites will be supported
1142 * by the context. The engine context has enough room to receive _all_
1143 * suites supported by the implementation. The provided array MUST NOT
1144 * contain duplicates.
1146 * If the engine is for a client, the "signaling" pseudo-cipher suite
1147 * `TLS_FALLBACK_SCSV` can be added at the end of the list, if the
1148 * calling application is performing a voluntary downgrade (voluntary
1149 * downgrades are not recommended, but if such a downgrade is done, then
1150 * adding the fallback pseudo-suite is a good idea).
1152 * \param cc SSL engine context.
1153 * \param suites cipher suites.
1154 * \param suites_num number of cipher suites.
1156 void br_ssl_engine_set_suites(br_ssl_engine_context
*cc
,
1157 const uint16_t *suites
, size_t suites_num
);
1160 * \brief Set the X.509 engine.
1162 * The caller shall ensure that the X.509 engine is properly initialised.
1164 * \param cc SSL engine context.
1165 * \param x509ctx X.509 certificate validation context.
1168 br_ssl_engine_set_x509(br_ssl_engine_context
*cc
, const br_x509_class
**x509ctx
)
1170 cc
->x509ctx
= x509ctx
;
1174 * \brief Set the supported protocol names.
1176 * Protocol names are part of the ALPN extension ([RFC
1177 * 7301](https://tools.ietf.org/html/rfc7301)). Each protocol name is a
1178 * character string, containing no more than 255 characters (256 with the
1179 * terminating zero). When names are set, then:
1181 * - The client will send an ALPN extension, containing the names. If
1182 * the server responds with an ALPN extension, the client will verify
1183 * that the response contains one of its name, and report that name
1184 * through `br_ssl_engine_get_selected_protocol()`.
1186 * - The server will parse incoming ALPN extension (from clients), and
1187 * try to find a common protocol; if none is found, the connection
1188 * is aborted with a fatal alert. On match, a response ALPN extension
1189 * is sent, and name is reported through
1190 * `br_ssl_engine_get_selected_protocol()`.
1192 * The provided array is linked in, and must remain valid while the
1193 * connection is live.
1195 * Names MUST NOT be empty. Names MUST NOT be longer than 255 characters
1196 * (excluding the terminating 0).
1198 * \param ctx SSL engine context.
1199 * \param names list of protocol names (zero-terminated).
1200 * \param num number of protocol names (MUST be 1 or more).
1203 br_ssl_engine_set_protocol_names(br_ssl_engine_context
*ctx
,
1204 const char **names
, size_t num
)
1206 ctx
->protocol_names
= names
;
1207 ctx
->protocol_names_num
= num
;
1211 * \brief Get the selected protocol.
1213 * If this context was initialised with a non-empty list of protocol
1214 * names, and both client and server sent ALPN extensions during the
1215 * handshake, and a common name was found, then that name is returned.
1216 * Otherwise, `NULL` is returned.
1218 * The returned pointer is one of the pointers provided to the context
1219 * with `br_ssl_engine_set_protocol_names()`.
1221 * \return the selected protocol, or `NULL`.
1223 static inline const char *
1224 br_ssl_engine_get_selected_protocol(br_ssl_engine_context
*ctx
)
1228 k
= ctx
->selected_protocol
;
1229 return (k
== 0 || k
== 0xFFFF) ? NULL
: ctx
->protocol_names
[k
- 1];
1233 * \brief Set a hash function implementation (by ID).
1235 * Hash functions set with this call will be used for SSL/TLS specific
1236 * usages, not X.509 certificate validation. Only "standard" hash functions
1237 * may be set (MD5, SHA-1, SHA-224, SHA-256, SHA-384, SHA-512). If `impl`
1238 * is `NULL`, then the hash function support is removed, not added.
1240 * \param ctx SSL engine context.
1241 * \param id hash function identifier.
1242 * \param impl hash function implementation (or `NULL`).
1245 br_ssl_engine_set_hash(br_ssl_engine_context
*ctx
,
1246 int id
, const br_hash_class
*impl
)
1248 br_multihash_setimpl(&ctx
->mhash
, id
, impl
);
1252 * \brief Get a hash function implementation (by ID).
1254 * This function retrieves a hash function implementation which was
1255 * set with `br_ssl_engine_set_hash()`.
1257 * \param ctx SSL engine context.
1258 * \param id hash function identifier.
1259 * \return the hash function implementation (or `NULL`).
1261 static inline const br_hash_class
*
1262 br_ssl_engine_get_hash(br_ssl_engine_context
*ctx
, int id
)
1264 return br_multihash_getimpl(&ctx
->mhash
, id
);
1268 * \brief Set the PRF implementation (for TLS 1.0 and 1.1).
1270 * This function sets (or removes, if `impl` is `NULL`) the implemenation
1271 * for the PRF used in TLS 1.0 and 1.1.
1273 * \param cc SSL engine context.
1274 * \param impl PRF implementation (or `NULL`).
1277 br_ssl_engine_set_prf10(br_ssl_engine_context
*cc
, br_tls_prf_impl impl
)
1283 * \brief Set the PRF implementation with SHA-256 (for TLS 1.2).
1285 * This function sets (or removes, if `impl` is `NULL`) the implemenation
1286 * for the SHA-256 variant of the PRF used in TLS 1.2.
1288 * \param cc SSL engine context.
1289 * \param impl PRF implementation (or `NULL`).
1292 br_ssl_engine_set_prf_sha256(br_ssl_engine_context
*cc
, br_tls_prf_impl impl
)
1294 cc
->prf_sha256
= impl
;
1298 * \brief Set the PRF implementation with SHA-384 (for TLS 1.2).
1300 * This function sets (or removes, if `impl` is `NULL`) the implemenation
1301 * for the SHA-384 variant of the PRF used in TLS 1.2.
1303 * \param cc SSL engine context.
1304 * \param impl PRF implementation (or `NULL`).
1307 br_ssl_engine_set_prf_sha384(br_ssl_engine_context
*cc
, br_tls_prf_impl impl
)
1309 cc
->prf_sha384
= impl
;
1313 * \brief Set the AES/CBC implementations.
1315 * \param cc SSL engine context.
1316 * \param impl_enc AES/CBC encryption implementation (or `NULL`).
1317 * \param impl_dec AES/CBC decryption implementation (or `NULL`).
1320 br_ssl_engine_set_aes_cbc(br_ssl_engine_context
*cc
,
1321 const br_block_cbcenc_class
*impl_enc
,
1322 const br_block_cbcdec_class
*impl_dec
)
1324 cc
->iaes_cbcenc
= impl_enc
;
1325 cc
->iaes_cbcdec
= impl_dec
;
1329 * \brief Set the "default" AES/CBC implementations.
1331 * This function configures in the engine the AES implementations that
1332 * should provide best runtime performance on the local system, while
1333 * still being safe (in particular, constant-time). It also sets the
1334 * handlers for CBC records.
1336 * \param cc SSL engine context.
1338 void br_ssl_engine_set_default_aes_cbc(br_ssl_engine_context
*cc
);
1341 * \brief Set the AES/CTR implementation.
1343 * \param cc SSL engine context.
1344 * \param impl AES/CTR encryption/decryption implementation (or `NULL`).
1347 br_ssl_engine_set_aes_ctr(br_ssl_engine_context
*cc
,
1348 const br_block_ctr_class
*impl
)
1350 cc
->iaes_ctr
= impl
;
1354 * \brief Set the "default" implementations for AES/GCM (AES/CTR + GHASH).
1356 * This function configures in the engine the AES/CTR and GHASH
1357 * implementation that should provide best runtime performance on the local
1358 * system, while still being safe (in particular, constant-time). It also
1359 * sets the handlers for GCM records.
1361 * \param cc SSL engine context.
1363 void br_ssl_engine_set_default_aes_gcm(br_ssl_engine_context
*cc
);
1366 * \brief Set the DES/CBC implementations.
1368 * \param cc SSL engine context.
1369 * \param impl_enc DES/CBC encryption implementation (or `NULL`).
1370 * \param impl_dec DES/CBC decryption implementation (or `NULL`).
1373 br_ssl_engine_set_des_cbc(br_ssl_engine_context
*cc
,
1374 const br_block_cbcenc_class
*impl_enc
,
1375 const br_block_cbcdec_class
*impl_dec
)
1377 cc
->ides_cbcenc
= impl_enc
;
1378 cc
->ides_cbcdec
= impl_dec
;
1382 * \brief Set the "default" DES/CBC implementations.
1384 * This function configures in the engine the DES implementations that
1385 * should provide best runtime performance on the local system, while
1386 * still being safe (in particular, constant-time). It also sets the
1387 * handlers for CBC records.
1389 * \param cc SSL engine context.
1391 void br_ssl_engine_set_default_des_cbc(br_ssl_engine_context
*cc
);
1394 * \brief Set the GHASH implementation (used in GCM mode).
1396 * \param cc SSL engine context.
1397 * \param impl GHASH implementation (or `NULL`).
1400 br_ssl_engine_set_ghash(br_ssl_engine_context
*cc
, br_ghash impl
)
1406 * \brief Set the ChaCha20 implementation.
1408 * \param cc SSL engine context.
1409 * \param ichacha ChaCha20 implementation (or `NULL`).
1412 br_ssl_engine_set_chacha20(br_ssl_engine_context
*cc
,
1413 br_chacha20_run ichacha
)
1415 cc
->ichacha
= ichacha
;
1419 * \brief Set the Poly1305 implementation.
1421 * \param cc SSL engine context.
1422 * \param ipoly Poly1305 implementation (or `NULL`).
1425 br_ssl_engine_set_poly1305(br_ssl_engine_context
*cc
,
1426 br_poly1305_run ipoly
)
1432 * \brief Set the "default" ChaCha20 and Poly1305 implementations.
1434 * This function configures in the engine the ChaCha20 and Poly1305
1435 * implementations that should provide best runtime performance on the
1436 * local system, while still being safe (in particular, constant-time).
1437 * It also sets the handlers for ChaCha20+Poly1305 records.
1439 * \param cc SSL engine context.
1441 void br_ssl_engine_set_default_chapol(br_ssl_engine_context
*cc
);
1444 * \brief Set the record encryption and decryption engines for CBC + HMAC.
1446 * \param cc SSL engine context.
1447 * \param impl_in record CBC decryption implementation (or `NULL`).
1448 * \param impl_out record CBC encryption implementation (or `NULL`).
1451 br_ssl_engine_set_cbc(br_ssl_engine_context
*cc
,
1452 const br_sslrec_in_cbc_class
*impl_in
,
1453 const br_sslrec_out_cbc_class
*impl_out
)
1455 cc
->icbc_in
= impl_in
;
1456 cc
->icbc_out
= impl_out
;
1460 * \brief Set the record encryption and decryption engines for GCM.
1462 * \param cc SSL engine context.
1463 * \param impl_in record GCM decryption implementation (or `NULL`).
1464 * \param impl_out record GCM encryption implementation (or `NULL`).
1467 br_ssl_engine_set_gcm(br_ssl_engine_context
*cc
,
1468 const br_sslrec_in_gcm_class
*impl_in
,
1469 const br_sslrec_out_gcm_class
*impl_out
)
1471 cc
->igcm_in
= impl_in
;
1472 cc
->igcm_out
= impl_out
;
1476 * \brief Set the record encryption and decryption engines for
1477 * ChaCha20+Poly1305.
1479 * \param cc SSL engine context.
1480 * \param impl_in record ChaCha20 decryption implementation (or `NULL`).
1481 * \param impl_out record ChaCha20 encryption implementation (or `NULL`).
1484 br_ssl_engine_set_chapol(br_ssl_engine_context
*cc
,
1485 const br_sslrec_in_chapol_class
*impl_in
,
1486 const br_sslrec_out_chapol_class
*impl_out
)
1488 cc
->ichapol_in
= impl_in
;
1489 cc
->ichapol_out
= impl_out
;
1493 * \brief Set the EC implementation.
1495 * The elliptic curve implementation will be used for ECDH and ECDHE
1496 * cipher suites, and for ECDSA support.
1498 * \param cc SSL engine context.
1499 * \param iec EC implementation (or `NULL`).
1502 br_ssl_engine_set_ec(br_ssl_engine_context
*cc
, const br_ec_impl
*iec
)
1508 * \brief Set the "default" EC implementation.
1510 * This function sets the elliptic curve implementation for ECDH and
1511 * ECDHE cipher suites, and for ECDSA support. It selects the fastest
1512 * implementation on the current system.
1514 * \param cc SSL engine context.
1516 void br_ssl_engine_set_default_ec(br_ssl_engine_context
*cc
);
1519 * \brief Get the EC implementation configured in the provided engine.
1521 * \param cc SSL engine context.
1522 * \return the EC implementation.
1524 static inline const br_ec_impl
*
1525 br_ssl_engine_get_ec(br_ssl_engine_context
*cc
)
1531 * \brief Set the RSA signature verification implementation.
1533 * On the client, this is used to verify the server's signature on its
1534 * ServerKeyExchange message (for ECDHE_RSA cipher suites). On the server,
1535 * this is used to verify the client's CertificateVerify message (if a
1536 * client certificate is requested, and that certificate contains a RSA key).
1538 * \param cc SSL engine context.
1539 * \param irsavrfy RSA signature verification implementation.
1542 br_ssl_engine_set_rsavrfy(br_ssl_engine_context
*cc
, br_rsa_pkcs1_vrfy irsavrfy
)
1544 cc
->irsavrfy
= irsavrfy
;
1548 * \brief Set the "default" RSA implementation (signature verification).
1550 * This function sets the RSA implementation (signature verification)
1551 * to the fastest implementation available on the current platform.
1553 * \param cc SSL engine context.
1555 void br_ssl_engine_set_default_rsavrfy(br_ssl_engine_context
*cc
);
1558 * \brief Get the RSA implementation (signature verification) configured
1559 * in the provided engine.
1561 * \param cc SSL engine context.
1562 * \return the RSA signature verification implementation.
1564 static inline br_rsa_pkcs1_vrfy
1565 br_ssl_engine_get_rsavrfy(br_ssl_engine_context
*cc
)
1567 return cc
->irsavrfy
;
1571 * \brief Set the ECDSA implementation (signature verification).
1573 * On the client, this is used to verify the server's signature on its
1574 * ServerKeyExchange message (for ECDHE_ECDSA cipher suites). On the server,
1575 * this is used to verify the client's CertificateVerify message (if a
1576 * client certificate is requested, that certificate contains an EC key,
1577 * and full-static ECDH is not used).
1579 * The ECDSA implementation will use the EC core implementation configured
1580 * in the engine context.
1582 * \param cc client context.
1583 * \param iecdsa ECDSA verification implementation.
1586 br_ssl_engine_set_ecdsa(br_ssl_engine_context
*cc
, br_ecdsa_vrfy iecdsa
)
1588 cc
->iecdsa
= iecdsa
;
1592 * \brief Set the "default" ECDSA implementation (signature verification).
1594 * This function sets the ECDSA implementation (signature verification)
1595 * to the fastest implementation available on the current platform. This
1596 * call also sets the elliptic curve implementation itself, there again
1597 * to the fastest EC implementation available.
1599 * \param cc SSL engine context.
1601 void br_ssl_engine_set_default_ecdsa(br_ssl_engine_context
*cc
);
1604 * \brief Get the ECDSA implementation (signature verification) configured
1605 * in the provided engine.
1607 * \param cc SSL engine context.
1608 * \return the ECDSA signature verification implementation.
1610 static inline br_ecdsa_vrfy
1611 br_ssl_engine_get_ecdsa(br_ssl_engine_context
*cc
)
1617 * \brief Set the I/O buffer for the SSL engine.
1619 * Once this call has been made, `br_ssl_client_reset()` or
1620 * `br_ssl_server_reset()` MUST be called before using the context.
1622 * The provided buffer will be used as long as the engine context is
1623 * used. The caller is responsible for keeping it available.
1625 * If `bidi` is 0, then the engine will operate in half-duplex mode
1626 * (it won't be able to send data while there is unprocessed incoming
1627 * data in the buffer, and it won't be able to receive data while there
1628 * is unsent data in the buffer). The optimal buffer size in half-duplex
1629 * mode is `BR_SSL_BUFSIZE_MONO`; if the buffer is larger, then extra
1630 * bytes are ignored. If the buffer is smaller, then this limits the
1631 * capacity of the engine to support all allowed record sizes.
1633 * If `bidi` is 1, then the engine will split the buffer into two
1634 * parts, for separate handling of outgoing and incoming data. This
1635 * enables full-duplex processing, but requires more RAM. The optimal
1636 * buffer size in full-duplex mode is `BR_SSL_BUFSIZE_BIDI`; if the
1637 * buffer is larger, then extra bytes are ignored. If the buffer is
1638 * smaller, then the split will favour the incoming part, so that
1639 * interoperability is maximised.
1641 * \param cc SSL engine context
1642 * \param iobuf I/O buffer.
1643 * \param iobuf_len I/O buffer length (in bytes).
1644 * \param bidi non-zero for full-duplex mode.
1646 void br_ssl_engine_set_buffer(br_ssl_engine_context
*cc
,
1647 void *iobuf
, size_t iobuf_len
, int bidi
);
1650 * \brief Set the I/O buffers for the SSL engine.
1652 * Once this call has been made, `br_ssl_client_reset()` or
1653 * `br_ssl_server_reset()` MUST be called before using the context.
1655 * This function is similar to `br_ssl_engine_set_buffer()`, except
1656 * that it enforces full-duplex mode, and the two I/O buffers are
1657 * provided as separate chunks.
1659 * The macros `BR_SSL_BUFSIZE_INPUT` and `BR_SSL_BUFSIZE_OUTPUT`
1660 * evaluate to the optimal (maximum) sizes for the input and output
1661 * buffer, respectively.
1663 * \param cc SSL engine context
1664 * \param ibuf input buffer.
1665 * \param ibuf_len input buffer length (in bytes).
1666 * \param obuf output buffer.
1667 * \param obuf_len output buffer length (in bytes).
1669 void br_ssl_engine_set_buffers_bidi(br_ssl_engine_context
*cc
,
1670 void *ibuf
, size_t ibuf_len
, void *obuf
, size_t obuf_len
);
1673 * \brief Inject some "initial entropy" in the context.
1675 * This entropy will be added to what can be obtained from the
1676 * underlying operating system, if that OS is supported.
1678 * This function may be called several times; all injected entropy chunks
1679 * are cumulatively mixed.
1681 * If entropy gathering from the OS is supported and compiled in, then this
1682 * step is optional. Otherwise, it is mandatory to inject randomness, and
1683 * the caller MUST take care to push (as one or several successive calls)
1684 * enough entropy to achieve cryptographic resistance (at least 80 bits,
1685 * preferably 128 or more). The engine will report an error if no entropy
1686 * was provided and none can be obtained from the OS.
1688 * Take care that this function cannot assess the cryptographic quality of
1689 * the provided bytes.
1691 * In all generality, "entropy" must here be considered to mean "that
1692 * which the attacker cannot predict". If your OS/architecture does not
1693 * have a suitable source of randomness, then you can make do with the
1694 * combination of a large enough secret value (possibly a copy of an
1695 * asymmetric private key that you also store on the system) AND a
1696 * non-repeating value (e.g. current time, provided that the local clock
1697 * cannot be reset or altered by the attacker).
1699 * \param cc SSL engine context.
1700 * \param data extra entropy to inject.
1701 * \param len length of the extra data (in bytes).
1703 void br_ssl_engine_inject_entropy(br_ssl_engine_context
*cc
,
1704 const void *data
, size_t len
);
1707 * \brief Get the "server name" in this engine.
1709 * For clients, this is the name provided with `br_ssl_client_reset()`;
1710 * for servers, this is the name received from the client as part of the
1711 * ClientHello message. If there is no such name (e.g. the client did
1712 * not send an SNI extension) then the returned string is empty
1713 * (returned pointer points to a byte of value 0).
1715 * The returned pointer refers to a buffer inside the context, which may
1716 * be overwritten as part of normal SSL activity (even within the same
1717 * connection, if a renegotiation occurs).
1719 * \param cc SSL engine context.
1720 * \return the server name (possibly empty).
1722 static inline const char *
1723 br_ssl_engine_get_server_name(const br_ssl_engine_context
*cc
)
1725 return cc
->server_name
;
1729 * \brief Get the protocol version.
1731 * This function returns the protocol version that is used by the
1732 * engine. That value is set after sending (for a server) or receiving
1733 * (for a client) the ServerHello message.
1735 * \param cc SSL engine context.
1736 * \return the protocol version.
1738 static inline unsigned
1739 br_ssl_engine_get_version(const br_ssl_engine_context
*cc
)
1741 return cc
->session
.version
;
1745 * \brief Get a copy of the session parameters.
1747 * The session parameters are filled during the handshake, so this
1748 * function shall not be called before completion of the handshake.
1749 * The initial handshake is completed when the context first allows
1750 * application data to be injected.
1752 * This function copies the current session parameters into the provided
1753 * structure. Beware that the session parameters include the master
1754 * secret, which is sensitive data, to handle with great care.
1756 * \param cc SSL engine context.
1757 * \param pp destination structure for the session parameters.
1760 br_ssl_engine_get_session_parameters(const br_ssl_engine_context
*cc
,
1761 br_ssl_session_parameters
*pp
)
1763 memcpy(pp
, &cc
->session
, sizeof *pp
);
1767 * \brief Set the session parameters to the provided values.
1769 * This function is meant to be used in the client, before doing a new
1770 * handshake; a session resumption will be attempted with these
1771 * parameters. In the server, this function has no effect.
1773 * \param cc SSL engine context.
1774 * \param pp source structure for the session parameters.
1777 br_ssl_engine_set_session_parameters(br_ssl_engine_context
*cc
,
1778 const br_ssl_session_parameters
*pp
)
1780 memcpy(&cc
->session
, pp
, sizeof *pp
);
1784 * \brief Get identifier for the curve used for key exchange.
1786 * If the cipher suite uses ECDHE, then this function returns the
1787 * identifier for the curve used for transient parameters. This is
1788 * defined during the course of the handshake, when the ServerKeyExchange
1789 * is sent (on the server) or received (on the client). If the
1790 * cipher suite does not use ECDHE (e.g. static ECDH, or RSA key
1791 * exchange), then this value is indeterminate.
1793 * @param cc SSL engine context.
1794 * @return the ECDHE curve identifier.
1797 br_ssl_engine_get_ecdhe_curve(br_ssl_engine_context
*cc
)
1799 return cc
->ecdhe_curve
;
1803 * \brief Get the current engine state.
1805 * An SSL engine (client or server) has, at any time, a state which is
1806 * the combination of zero, one or more of these flags:
1810 * Engine is finished, no more I/O (until next reset).
1812 * - `BR_SSL_SENDREC`
1814 * Engine has some bytes to send to the peer.
1816 * - `BR_SSL_RECVREC`
1818 * Engine expects some bytes from the peer.
1820 * - `BR_SSL_SENDAPP`
1822 * Engine may receive application data to send (or flush).
1824 * - `BR_SSL_RECVAPP`
1826 * Engine has obtained some application data from the peer,
1827 * that should be read by the caller.
1829 * If no flag at all is set (state value is 0), then the engine is not
1830 * fully initialised yet.
1832 * The `BR_SSL_CLOSED` flag is exclusive; when it is set, no other flag
1833 * is set. To distinguish between a normal closure and an error, use
1834 * `br_ssl_engine_last_error()`.
1836 * Generally speaking, `BR_SSL_SENDREC` and `BR_SSL_SENDAPP` are mutually
1837 * exclusive: the input buffer, at any point, either accumulates
1838 * plaintext data, or contains an assembled record that is being sent.
1839 * Similarly, `BR_SSL_RECVREC` and `BR_SSL_RECVAPP` are mutually exclusive.
1840 * This may change in a future library version.
1842 * \param cc SSL engine context.
1843 * \return the current engine state.
1845 unsigned br_ssl_engine_current_state(const br_ssl_engine_context
*cc
);
1847 /** \brief SSL engine state: closed or failed. */
1848 #define BR_SSL_CLOSED 0x0001
1849 /** \brief SSL engine state: record data is ready to be sent to the peer. */
1850 #define BR_SSL_SENDREC 0x0002
1851 /** \brief SSL engine state: engine may receive records from the peer. */
1852 #define BR_SSL_RECVREC 0x0004
1853 /** \brief SSL engine state: engine may accept application data to send. */
1854 #define BR_SSL_SENDAPP 0x0008
1855 /** \brief SSL engine state: engine has received application data. */
1856 #define BR_SSL_RECVAPP 0x0010
1859 * \brief Get the engine error indicator.
1861 * The error indicator is `BR_ERR_OK` (0) if no error was encountered
1862 * since the last call to `br_ssl_client_reset()` or
1863 * `br_ssl_server_reset()`. Other status values are "sticky": they
1864 * remain set, and prevent all I/O activity, until cleared. Only the
1865 * reset calls clear the error indicator.
1867 * \param cc SSL engine context.
1868 * \return 0, or a non-zero error code.
1871 br_ssl_engine_last_error(const br_ssl_engine_context
*cc
)
1877 * There are four I/O operations, each identified by a symbolic name:
1879 * sendapp inject application data in the engine
1880 * recvapp retrieving application data from the engine
1881 * sendrec sending records on the transport medium
1882 * recvrec receiving records from the transport medium
1884 * Terminology works thus: in a layered model where the SSL engine sits
1885 * between the application and the network, "send" designates operations
1886 * where bytes flow from application to network, and "recv" for the
1887 * reverse operation. Application data (the plaintext that is to be
1888 * conveyed through SSL) is "app", while encrypted records are "rec".
1889 * Note that from the SSL engine point of view, "sendapp" and "recvrec"
1890 * designate bytes that enter the engine ("inject" operation), while
1891 * "recvapp" and "sendrec" designate bytes that exit the engine
1892 * ("extract" operation).
1894 * For the operation 'xxx', two functions are defined:
1896 * br_ssl_engine_xxx_buf
1897 * Returns a pointer and length to the buffer to use for that
1898 * operation. '*len' is set to the number of bytes that may be read
1899 * from the buffer (extract operation) or written to the buffer
1900 * (inject operation). If no byte may be exchanged for that operation
1901 * at that point, then '*len' is set to zero, and NULL is returned.
1902 * The engine state is unmodified by this call.
1904 * br_ssl_engine_xxx_ack
1905 * Informs the engine that 'len' bytes have been read from the buffer
1906 * (extract operation) or written to the buffer (inject operation).
1907 * The 'len' value MUST NOT be zero. The 'len' value MUST NOT exceed
1908 * that which was obtained from a preceeding br_ssl_engine_xxx_buf()
1913 * \brief Get buffer for application data to send.
1915 * If the engine is ready to accept application data to send to the
1916 * peer, then this call returns a pointer to the buffer where such
1917 * data shall be written, and its length is written in `*len`.
1918 * Otherwise, `*len` is set to 0 and `NULL` is returned.
1920 * \param cc SSL engine context.
1921 * \param len receives the application data output buffer length, or 0.
1922 * \return the application data output buffer, or `NULL`.
1924 unsigned char *br_ssl_engine_sendapp_buf(
1925 const br_ssl_engine_context
*cc
, size_t *len
);
1928 * \brief Inform the engine of some new application data.
1930 * After writing `len` bytes in the buffer returned by
1931 * `br_ssl_engine_sendapp_buf()`, the application shall call this
1932 * function to trigger any relevant processing. The `len` parameter
1933 * MUST NOT be 0, and MUST NOT exceed the value obtained in the
1934 * `br_ssl_engine_sendapp_buf()` call.
1936 * \param cc SSL engine context.
1937 * \param len number of bytes pushed (not zero).
1939 void br_ssl_engine_sendapp_ack(br_ssl_engine_context
*cc
, size_t len
);
1942 * \brief Get buffer for received application data.
1944 * If the engine has received application data from the peer, hen this
1945 * call returns a pointer to the buffer from where such data shall be
1946 * read, and its length is written in `*len`. Otherwise, `*len` is set
1947 * to 0 and `NULL` is returned.
1949 * \param cc SSL engine context.
1950 * \param len receives the application data input buffer length, or 0.
1951 * \return the application data input buffer, or `NULL`.
1953 unsigned char *br_ssl_engine_recvapp_buf(
1954 const br_ssl_engine_context
*cc
, size_t *len
);
1957 * \brief Acknowledge some received application data.
1959 * After reading `len` bytes from the buffer returned by
1960 * `br_ssl_engine_recvapp_buf()`, the application shall call this
1961 * function to trigger any relevant processing. The `len` parameter
1962 * MUST NOT be 0, and MUST NOT exceed the value obtained in the
1963 * `br_ssl_engine_recvapp_buf()` call.
1965 * \param cc SSL engine context.
1966 * \param len number of bytes read (not zero).
1968 void br_ssl_engine_recvapp_ack(br_ssl_engine_context
*cc
, size_t len
);
1971 * \brief Get buffer for record data to send.
1973 * If the engine has prepared some records to send to the peer, then this
1974 * call returns a pointer to the buffer from where such data shall be
1975 * read, and its length is written in `*len`. Otherwise, `*len` is set
1976 * to 0 and `NULL` is returned.
1978 * \param cc SSL engine context.
1979 * \param len receives the record data output buffer length, or 0.
1980 * \return the record data output buffer, or `NULL`.
1982 unsigned char *br_ssl_engine_sendrec_buf(
1983 const br_ssl_engine_context
*cc
, size_t *len
);
1986 * \brief Acknowledge some sent record data.
1988 * After reading `len` bytes from the buffer returned by
1989 * `br_ssl_engine_sendrec_buf()`, the application shall call this
1990 * function to trigger any relevant processing. The `len` parameter
1991 * MUST NOT be 0, and MUST NOT exceed the value obtained in the
1992 * `br_ssl_engine_sendrec_buf()` call.
1994 * \param cc SSL engine context.
1995 * \param len number of bytes read (not zero).
1997 void br_ssl_engine_sendrec_ack(br_ssl_engine_context
*cc
, size_t len
);
2000 * \brief Get buffer for incoming records.
2002 * If the engine is ready to accept records from the peer, then this
2003 * call returns a pointer to the buffer where such data shall be
2004 * written, and its length is written in `*len`. Otherwise, `*len` is
2005 * set to 0 and `NULL` is returned.
2007 * \param cc SSL engine context.
2008 * \param len receives the record data input buffer length, or 0.
2009 * \return the record data input buffer, or `NULL`.
2011 unsigned char *br_ssl_engine_recvrec_buf(
2012 const br_ssl_engine_context
*cc
, size_t *len
);
2015 * \brief Inform the engine of some new record data.
2017 * After writing `len` bytes in the buffer returned by
2018 * `br_ssl_engine_recvrec_buf()`, the application shall call this
2019 * function to trigger any relevant processing. The `len` parameter
2020 * MUST NOT be 0, and MUST NOT exceed the value obtained in the
2021 * `br_ssl_engine_recvrec_buf()` call.
2023 * \param cc SSL engine context.
2024 * \param len number of bytes pushed (not zero).
2026 void br_ssl_engine_recvrec_ack(br_ssl_engine_context
*cc
, size_t len
);
2029 * \brief Flush buffered application data.
2031 * If some application data has been buffered in the engine, then wrap
2032 * it into a record and mark it for sending. If no application data has
2033 * been buffered but the engine would be ready to accept some, AND the
2034 * `force` parameter is non-zero, then an empty record is assembled and
2035 * marked for sending. In all other cases, this function does nothing.
2037 * Empty records are technically legal, but not all existing SSL/TLS
2038 * implementations support them. Empty records can be useful as a
2039 * transparent "keep-alive" mechanism to maintain some low-level
2042 * \param cc SSL engine context.
2043 * \param force non-zero to force sending an empty record.
2045 void br_ssl_engine_flush(br_ssl_engine_context
*cc
, int force
);
2048 * \brief Initiate a closure.
2050 * If, at that point, the context is open and in ready state, then a
2051 * `close_notify` alert is assembled and marked for sending; this
2052 * triggers the closure protocol. Otherwise, no such alert is assembled.
2054 * \param cc SSL engine context.
2056 void br_ssl_engine_close(br_ssl_engine_context
*cc
);
2059 * \brief Initiate a renegotiation.
2061 * If the engine is failed or closed, or if the peer is known not to
2062 * support secure renegotiation (RFC 5746), or if renegotiations have
2063 * been disabled with the `BR_OPT_NO_RENEGOTIATION` flag, then this
2064 * function returns 0 and nothing else happens.
2066 * Otherwise, this function returns 1, and a renegotiation attempt is
2067 * triggered (if a handshake is already ongoing at that point, then
2068 * no new handshake is triggered).
2070 * \param cc SSL engine context.
2071 * \return 1 on success, 0 on error.
2073 int br_ssl_engine_renegotiate(br_ssl_engine_context
*cc
);
2076 * Pre-declaration for the SSL client context.
2078 typedef struct br_ssl_client_context_ br_ssl_client_context
;
2081 * \brief Type for the client certificate, if requested by the server.
2085 * \brief Authentication type.
2087 * This is either `BR_AUTH_RSA` (RSA signature), `BR_AUTH_ECDSA`
2088 * (ECDSA signature), or `BR_AUTH_ECDH` (static ECDH key exchange).
2093 * \brief Hash function for computing the CertificateVerify.
2095 * This is the symbolic identifier for the hash function that
2096 * will be used to produce the hash of handshake messages, to
2097 * be signed into the CertificateVerify. For full static ECDH
2098 * (client and server certificates are both EC in the same
2099 * curve, and static ECDH is used), this value is set to -1.
2101 * Take care that with TLS 1.0 and 1.1, that value MUST match
2102 * the protocol requirements: value must be 0 (MD5+SHA-1) for
2103 * a RSA signature, or 2 (SHA-1) for an ECDSA signature. Only
2104 * TLS 1.2 allows for other hash functions.
2109 * \brief Certificate chain to send to the server.
2111 * This is an array of `br_x509_certificate` objects, each
2112 * normally containing a DER-encoded certificate. The client
2113 * code does not try to decode these elements. If there is no
2114 * chain to send to the server, then this pointer shall be
2117 const br_x509_certificate
*chain
;
2120 * \brief Certificate chain length (number of certificates).
2122 * If there is no chain to send to the server, then this value
2123 * shall be set to 0.
2127 } br_ssl_client_certificate
;
2130 * Note: the constants below for signatures match the TLS constants.
2133 /** \brief Client authentication type: static ECDH. */
2134 #define BR_AUTH_ECDH 0
2135 /** \brief Client authentication type: RSA signature. */
2136 #define BR_AUTH_RSA 1
2137 /** \brief Client authentication type: ECDSA signature. */
2138 #define BR_AUTH_ECDSA 3
2141 * \brief Class type for a certificate handler (client side).
2143 * A certificate handler selects a client certificate chain to send to
2144 * the server, upon explicit request from that server. It receives
2145 * the list of trust anchor DN from the server, and supported types
2146 * of certificates and signatures, and returns the chain to use. It
2147 * is also invoked to perform the corresponding private key operation
2148 * (a signature, or an ECDH computation).
2150 * The SSL client engine will first push the trust anchor DN with
2151 * `start_name_list()`, `start_name()`, `append_name()`, `end_name()`
2152 * and `end_name_list()`. Then it will call `choose()`, to select the
2153 * actual chain (and signature/hash algorithms). Finally, it will call
2154 * either `do_sign()` or `do_keyx()`, depending on the algorithm choices.
2156 typedef struct br_ssl_client_certificate_class_ br_ssl_client_certificate_class
;
2157 struct br_ssl_client_certificate_class_
{
2159 * \brief Context size (in bytes).
2161 size_t context_size
;
2164 * \brief Begin reception of a list of trust anchor names. This
2165 * is called while parsing the incoming CertificateRequest.
2167 * \param pctx certificate handler context.
2169 void (*start_name_list
)(const br_ssl_client_certificate_class
**pctx
);
2172 * \brief Begin reception of a new trust anchor name.
2174 * The total encoded name length is provided; it is less than
2177 * \param pctx certificate handler context.
2178 * \param len encoded name length (in bytes).
2180 void (*start_name
)(const br_ssl_client_certificate_class
**pctx
,
2184 * \brief Receive some more bytes for the current trust anchor name.
2186 * The provided reference (`data`) points to a transient buffer
2187 * they may be reused as soon as this function returns. The chunk
2188 * length (`len`) is never zero.
2190 * \param pctx certificate handler context.
2191 * \param data anchor name chunk.
2192 * \param len anchor name chunk length (in bytes).
2194 void (*append_name
)(const br_ssl_client_certificate_class
**pctx
,
2195 const unsigned char *data
, size_t len
);
2198 * \brief End current trust anchor name.
2200 * This function is called when all the encoded anchor name data
2201 * has been provided.
2203 * \param pctx certificate handler context.
2205 void (*end_name
)(const br_ssl_client_certificate_class
**pctx
);
2208 * \brief End list of trust anchor names.
2210 * This function is called when all the anchor names in the
2211 * CertificateRequest message have been obtained.
2213 * \param pctx certificate handler context.
2215 void (*end_name_list
)(const br_ssl_client_certificate_class
**pctx
);
2218 * \brief Select client certificate and algorithms.
2220 * This callback function shall fill the provided `choices`
2221 * structure with the selected algorithms and certificate chain.
2222 * The `hash_id`, `chain` and `chain_len` fields must be set. If
2223 * the client cannot or does not wish to send a certificate,
2224 * then it shall set `chain` to `NULL` and `chain_len` to 0.
2226 * The `auth_types` parameter describes the authentication types,
2227 * signature algorithms and hash functions that are supported by
2228 * both the client context and the server, and compatible with
2229 * the current protocol version. This is a bit field with the
2230 * following contents:
2232 * - If RSA signatures with hash function x are supported, then
2235 * - If ECDSA signatures with hash function x are supported,
2236 * then bit 8+x is set.
2238 * - If static ECDH is supported, with a RSA-signed certificate,
2239 * then bit 16 is set.
2241 * - If static ECDH is supported, with an ECDSA-signed certificate,
2242 * then bit 17 is set.
2246 * - When using TLS 1.0 or 1.1, the hash function for RSA
2247 * signatures is always the special MD5+SHA-1 (id 0), and the
2248 * hash function for ECDSA signatures is always SHA-1 (id 2).
2250 * - When using TLS 1.2, the list of hash functions is trimmed
2251 * down to include only hash functions that the client context
2252 * can support. The actual server list can be obtained with
2253 * `br_ssl_client_get_server_hashes()`; that list may be used
2254 * to select the certificate chain to send to the server.
2256 * \param pctx certificate handler context.
2257 * \param cc SSL client context.
2258 * \param auth_types supported authentication types and algorithms.
2259 * \param choices destination structure for the policy choices.
2261 void (*choose
)(const br_ssl_client_certificate_class
**pctx
,
2262 const br_ssl_client_context
*cc
, uint32_t auth_types
,
2263 br_ssl_client_certificate
*choices
);
2266 * \brief Perform key exchange (client part).
2268 * This callback is invoked in case of a full static ECDH key
2271 * - the cipher suite uses `ECDH_RSA` or `ECDH_ECDSA`;
2273 * - the server requests a client certificate;
2275 * - the client has, and sends, a client certificate that
2276 * uses an EC key in the same curve as the server's key,
2277 * and chooses static ECDH (the `hash_id` field in the choice
2278 * structure was set to -1).
2280 * In that situation, this callback is invoked to compute the
2281 * client-side ECDH: the provided `data` (of length `*len` bytes)
2282 * is the server's public key point (as decoded from its
2283 * certificate), and the client shall multiply that point with
2284 * its own private key, and write back the X coordinate of the
2285 * resulting point in the same buffer, starting at offset 0.
2286 * The `*len` value shall be modified to designate the actual
2287 * length of the X coordinate.
2289 * The callback must uphold the following:
2291 * - If the input array does not have the proper length for
2292 * an encoded curve point, then an error (0) shall be reported.
2294 * - If the input array has the proper length, then processing
2295 * MUST be constant-time, even if the data is not a valid
2298 * - This callback MUST check that the input point is valid.
2300 * Returned value is 1 on success, 0 on error.
2302 * \param pctx certificate handler context.
2303 * \param data server public key point.
2304 * \param len public key point length / X coordinate length.
2305 * \return 1 on success, 0 on error.
2307 uint32_t (*do_keyx
)(const br_ssl_client_certificate_class
**pctx
,
2308 unsigned char *data
, size_t *len
);
2311 * \brief Perform a signature (client authentication).
2313 * This callback is invoked when a client certificate was sent,
2314 * and static ECDH is not used. It shall compute a signature,
2315 * using the client's private key, over the provided hash value
2316 * (which is the hash of all previous handshake messages).
2318 * On input, the hash value to sign is in `data`, of size
2319 * `hv_len`; the involved hash function is identified by
2320 * `hash_id`. The signature shall be computed and written
2321 * back into `data`; the total size of that buffer is `len`
2324 * This callback shall verify that the signature length does not
2325 * exceed `len` bytes, and abstain from writing the signature if
2328 * For RSA signatures, the `hash_id` may be 0, in which case
2329 * this is the special header-less signature specified in TLS 1.0
2330 * and 1.1, with a 36-byte hash value. Otherwise, normal PKCS#1
2331 * v1.5 signatures shall be computed.
2333 * For ECDSA signatures, the signature value shall use the ASN.1
2336 * Returned value is the signature length (in bytes), or 0 on error.
2338 * \param pctx certificate handler context.
2339 * \param hash_id hash function identifier.
2340 * \param hv_len hash value length (in bytes).
2341 * \param data input/output buffer (hash value, then signature).
2342 * \param len total buffer length (in bytes).
2343 * \return signature length (in bytes) on success, or 0 on error.
2345 size_t (*do_sign
)(const br_ssl_client_certificate_class
**pctx
,
2346 int hash_id
, size_t hv_len
, unsigned char *data
, size_t len
);
2350 * \brief A single-chain RSA client certificate handler.
2352 * This handler uses a single certificate chain, with a RSA
2353 * signature. The list of trust anchor DN is ignored.
2355 * Apart from the first field (vtable pointer), its contents are
2356 * opaque and shall not be accessed directly.
2359 /** \brief Pointer to vtable. */
2360 const br_ssl_client_certificate_class
*vtable
;
2361 #ifndef BR_DOXYGEN_IGNORE
2362 const br_x509_certificate
*chain
;
2364 const br_rsa_private_key
*sk
;
2365 br_rsa_pkcs1_sign irsasign
;
2367 } br_ssl_client_certificate_rsa_context
;
2370 * \brief A single-chain EC client certificate handler.
2372 * This handler uses a single certificate chain, with a RSA
2373 * signature. The list of trust anchor DN is ignored.
2375 * This handler may support both static ECDH, and ECDSA signatures
2376 * (either usage may be selectively disabled).
2378 * Apart from the first field (vtable pointer), its contents are
2379 * opaque and shall not be accessed directly.
2382 /** \brief Pointer to vtable. */
2383 const br_ssl_client_certificate_class
*vtable
;
2384 #ifndef BR_DOXYGEN_IGNORE
2385 const br_x509_certificate
*chain
;
2387 const br_ec_private_key
*sk
;
2388 unsigned allowed_usages
;
2389 unsigned issuer_key_type
;
2390 const br_multihash_context
*mhash
;
2391 const br_ec_impl
*iec
;
2392 br_ecdsa_sign iecdsa
;
2394 } br_ssl_client_certificate_ec_context
;
2397 * \brief Context structure for a SSL client.
2399 * The first field (called `eng`) is the SSL engine; all functions that
2400 * work on a `br_ssl_engine_context` structure shall take as parameter
2401 * a pointer to that field. The other structure fields are opaque and
2402 * must not be accessed directly.
2404 struct br_ssl_client_context_
{
2406 * \brief The encapsulated engine context.
2408 br_ssl_engine_context eng
;
2410 #ifndef BR_DOXYGEN_IGNORE
2412 * Minimum ClientHello length; padding with an extension (RFC
2413 * 7685) is added if necessary to match at least that length.
2414 * Such padding is nominally unnecessary, but it has been used
2415 * to work around some server implementation bugs.
2417 uint16_t min_clienthello_len
;
2420 * Bit field for algoithms (hash + signature) supported by the
2421 * server when requesting a client certificate.
2426 * Server's public key curve.
2431 * Context for certificate handler.
2433 const br_ssl_client_certificate_class
**client_auth_vtable
;
2436 * Client authentication type.
2438 unsigned char auth_type
;
2441 * Hash function to use for the client signature. This is 0xFF
2442 * if static ECDH is used.
2444 unsigned char hash_id
;
2447 * For the core certificate handlers, thus avoiding (in most
2448 * cases) the need for an externally provided policy context.
2451 const br_ssl_client_certificate_class
*vtable
;
2452 br_ssl_client_certificate_rsa_context single_rsa
;
2453 br_ssl_client_certificate_ec_context single_ec
;
2459 br_rsa_public irsapub
;
2464 * \brief Get the hash functions and signature algorithms supported by
2467 * This value is a bit field:
2469 * - If RSA (PKCS#1 v1.5) is supported with hash function of ID `x`,
2470 * then bit `x` is set (hash function ID is 0 for the special MD5+SHA-1,
2471 * or 2 to 6 for the SHA family).
2473 * - If ECDSA is suported with hash function of ID `x`, then bit `8+x`
2476 * - Newer algorithms are symbolic 16-bit identifiers that do not
2477 * represent signature algorithm and hash function separately. If
2478 * the TLS-level identifier is `0x0800+x` for a `x` in the 0..15
2479 * range, then bit `16+x` is set.
2481 * "New algorithms" are currently defined only in draft documents, so
2482 * this support is subject to possible change. Right now (early 2017),
2483 * this maps ed25519 (EdDSA on Curve25519) to bit 23, and ed448 (EdDSA
2484 * on Curve448) to bit 24. If the identifiers on the wire change in
2485 * future document, then the decoding mechanism in BearSSL will be
2486 * amended to keep mapping ed25519 and ed448 on bits 23 and 24,
2487 * respectively. Mapping of other new algorithms (e.g. RSA/PSS) is not
2490 * \param cc client context.
2491 * \return the server-supported hash functions and signature algorithms.
2493 static inline uint32_t
2494 br_ssl_client_get_server_hashes(const br_ssl_client_context
*cc
)
2500 * \brief Get the server key curve.
2502 * This function returns the ID for the curve used by the server's public
2503 * key. This is set when the server's certificate chain is processed;
2504 * this value is 0 if the server's key is not an EC key.
2506 * \return the server's public key curve ID, or 0.
2509 br_ssl_client_get_server_curve(const br_ssl_client_context
*cc
)
2511 return cc
->server_curve
;
2515 * Each br_ssl_client_init_xxx() function sets the list of supported
2516 * cipher suites and used implementations, as specified by the profile
2517 * name 'xxx'. Defined profile names are:
2519 * full all supported versions and suites; constant-time implementations
2520 * TODO: add other profiles
2524 * \brief SSL client profile: full.
2526 * This function initialises the provided SSL client context with
2527 * all supported algorithms and cipher suites. It also initialises
2528 * a companion X.509 validation engine with all supported algorithms,
2529 * and the provided trust anchors; the X.509 engine will be used by
2530 * the client context to validate the server's certificate.
2532 * \param cc client context to initialise.
2533 * \param xc X.509 validation context to initialise.
2534 * \param trust_anchors trust anchors to use.
2535 * \param trust_anchors_num number of trust anchors.
2537 void br_ssl_client_init_full(br_ssl_client_context
*cc
,
2538 br_x509_minimal_context
*xc
,
2539 const br_x509_trust_anchor
*trust_anchors
, size_t trust_anchors_num
);
2542 * \brief Clear the complete contents of a SSL client context.
2544 * Everything is cleared, including the reference to the configured buffer,
2545 * implementations, cipher suites and state. This is a preparatory step
2546 * to assembling a custom profile.
2548 * \param cc client context to clear.
2550 void br_ssl_client_zero(br_ssl_client_context
*cc
);
2553 * \brief Set an externally provided client certificate handler context.
2555 * The handler's methods are invoked when the server requests a client
2558 * \param cc client context.
2559 * \param pctx certificate handler context (pointer to its vtable field).
2562 br_ssl_client_set_client_certificate(br_ssl_client_context
*cc
,
2563 const br_ssl_client_certificate_class
**pctx
)
2565 cc
->client_auth_vtable
= pctx
;
2569 * \brief Set the RSA public-key operations implementation.
2571 * This will be used to encrypt the pre-master secret with the server's
2572 * RSA public key (RSA-encryption cipher suites only).
2574 * \param cc client context.
2575 * \param irsapub RSA public-key encryption implementation.
2578 br_ssl_client_set_rsapub(br_ssl_client_context
*cc
, br_rsa_public irsapub
)
2580 cc
->irsapub
= irsapub
;
2584 * \brief Set the "default" RSA implementation for public-key operations.
2586 * This sets the RSA implementation in the client context (for encrypting
2587 * the pre-master secret, in `TLS_RSA_*` cipher suites) to the fastest
2588 * available on the current platform.
2590 * \param cc client context.
2592 void br_ssl_client_set_default_rsapub(br_ssl_client_context
*cc
);
2595 * \brief Set the minimum ClientHello length (RFC 7685 padding).
2597 * If this value is set and the ClientHello would be shorter, then
2598 * the Pad ClientHello extension will be added with enough padding bytes
2599 * to reach the target size. Because of the extension header, the resulting
2600 * size will sometimes be slightly more than `len` bytes if the target
2601 * size cannot be exactly met.
2603 * The target length relates to the _contents_ of the ClientHello, not
2604 * counting its 4-byte header. For instance, if `len` is set to 512,
2605 * then the padding will bring the ClientHello size to 516 bytes with its
2606 * header, and 521 bytes when counting the 5-byte record header.
2608 * \param cc client context.
2609 * \param len minimum ClientHello length (in bytes).
2612 br_ssl_client_set_min_clienthello_len(br_ssl_client_context
*cc
, uint16_t len
)
2614 cc
->min_clienthello_len
= len
;
2618 * \brief Prepare or reset a client context for a new connection.
2620 * The `server_name` parameter is used to fill the SNI extension; the
2621 * X.509 "minimal" engine will also match that name against the server
2622 * names included in the server's certificate. If the parameter is
2623 * `NULL` then no SNI extension will be sent, and the X.509 "minimal"
2624 * engine (if used for server certificate validation) will not check
2625 * presence of any specific name in the received certificate.
2627 * Therefore, setting the `server_name` to `NULL` shall be reserved
2628 * to cases where alternate or additional methods are used to ascertain
2629 * that the right server public key is used (e.g. a "known key" model).
2631 * If `resume_session` is non-zero and the context was previously used
2632 * then the session parameters may be reused (depending on whether the
2633 * server previously sent a non-empty session ID, and accepts the session
2634 * resumption). The session parameters for session resumption can also
2635 * be set explicitly with `br_ssl_engine_set_session_parameters()`.
2637 * On failure, the context is marked as failed, and this function
2638 * returns 0. A possible failure condition is when no initial entropy
2639 * was injected, and none could be obtained from the OS (either OS
2640 * randomness gathering is not supported, or it failed).
2642 * \param cc client context.
2643 * \param server_name target server name, or `NULL`.
2644 * \param resume_session non-zero to try session resumption.
2645 * \return 0 on failure, 1 on success.
2647 int br_ssl_client_reset(br_ssl_client_context
*cc
,
2648 const char *server_name
, int resume_session
);
2651 * \brief Forget any session in the context.
2653 * This means that the next handshake that uses this context will
2654 * necessarily be a full handshake (this applies both to new connections
2655 * and to renegotiations).
2657 * \param cc client context.
2660 br_ssl_client_forget_session(br_ssl_client_context
*cc
)
2662 cc
->eng
.session
.session_id_len
= 0;
2666 * \brief Set client certificate chain and key (single RSA case).
2668 * This function sets a client certificate chain, that the client will
2669 * send to the server whenever a client certificate is requested. This
2670 * certificate uses an RSA public key; the corresponding private key is
2671 * invoked for authentication. Trust anchor names sent by the server are
2674 * The provided chain and private key are linked in the client context;
2675 * they must remain valid as long as they may be used, i.e. normally
2676 * for the duration of the connection, since they might be invoked
2677 * again upon renegotiations.
2679 * \param cc SSL client context.
2680 * \param chain client certificate chain (SSL order: EE comes first).
2681 * \param chain_len client chain length (number of certificates).
2682 * \param sk client private key.
2683 * \param irsasign RSA signature implementation (PKCS#1 v1.5).
2685 void br_ssl_client_set_single_rsa(br_ssl_client_context
*cc
,
2686 const br_x509_certificate
*chain
, size_t chain_len
,
2687 const br_rsa_private_key
*sk
, br_rsa_pkcs1_sign irsasign
);
2690 * \brief Set the client certificate chain and key (single EC case).
2692 * This function sets a client certificate chain, that the client will
2693 * send to the server whenever a client certificate is requested. This
2694 * certificate uses an EC public key; the corresponding private key is
2695 * invoked for authentication. Trust anchor names sent by the server are
2698 * The provided chain and private key are linked in the client context;
2699 * they must remain valid as long as they may be used, i.e. normally
2700 * for the duration of the connection, since they might be invoked
2701 * again upon renegotiations.
2703 * The `allowed_usages` is a combination of usages, namely
2704 * `BR_KEYTYPE_KEYX` and/or `BR_KEYTYPE_SIGN`. The `BR_KEYTYPE_KEYX`
2705 * value allows full static ECDH, while the `BR_KEYTYPE_SIGN` value
2706 * allows ECDSA signatures. If ECDSA signatures are used, then an ECDSA
2707 * signature implementation must be provided; otherwise, the `iecdsa`
2708 * parameter may be 0.
2710 * The `cert_issuer_key_type` value is either `BR_KEYTYPE_RSA` or
2711 * `BR_KEYTYPE_EC`; it is the type of the public key used the the CA
2712 * that issued (signed) the client certificate. That value is used with
2713 * full static ECDH: support of the certificate by the server depends
2714 * on how the certificate was signed. (Note: when using TLS 1.2, this
2715 * parameter is ignored; but its value matters for TLS 1.0 and 1.1.)
2717 * \param cc server context.
2718 * \param chain server certificate chain to send.
2719 * \param chain_len chain length (number of certificates).
2720 * \param sk server private key (EC).
2721 * \param allowed_usages allowed private key usages.
2722 * \param cert_issuer_key_type issuing CA's key type.
2723 * \param iec EC core implementation.
2724 * \param iecdsa ECDSA signature implementation ("asn1" format).
2726 void br_ssl_client_set_single_ec(br_ssl_client_context
*cc
,
2727 const br_x509_certificate
*chain
, size_t chain_len
,
2728 const br_ec_private_key
*sk
, unsigned allowed_usages
,
2729 unsigned cert_issuer_key_type
,
2730 const br_ec_impl
*iec
, br_ecdsa_sign iecdsa
);
2733 * \brief Type for a "translated cipher suite", as an array of two
2736 * The first element is the cipher suite identifier (as used on the wire).
2737 * The second element is the concatenation of four 4-bit elements which
2738 * characterise the cipher suite contents. In most to least significant
2739 * order, these 4-bit elements are:
2741 * - Bits 12 to 15: key exchange + server key type
2743 * | val | symbolic constant | suite type | details |
2744 * | :-- | :----------------------- | :---------- | :----------------------------------------------- |
2745 * | 0 | `BR_SSLKEYX_RSA` | RSA | RSA key exchange, key is RSA (encryption) |
2746 * | 1 | `BR_SSLKEYX_ECDHE_RSA` | ECDHE_RSA | ECDHE key exchange, key is RSA (signature) |
2747 * | 2 | `BR_SSLKEYX_ECDHE_ECDSA` | ECDHE_ECDSA | ECDHE key exchange, key is EC (signature) |
2748 * | 3 | `BR_SSLKEYX_ECDH_RSA` | ECDH_RSA | Key is EC (key exchange), cert signed with RSA |
2749 * | 4 | `BR_SSLKEYX_ECDH_ECDSA` | ECDH_ECDSA | Key is EC (key exchange), cert signed with ECDSA |
2751 * - Bits 8 to 11: symmetric encryption algorithm
2753 * | val | symbolic constant | symmetric encryption | key strength (bits) |
2754 * | :-- | :--------------------- | :------------------- | :------------------ |
2755 * | 0 | `BR_SSLENC_3DES_CBC` | 3DES/CBC | 168 |
2756 * | 1 | `BR_SSLENC_AES128_CBC` | AES-128/CBC | 128 |
2757 * | 2 | `BR_SSLENC_AES256_CBC` | AES-256/CBC | 256 |
2758 * | 3 | `BR_SSLENC_AES128_GCM` | AES-128/GCM | 128 |
2759 * | 4 | `BR_SSLENC_AES256_GCM` | AES-256/GCM | 256 |
2760 * | 5 | `BR_SSLENC_CHACHA20` | ChaCha20/Poly1305 | 256 |
2762 * - Bits 4 to 7: MAC algorithm
2764 * | val | symbolic constant | MAC type | details |
2765 * | :-- | :----------------- | :----------- | :------------------------------------ |
2766 * | 0 | `BR_SSLMAC_AEAD` | AEAD | No dedicated MAC (encryption is AEAD) |
2767 * | 2 | `BR_SSLMAC_SHA1` | HMAC/SHA-1 | Value matches `br_sha1_ID` |
2768 * | 4 | `BR_SSLMAC_SHA256` | HMAC/SHA-256 | Value matches `br_sha256_ID` |
2769 * | 5 | `BR_SSLMAC_SHA384` | HMAC/SHA-384 | Value matches `br_sha384_ID` |
2771 * - Bits 0 to 3: hash function for PRF when used with TLS-1.2
2773 * | val | symbolic constant | hash function | details |
2774 * | :-- | :----------------- | :------------ | :----------------------------------- |
2775 * | 4 | `BR_SSLPRF_SHA256` | SHA-256 | Value matches `br_sha256_ID` |
2776 * | 5 | `BR_SSLPRF_SHA384` | SHA-384 | Value matches `br_sha384_ID` |
2778 * For instance, cipher suite `TLS_RSA_WITH_AES_128_GCM_SHA256` has
2779 * standard identifier 0x009C, and is translated to 0x0304, for, in
2780 * that order: RSA key exchange (0), AES-128/GCM (3), AEAD integrity (0),
2781 * SHA-256 in the TLS PRF (4).
2783 typedef uint16_t br_suite_translated
[2];
2785 #ifndef BR_DOXYGEN_IGNORE
2787 * Constants are already documented in the br_suite_translated type.
2790 #define BR_SSLKEYX_RSA 0
2791 #define BR_SSLKEYX_ECDHE_RSA 1
2792 #define BR_SSLKEYX_ECDHE_ECDSA 2
2793 #define BR_SSLKEYX_ECDH_RSA 3
2794 #define BR_SSLKEYX_ECDH_ECDSA 4
2796 #define BR_SSLENC_3DES_CBC 0
2797 #define BR_SSLENC_AES128_CBC 1
2798 #define BR_SSLENC_AES256_CBC 2
2799 #define BR_SSLENC_AES128_GCM 3
2800 #define BR_SSLENC_AES256_GCM 4
2801 #define BR_SSLENC_CHACHA20 5
2803 #define BR_SSLMAC_AEAD 0
2804 #define BR_SSLMAC_SHA1 br_sha1_ID
2805 #define BR_SSLMAC_SHA256 br_sha256_ID
2806 #define BR_SSLMAC_SHA384 br_sha384_ID
2808 #define BR_SSLPRF_SHA256 br_sha256_ID
2809 #define BR_SSLPRF_SHA384 br_sha384_ID
2814 * Pre-declaration for the SSL server context.
2816 typedef struct br_ssl_server_context_ br_ssl_server_context
;
2819 * \brief Type for the server policy choices, taken after analysis of
2820 * the client message (ClientHello).
2824 * \brief Cipher suite to use with that client.
2826 uint16_t cipher_suite
;
2829 * \brief Hash function or algorithm for signing the ServerKeyExchange.
2831 * This parameter is ignored for `TLS_RSA_*` and `TLS_ECDH_*`
2832 * cipher suites; it is used only for `TLS_ECDHE_*` suites, in
2833 * which the server _signs_ the ephemeral EC Diffie-Hellman
2834 * parameters sent to the client.
2836 * This identifier must be one of the following values:
2838 * - `0xFF00 + id`, where `id` is a hash function identifier
2839 * (0 for MD5+SHA-1, or 2 to 6 for one of the SHA functions);
2841 * - a full 16-bit identifier, lower than `0xFF00`.
2843 * If the first option is used, then the SSL engine will
2844 * compute the hash of the data that is to be signed, with the
2845 * designated hash function. The `do_sign()` method will be
2846 * invoked with that hash value provided in the the `data`
2849 * If the second option is used, then the SSL engine will NOT
2850 * compute a hash on the data; instead, it will provide the
2851 * to-be-signed data itself in `data`, i.e. the concatenation of
2852 * the client random, server random, and encoded ECDH
2853 * parameters. Furthermore, with TLS-1.2 and later, the 16-bit
2854 * identifier will be used "as is" in the protocol, in the
2855 * SignatureAndHashAlgorithm; for instance, `0x0401` stands for
2856 * RSA PKCS#1 v1.5 signature (the `01`) with SHA-256 as hash
2857 * function (the `04`).
2859 * Take care that with TLS 1.0 and 1.1, the hash function is
2860 * constrainted by the protocol: RSA signature must use
2861 * MD5+SHA-1 (so use `0xFF00`), while ECDSA must use SHA-1
2862 * (`0xFF02`). Since TLS 1.0 and 1.1 don't include a
2863 * SignatureAndHashAlgorithm field in their ServerKeyExchange
2864 * messages, any value below `0xFF00` will be usable to send the
2865 * raw ServerKeyExchange data to the `do_sign()` callback, but
2866 * that callback must still follow the protocol requirements
2867 * when generating the signature.
2872 * \brief Certificate chain to send to the client.
2874 * This is an array of `br_x509_certificate` objects, each
2875 * normally containing a DER-encoded certificate. The server
2876 * code does not try to decode these elements.
2878 const br_x509_certificate
*chain
;
2881 * \brief Certificate chain length (number of certificates).
2885 } br_ssl_server_choices
;
2888 * \brief Class type for a policy handler (server side).
2890 * A policy handler selects the policy parameters for a connection
2891 * (cipher suite and other algorithms, and certificate chain to send to
2892 * the client); it also performs the server-side computations involving
2893 * its permanent private key.
2895 * The SSL server engine will invoke first `choose()`, once the
2896 * ClientHello message has been received, then either `do_keyx()`
2897 * `do_sign()`, depending on the cipher suite.
2899 typedef struct br_ssl_server_policy_class_ br_ssl_server_policy_class
;
2900 struct br_ssl_server_policy_class_
{
2902 * \brief Context size (in bytes).
2904 size_t context_size
;
2907 * \brief Select algorithms and certificates for this connection.
2909 * This callback function shall fill the provided `choices`
2910 * structure with the policy choices for this connection. This
2911 * entails selecting the cipher suite, hash function for signing
2912 * the ServerKeyExchange (applicable only to ECDHE cipher suites),
2913 * and certificate chain to send.
2915 * The callback receives a pointer to the server context that
2916 * contains the relevant data. In particular, the functions
2917 * `br_ssl_server_get_client_suites()`,
2918 * `br_ssl_server_get_client_hashes()` and
2919 * `br_ssl_server_get_client_curves()` can be used to obtain
2920 * the cipher suites, hash functions and elliptic curves
2921 * supported by both the client and server, respectively. The
2922 * `br_ssl_engine_get_version()` and `br_ssl_engine_get_server_name()`
2923 * functions yield the protocol version and requested server name
2924 * (SNI), respectively.
2926 * This function may modify its context structure (`pctx`) in
2927 * arbitrary ways to keep track of its own choices.
2929 * This function shall return 1 if appropriate policy choices
2930 * could be made, or 0 if this connection cannot be pursued.
2932 * \param pctx policy context.
2933 * \param cc SSL server context.
2934 * \param choices destination structure for the policy choices.
2935 * \return 1 on success, 0 on error.
2937 int (*choose
)(const br_ssl_server_policy_class
**pctx
,
2938 const br_ssl_server_context
*cc
,
2939 br_ssl_server_choices
*choices
);
2942 * \brief Perform key exchange (server part).
2944 * This callback is invoked to perform the server-side cryptographic
2945 * operation for a key exchange that is not ECDHE. This callback
2946 * uses the private key.
2948 * **For RSA key exchange**, the provided `data` (of length `*len`
2949 * bytes) shall be decrypted with the server's private key, and
2950 * the 48-byte premaster secret copied back to the first 48 bytes
2953 * - The caller makes sure that `*len` is at least 59 bytes.
2955 * - This callback MUST check that the provided length matches
2956 * that of the key modulus; it shall report an error otherwise.
2958 * - If the length matches that of the RSA key modulus, then
2959 * processing MUST be constant-time, even if decryption fails,
2960 * or the padding is incorrect, or the plaintext message length
2961 * is not exactly 48 bytes.
2963 * - This callback needs not check the two first bytes of the
2964 * obtained pre-master secret (the caller will do that).
2966 * - If an error is reported (0), then what the callback put
2967 * in the first 48 bytes of `data` is unimportant (the caller
2968 * will use random bytes instead).
2970 * **For ECDH key exchange**, the provided `data` (of length `*len`
2971 * bytes) is the elliptic curve point from the client. The
2972 * callback shall multiply it with its private key, and store
2973 * the resulting X coordinate in `data`, starting at offset 0,
2974 * and set `*len` to the length of the X coordinate.
2976 * - If the input array does not have the proper length for
2977 * an encoded curve point, then an error (0) shall be reported.
2979 * - If the input array has the proper length, then processing
2980 * MUST be constant-time, even if the data is not a valid
2983 * - This callback MUST check that the input point is valid.
2985 * Returned value is 1 on success, 0 on error.
2987 * \param pctx policy context.
2988 * \param data key exchange data from the client.
2989 * \param len key exchange data length (in bytes).
2990 * \return 1 on success, 0 on error.
2992 uint32_t (*do_keyx
)(const br_ssl_server_policy_class
**pctx
,
2993 unsigned char *data
, size_t *len
);
2996 * \brief Perform a signature (for a ServerKeyExchange message).
2998 * This callback function is invoked for ECDHE cipher suites. On
2999 * input, the hash value or message to sign is in `data`, of
3000 * size `hv_len`; the involved hash function or algorithm is
3001 * identified by `algo_id`. The signature shall be computed and
3002 * written back into `data`; the total size of that buffer is
3005 * This callback shall verify that the signature length does not
3006 * exceed `len` bytes, and abstain from writing the signature if
3009 * The `algo_id` value matches that which was written in the
3010 * `choices` structures by the `choose()` callback. This will be
3011 * one of the following:
3013 * - `0xFF00 + id` for a hash function identifier `id`. In
3014 * that case, the `data` buffer contains a hash value
3015 * already computed over the data that is to be signed,
3016 * of length `hv_len`. The `id` may be 0 to designate the
3017 * special MD5+SHA-1 concatenation (old-style RSA signing).
3019 * - Another value, lower than `0xFF00`. The `data` buffer
3020 * then contains the raw, non-hashed data to be signed
3021 * (concatenation of the client and server randoms and
3022 * ECDH parameters). The callback is responsible to apply
3023 * any relevant hashing as part of the signing process.
3025 * Returned value is the signature length (in bytes), or 0 on error.
3027 * \param pctx policy context.
3028 * \param algo_id hash function / algorithm identifier.
3029 * \param data input/output buffer (message/hash, then signature).
3030 * \param hv_len hash value or message length (in bytes).
3031 * \param len total buffer length (in bytes).
3032 * \return signature length (in bytes) on success, or 0 on error.
3034 size_t (*do_sign
)(const br_ssl_server_policy_class
**pctx
,
3036 unsigned char *data
, size_t hv_len
, size_t len
);
3040 * \brief A single-chain RSA policy handler.
3042 * This policy context uses a single certificate chain, and a RSA
3043 * private key. The context can be restricted to only signatures or
3044 * only key exchange.
3046 * Apart from the first field (vtable pointer), its contents are
3047 * opaque and shall not be accessed directly.
3050 /** \brief Pointer to vtable. */
3051 const br_ssl_server_policy_class
*vtable
;
3052 #ifndef BR_DOXYGEN_IGNORE
3053 const br_x509_certificate
*chain
;
3055 const br_rsa_private_key
*sk
;
3056 unsigned allowed_usages
;
3057 br_rsa_private irsacore
;
3058 br_rsa_pkcs1_sign irsasign
;
3060 } br_ssl_server_policy_rsa_context
;
3063 * \brief A single-chain EC policy handler.
3065 * This policy context uses a single certificate chain, and an EC
3066 * private key. The context can be restricted to only signatures or
3067 * only key exchange.
3069 * Due to how TLS is defined, this context must be made aware whether
3070 * the server certificate was itself signed with RSA or ECDSA. The code
3071 * does not try to decode the certificate to obtain that information.
3073 * Apart from the first field (vtable pointer), its contents are
3074 * opaque and shall not be accessed directly.
3077 /** \brief Pointer to vtable. */
3078 const br_ssl_server_policy_class
*vtable
;
3079 #ifndef BR_DOXYGEN_IGNORE
3080 const br_x509_certificate
*chain
;
3082 const br_ec_private_key
*sk
;
3083 unsigned allowed_usages
;
3084 unsigned cert_issuer_key_type
;
3085 const br_multihash_context
*mhash
;
3086 const br_ec_impl
*iec
;
3087 br_ecdsa_sign iecdsa
;
3089 } br_ssl_server_policy_ec_context
;
3092 * \brief Class type for a session parameter cache.
3094 * Session parameters are saved in the cache with `save()`, and
3095 * retrieved with `load()`. The cache implementation can apply any
3096 * storage and eviction strategy that it sees fit. The SSL server
3097 * context that performs the request is provided, so that its
3098 * functionalities may be used by the implementation (e.g. hash
3099 * functions or random number generation).
3101 typedef struct br_ssl_session_cache_class_ br_ssl_session_cache_class
;
3102 struct br_ssl_session_cache_class_
{
3104 * \brief Context size (in bytes).
3106 size_t context_size
;
3109 * \brief Record a session.
3111 * This callback should record the provided session parameters.
3112 * The `params` structure is transient, so its contents shall
3113 * be copied into the cache. The session ID has been randomly
3114 * generated and always has length exactly 32 bytes.
3116 * \param ctx session cache context.
3117 * \param server_ctx SSL server context.
3118 * \param params session parameters to save.
3120 void (*save
)(const br_ssl_session_cache_class
**ctx
,
3121 br_ssl_server_context
*server_ctx
,
3122 const br_ssl_session_parameters
*params
);
3125 * \brief Lookup a session in the cache.
3127 * The session ID to lookup is in `params` and always has length
3128 * exactly 32 bytes. If the session parameters are found in the
3129 * cache, then the parameters shall be copied into the `params`
3130 * structure. Returned value is 1 on successful lookup, 0
3133 * \param ctx session cache context.
3134 * \param server_ctx SSL server context.
3135 * \param params destination for session parameters.
3136 * \return 1 if found, 0 otherwise.
3138 int (*load
)(const br_ssl_session_cache_class
**ctx
,
3139 br_ssl_server_context
*server_ctx
,
3140 br_ssl_session_parameters
*params
);
3144 * \brief Context for a basic cache system.
3146 * The system stores session parameters in a buffer provided at
3147 * initialisation time. Each entry uses exactly 100 bytes, and
3148 * buffer sizes up to 4294967295 bytes are supported.
3150 * Entries are evicted with a LRU (Least Recently Used) policy. A
3151 * search tree is maintained to keep lookups fast even with large
3154 * Apart from the first field (vtable pointer), the structure
3155 * contents are opaque and shall not be accessed directly.
3158 /** \brief Pointer to vtable. */
3159 const br_ssl_session_cache_class
*vtable
;
3160 #ifndef BR_DOXYGEN_IGNORE
3161 unsigned char *store
;
3162 size_t store_len
, store_ptr
;
3163 unsigned char index_key
[32];
3164 const br_hash_class
*hash
;
3166 uint32_t head
, tail
, root
;
3168 } br_ssl_session_cache_lru
;
3171 * \brief Initialise a LRU session cache with the provided storage space.
3173 * The provided storage space must remain valid as long as the cache
3174 * is used. Arbitrary lengths are supported, up to 4294967295 bytes;
3175 * each entry uses up exactly 100 bytes.
3177 * \param cc session cache context.
3178 * \param store storage space for cached entries.
3179 * \param store_len storage space length (in bytes).
3181 void br_ssl_session_cache_lru_init(br_ssl_session_cache_lru
*cc
,
3182 unsigned char *store
, size_t store_len
);
3185 * \brief Context structure for a SSL server.
3187 * The first field (called `eng`) is the SSL engine; all functions that
3188 * work on a `br_ssl_engine_context` structure shall take as parameter
3189 * a pointer to that field. The other structure fields are opaque and
3190 * must not be accessed directly.
3192 struct br_ssl_server_context_
{
3194 * \brief The encapsulated engine context.
3196 br_ssl_engine_context eng
;
3198 #ifndef BR_DOXYGEN_IGNORE
3200 * Maximum version from the client.
3202 uint16_t client_max_version
;
3207 const br_ssl_session_cache_class
**cache_vtable
;
3210 * Translated cipher suites supported by the client. The list
3211 * is trimmed to include only the cipher suites that the
3212 * server also supports; they are in the same order as in the
3215 br_suite_translated client_suites
[BR_MAX_CIPHER_SUITES
];
3216 unsigned char client_suites_num
;
3219 * Hash functions supported by the client, with ECDSA and RSA
3220 * (bit mask). For hash function with id 'x', set bit index is
3221 * x for RSA, x+8 for ECDSA. For newer algorithms, with ID
3222 * 0x08**, bit 16+k is set for algorithm 0x0800+k.
3227 * Curves supported by the client (bit mask, for named curves).
3232 * Context for chain handler.
3234 const br_ssl_server_policy_class
**policy_vtable
;
3235 uint16_t sign_hash_id
;
3238 * For the core handlers, thus avoiding (in most cases) the
3239 * need for an externally provided policy context.
3242 const br_ssl_server_policy_class
*vtable
;
3243 br_ssl_server_policy_rsa_context single_rsa
;
3244 br_ssl_server_policy_ec_context single_ec
;
3248 * Buffer for the ECDHE private key.
3250 unsigned char ecdhe_key
[70];
3251 size_t ecdhe_key_len
;
3254 * Trust anchor names for client authentication. "ta_names" and
3255 * "tas" cannot be both non-NULL.
3257 const br_x500_name
*ta_names
;
3258 const br_x509_trust_anchor
*tas
;
3260 size_t cur_dn_index
;
3261 const unsigned char *cur_dn
;
3265 * Buffer for the hash value computed over all handshake messages
3266 * prior to CertificateVerify, and identifier for the hash function.
3268 unsigned char hash_CV
[64];
3273 * Server-specific implementations.
3280 * Each br_ssl_server_init_xxx() function sets the list of supported
3281 * cipher suites and used implementations, as specified by the profile
3282 * name 'xxx'. Defined profile names are:
3284 * full_rsa all supported algorithm, server key type is RSA
3285 * full_ec all supported algorithm, server key type is EC
3286 * TODO: add other profiles
3288 * Naming scheme for "minimal" profiles: min123
3290 * -- character 1: key exchange
3296 * -- character 2: version / PRF
3297 * 0 = TLS 1.0 / 1.1 with MD5+SHA-1
3298 * 2 = TLS 1.2 with SHA-256
3299 * 3 = TLS 1.2 with SHA-384
3300 * -- character 3: encryption
3304 * c = ChaCha20+Poly1305
3308 * \brief SSL server profile: full_rsa.
3310 * This function initialises the provided SSL server context with
3311 * all supported algorithms and cipher suites that rely on a RSA
3314 * \param cc server context to initialise.
3315 * \param chain server certificate chain.
3316 * \param chain_len certificate chain length (number of certificate).
3317 * \param sk RSA private key.
3319 void br_ssl_server_init_full_rsa(br_ssl_server_context
*cc
,
3320 const br_x509_certificate
*chain
, size_t chain_len
,
3321 const br_rsa_private_key
*sk
);
3324 * \brief SSL server profile: full_ec.
3326 * This function initialises the provided SSL server context with
3327 * all supported algorithms and cipher suites that rely on an EC
3330 * The key type of the CA that issued the server's certificate must
3331 * be provided, since it matters for ECDH cipher suites (ECDH_RSA
3332 * suites require a RSA-powered CA). The key type is either
3333 * `BR_KEYTYPE_RSA` or `BR_KEYTYPE_EC`.
3335 * \param cc server context to initialise.
3336 * \param chain server certificate chain.
3337 * \param chain_len chain length (number of certificates).
3338 * \param cert_issuer_key_type certificate issuer's key type.
3339 * \param sk EC private key.
3341 void br_ssl_server_init_full_ec(br_ssl_server_context
*cc
,
3342 const br_x509_certificate
*chain
, size_t chain_len
,
3343 unsigned cert_issuer_key_type
, const br_ec_private_key
*sk
);
3346 * \brief SSL server profile: minr2g.
3348 * This profile uses only TLS_RSA_WITH_AES_128_GCM_SHA256. Server key is
3349 * RSA, and RSA key exchange is used (not forward secure, but uses little
3350 * CPU in the client).
3352 * \param cc server context to initialise.
3353 * \param chain server certificate chain.
3354 * \param chain_len certificate chain length (number of certificate).
3355 * \param sk RSA private key.
3357 void br_ssl_server_init_minr2g(br_ssl_server_context
*cc
,
3358 const br_x509_certificate
*chain
, size_t chain_len
,
3359 const br_rsa_private_key
*sk
);
3362 * \brief SSL server profile: mine2g.
3364 * This profile uses only TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256. Server key
3365 * is RSA, and ECDHE key exchange is used. This suite provides forward
3366 * security, with a higher CPU expense on the client, and a somewhat
3367 * larger code footprint (compared to "minr2g").
3369 * \param cc server context to initialise.
3370 * \param chain server certificate chain.
3371 * \param chain_len certificate chain length (number of certificate).
3372 * \param sk RSA private key.
3374 void br_ssl_server_init_mine2g(br_ssl_server_context
*cc
,
3375 const br_x509_certificate
*chain
, size_t chain_len
,
3376 const br_rsa_private_key
*sk
);
3379 * \brief SSL server profile: minf2g.
3381 * This profile uses only TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256.
3382 * Server key is EC, and ECDHE key exchange is used. This suite provides
3383 * forward security, with a higher CPU expense on the client and server
3384 * (by a factor of about 3 to 4), and a somewhat larger code footprint
3385 * (compared to "minu2g" and "minv2g").
3387 * \param cc server context to initialise.
3388 * \param chain server certificate chain.
3389 * \param chain_len certificate chain length (number of certificate).
3390 * \param sk EC private key.
3392 void br_ssl_server_init_minf2g(br_ssl_server_context
*cc
,
3393 const br_x509_certificate
*chain
, size_t chain_len
,
3394 const br_ec_private_key
*sk
);
3397 * \brief SSL server profile: minu2g.
3399 * This profile uses only TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256.
3400 * Server key is EC, and ECDH key exchange is used; the issuing CA used
3403 * The "minu2g" and "minv2g" profiles do not provide forward secrecy,
3404 * but are the lightest on the server (for CPU usage), and are rather
3405 * inexpensive on the client as well.
3407 * \param cc server context to initialise.
3408 * \param chain server certificate chain.
3409 * \param chain_len certificate chain length (number of certificate).
3410 * \param sk EC private key.
3412 void br_ssl_server_init_minu2g(br_ssl_server_context
*cc
,
3413 const br_x509_certificate
*chain
, size_t chain_len
,
3414 const br_ec_private_key
*sk
);
3417 * \brief SSL server profile: minv2g.
3419 * This profile uses only TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256.
3420 * Server key is EC, and ECDH key exchange is used; the issuing CA used
3423 * The "minu2g" and "minv2g" profiles do not provide forward secrecy,
3424 * but are the lightest on the server (for CPU usage), and are rather
3425 * inexpensive on the client as well.
3427 * \param cc server context to initialise.
3428 * \param chain server certificate chain.
3429 * \param chain_len certificate chain length (number of certificate).
3430 * \param sk EC private key.
3432 void br_ssl_server_init_minv2g(br_ssl_server_context
*cc
,
3433 const br_x509_certificate
*chain
, size_t chain_len
,
3434 const br_ec_private_key
*sk
);
3437 * \brief SSL server profile: mine2c.
3439 * This profile uses only TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256.
3440 * Server key is RSA, and ECDHE key exchange is used. This suite
3441 * provides forward security.
3443 * \param cc server context to initialise.
3444 * \param chain server certificate chain.
3445 * \param chain_len certificate chain length (number of certificate).
3446 * \param sk RSA private key.
3448 void br_ssl_server_init_mine2c(br_ssl_server_context
*cc
,
3449 const br_x509_certificate
*chain
, size_t chain_len
,
3450 const br_rsa_private_key
*sk
);
3453 * \brief SSL server profile: minf2c.
3455 * This profile uses only TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256.
3456 * Server key is EC, and ECDHE key exchange is used. This suite provides
3459 * \param cc server context to initialise.
3460 * \param chain server certificate chain.
3461 * \param chain_len certificate chain length (number of certificate).
3462 * \param sk EC private key.
3464 void br_ssl_server_init_minf2c(br_ssl_server_context
*cc
,
3465 const br_x509_certificate
*chain
, size_t chain_len
,
3466 const br_ec_private_key
*sk
);
3469 * \brief Get the supported client suites.
3471 * This function shall be called only after the ClientHello has been
3472 * processed, typically from the policy engine. The returned array
3473 * contains the cipher suites that are supported by both the client
3474 * and the server; these suites are in client preference order, unless
3475 * the `BR_OPT_ENFORCE_SERVER_PREFERENCES` flag was set, in which case
3476 * they are in server preference order.
3478 * The suites are _translated_, which means that each suite is given
3479 * as two 16-bit integers: the standard suite identifier, and its
3480 * translated version, broken down into its individual components,
3481 * as explained with the `br_suite_translated` type.
3483 * The returned array is allocated in the context and will be rewritten
3484 * by each handshake.
3486 * \param cc server context.
3487 * \param num receives the array size (number of suites).
3488 * \return the translated common cipher suites, in preference order.
3490 static inline const br_suite_translated
*
3491 br_ssl_server_get_client_suites(const br_ssl_server_context
*cc
, size_t *num
)
3493 *num
= cc
->client_suites_num
;
3494 return cc
->client_suites
;
3498 * \brief Get the hash functions and signature algorithms supported by
3501 * This value is a bit field:
3503 * - If RSA (PKCS#1 v1.5) is supported with hash function of ID `x`,
3504 * then bit `x` is set (hash function ID is 0 for the special MD5+SHA-1,
3505 * or 2 to 6 for the SHA family).
3507 * - If ECDSA is suported with hash function of ID `x`, then bit `8+x`
3510 * - Newer algorithms are symbolic 16-bit identifiers that do not
3511 * represent signature algorithm and hash function separately. If
3512 * the TLS-level identifier is `0x0800+x` for a `x` in the 0..15
3513 * range, then bit `16+x` is set.
3515 * "New algorithms" are currently defined only in draft documents, so
3516 * this support is subject to possible change. Right now (early 2017),
3517 * this maps ed25519 (EdDSA on Curve25519) to bit 23, and ed448 (EdDSA
3518 * on Curve448) to bit 24. If the identifiers on the wire change in
3519 * future document, then the decoding mechanism in BearSSL will be
3520 * amended to keep mapping ed25519 and ed448 on bits 23 and 24,
3521 * respectively. Mapping of other new algorithms (e.g. RSA/PSS) is not
3524 * \param cc server context.
3525 * \return the client-supported hash functions and signature algorithms.
3527 static inline uint32_t
3528 br_ssl_server_get_client_hashes(const br_ssl_server_context
*cc
)
3534 * \brief Get the elliptic curves supported by the client.
3536 * This is a bit field (bit x is set if curve of ID x is supported).
3538 * \param cc server context.
3539 * \return the client-supported elliptic curves.
3541 static inline uint32_t
3542 br_ssl_server_get_client_curves(const br_ssl_server_context
*cc
)
3548 * \brief Clear the complete contents of a SSL server context.
3550 * Everything is cleared, including the reference to the configured buffer,
3551 * implementations, cipher suites and state. This is a preparatory step
3552 * to assembling a custom profile.
3554 * \param cc server context to clear.
3556 void br_ssl_server_zero(br_ssl_server_context
*cc
);
3559 * \brief Set an externally provided policy context.
3561 * The policy context's methods are invoked to decide the cipher suite
3562 * and certificate chain, and to perform operations involving the server's
3565 * \param cc server context.
3566 * \param pctx policy context (pointer to its vtable field).
3569 br_ssl_server_set_policy(br_ssl_server_context
*cc
,
3570 const br_ssl_server_policy_class
**pctx
)
3572 cc
->policy_vtable
= pctx
;
3576 * \brief Set the server certificate chain and key (single RSA case).
3578 * This function uses a policy context included in the server context.
3579 * It configures use of a single server certificate chain with a RSA
3580 * private key. The `allowed_usages` is a combination of usages, namely
3581 * `BR_KEYTYPE_KEYX` and/or `BR_KEYTYPE_SIGN`; this enables or disables
3582 * the corresponding cipher suites (i.e. `TLS_RSA_*` use the RSA key for
3583 * key exchange, while `TLS_ECDHE_RSA_*` use the RSA key for signatures).
3585 * \param cc server context.
3586 * \param chain server certificate chain to send to the client.
3587 * \param chain_len chain length (number of certificates).
3588 * \param sk server private key (RSA).
3589 * \param allowed_usages allowed private key usages.
3590 * \param irsacore RSA core implementation.
3591 * \param irsasign RSA signature implementation (PKCS#1 v1.5).
3593 void br_ssl_server_set_single_rsa(br_ssl_server_context
*cc
,
3594 const br_x509_certificate
*chain
, size_t chain_len
,
3595 const br_rsa_private_key
*sk
, unsigned allowed_usages
,
3596 br_rsa_private irsacore
, br_rsa_pkcs1_sign irsasign
);
3599 * \brief Set the server certificate chain and key (single EC case).
3601 * This function uses a policy context included in the server context.
3602 * It configures use of a single server certificate chain with an EC
3603 * private key. The `allowed_usages` is a combination of usages, namely
3604 * `BR_KEYTYPE_KEYX` and/or `BR_KEYTYPE_SIGN`; this enables or disables
3605 * the corresponding cipher suites (i.e. `TLS_ECDH_*` use the EC key for
3606 * key exchange, while `TLS_ECDHE_ECDSA_*` use the EC key for signatures).
3608 * In order to support `TLS_ECDH_*` cipher suites (non-ephemeral ECDH),
3609 * the algorithm type of the key used by the issuing CA to sign the
3610 * server's certificate must be provided, as `cert_issuer_key_type`
3611 * parameter (this value is either `BR_KEYTYPE_RSA` or `BR_KEYTYPE_EC`).
3613 * \param cc server context.
3614 * \param chain server certificate chain to send.
3615 * \param chain_len chain length (number of certificates).
3616 * \param sk server private key (EC).
3617 * \param allowed_usages allowed private key usages.
3618 * \param cert_issuer_key_type issuing CA's key type.
3619 * \param iec EC core implementation.
3620 * \param iecdsa ECDSA signature implementation ("asn1" format).
3622 void br_ssl_server_set_single_ec(br_ssl_server_context
*cc
,
3623 const br_x509_certificate
*chain
, size_t chain_len
,
3624 const br_ec_private_key
*sk
, unsigned allowed_usages
,
3625 unsigned cert_issuer_key_type
,
3626 const br_ec_impl
*iec
, br_ecdsa_sign iecdsa
);
3629 * \brief Activate client certificate authentication.
3631 * The trust anchor encoded X.500 names (DN) to send to the client are
3632 * provided. A client certificate will be requested and validated through
3633 * the X.509 validator configured in the SSL engine. If `num` is 0, then
3634 * client certificate authentication is disabled.
3636 * If the client does not send a certificate, or on validation failure,
3637 * the handshake aborts. Unauthenticated clients can be tolerated by
3638 * setting the `BR_OPT_TOLERATE_NO_CLIENT_AUTH` flag.
3640 * The provided array is linked in, not copied, so that pointer must
3641 * remain valid as long as anchor names may be used.
3643 * \param cc server context.
3644 * \param ta_names encoded trust anchor names.
3645 * \param num number of encoded trust anchor names.
3648 br_ssl_server_set_trust_anchor_names(br_ssl_server_context
*cc
,
3649 const br_x500_name
*ta_names
, size_t num
)
3651 cc
->ta_names
= ta_names
;
3657 * \brief Activate client certificate authentication.
3659 * This is a variant for `br_ssl_server_set_trust_anchor_names()`: the
3660 * trust anchor names are provided not as an array of stand-alone names
3661 * (`br_x500_name` structures), but as an array of trust anchors
3662 * (`br_x509_trust_anchor` structures). The server engine itself will
3663 * only use the `dn` field of each trust anchor. This is meant to allow
3664 * defining a single array of trust anchors, to be used here and in the
3665 * X.509 validation engine itself.
3667 * The provided array is linked in, not copied, so that pointer must
3668 * remain valid as long as anchor names may be used.
3670 * \param cc server context.
3671 * \param tas trust anchors (only names are used).
3672 * \param num number of trust anchors.
3675 br_ssl_server_set_trust_anchor_names_alt(br_ssl_server_context
*cc
,
3676 const br_x509_trust_anchor
*tas
, size_t num
)
3678 cc
->ta_names
= NULL
;
3684 * \brief Configure the cache for session parameters.
3686 * The cache context is provided as a pointer to its first field (vtable
3689 * \param cc server context.
3690 * \param vtable session cache context.
3693 br_ssl_server_set_cache(br_ssl_server_context
*cc
,
3694 const br_ssl_session_cache_class
**vtable
)
3696 cc
->cache_vtable
= vtable
;
3700 * \brief Prepare or reset a server context for handling an incoming client.
3702 * \param cc server context.
3703 * \return 1 on success, 0 on error.
3705 int br_ssl_server_reset(br_ssl_server_context
*cc
);
3707 /* ===================================================================== */
3710 * Context for the simplified I/O context. The transport medium is accessed
3711 * through the low_read() and low_write() callback functions, each with
3712 * its own opaque context pointer.
3714 * low_read() read some bytes, at most 'len' bytes, into data[]. The
3715 * returned value is the number of read bytes, or -1 on error.
3716 * The 'len' parameter is guaranteed never to exceed 20000,
3717 * so the length always fits in an 'int' on all platforms.
3719 * low_write() write up to 'len' bytes, to be read from data[]. The
3720 * returned value is the number of written bytes, or -1 on
3721 * error. The 'len' parameter is guaranteed never to exceed
3722 * 20000, so the length always fits in an 'int' on all
3725 * A socket closure (if the transport medium is a socket) should be reported
3726 * as an error (-1). The callbacks shall endeavour to block until at least
3727 * one byte can be read or written; a callback returning 0 at times is
3728 * acceptable, but this normally leads to the callback being immediately
3729 * called again, so the callback should at least always try to block for
3730 * some time if no I/O can take place.
3732 * The SSL engine naturally applies some buffering, so the callbacks need
3733 * not apply buffers of their own.
3736 * \brief Context structure for the simplified SSL I/O wrapper.
3738 * This structure is initialised with `br_sslio_init()`. Its contents
3739 * are opaque and shall not be accessed directly.
3742 #ifndef BR_DOXYGEN_IGNORE
3743 br_ssl_engine_context
*engine
;
3744 int (*low_read
)(void *read_context
,
3745 unsigned char *data
, size_t len
);
3747 int (*low_write
)(void *write_context
,
3748 const unsigned char *data
, size_t len
);
3749 void *write_context
;
3754 * \brief Initialise a simplified I/O wrapper context.
3756 * The simplified I/O wrapper offers a simpler read/write API for a SSL
3757 * engine (client or server), using the provided callback functions for
3758 * reading data from, or writing data to, the transport medium.
3760 * The callback functions have the following semantics:
3762 * - Each callback receives an opaque context value (of type `void *`)
3763 * that the callback may use arbitrarily (or possibly ignore).
3765 * - `low_read()` reads at least one byte, at most `len` bytes, from
3766 * the transport medium. Read bytes shall be written in `data`.
3768 * - `low_write()` writes at least one byte, at most `len` bytes, unto
3769 * the transport medium. The bytes to write are read from `data`.
3771 * - The `len` parameter is never zero, and is always lower than 20000.
3773 * - The number of processed bytes (read or written) is returned. Since
3774 * that number is less than 20000, it always fits on an `int`.
3776 * - On error, the callbacks return -1. Reaching end-of-stream is an
3777 * error. Errors are permanent: the SSL connection is terminated.
3779 * - Callbacks SHOULD NOT return 0. This is tolerated, as long as
3780 * callbacks endeavour to block for some non-negligible amount of
3781 * time until at least one byte can be sent or received (if a
3782 * callback returns 0, then the wrapper invokes it again
3785 * - Callbacks MAY return as soon as at least one byte is processed;
3786 * they MAY also insist on reading or writing _all_ requested bytes.
3787 * Since SSL is a self-terminated protocol (each record has a length
3788 * header), this does not change semantics.
3790 * - Callbacks need not apply any buffering (for performance) since SSL
3791 * itself uses buffers.
3793 * \param ctx wrapper context to initialise.
3794 * \param engine SSL engine to wrap.
3795 * \param low_read callback for reading data from the transport.
3796 * \param read_context context pointer for `low_read()`.
3797 * \param low_write callback for writing data on the transport.
3798 * \param write_context context pointer for `low_write()`.
3800 void br_sslio_init(br_sslio_context
*ctx
,
3801 br_ssl_engine_context
*engine
,
3802 int (*low_read
)(void *read_context
,
3803 unsigned char *data
, size_t len
),
3805 int (*low_write
)(void *write_context
,
3806 const unsigned char *data
, size_t len
),
3807 void *write_context
);
3810 * \brief Read some application data from a SSL connection.
3812 * If `len` is zero, then this function returns 0 immediately. In
3813 * all other cases, it never returns 0.
3815 * This call returns only when at least one byte has been obtained.
3816 * Returned value is the number of bytes read, or -1 on error. The
3817 * number of bytes always fits on an 'int' (data from a single SSL/TLS
3818 * record is returned).
3820 * On error or SSL closure, this function returns -1. The caller should
3821 * inspect the error status on the SSL engine to distinguish between
3822 * normal closure and error.
3824 * \param cc SSL wrapper context.
3825 * \param dst destination buffer for application data.
3826 * \param len maximum number of bytes to obtain.
3827 * \return number of bytes obtained, or -1 on error.
3829 int br_sslio_read(br_sslio_context
*cc
, void *dst
, size_t len
);
3832 * \brief Read application data from a SSL connection.
3834 * This calls returns only when _all_ requested `len` bytes are read,
3835 * or an error is reached. Returned value is 0 on success, -1 on error.
3836 * A normal (verified) SSL closure before that many bytes are obtained
3837 * is reported as an error by this function.
3839 * \param cc SSL wrapper context.
3840 * \param dst destination buffer for application data.
3841 * \param len number of bytes to obtain.
3842 * \return 0 on success, or -1 on error.
3844 int br_sslio_read_all(br_sslio_context
*cc
, void *dst
, size_t len
);
3847 * \brief Write some application data unto a SSL connection.
3849 * If `len` is zero, then this function returns 0 immediately. In
3850 * all other cases, it never returns 0.
3852 * This call returns only when at least one byte has been written.
3853 * Returned value is the number of bytes written, or -1 on error. The
3854 * number of bytes always fits on an 'int' (less than 20000).
3856 * On error or SSL closure, this function returns -1. The caller should
3857 * inspect the error status on the SSL engine to distinguish between
3858 * normal closure and error.
3860 * **Important:** SSL is buffered; a "written" byte is a byte that was
3861 * injected into the wrapped SSL engine, but this does not necessarily mean
3862 * that it has been scheduled for sending. Use `br_sslio_flush()` to
3863 * ensure that all pending data has been sent to the transport medium.
3865 * \param cc SSL wrapper context.
3866 * \param src source buffer for application data.
3867 * \param len maximum number of bytes to write.
3868 * \return number of bytes written, or -1 on error.
3870 int br_sslio_write(br_sslio_context
*cc
, const void *src
, size_t len
);
3873 * \brief Write application data unto a SSL connection.
3875 * This calls returns only when _all_ requested `len` bytes have been
3876 * written, or an error is reached. Returned value is 0 on success, -1
3877 * on error. A normal (verified) SSL closure before that many bytes are
3878 * written is reported as an error by this function.
3880 * **Important:** SSL is buffered; a "written" byte is a byte that was
3881 * injected into the wrapped SSL engine, but this does not necessarily mean
3882 * that it has been scheduled for sending. Use `br_sslio_flush()` to
3883 * ensure that all pending data has been sent to the transport medium.
3885 * \param cc SSL wrapper context.
3886 * \param src source buffer for application data.
3887 * \param len number of bytes to write.
3888 * \return 0 on success, or -1 on error.
3890 int br_sslio_write_all(br_sslio_context
*cc
, const void *src
, size_t len
);
3893 * \brief Flush pending data.
3895 * This call makes sure that any buffered application data in the
3896 * provided context (including the wrapped SSL engine) has been sent
3897 * to the transport medium (i.e. accepted by the `low_write()` callback
3898 * method). If there is no such pending data, then this function does
3899 * nothing (and returns a success, i.e. 0).
3901 * If the underlying transport medium has its own buffers, then it is
3902 * up to the caller to ensure the corresponding flushing.
3904 * Returned value is 0 on success, -1 on error.
3906 * \param cc SSL wrapper context.
3907 * \return 0 on success, or -1 on error.
3909 int br_sslio_flush(br_sslio_context
*cc
);
3912 * \brief Close the SSL connection.
3914 * This call runs the SSL closure protocol (sending a `close_notify`,
3915 * receiving the response `close_notify`). When it returns, the SSL
3916 * connection is finished. It is still up to the caller to manage the
3917 * possible transport-level termination, if applicable (alternatively,
3918 * the underlying transport stream may be reused for non-SSL messages).
3920 * Returned value is 0 on success, -1 on error. A failure by the peer
3921 * to process the complete closure protocol (i.e. sending back the
3922 * `close_notify`) is an error.
3924 * \param cc SSL wrapper context.
3925 * \return 0 on success, or -1 on error.
3927 int br_sslio_close(br_sslio_context
*cc
);
3929 /* ===================================================================== */
3932 * Symbolic constants for cipher suites.
3936 #define BR_TLS_NULL_WITH_NULL_NULL 0x0000
3937 #define BR_TLS_RSA_WITH_NULL_MD5 0x0001
3938 #define BR_TLS_RSA_WITH_NULL_SHA 0x0002
3939 #define BR_TLS_RSA_WITH_NULL_SHA256 0x003B
3940 #define BR_TLS_RSA_WITH_RC4_128_MD5 0x0004
3941 #define BR_TLS_RSA_WITH_RC4_128_SHA 0x0005
3942 #define BR_TLS_RSA_WITH_3DES_EDE_CBC_SHA 0x000A
3943 #define BR_TLS_RSA_WITH_AES_128_CBC_SHA 0x002F
3944 #define BR_TLS_RSA_WITH_AES_256_CBC_SHA 0x0035
3945 #define BR_TLS_RSA_WITH_AES_128_CBC_SHA256 0x003C
3946 #define BR_TLS_RSA_WITH_AES_256_CBC_SHA256 0x003D
3947 #define BR_TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA 0x000D
3948 #define BR_TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA 0x0010
3949 #define BR_TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA 0x0013
3950 #define BR_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA 0x0016
3951 #define BR_TLS_DH_DSS_WITH_AES_128_CBC_SHA 0x0030
3952 #define BR_TLS_DH_RSA_WITH_AES_128_CBC_SHA 0x0031
3953 #define BR_TLS_DHE_DSS_WITH_AES_128_CBC_SHA 0x0032
3954 #define BR_TLS_DHE_RSA_WITH_AES_128_CBC_SHA 0x0033
3955 #define BR_TLS_DH_DSS_WITH_AES_256_CBC_SHA 0x0036
3956 #define BR_TLS_DH_RSA_WITH_AES_256_CBC_SHA 0x0037
3957 #define BR_TLS_DHE_DSS_WITH_AES_256_CBC_SHA 0x0038
3958 #define BR_TLS_DHE_RSA_WITH_AES_256_CBC_SHA 0x0039
3959 #define BR_TLS_DH_DSS_WITH_AES_128_CBC_SHA256 0x003E
3960 #define BR_TLS_DH_RSA_WITH_AES_128_CBC_SHA256 0x003F
3961 #define BR_TLS_DHE_DSS_WITH_AES_128_CBC_SHA256 0x0040
3962 #define BR_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 0x0067
3963 #define BR_TLS_DH_DSS_WITH_AES_256_CBC_SHA256 0x0068
3964 #define BR_TLS_DH_RSA_WITH_AES_256_CBC_SHA256 0x0069
3965 #define BR_TLS_DHE_DSS_WITH_AES_256_CBC_SHA256 0x006A
3966 #define BR_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 0x006B
3967 #define BR_TLS_DH_anon_WITH_RC4_128_MD5 0x0018
3968 #define BR_TLS_DH_anon_WITH_3DES_EDE_CBC_SHA 0x001B
3969 #define BR_TLS_DH_anon_WITH_AES_128_CBC_SHA 0x0034
3970 #define BR_TLS_DH_anon_WITH_AES_256_CBC_SHA 0x003A
3971 #define BR_TLS_DH_anon_WITH_AES_128_CBC_SHA256 0x006C
3972 #define BR_TLS_DH_anon_WITH_AES_256_CBC_SHA256 0x006D
3975 #define BR_TLS_ECDH_ECDSA_WITH_NULL_SHA 0xC001
3976 #define BR_TLS_ECDH_ECDSA_WITH_RC4_128_SHA 0xC002
3977 #define BR_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA 0xC003
3978 #define BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA 0xC004
3979 #define BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA 0xC005
3980 #define BR_TLS_ECDHE_ECDSA_WITH_NULL_SHA 0xC006
3981 #define BR_TLS_ECDHE_ECDSA_WITH_RC4_128_SHA 0xC007
3982 #define BR_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA 0xC008
3983 #define BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA 0xC009
3984 #define BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA 0xC00A
3985 #define BR_TLS_ECDH_RSA_WITH_NULL_SHA 0xC00B
3986 #define BR_TLS_ECDH_RSA_WITH_RC4_128_SHA 0xC00C
3987 #define BR_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA 0xC00D
3988 #define BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA 0xC00E
3989 #define BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA 0xC00F
3990 #define BR_TLS_ECDHE_RSA_WITH_NULL_SHA 0xC010
3991 #define BR_TLS_ECDHE_RSA_WITH_RC4_128_SHA 0xC011
3992 #define BR_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA 0xC012
3993 #define BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA 0xC013
3994 #define BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA 0xC014
3995 #define BR_TLS_ECDH_anon_WITH_NULL_SHA 0xC015
3996 #define BR_TLS_ECDH_anon_WITH_RC4_128_SHA 0xC016
3997 #define BR_TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA 0xC017
3998 #define BR_TLS_ECDH_anon_WITH_AES_128_CBC_SHA 0xC018
3999 #define BR_TLS_ECDH_anon_WITH_AES_256_CBC_SHA 0xC019
4002 #define BR_TLS_RSA_WITH_AES_128_GCM_SHA256 0x009C
4003 #define BR_TLS_RSA_WITH_AES_256_GCM_SHA384 0x009D
4004 #define BR_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 0x009E
4005 #define BR_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 0x009F
4006 #define BR_TLS_DH_RSA_WITH_AES_128_GCM_SHA256 0x00A0
4007 #define BR_TLS_DH_RSA_WITH_AES_256_GCM_SHA384 0x00A1
4008 #define BR_TLS_DHE_DSS_WITH_AES_128_GCM_SHA256 0x00A2
4009 #define BR_TLS_DHE_DSS_WITH_AES_256_GCM_SHA384 0x00A3
4010 #define BR_TLS_DH_DSS_WITH_AES_128_GCM_SHA256 0x00A4
4011 #define BR_TLS_DH_DSS_WITH_AES_256_GCM_SHA384 0x00A5
4012 #define BR_TLS_DH_anon_WITH_AES_128_GCM_SHA256 0x00A6
4013 #define BR_TLS_DH_anon_WITH_AES_256_GCM_SHA384 0x00A7
4016 #define BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 0xC023
4017 #define BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 0xC024
4018 #define BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 0xC025
4019 #define BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 0xC026
4020 #define BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 0xC027
4021 #define BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 0xC028
4022 #define BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 0xC029
4023 #define BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 0xC02A
4024 #define BR_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 0xC02B
4025 #define BR_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 0xC02C
4026 #define BR_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 0xC02D
4027 #define BR_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 0xC02E
4028 #define BR_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 0xC02F
4029 #define BR_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 0xC030
4030 #define BR_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 0xC031
4031 #define BR_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 0xC032
4034 #define BR_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 0xCCA8
4035 #define BR_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 0xCCA9
4036 #define BR_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 0xCCAA
4037 #define BR_TLS_PSK_WITH_CHACHA20_POLY1305_SHA256 0xCCAB
4038 #define BR_TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 0xCCAC
4039 #define BR_TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 0xCCAD
4040 #define BR_TLS_RSA_PSK_WITH_CHACHA20_POLY1305_SHA256 0xCCAE
4043 #define BR_TLS_FALLBACK_SCSV 0x5600
4046 * Symbolic constants for alerts.
4048 #define BR_ALERT_CLOSE_NOTIFY 0
4049 #define BR_ALERT_UNEXPECTED_MESSAGE 10
4050 #define BR_ALERT_BAD_RECORD_MAC 20
4051 #define BR_ALERT_RECORD_OVERFLOW 22
4052 #define BR_ALERT_DECOMPRESSION_FAILURE 30
4053 #define BR_ALERT_HANDSHAKE_FAILURE 40
4054 #define BR_ALERT_BAD_CERTIFICATE 42
4055 #define BR_ALERT_UNSUPPORTED_CERTIFICATE 43
4056 #define BR_ALERT_CERTIFICATE_REVOKED 44
4057 #define BR_ALERT_CERTIFICATE_EXPIRED 45
4058 #define BR_ALERT_CERTIFICATE_UNKNOWN 46
4059 #define BR_ALERT_ILLEGAL_PARAMETER 47
4060 #define BR_ALERT_UNKNOWN_CA 48
4061 #define BR_ALERT_ACCESS_DENIED 49
4062 #define BR_ALERT_DECODE_ERROR 50
4063 #define BR_ALERT_DECRYPT_ERROR 51
4064 #define BR_ALERT_PROTOCOL_VERSION 70
4065 #define BR_ALERT_INSUFFICIENT_SECURITY 71
4066 #define BR_ALERT_INTERNAL_ERROR 80
4067 #define BR_ALERT_USER_CANCELED 90
4068 #define BR_ALERT_NO_RENEGOTIATION 100
4069 #define BR_ALERT_UNSUPPORTED_EXTENSION 110
4070 #define BR_ALERT_NO_APPLICATION_PROTOCOL 120