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