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