xref: /freebsd/crypto/openssl/ssl/record/record.h (revision 3b68c491d37196bb76a95bce3c02f7c6d5ba22fd)
1 /*
2  * Copyright 1995-2024 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 /*****************************************************************************
11  *                                                                           *
12  * These structures should be considered PRIVATE to the record layer. No     *
13  * non-record layer code should be using these structures in any way.        *
14  *                                                                           *
15  *****************************************************************************/
16 
17 typedef struct ssl3_buffer_st {
18     /* at least SSL3_RT_MAX_PACKET_SIZE bytes, see ssl3_setup_buffers() */
19     unsigned char *buf;
20     /* default buffer size (or 0 if no default set) */
21     size_t default_len;
22     /* buffer size */
23     size_t len;
24     /* where to 'copy from' */
25     size_t offset;
26     /* how many bytes left */
27     size_t left;
28     /* 'buf' is from application for KTLS */
29     int app_buffer;
30 } SSL3_BUFFER;
31 
32 #define SEQ_NUM_SIZE                            8
33 
34 typedef struct ssl3_record_st {
35     /* Record layer version */
36     /* r */
37     int rec_version;
38     /* type of record */
39     /* r */
40     int type;
41     /* How many bytes available */
42     /* rw */
43     size_t length;
44     /*
45      * How many bytes were available before padding was removed? This is used
46      * to implement the MAC check in constant time for CBC records.
47      */
48     /* rw */
49     size_t orig_len;
50     /* read/write offset into 'buf' */
51     /* r */
52     size_t off;
53     /* pointer to the record data */
54     /* rw */
55     unsigned char *data;
56     /* where the decode bytes are */
57     /* rw */
58     unsigned char *input;
59     /* only used with decompression - malloc()ed */
60     /* r */
61     unsigned char *comp;
62     /* Whether the data from this record has already been read or not */
63     /* r */
64     unsigned int read;
65     /* epoch number, needed by DTLS1 */
66     /* r */
67     unsigned long epoch;
68     /* sequence number, needed by DTLS1 */
69     /* r */
70     unsigned char seq_num[SEQ_NUM_SIZE];
71 } SSL3_RECORD;
72 
73 typedef struct dtls1_bitmap_st {
74     /* Track 32 packets on 32-bit systems and 64 - on 64-bit systems */
75     unsigned long map;
76     /* Max record number seen so far, 64-bit value in big-endian encoding */
77     unsigned char max_seq_num[SEQ_NUM_SIZE];
78 } DTLS1_BITMAP;
79 
80 typedef struct record_pqueue_st {
81     unsigned short epoch;
82     struct pqueue_st *q;
83 } record_pqueue;
84 
85 typedef struct dtls1_record_data_st {
86     unsigned char *packet;
87     size_t packet_length;
88     SSL3_BUFFER rbuf;
89     SSL3_RECORD rrec;
90 #ifndef OPENSSL_NO_SCTP
91     struct bio_dgram_sctp_rcvinfo recordinfo;
92 #endif
93 } DTLS1_RECORD_DATA;
94 
95 typedef struct dtls_record_layer_st {
96     /*
97      * The current data and handshake epoch.  This is initially
98      * undefined, and starts at zero once the initial handshake is
99      * completed
100      */
101     unsigned short r_epoch;
102     unsigned short w_epoch;
103     /* records being received in the current epoch */
104     DTLS1_BITMAP bitmap;
105     /* renegotiation starts a new set of sequence numbers */
106     DTLS1_BITMAP next_bitmap;
107     /* Received handshake records (processed and unprocessed) */
108     record_pqueue unprocessed_rcds;
109     record_pqueue processed_rcds;
110     /*
111      * Buffered application records. Only for records between CCS and
112      * Finished to prevent either protocol violation or unnecessary message
113      * loss.
114      */
115     record_pqueue buffered_app_data;
116     /* save last and current sequence numbers for retransmissions */
117     unsigned char last_write_sequence[8];
118     unsigned char curr_write_sequence[8];
119 } DTLS_RECORD_LAYER;
120 
121 /*****************************************************************************
122  *                                                                           *
123  * This structure should be considered "opaque" to anything outside of the   *
124  * record layer. No non-record layer code should be accessing the members of *
125  * this structure.                                                           *
126  *                                                                           *
127  *****************************************************************************/
128 
129 typedef struct record_layer_st {
130     /* The parent SSL structure */
131     SSL *s;
132     /*
133      * Read as many input bytes as possible (for
134      * non-blocking reads)
135      */
136     int read_ahead;
137     /* where we are when reading */
138     int rstate;
139     /* How many pipelines can be used to read data */
140     size_t numrpipes;
141     /* How many pipelines can be used to write data */
142     size_t numwpipes;
143     /* read IO goes into here */
144     SSL3_BUFFER rbuf;
145     /* write IO goes into here */
146     SSL3_BUFFER wbuf[SSL_MAX_PIPELINES];
147     /* each decoded record goes in here */
148     SSL3_RECORD rrec[SSL_MAX_PIPELINES];
149     /* used internally to point at a raw packet */
150     unsigned char *packet;
151     size_t packet_length;
152     /* number of bytes sent so far */
153     size_t wnum;
154     unsigned char handshake_fragment[4];
155     size_t handshake_fragment_len;
156     /* The number of consecutive empty records we have received */
157     size_t empty_record_count;
158     /* partial write - check the numbers match */
159     /* number bytes written */
160     size_t wpend_tot;
161     int wpend_type;
162     /* number of bytes submitted */
163     size_t wpend_ret;
164     const unsigned char *wpend_buf;
165     unsigned char read_sequence[SEQ_NUM_SIZE];
166     unsigned char write_sequence[SEQ_NUM_SIZE];
167     /* Set to true if this is the first record in a connection */
168     unsigned int is_first_record;
169     /* Count of the number of consecutive warning alerts received */
170     unsigned int alert_count;
171     DTLS_RECORD_LAYER *d;
172 } RECORD_LAYER;
173 
174 /*****************************************************************************
175  *                                                                           *
176  * The following macros/functions represent the libssl internal API to the   *
177  * record layer. Any libssl code may call these functions/macros             *
178  *                                                                           *
179  *****************************************************************************/
180 
181 struct ssl_mac_buf_st {
182     unsigned char *mac;
183     int alloced;
184 };
185 typedef struct ssl_mac_buf_st SSL_MAC_BUF;
186 
187 #define MIN_SSL2_RECORD_LEN     9
188 
189 #define RECORD_LAYER_set_read_ahead(rl, ra)     ((rl)->read_ahead = (ra))
190 #define RECORD_LAYER_get_read_ahead(rl)         ((rl)->read_ahead)
191 #define RECORD_LAYER_get_packet(rl)             ((rl)->packet)
192 #define RECORD_LAYER_get_packet_length(rl)      ((rl)->packet_length)
193 #define RECORD_LAYER_add_packet_length(rl, inc) ((rl)->packet_length += (inc))
194 #define DTLS_RECORD_LAYER_get_w_epoch(rl)       ((rl)->d->w_epoch)
195 #define DTLS_RECORD_LAYER_get_processed_rcds(rl) \
196                                                 ((rl)->d->processed_rcds)
197 #define DTLS_RECORD_LAYER_get_unprocessed_rcds(rl) \
198                                                 ((rl)->d->unprocessed_rcds)
199 #define RECORD_LAYER_get_rbuf(rl)               (&(rl)->rbuf)
200 #define RECORD_LAYER_get_wbuf(rl)               ((rl)->wbuf)
201 
202 void RECORD_LAYER_init(RECORD_LAYER *rl, SSL *s);
203 void RECORD_LAYER_clear(RECORD_LAYER *rl);
204 void RECORD_LAYER_release(RECORD_LAYER *rl);
205 int RECORD_LAYER_read_pending(const RECORD_LAYER *rl);
206 int RECORD_LAYER_processed_read_pending(const RECORD_LAYER *rl);
207 int RECORD_LAYER_write_pending(const RECORD_LAYER *rl);
208 int RECORD_LAYER_data_present(const RECORD_LAYER *rl);
209 void RECORD_LAYER_reset_read_sequence(RECORD_LAYER *rl);
210 void RECORD_LAYER_reset_write_sequence(RECORD_LAYER *rl);
211 int RECORD_LAYER_is_sslv2_record(RECORD_LAYER *rl);
212 size_t RECORD_LAYER_get_rrec_length(RECORD_LAYER *rl);
213 __owur size_t ssl3_pending(const SSL *s);
214 __owur int ssl3_write_bytes(SSL *s, int type, const void *buf, size_t len,
215                             size_t *written);
216 int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
217                   size_t *pipelens, size_t numpipes,
218                   int create_empty_fragment, size_t *written);
219 __owur int ssl3_read_bytes(SSL *s, int type, int *recvd_type,
220                            unsigned char *buf, size_t len, int peek,
221                            size_t *readbytes);
222 __owur int ssl3_setup_buffers(SSL *s);
223 __owur int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, size_t n_recs, int send,
224                     SSL_MAC_BUF *mac, size_t macsize);
225 __owur int n_ssl3_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int send);
226 __owur int ssl3_write_pending(SSL *s, int type, const unsigned char *buf, size_t len,
227                               size_t *written);
228 __owur int tls1_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int sending,
229                     SSL_MAC_BUF *mac, size_t macsize);
230 __owur int tls1_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int send);
231 __owur int tls13_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int send,
232                      SSL_MAC_BUF *mac, size_t macsize);
233 int DTLS_RECORD_LAYER_new(RECORD_LAYER *rl);
234 void DTLS_RECORD_LAYER_free(RECORD_LAYER *rl);
235 void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl);
236 void DTLS_RECORD_LAYER_set_saved_w_epoch(RECORD_LAYER *rl, unsigned short e);
237 void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl);
238 void DTLS_RECORD_LAYER_set_write_sequence(RECORD_LAYER *rl, unsigned char *seq);
239 __owur int dtls1_read_bytes(SSL *s, int type, int *recvd_type,
240                             unsigned char *buf, size_t len, int peek,
241                             size_t *readbytes);
242 __owur int dtls1_write_bytes(SSL *s, int type, const void *buf, size_t len,
243                              size_t *written);
244 int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
245                    size_t len, int create_empty_fragment, size_t *written);
246 void dtls1_reset_seq_numbers(SSL *s, int rw);
247 int dtls_buffer_listen_record(SSL *s, size_t len, unsigned char *seq,
248                               size_t off);
249