Path: blob/main/crypto/openssl/ssl/record/record.h
48261 views
/*1* Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#include <openssl/core_dispatch.h>10#include "internal/recordmethod.h"1112/*****************************************************************************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*****************************************************************************/1819#define SEQ_NUM_SIZE 82021typedef struct tls_record_st {22void *rechandle;23int version;24uint8_t type;25/* The data buffer containing bytes from the record */26const unsigned char *data;27/*28* Buffer that we allocated to store data. If non NULL always the same as29* data (but non-const)30*/31unsigned char *allocdata;32/* Number of remaining to be read in the data buffer */33size_t length;34/* Offset into the data buffer where to start reading */35size_t off;36/* epoch number. DTLS only */37uint16_t epoch;38/* sequence number. DTLS only */39unsigned char seq_num[SEQ_NUM_SIZE];40#ifndef OPENSSL_NO_SCTP41struct bio_dgram_sctp_rcvinfo recordinfo;42#endif43} TLS_RECORD;4445typedef struct dtls_record_layer_st {46/*47* The current data and handshake epoch. This is initially48* undefined, and starts at zero once the initial handshake is49* completed50*/51uint16_t r_epoch;52uint16_t w_epoch;5354/*55* Buffered application records. Only for records between CCS and56* Finished to prevent either protocol violation or unnecessary message57* loss.58*/59struct pqueue_st *buffered_app_data;60} DTLS_RECORD_LAYER;6162/*****************************************************************************63* *64* This structure should be considered "opaque" to anything outside of the *65* record layer. No non-record layer code should be accessing the members of *66* this structure. *67* *68*****************************************************************************/6970typedef struct record_layer_st {71/* The parent SSL_CONNECTION structure */72SSL_CONNECTION *s;7374/* Custom record layer: always selected if set */75const OSSL_RECORD_METHOD *custom_rlmethod;76/* Record layer specific argument */77void *rlarg;78/* Method to use for the read record layer*/79const OSSL_RECORD_METHOD *rrlmethod;80/* Method to use for the write record layer*/81const OSSL_RECORD_METHOD *wrlmethod;82/* The read record layer object itself */83OSSL_RECORD_LAYER *rrl;84/* The write record layer object itself */85OSSL_RECORD_LAYER *wrl;86/* BIO to store data destined for the next read record layer epoch */87BIO *rrlnext;88/* Default read buffer length to be passed to the record layer */89size_t default_read_buf_len;9091/*92* Read as many input bytes as possible (for93* non-blocking reads)94*/95int read_ahead;9697/* number of bytes sent so far */98size_t wnum;99unsigned char handshake_fragment[4];100size_t handshake_fragment_len;101/* partial write - check the numbers match */102/* number bytes written */103size_t wpend_tot;104uint8_t wpend_type;105const unsigned char *wpend_buf;106107/* Count of the number of consecutive warning alerts received */108unsigned int alert_count;109DTLS_RECORD_LAYER *d;110111/* TLS1.3 padding callback */112size_t (*record_padding_cb)(SSL *s, int type, size_t len, void *arg);113void *record_padding_arg;114size_t block_padding;115size_t hs_padding;116117/* How many records we have read from the record layer */118size_t num_recs;119/* The next record from the record layer that we need to process */120size_t curr_rec;121/* Record layer data to be processed */122TLS_RECORD tlsrecs[SSL_MAX_PIPELINES];123124} RECORD_LAYER;125126/*****************************************************************************127* *128* The following macros/functions represent the libssl internal API to the *129* record layer. Any libssl code may call these functions/macros *130* *131*****************************************************************************/132133#define RECORD_LAYER_set_read_ahead(rl, ra) ((rl)->read_ahead = (ra))134#define RECORD_LAYER_get_read_ahead(rl) ((rl)->read_ahead)135136void RECORD_LAYER_init(RECORD_LAYER *rl, SSL_CONNECTION *s);137int RECORD_LAYER_clear(RECORD_LAYER *rl);138int RECORD_LAYER_reset(RECORD_LAYER *rl);139int RECORD_LAYER_read_pending(const RECORD_LAYER *rl);140int RECORD_LAYER_processed_read_pending(const RECORD_LAYER *rl);141int RECORD_LAYER_write_pending(const RECORD_LAYER *rl);142int RECORD_LAYER_is_sslv2_record(RECORD_LAYER *rl);143__owur size_t ssl3_pending(const SSL *s);144__owur int ssl3_write_bytes(SSL *s, uint8_t type, const void *buf, size_t len,145size_t *written);146__owur int ssl3_read_bytes(SSL *s, uint8_t type, uint8_t *recvd_type,147unsigned char *buf, size_t len, int peek,148size_t *readbytes);149150int DTLS_RECORD_LAYER_new(RECORD_LAYER *rl);151void DTLS_RECORD_LAYER_free(RECORD_LAYER *rl);152void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl);153__owur int dtls1_read_bytes(SSL *s, uint8_t type, uint8_t *recvd_type,154unsigned char *buf, size_t len, int peek,155size_t *readbytes);156__owur int dtls1_write_bytes(SSL_CONNECTION *s, uint8_t type, const void *buf,157size_t len, size_t *written);158int do_dtls1_write(SSL_CONNECTION *s, uint8_t type, const unsigned char *buf,159size_t len, size_t *written);160void dtls1_increment_epoch(SSL_CONNECTION *s, int rw);161uint16_t dtls1_get_epoch(SSL_CONNECTION *s, int rw);162int ssl_release_record(SSL_CONNECTION *s, TLS_RECORD *rr, size_t length);163164# define HANDLE_RLAYER_READ_RETURN(s, ret) \165ossl_tls_handle_rlayer_return(s, 0, ret, OPENSSL_FILE, OPENSSL_LINE)166167# define HANDLE_RLAYER_WRITE_RETURN(s, ret) \168ossl_tls_handle_rlayer_return(s, 1, ret, OPENSSL_FILE, OPENSSL_LINE)169170int ossl_tls_handle_rlayer_return(SSL_CONNECTION *s, int writing, int ret,171char *file, int line);172173int ssl_set_new_record_layer(SSL_CONNECTION *s, int version,174int direction, int level,175unsigned char *secret, size_t secretlen,176unsigned char *key, size_t keylen,177unsigned char *iv, size_t ivlen,178unsigned char *mackey, size_t mackeylen,179const EVP_CIPHER *ciph, size_t taglen,180int mactype, const EVP_MD *md,181const SSL_COMP *comp, const EVP_MD *kdfdigest);182int ssl_set_record_protocol_version(SSL_CONNECTION *s, int vers);183184# define OSSL_FUNC_RLAYER_SKIP_EARLY_DATA 1185OSSL_CORE_MAKE_FUNC(int, rlayer_skip_early_data, (void *cbarg))186# define OSSL_FUNC_RLAYER_MSG_CALLBACK 2187OSSL_CORE_MAKE_FUNC(void, rlayer_msg_callback, (int write_p, int version,188int content_type,189const void *buf, size_t len,190void *cbarg))191# define OSSL_FUNC_RLAYER_SECURITY 3192OSSL_CORE_MAKE_FUNC(int, rlayer_security, (void *cbarg, int op, int bits,193int nid, void *other))194# define OSSL_FUNC_RLAYER_PADDING 4195OSSL_CORE_MAKE_FUNC(size_t, rlayer_padding, (void *cbarg, int type, size_t len))196197198