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