VirtualBox

Ignore:
Timestamp:
Mar 3, 2022 7:17:34 PM (3 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
150325
Message:

libs/openssl-3.0.1: started applying and adjusting our OpenSSL changes to 3.0.1. bugref:10128

Location:
trunk/src/libs/openssl-3.0.1
Files:
1 deleted
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/libs/openssl-3.0.1

    • Property svn:mergeinfo
      •  

        old new  
        1212/vendor/openssl/1.1.1c:131722-131725
        1313/vendor/openssl/1.1.1k:145841-145843
         14/vendor/openssl/3.0.1:150323-150324
         15/vendor/openssl/current:147554-150322
  • trunk/src/libs/openssl-3.0.1/ssl/record/dtls1_bitmap.c

    r91772 r94082  
    22 * Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
    33 *
    4  * Licensed under the OpenSSL license (the "License").  You may not use
     4 * Licensed under the Apache License 2.0 (the "License").  You may not use
    55 * this file except in compliance with the License.  You can obtain a copy
    66 * in the file LICENSE in the source distribution or at
  • trunk/src/libs/openssl-3.0.1/ssl/record/rec_layer_d1.c

    r91772 r94082  
    22 * Copyright 2005-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 *
    4  * Licensed under the OpenSSL license (the "License").  You may not use
     4 * Licensed under the Apache License 2.0 (the "License").  You may not use
    55 * this file except in compliance with the License.  You can obtain a copy
    66 * in the file LICENSE in the source distribution or at
     
    1414#include <openssl/buffer.h>
    1515#include "record_local.h"
    16 #include "../packet_local.h"
     16#include "internal/packet.h"
    1717#include "internal/cryptlib.h"
    1818
     
    2222
    2323    if ((d = OPENSSL_malloc(sizeof(*d))) == NULL) {
    24         SSLerr(SSL_F_DTLS_RECORD_LAYER_NEW, ERR_R_MALLOC_FAILURE);
     24        ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
    2525        return 0;
    2626    }
     
    7878    while ((item = pqueue_pop(d->processed_rcds.q)) != NULL) {
    7979        rdata = (DTLS1_RECORD_DATA *)item->data;
     80        if (rl->s->options & SSL_OP_CLEANSE_PLAINTEXT)
     81            OPENSSL_cleanse(rdata->rbuf.buf, rdata->rbuf.len);
    8082        OPENSSL_free(rdata->rbuf.buf);
    8183        OPENSSL_free(item->data);
     
    8587    while ((item = pqueue_pop(d->buffered_app_data.q)) != NULL) {
    8688        rdata = (DTLS1_RECORD_DATA *)item->data;
     89        if (rl->s->options & SSL_OP_CLEANSE_PLAINTEXT)
     90            OPENSSL_cleanse(rdata->rbuf.buf, rdata->rbuf.len);
    8791        OPENSSL_free(rdata->rbuf.buf);
    8892        OPENSSL_free(item->data);
     
    154158        OPENSSL_free(rdata);
    155159        pitem_free(item);
    156         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_BUFFER_RECORD,
    157                  ERR_R_INTERNAL_ERROR);
     160        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    158161        return -1;
    159162    }
     
    263266                 * be the case because we already checked the epoch above
    264267                 */
    265                  SSLfatal(s, SSL_AD_INTERNAL_ERROR,
    266                           SSL_F_DTLS1_PROCESS_BUFFERED_RECORDS,
    267                           ERR_R_INTERNAL_ERROR);
     268                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    268269                 return 0;
    269270            }
     
    359360         (type != SSL3_RT_HANDSHAKE)) ||
    360361        (peek && (type != SSL3_RT_APPLICATION_DATA))) {
    361         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_READ_BYTES,
    362                  ERR_R_INTERNAL_ERROR);
     362        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    363363        return -1;
    364364    }
     
    378378
    379379    /*-
    380      * s->s3->rrec.type         - is the type of record
    381      * s->s3->rrec.data,    - data
    382      * s->s3->rrec.off,     - offset into 'data' for next read
    383      * s->s3->rrec.length,  - number of bytes.
     380     * s->s3.rrec.type     - is the type of record
     381     * s->s3.rrec.data,    - data
     382     * s->s3.rrec.off,     - offset into 'data' for next read
     383     * s->s3.rrec.length,  - number of bytes.
    384384     */
    385385    rr = s->rlayer.rrec;
     
    446446    /* we now have a packet which can be read and processed */
    447447
    448     if (s->s3->change_cipher_spec /* set when we receive ChangeCipherSpec,
    449                                    * reset by ssl3_get_finished */
     448    if (s->s3.change_cipher_spec /* set when we receive ChangeCipherSpec,
     449                                  * reset by ssl3_get_finished */
    450450        && (SSL3_RECORD_get_type(rr) != SSL3_RT_HANDSHAKE)) {
    451451        /*
     
    489489        if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&
    490490            (s->enc_read_ctx == NULL)) {
    491             SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_DTLS1_READ_BYTES,
     491            SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
    492492                     SSL_R_APP_DATA_IN_HANDSHAKE);
    493493            return -1;
     
    518518                SSL3_RECORD_set_read(rr);
    519519        } else {
     520            if (s->options & SSL_OP_CLEANSE_PLAINTEXT)
     521                OPENSSL_cleanse(&(SSL3_RECORD_get_data(rr)[SSL3_RECORD_get_off(rr)]), n);
    520522            SSL3_RECORD_sub_length(rr, n);
    521523            SSL3_RECORD_add_off(rr, n);
     
    558560                || !PACKET_get_1(&alert, &alert_descr)
    559561                || PACKET_remaining(&alert) != 0) {
    560             SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_DTLS1_READ_BYTES,
    561                      SSL_R_INVALID_ALERT);
     562            SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_INVALID_ALERT);
    562563            return -1;
    563564        }
     
    578579
    579580        if (alert_level == SSL3_AL_WARNING) {
    580             s->s3->warn_alert = alert_descr;
     581            s->s3.warn_alert = alert_descr;
    581582            SSL3_RECORD_set_read(rr);
    582583
    583584            s->rlayer.alert_count++;
    584585            if (s->rlayer.alert_count == MAX_WARN_ALERT_COUNT) {
    585                 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_DTLS1_READ_BYTES,
     586                SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
    586587                         SSL_R_TOO_MANY_WARN_ALERTS);
    587588                return -1;
     
    608609            }
    609610        } else if (alert_level == SSL3_AL_FATAL) {
    610             char tmp[16];
    611 
    612611            s->rwstate = SSL_NOTHING;
    613             s->s3->fatal_alert = alert_descr;
    614             SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_DTLS1_READ_BYTES,
    615                      SSL_AD_REASON_OFFSET + alert_descr);
    616             BIO_snprintf(tmp, sizeof tmp, "%d", alert_descr);
    617             ERR_add_error_data(2, "SSL alert number ", tmp);
     612            s->s3.fatal_alert = alert_descr;
     613            SSLfatal_data(s, SSL_AD_NO_ALERT,
     614                          SSL_AD_REASON_OFFSET + alert_descr,
     615                          "SSL alert number %d", alert_descr);
    618616            s->shutdown |= SSL_RECEIVED_SHUTDOWN;
    619617            SSL3_RECORD_set_read(rr);
     
    621619            return 0;
    622620        } else {
    623             SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_DTLS1_READ_BYTES,
    624                      SSL_R_UNKNOWN_ALERT_TYPE);
     621            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_ALERT_TYPE);
    625622            return -1;
    626623        }
     
    706703         */
    707704        if (!ossl_assert(SSL_is_init_finished(s))) {
    708             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_READ_BYTES,
    709                      ERR_R_INTERNAL_ERROR);
     705            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    710706            return -1;
    711707        }
     
    743739    switch (SSL3_RECORD_get_type(rr)) {
    744740    default:
    745         SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_DTLS1_READ_BYTES,
    746                  SSL_R_UNEXPECTED_RECORD);
     741        SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_RECORD);
    747742        return -1;
    748743    case SSL3_RT_CHANGE_CIPHER_SPEC:
     
    754749         * that should not happen when type != rr->type
    755750         */
    756         SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_DTLS1_READ_BYTES,
    757                  ERR_R_INTERNAL_ERROR);
     751        SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, ERR_R_INTERNAL_ERROR);
    758752        return -1;
    759753    case SSL3_RT_APPLICATION_DATA:
     
    765759         * started), we will indulge it.
    766760         */
    767         if (s->s3->in_read_app_data &&
    768             (s->s3->total_renegotiations != 0) &&
     761        if (s->s3.in_read_app_data &&
     762            (s->s3.total_renegotiations != 0) &&
    769763            ossl_statem_app_data_allowed(s)) {
    770             s->s3->in_read_app_data = 2;
     764            s->s3.in_read_app_data = 2;
    771765            return -1;
    772766        } else {
    773             SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_DTLS1_READ_BYTES,
    774                      SSL_R_UNEXPECTED_RECORD);
     767            SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_RECORD);
    775768            return -1;
    776769        }
     
    789782
    790783    if (!ossl_assert(len <= SSL3_RT_MAX_PLAIN_LENGTH)) {
    791         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_WRITE_BYTES,
    792                  ERR_R_INTERNAL_ERROR);
     784        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    793785        return -1;
    794786    }
     
    816808     */
    817809    if (!ossl_assert(SSL3_BUFFER_get_left(wb) == 0)) {
    818         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_DTLS1_WRITE,
    819                  ERR_R_INTERNAL_ERROR);
     810        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    820811        return 0;
    821812    }
    822813
    823814    /* If we have an alert to send, lets send it */
    824     if (s->s3->alert_dispatch) {
     815    if (s->s3.alert_dispatch) {
    825816        i = s->method->ssl_dispatch_alert(s);
    826817        if (i <= 0)
     
    833824
    834825    if (len > ssl_get_max_send_fragment(s)) {
    835         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_DTLS1_WRITE,
    836                  SSL_R_EXCEEDS_MAX_FRAGMENT_SIZE);
     826        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_EXCEEDS_MAX_FRAGMENT_SIZE);
    837827        return 0;
    838828    }
     
    840830    sess = s->session;
    841831
    842     if ((sess == NULL) ||
    843         (s->enc_write_ctx == NULL) || (EVP_MD_CTX_md(s->write_hash) == NULL))
     832    if ((sess == NULL)
     833            || (s->enc_write_ctx == NULL)
     834            || (EVP_MD_CTX_get0_md(s->write_hash) == NULL))
    844835        clear = 1;
    845836
     
    847838        mac_size = 0;
    848839    else {
    849         mac_size = EVP_MD_CTX_size(s->write_hash);
     840        mac_size = EVP_MD_CTX_get_size(s->write_hash);
    850841        if (mac_size < 0) {
    851             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_DTLS1_WRITE,
     842            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
    852843                     SSL_R_EXCEEDS_MAX_FRAGMENT_SIZE);
    853844            return -1;
     
    881872    /* Explicit IV length, block ciphers appropriate version flag */
    882873    if (s->enc_write_ctx) {
    883         int mode = EVP_CIPHER_CTX_mode(s->enc_write_ctx);
     874        int mode = EVP_CIPHER_CTX_get_mode(s->enc_write_ctx);
    884875        if (mode == EVP_CIPH_CBC_MODE) {
    885             eivlen = EVP_CIPHER_CTX_iv_length(s->enc_write_ctx);
     876            eivlen = EVP_CIPHER_CTX_get_iv_length(s->enc_write_ctx);
    886877            if (eivlen <= 1)
    887878                eivlen = 0;
     
    909900    if (s->compress != NULL) {
    910901        if (!ssl3_do_compress(s, &wr)) {
    911             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_DTLS1_WRITE,
    912                      SSL_R_COMPRESSION_FAILURE);
     902            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COMPRESSION_FAILURE);
    913903            return -1;
    914904        }
     
    929919                                      &(p[SSL3_RECORD_get_length(&wr) + eivlen]),
    930920                                      1)) {
    931             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_DTLS1_WRITE,
    932                      ERR_R_INTERNAL_ERROR);
     921            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    933922            return -1;
    934923        }
     
    943932        SSL3_RECORD_add_length(&wr, eivlen);
    944933
    945     if (s->method->ssl3_enc->enc(s, &wr, 1, 1) < 1) {
     934    if (s->method->ssl3_enc->enc(s, &wr, 1, 1, NULL, mac_size) < 1) {
    946935        if (!ossl_statem_in_error(s)) {
    947             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_DTLS1_WRITE,
    948                      ERR_R_INTERNAL_ERROR);
     936            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    949937        }
    950938        return -1;
     
    954942        if (!s->method->ssl3_enc->mac(s, &wr,
    955943                                      &(p[SSL3_RECORD_get_length(&wr)]), 1)) {
    956             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_DTLS1_WRITE,
    957                      ERR_R_INTERNAL_ERROR);
     944            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    958945            return -1;
    959946        }
  • trunk/src/libs/openssl-3.0.1/ssl/record/rec_layer_s3.c

    r91772 r94082  
    22 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 *
    4  * Licensed under the OpenSSL license (the "License").  You may not use
     4 * Licensed under the Apache License 2.0 (the "License").  You may not use
    55 * this file except in compliance with the License.  You can obtain a copy
    66 * in the file LICENSE in the source distribution or at
     
    1616#include <openssl/rand.h>
    1717#include "record_local.h"
    18 #include "../packet_local.h"
     18#include "internal/packet.h"
    1919
    2020#if     defined(OPENSSL_SMALL_FOOTPRINT) || \
    21         !(      defined(AESNI_ASM) &&   ( \
     21        !(      defined(AES_ASM) &&     ( \
    2222                defined(__x86_64)       || defined(__x86_64__)  || \
    2323                defined(_M_AMD64)       || defined(_M_X64)      ) \
     
    264264    if (n > rb->len - rb->offset) {
    265265        /* does not happen */
    266         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_READ_N,
    267                  ERR_R_INTERNAL_ERROR);
     266        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    268267        return -1;
    269268    }
    270269
    271     /* We always act like read_ahead is set for DTLS */
    272     if (!s->rlayer.read_ahead && !SSL_IS_DTLS(s))
     270    /*
     271     * Ktls always reads full records.
     272     * Also, we always act like read_ahead is set for DTLS.
     273     */
     274    if (!BIO_get_ktls_recv(s->rbio) && !s->rlayer.read_ahead
     275        && !SSL_IS_DTLS(s)) {
    273276        /* ignore max parameter */
    274277        max = n;
    275     else {
     278    } else {
    276279        if (max < n)
    277280            max = n;
     
    285288
    286289        /*
    287          * Now we have len+left bytes at the front of s->s3->rbuf.buf and
     290         * Now we have len+left bytes at the front of s->s3.rbuf.buf and
    288291         * need to read in more until we have len+n (up to len+max if
    289292         * possible)
     
    293296        if (s->rbio != NULL) {
    294297            s->rwstate = SSL_READING;
    295             /* TODO(size_t): Convert this function */
    296298            ret = BIO_read(s->rbio, pkt + len + left, max - left);
    297299            if (ret >= 0)
    298300                bioread = ret;
     301            if (ret <= 0
     302                    && !BIO_should_retry(s->rbio)
     303                    && BIO_eof(s->rbio)) {
     304                if (s->options & SSL_OP_IGNORE_UNEXPECTED_EOF) {
     305                    SSL_set_shutdown(s, SSL_RECEIVED_SHUTDOWN);
     306                    s->s3.warn_alert = SSL_AD_CLOSE_NOTIFY;
     307                } else {
     308                    SSLfatal(s, SSL_AD_DECODE_ERROR,
     309                             SSL_R_UNEXPECTED_EOF_WHILE_READING);
     310                }
     311            }
    299312        } else {
    300             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_READ_N,
    301                      SSL_R_READ_BIO_NOT_SET);
     313            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_READ_BIO_NOT_SET);
    302314            ret = -1;
    303315        }
     
    361373    if ((len < s->rlayer.wnum)
    362374        || ((wb->left != 0) && (len < (s->rlayer.wnum + s->rlayer.wpend_tot)))) {
    363         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_WRITE_BYTES,
    364                  SSL_R_BAD_LENGTH);
     375        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_LENGTH);
    365376        return -1;
    366377    }
     
    375386
    376387    /*
    377      * If we are supposed to be sending a KeyUpdate then go into init unless we
    378      * have writes pending - in which case we should finish doing that first.
    379      */
    380     if (wb->left == 0 && s->key_update != SSL_KEY_UPDATE_NONE)
     388     * If we are supposed to be sending a KeyUpdate or NewSessionTicket then go
     389     * into init unless we have writes pending - in which case we should finish
     390     * doing that first.
     391     */
     392    if (wb->left == 0 && (s->key_update != SSL_KEY_UPDATE_NONE
     393                          || s->ext.extra_tickets_expected > 0))
    381394        ossl_statem_set_in_init(s, 1);
    382395
     
    419432     * compromise is considered worthy.
    420433     */
    421     if (type == SSL3_RT_APPLICATION_DATA &&
    422         len >= 4 * (max_send_fragment = ssl_get_max_send_fragment(s)) &&
    423         s->compress == NULL && s->msg_callback == NULL &&
    424         !SSL_WRITE_ETM(s) && SSL_USE_EXPLICIT_IV(s) &&
    425         EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_write_ctx)) &
    426         EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK) {
     434    if (type == SSL3_RT_APPLICATION_DATA
     435            && len >= 4 * (max_send_fragment = ssl_get_max_send_fragment(s))
     436            && s->compress == NULL
     437            && s->msg_callback == NULL
     438            && !SSL_WRITE_ETM(s)
     439            && SSL_USE_EXPLICIT_IV(s)
     440            && BIO_get_ktls_send(s->wbio) == 0
     441            && (EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(s->enc_write_ctx))
     442                & EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK) != 0) {
    427443        unsigned char aad[13];
    428444        EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param;
     
    465481            }
    466482
    467             if (s->s3->alert_dispatch) {
     483            if (s->s3.alert_dispatch) {
    468484                i = s->method->ssl_dispatch_alert(s);
    469485                if (i <= 0) {
     
    567583         * shouldn't get here
    568584         */
    569         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_WRITE_BYTES,
    570                  ERR_R_INTERNAL_ERROR);
     585        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    571586        return -1;
    572587    }
    573588    if (maxpipes == 0
    574589        || s->enc_write_ctx == NULL
    575         || !(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_write_ctx))
    576              & EVP_CIPH_FLAG_PIPELINE)
     590        || (EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(s->enc_write_ctx))
     591            & EVP_CIPH_FLAG_PIPELINE) == 0
    577592        || !SSL_USE_EXPLICIT_IV(s))
    578593        maxpipes = 1;
    579     if (max_send_fragment == 0 || split_send_fragment == 0
    580         || split_send_fragment > max_send_fragment) {
     594    if (max_send_fragment == 0
     595            || split_send_fragment == 0
     596            || split_send_fragment > max_send_fragment) {
    581597        /*
    582598         * We should have prevented this when we set/get the split and max send
    583599         * fragments so we shouldn't get here
    584600         */
    585         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_WRITE_BYTES,
    586                  ERR_R_INTERNAL_ERROR);
     601        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    587602        return -1;
    588603    }
     
    634649             * in ciphersuites with known-IV weakness:
    635650             */
    636             s->s3->empty_fragment_done = 0;
     651            s->s3.empty_fragment_done = 0;
    637652
    638653            if (tmpwrit == n
     
    680695
    681696    /* If we have an alert to send, lets send it */
    682     if (s->s3->alert_dispatch) {
     697    if (s->s3.alert_dispatch) {
    683698        i = s->method->ssl_dispatch_alert(s);
    684699        if (i <= 0) {
     
    701716    sess = s->session;
    702717
    703     if ((sess == NULL) ||
    704         (s->enc_write_ctx == NULL) || (EVP_MD_CTX_md(s->write_hash) == NULL)) {
     718    if ((sess == NULL)
     719            || (s->enc_write_ctx == NULL)
     720            || (EVP_MD_CTX_get0_md(s->write_hash) == NULL)) {
    705721        clear = s->enc_write_ctx ? 0 : 1; /* must be AEAD cipher */
    706722        mac_size = 0;
    707723    } else {
    708         /* TODO(siz_t): Convert me */
    709         mac_size = EVP_MD_CTX_size(s->write_hash);
     724        mac_size = EVP_MD_CTX_get_size(s->write_hash);
    710725        if (mac_size < 0) {
    711             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
    712                      ERR_R_INTERNAL_ERROR);
     726            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    713727            goto err;
    714728        }
     
    718732     * 'create_empty_fragment' is true only when this function calls itself
    719733     */
    720     if (!clear && !create_empty_fragment && !s->s3->empty_fragment_done) {
     734    if (!clear && !create_empty_fragment && !s->s3.empty_fragment_done) {
    721735        /*
    722736         * countermeasure against known-IV weakness in CBC ciphersuites (see
     
    724738         */
    725739
    726         if (s->s3->need_empty_fragments && type == SSL3_RT_APPLICATION_DATA) {
     740        if (s->s3.need_empty_fragments && type == SSL3_RT_APPLICATION_DATA) {
    727741            /*
    728742             * recursive function call with 'create_empty_fragment' set; this
     
    743757                (SSL3_RT_HEADER_LENGTH + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD)) {
    744758                /* insufficient space */
    745                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
    746                          ERR_R_INTERNAL_ERROR);
     759                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    747760                goto err;
    748761            }
    749762        }
    750763
    751         s->s3->empty_fragment_done = 1;
     764        s->s3.empty_fragment_done = 1;
     765    }
     766
     767    if (BIO_get_ktls_send(s->wbio)) {
     768        /*
     769         * ktls doesn't modify the buffer, but to avoid a warning we need to
     770         * discard the const qualifier.
     771         * This doesn't leak memory because the buffers have been released when
     772         * switching to ktls.
     773         */
     774        SSL3_BUFFER_set_buf(&s->rlayer.wbuf[0], (unsigned char *)buf);
     775        SSL3_BUFFER_set_offset(&s->rlayer.wbuf[0], 0);
     776        SSL3_BUFFER_set_app_buffer(&s->rlayer.wbuf[0], 1);
     777        goto wpacket_init_complete;
    752778    }
    753779
     
    767793                                     SSL3_BUFFER_get_len(wb), 0)
    768794                || !WPACKET_allocate_bytes(&pkt[0], align, NULL)) {
    769             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
    770                      ERR_R_INTERNAL_ERROR);
     795            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    771796            goto err;
    772797        }
     
    779804                || !WPACKET_allocate_bytes(&pkt[0], SSL3_BUFFER_get_offset(wb)
    780805                                                    + prefix_len, NULL)) {
    781             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
    782                      ERR_R_INTERNAL_ERROR);
     806            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    783807            goto err;
    784808        }
     
    797821                                         SSL3_BUFFER_get_len(wb), 0)
    798822                    || !WPACKET_allocate_bytes(thispkt, align, NULL)) {
    799                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
    800                          ERR_R_INTERNAL_ERROR);
     823                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    801824                goto err;
    802825            }
     
    807830    /* Explicit IV length, block ciphers appropriate version flag */
    808831    if (s->enc_write_ctx && SSL_USE_EXPLICIT_IV(s) && !SSL_TREAT_AS_TLS13(s)) {
    809         int mode = EVP_CIPHER_CTX_mode(s->enc_write_ctx);
     832        int mode = EVP_CIPHER_CTX_get_mode(s->enc_write_ctx);
    810833        if (mode == EVP_CIPH_CBC_MODE) {
    811             /* TODO(size_t): Convert me */
    812             eivlen = EVP_CIPHER_CTX_iv_length(s->enc_write_ctx);
     834            eivlen = EVP_CIPHER_CTX_get_iv_length(s->enc_write_ctx);
    813835            if (eivlen <= 1)
    814836                eivlen = 0;
     
    820842        }
    821843    }
     844
     845 wpacket_init_complete:
    822846
    823847    totlen = 0;
     
    862886            maxcomplen += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
    863887
    864         /* write the header */
    865         if (!WPACKET_put_bytes_u8(thispkt, rectype)
     888        /*
     889         * When using offload kernel will write the header.
     890         * Otherwise write the header now
     891         */
     892        if (!BIO_get_ktls_send(s->wbio)
     893                && (!WPACKET_put_bytes_u8(thispkt, rectype)
    866894                || !WPACKET_put_bytes_u16(thispkt, version)
    867895                || !WPACKET_start_sub_packet_u16(thispkt)
     
    870898                || (maxcomplen > 0
    871899                    && !WPACKET_reserve_bytes(thispkt, maxcomplen,
    872                                               &compressdata))) {
    873             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
    874                      ERR_R_INTERNAL_ERROR);
     900                                              &compressdata)))) {
     901            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    875902            goto err;
    876903        }
     
    891918            if (!ssl3_do_compress(s, thiswr)
    892919                    || !WPACKET_allocate_bytes(thispkt, thiswr->length, NULL)) {
    893                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
    894                          SSL_R_COMPRESSION_FAILURE);
     920                SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COMPRESSION_FAILURE);
    895921                goto err;
    896922            }
    897923        } else {
    898             if (!WPACKET_memcpy(thispkt, thiswr->input, thiswr->length)) {
    899                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
    900                          ERR_R_INTERNAL_ERROR);
    901                 goto err;
    902             }
    903             SSL3_RECORD_reset_input(&wr[j]);
     924            if (BIO_get_ktls_send(s->wbio)) {
     925                SSL3_RECORD_reset_data(&wr[j]);
     926            } else {
     927                if (!WPACKET_memcpy(thispkt, thiswr->input, thiswr->length)) {
     928                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
     929                    goto err;
     930                }
     931                SSL3_RECORD_reset_input(&wr[j]);
     932            }
    904933        }
    905934
    906935        if (SSL_TREAT_AS_TLS13(s)
     936                && !BIO_get_ktls_send(s->wbio)
    907937                && s->enc_write_ctx != NULL
    908938                && (s->statem.enc_write_state != ENC_WRITE_STATE_WRITE_PLAIN_ALERTS
     
    911941
    912942            if (!WPACKET_put_bytes_u8(thispkt, type)) {
    913                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
    914                          ERR_R_INTERNAL_ERROR);
     943                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    915944                goto err;
    916945            }
     
    945974                        padding = max_padding;
    946975                    if (!WPACKET_memset(thispkt, 0, padding)) {
    947                         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
     976                        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
    948977                                 ERR_R_INTERNAL_ERROR);
    949978                        goto err;
     
    960989         */
    961990
    962         if (!SSL_WRITE_ETM(s) && mac_size != 0) {
     991        if (!BIO_get_ktls_send(s->wbio) && !SSL_WRITE_ETM(s) && mac_size != 0) {
    963992            unsigned char *mac;
    964993
    965994            if (!WPACKET_allocate_bytes(thispkt, mac_size, &mac)
    966995                    || !s->method->ssl3_enc->mac(s, thiswr, mac, 1)) {
    967                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
    968                          ERR_R_INTERNAL_ERROR);
     996                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    969997                goto err;
    970998            }
     
    9761004         * AEAD. SSL_RT_MAX_CIPHER_BLOCK_SIZE covers either case.
    9771005         */
    978         if (!WPACKET_reserve_bytes(thispkt, SSL_RT_MAX_CIPHER_BLOCK_SIZE,
    979                                    NULL)
    980                    /*
    981                     * We also need next the amount of bytes written to this
    982                     * sub-packet
    983                     */
     1006        if (!BIO_get_ktls_send(s->wbio)) {
     1007            if (!WPACKET_reserve_bytes(thispkt,
     1008                                        SSL_RT_MAX_CIPHER_BLOCK_SIZE,
     1009                                        NULL)
     1010                /*
     1011                 * We also need next the amount of bytes written to this
     1012                 * sub-packet
     1013                 */
    9841014                || !WPACKET_get_length(thispkt, &len)) {
    985             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
    986                      ERR_R_INTERNAL_ERROR);
     1015            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    9871016            goto err;
    988         }
    989 
    990         /* Get a pointer to the start of this record excluding header */
    991         recordstart = WPACKET_get_curr(thispkt) - len;
    992 
    993         SSL3_RECORD_set_data(thiswr, recordstart);
    994         SSL3_RECORD_reset_input(thiswr);
    995         SSL3_RECORD_set_length(thiswr, len);
     1017            }
     1018
     1019            /* Get a pointer to the start of this record excluding header */
     1020            recordstart = WPACKET_get_curr(thispkt) - len;
     1021            SSL3_RECORD_set_data(thiswr, recordstart);
     1022            SSL3_RECORD_reset_input(thiswr);
     1023            SSL3_RECORD_set_length(thiswr, len);
     1024        }
    9961025    }
    9971026
     
    10011030         * send early data - so we need to use the tls13enc function.
    10021031         */
    1003         if (tls13_enc(s, wr, numpipes, 1) < 1) {
     1032        if (tls13_enc(s, wr, numpipes, 1, NULL, mac_size) < 1) {
    10041033            if (!ossl_statem_in_error(s)) {
    1005                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
    1006                          ERR_R_INTERNAL_ERROR);
     1034                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    10071035            }
    10081036            goto err;
    10091037        }
    10101038    } else {
    1011         if (s->method->ssl3_enc->enc(s, wr, numpipes, 1) < 1) {
    1012             if (!ossl_statem_in_error(s)) {
    1013                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
    1014                          ERR_R_INTERNAL_ERROR);
    1015             }
    1016             goto err;
     1039        if (!BIO_get_ktls_send(s->wbio)) {
     1040            if (s->method->ssl3_enc->enc(s, wr, numpipes, 1, NULL,
     1041                                         mac_size) < 1) {
     1042                if (!ossl_statem_in_error(s)) {
     1043                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
     1044                }
     1045                goto err;
     1046            }
    10171047        }
    10181048    }
     
    10231053        thispkt = &pkt[j];
    10241054        thiswr = &wr[j];
     1055
     1056        if (BIO_get_ktls_send(s->wbio))
     1057            goto mac_done;
    10251058
    10261059        /* Allocate bytes for the encryption overhead */
     
    10301063                || (thiswr->length > origlen
    10311064                    && !WPACKET_allocate_bytes(thispkt,
    1032                                                thiswr->length - origlen, NULL))) {
    1033             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
    1034                     ERR_R_INTERNAL_ERROR);
     1065                                               thiswr->length - origlen,
     1066                                               NULL))) {
     1067            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    10351068            goto err;
    10361069        }
     
    10401073            if (!WPACKET_allocate_bytes(thispkt, mac_size, &mac)
    10411074                    || !s->method->ssl3_enc->mac(s, thiswr, mac, 1)) {
    1042                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
    1043                          ERR_R_INTERNAL_ERROR);
     1075                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    10441076                goto err;
    10451077            }
     
    10491081        if (!WPACKET_get_length(thispkt, &len)
    10501082                || !WPACKET_close(thispkt)) {
    1051             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
    1052                      ERR_R_INTERNAL_ERROR);
     1083            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    10531084            goto err;
    10541085        }
     
    10571088            recordstart = WPACKET_get_curr(thispkt) - len
    10581089                          - SSL3_RT_HEADER_LENGTH;
    1059             s->msg_callback(1, 0, SSL3_RT_HEADER, recordstart,
     1090            s->msg_callback(1, thiswr->rec_version, SSL3_RT_HEADER, recordstart,
    10601091                            SSL3_RT_HEADER_LENGTH, s,
    10611092                            s->msg_callback_arg);
     
    10641095                unsigned char ctype = type;
    10651096
    1066                 s->msg_callback(1, s->version, SSL3_RT_INNER_CONTENT_TYPE,
     1097                s->msg_callback(1, thiswr->rec_version, SSL3_RT_INNER_CONTENT_TYPE,
    10671098                                &ctype, 1, s, s->msg_callback_arg);
    10681099            }
     
    10701101
    10711102        if (!WPACKET_finish(thispkt)) {
    1072             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
    1073                      ERR_R_INTERNAL_ERROR);
     1103            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    10741104            goto err;
    10751105        }
    10761106
    1077         /*
    1078          * we should now have thiswr->data pointing to the encrypted data, which
    1079          * is thiswr->length long
    1080          */
    1081         SSL3_RECORD_set_type(thiswr, type); /* not needed but helps for
    1082                                              * debugging */
    1083         SSL3_RECORD_add_length(thiswr, SSL3_RT_HEADER_LENGTH);
     1107        /* header is added by the kernel when using offload */
     1108        SSL3_RECORD_add_length(&wr[j], SSL3_RT_HEADER_LENGTH);
    10841109
    10851110        if (create_empty_fragment) {
     
    10901115            if (j > 0) {
    10911116                /* We should never be pipelining an empty fragment!! */
    1092                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
    1093                          ERR_R_INTERNAL_ERROR);
     1117                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    10941118                goto err;
    10951119            }
     
    10971121            return 1;
    10981122        }
     1123
     1124 mac_done:
     1125        /*
     1126         * we should now have thiswr->data pointing to the encrypted data, which
     1127         * is thiswr->length long
     1128         */
     1129        SSL3_RECORD_set_type(thiswr, type); /* not needed but helps for
     1130                                             * debugging */
    10991131
    11001132        /* now let's set up wb */
     
    11201152}
    11211153
    1122 /* if s->s3->wbuf.left != 0, we need to call this
     1154/* if s->s3.wbuf.left != 0, we need to call this
    11231155 *
    11241156 * Return values are as per SSL_write()
     
    11361168            && (s->rlayer.wpend_buf != buf))
    11371169        || (s->rlayer.wpend_type != type)) {
    1138         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_WRITE_PENDING,
    1139                  SSL_R_BAD_WRITE_RETRY);
     1170        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_WRITE_RETRY);
    11401171        return -1;
    11411172    }
     
    11511182        if (s->wbio != NULL) {
    11521183            s->rwstate = SSL_WRITING;
    1153             /* TODO(size_t): Convert this call */
     1184
     1185            /*
     1186             * To prevent coalescing of control and data messages,
     1187             * such as in buffer_write, we flush the BIO
     1188             */
     1189            if (BIO_get_ktls_send(s->wbio) && type != SSL3_RT_APPLICATION_DATA) {
     1190                i = BIO_flush(s->wbio);
     1191                if (i <= 0)
     1192                    return i;
     1193                BIO_set_ktls_ctrl_msg(s->wbio, type);
     1194            }
    11541195            i = BIO_write(s->wbio, (char *)
    11551196                          &(SSL3_BUFFER_get_buf(&wb[currbuf])
     
    11591200                tmpwrit = i;
    11601201        } else {
    1161             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_WRITE_PENDING,
    1162                      SSL_R_BIO_NOT_SET);
     1202            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BIO_NOT_SET);
    11631203            i = -1;
    11641204        }
    1165         if (i > 0 && tmpwrit == SSL3_BUFFER_get_left(&wb[currbuf])) {
     1205
     1206        /*
     1207         * When an empty fragment is sent on a connection using KTLS,
     1208         * it is sent as a write of zero bytes.  If this zero byte
     1209         * write succeeds, i will be 0 rather than a non-zero value.
     1210         * Treat i == 0 as success rather than an error for zero byte
     1211         * writes to permit this case.
     1212         */
     1213        if (i >= 0 && tmpwrit == SSL3_BUFFER_get_left(&wb[currbuf])) {
    11661214            SSL3_BUFFER_set_left(&wb[currbuf], 0);
    11671215            SSL3_BUFFER_add_offset(&wb[currbuf], tmpwrit);
     
    11991247 * This function must handle any surprises the peer may have for us, such as
    12001248 * Alert records (e.g. close_notify) or renegotiation requests. ChangeCipherSpec
    1201  * messages are treated as if they were handshake messages *if* the |recd_type|
     1249 * messages are treated as if they were handshake messages *if* the |recvd_type|
    12021250 * argument is non NULL.
    12031251 * Also if record payloads contain fragments too small to process, we store
     
    12391287                                             && (type !=
    12401288                                                 SSL3_RT_APPLICATION_DATA))) {
    1241         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_READ_BYTES,
    1242                  ERR_R_INTERNAL_ERROR);
     1289        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    12431290        return -1;
    12441291    }
     
    13071354            if (num_recs == 0) {
    13081355                /* Shouldn't happen */
    1309                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_READ_BYTES,
    1310                          ERR_R_INTERNAL_ERROR);
     1356                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    13111357                return -1;
    13121358            }
     
    13271373            && SSL3_RECORD_get_type(rr) != SSL3_RT_HANDSHAKE
    13281374            && SSL_IS_TLS13(s)) {
    1329         SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_READ_BYTES,
     1375        SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
    13301376                 SSL_R_MIXED_HANDSHAKE_AND_NON_HANDSHAKE_DATA);
    13311377        return -1;
     
    13421388    /* we now have a packet which can be read and processed */
    13431389
    1344     if (s->s3->change_cipher_spec /* set when we receive ChangeCipherSpec,
    1345                                    * reset by ssl3_get_finished */
     1390    if (s->s3.change_cipher_spec /* set when we receive ChangeCipherSpec,
     1391                                  * reset by ssl3_get_finished */
    13461392        && (SSL3_RECORD_get_type(rr) != SSL3_RT_HANDSHAKE)) {
    1347         SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_READ_BYTES,
     1393        SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
    13481394                 SSL_R_DATA_BETWEEN_CCS_AND_FINISHED);
    13491395        return -1;
     
    13751421        if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&
    13761422            (s->enc_read_ctx == NULL)) {
    1377             SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_READ_BYTES,
    1378                      SSL_R_APP_DATA_IN_HANDSHAKE);
     1423            SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_APP_DATA_IN_HANDSHAKE);
    13791424            return -1;
    13801425        }
     
    13831428            && SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC
    13841429            && s->rlayer.handshake_fragment_len > 0) {
    1385             SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_READ_BYTES,
    1386                      SSL_R_CCS_RECEIVED_EARLY);
     1430            SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_CCS_RECEIVED_EARLY);
    13871431            return -1;
    13881432        }
     
    14161460                    SSL3_RECORD_set_read(rr);
    14171461            } else {
     1462                if (s->options & SSL_OP_CLEANSE_PLAINTEXT)
     1463                    OPENSSL_cleanse(&(rr->data[rr->off]), n);
    14181464                SSL3_RECORD_sub_length(rr, n);
    14191465                SSL3_RECORD_add_off(rr, n);
     
    14601506         * |rr->type|. If not then something has gone horribly wrong
    14611507         */
    1462         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_READ_BYTES,
    1463                  ERR_R_INTERNAL_ERROR);
     1508        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    14641509        return -1;
    14651510    }
     
    14691514        /*
    14701515         * If we've got this far and still haven't decided on what version
    1471          * we're using then this must be a client side alert we're dealing with
    1472          * (we don't allow heartbeats yet). We shouldn't be receiving anything
    1473          * other than a ClientHello if we are a server.
     1516         * we're using then this must be a client side alert we're dealing
     1517         * with. We shouldn't be receiving anything other than a ClientHello
     1518         * if we are a server.
    14741519         */
    14751520        s->version = rr->rec_version;
    1476         SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_READ_BYTES,
    1477                  SSL_R_UNEXPECTED_MESSAGE);
     1521        SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
    14781522        return -1;
    14791523    }
     
    14941538                || !PACKET_get_1(&alert, &alert_descr)
    14951539                || PACKET_remaining(&alert) != 0) {
    1496             SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_READ_BYTES,
    1497                      SSL_R_INVALID_ALERT);
     1540            SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_INVALID_ALERT);
    14981541            return -1;
    14991542        }
     
    15151558        if (alert_level == SSL3_AL_WARNING
    15161559                || (is_tls13 && alert_descr == SSL_AD_USER_CANCELLED)) {
    1517             s->s3->warn_alert = alert_descr;
     1560            s->s3.warn_alert = alert_descr;
    15181561            SSL3_RECORD_set_read(rr);
    15191562
    15201563            s->rlayer.alert_count++;
    15211564            if (s->rlayer.alert_count == MAX_WARN_ALERT_COUNT) {
    1522                 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_READ_BYTES,
     1565                SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
    15231566                         SSL_R_TOO_MANY_WARN_ALERTS);
    15241567                return -1;
     
    15371580            return 0;
    15381581        } else if (alert_level == SSL3_AL_FATAL || is_tls13) {
    1539             char tmp[16];
    1540 
    15411582            s->rwstate = SSL_NOTHING;
    1542             s->s3->fatal_alert = alert_descr;
    1543             SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_SSL3_READ_BYTES,
    1544                      SSL_AD_REASON_OFFSET + alert_descr);
    1545             BIO_snprintf(tmp, sizeof tmp, "%d", alert_descr);
    1546             ERR_add_error_data(2, "SSL alert number ", tmp);
     1583            s->s3.fatal_alert = alert_descr;
     1584            SSLfatal_data(s, SSL_AD_NO_ALERT,
     1585                          SSL_AD_REASON_OFFSET + alert_descr,
     1586                          "SSL alert number %d", alert_descr);
    15471587            s->shutdown |= SSL_RECEIVED_SHUTDOWN;
    15481588            SSL3_RECORD_set_read(rr);
     
    15581598             * the peer refused it where we carry on.
    15591599             */
    1560             SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_SSL3_READ_BYTES,
    1561                      SSL_R_NO_RENEGOTIATION);
     1600            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_NO_RENEGOTIATION);
    15621601            return -1;
    15631602        } else if (alert_level == SSL3_AL_WARNING) {
     
    15661605        }
    15671606
    1568         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_SSL3_READ_BYTES,
    1569                  SSL_R_UNKNOWN_ALERT_TYPE);
     1607        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_ALERT_TYPE);
    15701608        return -1;
    15711609    }
     
    16051643            SSL3_RECORD_set_length(rr, 0);
    16061644            SSL3_RECORD_set_read(rr);
    1607             SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_SSL3_READ_BYTES,
     1645            SSLfatal(s, SSL_AD_NO_ALERT,
    16081646                     SSL_R_APPLICATION_DATA_AFTER_CLOSE_NOTIFY);
    16091647            return -1;
     
    16401678
    16411679    if (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC) {
    1642         SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_READ_BYTES,
    1643                  SSL_R_CCS_RECEIVED_EARLY);
     1680        SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_CCS_RECEIVED_EARLY);
    16441681        return -1;
    16451682    }
     
    17011738         * record types, using up resources processing them.
    17021739         */
    1703         SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_READ_BYTES,
    1704                  SSL_R_UNEXPECTED_RECORD);
     1740        SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_RECORD);
    17051741        return -1;
    17061742    case SSL3_RT_CHANGE_CIPHER_SPEC:
     
    17121748         * that should not happen when type != rr->type
    17131749         */
    1714         SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_READ_BYTES,
    1715                  ERR_R_INTERNAL_ERROR);
     1750        SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, ERR_R_INTERNAL_ERROR);
    17161751        return -1;
    17171752    case SSL3_RT_APPLICATION_DATA:
     
    17241759         */
    17251760        if (ossl_statem_app_data_allowed(s)) {
    1726             s->s3->in_read_app_data = 2;
     1761            s->s3.in_read_app_data = 2;
    17271762            return -1;
    17281763        } else if (ossl_statem_skip_early_data(s)) {
     
    17441779            goto start;
    17451780        } else {
    1746             SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_READ_BYTES,
    1747                      SSL_R_UNEXPECTED_RECORD);
     1781            SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_RECORD);
    17481782            return -1;
    17491783        }
  • trunk/src/libs/openssl-3.0.1/ssl/record/record.h

    r91772 r94082  
    11/*
    2  * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
    33 *
    4  * Licensed under the OpenSSL license (the "License").  You may not use
     4 * Licensed under the Apache License 2.0 (the "License").  You may not use
    55 * this file except in compliance with the License.  You can obtain a copy
    66 * in the file LICENSE in the source distribution or at
     
    2626    /* how many bytes left */
    2727    size_t left;
     28    /* 'buf' is from application for KTLS */
     29    int app_buffer;
    2830} SSL3_BUFFER;
    2931
     
    177179 *****************************************************************************/
    178180
     181struct ssl_mac_buf_st {
     182    unsigned char *mac;
     183    int alloced;
     184};
     185typedef struct ssl_mac_buf_st SSL_MAC_BUF;
     186
    179187#define MIN_SSL2_RECORD_LEN     9
    180188
     
    212220                           size_t *readbytes);
    213221__owur int ssl3_setup_buffers(SSL *s);
    214 __owur int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, size_t n_recs, int send);
     222__owur int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, size_t n_recs, int send,
     223                    SSL_MAC_BUF *mac, size_t macsize);
    215224__owur int n_ssl3_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int send);
    216225__owur int ssl3_write_pending(SSL *s, int type, const unsigned char *buf, size_t len,
    217226                              size_t *written);
    218 __owur int tls1_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int send);
     227__owur int tls1_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int sending,
     228                    SSL_MAC_BUF *mac, size_t macsize);
    219229__owur int tls1_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int send);
    220 __owur int tls13_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int send);
     230__owur int tls13_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int send,
     231                     SSL_MAC_BUF *mac, size_t macsize);
    221232int DTLS_RECORD_LAYER_new(RECORD_LAYER *rl);
    222233void DTLS_RECORD_LAYER_free(RECORD_LAYER *rl);
  • trunk/src/libs/openssl-3.0.1/ssl/record/record_local.h

    r91772 r94082  
    11/*
    2  * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
    33 *
    4  * Licensed under the OpenSSL license (the "License").  You may not use
     4 * Licensed under the Apache License 2.0 (the "License").  You may not use
    55 * this file except in compliance with the License.  You can obtain a copy
    66 * in the file LICENSE in the source distribution or at
     
    6666#define SSL3_BUFFER_is_initialised(b)       ((b)->buf != NULL)
    6767#define SSL3_BUFFER_set_default_len(b, l)   ((b)->default_len = (l))
     68#define SSL3_BUFFER_set_app_buffer(b, l)    ((b)->app_buffer = (l))
     69#define SSL3_BUFFER_is_app_buffer(b)        ((b)->app_buffer)
    6870
    6971void SSL3_BUFFER_clear(SSL3_BUFFER *b);
     
    8991#define SSL3_RECORD_set_input(r, i)             ((r)->input = (i))
    9092#define SSL3_RECORD_reset_input(r)              ((r)->input = (r)->data)
     93#define SSL3_RECORD_reset_data(r)               ((r)->data = (r)->input)
    9194#define SSL3_RECORD_get_seq_num(r)              ((r)->seq_num)
    9295#define SSL3_RECORD_get_off(r)                  ((r)->off)
     
    105108__owur int ssl3_do_compress(SSL *ssl, SSL3_RECORD *wr);
    106109__owur int ssl3_do_uncompress(SSL *ssl, SSL3_RECORD *rr);
    107 int ssl3_cbc_copy_mac(unsigned char *out,
    108                        const SSL3_RECORD *rec, size_t md_size);
    109 __owur int ssl3_cbc_remove_padding(SSL3_RECORD *rec,
    110                                    size_t block_size, size_t mac_size);
    111 __owur int tls1_cbc_remove_padding(const SSL *s,
    112                                    SSL3_RECORD *rec,
    113                                    size_t block_size, size_t mac_size);
     110__owur int ssl3_cbc_remove_padding_and_mac(size_t *reclen,
     111                                           size_t origreclen,
     112                                           unsigned char *recdata,
     113                                           unsigned char **mac,
     114                                           int *alloced,
     115                                           size_t block_size, size_t mac_size,
     116                                           OSSL_LIB_CTX *libctx);
     117__owur int tls1_cbc_remove_padding_and_mac(size_t *reclen,
     118                                           size_t origreclen,
     119                                           unsigned char *recdata,
     120                                           unsigned char **mac,
     121                                           int *alloced,
     122                                           size_t block_size, size_t mac_size,
     123                                           int aead,
     124                                           OSSL_LIB_CTX *libctx);
    114125int dtls1_process_record(SSL *s, DTLS1_BITMAP *bitmap);
    115126__owur int dtls1_get_record(SSL *s);
  • trunk/src/libs/openssl-3.0.1/ssl/record/ssl3_buffer.c

    r91772 r94082  
    22 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 *
    4  * Licensed under the OpenSSL license (the "License").  You may not use
     4 * Licensed under the Apache License 2.0 (the "License").  You may not use
    55 * this file except in compliance with the License.  You can obtain a copy
    66 * in the file LICENSE in the source distribution or at
     
    6767             * alert.
    6868             */
    69             SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_SSL3_SETUP_READ_BUFFER,
    70                      ERR_R_MALLOC_FAILURE);
     69            SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_MALLOC_FAILURE);
    7170            return 0;
    7271        }
     
    111110        SSL3_BUFFER *thiswb = &wb[currpipe];
    112111
    113         if (thiswb->buf != NULL && thiswb->len != len) {
     112        if (thiswb->len != len) {
    114113            OPENSSL_free(thiswb->buf);
    115114            thiswb->buf = NULL;         /* force reallocation */
     
    117116
    118117        if (thiswb->buf == NULL) {
    119             p = OPENSSL_malloc(len);
    120             if (p == NULL) {
    121                 s->rlayer.numwpipes = currpipe;
    122                 /*
    123                  * We've got a malloc failure, and we're still initialising
    124                  * buffers. We assume we're so doomed that we won't even be able
    125                  * to send an alert.
    126                  */
    127                 SSLfatal(s, SSL_AD_NO_ALERT,
    128                          SSL_F_SSL3_SETUP_WRITE_BUFFER, ERR_R_MALLOC_FAILURE);
    129                 return 0;
     118            if (s->wbio == NULL || !BIO_get_ktls_send(s->wbio)) {
     119                p = OPENSSL_malloc(len);
     120                if (p == NULL) {
     121                    s->rlayer.numwpipes = currpipe;
     122                    /*
     123                     * We've got a malloc failure, and we're still initialising
     124                     * buffers. We assume we're so doomed that we won't even be able
     125                     * to send an alert.
     126                     */
     127                    SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_MALLOC_FAILURE);
     128                    return 0;
     129                }
     130            } else {
     131                p = NULL;
    130132            }
    131133            memset(thiswb, 0, sizeof(SSL3_BUFFER));
     
    160162        wb = &RECORD_LAYER_get_wbuf(&s->rlayer)[pipes - 1];
    161163
    162         OPENSSL_free(wb->buf);
     164        if (SSL3_BUFFER_is_app_buffer(wb))
     165            SSL3_BUFFER_set_app_buffer(wb, 0);
     166        else
     167            OPENSSL_free(wb->buf);
    163168        wb->buf = NULL;
    164169        pipes--;
     
    173178
    174179    b = RECORD_LAYER_get_rbuf(&s->rlayer);
     180    if (s->options & SSL_OP_CLEANSE_PLAINTEXT)
     181        OPENSSL_cleanse(b->buf, b->len);
    175182    OPENSSL_free(b->buf);
    176183    b->buf = NULL;
  • trunk/src/libs/openssl-3.0.1/ssl/record/ssl3_record.c

    r91772 r94082  
    22 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 *
    4  * Licensed under the OpenSSL license (the "License").  You may not use
     4 * Licensed under the Apache License 2.0 (the "License").  You may not use
    55 * this file except in compliance with the License.  You can obtain a copy
    66 * in the file LICENSE in the source distribution or at
     
    99
    1010#include "../ssl_local.h"
    11 #include "internal/constant_time.h"
     11#include <openssl/trace.h>
    1212#include <openssl/rand.h>
     13#include <openssl/core_names.h>
    1314#include "record_local.h"
    1415#include "internal/cryptlib.h"
     
    115116        if (!ossl_assert(s->psksession != NULL
    116117                         && s->psksession->ext.max_early_data > 0)) {
    117             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_EARLY_DATA_COUNT_OK,
    118                      ERR_R_INTERNAL_ERROR);
     118            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    119119            return 0;
    120120        }
     
    132132    if (max_early_data == 0) {
    133133        SSLfatal(s, send ? SSL_AD_INTERNAL_ERROR : SSL_AD_UNEXPECTED_MESSAGE,
    134                  SSL_F_EARLY_DATA_COUNT_OK, SSL_R_TOO_MUCH_EARLY_DATA);
     134                 SSL_R_TOO_MUCH_EARLY_DATA);
    135135        return 0;
    136136    }
     
    141141    if (s->early_data_count + length > max_early_data) {
    142142        SSLfatal(s, send ? SSL_AD_INTERNAL_ERROR : SSL_AD_UNEXPECTED_MESSAGE,
    143                  SSL_F_EARLY_DATA_COUNT_OK, SSL_R_TOO_MUCH_EARLY_DATA);
     143                 SSL_R_TOO_MUCH_EARLY_DATA);
    144144        return 0;
    145145    }
     
    182182    unsigned char md[EVP_MAX_MD_SIZE];
    183183    unsigned int version;
    184     size_t mac_size;
     184    size_t mac_size = 0;
    185185    int imac_size;
    186186    size_t num_recs = 0, max_recs, j;
    187187    PACKET pkt, sslv2pkt;
    188     size_t first_rec_len;
     188    int is_ktls_left;
     189    SSL_MAC_BUF *macbufs = NULL;
     190    int ret = -1;
    189191
    190192    rr = RECORD_LAYER_get_rrec(&s->rlayer);
    191193    rbuf = RECORD_LAYER_get_rbuf(&s->rlayer);
     194    is_ktls_left = (rbuf->left > 0);
    192195    max_recs = s->max_pipelines;
    193196    if (max_recs == 0)
     
    208211                               SSL3_BUFFER_get_len(rbuf), 0,
    209212                               num_recs == 0 ? 1 : 0, &n);
    210             if (rret <= 0)
    211                 return rret;     /* error or non-blocking */
     213            if (rret <= 0) {
     214#ifndef OPENSSL_NO_KTLS
     215                if (!BIO_get_ktls_recv(s->rbio) || rret == 0)
     216                    return rret;     /* error or non-blocking */
     217                switch (errno) {
     218                case EBADMSG:
     219                    SSLfatal(s, SSL_AD_BAD_RECORD_MAC,
     220                             SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
     221                    break;
     222                case EMSGSIZE:
     223                    SSLfatal(s, SSL_AD_RECORD_OVERFLOW,
     224                             SSL_R_PACKET_LENGTH_TOO_LONG);
     225                    break;
     226                case EINVAL:
     227                    SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
     228                             SSL_R_WRONG_VERSION_NUMBER);
     229                    break;
     230                default:
     231                    break;
     232                }
     233#endif
     234                return rret;
     235            }
    212236            RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);
    213237
     
    215239            if (!PACKET_buf_init(&pkt, RECORD_LAYER_get_packet(&s->rlayer),
    216240                                 RECORD_LAYER_get_packet_length(&s->rlayer))) {
    217                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_GET_RECORD,
    218                          ERR_R_INTERNAL_ERROR);
     241                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    219242                return -1;
    220243            }
     
    222245            if (!PACKET_get_net_2_len(&sslv2pkt, &sslv2len)
    223246                    || !PACKET_get_1(&sslv2pkt, &type)) {
    224                 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL3_GET_RECORD,
    225                          ERR_R_INTERNAL_ERROR);
     247                SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_INTERNAL_ERROR);
    226248                return -1;
    227249            }
     
    248270                if (thisrr->length > SSL3_BUFFER_get_len(rbuf)
    249271                    - SSL2_RT_HEADER_LENGTH) {
    250                     SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
     272                    SSLfatal(s, SSL_AD_RECORD_OVERFLOW,
    251273                             SSL_R_PACKET_LENGTH_TOO_LONG);
    252274                    return -1;
     
    254276
    255277                if (thisrr->length < MIN_SSL2_RECORD_LEN) {
    256                     SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL3_GET_RECORD,
    257                              SSL_R_LENGTH_TOO_SHORT);
     278                    SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
    258279                    return -1;
    259280                }
    260281            } else {
    261282                /* SSLv3+ style record */
    262                 if (s->msg_callback)
    263                     s->msg_callback(0, 0, SSL3_RT_HEADER, p, 5, s,
    264                                     s->msg_callback_arg);
    265283
    266284                /* Pull apart the header into the SSL3_RECORD */
     
    268286                        || !PACKET_get_net_2(&pkt, &version)
    269287                        || !PACKET_get_net_2_len(&pkt, &thisrr->length)) {
    270                     SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL3_GET_RECORD,
    271                              ERR_R_INTERNAL_ERROR);
     288                    if (s->msg_callback)
     289                        s->msg_callback(0, 0, SSL3_RT_HEADER, p, 5, s,
     290                                        s->msg_callback_arg);
     291                    SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_INTERNAL_ERROR);
    272292                    return -1;
    273293                }
    274294                thisrr->type = type;
    275295                thisrr->rec_version = version;
     296
     297                if (s->msg_callback)
     298                    s->msg_callback(0, version, SSL3_RT_HEADER, p, 5, s,
     299                                    s->msg_callback_arg);
    276300
    277301                /*
     
    296320                             * end.
    297321                             */
    298                             SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_SSL3_GET_RECORD,
     322                            SSLfatal(s, SSL_AD_NO_ALERT,
    299323                                     SSL_R_WRONG_VERSION_NUMBER);
    300324                            return -1;
     
    305329                        s->version = (unsigned short)version;
    306330                    }
    307                     SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_F_SSL3_GET_RECORD,
     331                    SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
    308332                             SSL_R_WRONG_VERSION_NUMBER);
    309333                    return -1;
     
    319343                            strncmp((char *)p, "HEAD ", 5) == 0 ||
    320344                            strncmp((char *)p, "PUT ", 4) == 0) {
    321                             SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_SSL3_GET_RECORD,
    322                                      SSL_R_HTTP_REQUEST);
     345                            SSLfatal(s, SSL_AD_NO_ALERT, SSL_R_HTTP_REQUEST);
    323346                            return -1;
    324347                        } else if (strncmp((char *)p, "CONNE", 5) == 0) {
    325                             SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_SSL3_GET_RECORD,
     348                            SSLfatal(s, SSL_AD_NO_ALERT,
    326349                                     SSL_R_HTTPS_PROXY_REQUEST);
    327350                            return -1;
     
    329352
    330353                        /* Doesn't look like TLS - don't send an alert */
    331                         SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_SSL3_GET_RECORD,
     354                        SSLfatal(s, SSL_AD_NO_ALERT,
    332355                                 SSL_R_WRONG_VERSION_NUMBER);
    333356                        return -1;
    334357                    } else {
    335358                        SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
    336                                  SSL_F_SSL3_GET_RECORD,
    337359                                 SSL_R_WRONG_VERSION_NUMBER);
    338360                        return -1;
     
    348370                                   != ENC_READ_STATE_ALLOW_PLAIN_ALERTS)) {
    349371                        SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
    350                                  SSL_F_SSL3_GET_RECORD, SSL_R_BAD_RECORD_TYPE);
     372                                 SSL_R_BAD_RECORD_TYPE);
    351373                        return -1;
    352374                    }
    353375                    if (thisrr->rec_version != TLS1_2_VERSION) {
    354                         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL3_GET_RECORD,
     376                        SSLfatal(s, SSL_AD_DECODE_ERROR,
    355377                                 SSL_R_WRONG_VERSION_NUMBER);
    356378                        return -1;
     
    360382                if (thisrr->length >
    361383                    SSL3_BUFFER_get_len(rbuf) - SSL3_RT_HEADER_LENGTH) {
    362                     SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
     384                    SSLfatal(s, SSL_AD_RECORD_OVERFLOW,
    363385                             SSL_R_PACKET_LENGTH_TOO_LONG);
    364386                    return -1;
     
    371393        if (SSL_IS_TLS13(s)) {
    372394            if (thisrr->length > SSL3_RT_MAX_TLS13_ENCRYPTED_LENGTH) {
    373                 SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
     395                SSLfatal(s, SSL_AD_RECORD_OVERFLOW,
    374396                         SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
    375397                return -1;
     
    387409#endif
    388410
    389             if (thisrr->length > len) {
    390                 SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
     411            if (thisrr->length > len && !BIO_get_ktls_recv(s->rbio)) {
     412                SSLfatal(s, SSL_AD_RECORD_OVERFLOW,
    391413                         SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
    392414                return -1;
     
    405427            more = thisrr->length;
    406428        }
     429
    407430        if (more > 0) {
    408431            /* now s->rlayer.packet_length == SSL3_RT_HEADER_LENGTH */
     
    458481             && SSL_USE_EXPLICIT_IV(s)
    459482             && s->enc_read_ctx != NULL
    460              && (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_read_ctx))
    461                  & EVP_CIPH_FLAG_PIPELINE)
     483             && (EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(s->enc_read_ctx))
     484                 & EVP_CIPH_FLAG_PIPELINE) != 0
    462485             && ssl3_record_app_data_waiting(s));
    463486
     
    470493         */
    471494        if (thisrr->length != 1 || thisrr->data[0] != 0x01) {
    472             SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_SSL3_GET_RECORD,
     495            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
    473496                     SSL_R_INVALID_CCS_MESSAGE);
    474497            return -1;
     
    482505        if (RECORD_LAYER_get_empty_record_count(&s->rlayer)
    483506            > MAX_EMPTY_RECORDS) {
    484             SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_GET_RECORD,
     507            SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
    485508                     SSL_R_UNEXPECTED_CCS_MESSAGE);
    486509            return -1;
     
    490513
    491514        return 1;
     515    }
     516
     517    /*
     518     * KTLS reads full records. If there is any data left,
     519     * then it is from before enabling ktls
     520     */
     521    if (BIO_get_ktls_recv(s->rbio) && !is_ktls_left)
     522        goto skip_decryption;
     523
     524    if (s->read_hash != NULL) {
     525        const EVP_MD *tmpmd = EVP_MD_CTX_get0_md(s->read_hash);
     526
     527        if (tmpmd != NULL) {
     528            imac_size = EVP_MD_get_size(tmpmd);
     529            if (!ossl_assert(imac_size >= 0 && imac_size <= EVP_MAX_MD_SIZE)) {
     530                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
     531                    return -1;
     532            }
     533            mac_size = (size_t)imac_size;
     534        }
    492535    }
    493536
     
    498541    if (SSL_READ_ETM(s) && s->read_hash) {
    499542        unsigned char *mac;
    500         /* TODO(size_t): convert this to do size_t properly */
    501         imac_size = EVP_MD_CTX_size(s->read_hash);
    502         if (!ossl_assert(imac_size >= 0 && imac_size <= EVP_MAX_MD_SIZE)) {
    503                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_GET_RECORD,
    504                          ERR_LIB_EVP);
    505                 return -1;
    506         }
    507         mac_size = (size_t)imac_size;
     543
    508544        for (j = 0; j < num_recs; j++) {
    509545            thisrr = &rr[j];
    510546
    511547            if (thisrr->length < mac_size) {
    512                 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL3_GET_RECORD,
    513                          SSL_R_LENGTH_TOO_SHORT);
     548                SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
    514549                return -1;
    515550            }
     
    518553            i = s->method->ssl3_enc->mac(s, thisrr, md, 0 /* not send */ );
    519554            if (i == 0 || CRYPTO_memcmp(md, mac, mac_size) != 0) {
    520                 SSLfatal(s, SSL_AD_BAD_RECORD_MAC, SSL_F_SSL3_GET_RECORD,
    521                        SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
     555                SSLfatal(s, SSL_AD_BAD_RECORD_MAC,
     556                         SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
    522557                return -1;
    523558            }
    524559        }
    525     }
    526 
    527     first_rec_len = rr[0].length;
    528 
    529     enc_err = s->method->ssl3_enc->enc(s, rr, num_recs, 0);
     560        /*
     561         * We've handled the mac now - there is no MAC inside the encrypted
     562         * record
     563         */
     564        mac_size = 0;
     565    }
     566
     567    if (mac_size > 0) {
     568        macbufs = OPENSSL_zalloc(sizeof(*macbufs) * num_recs);
     569        if (macbufs == NULL) {
     570            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
     571            return -1;
     572        }
     573    }
     574
     575    enc_err = s->method->ssl3_enc->enc(s, rr, num_recs, 0, macbufs, mac_size);
    530576
    531577    /*-
    532578     * enc_err is:
    533      *    0: (in non-constant time) if the record is publicly invalid.
    534      *    1: if the padding is valid
    535      *    -1: if the padding is invalid
     579     *    0: if the record is publicly invalid, or an internal error, or AEAD
     580     *       decryption failed, or ETM decryption failed.
     581     *    1: Success or MTE decryption failed (MAC will be randomised)
    536582     */
    537583    if (enc_err == 0) {
    538584        if (ossl_statem_in_error(s)) {
    539585            /* SSLfatal() already got called */
    540             return -1;
     586            goto end;
    541587        }
    542588        if (num_recs == 1 && ossl_statem_skip_early_data(s)) {
    543589            /*
    544              * Valid early_data that we cannot decrypt might fail here as
    545              * publicly invalid. We treat it like an empty record.
     590             * Valid early_data that we cannot decrypt will fail here. We treat
     591             * it like an empty record.
    546592             */
    547593
     
    551597                                     EARLY_DATA_CIPHERTEXT_OVERHEAD, 0)) {
    552598                /* SSLfatal() already called */
    553                 return -1;
     599                goto end;
    554600            }
    555601
     
    558604            RECORD_LAYER_set_numrpipes(&s->rlayer, 1);
    559605            RECORD_LAYER_reset_read_sequence(&s->rlayer);
    560             return 1;
    561         }
    562         SSLfatal(s, SSL_AD_BAD_RECORD_MAC, SSL_F_SSL3_GET_RECORD,
    563                  SSL_R_BLOCK_CIPHER_PAD_IS_WRONG);
    564         return -1;
    565     }
    566 #ifdef SSL_DEBUG
    567     printf("dec %lu\n", (unsigned long)rr[0].length);
    568     {
    569         size_t z;
    570         for (z = 0; z < rr[0].length; z++)
    571             printf("%02X%c", rr[0].data[z], ((z + 1) % 16) ? ' ' : '\n');
    572     }
    573     printf("\n");
    574 #endif
     606            ret = 1;
     607            goto end;
     608        }
     609        SSLfatal(s, SSL_AD_BAD_RECORD_MAC,
     610                 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
     611        goto end;
     612    }
     613    OSSL_TRACE_BEGIN(TLS) {
     614        BIO_printf(trc_out, "dec %lu\n", (unsigned long)rr[0].length);
     615        BIO_dump_indent(trc_out, rr[0].data, rr[0].length, 4);
     616    } OSSL_TRACE_END(TLS);
    575617
    576618    /* r->length is now the compressed data plus mac */
    577     if ((sess != NULL) &&
    578         (s->enc_read_ctx != NULL) &&
    579         (!SSL_READ_ETM(s) && EVP_MD_CTX_md(s->read_hash) != NULL)) {
     619    if ((sess != NULL)
     620            && (s->enc_read_ctx != NULL)
     621            && (!SSL_READ_ETM(s) && EVP_MD_CTX_get0_md(s->read_hash) != NULL)) {
    580622        /* s->read_hash != NULL => mac_size != -1 */
    581         unsigned char *mac = NULL;
    582         unsigned char mac_tmp[EVP_MAX_MD_SIZE];
    583 
    584         mac_size = EVP_MD_CTX_size(s->read_hash);
    585         if (!ossl_assert(mac_size <= EVP_MAX_MD_SIZE)) {
    586             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_GET_RECORD,
    587                      ERR_R_INTERNAL_ERROR);
    588             return -1;
    589         }
    590623
    591624        for (j = 0; j < num_recs; j++) {
     625            SSL_MAC_BUF *thismb = &macbufs[j];
    592626            thisrr = &rr[j];
    593             /*
    594              * orig_len is the length of the record before any padding was
    595              * removed. This is public information, as is the MAC in use,
    596              * therefore we can safely process the record in a different amount
    597              * of time if it's too short to possibly contain a MAC.
    598              */
    599             if (thisrr->orig_len < mac_size ||
    600                 /* CBC records must have a padding length byte too. */
    601                 (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
    602                  thisrr->orig_len < mac_size + 1)) {
    603                 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL3_GET_RECORD,
    604                          SSL_R_LENGTH_TOO_SHORT);
    605                 return -1;
    606             }
    607 
    608             if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {
    609                 /*
    610                  * We update the length so that the TLS header bytes can be
    611                  * constructed correctly but we need to extract the MAC in
    612                  * constant time from within the record, without leaking the
    613                  * contents of the padding bytes.
    614                  */
    615                 mac = mac_tmp;
    616                 if (!ssl3_cbc_copy_mac(mac_tmp, thisrr, mac_size)) {
    617                     SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_GET_RECORD,
    618                              ERR_R_INTERNAL_ERROR);
    619                     return -1;
    620                 }
    621                 thisrr->length -= mac_size;
    622             } else {
    623                 /*
    624                  * In this case there's no padding, so |rec->orig_len| equals
    625                  * |rec->length| and we checked that there's enough bytes for
    626                  * |mac_size| above.
    627                  */
    628                 thisrr->length -= mac_size;
    629                 mac = &thisrr->data[thisrr->length];
    630             }
    631627
    632628            i = s->method->ssl3_enc->mac(s, thisrr, md, 0 /* not send */ );
    633             if (i == 0 || mac == NULL
    634                 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0)
    635                 enc_err = -1;
     629            if (i == 0 || thismb == NULL || thismb->mac == NULL
     630                || CRYPTO_memcmp(md, thismb->mac, (size_t)mac_size) != 0)
     631                enc_err = 0;
    636632            if (thisrr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
    637                 enc_err = -1;
    638         }
    639     }
    640 
    641     if (enc_err < 0) {
     633                enc_err = 0;
     634        }
     635    }
     636
     637    if (enc_err == 0) {
    642638        if (ossl_statem_in_error(s)) {
    643639            /* We already called SSLfatal() */
    644             return -1;
    645         }
    646         if (num_recs == 1 && ossl_statem_skip_early_data(s)) {
    647             /*
    648              * We assume this is unreadable early_data - we treat it like an
    649              * empty record
    650              */
    651 
    652             /*
    653              * The record length may have been modified by the mac check above
    654              * so we use the previously saved value
    655              */
    656             if (!early_data_count_ok(s, first_rec_len,
    657                                      EARLY_DATA_CIPHERTEXT_OVERHEAD, 0)) {
    658                 /* SSLfatal() already called */
    659                 return -1;
    660             }
    661 
    662             thisrr = &rr[0];
    663             thisrr->length = 0;
    664             thisrr->read = 1;
    665             RECORD_LAYER_set_numrpipes(&s->rlayer, 1);
    666             RECORD_LAYER_reset_read_sequence(&s->rlayer);
    667             return 1;
     640            goto end;
    668641        }
    669642        /*
     
    674647         * visible to an attacker (e.g. via a logfile)
    675648         */
    676         SSLfatal(s, SSL_AD_BAD_RECORD_MAC, SSL_F_SSL3_GET_RECORD,
     649        SSLfatal(s, SSL_AD_BAD_RECORD_MAC,
    677650                 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
    678         return -1;
    679     }
     651        goto end;
     652    }
     653
     654 skip_decryption:
    680655
    681656    for (j = 0; j < num_recs; j++) {
     
    685660        if (s->expand != NULL) {
    686661            if (thisrr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
    687                 SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
     662                SSLfatal(s, SSL_AD_RECORD_OVERFLOW,
    688663                         SSL_R_COMPRESSED_LENGTH_TOO_LONG);
    689                 return -1;
     664                goto end;
    690665            }
    691666            if (!ssl3_do_uncompress(s, thisrr)) {
    692                 SSLfatal(s, SSL_AD_DECOMPRESSION_FAILURE, SSL_F_SSL3_GET_RECORD,
     667                SSLfatal(s, SSL_AD_DECOMPRESSION_FAILURE,
    693668                         SSL_R_BAD_DECOMPRESSION);
    694                 return -1;
     669                goto end;
    695670            }
    696671        }
     
    703678            if (thisrr->length == 0
    704679                    || thisrr->type != SSL3_RT_APPLICATION_DATA) {
    705                 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_GET_RECORD,
    706                          SSL_R_BAD_RECORD_TYPE);
    707                 return -1;
     680                SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_BAD_RECORD_TYPE);
     681                goto end;
    708682            }
    709683
     
    718692                    && thisrr->type != SSL3_RT_ALERT
    719693                    && thisrr->type != SSL3_RT_HANDSHAKE) {
    720                 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_GET_RECORD,
    721                          SSL_R_BAD_RECORD_TYPE);
    722                 return -1;
     694                SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_BAD_RECORD_TYPE);
     695                goto end;
    723696            }
    724697            if (s->msg_callback)
     
    735708                    || thisrr->type == SSL3_RT_ALERT)
    736709                && thisrr->length == 0) {
    737             SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_GET_RECORD,
    738                      SSL_R_BAD_LENGTH);
    739             return -1;
    740         }
    741 
    742         if (thisrr->length > SSL3_RT_MAX_PLAIN_LENGTH) {
    743             SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
    744                      SSL_R_DATA_LENGTH_TOO_LONG);
    745             return -1;
     710            SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_BAD_LENGTH);
     711            goto end;
     712        }
     713
     714        if (thisrr->length > SSL3_RT_MAX_PLAIN_LENGTH
     715            && !BIO_get_ktls_recv(s->rbio)) {
     716            SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_R_DATA_LENGTH_TOO_LONG);
     717            goto end;
    746718        }
    747719
    748720        /* If received packet overflows current Max Fragment Length setting */
    749721        if (s->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(s->session)
    750                 && thisrr->length > GET_MAX_FRAGMENT_LENGTH(s->session)) {
    751             SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
    752                     SSL_R_DATA_LENGTH_TOO_LONG);
    753             return -1;
     722                && thisrr->length > GET_MAX_FRAGMENT_LENGTH(s->session)
     723                && !BIO_get_ktls_recv(s->rbio)) {
     724            SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_R_DATA_LENGTH_TOO_LONG);
     725            goto end;
    754726        }
    755727
     
    768740            if (RECORD_LAYER_get_empty_record_count(&s->rlayer)
    769741                > MAX_EMPTY_RECORDS) {
    770                 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_GET_RECORD,
    771                          SSL_R_RECORD_TOO_SMALL);
    772                 return -1;
     742                SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_RECORD_TOO_SMALL);
     743                goto end;
    773744            }
    774745        } else {
     
    782753                && !early_data_count_ok(s, thisrr->length, 0, 0)) {
    783754            /* SSLfatal already called */
    784             return -1;
     755            goto end;
    785756        }
    786757    }
    787758
    788759    RECORD_LAYER_set_numrpipes(&s->rlayer, num_recs);
    789     return 1;
     760    ret = 1;
     761 end:
     762    if (macbufs != NULL) {
     763        for (j = 0; j < num_recs; j++) {
     764            if (macbufs[j].alloced)
     765                OPENSSL_free(macbufs[j].mac);
     766        }
     767        OPENSSL_free(macbufs);
     768    }
     769    return ret;
    790770}
    791771
     
    802782        return 0;
    803783
    804     /* TODO(size_t): Convert this call */
    805784    i = COMP_expand_block(ssl->expand, rr->comp,
    806785                          SSL3_RT_MAX_PLAIN_LENGTH, rr->data, (int)rr->length);
     
    819798    int i;
    820799
    821     /* TODO(size_t): Convert this call */
    822800    i = COMP_compress_block(ssl->compress, wr->data,
    823801                            (int)(wr->length + SSL3_RT_MAX_COMPRESSED_OVERHEAD),
     
    834812
    835813/*-
    836  * ssl3_enc encrypts/decrypts |n_recs| records in |inrecs|.  Will call
    837  * SSLfatal() for internal errors, but not otherwise.
     814 * ssl3_enc encrypts/decrypts |n_recs| records in |inrecs|. Calls SSLfatal on
     815 * internal error, but not otherwise. It is the responsibility of the caller to
     816 * report a bad_record_mac
    838817 *
    839818 * Returns:
    840  *   0: (in non-constant time) if the record is publicly invalid (i.e. too
    841  *       short etc).
    842  *   1: if the record's padding is valid / the encryption was successful.
    843  *   -1: if the record's padding is invalid or, if sending, an internal error
    844  *       occurred.
     819 *    0: if the record is publicly invalid, or an internal error
     820 *    1: Success or Mac-then-encrypt decryption failed (MAC will be randomised)
    845821 */
    846 int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, size_t n_recs, int sending)
     822int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, size_t n_recs, int sending,
     823             SSL_MAC_BUF *mac, size_t macsize)
    847824{
    848825    SSL3_RECORD *rec;
    849826    EVP_CIPHER_CTX *ds;
    850827    size_t l, i;
    851     size_t bs, mac_size = 0;
    852     int imac_size;
     828    size_t bs;
    853829    const EVP_CIPHER *enc;
    854830
     
    864840            enc = NULL;
    865841        else
    866             enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
     842            enc = EVP_CIPHER_CTX_get0_cipher(s->enc_write_ctx);
    867843    } else {
    868844        ds = s->enc_read_ctx;
     
    870846            enc = NULL;
    871847        else
    872             enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
     848            enc = EVP_CIPHER_CTX_get0_cipher(s->enc_read_ctx);
    873849    }
    874850
     
    877853        rec->input = rec->data;
    878854    } else {
     855        int provided = (EVP_CIPHER_get0_provider(enc) != NULL);
     856
    879857        l = rec->length;
    880         /* TODO(size_t): Convert this call */
    881         bs = EVP_CIPHER_CTX_block_size(ds);
     858        bs = EVP_CIPHER_CTX_get_block_size(ds);
    882859
    883860        /* COMPRESS */
    884861
    885         if ((bs != 1) && sending) {
     862        if ((bs != 1) && sending && !provided) {
     863            /*
     864             * We only do this for legacy ciphers. Provided ciphers add the
     865             * padding on the provider side.
     866             */
    886867            i = bs - (l % bs);
    887868
     
    898879
    899880        if (!sending) {
    900             if (l == 0 || l % bs != 0)
     881            if (l == 0 || l % bs != 0) {
     882                /* Publicly invalid */
    901883                return 0;
     884            }
    902885            /* otherwise, rec->length >= bs */
    903886        }
    904887
    905         /* TODO(size_t): Convert this call */
    906         if (EVP_Cipher(ds, rec->data, rec->input, (unsigned int)l) < 1)
    907             return -1;
    908 
    909         if (EVP_MD_CTX_md(s->read_hash) != NULL) {
    910             /* TODO(size_t): convert me */
    911             imac_size = EVP_MD_CTX_size(s->read_hash);
    912             if (imac_size < 0) {
    913                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_ENC,
    914                          ERR_R_INTERNAL_ERROR);
    915                 return -1;
    916             }
    917             mac_size = (size_t)imac_size;
    918         }
    919         if ((bs != 1) && !sending)
    920             return ssl3_cbc_remove_padding(rec, bs, mac_size);
     888        if (EVP_CIPHER_get0_provider(enc) != NULL) {
     889            int outlen;
     890
     891            if (!EVP_CipherUpdate(ds, rec->data, &outlen, rec->input,
     892                                  (unsigned int)l))
     893                return 0;
     894            rec->length = outlen;
     895
     896            if (!sending && mac != NULL) {
     897                /* Now get a pointer to the MAC */
     898                OSSL_PARAM params[2], *p = params;
     899
     900                /* Get the MAC */
     901                mac->alloced = 0;
     902
     903                *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_TLS_MAC,
     904                                                      (void **)&mac->mac,
     905                                                      macsize);
     906                *p = OSSL_PARAM_construct_end();
     907
     908                if (!EVP_CIPHER_CTX_get_params(ds, params)) {
     909                    /* Shouldn't normally happen */
     910                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
     911                    return 0;
     912                }
     913            }
     914        } else {
     915            if (EVP_Cipher(ds, rec->data, rec->input, (unsigned int)l) < 1) {
     916                /* Shouldn't happen */
     917                SSLfatal(s, SSL_AD_BAD_RECORD_MAC, ERR_R_INTERNAL_ERROR);
     918                return 0;
     919            }
     920
     921            if (!sending)
     922                return ssl3_cbc_remove_padding_and_mac(&rec->length,
     923                                           rec->orig_len,
     924                                           rec->data,
     925                                           (mac != NULL) ? &mac->mac : NULL,
     926                                           (mac != NULL) ? &mac->alloced : NULL,
     927                                           bs,
     928                                           macsize,
     929                                           s->ctx->libctx);
     930        }
    921931    }
    922932    return 1;
     
    925935#define MAX_PADDING 256
    926936/*-
    927  * tls1_enc encrypts/decrypts |n_recs| in |recs|.  Will call SSLfatal() for
    928  * internal errors, but not otherwise.
     937 * tls1_enc encrypts/decrypts |n_recs| in |recs|. Calls SSLfatal on internal
     938 * error, but not otherwise. It is the responsibility of the caller to report
     939 * a bad_record_mac - if appropriate (DTLS just drops the record).
    929940 *
    930941 * Returns:
    931  *   0: (in non-constant time) if the record is publicly invalid (i.e. too
    932  *       short etc).
    933  *   1: if the record's padding is valid / the encryption was successful.
    934  *   -1: if the record's padding/AEAD-authenticator is invalid or, if sending,
    935  *       an internal error occurred.
     942 *    0: if the record is publicly invalid, or an internal error, or AEAD
     943 *       decryption failed, or Encrypt-then-mac decryption failed.
     944 *    1: Success or Mac-then-encrypt decryption failed (MAC will be randomised)
    936945 */
    937 int tls1_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int sending)
     946int tls1_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int sending,
     947             SSL_MAC_BUF *macs, size_t macsize)
    938948{
    939949    EVP_CIPHER_CTX *ds;
    940950    size_t reclen[SSL_MAX_PIPELINES];
    941951    unsigned char buf[SSL_MAX_PIPELINES][EVP_AEAD_TLS1_AAD_LEN];
    942     int i, pad = 0, ret, tmpr;
    943     size_t bs, mac_size = 0, ctr, padnum, loop;
     952    int i, pad = 0, tmpr;
     953    size_t bs, ctr, padnum, loop;
    944954    unsigned char padval;
    945     int imac_size;
    946955    const EVP_CIPHER *enc;
     956    int tlstree_enc = sending ? (s->mac_flags & SSL_MAC_FLAG_WRITE_MAC_TLSTREE)
     957                              : (s->mac_flags & SSL_MAC_FLAG_READ_MAC_TLSTREE);
    947958
    948959    if (n_recs == 0) {
    949         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
    950                  ERR_R_INTERNAL_ERROR);
     960        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    951961        return 0;
    952962    }
    953963
    954964    if (sending) {
    955         if (EVP_MD_CTX_md(s->write_hash)) {
    956             int n = EVP_MD_CTX_size(s->write_hash);
     965        if (EVP_MD_CTX_get0_md(s->write_hash)) {
     966            int n = EVP_MD_CTX_get_size(s->write_hash);
    957967            if (!ossl_assert(n >= 0)) {
    958                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
    959                          ERR_R_INTERNAL_ERROR);
    960                 return -1;
     968                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
     969                return 0;
    961970            }
    962971        }
     
    966975        else {
    967976            int ivlen;
    968             enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
     977
     978            enc = EVP_CIPHER_CTX_get0_cipher(s->enc_write_ctx);
    969979            /* For TLSv1.1 and later explicit IV */
    970980            if (SSL_USE_EXPLICIT_IV(s)
    971                 && EVP_CIPHER_mode(enc) == EVP_CIPH_CBC_MODE)
    972                 ivlen = EVP_CIPHER_iv_length(enc);
     981                && EVP_CIPHER_get_mode(enc) == EVP_CIPH_CBC_MODE)
     982                ivlen = EVP_CIPHER_get_iv_length(enc);
    973983            else
    974984                ivlen = 0;
     
    980990                         * happen?? (steve)
    981991                         */
    982                         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
    983                                  ERR_R_INTERNAL_ERROR);
    984                         return -1;
    985                     } else if (RAND_bytes(recs[ctr].input, ivlen) <= 0) {
    986                         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
    987                                  ERR_R_INTERNAL_ERROR);
    988                         return -1;
     992                        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
     993                        return 0;
     994                    } else if (RAND_bytes_ex(s->ctx->libctx, recs[ctr].input,
     995                                             ivlen, 0) <= 0) {
     996                        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
     997                        return 0;
    989998                    }
    990999                }
     
    9921001        }
    9931002    } else {
    994         if (EVP_MD_CTX_md(s->read_hash)) {
    995             int n = EVP_MD_CTX_size(s->read_hash);
     1003        if (EVP_MD_CTX_get0_md(s->read_hash)) {
     1004            int n = EVP_MD_CTX_get_size(s->read_hash);
    9961005            if (!ossl_assert(n >= 0)) {
    997                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
    998                          ERR_R_INTERNAL_ERROR);
    999                 return -1;
     1006                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
     1007                return 0;
    10001008            }
    10011009        }
     
    10041012            enc = NULL;
    10051013        else
    1006             enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
     1014            enc = EVP_CIPHER_CTX_get0_cipher(s->enc_read_ctx);
    10071015    }
    10081016
     
    10121020            recs[ctr].input = recs[ctr].data;
    10131021        }
    1014         ret = 1;
    10151022    } else {
    1016         bs = EVP_CIPHER_block_size(EVP_CIPHER_CTX_cipher(ds));
     1023        int provided = (EVP_CIPHER_get0_provider(enc) != NULL);
     1024
     1025        bs = EVP_CIPHER_get_block_size(EVP_CIPHER_CTX_get0_cipher(ds));
    10171026
    10181027        if (n_recs > 1) {
    1019             if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
    1020                   & EVP_CIPH_FLAG_PIPELINE)) {
     1028            if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ds))
     1029                  & EVP_CIPH_FLAG_PIPELINE) == 0) {
    10211030                /*
    10221031                 * We shouldn't have been called with pipeline data if the
    10231032                 * cipher doesn't support pipelining
    10241033                 */
    1025                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
    1026                          SSL_R_PIPELINE_FAILURE);
    1027                 return -1;
     1034                SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_PIPELINE_FAILURE);
     1035                return 0;
    10281036            }
    10291037        }
     
    10311039            reclen[ctr] = recs[ctr].length;
    10321040
    1033             if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
    1034                 & EVP_CIPH_FLAG_AEAD_CIPHER) {
     1041            if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ds))
     1042                        & EVP_CIPH_FLAG_AEAD_CIPHER) != 0) {
    10351043                unsigned char *seq;
    10361044
     
    10401048                if (SSL_IS_DTLS(s)) {
    10411049                    /* DTLS does not support pipelining */
    1042                     unsigned char dtlsseq[9], *p = dtlsseq;
     1050                    unsigned char dtlsseq[8], *p = dtlsseq;
    10431051
    10441052                    s2n(sending ? DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer) :
     
    10631071                                          EVP_AEAD_TLS1_AAD_LEN, buf[ctr]);
    10641072                if (pad <= 0) {
    1065                     SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
    1066                              ERR_R_INTERNAL_ERROR);
    1067                     return -1;
     1073                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
     1074                    return 0;
    10681075                }
    10691076
     
    10731080                }
    10741081
    1075             } else if ((bs != 1) && sending) {
     1082            } else if ((bs != 1) && sending && !provided) {
     1083                /*
     1084                 * We only do this for legacy ciphers. Provided ciphers add the
     1085                 * padding on the provider side.
     1086                 */
    10761087                padnum = bs - (reclen[ctr] % bs);
    10771088
     
    10791090
    10801091                if (padnum > MAX_PADDING) {
    1081                     SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
    1082                              ERR_R_INTERNAL_ERROR);
    1083                     return -1;
     1092                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
     1093                    return 0;
    10841094                }
    10851095                /* we need to add 'padnum' padding bytes of value padval */
     
    10921102
    10931103            if (!sending) {
    1094                 if (reclen[ctr] == 0 || reclen[ctr] % bs != 0)
     1104                if (reclen[ctr] == 0 || reclen[ctr] % bs != 0) {
     1105                    /* Publicly invalid */
    10951106                    return 0;
     1107                }
    10961108            }
    10971109        }
     
    11051117            if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS,
    11061118                                    (int)n_recs, data) <= 0) {
    1107                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
    1108                          SSL_R_PIPELINE_FAILURE);
    1109                 return -1;
     1119                SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_PIPELINE_FAILURE);
     1120                return 0;
    11101121            }
    11111122            /* Set the input buffers */
     
    11171128                || EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_LENS,
    11181129                                       (int)n_recs, reclen) <= 0) {
    1119                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
    1120                          SSL_R_PIPELINE_FAILURE);
    1121                 return -1;
    1122             }
    1123         }
    1124 
    1125         /* TODO(size_t): Convert this call */
    1126         tmpr = EVP_Cipher(ds, recs[0].data, recs[0].input,
    1127                           (unsigned int)reclen[0]);
    1128         if ((EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
    1129              & EVP_CIPH_FLAG_CUSTOM_CIPHER)
    1130             ? (tmpr < 0)
    1131             : (tmpr == 0))
    1132             return -1;          /* AEAD can fail to verify MAC */
    1133 
    1134         if (sending == 0) {
    1135             if (EVP_CIPHER_mode(enc) == EVP_CIPH_GCM_MODE) {
     1130                SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_PIPELINE_FAILURE);
     1131                return 0;
     1132            }
     1133        }
     1134
     1135        if (!SSL_IS_DTLS(s) && tlstree_enc) {
     1136            unsigned char *seq;
     1137            int decrement_seq = 0;
     1138
     1139            /*
     1140             * When sending, seq is incremented after MAC calculation.
     1141             * So if we are in ETM mode, we use seq 'as is' in the ctrl-function.
     1142             * Otherwise we have to decrease it in the implementation
     1143             */
     1144            if (sending && !SSL_WRITE_ETM(s))
     1145                decrement_seq = 1;
     1146
     1147            seq = sending ? RECORD_LAYER_get_write_sequence(&s->rlayer)
     1148                          : RECORD_LAYER_get_read_sequence(&s->rlayer);
     1149            if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_TLSTREE, decrement_seq, seq) <= 0) {
     1150                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
     1151                return 0;
     1152            }
     1153        }
     1154
     1155        if (provided) {
     1156            int outlen;
     1157
     1158            /* Provided cipher - we do not support pipelining on this path */
     1159            if (n_recs > 1)  {
     1160                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
     1161                return 0;
     1162            }
     1163
     1164            if (!EVP_CipherUpdate(ds, recs[0].data, &outlen, recs[0].input,
     1165                                  (unsigned int)reclen[0]))
     1166                return 0;
     1167            recs[0].length = outlen;
     1168
     1169            /*
     1170             * The length returned from EVP_CipherUpdate above is the actual
     1171             * payload length. We need to adjust the data/input ptr to skip over
     1172             * any explicit IV
     1173             */
     1174            if (!sending) {
     1175                if (EVP_CIPHER_get_mode(enc) == EVP_CIPH_GCM_MODE) {
     1176                        recs[0].data += EVP_GCM_TLS_EXPLICIT_IV_LEN;
     1177                        recs[0].input += EVP_GCM_TLS_EXPLICIT_IV_LEN;
     1178                } else if (EVP_CIPHER_get_mode(enc) == EVP_CIPH_CCM_MODE) {
     1179                        recs[0].data += EVP_CCM_TLS_EXPLICIT_IV_LEN;
     1180                        recs[0].input += EVP_CCM_TLS_EXPLICIT_IV_LEN;
     1181                } else if (bs != 1 && SSL_USE_EXPLICIT_IV(s)) {
     1182                    recs[0].data += bs;
     1183                    recs[0].input += bs;
     1184                    recs[0].orig_len -= bs;
     1185                }
     1186
     1187                /* Now get a pointer to the MAC (if applicable) */
     1188                if (macs != NULL) {
     1189                    OSSL_PARAM params[2], *p = params;
     1190
     1191                    /* Get the MAC */
     1192                    macs[0].alloced = 0;
     1193
     1194                    *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_TLS_MAC,
     1195                                                          (void **)&macs[0].mac,
     1196                                                          macsize);
     1197                    *p = OSSL_PARAM_construct_end();
     1198
     1199                    if (!EVP_CIPHER_CTX_get_params(ds, params)) {
     1200                        /* Shouldn't normally happen */
     1201                        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
     1202                                 ERR_R_INTERNAL_ERROR);
     1203                        return 0;
     1204                    }
     1205                }
     1206            }
     1207        } else {
     1208            /* Legacy cipher */
     1209
     1210            tmpr = EVP_Cipher(ds, recs[0].data, recs[0].input,
     1211                              (unsigned int)reclen[0]);
     1212            if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ds))
     1213                 & EVP_CIPH_FLAG_CUSTOM_CIPHER) != 0
     1214                ? (tmpr < 0)
     1215                : (tmpr == 0)) {
     1216                /* AEAD can fail to verify MAC */
     1217                return 0;
     1218            }
     1219
     1220            if (!sending) {
    11361221                for (ctr = 0; ctr < n_recs; ctr++) {
    1137                     recs[ctr].data += EVP_GCM_TLS_EXPLICIT_IV_LEN;
    1138                     recs[ctr].input += EVP_GCM_TLS_EXPLICIT_IV_LEN;
    1139                     recs[ctr].length -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
     1222                    /* Adjust the record to remove the explicit IV/MAC/Tag */
     1223                    if (EVP_CIPHER_get_mode(enc) == EVP_CIPH_GCM_MODE) {
     1224                        recs[ctr].data += EVP_GCM_TLS_EXPLICIT_IV_LEN;
     1225                        recs[ctr].input += EVP_GCM_TLS_EXPLICIT_IV_LEN;
     1226                        recs[ctr].length -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
     1227                    } else if (EVP_CIPHER_get_mode(enc) == EVP_CIPH_CCM_MODE) {
     1228                        recs[ctr].data += EVP_CCM_TLS_EXPLICIT_IV_LEN;
     1229                        recs[ctr].input += EVP_CCM_TLS_EXPLICIT_IV_LEN;
     1230                        recs[ctr].length -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
     1231                    } else if (bs != 1 && SSL_USE_EXPLICIT_IV(s)) {
     1232                        if (recs[ctr].length < bs)
     1233                            return 0;
     1234                        recs[ctr].data += bs;
     1235                        recs[ctr].input += bs;
     1236                        recs[ctr].length -= bs;
     1237                        recs[ctr].orig_len -= bs;
     1238                    }
     1239
     1240                    /*
     1241                     * If using Mac-then-encrypt, then this will succeed but
     1242                     * with a random MAC if padding is invalid
     1243                     */
     1244                    if (!tls1_cbc_remove_padding_and_mac(&recs[ctr].length,
     1245                                         recs[ctr].orig_len,
     1246                                         recs[ctr].data,
     1247                                         (macs != NULL) ? &macs[ctr].mac : NULL,
     1248                                         (macs != NULL) ? &macs[ctr].alloced
     1249                                                        : NULL,
     1250                                         bs,
     1251                                         pad ? (size_t)pad : macsize,
     1252                                         (EVP_CIPHER_get_flags(enc)
     1253                                         & EVP_CIPH_FLAG_AEAD_CIPHER) != 0,
     1254                                         s->ctx->libctx))
     1255                        return 0;
    11401256                }
    1141             } else if (EVP_CIPHER_mode(enc) == EVP_CIPH_CCM_MODE) {
    1142                 for (ctr = 0; ctr < n_recs; ctr++) {
    1143                     recs[ctr].data += EVP_CCM_TLS_EXPLICIT_IV_LEN;
    1144                     recs[ctr].input += EVP_CCM_TLS_EXPLICIT_IV_LEN;
    1145                     recs[ctr].length -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
    1146                 }
    1147             }
    1148         }
    1149 
    1150         ret = 1;
    1151         if (!SSL_READ_ETM(s) && EVP_MD_CTX_md(s->read_hash) != NULL) {
    1152             imac_size = EVP_MD_CTX_size(s->read_hash);
    1153             if (imac_size < 0) {
    1154                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
    1155                          ERR_R_INTERNAL_ERROR);
    1156                 return -1;
    1157             }
    1158             mac_size = (size_t)imac_size;
    1159         }
    1160         if ((bs != 1) && !sending) {
    1161             int tmpret;
    1162             for (ctr = 0; ctr < n_recs; ctr++) {
    1163                 tmpret = tls1_cbc_remove_padding(s, &recs[ctr], bs, mac_size);
    1164                 /*
    1165                  * If tmpret == 0 then this means publicly invalid so we can
    1166                  * short circuit things here. Otherwise we must respect constant
    1167                  * time behaviour.
    1168                  */
    1169                 if (tmpret == 0)
    1170                     return 0;
    1171                 ret = constant_time_select_int(constant_time_eq_int(tmpret, 1),
    1172                                                ret, -1);
    1173             }
    1174         }
    1175         if (pad && !sending) {
    1176             for (ctr = 0; ctr < n_recs; ctr++) {
    1177                 recs[ctr].length -= pad;
    1178             }
    1179         }
    1180     }
    1181     return ret;
     1257            }
     1258        }
     1259    }
     1260    return 1;
     1261}
     1262
     1263/*
     1264 * ssl3_cbc_record_digest_supported returns 1 iff |ctx| uses a hash function
     1265 * which ssl3_cbc_digest_record supports.
     1266 */
     1267char ssl3_cbc_record_digest_supported(const EVP_MD_CTX *ctx)
     1268{
     1269    switch (EVP_MD_CTX_get_type(ctx)) {
     1270    case NID_md5:
     1271    case NID_sha1:
     1272    case NID_sha224:
     1273    case NID_sha256:
     1274    case NID_sha384:
     1275    case NID_sha512:
     1276        return 1;
     1277    default:
     1278        return 0;
     1279    }
    11821280}
    11831281
     
    11921290
    11931291    if (sending) {
    1194         mac_sec = &(ssl->s3->write_mac_secret[0]);
     1292        mac_sec = &(ssl->s3.write_mac_secret[0]);
    11951293        seq = RECORD_LAYER_get_write_sequence(&ssl->rlayer);
    11961294        hash = ssl->write_hash;
    11971295    } else {
    1198         mac_sec = &(ssl->s3->read_mac_secret[0]);
     1296        mac_sec = &(ssl->s3.read_mac_secret[0]);
    11991297        seq = RECORD_LAYER_get_read_sequence(&ssl->rlayer);
    12001298        hash = ssl->read_hash;
    12011299    }
    12021300
    1203     t = EVP_MD_CTX_size(hash);
     1301    t = EVP_MD_CTX_get_size(hash);
    12041302    if (t < 0)
    12051303        return 0;
     
    12071305    npad = (48 / md_size) * md_size;
    12081306
    1209     if (!sending &&
    1210         EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
    1211         ssl3_cbc_record_digest_supported(hash)) {
     1307    if (!sending
     1308        && EVP_CIPHER_CTX_get_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE
     1309        && ssl3_cbc_record_digest_supported(hash)) {
     1310#ifdef OPENSSL_NO_DEPRECATED_3_0
     1311        return 0;
     1312#else
    12121313        /*
    12131314         * This is a CBC-encrypted record. We must avoid leaking any
     
    12371338
    12381339        /* Final param == is SSLv3 */
    1239         if (ssl3_cbc_digest_record(hash,
     1340        if (ssl3_cbc_digest_record(EVP_MD_CTX_get0_md(hash),
    12401341                                   md, &md_size,
    12411342                                   header, rec->input,
    1242                                    rec->length + md_size, rec->orig_len,
     1343                                   rec->length, rec->orig_len,
    12431344                                   mac_sec, md_size, 1) <= 0)
    12441345            return 0;
     1346#endif
    12451347    } else {
    12461348        unsigned int md_size_u;
     
    12861388    EVP_MD_CTX *hmac = NULL, *mac_ctx;
    12871389    unsigned char header[13];
    1288     int stream_mac = (sending ? (ssl->mac_flags & SSL_MAC_FLAG_WRITE_MAC_STREAM)
    1289                       : (ssl->mac_flags & SSL_MAC_FLAG_READ_MAC_STREAM));
     1390    int stream_mac = sending ? (ssl->mac_flags & SSL_MAC_FLAG_WRITE_MAC_STREAM)
     1391                             : (ssl->mac_flags & SSL_MAC_FLAG_READ_MAC_STREAM);
     1392    int tlstree_mac = sending ? (ssl->mac_flags & SSL_MAC_FLAG_WRITE_MAC_TLSTREE)
     1393                              : (ssl->mac_flags & SSL_MAC_FLAG_READ_MAC_TLSTREE);
    12901394    int t;
    12911395
     
    12981402    }
    12991403
    1300     t = EVP_MD_CTX_size(hash);
     1404    t = EVP_MD_CTX_get_size(hash);
    13011405    if (!ossl_assert(t >= 0))
    13021406        return 0;
     
    13151419    }
    13161420
     1421    if (!SSL_IS_DTLS(ssl) && tlstree_mac && EVP_MD_CTX_ctrl(mac_ctx, EVP_MD_CTRL_TLSTREE, 0, seq) <= 0) {
     1422        EVP_MD_CTX_free(hmac);
     1423        return 0;
     1424    }
     1425
    13171426    if (SSL_IS_DTLS(ssl)) {
    13181427        unsigned char dtlsseq[8], *p = dtlsseq;
     
    13321441    header[12] = (unsigned char)(rec->length & 0xff);
    13331442
    1334     if (!sending && !SSL_READ_ETM(ssl) &&
    1335         EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
    1336         ssl3_cbc_record_digest_supported(mac_ctx)) {
    1337         /*
    1338          * This is a CBC-encrypted record. We must avoid leaking any
    1339          * timing-side channel information about how many blocks of data we
    1340          * are hashing because that gives an attacker a timing-oracle.
    1341          */
    1342         /* Final param == not SSLv3 */
    1343         if (ssl3_cbc_digest_record(mac_ctx,
    1344                                    md, &md_size,
    1345                                    header, rec->input,
    1346                                    rec->length + md_size, rec->orig_len,
    1347                                    ssl->s3->read_mac_secret,
    1348                                    ssl->s3->read_mac_secret_size, 0) <= 0) {
    1349             EVP_MD_CTX_free(hmac);
     1443    if (!sending && !SSL_READ_ETM(ssl)
     1444        && EVP_CIPHER_CTX_get_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE
     1445        && ssl3_cbc_record_digest_supported(mac_ctx)) {
     1446        OSSL_PARAM tls_hmac_params[2], *p = tls_hmac_params;
     1447
     1448        *p++ = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_TLS_DATA_SIZE,
     1449                                           &rec->orig_len);
     1450        *p++ = OSSL_PARAM_construct_end();
     1451
     1452        if (!EVP_PKEY_CTX_set_params(EVP_MD_CTX_get_pkey_ctx(mac_ctx),
     1453                                     tls_hmac_params))
    13501454            return 0;
    1351         }
    1352     } else {
    1353         /* TODO(size_t): Convert these calls */
    1354         if (EVP_DigestSignUpdate(mac_ctx, header, sizeof(header)) <= 0
    1355             || EVP_DigestSignUpdate(mac_ctx, rec->input, rec->length) <= 0
    1356             || EVP_DigestSignFinal(mac_ctx, md, &md_size) <= 0) {
    1357             EVP_MD_CTX_free(hmac);
    1358             return 0;
    1359         }
     1455    }
     1456
     1457    if (EVP_DigestSignUpdate(mac_ctx, header, sizeof(header)) <= 0
     1458        || EVP_DigestSignUpdate(mac_ctx, rec->input, rec->length) <= 0
     1459        || EVP_DigestSignFinal(mac_ctx, md, &md_size) <= 0) {
     1460        EVP_MD_CTX_free(hmac);
     1461        return 0;
    13601462    }
    13611463
    13621464    EVP_MD_CTX_free(hmac);
    13631465
    1364 #ifdef SSL_DEBUG
    1365     fprintf(stderr, "seq=");
    1366     {
    1367         int z;
    1368         for (z = 0; z < 8; z++)
    1369             fprintf(stderr, "%02X ", seq[z]);
    1370         fprintf(stderr, "\n");
    1371     }
    1372     fprintf(stderr, "rec=");
    1373     {
    1374         size_t z;
    1375         for (z = 0; z < rec->length; z++)
    1376             fprintf(stderr, "%02X ", rec->data[z]);
    1377         fprintf(stderr, "\n");
    1378     }
    1379 #endif
     1466    OSSL_TRACE_BEGIN(TLS) {
     1467        BIO_printf(trc_out, "seq:\n");
     1468        BIO_dump_indent(trc_out, seq, 8, 4);
     1469        BIO_printf(trc_out, "rec:\n");
     1470        BIO_dump_indent(trc_out, rec->data, rec->length, 4);
     1471    } OSSL_TRACE_END(TLS);
    13801472
    13811473    if (!SSL_IS_DTLS(ssl)) {
     
    13861478        }
    13871479    }
    1388 #ifdef SSL_DEBUG
    1389     {
    1390         unsigned int z;
    1391         for (z = 0; z < md_size; z++)
    1392             fprintf(stderr, "%02X ", md[z]);
    1393         fprintf(stderr, "\n");
    1394     }
    1395 #endif
    1396     return 1;
    1397 }
    1398 
    1399 /*-
    1400  * ssl3_cbc_remove_padding removes padding from the decrypted, SSLv3, CBC
    1401  * record in |rec| by updating |rec->length| in constant time.
    1402  *
    1403  * block_size: the block size of the cipher used to encrypt the record.
    1404  * returns:
    1405  *   0: (in non-constant time) if the record is publicly invalid.
    1406  *   1: if the padding was valid
    1407  *  -1: otherwise.
    1408  */
    1409 int ssl3_cbc_remove_padding(SSL3_RECORD *rec,
    1410                             size_t block_size, size_t mac_size)
    1411 {
    1412     size_t padding_length;
    1413     size_t good;
    1414     const size_t overhead = 1 /* padding length byte */  + mac_size;
    1415 
    1416     /*
    1417      * These lengths are all public so we can test them in non-constant time.
    1418      */
    1419     if (overhead > rec->length)
    1420         return 0;
    1421 
    1422     padding_length = rec->data[rec->length - 1];
    1423     good = constant_time_ge_s(rec->length, padding_length + overhead);
    1424     /* SSLv3 requires that the padding is minimal. */
    1425     good &= constant_time_ge_s(block_size, padding_length + 1);
    1426     rec->length -= good & (padding_length + 1);
    1427     return constant_time_select_int_s(good, 1, -1);
    1428 }
    1429 
    1430 /*-
    1431  * tls1_cbc_remove_padding removes the CBC padding from the decrypted, TLS, CBC
    1432  * record in |rec| in constant time and returns 1 if the padding is valid and
    1433  * -1 otherwise. It also removes any explicit IV from the start of the record
    1434  * without leaking any timing about whether there was enough space after the
    1435  * padding was removed.
    1436  *
    1437  * block_size: the block size of the cipher used to encrypt the record.
    1438  * returns:
    1439  *   0: (in non-constant time) if the record is publicly invalid.
    1440  *   1: if the padding was valid
    1441  *  -1: otherwise.
    1442  */
    1443 int tls1_cbc_remove_padding(const SSL *s,
    1444                             SSL3_RECORD *rec,
    1445                             size_t block_size, size_t mac_size)
    1446 {
    1447     size_t good;
    1448     size_t padding_length, to_check, i;
    1449     const size_t overhead = 1 /* padding length byte */  + mac_size;
    1450     /* Check if version requires explicit IV */
    1451     if (SSL_USE_EXPLICIT_IV(s)) {
    1452         /*
    1453          * These lengths are all public so we can test them in non-constant
    1454          * time.
    1455          */
    1456         if (overhead + block_size > rec->length)
    1457             return 0;
    1458         /* We can now safely skip explicit IV */
    1459         rec->data += block_size;
    1460         rec->input += block_size;
    1461         rec->length -= block_size;
    1462         rec->orig_len -= block_size;
    1463     } else if (overhead > rec->length)
    1464         return 0;
    1465 
    1466     padding_length = rec->data[rec->length - 1];
    1467 
    1468     if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_read_ctx)) &
    1469         EVP_CIPH_FLAG_AEAD_CIPHER) {
    1470         /* padding is already verified */
    1471         rec->length -= padding_length + 1;
    1472         return 1;
    1473     }
    1474 
    1475     good = constant_time_ge_s(rec->length, overhead + padding_length);
    1476     /*
    1477      * The padding consists of a length byte at the end of the record and
    1478      * then that many bytes of padding, all with the same value as the length
    1479      * byte. Thus, with the length byte included, there are i+1 bytes of
    1480      * padding. We can't check just |padding_length+1| bytes because that
    1481      * leaks decrypted information. Therefore we always have to check the
    1482      * maximum amount of padding possible. (Again, the length of the record
    1483      * is public information so we can use it.)
    1484      */
    1485     to_check = 256;            /* maximum amount of padding, inc length byte. */
    1486     if (to_check > rec->length)
    1487         to_check = rec->length;
    1488 
    1489     for (i = 0; i < to_check; i++) {
    1490         unsigned char mask = constant_time_ge_8_s(padding_length, i);
    1491         unsigned char b = rec->data[rec->length - 1 - i];
    1492         /*
    1493          * The final |padding_length+1| bytes should all have the value
    1494          * |padding_length|. Therefore the XOR should be zero.
    1495          */
    1496         good &= ~(mask & (padding_length ^ b));
    1497     }
    1498 
    1499     /*
    1500      * If any of the final |padding_length+1| bytes had the wrong value, one
    1501      * or more of the lower eight bits of |good| will be cleared.
    1502      */
    1503     good = constant_time_eq_s(0xff, good & 0xff);
    1504     rec->length -= good & (padding_length + 1);
    1505 
    1506     return constant_time_select_int_s(good, 1, -1);
    1507 }
    1508 
    1509 /*-
    1510  * ssl3_cbc_copy_mac copies |md_size| bytes from the end of |rec| to |out| in
    1511  * constant time (independent of the concrete value of rec->length, which may
    1512  * vary within a 256-byte window).
    1513  *
    1514  * ssl3_cbc_remove_padding or tls1_cbc_remove_padding must be called prior to
    1515  * this function.
    1516  *
    1517  * On entry:
    1518  *   rec->orig_len >= md_size
    1519  *   md_size <= EVP_MAX_MD_SIZE
    1520  *
    1521  * If CBC_MAC_ROTATE_IN_PLACE is defined then the rotation is performed with
    1522  * variable accesses in a 64-byte-aligned buffer. Assuming that this fits into
    1523  * a single or pair of cache-lines, then the variable memory accesses don't
    1524  * actually affect the timing. CPUs with smaller cache-lines [if any] are
    1525  * not multi-core and are not considered vulnerable to cache-timing attacks.
    1526  */
    1527 #define CBC_MAC_ROTATE_IN_PLACE
    1528 
    1529 int ssl3_cbc_copy_mac(unsigned char *out,
    1530                        const SSL3_RECORD *rec, size_t md_size)
    1531 {
    1532 #if defined(CBC_MAC_ROTATE_IN_PLACE)
    1533     unsigned char rotated_mac_buf[64 + EVP_MAX_MD_SIZE];
    1534     unsigned char *rotated_mac;
    1535 #else
    1536     unsigned char rotated_mac[EVP_MAX_MD_SIZE];
    1537 #endif
    1538 
    1539     /*
    1540      * mac_end is the index of |rec->data| just after the end of the MAC.
    1541      */
    1542     size_t mac_end = rec->length;
    1543     size_t mac_start = mac_end - md_size;
    1544     size_t in_mac;
    1545     /*
    1546      * scan_start contains the number of bytes that we can ignore because the
    1547      * MAC's position can only vary by 255 bytes.
    1548      */
    1549     size_t scan_start = 0;
    1550     size_t i, j;
    1551     size_t rotate_offset;
    1552 
    1553     if (!ossl_assert(rec->orig_len >= md_size
    1554                      && md_size <= EVP_MAX_MD_SIZE))
    1555         return 0;
    1556 
    1557 #if defined(CBC_MAC_ROTATE_IN_PLACE)
    1558     rotated_mac = rotated_mac_buf + ((0 - (size_t)rotated_mac_buf) & 63);
    1559 #endif
    1560 
    1561     /* This information is public so it's safe to branch based on it. */
    1562     if (rec->orig_len > md_size + 255 + 1)
    1563         scan_start = rec->orig_len - (md_size + 255 + 1);
    1564 
    1565     in_mac = 0;
    1566     rotate_offset = 0;
    1567     memset(rotated_mac, 0, md_size);
    1568     for (i = scan_start, j = 0; i < rec->orig_len; i++) {
    1569         size_t mac_started = constant_time_eq_s(i, mac_start);
    1570         size_t mac_ended = constant_time_lt_s(i, mac_end);
    1571         unsigned char b = rec->data[i];
    1572 
    1573         in_mac |= mac_started;
    1574         in_mac &= mac_ended;
    1575         rotate_offset |= j & mac_started;
    1576         rotated_mac[j++] |= b & in_mac;
    1577         j &= constant_time_lt_s(j, md_size);
    1578     }
    1579 
    1580     /* Now rotate the MAC */
    1581 #if defined(CBC_MAC_ROTATE_IN_PLACE)
    1582     j = 0;
    1583     for (i = 0; i < md_size; i++) {
    1584         /* in case cache-line is 32 bytes, touch second line */
    1585         ((volatile unsigned char *)rotated_mac)[rotate_offset ^ 32];
    1586         out[j++] = rotated_mac[rotate_offset++];
    1587         rotate_offset &= constant_time_lt_s(rotate_offset, md_size);
    1588     }
    1589 #else
    1590     memset(out, 0, md_size);
    1591     rotate_offset = md_size - rotate_offset;
    1592     rotate_offset &= constant_time_lt_s(rotate_offset, md_size);
    1593     for (i = 0; i < md_size; i++) {
    1594         for (j = 0; j < md_size; j++)
    1595             out[j] |= rotated_mac[i] & constant_time_eq_8_s(j, rotate_offset);
    1596         rotate_offset++;
    1597         rotate_offset &= constant_time_lt_s(rotate_offset, md_size);
    1598     }
    1599 #endif
    1600 
     1480    OSSL_TRACE_BEGIN(TLS) {
     1481        BIO_printf(trc_out, "md:\n");
     1482        BIO_dump_indent(trc_out, md, md_size, 4);
     1483    } OSSL_TRACE_END(TLS);
    16011484    return 1;
    16021485}
     
    16091492    SSL3_RECORD *rr;
    16101493    int imac_size;
    1611     size_t mac_size;
     1494    size_t mac_size = 0;
    16121495    unsigned char md[EVP_MAX_MD_SIZE];
    16131496    size_t max_plain_length = SSL3_RT_MAX_PLAIN_LENGTH;
     1497    SSL_MAC_BUF macbuf = { NULL, 0 };
     1498    int ret = 0;
    16141499
    16151500    rr = RECORD_LAYER_get_rrec(&s->rlayer);
     
    16361521    /* check is not needed I believe */
    16371522    if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
    1638         SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_DTLS1_PROCESS_RECORD,
    1639                  SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
     1523        SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
    16401524        return 0;
    16411525    }
     
    16451529    rr->orig_len = rr->length;
    16461530
     1531    if (s->read_hash != NULL) {
     1532        const EVP_MD *tmpmd = EVP_MD_CTX_get0_md(s->read_hash);
     1533
     1534        if (tmpmd != NULL) {
     1535            imac_size = EVP_MD_get_size(tmpmd);
     1536            if (!ossl_assert(imac_size >= 0 && imac_size <= EVP_MAX_MD_SIZE)) {
     1537                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
     1538                    return -1;
     1539            }
     1540            mac_size = (size_t)imac_size;
     1541        }
     1542    }
     1543
    16471544    if (SSL_READ_ETM(s) && s->read_hash) {
    16481545        unsigned char *mac;
    1649         mac_size = EVP_MD_CTX_size(s->read_hash);
    1650         if (!ossl_assert(mac_size <= EVP_MAX_MD_SIZE)) {
    1651             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_PROCESS_RECORD,
    1652                      ERR_R_INTERNAL_ERROR);
    1653             return 0;
    1654         }
     1546
    16551547        if (rr->orig_len < mac_size) {
    1656             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_DTLS1_PROCESS_RECORD,
    1657                      SSL_R_LENGTH_TOO_SHORT);
     1548            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
    16581549            return 0;
    16591550        }
     
    16621553        i = s->method->ssl3_enc->mac(s, rr, md, 0 /* not send */ );
    16631554        if (i == 0 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0) {
    1664             SSLfatal(s, SSL_AD_BAD_RECORD_MAC, SSL_F_DTLS1_PROCESS_RECORD,
    1665                    SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
     1555            SSLfatal(s, SSL_AD_BAD_RECORD_MAC,
     1556                     SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
    16661557            return 0;
    16671558        }
    1668     }
    1669 
    1670     enc_err = s->method->ssl3_enc->enc(s, rr, 1, 0);
     1559        /*
     1560         * We've handled the mac now - there is no MAC inside the encrypted
     1561         * record
     1562         */
     1563        mac_size = 0;
     1564    }
     1565
     1566    /*
     1567     * Set a mark around the packet decryption attempt.  This is DTLS, so
     1568     * bad packets are just ignored, and we don't want to leave stray
     1569     * errors in the queue from processing bogus junk that we ignored.
     1570     */
     1571    ERR_set_mark();
     1572    enc_err = s->method->ssl3_enc->enc(s, rr, 1, 0, &macbuf, mac_size);
     1573
    16711574    /*-
    16721575     * enc_err is:
    1673      *    0: (in non-constant time) if the record is publicly invalid.
    1674      *    1: if the padding is valid
    1675      *   -1: if the padding is invalid
     1576     *    0: if the record is publicly invalid, or an internal error, or AEAD
     1577     *       decryption failed, or ETM decryption failed.
     1578     *    1: Success or MTE decryption failed (MAC will be randomised)
    16761579     */
    16771580    if (enc_err == 0) {
     1581        ERR_pop_to_mark();
    16781582        if (ossl_statem_in_error(s)) {
    16791583            /* SSLfatal() got called */
    1680             return 0;
     1584            goto end;
    16811585        }
    16821586        /* For DTLS we simply ignore bad packets. */
    16831587        rr->length = 0;
    16841588        RECORD_LAYER_reset_packet_length(&s->rlayer);
    1685         return 0;
    1686     }
    1687 #ifdef SSL_DEBUG
    1688     printf("dec %ld\n", rr->length);
    1689     {
    1690         size_t z;
    1691         for (z = 0; z < rr->length; z++)
    1692             printf("%02X%c", rr->data[z], ((z + 1) % 16) ? ' ' : '\n');
    1693     }
    1694     printf("\n");
    1695 #endif
     1589        goto end;
     1590    }
     1591    ERR_clear_last_mark();
     1592    OSSL_TRACE_BEGIN(TLS) {
     1593        BIO_printf(trc_out, "dec %zd\n", rr->length);
     1594        BIO_dump_indent(trc_out, rr->data, rr->length, 4);
     1595    } OSSL_TRACE_END(TLS);
    16961596
    16971597    /* r->length is now the compressed data plus mac */
    1698     if ((sess != NULL) && !SSL_READ_ETM(s) &&
    1699         (s->enc_read_ctx != NULL) && (EVP_MD_CTX_md(s->read_hash) != NULL)) {
     1598    if ((sess != NULL)
     1599            && !SSL_READ_ETM(s)
     1600            && (s->enc_read_ctx != NULL)
     1601            && (EVP_MD_CTX_get0_md(s->read_hash) != NULL)) {
    17001602        /* s->read_hash != NULL => mac_size != -1 */
    1701         unsigned char *mac = NULL;
    1702         unsigned char mac_tmp[EVP_MAX_MD_SIZE];
    1703 
    1704         /* TODO(size_t): Convert this to do size_t properly */
    1705         imac_size = EVP_MD_CTX_size(s->read_hash);
    1706         if (imac_size < 0) {
    1707             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_PROCESS_RECORD,
    1708                      ERR_LIB_EVP);
    1709             return 0;
    1710         }
    1711         mac_size = (size_t)imac_size;
    1712         if (!ossl_assert(mac_size <= EVP_MAX_MD_SIZE)) {
    1713             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_PROCESS_RECORD,
    1714                      ERR_R_INTERNAL_ERROR);
    1715             return 0;
    1716         }
    1717 
    1718         /*
    1719          * orig_len is the length of the record before any padding was
    1720          * removed. This is public information, as is the MAC in use,
    1721          * therefore we can safely process the record in a different amount
    1722          * of time if it's too short to possibly contain a MAC.
    1723          */
    1724         if (rr->orig_len < mac_size ||
    1725             /* CBC records must have a padding length byte too. */
    1726             (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
    1727              rr->orig_len < mac_size + 1)) {
    1728             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_DTLS1_PROCESS_RECORD,
    1729                      SSL_R_LENGTH_TOO_SHORT);
    1730             return 0;
    1731         }
    1732 
    1733         if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {
    1734             /*
    1735              * We update the length so that the TLS header bytes can be
    1736              * constructed correctly but we need to extract the MAC in
    1737              * constant time from within the record, without leaking the
    1738              * contents of the padding bytes.
    1739              */
    1740             mac = mac_tmp;
    1741             if (!ssl3_cbc_copy_mac(mac_tmp, rr, mac_size)) {
    1742                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_PROCESS_RECORD,
    1743                          ERR_R_INTERNAL_ERROR);
    1744                 return 0;
    1745             }
    1746             rr->length -= mac_size;
    1747         } else {
    1748             /*
    1749              * In this case there's no padding, so |rec->orig_len| equals
    1750              * |rec->length| and we checked that there's enough bytes for
    1751              * |mac_size| above.
    1752              */
    1753             rr->length -= mac_size;
    1754             mac = &rr->data[rr->length];
    1755         }
    17561603
    17571604        i = s->method->ssl3_enc->mac(s, rr, md, 0 /* not send */ );
    1758         if (i == 0 || mac == NULL
    1759             || CRYPTO_memcmp(md, mac, mac_size) != 0)
    1760             enc_err = -1;
     1605        if (i == 0 || macbuf.mac == NULL
     1606            || CRYPTO_memcmp(md, macbuf.mac, mac_size) != 0)
     1607            enc_err = 0;
    17611608        if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
    1762             enc_err = -1;
    1763     }
    1764 
    1765     if (enc_err < 0) {
     1609            enc_err = 0;
     1610    }
     1611
     1612    if (enc_err == 0) {
    17661613        /* decryption failed, silently discard message */
    17671614        rr->length = 0;
    17681615        RECORD_LAYER_reset_packet_length(&s->rlayer);
    1769         return 0;
     1616        goto end;
    17701617    }
    17711618
     
    17731620    if (s->expand != NULL) {
    17741621        if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
    1775             SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_DTLS1_PROCESS_RECORD,
     1622            SSLfatal(s, SSL_AD_RECORD_OVERFLOW,
    17761623                     SSL_R_COMPRESSED_LENGTH_TOO_LONG);
    1777             return 0;
     1624            goto end;
    17781625        }
    17791626        if (!ssl3_do_uncompress(s, rr)) {
    1780             SSLfatal(s, SSL_AD_DECOMPRESSION_FAILURE,
    1781                      SSL_F_DTLS1_PROCESS_RECORD, SSL_R_BAD_DECOMPRESSION);
    1782             return 0;
     1627            SSLfatal(s, SSL_AD_DECOMPRESSION_FAILURE, SSL_R_BAD_DECOMPRESSION);
     1628            goto end;
    17831629        }
    17841630    }
     
    17901636    /* send overflow if the plaintext is too long now it has passed MAC */
    17911637    if (rr->length > max_plain_length) {
    1792         SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_DTLS1_PROCESS_RECORD,
    1793                  SSL_R_DATA_LENGTH_TOO_LONG);
    1794         return 0;
     1638        SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_R_DATA_LENGTH_TOO_LONG);
     1639        goto end;
    17951640    }
    17961641
     
    17981643    /*-
    17991644     * So at this point the following is true
    1800      * ssl->s3->rrec.type   is the type of record
    1801      * ssl->s3->rrec.length == number of bytes in record
    1802      * ssl->s3->rrec.off    == offset to first valid byte
    1803      * ssl->s3->rrec.data   == where to take bytes from, increment
    1804      *                         after use :-).
     1645     * ssl->s3.rrec.type   is the type of record
     1646     * ssl->s3.rrec.length == number of bytes in record
     1647     * ssl->s3.rrec.off    == offset to first valid byte
     1648     * ssl->s3.rrec.data   == where to take bytes from, increment
     1649     *                        after use :-).
    18051650     */
    18061651
     
    18111656    dtls1_record_bitmap_update(s, bitmap);
    18121657
    1813     return 1;
     1658    ret = 1;
     1659 end:
     1660    if (macbuf.alloced)
     1661        OPENSSL_free(macbuf.mac);
     1662    return ret;
    18141663}
    18151664
     
    18261675 * or non-blocking IO.
    18271676 * When it finishes, one packet has been decoded and can be found in
    1828  * ssl->s3->rrec.type    - is the type of record
    1829  * ssl->s3->rrec.data,   - data
    1830  * ssl->s3->rrec.length, - number of bytes
     1677 * ssl->s3.rrec.type    - is the type of record
     1678 * ssl->s3.rrec.data    - data
     1679 * ssl->s3.rrec.length - number of bytes
    18311680 */
    18321681/* used only by dtls1_read_bytes */
     
    19831832#endif
    19841833        /* Check whether this is a repeat, or aged record. */
    1985         /*
    1986          * TODO: Does it make sense to have replay protection in epoch 0 where
    1987          * we have no integrity negotiated yet?
    1988          */
    19891834        if (!dtls1_record_replay_check(s, bitmap)) {
    19901835            rr->length = 0;
  • trunk/src/libs/openssl-3.0.1/ssl/record/ssl3_record_tls13.c

    r91772 r94082  
    11/*
    2  * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 *
    4  * Licensed under the OpenSSL license (the "License").  You may not use
     4 * Licensed under the Apache License 2.0 (the "License").  You may not use
    55 * this file except in compliance with the License.  You can obtain a copy
    66 * in the file LICENSE in the source distribution or at
     
    1313
    1414/*-
    15  * tls13_enc encrypts/decrypts |n_recs| in |recs|. Will call SSLfatal() for
    16  * internal errors, but not otherwise.
     15 * tls13_enc encrypts/decrypts |n_recs| in |recs|. Calls SSLfatal on internal
     16 * error, but not otherwise. It is the responsibility of the caller to report
     17 * a bad_record_mac.
    1718 *
    1819 * Returns:
    19  *    0: (in non-constant time) if the record is publicly invalid (i.e. too
    20  *        short etc).
    21  *    1: if the record encryption was successful.
    22  *   -1: if the record's AEAD-authenticator is invalid or, if sending,
    23  *       an internal error occurred.
     20 *    0: On failure
     21 *    1: if the record encryption/decryption was successful.
    2422 */
    25 int tls13_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int sending)
     23int tls13_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int sending,
     24              ossl_unused SSL_MAC_BUF *mac, ossl_unused size_t macsize)
    2625{
    2726    EVP_CIPHER_CTX *ctx;
     
    3736    if (n_recs != 1) {
    3837        /* Should not happen */
    39         /* TODO(TLS1.3): Support pipelining */
    40         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
    41                  ERR_R_INTERNAL_ERROR);
    42         return -1;
     38        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
     39        return 0;
    4340    }
    4441
     
    6562    }
    6663
    67     ivlen = EVP_CIPHER_CTX_iv_length(ctx);
     64    ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
    6865
    6966    if (s->early_data_state == SSL_EARLY_DATA_WRITING
     
    7471            if (!ossl_assert(s->psksession != NULL
    7572                             && s->psksession->ext.max_early_data > 0)) {
    76                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
    77                          ERR_R_INTERNAL_ERROR);
    78                 return -1;
     73                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
     74                return 0;
    7975            }
    8076            alg_enc = s->psksession->cipher->algorithm_enc;
     
    8581         * be NULL
    8682         */
    87         if (!ossl_assert(s->s3->tmp.new_cipher != NULL)) {
    88             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
    89                      ERR_R_INTERNAL_ERROR);
    90             return -1;
     83        if (!ossl_assert(s->s3.tmp.new_cipher != NULL)) {
     84            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
     85            return 0;
    9186        }
    92         alg_enc = s->s3->tmp.new_cipher->algorithm_enc;
     87        alg_enc = s->s3.tmp.new_cipher->algorithm_enc;
    9388    }
    9489
     
    10095         if (sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen,
    10196                                         NULL) <= 0) {
    102             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
    103                      ERR_R_INTERNAL_ERROR);
    104             return -1;
     97            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
     98            return 0;
    10599        }
    106100    } else if (alg_enc & SSL_AESGCM) {
     
    109103        taglen = EVP_CHACHAPOLY_TLS_TAG_LEN;
    110104    } else {
    111         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
    112                  ERR_R_INTERNAL_ERROR);
    113         return -1;
     105        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
     106        return 0;
    114107    }
    115108
     
    127120    if (ivlen < SEQ_NUM_SIZE) {
    128121        /* Should not happen */
    129         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
    130                  ERR_R_INTERNAL_ERROR);
    131         return -1;
     122        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
     123        return 0;
    132124    }
    133125    offset = ivlen - SEQ_NUM_SIZE;
     
    144136    if (loop == 0) {
    145137        /* Sequence has wrapped */
    146         return -1;
     138        return 0;
    147139    }
    148140
    149     /* TODO(size_t): lenu/lenf should be a size_t but EVP doesn't support it */
    150141    if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, sending) <= 0
    151142            || (!sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
    152143                                             taglen,
    153144                                             rec->data + rec->length) <= 0)) {
    154         return -1;
     145        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
     146        return 0;
    155147    }
    156148
     
    163155            || hdrlen != SSL3_RT_HEADER_LENGTH
    164156            || !WPACKET_finish(&wpkt)) {
     157        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    165158        WPACKET_cleanup(&wpkt);
    166         return -1;
     159        return 0;
    167160    }
    168161
     
    180173            || EVP_CipherFinal_ex(ctx, rec->data + lenu, &lenf) <= 0
    181174            || (size_t)(lenu + lenf) != rec->length) {
    182         return -1;
     175        return 0;
    183176    }
    184177    if (sending) {
     
    186179        if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen,
    187180                                rec->data + rec->length) <= 0) {
    188             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
    189                      ERR_R_INTERNAL_ERROR);
    190             return -1;
     181            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
     182            return 0;
    191183        }
    192184        rec->length += taglen;
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette