2 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include <openssl/bio.h>
11 #include <openssl/ssl.h>
12 #include <openssl/err.h>
13 #include "../../ssl_local.h"
14 #include "../record_local.h"
16 typedef struct dtls_bitmap_st {
17 /* Track 64 packets */
19 /* Max record number seen so far, 64-bit value in big-endian encoding */
20 unsigned char max_seq_num[SEQ_NUM_SIZE];
23 /* Protocol version specific function pointers */
24 struct record_functions_st
27 * Returns either OSSL_RECORD_RETURN_SUCCESS, OSSL_RECORD_RETURN_FATAL or
28 * OSSL_RECORD_RETURN_NON_FATAL_ERR if we can keep trying to find an
29 * alternative record layer.
31 int (*set_crypto_state)(OSSL_RECORD_LAYER *rl, int level,
32 unsigned char *key, size_t keylen,
33 unsigned char *iv, size_t ivlen,
34 unsigned char *mackey, size_t mackeylen,
35 const EVP_CIPHER *ciph,
39 const SSL_COMP *comp);
41 int (*read_n)(OSSL_RECORD_LAYER *rl, size_t n, size_t max, int extend,
42 int clearold, size_t *readbytes);
44 int (*get_more_records)(OSSL_RECORD_LAYER *rl);
48 * 0: if the record is publicly invalid, or an internal error, or AEAD
49 * decryption failed, or EtM decryption failed.
50 * 1: Success or MtE decryption failed (MAC will be randomised)
52 int (*cipher)(OSSL_RECORD_LAYER *rl, SSL3_RECORD *recs, size_t n_recs,
53 int sending, SSL_MAC_BUF *macs, size_t macsize);
54 /* Returns 1 for success or 0 for error */
55 int (*mac)(OSSL_RECORD_LAYER *rl, SSL3_RECORD *rec, unsigned char *md,
58 /* Return 1 for success or 0 for error */
59 int (*set_protocol_version)(OSSL_RECORD_LAYER *rl, int version);
61 /* Return 1 for success or 0 for error */
62 int (*validate_record_header)(OSSL_RECORD_LAYER *rl, SSL3_RECORD *rec);
64 /* Return 1 for success or 0 for error */
65 int (*post_process_record)(OSSL_RECORD_LAYER *rl, SSL3_RECORD *rec);
68 struct ossl_record_layer_st
81 * A BIO containing any data read in the previous epoch that was destined
86 /* The transport BIO */
90 * A BIO where we will send any data read by us that is destined for the
95 /* Types match the equivalent fields in the SSL object */
99 /* write IO goes into here */
100 SSL3_BUFFER wbuf[SSL_MAX_PIPELINES + 1];
102 /* Next wbuf with pending data still to write */
105 /* How many pipelines can be used to write data */
108 /* read IO goes into here */
110 /* each decoded record goes in here */
111 SSL3_RECORD rrec[SSL_MAX_PIPELINES];
113 /* How many records have we got available in the rrec bufer */
116 /* The record number in the rrec buffer that can be read next */
119 /* The number of records that have been released via tls_release_record */
122 /* where we are when reading */
125 /* used internally to point at a raw packet */
126 unsigned char *packet;
127 size_t packet_length;
129 /* Sequence number for the next record */
130 unsigned char sequence[SEQ_NUM_SIZE];
135 * Read as many input bytes as possible (for non-blocking reads)
139 /* The number of consecutive empty records we have received */
140 size_t empty_record_count;
142 /* cryptographic state */
143 EVP_CIPHER_CTX *enc_ctx;
145 /* used for mac generation */
151 /* Set to 1 if this is the first handshake. 0 otherwise */
152 int is_first_handshake;
154 /* The negotiated maximum fragment length */
155 unsigned int max_frag_len;
157 /* The maxium amount of early data we can receive/send */
158 uint32_t max_early_data;
160 /* The amount of early data that we have sent/received */
161 size_t early_data_count;
163 /* Only used by SSLv3 */
164 unsigned char mac_secret[EVP_MAX_MD_SIZE];
166 /* TLSv1.0/TLSv1.1/TLSv1.2 */
169 /* Flags for GOST ciphers */
175 unsigned char iv[EVP_MAX_IV_LENGTH];
177 unsigned char read_iv[EVP_MAX_IV_LENGTH];
178 int allow_plain_alerts;
180 /* TLS "any" fields */
181 /* Set to true if this is the first record in a connection */
182 unsigned int is_first_record;
186 /* DTLS received handshake records (processed and unprocessed) */
187 record_pqueue unprocessed_rcds;
188 record_pqueue processed_rcds;
190 /* records being received in the current epoch */
192 /* renegotiation starts a new set of sequence numbers */
193 DTLS_BITMAP next_bitmap;
196 * Whether we are currently in a hanshake or not. Only maintained for DTLS
202 OSSL_FUNC_rlayer_skip_early_data_fn *skip_early_data;
203 OSSL_FUNC_rlayer_msg_callback_fn *msg_callback;
204 OSSL_FUNC_rlayer_security_fn *security;
206 size_t max_pipelines;
208 /* Function pointers for version specific functions */
209 struct record_functions_st *funcs;
212 typedef struct dtls_rlayer_record_data_st {
213 unsigned char *packet;
214 size_t packet_length;
217 } DTLS_RLAYER_RECORD_DATA;
219 extern struct record_functions_st ssl_3_0_funcs;
220 extern struct record_functions_st tls_1_funcs;
221 extern struct record_functions_st tls_1_3_funcs;
222 extern struct record_functions_st tls_any_funcs;
223 extern struct record_functions_st dtls_1_funcs;
224 extern struct record_functions_st dtls_any_funcs;
226 void ossl_rlayer_fatal(OSSL_RECORD_LAYER *rl, int al, int reason,
227 const char *fmt, ...);
229 #define RLAYERfatal(rl, al, r) RLAYERfatal_data((rl), (al), (r), NULL)
230 #define RLAYERfatal_data \
232 ERR_set_debug(OPENSSL_FILE, OPENSSL_LINE, OPENSSL_FUNC), \
235 #define RLAYER_USE_EXPLICIT_IV(rl) ((rl)->version == TLS1_1_VERSION \
236 || (rl)->version == TLS1_2_VERSION \
239 int ossl_set_tls_provider_parameters(OSSL_RECORD_LAYER *rl,
241 const EVP_CIPHER *ciph,
244 __owur char ssl3_cbc_record_digest_supported(const EVP_MD_CTX *ctx);
245 __owur int ssl3_cbc_digest_record(const EVP_MD *md,
246 unsigned char *md_out,
248 const unsigned char *header,
249 const unsigned char *data,
251 size_t data_plus_mac_plus_padding_size,
252 const unsigned char *mac_secret,
253 size_t mac_secret_length, char is_sslv3);
255 int tls_default_read_n(OSSL_RECORD_LAYER *rl, size_t n, size_t max, int extend,
256 int clearold, size_t *readbytes);
257 int tls_get_more_records(OSSL_RECORD_LAYER *rl);
258 int dtls_get_more_records(OSSL_RECORD_LAYER *rl);
260 int tls_default_set_protocol_version(OSSL_RECORD_LAYER *rl, int version);
261 int tls_default_validate_record_header(OSSL_RECORD_LAYER *rl, SSL3_RECORD *re);
262 int tls_do_uncompress(OSSL_RECORD_LAYER *rl, SSL3_RECORD *rec);
263 int tls_default_post_process_record(OSSL_RECORD_LAYER *rl, SSL3_RECORD *rec);
264 int tls13_common_post_process_record(OSSL_RECORD_LAYER *rl, SSL3_RECORD *rec);
267 tls_int_new_record_layer(OSSL_LIB_CTX *libctx, const char *propq, int vers,
268 int role, int direction, int level, unsigned char *key,
269 size_t keylen, unsigned char *iv, size_t ivlen,
270 unsigned char *mackey, size_t mackeylen,
271 const EVP_CIPHER *ciph, size_t taglen,
273 const EVP_MD *md, const SSL_COMP *comp, BIO *prev,
274 BIO *transport, BIO *next,
275 BIO_ADDR *local, BIO_ADDR *peer,
276 const OSSL_PARAM *settings, const OSSL_PARAM *options,
277 const OSSL_DISPATCH *fns, void *cbarg,
278 OSSL_RECORD_LAYER **retrl);
279 int tls_free(OSSL_RECORD_LAYER *rl);
280 int tls_reset(OSSL_RECORD_LAYER *rl);
281 int tls_unprocessed_read_pending(OSSL_RECORD_LAYER *rl);
282 int tls_processed_read_pending(OSSL_RECORD_LAYER *rl);
283 size_t tls_app_data_pending(OSSL_RECORD_LAYER *rl);
284 int tls_write_pending(OSSL_RECORD_LAYER *rl);
285 size_t tls_get_max_record_len(OSSL_RECORD_LAYER *rl);
286 size_t tls_get_max_records(OSSL_RECORD_LAYER *rl);
287 int tls_write_records(OSSL_RECORD_LAYER *rl, OSSL_RECORD_TEMPLATE *templates,
289 int tls_retry_write_records(OSSL_RECORD_LAYER *rl);
290 int tls_get_alert_code(OSSL_RECORD_LAYER *rl);
291 int tls_set1_bio(OSSL_RECORD_LAYER *rl, BIO *bio);
292 int tls_read_record(OSSL_RECORD_LAYER *rl, void **rechandle, int *rversion,
293 int *type, unsigned char **data, size_t *datalen,
294 uint16_t *epoch, unsigned char *seq_num);
295 int tls_release_record(OSSL_RECORD_LAYER *rl, void *rechandle);
296 int tls_default_set_protocol_version(OSSL_RECORD_LAYER *rl, int version);
297 int tls_set_protocol_version(OSSL_RECORD_LAYER *rl, int version);
298 void tls_set_plain_alerts(OSSL_RECORD_LAYER *rl, int allow);
299 void tls_set_first_handshake(OSSL_RECORD_LAYER *rl, int first);
300 void tls_set_max_pipelines(OSSL_RECORD_LAYER *rl, size_t max_pipelines);
301 void tls_get_state(OSSL_RECORD_LAYER *rl, const char **shortstr,
302 const char **longstr);
303 int tls_set_options(OSSL_RECORD_LAYER *rl, const OSSL_PARAM *options);
304 int tls_setup_read_buffer(OSSL_RECORD_LAYER *rl);