Remove the SSL3_RECORD read field
[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     /* epoch number, needed by DTLS1 */
69     /* r */
70     unsigned long epoch;
71     /* sequence number, needed by DTLS1 */
72     /* r */
73     unsigned char seq_num[SEQ_NUM_SIZE];
74 } SSL3_RECORD;
75
76 typedef struct tls_record_st {
77     void *rechandle;
78     int version;
79     int type;
80     /* The data buffer containing bytes from the record */
81     unsigned char *data;
82     /* Number of remaining to be read in the data buffer */
83     size_t length;
84     /* Offset into the data buffer where to start reading */
85     size_t off;
86     /* epoch number. DTLS only */
87     uint16_t epoch;
88     /* sequence number. DTLS only */
89     unsigned char seq_num[SEQ_NUM_SIZE];
90 #ifndef OPENSSL_NO_SCTP
91     struct bio_dgram_sctp_rcvinfo recordinfo;
92 #endif
93 } TLS_RECORD;
94
95 typedef struct dtls1_bitmap_st {
96     /* Track 32 packets on 32-bit systems and 64 - on 64-bit systems */
97     unsigned long map;
98     /* Max record number seen so far, 64-bit value in big-endian encoding */
99     unsigned char max_seq_num[SEQ_NUM_SIZE];
100 } DTLS1_BITMAP;
101
102 typedef struct record_pqueue_st {
103     unsigned short epoch;
104     struct pqueue_st *q;
105 } record_pqueue;
106
107 typedef struct dtls_record_layer_st {
108     /*
109      * The current data and handshake epoch.  This is initially
110      * undefined, and starts at zero once the initial handshake is
111      * completed
112      */
113     unsigned short r_epoch;
114     unsigned short w_epoch;
115
116     /*
117      * Buffered application records. Only for records between CCS and
118      * Finished to prevent either protocol violation or unnecessary message
119      * loss.
120      */
121     record_pqueue buffered_app_data;
122     /* save last and current sequence numbers for retransmissions */
123     unsigned char last_write_sequence[8];
124     unsigned char curr_write_sequence[8];
125 } DTLS_RECORD_LAYER;
126
127 /*****************************************************************************
128  *                                                                           *
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 *
131  * this structure.                                                           *
132  *                                                                           *
133  *****************************************************************************/
134
135 typedef struct record_layer_st {
136     /* The parent SSL_CONNECTION structure */
137     SSL_CONNECTION *s;
138
139     /* Method to use for the read record layer*/
140     const OSSL_RECORD_METHOD *rrlmethod;
141     /* The read record layer object itself */
142     OSSL_RECORD_LAYER *rrl;
143     /* BIO to store data destined for the next read record layer epoch */
144     BIO *rrlnext;
145     /* Default read buffer length to be passed to the record layer */
146     size_t default_read_buf_len;
147
148     /*
149      * Read as many input bytes as possible (for
150      * non-blocking reads)
151      */
152     int read_ahead;
153     /* How many pipelines can be used to write data */
154     size_t numwpipes;
155     /* write IO goes into here */
156     SSL3_BUFFER wbuf[SSL_MAX_PIPELINES];
157     /* number of bytes sent so far */
158     size_t wnum;
159     unsigned char handshake_fragment[4];
160     size_t handshake_fragment_len;
161     /* partial write - check the numbers match */
162     /* number bytes written */
163     size_t wpend_tot;
164     int wpend_type;
165     /* number of bytes submitted */
166     size_t wpend_ret;
167     const unsigned char *wpend_buf;
168
169     unsigned char write_sequence[SEQ_NUM_SIZE];
170     /* Count of the number of consecutive warning alerts received */
171     unsigned int alert_count;
172     DTLS_RECORD_LAYER *d;
173
174     /* How many records we have read from the record layer */
175     size_t num_recs;
176     /* The next record from the record layer that we need to process */
177     size_t curr_rec;
178     /* Record layer data to be processed */
179     TLS_RECORD tlsrecs[SSL_MAX_PIPELINES];
180
181 } RECORD_LAYER;
182
183 /*****************************************************************************
184  *                                                                           *
185  * The following macros/functions represent the libssl internal API to the   *
186  * record layer. Any libssl code may call these functions/macros             *
187  *                                                                           *
188  *****************************************************************************/
189
190 struct ssl_mac_buf_st {
191     unsigned char *mac;
192     int alloced;
193 };
194 typedef struct ssl_mac_buf_st SSL_MAC_BUF;
195
196 #define MIN_SSL2_RECORD_LEN     9
197
198 #define RECORD_LAYER_set_read_ahead(rl, ra)     ((rl)->read_ahead = (ra))
199 #define RECORD_LAYER_get_read_ahead(rl)         ((rl)->read_ahead)
200 #define RECORD_LAYER_get_packet(rl)             ((rl)->packet)
201 #define RECORD_LAYER_add_packet_length(rl, inc) ((rl)->packet_length += (inc))
202 #define DTLS_RECORD_LAYER_get_w_epoch(rl)       ((rl)->d->w_epoch)
203 #define RECORD_LAYER_get_rbuf(rl)               (&(rl)->rbuf)
204 #define RECORD_LAYER_get_wbuf(rl)               ((rl)->wbuf)
205
206 void RECORD_LAYER_init(RECORD_LAYER *rl, SSL_CONNECTION *s);
207 void RECORD_LAYER_clear(RECORD_LAYER *rl);
208 void RECORD_LAYER_release(RECORD_LAYER *rl);
209 int RECORD_LAYER_read_pending(const RECORD_LAYER *rl);
210 int RECORD_LAYER_processed_read_pending(const RECORD_LAYER *rl);
211 int RECORD_LAYER_write_pending(const RECORD_LAYER *rl);
212 void RECORD_LAYER_reset_write_sequence(RECORD_LAYER *rl);
213 int RECORD_LAYER_is_sslv2_record(RECORD_LAYER *rl);
214 __owur size_t ssl3_pending(const SSL *s);
215 __owur int ssl3_write_bytes(SSL *s, int type, const void *buf, size_t len,
216                             size_t *written);
217 int do_ssl3_write(SSL_CONNECTION *s, int type, const unsigned char *buf,
218                   size_t *pipelens, size_t numpipes,
219                   int create_empty_fragment, size_t *written);
220 __owur int ssl3_read_bytes(SSL *s, int type, int *recvd_type,
221                            unsigned char *buf, size_t len, int peek,
222                            size_t *readbytes);
223 __owur int ssl3_setup_buffers(SSL_CONNECTION *s);
224 __owur int ssl3_enc(SSL_CONNECTION *s, SSL3_RECORD *inrecs, size_t n_recs,
225                     int send, SSL_MAC_BUF *mac, size_t macsize);
226 __owur int n_ssl3_mac(SSL_CONNECTION *s, SSL3_RECORD *rec, unsigned char *md,
227                       int send);
228 __owur int ssl3_write_pending(SSL_CONNECTION *s, int type,
229                               const unsigned char *buf, size_t len,
230                               size_t *written);
231 __owur int tls1_enc(SSL_CONNECTION *s, SSL3_RECORD *recs, size_t n_recs,
232                     int sending, SSL_MAC_BUF *mac, size_t macsize);
233 __owur int tls1_mac_old(SSL_CONNECTION *s, SSL3_RECORD *rec, unsigned char *md,
234                         int send);
235 __owur int tls13_enc(SSL_CONNECTION *s, SSL3_RECORD *recs, size_t n_recs,
236                      int send, SSL_MAC_BUF *mac, size_t macsize);
237 int DTLS_RECORD_LAYER_new(RECORD_LAYER *rl);
238 void DTLS_RECORD_LAYER_free(RECORD_LAYER *rl);
239 void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl);
240 void DTLS_RECORD_LAYER_set_saved_w_epoch(RECORD_LAYER *rl, unsigned short e);
241 void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl);
242 void DTLS_RECORD_LAYER_set_write_sequence(RECORD_LAYER *rl, unsigned char *seq);
243 __owur int dtls1_read_bytes(SSL *s, int type, int *recvd_type,
244                             unsigned char *buf, size_t len, int peek,
245                             size_t *readbytes);
246 __owur int dtls1_write_bytes(SSL_CONNECTION *s, int type, const void *buf,
247                              size_t len, size_t *written);
248 int do_dtls1_write(SSL_CONNECTION *s, int type, const unsigned char *buf,
249                    size_t len, int create_empty_fragment, size_t *written);
250 void dtls1_reset_seq_numbers(SSL_CONNECTION *s, int rw);
251 void ssl_release_record(SSL_CONNECTION *s, TLS_RECORD *rr);
252
253 # define HANDLE_RLAYER_RETURN(s, ret) \
254     ossl_tls_handle_rlayer_return(s, ret, OPENSSL_FILE, OPENSSL_LINE)
255
256 int ossl_tls_handle_rlayer_return(SSL_CONNECTION *s, int ret, char *file,
257                                   int line);
258
259 int ssl_set_new_record_layer(SSL_CONNECTION *s, int version, int direction,
260                              int level, unsigned char *key, size_t keylen,
261                              unsigned char *iv,  size_t ivlen,
262                              unsigned char *mackey, size_t mackeylen,
263                              const EVP_CIPHER *ciph, size_t taglen,
264                              int mactype, const EVP_MD *md,
265                              const SSL_COMP *comp);
266
267 # define OSSL_FUNC_RLAYER_SKIP_EARLY_DATA        1
268 OSSL_CORE_MAKE_FUNC(const OSSL_PARAM *, rlayer_skip_early_data, (void *cbarg))
269 # define OSSL_FUNC_RLAYER_MSG_CALLBACK           2
270 OSSL_CORE_MAKE_FUNC(void, rlayer_msg_callback, (int write_p, int version,
271                                                 int content_type,
272                                                 const void *buf, size_t len,
273                                                 void *cbarg))
274 # define OSSL_FUNC_RLAYER_SECURITY               3
275 OSSL_CORE_MAKE_FUNC(int, rlayer_security, (void *cbarg, int op, int bits,
276                                            int nid, void *other))