2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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 /*****************************************************************************
12 * These structures should be considered PRIVATE to the record layer. No *
13 * non-record layer code should be using these structures in any way. *
15 *****************************************************************************/
17 typedef struct ssl3_buffer_st
{
18 /* at least SSL3_RT_MAX_PACKET_SIZE bytes, see ssl3_setup_buffers() */
20 /* default buffer size (or 0 if no default set) */
24 /* where to 'copy from' */
26 /* how many bytes left */
30 #define SEQ_NUM_SIZE 8
32 typedef struct ssl3_record_st
{
33 /* Record layer version */
41 /* How many bytes available */
46 * How many bytes were available before padding was removed? This is used
47 * to implement the MAC check in constant time for CBC records.
50 unsigned int orig_len
;
52 /* read/write offset into 'buf' */
56 /* pointer to the record data */
60 /* where the decode bytes are */
64 /* only used with decompression - malloc()ed */
68 /* epoch number, needed by DTLS1 */
72 /* sequence number, needed by DTLS1 */
74 unsigned char seq_num
[SEQ_NUM_SIZE
];
77 typedef struct dtls1_bitmap_st
{
78 /* Track 32 packets on 32-bit systems and 64 - on 64-bit systems */
81 /* Max record number seen so far, 64-bit value in big-endian encoding */
82 unsigned char max_seq_num
[SEQ_NUM_SIZE
];
85 typedef struct record_pqueue_st
{
90 typedef struct dtls1_record_data_st
{
91 unsigned char *packet
;
92 unsigned int packet_length
;
95 # ifndef OPENSSL_NO_SCTP
96 struct bio_dgram_sctp_rcvinfo recordinfo
;
101 typedef struct dtls_record_layer_st
{
103 * The current data and handshake epoch. This is initially
104 * undefined, and starts at zero once the initial handshake is
107 unsigned short r_epoch
;
108 unsigned short w_epoch
;
110 /* records being received in the current epoch */
112 /* renegotiation starts a new set of sequence numbers */
113 DTLS1_BITMAP next_bitmap
;
115 /* Received handshake records (processed and unprocessed) */
116 record_pqueue unprocessed_rcds
;
117 record_pqueue processed_rcds
;
119 * Buffered application records. Only for records between CCS and
120 * Finished to prevent either protocol violation or unnecessary message
123 record_pqueue buffered_app_data
;
125 * storage for Alert/Handshake protocol data received but not yet
126 * processed by ssl3_read_bytes:
128 unsigned char alert_fragment
[DTLS1_AL_HEADER_LENGTH
];
129 unsigned int alert_fragment_len
;
130 unsigned char handshake_fragment
[DTLS1_HM_HEADER_LENGTH
];
131 unsigned int handshake_fragment_len
;
133 /* save last and current sequence numbers for retransmissions */
134 unsigned char last_write_sequence
[8];
135 unsigned char curr_write_sequence
[8];
138 /*****************************************************************************
140 * This structure should be considered "opaque" to anything outside of the *
141 * record layer. No non-record layer code should be accessing the members of *
144 *****************************************************************************/
146 typedef struct record_layer_st
{
147 /* The parent SSL structure */
150 * Read as many input bytes as possible (for
151 * non-blocking reads)
154 /* where we are when reading */
157 /* How many pipelines can be used to read data */
158 unsigned int numrpipes
;
159 /* How many pipelines can be used to write data */
160 unsigned int numwpipes
;
161 /* read IO goes into here */
163 /* write IO goes into here */
164 SSL3_BUFFER wbuf
[SSL_MAX_PIPELINES
];
165 /* each decoded record goes in here */
166 SSL3_RECORD rrec
[SSL_MAX_PIPELINES
];
168 /* used internally to point at a raw packet */
169 unsigned char *packet
;
170 unsigned int packet_length
;
172 /* number of bytes sent so far */
176 * storage for Alert/Handshake protocol data received but not yet
177 * processed by ssl3_read_bytes:
179 unsigned char alert_fragment
[2];
180 unsigned int alert_fragment_len
;
181 unsigned char handshake_fragment
[4];
182 unsigned int handshake_fragment_len
;
184 /* partial write - check the numbers match */
185 /* number bytes written */
188 /* number of bytes submitted */
190 const unsigned char *wpend_buf
;
192 unsigned char read_sequence
[SEQ_NUM_SIZE
];
193 unsigned char write_sequence
[SEQ_NUM_SIZE
];
195 DTLS_RECORD_LAYER
*d
;
199 /*****************************************************************************
201 * The following macros/functions represent the libssl internal API to the *
202 * record layer. Any libssl code may call these functions/macros *
204 *****************************************************************************/
206 #define MIN_SSL2_RECORD_LEN 9
208 #define RECORD_LAYER_set_read_ahead(rl, ra) ((rl)->read_ahead = (ra))
209 #define RECORD_LAYER_get_read_ahead(rl) ((rl)->read_ahead)
210 #define RECORD_LAYER_get_packet(rl) ((rl)->packet)
211 #define RECORD_LAYER_get_packet_length(rl) ((rl)->packet_length)
212 #define RECORD_LAYER_add_packet_length(rl, inc) ((rl)->packet_length += (inc))
213 #define DTLS_RECORD_LAYER_get_w_epoch(rl) ((rl)->d->w_epoch)
214 #define DTLS_RECORD_LAYER_get_processed_rcds(rl) \
215 ((rl)->d->processed_rcds)
216 #define DTLS_RECORD_LAYER_get_unprocessed_rcds(rl) \
217 ((rl)->d->unprocessed_rcds)
219 void RECORD_LAYER_init(RECORD_LAYER
*rl
, SSL
*s
);
220 void RECORD_LAYER_clear(RECORD_LAYER
*rl
);
221 void RECORD_LAYER_release(RECORD_LAYER
*rl
);
222 int RECORD_LAYER_read_pending(const RECORD_LAYER
*rl
);
223 int RECORD_LAYER_write_pending(const RECORD_LAYER
*rl
);
224 int RECORD_LAYER_set_data(RECORD_LAYER
*rl
, const unsigned char *buf
, int len
);
225 void RECORD_LAYER_reset_read_sequence(RECORD_LAYER
*rl
);
226 void RECORD_LAYER_reset_write_sequence(RECORD_LAYER
*rl
);
227 int RECORD_LAYER_is_sslv2_record(RECORD_LAYER
*rl
);
228 unsigned int RECORD_LAYER_get_rrec_length(RECORD_LAYER
*rl
);
229 __owur
int ssl3_pending(const SSL
*s
);
230 __owur
int ssl3_write_bytes(SSL
*s
, int type
, const void *buf
, int len
);
231 __owur
int do_ssl3_write(SSL
*s
, int type
, const unsigned char *buf
,
232 unsigned int *pipelens
, unsigned int numpipes
,
233 int create_empty_fragment
);
234 __owur
int ssl3_read_bytes(SSL
*s
, int type
, int *recvd_type
,
235 unsigned char *buf
, int len
, int peek
);
236 __owur
int ssl3_setup_buffers(SSL
*s
);
237 __owur
int ssl3_enc(SSL
*s
, SSL3_RECORD
*inrecs
, unsigned int n_recs
, int send
);
238 __owur
int n_ssl3_mac(SSL
*ssl
, SSL3_RECORD
*rec
, unsigned char *md
, int send
);
239 __owur
int ssl3_write_pending(SSL
*s
, int type
, const unsigned char *buf
,
241 __owur
int tls1_enc(SSL
*s
, SSL3_RECORD
*recs
, unsigned int n_recs
, int send
);
242 __owur
int tls1_mac(SSL
*ssl
, SSL3_RECORD
*rec
, unsigned char *md
, int send
);
243 int DTLS_RECORD_LAYER_new(RECORD_LAYER
*rl
);
244 void DTLS_RECORD_LAYER_free(RECORD_LAYER
*rl
);
245 void DTLS_RECORD_LAYER_clear(RECORD_LAYER
*rl
);
246 void DTLS_RECORD_LAYER_set_saved_w_epoch(RECORD_LAYER
*rl
, unsigned short e
);
247 void DTLS_RECORD_LAYER_clear(RECORD_LAYER
*rl
);
248 void DTLS_RECORD_LAYER_resync_write(RECORD_LAYER
*rl
);
249 void DTLS_RECORD_LAYER_set_write_sequence(RECORD_LAYER
*rl
, unsigned char *seq
);
250 __owur
int dtls1_read_bytes(SSL
*s
, int type
, int *recvd_type
,
251 unsigned char *buf
, int len
, int peek
);
252 __owur
int dtls1_write_bytes(SSL
*s
, int type
, const void *buf
, int len
);
253 __owur
int do_dtls1_write(SSL
*s
, int type
, const unsigned char *buf
,
254 unsigned int len
, int create_empty_fragement
);
255 void dtls1_reset_seq_numbers(SSL
*s
, int rw
);