Blame ssl/record/record.h

Packit c4476c
/*
Packit c4476c
 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
Packit c4476c
 *
Packit c4476c
 * Licensed under the OpenSSL license (the "License").  You may not use
Packit c4476c
 * this file except in compliance with the License.  You can obtain a copy
Packit c4476c
 * in the file LICENSE in the source distribution or at
Packit c4476c
 * https://www.openssl.org/source/license.html
Packit c4476c
 */
Packit c4476c
Packit c4476c
/*****************************************************************************
Packit c4476c
 *                                                                           *
Packit c4476c
 * These structures should be considered PRIVATE to the record layer. No     *
Packit c4476c
 * non-record layer code should be using these structures in any way.        *
Packit c4476c
 *                                                                           *
Packit c4476c
 *****************************************************************************/
Packit c4476c
Packit c4476c
typedef struct ssl3_buffer_st {
Packit c4476c
    /* at least SSL3_RT_MAX_PACKET_SIZE bytes, see ssl3_setup_buffers() */
Packit c4476c
    unsigned char *buf;
Packit c4476c
    /* default buffer size (or 0 if no default set) */
Packit c4476c
    size_t default_len;
Packit c4476c
    /* buffer size */
Packit c4476c
    size_t len;
Packit c4476c
    /* where to 'copy from' */
Packit c4476c
    size_t offset;
Packit c4476c
    /* how many bytes left */
Packit c4476c
    size_t left;
Packit c4476c
} SSL3_BUFFER;
Packit c4476c
Packit c4476c
#define SEQ_NUM_SIZE                            8
Packit c4476c
Packit c4476c
typedef struct ssl3_record_st {
Packit c4476c
    /* Record layer version */
Packit c4476c
    /* r */
Packit c4476c
    int rec_version;
Packit c4476c
    /* type of record */
Packit c4476c
    /* r */
Packit c4476c
    int type;
Packit c4476c
    /* How many bytes available */
Packit c4476c
    /* rw */
Packit c4476c
    size_t length;
Packit c4476c
    /*
Packit c4476c
     * How many bytes were available before padding was removed? This is used
Packit c4476c
     * to implement the MAC check in constant time for CBC records.
Packit c4476c
     */
Packit c4476c
    /* rw */
Packit c4476c
    size_t orig_len;
Packit c4476c
    /* read/write offset into 'buf' */
Packit c4476c
    /* r */
Packit c4476c
    size_t off;
Packit c4476c
    /* pointer to the record data */
Packit c4476c
    /* rw */
Packit c4476c
    unsigned char *data;
Packit c4476c
    /* where the decode bytes are */
Packit c4476c
    /* rw */
Packit c4476c
    unsigned char *input;
Packit c4476c
    /* only used with decompression - malloc()ed */
Packit c4476c
    /* r */
Packit c4476c
    unsigned char *comp;
Packit c4476c
    /* Whether the data from this record has already been read or not */
Packit c4476c
    /* r */
Packit c4476c
    unsigned int read;
Packit c4476c
    /* epoch number, needed by DTLS1 */
Packit c4476c
    /* r */
Packit c4476c
    unsigned long epoch;
Packit c4476c
    /* sequence number, needed by DTLS1 */
Packit c4476c
    /* r */
Packit c4476c
    unsigned char seq_num[SEQ_NUM_SIZE];
Packit c4476c
} SSL3_RECORD;
Packit c4476c
Packit c4476c
typedef struct dtls1_bitmap_st {
Packit c4476c
    /* Track 32 packets on 32-bit systems and 64 - on 64-bit systems */
Packit c4476c
    unsigned long map;
Packit c4476c
    /* Max record number seen so far, 64-bit value in big-endian encoding */
Packit c4476c
    unsigned char max_seq_num[SEQ_NUM_SIZE];
Packit c4476c
} DTLS1_BITMAP;
Packit c4476c
Packit c4476c
typedef struct record_pqueue_st {
Packit c4476c
    unsigned short epoch;
Packit c4476c
    struct pqueue_st *q;
Packit c4476c
} record_pqueue;
Packit c4476c
Packit c4476c
typedef struct dtls1_record_data_st {
Packit c4476c
    unsigned char *packet;
Packit c4476c
    size_t packet_length;
Packit c4476c
    SSL3_BUFFER rbuf;
Packit c4476c
    SSL3_RECORD rrec;
Packit c4476c
#ifndef OPENSSL_NO_SCTP
Packit c4476c
    struct bio_dgram_sctp_rcvinfo recordinfo;
Packit c4476c
#endif
Packit c4476c
} DTLS1_RECORD_DATA;
Packit c4476c
Packit c4476c
typedef struct dtls_record_layer_st {
Packit c4476c
    /*
Packit c4476c
     * The current data and handshake epoch.  This is initially
Packit c4476c
     * undefined, and starts at zero once the initial handshake is
Packit c4476c
     * completed
Packit c4476c
     */
Packit c4476c
    unsigned short r_epoch;
Packit c4476c
    unsigned short w_epoch;
Packit c4476c
    /* records being received in the current epoch */
Packit c4476c
    DTLS1_BITMAP bitmap;
Packit c4476c
    /* renegotiation starts a new set of sequence numbers */
Packit c4476c
    DTLS1_BITMAP next_bitmap;
Packit c4476c
    /* Received handshake records (processed and unprocessed) */
Packit c4476c
    record_pqueue unprocessed_rcds;
Packit c4476c
    record_pqueue processed_rcds;
Packit c4476c
    /*
Packit c4476c
     * Buffered application records. Only for records between CCS and
Packit c4476c
     * Finished to prevent either protocol violation or unnecessary message
Packit c4476c
     * loss.
Packit c4476c
     */
Packit c4476c
    record_pqueue buffered_app_data;
Packit c4476c
    /* save last and current sequence numbers for retransmissions */
Packit c4476c
    unsigned char last_write_sequence[8];
Packit c4476c
    unsigned char curr_write_sequence[8];
Packit c4476c
} DTLS_RECORD_LAYER;
Packit c4476c
Packit c4476c
/*****************************************************************************
Packit c4476c
 *                                                                           *
Packit c4476c
 * This structure should be considered "opaque" to anything outside of the   *
Packit c4476c
 * record layer. No non-record layer code should be accessing the members of *
Packit c4476c
 * this structure.                                                           *
Packit c4476c
 *                                                                           *
Packit c4476c
 *****************************************************************************/
Packit c4476c
Packit c4476c
typedef struct record_layer_st {
Packit c4476c
    /* The parent SSL structure */
Packit c4476c
    SSL *s;
Packit c4476c
    /*
Packit c4476c
     * Read as many input bytes as possible (for
Packit c4476c
     * non-blocking reads)
Packit c4476c
     */
Packit c4476c
    int read_ahead;
Packit c4476c
    /* where we are when reading */
Packit c4476c
    int rstate;
Packit c4476c
    /* How many pipelines can be used to read data */
Packit c4476c
    size_t numrpipes;
Packit c4476c
    /* How many pipelines can be used to write data */
Packit c4476c
    size_t numwpipes;
Packit c4476c
    /* read IO goes into here */
Packit c4476c
    SSL3_BUFFER rbuf;
Packit c4476c
    /* write IO goes into here */
Packit c4476c
    SSL3_BUFFER wbuf[SSL_MAX_PIPELINES];
Packit c4476c
    /* each decoded record goes in here */
Packit c4476c
    SSL3_RECORD rrec[SSL_MAX_PIPELINES];
Packit c4476c
    /* used internally to point at a raw packet */
Packit c4476c
    unsigned char *packet;
Packit c4476c
    size_t packet_length;
Packit c4476c
    /* number of bytes sent so far */
Packit c4476c
    size_t wnum;
Packit c4476c
    unsigned char handshake_fragment[4];
Packit c4476c
    size_t handshake_fragment_len;
Packit c4476c
    /* The number of consecutive empty records we have received */
Packit c4476c
    size_t empty_record_count;
Packit c4476c
    /* partial write - check the numbers match */
Packit c4476c
    /* number bytes written */
Packit c4476c
    size_t wpend_tot;
Packit c4476c
    int wpend_type;
Packit c4476c
    /* number of bytes submitted */
Packit c4476c
    size_t wpend_ret;
Packit c4476c
    const unsigned char *wpend_buf;
Packit c4476c
    unsigned char read_sequence[SEQ_NUM_SIZE];
Packit c4476c
    unsigned char write_sequence[SEQ_NUM_SIZE];
Packit c4476c
    /* Set to true if this is the first record in a connection */
Packit c4476c
    unsigned int is_first_record;
Packit c4476c
    /* Count of the number of consecutive warning alerts received */
Packit c4476c
    unsigned int alert_count;
Packit c4476c
    DTLS_RECORD_LAYER *d;
Packit c4476c
} RECORD_LAYER;
Packit c4476c
Packit c4476c
/*****************************************************************************
Packit c4476c
 *                                                                           *
Packit c4476c
 * The following macros/functions represent the libssl internal API to the   *
Packit c4476c
 * record layer. Any libssl code may call these functions/macros             *
Packit c4476c
 *                                                                           *
Packit c4476c
 *****************************************************************************/
Packit c4476c
Packit c4476c
#define MIN_SSL2_RECORD_LEN     9
Packit c4476c
Packit c4476c
#define RECORD_LAYER_set_read_ahead(rl, ra)     ((rl)->read_ahead = (ra))
Packit c4476c
#define RECORD_LAYER_get_read_ahead(rl)         ((rl)->read_ahead)
Packit c4476c
#define RECORD_LAYER_get_packet(rl)             ((rl)->packet)
Packit c4476c
#define RECORD_LAYER_get_packet_length(rl)      ((rl)->packet_length)
Packit c4476c
#define RECORD_LAYER_add_packet_length(rl, inc) ((rl)->packet_length += (inc))
Packit c4476c
#define DTLS_RECORD_LAYER_get_w_epoch(rl)       ((rl)->d->w_epoch)
Packit c4476c
#define DTLS_RECORD_LAYER_get_processed_rcds(rl) \
Packit c4476c
                                                ((rl)->d->processed_rcds)
Packit c4476c
#define DTLS_RECORD_LAYER_get_unprocessed_rcds(rl) \
Packit c4476c
                                                ((rl)->d->unprocessed_rcds)
Packit c4476c
#define RECORD_LAYER_get_rbuf(rl)               (&(rl)->rbuf)
Packit c4476c
#define RECORD_LAYER_get_wbuf(rl)               ((rl)->wbuf)
Packit c4476c
Packit c4476c
void RECORD_LAYER_init(RECORD_LAYER *rl, SSL *s);
Packit c4476c
void RECORD_LAYER_clear(RECORD_LAYER *rl);
Packit c4476c
void RECORD_LAYER_release(RECORD_LAYER *rl);
Packit c4476c
int RECORD_LAYER_read_pending(const RECORD_LAYER *rl);
Packit c4476c
int RECORD_LAYER_processed_read_pending(const RECORD_LAYER *rl);
Packit c4476c
int RECORD_LAYER_write_pending(const RECORD_LAYER *rl);
Packit c4476c
void RECORD_LAYER_reset_read_sequence(RECORD_LAYER *rl);
Packit c4476c
void RECORD_LAYER_reset_write_sequence(RECORD_LAYER *rl);
Packit c4476c
int RECORD_LAYER_is_sslv2_record(RECORD_LAYER *rl);
Packit c4476c
size_t RECORD_LAYER_get_rrec_length(RECORD_LAYER *rl);
Packit c4476c
__owur size_t ssl3_pending(const SSL *s);
Packit c4476c
__owur int ssl3_write_bytes(SSL *s, int type, const void *buf, size_t len,
Packit c4476c
                            size_t *written);
Packit c4476c
int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
Packit c4476c
                  size_t *pipelens, size_t numpipes,
Packit c4476c
                  int create_empty_fragment, size_t *written);
Packit c4476c
__owur int ssl3_read_bytes(SSL *s, int type, int *recvd_type,
Packit c4476c
                           unsigned char *buf, size_t len, int peek,
Packit c4476c
                           size_t *readbytes);
Packit c4476c
__owur int ssl3_setup_buffers(SSL *s);
Packit c4476c
__owur int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, size_t n_recs, int send);
Packit c4476c
__owur int n_ssl3_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int send);
Packit c4476c
__owur int ssl3_write_pending(SSL *s, int type, const unsigned char *buf, size_t len,
Packit c4476c
                              size_t *written);
Packit c4476c
__owur int tls1_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int send);
Packit c4476c
__owur int tls1_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int send);
Packit c4476c
__owur int tls13_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int send);
Packit c4476c
int DTLS_RECORD_LAYER_new(RECORD_LAYER *rl);
Packit c4476c
void DTLS_RECORD_LAYER_free(RECORD_LAYER *rl);
Packit c4476c
void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl);
Packit c4476c
void DTLS_RECORD_LAYER_set_saved_w_epoch(RECORD_LAYER *rl, unsigned short e);
Packit c4476c
void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl);
Packit c4476c
void DTLS_RECORD_LAYER_set_write_sequence(RECORD_LAYER *rl, unsigned char *seq);
Packit c4476c
__owur int dtls1_read_bytes(SSL *s, int type, int *recvd_type,
Packit c4476c
                            unsigned char *buf, size_t len, int peek,
Packit c4476c
                            size_t *readbytes);
Packit c4476c
__owur int dtls1_write_bytes(SSL *s, int type, const void *buf, size_t len,
Packit c4476c
                             size_t *written);
Packit c4476c
int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
Packit c4476c
                   size_t len, int create_empty_fragment, size_t *written);
Packit c4476c
void dtls1_reset_seq_numbers(SSL *s, int rw);
Packit c4476c
int dtls_buffer_listen_record(SSL *s, size_t len, unsigned char *seq,
Packit c4476c
                              size_t off);