/*1* Copyright (c) 2016 Thomas Pornin <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining4* a copy of this software and associated documentation files (the5* "Software"), to deal in the Software without restriction, including6* without limitation the rights to use, copy, modify, merge, publish,7* distribute, sublicense, and/or sell copies of the Software, and to8* permit persons to whom the Software is furnished to do so, subject to9* the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*/2324#ifndef BR_BEARSSL_SSL_H__25#define BR_BEARSSL_SSL_H__2627#include <stddef.h>28#include <stdint.h>2930#include "bearssl_block.h"31#include "bearssl_hash.h"32#include "bearssl_hmac.h"33#include "bearssl_prf.h"34#include "bearssl_rand.h"35#include "bearssl_x509.h"3637#ifdef __cplusplus38extern "C" {39#endif4041/** \file bearssl_ssl.h42*43* # SSL44*45* For an overview of the SSL/TLS API, see [the BearSSL Web46* site](https://www.bearssl.org/api1.html).47*48* The `BR_TLS_*` constants correspond to the standard cipher suites and49* their values in the [IANA50* registry](http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-4).51*52* The `BR_ALERT_*` constants are for standard TLS alert messages. When53* a fatal alert message is sent of received, then the SSL engine context54* status is set to the sum of that alert value (an integer in the 0..25555* range) and a fixed offset (`BR_ERR_SEND_FATAL_ALERT` for a sent alert,56* `BR_ERR_RECV_FATAL_ALERT` for a received alert).57*/5859/** \brief Optimal input buffer size. */60#define BR_SSL_BUFSIZE_INPUT (16384 + 325)6162/** \brief Optimal output buffer size. */63#define BR_SSL_BUFSIZE_OUTPUT (16384 + 85)6465/** \brief Optimal buffer size for monodirectional engine66(shared input/output buffer). */67#define BR_SSL_BUFSIZE_MONO BR_SSL_BUFSIZE_INPUT6869/** \brief Optimal buffer size for bidirectional engine70(single buffer split into two separate input/output buffers). */71#define BR_SSL_BUFSIZE_BIDI (BR_SSL_BUFSIZE_INPUT + BR_SSL_BUFSIZE_OUTPUT)7273/*74* Constants for known SSL/TLS protocol versions (SSL 3.0, TLS 1.0, TLS 1.175* and TLS 1.2). Note that though there is a constant for SSL 3.0, that76* protocol version is not actually supported.77*/7879/** \brief Protocol version: SSL 3.0 (unsupported). */80#define BR_SSL30 0x030081/** \brief Protocol version: TLS 1.0. */82#define BR_TLS10 0x030183/** \brief Protocol version: TLS 1.1. */84#define BR_TLS11 0x030285/** \brief Protocol version: TLS 1.2. */86#define BR_TLS12 0x03038788/*89* Error constants. They are used to report the reason why a context has90* been marked as failed.91*92* Implementation note: SSL-level error codes should be in the 1..3193* range. The 32..63 range is for certificate decoding and validation94* errors. Received fatal alerts imply an error code in the 256..511 range.95*/9697/** \brief SSL status: no error so far (0). */98#define BR_ERR_OK 099100/** \brief SSL status: caller-provided parameter is incorrect. */101#define BR_ERR_BAD_PARAM 1102103/** \brief SSL status: operation requested by the caller cannot be applied104with the current context state (e.g. reading data while outgoing data105is waiting to be sent). */106#define BR_ERR_BAD_STATE 2107108/** \brief SSL status: incoming protocol or record version is unsupported. */109#define BR_ERR_UNSUPPORTED_VERSION 3110111/** \brief SSL status: incoming record version does not match the expected112version. */113#define BR_ERR_BAD_VERSION 4114115/** \brief SSL status: incoming record length is invalid. */116#define BR_ERR_BAD_LENGTH 5117118/** \brief SSL status: incoming record is too large to be processed, or119buffer is too small for the handshake message to send. */120#define BR_ERR_TOO_LARGE 6121122/** \brief SSL status: decryption found an invalid padding, or the record123MAC is not correct. */124#define BR_ERR_BAD_MAC 7125126/** \brief SSL status: no initial entropy was provided, and none can be127obtained from the OS. */128#define BR_ERR_NO_RANDOM 8129130/** \brief SSL status: incoming record type is unknown. */131#define BR_ERR_UNKNOWN_TYPE 9132133/** \brief SSL status: incoming record or message has wrong type with134regards to the current engine state. */135#define BR_ERR_UNEXPECTED 10136137/** \brief SSL status: ChangeCipherSpec message from the peer has invalid138contents. */139#define BR_ERR_BAD_CCS 12140141/** \brief SSL status: alert message from the peer has invalid contents142(odd length). */143#define BR_ERR_BAD_ALERT 13144145/** \brief SSL status: incoming handshake message decoding failed. */146#define BR_ERR_BAD_HANDSHAKE 14147148/** \brief SSL status: ServerHello contains a session ID which is larger149than 32 bytes. */150#define BR_ERR_OVERSIZED_ID 15151152/** \brief SSL status: server wants to use a cipher suite that we did153not claim to support. This is also reported if we tried to advertise154a cipher suite that we do not support. */155#define BR_ERR_BAD_CIPHER_SUITE 16156157/** \brief SSL status: server wants to use a compression that we did not158claim to support. */159#define BR_ERR_BAD_COMPRESSION 17160161/** \brief SSL status: server's max fragment length does not match162client's. */163#define BR_ERR_BAD_FRAGLEN 18164165/** \brief SSL status: secure renegotiation failed. */166#define BR_ERR_BAD_SECRENEG 19167168/** \brief SSL status: server sent an extension type that we did not169announce, or used the same extension type several times in a single170ServerHello. */171#define BR_ERR_EXTRA_EXTENSION 20172173/** \brief SSL status: invalid Server Name Indication contents (when174used by the server, this extension shall be empty). */175#define BR_ERR_BAD_SNI 21176177/** \brief SSL status: invalid ServerHelloDone from the server (length178is not 0). */179#define BR_ERR_BAD_HELLO_DONE 22180181/** \brief SSL status: internal limit exceeded (e.g. server's public key182is too large). */183#define BR_ERR_LIMIT_EXCEEDED 23184185/** \brief SSL status: Finished message from peer does not match the186expected value. */187#define BR_ERR_BAD_FINISHED 24188189/** \brief SSL status: session resumption attempt with distinct version190or cipher suite. */191#define BR_ERR_RESUME_MISMATCH 25192193/** \brief SSL status: unsupported or invalid algorithm (ECDHE curve,194signature algorithm, hash function). */195#define BR_ERR_INVALID_ALGORITHM 26196197/** \brief SSL status: invalid signature (on ServerKeyExchange from198server, or in CertificateVerify from client). */199#define BR_ERR_BAD_SIGNATURE 27200201/** \brief SSL status: peer's public key does not have the proper type202or is not allowed for requested operation. */203#define BR_ERR_WRONG_KEY_USAGE 28204205/** \brief SSL status: client did not send a certificate upon request,206or the client certificate could not be validated. */207#define BR_ERR_NO_CLIENT_AUTH 29208209/** \brief SSL status: I/O error or premature close on underlying210transport stream. This error code is set only by the simplified211I/O API ("br_sslio_*"). */212#define BR_ERR_IO 31213214/** \brief SSL status: base value for a received fatal alert.215216When a fatal alert is received from the peer, the alert value217is added to this constant. */218#define BR_ERR_RECV_FATAL_ALERT 256219220/** \brief SSL status: base value for a sent fatal alert.221222When a fatal alert is sent to the peer, the alert value is added223to this constant. */224#define BR_ERR_SEND_FATAL_ALERT 512225226/* ===================================================================== */227228/**229* \brief Decryption engine for SSL.230*231* When processing incoming records, the SSL engine will use a decryption232* engine that uses a specific context structure, and has a set of233* methods (a vtable) that follows this template.234*235* The decryption engine is responsible for applying decryption, verifying236* MAC, and keeping track of the record sequence number.237*/238typedef struct br_sslrec_in_class_ br_sslrec_in_class;239struct br_sslrec_in_class_ {240/**241* \brief Context size (in bytes).242*/243size_t context_size;244245/**246* \brief Test validity of the incoming record length.247*248* This function returns 1 if the announced length for an249* incoming record is valid, 0 otherwise,250*251* \param ctx decryption engine context.252* \param record_len incoming record length.253* \return 1 of a valid length, 0 otherwise.254*/255int (*check_length)(const br_sslrec_in_class *const *ctx,256size_t record_len);257258/**259* \brief Decrypt the incoming record.260*261* This function may assume that the record length is valid262* (it has been previously tested with `check_length()`).263* Decryption is done in place; `*len` is updated with the264* cleartext length, and the address of the first plaintext265* byte is returned. If the record is correct but empty, then266* `*len` is set to 0 and a non-`NULL` pointer is returned.267*268* On decryption/MAC error, `NULL` is returned.269*270* \param ctx decryption engine context.271* \param record_type record type (23 for application data, etc).272* \param version record version.273* \param payload address of encrypted payload.274* \param len pointer to payload length (updated).275* \return pointer to plaintext, or `NULL` on error.276*/277unsigned char *(*decrypt)(const br_sslrec_in_class **ctx,278int record_type, unsigned version,279void *payload, size_t *len);280};281282/**283* \brief Encryption engine for SSL.284*285* When building outgoing records, the SSL engine will use an encryption286* engine that uses a specific context structure, and has a set of287* methods (a vtable) that follows this template.288*289* The encryption engine is responsible for applying encryption and MAC,290* and keeping track of the record sequence number.291*/292typedef struct br_sslrec_out_class_ br_sslrec_out_class;293struct br_sslrec_out_class_ {294/**295* \brief Context size (in bytes).296*/297size_t context_size;298299/**300* \brief Compute maximum plaintext sizes and offsets.301*302* When this function is called, the `*start` and `*end`303* values contain offsets designating the free area in the304* outgoing buffer for plaintext data; that free area is305* preceded by a 5-byte space which will receive the record306* header.307*308* The `max_plaintext()` function is responsible for adjusting309* both `*start` and `*end` to make room for any record-specific310* header, MAC, padding, and possible split.311*312* \param ctx encryption engine context.313* \param start pointer to start of plaintext offset (updated).314* \param end pointer to start of plaintext offset (updated).315*/316void (*max_plaintext)(const br_sslrec_out_class *const *ctx,317size_t *start, size_t *end);318319/**320* \brief Perform record encryption.321*322* This function encrypts the record. The plaintext address and323* length are provided. Returned value is the start of the324* encrypted record (or sequence of records, if a split was325* performed), _including_ the 5-byte header, and `*len` is326* adjusted to the total size of the record(s), there again327* including the header(s).328*329* \param ctx decryption engine context.330* \param record_type record type (23 for application data, etc).331* \param version record version.332* \param plaintext address of plaintext.333* \param len pointer to plaintext length (updated).334* \return pointer to start of built record.335*/336unsigned char *(*encrypt)(const br_sslrec_out_class **ctx,337int record_type, unsigned version,338void *plaintext, size_t *len);339};340341/**342* \brief Context for a no-encryption engine.343*344* The no-encryption engine processes outgoing records during the initial345* handshake, before encryption is applied.346*/347typedef struct {348/** \brief No-encryption engine vtable. */349const br_sslrec_out_class *vtable;350} br_sslrec_out_clear_context;351352/** \brief Static, constant vtable for the no-encryption engine. */353extern const br_sslrec_out_class br_sslrec_out_clear_vtable;354355/* ===================================================================== */356357/**358* \brief Record decryption engine class, for CBC mode.359*360* This class type extends the decryption engine class with an361* initialisation method that receives the parameters needed362* for CBC processing: block cipher implementation, block cipher key,363* HMAC parameters (hash function, key, MAC length), and IV. If the364* IV is `NULL`, then a per-record IV will be used (TLS 1.1+).365*/366typedef struct br_sslrec_in_cbc_class_ br_sslrec_in_cbc_class;367struct br_sslrec_in_cbc_class_ {368/**369* \brief Superclass, as first vtable field.370*/371br_sslrec_in_class inner;372373/**374* \brief Engine initialisation method.375*376* This method sets the vtable field in the context.377*378* \param ctx context to initialise.379* \param bc_impl block cipher implementation (CBC decryption).380* \param bc_key block cipher key.381* \param bc_key_len block cipher key length (in bytes).382* \param dig_impl hash function for HMAC.383* \param mac_key HMAC key.384* \param mac_key_len HMAC key length (in bytes).385* \param mac_out_len HMAC output length (in bytes).386* \param iv initial IV (or `NULL`).387*/388void (*init)(const br_sslrec_in_cbc_class **ctx,389const br_block_cbcdec_class *bc_impl,390const void *bc_key, size_t bc_key_len,391const br_hash_class *dig_impl,392const void *mac_key, size_t mac_key_len, size_t mac_out_len,393const void *iv);394};395396/**397* \brief Record encryption engine class, for CBC mode.398*399* This class type extends the encryption engine class with an400* initialisation method that receives the parameters needed401* for CBC processing: block cipher implementation, block cipher key,402* HMAC parameters (hash function, key, MAC length), and IV. If the403* IV is `NULL`, then a per-record IV will be used (TLS 1.1+).404*/405typedef struct br_sslrec_out_cbc_class_ br_sslrec_out_cbc_class;406struct br_sslrec_out_cbc_class_ {407/**408* \brief Superclass, as first vtable field.409*/410br_sslrec_out_class inner;411412/**413* \brief Engine initialisation method.414*415* This method sets the vtable field in the context.416*417* \param ctx context to initialise.418* \param bc_impl block cipher implementation (CBC encryption).419* \param bc_key block cipher key.420* \param bc_key_len block cipher key length (in bytes).421* \param dig_impl hash function for HMAC.422* \param mac_key HMAC key.423* \param mac_key_len HMAC key length (in bytes).424* \param mac_out_len HMAC output length (in bytes).425* \param iv initial IV (or `NULL`).426*/427void (*init)(const br_sslrec_out_cbc_class **ctx,428const br_block_cbcenc_class *bc_impl,429const void *bc_key, size_t bc_key_len,430const br_hash_class *dig_impl,431const void *mac_key, size_t mac_key_len, size_t mac_out_len,432const void *iv);433};434435/**436* \brief Context structure for decrypting incoming records with437* CBC + HMAC.438*439* The first field points to the vtable. The other fields are opaque440* and shall not be accessed directly.441*/442typedef struct {443/** \brief Pointer to vtable. */444const br_sslrec_in_cbc_class *vtable;445#ifndef BR_DOXYGEN_IGNORE446uint64_t seq;447union {448const br_block_cbcdec_class *vtable;449br_aes_gen_cbcdec_keys aes;450br_des_gen_cbcdec_keys des;451} bc;452br_hmac_key_context mac;453size_t mac_len;454unsigned char iv[16];455int explicit_IV;456#endif457} br_sslrec_in_cbc_context;458459/**460* \brief Static, constant vtable for record decryption with CBC.461*/462extern const br_sslrec_in_cbc_class br_sslrec_in_cbc_vtable;463464/**465* \brief Context structure for encrypting outgoing records with466* CBC + HMAC.467*468* The first field points to the vtable. The other fields are opaque469* and shall not be accessed directly.470*/471typedef struct {472/** \brief Pointer to vtable. */473const br_sslrec_out_cbc_class *vtable;474#ifndef BR_DOXYGEN_IGNORE475uint64_t seq;476union {477const br_block_cbcenc_class *vtable;478br_aes_gen_cbcenc_keys aes;479br_des_gen_cbcenc_keys des;480} bc;481br_hmac_key_context mac;482size_t mac_len;483unsigned char iv[16];484int explicit_IV;485#endif486} br_sslrec_out_cbc_context;487488/**489* \brief Static, constant vtable for record encryption with CBC.490*/491extern const br_sslrec_out_cbc_class br_sslrec_out_cbc_vtable;492493/* ===================================================================== */494495/**496* \brief Record decryption engine class, for GCM mode.497*498* This class type extends the decryption engine class with an499* initialisation method that receives the parameters needed500* for GCM processing: block cipher implementation, block cipher key,501* GHASH implementation, and 4-byte IV.502*/503typedef struct br_sslrec_in_gcm_class_ br_sslrec_in_gcm_class;504struct br_sslrec_in_gcm_class_ {505/**506* \brief Superclass, as first vtable field.507*/508br_sslrec_in_class inner;509510/**511* \brief Engine initialisation method.512*513* This method sets the vtable field in the context.514*515* \param ctx context to initialise.516* \param bc_impl block cipher implementation (CTR).517* \param key block cipher key.518* \param key_len block cipher key length (in bytes).519* \param gh_impl GHASH implementation.520* \param iv static IV (4 bytes).521*/522void (*init)(const br_sslrec_in_gcm_class **ctx,523const br_block_ctr_class *bc_impl,524const void *key, size_t key_len,525br_ghash gh_impl,526const void *iv);527};528529/**530* \brief Record encryption engine class, for GCM mode.531*532* This class type extends the encryption engine class with an533* initialisation method that receives the parameters needed534* for GCM processing: block cipher implementation, block cipher key,535* GHASH implementation, and 4-byte IV.536*/537typedef struct br_sslrec_out_gcm_class_ br_sslrec_out_gcm_class;538struct br_sslrec_out_gcm_class_ {539/**540* \brief Superclass, as first vtable field.541*/542br_sslrec_out_class inner;543544/**545* \brief Engine initialisation method.546*547* This method sets the vtable field in the context.548*549* \param ctx context to initialise.550* \param bc_impl block cipher implementation (CTR).551* \param key block cipher key.552* \param key_len block cipher key length (in bytes).553* \param gh_impl GHASH implementation.554* \param iv static IV (4 bytes).555*/556void (*init)(const br_sslrec_out_gcm_class **ctx,557const br_block_ctr_class *bc_impl,558const void *key, size_t key_len,559br_ghash gh_impl,560const void *iv);561};562563/**564* \brief Context structure for processing records with GCM.565*566* The same context structure is used for encrypting and decrypting.567*568* The first field points to the vtable. The other fields are opaque569* and shall not be accessed directly.570*/571typedef struct {572/** \brief Pointer to vtable. */573union {574const void *gen;575const br_sslrec_in_gcm_class *in;576const br_sslrec_out_gcm_class *out;577} vtable;578#ifndef BR_DOXYGEN_IGNORE579uint64_t seq;580union {581const br_block_ctr_class *vtable;582br_aes_gen_ctr_keys aes;583} bc;584br_ghash gh;585unsigned char iv[4];586unsigned char h[16];587#endif588} br_sslrec_gcm_context;589590/**591* \brief Static, constant vtable for record decryption with GCM.592*/593extern const br_sslrec_in_gcm_class br_sslrec_in_gcm_vtable;594595/**596* \brief Static, constant vtable for record encryption with GCM.597*/598extern const br_sslrec_out_gcm_class br_sslrec_out_gcm_vtable;599600/* ===================================================================== */601602/**603* \brief Record decryption engine class, for ChaCha20+Poly1305.604*605* This class type extends the decryption engine class with an606* initialisation method that receives the parameters needed607* for ChaCha20+Poly1305 processing: ChaCha20 implementation,608* Poly1305 implementation, key, and 12-byte IV.609*/610typedef struct br_sslrec_in_chapol_class_ br_sslrec_in_chapol_class;611struct br_sslrec_in_chapol_class_ {612/**613* \brief Superclass, as first vtable field.614*/615br_sslrec_in_class inner;616617/**618* \brief Engine initialisation method.619*620* This method sets the vtable field in the context.621*622* \param ctx context to initialise.623* \param ichacha ChaCha20 implementation.624* \param ipoly Poly1305 implementation.625* \param key secret key (32 bytes).626* \param iv static IV (12 bytes).627*/628void (*init)(const br_sslrec_in_chapol_class **ctx,629br_chacha20_run ichacha,630br_poly1305_run ipoly,631const void *key, const void *iv);632};633634/**635* \brief Record encryption engine class, for ChaCha20+Poly1305.636*637* This class type extends the encryption engine class with an638* initialisation method that receives the parameters needed639* for ChaCha20+Poly1305 processing: ChaCha20 implementation,640* Poly1305 implementation, key, and 12-byte IV.641*/642typedef struct br_sslrec_out_chapol_class_ br_sslrec_out_chapol_class;643struct br_sslrec_out_chapol_class_ {644/**645* \brief Superclass, as first vtable field.646*/647br_sslrec_out_class inner;648649/**650* \brief Engine initialisation method.651*652* This method sets the vtable field in the context.653*654* \param ctx context to initialise.655* \param ichacha ChaCha20 implementation.656* \param ipoly Poly1305 implementation.657* \param key secret key (32 bytes).658* \param iv static IV (12 bytes).659*/660void (*init)(const br_sslrec_out_chapol_class **ctx,661br_chacha20_run ichacha,662br_poly1305_run ipoly,663const void *key, const void *iv);664};665666/**667* \brief Context structure for processing records with ChaCha20+Poly1305.668*669* The same context structure is used for encrypting and decrypting.670*671* The first field points to the vtable. The other fields are opaque672* and shall not be accessed directly.673*/674typedef struct {675/** \brief Pointer to vtable. */676union {677const void *gen;678const br_sslrec_in_chapol_class *in;679const br_sslrec_out_chapol_class *out;680} vtable;681#ifndef BR_DOXYGEN_IGNORE682uint64_t seq;683unsigned char key[32];684unsigned char iv[12];685br_chacha20_run ichacha;686br_poly1305_run ipoly;687#endif688} br_sslrec_chapol_context;689690/**691* \brief Static, constant vtable for record decryption with ChaCha20+Poly1305.692*/693extern const br_sslrec_in_chapol_class br_sslrec_in_chapol_vtable;694695/**696* \brief Static, constant vtable for record encryption with ChaCha20+Poly1305.697*/698extern const br_sslrec_out_chapol_class br_sslrec_out_chapol_vtable;699700/* ===================================================================== */701702/**703* \brief Record decryption engine class, for CCM mode.704*705* This class type extends the decryption engine class with an706* initialisation method that receives the parameters needed707* for CCM processing: block cipher implementation, block cipher key,708* and 4-byte IV.709*/710typedef struct br_sslrec_in_ccm_class_ br_sslrec_in_ccm_class;711struct br_sslrec_in_ccm_class_ {712/**713* \brief Superclass, as first vtable field.714*/715br_sslrec_in_class inner;716717/**718* \brief Engine initialisation method.719*720* This method sets the vtable field in the context.721*722* \param ctx context to initialise.723* \param bc_impl block cipher implementation (CTR+CBC).724* \param key block cipher key.725* \param key_len block cipher key length (in bytes).726* \param iv static IV (4 bytes).727* \param tag_len tag length (in bytes)728*/729void (*init)(const br_sslrec_in_ccm_class **ctx,730const br_block_ctrcbc_class *bc_impl,731const void *key, size_t key_len,732const void *iv, size_t tag_len);733};734735/**736* \brief Record encryption engine class, for CCM mode.737*738* This class type extends the encryption engine class with an739* initialisation method that receives the parameters needed740* for CCM processing: block cipher implementation, block cipher key,741* and 4-byte IV.742*/743typedef struct br_sslrec_out_ccm_class_ br_sslrec_out_ccm_class;744struct br_sslrec_out_ccm_class_ {745/**746* \brief Superclass, as first vtable field.747*/748br_sslrec_out_class inner;749750/**751* \brief Engine initialisation method.752*753* This method sets the vtable field in the context.754*755* \param ctx context to initialise.756* \param bc_impl block cipher implementation (CTR+CBC).757* \param key block cipher key.758* \param key_len block cipher key length (in bytes).759* \param iv static IV (4 bytes).760* \param tag_len tag length (in bytes)761*/762void (*init)(const br_sslrec_out_ccm_class **ctx,763const br_block_ctrcbc_class *bc_impl,764const void *key, size_t key_len,765const void *iv, size_t tag_len);766};767768/**769* \brief Context structure for processing records with CCM.770*771* The same context structure is used for encrypting and decrypting.772*773* The first field points to the vtable. The other fields are opaque774* and shall not be accessed directly.775*/776typedef struct {777/** \brief Pointer to vtable. */778union {779const void *gen;780const br_sslrec_in_ccm_class *in;781const br_sslrec_out_ccm_class *out;782} vtable;783#ifndef BR_DOXYGEN_IGNORE784uint64_t seq;785union {786const br_block_ctrcbc_class *vtable;787br_aes_gen_ctrcbc_keys aes;788} bc;789unsigned char iv[4];790size_t tag_len;791#endif792} br_sslrec_ccm_context;793794/**795* \brief Static, constant vtable for record decryption with CCM.796*/797extern const br_sslrec_in_ccm_class br_sslrec_in_ccm_vtable;798799/**800* \brief Static, constant vtable for record encryption with CCM.801*/802extern const br_sslrec_out_ccm_class br_sslrec_out_ccm_vtable;803804/* ===================================================================== */805806/**807* \brief Type for session parameters, to be saved for session resumption.808*/809typedef struct {810/** \brief Session ID buffer. */811unsigned char session_id[32];812/** \brief Session ID length (in bytes, at most 32). */813unsigned char session_id_len;814/** \brief Protocol version. */815uint16_t version;816/** \brief Cipher suite. */817uint16_t cipher_suite;818/** \brief Master secret. */819unsigned char master_secret[48];820} br_ssl_session_parameters;821822#ifndef BR_DOXYGEN_IGNORE823/*824* Maximum number of cipher suites supported by a client or server.825*/826#define BR_MAX_CIPHER_SUITES 48827#endif828829/**830* \brief Context structure for SSL engine.831*832* This strucuture is common to the client and server; both the client833* context (`br_ssl_client_context`) and the server context834* (`br_ssl_server_context`) include a `br_ssl_engine_context` as their835* first field.836*837* The engine context manages records, including alerts, closures, and838* transitions to new encryption/MAC algorithms. Processing of handshake839* records is delegated to externally provided code. This structure840* should not be used directly.841*842* Structure contents are opaque and shall not be accessed directly.843*/844typedef struct {845#ifndef BR_DOXYGEN_IGNORE846/*847* The error code. When non-zero, then the state is "failed" and848* no I/O may occur until reset.849*/850int err;851852/*853* Configured I/O buffers. They are either disjoint, or identical.854*/855unsigned char *ibuf, *obuf;856size_t ibuf_len, obuf_len;857858/*859* Maximum fragment length applies to outgoing records; incoming860* records can be processed as long as they fit in the input861* buffer. It is guaranteed that incoming records at least as big862* as max_frag_len can be processed.863*/864uint16_t max_frag_len;865unsigned char log_max_frag_len;866unsigned char peer_log_max_frag_len;867868/*869* Buffering management registers.870*/871size_t ixa, ixb, ixc;872size_t oxa, oxb, oxc;873unsigned char iomode;874unsigned char incrypt;875876/*877* Shutdown flag: when set to non-zero, incoming record bytes878* will not be accepted anymore. This is used after a close_notify879* has been received: afterwards, the engine no longer claims that880* it could receive bytes from the transport medium.881*/882unsigned char shutdown_recv;883884/*885* 'record_type_in' is set to the incoming record type when the886* record header has been received.887* 'record_type_out' is used to make the next outgoing record888* header when it is ready to go.889*/890unsigned char record_type_in, record_type_out;891892/*893* When a record is received, its version is extracted:894* -- if 'version_in' is 0, then it is set to the received version;895* -- otherwise, if the received version is not identical to896* the 'version_in' contents, then a failure is reported.897*898* This implements the SSL requirement that all records shall899* use the negotiated protocol version, once decided (in the900* ServerHello). It is up to the handshake handler to adjust this901* field when necessary.902*/903uint16_t version_in;904905/*906* 'version_out' is used when the next outgoing record is ready907* to go.908*/909uint16_t version_out;910911/*912* Record handler contexts.913*/914union {915const br_sslrec_in_class *vtable;916br_sslrec_in_cbc_context cbc;917br_sslrec_gcm_context gcm;918br_sslrec_chapol_context chapol;919br_sslrec_ccm_context ccm;920} in;921union {922const br_sslrec_out_class *vtable;923br_sslrec_out_clear_context clear;924br_sslrec_out_cbc_context cbc;925br_sslrec_gcm_context gcm;926br_sslrec_chapol_context chapol;927br_sslrec_ccm_context ccm;928} out;929930/*931* The "application data" flag. Value:932* 0 handshake is in process, no application data acceptable933* 1 application data can be sent and received934* 2 closing, no application data can be sent, but some935* can still be received (and discarded)936*/937unsigned char application_data;938939/*940* Context RNG.941*942* rng_init_done is initially 0. It is set to 1 when the943* basic structure of the RNG is set, and 2 when some944* entropy has been pushed in. The value 2 marks the RNG945* as "properly seeded".946*947* rng_os_rand_done is initially 0. It is set to 1 when948* some seeding from the OS or hardware has been attempted.949*/950br_hmac_drbg_context rng;951int rng_init_done;952int rng_os_rand_done;953954/*955* Supported minimum and maximum versions, and cipher suites.956*/957uint16_t version_min;958uint16_t version_max;959uint16_t suites_buf[BR_MAX_CIPHER_SUITES];960unsigned char suites_num;961962/*963* For clients, the server name to send as a SNI extension. For964* servers, the name received in the SNI extension (if any).965*/966char server_name[256];967968/*969* "Security parameters". These are filled by the handshake970* handler, and used when switching encryption state.971*/972unsigned char client_random[32];973unsigned char server_random[32];974br_ssl_session_parameters session;975976/*977* ECDHE elements: curve and point from the peer. The server also978* uses that buffer for the point to send to the client.979*/980unsigned char ecdhe_curve;981unsigned char ecdhe_point[133];982unsigned char ecdhe_point_len;983984/*985* Secure renegotiation (RFC 5746): 'reneg' can be:986* 0 first handshake (server support is not known)987* 1 peer does not support secure renegotiation988* 2 peer supports secure renegotiation989*990* The saved_finished buffer contains the client and the991* server "Finished" values from the last handshake, in992* that order (12 bytes each).993*/994unsigned char reneg;995unsigned char saved_finished[24];996997/*998* Behavioural flags.999*/1000uint32_t flags;10011002/*1003* Context variables for the handshake processor. The 'pad' must1004* be large enough to accommodate an RSA-encrypted pre-master1005* secret, or an RSA signature; since we want to support up to1006* RSA-4096, this means at least 512 bytes. (Other pad usages1007* require its length to be at least 256.)1008*/1009struct {1010uint32_t *dp;1011uint32_t *rp;1012const unsigned char *ip;1013} cpu;1014uint32_t dp_stack[32];1015uint32_t rp_stack[32];1016unsigned char pad[512];1017unsigned char *hbuf_in, *hbuf_out, *saved_hbuf_out;1018size_t hlen_in, hlen_out;1019void (*hsrun)(void *ctx);10201021/*1022* The 'action' value communicates OOB information between the1023* engine and the handshake processor.1024*1025* From the engine:1026* 0 invocation triggered by I/O1027* 1 invocation triggered by explicit close1028* 2 invocation triggered by explicit renegotiation1029*/1030unsigned char action;10311032/*1033* State for alert messages. Value is either 0, or the value of1034* the alert level byte (level is either 1 for warning, or 2 for1035* fatal; we convert all other values to 'fatal').1036*/1037unsigned char alert;10381039/*1040* Closure flags. This flag is set when a close_notify has been1041* received from the peer.1042*/1043unsigned char close_received;10441045/*1046* Multi-hasher for the handshake messages. The handshake handler1047* is responsible for resetting it when appropriate.1048*/1049br_multihash_context mhash;10501051/*1052* Pointer to the X.509 engine. The engine is supposed to be1053* already initialized. It is used to validate the peer's1054* certificate.1055*/1056const br_x509_class **x509ctx;10571058/*1059* Certificate chain to send. This is used by both client and1060* server, when they send their respective Certificate messages.1061* If chain_len is 0, then chain may be NULL.1062*/1063const br_x509_certificate *chain;1064size_t chain_len;1065const unsigned char *cert_cur;1066size_t cert_len;10671068/*1069* List of supported protocol names (ALPN extension). If unset,1070* (number of names is 0), then:1071* - the client sends no ALPN extension;1072* - the server ignores any incoming ALPN extension.1073*1074* Otherwise:1075* - the client sends an ALPN extension with all the names;1076* - the server selects the first protocol in its list that1077* the client also supports, or fails (fatal alert 120)1078* if the client sends an ALPN extension and there is no1079* match.1080*1081* The 'selected_protocol' field contains 1+n if the matching1082* name has index n in the list (the value is 0 if no match was1083* performed, e.g. the peer did not send an ALPN extension).1084*/1085const char **protocol_names;1086uint16_t protocol_names_num;1087uint16_t selected_protocol;10881089/*1090* Pointers to implementations; left to NULL for unsupported1091* functions. For the raw hash functions, implementations are1092* referenced from the multihasher (mhash field).1093*/1094br_tls_prf_impl prf10;1095br_tls_prf_impl prf_sha256;1096br_tls_prf_impl prf_sha384;1097const br_block_cbcenc_class *iaes_cbcenc;1098const br_block_cbcdec_class *iaes_cbcdec;1099const br_block_ctr_class *iaes_ctr;1100const br_block_ctrcbc_class *iaes_ctrcbc;1101const br_block_cbcenc_class *ides_cbcenc;1102const br_block_cbcdec_class *ides_cbcdec;1103br_ghash ighash;1104br_chacha20_run ichacha;1105br_poly1305_run ipoly;1106const br_sslrec_in_cbc_class *icbc_in;1107const br_sslrec_out_cbc_class *icbc_out;1108const br_sslrec_in_gcm_class *igcm_in;1109const br_sslrec_out_gcm_class *igcm_out;1110const br_sslrec_in_chapol_class *ichapol_in;1111const br_sslrec_out_chapol_class *ichapol_out;1112const br_sslrec_in_ccm_class *iccm_in;1113const br_sslrec_out_ccm_class *iccm_out;1114const br_ec_impl *iec;1115br_rsa_pkcs1_vrfy irsavrfy;1116br_ecdsa_vrfy iecdsa;1117#endif1118} br_ssl_engine_context;11191120/**1121* \brief Get currently defined engine behavioural flags.1122*1123* \param cc SSL engine context.1124* \return the flags.1125*/1126static inline uint32_t1127br_ssl_engine_get_flags(br_ssl_engine_context *cc)1128{1129return cc->flags;1130}11311132/**1133* \brief Set all engine behavioural flags.1134*1135* \param cc SSL engine context.1136* \param flags new value for all flags.1137*/1138static inline void1139br_ssl_engine_set_all_flags(br_ssl_engine_context *cc, uint32_t flags)1140{1141cc->flags = flags;1142}11431144/**1145* \brief Set some engine behavioural flags.1146*1147* The flags set in the `flags` parameter are set in the context; other1148* flags are untouched.1149*1150* \param cc SSL engine context.1151* \param flags additional set flags.1152*/1153static inline void1154br_ssl_engine_add_flags(br_ssl_engine_context *cc, uint32_t flags)1155{1156cc->flags |= flags;1157}11581159/**1160* \brief Clear some engine behavioural flags.1161*1162* The flags set in the `flags` parameter are cleared from the context; other1163* flags are untouched.1164*1165* \param cc SSL engine context.1166* \param flags flags to remove.1167*/1168static inline void1169br_ssl_engine_remove_flags(br_ssl_engine_context *cc, uint32_t flags)1170{1171cc->flags &= ~flags;1172}11731174/**1175* \brief Behavioural flag: enforce server preferences.1176*1177* If this flag is set, then the server will enforce its own cipher suite1178* preference order; otherwise, it follows the client preferences.1179*/1180#define BR_OPT_ENFORCE_SERVER_PREFERENCES ((uint32_t)1 << 0)11811182/**1183* \brief Behavioural flag: disable renegotiation.1184*1185* If this flag is set, then renegotiations are rejected unconditionally:1186* they won't be honoured if asked for programmatically, and requests from1187* the peer are rejected.1188*/1189#define BR_OPT_NO_RENEGOTIATION ((uint32_t)1 << 1)11901191/**1192* \brief Behavioural flag: tolerate lack of client authentication.1193*1194* If this flag is set in a server and the server requests a client1195* certificate, but the authentication fails (the client does not send1196* a certificate, or the client's certificate chain cannot be validated),1197* then the connection keeps on. Without this flag, a failed client1198* authentication terminates the connection.1199*1200* Notes:1201*1202* - If the client's certificate can be validated and its public key is1203* supported, then a wrong signature value terminates the connection1204* regardless of that flag.1205*1206* - If using full-static ECDH, then a failure to validate the client's1207* certificate prevents the handshake from succeeding.1208*/1209#define BR_OPT_TOLERATE_NO_CLIENT_AUTH ((uint32_t)1 << 2)12101211/**1212* \brief Behavioural flag: fail on application protocol mismatch.1213*1214* The ALPN extension ([RFC 7301](https://tools.ietf.org/html/rfc7301))1215* allows the client to send a list of application protocol names, and1216* the server to select one. A mismatch is one of the following occurrences:1217*1218* - On the client: the client sends a list of names, the server1219* responds with a protocol name which is _not_ part of the list of1220* names sent by the client.1221*1222* - On the server: the client sends a list of names, and the server1223* is also configured with a list of names, but there is no common1224* protocol name between the two lists.1225*1226* Normal behaviour in case of mismatch is to report no matching name1227* (`br_ssl_engine_get_selected_protocol()` returns `NULL`) and carry on.1228* If the flag is set, then a mismatch implies a protocol failure (if1229* the mismatch is detected by the server, it will send a fatal alert).1230*1231* Note: even with this flag, `br_ssl_engine_get_selected_protocol()`1232* may still return `NULL` if the client or the server does not send an1233* ALPN extension at all.1234*/1235#define BR_OPT_FAIL_ON_ALPN_MISMATCH ((uint32_t)1 << 3)12361237/**1238* \brief Set the minimum and maximum supported protocol versions.1239*1240* The two provided versions MUST be supported by the implementation1241* (i.e. TLS 1.0, 1.1 and 1.2), and `version_max` MUST NOT be lower1242* than `version_min`.1243*1244* \param cc SSL engine context.1245* \param version_min minimum supported TLS version.1246* \param version_max maximum supported TLS version.1247*/1248static inline void1249br_ssl_engine_set_versions(br_ssl_engine_context *cc,1250unsigned version_min, unsigned version_max)1251{1252cc->version_min = (uint16_t)version_min;1253cc->version_max = (uint16_t)version_max;1254}12551256/**1257* \brief Set the list of cipher suites advertised by this context.1258*1259* The provided array is copied into the context. It is the caller1260* responsibility to ensure that all provided suites will be supported1261* by the context. The engine context has enough room to receive _all_1262* suites supported by the implementation. The provided array MUST NOT1263* contain duplicates.1264*1265* If the engine is for a client, the "signaling" pseudo-cipher suite1266* `TLS_FALLBACK_SCSV` can be added at the end of the list, if the1267* calling application is performing a voluntary downgrade (voluntary1268* downgrades are not recommended, but if such a downgrade is done, then1269* adding the fallback pseudo-suite is a good idea).1270*1271* \param cc SSL engine context.1272* \param suites cipher suites.1273* \param suites_num number of cipher suites.1274*/1275void br_ssl_engine_set_suites(br_ssl_engine_context *cc,1276const uint16_t *suites, size_t suites_num);12771278/**1279* \brief Set the X.509 engine.1280*1281* The caller shall ensure that the X.509 engine is properly initialised.1282*1283* \param cc SSL engine context.1284* \param x509ctx X.509 certificate validation context.1285*/1286static inline void1287br_ssl_engine_set_x509(br_ssl_engine_context *cc, const br_x509_class **x509ctx)1288{1289cc->x509ctx = x509ctx;1290}12911292/**1293* \brief Set the supported protocol names.1294*1295* Protocol names are part of the ALPN extension ([RFC1296* 7301](https://tools.ietf.org/html/rfc7301)). Each protocol name is a1297* character string, containing no more than 255 characters (256 with the1298* terminating zero). When names are set, then:1299*1300* - The client will send an ALPN extension, containing the names. If1301* the server responds with an ALPN extension, the client will verify1302* that the response contains one of its name, and report that name1303* through `br_ssl_engine_get_selected_protocol()`.1304*1305* - The server will parse incoming ALPN extension (from clients), and1306* try to find a common protocol; if none is found, the connection1307* is aborted with a fatal alert. On match, a response ALPN extension1308* is sent, and name is reported through1309* `br_ssl_engine_get_selected_protocol()`.1310*1311* The provided array is linked in, and must remain valid while the1312* connection is live.1313*1314* Names MUST NOT be empty. Names MUST NOT be longer than 255 characters1315* (excluding the terminating 0).1316*1317* \param ctx SSL engine context.1318* \param names list of protocol names (zero-terminated).1319* \param num number of protocol names (MUST be 1 or more).1320*/1321static inline void1322br_ssl_engine_set_protocol_names(br_ssl_engine_context *ctx,1323const char **names, size_t num)1324{1325ctx->protocol_names = names;1326ctx->protocol_names_num = (uint16_t)num;1327}13281329/**1330* \brief Get the selected protocol.1331*1332* If this context was initialised with a non-empty list of protocol1333* names, and both client and server sent ALPN extensions during the1334* handshake, and a common name was found, then that name is returned.1335* Otherwise, `NULL` is returned.1336*1337* The returned pointer is one of the pointers provided to the context1338* with `br_ssl_engine_set_protocol_names()`.1339*1340* \return the selected protocol, or `NULL`.1341*/1342static inline const char *1343br_ssl_engine_get_selected_protocol(br_ssl_engine_context *ctx)1344{1345unsigned k;13461347k = ctx->selected_protocol;1348return (k == 0 || k == 0xFFFF) ? NULL : ctx->protocol_names[k - 1];1349}13501351/**1352* \brief Set a hash function implementation (by ID).1353*1354* Hash functions set with this call will be used for SSL/TLS specific1355* usages, not X.509 certificate validation. Only "standard" hash functions1356* may be set (MD5, SHA-1, SHA-224, SHA-256, SHA-384, SHA-512). If `impl`1357* is `NULL`, then the hash function support is removed, not added.1358*1359* \param ctx SSL engine context.1360* \param id hash function identifier.1361* \param impl hash function implementation (or `NULL`).1362*/1363static inline void1364br_ssl_engine_set_hash(br_ssl_engine_context *ctx,1365int id, const br_hash_class *impl)1366{1367br_multihash_setimpl(&ctx->mhash, id, impl);1368}13691370/**1371* \brief Get a hash function implementation (by ID).1372*1373* This function retrieves a hash function implementation which was1374* set with `br_ssl_engine_set_hash()`.1375*1376* \param ctx SSL engine context.1377* \param id hash function identifier.1378* \return the hash function implementation (or `NULL`).1379*/1380static inline const br_hash_class *1381br_ssl_engine_get_hash(br_ssl_engine_context *ctx, int id)1382{1383return br_multihash_getimpl(&ctx->mhash, id);1384}13851386/**1387* \brief Set the PRF implementation (for TLS 1.0 and 1.1).1388*1389* This function sets (or removes, if `impl` is `NULL`) the implementation1390* for the PRF used in TLS 1.0 and 1.1.1391*1392* \param cc SSL engine context.1393* \param impl PRF implementation (or `NULL`).1394*/1395static inline void1396br_ssl_engine_set_prf10(br_ssl_engine_context *cc, br_tls_prf_impl impl)1397{1398cc->prf10 = impl;1399}14001401/**1402* \brief Set the PRF implementation with SHA-256 (for TLS 1.2).1403*1404* This function sets (or removes, if `impl` is `NULL`) the implementation1405* for the SHA-256 variant of the PRF used in TLS 1.2.1406*1407* \param cc SSL engine context.1408* \param impl PRF implementation (or `NULL`).1409*/1410static inline void1411br_ssl_engine_set_prf_sha256(br_ssl_engine_context *cc, br_tls_prf_impl impl)1412{1413cc->prf_sha256 = impl;1414}14151416/**1417* \brief Set the PRF implementation with SHA-384 (for TLS 1.2).1418*1419* This function sets (or removes, if `impl` is `NULL`) the implementation1420* for the SHA-384 variant of the PRF used in TLS 1.2.1421*1422* \param cc SSL engine context.1423* \param impl PRF implementation (or `NULL`).1424*/1425static inline void1426br_ssl_engine_set_prf_sha384(br_ssl_engine_context *cc, br_tls_prf_impl impl)1427{1428cc->prf_sha384 = impl;1429}14301431/**1432* \brief Set the AES/CBC implementations.1433*1434* \param cc SSL engine context.1435* \param impl_enc AES/CBC encryption implementation (or `NULL`).1436* \param impl_dec AES/CBC decryption implementation (or `NULL`).1437*/1438static inline void1439br_ssl_engine_set_aes_cbc(br_ssl_engine_context *cc,1440const br_block_cbcenc_class *impl_enc,1441const br_block_cbcdec_class *impl_dec)1442{1443cc->iaes_cbcenc = impl_enc;1444cc->iaes_cbcdec = impl_dec;1445}14461447/**1448* \brief Set the "default" AES/CBC implementations.1449*1450* This function configures in the engine the AES implementations that1451* should provide best runtime performance on the local system, while1452* still being safe (in particular, constant-time). It also sets the1453* handlers for CBC records.1454*1455* \param cc SSL engine context.1456*/1457void br_ssl_engine_set_default_aes_cbc(br_ssl_engine_context *cc);14581459/**1460* \brief Set the AES/CTR implementation.1461*1462* \param cc SSL engine context.1463* \param impl AES/CTR encryption/decryption implementation (or `NULL`).1464*/1465static inline void1466br_ssl_engine_set_aes_ctr(br_ssl_engine_context *cc,1467const br_block_ctr_class *impl)1468{1469cc->iaes_ctr = impl;1470}14711472/**1473* \brief Set the "default" implementations for AES/GCM (AES/CTR + GHASH).1474*1475* This function configures in the engine the AES/CTR and GHASH1476* implementation that should provide best runtime performance on the local1477* system, while still being safe (in particular, constant-time). It also1478* sets the handlers for GCM records.1479*1480* \param cc SSL engine context.1481*/1482void br_ssl_engine_set_default_aes_gcm(br_ssl_engine_context *cc);14831484/**1485* \brief Set the DES/CBC implementations.1486*1487* \param cc SSL engine context.1488* \param impl_enc DES/CBC encryption implementation (or `NULL`).1489* \param impl_dec DES/CBC decryption implementation (or `NULL`).1490*/1491static inline void1492br_ssl_engine_set_des_cbc(br_ssl_engine_context *cc,1493const br_block_cbcenc_class *impl_enc,1494const br_block_cbcdec_class *impl_dec)1495{1496cc->ides_cbcenc = impl_enc;1497cc->ides_cbcdec = impl_dec;1498}14991500/**1501* \brief Set the "default" DES/CBC implementations.1502*1503* This function configures in the engine the DES implementations that1504* should provide best runtime performance on the local system, while1505* still being safe (in particular, constant-time). It also sets the1506* handlers for CBC records.1507*1508* \param cc SSL engine context.1509*/1510void br_ssl_engine_set_default_des_cbc(br_ssl_engine_context *cc);15111512/**1513* \brief Set the GHASH implementation (used in GCM mode).1514*1515* \param cc SSL engine context.1516* \param impl GHASH implementation (or `NULL`).1517*/1518static inline void1519br_ssl_engine_set_ghash(br_ssl_engine_context *cc, br_ghash impl)1520{1521cc->ighash = impl;1522}15231524/**1525* \brief Set the ChaCha20 implementation.1526*1527* \param cc SSL engine context.1528* \param ichacha ChaCha20 implementation (or `NULL`).1529*/1530static inline void1531br_ssl_engine_set_chacha20(br_ssl_engine_context *cc,1532br_chacha20_run ichacha)1533{1534cc->ichacha = ichacha;1535}15361537/**1538* \brief Set the Poly1305 implementation.1539*1540* \param cc SSL engine context.1541* \param ipoly Poly1305 implementation (or `NULL`).1542*/1543static inline void1544br_ssl_engine_set_poly1305(br_ssl_engine_context *cc,1545br_poly1305_run ipoly)1546{1547cc->ipoly = ipoly;1548}15491550/**1551* \brief Set the "default" ChaCha20 and Poly1305 implementations.1552*1553* This function configures in the engine the ChaCha20 and Poly13051554* implementations that should provide best runtime performance on the1555* local system, while still being safe (in particular, constant-time).1556* It also sets the handlers for ChaCha20+Poly1305 records.1557*1558* \param cc SSL engine context.1559*/1560void br_ssl_engine_set_default_chapol(br_ssl_engine_context *cc);15611562/**1563* \brief Set the AES/CTR+CBC implementation.1564*1565* \param cc SSL engine context.1566* \param impl AES/CTR+CBC encryption/decryption implementation (or `NULL`).1567*/1568static inline void1569br_ssl_engine_set_aes_ctrcbc(br_ssl_engine_context *cc,1570const br_block_ctrcbc_class *impl)1571{1572cc->iaes_ctrcbc = impl;1573}15741575/**1576* \brief Set the "default" implementations for AES/CCM.1577*1578* This function configures in the engine the AES/CTR+CBC1579* implementation that should provide best runtime performance on the local1580* system, while still being safe (in particular, constant-time). It also1581* sets the handlers for CCM records.1582*1583* \param cc SSL engine context.1584*/1585void br_ssl_engine_set_default_aes_ccm(br_ssl_engine_context *cc);15861587/**1588* \brief Set the record encryption and decryption engines for CBC + HMAC.1589*1590* \param cc SSL engine context.1591* \param impl_in record CBC decryption implementation (or `NULL`).1592* \param impl_out record CBC encryption implementation (or `NULL`).1593*/1594static inline void1595br_ssl_engine_set_cbc(br_ssl_engine_context *cc,1596const br_sslrec_in_cbc_class *impl_in,1597const br_sslrec_out_cbc_class *impl_out)1598{1599cc->icbc_in = impl_in;1600cc->icbc_out = impl_out;1601}16021603/**1604* \brief Set the record encryption and decryption engines for GCM.1605*1606* \param cc SSL engine context.1607* \param impl_in record GCM decryption implementation (or `NULL`).1608* \param impl_out record GCM encryption implementation (or `NULL`).1609*/1610static inline void1611br_ssl_engine_set_gcm(br_ssl_engine_context *cc,1612const br_sslrec_in_gcm_class *impl_in,1613const br_sslrec_out_gcm_class *impl_out)1614{1615cc->igcm_in = impl_in;1616cc->igcm_out = impl_out;1617}16181619/**1620* \brief Set the record encryption and decryption engines for CCM.1621*1622* \param cc SSL engine context.1623* \param impl_in record CCM decryption implementation (or `NULL`).1624* \param impl_out record CCM encryption implementation (or `NULL`).1625*/1626static inline void1627br_ssl_engine_set_ccm(br_ssl_engine_context *cc,1628const br_sslrec_in_ccm_class *impl_in,1629const br_sslrec_out_ccm_class *impl_out)1630{1631cc->iccm_in = impl_in;1632cc->iccm_out = impl_out;1633}16341635/**1636* \brief Set the record encryption and decryption engines for1637* ChaCha20+Poly1305.1638*1639* \param cc SSL engine context.1640* \param impl_in record ChaCha20 decryption implementation (or `NULL`).1641* \param impl_out record ChaCha20 encryption implementation (or `NULL`).1642*/1643static inline void1644br_ssl_engine_set_chapol(br_ssl_engine_context *cc,1645const br_sslrec_in_chapol_class *impl_in,1646const br_sslrec_out_chapol_class *impl_out)1647{1648cc->ichapol_in = impl_in;1649cc->ichapol_out = impl_out;1650}16511652/**1653* \brief Set the EC implementation.1654*1655* The elliptic curve implementation will be used for ECDH and ECDHE1656* cipher suites, and for ECDSA support.1657*1658* \param cc SSL engine context.1659* \param iec EC implementation (or `NULL`).1660*/1661static inline void1662br_ssl_engine_set_ec(br_ssl_engine_context *cc, const br_ec_impl *iec)1663{1664cc->iec = iec;1665}16661667/**1668* \brief Set the "default" EC implementation.1669*1670* This function sets the elliptic curve implementation for ECDH and1671* ECDHE cipher suites, and for ECDSA support. It selects the fastest1672* implementation on the current system.1673*1674* \param cc SSL engine context.1675*/1676void br_ssl_engine_set_default_ec(br_ssl_engine_context *cc);16771678/**1679* \brief Get the EC implementation configured in the provided engine.1680*1681* \param cc SSL engine context.1682* \return the EC implementation.1683*/1684static inline const br_ec_impl *1685br_ssl_engine_get_ec(br_ssl_engine_context *cc)1686{1687return cc->iec;1688}16891690/**1691* \brief Set the RSA signature verification implementation.1692*1693* On the client, this is used to verify the server's signature on its1694* ServerKeyExchange message (for ECDHE_RSA cipher suites). On the server,1695* this is used to verify the client's CertificateVerify message (if a1696* client certificate is requested, and that certificate contains a RSA key).1697*1698* \param cc SSL engine context.1699* \param irsavrfy RSA signature verification implementation.1700*/1701static inline void1702br_ssl_engine_set_rsavrfy(br_ssl_engine_context *cc, br_rsa_pkcs1_vrfy irsavrfy)1703{1704cc->irsavrfy = irsavrfy;1705}17061707/**1708* \brief Set the "default" RSA implementation (signature verification).1709*1710* This function sets the RSA implementation (signature verification)1711* to the fastest implementation available on the current platform.1712*1713* \param cc SSL engine context.1714*/1715void br_ssl_engine_set_default_rsavrfy(br_ssl_engine_context *cc);17161717/**1718* \brief Get the RSA implementation (signature verification) configured1719* in the provided engine.1720*1721* \param cc SSL engine context.1722* \return the RSA signature verification implementation.1723*/1724static inline br_rsa_pkcs1_vrfy1725br_ssl_engine_get_rsavrfy(br_ssl_engine_context *cc)1726{1727return cc->irsavrfy;1728}17291730/*1731* \brief Set the ECDSA implementation (signature verification).1732*1733* On the client, this is used to verify the server's signature on its1734* ServerKeyExchange message (for ECDHE_ECDSA cipher suites). On the server,1735* this is used to verify the client's CertificateVerify message (if a1736* client certificate is requested, that certificate contains an EC key,1737* and full-static ECDH is not used).1738*1739* The ECDSA implementation will use the EC core implementation configured1740* in the engine context.1741*1742* \param cc client context.1743* \param iecdsa ECDSA verification implementation.1744*/1745static inline void1746br_ssl_engine_set_ecdsa(br_ssl_engine_context *cc, br_ecdsa_vrfy iecdsa)1747{1748cc->iecdsa = iecdsa;1749}17501751/**1752* \brief Set the "default" ECDSA implementation (signature verification).1753*1754* This function sets the ECDSA implementation (signature verification)1755* to the fastest implementation available on the current platform. This1756* call also sets the elliptic curve implementation itself, there again1757* to the fastest EC implementation available.1758*1759* \param cc SSL engine context.1760*/1761void br_ssl_engine_set_default_ecdsa(br_ssl_engine_context *cc);17621763/**1764* \brief Get the ECDSA implementation (signature verification) configured1765* in the provided engine.1766*1767* \param cc SSL engine context.1768* \return the ECDSA signature verification implementation.1769*/1770static inline br_ecdsa_vrfy1771br_ssl_engine_get_ecdsa(br_ssl_engine_context *cc)1772{1773return cc->iecdsa;1774}17751776/**1777* \brief Set the I/O buffer for the SSL engine.1778*1779* Once this call has been made, `br_ssl_client_reset()` or1780* `br_ssl_server_reset()` MUST be called before using the context.1781*1782* The provided buffer will be used as long as the engine context is1783* used. The caller is responsible for keeping it available.1784*1785* If `bidi` is 0, then the engine will operate in half-duplex mode1786* (it won't be able to send data while there is unprocessed incoming1787* data in the buffer, and it won't be able to receive data while there1788* is unsent data in the buffer). The optimal buffer size in half-duplex1789* mode is `BR_SSL_BUFSIZE_MONO`; if the buffer is larger, then extra1790* bytes are ignored. If the buffer is smaller, then this limits the1791* capacity of the engine to support all allowed record sizes.1792*1793* If `bidi` is 1, then the engine will split the buffer into two1794* parts, for separate handling of outgoing and incoming data. This1795* enables full-duplex processing, but requires more RAM. The optimal1796* buffer size in full-duplex mode is `BR_SSL_BUFSIZE_BIDI`; if the1797* buffer is larger, then extra bytes are ignored. If the buffer is1798* smaller, then the split will favour the incoming part, so that1799* interoperability is maximised.1800*1801* \param cc SSL engine context1802* \param iobuf I/O buffer.1803* \param iobuf_len I/O buffer length (in bytes).1804* \param bidi non-zero for full-duplex mode.1805*/1806void br_ssl_engine_set_buffer(br_ssl_engine_context *cc,1807void *iobuf, size_t iobuf_len, int bidi);18081809/**1810* \brief Set the I/O buffers for the SSL engine.1811*1812* Once this call has been made, `br_ssl_client_reset()` or1813* `br_ssl_server_reset()` MUST be called before using the context.1814*1815* This function is similar to `br_ssl_engine_set_buffer()`, except1816* that it enforces full-duplex mode, and the two I/O buffers are1817* provided as separate chunks.1818*1819* The macros `BR_SSL_BUFSIZE_INPUT` and `BR_SSL_BUFSIZE_OUTPUT`1820* evaluate to the optimal (maximum) sizes for the input and output1821* buffer, respectively.1822*1823* \param cc SSL engine context1824* \param ibuf input buffer.1825* \param ibuf_len input buffer length (in bytes).1826* \param obuf output buffer.1827* \param obuf_len output buffer length (in bytes).1828*/1829void br_ssl_engine_set_buffers_bidi(br_ssl_engine_context *cc,1830void *ibuf, size_t ibuf_len, void *obuf, size_t obuf_len);18311832/**1833* \brief Inject some "initial entropy" in the context.1834*1835* This entropy will be added to what can be obtained from the1836* underlying operating system, if that OS is supported.1837*1838* This function may be called several times; all injected entropy chunks1839* are cumulatively mixed.1840*1841* If entropy gathering from the OS is supported and compiled in, then this1842* step is optional. Otherwise, it is mandatory to inject randomness, and1843* the caller MUST take care to push (as one or several successive calls)1844* enough entropy to achieve cryptographic resistance (at least 80 bits,1845* preferably 128 or more). The engine will report an error if no entropy1846* was provided and none can be obtained from the OS.1847*1848* Take care that this function cannot assess the cryptographic quality of1849* the provided bytes.1850*1851* In all generality, "entropy" must here be considered to mean "that1852* which the attacker cannot predict". If your OS/architecture does not1853* have a suitable source of randomness, then you can make do with the1854* combination of a large enough secret value (possibly a copy of an1855* asymmetric private key that you also store on the system) AND a1856* non-repeating value (e.g. current time, provided that the local clock1857* cannot be reset or altered by the attacker).1858*1859* \param cc SSL engine context.1860* \param data extra entropy to inject.1861* \param len length of the extra data (in bytes).1862*/1863void br_ssl_engine_inject_entropy(br_ssl_engine_context *cc,1864const void *data, size_t len);18651866/**1867* \brief Get the "server name" in this engine.1868*1869* For clients, this is the name provided with `br_ssl_client_reset()`;1870* for servers, this is the name received from the client as part of the1871* ClientHello message. If there is no such name (e.g. the client did1872* not send an SNI extension) then the returned string is empty1873* (returned pointer points to a byte of value 0).1874*1875* The returned pointer refers to a buffer inside the context, which may1876* be overwritten as part of normal SSL activity (even within the same1877* connection, if a renegotiation occurs).1878*1879* \param cc SSL engine context.1880* \return the server name (possibly empty).1881*/1882static inline const char *1883br_ssl_engine_get_server_name(const br_ssl_engine_context *cc)1884{1885return cc->server_name;1886}18871888/**1889* \brief Get the protocol version.1890*1891* This function returns the protocol version that is used by the1892* engine. That value is set after sending (for a server) or receiving1893* (for a client) the ServerHello message.1894*1895* \param cc SSL engine context.1896* \return the protocol version.1897*/1898static inline unsigned1899br_ssl_engine_get_version(const br_ssl_engine_context *cc)1900{1901return cc->session.version;1902}19031904/**1905* \brief Get a copy of the session parameters.1906*1907* The session parameters are filled during the handshake, so this1908* function shall not be called before completion of the handshake.1909* The initial handshake is completed when the context first allows1910* application data to be injected.1911*1912* This function copies the current session parameters into the provided1913* structure. Beware that the session parameters include the master1914* secret, which is sensitive data, to handle with great care.1915*1916* \param cc SSL engine context.1917* \param pp destination structure for the session parameters.1918*/1919static inline void1920br_ssl_engine_get_session_parameters(const br_ssl_engine_context *cc,1921br_ssl_session_parameters *pp)1922{1923memcpy(pp, &cc->session, sizeof *pp);1924}19251926/**1927* \brief Set the session parameters to the provided values.1928*1929* This function is meant to be used in the client, before doing a new1930* handshake; a session resumption will be attempted with these1931* parameters. In the server, this function has no effect.1932*1933* \param cc SSL engine context.1934* \param pp source structure for the session parameters.1935*/1936static inline void1937br_ssl_engine_set_session_parameters(br_ssl_engine_context *cc,1938const br_ssl_session_parameters *pp)1939{1940memcpy(&cc->session, pp, sizeof *pp);1941}19421943/**1944* \brief Get identifier for the curve used for key exchange.1945*1946* If the cipher suite uses ECDHE, then this function returns the1947* identifier for the curve used for transient parameters. This is1948* defined during the course of the handshake, when the ServerKeyExchange1949* is sent (on the server) or received (on the client). If the1950* cipher suite does not use ECDHE (e.g. static ECDH, or RSA key1951* exchange), then this value is indeterminate.1952*1953* @param cc SSL engine context.1954* @return the ECDHE curve identifier.1955*/1956static inline int1957br_ssl_engine_get_ecdhe_curve(br_ssl_engine_context *cc)1958{1959return cc->ecdhe_curve;1960}19611962/**1963* \brief Get the current engine state.1964*1965* An SSL engine (client or server) has, at any time, a state which is1966* the combination of zero, one or more of these flags:1967*1968* - `BR_SSL_CLOSED`1969*1970* Engine is finished, no more I/O (until next reset).1971*1972* - `BR_SSL_SENDREC`1973*1974* Engine has some bytes to send to the peer.1975*1976* - `BR_SSL_RECVREC`1977*1978* Engine expects some bytes from the peer.1979*1980* - `BR_SSL_SENDAPP`1981*1982* Engine may receive application data to send (or flush).1983*1984* - `BR_SSL_RECVAPP`1985*1986* Engine has obtained some application data from the peer,1987* that should be read by the caller.1988*1989* If no flag at all is set (state value is 0), then the engine is not1990* fully initialised yet.1991*1992* The `BR_SSL_CLOSED` flag is exclusive; when it is set, no other flag1993* is set. To distinguish between a normal closure and an error, use1994* `br_ssl_engine_last_error()`.1995*1996* Generally speaking, `BR_SSL_SENDREC` and `BR_SSL_SENDAPP` are mutually1997* exclusive: the input buffer, at any point, either accumulates1998* plaintext data, or contains an assembled record that is being sent.1999* Similarly, `BR_SSL_RECVREC` and `BR_SSL_RECVAPP` are mutually exclusive.2000* This may change in a future library version.2001*2002* \param cc SSL engine context.2003* \return the current engine state.2004*/2005unsigned br_ssl_engine_current_state(const br_ssl_engine_context *cc);20062007/** \brief SSL engine state: closed or failed. */2008#define BR_SSL_CLOSED 0x00012009/** \brief SSL engine state: record data is ready to be sent to the peer. */2010#define BR_SSL_SENDREC 0x00022011/** \brief SSL engine state: engine may receive records from the peer. */2012#define BR_SSL_RECVREC 0x00042013/** \brief SSL engine state: engine may accept application data to send. */2014#define BR_SSL_SENDAPP 0x00082015/** \brief SSL engine state: engine has received application data. */2016#define BR_SSL_RECVAPP 0x001020172018/**2019* \brief Get the engine error indicator.2020*2021* The error indicator is `BR_ERR_OK` (0) if no error was encountered2022* since the last call to `br_ssl_client_reset()` or2023* `br_ssl_server_reset()`. Other status values are "sticky": they2024* remain set, and prevent all I/O activity, until cleared. Only the2025* reset calls clear the error indicator.2026*2027* \param cc SSL engine context.2028* \return 0, or a non-zero error code.2029*/2030static inline int2031br_ssl_engine_last_error(const br_ssl_engine_context *cc)2032{2033return cc->err;2034}20352036/*2037* There are four I/O operations, each identified by a symbolic name:2038*2039* sendapp inject application data in the engine2040* recvapp retrieving application data from the engine2041* sendrec sending records on the transport medium2042* recvrec receiving records from the transport medium2043*2044* Terminology works thus: in a layered model where the SSL engine sits2045* between the application and the network, "send" designates operations2046* where bytes flow from application to network, and "recv" for the2047* reverse operation. Application data (the plaintext that is to be2048* conveyed through SSL) is "app", while encrypted records are "rec".2049* Note that from the SSL engine point of view, "sendapp" and "recvrec"2050* designate bytes that enter the engine ("inject" operation), while2051* "recvapp" and "sendrec" designate bytes that exit the engine2052* ("extract" operation).2053*2054* For the operation 'xxx', two functions are defined:2055*2056* br_ssl_engine_xxx_buf2057* Returns a pointer and length to the buffer to use for that2058* operation. '*len' is set to the number of bytes that may be read2059* from the buffer (extract operation) or written to the buffer2060* (inject operation). If no byte may be exchanged for that operation2061* at that point, then '*len' is set to zero, and NULL is returned.2062* The engine state is unmodified by this call.2063*2064* br_ssl_engine_xxx_ack2065* Informs the engine that 'len' bytes have been read from the buffer2066* (extract operation) or written to the buffer (inject operation).2067* The 'len' value MUST NOT be zero. The 'len' value MUST NOT exceed2068* that which was obtained from a preceding br_ssl_engine_xxx_buf()2069* call.2070*/20712072/**2073* \brief Get buffer for application data to send.2074*2075* If the engine is ready to accept application data to send to the2076* peer, then this call returns a pointer to the buffer where such2077* data shall be written, and its length is written in `*len`.2078* Otherwise, `*len` is set to 0 and `NULL` is returned.2079*2080* \param cc SSL engine context.2081* \param len receives the application data output buffer length, or 0.2082* \return the application data output buffer, or `NULL`.2083*/2084unsigned char *br_ssl_engine_sendapp_buf(2085const br_ssl_engine_context *cc, size_t *len);20862087/**2088* \brief Inform the engine of some new application data.2089*2090* After writing `len` bytes in the buffer returned by2091* `br_ssl_engine_sendapp_buf()`, the application shall call this2092* function to trigger any relevant processing. The `len` parameter2093* MUST NOT be 0, and MUST NOT exceed the value obtained in the2094* `br_ssl_engine_sendapp_buf()` call.2095*2096* \param cc SSL engine context.2097* \param len number of bytes pushed (not zero).2098*/2099void br_ssl_engine_sendapp_ack(br_ssl_engine_context *cc, size_t len);21002101/**2102* \brief Get buffer for received application data.2103*2104* If the engine has received application data from the peer, then this2105* call returns a pointer to the buffer from where such data shall be2106* read, and its length is written in `*len`. Otherwise, `*len` is set2107* to 0 and `NULL` is returned.2108*2109* \param cc SSL engine context.2110* \param len receives the application data input buffer length, or 0.2111* \return the application data input buffer, or `NULL`.2112*/2113unsigned char *br_ssl_engine_recvapp_buf(2114const br_ssl_engine_context *cc, size_t *len);21152116/**2117* \brief Acknowledge some received application data.2118*2119* After reading `len` bytes from the buffer returned by2120* `br_ssl_engine_recvapp_buf()`, the application shall call this2121* function to trigger any relevant processing. The `len` parameter2122* MUST NOT be 0, and MUST NOT exceed the value obtained in the2123* `br_ssl_engine_recvapp_buf()` call.2124*2125* \param cc SSL engine context.2126* \param len number of bytes read (not zero).2127*/2128void br_ssl_engine_recvapp_ack(br_ssl_engine_context *cc, size_t len);21292130/**2131* \brief Get buffer for record data to send.2132*2133* If the engine has prepared some records to send to the peer, then this2134* call returns a pointer to the buffer from where such data shall be2135* read, and its length is written in `*len`. Otherwise, `*len` is set2136* to 0 and `NULL` is returned.2137*2138* \param cc SSL engine context.2139* \param len receives the record data output buffer length, or 0.2140* \return the record data output buffer, or `NULL`.2141*/2142unsigned char *br_ssl_engine_sendrec_buf(2143const br_ssl_engine_context *cc, size_t *len);21442145/**2146* \brief Acknowledge some sent record data.2147*2148* After reading `len` bytes from the buffer returned by2149* `br_ssl_engine_sendrec_buf()`, the application shall call this2150* function to trigger any relevant processing. The `len` parameter2151* MUST NOT be 0, and MUST NOT exceed the value obtained in the2152* `br_ssl_engine_sendrec_buf()` call.2153*2154* \param cc SSL engine context.2155* \param len number of bytes read (not zero).2156*/2157void br_ssl_engine_sendrec_ack(br_ssl_engine_context *cc, size_t len);21582159/**2160* \brief Get buffer for incoming records.2161*2162* If the engine is ready to accept records from the peer, then this2163* call returns a pointer to the buffer where such data shall be2164* written, and its length is written in `*len`. Otherwise, `*len` is2165* set to 0 and `NULL` is returned.2166*2167* \param cc SSL engine context.2168* \param len receives the record data input buffer length, or 0.2169* \return the record data input buffer, or `NULL`.2170*/2171unsigned char *br_ssl_engine_recvrec_buf(2172const br_ssl_engine_context *cc, size_t *len);21732174/**2175* \brief Inform the engine of some new record data.2176*2177* After writing `len` bytes in the buffer returned by2178* `br_ssl_engine_recvrec_buf()`, the application shall call this2179* function to trigger any relevant processing. The `len` parameter2180* MUST NOT be 0, and MUST NOT exceed the value obtained in the2181* `br_ssl_engine_recvrec_buf()` call.2182*2183* \param cc SSL engine context.2184* \param len number of bytes pushed (not zero).2185*/2186void br_ssl_engine_recvrec_ack(br_ssl_engine_context *cc, size_t len);21872188/**2189* \brief Flush buffered application data.2190*2191* If some application data has been buffered in the engine, then wrap2192* it into a record and mark it for sending. If no application data has2193* been buffered but the engine would be ready to accept some, AND the2194* `force` parameter is non-zero, then an empty record is assembled and2195* marked for sending. In all other cases, this function does nothing.2196*2197* Empty records are technically legal, but not all existing SSL/TLS2198* implementations support them. Empty records can be useful as a2199* transparent "keep-alive" mechanism to maintain some low-level2200* network activity.2201*2202* \param cc SSL engine context.2203* \param force non-zero to force sending an empty record.2204*/2205void br_ssl_engine_flush(br_ssl_engine_context *cc, int force);22062207/**2208* \brief Initiate a closure.2209*2210* If, at that point, the context is open and in ready state, then a2211* `close_notify` alert is assembled and marked for sending; this2212* triggers the closure protocol. Otherwise, no such alert is assembled.2213*2214* \param cc SSL engine context.2215*/2216void br_ssl_engine_close(br_ssl_engine_context *cc);22172218/**2219* \brief Initiate a renegotiation.2220*2221* If the engine is failed or closed, or if the peer is known not to2222* support secure renegotiation (RFC 5746), or if renegotiations have2223* been disabled with the `BR_OPT_NO_RENEGOTIATION` flag, or if there2224* is buffered incoming application data, then this function returns 02225* and nothing else happens.2226*2227* Otherwise, this function returns 1, and a renegotiation attempt is2228* triggered (if a handshake is already ongoing at that point, then2229* no new handshake is triggered).2230*2231* \param cc SSL engine context.2232* \return 1 on success, 0 on error.2233*/2234int br_ssl_engine_renegotiate(br_ssl_engine_context *cc);22352236/**2237* \brief Export key material from a connected SSL engine (RFC 5705).2238*2239* This calls compute a secret key of arbitrary length from the master2240* secret of a connected SSL engine. If the provided context is not2241* currently in "application data" state (initial handshake is not2242* finished, another handshake is ongoing, or the connection failed or2243* was closed), then this function returns 0. Otherwise, a secret key of2244* length `len` bytes is computed and written in the buffer pointed to2245* by `dst`, and 1 is returned.2246*2247* The computed key follows the specification described in RFC 5705.2248* That RFC includes two key computations, with and without a "context2249* value". If `context` is `NULL`, then the variant without context is2250* used; otherwise, the `context_len` bytes located at the address2251* pointed to by `context` are used in the computation. Note that it2252* is possible to have a "with context" key with a context length of2253* zero bytes, by setting `context` to a non-`NULL` value but2254* `context_len` to 0.2255*2256* When context bytes are used, the context length MUST NOT exceed2257* 65535 bytes.2258*2259* \param cc SSL engine context.2260* \param dst destination buffer for exported key.2261* \param len exported key length (in bytes).2262* \param label disambiguation label.2263* \param context context value (or `NULL`).2264* \param context_len context length (in bytes).2265* \return 1 on success, 0 on error.2266*/2267int br_ssl_key_export(br_ssl_engine_context *cc,2268void *dst, size_t len, const char *label,2269const void *context, size_t context_len);22702271/*2272* Pre-declaration for the SSL client context.2273*/2274typedef struct br_ssl_client_context_ br_ssl_client_context;22752276/**2277* \brief Type for the client certificate, if requested by the server.2278*/2279typedef struct {2280/**2281* \brief Authentication type.2282*2283* This is either `BR_AUTH_RSA` (RSA signature), `BR_AUTH_ECDSA`2284* (ECDSA signature), or `BR_AUTH_ECDH` (static ECDH key exchange).2285*/2286int auth_type;22872288/**2289* \brief Hash function for computing the CertificateVerify.2290*2291* This is the symbolic identifier for the hash function that2292* will be used to produce the hash of handshake messages, to2293* be signed into the CertificateVerify. For full static ECDH2294* (client and server certificates are both EC in the same2295* curve, and static ECDH is used), this value is set to -1.2296*2297* Take care that with TLS 1.0 and 1.1, that value MUST match2298* the protocol requirements: value must be 0 (MD5+SHA-1) for2299* a RSA signature, or 2 (SHA-1) for an ECDSA signature. Only2300* TLS 1.2 allows for other hash functions.2301*/2302int hash_id;23032304/**2305* \brief Certificate chain to send to the server.2306*2307* This is an array of `br_x509_certificate` objects, each2308* normally containing a DER-encoded certificate. The client2309* code does not try to decode these elements. If there is no2310* chain to send to the server, then this pointer shall be2311* set to `NULL`.2312*/2313const br_x509_certificate *chain;23142315/**2316* \brief Certificate chain length (number of certificates).2317*2318* If there is no chain to send to the server, then this value2319* shall be set to 0.2320*/2321size_t chain_len;23222323} br_ssl_client_certificate;23242325/*2326* Note: the constants below for signatures match the TLS constants.2327*/23282329/** \brief Client authentication type: static ECDH. */2330#define BR_AUTH_ECDH 02331/** \brief Client authentication type: RSA signature. */2332#define BR_AUTH_RSA 12333/** \brief Client authentication type: ECDSA signature. */2334#define BR_AUTH_ECDSA 323352336/**2337* \brief Class type for a certificate handler (client side).2338*2339* A certificate handler selects a client certificate chain to send to2340* the server, upon explicit request from that server. It receives2341* the list of trust anchor DN from the server, and supported types2342* of certificates and signatures, and returns the chain to use. It2343* is also invoked to perform the corresponding private key operation2344* (a signature, or an ECDH computation).2345*2346* The SSL client engine will first push the trust anchor DN with2347* `start_name_list()`, `start_name()`, `append_name()`, `end_name()`2348* and `end_name_list()`. Then it will call `choose()`, to select the2349* actual chain (and signature/hash algorithms). Finally, it will call2350* either `do_sign()` or `do_keyx()`, depending on the algorithm choices.2351*/2352typedef struct br_ssl_client_certificate_class_ br_ssl_client_certificate_class;2353struct br_ssl_client_certificate_class_ {2354/**2355* \brief Context size (in bytes).2356*/2357size_t context_size;23582359/**2360* \brief Begin reception of a list of trust anchor names. This2361* is called while parsing the incoming CertificateRequest.2362*2363* \param pctx certificate handler context.2364*/2365void (*start_name_list)(const br_ssl_client_certificate_class **pctx);23662367/**2368* \brief Begin reception of a new trust anchor name.2369*2370* The total encoded name length is provided; it is less than2371* 65535 bytes.2372*2373* \param pctx certificate handler context.2374* \param len encoded name length (in bytes).2375*/2376void (*start_name)(const br_ssl_client_certificate_class **pctx,2377size_t len);23782379/**2380* \brief Receive some more bytes for the current trust anchor name.2381*2382* The provided reference (`data`) points to a transient buffer2383* they may be reused as soon as this function returns. The chunk2384* length (`len`) is never zero.2385*2386* \param pctx certificate handler context.2387* \param data anchor name chunk.2388* \param len anchor name chunk length (in bytes).2389*/2390void (*append_name)(const br_ssl_client_certificate_class **pctx,2391const unsigned char *data, size_t len);23922393/**2394* \brief End current trust anchor name.2395*2396* This function is called when all the encoded anchor name data2397* has been provided.2398*2399* \param pctx certificate handler context.2400*/2401void (*end_name)(const br_ssl_client_certificate_class **pctx);24022403/**2404* \brief End list of trust anchor names.2405*2406* This function is called when all the anchor names in the2407* CertificateRequest message have been obtained.2408*2409* \param pctx certificate handler context.2410*/2411void (*end_name_list)(const br_ssl_client_certificate_class **pctx);24122413/**2414* \brief Select client certificate and algorithms.2415*2416* This callback function shall fill the provided `choices`2417* structure with the selected algorithms and certificate chain.2418* The `hash_id`, `chain` and `chain_len` fields must be set. If2419* the client cannot or does not wish to send a certificate,2420* then it shall set `chain` to `NULL` and `chain_len` to 0.2421*2422* The `auth_types` parameter describes the authentication types,2423* signature algorithms and hash functions that are supported by2424* both the client context and the server, and compatible with2425* the current protocol version. This is a bit field with the2426* following contents:2427*2428* - If RSA signatures with hash function x are supported, then2429* bit x is set.2430*2431* - If ECDSA signatures with hash function x are supported,2432* then bit 8+x is set.2433*2434* - If static ECDH is supported, with a RSA-signed certificate,2435* then bit 16 is set.2436*2437* - If static ECDH is supported, with an ECDSA-signed certificate,2438* then bit 17 is set.2439*2440* Notes:2441*2442* - When using TLS 1.0 or 1.1, the hash function for RSA2443* signatures is always the special MD5+SHA-1 (id 0), and the2444* hash function for ECDSA signatures is always SHA-1 (id 2).2445*2446* - When using TLS 1.2, the list of hash functions is trimmed2447* down to include only hash functions that the client context2448* can support. The actual server list can be obtained with2449* `br_ssl_client_get_server_hashes()`; that list may be used2450* to select the certificate chain to send to the server.2451*2452* \param pctx certificate handler context.2453* \param cc SSL client context.2454* \param auth_types supported authentication types and algorithms.2455* \param choices destination structure for the policy choices.2456*/2457void (*choose)(const br_ssl_client_certificate_class **pctx,2458const br_ssl_client_context *cc, uint32_t auth_types,2459br_ssl_client_certificate *choices);24602461/**2462* \brief Perform key exchange (client part).2463*2464* This callback is invoked in case of a full static ECDH key2465* exchange:2466*2467* - the cipher suite uses `ECDH_RSA` or `ECDH_ECDSA`;2468*2469* - the server requests a client certificate;2470*2471* - the client has, and sends, a client certificate that2472* uses an EC key in the same curve as the server's key,2473* and chooses static ECDH (the `hash_id` field in the choice2474* structure was set to -1).2475*2476* In that situation, this callback is invoked to compute the2477* client-side ECDH: the provided `data` (of length `*len` bytes)2478* is the server's public key point (as decoded from its2479* certificate), and the client shall multiply that point with2480* its own private key, and write back the X coordinate of the2481* resulting point in the same buffer, starting at offset 0.2482* The `*len` value shall be modified to designate the actual2483* length of the X coordinate.2484*2485* The callback must uphold the following:2486*2487* - If the input array does not have the proper length for2488* an encoded curve point, then an error (0) shall be reported.2489*2490* - If the input array has the proper length, then processing2491* MUST be constant-time, even if the data is not a valid2492* encoded point.2493*2494* - This callback MUST check that the input point is valid.2495*2496* Returned value is 1 on success, 0 on error.2497*2498* \param pctx certificate handler context.2499* \param data server public key point.2500* \param len public key point length / X coordinate length.2501* \return 1 on success, 0 on error.2502*/2503uint32_t (*do_keyx)(const br_ssl_client_certificate_class **pctx,2504unsigned char *data, size_t *len);25052506/**2507* \brief Perform a signature (client authentication).2508*2509* This callback is invoked when a client certificate was sent,2510* and static ECDH is not used. It shall compute a signature,2511* using the client's private key, over the provided hash value2512* (which is the hash of all previous handshake messages).2513*2514* On input, the hash value to sign is in `data`, of size2515* `hv_len`; the involved hash function is identified by2516* `hash_id`. The signature shall be computed and written2517* back into `data`; the total size of that buffer is `len`2518* bytes.2519*2520* This callback shall verify that the signature length does not2521* exceed `len` bytes, and abstain from writing the signature if2522* it does not fit.2523*2524* For RSA signatures, the `hash_id` may be 0, in which case2525* this is the special header-less signature specified in TLS 1.02526* and 1.1, with a 36-byte hash value. Otherwise, normal PKCS#12527* v1.5 signatures shall be computed.2528*2529* For ECDSA signatures, the signature value shall use the ASN.12530* based encoding.2531*2532* Returned value is the signature length (in bytes), or 0 on error.2533*2534* \param pctx certificate handler context.2535* \param hash_id hash function identifier.2536* \param hv_len hash value length (in bytes).2537* \param data input/output buffer (hash value, then signature).2538* \param len total buffer length (in bytes).2539* \return signature length (in bytes) on success, or 0 on error.2540*/2541size_t (*do_sign)(const br_ssl_client_certificate_class **pctx,2542int hash_id, size_t hv_len, unsigned char *data, size_t len);2543};25442545/**2546* \brief A single-chain RSA client certificate handler.2547*2548* This handler uses a single certificate chain, with a RSA2549* signature. The list of trust anchor DN is ignored.2550*2551* Apart from the first field (vtable pointer), its contents are2552* opaque and shall not be accessed directly.2553*/2554typedef struct {2555/** \brief Pointer to vtable. */2556const br_ssl_client_certificate_class *vtable;2557#ifndef BR_DOXYGEN_IGNORE2558const br_x509_certificate *chain;2559size_t chain_len;2560const br_rsa_private_key *sk;2561br_rsa_pkcs1_sign irsasign;2562#endif2563} br_ssl_client_certificate_rsa_context;25642565/**2566* \brief A single-chain EC client certificate handler.2567*2568* This handler uses a single certificate chain, with a RSA2569* signature. The list of trust anchor DN is ignored.2570*2571* This handler may support both static ECDH, and ECDSA signatures2572* (either usage may be selectively disabled).2573*2574* Apart from the first field (vtable pointer), its contents are2575* opaque and shall not be accessed directly.2576*/2577typedef struct {2578/** \brief Pointer to vtable. */2579const br_ssl_client_certificate_class *vtable;2580#ifndef BR_DOXYGEN_IGNORE2581const br_x509_certificate *chain;2582size_t chain_len;2583const br_ec_private_key *sk;2584unsigned allowed_usages;2585unsigned issuer_key_type;2586const br_multihash_context *mhash;2587const br_ec_impl *iec;2588br_ecdsa_sign iecdsa;2589#endif2590} br_ssl_client_certificate_ec_context;25912592/**2593* \brief Context structure for a SSL client.2594*2595* The first field (called `eng`) is the SSL engine; all functions that2596* work on a `br_ssl_engine_context` structure shall take as parameter2597* a pointer to that field. The other structure fields are opaque and2598* must not be accessed directly.2599*/2600struct br_ssl_client_context_ {2601/**2602* \brief The encapsulated engine context.2603*/2604br_ssl_engine_context eng;26052606#ifndef BR_DOXYGEN_IGNORE2607/*2608* Minimum ClientHello length; padding with an extension (RFC2609* 7685) is added if necessary to match at least that length.2610* Such padding is nominally unnecessary, but it has been used2611* to work around some server implementation bugs.2612*/2613uint16_t min_clienthello_len;26142615/*2616* Bit field for algoithms (hash + signature) supported by the2617* server when requesting a client certificate.2618*/2619uint32_t hashes;26202621/*2622* Server's public key curve.2623*/2624int server_curve;26252626/*2627* Context for certificate handler.2628*/2629const br_ssl_client_certificate_class **client_auth_vtable;26302631/*2632* Client authentication type.2633*/2634unsigned char auth_type;26352636/*2637* Hash function to use for the client signature. This is 0xFF2638* if static ECDH is used.2639*/2640unsigned char hash_id;26412642/*2643* For the core certificate handlers, thus avoiding (in most2644* cases) the need for an externally provided policy context.2645*/2646union {2647const br_ssl_client_certificate_class *vtable;2648br_ssl_client_certificate_rsa_context single_rsa;2649br_ssl_client_certificate_ec_context single_ec;2650} client_auth;26512652/*2653* Implementations.2654*/2655br_rsa_public irsapub;2656#endif2657};26582659/**2660* \brief Get the hash functions and signature algorithms supported by2661* the server.2662*2663* This value is a bit field:2664*2665* - If RSA (PKCS#1 v1.5) is supported with hash function of ID `x`,2666* then bit `x` is set (hash function ID is 0 for the special MD5+SHA-1,2667* or 2 to 6 for the SHA family).2668*2669* - If ECDSA is supported with hash function of ID `x`, then bit `8+x`2670* is set.2671*2672* - Newer algorithms are symbolic 16-bit identifiers that do not2673* represent signature algorithm and hash function separately. If2674* the TLS-level identifier is `0x0800+x` for a `x` in the 0..152675* range, then bit `16+x` is set.2676*2677* "New algorithms" are currently defined only in draft documents, so2678* this support is subject to possible change. Right now (early 2017),2679* this maps ed25519 (EdDSA on Curve25519) to bit 23, and ed448 (EdDSA2680* on Curve448) to bit 24. If the identifiers on the wire change in2681* future document, then the decoding mechanism in BearSSL will be2682* amended to keep mapping ed25519 and ed448 on bits 23 and 24,2683* respectively. Mapping of other new algorithms (e.g. RSA/PSS) is not2684* guaranteed yet.2685*2686* \param cc client context.2687* \return the server-supported hash functions and signature algorithms.2688*/2689static inline uint32_t2690br_ssl_client_get_server_hashes(const br_ssl_client_context *cc)2691{2692return cc->hashes;2693}26942695/**2696* \brief Get the server key curve.2697*2698* This function returns the ID for the curve used by the server's public2699* key. This is set when the server's certificate chain is processed;2700* this value is 0 if the server's key is not an EC key.2701*2702* \return the server's public key curve ID, or 0.2703*/2704static inline int2705br_ssl_client_get_server_curve(const br_ssl_client_context *cc)2706{2707return cc->server_curve;2708}27092710/*2711* Each br_ssl_client_init_xxx() function sets the list of supported2712* cipher suites and used implementations, as specified by the profile2713* name 'xxx'. Defined profile names are:2714*2715* full all supported versions and suites; constant-time implementations2716* TODO: add other profiles2717*/27182719/**2720* \brief SSL client profile: full.2721*2722* This function initialises the provided SSL client context with2723* all supported algorithms and cipher suites. It also initialises2724* a companion X.509 validation engine with all supported algorithms,2725* and the provided trust anchors; the X.509 engine will be used by2726* the client context to validate the server's certificate.2727*2728* \param cc client context to initialise.2729* \param xc X.509 validation context to initialise.2730* \param trust_anchors trust anchors to use.2731* \param trust_anchors_num number of trust anchors.2732*/2733void br_ssl_client_init_full(br_ssl_client_context *cc,2734br_x509_minimal_context *xc,2735const br_x509_trust_anchor *trust_anchors, size_t trust_anchors_num);27362737/**2738* \brief Clear the complete contents of a SSL client context.2739*2740* Everything is cleared, including the reference to the configured buffer,2741* implementations, cipher suites and state. This is a preparatory step2742* to assembling a custom profile.2743*2744* \param cc client context to clear.2745*/2746void br_ssl_client_zero(br_ssl_client_context *cc);27472748/**2749* \brief Set an externally provided client certificate handler context.2750*2751* The handler's methods are invoked when the server requests a client2752* certificate.2753*2754* \param cc client context.2755* \param pctx certificate handler context (pointer to its vtable field).2756*/2757static inline void2758br_ssl_client_set_client_certificate(br_ssl_client_context *cc,2759const br_ssl_client_certificate_class **pctx)2760{2761cc->client_auth_vtable = pctx;2762}27632764/**2765* \brief Set the RSA public-key operations implementation.2766*2767* This will be used to encrypt the pre-master secret with the server's2768* RSA public key (RSA-encryption cipher suites only).2769*2770* \param cc client context.2771* \param irsapub RSA public-key encryption implementation.2772*/2773static inline void2774br_ssl_client_set_rsapub(br_ssl_client_context *cc, br_rsa_public irsapub)2775{2776cc->irsapub = irsapub;2777}27782779/**2780* \brief Set the "default" RSA implementation for public-key operations.2781*2782* This sets the RSA implementation in the client context (for encrypting2783* the pre-master secret, in `TLS_RSA_*` cipher suites) to the fastest2784* available on the current platform.2785*2786* \param cc client context.2787*/2788void br_ssl_client_set_default_rsapub(br_ssl_client_context *cc);27892790/**2791* \brief Set the minimum ClientHello length (RFC 7685 padding).2792*2793* If this value is set and the ClientHello would be shorter, then2794* the Pad ClientHello extension will be added with enough padding bytes2795* to reach the target size. Because of the extension header, the resulting2796* size will sometimes be slightly more than `len` bytes if the target2797* size cannot be exactly met.2798*2799* The target length relates to the _contents_ of the ClientHello, not2800* counting its 4-byte header. For instance, if `len` is set to 512,2801* then the padding will bring the ClientHello size to 516 bytes with its2802* header, and 521 bytes when counting the 5-byte record header.2803*2804* \param cc client context.2805* \param len minimum ClientHello length (in bytes).2806*/2807static inline void2808br_ssl_client_set_min_clienthello_len(br_ssl_client_context *cc, uint16_t len)2809{2810cc->min_clienthello_len = len;2811}28122813/**2814* \brief Prepare or reset a client context for a new connection.2815*2816* The `server_name` parameter is used to fill the SNI extension; the2817* X.509 "minimal" engine will also match that name against the server2818* names included in the server's certificate. If the parameter is2819* `NULL` then no SNI extension will be sent, and the X.509 "minimal"2820* engine (if used for server certificate validation) will not check2821* presence of any specific name in the received certificate.2822*2823* Therefore, setting the `server_name` to `NULL` shall be reserved2824* to cases where alternate or additional methods are used to ascertain2825* that the right server public key is used (e.g. a "known key" model).2826*2827* If `resume_session` is non-zero and the context was previously used2828* then the session parameters may be reused (depending on whether the2829* server previously sent a non-empty session ID, and accepts the session2830* resumption). The session parameters for session resumption can also2831* be set explicitly with `br_ssl_engine_set_session_parameters()`.2832*2833* On failure, the context is marked as failed, and this function2834* returns 0. A possible failure condition is when no initial entropy2835* was injected, and none could be obtained from the OS (either OS2836* randomness gathering is not supported, or it failed).2837*2838* \param cc client context.2839* \param server_name target server name, or `NULL`.2840* \param resume_session non-zero to try session resumption.2841* \return 0 on failure, 1 on success.2842*/2843int br_ssl_client_reset(br_ssl_client_context *cc,2844const char *server_name, int resume_session);28452846/**2847* \brief Forget any session in the context.2848*2849* This means that the next handshake that uses this context will2850* necessarily be a full handshake (this applies both to new connections2851* and to renegotiations).2852*2853* \param cc client context.2854*/2855static inline void2856br_ssl_client_forget_session(br_ssl_client_context *cc)2857{2858cc->eng.session.session_id_len = 0;2859}28602861/**2862* \brief Set client certificate chain and key (single RSA case).2863*2864* This function sets a client certificate chain, that the client will2865* send to the server whenever a client certificate is requested. This2866* certificate uses an RSA public key; the corresponding private key is2867* invoked for authentication. Trust anchor names sent by the server are2868* ignored.2869*2870* The provided chain and private key are linked in the client context;2871* they must remain valid as long as they may be used, i.e. normally2872* for the duration of the connection, since they might be invoked2873* again upon renegotiations.2874*2875* \param cc SSL client context.2876* \param chain client certificate chain (SSL order: EE comes first).2877* \param chain_len client chain length (number of certificates).2878* \param sk client private key.2879* \param irsasign RSA signature implementation (PKCS#1 v1.5).2880*/2881void br_ssl_client_set_single_rsa(br_ssl_client_context *cc,2882const br_x509_certificate *chain, size_t chain_len,2883const br_rsa_private_key *sk, br_rsa_pkcs1_sign irsasign);28842885/*2886* \brief Set the client certificate chain and key (single EC case).2887*2888* This function sets a client certificate chain, that the client will2889* send to the server whenever a client certificate is requested. This2890* certificate uses an EC public key; the corresponding private key is2891* invoked for authentication. Trust anchor names sent by the server are2892* ignored.2893*2894* The provided chain and private key are linked in the client context;2895* they must remain valid as long as they may be used, i.e. normally2896* for the duration of the connection, since they might be invoked2897* again upon renegotiations.2898*2899* The `allowed_usages` is a combination of usages, namely2900* `BR_KEYTYPE_KEYX` and/or `BR_KEYTYPE_SIGN`. The `BR_KEYTYPE_KEYX`2901* value allows full static ECDH, while the `BR_KEYTYPE_SIGN` value2902* allows ECDSA signatures. If ECDSA signatures are used, then an ECDSA2903* signature implementation must be provided; otherwise, the `iecdsa`2904* parameter may be 0.2905*2906* The `cert_issuer_key_type` value is either `BR_KEYTYPE_RSA` or2907* `BR_KEYTYPE_EC`; it is the type of the public key used the the CA2908* that issued (signed) the client certificate. That value is used with2909* full static ECDH: support of the certificate by the server depends2910* on how the certificate was signed. (Note: when using TLS 1.2, this2911* parameter is ignored; but its value matters for TLS 1.0 and 1.1.)2912*2913* \param cc server context.2914* \param chain server certificate chain to send.2915* \param chain_len chain length (number of certificates).2916* \param sk server private key (EC).2917* \param allowed_usages allowed private key usages.2918* \param cert_issuer_key_type issuing CA's key type.2919* \param iec EC core implementation.2920* \param iecdsa ECDSA signature implementation ("asn1" format).2921*/2922void br_ssl_client_set_single_ec(br_ssl_client_context *cc,2923const br_x509_certificate *chain, size_t chain_len,2924const br_ec_private_key *sk, unsigned allowed_usages,2925unsigned cert_issuer_key_type,2926const br_ec_impl *iec, br_ecdsa_sign iecdsa);29272928/**2929* \brief Type for a "translated cipher suite", as an array of two2930* 16-bit integers.2931*2932* The first element is the cipher suite identifier (as used on the wire).2933* The second element is the concatenation of four 4-bit elements which2934* characterise the cipher suite contents. In most to least significant2935* order, these 4-bit elements are:2936*2937* - Bits 12 to 15: key exchange + server key type2938*2939* | val | symbolic constant | suite type | details |2940* | :-- | :----------------------- | :---------- | :----------------------------------------------- |2941* | 0 | `BR_SSLKEYX_RSA` | RSA | RSA key exchange, key is RSA (encryption) |2942* | 1 | `BR_SSLKEYX_ECDHE_RSA` | ECDHE_RSA | ECDHE key exchange, key is RSA (signature) |2943* | 2 | `BR_SSLKEYX_ECDHE_ECDSA` | ECDHE_ECDSA | ECDHE key exchange, key is EC (signature) |2944* | 3 | `BR_SSLKEYX_ECDH_RSA` | ECDH_RSA | Key is EC (key exchange), cert signed with RSA |2945* | 4 | `BR_SSLKEYX_ECDH_ECDSA` | ECDH_ECDSA | Key is EC (key exchange), cert signed with ECDSA |2946*2947* - Bits 8 to 11: symmetric encryption algorithm2948*2949* | val | symbolic constant | symmetric encryption | key strength (bits) |2950* | :-- | :--------------------- | :------------------- | :------------------ |2951* | 0 | `BR_SSLENC_3DES_CBC` | 3DES/CBC | 168 |2952* | 1 | `BR_SSLENC_AES128_CBC` | AES-128/CBC | 128 |2953* | 2 | `BR_SSLENC_AES256_CBC` | AES-256/CBC | 256 |2954* | 3 | `BR_SSLENC_AES128_GCM` | AES-128/GCM | 128 |2955* | 4 | `BR_SSLENC_AES256_GCM` | AES-256/GCM | 256 |2956* | 5 | `BR_SSLENC_CHACHA20` | ChaCha20/Poly1305 | 256 |2957*2958* - Bits 4 to 7: MAC algorithm2959*2960* | val | symbolic constant | MAC type | details |2961* | :-- | :----------------- | :----------- | :------------------------------------ |2962* | 0 | `BR_SSLMAC_AEAD` | AEAD | No dedicated MAC (encryption is AEAD) |2963* | 2 | `BR_SSLMAC_SHA1` | HMAC/SHA-1 | Value matches `br_sha1_ID` |2964* | 4 | `BR_SSLMAC_SHA256` | HMAC/SHA-256 | Value matches `br_sha256_ID` |2965* | 5 | `BR_SSLMAC_SHA384` | HMAC/SHA-384 | Value matches `br_sha384_ID` |2966*2967* - Bits 0 to 3: hash function for PRF when used with TLS-1.22968*2969* | val | symbolic constant | hash function | details |2970* | :-- | :----------------- | :------------ | :----------------------------------- |2971* | 4 | `BR_SSLPRF_SHA256` | SHA-256 | Value matches `br_sha256_ID` |2972* | 5 | `BR_SSLPRF_SHA384` | SHA-384 | Value matches `br_sha384_ID` |2973*2974* For instance, cipher suite `TLS_RSA_WITH_AES_128_GCM_SHA256` has2975* standard identifier 0x009C, and is translated to 0x0304, for, in2976* that order: RSA key exchange (0), AES-128/GCM (3), AEAD integrity (0),2977* SHA-256 in the TLS PRF (4).2978*/2979typedef uint16_t br_suite_translated[2];29802981#ifndef BR_DOXYGEN_IGNORE2982/*2983* Constants are already documented in the br_suite_translated type.2984*/29852986#define BR_SSLKEYX_RSA 02987#define BR_SSLKEYX_ECDHE_RSA 12988#define BR_SSLKEYX_ECDHE_ECDSA 22989#define BR_SSLKEYX_ECDH_RSA 32990#define BR_SSLKEYX_ECDH_ECDSA 429912992#define BR_SSLENC_3DES_CBC 02993#define BR_SSLENC_AES128_CBC 12994#define BR_SSLENC_AES256_CBC 22995#define BR_SSLENC_AES128_GCM 32996#define BR_SSLENC_AES256_GCM 42997#define BR_SSLENC_CHACHA20 529982999#define BR_SSLMAC_AEAD 03000#define BR_SSLMAC_SHA1 br_sha1_ID3001#define BR_SSLMAC_SHA256 br_sha256_ID3002#define BR_SSLMAC_SHA384 br_sha384_ID30033004#define BR_SSLPRF_SHA256 br_sha256_ID3005#define BR_SSLPRF_SHA384 br_sha384_ID30063007#endif30083009/*3010* Pre-declaration for the SSL server context.3011*/3012typedef struct br_ssl_server_context_ br_ssl_server_context;30133014/**3015* \brief Type for the server policy choices, taken after analysis of3016* the client message (ClientHello).3017*/3018typedef struct {3019/**3020* \brief Cipher suite to use with that client.3021*/3022uint16_t cipher_suite;30233024/**3025* \brief Hash function or algorithm for signing the ServerKeyExchange.3026*3027* This parameter is ignored for `TLS_RSA_*` and `TLS_ECDH_*`3028* cipher suites; it is used only for `TLS_ECDHE_*` suites, in3029* which the server _signs_ the ephemeral EC Diffie-Hellman3030* parameters sent to the client.3031*3032* This identifier must be one of the following values:3033*3034* - `0xFF00 + id`, where `id` is a hash function identifier3035* (0 for MD5+SHA-1, or 2 to 6 for one of the SHA functions);3036*3037* - a full 16-bit identifier, lower than `0xFF00`.3038*3039* If the first option is used, then the SSL engine will3040* compute the hash of the data that is to be signed, with the3041* designated hash function. The `do_sign()` method will be3042* invoked with that hash value provided in the the `data`3043* buffer.3044*3045* If the second option is used, then the SSL engine will NOT3046* compute a hash on the data; instead, it will provide the3047* to-be-signed data itself in `data`, i.e. the concatenation of3048* the client random, server random, and encoded ECDH3049* parameters. Furthermore, with TLS-1.2 and later, the 16-bit3050* identifier will be used "as is" in the protocol, in the3051* SignatureAndHashAlgorithm; for instance, `0x0401` stands for3052* RSA PKCS#1 v1.5 signature (the `01`) with SHA-256 as hash3053* function (the `04`).3054*3055* Take care that with TLS 1.0 and 1.1, the hash function is3056* constrainted by the protocol: RSA signature must use3057* MD5+SHA-1 (so use `0xFF00`), while ECDSA must use SHA-13058* (`0xFF02`). Since TLS 1.0 and 1.1 don't include a3059* SignatureAndHashAlgorithm field in their ServerKeyExchange3060* messages, any value below `0xFF00` will be usable to send the3061* raw ServerKeyExchange data to the `do_sign()` callback, but3062* that callback must still follow the protocol requirements3063* when generating the signature.3064*/3065unsigned algo_id;30663067/**3068* \brief Certificate chain to send to the client.3069*3070* This is an array of `br_x509_certificate` objects, each3071* normally containing a DER-encoded certificate. The server3072* code does not try to decode these elements.3073*/3074const br_x509_certificate *chain;30753076/**3077* \brief Certificate chain length (number of certificates).3078*/3079size_t chain_len;30803081} br_ssl_server_choices;30823083/**3084* \brief Class type for a policy handler (server side).3085*3086* A policy handler selects the policy parameters for a connection3087* (cipher suite and other algorithms, and certificate chain to send to3088* the client); it also performs the server-side computations involving3089* its permanent private key.3090*3091* The SSL server engine will invoke first `choose()`, once the3092* ClientHello message has been received, then either `do_keyx()`3093* `do_sign()`, depending on the cipher suite.3094*/3095typedef struct br_ssl_server_policy_class_ br_ssl_server_policy_class;3096struct br_ssl_server_policy_class_ {3097/**3098* \brief Context size (in bytes).3099*/3100size_t context_size;31013102/**3103* \brief Select algorithms and certificates for this connection.3104*3105* This callback function shall fill the provided `choices`3106* structure with the policy choices for this connection. This3107* entails selecting the cipher suite, hash function for signing3108* the ServerKeyExchange (applicable only to ECDHE cipher suites),3109* and certificate chain to send.3110*3111* The callback receives a pointer to the server context that3112* contains the relevant data. In particular, the functions3113* `br_ssl_server_get_client_suites()`,3114* `br_ssl_server_get_client_hashes()` and3115* `br_ssl_server_get_client_curves()` can be used to obtain3116* the cipher suites, hash functions and elliptic curves3117* supported by both the client and server, respectively. The3118* `br_ssl_engine_get_version()` and `br_ssl_engine_get_server_name()`3119* functions yield the protocol version and requested server name3120* (SNI), respectively.3121*3122* This function may modify its context structure (`pctx`) in3123* arbitrary ways to keep track of its own choices.3124*3125* This function shall return 1 if appropriate policy choices3126* could be made, or 0 if this connection cannot be pursued.3127*3128* \param pctx policy context.3129* \param cc SSL server context.3130* \param choices destination structure for the policy choices.3131* \return 1 on success, 0 on error.3132*/3133int (*choose)(const br_ssl_server_policy_class **pctx,3134const br_ssl_server_context *cc,3135br_ssl_server_choices *choices);31363137/**3138* \brief Perform key exchange (server part).3139*3140* This callback is invoked to perform the server-side cryptographic3141* operation for a key exchange that is not ECDHE. This callback3142* uses the private key.3143*3144* **For RSA key exchange**, the provided `data` (of length `*len`3145* bytes) shall be decrypted with the server's private key, and3146* the 48-byte premaster secret copied back to the first 48 bytes3147* of `data`.3148*3149* - The caller makes sure that `*len` is at least 59 bytes.3150*3151* - This callback MUST check that the provided length matches3152* that of the key modulus; it shall report an error otherwise.3153*3154* - If the length matches that of the RSA key modulus, then3155* processing MUST be constant-time, even if decryption fails,3156* or the padding is incorrect, or the plaintext message length3157* is not exactly 48 bytes.3158*3159* - This callback needs not check the two first bytes of the3160* obtained pre-master secret (the caller will do that).3161*3162* - If an error is reported (0), then what the callback put3163* in the first 48 bytes of `data` is unimportant (the caller3164* will use random bytes instead).3165*3166* **For ECDH key exchange**, the provided `data` (of length `*len`3167* bytes) is the elliptic curve point from the client. The3168* callback shall multiply it with its private key, and store3169* the resulting X coordinate in `data`, starting at offset 0,3170* and set `*len` to the length of the X coordinate.3171*3172* - If the input array does not have the proper length for3173* an encoded curve point, then an error (0) shall be reported.3174*3175* - If the input array has the proper length, then processing3176* MUST be constant-time, even if the data is not a valid3177* encoded point.3178*3179* - This callback MUST check that the input point is valid.3180*3181* Returned value is 1 on success, 0 on error.3182*3183* \param pctx policy context.3184* \param data key exchange data from the client.3185* \param len key exchange data length (in bytes).3186* \return 1 on success, 0 on error.3187*/3188uint32_t (*do_keyx)(const br_ssl_server_policy_class **pctx,3189unsigned char *data, size_t *len);31903191/**3192* \brief Perform a signature (for a ServerKeyExchange message).3193*3194* This callback function is invoked for ECDHE cipher suites. On3195* input, the hash value or message to sign is in `data`, of3196* size `hv_len`; the involved hash function or algorithm is3197* identified by `algo_id`. The signature shall be computed and3198* written back into `data`; the total size of that buffer is3199* `len` bytes.3200*3201* This callback shall verify that the signature length does not3202* exceed `len` bytes, and abstain from writing the signature if3203* it does not fit.3204*3205* The `algo_id` value matches that which was written in the3206* `choices` structures by the `choose()` callback. This will be3207* one of the following:3208*3209* - `0xFF00 + id` for a hash function identifier `id`. In3210* that case, the `data` buffer contains a hash value3211* already computed over the data that is to be signed,3212* of length `hv_len`. The `id` may be 0 to designate the3213* special MD5+SHA-1 concatenation (old-style RSA signing).3214*3215* - Another value, lower than `0xFF00`. The `data` buffer3216* then contains the raw, non-hashed data to be signed3217* (concatenation of the client and server randoms and3218* ECDH parameters). The callback is responsible to apply3219* any relevant hashing as part of the signing process.3220*3221* Returned value is the signature length (in bytes), or 0 on error.3222*3223* \param pctx policy context.3224* \param algo_id hash function / algorithm identifier.3225* \param data input/output buffer (message/hash, then signature).3226* \param hv_len hash value or message length (in bytes).3227* \param len total buffer length (in bytes).3228* \return signature length (in bytes) on success, or 0 on error.3229*/3230size_t (*do_sign)(const br_ssl_server_policy_class **pctx,3231unsigned algo_id,3232unsigned char *data, size_t hv_len, size_t len);3233};32343235/**3236* \brief A single-chain RSA policy handler.3237*3238* This policy context uses a single certificate chain, and a RSA3239* private key. The context can be restricted to only signatures or3240* only key exchange.3241*3242* Apart from the first field (vtable pointer), its contents are3243* opaque and shall not be accessed directly.3244*/3245typedef struct {3246/** \brief Pointer to vtable. */3247const br_ssl_server_policy_class *vtable;3248#ifndef BR_DOXYGEN_IGNORE3249const br_x509_certificate *chain;3250size_t chain_len;3251const br_rsa_private_key *sk;3252unsigned allowed_usages;3253br_rsa_private irsacore;3254br_rsa_pkcs1_sign irsasign;3255#endif3256} br_ssl_server_policy_rsa_context;32573258/**3259* \brief A single-chain EC policy handler.3260*3261* This policy context uses a single certificate chain, and an EC3262* private key. The context can be restricted to only signatures or3263* only key exchange.3264*3265* Due to how TLS is defined, this context must be made aware whether3266* the server certificate was itself signed with RSA or ECDSA. The code3267* does not try to decode the certificate to obtain that information.3268*3269* Apart from the first field (vtable pointer), its contents are3270* opaque and shall not be accessed directly.3271*/3272typedef struct {3273/** \brief Pointer to vtable. */3274const br_ssl_server_policy_class *vtable;3275#ifndef BR_DOXYGEN_IGNORE3276const br_x509_certificate *chain;3277size_t chain_len;3278const br_ec_private_key *sk;3279unsigned allowed_usages;3280unsigned cert_issuer_key_type;3281const br_multihash_context *mhash;3282const br_ec_impl *iec;3283br_ecdsa_sign iecdsa;3284#endif3285} br_ssl_server_policy_ec_context;32863287/**3288* \brief Class type for a session parameter cache.3289*3290* Session parameters are saved in the cache with `save()`, and3291* retrieved with `load()`. The cache implementation can apply any3292* storage and eviction strategy that it sees fit. The SSL server3293* context that performs the request is provided, so that its3294* functionalities may be used by the implementation (e.g. hash3295* functions or random number generation).3296*/3297typedef struct br_ssl_session_cache_class_ br_ssl_session_cache_class;3298struct br_ssl_session_cache_class_ {3299/**3300* \brief Context size (in bytes).3301*/3302size_t context_size;33033304/**3305* \brief Record a session.3306*3307* This callback should record the provided session parameters.3308* The `params` structure is transient, so its contents shall3309* be copied into the cache. The session ID has been randomly3310* generated and always has length exactly 32 bytes.3311*3312* \param ctx session cache context.3313* \param server_ctx SSL server context.3314* \param params session parameters to save.3315*/3316void (*save)(const br_ssl_session_cache_class **ctx,3317br_ssl_server_context *server_ctx,3318const br_ssl_session_parameters *params);33193320/**3321* \brief Lookup a session in the cache.3322*3323* The session ID to lookup is in `params` and always has length3324* exactly 32 bytes. If the session parameters are found in the3325* cache, then the parameters shall be copied into the `params`3326* structure. Returned value is 1 on successful lookup, 03327* otherwise.3328*3329* \param ctx session cache context.3330* \param server_ctx SSL server context.3331* \param params destination for session parameters.3332* \return 1 if found, 0 otherwise.3333*/3334int (*load)(const br_ssl_session_cache_class **ctx,3335br_ssl_server_context *server_ctx,3336br_ssl_session_parameters *params);3337};33383339/**3340* \brief Context for a basic cache system.3341*3342* The system stores session parameters in a buffer provided at3343* initialisation time. Each entry uses exactly 100 bytes, and3344* buffer sizes up to 4294967295 bytes are supported.3345*3346* Entries are evicted with a LRU (Least Recently Used) policy. A3347* search tree is maintained to keep lookups fast even with large3348* caches.3349*3350* Apart from the first field (vtable pointer), the structure3351* contents are opaque and shall not be accessed directly.3352*/3353typedef struct {3354/** \brief Pointer to vtable. */3355const br_ssl_session_cache_class *vtable;3356#ifndef BR_DOXYGEN_IGNORE3357unsigned char *store;3358size_t store_len, store_ptr;3359unsigned char index_key[32];3360const br_hash_class *hash;3361int init_done;3362uint32_t head, tail, root;3363#endif3364} br_ssl_session_cache_lru;33653366/**3367* \brief Initialise a LRU session cache with the provided storage space.3368*3369* The provided storage space must remain valid as long as the cache3370* is used. Arbitrary lengths are supported, up to 4294967295 bytes;3371* each entry uses up exactly 100 bytes.3372*3373* \param cc session cache context.3374* \param store storage space for cached entries.3375* \param store_len storage space length (in bytes).3376*/3377void br_ssl_session_cache_lru_init(br_ssl_session_cache_lru *cc,3378unsigned char *store, size_t store_len);33793380/**3381* \brief Forget an entry in an LRU session cache.3382*3383* The session cache context must have been initialised. The entry3384* with the provided session ID (of exactly 32 bytes) is looked for3385* in the cache; if located, it is disabled.3386*3387* \param cc session cache context.3388* \param id session ID to forget.3389*/3390void br_ssl_session_cache_lru_forget(3391br_ssl_session_cache_lru *cc, const unsigned char *id);33923393/**3394* \brief Context structure for a SSL server.3395*3396* The first field (called `eng`) is the SSL engine; all functions that3397* work on a `br_ssl_engine_context` structure shall take as parameter3398* a pointer to that field. The other structure fields are opaque and3399* must not be accessed directly.3400*/3401struct br_ssl_server_context_ {3402/**3403* \brief The encapsulated engine context.3404*/3405br_ssl_engine_context eng;34063407#ifndef BR_DOXYGEN_IGNORE3408/*3409* Maximum version from the client.3410*/3411uint16_t client_max_version;34123413/*3414* Session cache.3415*/3416const br_ssl_session_cache_class **cache_vtable;34173418/*3419* Translated cipher suites supported by the client. The list3420* is trimmed to include only the cipher suites that the3421* server also supports; they are in the same order as in the3422* client message.3423*/3424br_suite_translated client_suites[BR_MAX_CIPHER_SUITES];3425unsigned char client_suites_num;34263427/*3428* Hash functions supported by the client, with ECDSA and RSA3429* (bit mask). For hash function with id 'x', set bit index is3430* x for RSA, x+8 for ECDSA. For newer algorithms, with ID3431* 0x08**, bit 16+k is set for algorithm 0x0800+k.3432*/3433uint32_t hashes;34343435/*3436* Curves supported by the client (bit mask, for named curves).3437*/3438uint32_t curves;34393440/*3441* Context for chain handler.3442*/3443const br_ssl_server_policy_class **policy_vtable;3444uint16_t sign_hash_id;34453446/*3447* For the core handlers, thus avoiding (in most cases) the3448* need for an externally provided policy context.3449*/3450union {3451const br_ssl_server_policy_class *vtable;3452br_ssl_server_policy_rsa_context single_rsa;3453br_ssl_server_policy_ec_context single_ec;3454} chain_handler;34553456/*3457* Buffer for the ECDHE private key.3458*/3459unsigned char ecdhe_key[70];3460size_t ecdhe_key_len;34613462/*3463* Trust anchor names for client authentication. "ta_names" and3464* "tas" cannot be both non-NULL.3465*/3466const br_x500_name *ta_names;3467const br_x509_trust_anchor *tas;3468size_t num_tas;3469size_t cur_dn_index;3470const unsigned char *cur_dn;3471size_t cur_dn_len;34723473/*3474* Buffer for the hash value computed over all handshake messages3475* prior to CertificateVerify, and identifier for the hash function.3476*/3477unsigned char hash_CV[64];3478size_t hash_CV_len;3479int hash_CV_id;34803481/*3482* Server-specific implementations.3483* (none for now)3484*/3485#endif3486};34873488/*3489* Each br_ssl_server_init_xxx() function sets the list of supported3490* cipher suites and used implementations, as specified by the profile3491* name 'xxx'. Defined profile names are:3492*3493* full_rsa all supported algorithm, server key type is RSA3494* full_ec all supported algorithm, server key type is EC3495* TODO: add other profiles3496*3497* Naming scheme for "minimal" profiles: min1233498*3499* -- character 1: key exchange3500* r = RSA3501* e = ECDHE_RSA3502* f = ECDHE_ECDSA3503* u = ECDH_RSA3504* v = ECDH_ECDSA3505* -- character 2: version / PRF3506* 0 = TLS 1.0 / 1.1 with MD5+SHA-13507* 2 = TLS 1.2 with SHA-2563508* 3 = TLS 1.2 with SHA-3843509* -- character 3: encryption3510* a = AES/CBC3511* d = 3DES/CBC3512* g = AES/GCM3513* c = ChaCha20+Poly13053514*/35153516/**3517* \brief SSL server profile: full_rsa.3518*3519* This function initialises the provided SSL server context with3520* all supported algorithms and cipher suites that rely on a RSA3521* key pair.3522*3523* \param cc server context to initialise.3524* \param chain server certificate chain.3525* \param chain_len certificate chain length (number of certificate).3526* \param sk RSA private key.3527*/3528void br_ssl_server_init_full_rsa(br_ssl_server_context *cc,3529const br_x509_certificate *chain, size_t chain_len,3530const br_rsa_private_key *sk);35313532/**3533* \brief SSL server profile: full_ec.3534*3535* This function initialises the provided SSL server context with3536* all supported algorithms and cipher suites that rely on an EC3537* key pair.3538*3539* The key type of the CA that issued the server's certificate must3540* be provided, since it matters for ECDH cipher suites (ECDH_RSA3541* suites require a RSA-powered CA). The key type is either3542* `BR_KEYTYPE_RSA` or `BR_KEYTYPE_EC`.3543*3544* \param cc server context to initialise.3545* \param chain server certificate chain.3546* \param chain_len chain length (number of certificates).3547* \param cert_issuer_key_type certificate issuer's key type.3548* \param sk EC private key.3549*/3550void br_ssl_server_init_full_ec(br_ssl_server_context *cc,3551const br_x509_certificate *chain, size_t chain_len,3552unsigned cert_issuer_key_type, const br_ec_private_key *sk);35533554/**3555* \brief SSL server profile: minr2g.3556*3557* This profile uses only TLS_RSA_WITH_AES_128_GCM_SHA256. Server key is3558* RSA, and RSA key exchange is used (not forward secure, but uses little3559* CPU in the client).3560*3561* \param cc server context to initialise.3562* \param chain server certificate chain.3563* \param chain_len certificate chain length (number of certificate).3564* \param sk RSA private key.3565*/3566void br_ssl_server_init_minr2g(br_ssl_server_context *cc,3567const br_x509_certificate *chain, size_t chain_len,3568const br_rsa_private_key *sk);35693570/**3571* \brief SSL server profile: mine2g.3572*3573* This profile uses only TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256. Server key3574* is RSA, and ECDHE key exchange is used. This suite provides forward3575* security, with a higher CPU expense on the client, and a somewhat3576* larger code footprint (compared to "minr2g").3577*3578* \param cc server context to initialise.3579* \param chain server certificate chain.3580* \param chain_len certificate chain length (number of certificate).3581* \param sk RSA private key.3582*/3583void br_ssl_server_init_mine2g(br_ssl_server_context *cc,3584const br_x509_certificate *chain, size_t chain_len,3585const br_rsa_private_key *sk);35863587/**3588* \brief SSL server profile: minf2g.3589*3590* This profile uses only TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256.3591* Server key is EC, and ECDHE key exchange is used. This suite provides3592* forward security, with a higher CPU expense on the client and server3593* (by a factor of about 3 to 4), and a somewhat larger code footprint3594* (compared to "minu2g" and "minv2g").3595*3596* \param cc server context to initialise.3597* \param chain server certificate chain.3598* \param chain_len certificate chain length (number of certificate).3599* \param sk EC private key.3600*/3601void br_ssl_server_init_minf2g(br_ssl_server_context *cc,3602const br_x509_certificate *chain, size_t chain_len,3603const br_ec_private_key *sk);36043605/**3606* \brief SSL server profile: minu2g.3607*3608* This profile uses only TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256.3609* Server key is EC, and ECDH key exchange is used; the issuing CA used3610* a RSA key.3611*3612* The "minu2g" and "minv2g" profiles do not provide forward secrecy,3613* but are the lightest on the server (for CPU usage), and are rather3614* inexpensive on the client as well.3615*3616* \param cc server context to initialise.3617* \param chain server certificate chain.3618* \param chain_len certificate chain length (number of certificate).3619* \param sk EC private key.3620*/3621void br_ssl_server_init_minu2g(br_ssl_server_context *cc,3622const br_x509_certificate *chain, size_t chain_len,3623const br_ec_private_key *sk);36243625/**3626* \brief SSL server profile: minv2g.3627*3628* This profile uses only TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256.3629* Server key is EC, and ECDH key exchange is used; the issuing CA used3630* an EC key.3631*3632* The "minu2g" and "minv2g" profiles do not provide forward secrecy,3633* but are the lightest on the server (for CPU usage), and are rather3634* inexpensive on the client as well.3635*3636* \param cc server context to initialise.3637* \param chain server certificate chain.3638* \param chain_len certificate chain length (number of certificate).3639* \param sk EC private key.3640*/3641void br_ssl_server_init_minv2g(br_ssl_server_context *cc,3642const br_x509_certificate *chain, size_t chain_len,3643const br_ec_private_key *sk);36443645/**3646* \brief SSL server profile: mine2c.3647*3648* This profile uses only TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256.3649* Server key is RSA, and ECDHE key exchange is used. This suite3650* provides forward security.3651*3652* \param cc server context to initialise.3653* \param chain server certificate chain.3654* \param chain_len certificate chain length (number of certificate).3655* \param sk RSA private key.3656*/3657void br_ssl_server_init_mine2c(br_ssl_server_context *cc,3658const br_x509_certificate *chain, size_t chain_len,3659const br_rsa_private_key *sk);36603661/**3662* \brief SSL server profile: minf2c.3663*3664* This profile uses only TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256.3665* Server key is EC, and ECDHE key exchange is used. This suite provides3666* forward security.3667*3668* \param cc server context to initialise.3669* \param chain server certificate chain.3670* \param chain_len certificate chain length (number of certificate).3671* \param sk EC private key.3672*/3673void br_ssl_server_init_minf2c(br_ssl_server_context *cc,3674const br_x509_certificate *chain, size_t chain_len,3675const br_ec_private_key *sk);36763677/**3678* \brief Get the supported client suites.3679*3680* This function shall be called only after the ClientHello has been3681* processed, typically from the policy engine. The returned array3682* contains the cipher suites that are supported by both the client3683* and the server; these suites are in client preference order, unless3684* the `BR_OPT_ENFORCE_SERVER_PREFERENCES` flag was set, in which case3685* they are in server preference order.3686*3687* The suites are _translated_, which means that each suite is given3688* as two 16-bit integers: the standard suite identifier, and its3689* translated version, broken down into its individual components,3690* as explained with the `br_suite_translated` type.3691*3692* The returned array is allocated in the context and will be rewritten3693* by each handshake.3694*3695* \param cc server context.3696* \param num receives the array size (number of suites).3697* \return the translated common cipher suites, in preference order.3698*/3699static inline const br_suite_translated *3700br_ssl_server_get_client_suites(const br_ssl_server_context *cc, size_t *num)3701{3702*num = cc->client_suites_num;3703return cc->client_suites;3704}37053706/**3707* \brief Get the hash functions and signature algorithms supported by3708* the client.3709*3710* This value is a bit field:3711*3712* - If RSA (PKCS#1 v1.5) is supported with hash function of ID `x`,3713* then bit `x` is set (hash function ID is 0 for the special MD5+SHA-1,3714* or 2 to 6 for the SHA family).3715*3716* - If ECDSA is supported with hash function of ID `x`, then bit `8+x`3717* is set.3718*3719* - Newer algorithms are symbolic 16-bit identifiers that do not3720* represent signature algorithm and hash function separately. If3721* the TLS-level identifier is `0x0800+x` for a `x` in the 0..153722* range, then bit `16+x` is set.3723*3724* "New algorithms" are currently defined only in draft documents, so3725* this support is subject to possible change. Right now (early 2017),3726* this maps ed25519 (EdDSA on Curve25519) to bit 23, and ed448 (EdDSA3727* on Curve448) to bit 24. If the identifiers on the wire change in3728* future document, then the decoding mechanism in BearSSL will be3729* amended to keep mapping ed25519 and ed448 on bits 23 and 24,3730* respectively. Mapping of other new algorithms (e.g. RSA/PSS) is not3731* guaranteed yet.3732*3733* \param cc server context.3734* \return the client-supported hash functions and signature algorithms.3735*/3736static inline uint32_t3737br_ssl_server_get_client_hashes(const br_ssl_server_context *cc)3738{3739return cc->hashes;3740}37413742/**3743* \brief Get the elliptic curves supported by the client.3744*3745* This is a bit field (bit x is set if curve of ID x is supported).3746*3747* \param cc server context.3748* \return the client-supported elliptic curves.3749*/3750static inline uint32_t3751br_ssl_server_get_client_curves(const br_ssl_server_context *cc)3752{3753return cc->curves;3754}37553756/**3757* \brief Clear the complete contents of a SSL server context.3758*3759* Everything is cleared, including the reference to the configured buffer,3760* implementations, cipher suites and state. This is a preparatory step3761* to assembling a custom profile.3762*3763* \param cc server context to clear.3764*/3765void br_ssl_server_zero(br_ssl_server_context *cc);37663767/**3768* \brief Set an externally provided policy context.3769*3770* The policy context's methods are invoked to decide the cipher suite3771* and certificate chain, and to perform operations involving the server's3772* private key.3773*3774* \param cc server context.3775* \param pctx policy context (pointer to its vtable field).3776*/3777static inline void3778br_ssl_server_set_policy(br_ssl_server_context *cc,3779const br_ssl_server_policy_class **pctx)3780{3781cc->policy_vtable = pctx;3782}37833784/**3785* \brief Set the server certificate chain and key (single RSA case).3786*3787* This function uses a policy context included in the server context.3788* It configures use of a single server certificate chain with a RSA3789* private key. The `allowed_usages` is a combination of usages, namely3790* `BR_KEYTYPE_KEYX` and/or `BR_KEYTYPE_SIGN`; this enables or disables3791* the corresponding cipher suites (i.e. `TLS_RSA_*` use the RSA key for3792* key exchange, while `TLS_ECDHE_RSA_*` use the RSA key for signatures).3793*3794* \param cc server context.3795* \param chain server certificate chain to send to the client.3796* \param chain_len chain length (number of certificates).3797* \param sk server private key (RSA).3798* \param allowed_usages allowed private key usages.3799* \param irsacore RSA core implementation.3800* \param irsasign RSA signature implementation (PKCS#1 v1.5).3801*/3802void br_ssl_server_set_single_rsa(br_ssl_server_context *cc,3803const br_x509_certificate *chain, size_t chain_len,3804const br_rsa_private_key *sk, unsigned allowed_usages,3805br_rsa_private irsacore, br_rsa_pkcs1_sign irsasign);38063807/**3808* \brief Set the server certificate chain and key (single EC case).3809*3810* This function uses a policy context included in the server context.3811* It configures use of a single server certificate chain with an EC3812* private key. The `allowed_usages` is a combination of usages, namely3813* `BR_KEYTYPE_KEYX` and/or `BR_KEYTYPE_SIGN`; this enables or disables3814* the corresponding cipher suites (i.e. `TLS_ECDH_*` use the EC key for3815* key exchange, while `TLS_ECDHE_ECDSA_*` use the EC key for signatures).3816*3817* In order to support `TLS_ECDH_*` cipher suites (non-ephemeral ECDH),3818* the algorithm type of the key used by the issuing CA to sign the3819* server's certificate must be provided, as `cert_issuer_key_type`3820* parameter (this value is either `BR_KEYTYPE_RSA` or `BR_KEYTYPE_EC`).3821*3822* \param cc server context.3823* \param chain server certificate chain to send.3824* \param chain_len chain length (number of certificates).3825* \param sk server private key (EC).3826* \param allowed_usages allowed private key usages.3827* \param cert_issuer_key_type issuing CA's key type.3828* \param iec EC core implementation.3829* \param iecdsa ECDSA signature implementation ("asn1" format).3830*/3831void br_ssl_server_set_single_ec(br_ssl_server_context *cc,3832const br_x509_certificate *chain, size_t chain_len,3833const br_ec_private_key *sk, unsigned allowed_usages,3834unsigned cert_issuer_key_type,3835const br_ec_impl *iec, br_ecdsa_sign iecdsa);38363837/**3838* \brief Activate client certificate authentication.3839*3840* The trust anchor encoded X.500 names (DN) to send to the client are3841* provided. A client certificate will be requested and validated through3842* the X.509 validator configured in the SSL engine. If `num` is 0, then3843* client certificate authentication is disabled.3844*3845* If the client does not send a certificate, or on validation failure,3846* the handshake aborts. Unauthenticated clients can be tolerated by3847* setting the `BR_OPT_TOLERATE_NO_CLIENT_AUTH` flag.3848*3849* The provided array is linked in, not copied, so that pointer must3850* remain valid as long as anchor names may be used.3851*3852* \param cc server context.3853* \param ta_names encoded trust anchor names.3854* \param num number of encoded trust anchor names.3855*/3856static inline void3857br_ssl_server_set_trust_anchor_names(br_ssl_server_context *cc,3858const br_x500_name *ta_names, size_t num)3859{3860cc->ta_names = ta_names;3861cc->tas = NULL;3862cc->num_tas = num;3863}38643865/**3866* \brief Activate client certificate authentication.3867*3868* This is a variant for `br_ssl_server_set_trust_anchor_names()`: the3869* trust anchor names are provided not as an array of stand-alone names3870* (`br_x500_name` structures), but as an array of trust anchors3871* (`br_x509_trust_anchor` structures). The server engine itself will3872* only use the `dn` field of each trust anchor. This is meant to allow3873* defining a single array of trust anchors, to be used here and in the3874* X.509 validation engine itself.3875*3876* The provided array is linked in, not copied, so that pointer must3877* remain valid as long as anchor names may be used.3878*3879* \param cc server context.3880* \param tas trust anchors (only names are used).3881* \param num number of trust anchors.3882*/3883static inline void3884br_ssl_server_set_trust_anchor_names_alt(br_ssl_server_context *cc,3885const br_x509_trust_anchor *tas, size_t num)3886{3887cc->ta_names = NULL;3888cc->tas = tas;3889cc->num_tas = num;3890}38913892/**3893* \brief Configure the cache for session parameters.3894*3895* The cache context is provided as a pointer to its first field (vtable3896* pointer).3897*3898* \param cc server context.3899* \param vtable session cache context.3900*/3901static inline void3902br_ssl_server_set_cache(br_ssl_server_context *cc,3903const br_ssl_session_cache_class **vtable)3904{3905cc->cache_vtable = vtable;3906}39073908/**3909* \brief Prepare or reset a server context for handling an incoming client.3910*3911* \param cc server context.3912* \return 1 on success, 0 on error.3913*/3914int br_ssl_server_reset(br_ssl_server_context *cc);39153916/* ===================================================================== */39173918/*3919* Context for the simplified I/O context. The transport medium is accessed3920* through the low_read() and low_write() callback functions, each with3921* its own opaque context pointer.3922*3923* low_read() read some bytes, at most 'len' bytes, into data[]. The3924* returned value is the number of read bytes, or -1 on error.3925* The 'len' parameter is guaranteed never to exceed 20000,3926* so the length always fits in an 'int' on all platforms.3927*3928* low_write() write up to 'len' bytes, to be read from data[]. The3929* returned value is the number of written bytes, or -1 on3930* error. The 'len' parameter is guaranteed never to exceed3931* 20000, so the length always fits in an 'int' on all3932* parameters.3933*3934* A socket closure (if the transport medium is a socket) should be reported3935* as an error (-1). The callbacks shall endeavour to block until at least3936* one byte can be read or written; a callback returning 0 at times is3937* acceptable, but this normally leads to the callback being immediately3938* called again, so the callback should at least always try to block for3939* some time if no I/O can take place.3940*3941* The SSL engine naturally applies some buffering, so the callbacks need3942* not apply buffers of their own.3943*/3944/**3945* \brief Context structure for the simplified SSL I/O wrapper.3946*3947* This structure is initialised with `br_sslio_init()`. Its contents3948* are opaque and shall not be accessed directly.3949*/3950typedef struct {3951#ifndef BR_DOXYGEN_IGNORE3952br_ssl_engine_context *engine;3953int (*low_read)(void *read_context,3954unsigned char *data, size_t len);3955void *read_context;3956int (*low_write)(void *write_context,3957const unsigned char *data, size_t len);3958void *write_context;3959#endif3960} br_sslio_context;39613962/**3963* \brief Initialise a simplified I/O wrapper context.3964*3965* The simplified I/O wrapper offers a simpler read/write API for a SSL3966* engine (client or server), using the provided callback functions for3967* reading data from, or writing data to, the transport medium.3968*3969* The callback functions have the following semantics:3970*3971* - Each callback receives an opaque context value (of type `void *`)3972* that the callback may use arbitrarily (or possibly ignore).3973*3974* - `low_read()` reads at least one byte, at most `len` bytes, from3975* the transport medium. Read bytes shall be written in `data`.3976*3977* - `low_write()` writes at least one byte, at most `len` bytes, unto3978* the transport medium. The bytes to write are read from `data`.3979*3980* - The `len` parameter is never zero, and is always lower than 20000.3981*3982* - The number of processed bytes (read or written) is returned. Since3983* that number is less than 20000, it always fits on an `int`.3984*3985* - On error, the callbacks return -1. Reaching end-of-stream is an3986* error. Errors are permanent: the SSL connection is terminated.3987*3988* - Callbacks SHOULD NOT return 0. This is tolerated, as long as3989* callbacks endeavour to block for some non-negligible amount of3990* time until at least one byte can be sent or received (if a3991* callback returns 0, then the wrapper invokes it again3992* immediately).3993*3994* - Callbacks MAY return as soon as at least one byte is processed;3995* they MAY also insist on reading or writing _all_ requested bytes.3996* Since SSL is a self-terminated protocol (each record has a length3997* header), this does not change semantics.3998*3999* - Callbacks need not apply any buffering (for performance) since SSL4000* itself uses buffers.4001*4002* \param ctx wrapper context to initialise.4003* \param engine SSL engine to wrap.4004* \param low_read callback for reading data from the transport.4005* \param read_context context pointer for `low_read()`.4006* \param low_write callback for writing data on the transport.4007* \param write_context context pointer for `low_write()`.4008*/4009void br_sslio_init(br_sslio_context *ctx,4010br_ssl_engine_context *engine,4011int (*low_read)(void *read_context,4012unsigned char *data, size_t len),4013void *read_context,4014int (*low_write)(void *write_context,4015const unsigned char *data, size_t len),4016void *write_context);40174018/**4019* \brief Read some application data from a SSL connection.4020*4021* If `len` is zero, then this function returns 0 immediately. In4022* all other cases, it never returns 0.4023*4024* This call returns only when at least one byte has been obtained.4025* Returned value is the number of bytes read, or -1 on error. The4026* number of bytes always fits on an 'int' (data from a single SSL/TLS4027* record is returned).4028*4029* On error or SSL closure, this function returns -1. The caller should4030* inspect the error status on the SSL engine to distinguish between4031* normal closure and error.4032*4033* \param cc SSL wrapper context.4034* \param dst destination buffer for application data.4035* \param len maximum number of bytes to obtain.4036* \return number of bytes obtained, or -1 on error.4037*/4038int br_sslio_read(br_sslio_context *cc, void *dst, size_t len);40394040/**4041* \brief Read application data from a SSL connection.4042*4043* This calls returns only when _all_ requested `len` bytes are read,4044* or an error is reached. Returned value is 0 on success, -1 on error.4045* A normal (verified) SSL closure before that many bytes are obtained4046* is reported as an error by this function.4047*4048* \param cc SSL wrapper context.4049* \param dst destination buffer for application data.4050* \param len number of bytes to obtain.4051* \return 0 on success, or -1 on error.4052*/4053int br_sslio_read_all(br_sslio_context *cc, void *dst, size_t len);40544055/**4056* \brief Write some application data unto a SSL connection.4057*4058* If `len` is zero, then this function returns 0 immediately. In4059* all other cases, it never returns 0.4060*4061* This call returns only when at least one byte has been written.4062* Returned value is the number of bytes written, or -1 on error. The4063* number of bytes always fits on an 'int' (less than 20000).4064*4065* On error or SSL closure, this function returns -1. The caller should4066* inspect the error status on the SSL engine to distinguish between4067* normal closure and error.4068*4069* **Important:** SSL is buffered; a "written" byte is a byte that was4070* injected into the wrapped SSL engine, but this does not necessarily mean4071* that it has been scheduled for sending. Use `br_sslio_flush()` to4072* ensure that all pending data has been sent to the transport medium.4073*4074* \param cc SSL wrapper context.4075* \param src source buffer for application data.4076* \param len maximum number of bytes to write.4077* \return number of bytes written, or -1 on error.4078*/4079int br_sslio_write(br_sslio_context *cc, const void *src, size_t len);40804081/**4082* \brief Write application data unto a SSL connection.4083*4084* This calls returns only when _all_ requested `len` bytes have been4085* written, or an error is reached. Returned value is 0 on success, -14086* on error. A normal (verified) SSL closure before that many bytes are4087* written is reported as an error by this function.4088*4089* **Important:** SSL is buffered; a "written" byte is a byte that was4090* injected into the wrapped SSL engine, but this does not necessarily mean4091* that it has been scheduled for sending. Use `br_sslio_flush()` to4092* ensure that all pending data has been sent to the transport medium.4093*4094* \param cc SSL wrapper context.4095* \param src source buffer for application data.4096* \param len number of bytes to write.4097* \return 0 on success, or -1 on error.4098*/4099int br_sslio_write_all(br_sslio_context *cc, const void *src, size_t len);41004101/**4102* \brief Flush pending data.4103*4104* This call makes sure that any buffered application data in the4105* provided context (including the wrapped SSL engine) has been sent4106* to the transport medium (i.e. accepted by the `low_write()` callback4107* method). If there is no such pending data, then this function does4108* nothing (and returns a success, i.e. 0).4109*4110* If the underlying transport medium has its own buffers, then it is4111* up to the caller to ensure the corresponding flushing.4112*4113* Returned value is 0 on success, -1 on error.4114*4115* \param cc SSL wrapper context.4116* \return 0 on success, or -1 on error.4117*/4118int br_sslio_flush(br_sslio_context *cc);41194120/**4121* \brief Close the SSL connection.4122*4123* This call runs the SSL closure protocol (sending a `close_notify`,4124* receiving the response `close_notify`). When it returns, the SSL4125* connection is finished. It is still up to the caller to manage the4126* possible transport-level termination, if applicable (alternatively,4127* the underlying transport stream may be reused for non-SSL messages).4128*4129* Returned value is 0 on success, -1 on error. A failure by the peer4130* to process the complete closure protocol (i.e. sending back the4131* `close_notify`) is an error.4132*4133* \param cc SSL wrapper context.4134* \return 0 on success, or -1 on error.4135*/4136int br_sslio_close(br_sslio_context *cc);41374138/* ===================================================================== */41394140/*4141* Symbolic constants for cipher suites.4142*/41434144/* From RFC 5246 */4145#define BR_TLS_NULL_WITH_NULL_NULL 0x00004146#define BR_TLS_RSA_WITH_NULL_MD5 0x00014147#define BR_TLS_RSA_WITH_NULL_SHA 0x00024148#define BR_TLS_RSA_WITH_NULL_SHA256 0x003B4149#define BR_TLS_RSA_WITH_RC4_128_MD5 0x00044150#define BR_TLS_RSA_WITH_RC4_128_SHA 0x00054151#define BR_TLS_RSA_WITH_3DES_EDE_CBC_SHA 0x000A4152#define BR_TLS_RSA_WITH_AES_128_CBC_SHA 0x002F4153#define BR_TLS_RSA_WITH_AES_256_CBC_SHA 0x00354154#define BR_TLS_RSA_WITH_AES_128_CBC_SHA256 0x003C4155#define BR_TLS_RSA_WITH_AES_256_CBC_SHA256 0x003D4156#define BR_TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA 0x000D4157#define BR_TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA 0x00104158#define BR_TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA 0x00134159#define BR_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA 0x00164160#define BR_TLS_DH_DSS_WITH_AES_128_CBC_SHA 0x00304161#define BR_TLS_DH_RSA_WITH_AES_128_CBC_SHA 0x00314162#define BR_TLS_DHE_DSS_WITH_AES_128_CBC_SHA 0x00324163#define BR_TLS_DHE_RSA_WITH_AES_128_CBC_SHA 0x00334164#define BR_TLS_DH_DSS_WITH_AES_256_CBC_SHA 0x00364165#define BR_TLS_DH_RSA_WITH_AES_256_CBC_SHA 0x00374166#define BR_TLS_DHE_DSS_WITH_AES_256_CBC_SHA 0x00384167#define BR_TLS_DHE_RSA_WITH_AES_256_CBC_SHA 0x00394168#define BR_TLS_DH_DSS_WITH_AES_128_CBC_SHA256 0x003E4169#define BR_TLS_DH_RSA_WITH_AES_128_CBC_SHA256 0x003F4170#define BR_TLS_DHE_DSS_WITH_AES_128_CBC_SHA256 0x00404171#define BR_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 0x00674172#define BR_TLS_DH_DSS_WITH_AES_256_CBC_SHA256 0x00684173#define BR_TLS_DH_RSA_WITH_AES_256_CBC_SHA256 0x00694174#define BR_TLS_DHE_DSS_WITH_AES_256_CBC_SHA256 0x006A4175#define BR_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 0x006B4176#define BR_TLS_DH_anon_WITH_RC4_128_MD5 0x00184177#define BR_TLS_DH_anon_WITH_3DES_EDE_CBC_SHA 0x001B4178#define BR_TLS_DH_anon_WITH_AES_128_CBC_SHA 0x00344179#define BR_TLS_DH_anon_WITH_AES_256_CBC_SHA 0x003A4180#define BR_TLS_DH_anon_WITH_AES_128_CBC_SHA256 0x006C4181#define BR_TLS_DH_anon_WITH_AES_256_CBC_SHA256 0x006D41824183/* From RFC 4492 */4184#define BR_TLS_ECDH_ECDSA_WITH_NULL_SHA 0xC0014185#define BR_TLS_ECDH_ECDSA_WITH_RC4_128_SHA 0xC0024186#define BR_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA 0xC0034187#define BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA 0xC0044188#define BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA 0xC0054189#define BR_TLS_ECDHE_ECDSA_WITH_NULL_SHA 0xC0064190#define BR_TLS_ECDHE_ECDSA_WITH_RC4_128_SHA 0xC0074191#define BR_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA 0xC0084192#define BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA 0xC0094193#define BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA 0xC00A4194#define BR_TLS_ECDH_RSA_WITH_NULL_SHA 0xC00B4195#define BR_TLS_ECDH_RSA_WITH_RC4_128_SHA 0xC00C4196#define BR_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA 0xC00D4197#define BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA 0xC00E4198#define BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA 0xC00F4199#define BR_TLS_ECDHE_RSA_WITH_NULL_SHA 0xC0104200#define BR_TLS_ECDHE_RSA_WITH_RC4_128_SHA 0xC0114201#define BR_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA 0xC0124202#define BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA 0xC0134203#define BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA 0xC0144204#define BR_TLS_ECDH_anon_WITH_NULL_SHA 0xC0154205#define BR_TLS_ECDH_anon_WITH_RC4_128_SHA 0xC0164206#define BR_TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA 0xC0174207#define BR_TLS_ECDH_anon_WITH_AES_128_CBC_SHA 0xC0184208#define BR_TLS_ECDH_anon_WITH_AES_256_CBC_SHA 0xC01942094210/* From RFC 5288 */4211#define BR_TLS_RSA_WITH_AES_128_GCM_SHA256 0x009C4212#define BR_TLS_RSA_WITH_AES_256_GCM_SHA384 0x009D4213#define BR_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 0x009E4214#define BR_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 0x009F4215#define BR_TLS_DH_RSA_WITH_AES_128_GCM_SHA256 0x00A04216#define BR_TLS_DH_RSA_WITH_AES_256_GCM_SHA384 0x00A14217#define BR_TLS_DHE_DSS_WITH_AES_128_GCM_SHA256 0x00A24218#define BR_TLS_DHE_DSS_WITH_AES_256_GCM_SHA384 0x00A34219#define BR_TLS_DH_DSS_WITH_AES_128_GCM_SHA256 0x00A44220#define BR_TLS_DH_DSS_WITH_AES_256_GCM_SHA384 0x00A54221#define BR_TLS_DH_anon_WITH_AES_128_GCM_SHA256 0x00A64222#define BR_TLS_DH_anon_WITH_AES_256_GCM_SHA384 0x00A742234224/* From RFC 5289 */4225#define BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 0xC0234226#define BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 0xC0244227#define BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 0xC0254228#define BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 0xC0264229#define BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 0xC0274230#define BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 0xC0284231#define BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 0xC0294232#define BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 0xC02A4233#define BR_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 0xC02B4234#define BR_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 0xC02C4235#define BR_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 0xC02D4236#define BR_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 0xC02E4237#define BR_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 0xC02F4238#define BR_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 0xC0304239#define BR_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 0xC0314240#define BR_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 0xC03242414242/* From RFC 6655 and 7251 */4243#define BR_TLS_RSA_WITH_AES_128_CCM 0xC09C4244#define BR_TLS_RSA_WITH_AES_256_CCM 0xC09D4245#define BR_TLS_RSA_WITH_AES_128_CCM_8 0xC0A04246#define BR_TLS_RSA_WITH_AES_256_CCM_8 0xC0A14247#define BR_TLS_ECDHE_ECDSA_WITH_AES_128_CCM 0xC0AC4248#define BR_TLS_ECDHE_ECDSA_WITH_AES_256_CCM 0xC0AD4249#define BR_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 0xC0AE4250#define BR_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 0xC0AF42514252/* From RFC 7905 */4253#define BR_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 0xCCA84254#define BR_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 0xCCA94255#define BR_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 0xCCAA4256#define BR_TLS_PSK_WITH_CHACHA20_POLY1305_SHA256 0xCCAB4257#define BR_TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 0xCCAC4258#define BR_TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 0xCCAD4259#define BR_TLS_RSA_PSK_WITH_CHACHA20_POLY1305_SHA256 0xCCAE42604261/* From RFC 7507 */4262#define BR_TLS_FALLBACK_SCSV 0x560042634264/*4265* Symbolic constants for alerts.4266*/4267#define BR_ALERT_CLOSE_NOTIFY 04268#define BR_ALERT_UNEXPECTED_MESSAGE 104269#define BR_ALERT_BAD_RECORD_MAC 204270#define BR_ALERT_RECORD_OVERFLOW 224271#define BR_ALERT_DECOMPRESSION_FAILURE 304272#define BR_ALERT_HANDSHAKE_FAILURE 404273#define BR_ALERT_BAD_CERTIFICATE 424274#define BR_ALERT_UNSUPPORTED_CERTIFICATE 434275#define BR_ALERT_CERTIFICATE_REVOKED 444276#define BR_ALERT_CERTIFICATE_EXPIRED 454277#define BR_ALERT_CERTIFICATE_UNKNOWN 464278#define BR_ALERT_ILLEGAL_PARAMETER 474279#define BR_ALERT_UNKNOWN_CA 484280#define BR_ALERT_ACCESS_DENIED 494281#define BR_ALERT_DECODE_ERROR 504282#define BR_ALERT_DECRYPT_ERROR 514283#define BR_ALERT_PROTOCOL_VERSION 704284#define BR_ALERT_INSUFFICIENT_SECURITY 714285#define BR_ALERT_INTERNAL_ERROR 804286#define BR_ALERT_USER_CANCELED 904287#define BR_ALERT_NO_RENEGOTIATION 1004288#define BR_ALERT_UNSUPPORTED_EXTENSION 1104289#define BR_ALERT_NO_APPLICATION_PROTOCOL 12042904291#ifdef __cplusplus4292}4293#endif42944295#endif429642974298