Make SSL_alloc_buffers() and SSL_free_buffers() work again
[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 */
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     /* The type of data stored in this buffer. Only used for writing */
37     int type;
38 };
39
40 #define SEQ_NUM_SIZE                            8
41
42 typedef struct ssl3_record_st {
43     /* Record layer version */
44     /* r */
45     int rec_version;
46     /* type of record */
47     /* r */
48     int type;
49     /* How many bytes available */
50     /* rw */
51     size_t length;
52     /*
53      * How many bytes were available before padding was removed? This is used
54      * to implement the MAC check in constant time for CBC records.
55      */
56     /* rw */
57     size_t orig_len;
58     /* read/write offset into 'buf' */
59     /* r */
60     size_t off;
61     /* pointer to the record data */
62     /* rw */
63     unsigned char *data;
64     /* where the decode bytes are */
65     /* rw */
66     unsigned char *input;
67     /* only used with decompression - malloc()ed */
68     /* r */
69     unsigned char *comp;
70     /* epoch number, needed by DTLS1 */
71     /* r */
72     uint16_t 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     /* epoch number. DTLS only */
89     uint16_t epoch;
90     /* sequence number. DTLS only */
91     unsigned char seq_num[SEQ_NUM_SIZE];
92 #ifndef OPENSSL_NO_SCTP
93     struct bio_dgram_sctp_rcvinfo recordinfo;
94 #endif
95 } TLS_RECORD;
96
97 typedef struct record_pqueue_st {
98     uint16_t epoch;
99     struct pqueue_st *q;
100 } record_pqueue;
101
102 typedef struct dtls_record_layer_st {
103     /*
104      * The current data and handshake epoch.  This is initially
105      * undefined, and starts at zero once the initial handshake is
106      * completed
107      */
108     uint16_t r_epoch;
109     uint16_t w_epoch;
110
111     /*
112      * Buffered application records. Only for records between CCS and
113      * Finished to prevent either protocol violation or unnecessary message
114      * loss.
115      */
116     record_pqueue buffered_app_data;
117 } DTLS_RECORD_LAYER;
118
119 /*****************************************************************************
120  *                                                                           *
121  * This structure should be considered "opaque" to anything outside of the   *
122  * record layer. No non-record layer code should be accessing the members of *
123  * this structure.                                                           *
124  *                                                                           *
125  *****************************************************************************/
126
127 typedef struct record_layer_st {
128     /* The parent SSL_CONNECTION structure */
129     SSL_CONNECTION *s;
130
131     /* Method to use for the read record layer*/
132     const OSSL_RECORD_METHOD *rrlmethod;
133     /* Method to use for the write record layer*/
134     const OSSL_RECORD_METHOD *wrlmethod;
135     /* The read record layer object itself */
136     OSSL_RECORD_LAYER *rrl;
137     /* The write record layer object itself */
138     OSSL_RECORD_LAYER *wrl;
139     /* BIO to store data destined for the next read record layer epoch */
140     BIO *rrlnext;
141     /* Default read buffer length to be passed to the record layer */
142     size_t default_read_buf_len;
143
144     /*
145      * Read as many input bytes as possible (for
146      * non-blocking reads)
147      */
148     int read_ahead;
149
150     /* number of bytes sent so far */
151     size_t wnum;
152     unsigned char handshake_fragment[4];
153     size_t handshake_fragment_len;
154     /* partial write - check the numbers match */
155     /* number bytes written */
156     size_t wpend_tot;
157     int wpend_type;
158     /* number of bytes submitted */
159     size_t wpend_ret;
160     const unsigned char *wpend_buf;
161
162     /* Count of the number of consecutive warning alerts received */
163     unsigned int alert_count;
164     DTLS_RECORD_LAYER *d;
165
166     /* TLS1.3 padding callback */
167     size_t (*record_padding_cb)(SSL *s, int type, size_t len, void *arg);
168     void *record_padding_arg;
169     size_t block_padding;
170
171     /* How many records we have read from the record layer */
172     size_t num_recs;
173     /* The next record from the record layer that we need to process */
174     size_t curr_rec;
175     /* Record layer data to be processed */
176     TLS_RECORD tlsrecs[SSL_MAX_PIPELINES];
177
178 } RECORD_LAYER;
179
180 /*****************************************************************************
181  *                                                                           *
182  * The following macros/functions represent the libssl internal API to the   *
183  * record layer. Any libssl code may call these functions/macros             *
184  *                                                                           *
185  *****************************************************************************/
186
187 struct ssl_mac_buf_st {
188     unsigned char *mac;
189     int alloced;
190 };
191 typedef struct ssl_mac_buf_st SSL_MAC_BUF;
192
193 #define MIN_SSL2_RECORD_LEN     9
194
195 #define RECORD_LAYER_set_read_ahead(rl, ra)     ((rl)->read_ahead = (ra))
196 #define RECORD_LAYER_get_read_ahead(rl)         ((rl)->read_ahead)
197 #define RECORD_LAYER_get_packet(rl)             ((rl)->packet)
198 #define RECORD_LAYER_add_packet_length(rl, inc) ((rl)->packet_length += (inc))
199 #define DTLS_RECORD_LAYER_get_w_epoch(rl)       ((rl)->d->w_epoch)
200 #define RECORD_LAYER_get_rbuf(rl)               (&(rl)->rbuf)
201 #define RECORD_LAYER_get_wbuf(rl)               ((rl)->wbuf)
202
203 void RECORD_LAYER_init(RECORD_LAYER *rl, SSL_CONNECTION *s);
204 void RECORD_LAYER_clear(RECORD_LAYER *rl);
205 int RECORD_LAYER_read_pending(const RECORD_LAYER *rl);
206 int RECORD_LAYER_processed_read_pending(const RECORD_LAYER *rl);
207 int RECORD_LAYER_write_pending(const RECORD_LAYER *rl);
208 int RECORD_LAYER_is_sslv2_record(RECORD_LAYER *rl);
209 __owur size_t ssl3_pending(const SSL *s);
210 __owur int ssl3_write_bytes(SSL *s, int type, const void *buf, size_t len,
211                             size_t *written);
212 __owur int ssl3_read_bytes(SSL *s, int type, int *recvd_type,
213                            unsigned char *buf, size_t len, int peek,
214                            size_t *readbytes);
215
216 int DTLS_RECORD_LAYER_new(RECORD_LAYER *rl);
217 void DTLS_RECORD_LAYER_free(RECORD_LAYER *rl);
218 void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl);
219 void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl);
220 __owur int dtls1_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 dtls1_write_bytes(SSL_CONNECTION *s, int type, const void *buf,
224                              size_t len, size_t *written);
225 int do_dtls1_write(SSL_CONNECTION *s, int type, const unsigned char *buf,
226                    size_t len, size_t *written);
227 void dtls1_increment_epoch(SSL_CONNECTION *s, int rw);
228 void ssl_release_record(SSL_CONNECTION *s, TLS_RECORD *rr);
229
230 # define HANDLE_RLAYER_READ_RETURN(s, ret) \
231     ossl_tls_handle_rlayer_return(s, 0, ret, OPENSSL_FILE, OPENSSL_LINE)
232
233 # define HANDLE_RLAYER_WRITE_RETURN(s, ret) \
234     ossl_tls_handle_rlayer_return(s, 1, ret, OPENSSL_FILE, OPENSSL_LINE)
235
236 int ossl_tls_handle_rlayer_return(SSL_CONNECTION *s, int writing, int ret,
237                                   char *file, int line);
238
239 int ssl_set_new_record_layer(SSL_CONNECTION *s, int version, int direction,
240                              int level, unsigned char *key, size_t keylen,
241                              unsigned char *iv,  size_t ivlen,
242                              unsigned char *mackey, size_t mackeylen,
243                              const EVP_CIPHER *ciph, size_t taglen,
244                              int mactype, const EVP_MD *md,
245                              const SSL_COMP *comp);
246 int ssl_set_record_protocol_version(SSL_CONNECTION *s, int vers);
247
248 # define OSSL_FUNC_RLAYER_SKIP_EARLY_DATA        1
249 OSSL_CORE_MAKE_FUNC(int, rlayer_skip_early_data, (void *cbarg))
250 # define OSSL_FUNC_RLAYER_MSG_CALLBACK           2
251 OSSL_CORE_MAKE_FUNC(void, rlayer_msg_callback, (int write_p, int version,
252                                                 int content_type,
253                                                 const void *buf, size_t len,
254                                                 void *cbarg))
255 # define OSSL_FUNC_RLAYER_SECURITY               3
256 OSSL_CORE_MAKE_FUNC(int, rlayer_security, (void *cbarg, int op, int bits,
257                                            int nid, void *other))
258 # define OSSL_FUNC_RLAYER_PADDING                4
259 OSSL_CORE_MAKE_FUNC(size_t, rlayer_padding, (void *cbarg, int type, size_t len))