Home | History | Annotate | Line # | Download | only in quic
      1      1.1  christos /*
      2  1.1.1.3  christos  * Copyright 2022-2026 The OpenSSL Project Authors. All Rights Reserved.
      3      1.1  christos  *
      4      1.1  christos  * Licensed under the Apache License 2.0 (the "License").  You may not use
      5      1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      6      1.1  christos  * in the file LICENSE in the source distribution or at
      7      1.1  christos  * https://www.openssl.org/source/license.html
      8      1.1  christos  */
      9      1.1  christos 
     10      1.1  christos #include "internal/packet_quic.h"
     11      1.1  christos #include "internal/nelem.h"
     12      1.1  christos #include "internal/quic_wire.h"
     13      1.1  christos #include "internal/quic_record_rx.h"
     14      1.1  christos #include "internal/quic_ackm.h"
     15      1.1  christos #include "internal/quic_rx_depack.h"
     16      1.1  christos #include "internal/quic_error.h"
     17      1.1  christos #include "internal/quic_fc.h"
     18      1.1  christos #include "internal/quic_channel.h"
     19      1.1  christos #include "internal/sockets.h"
     20      1.1  christos 
     21      1.1  christos #include "quic_local.h"
     22      1.1  christos #include "quic_channel_local.h"
     23      1.1  christos #include "../ssl_local.h"
     24      1.1  christos 
     25      1.1  christos /*
     26      1.1  christos  * Helper functions to process different frame types.
     27      1.1  christos  *
     28      1.1  christos  * Typically, those that are ACK eliciting will take an OSSL_ACKM_RX_PKT
     29      1.1  christos  * pointer argument, the few that aren't ACK eliciting will not.  This makes
     30      1.1  christos  * them a verifiable pattern against tables where this is specified.
     31      1.1  christos  */
     32      1.1  christos static int depack_do_implicit_stream_create(QUIC_CHANNEL *ch,
     33  1.1.1.2  christos     uint64_t stream_id,
     34  1.1.1.2  christos     uint64_t frame_type,
     35  1.1.1.2  christos     QUIC_STREAM **result);
     36      1.1  christos 
     37      1.1  christos static int depack_do_frame_padding(PACKET *pkt)
     38      1.1  christos {
     39      1.1  christos     /* We ignore this frame */
     40      1.1  christos     ossl_quic_wire_decode_padding(pkt);
     41      1.1  christos     return 1;
     42      1.1  christos }
     43      1.1  christos 
     44      1.1  christos static int depack_do_frame_ping(PACKET *pkt, QUIC_CHANNEL *ch,
     45  1.1.1.2  christos     uint32_t enc_level,
     46  1.1.1.2  christos     OSSL_ACKM_RX_PKT *ackm_data)
     47      1.1  christos {
     48      1.1  christos     /* We ignore this frame, apart from eliciting an ACK */
     49      1.1  christos     if (!ossl_quic_wire_decode_frame_ping(pkt)) {
     50      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
     51  1.1.1.2  christos             OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
     52  1.1.1.2  christos             OSSL_QUIC_FRAME_TYPE_PING,
     53  1.1.1.2  christos             "decode error");
     54      1.1  christos         return 0;
     55      1.1  christos     }
     56      1.1  christos 
     57      1.1  christos     ossl_quic_tx_packetiser_schedule_ack_eliciting(ch->txp, enc_level);
     58      1.1  christos     return 1;
     59      1.1  christos }
     60      1.1  christos 
     61      1.1  christos static int depack_do_frame_ack(PACKET *pkt, QUIC_CHANNEL *ch,
     62  1.1.1.2  christos     int packet_space, OSSL_TIME received,
     63  1.1.1.2  christos     uint64_t frame_type,
     64  1.1.1.2  christos     OSSL_QRX_PKT *qpacket)
     65      1.1  christos {
     66      1.1  christos     OSSL_QUIC_FRAME_ACK ack;
     67      1.1  christos     OSSL_QUIC_ACK_RANGE *p;
     68      1.1  christos     uint64_t total_ranges = 0;
     69      1.1  christos     uint32_t ack_delay_exp = ch->rx_ack_delay_exp;
     70      1.1  christos 
     71      1.1  christos     if (!ossl_quic_wire_peek_frame_ack_num_ranges(pkt, &total_ranges)
     72      1.1  christos         /* In case sizeof(uint64_t) > sizeof(size_t) */
     73      1.1  christos         || total_ranges > SIZE_MAX / sizeof(OSSL_QUIC_ACK_RANGE))
     74      1.1  christos         goto malformed;
     75      1.1  christos 
     76      1.1  christos     if (ch->num_ack_range_scratch < (size_t)total_ranges) {
     77      1.1  christos         if ((p = OPENSSL_realloc(ch->ack_range_scratch,
     78  1.1.1.2  christos                  sizeof(OSSL_QUIC_ACK_RANGE)
     79  1.1.1.2  christos                      * (size_t)total_ranges))
     80  1.1.1.2  christos             == NULL)
     81      1.1  christos             goto malformed;
     82      1.1  christos 
     83  1.1.1.2  christos         ch->ack_range_scratch = p;
     84  1.1.1.2  christos         ch->num_ack_range_scratch = (size_t)total_ranges;
     85      1.1  christos     }
     86      1.1  christos 
     87      1.1  christos     ack.ack_ranges = ch->ack_range_scratch;
     88      1.1  christos     ack.num_ack_ranges = (size_t)total_ranges;
     89      1.1  christos 
     90      1.1  christos     if (!ossl_quic_wire_decode_frame_ack(pkt, ack_delay_exp, &ack, NULL))
     91      1.1  christos         goto malformed;
     92      1.1  christos 
     93      1.1  christos     if (qpacket->hdr->type == QUIC_PKT_TYPE_1RTT
     94      1.1  christos         && (qpacket->key_epoch < ossl_qrx_get_key_epoch(ch->qrx)
     95      1.1  christos             || ch->rxku_expected)
     96      1.1  christos         && ack.ack_ranges[0].end >= ch->txku_pn) {
     97      1.1  christos         /*
     98      1.1  christos          * RFC 9001 s. 6.2: An endpoint that receives an acknowledgment that is
     99      1.1  christos          * carried in a packet protected with old keys where any acknowledged
    100      1.1  christos          * packet was protected with newer keys MAY treat that as a connection
    101      1.1  christos          * error of type KEY_UPDATE_ERROR.
    102      1.1  christos          *
    103      1.1  christos          * Two cases to handle here:
    104      1.1  christos          *
    105      1.1  christos          *   - We did spontaneous TXKU, the peer has responded in kind and we
    106      1.1  christos          *     have detected RXKU; !ch->rxku_expected, but then it sent a packet
    107      1.1  christos          *     with old keys acknowledging a packet in the new key epoch.
    108      1.1  christos          *
    109      1.1  christos          *     This also covers the case where we got RXKU and triggered
    110      1.1  christos          *     solicited TXKU, and then for some reason the peer sent an ACK of
    111      1.1  christos          *     a PN in our new TX key epoch with old keys.
    112      1.1  christos          *
    113      1.1  christos          *   - We did spontaneous TXKU; ch->txku_pn is the starting PN of our
    114      1.1  christos          *     new TX key epoch; the peer has not initiated a solicited TXKU in
    115      1.1  christos          *     response (so we have not detected RXKU); in this case the RX key
    116      1.1  christos          *     epoch has not incremented and ch->rxku_expected is still 1.
    117      1.1  christos          */
    118      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    119  1.1.1.2  christos             OSSL_QUIC_ERR_KEY_UPDATE_ERROR,
    120  1.1.1.2  christos             frame_type,
    121  1.1.1.2  christos             "acked packet which initiated a "
    122  1.1.1.2  christos             "key update without a "
    123  1.1.1.2  christos             "corresponding key update");
    124      1.1  christos         return 0;
    125      1.1  christos     }
    126      1.1  christos 
    127      1.1  christos     if (!ossl_ackm_on_rx_ack_frame(ch->ackm, &ack,
    128  1.1.1.2  christos             packet_space, received))
    129      1.1  christos         goto malformed;
    130      1.1  christos 
    131      1.1  christos     ++ch->diag_num_rx_ack;
    132      1.1  christos     return 1;
    133      1.1  christos 
    134      1.1  christos malformed:
    135      1.1  christos     ossl_quic_channel_raise_protocol_error(ch,
    136  1.1.1.2  christos         OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
    137  1.1.1.2  christos         frame_type,
    138  1.1.1.2  christos         "decode error");
    139      1.1  christos     return 0;
    140      1.1  christos }
    141      1.1  christos 
    142      1.1  christos static int depack_do_frame_reset_stream(PACKET *pkt,
    143  1.1.1.2  christos     QUIC_CHANNEL *ch,
    144  1.1.1.2  christos     OSSL_ACKM_RX_PKT *ackm_data)
    145      1.1  christos {
    146      1.1  christos     OSSL_QUIC_FRAME_RESET_STREAM frame_data;
    147      1.1  christos     QUIC_STREAM *stream = NULL;
    148      1.1  christos     uint64_t fce;
    149      1.1  christos 
    150      1.1  christos     if (!ossl_quic_wire_decode_frame_reset_stream(pkt, &frame_data)) {
    151      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    152  1.1.1.2  christos             OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
    153  1.1.1.2  christos             OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
    154  1.1.1.2  christos             "decode error");
    155      1.1  christos         return 0;
    156      1.1  christos     }
    157      1.1  christos 
    158      1.1  christos     if (!depack_do_implicit_stream_create(ch, frame_data.stream_id,
    159  1.1.1.2  christos             OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
    160  1.1.1.2  christos             &stream))
    161      1.1  christos         return 0; /* error already raised for us */
    162      1.1  christos 
    163      1.1  christos     if (stream == NULL)
    164      1.1  christos         return 1; /* old deleted stream, not a protocol violation, ignore */
    165      1.1  christos 
    166      1.1  christos     if (!ossl_quic_stream_has_recv(stream)) {
    167      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    168  1.1.1.2  christos             OSSL_QUIC_ERR_STREAM_STATE_ERROR,
    169  1.1.1.2  christos             OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
    170  1.1.1.2  christos             "RESET_STREAM frame for "
    171  1.1.1.2  christos             "TX only stream");
    172      1.1  christos         return 0;
    173      1.1  christos     }
    174      1.1  christos 
    175      1.1  christos     /*
    176      1.1  christos      * The final size field of the RESET_STREAM frame must be used to determine
    177      1.1  christos      * how much flow control credit the aborted stream was considered to have
    178      1.1  christos      * consumed.
    179      1.1  christos      *
    180      1.1  christos      * We also need to ensure that if we already have a final size for the
    181      1.1  christos      * stream, the RESET_STREAM frame's Final Size field matches this; we SHOULD
    182      1.1  christos      * terminate the connection otherwise (RFC 9000 s. 4.5). The RXFC takes care
    183      1.1  christos      * of this for us.
    184      1.1  christos      */
    185      1.1  christos     if (!ossl_quic_rxfc_on_rx_stream_frame(&stream->rxfc,
    186  1.1.1.2  christos             frame_data.final_size, /*is_fin=*/1)) {
    187      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    188  1.1.1.2  christos             OSSL_QUIC_ERR_INTERNAL_ERROR,
    189  1.1.1.2  christos             OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
    190  1.1.1.2  christos             "internal error (flow control)");
    191      1.1  christos         return 0;
    192      1.1  christos     }
    193      1.1  christos 
    194      1.1  christos     /* Has a flow control error occurred? */
    195      1.1  christos     fce = ossl_quic_rxfc_get_error(&stream->rxfc, 0);
    196      1.1  christos     if (fce != OSSL_QUIC_ERR_NO_ERROR) {
    197      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    198  1.1.1.2  christos             fce,
    199  1.1.1.2  christos             OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
    200  1.1.1.2  christos             "flow control violation");
    201      1.1  christos         return 0;
    202      1.1  christos     }
    203      1.1  christos 
    204      1.1  christos     /*
    205      1.1  christos      * Depending on the receive part state this is handled either as a reset
    206      1.1  christos      * transition or a no-op (e.g. if a reset has already been received before,
    207      1.1  christos      * or the application already retired a FIN). Best effort - there are no
    208      1.1  christos      * protocol error conditions we need to check for here.
    209      1.1  christos      */
    210      1.1  christos     ossl_quic_stream_map_notify_reset_recv_part(&ch->qsm, stream,
    211  1.1.1.2  christos         frame_data.app_error_code,
    212  1.1.1.2  christos         frame_data.final_size);
    213      1.1  christos 
    214      1.1  christos     ossl_quic_stream_map_update_state(&ch->qsm, stream);
    215      1.1  christos     return 1;
    216      1.1  christos }
    217      1.1  christos 
    218      1.1  christos static int depack_do_frame_stop_sending(PACKET *pkt,
    219  1.1.1.2  christos     QUIC_CHANNEL *ch,
    220  1.1.1.2  christos     OSSL_ACKM_RX_PKT *ackm_data)
    221      1.1  christos {
    222      1.1  christos     OSSL_QUIC_FRAME_STOP_SENDING frame_data;
    223      1.1  christos     QUIC_STREAM *stream = NULL;
    224      1.1  christos 
    225      1.1  christos     if (!ossl_quic_wire_decode_frame_stop_sending(pkt, &frame_data)) {
    226      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    227  1.1.1.2  christos             OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
    228  1.1.1.2  christos             OSSL_QUIC_FRAME_TYPE_STOP_SENDING,
    229  1.1.1.2  christos             "decode error");
    230      1.1  christos         return 0;
    231      1.1  christos     }
    232      1.1  christos 
    233      1.1  christos     if (!depack_do_implicit_stream_create(ch, frame_data.stream_id,
    234  1.1.1.2  christos             OSSL_QUIC_FRAME_TYPE_STOP_SENDING,
    235  1.1.1.2  christos             &stream))
    236      1.1  christos         return 0; /* error already raised for us */
    237      1.1  christos 
    238      1.1  christos     if (stream == NULL)
    239      1.1  christos         return 1; /* old deleted stream, not a protocol violation, ignore */
    240      1.1  christos 
    241      1.1  christos     if (!ossl_quic_stream_has_send(stream)) {
    242      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    243  1.1.1.2  christos             OSSL_QUIC_ERR_STREAM_STATE_ERROR,
    244  1.1.1.2  christos             OSSL_QUIC_FRAME_TYPE_STOP_SENDING,
    245  1.1.1.2  christos             "STOP_SENDING frame for "
    246  1.1.1.2  christos             "RX only stream");
    247      1.1  christos         return 0;
    248      1.1  christos     }
    249      1.1  christos 
    250  1.1.1.2  christos     stream->peer_stop_sending = 1;
    251  1.1.1.2  christos     stream->peer_stop_sending_aec = frame_data.app_error_code;
    252      1.1  christos 
    253      1.1  christos     /*
    254      1.1  christos      * RFC 9000 s. 3.5: Receiving a STOP_SENDING frame means we must respond in
    255      1.1  christos      * turn with a RESET_STREAM frame for the same part of the stream. The other
    256      1.1  christos      * part is unaffected.
    257      1.1  christos      */
    258      1.1  christos     ossl_quic_stream_map_reset_stream_send_part(&ch->qsm, stream,
    259  1.1.1.2  christos         frame_data.app_error_code);
    260      1.1  christos     return 1;
    261      1.1  christos }
    262      1.1  christos 
    263      1.1  christos static int depack_do_frame_crypto(PACKET *pkt, QUIC_CHANNEL *ch,
    264  1.1.1.2  christos     OSSL_QRX_PKT *parent_pkt,
    265  1.1.1.2  christos     OSSL_ACKM_RX_PKT *ackm_data,
    266  1.1.1.2  christos     uint64_t *datalen)
    267      1.1  christos {
    268      1.1  christos     OSSL_QUIC_FRAME_CRYPTO f;
    269      1.1  christos     QUIC_RSTREAM *rstream;
    270      1.1  christos     QUIC_RXFC *rxfc;
    271      1.1  christos 
    272      1.1  christos     *datalen = 0;
    273      1.1  christos 
    274      1.1  christos     if (!ossl_quic_wire_decode_frame_crypto(pkt, 0, &f)) {
    275      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    276  1.1.1.2  christos             OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
    277  1.1.1.2  christos             OSSL_QUIC_FRAME_TYPE_CRYPTO,
    278  1.1.1.2  christos             "decode error");
    279      1.1  christos         return 0;
    280      1.1  christos     }
    281      1.1  christos 
    282      1.1  christos     if (f.len == 0)
    283      1.1  christos         return 1; /* nothing to do */
    284      1.1  christos 
    285      1.1  christos     rstream = ch->crypto_recv[ackm_data->pkt_space];
    286      1.1  christos     if (!ossl_assert(rstream != NULL))
    287      1.1  christos         /*
    288      1.1  christos          * This should not happen; we should only have a NULL stream here if
    289      1.1  christos          * the EL has been discarded, and if the EL has been discarded we
    290      1.1  christos          * shouldn't be here.
    291      1.1  christos          */
    292      1.1  christos         return 0;
    293      1.1  christos 
    294      1.1  christos     rxfc = &ch->crypto_rxfc[ackm_data->pkt_space];
    295      1.1  christos 
    296      1.1  christos     if (!ossl_quic_rxfc_on_rx_stream_frame(rxfc, f.offset + f.len,
    297  1.1.1.2  christos             /*is_fin=*/0)) {
    298      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    299  1.1.1.2  christos             OSSL_QUIC_ERR_INTERNAL_ERROR,
    300  1.1.1.2  christos             OSSL_QUIC_FRAME_TYPE_CRYPTO,
    301  1.1.1.2  christos             "internal error (crypto RXFC)");
    302      1.1  christos         return 0;
    303      1.1  christos     }
    304      1.1  christos 
    305      1.1  christos     if (ossl_quic_rxfc_get_error(rxfc, 0) != OSSL_QUIC_ERR_NO_ERROR) {
    306      1.1  christos         ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_CRYPTO_BUFFER_EXCEEDED,
    307  1.1.1.2  christos             OSSL_QUIC_FRAME_TYPE_CRYPTO,
    308  1.1.1.2  christos             "exceeded maximum crypto buffer");
    309      1.1  christos         return 0;
    310      1.1  christos     }
    311      1.1  christos 
    312      1.1  christos     if (!ossl_quic_rstream_queue_data(rstream, parent_pkt,
    313  1.1.1.2  christos             f.offset, f.data, f.len, 0)) {
    314      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    315  1.1.1.2  christos             OSSL_QUIC_ERR_INTERNAL_ERROR,
    316  1.1.1.2  christos             OSSL_QUIC_FRAME_TYPE_CRYPTO,
    317  1.1.1.2  christos             "internal error (rstream queue)");
    318      1.1  christos         return 0;
    319      1.1  christos     }
    320      1.1  christos 
    321      1.1  christos     ch->did_crypto_frame = 1;
    322      1.1  christos     *datalen = f.len;
    323      1.1  christos 
    324      1.1  christos     return 1;
    325      1.1  christos }
    326      1.1  christos 
    327      1.1  christos static int depack_do_frame_new_token(PACKET *pkt, QUIC_CHANNEL *ch,
    328  1.1.1.2  christos     OSSL_ACKM_RX_PKT *ackm_data)
    329      1.1  christos {
    330      1.1  christos     const uint8_t *token;
    331      1.1  christos     size_t token_len;
    332      1.1  christos 
    333      1.1  christos     if (!ossl_quic_wire_decode_frame_new_token(pkt, &token, &token_len)) {
    334      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    335  1.1.1.2  christos             OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
    336  1.1.1.2  christos             OSSL_QUIC_FRAME_TYPE_NEW_TOKEN,
    337  1.1.1.2  christos             "decode error");
    338      1.1  christos         return 0;
    339      1.1  christos     }
    340      1.1  christos 
    341      1.1  christos     if (token_len == 0) {
    342      1.1  christos         /*
    343      1.1  christos          * RFC 9000 s. 19.7: "A client MUST treat receipt of a NEW_TOKEN frame
    344      1.1  christos          * with an empty Token field as a connection error of type
    345      1.1  christos          * FRAME_ENCODING_ERROR."
    346      1.1  christos          */
    347      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    348  1.1.1.2  christos             OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
    349  1.1.1.2  christos             OSSL_QUIC_FRAME_TYPE_NEW_TOKEN,
    350  1.1.1.2  christos             "zero-length NEW_TOKEN");
    351      1.1  christos         return 0;
    352      1.1  christos     }
    353      1.1  christos 
    354      1.1  christos     /* store the new token in our token cache */
    355      1.1  christos     if (!ossl_quic_set_peer_token(ossl_quic_port_get_channel_ctx(ch->port),
    356  1.1.1.2  christos             &ch->cur_peer_addr, token, token_len))
    357      1.1  christos         return 0;
    358      1.1  christos 
    359      1.1  christos     return 1;
    360      1.1  christos }
    361      1.1  christos 
    362      1.1  christos /*
    363      1.1  christos  * Returns 1 if no protocol violation has occurred. In this case *result will be
    364      1.1  christos  * non-NULL unless this is an old deleted stream and we should ignore the frame
    365      1.1  christos  * causing this function to be called. Returns 0 on protocol violation.
    366      1.1  christos  */
    367      1.1  christos static int depack_do_implicit_stream_create(QUIC_CHANNEL *ch,
    368  1.1.1.2  christos     uint64_t stream_id,
    369  1.1.1.2  christos     uint64_t frame_type,
    370  1.1.1.2  christos     QUIC_STREAM **result)
    371      1.1  christos {
    372      1.1  christos     QUIC_STREAM *stream;
    373      1.1  christos     uint64_t peer_role, stream_ordinal;
    374      1.1  christos     uint64_t *p_next_ordinal_local, *p_next_ordinal_remote;
    375      1.1  christos     QUIC_RXFC *max_streams_fc;
    376      1.1  christos     int is_uni, is_remote_init;
    377      1.1  christos 
    378      1.1  christos     stream = ossl_quic_stream_map_get_by_id(&ch->qsm, stream_id);
    379      1.1  christos     if (stream != NULL) {
    380      1.1  christos         *result = stream;
    381      1.1  christos         return 1;
    382      1.1  christos     }
    383      1.1  christos 
    384      1.1  christos     /*
    385      1.1  christos      * If we do not yet have a stream with the given ID, there are three
    386      1.1  christos      * possibilities:
    387      1.1  christos      *
    388      1.1  christos      *   (a) The stream ID is for a remotely-created stream and the peer
    389      1.1  christos      *       is creating a stream.
    390      1.1  christos      *
    391      1.1  christos      *   (b) The stream ID is for a locally-created stream which has
    392      1.1  christos      *       previously been deleted.
    393      1.1  christos      *
    394      1.1  christos      *   (c) The stream ID is for a locally-created stream which does
    395      1.1  christos      *       not exist yet. This is a protocol violation and we must
    396      1.1  christos      *       terminate the connection in this case.
    397      1.1  christos      *
    398      1.1  christos      * We distinguish between (b) and (c) using the stream ID allocator
    399      1.1  christos      * variable. Since stream ordinals are allocated monotonically, we
    400      1.1  christos      * simply determine if the stream ordinal is in the future.
    401      1.1  christos      */
    402      1.1  christos     peer_role = ch->is_server
    403      1.1  christos         ? QUIC_STREAM_INITIATOR_CLIENT
    404      1.1  christos         : QUIC_STREAM_INITIATOR_SERVER;
    405      1.1  christos 
    406      1.1  christos     is_remote_init = ((stream_id & QUIC_STREAM_INITIATOR_MASK) == peer_role);
    407      1.1  christos     is_uni = ((stream_id & QUIC_STREAM_DIR_MASK) == QUIC_STREAM_DIR_UNI);
    408      1.1  christos 
    409      1.1  christos     stream_ordinal = stream_id >> 2;
    410      1.1  christos 
    411      1.1  christos     if (is_remote_init) {
    412      1.1  christos         /*
    413      1.1  christos          * Peer-created stream which does not yet exist. Create it. QUIC stream
    414      1.1  christos          * ordinals within a given stream type MUST be used in sequence and
    415      1.1  christos          * receiving a STREAM frame for ordinal n must implicitly create streams
    416      1.1  christos          * with ordinals [0, n) within that stream type even if no explicit
    417      1.1  christos          * STREAM frames are received for those ordinals.
    418      1.1  christos          */
    419      1.1  christos         p_next_ordinal_remote = is_uni
    420      1.1  christos             ? &ch->next_remote_stream_ordinal_uni
    421      1.1  christos             : &ch->next_remote_stream_ordinal_bidi;
    422      1.1  christos 
    423      1.1  christos         /* Check this isn't violating stream count flow control. */
    424      1.1  christos         max_streams_fc = is_uni
    425      1.1  christos             ? &ch->max_streams_uni_rxfc
    426      1.1  christos             : &ch->max_streams_bidi_rxfc;
    427      1.1  christos 
    428      1.1  christos         if (!ossl_quic_rxfc_on_rx_stream_frame(max_streams_fc,
    429  1.1.1.2  christos                 stream_ordinal + 1,
    430  1.1.1.2  christos                 /*is_fin=*/0)) {
    431      1.1  christos             ossl_quic_channel_raise_protocol_error(ch,
    432  1.1.1.2  christos                 OSSL_QUIC_ERR_INTERNAL_ERROR,
    433  1.1.1.2  christos                 frame_type,
    434  1.1.1.2  christos                 "internal error (stream count RXFC)");
    435      1.1  christos             return 0;
    436      1.1  christos         }
    437      1.1  christos 
    438      1.1  christos         if (ossl_quic_rxfc_get_error(max_streams_fc, 0) != OSSL_QUIC_ERR_NO_ERROR) {
    439      1.1  christos             ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_STREAM_LIMIT_ERROR,
    440  1.1.1.2  christos                 frame_type,
    441  1.1.1.2  christos                 "exceeded maximum allowed streams");
    442      1.1  christos             return 0;
    443      1.1  christos         }
    444      1.1  christos 
    445      1.1  christos         /*
    446      1.1  christos          * Create the named stream and any streams coming before it yet to be
    447      1.1  christos          * created.
    448      1.1  christos          */
    449      1.1  christos         while (*p_next_ordinal_remote <= stream_ordinal) {
    450  1.1.1.2  christos             uint64_t cur_stream_id = (*p_next_ordinal_remote << 2) | (stream_id & (QUIC_STREAM_DIR_MASK | QUIC_STREAM_INITIATOR_MASK));
    451      1.1  christos 
    452      1.1  christos             stream = ossl_quic_channel_new_stream_remote(ch, cur_stream_id);
    453      1.1  christos             if (stream == NULL) {
    454      1.1  christos                 ossl_quic_channel_raise_protocol_error(ch,
    455  1.1.1.2  christos                     OSSL_QUIC_ERR_INTERNAL_ERROR,
    456  1.1.1.2  christos                     frame_type,
    457  1.1.1.2  christos                     "internal error (stream allocation)");
    458      1.1  christos                 return 0;
    459      1.1  christos             }
    460      1.1  christos 
    461      1.1  christos             ++*p_next_ordinal_remote;
    462      1.1  christos         }
    463      1.1  christos 
    464      1.1  christos         *result = stream;
    465      1.1  christos     } else {
    466      1.1  christos         /* Locally-created stream which does not yet exist. */
    467      1.1  christos         p_next_ordinal_local = is_uni
    468      1.1  christos             ? &ch->next_local_stream_ordinal_uni
    469      1.1  christos             : &ch->next_local_stream_ordinal_bidi;
    470      1.1  christos 
    471      1.1  christos         if (stream_ordinal >= *p_next_ordinal_local) {
    472      1.1  christos             /*
    473      1.1  christos              * We never created this stream yet, this is a protocol
    474      1.1  christos              * violation.
    475      1.1  christos              */
    476      1.1  christos             ossl_quic_channel_raise_protocol_error(ch,
    477  1.1.1.2  christos                 OSSL_QUIC_ERR_STREAM_STATE_ERROR,
    478  1.1.1.2  christos                 frame_type,
    479  1.1.1.2  christos                 "STREAM frame for nonexistent "
    480  1.1.1.2  christos                 "stream");
    481      1.1  christos             return 0;
    482      1.1  christos         }
    483      1.1  christos 
    484      1.1  christos         /*
    485      1.1  christos          * Otherwise this is for an old locally-initiated stream which we
    486      1.1  christos          * have subsequently deleted. Ignore the data; it may simply be a
    487      1.1  christos          * retransmission. We already take care of notifying the peer of the
    488      1.1  christos          * termination of the stream during the stream deletion lifecycle.
    489      1.1  christos          */
    490      1.1  christos         *result = NULL;
    491      1.1  christos     }
    492      1.1  christos 
    493      1.1  christos     return 1;
    494      1.1  christos }
    495      1.1  christos 
    496      1.1  christos static int depack_do_frame_stream(PACKET *pkt, QUIC_CHANNEL *ch,
    497  1.1.1.2  christos     OSSL_QRX_PKT *parent_pkt,
    498  1.1.1.2  christos     OSSL_ACKM_RX_PKT *ackm_data,
    499  1.1.1.2  christos     uint64_t frame_type,
    500  1.1.1.2  christos     uint64_t *datalen)
    501      1.1  christos {
    502      1.1  christos     OSSL_QUIC_FRAME_STREAM frame_data;
    503      1.1  christos     QUIC_STREAM *stream;
    504      1.1  christos     uint64_t fce;
    505      1.1  christos     size_t rs_avail;
    506      1.1  christos     int rs_fin = 0;
    507      1.1  christos 
    508      1.1  christos     *datalen = 0;
    509      1.1  christos 
    510      1.1  christos     if (!ossl_quic_wire_decode_frame_stream(pkt, 0, &frame_data)) {
    511      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    512  1.1.1.2  christos             OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
    513  1.1.1.2  christos             frame_type,
    514  1.1.1.2  christos             "decode error");
    515      1.1  christos         return 0;
    516      1.1  christos     }
    517      1.1  christos 
    518      1.1  christos     if (!depack_do_implicit_stream_create(ch, frame_data.stream_id,
    519  1.1.1.2  christos             frame_type, &stream))
    520      1.1  christos         return 0; /* protocol error raised by above call */
    521      1.1  christos 
    522      1.1  christos     if (stream == NULL)
    523      1.1  christos         /*
    524      1.1  christos          * Data for old stream which is not a protocol violation but should be
    525      1.1  christos          * ignored, so stop here.
    526      1.1  christos          */
    527      1.1  christos         return 1;
    528      1.1  christos 
    529      1.1  christos     if (!ossl_quic_stream_has_recv(stream)) {
    530      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    531  1.1.1.2  christos             OSSL_QUIC_ERR_STREAM_STATE_ERROR,
    532  1.1.1.2  christos             frame_type,
    533  1.1.1.2  christos             "STREAM frame for TX only "
    534  1.1.1.2  christos             "stream");
    535      1.1  christos         return 0;
    536      1.1  christos     }
    537      1.1  christos 
    538      1.1  christos     /* Notify stream flow controller. */
    539      1.1  christos     if (!ossl_quic_rxfc_on_rx_stream_frame(&stream->rxfc,
    540  1.1.1.2  christos             frame_data.offset + frame_data.len,
    541  1.1.1.2  christos             frame_data.is_fin)) {
    542      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    543  1.1.1.2  christos             OSSL_QUIC_ERR_INTERNAL_ERROR,
    544  1.1.1.2  christos             frame_type,
    545  1.1.1.2  christos             "internal error (flow control)");
    546      1.1  christos         return 0;
    547      1.1  christos     }
    548      1.1  christos 
    549      1.1  christos     /* Has a flow control error occurred? */
    550      1.1  christos     fce = ossl_quic_rxfc_get_error(&stream->rxfc, 0);
    551      1.1  christos     if (fce != OSSL_QUIC_ERR_NO_ERROR) {
    552      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    553  1.1.1.2  christos             fce,
    554  1.1.1.2  christos             frame_type,
    555  1.1.1.2  christos             "flow control violation");
    556      1.1  christos         return 0;
    557      1.1  christos     }
    558      1.1  christos 
    559      1.1  christos     switch (stream->recv_state) {
    560      1.1  christos     case QUIC_RSTREAM_STATE_RECV:
    561      1.1  christos     case QUIC_RSTREAM_STATE_SIZE_KNOWN:
    562      1.1  christos         /*
    563      1.1  christos          * It only makes sense to process incoming STREAM frames in these
    564      1.1  christos          * states.
    565      1.1  christos          */
    566      1.1  christos         break;
    567      1.1  christos 
    568      1.1  christos     case QUIC_RSTREAM_STATE_DATA_RECVD:
    569      1.1  christos     case QUIC_RSTREAM_STATE_DATA_READ:
    570      1.1  christos     case QUIC_RSTREAM_STATE_RESET_RECVD:
    571      1.1  christos     case QUIC_RSTREAM_STATE_RESET_READ:
    572      1.1  christos     default:
    573      1.1  christos         /*
    574      1.1  christos          * We have no use for STREAM frames once the receive part reaches any of
    575      1.1  christos          * these states, so just ignore.
    576      1.1  christos          */
    577      1.1  christos         return 1;
    578      1.1  christos     }
    579      1.1  christos 
    580      1.1  christos     /* If we are in RECV, auto-transition to SIZE_KNOWN on FIN. */
    581      1.1  christos     if (frame_data.is_fin
    582      1.1  christos         && !ossl_quic_stream_recv_get_final_size(stream, NULL)) {
    583      1.1  christos 
    584      1.1  christos         /* State was already checked above, so can't fail. */
    585      1.1  christos         ossl_quic_stream_map_notify_size_known_recv_part(&ch->qsm, stream,
    586  1.1.1.2  christos             frame_data.offset
    587  1.1.1.2  christos                 + frame_data.len);
    588      1.1  christos     }
    589      1.1  christos 
    590      1.1  christos     /*
    591      1.1  christos      * If we requested STOP_SENDING do not bother buffering the data. Note that
    592      1.1  christos      * this must happen after RXFC checks above as even if we sent STOP_SENDING
    593      1.1  christos      * we must still enforce correct flow control (RFC 9000 s. 3.5).
    594      1.1  christos      */
    595      1.1  christos     if (stream->stop_sending)
    596      1.1  christos         return 1; /* not an error - packet reordering, etc. */
    597      1.1  christos 
    598      1.1  christos     /*
    599      1.1  christos      * The receive stream buffer may or may not choose to consume the data
    600      1.1  christos      * without copying by reffing the OSSL_QRX_PKT. In this case
    601      1.1  christos      * ossl_qrx_pkt_release() will be eventually called when the data is no
    602      1.1  christos      * longer needed.
    603      1.1  christos      *
    604      1.1  christos      * It is OK for the peer to send us a zero-length non-FIN STREAM frame,
    605      1.1  christos      * which is a no-op, aside from the fact that it ensures the stream exists.
    606      1.1  christos      * In this case we have nothing to report to the receive buffer.
    607      1.1  christos      */
    608      1.1  christos     if ((frame_data.len > 0 || frame_data.is_fin)
    609      1.1  christos         && !ossl_quic_rstream_queue_data(stream->rstream, parent_pkt,
    610  1.1.1.2  christos             frame_data.offset,
    611  1.1.1.2  christos             frame_data.data,
    612  1.1.1.2  christos             frame_data.len,
    613  1.1.1.2  christos             frame_data.is_fin)) {
    614  1.1.1.2  christos         ossl_quic_channel_raise_protocol_error(ch,
    615  1.1.1.2  christos             OSSL_QUIC_ERR_INTERNAL_ERROR,
    616  1.1.1.2  christos             frame_type,
    617  1.1.1.2  christos             "internal error (rstream queue)");
    618      1.1  christos         return 0;
    619      1.1  christos     }
    620      1.1  christos 
    621      1.1  christos     /*
    622      1.1  christos      * rs_fin will be 1 only if we can read all data up to and including the FIN
    623      1.1  christos      * without any gaps before it; this implies we have received all data. Avoid
    624      1.1  christos      * calling ossl_quic_rstream_available() where it is not necessary as it is
    625      1.1  christos      * more expensive.
    626      1.1  christos      */
    627      1.1  christos     if (stream->recv_state == QUIC_RSTREAM_STATE_SIZE_KNOWN
    628      1.1  christos         && !ossl_quic_rstream_available(stream->rstream, &rs_avail, &rs_fin)) {
    629      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    630  1.1.1.2  christos             OSSL_QUIC_ERR_INTERNAL_ERROR,
    631  1.1.1.2  christos             frame_type,
    632  1.1.1.2  christos             "internal error (rstream available)");
    633      1.1  christos         return 0;
    634      1.1  christos     }
    635      1.1  christos 
    636      1.1  christos     if (rs_fin)
    637      1.1  christos         ossl_quic_stream_map_notify_totally_received(&ch->qsm, stream);
    638      1.1  christos 
    639      1.1  christos     *datalen = frame_data.len;
    640      1.1  christos 
    641      1.1  christos     return 1;
    642      1.1  christos }
    643      1.1  christos 
    644      1.1  christos static void update_streams(QUIC_STREAM *s, void *arg)
    645      1.1  christos {
    646      1.1  christos     QUIC_CHANNEL *ch = arg;
    647      1.1  christos 
    648      1.1  christos     ossl_quic_stream_map_update_state(&ch->qsm, s);
    649      1.1  christos }
    650      1.1  christos 
    651      1.1  christos static void update_streams_bidi(QUIC_STREAM *s, void *arg)
    652      1.1  christos {
    653      1.1  christos     QUIC_CHANNEL *ch = arg;
    654      1.1  christos 
    655      1.1  christos     if (!ossl_quic_stream_is_bidi(s))
    656      1.1  christos         return;
    657      1.1  christos 
    658      1.1  christos     ossl_quic_stream_map_update_state(&ch->qsm, s);
    659      1.1  christos }
    660      1.1  christos 
    661      1.1  christos static void update_streams_uni(QUIC_STREAM *s, void *arg)
    662      1.1  christos {
    663      1.1  christos     QUIC_CHANNEL *ch = arg;
    664      1.1  christos 
    665      1.1  christos     if (ossl_quic_stream_is_bidi(s))
    666      1.1  christos         return;
    667      1.1  christos 
    668      1.1  christos     ossl_quic_stream_map_update_state(&ch->qsm, s);
    669      1.1  christos }
    670      1.1  christos 
    671      1.1  christos static int depack_do_frame_max_data(PACKET *pkt, QUIC_CHANNEL *ch,
    672  1.1.1.2  christos     OSSL_ACKM_RX_PKT *ackm_data)
    673      1.1  christos {
    674      1.1  christos     uint64_t max_data = 0;
    675      1.1  christos 
    676      1.1  christos     if (!ossl_quic_wire_decode_frame_max_data(pkt, &max_data)) {
    677      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    678  1.1.1.2  christos             OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
    679  1.1.1.2  christos             OSSL_QUIC_FRAME_TYPE_MAX_DATA,
    680  1.1.1.2  christos             "decode error");
    681      1.1  christos         return 0;
    682      1.1  christos     }
    683      1.1  christos 
    684      1.1  christos     ossl_quic_txfc_bump_cwm(&ch->conn_txfc, max_data);
    685      1.1  christos     ossl_quic_stream_map_visit(&ch->qsm, update_streams, ch);
    686      1.1  christos     return 1;
    687      1.1  christos }
    688      1.1  christos 
    689      1.1  christos static int depack_do_frame_max_stream_data(PACKET *pkt,
    690  1.1.1.2  christos     QUIC_CHANNEL *ch,
    691  1.1.1.2  christos     OSSL_ACKM_RX_PKT *ackm_data)
    692      1.1  christos {
    693      1.1  christos     uint64_t stream_id = 0;
    694      1.1  christos     uint64_t max_stream_data = 0;
    695      1.1  christos     QUIC_STREAM *stream;
    696      1.1  christos 
    697      1.1  christos     if (!ossl_quic_wire_decode_frame_max_stream_data(pkt, &stream_id,
    698  1.1.1.2  christos             &max_stream_data)) {
    699      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    700  1.1.1.2  christos             OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
    701  1.1.1.2  christos             OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA,
    702  1.1.1.2  christos             "decode error");
    703      1.1  christos         return 0;
    704      1.1  christos     }
    705      1.1  christos 
    706      1.1  christos     if (!depack_do_implicit_stream_create(ch, stream_id,
    707  1.1.1.2  christos             OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA,
    708  1.1.1.2  christos             &stream))
    709      1.1  christos         return 0; /* error already raised for us */
    710      1.1  christos 
    711      1.1  christos     if (stream == NULL)
    712      1.1  christos         return 1; /* old deleted stream, not a protocol violation, ignore */
    713      1.1  christos 
    714      1.1  christos     if (!ossl_quic_stream_has_send(stream)) {
    715      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    716  1.1.1.2  christos             OSSL_QUIC_ERR_STREAM_STATE_ERROR,
    717  1.1.1.2  christos             OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA,
    718  1.1.1.2  christos             "MAX_STREAM_DATA for TX only "
    719  1.1.1.2  christos             "stream");
    720      1.1  christos         return 0;
    721      1.1  christos     }
    722      1.1  christos 
    723      1.1  christos     ossl_quic_txfc_bump_cwm(&stream->txfc, max_stream_data);
    724      1.1  christos     ossl_quic_stream_map_update_state(&ch->qsm, stream);
    725      1.1  christos     return 1;
    726      1.1  christos }
    727      1.1  christos 
    728      1.1  christos static int depack_do_frame_max_streams(PACKET *pkt,
    729  1.1.1.2  christos     QUIC_CHANNEL *ch,
    730  1.1.1.2  christos     OSSL_ACKM_RX_PKT *ackm_data,
    731  1.1.1.2  christos     uint64_t frame_type)
    732      1.1  christos {
    733      1.1  christos     uint64_t max_streams = 0;
    734      1.1  christos 
    735      1.1  christos     if (!ossl_quic_wire_decode_frame_max_streams(pkt, &max_streams)) {
    736      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    737  1.1.1.2  christos             OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
    738  1.1.1.2  christos             frame_type,
    739  1.1.1.2  christos             "decode error");
    740      1.1  christos         return 0;
    741      1.1  christos     }
    742      1.1  christos 
    743      1.1  christos     if (max_streams > (((uint64_t)1) << 60)) {
    744      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    745  1.1.1.2  christos             OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
    746  1.1.1.2  christos             frame_type,
    747  1.1.1.2  christos             "invalid max streams value");
    748      1.1  christos         return 0;
    749      1.1  christos     }
    750      1.1  christos 
    751      1.1  christos     switch (frame_type) {
    752      1.1  christos     case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI:
    753      1.1  christos         if (max_streams > ch->max_local_streams_bidi)
    754      1.1  christos             ch->max_local_streams_bidi = max_streams;
    755      1.1  christos 
    756      1.1  christos         /* Some streams may now be able to send. */
    757      1.1  christos         ossl_quic_stream_map_visit(&ch->qsm, update_streams_bidi, ch);
    758      1.1  christos         break;
    759      1.1  christos     case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI:
    760      1.1  christos         if (max_streams > ch->max_local_streams_uni)
    761      1.1  christos             ch->max_local_streams_uni = max_streams;
    762      1.1  christos 
    763      1.1  christos         /* Some streams may now be able to send. */
    764      1.1  christos         ossl_quic_stream_map_visit(&ch->qsm, update_streams_uni, ch);
    765      1.1  christos         break;
    766      1.1  christos     default:
    767      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    768  1.1.1.2  christos             OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
    769  1.1.1.2  christos             frame_type,
    770  1.1.1.2  christos             "decode error");
    771      1.1  christos         return 0;
    772      1.1  christos     }
    773      1.1  christos 
    774      1.1  christos     return 1;
    775      1.1  christos }
    776      1.1  christos 
    777      1.1  christos static int depack_do_frame_data_blocked(PACKET *pkt,
    778  1.1.1.2  christos     QUIC_CHANNEL *ch,
    779  1.1.1.2  christos     OSSL_ACKM_RX_PKT *ackm_data)
    780      1.1  christos {
    781      1.1  christos     uint64_t max_data = 0;
    782      1.1  christos 
    783      1.1  christos     if (!ossl_quic_wire_decode_frame_data_blocked(pkt, &max_data)) {
    784      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    785  1.1.1.2  christos             OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
    786  1.1.1.2  christos             OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED,
    787  1.1.1.2  christos             "decode error");
    788      1.1  christos         return 0;
    789      1.1  christos     }
    790      1.1  christos 
    791      1.1  christos     /* No-op - informative/debugging frame. */
    792      1.1  christos     return 1;
    793      1.1  christos }
    794      1.1  christos 
    795      1.1  christos static int depack_do_frame_stream_data_blocked(PACKET *pkt,
    796  1.1.1.2  christos     QUIC_CHANNEL *ch,
    797  1.1.1.2  christos     OSSL_ACKM_RX_PKT *ackm_data)
    798      1.1  christos {
    799      1.1  christos     uint64_t stream_id = 0;
    800      1.1  christos     uint64_t max_data = 0;
    801      1.1  christos     QUIC_STREAM *stream;
    802      1.1  christos 
    803      1.1  christos     if (!ossl_quic_wire_decode_frame_stream_data_blocked(pkt, &stream_id,
    804  1.1.1.2  christos             &max_data)) {
    805      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    806  1.1.1.2  christos             OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
    807  1.1.1.2  christos             OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED,
    808  1.1.1.2  christos             "decode error");
    809      1.1  christos         return 0;
    810      1.1  christos     }
    811      1.1  christos 
    812      1.1  christos     /*
    813      1.1  christos      * This is an informative/debugging frame, so we don't have to do anything,
    814      1.1  christos      * but it does trigger stream creation.
    815      1.1  christos      */
    816      1.1  christos     if (!depack_do_implicit_stream_create(ch, stream_id,
    817  1.1.1.2  christos             OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED,
    818  1.1.1.2  christos             &stream))
    819      1.1  christos         return 0; /* error already raised for us */
    820      1.1  christos 
    821      1.1  christos     if (stream == NULL)
    822      1.1  christos         return 1; /* old deleted stream, not a protocol violation, ignore */
    823      1.1  christos 
    824      1.1  christos     if (!ossl_quic_stream_has_recv(stream)) {
    825      1.1  christos         /*
    826      1.1  christos          * RFC 9000 s. 19.14: "An endpoint that receives a STREAM_DATA_BLOCKED
    827      1.1  christos          * frame for a send-only stream MUST terminate the connection with error
    828      1.1  christos          * STREAM_STATE_ERROR."
    829      1.1  christos          */
    830      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    831  1.1.1.2  christos             OSSL_QUIC_ERR_STREAM_STATE_ERROR,
    832  1.1.1.2  christos             OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED,
    833  1.1.1.2  christos             "STREAM_DATA_BLOCKED frame for "
    834  1.1.1.2  christos             "TX only stream");
    835      1.1  christos         return 0;
    836      1.1  christos     }
    837      1.1  christos 
    838      1.1  christos     /* No-op - informative/debugging frame. */
    839      1.1  christos     return 1;
    840      1.1  christos }
    841      1.1  christos 
    842      1.1  christos static int depack_do_frame_streams_blocked(PACKET *pkt,
    843  1.1.1.2  christos     QUIC_CHANNEL *ch,
    844  1.1.1.2  christos     OSSL_ACKM_RX_PKT *ackm_data,
    845  1.1.1.2  christos     uint64_t frame_type)
    846      1.1  christos {
    847      1.1  christos     uint64_t max_data = 0;
    848      1.1  christos 
    849      1.1  christos     if (!ossl_quic_wire_decode_frame_streams_blocked(pkt, &max_data)) {
    850      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    851  1.1.1.2  christos             OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
    852  1.1.1.2  christos             frame_type,
    853  1.1.1.2  christos             "decode error");
    854      1.1  christos         return 0;
    855      1.1  christos     }
    856      1.1  christos 
    857      1.1  christos     if (max_data > (((uint64_t)1) << 60)) {
    858      1.1  christos         /*
    859      1.1  christos          * RFC 9000 s. 19.14: "This value cannot exceed 2**60, as it is not
    860      1.1  christos          * possible to encode stream IDs larger than 2**62 - 1. Receipt of a
    861      1.1  christos          * frame that encodes a larger stream ID MUST be treated as a connection
    862      1.1  christos          * error of type STREAM_LIMIT_ERROR or FRAME_ENCODING_ERROR."
    863      1.1  christos          */
    864      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    865  1.1.1.2  christos             OSSL_QUIC_ERR_STREAM_LIMIT_ERROR,
    866  1.1.1.2  christos             frame_type,
    867  1.1.1.2  christos             "invalid stream count limit");
    868      1.1  christos         return 0;
    869      1.1  christos     }
    870      1.1  christos 
    871      1.1  christos     /* No-op - informative/debugging frame. */
    872      1.1  christos     return 1;
    873      1.1  christos }
    874      1.1  christos 
    875      1.1  christos static int depack_do_frame_new_conn_id(PACKET *pkt,
    876  1.1.1.2  christos     QUIC_CHANNEL *ch,
    877  1.1.1.2  christos     OSSL_ACKM_RX_PKT *ackm_data)
    878      1.1  christos {
    879      1.1  christos     OSSL_QUIC_FRAME_NEW_CONN_ID frame_data;
    880      1.1  christos 
    881      1.1  christos     if (!ossl_quic_wire_decode_frame_new_conn_id(pkt, &frame_data)) {
    882      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    883  1.1.1.2  christos             OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
    884  1.1.1.2  christos             OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID,
    885  1.1.1.2  christos             "decode error");
    886      1.1  christos         return 0;
    887      1.1  christos     }
    888      1.1  christos 
    889      1.1  christos     ossl_quic_channel_on_new_conn_id(ch, &frame_data);
    890      1.1  christos 
    891      1.1  christos     return 1;
    892      1.1  christos }
    893      1.1  christos 
    894      1.1  christos static int depack_do_frame_retire_conn_id(PACKET *pkt,
    895  1.1.1.2  christos     QUIC_CHANNEL *ch,
    896  1.1.1.2  christos     OSSL_ACKM_RX_PKT *ackm_data)
    897      1.1  christos {
    898      1.1  christos     uint64_t seq_num;
    899      1.1  christos 
    900      1.1  christos     if (!ossl_quic_wire_decode_frame_retire_conn_id(pkt, &seq_num)) {
    901      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    902  1.1.1.2  christos             OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
    903  1.1.1.2  christos             OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID,
    904  1.1.1.2  christos             "decode error");
    905      1.1  christos         return 0;
    906      1.1  christos     }
    907      1.1  christos 
    908      1.1  christos     /*
    909      1.1  christos      * RFC 9000 s. 19.16: "An endpoint cannot send this frame if it was provided
    910      1.1  christos      * with a zero-length connection ID by its peer. An endpoint that provides a
    911      1.1  christos      * zero-length connection ID MUST treat receipt of a RETIRE_CONNECTION_ID
    912      1.1  christos      * frame as a connection error of type PROTOCOL_VIOLATION."
    913      1.1  christos      *
    914      1.1  christos      * Since we always use a zero-length SCID as a client, there is no case
    915      1.1  christos      * where it is valid for a server to send this. Our server support is
    916      1.1  christos      * currently non-conformant and for internal testing use; simply handle it
    917      1.1  christos      * as a no-op in this case.
    918      1.1  christos      *
    919      1.1  christos      * TODO(QUIC FUTURE): Revise and implement correctly for server support.
    920      1.1  christos      */
    921      1.1  christos     if (!ch->is_server) {
    922      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    923  1.1.1.2  christos             OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
    924  1.1.1.2  christos             OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID,
    925  1.1.1.2  christos             "conn has zero-length CID");
    926      1.1  christos         return 0;
    927      1.1  christos     }
    928      1.1  christos 
    929      1.1  christos     return 1;
    930      1.1  christos }
    931      1.1  christos 
    932      1.1  christos static void free_path_response(unsigned char *buf, size_t buf_len, void *arg)
    933      1.1  christos {
    934  1.1.1.4  christos     QUIC_CHANNEL *ch = (QUIC_CHANNEL *)arg;
    935  1.1.1.4  christos 
    936  1.1.1.4  christos     assert(ch->path_response_limit > 0);
    937  1.1.1.4  christos 
    938  1.1.1.4  christos     ch->path_response_limit--;
    939  1.1.1.4  christos 
    940  1.1.1.4  christos     /*
    941  1.1.1.4  christos      * Assume path response frame is being freed on behalf of
    942  1.1.1.4  christos      * finished TX operation. This is for unit testing purposes
    943  1.1.1.4  christos      * only. The counter is also bumped when channel is being
    944  1.1.1.4  christos      * destroyed and CFQ (control frame queue) is freed.
    945  1.1.1.4  christos      * This currently does not matter for check_pc_flood
    946  1.1.1.4  christos      * in test/radix/quic_tests.c.
    947  1.1.1.4  christos      */
    948  1.1.1.4  christos     ch->path_response_tx++;
    949  1.1.1.4  christos 
    950      1.1  christos     OPENSSL_free(buf);
    951      1.1  christos }
    952      1.1  christos 
    953      1.1  christos static int depack_do_frame_path_challenge(PACKET *pkt,
    954  1.1.1.2  christos     QUIC_CHANNEL *ch,
    955  1.1.1.2  christos     OSSL_ACKM_RX_PKT *ackm_data)
    956      1.1  christos {
    957      1.1  christos     uint64_t frame_data = 0;
    958      1.1  christos     unsigned char *encoded = NULL;
    959      1.1  christos     size_t encoded_len;
    960      1.1  christos     WPACKET wpkt;
    961      1.1  christos 
    962      1.1  christos     if (!ossl_quic_wire_decode_frame_path_challenge(pkt, &frame_data)) {
    963      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
    964  1.1.1.2  christos             OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
    965  1.1.1.2  christos             OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE,
    966  1.1.1.2  christos             "decode error");
    967      1.1  christos         return 0;
    968      1.1  christos     }
    969      1.1  christos 
    970  1.1.1.4  christos     if (ch->seen_path_challenge == 0
    971  1.1.1.4  christos         && ch->path_response_limit < QUIC_PATH_RESPONSE_QLEN) {
    972  1.1.1.4  christos         /*
    973  1.1.1.4  christos          * RFC 9000 s. 8.2.2: On receiving a PATH_CHALLENGE frame, an endpoint
    974  1.1.1.4  christos          * MUST respond by echoing the data contained in the PATH_CHALLENGE
    975  1.1.1.4  christos          * frame in a PATH_RESPONSE frame.
    976  1.1.1.4  christos          *
    977  1.1.1.4  christos          * TODO(QUIC FUTURE): We should try to avoid allocation here in the
    978  1.1.1.4  christos          * future.
    979  1.1.1.4  christos          */
    980  1.1.1.4  christos         encoded_len = sizeof(uint64_t) + 1;
    981  1.1.1.4  christos         if ((encoded = OPENSSL_malloc(encoded_len)) == NULL)
    982  1.1.1.4  christos             goto err;
    983  1.1.1.4  christos 
    984  1.1.1.4  christos         if (!WPACKET_init_static_len(&wpkt, encoded, encoded_len, 0))
    985  1.1.1.4  christos             goto err;
    986  1.1.1.4  christos 
    987  1.1.1.4  christos         if (!ossl_quic_wire_encode_frame_path_response(&wpkt, frame_data)) {
    988  1.1.1.4  christos             WPACKET_cleanup(&wpkt);
    989  1.1.1.4  christos             goto err;
    990  1.1.1.4  christos         }
    991      1.1  christos 
    992  1.1.1.4  christos         WPACKET_finish(&wpkt);
    993      1.1  christos 
    994  1.1.1.4  christos         if (!ossl_quic_cfq_add_frame(ch->cfq, 0, QUIC_PN_SPACE_APP,
    995  1.1.1.4  christos                 OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE,
    996  1.1.1.4  christos                 QUIC_CFQ_ITEM_FLAG_UNRELIABLE,
    997  1.1.1.4  christos                 encoded, encoded_len,
    998  1.1.1.4  christos                 free_path_response, ch))
    999  1.1.1.4  christos             goto err;
   1000  1.1.1.4  christos         ch->seen_path_challenge = 1;
   1001  1.1.1.4  christos         ch->path_response_limit++;
   1002  1.1.1.4  christos     }
   1003  1.1.1.4  christos 
   1004  1.1.1.4  christos     ch->path_challenge_rx++;
   1005      1.1  christos 
   1006      1.1  christos     return 1;
   1007      1.1  christos 
   1008      1.1  christos err:
   1009      1.1  christos     OPENSSL_free(encoded);
   1010      1.1  christos     ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_INTERNAL_ERROR,
   1011  1.1.1.2  christos         OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE,
   1012  1.1.1.2  christos         "internal error");
   1013      1.1  christos     return 0;
   1014      1.1  christos }
   1015      1.1  christos 
   1016      1.1  christos static int depack_do_frame_path_response(PACKET *pkt,
   1017  1.1.1.2  christos     QUIC_CHANNEL *ch,
   1018  1.1.1.2  christos     OSSL_ACKM_RX_PKT *ackm_data)
   1019      1.1  christos {
   1020      1.1  christos     uint64_t frame_data = 0;
   1021      1.1  christos 
   1022      1.1  christos     if (!ossl_quic_wire_decode_frame_path_response(pkt, &frame_data)) {
   1023      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
   1024  1.1.1.2  christos             OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
   1025  1.1.1.2  christos             OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE,
   1026  1.1.1.2  christos             "decode error");
   1027      1.1  christos         return 0;
   1028      1.1  christos     }
   1029      1.1  christos 
   1030      1.1  christos     /* TODO(QUIC MULTIPATH): ADD CODE to send |frame_data| to the ch manager */
   1031      1.1  christos 
   1032      1.1  christos     return 1;
   1033      1.1  christos }
   1034      1.1  christos 
   1035      1.1  christos static int depack_do_frame_conn_close(PACKET *pkt, QUIC_CHANNEL *ch,
   1036  1.1.1.2  christos     uint64_t frame_type)
   1037      1.1  christos {
   1038      1.1  christos     OSSL_QUIC_FRAME_CONN_CLOSE frame_data;
   1039      1.1  christos 
   1040      1.1  christos     if (!ossl_quic_wire_decode_frame_conn_close(pkt, &frame_data)) {
   1041      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
   1042  1.1.1.2  christos             OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
   1043  1.1.1.2  christos             frame_type,
   1044  1.1.1.2  christos             "decode error");
   1045      1.1  christos         return 0;
   1046      1.1  christos     }
   1047      1.1  christos 
   1048      1.1  christos     ossl_quic_channel_on_remote_conn_close(ch, &frame_data);
   1049      1.1  christos     return 1;
   1050      1.1  christos }
   1051      1.1  christos 
   1052      1.1  christos static int depack_do_frame_handshake_done(PACKET *pkt,
   1053  1.1.1.2  christos     QUIC_CHANNEL *ch,
   1054  1.1.1.2  christos     OSSL_ACKM_RX_PKT *ackm_data)
   1055      1.1  christos {
   1056      1.1  christos     if (!ossl_quic_wire_decode_frame_handshake_done(pkt)) {
   1057      1.1  christos         /* This can fail only with an internal error. */
   1058      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
   1059  1.1.1.2  christos             OSSL_QUIC_ERR_INTERNAL_ERROR,
   1060  1.1.1.2  christos             OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE,
   1061  1.1.1.2  christos             "internal error (decode frame handshake done)");
   1062      1.1  christos         return 0;
   1063      1.1  christos     }
   1064      1.1  christos 
   1065      1.1  christos     ossl_quic_channel_on_handshake_confirmed(ch);
   1066      1.1  christos     return 1;
   1067      1.1  christos }
   1068      1.1  christos 
   1069      1.1  christos /* Main frame processor */
   1070      1.1  christos 
   1071      1.1  christos static int depack_process_frames(QUIC_CHANNEL *ch, PACKET *pkt,
   1072  1.1.1.2  christos     OSSL_QRX_PKT *parent_pkt, uint32_t enc_level,
   1073  1.1.1.2  christos     OSSL_TIME received, OSSL_ACKM_RX_PKT *ackm_data)
   1074      1.1  christos {
   1075      1.1  christos     uint32_t pkt_type = parent_pkt->hdr->type;
   1076      1.1  christos     uint32_t packet_space = ossl_quic_enc_level_to_pn_space(enc_level);
   1077      1.1  christos 
   1078      1.1  christos     if (PACKET_remaining(pkt) == 0) {
   1079      1.1  christos         /*
   1080      1.1  christos          * RFC 9000 s. 12.4: An endpoint MUST treat receipt of a packet
   1081      1.1  christos          * containing no frames as a connection error of type
   1082      1.1  christos          * PROTOCOL_VIOLATION.
   1083      1.1  christos          */
   1084      1.1  christos         ossl_quic_channel_raise_protocol_error(ch,
   1085  1.1.1.2  christos             OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
   1086  1.1.1.2  christos             0,
   1087  1.1.1.2  christos             "empty packet payload");
   1088      1.1  christos         return 0;
   1089      1.1  christos     }
   1090      1.1  christos 
   1091      1.1  christos     while (PACKET_remaining(pkt) > 0) {
   1092      1.1  christos         int was_minimal;
   1093      1.1  christos         uint64_t frame_type;
   1094      1.1  christos         const unsigned char *sof = NULL;
   1095      1.1  christos         uint64_t datalen = 0;
   1096      1.1  christos 
   1097      1.1  christos         if (ch->msg_callback != NULL)
   1098      1.1  christos             sof = PACKET_data(pkt);
   1099      1.1  christos 
   1100      1.1  christos         if (!ossl_quic_wire_peek_frame_header(pkt, &frame_type, &was_minimal)) {
   1101      1.1  christos             ossl_quic_channel_raise_protocol_error(ch,
   1102  1.1.1.2  christos                 OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
   1103  1.1.1.2  christos                 0,
   1104  1.1.1.2  christos                 "malformed frame header");
   1105      1.1  christos             return 0;
   1106      1.1  christos         }
   1107      1.1  christos 
   1108      1.1  christos         if (!was_minimal) {
   1109      1.1  christos             ossl_quic_channel_raise_protocol_error(ch,
   1110  1.1.1.2  christos                 OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
   1111  1.1.1.2  christos                 frame_type,
   1112  1.1.1.2  christos                 "non-minimal frame type encoding");
   1113      1.1  christos             return 0;
   1114      1.1  christos         }
   1115      1.1  christos 
   1116      1.1  christos         /*
   1117      1.1  christos          * There are only a few frame types which are not ACK-eliciting. Handle
   1118      1.1  christos          * these centrally to make error handling cases more resilient, as we
   1119      1.1  christos          * should tell the ACKM about an ACK-eliciting frame even if it was not
   1120      1.1  christos          * successfully handled.
   1121      1.1  christos          */
   1122      1.1  christos         switch (frame_type) {
   1123      1.1  christos         case OSSL_QUIC_FRAME_TYPE_PADDING:
   1124      1.1  christos         case OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN:
   1125      1.1  christos         case OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN:
   1126      1.1  christos         case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT:
   1127      1.1  christos         case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP:
   1128      1.1  christos             break;
   1129      1.1  christos         default:
   1130      1.1  christos             ackm_data->is_ack_eliciting = 1;
   1131      1.1  christos             break;
   1132      1.1  christos         }
   1133      1.1  christos 
   1134      1.1  christos         switch (frame_type) {
   1135      1.1  christos         case OSSL_QUIC_FRAME_TYPE_PING:
   1136      1.1  christos             /* Allowed in all packet types */
   1137      1.1  christos             if (!depack_do_frame_ping(pkt, ch, enc_level, ackm_data))
   1138      1.1  christos                 return 0;
   1139      1.1  christos             break;
   1140      1.1  christos         case OSSL_QUIC_FRAME_TYPE_PADDING:
   1141      1.1  christos             /* Allowed in all packet types */
   1142      1.1  christos             if (!depack_do_frame_padding(pkt))
   1143      1.1  christos                 return 0;
   1144      1.1  christos             break;
   1145      1.1  christos 
   1146      1.1  christos         case OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN:
   1147      1.1  christos         case OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN:
   1148      1.1  christos             /* ACK frames are valid everywhere except in 0RTT packets */
   1149      1.1  christos             if (pkt_type == QUIC_PKT_TYPE_0RTT) {
   1150      1.1  christos                 ossl_quic_channel_raise_protocol_error(ch,
   1151  1.1.1.2  christos                     OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
   1152  1.1.1.2  christos                     frame_type,
   1153  1.1.1.2  christos                     "ACK not valid in 0-RTT");
   1154      1.1  christos                 return 0;
   1155      1.1  christos             }
   1156      1.1  christos             if (!depack_do_frame_ack(pkt, ch, packet_space, received,
   1157  1.1.1.2  christos                     frame_type, parent_pkt))
   1158      1.1  christos                 return 0;
   1159      1.1  christos             break;
   1160      1.1  christos 
   1161      1.1  christos         case OSSL_QUIC_FRAME_TYPE_RESET_STREAM:
   1162      1.1  christos             /* RESET_STREAM frames are valid in 0RTT and 1RTT packets */
   1163      1.1  christos             if (pkt_type != QUIC_PKT_TYPE_0RTT
   1164      1.1  christos                 && pkt_type != QUIC_PKT_TYPE_1RTT) {
   1165      1.1  christos                 ossl_quic_channel_raise_protocol_error(ch,
   1166  1.1.1.2  christos                     OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
   1167  1.1.1.2  christos                     frame_type,
   1168  1.1.1.2  christos                     "RESET_STREAM not valid in "
   1169  1.1.1.2  christos                     "INITIAL/HANDSHAKE");
   1170      1.1  christos                 return 0;
   1171      1.1  christos             }
   1172      1.1  christos             if (!depack_do_frame_reset_stream(pkt, ch, ackm_data))
   1173      1.1  christos                 return 0;
   1174      1.1  christos             break;
   1175      1.1  christos         case OSSL_QUIC_FRAME_TYPE_STOP_SENDING:
   1176      1.1  christos             /* STOP_SENDING frames are valid in 0RTT and 1RTT packets */
   1177      1.1  christos             if (pkt_type != QUIC_PKT_TYPE_0RTT
   1178      1.1  christos                 && pkt_type != QUIC_PKT_TYPE_1RTT) {
   1179      1.1  christos                 ossl_quic_channel_raise_protocol_error(ch,
   1180  1.1.1.2  christos                     OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
   1181  1.1.1.2  christos                     frame_type,
   1182  1.1.1.2  christos                     "STOP_SENDING not valid in "
   1183  1.1.1.2  christos                     "INITIAL/HANDSHAKE");
   1184      1.1  christos                 return 0;
   1185      1.1  christos             }
   1186      1.1  christos             if (!depack_do_frame_stop_sending(pkt, ch, ackm_data))
   1187      1.1  christos                 return 0;
   1188      1.1  christos             break;
   1189      1.1  christos         case OSSL_QUIC_FRAME_TYPE_CRYPTO:
   1190      1.1  christos             /* CRYPTO frames are valid everywhere except in 0RTT packets */
   1191      1.1  christos             if (pkt_type == QUIC_PKT_TYPE_0RTT) {
   1192      1.1  christos                 ossl_quic_channel_raise_protocol_error(ch,
   1193  1.1.1.2  christos                     OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
   1194  1.1.1.2  christos                     frame_type,
   1195  1.1.1.2  christos                     "CRYPTO frame not valid in 0-RTT");
   1196      1.1  christos                 return 0;
   1197      1.1  christos             }
   1198      1.1  christos             if (!depack_do_frame_crypto(pkt, ch, parent_pkt, ackm_data, &datalen))
   1199      1.1  christos                 return 0;
   1200      1.1  christos             break;
   1201      1.1  christos         case OSSL_QUIC_FRAME_TYPE_NEW_TOKEN:
   1202      1.1  christos             /* NEW_TOKEN frames are valid in 1RTT packets */
   1203      1.1  christos             if (pkt_type != QUIC_PKT_TYPE_1RTT) {
   1204      1.1  christos                 ossl_quic_channel_raise_protocol_error(ch,
   1205  1.1.1.2  christos                     OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
   1206  1.1.1.2  christos                     frame_type,
   1207  1.1.1.2  christos                     "NEW_TOKEN valid only in 1-RTT");
   1208      1.1  christos                 return 0;
   1209      1.1  christos             }
   1210      1.1  christos 
   1211      1.1  christos             /*
   1212      1.1  christos              * RFC 9000 s. 19.7: "A server MUST treat receipt of a NEW_TOKEN
   1213      1.1  christos              * frame as a connection error of type PROTOCOL_VIOLATION."
   1214      1.1  christos              */
   1215      1.1  christos             if (ch->is_server) {
   1216      1.1  christos                 ossl_quic_channel_raise_protocol_error(ch,
   1217  1.1.1.2  christos                     OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
   1218  1.1.1.2  christos                     frame_type,
   1219  1.1.1.2  christos                     "NEW_TOKEN can only be sent by a server");
   1220      1.1  christos                 return 0;
   1221      1.1  christos             }
   1222      1.1  christos 
   1223      1.1  christos             if (!depack_do_frame_new_token(pkt, ch, ackm_data))
   1224      1.1  christos                 return 0;
   1225      1.1  christos             break;
   1226      1.1  christos 
   1227      1.1  christos         case OSSL_QUIC_FRAME_TYPE_STREAM:
   1228      1.1  christos         case OSSL_QUIC_FRAME_TYPE_STREAM_FIN:
   1229      1.1  christos         case OSSL_QUIC_FRAME_TYPE_STREAM_LEN:
   1230      1.1  christos         case OSSL_QUIC_FRAME_TYPE_STREAM_LEN_FIN:
   1231      1.1  christos         case OSSL_QUIC_FRAME_TYPE_STREAM_OFF:
   1232      1.1  christos         case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_FIN:
   1233      1.1  christos         case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN:
   1234      1.1  christos         case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN_FIN:
   1235      1.1  christos             /* STREAM frames are valid in 0RTT and 1RTT packets */
   1236      1.1  christos             if (pkt_type != QUIC_PKT_TYPE_0RTT
   1237      1.1  christos                 && pkt_type != QUIC_PKT_TYPE_1RTT) {
   1238      1.1  christos                 ossl_quic_channel_raise_protocol_error(ch,
   1239  1.1.1.2  christos                     OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
   1240  1.1.1.2  christos                     frame_type,
   1241  1.1.1.2  christos                     "STREAM valid only in 0/1-RTT");
   1242      1.1  christos                 return 0;
   1243      1.1  christos             }
   1244      1.1  christos             if (!depack_do_frame_stream(pkt, ch, parent_pkt, ackm_data,
   1245  1.1.1.2  christos                     frame_type, &datalen))
   1246      1.1  christos                 return 0;
   1247      1.1  christos             break;
   1248      1.1  christos 
   1249      1.1  christos         case OSSL_QUIC_FRAME_TYPE_MAX_DATA:
   1250      1.1  christos             /* MAX_DATA frames are valid in 0RTT and 1RTT packets */
   1251      1.1  christos             if (pkt_type != QUIC_PKT_TYPE_0RTT
   1252      1.1  christos                 && pkt_type != QUIC_PKT_TYPE_1RTT) {
   1253      1.1  christos                 ossl_quic_channel_raise_protocol_error(ch,
   1254  1.1.1.2  christos                     OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
   1255  1.1.1.2  christos                     frame_type,
   1256  1.1.1.2  christos                     "MAX_DATA valid only in 0/1-RTT");
   1257      1.1  christos                 return 0;
   1258      1.1  christos             }
   1259      1.1  christos             if (!depack_do_frame_max_data(pkt, ch, ackm_data))
   1260      1.1  christos                 return 0;
   1261      1.1  christos             break;
   1262      1.1  christos         case OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA:
   1263      1.1  christos             /* MAX_STREAM_DATA frames are valid in 0RTT and 1RTT packets */
   1264      1.1  christos             if (pkt_type != QUIC_PKT_TYPE_0RTT
   1265      1.1  christos                 && pkt_type != QUIC_PKT_TYPE_1RTT) {
   1266      1.1  christos                 ossl_quic_channel_raise_protocol_error(ch,
   1267  1.1.1.2  christos                     OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
   1268  1.1.1.2  christos                     frame_type,
   1269  1.1.1.2  christos                     "MAX_STREAM_DATA valid only in 0/1-RTT");
   1270      1.1  christos                 return 0;
   1271      1.1  christos             }
   1272      1.1  christos             if (!depack_do_frame_max_stream_data(pkt, ch, ackm_data))
   1273      1.1  christos                 return 0;
   1274      1.1  christos             break;
   1275      1.1  christos 
   1276      1.1  christos         case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI:
   1277      1.1  christos         case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI:
   1278      1.1  christos             /* MAX_STREAMS frames are valid in 0RTT and 1RTT packets */
   1279      1.1  christos             if (pkt_type != QUIC_PKT_TYPE_0RTT
   1280      1.1  christos                 && pkt_type != QUIC_PKT_TYPE_1RTT) {
   1281      1.1  christos                 ossl_quic_channel_raise_protocol_error(ch,
   1282  1.1.1.2  christos                     OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
   1283  1.1.1.2  christos                     frame_type,
   1284  1.1.1.2  christos                     "MAX_STREAMS valid only in 0/1-RTT");
   1285      1.1  christos                 return 0;
   1286      1.1  christos             }
   1287      1.1  christos             if (!depack_do_frame_max_streams(pkt, ch, ackm_data,
   1288  1.1.1.2  christos                     frame_type))
   1289      1.1  christos                 return 0;
   1290      1.1  christos             break;
   1291      1.1  christos 
   1292      1.1  christos         case OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED:
   1293      1.1  christos             /* DATA_BLOCKED frames are valid in 0RTT and 1RTT packets */
   1294      1.1  christos             if (pkt_type != QUIC_PKT_TYPE_0RTT
   1295      1.1  christos                 && pkt_type != QUIC_PKT_TYPE_1RTT) {
   1296      1.1  christos                 ossl_quic_channel_raise_protocol_error(ch,
   1297  1.1.1.2  christos                     OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
   1298  1.1.1.2  christos                     frame_type,
   1299  1.1.1.2  christos                     "DATA_BLOCKED valid only in 0/1-RTT");
   1300      1.1  christos                 return 0;
   1301      1.1  christos             }
   1302      1.1  christos             if (!depack_do_frame_data_blocked(pkt, ch, ackm_data))
   1303      1.1  christos                 return 0;
   1304      1.1  christos             break;
   1305      1.1  christos         case OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED:
   1306      1.1  christos             /* STREAM_DATA_BLOCKED frames are valid in 0RTT and 1RTT packets */
   1307      1.1  christos             if (pkt_type != QUIC_PKT_TYPE_0RTT
   1308      1.1  christos                 && pkt_type != QUIC_PKT_TYPE_1RTT) {
   1309      1.1  christos                 ossl_quic_channel_raise_protocol_error(ch,
   1310  1.1.1.2  christos                     OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
   1311  1.1.1.2  christos                     frame_type,
   1312  1.1.1.2  christos                     "STREAM_DATA_BLOCKED valid only in 0/1-RTT");
   1313      1.1  christos                 return 0;
   1314      1.1  christos             }
   1315      1.1  christos             if (!depack_do_frame_stream_data_blocked(pkt, ch, ackm_data))
   1316      1.1  christos                 return 0;
   1317      1.1  christos             break;
   1318      1.1  christos 
   1319      1.1  christos         case OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI:
   1320      1.1  christos         case OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI:
   1321      1.1  christos             /* STREAMS_BLOCKED frames are valid in 0RTT and 1RTT packets */
   1322      1.1  christos             if (pkt_type != QUIC_PKT_TYPE_0RTT
   1323      1.1  christos                 && pkt_type != QUIC_PKT_TYPE_1RTT) {
   1324      1.1  christos                 ossl_quic_channel_raise_protocol_error(ch,
   1325  1.1.1.2  christos                     OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
   1326  1.1.1.2  christos                     frame_type,
   1327  1.1.1.2  christos                     "STREAMS valid only in 0/1-RTT");
   1328      1.1  christos                 return 0;
   1329      1.1  christos             }
   1330      1.1  christos             if (!depack_do_frame_streams_blocked(pkt, ch, ackm_data,
   1331  1.1.1.2  christos                     frame_type))
   1332      1.1  christos                 return 0;
   1333      1.1  christos             break;
   1334      1.1  christos 
   1335      1.1  christos         case OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID:
   1336      1.1  christos             /* NEW_CONN_ID frames are valid in 0RTT and 1RTT packets */
   1337      1.1  christos             if (pkt_type != QUIC_PKT_TYPE_0RTT
   1338      1.1  christos                 && pkt_type != QUIC_PKT_TYPE_1RTT) {
   1339      1.1  christos                 ossl_quic_channel_raise_protocol_error(ch,
   1340  1.1.1.2  christos                     OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
   1341  1.1.1.2  christos                     frame_type,
   1342  1.1.1.2  christos                     "NEW_CONN_ID valid only in 0/1-RTT");
   1343  1.1.1.3  christos                 return 0;
   1344      1.1  christos             }
   1345      1.1  christos             if (!depack_do_frame_new_conn_id(pkt, ch, ackm_data))
   1346      1.1  christos                 return 0;
   1347      1.1  christos             break;
   1348      1.1  christos         case OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID:
   1349      1.1  christos             /* RETIRE_CONN_ID frames are valid in 0RTT and 1RTT packets */
   1350      1.1  christos             if (pkt_type != QUIC_PKT_TYPE_0RTT
   1351      1.1  christos                 && pkt_type != QUIC_PKT_TYPE_1RTT) {
   1352      1.1  christos                 ossl_quic_channel_raise_protocol_error(ch,
   1353  1.1.1.2  christos                     OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
   1354  1.1.1.2  christos                     frame_type,
   1355  1.1.1.2  christos                     "RETIRE_CONN_ID valid only in 0/1-RTT");
   1356      1.1  christos                 return 0;
   1357      1.1  christos             }
   1358      1.1  christos             if (!depack_do_frame_retire_conn_id(pkt, ch, ackm_data))
   1359      1.1  christos                 return 0;
   1360      1.1  christos             break;
   1361      1.1  christos         case OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE:
   1362      1.1  christos             /* PATH_CHALLENGE frames are valid in 0RTT and 1RTT packets */
   1363      1.1  christos             if (pkt_type != QUIC_PKT_TYPE_0RTT
   1364      1.1  christos                 && pkt_type != QUIC_PKT_TYPE_1RTT) {
   1365      1.1  christos                 ossl_quic_channel_raise_protocol_error(ch,
   1366  1.1.1.2  christos                     OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
   1367  1.1.1.2  christos                     frame_type,
   1368  1.1.1.2  christos                     "PATH_CHALLENGE valid only in 0/1-RTT");
   1369      1.1  christos                 return 0;
   1370      1.1  christos             }
   1371      1.1  christos             if (!depack_do_frame_path_challenge(pkt, ch, ackm_data))
   1372      1.1  christos                 return 0;
   1373      1.1  christos 
   1374      1.1  christos             break;
   1375      1.1  christos         case OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE:
   1376      1.1  christos             /* PATH_RESPONSE frames are valid in 1RTT packets */
   1377      1.1  christos             if (pkt_type != QUIC_PKT_TYPE_1RTT) {
   1378      1.1  christos                 ossl_quic_channel_raise_protocol_error(ch,
   1379  1.1.1.2  christos                     OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
   1380  1.1.1.2  christos                     frame_type,
   1381  1.1.1.2  christos                     "PATH_CHALLENGE valid only in 1-RTT");
   1382      1.1  christos                 return 0;
   1383      1.1  christos             }
   1384      1.1  christos             if (!depack_do_frame_path_response(pkt, ch, ackm_data))
   1385      1.1  christos                 return 0;
   1386      1.1  christos             break;
   1387      1.1  christos 
   1388      1.1  christos         case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP:
   1389      1.1  christos             /* CONN_CLOSE_APP frames are valid in 0RTT and 1RTT packets */
   1390      1.1  christos             if (pkt_type != QUIC_PKT_TYPE_0RTT
   1391      1.1  christos                 && pkt_type != QUIC_PKT_TYPE_1RTT) {
   1392      1.1  christos                 ossl_quic_channel_raise_protocol_error(ch,
   1393  1.1.1.2  christos                     OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
   1394  1.1.1.2  christos                     frame_type,
   1395  1.1.1.2  christos                     "CONN_CLOSE (APP) valid only in 0/1-RTT");
   1396      1.1  christos                 return 0;
   1397      1.1  christos             }
   1398      1.1  christos             /* FALLTHRU */
   1399      1.1  christos         case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT:
   1400      1.1  christos             /* CONN_CLOSE_TRANSPORT frames are valid in all packets */
   1401      1.1  christos             if (!depack_do_frame_conn_close(pkt, ch, frame_type))
   1402      1.1  christos                 return 0;
   1403      1.1  christos             break;
   1404      1.1  christos 
   1405      1.1  christos         case OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE:
   1406      1.1  christos             /* HANDSHAKE_DONE frames are valid in 1RTT packets */
   1407      1.1  christos             if (pkt_type != QUIC_PKT_TYPE_1RTT) {
   1408      1.1  christos                 ossl_quic_channel_raise_protocol_error(ch,
   1409  1.1.1.2  christos                     OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
   1410  1.1.1.2  christos                     frame_type,
   1411  1.1.1.2  christos                     "HANDSHAKE_DONE valid only in 1-RTT");
   1412      1.1  christos                 return 0;
   1413      1.1  christos             }
   1414      1.1  christos             if (!depack_do_frame_handshake_done(pkt, ch, ackm_data))
   1415      1.1  christos                 return 0;
   1416      1.1  christos             break;
   1417      1.1  christos 
   1418      1.1  christos         default:
   1419      1.1  christos             /* Unknown frame type */
   1420      1.1  christos             ossl_quic_channel_raise_protocol_error(ch,
   1421  1.1.1.2  christos                 OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
   1422  1.1.1.2  christos                 frame_type,
   1423  1.1.1.2  christos                 "Unknown frame type received");
   1424      1.1  christos             return 0;
   1425      1.1  christos         }
   1426      1.1  christos 
   1427      1.1  christos         if (ch->msg_callback != NULL) {
   1428      1.1  christos             int ctype = SSL3_RT_QUIC_FRAME_FULL;
   1429      1.1  christos 
   1430      1.1  christos             size_t framelen = PACKET_data(pkt) - sof;
   1431      1.1  christos 
   1432      1.1  christos             if (frame_type == OSSL_QUIC_FRAME_TYPE_PADDING) {
   1433      1.1  christos                 ctype = SSL3_RT_QUIC_FRAME_PADDING;
   1434      1.1  christos             } else if (OSSL_QUIC_FRAME_TYPE_IS_STREAM(frame_type)
   1435  1.1.1.2  christos                 || frame_type == OSSL_QUIC_FRAME_TYPE_CRYPTO) {
   1436      1.1  christos                 ctype = SSL3_RT_QUIC_FRAME_HEADER;
   1437      1.1  christos                 framelen -= (size_t)datalen;
   1438      1.1  christos             }
   1439      1.1  christos 
   1440      1.1  christos             ch->msg_callback(0, OSSL_QUIC1_VERSION, ctype, sof, framelen,
   1441  1.1.1.2  christos                 ch->msg_callback_ssl, ch->msg_callback_arg);
   1442      1.1  christos         }
   1443      1.1  christos     }
   1444      1.1  christos 
   1445      1.1  christos     return 1;
   1446      1.1  christos }
   1447      1.1  christos 
   1448      1.1  christos QUIC_NEEDS_LOCK
   1449      1.1  christos int ossl_quic_handle_frames(QUIC_CHANNEL *ch, OSSL_QRX_PKT *qpacket)
   1450      1.1  christos {
   1451      1.1  christos     PACKET pkt;
   1452      1.1  christos     OSSL_ACKM_RX_PKT ackm_data;
   1453      1.1  christos     uint32_t enc_level;
   1454      1.1  christos     size_t dgram_len = qpacket->datagram_len;
   1455      1.1  christos 
   1456      1.1  christos     if (ch == NULL)
   1457  1.1.1.2  christos         return 0;
   1458      1.1  christos 
   1459  1.1.1.4  christos     ossl_ch_reset_rx_state(ch);
   1460      1.1  christos 
   1461      1.1  christos     /* Initialize |ackm_data| (and reinitialize |ok|)*/
   1462      1.1  christos     memset(&ackm_data, 0, sizeof(ackm_data));
   1463      1.1  christos     /*
   1464      1.1  christos      * ASSUMPTION: All packets that aren't special case have a
   1465      1.1  christos      * packet number.
   1466      1.1  christos      */
   1467      1.1  christos     ackm_data.pkt_num = qpacket->pn;
   1468      1.1  christos     ackm_data.time = qpacket->time;
   1469      1.1  christos     enc_level = ossl_quic_pkt_type_to_enc_level(qpacket->hdr->type);
   1470      1.1  christos     if (enc_level >= QUIC_ENC_LEVEL_NUM)
   1471      1.1  christos         /*
   1472      1.1  christos          * Retry and Version Negotiation packets should not be passed to this
   1473      1.1  christos          * function.
   1474      1.1  christos          */
   1475  1.1.1.2  christos         return 0;
   1476      1.1  christos 
   1477      1.1  christos     ackm_data.pkt_space = ossl_quic_enc_level_to_pn_space(enc_level);
   1478      1.1  christos 
   1479      1.1  christos     /*
   1480      1.1  christos      * RFC 9000 s. 8.1
   1481      1.1  christos      * We can consider the connection to be validated, if we receive a packet
   1482      1.1  christos      * from the client protected via handshake keys, meaning that the
   1483      1.1  christos      * amplification limit no longer applies (i.e. we can set it as validated.
   1484      1.1  christos      * Otherwise, add the size of this packet to the unvalidated credit for
   1485      1.1  christos      * the connection.
   1486      1.1  christos      */
   1487      1.1  christos     if (enc_level == QUIC_ENC_LEVEL_HANDSHAKE)
   1488      1.1  christos         ossl_quic_tx_packetiser_set_validated(ch->txp);
   1489      1.1  christos     else
   1490      1.1  christos         ossl_quic_tx_packetiser_add_unvalidated_credit(ch->txp, dgram_len);
   1491      1.1  christos 
   1492      1.1  christos     /* Now that special cases are out of the way, parse frames */
   1493      1.1  christos     if (!PACKET_buf_init(&pkt, qpacket->hdr->data, qpacket->hdr->len)
   1494      1.1  christos         || !depack_process_frames(ch, &pkt, qpacket,
   1495  1.1.1.2  christos             enc_level,
   1496  1.1.1.2  christos             qpacket->time,
   1497  1.1.1.2  christos             &ackm_data))
   1498  1.1.1.2  christos         return 0;
   1499      1.1  christos 
   1500  1.1.1.2  christos     ossl_ackm_on_rx_packet(ch->ackm, &ackm_data);
   1501      1.1  christos 
   1502  1.1.1.2  christos     return 1;
   1503      1.1  christos }
   1504