Convert dtls_write_records to use standard record layer functions
[openssl.git] / ssl / record / methods / recmethod_local.h
index 1aa7f9b3eb582c073688433d4b012277e8622567..2ee6c2e7531f9af2e7883e90ea204e7ec125ed90 100644 (file)
 #include "../../ssl_local.h"
 #include "../record_local.h"
 
+typedef struct dtls_bitmap_st {
+    /* Track 64 packets */
+    uint64_t map;
+    /* Max record number seen so far, 64-bit value in big-endian encoding */
+    unsigned char max_seq_num[SEQ_NUM_SIZE];
+} DTLS_BITMAP;
+
 /* Protocol version specific function pointers */
 struct record_functions_st
 {
@@ -27,13 +34,10 @@ struct record_functions_st
                             unsigned char *mackey, size_t mackeylen,
                             const EVP_CIPHER *ciph,
                             size_t taglen,
-                            /* TODO(RECLAYER): This probably should not be an int */
                             int mactype,
                             const EVP_MD *md,
-                            const SSL_COMP *comp);
+                            COMP_METHOD *comp);
 
-    int (*read_n)(OSSL_RECORD_LAYER *rl, size_t n, size_t max, int extend,
-                  int clearold, size_t *readbytes);
     /*
      * Returns:
      *    0: if the record is publicly invalid, or an internal error, or AEAD
@@ -49,11 +53,90 @@ struct record_functions_st
     /* Return 1 for success or 0 for error */
     int (*set_protocol_version)(OSSL_RECORD_LAYER *rl, int version);
 
+    /* Read related functions */
+
+    int (*read_n)(OSSL_RECORD_LAYER *rl, size_t n, size_t max, int extend,
+                  int clearold, size_t *readbytes);
+
+    int (*get_more_records)(OSSL_RECORD_LAYER *rl);
+
     /* Return 1 for success or 0 for error */
     int (*validate_record_header)(OSSL_RECORD_LAYER *rl, SSL3_RECORD *rec);
 
     /* Return 1 for success or 0 for error */
     int (*post_process_record)(OSSL_RECORD_LAYER *rl, SSL3_RECORD *rec);
+
+    /* Write related functions */
+
+    size_t (*get_max_records)(OSSL_RECORD_LAYER *rl, int type, size_t len,
+                              size_t maxfrag, size_t *preffrag);
+
+    /* Return 1 for success or 0 for error */
+    int (*write_records)(OSSL_RECORD_LAYER *rl, OSSL_RECORD_TEMPLATE *templates,
+                         size_t numtempl);
+
+    /* Allocate the rl->wbuf buffers. Return 1 for success or 0 for error */
+    int (*allocate_write_buffers)(OSSL_RECORD_LAYER *rl,
+                                  OSSL_RECORD_TEMPLATE *templates,
+                                  size_t numtempl, size_t *prefix);
+
+    /*
+     * Initialise the packets in the |pkt| array using the buffers in |rl->wbuf|.
+     * Some protocol versions may use the space in |prefixtempl| to add
+     * an artificial template in front of the |templates| array and hence may
+     * initialise 1 more WPACKET than there are templates. |*wpinited|
+     * returns the number of WPACKETs in |pkt| that were successfully
+     * initialised. This must be 0 on entry and will be filled in even on error.
+     */
+    int (*initialise_write_packets)(OSSL_RECORD_LAYER *rl,
+                                    OSSL_RECORD_TEMPLATE *templates,
+                                    size_t numtempl,
+                                    OSSL_RECORD_TEMPLATE *prefixtempl,
+                                    WPACKET *pkt,
+                                    SSL3_BUFFER *bufs,
+                                    size_t *wpinited);
+
+    /* Get the actual record type to be used for a given template */
+    unsigned int (*get_record_type)(OSSL_RECORD_LAYER *rl,
+                                    OSSL_RECORD_TEMPLATE *template);
+
+    /* Write the record header data to the WPACKET */
+    int (*prepare_record_header)(OSSL_RECORD_LAYER *rl, WPACKET *thispkt,
+                                 OSSL_RECORD_TEMPLATE *templ,
+                                 unsigned int rectype,
+                                 unsigned char **recdata);
+
+    int (*add_record_padding)(OSSL_RECORD_LAYER *rl,
+                              OSSL_RECORD_TEMPLATE *thistempl,
+                              WPACKET *thispkt,
+                              SSL3_RECORD *thiswr);
+
+    /*
+     * This applies any mac that might be necessary, ensures that we have enough
+     * space in the WPACKET to perform the encryption and sets up the
+     * SSL3_RECORD ready for that encryption.
+     */
+    int (*prepare_for_encryption)(OSSL_RECORD_LAYER *rl,
+                                  size_t mac_size,
+                                  WPACKET *thispkt,
+                                  SSL3_RECORD *thiswr);
+
+    /*
+     * Any updates required to the record after encryption has been applied. For
+     * example, adding a MAC if using encrypt-then-mac
+     */
+    int (*post_encryption_processing)(OSSL_RECORD_LAYER *rl,
+                                      size_t mac_size,
+                                      OSSL_RECORD_TEMPLATE *thistempl,
+                                      WPACKET *thispkt,
+                                      SSL3_RECORD *thiswr);
+
+    /*
+     * Some record layer implementations need to do some custom preparation of
+     * the BIO before we write to it. KTLS does this to prevent coalescing of
+     * control and data messages.
+     */
+    int (*prepare_write_bio)(OSSL_RECORD_LAYER *rl, int type);
 };
 
 struct ossl_record_layer_st
@@ -65,6 +148,8 @@ struct ossl_record_layer_st
     int role;
     int direction;
     int level;
+    /* DTLS only */
+    uint16_t epoch;
 
     /*
      * A BIO containing any data read in the previous epoch that was destined
@@ -81,14 +166,19 @@ struct ossl_record_layer_st
      */
     BIO *next;
 
-    /* Types match the equivalent structures in the SSL object */
+    /* Types match the equivalent fields in the SSL object */
     uint64_t options;
-    /*
-     * TODO(RECLAYER): Should we take the opportunity to make this uint64_t
-     * even though upper layer continue to use uint32_t?
-     */
     uint32_t mode;
 
+    /* write IO goes into here */
+    SSL3_BUFFER wbuf[SSL_MAX_PIPELINES + 1];
+
+    /* Next wbuf with pending data still to write */
+    size_t nextwbuf;
+
+    /* How many pipelines can be used to write data */
+    size_t numwpipes;
+
     /* read IO goes into here */
     SSL3_BUFFER rbuf;
     /* each decoded record goes in here */
@@ -113,31 +203,43 @@ struct ossl_record_layer_st
     /* Sequence number for the next record */
     unsigned char sequence[SEQ_NUM_SIZE];
 
+    /* Alert code to be used if an error occurs */
     int alert;
 
     /*
-     * Read as many input bytes as possible (for
-     * non-blocking reads)
-     * TODO(RECLAYER): Why isn't this just an option?
+     * Read as many input bytes as possible (for non-blocking reads)
      */
     int read_ahead;
 
     /* The number of consecutive empty records we have received */
     size_t empty_record_count;
 
+    /*
+     * Do we need to send a prefix empty record before application data as a
+     * countermeasure against known-IV weakness (necessary for SSLv3 and
+     * TLSv1.0)
+     */
+    int need_empty_fragments;
+
     /* cryptographic state */
     EVP_CIPHER_CTX *enc_ctx;
 
+    /* Explicit IV length */
+    size_t eivlen;
+
     /* used for mac generation */
     EVP_MD_CTX *md_ctx;
 
-    /* uncompress */
-    COMP_CTX *expand;
+    /* compress/uncompress */
+    COMP_CTX *compctx;
 
     /* Set to 1 if this is the first handshake. 0 otherwise */
     int is_first_handshake;
 
-    /* The negotiated maximum fragment length */
+    /*
+     * The smaller of the configured and negotiated maximum fragment length
+     * or SSL3_RT_MAX_PLAIN_LENGTH if none
+     */
     unsigned int max_frag_len;
 
     /* The maxium amount of early data we can receive/send */
@@ -146,6 +248,9 @@ struct ossl_record_layer_st
     /* The amount of early data that we have sent/received */
     size_t early_data_count;
 
+    /* TLSv1.3 record padding */
+    size_t block_padding;
+
     /* Only used by SSLv3 */
     unsigned char mac_secret[EVP_MAX_MD_SIZE];
 
@@ -169,11 +274,26 @@ struct ossl_record_layer_st
 
     size_t taglen;
 
+    /* DTLS received handshake records (processed and unprocessed) */
+    record_pqueue unprocessed_rcds;
+    record_pqueue processed_rcds;
+
+    /* records being received in the current epoch */
+    DTLS_BITMAP bitmap;
+    /* renegotiation starts a new set of sequence numbers */
+    DTLS_BITMAP next_bitmap;
+
+    /*
+     * Whether we are currently in a hanshake or not. Only maintained for DTLS
+     */
+    int in_init;
+
     /* Callbacks */
     void *cbarg;
     OSSL_FUNC_rlayer_skip_early_data_fn *skip_early_data;
     OSSL_FUNC_rlayer_msg_callback_fn *msg_callback;
     OSSL_FUNC_rlayer_security_fn *security;
+    OSSL_FUNC_rlayer_padding_fn *padding;
 
     size_t max_pipelines;
 
@@ -181,22 +301,32 @@ struct ossl_record_layer_st
     struct record_functions_st *funcs;
 };
 
+typedef struct dtls_rlayer_record_data_st {
+    unsigned char *packet;
+    size_t packet_length;
+    SSL3_BUFFER rbuf;
+    SSL3_RECORD rrec;
+} DTLS_RLAYER_RECORD_DATA;
+
 extern struct record_functions_st ssl_3_0_funcs;
 extern struct record_functions_st tls_1_funcs;
 extern struct record_functions_st tls_1_3_funcs;
 extern struct record_functions_st tls_any_funcs;
+extern struct record_functions_st dtls_1_funcs;
+extern struct record_functions_st dtls_any_funcs;
 
 void ossl_rlayer_fatal(OSSL_RECORD_LAYER *rl, int al, int reason,
                        const char *fmt, ...);
 
-# define RLAYERfatal(rl, al, r) RLAYERfatal_data((rl), (al), (r), NULL)
-# define RLAYERfatal_data                                          \
+#define RLAYERfatal(rl, al, r) RLAYERfatal_data((rl), (al), (r), NULL)
+#define RLAYERfatal_data                                           \
     (ERR_new(),                                                    \
      ERR_set_debug(OPENSSL_FILE, OPENSSL_LINE, OPENSSL_FUNC),      \
      ossl_rlayer_fatal)
 
-# define RLAYER_USE_EXPLICIT_IV(rl) ((rl)->version == TLS1_1_VERSION \
-                                     || (rl)->version == TLS1_2_VERSION)
+#define RLAYER_USE_EXPLICIT_IV(rl) ((rl)->version == TLS1_1_VERSION \
+                                    || (rl)->version == TLS1_2_VERSION \
+                                    || (rl)->isdtls)
 
 int ossl_set_tls_provider_parameters(OSSL_RECORD_LAYER *rl,
                                      EVP_CIPHER_CTX *ctx,
@@ -216,9 +346,21 @@ __owur int ssl3_cbc_digest_record(const EVP_MD *md,
 
 int tls_default_read_n(OSSL_RECORD_LAYER *rl, size_t n, size_t max, int extend,
                        int clearold, size_t *readbytes);
+int tls_get_more_records(OSSL_RECORD_LAYER *rl);
+int dtls_get_more_records(OSSL_RECORD_LAYER *rl);
+
+int dtls_prepare_record_header(OSSL_RECORD_LAYER *rl,
+                               WPACKET *thispkt,
+                               OSSL_RECORD_TEMPLATE *templ,
+                               unsigned int rectype,
+                               unsigned char **recdata);
+int dtls_write_records(OSSL_RECORD_LAYER *rl, OSSL_RECORD_TEMPLATE *templates,
+                       size_t numtempl);
 
 int tls_default_set_protocol_version(OSSL_RECORD_LAYER *rl, int version);
 int tls_default_validate_record_header(OSSL_RECORD_LAYER *rl, SSL3_RECORD *re);
+int tls_do_compress(OSSL_RECORD_LAYER *rl, SSL3_RECORD *wr);
+int tls_do_uncompress(OSSL_RECORD_LAYER *rl, SSL3_RECORD *rec);
 int tls_default_post_process_record(OSSL_RECORD_LAYER *rl, SSL3_RECORD *rec);
 int tls13_common_post_process_record(OSSL_RECORD_LAYER *rl, SSL3_RECORD *rec);
 
@@ -228,9 +370,8 @@ tls_int_new_record_layer(OSSL_LIB_CTX *libctx, const char *propq, int vers,
                          size_t keylen, unsigned char *iv, size_t ivlen,
                          unsigned char *mackey, size_t mackeylen,
                          const EVP_CIPHER *ciph, size_t taglen,
-                         /* TODO(RECLAYER): This probably should not be an int */
                          int mactype,
-                         const EVP_MD *md, const SSL_COMP *comp, BIO *prev,
+                         const EVP_MD *md, COMP_METHOD *comp, BIO *prev,
                          BIO *transport, BIO *next,
                          BIO_ADDR *local, BIO_ADDR *peer,
                          const OSSL_PARAM *settings, const OSSL_PARAM *options,
@@ -243,14 +384,14 @@ int tls_processed_read_pending(OSSL_RECORD_LAYER *rl);
 size_t tls_app_data_pending(OSSL_RECORD_LAYER *rl);
 int tls_write_pending(OSSL_RECORD_LAYER *rl);
 size_t tls_get_max_record_len(OSSL_RECORD_LAYER *rl);
-size_t tls_get_max_records(OSSL_RECORD_LAYER *rl);
-int tls_write_records(OSSL_RECORD_LAYER *rl, OSSL_RECORD_TEMPLATE **templates,
-                      size_t numtempl,  size_t allowance, size_t *sent);
-int tls_retry_write_records(OSSL_RECORD_LAYER *rl, size_t allowance,
-                            size_t *sent);
+size_t tls_get_max_records(OSSL_RECORD_LAYER *rl, int type, size_t len,
+                           size_t maxfrag, size_t *preffrag);
+int tls_write_records(OSSL_RECORD_LAYER *rl, OSSL_RECORD_TEMPLATE *templates,
+                      size_t numtempl);
+int tls_retry_write_records(OSSL_RECORD_LAYER *rl);
 int tls_get_alert_code(OSSL_RECORD_LAYER *rl);
 int tls_set1_bio(OSSL_RECORD_LAYER *rl, BIO *bio);
-int tls_read_record(OSSL_RECORD_LAYER *rl, void **rechandle,  int *rversion,
+int tls_read_record(OSSL_RECORD_LAYER *rl, void **rechandle, int *rversion,
                     int *type, unsigned char **data, size_t *datalen,
                     uint16_t *epoch, unsigned char *seq_num);
 int tls_release_record(OSSL_RECORD_LAYER *rl, void *rechandle);
@@ -259,9 +400,58 @@ int tls_set_protocol_version(OSSL_RECORD_LAYER *rl, int version);
 void tls_set_plain_alerts(OSSL_RECORD_LAYER *rl, int allow);
 void tls_set_first_handshake(OSSL_RECORD_LAYER *rl, int first);
 void tls_set_max_pipelines(OSSL_RECORD_LAYER *rl, size_t max_pipelines);
-SSL3_BUFFER *tls_get0_rbuf(OSSL_RECORD_LAYER *rl);
-unsigned char *tls_get0_packet(OSSL_RECORD_LAYER *rl);
-void tls_set0_packet(OSSL_RECORD_LAYER *rl, unsigned char *packet,
-                     size_t packetlen);
-size_t tls_get_packet_length(OSSL_RECORD_LAYER *rl);
-void tls_reset_packet_length(OSSL_RECORD_LAYER *rl);
+void tls_get_state(OSSL_RECORD_LAYER *rl, const char **shortstr,
+                   const char **longstr);
+int tls_set_options(OSSL_RECORD_LAYER *rl, const OSSL_PARAM *options);
+const COMP_METHOD *tls_get_compression(OSSL_RECORD_LAYER *rl);
+void tls_set_max_frag_len(OSSL_RECORD_LAYER *rl, size_t max_frag_len);
+int tls_setup_read_buffer(OSSL_RECORD_LAYER *rl);
+int tls_setup_write_buffer(OSSL_RECORD_LAYER *rl, size_t numwpipes,
+                           size_t firstlen, size_t nextlen);
+
+int tls_write_records_multiblock(OSSL_RECORD_LAYER *rl,
+                                 OSSL_RECORD_TEMPLATE *templates,
+                                 size_t numtempl);
+
+size_t tls_get_max_records_default(OSSL_RECORD_LAYER *rl, int type, size_t len,
+                                   size_t maxfrag, size_t *preffrag);
+size_t tls_get_max_records_multiblock(OSSL_RECORD_LAYER *rl, int type,
+                                      size_t len, size_t maxfrag,
+                                      size_t *preffrag);
+int tls_allocate_write_buffers_default(OSSL_RECORD_LAYER *rl,
+                                       OSSL_RECORD_TEMPLATE *templates,
+                                       size_t numtempl, size_t *prefix);
+int tls_initialise_write_packets_default(OSSL_RECORD_LAYER *rl,
+                                         OSSL_RECORD_TEMPLATE *templates,
+                                         size_t numtempl,
+                                         OSSL_RECORD_TEMPLATE *prefixtempl,
+                                         WPACKET *pkt,
+                                         SSL3_BUFFER *bufs,
+                                         size_t *wpinited);
+int tls1_allocate_write_buffers(OSSL_RECORD_LAYER *rl,
+                                OSSL_RECORD_TEMPLATE *templates,
+                                size_t numtempl, size_t *prefix);
+int tls1_initialise_write_packets(OSSL_RECORD_LAYER *rl,
+                                  OSSL_RECORD_TEMPLATE *templates,
+                                  size_t numtempl,
+                                  OSSL_RECORD_TEMPLATE *prefixtempl,
+                                  WPACKET *pkt,
+                                  SSL3_BUFFER *bufs,
+                                  size_t *wpinited);
+int tls_prepare_record_header_default(OSSL_RECORD_LAYER *rl,
+                                      WPACKET *thispkt,
+                                      OSSL_RECORD_TEMPLATE *templ,
+                                      unsigned int rectype,
+                                      unsigned char **recdata);
+int tls_prepare_for_encryption_default(OSSL_RECORD_LAYER *rl,
+                                       size_t mac_size,
+                                       WPACKET *thispkt,
+                                       SSL3_RECORD *thiswr);
+int tls_post_encryption_processing_default(OSSL_RECORD_LAYER *rl,
+                                           size_t mac_size,
+                                           OSSL_RECORD_TEMPLATE *thistempl,
+                                           WPACKET *thispkt,
+                                           SSL3_RECORD *thiswr);
+int tls_write_records_default(OSSL_RECORD_LAYER *rl,
+                              OSSL_RECORD_TEMPLATE *templates,
+                              size_t numtempl);