2016-05-18 02:18:30 +08:00
|
|
|
/*
|
2020-04-23 20:55:52 +08:00
|
|
|
* Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
|
2015-01-30 21:46:43 +08:00
|
|
|
*
|
2018-12-06 20:08:51 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:18:30 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2015-01-30 21:46:43 +08:00
|
|
|
*/
|
|
|
|
|
2022-06-20 23:11:28 +08:00
|
|
|
typedef struct ssl_connection_st SSL_CONNECTION;
|
2022-05-06 22:10:00 +08:00
|
|
|
|
2022-05-25 22:16:48 +08:00
|
|
|
#include <openssl/core_dispatch.h>
|
2022-11-10 22:25:21 +08:00
|
|
|
#include "internal/recordmethod.h"
|
2022-06-20 23:11:28 +08:00
|
|
|
|
2015-02-02 19:41:29 +08:00
|
|
|
/*****************************************************************************
|
|
|
|
* *
|
2015-02-04 23:44:12 +08:00
|
|
|
* These structures should be considered PRIVATE to the record layer. No *
|
|
|
|
* non-record layer code should be using these structures in any way. *
|
2015-02-02 19:41:29 +08:00
|
|
|
* *
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2015-03-26 21:12:24 +08:00
|
|
|
#define SEQ_NUM_SIZE 8
|
|
|
|
|
2022-04-12 21:50:28 +08:00
|
|
|
typedef struct tls_record_st {
|
|
|
|
void *rechandle;
|
|
|
|
int version;
|
|
|
|
int type;
|
|
|
|
/* The data buffer containing bytes from the record */
|
|
|
|
unsigned char *data;
|
|
|
|
/* Number of remaining to be read in the data buffer */
|
|
|
|
size_t length;
|
|
|
|
/* Offset into the data buffer where to start reading */
|
|
|
|
size_t off;
|
2022-06-02 23:29:04 +08:00
|
|
|
/* epoch number. DTLS only */
|
|
|
|
uint16_t epoch;
|
|
|
|
/* sequence number. DTLS only */
|
|
|
|
unsigned char seq_num[SEQ_NUM_SIZE];
|
|
|
|
#ifndef OPENSSL_NO_SCTP
|
|
|
|
struct bio_dgram_sctp_rcvinfo recordinfo;
|
|
|
|
#endif
|
2022-04-12 21:50:28 +08:00
|
|
|
} TLS_RECORD;
|
|
|
|
|
2015-02-02 18:38:12 +08:00
|
|
|
typedef struct record_pqueue_st {
|
2022-07-27 21:37:27 +08:00
|
|
|
uint16_t epoch;
|
2016-01-23 03:54:01 +08:00
|
|
|
struct pqueue_st *q;
|
2015-02-02 18:38:12 +08:00
|
|
|
} record_pqueue;
|
|
|
|
|
2015-02-03 22:54:13 +08:00
|
|
|
typedef struct dtls_record_layer_st {
|
2015-02-03 23:14:24 +08:00
|
|
|
/*
|
|
|
|
* The current data and handshake epoch. This is initially
|
|
|
|
* undefined, and starts at zero once the initial handshake is
|
|
|
|
* completed
|
|
|
|
*/
|
2022-07-27 21:37:27 +08:00
|
|
|
uint16_t r_epoch;
|
|
|
|
uint16_t w_epoch;
|
2022-07-22 00:01:54 +08:00
|
|
|
|
2015-02-04 18:27:43 +08:00
|
|
|
/*
|
|
|
|
* Buffered application records. Only for records between CCS and
|
|
|
|
* Finished to prevent either protocol violation or unnecessary message
|
|
|
|
* loss.
|
|
|
|
*/
|
|
|
|
record_pqueue buffered_app_data;
|
2015-02-03 22:54:13 +08:00
|
|
|
} DTLS_RECORD_LAYER;
|
|
|
|
|
2015-02-04 23:44:12 +08:00
|
|
|
/*****************************************************************************
|
|
|
|
* *
|
|
|
|
* This structure should be considered "opaque" to anything outside of the *
|
|
|
|
* record layer. No non-record layer code should be accessing the members of *
|
|
|
|
* this structure. *
|
|
|
|
* *
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2015-01-30 21:46:43 +08:00
|
|
|
typedef struct record_layer_st {
|
2022-06-20 23:11:28 +08:00
|
|
|
/* The parent SSL_CONNECTION structure */
|
|
|
|
SSL_CONNECTION *s;
|
2022-07-22 19:39:24 +08:00
|
|
|
|
2022-11-11 00:05:16 +08:00
|
|
|
/* Custom record layer: always selected if set */
|
|
|
|
const OSSL_RECORD_METHOD *custom_rlmethod;
|
|
|
|
/* Record layer specific argument */
|
|
|
|
void *rlarg;
|
2022-07-22 19:39:24 +08:00
|
|
|
/* Method to use for the read record layer*/
|
|
|
|
const OSSL_RECORD_METHOD *rrlmethod;
|
2022-08-19 23:54:09 +08:00
|
|
|
/* Method to use for the write record layer*/
|
|
|
|
const OSSL_RECORD_METHOD *wrlmethod;
|
2022-07-22 19:39:24 +08:00
|
|
|
/* The read record layer object itself */
|
|
|
|
OSSL_RECORD_LAYER *rrl;
|
2022-08-19 23:54:09 +08:00
|
|
|
/* The write record layer object itself */
|
|
|
|
OSSL_RECORD_LAYER *wrl;
|
2022-07-22 19:39:24 +08:00
|
|
|
/* BIO to store data destined for the next read record layer epoch */
|
|
|
|
BIO *rrlnext;
|
|
|
|
/* Default read buffer length to be passed to the record layer */
|
|
|
|
size_t default_read_buf_len;
|
|
|
|
|
2015-01-30 21:46:43 +08:00
|
|
|
/*
|
|
|
|
* Read as many input bytes as possible (for
|
|
|
|
* non-blocking reads)
|
|
|
|
*/
|
|
|
|
int read_ahead;
|
2022-10-17 22:46:02 +08:00
|
|
|
|
2015-02-03 19:16:30 +08:00
|
|
|
/* number of bytes sent so far */
|
2016-09-07 18:34:39 +08:00
|
|
|
size_t wnum;
|
2015-02-03 21:12:22 +08:00
|
|
|
unsigned char handshake_fragment[4];
|
2016-09-06 19:05:25 +08:00
|
|
|
size_t handshake_fragment_len;
|
2015-02-03 21:22:12 +08:00
|
|
|
/* partial write - check the numbers match */
|
|
|
|
/* number bytes written */
|
2016-09-07 18:34:39 +08:00
|
|
|
size_t wpend_tot;
|
2015-02-03 21:22:12 +08:00
|
|
|
int wpend_type;
|
|
|
|
/* number of bytes submitted */
|
2016-09-07 18:34:39 +08:00
|
|
|
size_t wpend_ret;
|
2015-02-03 21:22:12 +08:00
|
|
|
const unsigned char *wpend_buf;
|
2022-07-26 19:44:09 +08:00
|
|
|
|
2016-09-21 21:07:31 +08:00
|
|
|
/* Count of the number of consecutive warning alerts received */
|
|
|
|
unsigned int alert_count;
|
2015-02-03 22:54:13 +08:00
|
|
|
DTLS_RECORD_LAYER *d;
|
2022-04-12 21:50:28 +08:00
|
|
|
|
2022-08-31 23:39:36 +08:00
|
|
|
/* TLS1.3 padding callback */
|
|
|
|
size_t (*record_padding_cb)(SSL *s, int type, size_t len, void *arg);
|
|
|
|
void *record_padding_arg;
|
|
|
|
size_t block_padding;
|
|
|
|
|
2022-04-12 21:50:28 +08:00
|
|
|
/* How many records we have read from the record layer */
|
|
|
|
size_t num_recs;
|
|
|
|
/* The next record from the record layer that we need to process */
|
|
|
|
size_t curr_rec;
|
|
|
|
/* Record layer data to be processed */
|
|
|
|
TLS_RECORD tlsrecs[SSL_MAX_PIPELINES];
|
|
|
|
|
2015-01-30 21:46:43 +08:00
|
|
|
} RECORD_LAYER;
|
|
|
|
|
2015-02-02 19:41:29 +08:00
|
|
|
/*****************************************************************************
|
|
|
|
* *
|
|
|
|
* The following macros/functions represent the libssl internal API to the *
|
2015-02-04 23:44:12 +08:00
|
|
|
* record layer. Any libssl code may call these functions/macros *
|
2015-02-02 19:41:29 +08:00
|
|
|
* *
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2015-01-30 21:46:43 +08:00
|
|
|
#define RECORD_LAYER_set_read_ahead(rl, ra) ((rl)->read_ahead = (ra))
|
|
|
|
#define RECORD_LAYER_get_read_ahead(rl) ((rl)->read_ahead)
|
2015-02-03 23:14:24 +08:00
|
|
|
#define DTLS_RECORD_LAYER_get_w_epoch(rl) ((rl)->d->w_epoch)
|
2015-02-02 18:38:12 +08:00
|
|
|
|
2022-06-20 23:11:28 +08:00
|
|
|
void RECORD_LAYER_init(RECORD_LAYER *rl, SSL_CONNECTION *s);
|
2015-02-02 19:53:20 +08:00
|
|
|
void RECORD_LAYER_clear(RECORD_LAYER *rl);
|
2016-02-12 20:03:58 +08:00
|
|
|
int RECORD_LAYER_read_pending(const RECORD_LAYER *rl);
|
2017-03-03 20:41:39 +08:00
|
|
|
int RECORD_LAYER_processed_read_pending(const RECORD_LAYER *rl);
|
2016-02-12 20:03:58 +08:00
|
|
|
int RECORD_LAYER_write_pending(const RECORD_LAYER *rl);
|
2015-03-28 07:01:51 +08:00
|
|
|
int RECORD_LAYER_is_sslv2_record(RECORD_LAYER *rl);
|
2016-10-07 02:17:54 +08:00
|
|
|
__owur size_t ssl3_pending(const SSL *s);
|
2016-09-07 18:34:39 +08:00
|
|
|
__owur int ssl3_write_bytes(SSL *s, int type, const void *buf, size_t len,
|
|
|
|
size_t *written);
|
2015-05-11 16:35:41 +08:00
|
|
|
__owur int ssl3_read_bytes(SSL *s, int type, int *recvd_type,
|
2016-09-06 19:05:25 +08:00
|
|
|
unsigned char *buf, size_t len, int peek,
|
2016-10-26 17:43:34 +08:00
|
|
|
size_t *readbytes);
|
2022-10-17 23:33:40 +08:00
|
|
|
|
2015-02-03 22:54:13 +08:00
|
|
|
int DTLS_RECORD_LAYER_new(RECORD_LAYER *rl);
|
|
|
|
void DTLS_RECORD_LAYER_free(RECORD_LAYER *rl);
|
|
|
|
void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl);
|
2015-05-11 16:35:41 +08:00
|
|
|
__owur int dtls1_read_bytes(SSL *s, int type, int *recvd_type,
|
2016-09-06 19:05:25 +08:00
|
|
|
unsigned char *buf, size_t len, int peek,
|
2016-10-26 17:43:34 +08:00
|
|
|
size_t *readbytes);
|
2022-06-20 23:11:28 +08:00
|
|
|
__owur int dtls1_write_bytes(SSL_CONNECTION *s, int type, const void *buf,
|
|
|
|
size_t len, size_t *written);
|
|
|
|
int do_dtls1_write(SSL_CONNECTION *s, int type, const unsigned char *buf,
|
2022-10-06 20:18:43 +08:00
|
|
|
size_t len, size_t *written);
|
2022-10-17 23:33:40 +08:00
|
|
|
void dtls1_increment_epoch(SSL_CONNECTION *s, int rw);
|
2022-06-02 23:29:04 +08:00
|
|
|
void ssl_release_record(SSL_CONNECTION *s, TLS_RECORD *rr);
|
2022-04-12 21:50:28 +08:00
|
|
|
|
2022-09-01 00:37:48 +08:00
|
|
|
# define HANDLE_RLAYER_READ_RETURN(s, ret) \
|
|
|
|
ossl_tls_handle_rlayer_return(s, 0, ret, OPENSSL_FILE, OPENSSL_LINE)
|
2022-04-12 21:50:28 +08:00
|
|
|
|
2022-09-01 00:37:48 +08:00
|
|
|
# define HANDLE_RLAYER_WRITE_RETURN(s, ret) \
|
|
|
|
ossl_tls_handle_rlayer_return(s, 1, ret, OPENSSL_FILE, OPENSSL_LINE)
|
|
|
|
|
|
|
|
int ossl_tls_handle_rlayer_return(SSL_CONNECTION *s, int writing, int ret,
|
|
|
|
char *file, int line);
|
2022-05-06 22:10:00 +08:00
|
|
|
|
2022-11-14 22:19:53 +08:00
|
|
|
int ssl_set_new_record_layer(SSL_CONNECTION *s, int version,
|
|
|
|
int direction, int level,
|
|
|
|
unsigned char *secret, size_t secretlen,
|
|
|
|
unsigned char *key, size_t keylen,
|
2022-05-06 22:10:00 +08:00
|
|
|
unsigned char *iv, size_t ivlen,
|
|
|
|
unsigned char *mackey, size_t mackeylen,
|
|
|
|
const EVP_CIPHER *ciph, size_t taglen,
|
|
|
|
int mactype, const EVP_MD *md,
|
2022-11-14 22:19:53 +08:00
|
|
|
const SSL_COMP *comp, const EVP_MD *kdfdigest);
|
2022-08-27 00:34:40 +08:00
|
|
|
int ssl_set_record_protocol_version(SSL_CONNECTION *s, int vers);
|
2022-05-25 22:16:48 +08:00
|
|
|
|
|
|
|
# define OSSL_FUNC_RLAYER_SKIP_EARLY_DATA 1
|
2022-08-23 21:45:32 +08:00
|
|
|
OSSL_CORE_MAKE_FUNC(int, rlayer_skip_early_data, (void *cbarg))
|
2022-05-25 23:41:30 +08:00
|
|
|
# define OSSL_FUNC_RLAYER_MSG_CALLBACK 2
|
|
|
|
OSSL_CORE_MAKE_FUNC(void, rlayer_msg_callback, (int write_p, int version,
|
|
|
|
int content_type,
|
|
|
|
const void *buf, size_t len,
|
|
|
|
void *cbarg))
|
2022-05-26 00:10:38 +08:00
|
|
|
# define OSSL_FUNC_RLAYER_SECURITY 3
|
|
|
|
OSSL_CORE_MAKE_FUNC(int, rlayer_security, (void *cbarg, int op, int bits,
|
2022-08-31 22:41:16 +08:00
|
|
|
int nid, void *other))
|
|
|
|
# define OSSL_FUNC_RLAYER_PADDING 4
|
|
|
|
OSSL_CORE_MAKE_FUNC(size_t, rlayer_padding, (void *cbarg, int type, size_t len))
|