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