Use the configured max_send_fragment value in the write record layer
[openssl.git] / ssl / record / methods / recmethod_local.h
1 /*
2  * Copyright 2022 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 #include <openssl/bio.h>
11 #include <openssl/ssl.h>
12 #include <openssl/err.h>
13 #include "../../ssl_local.h"
14 #include "../record_local.h"
15
16 typedef struct dtls_bitmap_st {
17     /* Track 64 packets */
18     uint64_t map;
19     /* Max record number seen so far, 64-bit value in big-endian encoding */
20     unsigned char max_seq_num[SEQ_NUM_SIZE];
21 } DTLS_BITMAP;
22
23 /* Protocol version specific function pointers */
24 struct record_functions_st
25 {
26     /*
27      * Returns either OSSL_RECORD_RETURN_SUCCESS, OSSL_RECORD_RETURN_FATAL or
28      * OSSL_RECORD_RETURN_NON_FATAL_ERR if we can keep trying to find an
29      * alternative record layer.
30      */
31     int (*set_crypto_state)(OSSL_RECORD_LAYER *rl, int level,
32                             unsigned char *key, size_t keylen,
33                             unsigned char *iv, size_t ivlen,
34                             unsigned char *mackey, size_t mackeylen,
35                             const EVP_CIPHER *ciph,
36                             size_t taglen,
37                             int mactype,
38                             const EVP_MD *md,
39                             COMP_METHOD *comp);
40
41     /*
42      * Returns:
43      *    0: if the record is publicly invalid, or an internal error, or AEAD
44      *       decryption failed, or EtM decryption failed.
45      *    1: Success or MtE decryption failed (MAC will be randomised)
46      */
47     int (*cipher)(OSSL_RECORD_LAYER *rl, SSL3_RECORD *recs, size_t n_recs,
48                   int sending, SSL_MAC_BUF *macs, size_t macsize);
49     /* Returns 1 for success or 0 for error */
50     int (*mac)(OSSL_RECORD_LAYER *rl, SSL3_RECORD *rec, unsigned char *md,
51                int sending);
52
53     /* Return 1 for success or 0 for error */
54     int (*set_protocol_version)(OSSL_RECORD_LAYER *rl, int version);
55
56     /* Read related functions */
57
58     int (*read_n)(OSSL_RECORD_LAYER *rl, size_t n, size_t max, int extend,
59                   int clearold, size_t *readbytes);
60
61     int (*get_more_records)(OSSL_RECORD_LAYER *rl);
62
63     /* Return 1 for success or 0 for error */
64     int (*validate_record_header)(OSSL_RECORD_LAYER *rl, SSL3_RECORD *rec);
65
66     /* Return 1 for success or 0 for error */
67     int (*post_process_record)(OSSL_RECORD_LAYER *rl, SSL3_RECORD *rec);
68
69     /* Write related functions */
70
71     size_t (*get_max_records)(OSSL_RECORD_LAYER *rl, int type, size_t len,
72                               size_t maxfrag, size_t *preffrag);
73
74     /* Return 1 for success or 0 for error */
75     int (*write_records)(OSSL_RECORD_LAYER *rl, OSSL_RECORD_TEMPLATE *templates,
76                          size_t numtempl);
77 };
78
79 struct ossl_record_layer_st
80 {
81     OSSL_LIB_CTX *libctx;
82     const char *propq;
83     int isdtls;
84     int version;
85     int role;
86     int direction;
87     int level;
88     /* DTLS only */
89     uint16_t epoch;
90
91     /*
92      * A BIO containing any data read in the previous epoch that was destined
93      * for this epoch
94      */
95     BIO *prev;
96
97     /* The transport BIO */
98     BIO *bio;
99
100     /*
101      * A BIO where we will send any data read by us that is destined for the
102      * next epoch.
103      */
104     BIO *next;
105
106     /* Types match the equivalent fields in the SSL object */
107     uint64_t options;
108     uint32_t mode;
109
110     /* write IO goes into here */
111     SSL3_BUFFER wbuf[SSL_MAX_PIPELINES + 1];
112
113     /* Next wbuf with pending data still to write */
114     size_t nextwbuf;
115
116     /* How many pipelines can be used to write data */
117     size_t numwpipes;
118
119     /* read IO goes into here */
120     SSL3_BUFFER rbuf;
121     /* each decoded record goes in here */
122     SSL3_RECORD rrec[SSL_MAX_PIPELINES];
123
124     /* How many records have we got available in the rrec bufer */
125     size_t num_recs;
126
127     /* The record number in the rrec buffer that can be read next */
128     size_t curr_rec;
129
130     /* The number of records that have been released via tls_release_record */
131     size_t num_released;
132
133     /* where we are when reading */
134     int rstate;
135
136     /* used internally to point at a raw packet */
137     unsigned char *packet;
138     size_t packet_length;
139
140     /* Sequence number for the next record */
141     unsigned char sequence[SEQ_NUM_SIZE];
142
143     /* Alert code to be used if an error occurs */
144     int alert;
145
146     /*
147      * Read as many input bytes as possible (for non-blocking reads)
148      */
149     int read_ahead;
150
151     /* The number of consecutive empty records we have received */
152     size_t empty_record_count;
153
154     /*
155      * Do we need to send a prefix empty record before application data as a
156      * countermeasure against known-IV weakness (necessary for SSLv3 and
157      * TLSv1.0)
158      */
159     int need_empty_fragments;
160
161     /* cryptographic state */
162     EVP_CIPHER_CTX *enc_ctx;
163
164     /* Explicit IV length */
165     size_t eivlen;
166
167     /* used for mac generation */
168     EVP_MD_CTX *md_ctx;
169
170     /* compress/uncompress */
171     COMP_CTX *compctx;
172
173     /* Set to 1 if this is the first handshake. 0 otherwise */
174     int is_first_handshake;
175
176     /*
177      * The smaller of the configured and negotiated maximum fragment length
178      * or SSL3_RT_MAX_PLAIN_LENGTH if none
179      */
180     unsigned int max_frag_len;
181
182     /* The maxium amount of early data we can receive/send */
183     uint32_t max_early_data;
184
185     /* The amount of early data that we have sent/received */
186     size_t early_data_count;
187
188     /* TLSv1.3 record padding */
189     size_t block_padding;
190
191     /* Only used by SSLv3 */
192     unsigned char mac_secret[EVP_MAX_MD_SIZE];
193
194     /* TLSv1.0/TLSv1.1/TLSv1.2 */
195     int use_etm;
196
197     /* Flags for GOST ciphers */
198     int stream_mac;
199     int tlstree;
200
201     /* TLSv1.3 fields */
202     /* static IV */
203     unsigned char iv[EVP_MAX_IV_LENGTH];
204     /* static read IV */
205     unsigned char read_iv[EVP_MAX_IV_LENGTH];
206     int allow_plain_alerts;
207
208     /* TLS "any" fields */
209     /* Set to true if this is the first record in a connection */
210     unsigned int is_first_record;
211
212     size_t taglen;
213
214     /* DTLS received handshake records (processed and unprocessed) */
215     record_pqueue unprocessed_rcds;
216     record_pqueue processed_rcds;
217
218     /* records being received in the current epoch */
219     DTLS_BITMAP bitmap;
220     /* renegotiation starts a new set of sequence numbers */
221     DTLS_BITMAP next_bitmap;
222
223     /*
224      * Whether we are currently in a hanshake or not. Only maintained for DTLS
225      */
226     int in_init;
227
228     /* Callbacks */
229     void *cbarg;
230     OSSL_FUNC_rlayer_skip_early_data_fn *skip_early_data;
231     OSSL_FUNC_rlayer_msg_callback_fn *msg_callback;
232     OSSL_FUNC_rlayer_security_fn *security;
233     OSSL_FUNC_rlayer_padding_fn *padding;
234
235     size_t max_pipelines;
236
237     /* Function pointers for version specific functions */
238     struct record_functions_st *funcs;
239 };
240
241 typedef struct dtls_rlayer_record_data_st {
242     unsigned char *packet;
243     size_t packet_length;
244     SSL3_BUFFER rbuf;
245     SSL3_RECORD rrec;
246 } DTLS_RLAYER_RECORD_DATA;
247
248 extern struct record_functions_st ssl_3_0_funcs;
249 extern struct record_functions_st tls_1_funcs;
250 extern struct record_functions_st tls_1_3_funcs;
251 extern struct record_functions_st tls_any_funcs;
252 extern struct record_functions_st dtls_1_funcs;
253 extern struct record_functions_st dtls_any_funcs;
254
255 void ossl_rlayer_fatal(OSSL_RECORD_LAYER *rl, int al, int reason,
256                        const char *fmt, ...);
257
258 #define RLAYERfatal(rl, al, r) RLAYERfatal_data((rl), (al), (r), NULL)
259 #define RLAYERfatal_data                                           \
260     (ERR_new(),                                                    \
261      ERR_set_debug(OPENSSL_FILE, OPENSSL_LINE, OPENSSL_FUNC),      \
262      ossl_rlayer_fatal)
263
264 #define RLAYER_USE_EXPLICIT_IV(rl) ((rl)->version == TLS1_1_VERSION \
265                                     || (rl)->version == TLS1_2_VERSION \
266                                     || (rl)->isdtls)
267
268 int ossl_set_tls_provider_parameters(OSSL_RECORD_LAYER *rl,
269                                      EVP_CIPHER_CTX *ctx,
270                                      const EVP_CIPHER *ciph,
271                                      const EVP_MD *md);
272 /* ssl3_cbc.c */
273 __owur char ssl3_cbc_record_digest_supported(const EVP_MD_CTX *ctx);
274 __owur int ssl3_cbc_digest_record(const EVP_MD *md,
275                                   unsigned char *md_out,
276                                   size_t *md_out_size,
277                                   const unsigned char *header,
278                                   const unsigned char *data,
279                                   size_t data_size,
280                                   size_t data_plus_mac_plus_padding_size,
281                                   const unsigned char *mac_secret,
282                                   size_t mac_secret_length, char is_sslv3);
283
284 int tls_default_read_n(OSSL_RECORD_LAYER *rl, size_t n, size_t max, int extend,
285                        int clearold, size_t *readbytes);
286 int tls_get_more_records(OSSL_RECORD_LAYER *rl);
287 int dtls_get_more_records(OSSL_RECORD_LAYER *rl);
288
289 int tls_default_set_protocol_version(OSSL_RECORD_LAYER *rl, int version);
290 int tls_default_validate_record_header(OSSL_RECORD_LAYER *rl, SSL3_RECORD *re);
291 int tls_do_uncompress(OSSL_RECORD_LAYER *rl, SSL3_RECORD *rec);
292 int tls_default_post_process_record(OSSL_RECORD_LAYER *rl, SSL3_RECORD *rec);
293 int tls13_common_post_process_record(OSSL_RECORD_LAYER *rl, SSL3_RECORD *rec);
294
295 int
296 tls_int_new_record_layer(OSSL_LIB_CTX *libctx, const char *propq, int vers,
297                          int role, int direction, int level, unsigned char *key,
298                          size_t keylen, unsigned char *iv, size_t ivlen,
299                          unsigned char *mackey, size_t mackeylen,
300                          const EVP_CIPHER *ciph, size_t taglen,
301                          int mactype,
302                          const EVP_MD *md, COMP_METHOD *comp, BIO *prev,
303                          BIO *transport, BIO *next,
304                          BIO_ADDR *local, BIO_ADDR *peer,
305                          const OSSL_PARAM *settings, const OSSL_PARAM *options,
306                          const OSSL_DISPATCH *fns, void *cbarg,
307                          OSSL_RECORD_LAYER **retrl);
308 int tls_free(OSSL_RECORD_LAYER *rl);
309 int tls_reset(OSSL_RECORD_LAYER *rl);
310 int tls_unprocessed_read_pending(OSSL_RECORD_LAYER *rl);
311 int tls_processed_read_pending(OSSL_RECORD_LAYER *rl);
312 size_t tls_app_data_pending(OSSL_RECORD_LAYER *rl);
313 int tls_write_pending(OSSL_RECORD_LAYER *rl);
314 size_t tls_get_max_record_len(OSSL_RECORD_LAYER *rl);
315 size_t tls_get_max_records(OSSL_RECORD_LAYER *rl, int type, size_t len,
316                            size_t maxfrag, size_t *preffrag);
317 int tls_write_records(OSSL_RECORD_LAYER *rl, OSSL_RECORD_TEMPLATE *templates,
318                       size_t numtempl);
319 int tls_retry_write_records(OSSL_RECORD_LAYER *rl);
320 int tls_get_alert_code(OSSL_RECORD_LAYER *rl);
321 int tls_set1_bio(OSSL_RECORD_LAYER *rl, BIO *bio);
322 int tls_read_record(OSSL_RECORD_LAYER *rl, void **rechandle, int *rversion,
323                     int *type, unsigned char **data, size_t *datalen,
324                     uint16_t *epoch, unsigned char *seq_num);
325 int tls_release_record(OSSL_RECORD_LAYER *rl, void *rechandle);
326 int tls_default_set_protocol_version(OSSL_RECORD_LAYER *rl, int version);
327 int tls_set_protocol_version(OSSL_RECORD_LAYER *rl, int version);
328 void tls_set_plain_alerts(OSSL_RECORD_LAYER *rl, int allow);
329 void tls_set_first_handshake(OSSL_RECORD_LAYER *rl, int first);
330 void tls_set_max_pipelines(OSSL_RECORD_LAYER *rl, size_t max_pipelines);
331 void tls_get_state(OSSL_RECORD_LAYER *rl, const char **shortstr,
332                    const char **longstr);
333 int tls_set_options(OSSL_RECORD_LAYER *rl, const OSSL_PARAM *options);
334 const COMP_METHOD *tls_get_compression(OSSL_RECORD_LAYER *rl);
335 void tls_set_max_frag_len(OSSL_RECORD_LAYER *rl, size_t max_frag_len);
336 int tls_setup_read_buffer(OSSL_RECORD_LAYER *rl);
337 int tls_setup_write_buffer(OSSL_RECORD_LAYER *rl, size_t numwpipes,
338                            size_t firstlen, size_t nextlen);
339
340 int tls_write_records_multiblock(OSSL_RECORD_LAYER *rl,
341                                  OSSL_RECORD_TEMPLATE *templates,
342                                  size_t numtempl);
343
344 size_t tls_get_max_records_default(OSSL_RECORD_LAYER *rl, int type, size_t len,
345                                    size_t maxfrag, size_t *preffrag);
346 size_t tls_get_max_records_multiblock(OSSL_RECORD_LAYER *rl, int type,
347                                       size_t len, size_t maxfrag,
348                                       size_t *preffrag);
349 int tls_write_records_default(OSSL_RECORD_LAYER *rl,
350                               OSSL_RECORD_TEMPLATE *templates,
351                               size_t numtempl);