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 * Pointers to implementations; left to NULL for unsupported
950 * functions. For the raw hash functions, implementations are
951 * referenced from the multihasher (mhash field).
953 br_tls_prf_impl prf10
;
954 br_tls_prf_impl prf_sha256
;
955 br_tls_prf_impl prf_sha384
;
956 const br_block_cbcenc_class
*iaes_cbcenc
;
957 const br_block_cbcdec_class
*iaes_cbcdec
;
958 const br_block_ctr_class
*iaes_ctr
;
959 const br_block_cbcenc_class
*ides_cbcenc
;
960 const br_block_cbcdec_class
*ides_cbcdec
;
962 br_chacha20_run ichacha
;
963 br_poly1305_run ipoly
;
964 const br_sslrec_in_cbc_class
*icbc_in
;
965 const br_sslrec_out_cbc_class
*icbc_out
;
966 const br_sslrec_in_gcm_class
*igcm_in
;
967 const br_sslrec_out_gcm_class
*igcm_out
;
968 const br_sslrec_in_chapol_class
*ichapol_in
;
969 const br_sslrec_out_chapol_class
*ichapol_out
;
970 const br_ec_impl
*iec
;
971 br_rsa_pkcs1_vrfy irsavrfy
;
972 br_ecdsa_vrfy iecdsa
;
974 } br_ssl_engine_context
;
977 * \brief Get currently defined engine behavioural flags.
979 * \param cc SSL engine context.
982 static inline uint32_t
983 br_ssl_engine_get_flags(br_ssl_engine_context
*cc
)
989 * \brief Set all engine behavioural flags.
991 * \param cc SSL engine context.
992 * \param flags new value for all flags.
995 br_ssl_engine_set_all_flags(br_ssl_engine_context
*cc
, uint32_t flags
)
1001 * \brief Set some engine behavioural flags.
1003 * The flags set in the `flags` parameter are set in the context; other
1004 * flags are untouched.
1006 * \param cc SSL engine context.
1007 * \param flags additional set flags.
1010 br_ssl_engine_add_flags(br_ssl_engine_context
*cc
, uint32_t flags
)
1016 * \brief Clear some engine behavioural flags.
1018 * The flags set in the `flags` parameter are cleared from the context; other
1019 * flags are untouched.
1021 * \param cc SSL engine context.
1022 * \param flags flags to remove.
1025 br_ssl_engine_remove_flags(br_ssl_engine_context
*cc
, uint32_t flags
)
1027 cc
->flags
&= ~flags
;
1031 * \brief Behavioural flag: enforce server preferences.
1033 * If this flag is set, then the server will enforce its own cipher suite
1034 * preference order; otherwise, it follows the client preferences.
1036 #define BR_OPT_ENFORCE_SERVER_PREFERENCES ((uint32_t)1 << 0)
1039 * \brief Behavioural flag: disable renegotiation.
1041 * If this flag is set, then renegotiations are rejected unconditionally:
1042 * they won't be honoured if asked for programmatically, and requests from
1043 * the peer are rejected.
1045 #define BR_OPT_NO_RENEGOTIATION ((uint32_t)1 << 1)
1048 * \brief Behavioural flag: tolerate lack of client authentication.
1050 * If this flag is set in a server and the server requests a client
1051 * certificate, but the authentication fails (the client does not send
1052 * a certificate, or the client's certificate chain cannot be validated),
1053 * then the connection keeps on. Without this flag, a failed client
1054 * authentication terminates the connection.
1058 * - If the client's certificate can be validated and its public key is
1059 * supported, then a wrong signature value terminates the connection
1060 * regardless of that flag.
1062 * - If using full-static ECDH, then a failure to validate the client's
1063 * certificate prevents the handshake from succeeding.
1065 #define BR_OPT_TOLERATE_NO_CLIENT_AUTH ((uint32_t)1 << 2)
1068 * \brief Set the minimum and maximum supported protocol versions.
1070 * The two provided versions MUST be supported by the implementation
1071 * (i.e. TLS 1.0, 1.1 and 1.2), and `version_max` MUST NOT be lower
1072 * than `version_min`.
1074 * \param cc SSL engine context.
1075 * \param version_min minimum supported TLS version.
1076 * \param version_max maximum supported TLS version.
1079 br_ssl_engine_set_versions(br_ssl_engine_context
*cc
,
1080 unsigned version_min
, unsigned version_max
)
1082 cc
->version_min
= version_min
;
1083 cc
->version_max
= version_max
;
1087 * \brief Set the list of cipher suites advertised by this context.
1089 * The provided array is copied into the context. It is the caller
1090 * responsibility to ensure that all provided suites will be supported
1091 * by the context. The engine context has enough room to receive _all_
1092 * suites supported by the implementation. The provided array MUST NOT
1093 * contain duplicates.
1095 * If the engine is for a client, the "signaling" pseudo-cipher suite
1096 * `TLS_FALLBACK_SCSV` can be added at the end of the list, if the
1097 * calling application is performing a voluntary downgrade (voluntary
1098 * downgrades are not recommended, but if such a downgrade is done, then
1099 * adding the fallback pseudo-suite is a good idea).
1101 * \param cc SSL engine context.
1102 * \param suites cipher suites.
1103 * \param suites_num number of cipher suites.
1105 void br_ssl_engine_set_suites(br_ssl_engine_context
*cc
,
1106 const uint16_t *suites
, size_t suites_num
);
1109 * \brief Set the X.509 engine.
1111 * The caller shall ensure that the X.509 engine is properly initialised.
1113 * \param cc SSL engine context.
1114 * \param x509ctx X.509 certificate validation context.
1117 br_ssl_engine_set_x509(br_ssl_engine_context
*cc
, const br_x509_class
**x509ctx
)
1119 cc
->x509ctx
= x509ctx
;
1123 * \brief Set a hash function implementation (by ID).
1125 * Hash functions set with this call will be used for SSL/TLS specific
1126 * usages, not X.509 certificate validation. Only "standard" hash functions
1127 * may be set (MD5, SHA-1, SHA-224, SHA-256, SHA-384, SHA-512). If `impl`
1128 * is `NULL`, then the hash function support is removed, not added.
1130 * \param ctx SSL engine context.
1131 * \param id hash function identifier.
1132 * \param impl hash function implementation (or `NULL`).
1135 br_ssl_engine_set_hash(br_ssl_engine_context
*ctx
,
1136 int id
, const br_hash_class
*impl
)
1138 br_multihash_setimpl(&ctx
->mhash
, id
, impl
);
1142 * \brief Get a hash function implementation (by ID).
1144 * This function retrieves a hash function implementation which was
1145 * set with `br_ssl_engine_set_hash()`.
1147 * \param ctx SSL engine context.
1148 * \param id hash function identifier.
1149 * \return the hash function implementation (or `NULL`).
1151 static inline const br_hash_class
*
1152 br_ssl_engine_get_hash(br_ssl_engine_context
*ctx
, int id
)
1154 return br_multihash_getimpl(&ctx
->mhash
, id
);
1158 * \brief Set the PRF implementation (for TLS 1.0 and 1.1).
1160 * This function sets (or removes, if `impl` is `NULL`) the implemenation
1161 * for the PRF used in TLS 1.0 and 1.1.
1163 * \param cc SSL engine context.
1164 * \param impl PRF implementation (or `NULL`).
1167 br_ssl_engine_set_prf10(br_ssl_engine_context
*cc
, br_tls_prf_impl impl
)
1173 * \brief Set the PRF implementation with SHA-256 (for TLS 1.2).
1175 * This function sets (or removes, if `impl` is `NULL`) the implemenation
1176 * for the SHA-256 variant of the PRF used in TLS 1.2.
1178 * \param cc SSL engine context.
1179 * \param impl PRF implementation (or `NULL`).
1182 br_ssl_engine_set_prf_sha256(br_ssl_engine_context
*cc
, br_tls_prf_impl impl
)
1184 cc
->prf_sha256
= impl
;
1188 * \brief Set the PRF implementation with SHA-384 (for TLS 1.2).
1190 * This function sets (or removes, if `impl` is `NULL`) the implemenation
1191 * for the SHA-384 variant of the PRF used in TLS 1.2.
1193 * \param cc SSL engine context.
1194 * \param impl PRF implementation (or `NULL`).
1197 br_ssl_engine_set_prf_sha384(br_ssl_engine_context
*cc
, br_tls_prf_impl impl
)
1199 cc
->prf_sha384
= impl
;
1203 * \brief Set the AES/CBC implementations.
1205 * \param cc SSL engine context.
1206 * \param impl_enc AES/CBC encryption implementation (or `NULL`).
1207 * \param impl_dec AES/CBC decryption implementation (or `NULL`).
1210 br_ssl_engine_set_aes_cbc(br_ssl_engine_context
*cc
,
1211 const br_block_cbcenc_class
*impl_enc
,
1212 const br_block_cbcdec_class
*impl_dec
)
1214 cc
->iaes_cbcenc
= impl_enc
;
1215 cc
->iaes_cbcdec
= impl_dec
;
1219 * \brief Set the AES/CTR implementation.
1221 * \param cc SSL engine context.
1222 * \param impl AES/CTR encryption/decryption implementation (or `NULL`).
1225 br_ssl_engine_set_aes_ctr(br_ssl_engine_context
*cc
,
1226 const br_block_ctr_class
*impl
)
1228 cc
->iaes_ctr
= impl
;
1232 * \brief Set the DES/CBC implementations.
1234 * \param cc SSL engine context.
1235 * \param impl_enc DES/CBC encryption implementation (or `NULL`).
1236 * \param impl_dec DES/CBC decryption implementation (or `NULL`).
1239 br_ssl_engine_set_des_cbc(br_ssl_engine_context
*cc
,
1240 const br_block_cbcenc_class
*impl_enc
,
1241 const br_block_cbcdec_class
*impl_dec
)
1243 cc
->ides_cbcenc
= impl_enc
;
1244 cc
->ides_cbcdec
= impl_dec
;
1248 * \brief Set the GHASH implementation (used in GCM mode).
1250 * \param cc SSL engine context.
1251 * \param impl GHASH implementation (or `NULL`).
1254 br_ssl_engine_set_ghash(br_ssl_engine_context
*cc
, br_ghash impl
)
1260 * \brief Set the ChaCha20 implementation.
1262 * \param cc SSL engine context.
1263 * \param ichacha ChaCha20 implementation (or `NULL`).
1266 br_ssl_engine_set_chacha20(br_ssl_engine_context
*cc
,
1267 br_chacha20_run ichacha
)
1269 cc
->ichacha
= ichacha
;
1273 * \brief Set the Poly1305 implementation.
1275 * \param cc SSL engine context.
1276 * \param ipoly Poly1305 implementation (or `NULL`).
1279 br_ssl_engine_set_poly1305(br_ssl_engine_context
*cc
,
1280 br_poly1305_run ipoly
)
1286 * \brief Set the record encryption and decryption engines for CBC + HMAC.
1288 * \param cc SSL engine context.
1289 * \param impl_in record CBC decryption implementation (or `NULL`).
1290 * \param impl_out record CBC encryption implementation (or `NULL`).
1293 br_ssl_engine_set_cbc(br_ssl_engine_context
*cc
,
1294 const br_sslrec_in_cbc_class
*impl_in
,
1295 const br_sslrec_out_cbc_class
*impl_out
)
1297 cc
->icbc_in
= impl_in
;
1298 cc
->icbc_out
= impl_out
;
1302 * \brief Set the record encryption and decryption engines for GCM.
1304 * \param cc SSL engine context.
1305 * \param impl_in record GCM decryption implementation (or `NULL`).
1306 * \param impl_out record GCM encryption implementation (or `NULL`).
1309 br_ssl_engine_set_gcm(br_ssl_engine_context
*cc
,
1310 const br_sslrec_in_gcm_class
*impl_in
,
1311 const br_sslrec_out_gcm_class
*impl_out
)
1313 cc
->igcm_in
= impl_in
;
1314 cc
->igcm_out
= impl_out
;
1318 * \brief Set the record encryption and decryption engines for
1319 * ChaCha20+Poly1305.
1321 * \param cc SSL engine context.
1322 * \param impl_in record ChaCha20 decryption implementation (or `NULL`).
1323 * \param impl_out record ChaCha20 encryption implementation (or `NULL`).
1326 br_ssl_engine_set_chapol(br_ssl_engine_context
*cc
,
1327 const br_sslrec_in_chapol_class
*impl_in
,
1328 const br_sslrec_out_chapol_class
*impl_out
)
1330 cc
->ichapol_in
= impl_in
;
1331 cc
->ichapol_out
= impl_out
;
1335 * \brief Set the EC implementation.
1337 * The elliptic curve implementation will be used for ECDH and ECDHE
1338 * cipher suites, and for ECDSA support.
1340 * \param cc SSL engine context.
1341 * \param iec EC implementation (or `NULL`).
1344 br_ssl_engine_set_ec(br_ssl_engine_context
*cc
, const br_ec_impl
*iec
)
1350 * \brief Set the RSA signature verification implementation.
1352 * On the client, this is used to verify the server's signature on its
1353 * ServerKeyExchange message (for ECDHE_RSA cipher suites). On the server,
1354 * this is used to verify the client's CertificateVerify message (if a
1355 * client certificate is requested, and that certificate contains a RSA key).
1357 * \param cc SSL engine context.
1358 * \param irsavrfy RSA signature verification implementation.
1361 br_ssl_engine_set_rsavrfy(br_ssl_engine_context
*cc
, br_rsa_pkcs1_vrfy irsavrfy
)
1363 cc
->irsavrfy
= irsavrfy
;
1367 * \brief Set the ECDSA implementation (signature verification).
1369 * On the client, this is used to verify the server's signature on its
1370 * ServerKeyExchange message (for ECDHE_ECDSA cipher suites). On the server,
1371 * this is used to verify the client's CertificateVerify message (if a
1372 * client certificate is requested, that certificate contains an EC key,
1373 * and full-static ECDH is not used).
1375 * The ECDSA implementation will use the EC core implementation configured
1376 * in the engine context.
1378 * \param cc client context.
1379 * \param iecdsa ECDSA verification implementation.
1382 br_ssl_engine_set_ecdsa(br_ssl_engine_context
*cc
, br_ecdsa_vrfy iecdsa
)
1384 cc
->iecdsa
= iecdsa
;
1388 * \brief Set the I/O buffer for the SSL engine.
1390 * Once this call has been made, `br_ssl_client_reset()` or
1391 * `br_ssl_server_reset()` MUST be called before using the context.
1393 * The provided buffer will be used as long as the engine context is
1394 * used. The caller is responsible for keeping it available.
1396 * If `bidi` is 0, then the engine will operate in half-duplex mode
1397 * (it won't be able to send data while there is unprocessed incoming
1398 * data in the buffer, and it won't be able to receive data while there
1399 * is unsent data in the buffer). The optimal buffer size in half-duplex
1400 * mode is `BR_SSL_BUFSIZE_MONO`; if the buffer is larger, then extra
1401 * bytes are ignored. If the buffer is smaller, then this limits the
1402 * capacity of the engine to support all allowed record sizes.
1404 * If `bidi` is 1, then the engine will split the buffer into two
1405 * parts, for separate handling of outgoing and incoming data. This
1406 * enables full-duplex processing, but requires more RAM. The optimal
1407 * buffer size in full-duplex mode is `BR_SSL_BUFSIZE_BIDI`; if the
1408 * buffer is larger, then extra bytes are ignored. If the buffer is
1409 * smaller, then the split will favour the incoming part, so that
1410 * interoperability is maximised.
1412 * \param cc SSL engine context
1413 * \param iobuf I/O buffer.
1414 * \param iobuf_len I/O buffer length (in bytes).
1415 * \param bidi non-zero for full-duplex mode.
1417 void br_ssl_engine_set_buffer(br_ssl_engine_context
*cc
,
1418 void *iobuf
, size_t iobuf_len
, int bidi
);
1421 * \brief Set the I/O buffers for the SSL engine.
1423 * Once this call has been made, `br_ssl_client_reset()` or
1424 * `br_ssl_server_reset()` MUST be called before using the context.
1426 * This function is similar to `br_ssl_engine_set_buffer()`, except
1427 * that it enforces full-duplex mode, and the two I/O buffers are
1428 * provided as separate chunks.
1430 * The macros `BR_SSL_BUFSIZE_INPUT` and `BR_SSL_BUFSIZE_OUTPUT`
1431 * evaluate to the optimal (maximum) sizes for the input and output
1432 * buffer, respectively.
1434 * \param cc SSL engine context
1435 * \param ibuf input buffer.
1436 * \param ibuf_len input buffer length (in bytes).
1437 * \param obuf output buffer.
1438 * \param obuf_len output buffer length (in bytes).
1440 void br_ssl_engine_set_buffers_bidi(br_ssl_engine_context
*cc
,
1441 void *ibuf
, size_t ibuf_len
, void *obuf
, size_t obuf_len
);
1444 * \brief Inject some "initial entropy" in the context.
1446 * This entropy will be added to what can be obtained from the
1447 * underlying operating system, if that OS is supported.
1449 * This function may be called several times; all injected entropy chunks
1450 * are cumulatively mixed.
1452 * If entropy gathering from the OS is supported and compiled in, then this
1453 * step is optional. Otherwise, it is mandatory to inject randomness, and
1454 * the caller MUST take care to push (as one or several successive calls)
1455 * enough entropy to achieve cryptographic resistance (at least 80 bits,
1456 * preferably 128 or more). The engine will report an error if no entropy
1457 * was provided and none can be obtained from the OS.
1459 * Take care that this function cannot assess the cryptographic quality of
1460 * the provided bytes.
1462 * In all generality, "entropy" must here be considered to mean "that
1463 * which the attacker cannot predict". If your OS/architecture does not
1464 * have a suitable source of randomness, then you can make do with the
1465 * combination of a large enough secret value (possibly a copy of an
1466 * asymmetric private key that you also store on the system) AND a
1467 * non-repeating value (e.g. current time, provided that the local clock
1468 * cannot be reset or altered by the attacker).
1470 * \param cc SSL engine context.
1471 * \param data extra entropy to inject.
1472 * \param len length of the extra data (in bytes).
1474 void br_ssl_engine_inject_entropy(br_ssl_engine_context
*cc
,
1475 const void *data
, size_t len
);
1478 * \brief Get the "server name" in this engine.
1480 * For clients, this is the name provided with `br_ssl_client_reset()`;
1481 * for servers, this is the name received from the client as part of the
1482 * ClientHello message. If there is no such name (e.g. the client did
1483 * not send an SNI extension) then the returned string is empty
1484 * (returned pointer points to a byte of value 0).
1486 * The returned pointer refers to a buffer inside the context, which may
1487 * be overwritten as part of normal SSL activity (even within the same
1488 * connection, if a renegotiation occurs).
1490 * \param cc SSL engine context.
1491 * \return the server name (possibly empty).
1493 static inline const char *
1494 br_ssl_engine_get_server_name(const br_ssl_engine_context
*cc
)
1496 return cc
->server_name
;
1500 * \brief Get the protocol version.
1502 * This function returns the protocol version that is used by the
1503 * engine. That value is set after sending (for a server) or receiving
1504 * (for a client) the ServerHello message.
1506 * \param cc SSL engine context.
1507 * \return the protocol version.
1509 static inline unsigned
1510 br_ssl_engine_get_version(const br_ssl_engine_context
*cc
)
1512 return cc
->session
.version
;
1516 * \brief Get a copy of the session parameters.
1518 * The session parameters are filled during the handshake, so this
1519 * function shall not be called before completion of the handshake.
1520 * The initial handshake is completed when the context first allows
1521 * application data to be injected.
1523 * This function copies the current session parameters into the provided
1524 * structure. Beware that the session parameters include the master
1525 * secret, which is sensitive data, to handle with great care.
1527 * \param cc SSL engine context.
1528 * \param pp destination structure for the session parameters.
1531 br_ssl_engine_get_session_parameters(const br_ssl_engine_context
*cc
,
1532 br_ssl_session_parameters
*pp
)
1534 memcpy(pp
, &cc
->session
, sizeof *pp
);
1538 * \brief Set the session parameters to the provided values.
1540 * This function is meant to be used in the client, before doing a new
1541 * handshake; a session resumption will be attempted with these
1542 * parameters. In the server, this function has no effect.
1544 * \param cc SSL engine context.
1545 * \param pp source structure for the session parameters.
1548 br_ssl_engine_set_session_parameters(br_ssl_engine_context
*cc
,
1549 const br_ssl_session_parameters
*pp
)
1551 memcpy(&cc
->session
, pp
, sizeof *pp
);
1555 * \brief Get the current engine state.
1557 * An SSL engine (client or server) has, at any time, a state which is
1558 * the combination of zero, one or more of these flags:
1562 * Engine is finished, no more I/O (until next reset).
1564 * - `BR_SSL_SENDREC`
1566 * Engine has some bytes to send to the peer.
1568 * - `BR_SSL_RECVREC`
1570 * Engine expects some bytes from the peer.
1572 * - `BR_SSL_SENDAPP`
1574 * Engine may receive application data to send (or flush).
1576 * - `BR_SSL_RECVAPP`
1578 * Engine has obtained some application data from the peer,
1579 * that should be read by the caller.
1581 * If no flag at all is set (state value is 0), then the engine is not
1582 * fully initialised yet.
1584 * The `BR_SSL_CLOSED` flag is exclusive; when it is set, no other flag
1585 * is set. To distinguish between a normal closure and an error, use
1586 * `br_ssl_engine_last_error()`.
1588 * Generally speaking, `BR_SSL_SENDREC` and `BR_SSL_SENDAPP` are mutually
1589 * exclusive: the input buffer, at any point, either accumulates
1590 * plaintext data, or contains an assembled record that is being sent.
1591 * Similarly, `BR_SSL_RECVREC` and `BR_SSL_RECVAPP` are mutually exclusive.
1592 * This may change in a future library version.
1594 * \param cc SSL engine context.
1595 * \return the current engine state.
1597 unsigned br_ssl_engine_current_state(const br_ssl_engine_context
*cc
);
1599 /** \brief SSL engine state: closed or failed. */
1600 #define BR_SSL_CLOSED 0x0001
1601 /** \brief SSL engine state: record data is ready to be sent to the peer. */
1602 #define BR_SSL_SENDREC 0x0002
1603 /** \brief SSL engine state: engine may receive records from the peer. */
1604 #define BR_SSL_RECVREC 0x0004
1605 /** \brief SSL engine state: engine may accept application data to send. */
1606 #define BR_SSL_SENDAPP 0x0008
1607 /** \brief SSL engine state: engine has received application data. */
1608 #define BR_SSL_RECVAPP 0x0010
1611 * \brief Get the engine error indicator.
1613 * The error indicator is `BR_ERR_OK` (0) if no error was encountered
1614 * since the last call to `br_ssl_client_reset()` or
1615 * `br_ssl_server_reset()`. Other status values are "sticky": they
1616 * remain set, and prevent all I/O activity, until cleared. Only the
1617 * reset calls clear the error indicator.
1619 * \param cc SSL engine context.
1620 * \return 0, or a non-zero error code.
1623 br_ssl_engine_last_error(const br_ssl_engine_context
*cc
)
1629 * There are four I/O operations, each identified by a symbolic name:
1631 * sendapp inject application data in the engine
1632 * recvapp retrieving application data from the engine
1633 * sendrec sending records on the transport medium
1634 * recvrec receiving records from the transport medium
1636 * Terminology works thus: in a layered model where the SSL engine sits
1637 * between the application and the network, "send" designates operations
1638 * where bytes flow from application to network, and "recv" for the
1639 * reverse operation. Application data (the plaintext that is to be
1640 * conveyed through SSL) is "app", while encrypted records are "rec".
1641 * Note that from the SSL engine point of view, "sendapp" and "recvrec"
1642 * designate bytes that enter the engine ("inject" operation), while
1643 * "recvapp" and "sendrec" designate bytes that exit the engine
1644 * ("extract" operation).
1646 * For the operation 'xxx', two functions are defined:
1648 * br_ssl_engine_xxx_buf
1649 * Returns a pointer and length to the buffer to use for that
1650 * operation. '*len' is set to the number of bytes that may be read
1651 * from the buffer (extract operation) or written to the buffer
1652 * (inject operation). If no byte may be exchanged for that operation
1653 * at that point, then '*len' is set to zero, and NULL is returned.
1654 * The engine state is unmodified by this call.
1656 * br_ssl_engine_xxx_ack
1657 * Informs the engine that 'len' bytes have been read from the buffer
1658 * (extract operation) or written to the buffer (inject operation).
1659 * The 'len' value MUST NOT be zero. The 'len' value MUST NOT exceed
1660 * that which was obtained from a preceeding br_ssl_engine_xxx_buf()
1665 * \brief Get buffer for application data to send.
1667 * If the engine is ready to accept application data to send to the
1668 * peer, then this call returns a pointer to the buffer where such
1669 * data shall be written, and its length is written in `*len`.
1670 * Otherwise, `*len` is set to 0 and `NULL` is returned.
1672 * \param cc SSL engine context.
1673 * \param len receives the application data output buffer length, or 0.
1674 * \return the application data output buffer, or `NULL`.
1676 unsigned char *br_ssl_engine_sendapp_buf(
1677 const br_ssl_engine_context
*cc
, size_t *len
);
1680 * \brief Inform the engine of some new application data.
1682 * After writing `len` bytes in the buffer returned by
1683 * `br_ssl_engine_sendapp_buf()`, the application shall call this
1684 * function to trigger any relevant processing. The `len` parameter
1685 * MUST NOT be 0, and MUST NOT exceed the value obtained in the
1686 * `br_ssl_engine_sendapp_buf()` call.
1688 * \param cc SSL engine context.
1689 * \param len number of bytes pushed (not zero).
1691 void br_ssl_engine_sendapp_ack(br_ssl_engine_context
*cc
, size_t len
);
1694 * \brief Get buffer for received application data.
1696 * If the engine has received application data from the peer, hen this
1697 * call returns a pointer to the buffer from where such data shall be
1698 * read, and its length is written in `*len`. Otherwise, `*len` is set
1699 * to 0 and `NULL` is returned.
1701 * \param cc SSL engine context.
1702 * \param len receives the application data input buffer length, or 0.
1703 * \return the application data input buffer, or `NULL`.
1705 unsigned char *br_ssl_engine_recvapp_buf(
1706 const br_ssl_engine_context
*cc
, size_t *len
);
1709 * \brief Acknowledge some received application data.
1711 * After reading `len` bytes from the buffer returned by
1712 * `br_ssl_engine_recvapp_buf()`, the application shall call this
1713 * function to trigger any relevant processing. The `len` parameter
1714 * MUST NOT be 0, and MUST NOT exceed the value obtained in the
1715 * `br_ssl_engine_recvapp_buf()` call.
1717 * \param cc SSL engine context.
1718 * \param len number of bytes read (not zero).
1720 void br_ssl_engine_recvapp_ack(br_ssl_engine_context
*cc
, size_t len
);
1723 * \brief Get buffer for record data to send.
1725 * If the engine has prepared some records to send to the peer, then this
1726 * call returns a pointer to the buffer from where such data shall be
1727 * read, and its length is written in `*len`. Otherwise, `*len` is set
1728 * to 0 and `NULL` is returned.
1730 * \param cc SSL engine context.
1731 * \param len receives the record data output buffer length, or 0.
1732 * \return the record data output buffer, or `NULL`.
1734 unsigned char *br_ssl_engine_sendrec_buf(
1735 const br_ssl_engine_context
*cc
, size_t *len
);
1738 * \brief Acknowledge some sent record data.
1740 * After reading `len` bytes from the buffer returned by
1741 * `br_ssl_engine_sendrec_buf()`, the application shall call this
1742 * function to trigger any relevant processing. The `len` parameter
1743 * MUST NOT be 0, and MUST NOT exceed the value obtained in the
1744 * `br_ssl_engine_sendrec_buf()` call.
1746 * \param cc SSL engine context.
1747 * \param len number of bytes read (not zero).
1749 void br_ssl_engine_sendrec_ack(br_ssl_engine_context
*cc
, size_t len
);
1752 * \brief Get buffer for incoming records.
1754 * If the engine is ready to accept records from the peer, then this
1755 * call returns a pointer to the buffer where such data shall be
1756 * written, and its length is written in `*len`. Otherwise, `*len` is
1757 * set to 0 and `NULL` is returned.
1759 * \param cc SSL engine context.
1760 * \param len receives the record data input buffer length, or 0.
1761 * \return the record data input buffer, or `NULL`.
1763 unsigned char *br_ssl_engine_recvrec_buf(
1764 const br_ssl_engine_context
*cc
, size_t *len
);
1767 * \brief Inform the engine of some new record data.
1769 * After writing `len` bytes in the buffer returned by
1770 * `br_ssl_engine_recvrec_buf()`, the application shall call this
1771 * function to trigger any relevant processing. The `len` parameter
1772 * MUST NOT be 0, and MUST NOT exceed the value obtained in the
1773 * `br_ssl_engine_recvrec_buf()` call.
1775 * \param cc SSL engine context.
1776 * \param len number of bytes pushed (not zero).
1778 void br_ssl_engine_recvrec_ack(br_ssl_engine_context
*cc
, size_t len
);
1781 * \brief Flush buffered application data.
1783 * If some application data has been buffered in the engine, then wrap
1784 * it into a record and mark it for sending. If no application data has
1785 * been buffered but the engine would be ready to accept some, AND the
1786 * `force` parameter is non-zero, then an empty record is assembled and
1787 * marked for sending. In all other cases, this function does nothing.
1789 * Empty records are technically legal, but not all existing SSL/TLS
1790 * implementations support them. Empty records can be useful as a
1791 * transparent "keep-alive" mechanism to maintain some low-level
1794 * \param cc SSL engine context.
1795 * \param force non-zero to force sending an empty record.
1797 void br_ssl_engine_flush(br_ssl_engine_context
*cc
, int force
);
1800 * \brief Initiate a closure.
1802 * If, at that point, the context is open and in ready state, then a
1803 * `close_notify` alert is assembled and marked for sending; this
1804 * triggers the closure protocol. Otherwise, no such alert is assembled.
1806 * \param cc SSL engine context.
1808 void br_ssl_engine_close(br_ssl_engine_context
*cc
);
1811 * \brief Initiate a renegotiation.
1813 * If the engine is failed or closed, or if the peer is known not to
1814 * support secure renegotiation (RFC 5746), or if renegotiations have
1815 * been disabled with the `BR_OPT_NO_RENEGOTIATION` flag, then this
1816 * function returns 0 and nothing else happens.
1818 * Otherwise, this function returns 1, and a renegotiation attempt is
1819 * triggered (if a handshake is already ongoing at that point, then
1820 * no new handshake is triggered).
1822 * \param cc SSL engine context.
1823 * \return 1 on success, 0 on error.
1825 int br_ssl_engine_renegotiate(br_ssl_engine_context
*cc
);
1828 * Pre-declaration for the SSL client context.
1830 typedef struct br_ssl_client_context_ br_ssl_client_context
;
1833 * \brief Type for the client certificate, if requested by the server.
1837 * \brief Authentication type.
1839 * This is either `BR_AUTH_RSA` (RSA signature), `BR_AUTH_ECDSA`
1840 * (ECDSA signature), or `BR_AUTH_ECDH` (static ECDH key exchange).
1845 * \brief Hash function for computing the CertificateVerify.
1847 * This is the symbolic identifier for the hash function that
1848 * will be used to produce the hash of handshake messages, to
1849 * be signed into the CertificateVerify. For full static ECDH
1850 * (client and server certificates are both EC in the same
1851 * curve, and static ECDH is used), this value is set to -1.
1853 * Take care that with TLS 1.0 and 1.1, that value MUST match
1854 * the protocol requirements: value must be 0 (MD5+SHA-1) for
1855 * a RSA signature, or 2 (SHA-1) for an ECDSA signature. Only
1856 * TLS 1.2 allows for other hash functions.
1861 * \brief Certificate chain to send to the server.
1863 * This is an array of `br_x509_certificate` objects, each
1864 * normally containing a DER-encoded certificate. The client
1865 * code does not try to decode these elements. If there is no
1866 * chain to send to the server, then this pointer shall be
1869 const br_x509_certificate
*chain
;
1872 * \brief Certificate chain length (number of certificates).
1874 * If there is no chain to send to the server, then this value
1875 * shall be set to 0.
1879 } br_ssl_client_certificate
;
1882 * Note: the constants below for signatures match the TLS constants.
1885 /** \brief Client authentication type: static ECDH. */
1886 #define BR_AUTH_ECDH 0
1887 /** \brief Client authentication type: RSA signature. */
1888 #define BR_AUTH_RSA 1
1889 /** \brief Client authentication type: ECDSA signature. */
1890 #define BR_AUTH_ECDSA 3
1893 * \brief Class type for a certificate handler (client side).
1895 * A certificate handler selects a client certificate chain to send to
1896 * the server, upon explicit request from that server. It receives
1897 * the list of trust anchor DN from the server, and supported types
1898 * of certificates and signatures, and returns the chain to use. It
1899 * is also invoked to perform the corresponding private key operation
1900 * (a signature, or an ECDH computation).
1902 * The SSL client engine will first push the trust anchor DN with
1903 * `start_name_list()`, `start_name()`, `append_name()`, `end_name()`
1904 * and `end_name_list()`. Then it will call `choose()`, to select the
1905 * actual chain (and signature/hash algorithms). Finally, it will call
1906 * either `do_sign()` or `do_keyx()`, depending on the algorithm choices.
1908 typedef struct br_ssl_client_certificate_class_ br_ssl_client_certificate_class
;
1909 struct br_ssl_client_certificate_class_
{
1911 * \brief Context size (in bytes).
1913 size_t context_size
;
1916 * \brief Begin reception of a list of trust anchor names. This
1917 * is called while parsing the incoming CertificateRequest.
1919 * \param pctx certificate handler context.
1921 void (*start_name_list
)(const br_ssl_client_certificate_class
**pctx
);
1924 * \brief Begin reception of a new trust anchor name.
1926 * The total encoded name length is provided; it is less than
1929 * \param pctx certificate handler context.
1930 * \param len encoded name length (in bytes).
1932 void (*start_name
)(const br_ssl_client_certificate_class
**pctx
,
1936 * \brief Receive some more bytes for the current trust anchor name.
1938 * The provided reference (`data`) points to a transient buffer
1939 * they may be reused as soon as this function returns. The chunk
1940 * length (`len`) is never zero.
1942 * \param pctx certificate handler context.
1943 * \param data anchor name chunk.
1944 * \param len anchor name chunk length (in bytes).
1946 void (*append_name
)(const br_ssl_client_certificate_class
**pctx
,
1947 const unsigned char *data
, size_t len
);
1950 * \brief End current trust anchor name.
1952 * This function is called when all the encoded anchor name data
1953 * has been provided.
1955 * \param pctx certificate handler context.
1957 void (*end_name
)(const br_ssl_client_certificate_class
**pctx
);
1960 * \brief End list of trust anchor names.
1962 * This function is called when all the anchor names in the
1963 * CertificateRequest message have been obtained.
1965 * \param pctx certificate handler context.
1967 void (*end_name_list
)(const br_ssl_client_certificate_class
**pctx
);
1970 * \brief Select client certificate and algorithms.
1972 * This callback function shall fill the provided `choices`
1973 * structure with the selected algorithms and certificate chain.
1974 * The `hash_id`, `chain` and `chain_len` fields must be set. If
1975 * the client cannot or does not wish to send a certificate,
1976 * then it shall set `chain` to `NULL` and `chain_len` to 0.
1978 * The `auth_types` parameter describes the authentication types,
1979 * signature algorithms and hash functions that are supported by
1980 * both the client context and the server, and compatible with
1981 * the current protocol version. This is a bit field with the
1982 * following contents:
1984 * - If RSA signatures with hash function x are supported, then
1987 * - If ECDSA signatures with hash function x are supported,
1988 * then bit 8+x is set.
1990 * - If static ECDH is supported, with a RSA-signed certificate,
1991 * then bit 16 is set.
1993 * - If static ECDH is supported, with an ECDSA-signed certificate,
1994 * then bit 17 is set.
1998 * - When using TLS 1.0 or 1.1, the hash function for RSA
1999 * signatures is always the special MD5+SHA-1 (id 0), and the
2000 * hash function for ECDSA signatures is always SHA-1 (id 2).
2002 * - When using TLS 1.2, the list of hash functions is trimmed
2003 * down to include only hash functions that the client context
2004 * can support. The actual server list can be obtained with
2005 * `br_ssl_client_get_server_hashes()`; that list may be used
2006 * to select the certificate chain to send to the server.
2008 * \param pctx certificate handler context.
2009 * \param cc SSL client context.
2010 * \param auth_types supported authentication types and algorithms.
2011 * \param choices destination structure for the policy choices.
2013 void (*choose
)(const br_ssl_client_certificate_class
**pctx
,
2014 const br_ssl_client_context
*cc
, uint32_t auth_types
,
2015 br_ssl_client_certificate
*choices
);
2018 * \brief Perform key exchange (client part).
2020 * This callback is invoked in case of a full static ECDH key
2023 * - the cipher suite uses `ECDH_RSA` or `ECDH_ECDSA`;
2025 * - the server requests a client certificate;
2027 * - the client has, and sends, a client certificate that
2028 * uses an EC key in the same curve as the server's key,
2029 * and chooses static ECDH (the `hash_id` field in the choice
2030 * structure was set to -1).
2032 * In that situation, this callback is invoked to compute the
2033 * client-side ECDH: the provided `data` (of length `len` bytes)
2034 * is the server's public key point (as decoded from its
2035 * certificate), and the client shall multiply that point with
2036 * its own private key, and write back the X coordinate of the
2037 * resulting point in the same buffer, starting at offset 1
2038 * (therefore, writing back the complete encoded point works).
2040 * The callback must uphold the following:
2042 * - If the input array does not have the proper length for
2043 * an encoded curve point, then an error (0) shall be reported.
2045 * - If the input array has the proper length, then processing
2046 * MUST be constant-time, even if the data is not a valid
2049 * - This callback MUST check that the input point is valid.
2051 * Returned value is 1 on success, 0 on error.
2053 * \param pctx certificate handler context.
2054 * \param data server public key point.
2055 * \param len server public key point length (in bytes).
2056 * \return 1 on success, 0 on error.
2058 uint32_t (*do_keyx
)(const br_ssl_client_certificate_class
**pctx
,
2059 unsigned char *data
, size_t len
);
2062 * \brief Perform a signature (client authentication).
2064 * This callback is invoked when a client certificate was sent,
2065 * and static ECDH is not used. It shall compute a signature,
2066 * using the client's private key, over the provided hash value
2067 * (which is the hash of all previous handshake messages).
2069 * On input, the hash value to sign is in `data`, of size
2070 * `hv_len`; the involved hash function is identified by
2071 * `hash_id`. The signature shall be computed and written
2072 * back into `data`; the total size of that buffer is `len`
2075 * This callback shall verify that the signature length does not
2076 * exceed `len` bytes, and abstain from writing the signature if
2079 * For RSA signatures, the `hash_id` may be 0, in which case
2080 * this is the special header-less signature specified in TLS 1.0
2081 * and 1.1, with a 36-byte hash value. Otherwise, normal PKCS#1
2082 * v1.5 signatures shall be computed.
2084 * For ECDSA signatures, the signature value shall use the ASN.1
2087 * Returned value is the signature length (in bytes), or 0 on error.
2089 * \param pctx certificate handler context.
2090 * \param hash_id hash function identifier.
2091 * \param hv_len hash value length (in bytes).
2092 * \param data input/output buffer (hash value, then signature).
2093 * \param len total buffer length (in bytes).
2094 * \return signature length (in bytes) on success, or 0 on error.
2096 size_t (*do_sign
)(const br_ssl_client_certificate_class
**pctx
,
2097 int hash_id
, size_t hv_len
, unsigned char *data
, size_t len
);
2101 * \brief A single-chain RSA client certificate handler.
2103 * This handler uses a single certificate chain, with a RSA
2104 * signature. The list of trust anchor DN is ignored.
2106 * Apart from the first field (vtable pointer), its contents are
2107 * opaque and shall not be accessed directly.
2110 /** \brief Pointer to vtable. */
2111 const br_ssl_client_certificate_class
*vtable
;
2112 #ifndef BR_DOXYGEN_IGNORE
2113 const br_x509_certificate
*chain
;
2115 const br_rsa_private_key
*sk
;
2116 br_rsa_pkcs1_sign irsasign
;
2118 } br_ssl_client_certificate_rsa_context
;
2121 * \brief A single-chain EC client certificate handler.
2123 * This handler uses a single certificate chain, with a RSA
2124 * signature. The list of trust anchor DN is ignored.
2126 * This handler may support both static ECDH, and ECDSA signatures
2127 * (either usage may be selectively disabled).
2129 * Apart from the first field (vtable pointer), its contents are
2130 * opaque and shall not be accessed directly.
2133 /** \brief Pointer to vtable. */
2134 const br_ssl_client_certificate_class
*vtable
;
2135 #ifndef BR_DOXYGEN_IGNORE
2136 const br_x509_certificate
*chain
;
2138 const br_ec_private_key
*sk
;
2139 unsigned allowed_usages
;
2140 unsigned issuer_key_type
;
2141 const br_multihash_context
*mhash
;
2142 const br_ec_impl
*iec
;
2143 br_ecdsa_sign iecdsa
;
2145 } br_ssl_client_certificate_ec_context
;
2148 * \brief Context structure for a SSL client.
2150 * The first field (called `eng`) is the SSL engine; all functions that
2151 * work on a `br_ssl_engine_context` structure shall take as parameter
2152 * a pointer to that field. The other structure fields are opaque and
2153 * must not be accessed directly.
2155 struct br_ssl_client_context_
{
2157 * \brief The encapsulated engine context.
2159 br_ssl_engine_context eng
;
2161 #ifndef BR_DOXYGEN_IGNORE
2163 * Minimum ClientHello length; padding with an extension (RFC
2164 * 7685) is added if necessary to match at least that length.
2165 * Such padding is nominally unnecessary, but it has been used
2166 * to work around some server implementation bugs.
2168 uint16_t min_clienthello_len
;
2171 * Bit field for algoithms (hash + signature) supported by the
2172 * server when requesting a client certificate.
2177 * Server's public key curve.
2182 * Context for certificate handler.
2184 const br_ssl_client_certificate_class
**client_auth_vtable
;
2187 * Client authentication type.
2189 unsigned char auth_type
;
2192 * Hash function to use for the client signature. This is 0xFF
2193 * if static ECDH is used.
2195 unsigned char hash_id
;
2198 * For the core certificate handlers, thus avoiding (in most
2199 * cases) the need for an externally provided policy context.
2202 const br_ssl_client_certificate_class
*vtable
;
2203 br_ssl_client_certificate_rsa_context single_rsa
;
2204 br_ssl_client_certificate_ec_context single_ec
;
2210 br_rsa_public irsapub
;
2215 * \brief Get the hash functions and signature algorithms supported by
2218 * This is a field of bits: for hash function of ID x, bit x is set if
2219 * the hash function is supported in RSA signatures, 8+x if it is supported
2220 * with ECDSA. This information is conveyed by the server when requesting
2221 * a client certificate.
2223 * \param cc client context.
2224 * \return the server-supported hash functions (for signatures).
2226 static inline uint16_t
2227 br_ssl_client_get_server_hashes(const br_ssl_client_context
*cc
)
2233 * \brief Get the server key curve.
2235 * This function returns the ID for the curve used by the server's public
2236 * key. This is set when the server's certificate chain is processed;
2237 * this value is 0 if the server's key is not an EC key.
2239 * \return the server's public key curve ID, or 0.
2242 br_ssl_client_get_server_curve(const br_ssl_client_context
*cc
)
2244 return cc
->server_curve
;
2248 * Each br_ssl_client_init_xxx() function sets the list of supported
2249 * cipher suites and used implementations, as specified by the profile
2250 * name 'xxx'. Defined profile names are:
2252 * full all supported versions and suites; constant-time implementations
2253 * TODO: add other profiles
2257 * \brief SSL client profile: full.
2259 * This function initialises the provided SSL client context with
2260 * all supported algorithms and cipher suites. It also initialises
2261 * a companion X.509 validation engine with all supported algorithms,
2262 * and the provided trust anchors; the X.509 engine will be used by
2263 * the client context to validate the server's certificate.
2265 * \param cc client context to initialise.
2266 * \param xc X.509 validation context to initialise.
2267 * \param trust_anchors trust anchors to use.
2268 * \param trust_anchors_num number of trust anchors.
2270 void br_ssl_client_init_full(br_ssl_client_context
*cc
,
2271 br_x509_minimal_context
*xc
,
2272 const br_x509_trust_anchor
*trust_anchors
, size_t trust_anchors_num
);
2275 * \brief Clear the complete contents of a SSL client context.
2277 * Everything is cleared, including the reference to the configured buffer,
2278 * implementations, cipher suites and state. This is a preparatory step
2279 * to assembling a custom profile.
2281 * \param cc client context to clear.
2283 void br_ssl_client_zero(br_ssl_client_context
*cc
);
2286 * \brief Set an externally provided client certificate handler context.
2288 * The handler's methods are invoked when the server requests a client
2291 * \param cc client context.
2292 * \param pctx certificate handler context (pointer to its vtable field).
2295 br_ssl_client_set_client_certificate(br_ssl_client_context
*cc
,
2296 const br_ssl_client_certificate_class
**pctx
)
2298 cc
->client_auth_vtable
= pctx
;
2302 * \brief Set the RSA public-key operations implementation.
2304 * This will be used to encrypt the pre-master secret with the server's
2305 * RSA public key (RSA-encryption cipher suites only).
2307 * \param cc client context.
2308 * \param irsapub RSA public-key encryption implementation.
2311 br_ssl_client_set_rsapub(br_ssl_client_context
*cc
, br_rsa_public irsapub
)
2313 cc
->irsapub
= irsapub
;
2317 * \brief Set the minimum ClientHello length (RFC 7685 padding).
2319 * If this value is set and the ClientHello would be shorter, then
2320 * the Pad ClientHello extension will be added with enough padding bytes
2321 * to reach the target size. Because of the extension header, the resulting
2322 * size will sometimes be slightly more than `len` bytes if the target
2323 * size cannot be exactly met.
2325 * The target length relates to the _contents_ of the ClientHello, not
2326 * counting its 4-byte header. For instance, if `len` is set to 512,
2327 * then the padding will bring the ClientHello size to 516 bytes with its
2328 * header, and 521 bytes when counting the 5-byte record header.
2330 * \param cc client context.
2331 * \param len minimum ClientHello length (in bytes).
2334 br_ssl_client_set_min_clienthello_len(br_ssl_client_context
*cc
, uint16_t len
)
2336 cc
->min_clienthello_len
= len
;
2340 * \brief Prepare or reset a client context for a new connection.
2342 * The `server_name` parameter is used to fill the SNI extension; the
2343 * X.509 "minimal" engine will also match that name against the server
2344 * names included in the server's certificate. If the parameter is
2345 * `NULL` then no SNI extension will be sent, and the X.509 "minimal"
2346 * engine (if used for server certificate validation) will not check
2347 * presence of any specific name in the received certificate.
2349 * Therefore, setting the `server_name` to `NULL` shall be reserved
2350 * to cases where alternate or additional methods are used to ascertain
2351 * that the right server public key is used (e.g. a "known key" model).
2353 * If `resume_session` is non-zero and the context was previously used
2354 * then the session parameters may be reused (depending on whether the
2355 * server previously sent a non-empty session ID, and accepts the session
2356 * resumption). The session parameters for session resumption can also
2357 * be set explicitly with `br_ssl_engine_set_session_parameters()`.
2359 * On failure, the context is marked as failed, and this function
2360 * returns 0. A possible failure condition is when no initial entropy
2361 * was injected, and none could be obtained from the OS (either OS
2362 * randomness gathering is not supported, or it failed).
2364 * \param cc client context.
2365 * \param server_name target server name, or `NULL`.
2366 * \param resume_session non-zero to try session resumption.
2367 * \return 0 on failure, 1 on success.
2369 int br_ssl_client_reset(br_ssl_client_context
*cc
,
2370 const char *server_name
, int resume_session
);
2373 * \brief Forget any session in the context.
2375 * This means that the next handshake that uses this context will
2376 * necessarily be a full handshake (this applies both to new connections
2377 * and to renegotiations).
2379 * \param cc client context.
2382 br_ssl_client_forget_session(br_ssl_client_context
*cc
)
2384 cc
->eng
.session
.session_id_len
= 0;
2388 * \brief Set client certificate chain and key (single RSA case).
2390 * This function sets a client certificate chain, that the client will
2391 * send to the server whenever a client certificate is requested. This
2392 * certificate uses an RSA public key; the corresponding private key is
2393 * invoked for authentication. Trust anchor names sent by the server are
2396 * The provided chain and private key are linked in the client context;
2397 * they must remain valid as long as they may be used, i.e. normally
2398 * for the duration of the connection, since they might be invoked
2399 * again upon renegotiations.
2401 * \param cc SSL client context.
2402 * \param chain client certificate chain (SSL order: EE comes first).
2403 * \param chain_len client chain length (number of certificates).
2404 * \param sk client private key.
2405 * \param irsasign RSA signature implementation (PKCS#1 v1.5).
2407 void br_ssl_client_set_single_rsa(br_ssl_client_context
*cc
,
2408 const br_x509_certificate
*chain
, size_t chain_len
,
2409 const br_rsa_private_key
*sk
, br_rsa_pkcs1_sign irsasign
);
2412 * \brief Set the client certificate chain and key (single EC case).
2414 * This function sets a client certificate chain, that the client will
2415 * send to the server whenever a client certificate is requested. This
2416 * certificate uses an EC public key; the corresponding private key is
2417 * invoked for authentication. Trust anchor names sent by the server are
2420 * The provided chain and private key are linked in the client context;
2421 * they must remain valid as long as they may be used, i.e. normally
2422 * for the duration of the connection, since they might be invoked
2423 * again upon renegotiations.
2425 * The `allowed_usages` is a combination of usages, namely
2426 * `BR_KEYTYPE_KEYX` and/or `BR_KEYTYPE_SIGN`. The `BR_KEYTYPE_KEYX`
2427 * value allows full static ECDH, while the `BR_KEYTYPE_SIGN` value
2428 * allows ECDSA signatures. If ECDSA signatures are used, then an ECDSA
2429 * signature implementation must be provided; otherwise, the `iecdsa`
2430 * parameter may be 0.
2432 * The `cert_issuer_key_type` value is either `BR_KEYTYPE_RSA` or
2433 * `BR_KEYTYPE_EC`; it is the type of the public key used the the CA
2434 * that issued (signed) the client certificate. That value is used with
2435 * full static ECDH: support of the certificate by the server depends
2436 * on how the certificate was signed. (Note: when using TLS 1.2, this
2437 * parameter is ignored; but its value matters for TLS 1.0 and 1.1.)
2439 * \param cc server context.
2440 * \param chain server certificate chain to send.
2441 * \param chain_len chain length (number of certificates).
2442 * \param sk server private key (EC).
2443 * \param allowed_usages allowed private key usages.
2444 * \param cert_issuer_key_type issuing CA's key type.
2445 * \param iec EC core implementation.
2446 * \param iecdsa ECDSA signature implementation ("asn1" format).
2448 void br_ssl_client_set_single_ec(br_ssl_client_context
*cc
,
2449 const br_x509_certificate
*chain
, size_t chain_len
,
2450 const br_ec_private_key
*sk
, unsigned allowed_usages
,
2451 unsigned cert_issuer_key_type
,
2452 const br_ec_impl
*iec
, br_ecdsa_sign iecdsa
);
2455 * \brief Type for a "translated cipher suite", as an array of two
2458 * The first element is the cipher suite identifier (as used on the wire).
2459 * The second element is the concatenation of four 4-bit elements which
2460 * characterise the cipher suite contents. In most to least significant
2461 * order, these 4-bit elements are:
2463 * - Bits 12 to 15: key exchange + server key type
2465 * | val | symbolic constant | suite type | details |
2466 * | :-- | :----------------------- | :---------- | :----------------------------------------------- |
2467 * | 0 | `BR_SSLKEYX_RSA` | RSA | RSA key exchange, key is RSA (encryption) |
2468 * | 1 | `BR_SSLKEYX_ECDHE_RSA` | ECDHE_RSA | ECDHE key exchange, key is RSA (signature) |
2469 * | 2 | `BR_SSLKEYX_ECDHE_ECDSA` | ECDHE_ECDSA | ECDHE key exchange, key is EC (signature) |
2470 * | 3 | `BR_SSLKEYX_ECDH_RSA` | ECDH_RSA | Key is EC (key exchange), cert signed with RSA |
2471 * | 4 | `BR_SSLKEYX_ECDH_ECDSA` | ECDH_ECDSA | Key is EC (key exchange), cert signed with ECDSA |
2473 * - Bits 8 to 11: symmetric encryption algorithm
2475 * | val | symbolic constant | symmetric encryption | key strength (bits) |
2476 * | :-- | :--------------------- | :------------------- | :------------------ |
2477 * | 0 | `BR_SSLENC_3DES_CBC` | 3DES/CBC | 168 |
2478 * | 1 | `BR_SSLENC_AES128_CBC` | AES-128/CBC | 128 |
2479 * | 2 | `BR_SSLENC_AES256_CBC` | AES-256/CBC | 256 |
2480 * | 3 | `BR_SSLENC_AES128_GCM` | AES-128/GCM | 128 |
2481 * | 4 | `BR_SSLENC_AES256_GCM` | AES-256/GCM | 256 |
2482 * | 5 | `BR_SSLENC_CHACHA20` | ChaCha20/Poly1305 | 256 |
2484 * - Bits 4 to 7: MAC algorithm
2486 * | val | symbolic constant | MAC type | details |
2487 * | :-- | :----------------- | :----------- | :------------------------------------ |
2488 * | 0 | `BR_SSLMAC_AEAD` | AEAD | No dedicated MAC (encryption is AEAD) |
2489 * | 2 | `BR_SSLMAC_SHA1` | HMAC/SHA-1 | Value matches `br_sha1_ID` |
2490 * | 4 | `BR_SSLMAC_SHA256` | HMAC/SHA-256 | Value matches `br_sha256_ID` |
2491 * | 5 | `BR_SSLMAC_SHA384` | HMAC/SHA-384 | Value matches `br_sha384_ID` |
2493 * - Bits 0 to 3: hash function for PRF when used with TLS-1.2
2495 * | val | symbolic constant | hash function | details |
2496 * | :-- | :----------------- | :------------ | :----------------------------------- |
2497 * | 4 | `BR_SSLPRF_SHA256` | SHA-256 | Value matches `br_sha256_ID` |
2498 * | 5 | `BR_SSLPRF_SHA384` | SHA-384 | Value matches `br_sha384_ID` |
2500 * For instance, cipher suite `TLS_RSA_WITH_AES_128_GCM_SHA256` has
2501 * standard identifier 0x009C, and is translated to 0x0304, for, in
2502 * that order: RSA key exchange (0), AES-128/GCM (3), AEAD integrity (0),
2503 * SHA-256 in the TLS PRF (4).
2505 typedef uint16_t br_suite_translated
[2];
2507 #ifndef BR_DOXYGEN_IGNORE
2509 * Constants are already documented in the br_suite_translated type.
2512 #define BR_SSLKEYX_RSA 0
2513 #define BR_SSLKEYX_ECDHE_RSA 1
2514 #define BR_SSLKEYX_ECDHE_ECDSA 2
2515 #define BR_SSLKEYX_ECDH_RSA 3
2516 #define BR_SSLKEYX_ECDH_ECDSA 4
2518 #define BR_SSLENC_3DES_CBC 0
2519 #define BR_SSLENC_AES128_CBC 1
2520 #define BR_SSLENC_AES256_CBC 2
2521 #define BR_SSLENC_AES128_GCM 3
2522 #define BR_SSLENC_AES256_GCM 4
2523 #define BR_SSLENC_CHACHA20 5
2525 #define BR_SSLMAC_AEAD 0
2526 #define BR_SSLMAC_SHA1 br_sha1_ID
2527 #define BR_SSLMAC_SHA256 br_sha256_ID
2528 #define BR_SSLMAC_SHA384 br_sha384_ID
2530 #define BR_SSLPRF_SHA256 br_sha256_ID
2531 #define BR_SSLPRF_SHA384 br_sha384_ID
2536 * Pre-declaration for the SSL server context.
2538 typedef struct br_ssl_server_context_ br_ssl_server_context
;
2541 * \brief Type for the server policy choices, taken after analysis of
2542 * the client message (ClientHello).
2546 * \brief Cipher suite to use with that client.
2548 uint16_t cipher_suite
;
2551 * \brief Hash function for signing the ServerKeyExchange.
2553 * This is the symbolic identifier for the hash function that
2554 * will be used to sign the ServerKeyExchange message, for ECDHE
2555 * cipher suites. This is ignored for RSA and ECDH cipher suites.
2557 * Take care that with TLS 1.0 and 1.1, that value MUST match
2558 * the protocol requirements: value must be 0 (MD5+SHA-1) for
2559 * a RSA signature, or 2 (SHA-1) for an ECDSA signature. Only
2560 * TLS 1.2 allows for other hash functions.
2565 * \brief Certificate chain to send to the client.
2567 * This is an array of `br_x509_certificate` objects, each
2568 * normally containing a DER-encoded certificate. The server
2569 * code does not try to decode these elements.
2571 const br_x509_certificate
*chain
;
2574 * \brief Certificate chain length (number of certificates).
2578 } br_ssl_server_choices
;
2581 * \brief Class type for a policy handler (server side).
2583 * A policy handler selects the policy parameters for a connection
2584 * (cipher suite and other algorithms, and certificate chain to send to
2585 * the client); it also performs the server-side computations involving
2586 * its permanent private key.
2588 * The SSL server engine will invoke first `choose()`, once the
2589 * ClientHello message has been received, then either `do_keyx()`
2590 * `do_sign()`, depending on the cipher suite.
2592 typedef struct br_ssl_server_policy_class_ br_ssl_server_policy_class
;
2593 struct br_ssl_server_policy_class_
{
2595 * \brief Context size (in bytes).
2597 size_t context_size
;
2600 * \brief Select algorithms and certificates for this connection.
2602 * This callback function shall fill the provided `choices`
2603 * structure with the policy choices for this connection. This
2604 * entails selecting the cipher suite, hash function for signing
2605 * the ServerKeyExchange (applicable only to ECDHE cipher suites),
2606 * and certificate chain to send.
2608 * The callback receives a pointer to the server context that
2609 * contains the relevant data. In particular, the functions
2610 * `br_ssl_server_get_client_suites()`,
2611 * `br_ssl_server_get_client_hashes()` and
2612 * `br_ssl_server_get_client_curves()` can be used to obtain
2613 * the cipher suites, hash functions and elliptic curves
2614 * supported by both the client and server, respectively. The
2615 * `br_ssl_engine_get_version()` and `br_ssl_engine_get_server_name()`
2616 * functions yield the protocol version and requested server name
2617 * (SNI), respectively.
2619 * This function may modify its context structure (`pctx`) in
2620 * arbitrary ways to keep track of its own choices.
2622 * This function shall return 1 if appropriate policy choices
2623 * could be made, or 0 if this connection cannot be pursued.
2625 * \param pctx policy context.
2626 * \param cc SSL server context.
2627 * \param choices destination structure for the policy choices.
2628 * \return 1 on success, 0 on error.
2630 int (*choose
)(const br_ssl_server_policy_class
**pctx
,
2631 const br_ssl_server_context
*cc
,
2632 br_ssl_server_choices
*choices
);
2635 * \brief Perform key exchange (server part).
2637 * This callback is invoked to perform the server-side cryptographic
2638 * operation for a key exchange that is not ECDHE. This callback
2639 * uses the private key.
2641 * **For RSA key exchange**, the provided `data` (of length `len`
2642 * bytes) shall be decrypted with the server's private key, and
2643 * the 48-byte premaster secret copied back to the first 48 bytes
2646 * - The caller makes sure that `len` is at least 59 bytes.
2648 * - This callback MUST check that the provided length matches
2649 * that of the key modulus; it shall report an error otherwise.
2651 * - If the length matches that of the RSA key modulus, then
2652 * processing MUST be constant-time, even if decryption fails,
2653 * or the padding is incorrect, or the plaintext message length
2654 * is not exactly 48 bytes.
2656 * - This callback needs not check the two first bytes of the
2657 * obtained pre-master secret (the caller will do that).
2659 * - If an error is reported (0), then what the callback put
2660 * in the first 48 bytes of `data` is unimportant (the caller
2661 * will use random bytes instead).
2663 * **For ECDH key exchange**, the provided `data` (of length `len`
2664 * bytes) is the elliptic curve point from the client. The
2665 * callback shall multiply it with its private key, and store
2666 * the resulting X coordinate in `data`, starting at offset 1
2667 * (thus, simply encoding the point in compressed or uncompressed
2668 * format in `data` is fine).
2670 * - If the input array does not have the proper length for
2671 * an encoded curve point, then an error (0) shall be reported.
2673 * - If the input array has the proper length, then processing
2674 * MUST be constant-time, even if the data is not a valid
2677 * - This callback MUST check that the input point is valid.
2679 * Returned value is 1 on success, 0 on error.
2681 * \param pctx policy context.
2682 * \param data key exchange data from the client.
2683 * \param len key exchange data length (in bytes).
2684 * \return 1 on success, 0 on error.
2686 uint32_t (*do_keyx
)(const br_ssl_server_policy_class
**pctx
,
2687 unsigned char *data
, size_t len
);
2690 * \brief Perform a signature (for a ServerKeyExchange message).
2692 * This callback function is invoked for ECDHE cipher suites.
2693 * On input, the hash value to sign is in `data`, of size
2694 * `hv_len`; the involved hash function is identified by
2695 * `hash_id`. The signature shall be computed and written
2696 * back into `data`; the total size of that buffer is `len`
2699 * This callback shall verify that the signature length does not
2700 * exceed `len` bytes, and abstain from writing the signature if
2703 * For RSA signatures, the `hash_id` may be 0, in which case
2704 * this is the special header-less signature specified in TLS 1.0
2705 * and 1.1, with a 36-byte hash value. Otherwise, normal PKCS#1
2706 * v1.5 signatures shall be computed.
2708 * Returned value is the signature length (in bytes), or 0 on error.
2710 * \param pctx policy context.
2711 * \param hash_id hash function identifier.
2712 * \param hv_len hash value length (in bytes).
2713 * \param data input/output buffer (hash value, then signature).
2714 * \param len total buffer length (in bytes).
2715 * \return signature length (in bytes) on success, or 0 on error.
2717 size_t (*do_sign
)(const br_ssl_server_policy_class
**pctx
,
2718 int hash_id
, size_t hv_len
, unsigned char *data
, size_t len
);
2722 * \brief A single-chain RSA policy handler.
2724 * This policy context uses a single certificate chain, and a RSA
2725 * private key. The context can be restricted to only signatures or
2726 * only key exchange.
2728 * Apart from the first field (vtable pointer), its contents are
2729 * opaque and shall not be accessed directly.
2732 /** \brief Pointer to vtable. */
2733 const br_ssl_server_policy_class
*vtable
;
2734 #ifndef BR_DOXYGEN_IGNORE
2735 const br_x509_certificate
*chain
;
2737 const br_rsa_private_key
*sk
;
2738 unsigned allowed_usages
;
2739 br_rsa_private irsacore
;
2740 br_rsa_pkcs1_sign irsasign
;
2742 } br_ssl_server_policy_rsa_context
;
2745 * \brief A single-chain EC policy handler.
2747 * This policy context uses a single certificate chain, and an EC
2748 * private key. The context can be restricted to only signatures or
2749 * only key exchange.
2751 * Due to how TLS is defined, this context must be made aware whether
2752 * the server certificate was itself signed with RSA or ECDSA. The code
2753 * does not try to decode the certificate to obtain that information.
2755 * Apart from the first field (vtable pointer), its contents are
2756 * opaque and shall not be accessed directly.
2759 /** \brief Pointer to vtable. */
2760 const br_ssl_server_policy_class
*vtable
;
2761 #ifndef BR_DOXYGEN_IGNORE
2762 const br_x509_certificate
*chain
;
2764 const br_ec_private_key
*sk
;
2765 unsigned allowed_usages
;
2766 unsigned cert_issuer_key_type
;
2767 const br_multihash_context
*mhash
;
2768 const br_ec_impl
*iec
;
2769 br_ecdsa_sign iecdsa
;
2771 } br_ssl_server_policy_ec_context
;
2774 * \brief Class type for a session parameter cache.
2776 * Session parameters are saved in the cache with `save()`, and
2777 * retrieved with `load()`. The cache implementation can apply any
2778 * storage and eviction strategy that it sees fit. The SSL server
2779 * context that performs the request is provided, so that its
2780 * functionalities may be used by the implementation (e.g. hash
2781 * functions or random number generation).
2783 typedef struct br_ssl_session_cache_class_ br_ssl_session_cache_class
;
2784 struct br_ssl_session_cache_class_
{
2786 * \brief Context size (in bytes).
2788 size_t context_size
;
2791 * \brief Record a session.
2793 * This callback should record the provided session parameters.
2794 * The `params` structure is transient, so its contents shall
2795 * be copied into the cache. The session ID has been randomly
2796 * generated and always has length exactly 32 bytes.
2798 * \param ctx session cache context.
2799 * \param server_ctx SSL server context.
2800 * \param params session parameters to save.
2802 void (*save
)(const br_ssl_session_cache_class
**ctx
,
2803 br_ssl_server_context
*server_ctx
,
2804 const br_ssl_session_parameters
*params
);
2807 * \brief Lookup a session in the cache.
2809 * The session ID to lookup is in `params` and always has length
2810 * exactly 32 bytes. If the session parameters are found in the
2811 * cache, then the parameters shall be copied into the `params`
2812 * structure. Returned value is 1 on successful lookup, 0
2815 * \param ctx session cache context.
2816 * \param server_ctx SSL server context.
2817 * \param params destination for session parameters.
2818 * \return 1 if found, 0 otherwise.
2820 int (*load
)(const br_ssl_session_cache_class
**ctx
,
2821 br_ssl_server_context
*server_ctx
,
2822 br_ssl_session_parameters
*params
);
2826 * \brief Context for a basic cache system.
2828 * The system stores session parameters in a buffer provided at
2829 * initialisation time. Each entry uses exactly 100 bytes, and
2830 * buffer sizes up to 4294967295 bytes are supported.
2832 * Entries are evicted with a LRU (Least Recently Used) policy. A
2833 * search tree is maintained to keep lookups fast even with large
2836 * Apart from the first field (vtable pointer), the structure
2837 * contents are opaque and shall not be accessed directly.
2840 /** \brief Pointer to vtable. */
2841 const br_ssl_session_cache_class
*vtable
;
2842 #ifndef BR_DOXYGEN_IGNORE
2843 unsigned char *store
;
2844 size_t store_len
, store_ptr
;
2845 unsigned char index_key
[32];
2846 const br_hash_class
*hash
;
2848 uint32_t head
, tail
, root
;
2850 } br_ssl_session_cache_lru
;
2853 * \brief Initialise a LRU session cache with the provided storage space.
2855 * The provided storage space must remain valid as long as the cache
2856 * is used. Arbitrary lengths are supported, up to 4294967295 bytes;
2857 * each entry uses up exactly 100 bytes.
2859 * \param cc session cache context.
2860 * \param store storage space for cached entries.
2861 * \param store_len storage space length (in bytes).
2863 void br_ssl_session_cache_lru_init(br_ssl_session_cache_lru
*cc
,
2864 unsigned char *store
, size_t store_len
);
2867 * \brief Context structure for a SSL server.
2869 * The first field (called `eng`) is the SSL engine; all functions that
2870 * work on a `br_ssl_engine_context` structure shall take as parameter
2871 * a pointer to that field. The other structure fields are opaque and
2872 * must not be accessed directly.
2874 struct br_ssl_server_context_
{
2876 * \brief The encapsulated engine context.
2878 br_ssl_engine_context eng
;
2880 #ifndef BR_DOXYGEN_IGNORE
2882 * Maximum version from the client.
2884 uint16_t client_max_version
;
2889 const br_ssl_session_cache_class
**cache_vtable
;
2892 * Translated cipher suites supported by the client. The list
2893 * is trimmed to include only the cipher suites that the
2894 * server also supports; they are in the same order as in the
2897 br_suite_translated client_suites
[BR_MAX_CIPHER_SUITES
];
2898 unsigned char client_suites_num
;
2901 * Hash functions supported by the client, with ECDSA and RSA
2902 * (bit mask). For hash function with id 'x', set bit index is
2903 * x for RSA, x+8 for ECDSA.
2908 * Curves supported by the client (bit mask, for named curves).
2913 * Context for chain handler.
2915 const br_ssl_server_policy_class
**policy_vtable
;
2916 unsigned char sign_hash_id
;
2919 * For the core handlers, thus avoiding (in most cases) the
2920 * need for an externally provided policy context.
2923 const br_ssl_server_policy_class
*vtable
;
2924 br_ssl_server_policy_rsa_context single_rsa
;
2925 br_ssl_server_policy_ec_context single_ec
;
2929 * Buffer for the ECDHE private key.
2931 unsigned char ecdhe_key
[70];
2932 size_t ecdhe_key_len
;
2935 * Trust anchor names for client authentication. "ta_names" and
2936 * "tas" cannot be both non-NULL.
2938 const br_x500_name
*ta_names
;
2939 const br_x509_trust_anchor
*tas
;
2941 size_t cur_dn_index
;
2942 const unsigned char *cur_dn
;
2946 * Buffer for the hash value computed over all handshake messages
2947 * prior to CertificateVerify, and identifier for the hash function.
2949 unsigned char hash_CV
[64];
2954 * Server-specific implementations.
2961 * Each br_ssl_server_init_xxx() function sets the list of supported
2962 * cipher suites and used implementations, as specified by the profile
2963 * name 'xxx'. Defined profile names are:
2965 * full_rsa all supported algorithm, server key type is RSA
2966 * full_ec all supported algorithm, server key type is EC
2967 * TODO: add other profiles
2969 * Naming scheme for "minimal" profiles: min123
2971 * -- character 1: key exchange
2977 * -- character 2: version / PRF
2978 * 0 = TLS 1.0 / 1.1 with MD5+SHA-1
2979 * 2 = TLS 1.2 with SHA-256
2980 * 3 = TLS 1.2 with SHA-384
2981 * -- character 3: encryption
2988 * \brief SSL server profile: full_rsa.
2990 * This function initialises the provided SSL server context with
2991 * all supported algorithms and cipher suites that rely on a RSA
2994 * \param cc server context to initialise.
2995 * \param chain server certificate chain.
2996 * \param chain_len certificate chain length (number of certificate).
2997 * \param sk RSA private key.
2999 void br_ssl_server_init_full_rsa(br_ssl_server_context
*cc
,
3000 const br_x509_certificate
*chain
, size_t chain_len
,
3001 const br_rsa_private_key
*sk
);
3004 * \brief SSL server profile: full_ec.
3006 * This function initialises the provided SSL server context with
3007 * all supported algorithms and cipher suites that rely on an EC
3010 * The key type of the CA that issued the server's certificate must
3011 * be provided, since it matters for ECDH cipher suites (ECDH_RSA
3012 * suites require a RSA-powered CA). The key type is either
3013 * `BR_KEYTYPE_RSA` or `BR_KEYTYPE_EC`.
3015 * \param cc server context to initialise.
3016 * \param chain server certificate chain.
3017 * \param chain_len chain length (number of certificates).
3018 * \param cert_issuer_key_type certificate issuer's key type.
3019 * \param sk EC private key.
3021 void br_ssl_server_init_full_ec(br_ssl_server_context
*cc
,
3022 const br_x509_certificate
*chain
, size_t chain_len
,
3023 unsigned cert_issuer_key_type
, const br_ec_private_key
*sk
);
3026 * \brief SSL server profile: minr2g.
3028 * This profile uses only TLS_RSA_WITH_AES_128_GCM_SHA256. Server key is
3029 * RSA, and RSA key exchange is used (not forward secure, but uses little
3030 * CPU in the client).
3032 * \param cc server context to initialise.
3033 * \param chain server certificate chain.
3034 * \param chain_len certificate chain length (number of certificate).
3035 * \param sk RSA private key.
3037 void br_ssl_server_init_minr2g(br_ssl_server_context
*cc
,
3038 const br_x509_certificate
*chain
, size_t chain_len
,
3039 const br_rsa_private_key
*sk
);
3042 * \brief SSL server profile: mine2g.
3044 * This profile uses only TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256. Server key
3045 * is RSA, and ECDHE key exchange is used. This suite provides forward
3046 * security, with a higher CPU expense on the client, and a somewhat
3047 * larger code footprint (compared to "minr2g").
3049 * \param cc server context to initialise.
3050 * \param chain server certificate chain.
3051 * \param chain_len certificate chain length (number of certificate).
3052 * \param sk RSA private key.
3054 void br_ssl_server_init_mine2g(br_ssl_server_context
*cc
,
3055 const br_x509_certificate
*chain
, size_t chain_len
,
3056 const br_rsa_private_key
*sk
);
3059 * \brief SSL server profile: minf2g.
3061 * This profile uses only TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256.
3062 * Server key is EC, and ECDHE key exchange is used. This suite provides
3063 * forward security, with a higher CPU expense on the client and server
3064 * (by a factor of about 3 to 4), and a somewhat larger code footprint
3065 * (compared to "minu2g" and "minv2g").
3067 * \param cc server context to initialise.
3068 * \param chain server certificate chain.
3069 * \param chain_len certificate chain length (number of certificate).
3070 * \param sk EC private key.
3072 void br_ssl_server_init_minf2g(br_ssl_server_context
*cc
,
3073 const br_x509_certificate
*chain
, size_t chain_len
,
3074 const br_ec_private_key
*sk
);
3077 * \brief SSL server profile: minu2g.
3079 * This profile uses only TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256.
3080 * Server key is EC, and ECDH key exchange is used; the issuing CA used
3083 * The "minu2g" and "minv2g" profiles do not provide forward secrecy,
3084 * but are the lightest on the server (for CPU usage), and are rather
3085 * inexpensive on the client as well.
3087 * \param cc server context to initialise.
3088 * \param chain server certificate chain.
3089 * \param chain_len certificate chain length (number of certificate).
3090 * \param sk EC private key.
3092 void br_ssl_server_init_minu2g(br_ssl_server_context
*cc
,
3093 const br_x509_certificate
*chain
, size_t chain_len
,
3094 const br_ec_private_key
*sk
);
3097 * \brief SSL server profile: minv2g.
3099 * This profile uses only TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256.
3100 * Server key is EC, and ECDH key exchange is used; the issuing CA used
3103 * The "minu2g" and "minv2g" profiles do not provide forward secrecy,
3104 * but are the lightest on the server (for CPU usage), and are rather
3105 * inexpensive on the client as well.
3107 * \param cc server context to initialise.
3108 * \param chain server certificate chain.
3109 * \param chain_len certificate chain length (number of certificate).
3110 * \param sk EC private key.
3112 void br_ssl_server_init_minv2g(br_ssl_server_context
*cc
,
3113 const br_x509_certificate
*chain
, size_t chain_len
,
3114 const br_ec_private_key
*sk
);
3117 * \brief SSL server profile: mine2c.
3119 * This profile uses only TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256.
3120 * Server key is RSA, and ECDHE key exchange is used. This suite
3121 * provides forward security.
3123 * \param cc server context to initialise.
3124 * \param chain server certificate chain.
3125 * \param chain_len certificate chain length (number of certificate).
3126 * \param sk RSA private key.
3128 void br_ssl_server_init_mine2c(br_ssl_server_context
*cc
,
3129 const br_x509_certificate
*chain
, size_t chain_len
,
3130 const br_rsa_private_key
*sk
);
3133 * \brief SSL server profile: minf2c.
3135 * This profile uses only TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256.
3136 * Server key is EC, and ECDHE key exchange is used. This suite provides
3139 * \param cc server context to initialise.
3140 * \param chain server certificate chain.
3141 * \param chain_len certificate chain length (number of certificate).
3142 * \param sk EC private key.
3144 void br_ssl_server_init_minf2c(br_ssl_server_context
*cc
,
3145 const br_x509_certificate
*chain
, size_t chain_len
,
3146 const br_ec_private_key
*sk
);
3149 * \brief Get the supported client suites.
3151 * This function shall be called only after the ClientHello has been
3152 * processed, typically from the policy engine. The returned array
3153 * contains the cipher suites that are supported by both the client
3154 * and the server; these suites are in client preference order, unless
3155 * the `BR_OPT_ENFORCE_SERVER_PREFERENCES` flag was set, in which case
3156 * they are in server preference order.
3158 * The suites are _translated_, which means that each suite is given
3159 * as two 16-bit integers: the standard suite identifier, and its
3160 * translated version, broken down into its individual components,
3161 * as explained with the `br_suite_translated` type.
3163 * The returned array is allocated in the context and will be rewritten
3164 * by each handshake.
3166 * \param cc server context.
3167 * \param num receives the array size (number of suites).
3168 * \return the translated common cipher suites, in preference order.
3170 static inline const br_suite_translated
*
3171 br_ssl_server_get_client_suites(const br_ssl_server_context
*cc
, size_t *num
)
3173 *num
= cc
->client_suites_num
;
3174 return cc
->client_suites
;
3178 * \brief Get the hash functions supported by the client.
3180 * This is a field of bits: for hash function of ID x, bit x is set if
3181 * the hash function is supported in RSA signatures, 8+x if it is supported
3184 * \param cc server context.
3185 * \return the client-supported hash functions (for signatures).
3187 static inline uint16_t
3188 br_ssl_server_get_client_hashes(const br_ssl_server_context
*cc
)
3194 * \brief Get the elliptic curves supported by the client.
3196 * This is a bit field (bit x is set if curve of ID x is supported).
3198 * \param cc server context.
3199 * \return the client-supported elliptic curves.
3201 static inline uint32_t
3202 br_ssl_server_get_client_curves(const br_ssl_server_context
*cc
)
3208 * \brief Clear the complete contents of a SSL server context.
3210 * Everything is cleared, including the reference to the configured buffer,
3211 * implementations, cipher suites and state. This is a preparatory step
3212 * to assembling a custom profile.
3214 * \param cc server context to clear.
3216 void br_ssl_server_zero(br_ssl_server_context
*cc
);
3219 * \brief Set an externally provided policy context.
3221 * The policy context's methods are invoked to decide the cipher suite
3222 * and certificate chain, and to perform operations involving the server's
3225 * \param cc server context.
3226 * \param pctx policy context (pointer to its vtable field).
3229 br_ssl_server_set_policy(br_ssl_server_context
*cc
,
3230 const br_ssl_server_policy_class
**pctx
)
3232 cc
->policy_vtable
= pctx
;
3236 * \brief Set the server certificate chain and key (single RSA case).
3238 * This function uses a policy context included in the server context.
3239 * It configures use of a single server certificate chain with a RSA
3240 * private key. The `allowed_usages` is a combination of usages, namely
3241 * `BR_KEYTYPE_KEYX` and/or `BR_KEYTYPE_SIGN`; this enables or disables
3242 * the corresponding cipher suites (i.e. `TLS_RSA_*` use the RSA key for
3243 * key exchange, while `TLS_ECDHE_RSA_*` use the RSA key for signatures).
3245 * \param cc server context.
3246 * \param chain server certificate chain to send to the client.
3247 * \param chain_len chain length (number of certificates).
3248 * \param sk server private key (RSA).
3249 * \param allowed_usages allowed private key usages.
3250 * \param irsacore RSA core implementation.
3251 * \param irsasign RSA signature implementation (PKCS#1 v1.5).
3253 void br_ssl_server_set_single_rsa(br_ssl_server_context
*cc
,
3254 const br_x509_certificate
*chain
, size_t chain_len
,
3255 const br_rsa_private_key
*sk
, unsigned allowed_usages
,
3256 br_rsa_private irsacore
, br_rsa_pkcs1_sign irsasign
);
3259 * \brief Set the server certificate chain and key (single EC case).
3261 * This function uses a policy context included in the server context.
3262 * It configures use of a single server certificate chain with an EC
3263 * private key. The `allowed_usages` is a combination of usages, namely
3264 * `BR_KEYTYPE_KEYX` and/or `BR_KEYTYPE_SIGN`; this enables or disables
3265 * the corresponding cipher suites (i.e. `TLS_ECDH_*` use the EC key for
3266 * key exchange, while `TLS_ECDHE_ECDSA_*` use the EC key for signatures).
3268 * In order to support `TLS_ECDH_*` cipher suites (non-ephemeral ECDH),
3269 * the algorithm type of the key used by the issuing CA to sign the
3270 * server's certificate must be provided, as `cert_issuer_key_type`
3271 * parameter (this value is either `BR_KEYTYPE_RSA` or `BR_KEYTYPE_EC`).
3273 * \param cc server context.
3274 * \param chain server certificate chain to send.
3275 * \param chain_len chain length (number of certificates).
3276 * \param sk server private key (EC).
3277 * \param allowed_usages allowed private key usages.
3278 * \param cert_issuer_key_type issuing CA's key type.
3279 * \param iec EC core implementation.
3280 * \param iecdsa ECDSA signature implementation ("asn1" format).
3282 void br_ssl_server_set_single_ec(br_ssl_server_context
*cc
,
3283 const br_x509_certificate
*chain
, size_t chain_len
,
3284 const br_ec_private_key
*sk
, unsigned allowed_usages
,
3285 unsigned cert_issuer_key_type
,
3286 const br_ec_impl
*iec
, br_ecdsa_sign iecdsa
);
3289 * \brief Activate client certificate authentication.
3291 * The trust anchor encoded X.500 names (DN) to send to the client are
3292 * provided. A client certificate will be requested and validated through
3293 * the X.509 validator configured in the SSL engine. If `num` is 0, then
3294 * client certificate authentication is disabled.
3296 * If the client does not send a certificate, or on validation failure,
3297 * the handshake aborts. Unauthenticated clients can be tolerated by
3298 * setting the `BR_OPT_TOLERATE_NO_CLIENT_AUTH` flag.
3300 * The provided array is linked in, not copied, so that pointer must
3301 * remain valid as long as anchor names may be used.
3303 * \param cc server context.
3304 * \param ta_names encoded trust anchor names.
3305 * \param num number of encoded trust anchor names.
3308 br_ssl_server_set_trust_anchor_names(br_ssl_server_context
*cc
,
3309 const br_x500_name
*ta_names
, size_t num
)
3311 cc
->ta_names
= ta_names
;
3317 * \brief Activate client certificate authentication.
3319 * This is a variant for `br_ssl_server_set_trust_anchor_names()`: the
3320 * trust anchor names are provided not as an array of stand-alone names
3321 * (`br_x500_name` structures), but as an array of trust anchors
3322 * (`br_x509_trust_anchor` structures). The server engine itself will
3323 * only use the `dn` field of each trust anchor. This is meant to allow
3324 * defining a single array of trust anchors, to be used here and in the
3325 * X.509 validation engine itself.
3327 * The provided array is linked in, not copied, so that pointer must
3328 * remain valid as long as anchor names may be used.
3330 * \param cc server context.
3331 * \param tas trust anchors (only names are used).
3332 * \param num number of trust anchors.
3335 br_ssl_server_set_trust_anchor_names_alt(br_ssl_server_context
*cc
,
3336 const br_x509_trust_anchor
*tas
, size_t num
)
3338 cc
->ta_names
= NULL
;
3344 * \brief Configure the cache for session parameters.
3346 * The cache context is provided as a pointer to its first field (vtable
3349 * \param cc server context.
3350 * \param vtable session cache context.
3353 br_ssl_server_set_cache(br_ssl_server_context
*cc
,
3354 const br_ssl_session_cache_class
**vtable
)
3356 cc
->cache_vtable
= vtable
;
3360 * \brief Prepare or reset a server context for handling an incoming client.
3362 * \param cc server context.
3363 * \return 1 on success, 0 on error.
3365 int br_ssl_server_reset(br_ssl_server_context
*cc
);
3367 /* ===================================================================== */
3370 * Context for the simplified I/O context. The transport medium is accessed
3371 * through the low_read() and low_write() callback functions, each with
3372 * its own opaque context pointer.
3374 * low_read() read some bytes, at most 'len' bytes, into data[]. The
3375 * returned value is the number of read bytes, or -1 on error.
3376 * The 'len' parameter is guaranteed never to exceed 20000,
3377 * so the length always fits in an 'int' on all platforms.
3379 * low_write() write up to 'len' bytes, to be read from data[]. The
3380 * returned value is the number of written bytes, or -1 on
3381 * error. The 'len' parameter is guaranteed never to exceed
3382 * 20000, so the length always fits in an 'int' on all
3385 * A socket closure (if the transport medium is a socket) should be reported
3386 * as an error (-1). The callbacks shall endeavour to block until at least
3387 * one byte can be read or written; a callback returning 0 at times is
3388 * acceptable, but this normally leads to the callback being immediately
3389 * called again, so the callback should at least always try to block for
3390 * some time if no I/O can take place.
3392 * The SSL engine naturally applies some buffering, so the callbacks need
3393 * not apply buffers of their own.
3396 * \brief Context structure for the simplified SSL I/O wrapper.
3398 * This structure is initialised with `br_sslio_init()`. Its contents
3399 * are opaque and shall not be accessed directly.
3402 #ifndef BR_DOXYGEN_IGNORE
3403 br_ssl_engine_context
*engine
;
3404 int (*low_read
)(void *read_context
,
3405 unsigned char *data
, size_t len
);
3407 int (*low_write
)(void *write_context
,
3408 const unsigned char *data
, size_t len
);
3409 void *write_context
;
3414 * \brief Initialise a simplified I/O wrapper context.
3416 * The simplified I/O wrapper offers a simpler read/write API for a SSL
3417 * engine (client or server), using the provided callback functions for
3418 * reading data from, or writing data to, the transport medium.
3420 * The callback functions have the following semantics:
3422 * - Each callback receives an opaque context value (of type `void *`)
3423 * that the callback may use arbitrarily (or possibly ignore).
3425 * - `low_read()` reads at least one byte, at most `len` bytes, from
3426 * the transport medium. Read bytes shall be written in `data`.
3428 * - `low_write()` writes at least one byte, at most `len` bytes, unto
3429 * the transport medium. The bytes to write are read from `data`.
3431 * - The `len` parameter is never zero, and is always lower than 20000.
3433 * - The number of processed bytes (read or written) is returned. Since
3434 * that number is less than 20000, it always fits on an `int`.
3436 * - On error, the callbacks return -1. Reaching end-of-stream is an
3437 * error. Errors are permanent: the SSL connection is terminated.
3439 * - Callbacks SHOULD NOT return 0. This is tolerated, as long as
3440 * callbacks endeavour to block for some non-negligible amount of
3441 * time until at least one byte can be sent or received (if a
3442 * callback returns 0, then the wrapper invokes it again
3445 * - Callbacks MAY return as soon as at least one byte is processed;
3446 * they MAY also insist on reading or writing _all_ requested bytes.
3447 * Since SSL is a self-terminated protocol (each record has a length
3448 * header), this does not change semantics.
3450 * - Callbacks need not apply any buffering (for performance) since SSL
3451 * itself uses buffers.
3453 * \param ctx wrapper context to initialise.
3454 * \param engine SSL engine to wrap.
3455 * \param low_read callback for reading data from the transport.
3456 * \param read_context context pointer for `low_read()`.
3457 * \param low_write callback for writing data on the transport.
3458 * \param write_context context pointer for `low_write()`.
3460 void br_sslio_init(br_sslio_context
*ctx
,
3461 br_ssl_engine_context
*engine
,
3462 int (*low_read
)(void *read_context
,
3463 unsigned char *data
, size_t len
),
3465 int (*low_write
)(void *write_context
,
3466 const unsigned char *data
, size_t len
),
3467 void *write_context
);
3470 * \brief Read some application data from a SSL connection.
3472 * If `len` is zero, then this function returns 0 immediately. In
3473 * all other cases, it never returns 0.
3475 * This call returns only when at least one byte has been obtained.
3476 * Returned value is the number of bytes read, or -1 on error. The
3477 * number of bytes always fits on an 'int' (data from a single SSL/TLS
3478 * record is returned).
3480 * On error or SSL closure, this function returns -1. The caller should
3481 * inspect the error status on the SSL engine to distinguish between
3482 * normal closure and error.
3484 * \param cc SSL wrapper context.
3485 * \param dst destination buffer for application data.
3486 * \param len maximum number of bytes to obtain.
3487 * \return number of bytes obtained, or -1 on error.
3489 int br_sslio_read(br_sslio_context
*cc
, void *dst
, size_t len
);
3492 * \brief Read application data from a SSL connection.
3494 * This calls returns only when _all_ requested `len` bytes are read,
3495 * or an error is reached. Returned value is 0 on success, -1 on error.
3496 * A normal (verified) SSL closure before that many bytes are obtained
3497 * is reported as an error by this function.
3499 * \param cc SSL wrapper context.
3500 * \param dst destination buffer for application data.
3501 * \param len number of bytes to obtain.
3502 * \return 0 on success, or -1 on error.
3504 int br_sslio_read_all(br_sslio_context
*cc
, void *dst
, size_t len
);
3507 * \brief Write some application data unto a SSL connection.
3509 * If `len` is zero, then this function returns 0 immediately. In
3510 * all other cases, it never returns 0.
3512 * This call returns only when at least one byte has been written.
3513 * Returned value is the number of bytes written, or -1 on error. The
3514 * number of bytes always fits on an 'int' (less than 20000).
3516 * On error or SSL closure, this function returns -1. The caller should
3517 * inspect the error status on the SSL engine to distinguish between
3518 * normal closure and error.
3520 * **Important:** SSL is buffered; a "written" byte is a byte that was
3521 * injected into the wrapped SSL engine, but this does not necessarily mean
3522 * that it has been scheduled for sending. Use `br_sslio_flush()` to
3523 * ensure that all pending data has been sent to the transport medium.
3525 * \param cc SSL wrapper context.
3526 * \param src source buffer for application data.
3527 * \param len maximum number of bytes to write.
3528 * \return number of bytes written, or -1 on error.
3530 int br_sslio_write(br_sslio_context
*cc
, const void *src
, size_t len
);
3533 * \brief Write application data unto a SSL connection.
3535 * This calls returns only when _all_ requested `len` bytes have been
3536 * written, or an error is reached. Returned value is 0 on success, -1
3537 * on error. A normal (verified) SSL closure before that many bytes are
3538 * written is reported as an error by this function.
3540 * **Important:** SSL is buffered; a "written" byte is a byte that was
3541 * injected into the wrapped SSL engine, but this does not necessarily mean
3542 * that it has been scheduled for sending. Use `br_sslio_flush()` to
3543 * ensure that all pending data has been sent to the transport medium.
3545 * \param cc SSL wrapper context.
3546 * \param src source buffer for application data.
3547 * \param len number of bytes to write.
3548 * \return 0 on success, or -1 on error.
3550 int br_sslio_write_all(br_sslio_context
*cc
, const void *src
, size_t len
);
3553 * \brief Flush pending data.
3555 * This call makes sure that any buffered application data in the
3556 * provided context (including the wrapped SSL engine) has been sent
3557 * to the transport medium (i.e. accepted by the `low_write()` callback
3558 * method). If there is no such pending data, then this function does
3559 * nothing (and returns a success, i.e. 0).
3561 * If the underlying transport medium has its own buffers, then it is
3562 * up to the caller to ensure the corresponding flushing.
3564 * Returned value is 0 on success, -1 on error.
3566 * \param cc SSL wrapper context.
3567 * \return 0 on success, or -1 on error.
3569 int br_sslio_flush(br_sslio_context
*cc
);
3572 * \brief Close the SSL connection.
3574 * This call runs the SSL closure protocol (sending a `close_notify`,
3575 * receiving the response `close_notify`). When it returns, the SSL
3576 * connection is finished. It is still up to the caller to manage the
3577 * possible transport-level termination, if applicable (alternatively,
3578 * the underlying transport stream may be reused for non-SSL messages).
3580 * Returned value is 0 on success, -1 on error. A failure by the peer
3581 * to process the complete closure protocol (i.e. sending back the
3582 * `close_notify`) is an error.
3584 * \param cc SSL wrapper context.
3585 * \return 0 on success, or -1 on error.
3587 int br_sslio_close(br_sslio_context
*cc
);
3589 /* ===================================================================== */
3592 * Symbolic constants for cipher suites.
3596 #define BR_TLS_NULL_WITH_NULL_NULL 0x0000
3597 #define BR_TLS_RSA_WITH_NULL_MD5 0x0001
3598 #define BR_TLS_RSA_WITH_NULL_SHA 0x0002
3599 #define BR_TLS_RSA_WITH_NULL_SHA256 0x003B
3600 #define BR_TLS_RSA_WITH_RC4_128_MD5 0x0004
3601 #define BR_TLS_RSA_WITH_RC4_128_SHA 0x0005
3602 #define BR_TLS_RSA_WITH_3DES_EDE_CBC_SHA 0x000A
3603 #define BR_TLS_RSA_WITH_AES_128_CBC_SHA 0x002F
3604 #define BR_TLS_RSA_WITH_AES_256_CBC_SHA 0x0035
3605 #define BR_TLS_RSA_WITH_AES_128_CBC_SHA256 0x003C
3606 #define BR_TLS_RSA_WITH_AES_256_CBC_SHA256 0x003D
3607 #define BR_TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA 0x000D
3608 #define BR_TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA 0x0010
3609 #define BR_TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA 0x0013
3610 #define BR_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA 0x0016
3611 #define BR_TLS_DH_DSS_WITH_AES_128_CBC_SHA 0x0030
3612 #define BR_TLS_DH_RSA_WITH_AES_128_CBC_SHA 0x0031
3613 #define BR_TLS_DHE_DSS_WITH_AES_128_CBC_SHA 0x0032
3614 #define BR_TLS_DHE_RSA_WITH_AES_128_CBC_SHA 0x0033
3615 #define BR_TLS_DH_DSS_WITH_AES_256_CBC_SHA 0x0036
3616 #define BR_TLS_DH_RSA_WITH_AES_256_CBC_SHA 0x0037
3617 #define BR_TLS_DHE_DSS_WITH_AES_256_CBC_SHA 0x0038
3618 #define BR_TLS_DHE_RSA_WITH_AES_256_CBC_SHA 0x0039
3619 #define BR_TLS_DH_DSS_WITH_AES_128_CBC_SHA256 0x003E
3620 #define BR_TLS_DH_RSA_WITH_AES_128_CBC_SHA256 0x003F
3621 #define BR_TLS_DHE_DSS_WITH_AES_128_CBC_SHA256 0x0040
3622 #define BR_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 0x0067
3623 #define BR_TLS_DH_DSS_WITH_AES_256_CBC_SHA256 0x0068
3624 #define BR_TLS_DH_RSA_WITH_AES_256_CBC_SHA256 0x0069
3625 #define BR_TLS_DHE_DSS_WITH_AES_256_CBC_SHA256 0x006A
3626 #define BR_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 0x006B
3627 #define BR_TLS_DH_anon_WITH_RC4_128_MD5 0x0018
3628 #define BR_TLS_DH_anon_WITH_3DES_EDE_CBC_SHA 0x001B
3629 #define BR_TLS_DH_anon_WITH_AES_128_CBC_SHA 0x0034
3630 #define BR_TLS_DH_anon_WITH_AES_256_CBC_SHA 0x003A
3631 #define BR_TLS_DH_anon_WITH_AES_128_CBC_SHA256 0x006C
3632 #define BR_TLS_DH_anon_WITH_AES_256_CBC_SHA256 0x006D
3635 #define BR_TLS_ECDH_ECDSA_WITH_NULL_SHA 0xC001
3636 #define BR_TLS_ECDH_ECDSA_WITH_RC4_128_SHA 0xC002
3637 #define BR_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA 0xC003
3638 #define BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA 0xC004
3639 #define BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA 0xC005
3640 #define BR_TLS_ECDHE_ECDSA_WITH_NULL_SHA 0xC006
3641 #define BR_TLS_ECDHE_ECDSA_WITH_RC4_128_SHA 0xC007
3642 #define BR_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA 0xC008
3643 #define BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA 0xC009
3644 #define BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA 0xC00A
3645 #define BR_TLS_ECDH_RSA_WITH_NULL_SHA 0xC00B
3646 #define BR_TLS_ECDH_RSA_WITH_RC4_128_SHA 0xC00C
3647 #define BR_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA 0xC00D
3648 #define BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA 0xC00E
3649 #define BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA 0xC00F
3650 #define BR_TLS_ECDHE_RSA_WITH_NULL_SHA 0xC010
3651 #define BR_TLS_ECDHE_RSA_WITH_RC4_128_SHA 0xC011
3652 #define BR_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA 0xC012
3653 #define BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA 0xC013
3654 #define BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA 0xC014
3655 #define BR_TLS_ECDH_anon_WITH_NULL_SHA 0xC015
3656 #define BR_TLS_ECDH_anon_WITH_RC4_128_SHA 0xC016
3657 #define BR_TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA 0xC017
3658 #define BR_TLS_ECDH_anon_WITH_AES_128_CBC_SHA 0xC018
3659 #define BR_TLS_ECDH_anon_WITH_AES_256_CBC_SHA 0xC019
3662 #define BR_TLS_RSA_WITH_AES_128_GCM_SHA256 0x009C
3663 #define BR_TLS_RSA_WITH_AES_256_GCM_SHA384 0x009D
3664 #define BR_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 0x009E
3665 #define BR_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 0x009F
3666 #define BR_TLS_DH_RSA_WITH_AES_128_GCM_SHA256 0x00A0
3667 #define BR_TLS_DH_RSA_WITH_AES_256_GCM_SHA384 0x00A1
3668 #define BR_TLS_DHE_DSS_WITH_AES_128_GCM_SHA256 0x00A2
3669 #define BR_TLS_DHE_DSS_WITH_AES_256_GCM_SHA384 0x00A3
3670 #define BR_TLS_DH_DSS_WITH_AES_128_GCM_SHA256 0x00A4
3671 #define BR_TLS_DH_DSS_WITH_AES_256_GCM_SHA384 0x00A5
3672 #define BR_TLS_DH_anon_WITH_AES_128_GCM_SHA256 0x00A6
3673 #define BR_TLS_DH_anon_WITH_AES_256_GCM_SHA384 0x00A7
3676 #define BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 0xC023
3677 #define BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 0xC024
3678 #define BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 0xC025
3679 #define BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 0xC026
3680 #define BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 0xC027
3681 #define BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 0xC028
3682 #define BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 0xC029
3683 #define BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 0xC02A
3684 #define BR_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 0xC02B
3685 #define BR_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 0xC02C
3686 #define BR_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 0xC02D
3687 #define BR_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 0xC02E
3688 #define BR_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 0xC02F
3689 #define BR_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 0xC030
3690 #define BR_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 0xC031
3691 #define BR_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 0xC032
3694 #define BR_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 0xCCA8
3695 #define BR_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 0xCCA9
3696 #define BR_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 0xCCAA
3697 #define BR_TLS_PSK_WITH_CHACHA20_POLY1305_SHA256 0xCCAB
3698 #define BR_TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 0xCCAC
3699 #define BR_TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 0xCCAD
3700 #define BR_TLS_RSA_PSK_WITH_CHACHA20_POLY1305_SHA256 0xCCAE
3703 #define BR_TLS_FALLBACK_SCSV 0x5600
3706 * Symbolic constants for alerts.
3708 #define BR_ALERT_CLOSE_NOTIFY 0
3709 #define BR_ALERT_UNEXPECTED_MESSAGE 10
3710 #define BR_ALERT_BAD_RECORD_MAC 20
3711 #define BR_ALERT_RECORD_OVERFLOW 22
3712 #define BR_ALERT_DECOMPRESSION_FAILURE 30
3713 #define BR_ALERT_HANDSHAKE_FAILURE 40
3714 #define BR_ALERT_BAD_CERTIFICATE 42