Convert dtls_write_records to use standard record layer functions
[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     /* Allocate the rl->wbuf buffers. Return 1 for success or 0 for error */
79     int (*allocate_write_buffers)(OSSL_RECORD_LAYER *rl,
80                                   OSSL_RECORD_TEMPLATE *templates,
81                                   size_t numtempl, size_t *prefix);
82
83     /*
84      * Initialise the packets in the |pkt| array using the buffers in |rl->wbuf|.
85      * Some protocol versions may use the space in |prefixtempl| to add
86      * an artificial template in front of the |templates| array and hence may
87      * initialise 1 more WPACKET than there are templates. |*wpinited|
88      * returns the number of WPACKETs in |pkt| that were successfully
89      * initialised. This must be 0 on entry and will be filled in even on error.
90      */
91     int (*initialise_write_packets)(OSSL_RECORD_LAYER *rl,
92                                     OSSL_RECORD_TEMPLATE *templates,
93                                     size_t numtempl,
94                                     OSSL_RECORD_TEMPLATE *prefixtempl,
95                                     WPACKET *pkt,
96                                     SSL3_BUFFER *bufs,
97                                     size_t *wpinited);
98
99     /* Get the actual record type to be used for a given template */
100     unsigned int (*get_record_type)(OSSL_RECORD_LAYER *rl,
101                                     OSSL_RECORD_TEMPLATE *template);
102
103     /* Write the record header data to the WPACKET */
104     int (*prepare_record_header)(OSSL_RECORD_LAYER *rl, WPACKET *thispkt,
105                                  OSSL_RECORD_TEMPLATE *templ,
106                                  unsigned int rectype,
107                                  unsigned char **recdata);
108
109     int (*add_record_padding)(OSSL_RECORD_LAYER *rl,
110                               OSSL_RECORD_TEMPLATE *thistempl,
111                               WPACKET *thispkt,
112                               SSL3_RECORD *thiswr);
113
114     /*
115      * This applies any mac that might be necessary, ensures that we have enough
116      * space in the WPACKET to perform the encryption and sets up the
117      * SSL3_RECORD ready for that encryption.
118      */
119     int (*prepare_for_encryption)(OSSL_RECORD_LAYER *rl,
120                                   size_t mac_size,
121                                   WPACKET *thispkt,
122                                   SSL3_RECORD *thiswr);
123
124     /*
125      * Any updates required to the record after encryption has been applied. For
126      * example, adding a MAC if using encrypt-then-mac
127      */
128     int (*post_encryption_processing)(OSSL_RECORD_LAYER *rl,
129                                       size_t mac_size,
130                                       OSSL_RECORD_TEMPLATE *thistempl,
131                                       WPACKET *thispkt,
132                                       SSL3_RECORD *thiswr);
133
134     /*
135      * Some record layer implementations need to do some custom preparation of
136      * the BIO before we write to it. KTLS does this to prevent coalescing of
137      * control and data messages.
138      */
139     int (*prepare_write_bio)(OSSL_RECORD_LAYER *rl, int type);
140 };
141
142 struct ossl_record_layer_st
143 {
144     OSSL_LIB_CTX *libctx;
145     const char *propq;
146     int isdtls;
147     int version;
148     int role;
149     int direction;
150     int level;
151     /* DTLS only */
152     uint16_t epoch;
153
154     /*
155      * A BIO containing any data read in the previous epoch that was destined
156      * for this epoch
157      */
158     BIO *prev;
159
160     /* The transport BIO */
161     BIO *bio;
162
163     /*
164      * A BIO where we will send any data read by us that is destined for the
165      * next epoch.
166      */
167     BIO *next;
168
169     /* Types match the equivalent fields in the SSL object */
170     uint64_t options;
171     uint32_t mode;
172
173     /* write IO goes into here */
174     SSL3_BUFFER wbuf[SSL_MAX_PIPELINES + 1];
175
176     /* Next wbuf with pending data still to write */
177     size_t nextwbuf;
178
179     /* How many pipelines can be used to write data */
180     size_t numwpipes;
181
182     /* read IO goes into here */
183     SSL3_BUFFER rbuf;
184     /* each decoded record goes in here */
185     SSL3_RECORD rrec[SSL_MAX_PIPELINES];
186
187     /* How many records have we got available in the rrec bufer */
188     size_t num_recs;
189
190     /* The record number in the rrec buffer that can be read next */
191     size_t curr_rec;
192
193     /* The number of records that have been released via tls_release_record */
194     size_t num_released;
195
196     /* where we are when reading */
197     int rstate;
198
199     /* used internally to point at a raw packet */
200     unsigned char *packet;
201     size_t packet_length;
202
203     /* Sequence number for the next record */
204     unsigned char sequence[SEQ_NUM_SIZE];
205
206     /* Alert code to be used if an error occurs */
207     int alert;
208
209     /*
210      * Read as many input bytes as possible (for non-blocking reads)
211      */
212     int read_ahead;
213
214     /* The number of consecutive empty records we have received */
215     size_t empty_record_count;
216
217     /*
218      * Do we need to send a prefix empty record before application data as a
219      * countermeasure against known-IV weakness (necessary for SSLv3 and
220      * TLSv1.0)
221      */
222     int need_empty_fragments;
223
224     /* cryptographic state */
225     EVP_CIPHER_CTX *enc_ctx;
226
227     /* Explicit IV length */
228     size_t eivlen;
229
230     /* used for mac generation */
231     EVP_MD_CTX *md_ctx;
232
233     /* compress/uncompress */
234     COMP_CTX *compctx;
235
236     /* Set to 1 if this is the first handshake. 0 otherwise */
237     int is_first_handshake;
238
239     /*
240      * The smaller of the configured and negotiated maximum fragment length
241      * or SSL3_RT_MAX_PLAIN_LENGTH if none
242      */
243     unsigned int max_frag_len;
244
245     /* The maxium amount of early data we can receive/send */
246     uint32_t max_early_data;
247
248     /* The amount of early data that we have sent/received */
249     size_t early_data_count;
250
251     /* TLSv1.3 record padding */
252     size_t block_padding;
253
254     /* Only used by SSLv3 */
255     unsigned char mac_secret[EVP_MAX_MD_SIZE];
256
257     /* TLSv1.0/TLSv1.1/TLSv1.2 */
258     int use_etm;
259
260     /* Flags for GOST ciphers */
261     int stream_mac;
262     int tlstree;
263
264     /* TLSv1.3 fields */
265     /* static IV */
266     unsigned char iv[EVP_MAX_IV_LENGTH];
267     /* static read IV */
268     unsigned char read_iv[EVP_MAX_IV_LENGTH];
269     int allow_plain_alerts;
270
271     /* TLS "any" fields */
272     /* Set to true if this is the first record in a connection */
273     unsigned int is_first_record;
274
275     size_t taglen;
276
277     /* DTLS received handshake records (processed and unprocessed) */
278     record_pqueue unprocessed_rcds;
279     record_pqueue processed_rcds;
280
281     /* records being received in the current epoch */
282     DTLS_BITMAP bitmap;
283     /* renegotiation starts a new set of sequence numbers */
284     DTLS_BITMAP next_bitmap;
285
286     /*
287      * Whether we are currently in a hanshake or not. Only maintained for DTLS
288      */
289     int in_init;
290
291     /* Callbacks */
292     void *cbarg;
293     OSSL_FUNC_rlayer_skip_early_data_fn *skip_early_data;
294     OSSL_FUNC_rlayer_msg_callback_fn *msg_callback;
295     OSSL_FUNC_rlayer_security_fn *security;
296     OSSL_FUNC_rlayer_padding_fn *padding;
297
298     size_t max_pipelines;
299
300     /* Function pointers for version specific functions */
301     struct record_functions_st *funcs;
302 };
303
304 typedef struct dtls_rlayer_record_data_st {
305     unsigned char *packet;
306     size_t packet_length;
307     SSL3_BUFFER rbuf;
308     SSL3_RECORD rrec;
309 } DTLS_RLAYER_RECORD_DATA;
310
311 extern struct record_functions_st ssl_3_0_funcs;
312 extern struct record_functions_st tls_1_funcs;
313 extern struct record_functions_st tls_1_3_funcs;
314 extern struct record_functions_st tls_any_funcs;
315 extern struct record_functions_st dtls_1_funcs;
316 extern struct record_functions_st dtls_any_funcs;
317
318 void ossl_rlayer_fatal(OSSL_RECORD_LAYER *rl, int al, int reason,
319                        const char *fmt, ...);
320
321 #define RLAYERfatal(rl, al, r) RLAYERfatal_data((rl), (al), (r), NULL)
322 #define RLAYERfatal_data                                           \
323     (ERR_new(),                                                    \
324      ERR_set_debug(OPENSSL_FILE, OPENSSL_LINE, OPENSSL_FUNC),      \
325      ossl_rlayer_fatal)
326
327 #define RLAYER_USE_EXPLICIT_IV(rl) ((rl)->version == TLS1_1_VERSION \
328                                     || (rl)->version == TLS1_2_VERSION \
329                                     || (rl)->isdtls)
330
331 int ossl_set_tls_provider_parameters(OSSL_RECORD_LAYER *rl,
332                                      EVP_CIPHER_CTX *ctx,
333                                      const EVP_CIPHER *ciph,
334                                      const EVP_MD *md);
335 /* ssl3_cbc.c */
336 __owur char ssl3_cbc_record_digest_supported(const EVP_MD_CTX *ctx);
337 __owur int ssl3_cbc_digest_record(const EVP_MD *md,
338                                   unsigned char *md_out,
339                                   size_t *md_out_size,
340                                   const unsigned char *header,
341                                   const unsigned char *data,
342                                   size_t data_size,
343                                   size_t data_plus_mac_plus_padding_size,
344                                   const unsigned char *mac_secret,
345                                   size_t mac_secret_length, char is_sslv3);
346
347 int tls_default_read_n(OSSL_RECORD_LAYER *rl, size_t n, size_t max, int extend,
348                        int clearold, size_t *readbytes);
349 int tls_get_more_records(OSSL_RECORD_LAYER *rl);
350 int dtls_get_more_records(OSSL_RECORD_LAYER *rl);
351
352 int dtls_prepare_record_header(OSSL_RECORD_LAYER *rl,
353                                WPACKET *thispkt,
354                                OSSL_RECORD_TEMPLATE *templ,
355                                unsigned int rectype,
356                                unsigned char **recdata);
357 int dtls_write_records(OSSL_RECORD_LAYER *rl, OSSL_RECORD_TEMPLATE *templates,
358                        size_t numtempl);
359
360 int tls_default_set_protocol_version(OSSL_RECORD_LAYER *rl, int version);
361 int tls_default_validate_record_header(OSSL_RECORD_LAYER *rl, SSL3_RECORD *re);
362 int tls_do_compress(OSSL_RECORD_LAYER *rl, SSL3_RECORD *wr);
363 int tls_do_uncompress(OSSL_RECORD_LAYER *rl, SSL3_RECORD *rec);
364 int tls_default_post_process_record(OSSL_RECORD_LAYER *rl, SSL3_RECORD *rec);
365 int tls13_common_post_process_record(OSSL_RECORD_LAYER *rl, SSL3_RECORD *rec);
366
367 int
368 tls_int_new_record_layer(OSSL_LIB_CTX *libctx, const char *propq, int vers,
369                          int role, int direction, int level, unsigned char *key,
370                          size_t keylen, unsigned char *iv, size_t ivlen,
371                          unsigned char *mackey, size_t mackeylen,
372                          const EVP_CIPHER *ciph, size_t taglen,
373                          int mactype,
374                          const EVP_MD *md, COMP_METHOD *comp, BIO *prev,
375                          BIO *transport, BIO *next,
376                          BIO_ADDR *local, BIO_ADDR *peer,
377                          const OSSL_PARAM *settings, const OSSL_PARAM *options,
378                          const OSSL_DISPATCH *fns, void *cbarg,
379                          OSSL_RECORD_LAYER **retrl);
380 int tls_free(OSSL_RECORD_LAYER *rl);
381 int tls_reset(OSSL_RECORD_LAYER *rl);
382 int tls_unprocessed_read_pending(OSSL_RECORD_LAYER *rl);
383 int tls_processed_read_pending(OSSL_RECORD_LAYER *rl);
384 size_t tls_app_data_pending(OSSL_RECORD_LAYER *rl);
385 int tls_write_pending(OSSL_RECORD_LAYER *rl);
386 size_t tls_get_max_record_len(OSSL_RECORD_LAYER *rl);
387 size_t tls_get_max_records(OSSL_RECORD_LAYER *rl, int type, size_t len,
388                            size_t maxfrag, size_t *preffrag);
389 int tls_write_records(OSSL_RECORD_LAYER *rl, OSSL_RECORD_TEMPLATE *templates,
390                       size_t numtempl);
391 int tls_retry_write_records(OSSL_RECORD_LAYER *rl);
392 int tls_get_alert_code(OSSL_RECORD_LAYER *rl);
393 int tls_set1_bio(OSSL_RECORD_LAYER *rl, BIO *bio);
394 int tls_read_record(OSSL_RECORD_LAYER *rl, void **rechandle, int *rversion,
395                     int *type, unsigned char **data, size_t *datalen,
396                     uint16_t *epoch, unsigned char *seq_num);
397 int tls_release_record(OSSL_RECORD_LAYER *rl, void *rechandle);
398 int tls_default_set_protocol_version(OSSL_RECORD_LAYER *rl, int version);
399 int tls_set_protocol_version(OSSL_RECORD_LAYER *rl, int version);
400 void tls_set_plain_alerts(OSSL_RECORD_LAYER *rl, int allow);
401 void tls_set_first_handshake(OSSL_RECORD_LAYER *rl, int first);
402 void tls_set_max_pipelines(OSSL_RECORD_LAYER *rl, size_t max_pipelines);
403 void tls_get_state(OSSL_RECORD_LAYER *rl, const char **shortstr,
404                    const char **longstr);
405 int tls_set_options(OSSL_RECORD_LAYER *rl, const OSSL_PARAM *options);
406 const COMP_METHOD *tls_get_compression(OSSL_RECORD_LAYER *rl);
407 void tls_set_max_frag_len(OSSL_RECORD_LAYER *rl, size_t max_frag_len);
408 int tls_setup_read_buffer(OSSL_RECORD_LAYER *rl);
409 int tls_setup_write_buffer(OSSL_RECORD_LAYER *rl, size_t numwpipes,
410                            size_t firstlen, size_t nextlen);
411
412 int tls_write_records_multiblock(OSSL_RECORD_LAYER *rl,
413                                  OSSL_RECORD_TEMPLATE *templates,
414                                  size_t numtempl);
415
416 size_t tls_get_max_records_default(OSSL_RECORD_LAYER *rl, int type, size_t len,
417                                    size_t maxfrag, size_t *preffrag);
418 size_t tls_get_max_records_multiblock(OSSL_RECORD_LAYER *rl, int type,
419                                       size_t len, size_t maxfrag,
420                                       size_t *preffrag);
421 int tls_allocate_write_buffers_default(OSSL_RECORD_LAYER *rl,
422                                        OSSL_RECORD_TEMPLATE *templates,
423                                        size_t numtempl, size_t *prefix);
424 int tls_initialise_write_packets_default(OSSL_RECORD_LAYER *rl,
425                                          OSSL_RECORD_TEMPLATE *templates,
426                                          size_t numtempl,
427                                          OSSL_RECORD_TEMPLATE *prefixtempl,
428                                          WPACKET *pkt,
429                                          SSL3_BUFFER *bufs,
430                                          size_t *wpinited);
431 int tls1_allocate_write_buffers(OSSL_RECORD_LAYER *rl,
432                                 OSSL_RECORD_TEMPLATE *templates,
433                                 size_t numtempl, size_t *prefix);
434 int tls1_initialise_write_packets(OSSL_RECORD_LAYER *rl,
435                                   OSSL_RECORD_TEMPLATE *templates,
436                                   size_t numtempl,
437                                   OSSL_RECORD_TEMPLATE *prefixtempl,
438                                   WPACKET *pkt,
439                                   SSL3_BUFFER *bufs,
440                                   size_t *wpinited);
441 int tls_prepare_record_header_default(OSSL_RECORD_LAYER *rl,
442                                       WPACKET *thispkt,
443                                       OSSL_RECORD_TEMPLATE *templ,
444                                       unsigned int rectype,
445                                       unsigned char **recdata);
446 int tls_prepare_for_encryption_default(OSSL_RECORD_LAYER *rl,
447                                        size_t mac_size,
448                                        WPACKET *thispkt,
449                                        SSL3_RECORD *thiswr);
450 int tls_post_encryption_processing_default(OSSL_RECORD_LAYER *rl,
451                                            size_t mac_size,
452                                            OSSL_RECORD_TEMPLATE *thistempl,
453                                            WPACKET *thispkt,
454                                            SSL3_RECORD *thiswr);
455 int tls_write_records_default(OSSL_RECORD_LAYER *rl,
456                               OSSL_RECORD_TEMPLATE *templates,
457                               size_t numtempl);