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 /* Protocol version specific function pointers */
17 struct record_functions_st
20 * Returns either OSSL_RECORD_RETURN_SUCCESS, OSSL_RECORD_RETURN_FATAL or
21 * OSSL_RECORD_RETURN_NON_FATAL_ERR if we can keep trying to find an
22 * alternative record layer.
24 int (*set_crypto_state
)(OSSL_RECORD_LAYER
*rl
, int level
,
25 unsigned char *key
, size_t keylen
,
26 unsigned char *iv
, size_t ivlen
,
27 unsigned char *mackey
, size_t mackeylen
,
28 const EVP_CIPHER
*ciph
,
32 const SSL_COMP
*comp
);
34 int (*read_n
)(OSSL_RECORD_LAYER
*rl
, size_t n
, size_t max
, int extend
,
35 int clearold
, size_t *readbytes
);
37 int (*get_more_records
)(OSSL_RECORD_LAYER
*rl
);
41 * 0: if the record is publicly invalid, or an internal error, or AEAD
42 * decryption failed, or EtM decryption failed.
43 * 1: Success or MtE decryption failed (MAC will be randomised)
45 int (*cipher
)(OSSL_RECORD_LAYER
*rl
, SSL3_RECORD
*recs
, size_t n_recs
,
46 int sending
, SSL_MAC_BUF
*macs
, size_t macsize
);
47 /* Returns 1 for success or 0 for error */
48 int (*mac
)(OSSL_RECORD_LAYER
*rl
, SSL3_RECORD
*rec
, unsigned char *md
,
51 /* Return 1 for success or 0 for error */
52 int (*set_protocol_version
)(OSSL_RECORD_LAYER
*rl
, int version
);
54 /* Return 1 for success or 0 for error */
55 int (*validate_record_header
)(OSSL_RECORD_LAYER
*rl
, SSL3_RECORD
*rec
);
57 /* Return 1 for success or 0 for error */
58 int (*post_process_record
)(OSSL_RECORD_LAYER
*rl
, SSL3_RECORD
*rec
);
61 struct ossl_record_layer_st
74 * A BIO containing any data read in the previous epoch that was destined
79 /* The transport BIO */
83 * A BIO where we will send any data read by us that is destined for the
88 /* Types match the equivalent structures in the SSL object */
91 * TODO(RECLAYER): Should we take the opportunity to make this uint64_t
92 * even though upper layer continue to use uint32_t?
96 /* read IO goes into here */
98 /* each decoded record goes in here */
99 SSL3_RECORD rrec
[SSL_MAX_PIPELINES
];
101 /* How many records have we got available in the rrec bufer */
104 /* The record number in the rrec buffer that can be read next */
107 /* The number of records that have been released via tls_release_record */
110 /* where we are when reading */
113 /* used internally to point at a raw packet */
114 unsigned char *packet
;
115 size_t packet_length
;
117 /* Sequence number for the next record */
118 unsigned char sequence
[SEQ_NUM_SIZE
];
123 * Read as many input bytes as possible (for
124 * non-blocking reads)
125 * TODO(RECLAYER): Why isn't this just an option?
129 /* The number of consecutive empty records we have received */
130 size_t empty_record_count
;
132 /* cryptographic state */
133 EVP_CIPHER_CTX
*enc_ctx
;
135 /* used for mac generation */
141 /* Set to 1 if this is the first handshake. 0 otherwise */
142 int is_first_handshake
;
144 /* The negotiated maximum fragment length */
145 unsigned int max_frag_len
;
147 /* The maxium amount of early data we can receive/send */
148 uint32_t max_early_data
;
150 /* The amount of early data that we have sent/received */
151 size_t early_data_count
;
153 /* Only used by SSLv3 */
154 unsigned char mac_secret
[EVP_MAX_MD_SIZE
];
156 /* TLSv1.0/TLSv1.1/TLSv1.2 */
159 /* Flags for GOST ciphers */
165 unsigned char iv
[EVP_MAX_IV_LENGTH
];
167 unsigned char read_iv
[EVP_MAX_IV_LENGTH
];
168 int allow_plain_alerts
;
170 /* TLS "any" fields */
171 /* Set to true if this is the first record in a connection */
172 unsigned int is_first_record
;
176 /* DTLS received handshake records (processed and unprocessed) */
177 record_pqueue unprocessed_rcds
;
178 record_pqueue processed_rcds
;
180 /* records being received in the current epoch */
182 /* renegotiation starts a new set of sequence numbers */
183 DTLS1_BITMAP next_bitmap
;
186 * Whether we are currently in a hanshake or not. Only maintained for DTLS
192 OSSL_FUNC_rlayer_skip_early_data_fn
*skip_early_data
;
193 OSSL_FUNC_rlayer_msg_callback_fn
*msg_callback
;
194 OSSL_FUNC_rlayer_security_fn
*security
;
196 size_t max_pipelines
;
198 /* Function pointers for version specific functions */
199 struct record_functions_st
*funcs
;
202 typedef struct dtls_rlayer_record_data_st
{
203 unsigned char *packet
;
204 size_t packet_length
;
207 } DTLS_RLAYER_RECORD_DATA
;
210 extern struct record_functions_st ssl_3_0_funcs
;
211 extern struct record_functions_st tls_1_funcs
;
212 extern struct record_functions_st tls_1_3_funcs
;
213 extern struct record_functions_st tls_any_funcs
;
214 extern struct record_functions_st dtls_1_funcs
;
215 extern struct record_functions_st dtls_any_funcs
;
217 void ossl_rlayer_fatal(OSSL_RECORD_LAYER
*rl
, int al
, int reason
,
218 const char *fmt
, ...);
220 # define RLAYERfatal(rl, al, r) RLAYERfatal_data((rl), (al), (r), NULL)
221 # define RLAYERfatal_data \
223 ERR_set_debug(OPENSSL_FILE, OPENSSL_LINE, OPENSSL_FUNC), \
226 # define RLAYER_USE_EXPLICIT_IV(rl) ((rl)->version == TLS1_1_VERSION \
227 || (rl)->version == TLS1_2_VERSION \
230 int ossl_set_tls_provider_parameters(OSSL_RECORD_LAYER
*rl
,
232 const EVP_CIPHER
*ciph
,
235 __owur
char ssl3_cbc_record_digest_supported(const EVP_MD_CTX
*ctx
);
236 __owur
int ssl3_cbc_digest_record(const EVP_MD
*md
,
237 unsigned char *md_out
,
239 const unsigned char *header
,
240 const unsigned char *data
,
242 size_t data_plus_mac_plus_padding_size
,
243 const unsigned char *mac_secret
,
244 size_t mac_secret_length
, char is_sslv3
);
246 int tls_default_read_n(OSSL_RECORD_LAYER
*rl
, size_t n
, size_t max
, int extend
,
247 int clearold
, size_t *readbytes
);
248 int tls_get_more_records(OSSL_RECORD_LAYER
*rl
);
249 int dtls_get_more_records(OSSL_RECORD_LAYER
*rl
);
251 int tls_default_set_protocol_version(OSSL_RECORD_LAYER
*rl
, int version
);
252 int tls_default_validate_record_header(OSSL_RECORD_LAYER
*rl
, SSL3_RECORD
*re
);
253 int tls_do_uncompress(OSSL_RECORD_LAYER
*rl
, SSL3_RECORD
*rec
);
254 int tls_default_post_process_record(OSSL_RECORD_LAYER
*rl
, SSL3_RECORD
*rec
);
255 int tls13_common_post_process_record(OSSL_RECORD_LAYER
*rl
, SSL3_RECORD
*rec
);
258 tls_int_new_record_layer(OSSL_LIB_CTX
*libctx
, const char *propq
, int vers
,
259 int role
, int direction
, int level
, unsigned char *key
,
260 size_t keylen
, unsigned char *iv
, size_t ivlen
,
261 unsigned char *mackey
, size_t mackeylen
,
262 const EVP_CIPHER
*ciph
, size_t taglen
,
264 const EVP_MD
*md
, const SSL_COMP
*comp
, BIO
*prev
,
265 BIO
*transport
, BIO
*next
,
266 BIO_ADDR
*local
, BIO_ADDR
*peer
,
267 const OSSL_PARAM
*settings
, const OSSL_PARAM
*options
,
268 const OSSL_DISPATCH
*fns
, void *cbarg
,
269 OSSL_RECORD_LAYER
**retrl
);
270 int tls_free(OSSL_RECORD_LAYER
*rl
);
271 int tls_reset(OSSL_RECORD_LAYER
*rl
);
272 int tls_unprocessed_read_pending(OSSL_RECORD_LAYER
*rl
);
273 int tls_processed_read_pending(OSSL_RECORD_LAYER
*rl
);
274 size_t tls_app_data_pending(OSSL_RECORD_LAYER
*rl
);
275 int tls_write_pending(OSSL_RECORD_LAYER
*rl
);
276 size_t tls_get_max_record_len(OSSL_RECORD_LAYER
*rl
);
277 size_t tls_get_max_records(OSSL_RECORD_LAYER
*rl
);
278 int tls_write_records(OSSL_RECORD_LAYER
*rl
, OSSL_RECORD_TEMPLATE
**templates
,
279 size_t numtempl
, size_t allowance
, size_t *sent
);
280 int tls_retry_write_records(OSSL_RECORD_LAYER
*rl
, size_t allowance
,
282 int tls_get_alert_code(OSSL_RECORD_LAYER
*rl
);
283 int tls_set1_bio(OSSL_RECORD_LAYER
*rl
, BIO
*bio
);
284 int tls_read_record(OSSL_RECORD_LAYER
*rl
, void **rechandle
, int *rversion
,
285 int *type
, unsigned char **data
, size_t *datalen
,
286 uint16_t *epoch
, unsigned char *seq_num
);
287 int tls_release_record(OSSL_RECORD_LAYER
*rl
, void *rechandle
);
288 int tls_default_set_protocol_version(OSSL_RECORD_LAYER
*rl
, int version
);
289 int tls_set_protocol_version(OSSL_RECORD_LAYER
*rl
, int version
);
290 void tls_set_plain_alerts(OSSL_RECORD_LAYER
*rl
, int allow
);
291 void tls_set_first_handshake(OSSL_RECORD_LAYER
*rl
, int first
);
292 void tls_set_max_pipelines(OSSL_RECORD_LAYER
*rl
, size_t max_pipelines
);
293 int rlayer_setup_read_buffer(OSSL_RECORD_LAYER
*rl
);