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
|
|
|
typedef struct ssl3_buffer_st SSL3_BUFFER;
|
|
|
|
|
2022-05-25 22:16:48 +08:00
|
|
|
#include <openssl/core_dispatch.h>
|
2022-05-06 22:10:00 +08:00
|
|
|
#include "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
|
|
|
* *
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2022-05-06 22:10:00 +08:00
|
|
|
struct ssl3_buffer_st {
|
2015-02-04 23:44:12 +08:00
|
|
|
/* at least SSL3_RT_MAX_PACKET_SIZE bytes, see ssl3_setup_buffers() */
|
|
|
|
unsigned char *buf;
|
2016-01-13 22:20:25 +08:00
|
|
|
/* default buffer size (or 0 if no default set) */
|
|
|
|
size_t default_len;
|
2015-02-04 23:44:12 +08:00
|
|
|
/* buffer size */
|
|
|
|
size_t len;
|
|
|
|
/* where to 'copy from' */
|
2016-09-06 16:24:19 +08:00
|
|
|
size_t offset;
|
2015-02-04 23:44:12 +08:00
|
|
|
/* how many bytes left */
|
2016-09-06 16:24:19 +08:00
|
|
|
size_t left;
|
2019-11-21 05:40:12 +08:00
|
|
|
/* 'buf' is from application for KTLS */
|
|
|
|
int app_buffer;
|
2022-05-06 22:10:00 +08:00
|
|
|
};
|
2015-02-04 23:44:12 +08:00
|
|
|
|
2015-03-26 21:12:24 +08:00
|
|
|
#define SEQ_NUM_SIZE 8
|
|
|
|
|
2015-02-04 23:44:12 +08:00
|
|
|
typedef struct ssl3_record_st {
|
2015-03-28 07:01:51 +08:00
|
|
|
/* Record layer version */
|
|
|
|
/* r */
|
|
|
|
int rec_version;
|
2015-02-04 23:44:12 +08:00
|
|
|
/* type of record */
|
2015-03-26 21:17:38 +08:00
|
|
|
/* r */
|
|
|
|
int type;
|
2015-02-04 23:44:12 +08:00
|
|
|
/* How many bytes available */
|
2015-03-26 21:17:38 +08:00
|
|
|
/* rw */
|
2016-09-06 19:05:25 +08:00
|
|
|
size_t length;
|
2015-02-04 23:44:12 +08:00
|
|
|
/*
|
|
|
|
* How many bytes were available before padding was removed? This is used
|
|
|
|
* to implement the MAC check in constant time for CBC records.
|
|
|
|
*/
|
2015-03-26 21:17:38 +08:00
|
|
|
/* rw */
|
2016-09-06 19:05:25 +08:00
|
|
|
size_t orig_len;
|
2015-02-04 23:44:12 +08:00
|
|
|
/* read/write offset into 'buf' */
|
2015-03-26 21:17:38 +08:00
|
|
|
/* r */
|
2016-09-06 19:05:25 +08:00
|
|
|
size_t off;
|
2015-02-04 23:44:12 +08:00
|
|
|
/* pointer to the record data */
|
2015-03-26 21:17:38 +08:00
|
|
|
/* rw */
|
|
|
|
unsigned char *data;
|
2015-02-04 23:44:12 +08:00
|
|
|
/* where the decode bytes are */
|
2015-03-26 21:17:38 +08:00
|
|
|
/* rw */
|
|
|
|
unsigned char *input;
|
2015-02-04 23:44:12 +08:00
|
|
|
/* only used with decompression - malloc()ed */
|
2015-03-26 21:17:38 +08:00
|
|
|
/* r */
|
|
|
|
unsigned char *comp;
|
2016-06-01 23:31:11 +08:00
|
|
|
/* Whether the data from this record has already been read or not */
|
|
|
|
/* r */
|
|
|
|
unsigned int read;
|
2015-02-04 23:44:12 +08:00
|
|
|
/* epoch number, needed by DTLS1 */
|
2015-03-26 21:17:38 +08:00
|
|
|
/* r */
|
|
|
|
unsigned long epoch;
|
2015-02-04 23:44:12 +08:00
|
|
|
/* sequence number, needed by DTLS1 */
|
2015-03-26 21:17:38 +08:00
|
|
|
/* r */
|
|
|
|
unsigned char seq_num[SEQ_NUM_SIZE];
|
2015-02-04 23:44:12 +08:00
|
|
|
} SSL3_RECORD;
|
|
|
|
|
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;
|
|
|
|
} TLS_RECORD;
|
|
|
|
|
2015-02-04 23:44:12 +08:00
|
|
|
typedef struct dtls1_bitmap_st {
|
2015-03-26 21:12:24 +08:00
|
|
|
/* Track 32 packets on 32-bit systems and 64 - on 64-bit systems */
|
|
|
|
unsigned long map;
|
|
|
|
/* Max record number seen so far, 64-bit value in big-endian encoding */
|
|
|
|
unsigned char max_seq_num[SEQ_NUM_SIZE];
|
2015-02-04 23:44:12 +08:00
|
|
|
} DTLS1_BITMAP;
|
|
|
|
|
2015-02-02 18:38:12 +08:00
|
|
|
typedef struct record_pqueue_st {
|
|
|
|
unsigned short 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:32:15 +08:00
|
|
|
typedef struct dtls1_record_data_st {
|
|
|
|
unsigned char *packet;
|
2016-09-06 19:05:25 +08:00
|
|
|
size_t packet_length;
|
2015-02-03 22:32:15 +08:00
|
|
|
SSL3_BUFFER rbuf;
|
|
|
|
SSL3_RECORD rrec;
|
2016-08-06 01:03:17 +08:00
|
|
|
#ifndef OPENSSL_NO_SCTP
|
2015-02-03 22:32:15 +08:00
|
|
|
struct bio_dgram_sctp_rcvinfo recordinfo;
|
2016-08-06 01:03:17 +08:00
|
|
|
#endif
|
2015-02-03 22:32:15 +08:00
|
|
|
} DTLS1_RECORD_DATA;
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
unsigned short r_epoch;
|
|
|
|
unsigned short w_epoch;
|
2015-02-03 23:39:06 +08:00
|
|
|
/* records being received in the current epoch */
|
|
|
|
DTLS1_BITMAP bitmap;
|
|
|
|
/* renegotiation starts a new set of sequence numbers */
|
|
|
|
DTLS1_BITMAP next_bitmap;
|
2015-02-04 00:05:28 +08:00
|
|
|
/* Received handshake records (processed and unprocessed) */
|
|
|
|
record_pqueue unprocessed_rcds;
|
|
|
|
record_pqueue processed_rcds;
|
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-04 19:57:34 +08:00
|
|
|
/* save last and current sequence numbers for retransmissions */
|
|
|
|
unsigned char last_write_sequence[8];
|
|
|
|
unsigned char curr_write_sequence[8];
|
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;
|
2015-01-30 21:46:43 +08:00
|
|
|
/*
|
|
|
|
* Read as many input bytes as possible (for
|
|
|
|
* non-blocking reads)
|
|
|
|
*/
|
|
|
|
int read_ahead;
|
2015-02-03 18:48:28 +08:00
|
|
|
/* where we are when reading */
|
|
|
|
int rstate;
|
2016-01-12 22:52:35 +08:00
|
|
|
/* How many pipelines can be used to read data */
|
2016-09-06 19:05:25 +08:00
|
|
|
size_t numrpipes;
|
2016-01-12 22:52:35 +08:00
|
|
|
/* How many pipelines can be used to write data */
|
2016-09-07 18:34:39 +08:00
|
|
|
size_t numwpipes;
|
2015-01-30 23:38:10 +08:00
|
|
|
/* read IO goes into here */
|
|
|
|
SSL3_BUFFER rbuf;
|
2015-01-31 00:17:25 +08:00
|
|
|
/* write IO goes into here */
|
2015-09-22 18:12:50 +08:00
|
|
|
SSL3_BUFFER wbuf[SSL_MAX_PIPELINES];
|
2015-01-31 07:27:17 +08:00
|
|
|
/* each decoded record goes in here */
|
2016-01-12 22:52:35 +08:00
|
|
|
SSL3_RECORD rrec[SSL_MAX_PIPELINES];
|
2015-02-03 04:55:15 +08:00
|
|
|
/* used internally to point at a raw packet */
|
|
|
|
unsigned char *packet;
|
2016-09-06 16:24:19 +08:00
|
|
|
size_t packet_length;
|
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;
|
2016-06-01 23:31:11 +08:00
|
|
|
/* The number of consecutive empty records we have received */
|
2016-09-06 19:05:25 +08:00
|
|
|
size_t empty_record_count;
|
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;
|
2015-09-15 05:49:35 +08:00
|
|
|
unsigned char read_sequence[SEQ_NUM_SIZE];
|
|
|
|
unsigned char write_sequence[SEQ_NUM_SIZE];
|
2016-08-02 00:15:13 +08:00
|
|
|
/* Set to true if this is the first record in a connection */
|
|
|
|
unsigned int is_first_record;
|
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
|
|
|
|
|
|
|
/* TODO(RECLAYER): Tidy me up. New fields for record management */
|
|
|
|
|
|
|
|
/* 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
|
|
|
* *
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2020-06-04 00:42:01 +08:00
|
|
|
struct ssl_mac_buf_st {
|
|
|
|
unsigned char *mac;
|
|
|
|
int alloced;
|
|
|
|
};
|
|
|
|
typedef struct ssl_mac_buf_st SSL_MAC_BUF;
|
|
|
|
|
2015-03-28 07:01:51 +08:00
|
|
|
#define MIN_SSL2_RECORD_LEN 9
|
|
|
|
|
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 04:55:15 +08:00
|
|
|
#define RECORD_LAYER_get_packet(rl) ((rl)->packet)
|
|
|
|
#define RECORD_LAYER_get_packet_length(rl) ((rl)->packet_length)
|
|
|
|
#define RECORD_LAYER_add_packet_length(rl, inc) ((rl)->packet_length += (inc))
|
2015-02-03 23:14:24 +08:00
|
|
|
#define DTLS_RECORD_LAYER_get_w_epoch(rl) ((rl)->d->w_epoch)
|
2015-02-04 00:05:28 +08:00
|
|
|
#define DTLS_RECORD_LAYER_get_processed_rcds(rl) \
|
|
|
|
((rl)->d->processed_rcds)
|
|
|
|
#define DTLS_RECORD_LAYER_get_unprocessed_rcds(rl) \
|
|
|
|
((rl)->d->unprocessed_rcds)
|
2018-10-08 22:46:51 +08:00
|
|
|
#define RECORD_LAYER_get_rbuf(rl) (&(rl)->rbuf)
|
|
|
|
#define RECORD_LAYER_get_wbuf(rl) ((rl)->wbuf)
|
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);
|
2015-02-02 20:18:03 +08:00
|
|
|
void RECORD_LAYER_release(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-02-03 22:26:50 +08:00
|
|
|
void RECORD_LAYER_reset_read_sequence(RECORD_LAYER *rl);
|
|
|
|
void RECORD_LAYER_reset_write_sequence(RECORD_LAYER *rl);
|
2015-03-28 07:01:51 +08:00
|
|
|
int RECORD_LAYER_is_sslv2_record(RECORD_LAYER *rl);
|
2016-09-06 19:05:25 +08:00
|
|
|
size_t RECORD_LAYER_get_rrec_length(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);
|
2022-06-20 23:11:28 +08:00
|
|
|
int do_ssl3_write(SSL_CONNECTION *s, int type, const unsigned char *buf,
|
2016-09-07 18:34:39 +08:00
|
|
|
size_t *pipelens, size_t numpipes,
|
|
|
|
int create_empty_fragment, 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-06-20 23:11:28 +08:00
|
|
|
__owur int ssl3_setup_buffers(SSL_CONNECTION *s);
|
|
|
|
__owur int ssl3_enc(SSL_CONNECTION *s, SSL3_RECORD *inrecs, size_t n_recs,
|
|
|
|
int send, SSL_MAC_BUF *mac, size_t macsize);
|
|
|
|
__owur int n_ssl3_mac(SSL_CONNECTION *s, SSL3_RECORD *rec, unsigned char *md,
|
|
|
|
int send);
|
|
|
|
__owur int ssl3_write_pending(SSL_CONNECTION *s, int type,
|
|
|
|
const unsigned char *buf, size_t len,
|
2016-09-07 18:34:39 +08:00
|
|
|
size_t *written);
|
2022-06-20 23:11:28 +08:00
|
|
|
__owur int tls1_enc(SSL_CONNECTION *s, SSL3_RECORD *recs, size_t n_recs,
|
|
|
|
int sending, SSL_MAC_BUF *mac, size_t macsize);
|
2022-04-18 15:57:32 +08:00
|
|
|
__owur int tls1_mac_old(SSL_CONNECTION *s, SSL3_RECORD *rec, unsigned char *md,
|
|
|
|
int send);
|
2022-06-20 23:11:28 +08:00
|
|
|
__owur int tls13_enc(SSL_CONNECTION *s, SSL3_RECORD *recs, size_t n_recs,
|
|
|
|
int send, SSL_MAC_BUF *mac, size_t macsize);
|
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-02-04 19:57:34 +08:00
|
|
|
void DTLS_RECORD_LAYER_set_saved_w_epoch(RECORD_LAYER *rl, unsigned short e);
|
2015-02-04 23:44:12 +08:00
|
|
|
void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl);
|
2015-09-15 05:49:35 +08:00
|
|
|
void DTLS_RECORD_LAYER_set_write_sequence(RECORD_LAYER *rl, unsigned char *seq);
|
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,
|
2016-09-07 18:34:39 +08:00
|
|
|
size_t len, int create_empty_fragment, size_t *written);
|
2022-06-20 23:11:28 +08:00
|
|
|
void dtls1_reset_seq_numbers(SSL_CONNECTION *s, int rw);
|
|
|
|
int dtls_buffer_listen_record(SSL_CONNECTION *s, size_t len, unsigned char *seq,
|
2018-10-09 17:22:06 +08:00
|
|
|
size_t off);
|
2022-04-12 21:50:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
# define HANDLE_RLAYER_RETURN(s, ret) \
|
|
|
|
ossl_tls_handle_rlayer_return(s, ret, OPENSSL_FILE, OPENSSL_LINE)
|
|
|
|
|
|
|
|
int ossl_tls_handle_rlayer_return(SSL_CONNECTION *s, int ret, char *file,
|
|
|
|
int line);
|
2022-05-06 22:10:00 +08:00
|
|
|
|
2022-05-11 01:50:00 +08:00
|
|
|
int ssl_set_new_record_layer(SSL_CONNECTION *s, int version, int direction,
|
|
|
|
int level, 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,
|
|
|
|
const SSL_COMP *comp);
|
2022-05-25 22:16:48 +08:00
|
|
|
|
|
|
|
# define OSSL_FUNC_RLAYER_SKIP_EARLY_DATA 1
|
2022-05-25 23:41:30 +08:00
|
|
|
OSSL_CORE_MAKE_FUNC(const OSSL_PARAM *, rlayer_skip_early_data, (void *cbarg))
|
|
|
|
# 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))
|