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