2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/core_dispatch.h>
11 #include "internal/recordmethod.h"
13 /*****************************************************************************
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. *
18 *****************************************************************************/
20 #define SEQ_NUM_SIZE 8
22 typedef struct tls_record_st {
26 /* The data buffer containing bytes from the record */
28 /* Number of remaining to be read in the data buffer */
30 /* Offset into the data buffer where to start reading */
32 /* epoch number. DTLS only */
34 /* sequence number. DTLS only */
35 unsigned char seq_num[SEQ_NUM_SIZE];
36 #ifndef OPENSSL_NO_SCTP
37 struct bio_dgram_sctp_rcvinfo recordinfo;
41 typedef struct record_pqueue_st {
46 typedef struct dtls_record_layer_st {
48 * The current data and handshake epoch. This is initially
49 * undefined, and starts at zero once the initial handshake is
56 * Buffered application records. Only for records between CCS and
57 * Finished to prevent either protocol violation or unnecessary message
60 record_pqueue buffered_app_data;
63 /*****************************************************************************
65 * This structure should be considered "opaque" to anything outside of the *
66 * record layer. No non-record layer code should be accessing the members of *
69 *****************************************************************************/
71 typedef struct record_layer_st {
72 /* The parent SSL_CONNECTION structure */
75 /* Custom record layer: always selected if set */
76 const OSSL_RECORD_METHOD *custom_rlmethod;
77 /* Record layer specific argument */
79 /* Method to use for the read record layer*/
80 const OSSL_RECORD_METHOD *rrlmethod;
81 /* Method to use for the write record layer*/
82 const OSSL_RECORD_METHOD *wrlmethod;
83 /* The read record layer object itself */
84 OSSL_RECORD_LAYER *rrl;
85 /* The write record layer object itself */
86 OSSL_RECORD_LAYER *wrl;
87 /* BIO to store data destined for the next read record layer epoch */
89 /* Default read buffer length to be passed to the record layer */
90 size_t default_read_buf_len;
93 * Read as many input bytes as possible (for
98 /* number of bytes sent so far */
100 unsigned char handshake_fragment[4];
101 size_t handshake_fragment_len;
102 /* partial write - check the numbers match */
103 /* number bytes written */
106 /* number of bytes submitted */
108 const unsigned char *wpend_buf;
110 /* Count of the number of consecutive warning alerts received */
111 unsigned int alert_count;
112 DTLS_RECORD_LAYER *d;
114 /* TLS1.3 padding callback */
115 size_t (*record_padding_cb)(SSL *s, int type, size_t len, void *arg);
116 void *record_padding_arg;
117 size_t block_padding;
119 /* How many records we have read from the record layer */
121 /* The next record from the record layer that we need to process */
123 /* Record layer data to be processed */
124 TLS_RECORD tlsrecs[SSL_MAX_PIPELINES];
128 /*****************************************************************************
130 * The following macros/functions represent the libssl internal API to the *
131 * record layer. Any libssl code may call these functions/macros *
133 *****************************************************************************/
135 #define RECORD_LAYER_set_read_ahead(rl, ra) ((rl)->read_ahead = (ra))
136 #define RECORD_LAYER_get_read_ahead(rl) ((rl)->read_ahead)
137 #define DTLS_RECORD_LAYER_get_w_epoch(rl) ((rl)->d->w_epoch)
139 void RECORD_LAYER_init(RECORD_LAYER *rl, SSL_CONNECTION *s);
140 void RECORD_LAYER_clear(RECORD_LAYER *rl);
141 int RECORD_LAYER_read_pending(const RECORD_LAYER *rl);
142 int RECORD_LAYER_processed_read_pending(const RECORD_LAYER *rl);
143 int RECORD_LAYER_write_pending(const RECORD_LAYER *rl);
144 int RECORD_LAYER_is_sslv2_record(RECORD_LAYER *rl);
145 __owur size_t ssl3_pending(const SSL *s);
146 __owur int ssl3_write_bytes(SSL *s, int type, const void *buf, size_t len,
148 __owur int ssl3_read_bytes(SSL *s, int type, int *recvd_type,
149 unsigned char *buf, size_t len, int peek,
152 int DTLS_RECORD_LAYER_new(RECORD_LAYER *rl);
153 void DTLS_RECORD_LAYER_free(RECORD_LAYER *rl);
154 void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl);
155 __owur int dtls1_read_bytes(SSL *s, int type, int *recvd_type,
156 unsigned char *buf, size_t len, int peek,
158 __owur int dtls1_write_bytes(SSL_CONNECTION *s, int type, const void *buf,
159 size_t len, size_t *written);
160 int do_dtls1_write(SSL_CONNECTION *s, int type, const unsigned char *buf,
161 size_t len, size_t *written);
162 void dtls1_increment_epoch(SSL_CONNECTION *s, int rw);
163 void ssl_release_record(SSL_CONNECTION *s, TLS_RECORD *rr);
165 # define HANDLE_RLAYER_READ_RETURN(s, ret) \
166 ossl_tls_handle_rlayer_return(s, 0, ret, OPENSSL_FILE, OPENSSL_LINE)
168 # define HANDLE_RLAYER_WRITE_RETURN(s, ret) \
169 ossl_tls_handle_rlayer_return(s, 1, ret, OPENSSL_FILE, OPENSSL_LINE)
171 int ossl_tls_handle_rlayer_return(SSL_CONNECTION *s, int writing, int ret,
172 char *file, int line);
174 int ssl_set_new_record_layer(SSL_CONNECTION *s, int version,
175 int direction, int level,
176 unsigned char *secret, size_t secretlen,
177 unsigned char *key, size_t keylen,
178 unsigned char *iv, size_t ivlen,
179 unsigned char *mackey, size_t mackeylen,
180 const EVP_CIPHER *ciph, size_t taglen,
181 int mactype, const EVP_MD *md,
182 const SSL_COMP *comp, const EVP_MD *kdfdigest);
183 int ssl_set_record_protocol_version(SSL_CONNECTION *s, int vers);
185 # define OSSL_FUNC_RLAYER_SKIP_EARLY_DATA 1
186 OSSL_CORE_MAKE_FUNC(int, rlayer_skip_early_data, (void *cbarg))
187 # define OSSL_FUNC_RLAYER_MSG_CALLBACK 2
188 OSSL_CORE_MAKE_FUNC(void, rlayer_msg_callback, (int write_p, int version,
190 const void *buf, size_t len,
192 # define OSSL_FUNC_RLAYER_SECURITY 3
193 OSSL_CORE_MAKE_FUNC(int, rlayer_security, (void *cbarg, int op, int bits,
194 int nid, void *other))
195 # define OSSL_FUNC_RLAYER_PADDING 4
196 OSSL_CORE_MAKE_FUNC(size_t, rlayer_padding, (void *cbarg, int type, size_t len))