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 */
39 /* How many bytes available */
43 * How many bytes were available before padding was removed? This is used
44 * to implement the MAC check in constant time for CBC records.
47 unsigned int orig_len;
48 /* read/write offset into 'buf' */
51 /* pointer to the record data */
54 /* where the decode bytes are */
57 /* only used with decompression - malloc()ed */
60 /* Whether the data from this record has already been read or not */
63 /* epoch number, needed by DTLS1 */
66 /* sequence number, needed by DTLS1 */
68 unsigned char seq_num[SEQ_NUM_SIZE];
71 typedef struct dtls1_bitmap_st {
72 /* Track 32 packets on 32-bit systems and 64 - on 64-bit systems */
74 /* Max record number seen so far, 64-bit value in big-endian encoding */
75 unsigned char max_seq_num[SEQ_NUM_SIZE];
78 typedef struct record_pqueue_st {
83 typedef struct dtls1_record_data_st {
84 unsigned char *packet;
85 unsigned int packet_length;
88 #ifndef OPENSSL_NO_SCTP
89 struct bio_dgram_sctp_rcvinfo recordinfo;
93 typedef struct dtls_record_layer_st {
95 * The current data and handshake epoch. This is initially
96 * undefined, and starts at zero once the initial handshake is
99 unsigned short r_epoch;
100 unsigned short w_epoch;
101 /* records being received in the current epoch */
103 /* renegotiation starts a new set of sequence numbers */
104 DTLS1_BITMAP next_bitmap;
105 /* Received handshake records (processed and unprocessed) */
106 record_pqueue unprocessed_rcds;
107 record_pqueue processed_rcds;
109 * Buffered application records. Only for records between CCS and
110 * Finished to prevent either protocol violation or unnecessary message
113 record_pqueue buffered_app_data;
115 * storage for Alert/Handshake protocol data received but not yet
116 * processed by ssl3_read_bytes:
118 unsigned char alert_fragment[DTLS1_AL_HEADER_LENGTH];
119 unsigned int alert_fragment_len;
120 unsigned char handshake_fragment[DTLS1_HM_HEADER_LENGTH];
121 unsigned int handshake_fragment_len;
122 /* save last and current sequence numbers for retransmissions */
123 unsigned char last_write_sequence[8];
124 unsigned char curr_write_sequence[8];
127 /*****************************************************************************
129 * This structure should be considered "opaque" to anything outside of the *
130 * record layer. No non-record layer code should be accessing the members of *
133 *****************************************************************************/
135 typedef struct record_layer_st {
136 /* The parent SSL structure */
139 * Read as many input bytes as possible (for
140 * non-blocking reads)
143 /* where we are when reading */
145 /* How many pipelines can be used to read data */
146 unsigned int numrpipes;
147 /* How many pipelines can be used to write data */
148 unsigned int numwpipes;
149 /* read IO goes into here */
151 /* write IO goes into here */
152 SSL3_BUFFER wbuf[SSL_MAX_PIPELINES];
153 /* each decoded record goes in here */
154 SSL3_RECORD rrec[SSL_MAX_PIPELINES];
155 /* used internally to point at a raw packet */
156 unsigned char *packet;
157 unsigned int packet_length;
158 /* number of bytes sent so far */
161 * storage for Alert/Handshake protocol data received but not yet
162 * processed by ssl3_read_bytes:
164 unsigned char alert_fragment[2];
165 unsigned int alert_fragment_len;
166 unsigned char handshake_fragment[4];
167 unsigned int handshake_fragment_len;
168 /* The number of consecutive empty records we have received */
169 unsigned int empty_record_count;
170 /* partial write - check the numbers match */
171 /* number bytes written */
174 /* number of bytes submitted */
176 const unsigned char *wpend_buf;
177 unsigned char read_sequence[SEQ_NUM_SIZE];
178 unsigned char write_sequence[SEQ_NUM_SIZE];
179 /* Set to true if this is the first record in a connection */
180 unsigned int is_first_record;
181 DTLS_RECORD_LAYER *d;
184 /*****************************************************************************
186 * The following macros/functions represent the libssl internal API to the *
187 * record layer. Any libssl code may call these functions/macros *
189 *****************************************************************************/
191 #define MIN_SSL2_RECORD_LEN 9
193 #define RECORD_LAYER_set_read_ahead(rl, ra) ((rl)->read_ahead = (ra))
194 #define RECORD_LAYER_get_read_ahead(rl) ((rl)->read_ahead)
195 #define RECORD_LAYER_get_packet(rl) ((rl)->packet)
196 #define RECORD_LAYER_get_packet_length(rl) ((rl)->packet_length)
197 #define RECORD_LAYER_add_packet_length(rl, inc) ((rl)->packet_length += (inc))
198 #define DTLS_RECORD_LAYER_get_w_epoch(rl) ((rl)->d->w_epoch)
199 #define DTLS_RECORD_LAYER_get_processed_rcds(rl) \
200 ((rl)->d->processed_rcds)
201 #define DTLS_RECORD_LAYER_get_unprocessed_rcds(rl) \
202 ((rl)->d->unprocessed_rcds)
204 void RECORD_LAYER_init(RECORD_LAYER *rl, SSL *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_write_pending(const RECORD_LAYER *rl);
209 int RECORD_LAYER_set_data(RECORD_LAYER *rl, const unsigned char *buf, int len);
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 unsigned int RECORD_LAYER_get_rrec_length(RECORD_LAYER *rl);
214 __owur int ssl3_pending(const SSL *s);
215 __owur int ssl3_write_bytes(SSL *s, int type, const void *buf, int len);
216 __owur int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
217 unsigned int *pipelens, unsigned int numpipes,
218 int create_empty_fragment);
219 __owur int ssl3_read_bytes(SSL *s, int type, int *recvd_type,
220 unsigned char *buf, int len, int peek);
221 __owur int ssl3_setup_buffers(SSL *s);
222 __owur int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, unsigned int n_recs, int send);
223 __owur int n_ssl3_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int send);
224 __owur int ssl3_write_pending(SSL *s, int type, const unsigned char *buf,
226 __owur int tls1_enc(SSL *s, SSL3_RECORD *recs, unsigned int n_recs, int send);
227 __owur int tls1_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int send);
228 int DTLS_RECORD_LAYER_new(RECORD_LAYER *rl);
229 void DTLS_RECORD_LAYER_free(RECORD_LAYER *rl);
230 void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl);
231 void DTLS_RECORD_LAYER_set_saved_w_epoch(RECORD_LAYER *rl, unsigned short e);
232 void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl);
233 void DTLS_RECORD_LAYER_resync_write(RECORD_LAYER *rl);
234 void DTLS_RECORD_LAYER_set_write_sequence(RECORD_LAYER *rl, unsigned char *seq);
235 __owur int dtls1_read_bytes(SSL *s, int type, int *recvd_type,
236 unsigned char *buf, int len, int peek);
237 __owur int dtls1_write_bytes(SSL *s, int type, const void *buf, int len);
238 __owur int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
239 unsigned int len, int create_empty_fragement);
240 void dtls1_reset_seq_numbers(SSL *s, int rw);