Don't attempt to set provider params on an ENGINE based cipher
[openssl.git] / ssl / record / record.h
1 /*
2  * Copyright 1995-2023 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/core_dispatch.h>
11 #include "internal/recordmethod.h"
12
13 /*****************************************************************************
14  *                                                                           *
15  * These structures should be considered PRIVATE to the record layer. No     *
16  * non-record layer code should be using these structures in any way.        *
17  *                                                                           *
18  *****************************************************************************/
19
20 #define SEQ_NUM_SIZE                            8
21
22 typedef struct tls_record_st {
23     void *rechandle;
24     int version;
25     uint8_t type;
26     /* The data buffer containing bytes from the record */
27     const unsigned char *data;
28     /*
29      * Buffer that we allocated to store data. If non NULL always the same as
30      * data (but non-const)
31      */
32     unsigned char *allocdata;
33     /* Number of remaining to be read in the data buffer */
34     size_t length;
35     /* Offset into the data buffer where to start reading */
36     size_t off;
37     /* epoch number. DTLS only */
38     uint16_t epoch;
39     /* sequence number. DTLS only */
40     unsigned char seq_num[SEQ_NUM_SIZE];
41 #ifndef OPENSSL_NO_SCTP
42     struct bio_dgram_sctp_rcvinfo recordinfo;
43 #endif
44 } TLS_RECORD;
45
46 typedef struct record_pqueue_st {
47     uint16_t epoch;
48     struct pqueue_st *q;
49 } record_pqueue;
50
51 typedef struct dtls_record_layer_st {
52     /*
53      * The current data and handshake epoch.  This is initially
54      * undefined, and starts at zero once the initial handshake is
55      * completed
56      */
57     uint16_t r_epoch;
58     uint16_t w_epoch;
59
60     /*
61      * Buffered application records. Only for records between CCS and
62      * Finished to prevent either protocol violation or unnecessary message
63      * loss.
64      */
65     record_pqueue buffered_app_data;
66 } DTLS_RECORD_LAYER;
67
68 /*****************************************************************************
69  *                                                                           *
70  * This structure should be considered "opaque" to anything outside of the   *
71  * record layer. No non-record layer code should be accessing the members of *
72  * this structure.                                                           *
73  *                                                                           *
74  *****************************************************************************/
75
76 typedef struct record_layer_st {
77     /* The parent SSL_CONNECTION structure */
78     SSL_CONNECTION *s;
79
80     /* Custom record layer: always selected if set */
81     const OSSL_RECORD_METHOD *custom_rlmethod;
82     /* Record layer specific argument */
83     void *rlarg;
84     /* Method to use for the read record layer*/
85     const OSSL_RECORD_METHOD *rrlmethod;
86     /* Method to use for the write record layer*/
87     const OSSL_RECORD_METHOD *wrlmethod;
88     /* The read record layer object itself */
89     OSSL_RECORD_LAYER *rrl;
90     /* The write record layer object itself */
91     OSSL_RECORD_LAYER *wrl;
92     /* BIO to store data destined for the next read record layer epoch */
93     BIO *rrlnext;
94     /* Default read buffer length to be passed to the record layer */
95     size_t default_read_buf_len;
96
97     /*
98      * Read as many input bytes as possible (for
99      * non-blocking reads)
100      */
101     int read_ahead;
102
103     /* number of bytes sent so far */
104     size_t wnum;
105     unsigned char handshake_fragment[4];
106     size_t handshake_fragment_len;
107     /* partial write - check the numbers match */
108     /* number bytes written */
109     size_t wpend_tot;
110     uint8_t wpend_type;
111     /* number of bytes submitted */
112     size_t wpend_ret;
113     const unsigned char *wpend_buf;
114
115     /* Count of the number of consecutive warning alerts received */
116     unsigned int alert_count;
117     DTLS_RECORD_LAYER *d;
118
119     /* TLS1.3 padding callback */
120     size_t (*record_padding_cb)(SSL *s, int type, size_t len, void *arg);
121     void *record_padding_arg;
122     size_t block_padding;
123
124     /* How many records we have read from the record layer */
125     size_t num_recs;
126     /* The next record from the record layer that we need to process */
127     size_t curr_rec;
128     /* Record layer data to be processed */
129     TLS_RECORD tlsrecs[SSL_MAX_PIPELINES];
130
131 } RECORD_LAYER;
132
133 /*****************************************************************************
134  *                                                                           *
135  * The following macros/functions represent the libssl internal API to the   *
136  * record layer. Any libssl code may call these functions/macros             *
137  *                                                                           *
138  *****************************************************************************/
139
140 #define RECORD_LAYER_set_read_ahead(rl, ra)     ((rl)->read_ahead = (ra))
141 #define RECORD_LAYER_get_read_ahead(rl)         ((rl)->read_ahead)
142 #define DTLS_RECORD_LAYER_get_w_epoch(rl)       ((rl)->d->w_epoch)
143
144 void RECORD_LAYER_init(RECORD_LAYER *rl, SSL_CONNECTION *s);
145 void RECORD_LAYER_clear(RECORD_LAYER *rl);
146 int RECORD_LAYER_read_pending(const RECORD_LAYER *rl);
147 int RECORD_LAYER_processed_read_pending(const RECORD_LAYER *rl);
148 int RECORD_LAYER_write_pending(const RECORD_LAYER *rl);
149 int RECORD_LAYER_is_sslv2_record(RECORD_LAYER *rl);
150 __owur size_t ssl3_pending(const SSL *s);
151 __owur int ssl3_write_bytes(SSL *s, uint8_t type, const void *buf, size_t len,
152                             size_t *written);
153 __owur int ssl3_read_bytes(SSL *s, uint8_t type, uint8_t *recvd_type,
154                            unsigned char *buf, size_t len, int peek,
155                            size_t *readbytes);
156
157 int DTLS_RECORD_LAYER_new(RECORD_LAYER *rl);
158 void DTLS_RECORD_LAYER_free(RECORD_LAYER *rl);
159 void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl);
160 __owur int dtls1_read_bytes(SSL *s, uint8_t type, uint8_t *recvd_type,
161                             unsigned char *buf, size_t len, int peek,
162                             size_t *readbytes);
163 __owur int dtls1_write_bytes(SSL_CONNECTION *s, uint8_t type, const void *buf,
164                              size_t len, size_t *written);
165 int do_dtls1_write(SSL_CONNECTION *s, uint8_t type, const unsigned char *buf,
166                    size_t len, size_t *written);
167 void dtls1_increment_epoch(SSL_CONNECTION *s, int rw);
168 int ssl_release_record(SSL_CONNECTION *s, TLS_RECORD *rr, size_t length);
169
170 # define HANDLE_RLAYER_READ_RETURN(s, ret) \
171     ossl_tls_handle_rlayer_return(s, 0, ret, OPENSSL_FILE, OPENSSL_LINE)
172
173 # define HANDLE_RLAYER_WRITE_RETURN(s, ret) \
174     ossl_tls_handle_rlayer_return(s, 1, ret, OPENSSL_FILE, OPENSSL_LINE)
175
176 int ossl_tls_handle_rlayer_return(SSL_CONNECTION *s, int writing, int ret,
177                                   char *file, int line);
178
179 int ssl_set_new_record_layer(SSL_CONNECTION *s, int version,
180                              int direction, int level,
181                              unsigned char *secret, size_t secretlen,
182                              unsigned char *key, size_t keylen,
183                              unsigned char *iv,  size_t ivlen,
184                              unsigned char *mackey, size_t mackeylen,
185                              const EVP_CIPHER *ciph, size_t taglen,
186                              int mactype, const EVP_MD *md,
187                              const SSL_COMP *comp, const EVP_MD *kdfdigest);
188 int ssl_set_record_protocol_version(SSL_CONNECTION *s, int vers);
189
190 # define OSSL_FUNC_RLAYER_SKIP_EARLY_DATA        1
191 OSSL_CORE_MAKE_FUNC(int, rlayer_skip_early_data, (void *cbarg))
192 # define OSSL_FUNC_RLAYER_MSG_CALLBACK           2
193 OSSL_CORE_MAKE_FUNC(void, rlayer_msg_callback, (int write_p, int version,
194                                                 int content_type,
195                                                 const void *buf, size_t len,
196                                                 void *cbarg))
197 # define OSSL_FUNC_RLAYER_SECURITY               3
198 OSSL_CORE_MAKE_FUNC(int, rlayer_security, (void *cbarg, int op, int bits,
199                                            int nid, void *other))
200 # define OSSL_FUNC_RLAYER_PADDING                4
201 OSSL_CORE_MAKE_FUNC(size_t, rlayer_padding, (void *cbarg, int type, size_t len))