2 * Copyright 1995-2020 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 typedef struct ssl_connection_st SSL_CONNECTION
;
11 typedef struct ssl3_buffer_st SSL3_BUFFER
;
13 #include <openssl/core_dispatch.h>
14 #include "recordmethod.h"
16 /*****************************************************************************
18 * These structures should be considered PRIVATE to the record layer. No *
19 * non-record layer code should be using these structures in any way. *
21 *****************************************************************************/
23 struct ssl3_buffer_st
{
24 /* at least SSL3_RT_MAX_PACKET_SIZE bytes, see ssl3_setup_buffers() */
26 /* default buffer size (or 0 if no default set) */
30 /* where to 'copy from' */
32 /* how many bytes left */
34 /* 'buf' is from application for KTLS */
36 /* The type of data stored in this buffer. Only used for writing */
40 #define SEQ_NUM_SIZE 8
42 typedef struct ssl3_record_st
{
43 /* Record layer version */
49 /* How many bytes available */
53 * How many bytes were available before padding was removed? This is used
54 * to implement the MAC check in constant time for CBC records.
58 /* read/write offset into 'buf' */
61 /* pointer to the record data */
64 /* where the decode bytes are */
67 /* only used with decompression - malloc()ed */
70 /* epoch number, needed by DTLS1 */
73 /* sequence number, needed by DTLS1 */
75 unsigned char seq_num
[SEQ_NUM_SIZE
];
78 typedef struct tls_record_st
{
82 /* The data buffer containing bytes from the record */
84 /* Number of remaining to be read in the data buffer */
86 /* Offset into the data buffer where to start reading */
88 /* epoch number. DTLS only */
90 /* sequence number. DTLS only */
91 unsigned char seq_num
[SEQ_NUM_SIZE
];
92 #ifndef OPENSSL_NO_SCTP
93 struct bio_dgram_sctp_rcvinfo recordinfo
;
97 typedef struct record_pqueue_st
{
102 typedef struct dtls_record_layer_st
{
104 * The current data and handshake epoch. This is initially
105 * undefined, and starts at zero once the initial handshake is
112 * Buffered application records. Only for records between CCS and
113 * Finished to prevent either protocol violation or unnecessary message
116 record_pqueue buffered_app_data
;
117 /* save last and current sequence numbers for retransmissions */
118 unsigned char last_write_sequence
[8];
119 unsigned char curr_write_sequence
[8];
122 /*****************************************************************************
124 * This structure should be considered "opaque" to anything outside of the *
125 * record layer. No non-record layer code should be accessing the members of *
128 *****************************************************************************/
130 typedef struct record_layer_st
{
131 /* The parent SSL_CONNECTION structure */
134 /* Method to use for the read record layer*/
135 const OSSL_RECORD_METHOD
*rrlmethod
;
136 /* Method to use for the write record layer*/
137 const OSSL_RECORD_METHOD
*wrlmethod
;
138 /* The read record layer object itself */
139 OSSL_RECORD_LAYER
*rrl
;
140 /* The write record layer object itself */
141 OSSL_RECORD_LAYER
*wrl
;
142 /* BIO to store data destined for the next read record layer epoch */
144 /* Default read buffer length to be passed to the record layer */
145 size_t default_read_buf_len
;
148 * Read as many input bytes as possible (for
149 * non-blocking reads)
153 * TODO(RECLAYER): These next 2 fields can be removed when DTLS is moved to
154 * the new write record layer architecture.
156 /* How many pipelines can be used to write data */
158 /* write IO goes into here */
159 SSL3_BUFFER wbuf
[SSL_MAX_PIPELINES
+ 1];
160 /* number of bytes sent so far */
162 unsigned char handshake_fragment
[4];
163 size_t handshake_fragment_len
;
164 /* partial write - check the numbers match */
165 /* number bytes written */
168 /* number of bytes submitted */
170 const unsigned char *wpend_buf
;
172 unsigned char write_sequence
[SEQ_NUM_SIZE
];
173 /* Count of the number of consecutive warning alerts received */
174 unsigned int alert_count
;
175 DTLS_RECORD_LAYER
*d
;
177 /* TLS1.3 padding callback */
178 size_t (*record_padding_cb
)(SSL
*s
, int type
, size_t len
, void *arg
);
179 void *record_padding_arg
;
180 size_t block_padding
;
182 /* How many records we have read from the record layer */
184 /* The next record from the record layer that we need to process */
186 /* Record layer data to be processed */
187 TLS_RECORD tlsrecs
[SSL_MAX_PIPELINES
];
191 /*****************************************************************************
193 * The following macros/functions represent the libssl internal API to the *
194 * record layer. Any libssl code may call these functions/macros *
196 *****************************************************************************/
198 struct ssl_mac_buf_st
{
202 typedef struct ssl_mac_buf_st SSL_MAC_BUF
;
204 #define MIN_SSL2_RECORD_LEN 9
206 #define RECORD_LAYER_set_read_ahead(rl, ra) ((rl)->read_ahead = (ra))
207 #define RECORD_LAYER_get_read_ahead(rl) ((rl)->read_ahead)
208 #define RECORD_LAYER_get_packet(rl) ((rl)->packet)
209 #define RECORD_LAYER_add_packet_length(rl, inc) ((rl)->packet_length += (inc))
210 #define DTLS_RECORD_LAYER_get_w_epoch(rl) ((rl)->d->w_epoch)
211 #define RECORD_LAYER_get_rbuf(rl) (&(rl)->rbuf)
212 #define RECORD_LAYER_get_wbuf(rl) ((rl)->wbuf)
214 void RECORD_LAYER_init(RECORD_LAYER
*rl
, SSL_CONNECTION
*s
);
215 void RECORD_LAYER_clear(RECORD_LAYER
*rl
);
216 void RECORD_LAYER_release(RECORD_LAYER
*rl
);
217 int RECORD_LAYER_read_pending(const RECORD_LAYER
*rl
);
218 int RECORD_LAYER_processed_read_pending(const RECORD_LAYER
*rl
);
219 int RECORD_LAYER_write_pending(const RECORD_LAYER
*rl
);
220 void RECORD_LAYER_reset_write_sequence(RECORD_LAYER
*rl
);
221 int RECORD_LAYER_is_sslv2_record(RECORD_LAYER
*rl
);
222 __owur
size_t ssl3_pending(const SSL
*s
);
223 __owur
int ssl3_write_bytes(SSL
*s
, int type
, const void *buf
, size_t len
,
225 __owur
int ssl3_read_bytes(SSL
*s
, int type
, int *recvd_type
,
226 unsigned char *buf
, size_t len
, int peek
,
228 __owur
int ssl3_setup_buffers(SSL_CONNECTION
*s
);
229 __owur
int ssl3_enc(SSL_CONNECTION
*s
, SSL3_RECORD
*inrecs
, size_t n_recs
,
230 int send
, SSL_MAC_BUF
*mac
, size_t macsize
);
231 __owur
int n_ssl3_mac(SSL_CONNECTION
*s
, SSL3_RECORD
*rec
, unsigned char *md
,
233 __owur
int tls1_enc(SSL_CONNECTION
*s
, SSL3_RECORD
*recs
, size_t n_recs
,
234 int sending
, SSL_MAC_BUF
*mac
, size_t macsize
);
235 __owur
int tls1_mac_old(SSL_CONNECTION
*s
, SSL3_RECORD
*rec
, unsigned char *md
,
237 __owur
int tls13_enc(SSL_CONNECTION
*s
, SSL3_RECORD
*recs
, size_t n_recs
,
238 int send
, SSL_MAC_BUF
*mac
, size_t macsize
);
239 int DTLS_RECORD_LAYER_new(RECORD_LAYER
*rl
);
240 void DTLS_RECORD_LAYER_free(RECORD_LAYER
*rl
);
241 void DTLS_RECORD_LAYER_clear(RECORD_LAYER
*rl
);
242 void DTLS_RECORD_LAYER_set_saved_w_epoch(RECORD_LAYER
*rl
, unsigned short e
);
243 void DTLS_RECORD_LAYER_clear(RECORD_LAYER
*rl
);
244 void DTLS_RECORD_LAYER_set_write_sequence(RECORD_LAYER
*rl
, unsigned char *seq
);
245 __owur
int dtls1_read_bytes(SSL
*s
, int type
, int *recvd_type
,
246 unsigned char *buf
, size_t len
, int peek
,
248 __owur
int dtls1_write_bytes(SSL_CONNECTION
*s
, int type
, const void *buf
,
249 size_t len
, size_t *written
);
250 int do_dtls1_write(SSL_CONNECTION
*s
, int type
, const unsigned char *buf
,
251 size_t len
, int create_empty_fragment
, size_t *written
);
252 void dtls1_reset_seq_numbers(SSL_CONNECTION
*s
, int rw
);
253 void ssl_release_record(SSL_CONNECTION
*s
, TLS_RECORD
*rr
);
255 # define HANDLE_RLAYER_RETURN(s, ret) \
256 ossl_tls_handle_rlayer_return(s, ret, OPENSSL_FILE, OPENSSL_LINE)
258 int ossl_tls_handle_rlayer_return(SSL_CONNECTION
*s
, int ret
, char *file
,
261 int ssl_set_new_record_layer(SSL_CONNECTION
*s
, int version
, int direction
,
262 int level
, unsigned char *key
, size_t keylen
,
263 unsigned char *iv
, size_t ivlen
,
264 unsigned char *mackey
, size_t mackeylen
,
265 const EVP_CIPHER
*ciph
, size_t taglen
,
266 int mactype
, const EVP_MD
*md
,
267 const SSL_COMP
*comp
);
268 int ssl_set_record_protocol_version(SSL_CONNECTION
*s
, int vers
);
270 # define OSSL_FUNC_RLAYER_SKIP_EARLY_DATA 1
271 OSSL_CORE_MAKE_FUNC(int, rlayer_skip_early_data
, (void *cbarg
))
272 # define OSSL_FUNC_RLAYER_MSG_CALLBACK 2
273 OSSL_CORE_MAKE_FUNC(void, rlayer_msg_callback
, (int write_p
, int version
,
275 const void *buf
, size_t len
,
277 # define OSSL_FUNC_RLAYER_SECURITY 3
278 OSSL_CORE_MAKE_FUNC(int, rlayer_security
, (void *cbarg
, int op
, int bits
,
279 int nid
, void *other
))
280 # define OSSL_FUNC_RLAYER_PADDING 4
281 OSSL_CORE_MAKE_FUNC(size_t, rlayer_padding
, (void *cbarg
, int type
, size_t len
))