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"
38 /** \file bearssl_ssl.h
42 * For an overview of the SSL/TLS API, see [the BearSSL Web
43 * site](https://www.bearssl.org/api1.html).
45 * The `BR_TLS_*` constants correspond to the standard cipher suites and
46 * their values in the [IANA
47 * registry](http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-4).
49 * The `BR_ALERT_*` constants are for standard TLS alert messages. When
50 * a fatal alert message is sent of received, then the SSL engine context
51 * status is set to the sum of that alert value (an integer in the 0..255
52 * range) and a fixed offset (`BR_ERR_SEND_FATAL_ALERT` for a sent alert,
53 * `BR_ERR_RECV_FATAL_ALERT` for a received alert).
56 /** \brief Optimal input buffer size. */
57 #define BR_SSL_BUFSIZE_INPUT (16384 + 325)
59 /** \brief Optimal output buffer size. */
60 #define BR_SSL_BUFSIZE_OUTPUT (16384 + 85)
62 /** \brief Optimal buffer size for monodirectional engine
63 (shared input/output buffer). */
64 #define BR_SSL_BUFSIZE_MONO BR_SSL_BUFSIZE_INPUT
66 /** \brief Optimal buffer size for bidirectional engine
67 (single buffer split into two separate input/output buffers). */
68 #define BR_SSL_BUFSIZE_BIDI (BR_SSL_BUFSIZE_INPUT + BR_SSL_BUFSIZE_OUTPUT)
71 * Constants for known SSL/TLS protocol versions (SSL 3.0, TLS 1.0, TLS 1.1
72 * and TLS 1.2). Note that though there is a constant for SSL 3.0, that
73 * protocol version is not actually supported.
76 /** \brief Protocol version: SSL 3.0 (unsupported). */
77 #define BR_SSL30 0x0300
78 /** \brief Protocol version: TLS 1.0. */
79 #define BR_TLS10 0x0301
80 /** \brief Protocol version: TLS 1.1. */
81 #define BR_TLS11 0x0302
82 /** \brief Protocol version: TLS 1.2. */
83 #define BR_TLS12 0x0303
86 * Error constants. They are used to report the reason why a context has
87 * been marked as failed.
89 * Implementation note: SSL-level error codes should be in the 1..31
90 * range. The 32..63 range is for certificate decoding and validation
91 * errors. Received fatal alerts imply an error code in the 256..511 range.
94 /** \brief SSL status: no error so far (0). */
97 /** \brief SSL status: caller-provided parameter is incorrect. */
98 #define BR_ERR_BAD_PARAM 1
100 /** \brief SSL status: operation requested by the caller cannot be applied
101 with the current context state (e.g. reading data while outgoing data
102 is waiting to be sent). */
103 #define BR_ERR_BAD_STATE 2
105 /** \brief SSL status: incoming protocol or record version is unsupported. */
106 #define BR_ERR_UNSUPPORTED_VERSION 3
108 /** \brief SSL status: incoming record version does not match the expected
110 #define BR_ERR_BAD_VERSION 4
112 /** \brief SSL status: incoming record length is invalid. */
113 #define BR_ERR_BAD_LENGTH 5
115 /** \brief SSL status: incoming record is too large to be processed, or
116 buffer is too small for the handshake message to send. */
117 #define BR_ERR_TOO_LARGE 6
119 /** \brief SSL status: decryption found an invalid padding, or the record
120 MAC is not correct. */
121 #define BR_ERR_BAD_MAC 7
123 /** \brief SSL status: no initial entropy was provided, and none can be
124 obtained from the OS. */
125 #define BR_ERR_NO_RANDOM 8
127 /** \brief SSL status: incoming record type is unknown. */
128 #define BR_ERR_UNKNOWN_TYPE 9
130 /** \brief SSL status: incoming record or message has wrong type with
131 regards to the current engine state. */
132 #define BR_ERR_UNEXPECTED 10
134 /** \brief SSL status: ChangeCipherSpec message from the peer has invalid
136 #define BR_ERR_BAD_CCS 12
138 /** \brief SSL status: alert message from the peer has invalid contents
140 #define BR_ERR_BAD_ALERT 13
142 /** \brief SSL status: incoming handshake message decoding failed. */
143 #define BR_ERR_BAD_HANDSHAKE 14
145 /** \brief SSL status: ServerHello contains a session ID which is larger
147 #define BR_ERR_OVERSIZED_ID 15
149 /** \brief SSL status: server wants to use a cipher suite that we did
150 not claim to support. This is also reported if we tried to advertise
151 a cipher suite that we do not support. */
152 #define BR_ERR_BAD_CIPHER_SUITE 16
154 /** \brief SSL status: server wants to use a compression that we did not
156 #define BR_ERR_BAD_COMPRESSION 17
158 /** \brief SSL status: server's max fragment length does not match
160 #define BR_ERR_BAD_FRAGLEN 18
162 /** \brief SSL status: secure renegotiation failed. */
163 #define BR_ERR_BAD_SECRENEG 19
165 /** \brief SSL status: server sent an extension type that we did not
166 announce, or used the same extension type several times in a single
168 #define BR_ERR_EXTRA_EXTENSION 20
170 /** \brief SSL status: invalid Server Name Indication contents (when
171 used by the server, this extension shall be empty). */
172 #define BR_ERR_BAD_SNI 21
174 /** \brief SSL status: invalid ServerHelloDone from the server (length
176 #define BR_ERR_BAD_HELLO_DONE 22
178 /** \brief SSL status: internal limit exceeded (e.g. server's public key
180 #define BR_ERR_LIMIT_EXCEEDED 23
182 /** \brief SSL status: Finished message from peer does not match the
184 #define BR_ERR_BAD_FINISHED 24
186 /** \brief SSL status: session resumption attempt with distinct version
188 #define BR_ERR_RESUME_MISMATCH 25
190 /** \brief SSL status: unsupported or invalid algorithm (ECDHE curve,
191 signature algorithm, hash function). */
192 #define BR_ERR_INVALID_ALGORITHM 26
194 /** \brief SSL status: invalid signature (on ServerKeyExchange from
195 server, or in CertificateVerify from client). */
196 #define BR_ERR_BAD_SIGNATURE 27
198 /** \brief SSL status: peer's public key does not have the proper type
199 or is not allowed for requested operation. */
200 #define BR_ERR_WRONG_KEY_USAGE 28
202 /** \brief SSL status: client did not send a certificate upon request,
203 or the client certificate could not be validated. */
204 #define BR_ERR_NO_CLIENT_AUTH 29
206 /** \brief SSL status: I/O error or premature close on underlying
207 transport stream. This error code is set only by the simplified
208 I/O API ("br_sslio_*"). */
211 /** \brief SSL status: base value for a received fatal alert.
213 When a fatal alert is received from the peer, the alert value
214 is added to this constant. */
215 #define BR_ERR_RECV_FATAL_ALERT 256
217 /** \brief SSL status: base value for a sent fatal alert.
219 When a fatal alert is sent to the peer, the alert value is added
221 #define BR_ERR_SEND_FATAL_ALERT 512
223 /* ===================================================================== */
226 * \brief Decryption engine for SSL.
228 * When processing incoming records, the SSL engine will use a decryption
229 * engine that uses a specific context structure, and has a set of
230 * methods (a vtable) that follows this template.
232 * The decryption engine is responsible for applying decryption, verifying
233 * MAC, and keeping track of the record sequence number.
235 typedef struct br_sslrec_in_class_ br_sslrec_in_class
;
236 struct br_sslrec_in_class_
{
238 * \brief Context size (in bytes).
243 * \brief Test validity of the incoming record length.
245 * This function returns 1 if the announced length for an
246 * incoming record is valid, 0 otherwise,
248 * \param ctx decryption engine context.
249 * \param record_len incoming record length.
250 * \return 1 of a valid length, 0 otherwise.
252 int (*check_length
)(const br_sslrec_in_class
*const *ctx
,
256 * \brief Decrypt the incoming record.
258 * This function may assume that the record length is valid
259 * (it has been previously tested with `check_length()`).
260 * Decryption is done in place; `*len` is updated with the
261 * cleartext length, and the address of the first plaintext
262 * byte is returned. If the record is correct but empty, then
263 * `*len` is set to 0 and a non-`NULL` pointer is returned.
265 * On decryption/MAC error, `NULL` is returned.
267 * \param ctx decryption engine context.
268 * \param record_type record type (23 for application data, etc).
269 * \param version record version.
270 * \param payload address of encrypted payload.
271 * \param len pointer to payload length (updated).
272 * \return pointer to plaintext, or `NULL` on error.
274 unsigned char *(*decrypt
)(const br_sslrec_in_class
**ctx
,
275 int record_type
, unsigned version
,
276 void *payload
, size_t *len
);
280 * \brief Encryption engine for SSL.
282 * When building outgoing records, the SSL engine will use an encryption
283 * engine that uses a specific context structure, and has a set of
284 * methods (a vtable) that follows this template.
286 * The encryption engine is responsible for applying encryption and MAC,
287 * and keeping track of the record sequence number.
289 typedef struct br_sslrec_out_class_ br_sslrec_out_class
;
290 struct br_sslrec_out_class_
{
292 * \brief Context size (in bytes).
297 * \brief Compute maximum plaintext sizes and offsets.
299 * When this function is called, the `*start` and `*end`
300 * values contain offsets designating the free area in the
301 * outgoing buffer for plaintext data; that free area is
302 * preceded by a 5-byte space which will receive the record
305 * The `max_plaintext()` function is responsible for adjusting
306 * both `*start` and `*end` to make room for any record-specific
307 * header, MAC, padding, and possible split.
309 * \param ctx encryption engine context.
310 * \param start pointer to start of plaintext offset (updated).
311 * \param end pointer to start of plaintext offset (updated).
313 void (*max_plaintext
)(const br_sslrec_out_class
*const *ctx
,
314 size_t *start
, size_t *end
);
317 * \brief Perform record encryption.
319 * This function encrypts the record. The plaintext address and
320 * length are provided. Returned value is the start of the
321 * encrypted record (or sequence of records, if a split was
322 * performed), _including_ the 5-byte header, and `*len` is
323 * adjusted to the total size of the record(s), there again
324 * including the header(s).
326 * \param ctx decryption engine context.
327 * \param record_type record type (23 for application data, etc).
328 * \param version record version.
329 * \param plaintext address of plaintext.
330 * \param len pointer to plaintext length (updated).
331 * \return pointer to start of built record.
333 unsigned char *(*encrypt
)(const br_sslrec_out_class
**ctx
,
334 int record_type
, unsigned version
,
335 void *plaintext
, size_t *len
);
339 * \brief Context for a no-encryption engine.
341 * The no-encryption engine processes outgoing records during the initial
342 * handshake, before encryption is applied.
345 /** \brief No-encryption engine vtable. */
346 const br_sslrec_out_class
*vtable
;
347 } br_sslrec_out_clear_context
;
349 /** \brief Static, constant vtable for the no-encryption engine. */
350 extern const br_sslrec_out_class br_sslrec_out_clear_vtable
;
352 /* ===================================================================== */
355 * \brief Record decryption engine class, for CBC mode.
357 * This class type extends the decryption engine class with an
358 * initialisation method that receives the parameters needed
359 * for CBC processing: block cipher implementation, block cipher key,
360 * HMAC parameters (hash function, key, MAC length), and IV. If the
361 * IV is `NULL`, then a per-record IV will be used (TLS 1.1+).
363 typedef struct br_sslrec_in_cbc_class_ br_sslrec_in_cbc_class
;
364 struct br_sslrec_in_cbc_class_
{
366 * \brief Superclass, as first vtable field.
368 br_sslrec_in_class inner
;
371 * \brief Engine initialisation method.
373 * This method sets the vtable field in the context.
375 * \param ctx context to initialise.
376 * \param bc_impl block cipher implementation (CBC decryption).
377 * \param bc_key block cipher key.
378 * \param bc_key_len block cipher key length (in bytes).
379 * \param dig_impl hash function for HMAC.
380 * \param mac_key HMAC key.
381 * \param mac_key_len HMAC key length (in bytes).
382 * \param mac_out_len HMAC output length (in bytes).
383 * \param iv initial IV (or `NULL`).
385 void (*init
)(const br_sslrec_in_cbc_class
**ctx
,
386 const br_block_cbcdec_class
*bc_impl
,
387 const void *bc_key
, size_t bc_key_len
,
388 const br_hash_class
*dig_impl
,
389 const void *mac_key
, size_t mac_key_len
, size_t mac_out_len
,
394 * \brief Record encryption engine class, for CBC mode.
396 * This class type extends the encryption engine class with an
397 * initialisation method that receives the parameters needed
398 * for CBC processing: block cipher implementation, block cipher key,
399 * HMAC parameters (hash function, key, MAC length), and IV. If the
400 * IV is `NULL`, then a per-record IV will be used (TLS 1.1+).
402 typedef struct br_sslrec_out_cbc_class_ br_sslrec_out_cbc_class
;
403 struct br_sslrec_out_cbc_class_
{
405 * \brief Superclass, as first vtable field.
407 br_sslrec_out_class inner
;
410 * \brief Engine initialisation method.
412 * This method sets the vtable field in the context.
414 * \param ctx context to initialise.
415 * \param bc_impl block cipher implementation (CBC encryption).
416 * \param bc_key block cipher key.
417 * \param bc_key_len block cipher key length (in bytes).
418 * \param dig_impl hash function for HMAC.
419 * \param mac_key HMAC key.
420 * \param mac_key_len HMAC key length (in bytes).
421 * \param mac_out_len HMAC output length (in bytes).
422 * \param iv initial IV (or `NULL`).
424 void (*init
)(const br_sslrec_out_cbc_class
**ctx
,
425 const br_block_cbcenc_class
*bc_impl
,
426 const void *bc_key
, size_t bc_key_len
,
427 const br_hash_class
*dig_impl
,
428 const void *mac_key
, size_t mac_key_len
, size_t mac_out_len
,
433 * \brief Context structure for decrypting incoming records with
436 * The first field points to the vtable. The other fields are opaque
437 * and shall not be accessed directly.
440 /** \brief Pointer to vtable. */
441 const br_sslrec_in_cbc_class
*vtable
;
442 #ifndef BR_DOXYGEN_IGNORE
445 const br_block_cbcdec_class
*vtable
;
446 br_aes_gen_cbcdec_keys aes
;
447 br_des_gen_cbcdec_keys des
;
449 br_hmac_key_context mac
;
451 unsigned char iv
[16];
454 } br_sslrec_in_cbc_context
;
457 * \brief Static, constant vtable for record decryption with CBC.
459 extern const br_sslrec_in_cbc_class br_sslrec_in_cbc_vtable
;
462 * \brief Context structure for encrypting outgoing records with
465 * The first field points to the vtable. The other fields are opaque
466 * and shall not be accessed directly.
469 /** \brief Pointer to vtable. */
470 const br_sslrec_out_cbc_class
*vtable
;
471 #ifndef BR_DOXYGEN_IGNORE
474 const br_block_cbcenc_class
*vtable
;
475 br_aes_gen_cbcenc_keys aes
;
476 br_des_gen_cbcenc_keys des
;
478 br_hmac_key_context mac
;
480 unsigned char iv
[16];
483 } br_sslrec_out_cbc_context
;
486 * \brief Static, constant vtable for record encryption with CBC.
488 extern const br_sslrec_out_cbc_class br_sslrec_out_cbc_vtable
;
490 /* ===================================================================== */
493 * \brief Record decryption engine class, for GCM mode.
495 * This class type extends the decryption engine class with an
496 * initialisation method that receives the parameters needed
497 * for GCM processing: block cipher implementation, block cipher key,
498 * GHASH implementation, and 4-byte IV.
500 typedef struct br_sslrec_in_gcm_class_ br_sslrec_in_gcm_class
;
501 struct br_sslrec_in_gcm_class_
{
503 * \brief Superclass, as first vtable field.
505 br_sslrec_in_class inner
;
508 * \brief Engine initialisation method.
510 * This method sets the vtable field in the context.
512 * \param ctx context to initialise.
513 * \param bc_impl block cipher implementation (CTR).
514 * \param key block cipher key.
515 * \param key_len block cipher key length (in bytes).
516 * \param gh_impl GHASH implementation.
517 * \param iv static IV (4 bytes).
519 void (*init
)(const br_sslrec_in_gcm_class
**ctx
,
520 const br_block_ctr_class
*bc_impl
,
521 const void *key
, size_t key_len
,
527 * \brief Record encryption engine class, for GCM mode.
529 * This class type extends the encryption engine class with an
530 * initialisation method that receives the parameters needed
531 * for GCM processing: block cipher implementation, block cipher key,
532 * GHASH implementation, and 4-byte IV.
534 typedef struct br_sslrec_out_gcm_class_ br_sslrec_out_gcm_class
;
535 struct br_sslrec_out_gcm_class_
{
537 * \brief Superclass, as first vtable field.
539 br_sslrec_out_class inner
;
542 * \brief Engine initialisation method.
544 * This method sets the vtable field in the context.
546 * \param ctx context to initialise.
547 * \param bc_impl block cipher implementation (CTR).
548 * \param key block cipher key.
549 * \param key_len block cipher key length (in bytes).
550 * \param gh_impl GHASH implementation.
551 * \param iv static IV (4 bytes).
553 void (*init
)(const br_sslrec_out_gcm_class
**ctx
,
554 const br_block_ctr_class
*bc_impl
,
555 const void *key
, size_t key_len
,
561 * \brief Context structure for processing records with GCM.
563 * The same context structure is used for encrypting and decrypting.
565 * The first field points to the vtable. The other fields are opaque
566 * and shall not be accessed directly.
569 /** \brief Pointer to vtable. */
572 const br_sslrec_in_gcm_class
*in
;
573 const br_sslrec_out_gcm_class
*out
;
575 #ifndef BR_DOXYGEN_IGNORE
578 const br_block_ctr_class
*vtable
;
579 br_aes_gen_ctr_keys aes
;
585 } br_sslrec_gcm_context
;
588 * \brief Static, constant vtable for record decryption with GCM.
590 extern const br_sslrec_in_gcm_class br_sslrec_in_gcm_vtable
;
593 * \brief Static, constant vtable for record encryption with GCM.
595 extern const br_sslrec_out_gcm_class br_sslrec_out_gcm_vtable
;
597 /* ===================================================================== */
600 * \brief Record decryption engine class, for ChaCha20+Poly1305.
602 * This class type extends the decryption engine class with an
603 * initialisation method that receives the parameters needed
604 * for ChaCha20+Poly1305 processing: ChaCha20 implementation,
605 * Poly1305 implementation, key, and 12-byte IV.
607 typedef struct br_sslrec_in_chapol_class_ br_sslrec_in_chapol_class
;
608 struct br_sslrec_in_chapol_class_
{
610 * \brief Superclass, as first vtable field.
612 br_sslrec_in_class inner
;
615 * \brief Engine initialisation method.
617 * This method sets the vtable field in the context.
619 * \param ctx context to initialise.
620 * \param ichacha ChaCha20 implementation.
621 * \param ipoly Poly1305 implementation.
622 * \param key secret key (32 bytes).
623 * \param iv static IV (12 bytes).
625 void (*init
)(const br_sslrec_in_chapol_class
**ctx
,
626 br_chacha20_run ichacha
,
627 br_poly1305_run ipoly
,
628 const void *key
, const void *iv
);
632 * \brief Record encryption engine class, for ChaCha20+Poly1305.
634 * This class type extends the encryption engine class with an
635 * initialisation method that receives the parameters needed
636 * for ChaCha20+Poly1305 processing: ChaCha20 implementation,
637 * Poly1305 implementation, key, and 12-byte IV.
639 typedef struct br_sslrec_out_chapol_class_ br_sslrec_out_chapol_class
;
640 struct br_sslrec_out_chapol_class_
{
642 * \brief Superclass, as first vtable field.
644 br_sslrec_out_class inner
;
647 * \brief Engine initialisation method.
649 * This method sets the vtable field in the context.
651 * \param ctx context to initialise.
652 * \param ichacha ChaCha20 implementation.
653 * \param ipoly Poly1305 implementation.
654 * \param key secret key (32 bytes).
655 * \param iv static IV (12 bytes).
657 void (*init
)(const br_sslrec_out_chapol_class
**ctx
,
658 br_chacha20_run ichacha
,
659 br_poly1305_run ipoly
,
660 const void *key
, const void *iv
);
664 * \brief Context structure for processing records with ChaCha20+Poly1305.
666 * The same context structure is used for encrypting and decrypting.
668 * The first field points to the vtable. The other fields are opaque
669 * and shall not be accessed directly.
672 /** \brief Pointer to vtable. */
675 const br_sslrec_in_chapol_class
*in
;
676 const br_sslrec_out_chapol_class
*out
;
678 #ifndef BR_DOXYGEN_IGNORE
680 unsigned char key
[32];
681 unsigned char iv
[12];
682 br_chacha20_run ichacha
;
683 br_poly1305_run ipoly
;
685 } br_sslrec_chapol_context
;
688 * \brief Static, constant vtable for record decryption with ChaCha20+Poly1305.
690 extern const br_sslrec_in_chapol_class br_sslrec_in_chapol_vtable
;
693 * \brief Static, constant vtable for record encryption with ChaCha20+Poly1305.
695 extern const br_sslrec_out_chapol_class br_sslrec_out_chapol_vtable
;
697 /* ===================================================================== */
700 * \brief Type for session parameters, to be saved for session resumption.
703 /** \brief Session ID buffer. */
704 unsigned char session_id
[32];
705 /** \brief Session ID length (in bytes, at most 32). */
706 unsigned char session_id_len
;
707 /** \brief Protocol version. */
709 /** \brief Cipher suite. */
710 uint16_t cipher_suite
;
711 /** \brief Master secret. */
712 unsigned char master_secret
[48];
713 } br_ssl_session_parameters
;
715 #ifndef BR_DOXYGEN_IGNORE
717 * Maximum numnber of cipher suites supported by a client or server.
719 #define BR_MAX_CIPHER_SUITES 40
723 * \brief Context structure for SSL engine.
725 * This strucuture is common to the client and server; both the client
726 * context (`br_ssl_client_context`) and the server context
727 * (`br_ssl_server_context`) include a `br_ssl_engine_context` as their
730 * The engine context manages records, including alerts, closures, and
731 * transitions to new encryption/MAC algorithms. Processing of handshake
732 * records is delegated to externally provided code. This structure
733 * should not be used directly.
735 * Structure contents are opaque and shall not be accessed directly.
738 #ifndef BR_DOXYGEN_IGNORE
740 * The error code. When non-zero, then the state is "failed" and
741 * no I/O may occur until reset.
746 * Configured I/O buffers. They are either disjoint, or identical.
748 unsigned char *ibuf
, *obuf
;
749 size_t ibuf_len
, obuf_len
;
752 * Maximum fragment length applies to outgoing records; incoming
753 * records can be processed as long as they fit in the input
754 * buffer. It is guaranteed that incoming records at least as big
755 * as max_frag_len can be processed.
757 uint16_t max_frag_len
;
758 unsigned char log_max_frag_len
;
759 unsigned char peer_log_max_frag_len
;
762 * Buffering management registers.
764 size_t ixa
, ixb
, ixc
;
765 size_t oxa
, oxb
, oxc
;
766 unsigned char iomode
;
767 unsigned char incrypt
;
770 * Shutdown flag: when set to non-zero, incoming record bytes
771 * will not be accepted anymore. This is used after a close_notify
772 * has been received: afterwards, the engine no longer claims that
773 * it could receive bytes from the transport medium.
775 unsigned char shutdown_recv
;
778 * 'record_type_in' is set to the incoming record type when the
779 * record header has been received.
780 * 'record_type_out' is used to make the next outgoing record
781 * header when it is ready to go.
783 unsigned char record_type_in
, record_type_out
;
786 * When a record is received, its version is extracted:
787 * -- if 'version_in' is 0, then it is set to the received version;
788 * -- otherwise, if the received version is not identical to
789 * the 'version_in' contents, then a failure is reported.
791 * This implements the SSL requirement that all records shall
792 * use the negotiated protocol version, once decided (in the
793 * ServerHello). It is up to the handshake handler to adjust this
794 * field when necessary.
799 * 'version_out' is used when the next outgoing record is ready
802 uint16_t version_out
;
805 * Record handler contexts.
808 const br_sslrec_in_class
*vtable
;
809 br_sslrec_in_cbc_context cbc
;
810 br_sslrec_gcm_context gcm
;
811 br_sslrec_chapol_context chapol
;
814 const br_sslrec_out_class
*vtable
;
815 br_sslrec_out_clear_context clear
;
816 br_sslrec_out_cbc_context cbc
;
817 br_sslrec_gcm_context gcm
;
818 br_sslrec_chapol_context chapol
;
822 * The "application data" flag. It is set when application data
823 * can be exchanged, cleared otherwise.
825 unsigned char application_data
;
830 br_hmac_drbg_context rng
;
832 int rng_os_rand_done
;
835 * Supported minimum and maximum versions, and cipher suites.
837 uint16_t version_min
;
838 uint16_t version_max
;
839 uint16_t suites_buf
[BR_MAX_CIPHER_SUITES
];
840 unsigned char suites_num
;
843 * For clients, the server name to send as a SNI extension. For
844 * servers, the name received in the SNI extension (if any).
846 char server_name
[256];
849 * "Security parameters". These are filled by the handshake
850 * handler, and used when switching encryption state.
852 unsigned char client_random
[32];
853 unsigned char server_random
[32];
854 br_ssl_session_parameters session
;
857 * ECDHE elements: curve and point from the peer. The server also
858 * uses that buffer for the point to send to the client.
860 unsigned char ecdhe_curve
;
861 unsigned char ecdhe_point
[133];
862 unsigned char ecdhe_point_len
;
865 * Secure renegotiation (RFC 5746): 'reneg' can be:
866 * 0 first handshake (server support is not known)
867 * 1 server does not support secure renegotiation
868 * 2 server supports secure renegotiation
870 * The saved_finished buffer contains the client and the
871 * server "Finished" values from the last handshake, in
872 * that order (12 bytes each).
875 unsigned char saved_finished
[24];
883 * Context variables for the handshake processor. The 'pad' must
884 * be large enough to accommodate an RSA-encrypted pre-master
885 * secret, or an RSA signature; since we want to support up to
886 * RSA-4096, this means at least 512 bytes. (Other pad usages
887 * require its length to be at least 256.)
892 const unsigned char *ip
;
894 uint32_t dp_stack
[32];
895 uint32_t rp_stack
[32];
896 unsigned char pad
[512];
897 unsigned char *hbuf_in
, *hbuf_out
, *saved_hbuf_out
;
898 size_t hlen_in
, hlen_out
;
899 void (*hsrun
)(void *ctx
);
902 * The 'action' value communicates OOB information between the
903 * engine and the handshake processor.
906 * 0 invocation triggered by I/O
907 * 1 invocation triggered by explicit close
908 * 2 invocation triggered by explicit renegotiation
910 unsigned char action
;
913 * State for alert messages. Value is either 0, or the value of
914 * the alert level byte (level is either 1 for warning, or 2 for
915 * fatal; we convert all other values to 'fatal').
920 * Closure flags. This flag is set when a close_notify has been
921 * received from the peer.
923 unsigned char close_received
;
926 * Multi-hasher for the handshake messages. The handshake handler
927 * is responsible for resetting it when appropriate.
929 br_multihash_context mhash
;
932 * Pointer to the X.509 engine. The engine is supposed to be
933 * already initialized. It is used to validate the peer's
936 const br_x509_class
**x509ctx
;
939 * Certificate chain to send. This is used by both client and
940 * server, when they send their respective Certificate messages.
941 * If chain_len is 0, then chain may be NULL.
943 const br_x509_certificate
*chain
;
945 const unsigned char *cert_cur
;
949 * List of supported protocol names (ALPN extension). If unset,
950 * (number of names is 0), then:
951 * - the client sends no ALPN extension;
952 * - the server ignores any incoming ALPN extension.
955 * - the client sends an ALPN extension with all the names;
956 * - the server selects the first protocol in its list that
957 * the client also supports, or fails (fatal alert 120)
958 * if the client sends an ALPN extension and there is no
961 * The 'selected_protocol' field contains 1+n if the matching
962 * name has index n in the list (the value is 0 if no match was
963 * performed, e.g. the peer did not send an ALPN extension).
965 const char **protocol_names
;
966 uint16_t protocol_names_num
;
967 uint16_t selected_protocol
;
970 * Pointers to implementations; left to NULL for unsupported
971 * functions. For the raw hash functions, implementations are
972 * referenced from the multihasher (mhash field).
974 br_tls_prf_impl prf10
;
975 br_tls_prf_impl prf_sha256
;
976 br_tls_prf_impl prf_sha384
;
977 const br_block_cbcenc_class
*iaes_cbcenc
;
978 const br_block_cbcdec_class
*iaes_cbcdec
;
979 const br_block_ctr_class
*iaes_ctr
;
980 const br_block_cbcenc_class
*ides_cbcenc
;
981 const br_block_cbcdec_class
*ides_cbcdec
;
983 br_chacha20_run ichacha
;
984 br_poly1305_run ipoly
;
985 const br_sslrec_in_cbc_class
*icbc_in
;
986 const br_sslrec_out_cbc_class
*icbc_out
;
987 const br_sslrec_in_gcm_class
*igcm_in
;
988 const br_sslrec_out_gcm_class
*igcm_out
;
989 const br_sslrec_in_chapol_class
*ichapol_in
;
990 const br_sslrec_out_chapol_class
*ichapol_out
;
991 const br_ec_impl
*iec
;
992 br_rsa_pkcs1_vrfy irsavrfy
;
993 br_ecdsa_vrfy iecdsa
;
995 } br_ssl_engine_context
;
998 * \brief Get currently defined engine behavioural flags.
1000 * \param cc SSL engine context.
1001 * \return the flags.
1003 static inline uint32_t
1004 br_ssl_engine_get_flags(br_ssl_engine_context
*cc
)
1010 * \brief Set all engine behavioural flags.
1012 * \param cc SSL engine context.
1013 * \param flags new value for all flags.
1016 br_ssl_engine_set_all_flags(br_ssl_engine_context
*cc
, uint32_t flags
)
1022 * \brief Set some engine behavioural flags.
1024 * The flags set in the `flags` parameter are set in the context; other
1025 * flags are untouched.
1027 * \param cc SSL engine context.
1028 * \param flags additional set flags.
1031 br_ssl_engine_add_flags(br_ssl_engine_context
*cc
, uint32_t flags
)
1037 * \brief Clear some engine behavioural flags.
1039 * The flags set in the `flags` parameter are cleared from the context; other
1040 * flags are untouched.
1042 * \param cc SSL engine context.
1043 * \param flags flags to remove.
1046 br_ssl_engine_remove_flags(br_ssl_engine_context
*cc
, uint32_t flags
)
1048 cc
->flags
&= ~flags
;
1052 * \brief Behavioural flag: enforce server preferences.
1054 * If this flag is set, then the server will enforce its own cipher suite
1055 * preference order; otherwise, it follows the client preferences.
1057 #define BR_OPT_ENFORCE_SERVER_PREFERENCES ((uint32_t)1 << 0)
1060 * \brief Behavioural flag: disable renegotiation.
1062 * If this flag is set, then renegotiations are rejected unconditionally:
1063 * they won't be honoured if asked for programmatically, and requests from
1064 * the peer are rejected.
1066 #define BR_OPT_NO_RENEGOTIATION ((uint32_t)1 << 1)
1069 * \brief Behavioural flag: tolerate lack of client authentication.
1071 * If this flag is set in a server and the server requests a client
1072 * certificate, but the authentication fails (the client does not send
1073 * a certificate, or the client's certificate chain cannot be validated),
1074 * then the connection keeps on. Without this flag, a failed client
1075 * authentication terminates the connection.
1079 * - If the client's certificate can be validated and its public key is
1080 * supported, then a wrong signature value terminates the connection
1081 * regardless of that flag.
1083 * - If using full-static ECDH, then a failure to validate the client's
1084 * certificate prevents the handshake from succeeding.
1086 #define BR_OPT_TOLERATE_NO_CLIENT_AUTH ((uint32_t)1 << 2)
1089 * \brief Behavioural flag: fail on application protocol mismatch.
1091 * The ALPN extension ([RFC 7301](https://tools.ietf.org/html/rfc7301))
1092 * allows the client to send a list of application protocol names, and
1093 * the server to select one. A mismatch is one of the following occurrences:
1095 * - On the client: the client sends a list of names, the server
1096 * responds with a protocol name which is _not_ part of the list of
1097 * names sent by the client.
1099 * - On the server: the client sends a list of names, and the server
1100 * is also configured with a list of names, but there is no common
1101 * protocol name between the two lists.
1103 * Normal behaviour in case of mismatch is to report no matching name
1104 * (`br_ssl_engine_get_selected_protocol()` returns `NULL`) and carry on.
1105 * If the flag is set, then a mismatch implies a protocol failure (if
1106 * the mismatch is detected by the server, it will send a fatal alert).
1108 * Note: even with this flag, `br_ssl_engine_get_selected_protocol()`
1109 * may still return `NULL` if the client or the server does not send an
1110 * ALPN extension at all.
1112 #define BR_OPT_FAIL_ON_ALPN_MISMATCH ((uint32_t)1 << 3)
1115 * \brief Set the minimum and maximum supported protocol versions.
1117 * The two provided versions MUST be supported by the implementation
1118 * (i.e. TLS 1.0, 1.1 and 1.2), and `version_max` MUST NOT be lower
1119 * than `version_min`.
1121 * \param cc SSL engine context.
1122 * \param version_min minimum supported TLS version.
1123 * \param version_max maximum supported TLS version.
1126 br_ssl_engine_set_versions(br_ssl_engine_context
*cc
,
1127 unsigned version_min
, unsigned version_max
)
1129 cc
->version_min
= version_min
;
1130 cc
->version_max
= version_max
;
1134 * \brief Set the list of cipher suites advertised by this context.
1136 * The provided array is copied into the context. It is the caller
1137 * responsibility to ensure that all provided suites will be supported
1138 * by the context. The engine context has enough room to receive _all_
1139 * suites supported by the implementation. The provided array MUST NOT
1140 * contain duplicates.
1142 * If the engine is for a client, the "signaling" pseudo-cipher suite
1143 * `TLS_FALLBACK_SCSV` can be added at the end of the list, if the
1144 * calling application is performing a voluntary downgrade (voluntary
1145 * downgrades are not recommended, but if such a downgrade is done, then
1146 * adding the fallback pseudo-suite is a good idea).
1148 * \param cc SSL engine context.
1149 * \param suites cipher suites.
1150 * \param suites_num number of cipher suites.
1152 void br_ssl_engine_set_suites(br_ssl_engine_context
*cc
,
1153 const uint16_t *suites
, size_t suites_num
);
1156 * \brief Set the X.509 engine.
1158 * The caller shall ensure that the X.509 engine is properly initialised.
1160 * \param cc SSL engine context.
1161 * \param x509ctx X.509 certificate validation context.
1164 br_ssl_engine_set_x509(br_ssl_engine_context
*cc
, const br_x509_class
**x509ctx
)
1166 cc
->x509ctx
= x509ctx
;
1170 * \brief Set the supported protocol names.
1172 * Protocol names are part of the ALPN extension ([RFC
1173 * 7301](https://tools.ietf.org/html/rfc7301)). Each protocol name is a
1174 * character string, containing no more than 255 characters (256 with the
1175 * terminating zero). When names are set, then:
1177 * - The client will send an ALPN extension, containing the names. If
1178 * the server responds with an ALPN extension, the client will verify
1179 * that the response contains one of its name, and report that name
1180 * through `br_ssl_engine_get_selected_protocol()`.
1182 * - The server will parse incoming ALPN extension (from clients), and
1183 * try to find a common protocol; if none is found, the connection
1184 * is aborted with a fatal alert. On match, a response ALPN extension
1185 * is sent, and name is reported through
1186 * `br_ssl_engine_get_selected_protocol()`.
1188 * The provided array is linked in, and must remain valid while the
1189 * connection is live.
1191 * Names MUST NOT be empty. Names MUST NOT be longer than 255 characters
1192 * (excluding the terminating 0).
1194 * \param ctx SSL engine context.
1195 * \param names list of protocol names (zero-terminated).
1196 * \param num number of protocol names (MUST be 1 or more).
1199 br_ssl_engine_set_protocol_names(br_ssl_engine_context
*ctx
,
1200 const char **names
, size_t num
)
1202 ctx
->protocol_names
= names
;
1203 ctx
->protocol_names_num
= num
;
1207 * \brief Get the selected protocol.
1209 * If this context was initialised with a non-empty list of protocol
1210 * names, and both client and server sent ALPN extensions during the
1211 * handshake, and a common name was found, then that name is returned.
1212 * Otherwise, `NULL` is returned.
1214 * The returned pointer is one of the pointers provided to the context
1215 * with `br_ssl_engine_set_protocol_names()`.
1217 * \return the selected protocol, or `NULL`.
1219 static inline const char *
1220 br_ssl_engine_get_selected_protocol(br_ssl_engine_context
*ctx
)
1224 k
= ctx
->selected_protocol
;
1225 return (k
== 0 || k
== 0xFFFF) ? NULL
: ctx
->protocol_names
[k
- 1];
1229 * \brief Set a hash function implementation (by ID).
1231 * Hash functions set with this call will be used for SSL/TLS specific
1232 * usages, not X.509 certificate validation. Only "standard" hash functions
1233 * may be set (MD5, SHA-1, SHA-224, SHA-256, SHA-384, SHA-512). If `impl`
1234 * is `NULL`, then the hash function support is removed, not added.
1236 * \param ctx SSL engine context.
1237 * \param id hash function identifier.
1238 * \param impl hash function implementation (or `NULL`).
1241 br_ssl_engine_set_hash(br_ssl_engine_context
*ctx
,
1242 int id
, const br_hash_class
*impl
)
1244 br_multihash_setimpl(&ctx
->mhash
, id
, impl
);
1248 * \brief Get a hash function implementation (by ID).
1250 * This function retrieves a hash function implementation which was
1251 * set with `br_ssl_engine_set_hash()`.
1253 * \param ctx SSL engine context.
1254 * \param id hash function identifier.
1255 * \return the hash function implementation (or `NULL`).
1257 static inline const br_hash_class
*
1258 br_ssl_engine_get_hash(br_ssl_engine_context
*ctx
, int id
)
1260 return br_multihash_getimpl(&ctx
->mhash
, id
);
1264 * \brief Set the PRF implementation (for TLS 1.0 and 1.1).
1266 * This function sets (or removes, if `impl` is `NULL`) the implemenation
1267 * for the PRF used in TLS 1.0 and 1.1.
1269 * \param cc SSL engine context.
1270 * \param impl PRF implementation (or `NULL`).
1273 br_ssl_engine_set_prf10(br_ssl_engine_context
*cc
, br_tls_prf_impl impl
)
1279 * \brief Set the PRF implementation with SHA-256 (for TLS 1.2).
1281 * This function sets (or removes, if `impl` is `NULL`) the implemenation
1282 * for the SHA-256 variant of the PRF used in TLS 1.2.
1284 * \param cc SSL engine context.
1285 * \param impl PRF implementation (or `NULL`).
1288 br_ssl_engine_set_prf_sha256(br_ssl_engine_context
*cc
, br_tls_prf_impl impl
)
1290 cc
->prf_sha256
= impl
;
1294 * \brief Set the PRF implementation with SHA-384 (for TLS 1.2).
1296 * This function sets (or removes, if `impl` is `NULL`) the implemenation
1297 * for the SHA-384 variant of the PRF used in TLS 1.2.
1299 * \param cc SSL engine context.
1300 * \param impl PRF implementation (or `NULL`).
1303 br_ssl_engine_set_prf_sha384(br_ssl_engine_context
*cc
, br_tls_prf_impl impl
)
1305 cc
->prf_sha384
= impl
;
1309 * \brief Set the AES/CBC implementations.
1311 * \param cc SSL engine context.
1312 * \param impl_enc AES/CBC encryption implementation (or `NULL`).
1313 * \param impl_dec AES/CBC decryption implementation (or `NULL`).
1316 br_ssl_engine_set_aes_cbc(br_ssl_engine_context
*cc
,
1317 const br_block_cbcenc_class
*impl_enc
,
1318 const br_block_cbcdec_class
*impl_dec
)
1320 cc
->iaes_cbcenc
= impl_enc
;
1321 cc
->iaes_cbcdec
= impl_dec
;
1325 * \brief Set the "default" AES/CBC implementations.
1327 * This function configures in the engine the AES implementations that
1328 * should provide best runtime performance on the local system, while
1329 * still being safe (in particular, constant-time). It also sets the
1330 * handlers for CBC records.
1332 * \param cc SSL engine context.
1334 void br_ssl_engine_set_default_aes_cbc(br_ssl_engine_context
*cc
);
1337 * \brief Set the AES/CTR implementation.
1339 * \param cc SSL engine context.
1340 * \param impl AES/CTR encryption/decryption implementation (or `NULL`).
1343 br_ssl_engine_set_aes_ctr(br_ssl_engine_context
*cc
,
1344 const br_block_ctr_class
*impl
)
1346 cc
->iaes_ctr
= impl
;
1350 * \brief Set the "default" implementations for AES/GCM (AES/CTR + GHASH).
1352 * This function configures in the engine the AES/CTR and GHASH
1353 * implementation that should provide best runtime performance on the local
1354 * system, while still being safe (in particular, constant-time). It also
1355 * sets the handlers for GCM records.
1357 * \param cc SSL engine context.
1359 void br_ssl_engine_set_default_aes_gcm(br_ssl_engine_context
*cc
);
1362 * \brief Set the DES/CBC implementations.
1364 * \param cc SSL engine context.
1365 * \param impl_enc DES/CBC encryption implementation (or `NULL`).
1366 * \param impl_dec DES/CBC decryption implementation (or `NULL`).
1369 br_ssl_engine_set_des_cbc(br_ssl_engine_context
*cc
,
1370 const br_block_cbcenc_class
*impl_enc
,
1371 const br_block_cbcdec_class
*impl_dec
)
1373 cc
->ides_cbcenc
= impl_enc
;
1374 cc
->ides_cbcdec
= impl_dec
;
1378 * \brief Set the "default" DES/CBC implementations.
1380 * This function configures in the engine the DES implementations that
1381 * should provide best runtime performance on the local system, while
1382 * still being safe (in particular, constant-time). It also sets the
1383 * handlers for CBC records.
1385 * \param cc SSL engine context.
1387 void br_ssl_engine_set_default_des_cbc(br_ssl_engine_context
*cc
);
1390 * \brief Set the GHASH implementation (used in GCM mode).
1392 * \param cc SSL engine context.
1393 * \param impl GHASH implementation (or `NULL`).
1396 br_ssl_engine_set_ghash(br_ssl_engine_context
*cc
, br_ghash impl
)
1402 * \brief Set the ChaCha20 implementation.
1404 * \param cc SSL engine context.
1405 * \param ichacha ChaCha20 implementation (or `NULL`).
1408 br_ssl_engine_set_chacha20(br_ssl_engine_context
*cc
,
1409 br_chacha20_run ichacha
)
1411 cc
->ichacha
= ichacha
;
1415 * \brief Set the Poly1305 implementation.
1417 * \param cc SSL engine context.
1418 * \param ipoly Poly1305 implementation (or `NULL`).
1421 br_ssl_engine_set_poly1305(br_ssl_engine_context
*cc
,
1422 br_poly1305_run ipoly
)
1428 * \brief Set the "default" ChaCha20 and Poly1305 implementations.
1430 * This function configures in the engine the ChaCha20 and Poly1305
1431 * implementations that should provide best runtime performance on the
1432 * local system, while still being safe (in particular, constant-time).
1433 * It also sets the handlers for ChaCha20+Poly1305 records.
1435 * \param cc SSL engine context.
1437 void br_ssl_engine_set_default_chapol(br_ssl_engine_context
*cc
);
1440 * \brief Set the record encryption and decryption engines for CBC + HMAC.
1442 * \param cc SSL engine context.
1443 * \param impl_in record CBC decryption implementation (or `NULL`).
1444 * \param impl_out record CBC encryption implementation (or `NULL`).
1447 br_ssl_engine_set_cbc(br_ssl_engine_context
*cc
,
1448 const br_sslrec_in_cbc_class
*impl_in
,
1449 const br_sslrec_out_cbc_class
*impl_out
)
1451 cc
->icbc_in
= impl_in
;
1452 cc
->icbc_out
= impl_out
;
1456 * \brief Set the record encryption and decryption engines for GCM.
1458 * \param cc SSL engine context.
1459 * \param impl_in record GCM decryption implementation (or `NULL`).
1460 * \param impl_out record GCM encryption implementation (or `NULL`).
1463 br_ssl_engine_set_gcm(br_ssl_engine_context
*cc
,
1464 const br_sslrec_in_gcm_class
*impl_in
,
1465 const br_sslrec_out_gcm_class
*impl_out
)
1467 cc
->igcm_in
= impl_in
;
1468 cc
->igcm_out
= impl_out
;
1472 * \brief Set the record encryption and decryption engines for
1473 * ChaCha20+Poly1305.
1475 * \param cc SSL engine context.
1476 * \param impl_in record ChaCha20 decryption implementation (or `NULL`).
1477 * \param impl_out record ChaCha20 encryption implementation (or `NULL`).
1480 br_ssl_engine_set_chapol(br_ssl_engine_context
*cc
,
1481 const br_sslrec_in_chapol_class
*impl_in
,
1482 const br_sslrec_out_chapol_class
*impl_out
)
1484 cc
->ichapol_in
= impl_in
;
1485 cc
->ichapol_out
= impl_out
;
1489 * \brief Set the EC implementation.
1491 * The elliptic curve implementation will be used for ECDH and ECDHE
1492 * cipher suites, and for ECDSA support.
1494 * \param cc SSL engine context.
1495 * \param iec EC implementation (or `NULL`).
1498 br_ssl_engine_set_ec(br_ssl_engine_context
*cc
, const br_ec_impl
*iec
)
1504 * \brief Set the "default" EC implementation.
1506 * This function sets the elliptic curve implementation for ECDH and
1507 * ECDHE cipher suites, and for ECDSA support. It selects the fastest
1508 * implementation on the current system.
1510 * \param cc SSL engine context.
1512 void br_ssl_engine_set_default_ec(br_ssl_engine_context
*cc
);
1515 * \brief Get the EC implementation configured in the provided engine.
1517 * \param cc SSL engine context.
1518 * \return the EC implementation.
1520 static inline const br_ec_impl
*
1521 br_ssl_engine_get_ec(br_ssl_engine_context
*cc
)
1527 * \brief Set the RSA signature verification implementation.
1529 * On the client, this is used to verify the server's signature on its
1530 * ServerKeyExchange message (for ECDHE_RSA cipher suites). On the server,
1531 * this is used to verify the client's CertificateVerify message (if a
1532 * client certificate is requested, and that certificate contains a RSA key).
1534 * \param cc SSL engine context.
1535 * \param irsavrfy RSA signature verification implementation.
1538 br_ssl_engine_set_rsavrfy(br_ssl_engine_context
*cc
, br_rsa_pkcs1_vrfy irsavrfy
)
1540 cc
->irsavrfy
= irsavrfy
;
1544 * \brief Set the "default" RSA implementation (signature verification).
1546 * This function sets the RSA implementation (signature verification)
1547 * to the fastest implementation available on the current platform.
1549 * \param cc SSL engine context.
1551 void br_ssl_engine_set_default_rsavrfy(br_ssl_engine_context
*cc
);
1554 * \brief Get the RSA implementation (signature verification) configured
1555 * in the provided engine.
1557 * \param cc SSL engine context.
1558 * \return the RSA signature verification implementation.
1560 static inline br_rsa_pkcs1_vrfy
1561 br_ssl_engine_get_rsavrfy(br_ssl_engine_context
*cc
)
1563 return cc
->irsavrfy
;
1567 * \brief Set the ECDSA implementation (signature verification).
1569 * On the client, this is used to verify the server's signature on its
1570 * ServerKeyExchange message (for ECDHE_ECDSA cipher suites). On the server,
1571 * this is used to verify the client's CertificateVerify message (if a
1572 * client certificate is requested, that certificate contains an EC key,
1573 * and full-static ECDH is not used).
1575 * The ECDSA implementation will use the EC core implementation configured
1576 * in the engine context.
1578 * \param cc client context.
1579 * \param iecdsa ECDSA verification implementation.
1582 br_ssl_engine_set_ecdsa(br_ssl_engine_context
*cc
, br_ecdsa_vrfy iecdsa
)
1584 cc
->iecdsa
= iecdsa
;
1588 * \brief Set the "default" ECDSA implementation (signature verification).
1590 * This function sets the ECDSA implementation (signature verification)
1591 * to the fastest implementation available on the current platform. This
1592 * call also sets the elliptic curve implementation itself, there again
1593 * to the fastest EC implementation available.
1595 * \param cc SSL engine context.
1597 void br_ssl_engine_set_default_ecdsa(br_ssl_engine_context
*cc
);
1600 * \brief Get the ECDSA implementation (signature verification) configured
1601 * in the provided engine.
1603 * \param cc SSL engine context.
1604 * \return the ECDSA signature verification implementation.
1606 static inline br_ecdsa_vrfy
1607 br_ssl_engine_get_ecdsa(br_ssl_engine_context
*cc
)
1613 * \brief Set the I/O buffer for the SSL engine.
1615 * Once this call has been made, `br_ssl_client_reset()` or
1616 * `br_ssl_server_reset()` MUST be called before using the context.
1618 * The provided buffer will be used as long as the engine context is
1619 * used. The caller is responsible for keeping it available.
1621 * If `bidi` is 0, then the engine will operate in half-duplex mode
1622 * (it won't be able to send data while there is unprocessed incoming
1623 * data in the buffer, and it won't be able to receive data while there
1624 * is unsent data in the buffer). The optimal buffer size in half-duplex
1625 * mode is `BR_SSL_BUFSIZE_MONO`; if the buffer is larger, then extra
1626 * bytes are ignored. If the buffer is smaller, then this limits the
1627 * capacity of the engine to support all allowed record sizes.
1629 * If `bidi` is 1, then the engine will split the buffer into two
1630 * parts, for separate handling of outgoing and incoming data. This
1631 * enables full-duplex processing, but requires more RAM. The optimal
1632 * buffer size in full-duplex mode is `BR_SSL_BUFSIZE_BIDI`; if the
1633 * buffer is larger, then extra bytes are ignored. If the buffer is
1634 * smaller, then the split will favour the incoming part, so that
1635 * interoperability is maximised.
1637 * \param cc SSL engine context
1638 * \param iobuf I/O buffer.
1639 * \param iobuf_len I/O buffer length (in bytes).
1640 * \param bidi non-zero for full-duplex mode.
1642 void br_ssl_engine_set_buffer(br_ssl_engine_context
*cc
,
1643 void *iobuf
, size_t iobuf_len
, int bidi
);
1646 * \brief Set the I/O buffers for the SSL engine.
1648 * Once this call has been made, `br_ssl_client_reset()` or
1649 * `br_ssl_server_reset()` MUST be called before using the context.
1651 * This function is similar to `br_ssl_engine_set_buffer()`, except
1652 * that it enforces full-duplex mode, and the two I/O buffers are
1653 * provided as separate chunks.
1655 * The macros `BR_SSL_BUFSIZE_INPUT` and `BR_SSL_BUFSIZE_OUTPUT`
1656 * evaluate to the optimal (maximum) sizes for the input and output
1657 * buffer, respectively.
1659 * \param cc SSL engine context
1660 * \param ibuf input buffer.
1661 * \param ibuf_len input buffer length (in bytes).
1662 * \param obuf output buffer.
1663 * \param obuf_len output buffer length (in bytes).
1665 void br_ssl_engine_set_buffers_bidi(br_ssl_engine_context
*cc
,
1666 void *ibuf
, size_t ibuf_len
, void *obuf
, size_t obuf_len
);
1669 * \brief Inject some "initial entropy" in the context.
1671 * This entropy will be added to what can be obtained from the
1672 * underlying operating system, if that OS is supported.
1674 * This function may be called several times; all injected entropy chunks
1675 * are cumulatively mixed.
1677 * If entropy gathering from the OS is supported and compiled in, then this
1678 * step is optional. Otherwise, it is mandatory to inject randomness, and
1679 * the caller MUST take care to push (as one or several successive calls)
1680 * enough entropy to achieve cryptographic resistance (at least 80 bits,
1681 * preferably 128 or more). The engine will report an error if no entropy
1682 * was provided and none can be obtained from the OS.
1684 * Take care that this function cannot assess the cryptographic quality of
1685 * the provided bytes.
1687 * In all generality, "entropy" must here be considered to mean "that
1688 * which the attacker cannot predict". If your OS/architecture does not
1689 * have a suitable source of randomness, then you can make do with the
1690 * combination of a large enough secret value (possibly a copy of an
1691 * asymmetric private key that you also store on the system) AND a
1692 * non-repeating value (e.g. current time, provided that the local clock
1693 * cannot be reset or altered by the attacker).
1695 * \param cc SSL engine context.
1696 * \param data extra entropy to inject.
1697 * \param len length of the extra data (in bytes).
1699 void br_ssl_engine_inject_entropy(br_ssl_engine_context
*cc
,
1700 const void *data
, size_t len
);
1703 * \brief Get the "server name" in this engine.
1705 * For clients, this is the name provided with `br_ssl_client_reset()`;
1706 * for servers, this is the name received from the client as part of the
1707 * ClientHello message. If there is no such name (e.g. the client did
1708 * not send an SNI extension) then the returned string is empty
1709 * (returned pointer points to a byte of value 0).
1711 * The returned pointer refers to a buffer inside the context, which may
1712 * be overwritten as part of normal SSL activity (even within the same
1713 * connection, if a renegotiation occurs).
1715 * \param cc SSL engine context.
1716 * \return the server name (possibly empty).
1718 static inline const char *
1719 br_ssl_engine_get_server_name(const br_ssl_engine_context
*cc
)
1721 return cc
->server_name
;
1725 * \brief Get the protocol version.
1727 * This function returns the protocol version that is used by the
1728 * engine. That value is set after sending (for a server) or receiving
1729 * (for a client) the ServerHello message.
1731 * \param cc SSL engine context.
1732 * \return the protocol version.
1734 static inline unsigned
1735 br_ssl_engine_get_version(const br_ssl_engine_context
*cc
)
1737 return cc
->session
.version
;
1741 * \brief Get a copy of the session parameters.
1743 * The session parameters are filled during the handshake, so this
1744 * function shall not be called before completion of the handshake.
1745 * The initial handshake is completed when the context first allows
1746 * application data to be injected.
1748 * This function copies the current session parameters into the provided
1749 * structure. Beware that the session parameters include the master
1750 * secret, which is sensitive data, to handle with great care.
1752 * \param cc SSL engine context.
1753 * \param pp destination structure for the session parameters.
1756 br_ssl_engine_get_session_parameters(const br_ssl_engine_context
*cc
,
1757 br_ssl_session_parameters
*pp
)
1759 memcpy(pp
, &cc
->session
, sizeof *pp
);
1763 * \brief Set the session parameters to the provided values.
1765 * This function is meant to be used in the client, before doing a new
1766 * handshake; a session resumption will be attempted with these
1767 * parameters. In the server, this function has no effect.
1769 * \param cc SSL engine context.
1770 * \param pp source structure for the session parameters.
1773 br_ssl_engine_set_session_parameters(br_ssl_engine_context
*cc
,
1774 const br_ssl_session_parameters
*pp
)
1776 memcpy(&cc
->session
, pp
, sizeof *pp
);
1780 * \brief Get identifier for the curve used for key exchange.
1782 * If the cipher suite uses ECDHE, then this function returns the
1783 * identifier for the curve used for transient parameters. This is
1784 * defined during the course of the handshake, when the ServerKeyExchange
1785 * is sent (on the server) or received (on the client). If the
1786 * cipher suite does not use ECDHE (e.g. static ECDH, or RSA key
1787 * exchange), then this value is indeterminate.
1789 * @param cc SSL engine context.
1790 * @return the ECDHE curve identifier.
1793 br_ssl_engine_get_ecdhe_curve(br_ssl_engine_context
*cc
)
1795 return cc
->ecdhe_curve
;
1799 * \brief Get the current engine state.
1801 * An SSL engine (client or server) has, at any time, a state which is
1802 * the combination of zero, one or more of these flags:
1806 * Engine is finished, no more I/O (until next reset).
1808 * - `BR_SSL_SENDREC`
1810 * Engine has some bytes to send to the peer.
1812 * - `BR_SSL_RECVREC`
1814 * Engine expects some bytes from the peer.
1816 * - `BR_SSL_SENDAPP`
1818 * Engine may receive application data to send (or flush).
1820 * - `BR_SSL_RECVAPP`
1822 * Engine has obtained some application data from the peer,
1823 * that should be read by the caller.
1825 * If no flag at all is set (state value is 0), then the engine is not
1826 * fully initialised yet.
1828 * The `BR_SSL_CLOSED` flag is exclusive; when it is set, no other flag
1829 * is set. To distinguish between a normal closure and an error, use
1830 * `br_ssl_engine_last_error()`.
1832 * Generally speaking, `BR_SSL_SENDREC` and `BR_SSL_SENDAPP` are mutually
1833 * exclusive: the input buffer, at any point, either accumulates
1834 * plaintext data, or contains an assembled record that is being sent.
1835 * Similarly, `BR_SSL_RECVREC` and `BR_SSL_RECVAPP` are mutually exclusive.
1836 * This may change in a future library version.
1838 * \param cc SSL engine context.
1839 * \return the current engine state.
1841 unsigned br_ssl_engine_current_state(const br_ssl_engine_context
*cc
);
1843 /** \brief SSL engine state: closed or failed. */
1844 #define BR_SSL_CLOSED 0x0001
1845 /** \brief SSL engine state: record data is ready to be sent to the peer. */
1846 #define BR_SSL_SENDREC 0x0002
1847 /** \brief SSL engine state: engine may receive records from the peer. */
1848 #define BR_SSL_RECVREC 0x0004
1849 /** \brief SSL engine state: engine may accept application data to send. */
1850 #define BR_SSL_SENDAPP 0x0008
1851 /** \brief SSL engine state: engine has received application data. */
1852 #define BR_SSL_RECVAPP 0x0010
1855 * \brief Get the engine error indicator.
1857 * The error indicator is `BR_ERR_OK` (0) if no error was encountered
1858 * since the last call to `br_ssl_client_reset()` or
1859 * `br_ssl_server_reset()`. Other status values are "sticky": they
1860 * remain set, and prevent all I/O activity, until cleared. Only the
1861 * reset calls clear the error indicator.
1863 * \param cc SSL engine context.
1864 * \return 0, or a non-zero error code.
1867 br_ssl_engine_last_error(const br_ssl_engine_context
*cc
)
1873 * There are four I/O operations, each identified by a symbolic name:
1875 * sendapp inject application data in the engine
1876 * recvapp retrieving application data from the engine
1877 * sendrec sending records on the transport medium
1878 * recvrec receiving records from the transport medium
1880 * Terminology works thus: in a layered model where the SSL engine sits
1881 * between the application and the network, "send" designates operations
1882 * where bytes flow from application to network, and "recv" for the
1883 * reverse operation. Application data (the plaintext that is to be
1884 * conveyed through SSL) is "app", while encrypted records are "rec".
1885 * Note that from the SSL engine point of view, "sendapp" and "recvrec"
1886 * designate bytes that enter the engine ("inject" operation), while
1887 * "recvapp" and "sendrec" designate bytes that exit the engine
1888 * ("extract" operation).
1890 * For the operation 'xxx', two functions are defined:
1892 * br_ssl_engine_xxx_buf
1893 * Returns a pointer and length to the buffer to use for that
1894 * operation. '*len' is set to the number of bytes that may be read
1895 * from the buffer (extract operation) or written to the buffer
1896 * (inject operation). If no byte may be exchanged for that operation
1897 * at that point, then '*len' is set to zero, and NULL is returned.
1898 * The engine state is unmodified by this call.
1900 * br_ssl_engine_xxx_ack
1901 * Informs the engine that 'len' bytes have been read from the buffer
1902 * (extract operation) or written to the buffer (inject operation).
1903 * The 'len' value MUST NOT be zero. The 'len' value MUST NOT exceed
1904 * that which was obtained from a preceeding br_ssl_engine_xxx_buf()
1909 * \brief Get buffer for application data to send.
1911 * If the engine is ready to accept application data to send to the
1912 * peer, then this call returns a pointer to the buffer where such
1913 * data shall be written, and its length is written in `*len`.
1914 * Otherwise, `*len` is set to 0 and `NULL` is returned.
1916 * \param cc SSL engine context.
1917 * \param len receives the application data output buffer length, or 0.
1918 * \return the application data output buffer, or `NULL`.
1920 unsigned char *br_ssl_engine_sendapp_buf(
1921 const br_ssl_engine_context
*cc
, size_t *len
);
1924 * \brief Inform the engine of some new application data.
1926 * After writing `len` bytes in the buffer returned by
1927 * `br_ssl_engine_sendapp_buf()`, the application shall call this
1928 * function to trigger any relevant processing. The `len` parameter
1929 * MUST NOT be 0, and MUST NOT exceed the value obtained in the
1930 * `br_ssl_engine_sendapp_buf()` call.
1932 * \param cc SSL engine context.
1933 * \param len number of bytes pushed (not zero).
1935 void br_ssl_engine_sendapp_ack(br_ssl_engine_context
*cc
, size_t len
);
1938 * \brief Get buffer for received application data.
1940 * If the engine has received application data from the peer, hen this
1941 * call returns a pointer to the buffer from where such data shall be
1942 * read, and its length is written in `*len`. Otherwise, `*len` is set
1943 * to 0 and `NULL` is returned.
1945 * \param cc SSL engine context.
1946 * \param len receives the application data input buffer length, or 0.
1947 * \return the application data input buffer, or `NULL`.
1949 unsigned char *br_ssl_engine_recvapp_buf(
1950 const br_ssl_engine_context
*cc
, size_t *len
);
1953 * \brief Acknowledge some received application data.
1955 * After reading `len` bytes from the buffer returned by
1956 * `br_ssl_engine_recvapp_buf()`, the application shall call this
1957 * function to trigger any relevant processing. The `len` parameter
1958 * MUST NOT be 0, and MUST NOT exceed the value obtained in the
1959 * `br_ssl_engine_recvapp_buf()` call.
1961 * \param cc SSL engine context.
1962 * \param len number of bytes read (not zero).
1964 void br_ssl_engine_recvapp_ack(br_ssl_engine_context
*cc
, size_t len
);
1967 * \brief Get buffer for record data to send.
1969 * If the engine has prepared some records to send to the peer, then this
1970 * call returns a pointer to the buffer from where such data shall be
1971 * read, and its length is written in `*len`. Otherwise, `*len` is set
1972 * to 0 and `NULL` is returned.
1974 * \param cc SSL engine context.
1975 * \param len receives the record data output buffer length, or 0.
1976 * \return the record data output buffer, or `NULL`.
1978 unsigned char *br_ssl_engine_sendrec_buf(
1979 const br_ssl_engine_context
*cc
, size_t *len
);
1982 * \brief Acknowledge some sent record data.
1984 * After reading `len` bytes from the buffer returned by
1985 * `br_ssl_engine_sendrec_buf()`, the application shall call this
1986 * function to trigger any relevant processing. The `len` parameter
1987 * MUST NOT be 0, and MUST NOT exceed the value obtained in the
1988 * `br_ssl_engine_sendrec_buf()` call.
1990 * \param cc SSL engine context.
1991 * \param len number of bytes read (not zero).
1993 void br_ssl_engine_sendrec_ack(br_ssl_engine_context
*cc
, size_t len
);
1996 * \brief Get buffer for incoming records.
1998 * If the engine is ready to accept records from the peer, then this
1999 * call returns a pointer to the buffer where such data shall be
2000 * written, and its length is written in `*len`. Otherwise, `*len` is
2001 * set to 0 and `NULL` is returned.
2003 * \param cc SSL engine context.
2004 * \param len receives the record data input buffer length, or 0.
2005 * \return the record data input buffer, or `NULL`.
2007 unsigned char *br_ssl_engine_recvrec_buf(
2008 const br_ssl_engine_context
*cc
, size_t *len
);
2011 * \brief Inform the engine of some new record data.
2013 * After writing `len` bytes in the buffer returned by
2014 * `br_ssl_engine_recvrec_buf()`, the application shall call this
2015 * function to trigger any relevant processing. The `len` parameter
2016 * MUST NOT be 0, and MUST NOT exceed the value obtained in the
2017 * `br_ssl_engine_recvrec_buf()` call.
2019 * \param cc SSL engine context.
2020 * \param len number of bytes pushed (not zero).
2022 void br_ssl_engine_recvrec_ack(br_ssl_engine_context
*cc
, size_t len
);
2025 * \brief Flush buffered application data.
2027 * If some application data has been buffered in the engine, then wrap
2028 * it into a record and mark it for sending. If no application data has
2029 * been buffered but the engine would be ready to accept some, AND the
2030 * `force` parameter is non-zero, then an empty record is assembled and
2031 * marked for sending. In all other cases, this function does nothing.
2033 * Empty records are technically legal, but not all existing SSL/TLS
2034 * implementations support them. Empty records can be useful as a
2035 * transparent "keep-alive" mechanism to maintain some low-level
2038 * \param cc SSL engine context.
2039 * \param force non-zero to force sending an empty record.
2041 void br_ssl_engine_flush(br_ssl_engine_context
*cc
, int force
);
2044 * \brief Initiate a closure.
2046 * If, at that point, the context is open and in ready state, then a
2047 * `close_notify` alert is assembled and marked for sending; this
2048 * triggers the closure protocol. Otherwise, no such alert is assembled.
2050 * \param cc SSL engine context.
2052 void br_ssl_engine_close(br_ssl_engine_context
*cc
);
2055 * \brief Initiate a renegotiation.
2057 * If the engine is failed or closed, or if the peer is known not to
2058 * support secure renegotiation (RFC 5746), or if renegotiations have
2059 * been disabled with the `BR_OPT_NO_RENEGOTIATION` flag, then this
2060 * function returns 0 and nothing else happens.
2062 * Otherwise, this function returns 1, and a renegotiation attempt is
2063 * triggered (if a handshake is already ongoing at that point, then
2064 * no new handshake is triggered).
2066 * \param cc SSL engine context.
2067 * \return 1 on success, 0 on error.
2069 int br_ssl_engine_renegotiate(br_ssl_engine_context
*cc
);
2072 * Pre-declaration for the SSL client context.
2074 typedef struct br_ssl_client_context_ br_ssl_client_context
;
2077 * \brief Type for the client certificate, if requested by the server.
2081 * \brief Authentication type.
2083 * This is either `BR_AUTH_RSA` (RSA signature), `BR_AUTH_ECDSA`
2084 * (ECDSA signature), or `BR_AUTH_ECDH` (static ECDH key exchange).
2089 * \brief Hash function for computing the CertificateVerify.
2091 * This is the symbolic identifier for the hash function that
2092 * will be used to produce the hash of handshake messages, to
2093 * be signed into the CertificateVerify. For full static ECDH
2094 * (client and server certificates are both EC in the same
2095 * curve, and static ECDH is used), this value is set to -1.
2097 * Take care that with TLS 1.0 and 1.1, that value MUST match
2098 * the protocol requirements: value must be 0 (MD5+SHA-1) for
2099 * a RSA signature, or 2 (SHA-1) for an ECDSA signature. Only
2100 * TLS 1.2 allows for other hash functions.
2105 * \brief Certificate chain to send to the server.
2107 * This is an array of `br_x509_certificate` objects, each
2108 * normally containing a DER-encoded certificate. The client
2109 * code does not try to decode these elements. If there is no
2110 * chain to send to the server, then this pointer shall be
2113 const br_x509_certificate
*chain
;
2116 * \brief Certificate chain length (number of certificates).
2118 * If there is no chain to send to the server, then this value
2119 * shall be set to 0.
2123 } br_ssl_client_certificate
;
2126 * Note: the constants below for signatures match the TLS constants.
2129 /** \brief Client authentication type: static ECDH. */
2130 #define BR_AUTH_ECDH 0
2131 /** \brief Client authentication type: RSA signature. */
2132 #define BR_AUTH_RSA 1
2133 /** \brief Client authentication type: ECDSA signature. */
2134 #define BR_AUTH_ECDSA 3
2137 * \brief Class type for a certificate handler (client side).
2139 * A certificate handler selects a client certificate chain to send to
2140 * the server, upon explicit request from that server. It receives
2141 * the list of trust anchor DN from the server, and supported types
2142 * of certificates and signatures, and returns the chain to use. It
2143 * is also invoked to perform the corresponding private key operation
2144 * (a signature, or an ECDH computation).
2146 * The SSL client engine will first push the trust anchor DN with
2147 * `start_name_list()`, `start_name()`, `append_name()`, `end_name()`
2148 * and `end_name_list()`. Then it will call `choose()`, to select the
2149 * actual chain (and signature/hash algorithms). Finally, it will call
2150 * either `do_sign()` or `do_keyx()`, depending on the algorithm choices.
2152 typedef struct br_ssl_client_certificate_class_ br_ssl_client_certificate_class
;
2153 struct br_ssl_client_certificate_class_
{
2155 * \brief Context size (in bytes).
2157 size_t context_size
;
2160 * \brief Begin reception of a list of trust anchor names. This
2161 * is called while parsing the incoming CertificateRequest.
2163 * \param pctx certificate handler context.
2165 void (*start_name_list
)(const br_ssl_client_certificate_class
**pctx
);
2168 * \brief Begin reception of a new trust anchor name.
2170 * The total encoded name length is provided; it is less than
2173 * \param pctx certificate handler context.
2174 * \param len encoded name length (in bytes).
2176 void (*start_name
)(const br_ssl_client_certificate_class
**pctx
,
2180 * \brief Receive some more bytes for the current trust anchor name.
2182 * The provided reference (`data`) points to a transient buffer
2183 * they may be reused as soon as this function returns. The chunk
2184 * length (`len`) is never zero.
2186 * \param pctx certificate handler context.
2187 * \param data anchor name chunk.
2188 * \param len anchor name chunk length (in bytes).
2190 void (*append_name
)(const br_ssl_client_certificate_class
**pctx
,
2191 const unsigned char *data
, size_t len
);
2194 * \brief End current trust anchor name.
2196 * This function is called when all the encoded anchor name data
2197 * has been provided.
2199 * \param pctx certificate handler context.
2201 void (*end_name
)(const br_ssl_client_certificate_class
**pctx
);
2204 * \brief End list of trust anchor names.
2206 * This function is called when all the anchor names in the
2207 * CertificateRequest message have been obtained.
2209 * \param pctx certificate handler context.
2211 void (*end_name_list
)(const br_ssl_client_certificate_class
**pctx
);
2214 * \brief Select client certificate and algorithms.
2216 * This callback function shall fill the provided `choices`
2217 * structure with the selected algorithms and certificate chain.
2218 * The `hash_id`, `chain` and `chain_len` fields must be set. If
2219 * the client cannot or does not wish to send a certificate,
2220 * then it shall set `chain` to `NULL` and `chain_len` to 0.
2222 * The `auth_types` parameter describes the authentication types,
2223 * signature algorithms and hash functions that are supported by
2224 * both the client context and the server, and compatible with
2225 * the current protocol version. This is a bit field with the
2226 * following contents:
2228 * - If RSA signatures with hash function x are supported, then
2231 * - If ECDSA signatures with hash function x are supported,
2232 * then bit 8+x is set.
2234 * - If static ECDH is supported, with a RSA-signed certificate,
2235 * then bit 16 is set.
2237 * - If static ECDH is supported, with an ECDSA-signed certificate,
2238 * then bit 17 is set.
2242 * - When using TLS 1.0 or 1.1, the hash function for RSA
2243 * signatures is always the special MD5+SHA-1 (id 0), and the
2244 * hash function for ECDSA signatures is always SHA-1 (id 2).
2246 * - When using TLS 1.2, the list of hash functions is trimmed
2247 * down to include only hash functions that the client context
2248 * can support. The actual server list can be obtained with
2249 * `br_ssl_client_get_server_hashes()`; that list may be used
2250 * to select the certificate chain to send to the server.
2252 * \param pctx certificate handler context.
2253 * \param cc SSL client context.
2254 * \param auth_types supported authentication types and algorithms.
2255 * \param choices destination structure for the policy choices.
2257 void (*choose
)(const br_ssl_client_certificate_class
**pctx
,
2258 const br_ssl_client_context
*cc
, uint32_t auth_types
,
2259 br_ssl_client_certificate
*choices
);
2262 * \brief Perform key exchange (client part).
2264 * This callback is invoked in case of a full static ECDH key
2267 * - the cipher suite uses `ECDH_RSA` or `ECDH_ECDSA`;
2269 * - the server requests a client certificate;
2271 * - the client has, and sends, a client certificate that
2272 * uses an EC key in the same curve as the server's key,
2273 * and chooses static ECDH (the `hash_id` field in the choice
2274 * structure was set to -1).
2276 * In that situation, this callback is invoked to compute the
2277 * client-side ECDH: the provided `data` (of length `*len` bytes)
2278 * is the server's public key point (as decoded from its
2279 * certificate), and the client shall multiply that point with
2280 * its own private key, and write back the X coordinate of the
2281 * resulting point in the same buffer, starting at offset 0.
2282 * The `*len` value shall be modified to designate the actual
2283 * length of the X coordinate.
2285 * The callback must uphold the following:
2287 * - If the input array does not have the proper length for
2288 * an encoded curve point, then an error (0) shall be reported.
2290 * - If the input array has the proper length, then processing
2291 * MUST be constant-time, even if the data is not a valid
2294 * - This callback MUST check that the input point is valid.
2296 * Returned value is 1 on success, 0 on error.
2298 * \param pctx certificate handler context.
2299 * \param data server public key point.
2300 * \param len public key point length / X coordinate length.
2301 * \return 1 on success, 0 on error.
2303 uint32_t (*do_keyx
)(const br_ssl_client_certificate_class
**pctx
,
2304 unsigned char *data
, size_t *len
);
2307 * \brief Perform a signature (client authentication).
2309 * This callback is invoked when a client certificate was sent,
2310 * and static ECDH is not used. It shall compute a signature,
2311 * using the client's private key, over the provided hash value
2312 * (which is the hash of all previous handshake messages).
2314 * On input, the hash value to sign is in `data`, of size
2315 * `hv_len`; the involved hash function is identified by
2316 * `hash_id`. The signature shall be computed and written
2317 * back into `data`; the total size of that buffer is `len`
2320 * This callback shall verify that the signature length does not
2321 * exceed `len` bytes, and abstain from writing the signature if
2324 * For RSA signatures, the `hash_id` may be 0, in which case
2325 * this is the special header-less signature specified in TLS 1.0
2326 * and 1.1, with a 36-byte hash value. Otherwise, normal PKCS#1
2327 * v1.5 signatures shall be computed.
2329 * For ECDSA signatures, the signature value shall use the ASN.1
2332 * Returned value is the signature length (in bytes), or 0 on error.
2334 * \param pctx certificate handler context.
2335 * \param hash_id hash function identifier.
2336 * \param hv_len hash value length (in bytes).
2337 * \param data input/output buffer (hash value, then signature).
2338 * \param len total buffer length (in bytes).
2339 * \return signature length (in bytes) on success, or 0 on error.
2341 size_t (*do_sign
)(const br_ssl_client_certificate_class
**pctx
,
2342 int hash_id
, size_t hv_len
, unsigned char *data
, size_t len
);
2346 * \brief A single-chain RSA client certificate handler.
2348 * This handler uses a single certificate chain, with a RSA
2349 * signature. The list of trust anchor DN is ignored.
2351 * Apart from the first field (vtable pointer), its contents are
2352 * opaque and shall not be accessed directly.
2355 /** \brief Pointer to vtable. */
2356 const br_ssl_client_certificate_class
*vtable
;
2357 #ifndef BR_DOXYGEN_IGNORE
2358 const br_x509_certificate
*chain
;
2360 const br_rsa_private_key
*sk
;
2361 br_rsa_pkcs1_sign irsasign
;
2363 } br_ssl_client_certificate_rsa_context
;
2366 * \brief A single-chain EC client certificate handler.
2368 * This handler uses a single certificate chain, with a RSA
2369 * signature. The list of trust anchor DN is ignored.
2371 * This handler may support both static ECDH, and ECDSA signatures
2372 * (either usage may be selectively disabled).
2374 * Apart from the first field (vtable pointer), its contents are
2375 * opaque and shall not be accessed directly.
2378 /** \brief Pointer to vtable. */
2379 const br_ssl_client_certificate_class
*vtable
;
2380 #ifndef BR_DOXYGEN_IGNORE
2381 const br_x509_certificate
*chain
;
2383 const br_ec_private_key
*sk
;
2384 unsigned allowed_usages
;
2385 unsigned issuer_key_type
;
2386 const br_multihash_context
*mhash
;
2387 const br_ec_impl
*iec
;
2388 br_ecdsa_sign iecdsa
;
2390 } br_ssl_client_certificate_ec_context
;
2393 * \brief Context structure for a SSL client.
2395 * The first field (called `eng`) is the SSL engine; all functions that
2396 * work on a `br_ssl_engine_context` structure shall take as parameter
2397 * a pointer to that field. The other structure fields are opaque and
2398 * must not be accessed directly.
2400 struct br_ssl_client_context_
{
2402 * \brief The encapsulated engine context.
2404 br_ssl_engine_context eng
;
2406 #ifndef BR_DOXYGEN_IGNORE
2408 * Minimum ClientHello length; padding with an extension (RFC
2409 * 7685) is added if necessary to match at least that length.
2410 * Such padding is nominally unnecessary, but it has been used
2411 * to work around some server implementation bugs.
2413 uint16_t min_clienthello_len
;
2416 * Bit field for algoithms (hash + signature) supported by the
2417 * server when requesting a client certificate.
2422 * Server's public key curve.
2427 * Context for certificate handler.
2429 const br_ssl_client_certificate_class
**client_auth_vtable
;
2432 * Client authentication type.
2434 unsigned char auth_type
;
2437 * Hash function to use for the client signature. This is 0xFF
2438 * if static ECDH is used.
2440 unsigned char hash_id
;
2443 * For the core certificate handlers, thus avoiding (in most
2444 * cases) the need for an externally provided policy context.
2447 const br_ssl_client_certificate_class
*vtable
;
2448 br_ssl_client_certificate_rsa_context single_rsa
;
2449 br_ssl_client_certificate_ec_context single_ec
;
2455 br_rsa_public irsapub
;
2460 * \brief Get the hash functions and signature algorithms supported by
2463 * This value is a bit field:
2465 * - If RSA (PKCS#1 v1.5) is supported with hash function of ID `x`,
2466 * then bit `x` is set (hash function ID is 0 for the special MD5+SHA-1,
2467 * or 2 to 6 for the SHA family).
2469 * - If ECDSA is suported with hash function of ID `x`, then bit `8+x`
2472 * - Newer algorithms are symbolic 16-bit identifiers that do not
2473 * represent signature algorithm and hash function separately. If
2474 * the TLS-level identifier is `0x0800+x` for a `x` in the 0..15
2475 * range, then bit `16+x` is set.
2477 * "New algorithms" are currently defined only in draft documents, so
2478 * this support is subject to possible change. Right now (early 2017),
2479 * this maps ed25519 (EdDSA on Curve25519) to bit 23, and ed448 (EdDSA
2480 * on Curve448) to bit 24. If the identifiers on the wire change in
2481 * future document, then the decoding mechanism in BearSSL will be
2482 * amended to keep mapping ed25519 and ed448 on bits 23 and 24,
2483 * respectively. Mapping of other new algorithms (e.g. RSA/PSS) is not
2486 * \param cc client context.
2487 * \return the server-supported hash functions and signature algorithms.
2489 static inline uint32_t
2490 br_ssl_client_get_server_hashes(const br_ssl_client_context
*cc
)
2496 * \brief Get the server key curve.
2498 * This function returns the ID for the curve used by the server's public
2499 * key. This is set when the server's certificate chain is processed;
2500 * this value is 0 if the server's key is not an EC key.
2502 * \return the server's public key curve ID, or 0.
2505 br_ssl_client_get_server_curve(const br_ssl_client_context
*cc
)
2507 return cc
->server_curve
;
2511 * Each br_ssl_client_init_xxx() function sets the list of supported
2512 * cipher suites and used implementations, as specified by the profile
2513 * name 'xxx'. Defined profile names are:
2515 * full all supported versions and suites; constant-time implementations
2516 * TODO: add other profiles
2520 * \brief SSL client profile: full.
2522 * This function initialises the provided SSL client context with
2523 * all supported algorithms and cipher suites. It also initialises
2524 * a companion X.509 validation engine with all supported algorithms,
2525 * and the provided trust anchors; the X.509 engine will be used by
2526 * the client context to validate the server's certificate.
2528 * \param cc client context to initialise.
2529 * \param xc X.509 validation context to initialise.
2530 * \param trust_anchors trust anchors to use.
2531 * \param trust_anchors_num number of trust anchors.
2533 void br_ssl_client_init_full(br_ssl_client_context
*cc
,
2534 br_x509_minimal_context
*xc
,
2535 const br_x509_trust_anchor
*trust_anchors
, size_t trust_anchors_num
);
2538 * \brief Clear the complete contents of a SSL client context.
2540 * Everything is cleared, including the reference to the configured buffer,
2541 * implementations, cipher suites and state. This is a preparatory step
2542 * to assembling a custom profile.
2544 * \param cc client context to clear.
2546 void br_ssl_client_zero(br_ssl_client_context
*cc
);
2549 * \brief Set an externally provided client certificate handler context.
2551 * The handler's methods are invoked when the server requests a client
2554 * \param cc client context.
2555 * \param pctx certificate handler context (pointer to its vtable field).
2558 br_ssl_client_set_client_certificate(br_ssl_client_context
*cc
,
2559 const br_ssl_client_certificate_class
**pctx
)
2561 cc
->client_auth_vtable
= pctx
;
2565 * \brief Set the RSA public-key operations implementation.
2567 * This will be used to encrypt the pre-master secret with the server's
2568 * RSA public key (RSA-encryption cipher suites only).
2570 * \param cc client context.
2571 * \param irsapub RSA public-key encryption implementation.
2574 br_ssl_client_set_rsapub(br_ssl_client_context
*cc
, br_rsa_public irsapub
)
2576 cc
->irsapub
= irsapub
;
2580 * \brief Set the "default" RSA implementation for public-key operations.
2582 * This sets the RSA implementation in the client context (for encrypting
2583 * the pre-master secret, in `TLS_RSA_*` cipher suites) to the fastest
2584 * available on the current platform.
2586 * \param cc client context.
2588 void br_ssl_client_set_default_rsapub(br_ssl_client_context
*cc
);
2591 * \brief Set the minimum ClientHello length (RFC 7685 padding).
2593 * If this value is set and the ClientHello would be shorter, then
2594 * the Pad ClientHello extension will be added with enough padding bytes
2595 * to reach the target size. Because of the extension header, the resulting
2596 * size will sometimes be slightly more than `len` bytes if the target
2597 * size cannot be exactly met.
2599 * The target length relates to the _contents_ of the ClientHello, not
2600 * counting its 4-byte header. For instance, if `len` is set to 512,
2601 * then the padding will bring the ClientHello size to 516 bytes with its
2602 * header, and 521 bytes when counting the 5-byte record header.
2604 * \param cc client context.
2605 * \param len minimum ClientHello length (in bytes).
2608 br_ssl_client_set_min_clienthello_len(br_ssl_client_context
*cc
, uint16_t len
)
2610 cc
->min_clienthello_len
= len
;
2614 * \brief Prepare or reset a client context for a new connection.
2616 * The `server_name` parameter is used to fill the SNI extension; the
2617 * X.509 "minimal" engine will also match that name against the server
2618 * names included in the server's certificate. If the parameter is
2619 * `NULL` then no SNI extension will be sent, and the X.509 "minimal"
2620 * engine (if used for server certificate validation) will not check
2621 * presence of any specific name in the received certificate.
2623 * Therefore, setting the `server_name` to `NULL` shall be reserved
2624 * to cases where alternate or additional methods are used to ascertain
2625 * that the right server public key is used (e.g. a "known key" model).
2627 * If `resume_session` is non-zero and the context was previously used
2628 * then the session parameters may be reused (depending on whether the
2629 * server previously sent a non-empty session ID, and accepts the session
2630 * resumption). The session parameters for session resumption can also
2631 * be set explicitly with `br_ssl_engine_set_session_parameters()`.
2633 * On failure, the context is marked as failed, and this function
2634 * returns 0. A possible failure condition is when no initial entropy
2635 * was injected, and none could be obtained from the OS (either OS
2636 * randomness gathering is not supported, or it failed).
2638 * \param cc client context.
2639 * \param server_name target server name, or `NULL`.
2640 * \param resume_session non-zero to try session resumption.
2641 * \return 0 on failure, 1 on success.
2643 int br_ssl_client_reset(br_ssl_client_context
*cc
,
2644 const char *server_name
, int resume_session
);
2647 * \brief Forget any session in the context.
2649 * This means that the next handshake that uses this context will
2650 * necessarily be a full handshake (this applies both to new connections
2651 * and to renegotiations).
2653 * \param cc client context.
2656 br_ssl_client_forget_session(br_ssl_client_context
*cc
)
2658 cc
->eng
.session
.session_id_len
= 0;
2662 * \brief Set client certificate chain and key (single RSA case).
2664 * This function sets a client certificate chain, that the client will
2665 * send to the server whenever a client certificate is requested. This
2666 * certificate uses an RSA public key; the corresponding private key is
2667 * invoked for authentication. Trust anchor names sent by the server are
2670 * The provided chain and private key are linked in the client context;
2671 * they must remain valid as long as they may be used, i.e. normally
2672 * for the duration of the connection, since they might be invoked
2673 * again upon renegotiations.
2675 * \param cc SSL client context.
2676 * \param chain client certificate chain (SSL order: EE comes first).
2677 * \param chain_len client chain length (number of certificates).
2678 * \param sk client private key.
2679 * \param irsasign RSA signature implementation (PKCS#1 v1.5).
2681 void br_ssl_client_set_single_rsa(br_ssl_client_context
*cc
,
2682 const br_x509_certificate
*chain
, size_t chain_len
,
2683 const br_rsa_private_key
*sk
, br_rsa_pkcs1_sign irsasign
);
2686 * \brief Set the client certificate chain and key (single EC case).
2688 * This function sets a client certificate chain, that the client will
2689 * send to the server whenever a client certificate is requested. This
2690 * certificate uses an EC public key; the corresponding private key is
2691 * invoked for authentication. Trust anchor names sent by the server are
2694 * The provided chain and private key are linked in the client context;
2695 * they must remain valid as long as they may be used, i.e. normally
2696 * for the duration of the connection, since they might be invoked
2697 * again upon renegotiations.
2699 * The `allowed_usages` is a combination of usages, namely
2700 * `BR_KEYTYPE_KEYX` and/or `BR_KEYTYPE_SIGN`. The `BR_KEYTYPE_KEYX`
2701 * value allows full static ECDH, while the `BR_KEYTYPE_SIGN` value
2702 * allows ECDSA signatures. If ECDSA signatures are used, then an ECDSA
2703 * signature implementation must be provided; otherwise, the `iecdsa`
2704 * parameter may be 0.
2706 * The `cert_issuer_key_type` value is either `BR_KEYTYPE_RSA` or
2707 * `BR_KEYTYPE_EC`; it is the type of the public key used the the CA
2708 * that issued (signed) the client certificate. That value is used with
2709 * full static ECDH: support of the certificate by the server depends
2710 * on how the certificate was signed. (Note: when using TLS 1.2, this
2711 * parameter is ignored; but its value matters for TLS 1.0 and 1.1.)
2713 * \param cc server context.
2714 * \param chain server certificate chain to send.
2715 * \param chain_len chain length (number of certificates).
2716 * \param sk server private key (EC).
2717 * \param allowed_usages allowed private key usages.
2718 * \param cert_issuer_key_type issuing CA's key type.
2719 * \param iec EC core implementation.
2720 * \param iecdsa ECDSA signature implementation ("asn1" format).
2722 void br_ssl_client_set_single_ec(br_ssl_client_context
*cc
,
2723 const br_x509_certificate
*chain
, size_t chain_len
,
2724 const br_ec_private_key
*sk
, unsigned allowed_usages
,
2725 unsigned cert_issuer_key_type
,
2726 const br_ec_impl
*iec
, br_ecdsa_sign iecdsa
);
2729 * \brief Type for a "translated cipher suite", as an array of two
2732 * The first element is the cipher suite identifier (as used on the wire).
2733 * The second element is the concatenation of four 4-bit elements which
2734 * characterise the cipher suite contents. In most to least significant
2735 * order, these 4-bit elements are:
2737 * - Bits 12 to 15: key exchange + server key type
2739 * | val | symbolic constant | suite type | details |
2740 * | :-- | :----------------------- | :---------- | :----------------------------------------------- |
2741 * | 0 | `BR_SSLKEYX_RSA` | RSA | RSA key exchange, key is RSA (encryption) |
2742 * | 1 | `BR_SSLKEYX_ECDHE_RSA` | ECDHE_RSA | ECDHE key exchange, key is RSA (signature) |
2743 * | 2 | `BR_SSLKEYX_ECDHE_ECDSA` | ECDHE_ECDSA | ECDHE key exchange, key is EC (signature) |
2744 * | 3 | `BR_SSLKEYX_ECDH_RSA` | ECDH_RSA | Key is EC (key exchange), cert signed with RSA |
2745 * | 4 | `BR_SSLKEYX_ECDH_ECDSA` | ECDH_ECDSA | Key is EC (key exchange), cert signed with ECDSA |
2747 * - Bits 8 to 11: symmetric encryption algorithm
2749 * | val | symbolic constant | symmetric encryption | key strength (bits) |
2750 * | :-- | :--------------------- | :------------------- | :------------------ |
2751 * | 0 | `BR_SSLENC_3DES_CBC` | 3DES/CBC | 168 |
2752 * | 1 | `BR_SSLENC_AES128_CBC` | AES-128/CBC | 128 |
2753 * | 2 | `BR_SSLENC_AES256_CBC` | AES-256/CBC | 256 |
2754 * | 3 | `BR_SSLENC_AES128_GCM` | AES-128/GCM | 128 |
2755 * | 4 | `BR_SSLENC_AES256_GCM` | AES-256/GCM | 256 |
2756 * | 5 | `BR_SSLENC_CHACHA20` | ChaCha20/Poly1305 | 256 |
2758 * - Bits 4 to 7: MAC algorithm
2760 * | val | symbolic constant | MAC type | details |
2761 * | :-- | :----------------- | :----------- | :------------------------------------ |
2762 * | 0 | `BR_SSLMAC_AEAD` | AEAD | No dedicated MAC (encryption is AEAD) |
2763 * | 2 | `BR_SSLMAC_SHA1` | HMAC/SHA-1 | Value matches `br_sha1_ID` |
2764 * | 4 | `BR_SSLMAC_SHA256` | HMAC/SHA-256 | Value matches `br_sha256_ID` |
2765 * | 5 | `BR_SSLMAC_SHA384` | HMAC/SHA-384 | Value matches `br_sha384_ID` |
2767 * - Bits 0 to 3: hash function for PRF when used with TLS-1.2
2769 * | val | symbolic constant | hash function | details |
2770 * | :-- | :----------------- | :------------ | :----------------------------------- |
2771 * | 4 | `BR_SSLPRF_SHA256` | SHA-256 | Value matches `br_sha256_ID` |
2772 * | 5 | `BR_SSLPRF_SHA384` | SHA-384 | Value matches `br_sha384_ID` |
2774 * For instance, cipher suite `TLS_RSA_WITH_AES_128_GCM_SHA256` has
2775 * standard identifier 0x009C, and is translated to 0x0304, for, in
2776 * that order: RSA key exchange (0), AES-128/GCM (3), AEAD integrity (0),
2777 * SHA-256 in the TLS PRF (4).
2779 typedef uint16_t br_suite_translated
[2];
2781 #ifndef BR_DOXYGEN_IGNORE
2783 * Constants are already documented in the br_suite_translated type.
2786 #define BR_SSLKEYX_RSA 0
2787 #define BR_SSLKEYX_ECDHE_RSA 1
2788 #define BR_SSLKEYX_ECDHE_ECDSA 2
2789 #define BR_SSLKEYX_ECDH_RSA 3
2790 #define BR_SSLKEYX_ECDH_ECDSA 4
2792 #define BR_SSLENC_3DES_CBC 0
2793 #define BR_SSLENC_AES128_CBC 1
2794 #define BR_SSLENC_AES256_CBC 2
2795 #define BR_SSLENC_AES128_GCM 3
2796 #define BR_SSLENC_AES256_GCM 4
2797 #define BR_SSLENC_CHACHA20 5
2799 #define BR_SSLMAC_AEAD 0
2800 #define BR_SSLMAC_SHA1 br_sha1_ID
2801 #define BR_SSLMAC_SHA256 br_sha256_ID
2802 #define BR_SSLMAC_SHA384 br_sha384_ID
2804 #define BR_SSLPRF_SHA256 br_sha256_ID
2805 #define BR_SSLPRF_SHA384 br_sha384_ID
2810 * Pre-declaration for the SSL server context.
2812 typedef struct br_ssl_server_context_ br_ssl_server_context
;
2815 * \brief Type for the server policy choices, taken after analysis of
2816 * the client message (ClientHello).
2820 * \brief Cipher suite to use with that client.
2822 uint16_t cipher_suite
;
2825 * \brief Hash function or algorithm for signing the ServerKeyExchange.
2827 * This parameter is ignored for `TLS_RSA_*` and `TLS_ECDH_*`
2828 * cipher suites; it is used only for `TLS_ECDHE_*` suites, in
2829 * which the server _signs_ the ephemeral EC Diffie-Hellman
2830 * parameters sent to the client.
2832 * This identifier must be one of the following values:
2834 * - `0xFF00 + id`, where `id` is a hash function identifier
2835 * (0 for MD5+SHA-1, or 2 to 6 for one of the SHA functions);
2837 * - a full 16-bit identifier, lower than `0xFF00`.
2839 * If the first option is used, then the SSL engine will
2840 * compute the hash of the data that is to be signed, with the
2841 * designated hash function. The `do_sign()` method will be
2842 * invoked with that hash value provided in the the `data`
2845 * If the second option is used, then the SSL engine will NOT
2846 * compute a hash on the data; instead, it will provide the
2847 * to-be-signed data itself in `data`, i.e. the concatenation of
2848 * the client random, server random, and encoded ECDH
2849 * parameters. Furthermore, with TLS-1.2 and later, the 16-bit
2850 * identifier will be used "as is" in the protocol, in the
2851 * SignatureAndHashAlgorithm; for instance, `0x0401` stands for
2852 * RSA PKCS#1 v1.5 signature (the `01`) with SHA-256 as hash
2853 * function (the `04`).
2855 * Take care that with TLS 1.0 and 1.1, the hash function is
2856 * constrainted by the protocol: RSA signature must use
2857 * MD5+SHA-1 (so use `0xFF00`), while ECDSA must use SHA-1
2858 * (`0xFF02`). Since TLS 1.0 and 1.1 don't include a
2859 * SignatureAndHashAlgorithm field in their ServerKeyExchange
2860 * messages, any value below `0xFF00` will be usable to send the
2861 * raw ServerKeyExchange data to the `do_sign()` callback, but
2862 * that callback must still follow the protocol requirements
2863 * when generating the signature.
2868 * \brief Certificate chain to send to the client.
2870 * This is an array of `br_x509_certificate` objects, each
2871 * normally containing a DER-encoded certificate. The server
2872 * code does not try to decode these elements.
2874 const br_x509_certificate
*chain
;
2877 * \brief Certificate chain length (number of certificates).
2881 } br_ssl_server_choices
;
2884 * \brief Class type for a policy handler (server side).
2886 * A policy handler selects the policy parameters for a connection
2887 * (cipher suite and other algorithms, and certificate chain to send to
2888 * the client); it also performs the server-side computations involving
2889 * its permanent private key.
2891 * The SSL server engine will invoke first `choose()`, once the
2892 * ClientHello message has been received, then either `do_keyx()`
2893 * `do_sign()`, depending on the cipher suite.
2895 typedef struct br_ssl_server_policy_class_ br_ssl_server_policy_class
;
2896 struct br_ssl_server_policy_class_
{
2898 * \brief Context size (in bytes).
2900 size_t context_size
;
2903 * \brief Select algorithms and certificates for this connection.
2905 * This callback function shall fill the provided `choices`
2906 * structure with the policy choices for this connection. This
2907 * entails selecting the cipher suite, hash function for signing
2908 * the ServerKeyExchange (applicable only to ECDHE cipher suites),
2909 * and certificate chain to send.
2911 * The callback receives a pointer to the server context that
2912 * contains the relevant data. In particular, the functions
2913 * `br_ssl_server_get_client_suites()`,
2914 * `br_ssl_server_get_client_hashes()` and
2915 * `br_ssl_server_get_client_curves()` can be used to obtain
2916 * the cipher suites, hash functions and elliptic curves
2917 * supported by both the client and server, respectively. The
2918 * `br_ssl_engine_get_version()` and `br_ssl_engine_get_server_name()`
2919 * functions yield the protocol version and requested server name
2920 * (SNI), respectively.
2922 * This function may modify its context structure (`pctx`) in
2923 * arbitrary ways to keep track of its own choices.
2925 * This function shall return 1 if appropriate policy choices
2926 * could be made, or 0 if this connection cannot be pursued.
2928 * \param pctx policy context.
2929 * \param cc SSL server context.
2930 * \param choices destination structure for the policy choices.
2931 * \return 1 on success, 0 on error.
2933 int (*choose
)(const br_ssl_server_policy_class
**pctx
,
2934 const br_ssl_server_context
*cc
,
2935 br_ssl_server_choices
*choices
);
2938 * \brief Perform key exchange (server part).
2940 * This callback is invoked to perform the server-side cryptographic
2941 * operation for a key exchange that is not ECDHE. This callback
2942 * uses the private key.
2944 * **For RSA key exchange**, the provided `data` (of length `*len`
2945 * bytes) shall be decrypted with the server's private key, and
2946 * the 48-byte premaster secret copied back to the first 48 bytes
2949 * - The caller makes sure that `*len` is at least 59 bytes.
2951 * - This callback MUST check that the provided length matches
2952 * that of the key modulus; it shall report an error otherwise.
2954 * - If the length matches that of the RSA key modulus, then
2955 * processing MUST be constant-time, even if decryption fails,
2956 * or the padding is incorrect, or the plaintext message length
2957 * is not exactly 48 bytes.
2959 * - This callback needs not check the two first bytes of the
2960 * obtained pre-master secret (the caller will do that).
2962 * - If an error is reported (0), then what the callback put
2963 * in the first 48 bytes of `data` is unimportant (the caller
2964 * will use random bytes instead).
2966 * **For ECDH key exchange**, the provided `data` (of length `*len`
2967 * bytes) is the elliptic curve point from the client. The
2968 * callback shall multiply it with its private key, and store
2969 * the resulting X coordinate in `data`, starting at offset 0,
2970 * and set `*len` to the length of the X coordinate.
2972 * - If the input array does not have the proper length for
2973 * an encoded curve point, then an error (0) shall be reported.
2975 * - If the input array has the proper length, then processing
2976 * MUST be constant-time, even if the data is not a valid
2979 * - This callback MUST check that the input point is valid.
2981 * Returned value is 1 on success, 0 on error.
2983 * \param pctx policy context.
2984 * \param data key exchange data from the client.
2985 * \param len key exchange data length (in bytes).
2986 * \return 1 on success, 0 on error.
2988 uint32_t (*do_keyx
)(const br_ssl_server_policy_class
**pctx
,
2989 unsigned char *data
, size_t *len
);
2992 * \brief Perform a signature (for a ServerKeyExchange message).
2994 * This callback function is invoked for ECDHE cipher suites. On
2995 * input, the hash value or message to sign is in `data`, of
2996 * size `hv_len`; the involved hash function or algorithm is
2997 * identified by `algo_id`. The signature shall be computed and
2998 * written back into `data`; the total size of that buffer is
3001 * This callback shall verify that the signature length does not
3002 * exceed `len` bytes, and abstain from writing the signature if
3005 * The `algo_id` value matches that which was written in the
3006 * `choices` structures by the `choose()` callback. This will be
3007 * one of the following:
3009 * - `0xFF00 + id` for a hash function identifier `id`. In
3010 * that case, the `data` buffer contains a hash value
3011 * already computed over the data that is to be signed,
3012 * of length `hv_len`. The `id` may be 0 to designate the
3013 * special MD5+SHA-1 concatenation (old-style RSA signing).
3015 * - Another value, lower than `0xFF00`. The `data` buffer
3016 * then contains the raw, non-hashed data to be signed
3017 * (concatenation of the client and server randoms and
3018 * ECDH parameters). The callback is responsible to apply
3019 * any relevant hashing as part of the signing process.
3021 * Returned value is the signature length (in bytes), or 0 on error.
3023 * \param pctx policy context.
3024 * \param algo_id hash function / algorithm identifier.
3025 * \param data input/output buffer (message/hash, then signature).
3026 * \param hv_len hash value or message length (in bytes).
3027 * \param len total buffer length (in bytes).
3028 * \return signature length (in bytes) on success, or 0 on error.
3030 size_t (*do_sign
)(const br_ssl_server_policy_class
**pctx
,
3032 unsigned char *data
, size_t hv_len
, size_t len
);
3036 * \brief A single-chain RSA policy handler.
3038 * This policy context uses a single certificate chain, and a RSA
3039 * private key. The context can be restricted to only signatures or
3040 * only key exchange.
3042 * Apart from the first field (vtable pointer), its contents are
3043 * opaque and shall not be accessed directly.
3046 /** \brief Pointer to vtable. */
3047 const br_ssl_server_policy_class
*vtable
;
3048 #ifndef BR_DOXYGEN_IGNORE
3049 const br_x509_certificate
*chain
;
3051 const br_rsa_private_key
*sk
;
3052 unsigned allowed_usages
;
3053 br_rsa_private irsacore
;
3054 br_rsa_pkcs1_sign irsasign
;
3056 } br_ssl_server_policy_rsa_context
;
3059 * \brief A single-chain EC policy handler.
3061 * This policy context uses a single certificate chain, and an EC
3062 * private key. The context can be restricted to only signatures or
3063 * only key exchange.
3065 * Due to how TLS is defined, this context must be made aware whether
3066 * the server certificate was itself signed with RSA or ECDSA. The code
3067 * does not try to decode the certificate to obtain that information.
3069 * Apart from the first field (vtable pointer), its contents are
3070 * opaque and shall not be accessed directly.
3073 /** \brief Pointer to vtable. */
3074 const br_ssl_server_policy_class
*vtable
;
3075 #ifndef BR_DOXYGEN_IGNORE
3076 const br_x509_certificate
*chain
;
3078 const br_ec_private_key
*sk
;
3079 unsigned allowed_usages
;
3080 unsigned cert_issuer_key_type
;
3081 const br_multihash_context
*mhash
;
3082 const br_ec_impl
*iec
;
3083 br_ecdsa_sign iecdsa
;
3085 } br_ssl_server_policy_ec_context
;
3088 * \brief Class type for a session parameter cache.
3090 * Session parameters are saved in the cache with `save()`, and
3091 * retrieved with `load()`. The cache implementation can apply any
3092 * storage and eviction strategy that it sees fit. The SSL server
3093 * context that performs the request is provided, so that its
3094 * functionalities may be used by the implementation (e.g. hash
3095 * functions or random number generation).
3097 typedef struct br_ssl_session_cache_class_ br_ssl_session_cache_class
;
3098 struct br_ssl_session_cache_class_
{
3100 * \brief Context size (in bytes).
3102 size_t context_size
;
3105 * \brief Record a session.
3107 * This callback should record the provided session parameters.
3108 * The `params` structure is transient, so its contents shall
3109 * be copied into the cache. The session ID has been randomly
3110 * generated and always has length exactly 32 bytes.
3112 * \param ctx session cache context.
3113 * \param server_ctx SSL server context.
3114 * \param params session parameters to save.
3116 void (*save
)(const br_ssl_session_cache_class
**ctx
,
3117 br_ssl_server_context
*server_ctx
,
3118 const br_ssl_session_parameters
*params
);
3121 * \brief Lookup a session in the cache.
3123 * The session ID to lookup is in `params` and always has length
3124 * exactly 32 bytes. If the session parameters are found in the
3125 * cache, then the parameters shall be copied into the `params`
3126 * structure. Returned value is 1 on successful lookup, 0
3129 * \param ctx session cache context.
3130 * \param server_ctx SSL server context.
3131 * \param params destination for session parameters.
3132 * \return 1 if found, 0 otherwise.
3134 int (*load
)(const br_ssl_session_cache_class
**ctx
,
3135 br_ssl_server_context
*server_ctx
,
3136 br_ssl_session_parameters
*params
);
3140 * \brief Context for a basic cache system.
3142 * The system stores session parameters in a buffer provided at
3143 * initialisation time. Each entry uses exactly 100 bytes, and
3144 * buffer sizes up to 4294967295 bytes are supported.
3146 * Entries are evicted with a LRU (Least Recently Used) policy. A
3147 * search tree is maintained to keep lookups fast even with large
3150 * Apart from the first field (vtable pointer), the structure
3151 * contents are opaque and shall not be accessed directly.
3154 /** \brief Pointer to vtable. */
3155 const br_ssl_session_cache_class
*vtable
;
3156 #ifndef BR_DOXYGEN_IGNORE
3157 unsigned char *store
;
3158 size_t store_len
, store_ptr
;
3159 unsigned char index_key
[32];
3160 const br_hash_class
*hash
;
3162 uint32_t head
, tail
, root
;
3164 } br_ssl_session_cache_lru
;
3167 * \brief Initialise a LRU session cache with the provided storage space.
3169 * The provided storage space must remain valid as long as the cache
3170 * is used. Arbitrary lengths are supported, up to 4294967295 bytes;
3171 * each entry uses up exactly 100 bytes.
3173 * \param cc session cache context.
3174 * \param store storage space for cached entries.
3175 * \param store_len storage space length (in bytes).
3177 void br_ssl_session_cache_lru_init(br_ssl_session_cache_lru
*cc
,
3178 unsigned char *store
, size_t store_len
);
3181 * \brief Context structure for a SSL server.
3183 * The first field (called `eng`) is the SSL engine; all functions that
3184 * work on a `br_ssl_engine_context` structure shall take as parameter
3185 * a pointer to that field. The other structure fields are opaque and
3186 * must not be accessed directly.
3188 struct br_ssl_server_context_
{
3190 * \brief The encapsulated engine context.
3192 br_ssl_engine_context eng
;
3194 #ifndef BR_DOXYGEN_IGNORE
3196 * Maximum version from the client.
3198 uint16_t client_max_version
;
3203 const br_ssl_session_cache_class
**cache_vtable
;
3206 * Translated cipher suites supported by the client. The list
3207 * is trimmed to include only the cipher suites that the
3208 * server also supports; they are in the same order as in the
3211 br_suite_translated client_suites
[BR_MAX_CIPHER_SUITES
];
3212 unsigned char client_suites_num
;
3215 * Hash functions supported by the client, with ECDSA and RSA
3216 * (bit mask). For hash function with id 'x', set bit index is
3217 * x for RSA, x+8 for ECDSA. For newer algorithms, with ID
3218 * 0x08**, bit 16+k is set for algorithm 0x0800+k.
3223 * Curves supported by the client (bit mask, for named curves).
3228 * Context for chain handler.
3230 const br_ssl_server_policy_class
**policy_vtable
;
3231 uint16_t sign_hash_id
;
3234 * For the core handlers, thus avoiding (in most cases) the
3235 * need for an externally provided policy context.
3238 const br_ssl_server_policy_class
*vtable
;
3239 br_ssl_server_policy_rsa_context single_rsa
;
3240 br_ssl_server_policy_ec_context single_ec
;
3244 * Buffer for the ECDHE private key.
3246 unsigned char ecdhe_key
[70];
3247 size_t ecdhe_key_len
;
3250 * Trust anchor names for client authentication. "ta_names" and
3251 * "tas" cannot be both non-NULL.
3253 const br_x500_name
*ta_names
;
3254 const br_x509_trust_anchor
*tas
;
3256 size_t cur_dn_index
;
3257 const unsigned char *cur_dn
;
3261 * Buffer for the hash value computed over all handshake messages
3262 * prior to CertificateVerify, and identifier for the hash function.
3264 unsigned char hash_CV
[64];
3269 * Server-specific implementations.
3276 * Each br_ssl_server_init_xxx() function sets the list of supported
3277 * cipher suites and used implementations, as specified by the profile
3278 * name 'xxx'. Defined profile names are:
3280 * full_rsa all supported algorithm, server key type is RSA
3281 * full_ec all supported algorithm, server key type is EC
3282 * TODO: add other profiles
3284 * Naming scheme for "minimal" profiles: min123
3286 * -- character 1: key exchange
3292 * -- character 2: version / PRF
3293 * 0 = TLS 1.0 / 1.1 with MD5+SHA-1
3294 * 2 = TLS 1.2 with SHA-256
3295 * 3 = TLS 1.2 with SHA-384
3296 * -- character 3: encryption
3300 * c = ChaCha20+Poly1305
3304 * \brief SSL server profile: full_rsa.
3306 * This function initialises the provided SSL server context with
3307 * all supported algorithms and cipher suites that rely on a RSA
3310 * \param cc server context to initialise.
3311 * \param chain server certificate chain.
3312 * \param chain_len certificate chain length (number of certificate).
3313 * \param sk RSA private key.
3315 void br_ssl_server_init_full_rsa(br_ssl_server_context
*cc
,
3316 const br_x509_certificate
*chain
, size_t chain_len
,
3317 const br_rsa_private_key
*sk
);
3320 * \brief SSL server profile: full_ec.
3322 * This function initialises the provided SSL server context with
3323 * all supported algorithms and cipher suites that rely on an EC
3326 * The key type of the CA that issued the server's certificate must
3327 * be provided, since it matters for ECDH cipher suites (ECDH_RSA
3328 * suites require a RSA-powered CA). The key type is either
3329 * `BR_KEYTYPE_RSA` or `BR_KEYTYPE_EC`.
3331 * \param cc server context to initialise.
3332 * \param chain server certificate chain.
3333 * \param chain_len chain length (number of certificates).
3334 * \param cert_issuer_key_type certificate issuer's key type.
3335 * \param sk EC private key.
3337 void br_ssl_server_init_full_ec(br_ssl_server_context
*cc
,
3338 const br_x509_certificate
*chain
, size_t chain_len
,
3339 unsigned cert_issuer_key_type
, const br_ec_private_key
*sk
);
3342 * \brief SSL server profile: minr2g.
3344 * This profile uses only TLS_RSA_WITH_AES_128_GCM_SHA256. Server key is
3345 * RSA, and RSA key exchange is used (not forward secure, but uses little
3346 * CPU in the client).
3348 * \param cc server context to initialise.
3349 * \param chain server certificate chain.
3350 * \param chain_len certificate chain length (number of certificate).
3351 * \param sk RSA private key.
3353 void br_ssl_server_init_minr2g(br_ssl_server_context
*cc
,
3354 const br_x509_certificate
*chain
, size_t chain_len
,
3355 const br_rsa_private_key
*sk
);
3358 * \brief SSL server profile: mine2g.
3360 * This profile uses only TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256. Server key
3361 * is RSA, and ECDHE key exchange is used. This suite provides forward
3362 * security, with a higher CPU expense on the client, and a somewhat
3363 * larger code footprint (compared to "minr2g").
3365 * \param cc server context to initialise.
3366 * \param chain server certificate chain.
3367 * \param chain_len certificate chain length (number of certificate).
3368 * \param sk RSA private key.
3370 void br_ssl_server_init_mine2g(br_ssl_server_context
*cc
,
3371 const br_x509_certificate
*chain
, size_t chain_len
,
3372 const br_rsa_private_key
*sk
);
3375 * \brief SSL server profile: minf2g.
3377 * This profile uses only TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256.
3378 * Server key is EC, and ECDHE key exchange is used. This suite provides
3379 * forward security, with a higher CPU expense on the client and server
3380 * (by a factor of about 3 to 4), and a somewhat larger code footprint
3381 * (compared to "minu2g" and "minv2g").
3383 * \param cc server context to initialise.
3384 * \param chain server certificate chain.
3385 * \param chain_len certificate chain length (number of certificate).
3386 * \param sk EC private key.
3388 void br_ssl_server_init_minf2g(br_ssl_server_context
*cc
,
3389 const br_x509_certificate
*chain
, size_t chain_len
,
3390 const br_ec_private_key
*sk
);
3393 * \brief SSL server profile: minu2g.
3395 * This profile uses only TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256.
3396 * Server key is EC, and ECDH key exchange is used; the issuing CA used
3399 * The "minu2g" and "minv2g" profiles do not provide forward secrecy,
3400 * but are the lightest on the server (for CPU usage), and are rather
3401 * inexpensive on the client as well.
3403 * \param cc server context to initialise.
3404 * \param chain server certificate chain.
3405 * \param chain_len certificate chain length (number of certificate).
3406 * \param sk EC private key.
3408 void br_ssl_server_init_minu2g(br_ssl_server_context
*cc
,
3409 const br_x509_certificate
*chain
, size_t chain_len
,
3410 const br_ec_private_key
*sk
);
3413 * \brief SSL server profile: minv2g.
3415 * This profile uses only TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256.
3416 * Server key is EC, and ECDH key exchange is used; the issuing CA used
3419 * The "minu2g" and "minv2g" profiles do not provide forward secrecy,
3420 * but are the lightest on the server (for CPU usage), and are rather
3421 * inexpensive on the client as well.
3423 * \param cc server context to initialise.
3424 * \param chain server certificate chain.
3425 * \param chain_len certificate chain length (number of certificate).
3426 * \param sk EC private key.
3428 void br_ssl_server_init_minv2g(br_ssl_server_context
*cc
,
3429 const br_x509_certificate
*chain
, size_t chain_len
,
3430 const br_ec_private_key
*sk
);
3433 * \brief SSL server profile: mine2c.
3435 * This profile uses only TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256.
3436 * Server key is RSA, and ECDHE key exchange is used. This suite
3437 * provides forward security.
3439 * \param cc server context to initialise.
3440 * \param chain server certificate chain.
3441 * \param chain_len certificate chain length (number of certificate).
3442 * \param sk RSA private key.
3444 void br_ssl_server_init_mine2c(br_ssl_server_context
*cc
,
3445 const br_x509_certificate
*chain
, size_t chain_len
,
3446 const br_rsa_private_key
*sk
);
3449 * \brief SSL server profile: minf2c.
3451 * This profile uses only TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256.
3452 * Server key is EC, and ECDHE key exchange is used. This suite provides
3455 * \param cc server context to initialise.
3456 * \param chain server certificate chain.
3457 * \param chain_len certificate chain length (number of certificate).
3458 * \param sk EC private key.
3460 void br_ssl_server_init_minf2c(br_ssl_server_context
*cc
,
3461 const br_x509_certificate
*chain
, size_t chain_len
,
3462 const br_ec_private_key
*sk
);
3465 * \brief Get the supported client suites.
3467 * This function shall be called only after the ClientHello has been
3468 * processed, typically from the policy engine. The returned array
3469 * contains the cipher suites that are supported by both the client
3470 * and the server; these suites are in client preference order, unless
3471 * the `BR_OPT_ENFORCE_SERVER_PREFERENCES` flag was set, in which case
3472 * they are in server preference order.
3474 * The suites are _translated_, which means that each suite is given
3475 * as two 16-bit integers: the standard suite identifier, and its
3476 * translated version, broken down into its individual components,
3477 * as explained with the `br_suite_translated` type.
3479 * The returned array is allocated in the context and will be rewritten
3480 * by each handshake.
3482 * \param cc server context.
3483 * \param num receives the array size (number of suites).
3484 * \return the translated common cipher suites, in preference order.
3486 static inline const br_suite_translated
*
3487 br_ssl_server_get_client_suites(const br_ssl_server_context
*cc
, size_t *num
)
3489 *num
= cc
->client_suites_num
;
3490 return cc
->client_suites
;
3494 * \brief Get the hash functions and signature algorithms supported by
3497 * This value is a bit field:
3499 * - If RSA (PKCS#1 v1.5) is supported with hash function of ID `x`,
3500 * then bit `x` is set (hash function ID is 0 for the special MD5+SHA-1,
3501 * or 2 to 6 for the SHA family).
3503 * - If ECDSA is suported with hash function of ID `x`, then bit `8+x`
3506 * - Newer algorithms are symbolic 16-bit identifiers that do not
3507 * represent signature algorithm and hash function separately. If
3508 * the TLS-level identifier is `0x0800+x` for a `x` in the 0..15
3509 * range, then bit `16+x` is set.
3511 * "New algorithms" are currently defined only in draft documents, so
3512 * this support is subject to possible change. Right now (early 2017),
3513 * this maps ed25519 (EdDSA on Curve25519) to bit 23, and ed448 (EdDSA
3514 * on Curve448) to bit 24. If the identifiers on the wire change in
3515 * future document, then the decoding mechanism in BearSSL will be
3516 * amended to keep mapping ed25519 and ed448 on bits 23 and 24,
3517 * respectively. Mapping of other new algorithms (e.g. RSA/PSS) is not
3520 * \param cc server context.
3521 * \return the client-supported hash functions and signature algorithms.
3523 static inline uint32_t
3524 br_ssl_server_get_client_hashes(const br_ssl_server_context
*cc
)
3530 * \brief Get the elliptic curves supported by the client.
3532 * This is a bit field (bit x is set if curve of ID x is supported).
3534 * \param cc server context.
3535 * \return the client-supported elliptic curves.
3537 static inline uint32_t
3538 br_ssl_server_get_client_curves(const br_ssl_server_context
*cc
)
3544 * \brief Clear the complete contents of a SSL server context.
3546 * Everything is cleared, including the reference to the configured buffer,
3547 * implementations, cipher suites and state. This is a preparatory step
3548 * to assembling a custom profile.
3550 * \param cc server context to clear.
3552 void br_ssl_server_zero(br_ssl_server_context
*cc
);
3555 * \brief Set an externally provided policy context.
3557 * The policy context's methods are invoked to decide the cipher suite
3558 * and certificate chain, and to perform operations involving the server's
3561 * \param cc server context.
3562 * \param pctx policy context (pointer to its vtable field).
3565 br_ssl_server_set_policy(br_ssl_server_context
*cc
,
3566 const br_ssl_server_policy_class
**pctx
)
3568 cc
->policy_vtable
= pctx
;
3572 * \brief Set the server certificate chain and key (single RSA case).
3574 * This function uses a policy context included in the server context.
3575 * It configures use of a single server certificate chain with a RSA
3576 * private key. The `allowed_usages` is a combination of usages, namely
3577 * `BR_KEYTYPE_KEYX` and/or `BR_KEYTYPE_SIGN`; this enables or disables
3578 * the corresponding cipher suites (i.e. `TLS_RSA_*` use the RSA key for
3579 * key exchange, while `TLS_ECDHE_RSA_*` use the RSA key for signatures).
3581 * \param cc server context.
3582 * \param chain server certificate chain to send to the client.
3583 * \param chain_len chain length (number of certificates).
3584 * \param sk server private key (RSA).
3585 * \param allowed_usages allowed private key usages.
3586 * \param irsacore RSA core implementation.
3587 * \param irsasign RSA signature implementation (PKCS#1 v1.5).
3589 void br_ssl_server_set_single_rsa(br_ssl_server_context
*cc
,
3590 const br_x509_certificate
*chain
, size_t chain_len
,
3591 const br_rsa_private_key
*sk
, unsigned allowed_usages
,
3592 br_rsa_private irsacore
, br_rsa_pkcs1_sign irsasign
);
3595 * \brief Set the server certificate chain and key (single EC case).
3597 * This function uses a policy context included in the server context.
3598 * It configures use of a single server certificate chain with an EC
3599 * private key. The `allowed_usages` is a combination of usages, namely
3600 * `BR_KEYTYPE_KEYX` and/or `BR_KEYTYPE_SIGN`; this enables or disables
3601 * the corresponding cipher suites (i.e. `TLS_ECDH_*` use the EC key for
3602 * key exchange, while `TLS_ECDHE_ECDSA_*` use the EC key for signatures).
3604 * In order to support `TLS_ECDH_*` cipher suites (non-ephemeral ECDH),
3605 * the algorithm type of the key used by the issuing CA to sign the
3606 * server's certificate must be provided, as `cert_issuer_key_type`
3607 * parameter (this value is either `BR_KEYTYPE_RSA` or `BR_KEYTYPE_EC`).
3609 * \param cc server context.
3610 * \param chain server certificate chain to send.
3611 * \param chain_len chain length (number of certificates).
3612 * \param sk server private key (EC).
3613 * \param allowed_usages allowed private key usages.
3614 * \param cert_issuer_key_type issuing CA's key type.
3615 * \param iec EC core implementation.
3616 * \param iecdsa ECDSA signature implementation ("asn1" format).
3618 void br_ssl_server_set_single_ec(br_ssl_server_context
*cc
,
3619 const br_x509_certificate
*chain
, size_t chain_len
,
3620 const br_ec_private_key
*sk
, unsigned allowed_usages
,
3621 unsigned cert_issuer_key_type
,
3622 const br_ec_impl
*iec
, br_ecdsa_sign iecdsa
);
3625 * \brief Activate client certificate authentication.
3627 * The trust anchor encoded X.500 names (DN) to send to the client are
3628 * provided. A client certificate will be requested and validated through
3629 * the X.509 validator configured in the SSL engine. If `num` is 0, then
3630 * client certificate authentication is disabled.
3632 * If the client does not send a certificate, or on validation failure,
3633 * the handshake aborts. Unauthenticated clients can be tolerated by
3634 * setting the `BR_OPT_TOLERATE_NO_CLIENT_AUTH` flag.
3636 * The provided array is linked in, not copied, so that pointer must
3637 * remain valid as long as anchor names may be used.
3639 * \param cc server context.
3640 * \param ta_names encoded trust anchor names.
3641 * \param num number of encoded trust anchor names.
3644 br_ssl_server_set_trust_anchor_names(br_ssl_server_context
*cc
,
3645 const br_x500_name
*ta_names
, size_t num
)
3647 cc
->ta_names
= ta_names
;
3653 * \brief Activate client certificate authentication.
3655 * This is a variant for `br_ssl_server_set_trust_anchor_names()`: the
3656 * trust anchor names are provided not as an array of stand-alone names
3657 * (`br_x500_name` structures), but as an array of trust anchors
3658 * (`br_x509_trust_anchor` structures). The server engine itself will
3659 * only use the `dn` field of each trust anchor. This is meant to allow
3660 * defining a single array of trust anchors, to be used here and in the
3661 * X.509 validation engine itself.
3663 * The provided array is linked in, not copied, so that pointer must
3664 * remain valid as long as anchor names may be used.
3666 * \param cc server context.
3667 * \param tas trust anchors (only names are used).
3668 * \param num number of trust anchors.
3671 br_ssl_server_set_trust_anchor_names_alt(br_ssl_server_context
*cc
,
3672 const br_x509_trust_anchor
*tas
, size_t num
)
3674 cc
->ta_names
= NULL
;
3680 * \brief Configure the cache for session parameters.
3682 * The cache context is provided as a pointer to its first field (vtable
3685 * \param cc server context.
3686 * \param vtable session cache context.
3689 br_ssl_server_set_cache(br_ssl_server_context
*cc
,
3690 const br_ssl_session_cache_class
**vtable
)
3692 cc
->cache_vtable
= vtable
;
3696 * \brief Prepare or reset a server context for handling an incoming client.
3698 * \param cc server context.
3699 * \return 1 on success, 0 on error.
3701 int br_ssl_server_reset(br_ssl_server_context
*cc
);
3703 /* ===================================================================== */
3706 * Context for the simplified I/O context. The transport medium is accessed
3707 * through the low_read() and low_write() callback functions, each with
3708 * its own opaque context pointer.
3710 * low_read() read some bytes, at most 'len' bytes, into data[]. The
3711 * returned value is the number of read bytes, or -1 on error.
3712 * The 'len' parameter is guaranteed never to exceed 20000,
3713 * so the length always fits in an 'int' on all platforms.
3715 * low_write() write up to 'len' bytes, to be read from data[]. The
3716 * returned value is the number of written bytes, or -1 on
3717 * error. The 'len' parameter is guaranteed never to exceed
3718 * 20000, so the length always fits in an 'int' on all