Address feedback on SSLv2 ClientHello processing
[openssl.git] / ssl / record / record.h
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 /*****************************************************************************
11  *                                                                           *
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.        *
14  *                                                                           *
15  *****************************************************************************/
16
17 typedef struct ssl3_buffer_st {
18     /* at least SSL3_RT_MAX_PACKET_SIZE bytes, see ssl3_setup_buffers() */
19     unsigned char *buf;
20     /* default buffer size (or 0 if no default set) */
21     size_t default_len;
22     /* buffer size */
23     size_t len;
24     /* where to 'copy from' */
25     int offset;
26     /* how many bytes left */
27     int left;
28 } SSL3_BUFFER;
29
30 #define SEQ_NUM_SIZE                            8
31
32 typedef struct ssl3_record_st {
33     /* Record layer version */
34     /* r */
35     int rec_version;
36
37     /* type of record */
38     /* r */
39     int type;
40
41     /* How many bytes available */
42     /* rw */
43     unsigned int length;
44
45     /*
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.
48      */
49     /* rw */
50     unsigned int orig_len;
51
52     /* read/write offset into 'buf' */
53     /* r */
54     unsigned int off;
55
56     /* pointer to the record data */
57     /* rw */
58     unsigned char *data;
59
60     /* where the decode bytes are */
61     /* rw */
62     unsigned char *input;
63
64     /* only used with decompression - malloc()ed */
65     /* r */
66     unsigned char *comp;
67
68     /* Whether the data from this record has already been read or not */
69     /* r */
70     unsigned int read;
71
72     /* epoch number, needed by DTLS1 */
73     /* r */
74     unsigned long epoch;
75
76     /* sequence number, needed by DTLS1 */
77     /* r */
78     unsigned char seq_num[SEQ_NUM_SIZE];
79 } SSL3_RECORD;
80
81 typedef struct dtls1_bitmap_st {
82     /* Track 32 packets on 32-bit systems and 64 - on 64-bit systems */
83     unsigned long map;
84
85     /* Max record number seen so far, 64-bit value in big-endian encoding */
86     unsigned char max_seq_num[SEQ_NUM_SIZE];
87 } DTLS1_BITMAP;
88
89 typedef struct record_pqueue_st {
90     unsigned short epoch;
91     struct pqueue_st *q;
92 } record_pqueue;
93
94 typedef struct dtls1_record_data_st {
95     unsigned char *packet;
96     unsigned int packet_length;
97     SSL3_BUFFER rbuf;
98     SSL3_RECORD rrec;
99 #  ifndef OPENSSL_NO_SCTP
100     struct bio_dgram_sctp_rcvinfo recordinfo;
101 #  endif
102 } DTLS1_RECORD_DATA;
103
104
105 typedef struct dtls_record_layer_st {
106     /*
107      * The current data and handshake epoch.  This is initially
108      * undefined, and starts at zero once the initial handshake is
109      * completed
110      */
111     unsigned short r_epoch;
112     unsigned short w_epoch;
113
114     /* records being received in the current epoch */
115     DTLS1_BITMAP bitmap;
116     /* renegotiation starts a new set of sequence numbers */
117     DTLS1_BITMAP next_bitmap;
118
119     /* Received handshake records (processed and unprocessed) */
120     record_pqueue unprocessed_rcds;
121     record_pqueue processed_rcds;
122     /*
123      * Buffered application records. Only for records between CCS and
124      * Finished to prevent either protocol violation or unnecessary message
125      * loss.
126      */
127     record_pqueue buffered_app_data;
128     /*
129      * storage for Alert/Handshake protocol data received but not yet
130      * processed by ssl3_read_bytes:
131      */
132     unsigned char alert_fragment[DTLS1_AL_HEADER_LENGTH];
133     unsigned int alert_fragment_len;
134     unsigned char handshake_fragment[DTLS1_HM_HEADER_LENGTH];
135     unsigned int handshake_fragment_len;
136
137     /* save last and current sequence numbers for retransmissions */
138     unsigned char last_write_sequence[8];
139     unsigned char curr_write_sequence[8];
140 } DTLS_RECORD_LAYER;
141
142 /*****************************************************************************
143  *                                                                           *
144  * This structure should be considered "opaque" to anything outside of the   *
145  * record layer. No non-record layer code should be accessing the members of *
146  * this structure.                                                           *
147  *                                                                           *
148  *****************************************************************************/
149
150 typedef struct record_layer_st {
151     /* The parent SSL structure */
152     SSL *s;
153     /*
154      * Read as many input bytes as possible (for
155      * non-blocking reads)
156      */
157     int read_ahead;
158     /* where we are when reading */
159     int rstate;
160
161     /* How many pipelines can be used to read data */
162     unsigned int numrpipes;
163     /* How many pipelines can be used to write data */
164     unsigned int numwpipes;
165     /* read IO goes into here */
166     SSL3_BUFFER rbuf;
167     /* write IO goes into here */
168     SSL3_BUFFER wbuf[SSL_MAX_PIPELINES];
169     /* each decoded record goes in here */
170     SSL3_RECORD rrec[SSL_MAX_PIPELINES];
171
172     /* used internally to point at a raw packet */
173     unsigned char *packet;
174     unsigned int packet_length;
175
176     /* number of bytes sent so far */
177     unsigned int wnum;
178
179     /*
180      * storage for Alert/Handshake protocol data received but not yet
181      * processed by ssl3_read_bytes:
182      */
183     unsigned char alert_fragment[2];
184     unsigned int alert_fragment_len;
185     unsigned char handshake_fragment[4];
186     unsigned int handshake_fragment_len;
187
188     /* The number of consecutive empty records we have received */
189     unsigned int empty_record_count;
190
191     /* partial write - check the numbers match */
192     /* number bytes written */
193     int wpend_tot;
194     int wpend_type;
195     /* number of bytes submitted */
196     int wpend_ret;
197     const unsigned char *wpend_buf;
198
199     unsigned char read_sequence[SEQ_NUM_SIZE];
200     unsigned char write_sequence[SEQ_NUM_SIZE];
201
202     /* Set to true if this is the first record in a connection */
203     unsigned int is_first_record;
204
205     DTLS_RECORD_LAYER *d;
206 } RECORD_LAYER;
207
208
209 /*****************************************************************************
210  *                                                                           *
211  * The following macros/functions represent the libssl internal API to the   *
212  * record layer. Any libssl code may call these functions/macros             *
213  *                                                                           *
214  *****************************************************************************/
215
216 #define MIN_SSL2_RECORD_LEN     9
217
218 #define RECORD_LAYER_set_read_ahead(rl, ra)     ((rl)->read_ahead = (ra))
219 #define RECORD_LAYER_get_read_ahead(rl)         ((rl)->read_ahead)
220 #define RECORD_LAYER_get_packet(rl)             ((rl)->packet)
221 #define RECORD_LAYER_get_packet_length(rl)      ((rl)->packet_length)
222 #define RECORD_LAYER_add_packet_length(rl, inc) ((rl)->packet_length += (inc))
223 #define DTLS_RECORD_LAYER_get_w_epoch(rl)       ((rl)->d->w_epoch)
224 #define DTLS_RECORD_LAYER_get_processed_rcds(rl) \
225                                                 ((rl)->d->processed_rcds)
226 #define DTLS_RECORD_LAYER_get_unprocessed_rcds(rl) \
227                                                 ((rl)->d->unprocessed_rcds)
228
229 void RECORD_LAYER_init(RECORD_LAYER *rl, SSL *s);
230 void RECORD_LAYER_clear(RECORD_LAYER *rl);
231 void RECORD_LAYER_release(RECORD_LAYER *rl);
232 int RECORD_LAYER_read_pending(const RECORD_LAYER *rl);
233 int RECORD_LAYER_write_pending(const RECORD_LAYER *rl);
234 int RECORD_LAYER_set_data(RECORD_LAYER *rl, const unsigned char *buf, int len);
235 void RECORD_LAYER_reset_read_sequence(RECORD_LAYER *rl);
236 void RECORD_LAYER_reset_write_sequence(RECORD_LAYER *rl);
237 int RECORD_LAYER_is_sslv2_record(RECORD_LAYER *rl);
238 unsigned int RECORD_LAYER_get_rrec_length(RECORD_LAYER *rl);
239 __owur int ssl3_pending(const SSL *s);
240 __owur int ssl3_write_bytes(SSL *s, int type, const void *buf, int len);
241 __owur int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
242                          unsigned int *pipelens, unsigned int numpipes,
243                          int create_empty_fragment);
244 __owur int ssl3_read_bytes(SSL *s, int type, int *recvd_type,
245                            unsigned char *buf, int len, int peek);
246 __owur int ssl3_setup_buffers(SSL *s);
247 __owur int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, unsigned int n_recs, int send);
248 __owur int n_ssl3_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int send);
249 __owur int ssl3_write_pending(SSL *s, int type, const unsigned char *buf,
250                        unsigned int len);
251 __owur int tls1_enc(SSL *s, SSL3_RECORD *recs, unsigned int n_recs, int send);
252 __owur int tls1_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int send);
253 int DTLS_RECORD_LAYER_new(RECORD_LAYER *rl);
254 void DTLS_RECORD_LAYER_free(RECORD_LAYER *rl);
255 void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl);
256 void DTLS_RECORD_LAYER_set_saved_w_epoch(RECORD_LAYER *rl, unsigned short e);
257 void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl);
258 void DTLS_RECORD_LAYER_resync_write(RECORD_LAYER *rl);
259 void DTLS_RECORD_LAYER_set_write_sequence(RECORD_LAYER *rl, unsigned char *seq);
260 __owur int dtls1_read_bytes(SSL *s, int type, int *recvd_type,
261                             unsigned char *buf, int len, int peek);
262 __owur int dtls1_write_bytes(SSL *s, int type, const void *buf, int len);
263 __owur int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
264                    unsigned int len, int create_empty_fragement);
265 void dtls1_reset_seq_numbers(SSL *s, int rw);
266