Move early data counting out of the SSL object and into the record layer
[openssl.git] / ssl / record / record.h
1 /*
2  * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 typedef struct ssl_connection_st SSL_CONNECTION;
11 typedef struct ssl3_buffer_st SSL3_BUFFER;
12
13 #include <openssl/core_dispatch.h>
14 #include "recordmethod.h"
15
16 /*****************************************************************************
17  *                                                                           *
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.        *
20  *                                                                           *
21  *****************************************************************************/
22
23 struct ssl3_buffer_st {
24     /* at least SSL3_RT_MAX_PACKET_SIZE bytes, see ssl3_setup_buffers() */
25     unsigned char *buf;
26     /* default buffer size (or 0 if no default set) */
27     size_t default_len;
28     /* buffer size */
29     size_t len;
30     /* where to 'copy from' */
31     size_t offset;
32     /* how many bytes left */
33     size_t left;
34     /* 'buf' is from application for KTLS */
35     int app_buffer;
36 };
37
38 #define SEQ_NUM_SIZE                            8
39
40 typedef struct ssl3_record_st {
41     /* Record layer version */
42     /* r */
43     int rec_version;
44     /* type of record */
45     /* r */
46     int type;
47     /* How many bytes available */
48     /* rw */
49     size_t length;
50     /*
51      * How many bytes were available before padding was removed? This is used
52      * to implement the MAC check in constant time for CBC records.
53      */
54     /* rw */
55     size_t orig_len;
56     /* read/write offset into 'buf' */
57     /* r */
58     size_t off;
59     /* pointer to the record data */
60     /* rw */
61     unsigned char *data;
62     /* where the decode bytes are */
63     /* rw */
64     unsigned char *input;
65     /* only used with decompression - malloc()ed */
66     /* r */
67     unsigned char *comp;
68     /* Whether the data from this record has already been read or not */
69     /* r */
70     unsigned int read;
71     /* epoch number, needed by DTLS1 */
72     /* r */
73     unsigned long epoch;
74     /* sequence number, needed by DTLS1 */
75     /* r */
76     unsigned char seq_num[SEQ_NUM_SIZE];
77 } SSL3_RECORD;
78
79 typedef struct tls_record_st {
80     void *rechandle;
81     int version;
82     int type;
83     /* The data buffer containing bytes from the record */
84     unsigned char *data;
85     /* Number of remaining to be read in the data buffer */
86     size_t length;
87     /* Offset into the data buffer where to start reading */
88     size_t off;
89 } TLS_RECORD;
90
91 typedef struct dtls1_bitmap_st {
92     /* Track 32 packets on 32-bit systems and 64 - on 64-bit systems */
93     unsigned long map;
94     /* Max record number seen so far, 64-bit value in big-endian encoding */
95     unsigned char max_seq_num[SEQ_NUM_SIZE];
96 } DTLS1_BITMAP;
97
98 typedef struct record_pqueue_st {
99     unsigned short epoch;
100     struct pqueue_st *q;
101 } record_pqueue;
102
103 typedef struct dtls1_record_data_st {
104     unsigned char *packet;
105     size_t packet_length;
106     SSL3_BUFFER rbuf;
107     SSL3_RECORD rrec;
108 #ifndef OPENSSL_NO_SCTP
109     struct bio_dgram_sctp_rcvinfo recordinfo;
110 #endif
111 } DTLS1_RECORD_DATA;
112
113 typedef struct dtls_record_layer_st {
114     /*
115      * The current data and handshake epoch.  This is initially
116      * undefined, and starts at zero once the initial handshake is
117      * completed
118      */
119     unsigned short r_epoch;
120     unsigned short w_epoch;
121     /* records being received in the current epoch */
122     DTLS1_BITMAP bitmap;
123     /* renegotiation starts a new set of sequence numbers */
124     DTLS1_BITMAP next_bitmap;
125     /* Received handshake records (processed and unprocessed) */
126     record_pqueue unprocessed_rcds;
127     record_pqueue processed_rcds;
128     /*
129      * Buffered application records. Only for records between CCS and
130      * Finished to prevent either protocol violation or unnecessary message
131      * loss.
132      */
133     record_pqueue buffered_app_data;
134     /* save last and current sequence numbers for retransmissions */
135     unsigned char last_write_sequence[8];
136     unsigned char curr_write_sequence[8];
137 } DTLS_RECORD_LAYER;
138
139 /*****************************************************************************
140  *                                                                           *
141  * This structure should be considered "opaque" to anything outside of the   *
142  * record layer. No non-record layer code should be accessing the members of *
143  * this structure.                                                           *
144  *                                                                           *
145  *****************************************************************************/
146
147 typedef struct record_layer_st {
148     /* The parent SSL_CONNECTION structure */
149     SSL_CONNECTION *s;
150     /*
151      * Read as many input bytes as possible (for
152      * non-blocking reads)
153      */
154     int read_ahead;
155     /* where we are when reading */
156     int rstate;
157     /* How many pipelines can be used to read data */
158     size_t numrpipes;
159     /* How many pipelines can be used to write data */
160     size_t numwpipes;
161     /* read IO goes into here */
162     SSL3_BUFFER rbuf;
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];
167     /* used internally to point at a raw packet */
168     unsigned char *packet;
169     size_t packet_length;
170     /* number of bytes sent so far */
171     size_t wnum;
172     unsigned char handshake_fragment[4];
173     size_t handshake_fragment_len;
174     /* The number of consecutive empty records we have received */
175     size_t empty_record_count;
176     /* partial write - check the numbers match */
177     /* number bytes written */
178     size_t wpend_tot;
179     int wpend_type;
180     /* number of bytes submitted */
181     size_t wpend_ret;
182     const unsigned char *wpend_buf;
183     unsigned char read_sequence[SEQ_NUM_SIZE];
184     unsigned char write_sequence[SEQ_NUM_SIZE];
185     /* Set to true if this is the first record in a connection */
186     unsigned int is_first_record;
187     /* Count of the number of consecutive warning alerts received */
188     unsigned int alert_count;
189     DTLS_RECORD_LAYER *d;
190
191     /* TODO(RECLAYER): Tidy me up. New fields for record management */
192
193     /* How many records we have read from the record layer */
194     size_t num_recs;
195     /* The next record from the record layer that we need to process */
196     size_t curr_rec;
197     /* Record layer data to be processed */
198     TLS_RECORD tlsrecs[SSL_MAX_PIPELINES];
199
200 } RECORD_LAYER;
201
202 /*****************************************************************************
203  *                                                                           *
204  * The following macros/functions represent the libssl internal API to the   *
205  * record layer. Any libssl code may call these functions/macros             *
206  *                                                                           *
207  *****************************************************************************/
208
209 struct ssl_mac_buf_st {
210     unsigned char *mac;
211     int alloced;
212 };
213 typedef struct ssl_mac_buf_st SSL_MAC_BUF;
214
215 #define MIN_SSL2_RECORD_LEN     9
216
217 #define RECORD_LAYER_set_read_ahead(rl, ra)     ((rl)->read_ahead = (ra))
218 #define RECORD_LAYER_get_read_ahead(rl)         ((rl)->read_ahead)
219 #define RECORD_LAYER_get_packet(rl)             ((rl)->packet)
220 #define RECORD_LAYER_get_packet_length(rl)      ((rl)->packet_length)
221 #define RECORD_LAYER_add_packet_length(rl, inc) ((rl)->packet_length += (inc))
222 #define DTLS_RECORD_LAYER_get_w_epoch(rl)       ((rl)->d->w_epoch)
223 #define DTLS_RECORD_LAYER_get_processed_rcds(rl) \
224                                                 ((rl)->d->processed_rcds)
225 #define DTLS_RECORD_LAYER_get_unprocessed_rcds(rl) \
226                                                 ((rl)->d->unprocessed_rcds)
227 #define RECORD_LAYER_get_rbuf(rl)               (&(rl)->rbuf)
228 #define RECORD_LAYER_get_wbuf(rl)               ((rl)->wbuf)
229
230 void RECORD_LAYER_init(RECORD_LAYER *rl, SSL_CONNECTION *s);
231 void RECORD_LAYER_clear(RECORD_LAYER *rl);
232 void RECORD_LAYER_release(RECORD_LAYER *rl);
233 int RECORD_LAYER_read_pending(const RECORD_LAYER *rl);
234 int RECORD_LAYER_processed_read_pending(const RECORD_LAYER *rl);
235 int RECORD_LAYER_write_pending(const RECORD_LAYER *rl);
236 void RECORD_LAYER_reset_read_sequence(RECORD_LAYER *rl);
237 void RECORD_LAYER_reset_write_sequence(RECORD_LAYER *rl);
238 int RECORD_LAYER_is_sslv2_record(RECORD_LAYER *rl);
239 size_t RECORD_LAYER_get_rrec_length(RECORD_LAYER *rl);
240 __owur size_t ssl3_pending(const SSL *s);
241 __owur int ssl3_write_bytes(SSL *s, int type, const void *buf, size_t len,
242                             size_t *written);
243 int do_ssl3_write(SSL_CONNECTION *s, int type, const unsigned char *buf,
244                   size_t *pipelens, size_t numpipes,
245                   int create_empty_fragment, size_t *written);
246 __owur int ssl3_read_bytes(SSL *s, int type, int *recvd_type,
247                            unsigned char *buf, size_t len, int peek,
248                            size_t *readbytes);
249 __owur int ssl3_setup_buffers(SSL_CONNECTION *s);
250 __owur int ssl3_enc(SSL_CONNECTION *s, SSL3_RECORD *inrecs, size_t n_recs,
251                     int send, SSL_MAC_BUF *mac, size_t macsize);
252 __owur int n_ssl3_mac(SSL_CONNECTION *s, SSL3_RECORD *rec, unsigned char *md,
253                       int send);
254 __owur int ssl3_write_pending(SSL_CONNECTION *s, int type,
255                               const unsigned char *buf, size_t len,
256                               size_t *written);
257 __owur int tls1_enc(SSL_CONNECTION *s, SSL3_RECORD *recs, size_t n_recs,
258                     int sending, SSL_MAC_BUF *mac, size_t macsize);
259 __owur int tls1_mac_old(SSL_CONNECTION *s, SSL3_RECORD *rec, unsigned char *md,
260                         int send);
261 __owur int tls13_enc(SSL_CONNECTION *s, SSL3_RECORD *recs, size_t n_recs,
262                      int send, SSL_MAC_BUF *mac, size_t macsize);
263 int DTLS_RECORD_LAYER_new(RECORD_LAYER *rl);
264 void DTLS_RECORD_LAYER_free(RECORD_LAYER *rl);
265 void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl);
266 void DTLS_RECORD_LAYER_set_saved_w_epoch(RECORD_LAYER *rl, unsigned short e);
267 void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl);
268 void DTLS_RECORD_LAYER_set_write_sequence(RECORD_LAYER *rl, unsigned char *seq);
269 __owur int dtls1_read_bytes(SSL *s, int type, int *recvd_type,
270                             unsigned char *buf, size_t len, int peek,
271                             size_t *readbytes);
272 __owur int dtls1_write_bytes(SSL_CONNECTION *s, int type, const void *buf,
273                              size_t len, size_t *written);
274 int do_dtls1_write(SSL_CONNECTION *s, int type, const unsigned char *buf,
275                    size_t len, int create_empty_fragment, size_t *written);
276 void dtls1_reset_seq_numbers(SSL_CONNECTION *s, int rw);
277 int dtls_buffer_listen_record(SSL_CONNECTION *s, size_t len, unsigned char *seq,
278                               size_t off);
279
280
281 # define HANDLE_RLAYER_RETURN(s, ret) \
282     ossl_tls_handle_rlayer_return(s, ret, OPENSSL_FILE, OPENSSL_LINE)
283
284 int ossl_tls_handle_rlayer_return(SSL_CONNECTION *s, int ret, char *file,
285                                   int line);
286
287 int ssl_set_new_record_layer(SSL_CONNECTION *s, int version, int direction,
288                              int level, unsigned char *key, size_t keylen,
289                              unsigned char *iv,  size_t ivlen,
290                              unsigned char *mackey, size_t mackeylen,
291                              const EVP_CIPHER *ciph, size_t taglen,
292                              int mactype, const EVP_MD *md,
293                              const SSL_COMP *comp);
294
295 # define OSSL_FUNC_RLAYER_SKIP_EARLY_DATA        1
296 OSSL_CORE_MAKE_FUNC(const OSSL_PARAM *, rlayer_skip_early_data,(void *cbarg))