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
;
12 /*****************************************************************************
14 * These structures should be considered PRIVATE to the record layer. No *
15 * non-record layer code should be using these structures in any way. *
17 *****************************************************************************/
19 typedef struct ssl3_buffer_st
{
20 /* at least SSL3_RT_MAX_PACKET_SIZE bytes, see ssl3_setup_buffers() */
22 /* default buffer size (or 0 if no default set) */
26 /* where to 'copy from' */
28 /* how many bytes left */
30 /* 'buf' is from application for KTLS */
34 #define SEQ_NUM_SIZE 8
36 typedef struct ssl3_record_st
{
37 /* Record layer version */
43 /* How many bytes available */
47 * How many bytes were available before padding was removed? This is used
48 * to implement the MAC check in constant time for CBC records.
52 /* read/write offset into 'buf' */
55 /* pointer to the record data */
58 /* where the decode bytes are */
61 /* only used with decompression - malloc()ed */
64 /* Whether the data from this record has already been read or not */
67 /* epoch number, needed by DTLS1 */
70 /* sequence number, needed by DTLS1 */
72 unsigned char seq_num
[SEQ_NUM_SIZE
];
75 typedef struct dtls1_bitmap_st
{
76 /* Track 32 packets on 32-bit systems and 64 - on 64-bit systems */
78 /* Max record number seen so far, 64-bit value in big-endian encoding */
79 unsigned char max_seq_num
[SEQ_NUM_SIZE
];
82 typedef struct record_pqueue_st
{
87 typedef struct dtls1_record_data_st
{
88 unsigned char *packet
;
92 #ifndef OPENSSL_NO_SCTP
93 struct bio_dgram_sctp_rcvinfo recordinfo
;
97 typedef struct dtls_record_layer_st
{
99 * The current data and handshake epoch. This is initially
100 * undefined, and starts at zero once the initial handshake is
103 unsigned short r_epoch
;
104 unsigned short w_epoch
;
105 /* records being received in the current epoch */
107 /* renegotiation starts a new set of sequence numbers */
108 DTLS1_BITMAP next_bitmap
;
109 /* Received handshake records (processed and unprocessed) */
110 record_pqueue unprocessed_rcds
;
111 record_pqueue processed_rcds
;
113 * Buffered application records. Only for records between CCS and
114 * Finished to prevent either protocol violation or unnecessary message
117 record_pqueue buffered_app_data
;
118 /* save last and current sequence numbers for retransmissions */
119 unsigned char last_write_sequence
[8];
120 unsigned char curr_write_sequence
[8];
123 /*****************************************************************************
125 * This structure should be considered "opaque" to anything outside of the *
126 * record layer. No non-record layer code should be accessing the members of *
129 *****************************************************************************/
131 typedef struct record_layer_st
{
132 /* The parent SSL_CONNECTION structure */
135 * Read as many input bytes as possible (for
136 * non-blocking reads)
139 /* where we are when reading */
141 /* How many pipelines can be used to read data */
143 /* How many pipelines can be used to write data */
145 /* read IO goes into here */
147 /* write IO goes into here */
148 SSL3_BUFFER wbuf
[SSL_MAX_PIPELINES
];
149 /* each decoded record goes in here */
150 SSL3_RECORD rrec
[SSL_MAX_PIPELINES
];
151 /* used internally to point at a raw packet */
152 unsigned char *packet
;
153 size_t packet_length
;
154 /* number of bytes sent so far */
156 unsigned char handshake_fragment
[4];
157 size_t handshake_fragment_len
;
158 /* The number of consecutive empty records we have received */
159 size_t empty_record_count
;
160 /* partial write - check the numbers match */
161 /* number bytes written */
164 /* number of bytes submitted */
166 const unsigned char *wpend_buf
;
167 unsigned char read_sequence
[SEQ_NUM_SIZE
];
168 unsigned char write_sequence
[SEQ_NUM_SIZE
];
169 /* Set to true if this is the first record in a connection */
170 unsigned int is_first_record
;
171 /* Count of the number of consecutive warning alerts received */
172 unsigned int alert_count
;
173 DTLS_RECORD_LAYER
*d
;
176 /*****************************************************************************
178 * The following macros/functions represent the libssl internal API to the *
179 * record layer. Any libssl code may call these functions/macros *
181 *****************************************************************************/
183 struct ssl_mac_buf_st
{
187 typedef struct ssl_mac_buf_st SSL_MAC_BUF
;
189 #define MIN_SSL2_RECORD_LEN 9
191 #define RECORD_LAYER_set_read_ahead(rl, ra) ((rl)->read_ahead = (ra))
192 #define RECORD_LAYER_get_read_ahead(rl) ((rl)->read_ahead)
193 #define RECORD_LAYER_get_packet(rl) ((rl)->packet)
194 #define RECORD_LAYER_get_packet_length(rl) ((rl)->packet_length)
195 #define RECORD_LAYER_add_packet_length(rl, inc) ((rl)->packet_length += (inc))
196 #define DTLS_RECORD_LAYER_get_w_epoch(rl) ((rl)->d->w_epoch)
197 #define DTLS_RECORD_LAYER_get_processed_rcds(rl) \
198 ((rl)->d->processed_rcds)
199 #define DTLS_RECORD_LAYER_get_unprocessed_rcds(rl) \
200 ((rl)->d->unprocessed_rcds)
201 #define RECORD_LAYER_get_rbuf(rl) (&(rl)->rbuf)
202 #define RECORD_LAYER_get_wbuf(rl) ((rl)->wbuf)
204 void RECORD_LAYER_init(RECORD_LAYER
*rl
, SSL_CONNECTION
*s
);
205 void RECORD_LAYER_clear(RECORD_LAYER
*rl
);
206 void RECORD_LAYER_release(RECORD_LAYER
*rl
);
207 int RECORD_LAYER_read_pending(const RECORD_LAYER
*rl
);
208 int RECORD_LAYER_processed_read_pending(const RECORD_LAYER
*rl
);
209 int RECORD_LAYER_write_pending(const RECORD_LAYER
*rl
);
210 void RECORD_LAYER_reset_read_sequence(RECORD_LAYER
*rl
);
211 void RECORD_LAYER_reset_write_sequence(RECORD_LAYER
*rl
);
212 int RECORD_LAYER_is_sslv2_record(RECORD_LAYER
*rl
);
213 size_t RECORD_LAYER_get_rrec_length(RECORD_LAYER
*rl
);
214 __owur
size_t ssl3_pending(const SSL
*s
);
215 __owur
int ssl3_write_bytes(SSL
*s
, int type
, const void *buf
, size_t len
,
217 int do_ssl3_write(SSL_CONNECTION
*s
, int type
, const unsigned char *buf
,
218 size_t *pipelens
, size_t numpipes
,
219 int create_empty_fragment
, size_t *written
);
220 __owur
int ssl3_read_bytes(SSL
*s
, int type
, int *recvd_type
,
221 unsigned char *buf
, size_t len
, int peek
,
223 __owur
int ssl3_setup_buffers(SSL_CONNECTION
*s
);
224 __owur
int ssl3_enc(SSL_CONNECTION
*s
, SSL3_RECORD
*inrecs
, size_t n_recs
,
225 int send
, SSL_MAC_BUF
*mac
, size_t macsize
);
226 __owur
int n_ssl3_mac(SSL_CONNECTION
*s
, SSL3_RECORD
*rec
, unsigned char *md
,
228 __owur
int ssl3_write_pending(SSL_CONNECTION
*s
, int type
,
229 const unsigned char *buf
, size_t len
,
231 __owur
int tls1_enc(SSL_CONNECTION
*s
, SSL3_RECORD
*recs
, size_t n_recs
,
232 int sending
, SSL_MAC_BUF
*mac
, size_t macsize
);
233 __owur
int tls1_mac(SSL_CONNECTION
*s
, SSL3_RECORD
*rec
, unsigned char *md
,
235 __owur
int tls13_enc(SSL_CONNECTION
*s
, SSL3_RECORD
*recs
, size_t n_recs
,
236 int send
, SSL_MAC_BUF
*mac
, size_t macsize
);
237 int DTLS_RECORD_LAYER_new(RECORD_LAYER
*rl
);
238 void DTLS_RECORD_LAYER_free(RECORD_LAYER
*rl
);
239 void DTLS_RECORD_LAYER_clear(RECORD_LAYER
*rl
);
240 void DTLS_RECORD_LAYER_set_saved_w_epoch(RECORD_LAYER
*rl
, unsigned short e
);
241 void DTLS_RECORD_LAYER_clear(RECORD_LAYER
*rl
);
242 void DTLS_RECORD_LAYER_set_write_sequence(RECORD_LAYER
*rl
, unsigned char *seq
);
243 __owur
int dtls1_read_bytes(SSL
*s
, int type
, int *recvd_type
,
244 unsigned char *buf
, size_t len
, int peek
,
246 __owur
int dtls1_write_bytes(SSL_CONNECTION
*s
, int type
, const void *buf
,
247 size_t len
, size_t *written
);
248 int do_dtls1_write(SSL_CONNECTION
*s
, int type
, const unsigned char *buf
,
249 size_t len
, int create_empty_fragment
, size_t *written
);
250 void dtls1_reset_seq_numbers(SSL_CONNECTION
*s
, int rw
);
251 int dtls_buffer_listen_record(SSL_CONNECTION
*s
, size_t len
, unsigned char *seq
,