1 | /*
|
---|
2 | * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License 2.0 (the "License"). You may not use
|
---|
5 | * this file except in compliance with the License. You can obtain a copy
|
---|
6 | * in the file LICENSE in the source distribution or at
|
---|
7 | * https://www.openssl.org/source/license.html
|
---|
8 | */
|
---|
9 |
|
---|
10 | #include <openssl/bio.h>
|
---|
11 | #include "quictestlib.h"
|
---|
12 | #include "../testutil.h"
|
---|
13 |
|
---|
14 | static long pkt_split_dgram_ctrl(BIO *bio, int cmd, long num, void *ptr)
|
---|
15 | {
|
---|
16 | long ret;
|
---|
17 | BIO *next = BIO_next(bio);
|
---|
18 |
|
---|
19 | if (next == NULL)
|
---|
20 | return 0;
|
---|
21 |
|
---|
22 | switch (cmd) {
|
---|
23 | case BIO_CTRL_DUP:
|
---|
24 | ret = 0L;
|
---|
25 | break;
|
---|
26 | default:
|
---|
27 | ret = BIO_ctrl(next, cmd, num, ptr);
|
---|
28 | break;
|
---|
29 | }
|
---|
30 | return ret;
|
---|
31 | }
|
---|
32 |
|
---|
33 | static int pkt_split_dgram_sendmmsg(BIO *bio, BIO_MSG *msg, size_t stride,
|
---|
34 | size_t num_msg, uint64_t flags,
|
---|
35 | size_t *msgs_processed)
|
---|
36 | {
|
---|
37 | BIO *next = BIO_next(bio);
|
---|
38 |
|
---|
39 | if (next == NULL)
|
---|
40 | return 0;
|
---|
41 |
|
---|
42 | /*
|
---|
43 | * We only introduce noise when receiving messages. We just pass this on
|
---|
44 | * to the underlying BIO.
|
---|
45 | */
|
---|
46 | return BIO_sendmmsg(next, msg, stride, num_msg, flags, msgs_processed);
|
---|
47 | }
|
---|
48 |
|
---|
49 | static int pkt_split_dgram_recvmmsg(BIO *bio, BIO_MSG *msg, size_t stride,
|
---|
50 | size_t num_msg, uint64_t flags,
|
---|
51 | size_t *msgs_processed)
|
---|
52 | {
|
---|
53 | BIO *next = BIO_next(bio);
|
---|
54 | size_t i, j, data_len = 0, msg_cnt = 0;
|
---|
55 | BIO_MSG *thismsg;
|
---|
56 |
|
---|
57 | if (!TEST_ptr(next))
|
---|
58 | return 0;
|
---|
59 |
|
---|
60 | /*
|
---|
61 | * For simplicity we assume that all elements in the msg array have the
|
---|
62 | * same data_len. They are not required to by the API, but it would be quite
|
---|
63 | * strange for that not to be the case - and our code that calls
|
---|
64 | * BIO_recvmmsg does do this (which is all that is important for this test
|
---|
65 | * code). We test the invariant here.
|
---|
66 | */
|
---|
67 | for (i = 0; i < num_msg; i++) {
|
---|
68 | if (i == 0)
|
---|
69 | data_len = msg[i].data_len;
|
---|
70 | else if (!TEST_size_t_eq(msg[i].data_len, data_len))
|
---|
71 | return 0;
|
---|
72 | }
|
---|
73 |
|
---|
74 | if (!BIO_recvmmsg(next, msg, stride, num_msg, flags, msgs_processed))
|
---|
75 | return 0;
|
---|
76 |
|
---|
77 | msg_cnt = *msgs_processed;
|
---|
78 | if (msg_cnt == num_msg)
|
---|
79 | return 1; /* We've used all our slots and can't split any more */
|
---|
80 | assert(msg_cnt < num_msg);
|
---|
81 |
|
---|
82 | for (i = 0, thismsg = msg; i < msg_cnt; i++, thismsg++) {
|
---|
83 | QUIC_PKT_HDR hdr;
|
---|
84 | PACKET pkt;
|
---|
85 | size_t remain;
|
---|
86 |
|
---|
87 | if (!PACKET_buf_init(&pkt, thismsg->data, thismsg->data_len))
|
---|
88 | return 0;
|
---|
89 |
|
---|
90 | /* Decode the packet header */
|
---|
91 | /*
|
---|
92 | * TODO(QUIC SERVER): We need to query the short connection id len
|
---|
93 | * here, e.g. via some API SSL_get_short_conn_id_len()
|
---|
94 | */
|
---|
95 | if (ossl_quic_wire_decode_pkt_hdr(&pkt, 0, 0, 0, &hdr, NULL) != 1)
|
---|
96 | return 0;
|
---|
97 | remain = PACKET_remaining(&pkt);
|
---|
98 | if (remain > 0) {
|
---|
99 | for (j = msg_cnt; j > i; j--) {
|
---|
100 | if (!bio_msg_copy(&msg[j], &msg[j - 1]))
|
---|
101 | return 0;
|
---|
102 | }
|
---|
103 | thismsg->data_len -= remain;
|
---|
104 | msg[i + 1].data_len = remain;
|
---|
105 | memmove(msg[i + 1].data,
|
---|
106 | (unsigned char *)msg[i + 1].data + thismsg->data_len,
|
---|
107 | remain);
|
---|
108 | msg_cnt++;
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | *msgs_processed = msg_cnt;
|
---|
113 | return 1;
|
---|
114 | }
|
---|
115 |
|
---|
116 | /* Choose a sufficiently large type likely to be unused for this custom BIO */
|
---|
117 | #define BIO_TYPE_PKT_SPLIT_DGRAM_FILTER (0x81 | BIO_TYPE_FILTER)
|
---|
118 |
|
---|
119 | static BIO_METHOD *method_pkt_split_dgram = NULL;
|
---|
120 |
|
---|
121 | /* Note: Not thread safe! */
|
---|
122 | const BIO_METHOD *bio_f_pkt_split_dgram_filter(void)
|
---|
123 | {
|
---|
124 | if (method_pkt_split_dgram == NULL) {
|
---|
125 | method_pkt_split_dgram = BIO_meth_new(BIO_TYPE_PKT_SPLIT_DGRAM_FILTER,
|
---|
126 | "Packet splitting datagram filter");
|
---|
127 | if (method_pkt_split_dgram == NULL
|
---|
128 | || !BIO_meth_set_ctrl(method_pkt_split_dgram, pkt_split_dgram_ctrl)
|
---|
129 | || !BIO_meth_set_sendmmsg(method_pkt_split_dgram,
|
---|
130 | pkt_split_dgram_sendmmsg)
|
---|
131 | || !BIO_meth_set_recvmmsg(method_pkt_split_dgram,
|
---|
132 | pkt_split_dgram_recvmmsg))
|
---|
133 | return NULL;
|
---|
134 | }
|
---|
135 | return method_pkt_split_dgram;
|
---|
136 | }
|
---|
137 |
|
---|
138 | void bio_f_pkt_split_dgram_filter_free(void)
|
---|
139 | {
|
---|
140 | BIO_meth_free(method_pkt_split_dgram);
|
---|
141 | }
|
---|