xref: /freebsd/crypto/openssl/test/quic_multistream_test.c (revision 78e936b2d0b5e6554425009199be31e76bc67c10)
1 /*
2  * Copyright 2023-2026 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 #include <openssl/ssl.h>
10 #include <openssl/quic.h>
11 #include <openssl/bio.h>
12 #include <openssl/lhash.h>
13 #include <openssl/rand.h>
14 #include "internal/quic_tserver.h"
15 #include "internal/quic_ssl.h"
16 #include "internal/quic_error.h"
17 #include "internal/quic_stream_map.h"
18 #include "internal/quic_engine.h"
19 #include "testutil.h"
20 #include "helpers/quictestlib.h"
21 #if defined(OPENSSL_THREADS)
22 #include "internal/thread_arch.h"
23 #endif
24 #include "internal/numbers.h" /* UINT64_C */
25 
26 static const char *certfile, *keyfile;
27 
28 #if defined(_AIX)
29 /*
30  * Some versions of AIX define macros for events and revents for use when
31  * accessing pollfd structures (see Github issue #24236). That interferes
32  * with our use of these names here. We simply undef them.
33  */
34 #undef revents
35 #undef events
36 #endif
37 
38 #if defined(OPENSSL_THREADS)
39 struct child_thread_args {
40     struct helper *h;
41     const struct script_op *script;
42     const char *script_name;
43     int thread_idx;
44 
45     CRYPTO_THREAD *t;
46     CRYPTO_MUTEX *m;
47     int testresult;
48     int done;
49     int s_checked_out;
50 };
51 #endif
52 
53 typedef struct stream_info {
54     const char *name;
55     SSL *c_stream;
56     uint64_t s_stream_id;
57 } STREAM_INFO;
58 
59 DEFINE_LHASH_OF_EX(STREAM_INFO);
60 
61 struct helper {
62     int s_fd;
63     BIO *s_net_bio, *s_net_bio_own, *s_qtf_wbio, *s_qtf_wbio_own;
64     /* The BIO_ADDR used for BIO_bind() */
65     BIO_ADDR *s_net_bio_orig_addr;
66     /* The resulting address, which is the one to connect to */
67     BIO_ADDR *s_net_bio_addr;
68 
69     /*
70      * When doing a blocking mode test run, s_priv always points to the TSERVER
71      * and s is NULL when the main thread should not be touching s_priv.
72      */
73     QUIC_TSERVER *s, *s_priv;
74     LHASH_OF(STREAM_INFO) *s_streams;
75 
76     int c_fd;
77     BIO *c_net_bio, *c_net_bio_own;
78     SSL_CTX *c_ctx;
79     SSL *c_conn;
80     LHASH_OF(STREAM_INFO) *c_streams;
81 
82 #if defined(OPENSSL_THREADS)
83     struct child_thread_args *threads;
84     size_t num_threads;
85     CRYPTO_MUTEX *misc_m;
86     CRYPTO_CONDVAR *misc_cv;
87 #endif
88 
89     OSSL_TIME start_time;
90 
91     /*
92      * This is a duration recording the amount of time we have skipped forwards
93      * for testing purposes relative to the real ossl_time_now() clock. We add
94      * a quantity of time to this every time we skip some time.
95      */
96     CRYPTO_RWLOCK *time_lock;
97     OSSL_TIME time_slip; /* protected by time_lock */
98 
99     QTEST_FAULT *qtf;
100 
101     int init, blocking, check_spin_again;
102     int free_order, need_injector;
103 
104     int (*qtf_packet_plain_cb)(struct helper *h, QUIC_PKT_HDR *hdr,
105         unsigned char *buf, size_t buf_len);
106     int (*qtf_handshake_cb)(struct helper *h,
107         unsigned char *buf, size_t buf_len);
108     int (*qtf_datagram_cb)(struct helper *h,
109         BIO_MSG *m, size_t stride);
110     uint64_t inject_word0, inject_word1;
111     uint64_t scratch0, scratch1, fail_count;
112 #if defined(OPENSSL_THREADS)
113     struct {
114         CRYPTO_THREAD *t;
115         CRYPTO_MUTEX *m;
116         CRYPTO_CONDVAR *c;
117         int ready, stop;
118     } server_thread;
119     int s_checked_out;
120 #endif
121 };
122 
123 struct helper_local {
124     struct helper *h;
125     LHASH_OF(STREAM_INFO) *c_streams;
126     int thread_idx;
127     const struct script_op *check_op;
128     int explicit_event_handling;
129 };
130 
131 struct script_op {
132     uint32_t op;
133     const void *arg0;
134     size_t arg1;
135     int (*check_func)(struct helper *h, struct helper_local *hl);
136     const char *stream_name;
137     uint64_t arg2;
138     int (*qtf_packet_plain_cb)(struct helper *h, QUIC_PKT_HDR *hdr,
139         unsigned char *buf, size_t buf_len);
140     int (*qtf_handshake_cb)(struct helper *h,
141         unsigned char *buf, size_t buf_len);
142     int (*qtf_datagram_cb)(struct helper *h,
143         BIO_MSG *m, size_t stride);
144 };
145 
146 #define OPK_END 0
147 #define OPK_CHECK 1
148 #define OPK_C_SET_ALPN 2
149 #define OPK_C_CONNECT_WAIT 3
150 #define OPK_C_WRITE 4
151 #define OPK_S_WRITE 5
152 #define OPK_C_READ_EXPECT 6
153 #define OPK_S_READ_EXPECT 7
154 #define OPK_C_EXPECT_FIN 8
155 #define OPK_S_EXPECT_FIN 9
156 #define OPK_C_CONCLUDE 10
157 #define OPK_S_CONCLUDE 11
158 #define OPK_C_DETACH 12
159 #define OPK_C_ATTACH 13
160 #define OPK_C_NEW_STREAM 14
161 #define OPK_S_NEW_STREAM 15
162 #define OPK_C_ACCEPT_STREAM_WAIT 16
163 #define OPK_C_ACCEPT_STREAM_NONE 17
164 #define OPK_C_FREE_STREAM 18
165 #define OPK_C_SET_DEFAULT_STREAM_MODE 19
166 #define OPK_C_SET_INCOMING_STREAM_POLICY 20
167 #define OPK_C_SHUTDOWN_WAIT 21
168 #define OPK_C_EXPECT_CONN_CLOSE_INFO 22
169 #define OPK_S_EXPECT_CONN_CLOSE_INFO 23
170 #define OPK_S_BIND_STREAM_ID 24
171 #define OPK_C_WAIT_FOR_DATA 25
172 #define OPK_C_WRITE_FAIL 26
173 #define OPK_S_WRITE_FAIL 27
174 #define OPK_C_READ_FAIL 28
175 #define OPK_C_STREAM_RESET 29
176 #define OPK_S_ACCEPT_STREAM_WAIT 30
177 #define OPK_NEW_THREAD 31
178 #define OPK_BEGIN_REPEAT 32
179 #define OPK_END_REPEAT 33
180 #define OPK_S_UNBIND_STREAM_ID 34
181 #define OPK_C_READ_FAIL_WAIT 35
182 #define OPK_C_CLOSE_SOCKET 36
183 #define OPK_C_EXPECT_SSL_ERR 37
184 #define OPK_EXPECT_ERR_REASON 38
185 #define OPK_EXPECT_ERR_LIB 39
186 #define OPK_SLEEP 40
187 #define OPK_S_READ_FAIL 41
188 #define OPK_S_SET_INJECT_PLAIN 42
189 #define OPK_SET_INJECT_WORD 43
190 #define OPK_C_INHIBIT_TICK 44
191 #define OPK_C_SET_WRITE_BUF_SIZE 45
192 #define OPK_S_SET_INJECT_HANDSHAKE 46
193 #define OPK_S_NEW_TICKET 47
194 #define OPK_C_SKIP_IF_UNBOUND 48
195 #define OPK_S_SET_INJECT_DATAGRAM 49
196 #define OPK_S_SHUTDOWN 50
197 #define OPK_POP_ERR 51
198 #define OPK_C_WRITE_EX2 52
199 #define OPK_SKIP_IF_BLOCKING 53
200 #define OPK_C_STREAM_RESET_FAIL 54
201 
202 #define EXPECT_CONN_CLOSE_APP (1U << 0)
203 #define EXPECT_CONN_CLOSE_REMOTE (1U << 1)
204 
205 /* OPK_C_NEW_STREAM */
206 #define ALLOW_FAIL (1U << 16)
207 
208 #define C_BIDI_ID(ordinal) \
209     (((ordinal) << 2) | QUIC_STREAM_INITIATOR_CLIENT | QUIC_STREAM_DIR_BIDI)
210 #define S_BIDI_ID(ordinal) \
211     (((ordinal) << 2) | QUIC_STREAM_INITIATOR_SERVER | QUIC_STREAM_DIR_BIDI)
212 #define C_UNI_ID(ordinal) \
213     (((ordinal) << 2) | QUIC_STREAM_INITIATOR_CLIENT | QUIC_STREAM_DIR_UNI)
214 #define S_UNI_ID(ordinal) \
215     (((ordinal) << 2) | QUIC_STREAM_INITIATOR_SERVER | QUIC_STREAM_DIR_UNI)
216 
217 #define ANY_ID UINT64_MAX
218 
219 #define OP_END \
220     { OPK_END }
221 #define OP_CHECK(func, arg2) \
222     { OPK_CHECK, NULL, 0, (func), NULL, (arg2) },
223 #define OP_C_SET_ALPN(alpn) \
224     { OPK_C_SET_ALPN, (alpn), 0, NULL, NULL },
225 #define OP_C_CONNECT_WAIT() \
226     { OPK_C_CONNECT_WAIT, NULL, 0, NULL, NULL },
227 #define OP_C_CONNECT_WAIT_OR_FAIL() \
228     { OPK_C_CONNECT_WAIT, NULL, 1, NULL, NULL },
229 #define OP_C_WRITE(stream_name, buf, buf_len) \
230     { OPK_C_WRITE, (buf), (buf_len), NULL, #stream_name },
231 #define OP_S_WRITE(stream_name, buf, buf_len) \
232     { OPK_S_WRITE, (buf), (buf_len), NULL, #stream_name },
233 #define OP_C_READ_EXPECT(stream_name, buf, buf_len) \
234     { OPK_C_READ_EXPECT, (buf), (buf_len), NULL, #stream_name },
235 #define OP_S_READ_EXPECT(stream_name, buf, buf_len) \
236     { OPK_S_READ_EXPECT, (buf), (buf_len), NULL, #stream_name },
237 #define OP_C_EXPECT_FIN(stream_name) \
238     { OPK_C_EXPECT_FIN, NULL, 0, NULL, #stream_name },
239 #define OP_S_EXPECT_FIN(stream_name) \
240     { OPK_S_EXPECT_FIN, NULL, 0, NULL, #stream_name },
241 #define OP_C_CONCLUDE(stream_name) \
242     { OPK_C_CONCLUDE, NULL, 0, NULL, #stream_name },
243 #define OP_S_CONCLUDE(stream_name) \
244     { OPK_S_CONCLUDE, NULL, 0, NULL, #stream_name },
245 #define OP_C_DETACH(stream_name) \
246     { OPK_C_DETACH, NULL, 0, NULL, #stream_name },
247 #define OP_C_ATTACH(stream_name) \
248     { OPK_C_ATTACH, NULL, 0, NULL, #stream_name },
249 #define OP_C_NEW_STREAM_BIDI(stream_name, expect_id) \
250     { OPK_C_NEW_STREAM, NULL, 0, NULL, #stream_name, (expect_id) },
251 #define OP_C_NEW_STREAM_BIDI_EX(stream_name, expect_id, flags) \
252     { OPK_C_NEW_STREAM, NULL, (flags), NULL, #stream_name, (expect_id) },
253 #define OP_C_NEW_STREAM_UNI(stream_name, expect_id) \
254     { OPK_C_NEW_STREAM, NULL, SSL_STREAM_FLAG_UNI,  \
255         NULL, #stream_name, (expect_id) },
256 #define OP_C_NEW_STREAM_UNI_EX(stream_name, expect_id, flags) \
257     { OPK_C_NEW_STREAM, NULL, (flags) | SSL_STREAM_FLAG_UNI,  \
258         NULL, #stream_name, (expect_id) },
259 #define OP_S_NEW_STREAM_BIDI(stream_name, expect_id) \
260     { OPK_S_NEW_STREAM, NULL, 0, NULL, #stream_name, (expect_id) },
261 #define OP_S_NEW_STREAM_UNI(stream_name, expect_id) \
262     { OPK_S_NEW_STREAM, NULL, 1, NULL, #stream_name, (expect_id) },
263 #define OP_C_ACCEPT_STREAM_WAIT(stream_name) \
264     { OPK_C_ACCEPT_STREAM_WAIT, NULL, 0, NULL, #stream_name },
265 #define OP_C_ACCEPT_STREAM_NONE() \
266     { OPK_C_ACCEPT_STREAM_NONE, NULL, 0, NULL, NULL },
267 #define OP_C_FREE_STREAM(stream_name) \
268     { OPK_C_FREE_STREAM, NULL, 0, NULL, #stream_name },
269 #define OP_C_SET_DEFAULT_STREAM_MODE(mode) \
270     { OPK_C_SET_DEFAULT_STREAM_MODE, NULL, (mode), NULL, NULL },
271 #define OP_C_SET_INCOMING_STREAM_POLICY(policy) \
272     { OPK_C_SET_INCOMING_STREAM_POLICY, NULL, (policy), NULL, NULL },
273 #define OP_C_SHUTDOWN_WAIT(reason, flags) \
274     { OPK_C_SHUTDOWN_WAIT, (reason), (flags), NULL, NULL },
275 #define OP_C_EXPECT_CONN_CLOSE_INFO(ec, app, remote)                                     \
276     { OPK_C_EXPECT_CONN_CLOSE_INFO, NULL,                                                \
277         ((app) ? EXPECT_CONN_CLOSE_APP : 0) | ((remote) ? EXPECT_CONN_CLOSE_REMOTE : 0), \
278         NULL, NULL, (ec) },
279 #define OP_S_EXPECT_CONN_CLOSE_INFO(ec, app, remote)                                     \
280     { OPK_S_EXPECT_CONN_CLOSE_INFO, NULL,                                                \
281         ((app) ? EXPECT_CONN_CLOSE_APP : 0) | ((remote) ? EXPECT_CONN_CLOSE_REMOTE : 0), \
282         NULL, NULL, (ec) },
283 #define OP_S_BIND_STREAM_ID(stream_name, stream_id) \
284     { OPK_S_BIND_STREAM_ID, NULL, 0, NULL, #stream_name, (stream_id) },
285 #define OP_C_WAIT_FOR_DATA(stream_name) \
286     { OPK_C_WAIT_FOR_DATA, NULL, 0, NULL, #stream_name },
287 #define OP_C_WRITE_FAIL(stream_name) \
288     { OPK_C_WRITE_FAIL, NULL, 0, NULL, #stream_name },
289 #define OP_S_WRITE_FAIL(stream_name) \
290     { OPK_S_WRITE_FAIL, NULL, 0, NULL, #stream_name },
291 #define OP_C_READ_FAIL(stream_name) \
292     { OPK_C_READ_FAIL, NULL, 0, NULL, #stream_name },
293 #define OP_S_READ_FAIL(stream_name, allow_zero_len) \
294     { OPK_S_READ_FAIL, NULL, (allow_zero_len), NULL, #stream_name },
295 #define OP_C_STREAM_RESET(stream_name, aec) \
296     { OPK_C_STREAM_RESET, NULL, 0, NULL, #stream_name, (aec) },
297 #define OP_C_STREAM_RESET_FAIL(stream_name, aec) \
298     { OPK_C_STREAM_RESET_FAIL, NULL, 0, NULL, #stream_name, (aec) },
299 #define OP_S_ACCEPT_STREAM_WAIT(stream_name) \
300     { OPK_S_ACCEPT_STREAM_WAIT, NULL, 0, NULL, #stream_name },
301 #define OP_NEW_THREAD(num_threads, script) \
302     { OPK_NEW_THREAD, (script), (num_threads), NULL, NULL, 0 },
303 #define OP_BEGIN_REPEAT(n) \
304     { OPK_BEGIN_REPEAT, NULL, (n) },
305 #define OP_END_REPEAT() \
306     { OPK_END_REPEAT },
307 #define OP_S_UNBIND_STREAM_ID(stream_name) \
308     { OPK_S_UNBIND_STREAM_ID, NULL, 0, NULL, #stream_name },
309 #define OP_C_READ_FAIL_WAIT(stream_name) \
310     { OPK_C_READ_FAIL_WAIT, NULL, 0, NULL, #stream_name },
311 #define OP_C_CLOSE_SOCKET() \
312     { OPK_C_CLOSE_SOCKET },
313 #define OP_C_EXPECT_SSL_ERR(stream_name, err) \
314     { OPK_C_EXPECT_SSL_ERR, NULL, (err), NULL, #stream_name },
315 #define OP_EXPECT_ERR_REASON(err) \
316     { OPK_EXPECT_ERR_REASON, NULL, (err) },
317 #define OP_EXPECT_ERR_LIB(lib) \
318     { OPK_EXPECT_ERR_LIB, NULL, (lib) },
319 #define OP_SLEEP(ms) \
320     { OPK_SLEEP, NULL, 0, NULL, NULL, (ms) },
321 #define OP_S_SET_INJECT_PLAIN(f) \
322     { OPK_S_SET_INJECT_PLAIN, NULL, 0, NULL, NULL, 0, (f) },
323 #define OP_SET_INJECT_WORD(w0, w1) \
324     { OPK_SET_INJECT_WORD, NULL, (w0), NULL, NULL, (w1), NULL },
325 #define OP_C_INHIBIT_TICK(inhibit) \
326     { OPK_C_INHIBIT_TICK, NULL, (inhibit), NULL, NULL, 0, NULL },
327 #define OP_C_SET_WRITE_BUF_SIZE(stream_name, size) \
328     { OPK_C_SET_WRITE_BUF_SIZE, NULL, (size), NULL, #stream_name },
329 #define OP_S_SET_INJECT_HANDSHAKE(f) \
330     { OPK_S_SET_INJECT_HANDSHAKE, NULL, 0, NULL, NULL, 0, NULL, (f) },
331 #define OP_S_NEW_TICKET() \
332     { OPK_S_NEW_TICKET },
333 #define OP_C_SKIP_IF_UNBOUND(stream_name, n) \
334     { OPK_C_SKIP_IF_UNBOUND, NULL, (n), NULL, #stream_name },
335 #define OP_S_SET_INJECT_DATAGRAM(f) \
336     { OPK_S_SET_INJECT_DATAGRAM, NULL, 0, NULL, NULL, 0, NULL, NULL, (f) },
337 #define OP_S_SHUTDOWN(error_code) \
338     { OPK_S_SHUTDOWN, NULL, (error_code) },
339 #define OP_POP_ERR() \
340     { OPK_POP_ERR },
341 #define OP_C_WRITE_EX2(stream_name, buf, buf_len, flags) \
342     { OPK_C_WRITE_EX2, (buf), (buf_len), NULL, #stream_name, (flags) },
343 #define OP_CHECK2(func, arg1, arg2) \
344     { OPK_CHECK, NULL, (arg1), (func), NULL, (arg2) },
345 #define OP_SKIP_IF_BLOCKING(n) \
346     { OPK_SKIP_IF_BLOCKING, NULL, (n), NULL, 0 },
347 
get_time(void * arg)348 static OSSL_TIME get_time(void *arg)
349 {
350     struct helper *h = arg;
351     OSSL_TIME t;
352 
353     if (!TEST_true(CRYPTO_THREAD_read_lock(h->time_lock)))
354         return ossl_time_zero();
355 
356     t = ossl_time_add(ossl_time_now(), h->time_slip);
357 
358     CRYPTO_THREAD_unlock(h->time_lock);
359     return t;
360 }
361 
skip_time_ms(struct helper * h,struct helper_local * hl)362 static int skip_time_ms(struct helper *h, struct helper_local *hl)
363 {
364     if (!TEST_true(CRYPTO_THREAD_write_lock(h->time_lock)))
365         return 0;
366 
367     h->time_slip = ossl_time_add(h->time_slip, ossl_ms2time(hl->check_op->arg2));
368 
369     CRYPTO_THREAD_unlock(h->time_lock);
370     return 1;
371 }
372 
373 static QUIC_TSERVER *s_lock(struct helper *h, struct helper_local *hl);
374 static void s_unlock(struct helper *h, struct helper_local *hl);
375 
376 #define ACQUIRE_S() s_lock(h, hl)
377 #define ACQUIRE_S_NOHL() s_lock(h, NULL)
378 
check_rejected(struct helper * h,struct helper_local * hl)379 static int check_rejected(struct helper *h, struct helper_local *hl)
380 {
381     uint64_t stream_id = hl->check_op->arg2;
382 
383     if (!ossl_quic_tserver_stream_has_peer_stop_sending(ACQUIRE_S(), stream_id, NULL)
384         || !ossl_quic_tserver_stream_has_peer_reset_stream(ACQUIRE_S(), stream_id, NULL)) {
385         h->check_spin_again = 1;
386         return 0;
387     }
388 
389     return 1;
390 }
391 
check_stream_reset(struct helper * h,struct helper_local * hl)392 static int check_stream_reset(struct helper *h, struct helper_local *hl)
393 {
394     uint64_t stream_id = hl->check_op->arg2, aec = 0;
395 
396     if (!ossl_quic_tserver_stream_has_peer_reset_stream(ACQUIRE_S(), stream_id, &aec)) {
397         h->check_spin_again = 1;
398         return 0;
399     }
400 
401     return TEST_uint64_t_eq(aec, 42);
402 }
403 
check_stream_stopped(struct helper * h,struct helper_local * hl)404 static int check_stream_stopped(struct helper *h, struct helper_local *hl)
405 {
406     uint64_t stream_id = hl->check_op->arg2;
407 
408     if (!ossl_quic_tserver_stream_has_peer_stop_sending(ACQUIRE_S(), stream_id, NULL)) {
409         h->check_spin_again = 1;
410         return 0;
411     }
412 
413     return 1;
414 }
415 
override_key_update(struct helper * h,struct helper_local * hl)416 static int override_key_update(struct helper *h, struct helper_local *hl)
417 {
418     QUIC_CHANNEL *ch = ossl_quic_conn_get_channel(h->c_conn);
419 
420     ossl_quic_channel_set_txku_threshold_override(ch, hl->check_op->arg2);
421     return 1;
422 }
423 
trigger_key_update(struct helper * h,struct helper_local * hl)424 static int trigger_key_update(struct helper *h, struct helper_local *hl)
425 {
426     if (!TEST_true(SSL_key_update(h->c_conn, SSL_KEY_UPDATE_REQUESTED)))
427         return 0;
428 
429     return 1;
430 }
431 
check_key_update_ge(struct helper * h,struct helper_local * hl)432 static int check_key_update_ge(struct helper *h, struct helper_local *hl)
433 {
434     QUIC_CHANNEL *ch = ossl_quic_conn_get_channel(h->c_conn);
435     int64_t txke = (int64_t)ossl_quic_channel_get_tx_key_epoch(ch);
436     int64_t rxke = (int64_t)ossl_quic_channel_get_rx_key_epoch(ch);
437     int64_t diff = txke - rxke;
438 
439     /*
440      * TXKE must always be equal to or ahead of RXKE.
441      * It can be ahead of RXKE by at most 1.
442      */
443     if (!TEST_int64_t_ge(diff, 0) || !TEST_int64_t_le(diff, 1))
444         return 0;
445 
446     /* Caller specifies a minimum number of RXKEs which must have happened. */
447     if (!TEST_uint64_t_ge((uint64_t)rxke, hl->check_op->arg2))
448         return 0;
449 
450     return 1;
451 }
452 
check_key_update_lt(struct helper * h,struct helper_local * hl)453 static int check_key_update_lt(struct helper *h, struct helper_local *hl)
454 {
455     QUIC_CHANNEL *ch = ossl_quic_conn_get_channel(h->c_conn);
456     uint64_t txke = ossl_quic_channel_get_tx_key_epoch(ch);
457 
458     /* Caller specifies a maximum number of TXKEs which must have happened. */
459     if (!TEST_uint64_t_lt(txke, hl->check_op->arg2))
460         return 0;
461 
462     return 1;
463 }
464 
stream_info_hash(const STREAM_INFO * info)465 static unsigned long stream_info_hash(const STREAM_INFO *info)
466 {
467     return OPENSSL_LH_strhash(info->name);
468 }
469 
stream_info_cmp(const STREAM_INFO * a,const STREAM_INFO * b)470 static int stream_info_cmp(const STREAM_INFO *a, const STREAM_INFO *b)
471 {
472     return strcmp(a->name, b->name);
473 }
474 
cleanup_stream(STREAM_INFO * info)475 static void cleanup_stream(STREAM_INFO *info)
476 {
477     SSL_free(info->c_stream);
478     OPENSSL_free(info);
479 }
480 
helper_cleanup_streams(LHASH_OF (STREAM_INFO)** lh)481 static void helper_cleanup_streams(LHASH_OF(STREAM_INFO) **lh)
482 {
483     if (*lh == NULL)
484         return;
485 
486     lh_STREAM_INFO_doall(*lh, cleanup_stream);
487     lh_STREAM_INFO_free(*lh);
488     *lh = NULL;
489 }
490 
491 #if defined(OPENSSL_THREADS)
492 static CRYPTO_THREAD_RETVAL run_script_child_thread(void *arg);
493 
join_threads(struct child_thread_args * threads,size_t num_threads)494 static int join_threads(struct child_thread_args *threads, size_t num_threads)
495 {
496     int ok = 1;
497     size_t i;
498     CRYPTO_THREAD_RETVAL rv;
499 
500     for (i = 0; i < num_threads; ++i) {
501         if (threads[i].t != NULL) {
502             ossl_crypto_thread_native_join(threads[i].t, &rv);
503 
504             if (!threads[i].testresult)
505                 /* Do not log failure here, worker will do it. */
506                 ok = 0;
507 
508             ossl_crypto_thread_native_clean(threads[i].t);
509             threads[i].t = NULL;
510         }
511 
512         ossl_crypto_mutex_free(&threads[i].m);
513     }
514 
515     return ok;
516 }
517 
join_server_thread(struct helper * h)518 static int join_server_thread(struct helper *h)
519 {
520     CRYPTO_THREAD_RETVAL rv;
521 
522     if (h->server_thread.t == NULL)
523         return 1;
524 
525     ossl_crypto_mutex_lock(h->server_thread.m);
526     h->server_thread.stop = 1;
527     ossl_crypto_condvar_signal(h->server_thread.c);
528     ossl_crypto_mutex_unlock(h->server_thread.m);
529 
530     ossl_crypto_thread_native_join(h->server_thread.t, &rv);
531     ossl_crypto_thread_native_clean(h->server_thread.t);
532     h->server_thread.t = NULL;
533     return 1;
534 }
535 
536 /* Ensure the server-state lock is currently held. Idempotent. */
s_checked_out_p(struct helper * h,int thread_idx)537 static int *s_checked_out_p(struct helper *h, int thread_idx)
538 {
539     return (thread_idx < 0) ? &h->s_checked_out
540                             : &h->threads[thread_idx].s_checked_out;
541 }
542 
s_lock(struct helper * h,struct helper_local * hl)543 static QUIC_TSERVER *s_lock(struct helper *h, struct helper_local *hl)
544 {
545     int *p_checked_out = s_checked_out_p(h, hl == NULL ? -1 : hl->thread_idx);
546 
547     if (h->server_thread.m == NULL || *p_checked_out)
548         return h->s;
549 
550     ossl_crypto_mutex_lock(h->server_thread.m);
551     h->s = h->s_priv;
552     *p_checked_out = 1;
553     return h->s;
554 }
555 
556 /* Ensure the server-state lock is currently not held. Idempotent. */
s_unlock(struct helper * h,struct helper_local * hl)557 static void s_unlock(struct helper *h, struct helper_local *hl)
558 {
559     int *p_checked_out = s_checked_out_p(h, hl->thread_idx);
560 
561     if (h->server_thread.m == NULL || !*p_checked_out)
562         return;
563 
564     *p_checked_out = 0;
565     h->s = NULL;
566     ossl_crypto_mutex_unlock(h->server_thread.m);
567 }
568 
server_helper_thread(void * arg)569 static unsigned int server_helper_thread(void *arg)
570 {
571     struct helper *h = arg;
572 
573     ossl_crypto_mutex_lock(h->server_thread.m);
574 
575     for (;;) {
576         int ready, stop;
577 
578         ready = h->server_thread.ready;
579         stop = h->server_thread.stop;
580 
581         if (stop)
582             break;
583 
584         if (!ready) {
585             ossl_crypto_condvar_wait(h->server_thread.c, h->server_thread.m);
586             continue;
587         }
588 
589         ossl_quic_tserver_tick(h->s_priv);
590         ossl_crypto_mutex_unlock(h->server_thread.m);
591         /*
592          * Give the main thread an opportunity to get the mutex, which is
593          * sometimes necessary in some script operations.
594          */
595         OSSL_sleep(1);
596         ossl_crypto_mutex_lock(h->server_thread.m);
597     }
598 
599     ossl_crypto_mutex_unlock(h->server_thread.m);
600     return 1;
601 }
602 
603 #else
604 
s_lock(struct helper * h,struct helper_local * hl)605 static QUIC_TSERVER *s_lock(struct helper *h, struct helper_local *hl)
606 {
607     return h->s;
608 }
609 
s_unlock(struct helper * h,struct helper_local * hl)610 static void s_unlock(struct helper *h, struct helper_local *hl)
611 {
612 }
613 
614 #endif
615 
helper_cleanup(struct helper * h)616 static void helper_cleanup(struct helper *h)
617 {
618 #if defined(OPENSSL_THREADS)
619     join_threads(h->threads, h->num_threads);
620     join_server_thread(h);
621     OPENSSL_free(h->threads);
622     h->threads = NULL;
623     h->num_threads = 0;
624 #endif
625 
626     if (h->free_order == 0) {
627         /* order 0: streams, then conn */
628         helper_cleanup_streams(&h->c_streams);
629 
630         SSL_free(h->c_conn);
631         h->c_conn = NULL;
632     } else {
633         /* order 1: conn, then streams */
634         SSL_free(h->c_conn);
635         h->c_conn = NULL;
636 
637         helper_cleanup_streams(&h->c_streams);
638     }
639 
640     helper_cleanup_streams(&h->s_streams);
641     ossl_quic_tserver_free(h->s_priv);
642     h->s_priv = h->s = NULL;
643 
644     BIO_free(h->s_net_bio_own);
645     h->s_net_bio_own = NULL;
646 
647     BIO_free(h->c_net_bio_own);
648     h->c_net_bio_own = NULL;
649 
650     BIO_free(h->s_qtf_wbio_own);
651     h->s_qtf_wbio_own = NULL;
652 
653     qtest_fault_free(h->qtf);
654     h->qtf = NULL;
655 
656     if (h->s_fd >= 0) {
657         BIO_closesocket(h->s_fd);
658         h->s_fd = -1;
659     }
660 
661     if (h->c_fd >= 0) {
662         BIO_closesocket(h->c_fd);
663         h->c_fd = -1;
664     }
665 
666     BIO_ADDR_free(h->s_net_bio_addr);
667     h->s_net_bio_addr = NULL;
668     BIO_ADDR_free(h->s_net_bio_orig_addr);
669     h->s_net_bio_orig_addr = NULL;
670 
671     SSL_CTX_free(h->c_ctx);
672     h->c_ctx = NULL;
673 
674     CRYPTO_THREAD_lock_free(h->time_lock);
675     h->time_lock = NULL;
676 
677 #if defined(OPENSSL_THREADS)
678     ossl_crypto_mutex_free(&h->misc_m);
679     ossl_crypto_condvar_free(&h->misc_cv);
680     ossl_crypto_mutex_free(&h->server_thread.m);
681     ossl_crypto_condvar_free(&h->server_thread.c);
682 #endif
683 }
684 
helper_init(struct helper * h,const char * script_name,int free_order,int blocking,int need_injector)685 static int helper_init(struct helper *h, const char *script_name,
686     int free_order, int blocking,
687     int need_injector)
688 {
689     struct in_addr ina = { 0 };
690     QUIC_TSERVER_ARGS s_args = { 0 };
691     union BIO_sock_info_u info;
692     char title[128];
693     QTEST_DATA *bdata = NULL;
694 
695     memset(h, 0, sizeof(*h));
696     h->c_fd = -1;
697     h->s_fd = -1;
698     h->free_order = free_order;
699     h->blocking = blocking;
700     h->need_injector = need_injector;
701     h->time_slip = ossl_time_zero();
702 
703     bdata = OPENSSL_zalloc(sizeof(QTEST_DATA));
704     if (bdata == NULL)
705         goto err;
706 
707     if (!TEST_ptr(h->time_lock = CRYPTO_THREAD_lock_new()))
708         goto err;
709 
710     if (!TEST_ptr(h->s_streams = lh_STREAM_INFO_new(stream_info_hash,
711                       stream_info_cmp)))
712         goto err;
713 
714     if (!TEST_ptr(h->c_streams = lh_STREAM_INFO_new(stream_info_hash,
715                       stream_info_cmp)))
716         goto err;
717 
718     ina.s_addr = htonl(0x7f000001UL);
719 
720     h->s_fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0);
721     if (!TEST_int_ge(h->s_fd, 0))
722         goto err;
723 
724     if (!TEST_true(BIO_socket_nbio(h->s_fd, 1)))
725         goto err;
726 
727     if (!TEST_ptr(h->s_net_bio_orig_addr = BIO_ADDR_new())
728         || !TEST_ptr(h->s_net_bio_addr = BIO_ADDR_new()))
729         goto err;
730 
731     if (!TEST_true(BIO_ADDR_rawmake(h->s_net_bio_orig_addr, AF_INET,
732             &ina, sizeof(ina), 0)))
733         goto err;
734 
735     if (!TEST_true(BIO_bind(h->s_fd, h->s_net_bio_orig_addr, 0)))
736         goto err;
737 
738     info.addr = h->s_net_bio_addr;
739     if (!TEST_true(BIO_sock_info(h->s_fd, BIO_SOCK_INFO_ADDRESS, &info)))
740         goto err;
741 
742     if (!TEST_int_gt(BIO_ADDR_rawport(h->s_net_bio_addr), 0))
743         goto err;
744 
745     if (!TEST_ptr(h->s_net_bio = h->s_net_bio_own = BIO_new_dgram(h->s_fd, 0)))
746         goto err;
747 
748     if (!BIO_up_ref(h->s_net_bio))
749         goto err;
750 
751     if (need_injector) {
752         h->s_qtf_wbio = h->s_qtf_wbio_own = BIO_new(qtest_get_bio_method());
753         if (!TEST_ptr(h->s_qtf_wbio))
754             goto err;
755 
756         if (!TEST_ptr(BIO_push(h->s_qtf_wbio, h->s_net_bio)))
757             goto err;
758 
759         s_args.net_wbio = h->s_qtf_wbio;
760     } else {
761         s_args.net_wbio = h->s_net_bio;
762     }
763 
764     s_args.net_rbio = h->s_net_bio;
765     s_args.alpn = NULL;
766     s_args.now_cb = get_time;
767     s_args.now_cb_arg = h;
768     s_args.ctx = NULL;
769 
770     if (!TEST_ptr(h->s_priv = ossl_quic_tserver_new(&s_args, certfile, keyfile)))
771         goto err;
772 
773     if (!blocking)
774         h->s = h->s_priv;
775 
776     if (need_injector) {
777         h->qtf = qtest_create_injector(h->s_priv);
778         if (!TEST_ptr(h->qtf))
779             goto err;
780         bdata->fault = h->qtf;
781         BIO_set_data(h->s_qtf_wbio, bdata);
782     }
783 
784     h->s_net_bio_own = NULL;
785     h->s_qtf_wbio_own = NULL;
786 
787     h->c_fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0);
788     if (!TEST_int_ge(h->c_fd, 0))
789         goto err;
790 
791     if (!TEST_true(BIO_socket_nbio(h->c_fd, 1)))
792         goto err;
793 
794     if (!TEST_ptr(h->c_net_bio = h->c_net_bio_own = BIO_new_dgram(h->c_fd, 0)))
795         goto err;
796 
797     if (!TEST_true(BIO_dgram_set_peer(h->c_net_bio, h->s_net_bio_addr)))
798         goto err;
799 
800     if (!TEST_ptr(h->c_ctx = SSL_CTX_new(OSSL_QUIC_client_method())))
801         goto err;
802 
803     /* Set title for qlog purposes. */
804     BIO_snprintf(title, sizeof(title), "quic_multistream_test: %s", script_name);
805     if (!TEST_true(ossl_quic_set_diag_title(h->c_ctx, title)))
806         goto err;
807 
808     if (!TEST_ptr(h->c_conn = SSL_new(h->c_ctx)))
809         goto err;
810 
811     /* Use custom time function for virtual time skip. */
812     if (!TEST_true(ossl_quic_set_override_now_cb(h->c_conn, get_time, h)))
813         goto err;
814 
815     /* Takes ownership of our reference to the BIO. */
816     SSL_set0_rbio(h->c_conn, h->c_net_bio);
817     h->c_net_bio_own = NULL;
818 
819     if (!TEST_true(BIO_up_ref(h->c_net_bio)))
820         goto err;
821 
822     SSL_set0_wbio(h->c_conn, h->c_net_bio);
823 
824     if (!TEST_true(SSL_set_blocking_mode(h->c_conn, h->blocking)))
825         goto err;
826 
827 #if defined(OPENSSL_THREADS)
828     if (!TEST_ptr(h->misc_m = ossl_crypto_mutex_new()))
829         goto err;
830     if (!TEST_ptr(h->misc_cv = ossl_crypto_condvar_new()))
831         goto err;
832 #endif
833 
834     if (h->blocking) {
835 #if defined(OPENSSL_THREADS)
836         if (!TEST_ptr(h->server_thread.m = ossl_crypto_mutex_new()))
837             goto err;
838 
839         if (!TEST_ptr(h->server_thread.c = ossl_crypto_condvar_new()))
840             goto err;
841 
842         h->server_thread.t
843             = ossl_crypto_thread_native_start(server_helper_thread, h, 1);
844         if (!TEST_ptr(h->server_thread.t))
845             goto err;
846 #else
847         TEST_error("cannot support blocking mode without threads");
848         goto err;
849 #endif
850     }
851 
852     h->start_time = ossl_time_now();
853     h->init = 1;
854     return 1;
855 
856 err:
857     helper_cleanup(h);
858     return 0;
859 }
860 
helper_local_init(struct helper_local * hl,struct helper * h,int thread_idx)861 static int helper_local_init(struct helper_local *hl, struct helper *h,
862     int thread_idx)
863 {
864     hl->h = h;
865     hl->c_streams = NULL;
866     hl->thread_idx = thread_idx;
867     hl->explicit_event_handling = 0;
868 
869     if (!TEST_ptr(h))
870         return 0;
871 
872     if (thread_idx < 0) {
873         hl->c_streams = h->c_streams;
874     } else {
875         if (!TEST_ptr(hl->c_streams = lh_STREAM_INFO_new(stream_info_hash,
876                           stream_info_cmp)))
877             return 0;
878     }
879 
880     return 1;
881 }
882 
helper_local_cleanup(struct helper_local * hl)883 static void helper_local_cleanup(struct helper_local *hl)
884 {
885     if (hl->h == NULL)
886         return;
887 
888     if (hl->thread_idx >= 0)
889         helper_cleanup_streams(&hl->c_streams);
890 
891     hl->h = NULL;
892 }
893 
get_stream_info(LHASH_OF (STREAM_INFO)* lh,const char * stream_name)894 static STREAM_INFO *get_stream_info(LHASH_OF(STREAM_INFO) *lh,
895     const char *stream_name)
896 {
897     STREAM_INFO key, *info;
898 
899     if (!TEST_ptr(stream_name))
900         return NULL;
901 
902     if (!strcmp(stream_name, "DEFAULT"))
903         return NULL;
904 
905     key.name = stream_name;
906     info = lh_STREAM_INFO_retrieve(lh, &key);
907     if (info == NULL) {
908         info = OPENSSL_zalloc(sizeof(*info));
909         if (info == NULL)
910             return NULL;
911 
912         info->name = stream_name;
913         info->s_stream_id = UINT64_MAX;
914         lh_STREAM_INFO_insert(lh, info);
915     }
916 
917     return info;
918 }
919 
helper_local_set_c_stream(struct helper_local * hl,const char * stream_name,SSL * c_stream)920 static int helper_local_set_c_stream(struct helper_local *hl,
921     const char *stream_name,
922     SSL *c_stream)
923 {
924     STREAM_INFO *info = get_stream_info(hl->c_streams, stream_name);
925 
926     if (info == NULL)
927         return 0;
928 
929     info->c_stream = c_stream;
930     info->s_stream_id = UINT64_MAX;
931     return 1;
932 }
933 
helper_local_get_c_stream(struct helper_local * hl,const char * stream_name)934 static SSL *helper_local_get_c_stream(struct helper_local *hl,
935     const char *stream_name)
936 {
937     STREAM_INFO *info;
938 
939     if (!strcmp(stream_name, "DEFAULT"))
940         return hl->h->c_conn;
941 
942     info = get_stream_info(hl->c_streams, stream_name);
943     if (info == NULL)
944         return NULL;
945 
946     return info->c_stream;
947 }
948 
949 static int
helper_set_s_stream(struct helper * h,const char * stream_name,uint64_t s_stream_id)950 helper_set_s_stream(struct helper *h, const char *stream_name,
951     uint64_t s_stream_id)
952 {
953     STREAM_INFO *info;
954 
955     if (!strcmp(stream_name, "DEFAULT"))
956         return 0;
957 
958     info = get_stream_info(h->s_streams, stream_name);
959     if (info == NULL)
960         return 0;
961 
962     info->c_stream = NULL;
963     info->s_stream_id = s_stream_id;
964     return 1;
965 }
966 
helper_get_s_stream(struct helper * h,const char * stream_name)967 static uint64_t helper_get_s_stream(struct helper *h, const char *stream_name)
968 {
969     STREAM_INFO *info;
970 
971     if (!strcmp(stream_name, "DEFAULT"))
972         return UINT64_MAX;
973 
974     info = get_stream_info(h->s_streams, stream_name);
975     if (info == NULL)
976         return UINT64_MAX;
977 
978     return info->s_stream_id;
979 }
980 
helper_packet_plain_listener(QTEST_FAULT * qtf,QUIC_PKT_HDR * hdr,unsigned char * buf,size_t buf_len,void * arg)981 static int helper_packet_plain_listener(QTEST_FAULT *qtf, QUIC_PKT_HDR *hdr,
982     unsigned char *buf, size_t buf_len,
983     void *arg)
984 {
985     struct helper *h = arg;
986 
987     return h->qtf_packet_plain_cb(h, hdr, buf, buf_len);
988 }
989 
helper_handshake_listener(QTEST_FAULT * fault,unsigned char * buf,size_t buf_len,void * arg)990 static int helper_handshake_listener(QTEST_FAULT *fault,
991     unsigned char *buf, size_t buf_len,
992     void *arg)
993 {
994     struct helper *h = arg;
995 
996     return h->qtf_handshake_cb(h, buf, buf_len);
997 }
998 
helper_datagram_listener(QTEST_FAULT * fault,BIO_MSG * msg,size_t stride,void * arg)999 static int helper_datagram_listener(QTEST_FAULT *fault,
1000     BIO_MSG *msg, size_t stride,
1001     void *arg)
1002 {
1003     struct helper *h = arg;
1004 
1005     return h->qtf_datagram_cb(h, msg, stride);
1006 }
1007 
is_want(SSL * s,int ret)1008 static int is_want(SSL *s, int ret)
1009 {
1010     int ec = SSL_get_error(s, ret);
1011 
1012     return ec == SSL_ERROR_WANT_READ || ec == SSL_ERROR_WANT_WRITE;
1013 }
1014 
check_consistent_want(SSL * s,int ret)1015 static int check_consistent_want(SSL *s, int ret)
1016 {
1017     int ec = SSL_get_error(s, ret);
1018     int w = SSL_want(s);
1019 
1020     int ok = TEST_true(
1021         (ec == SSL_ERROR_NONE && w == SSL_NOTHING)
1022         || (ec == SSL_ERROR_ZERO_RETURN && w == SSL_NOTHING)
1023         || (ec == SSL_ERROR_SSL && w == SSL_NOTHING)
1024         || (ec == SSL_ERROR_SYSCALL && w == SSL_NOTHING)
1025         || (ec == SSL_ERROR_WANT_READ && w == SSL_READING)
1026         || (ec == SSL_ERROR_WANT_WRITE && w == SSL_WRITING)
1027         || (ec == SSL_ERROR_WANT_CLIENT_HELLO_CB && w == SSL_CLIENT_HELLO_CB)
1028         || (ec == SSL_ERROR_WANT_X509_LOOKUP && w == SSL_X509_LOOKUP)
1029         || (ec == SSL_ERROR_WANT_RETRY_VERIFY && w == SSL_RETRY_VERIFY));
1030 
1031     if (!ok)
1032         TEST_error("got error=%d, want=%d", ec, w);
1033 
1034     return ok;
1035 }
1036 
run_script_worker(struct helper * h,const struct script_op * script,const char * script_name,int thread_idx)1037 static int run_script_worker(struct helper *h, const struct script_op *script,
1038     const char *script_name,
1039     int thread_idx)
1040 {
1041     int testresult = 0;
1042     unsigned char *tmp_buf = NULL;
1043     int connect_started = 0;
1044     size_t offset = 0;
1045     size_t op_idx = 0;
1046     const struct script_op *op = NULL;
1047     int no_advance = 0, first = 1;
1048 #if defined(OPENSSL_THREADS)
1049     int end_wait_warning = 0;
1050 #endif
1051     OSSL_TIME op_start_time = ossl_time_zero(), op_deadline = ossl_time_zero();
1052     struct helper_local hl_, *hl = &hl_;
1053 #define REPEAT_SLOTS 8
1054     size_t repeat_stack_idx[REPEAT_SLOTS], repeat_stack_done[REPEAT_SLOTS];
1055     size_t repeat_stack_limit[REPEAT_SLOTS];
1056     size_t repeat_stack_len = 0;
1057 
1058     if (!TEST_true(helper_local_init(hl, h, thread_idx)))
1059         goto out;
1060 
1061 #define COMMON_SPIN_AGAIN() \
1062     {                       \
1063         no_advance = 1;     \
1064         continue;           \
1065     }
1066 #define S_SPIN_AGAIN()                \
1067     {                                 \
1068         s_lock(h, hl);                \
1069         ossl_quic_tserver_tick(h->s); \
1070         COMMON_SPIN_AGAIN();          \
1071     }
1072 #define C_SPIN_AGAIN()                                 \
1073     {                                                  \
1074         if (h->blocking) {                             \
1075             TEST_error("spin again in blocking mode"); \
1076             goto out;                                  \
1077         }                                              \
1078         COMMON_SPIN_AGAIN();                           \
1079     }
1080 
1081     for (;;) {
1082         SSL *c_tgt = h->c_conn;
1083         uint64_t s_stream_id = UINT64_MAX;
1084 
1085         s_unlock(h, hl);
1086 
1087         if (no_advance) {
1088             no_advance = 0;
1089         } else {
1090             if (!first)
1091                 ++op_idx;
1092 
1093             first = 0;
1094             offset = 0;
1095             op_start_time = ossl_time_now();
1096             op_deadline = ossl_time_add(op_start_time, ossl_ms2time(60000));
1097         }
1098 
1099         if (!TEST_int_le(ossl_time_compare(ossl_time_now(), op_deadline), 0)) {
1100             TEST_error("op %zu timed out on thread %d", op_idx + 1, thread_idx);
1101             goto out;
1102         }
1103 
1104         op = &script[op_idx];
1105 
1106         if (op->stream_name != NULL) {
1107             c_tgt = helper_local_get_c_stream(hl, op->stream_name);
1108             if (thread_idx < 0)
1109                 s_stream_id = helper_get_s_stream(h, op->stream_name);
1110             else
1111                 s_stream_id = UINT64_MAX;
1112         }
1113 
1114         if (thread_idx < 0) {
1115             if (!h->blocking) {
1116                 ossl_quic_tserver_tick(h->s);
1117             }
1118 #if defined(OPENSSL_THREADS)
1119             else if (h->blocking && !h->server_thread.ready) {
1120                 ossl_crypto_mutex_lock(h->server_thread.m);
1121                 h->server_thread.ready = 1;
1122                 ossl_crypto_condvar_signal(h->server_thread.c);
1123                 ossl_crypto_mutex_unlock(h->server_thread.m);
1124             }
1125             if (h->blocking)
1126                 assert(h->s == NULL);
1127 #endif
1128         }
1129 
1130         if (!hl->explicit_event_handling
1131             && (thread_idx >= 0 || connect_started))
1132             SSL_handle_events(h->c_conn);
1133 
1134         if (thread_idx >= 0) {
1135             /* Only allow certain opcodes on child threads. */
1136             switch (op->op) {
1137             case OPK_END:
1138             case OPK_CHECK:
1139             case OPK_C_ACCEPT_STREAM_WAIT:
1140             case OPK_C_NEW_STREAM:
1141             case OPK_C_READ_EXPECT:
1142             case OPK_C_EXPECT_FIN:
1143             case OPK_C_WRITE:
1144             case OPK_C_WRITE_EX2:
1145             case OPK_C_CONCLUDE:
1146             case OPK_C_FREE_STREAM:
1147             case OPK_BEGIN_REPEAT:
1148             case OPK_END_REPEAT:
1149             case OPK_C_READ_FAIL_WAIT:
1150             case OPK_C_EXPECT_SSL_ERR:
1151             case OPK_EXPECT_ERR_REASON:
1152             case OPK_EXPECT_ERR_LIB:
1153             case OPK_POP_ERR:
1154             case OPK_SLEEP:
1155                 break;
1156 
1157             default:
1158                 TEST_error("opcode %lu not allowed on child thread",
1159                     (unsigned long)op->op);
1160                 goto out;
1161             }
1162         }
1163 
1164         switch (op->op) {
1165         case OPK_END:
1166             if (!TEST_size_t_eq(repeat_stack_len, 0))
1167                 goto out;
1168 
1169 #if defined(OPENSSL_THREADS)
1170             if (thread_idx < 0) {
1171                 int done;
1172                 size_t i;
1173 
1174                 for (i = 0; i < h->num_threads; ++i) {
1175                     if (h->threads[i].m == NULL)
1176                         continue;
1177 
1178                     ossl_crypto_mutex_lock(h->threads[i].m);
1179                     done = h->threads[i].done;
1180                     ossl_crypto_mutex_unlock(h->threads[i].m);
1181 
1182                     if (!done) {
1183                         if (!end_wait_warning) {
1184                             TEST_info("still waiting for other threads to finish (%zu)", i);
1185                             end_wait_warning = 1;
1186                         }
1187 
1188                         S_SPIN_AGAIN();
1189                     }
1190                 }
1191             }
1192 #endif
1193 
1194             TEST_info("script \"%s\" finished on thread %d", script_name, thread_idx);
1195             testresult = 1;
1196             goto out;
1197 
1198         case OPK_BEGIN_REPEAT:
1199             if (!TEST_size_t_lt(repeat_stack_len, OSSL_NELEM(repeat_stack_idx)))
1200                 goto out;
1201 
1202             if (!TEST_size_t_gt(op->arg1, 0))
1203                 goto out;
1204 
1205             repeat_stack_idx[repeat_stack_len] = op_idx + 1;
1206             repeat_stack_done[repeat_stack_len] = 0;
1207             repeat_stack_limit[repeat_stack_len] = op->arg1;
1208             ++repeat_stack_len;
1209             break;
1210 
1211         case OPK_C_SKIP_IF_UNBOUND:
1212             if (c_tgt != NULL)
1213                 break;
1214 
1215             op_idx += op->arg1;
1216             break;
1217 
1218         case OPK_SKIP_IF_BLOCKING:
1219             if (!h->blocking)
1220                 break;
1221 
1222             op_idx += op->arg1;
1223             break;
1224 
1225         case OPK_END_REPEAT:
1226             if (!TEST_size_t_gt(repeat_stack_len, 0))
1227                 goto out;
1228 
1229             if (++repeat_stack_done[repeat_stack_len - 1]
1230                 == repeat_stack_limit[repeat_stack_len - 1]) {
1231                 --repeat_stack_len;
1232             } else {
1233                 op_idx = repeat_stack_idx[repeat_stack_len - 1];
1234                 no_advance = 1;
1235                 continue;
1236             }
1237 
1238             break;
1239 
1240         case OPK_CHECK: {
1241             int ok;
1242 
1243             hl->check_op = op;
1244             ok = op->check_func(h, hl);
1245             hl->check_op = NULL;
1246 
1247             if (thread_idx < 0 && h->check_spin_again) {
1248                 h->check_spin_again = 0;
1249                 S_SPIN_AGAIN();
1250             }
1251 
1252             if (!TEST_true(ok))
1253                 goto out;
1254         } break;
1255 
1256         case OPK_C_SET_ALPN: {
1257             const char *alpn = op->arg0;
1258             size_t alpn_len = strlen(alpn);
1259 
1260             if (!TEST_size_t_le(alpn_len, UINT8_MAX)
1261                 || !TEST_ptr(tmp_buf = (unsigned char *)OPENSSL_malloc(alpn_len + 1)))
1262                 goto out;
1263 
1264             memcpy(tmp_buf + 1, alpn, alpn_len);
1265             tmp_buf[0] = (unsigned char)alpn_len;
1266 
1267             /* 0 is the success case for SSL_set_alpn_protos(). */
1268             if (!TEST_false(SSL_set_alpn_protos(h->c_conn, tmp_buf,
1269                     alpn_len + 1)))
1270                 goto out;
1271 
1272             OPENSSL_free(tmp_buf);
1273             tmp_buf = NULL;
1274         } break;
1275 
1276         case OPK_C_CONNECT_WAIT: {
1277             int ret;
1278 
1279             connect_started = 1;
1280 
1281             ret = SSL_connect(h->c_conn);
1282             if (!check_consistent_want(c_tgt, ret))
1283                 goto out;
1284             if (ret != 1) {
1285                 if (!h->blocking && is_want(h->c_conn, ret))
1286                     C_SPIN_AGAIN();
1287 
1288                 if (op->arg1 == 0 && !TEST_int_eq(ret, 1))
1289                     goto out;
1290             }
1291         } break;
1292 
1293         case OPK_C_WRITE: {
1294             size_t bytes_written = 0;
1295             int r;
1296 
1297             if (!TEST_ptr(c_tgt))
1298                 goto out;
1299 
1300             r = SSL_write_ex(c_tgt, op->arg0, op->arg1, &bytes_written);
1301             if (!TEST_true(r)
1302                 || !check_consistent_want(c_tgt, r)
1303                 || !TEST_size_t_eq(bytes_written, op->arg1))
1304                 goto out;
1305         } break;
1306 
1307         case OPK_C_WRITE_EX2: {
1308             size_t bytes_written = 0;
1309             int r;
1310 
1311             if (!TEST_ptr(c_tgt))
1312                 goto out;
1313 
1314             r = SSL_write_ex2(c_tgt, op->arg0, op->arg1, op->arg2,
1315                 &bytes_written);
1316             if (!TEST_true(r)
1317                 || !check_consistent_want(c_tgt, r)
1318                 || !TEST_size_t_eq(bytes_written, op->arg1))
1319                 goto out;
1320         } break;
1321 
1322         case OPK_S_WRITE: {
1323             size_t bytes_written = 0;
1324 
1325             if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
1326                 goto out;
1327 
1328             if (!TEST_true(ossl_quic_tserver_write(ACQUIRE_S(), s_stream_id,
1329                     op->arg0, op->arg1,
1330                     &bytes_written))
1331                 || !TEST_size_t_eq(bytes_written, op->arg1))
1332                 goto out;
1333         } break;
1334 
1335         case OPK_C_CONCLUDE: {
1336             if (!TEST_true(SSL_stream_conclude(c_tgt, 0)))
1337                 goto out;
1338         } break;
1339 
1340         case OPK_S_CONCLUDE: {
1341             if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
1342                 goto out;
1343 
1344             ossl_quic_tserver_conclude(ACQUIRE_S(), s_stream_id);
1345         } break;
1346 
1347         case OPK_C_WAIT_FOR_DATA: {
1348             char buf[1];
1349             size_t bytes_read = 0;
1350 
1351             if (!TEST_ptr(c_tgt))
1352                 goto out;
1353 
1354             if (!SSL_peek_ex(c_tgt, buf, sizeof(buf), &bytes_read)
1355                 || bytes_read == 0)
1356                 C_SPIN_AGAIN();
1357         } break;
1358 
1359         case OPK_C_READ_EXPECT: {
1360             size_t bytes_read = 0;
1361             int r;
1362 
1363             if (op->arg1 > 0 && tmp_buf == NULL
1364                 && !TEST_ptr(tmp_buf = OPENSSL_malloc(op->arg1)))
1365                 goto out;
1366 
1367             r = SSL_read_ex(c_tgt, tmp_buf + offset, op->arg1 - offset,
1368                 &bytes_read);
1369             if (!check_consistent_want(c_tgt, r))
1370                 goto out;
1371 
1372             if (!r)
1373                 C_SPIN_AGAIN();
1374 
1375             if (bytes_read + offset != op->arg1) {
1376                 offset += bytes_read;
1377                 C_SPIN_AGAIN();
1378             }
1379 
1380             if (op->arg1 > 0
1381                 && !TEST_mem_eq(tmp_buf, op->arg1, op->arg0, op->arg1))
1382                 goto out;
1383 
1384             OPENSSL_free(tmp_buf);
1385             tmp_buf = NULL;
1386         } break;
1387 
1388         case OPK_S_READ_EXPECT: {
1389             size_t bytes_read = 0;
1390 
1391             if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
1392                 goto out;
1393 
1394             if (op->arg1 > 0 && tmp_buf == NULL
1395                 && !TEST_ptr(tmp_buf = OPENSSL_malloc(op->arg1)))
1396                 goto out;
1397 
1398             if (!TEST_true(ossl_quic_tserver_read(ACQUIRE_S(), s_stream_id,
1399                     tmp_buf + offset,
1400                     op->arg1 - offset,
1401                     &bytes_read)))
1402                 goto out;
1403 
1404             if (bytes_read + offset != op->arg1) {
1405                 offset += bytes_read;
1406                 S_SPIN_AGAIN();
1407             }
1408 
1409             if (op->arg1 > 0
1410                 && !TEST_mem_eq(tmp_buf, op->arg1, op->arg0, op->arg1))
1411                 goto out;
1412 
1413             OPENSSL_free(tmp_buf);
1414             tmp_buf = NULL;
1415         } break;
1416 
1417         case OPK_C_EXPECT_FIN: {
1418             char buf[1];
1419             size_t bytes_read = 0;
1420             int r;
1421 
1422             r = SSL_read_ex(c_tgt, buf, sizeof(buf), &bytes_read);
1423             if (!check_consistent_want(c_tgt, r)
1424                 || !TEST_false(r)
1425                 || !TEST_size_t_eq(bytes_read, 0))
1426                 goto out;
1427 
1428             if (is_want(c_tgt, 0))
1429                 C_SPIN_AGAIN();
1430 
1431             if (!TEST_int_eq(SSL_get_error(c_tgt, 0),
1432                     SSL_ERROR_ZERO_RETURN))
1433                 goto out;
1434 
1435             if (!TEST_int_eq(SSL_want(c_tgt), SSL_NOTHING))
1436                 goto out;
1437         } break;
1438 
1439         case OPK_S_EXPECT_FIN: {
1440             if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
1441                 goto out;
1442 
1443             if (!ossl_quic_tserver_has_read_ended(ACQUIRE_S(), s_stream_id))
1444                 S_SPIN_AGAIN();
1445         } break;
1446 
1447         case OPK_C_DETACH: {
1448             SSL *c_stream;
1449 
1450             if (!TEST_ptr_null(c_tgt))
1451                 goto out; /* don't overwrite existing stream with same name */
1452 
1453             if (!TEST_ptr(op->stream_name))
1454                 goto out;
1455 
1456             if (!TEST_ptr(c_stream = ossl_quic_detach_stream(h->c_conn)))
1457                 goto out;
1458 
1459             if (!TEST_true(helper_local_set_c_stream(hl, op->stream_name, c_stream)))
1460                 goto out;
1461         } break;
1462 
1463         case OPK_C_ATTACH: {
1464             if (!TEST_ptr(c_tgt))
1465                 goto out;
1466 
1467             if (!TEST_ptr(op->stream_name))
1468                 goto out;
1469 
1470             if (!TEST_true(ossl_quic_attach_stream(h->c_conn, c_tgt)))
1471                 goto out;
1472 
1473             if (!TEST_true(helper_local_set_c_stream(hl, op->stream_name, NULL)))
1474                 goto out;
1475         } break;
1476 
1477         case OPK_C_NEW_STREAM: {
1478             SSL *c_stream;
1479             uint64_t flags = op->arg1;
1480             int allow_fail = ((flags & ALLOW_FAIL) != 0);
1481 
1482             flags &= ~(uint64_t)ALLOW_FAIL;
1483 
1484             if (!TEST_ptr_null(c_tgt))
1485                 goto out; /* don't overwrite existing stream with same name */
1486 
1487             if (!TEST_ptr(op->stream_name))
1488                 goto out;
1489 
1490             c_stream = SSL_new_stream(h->c_conn, flags);
1491             if (!allow_fail && !TEST_ptr(c_stream))
1492                 goto out;
1493 
1494             if (allow_fail && c_stream == NULL) {
1495                 if (!TEST_size_t_eq(ERR_GET_REASON(ERR_get_error()),
1496                         SSL_R_STREAM_COUNT_LIMITED))
1497                     goto out;
1498 
1499                 ++h->fail_count;
1500                 break;
1501             }
1502 
1503             if (op->arg2 != UINT64_MAX
1504                 && !TEST_uint64_t_eq(SSL_get_stream_id(c_stream),
1505                     op->arg2))
1506                 goto out;
1507 
1508             if (!TEST_true(helper_local_set_c_stream(hl, op->stream_name, c_stream)))
1509                 goto out;
1510         } break;
1511 
1512         case OPK_S_NEW_STREAM: {
1513             uint64_t stream_id = UINT64_MAX;
1514 
1515             if (!TEST_uint64_t_eq(s_stream_id, UINT64_MAX))
1516                 goto out; /* don't overwrite existing stream with same name */
1517 
1518             if (!TEST_ptr(op->stream_name))
1519                 goto out;
1520 
1521             if (!TEST_true(ossl_quic_tserver_stream_new(ACQUIRE_S(),
1522                     op->arg1 > 0,
1523                     &stream_id)))
1524                 goto out;
1525 
1526             if (op->arg2 != UINT64_MAX
1527                 && !TEST_uint64_t_eq(stream_id, op->arg2))
1528                 goto out;
1529 
1530             if (!TEST_true(helper_set_s_stream(h, op->stream_name,
1531                     stream_id)))
1532                 goto out;
1533         } break;
1534 
1535         case OPK_C_ACCEPT_STREAM_WAIT: {
1536             SSL *c_stream;
1537 
1538             if (!TEST_ptr_null(c_tgt))
1539                 goto out; /* don't overwrite existing stream with same name */
1540 
1541             if (!TEST_ptr(op->stream_name))
1542                 goto out;
1543 
1544             if ((c_stream = SSL_accept_stream(h->c_conn, 0)) == NULL)
1545                 C_SPIN_AGAIN();
1546 
1547             if (!TEST_true(helper_local_set_c_stream(hl, op->stream_name,
1548                     c_stream)))
1549                 goto out;
1550         } break;
1551 
1552         case OPK_S_ACCEPT_STREAM_WAIT: {
1553             uint64_t new_stream_id;
1554 
1555             if (!TEST_uint64_t_eq(s_stream_id, UINT64_MAX))
1556                 goto out;
1557 
1558             if (!TEST_ptr(op->stream_name))
1559                 goto out;
1560 
1561             new_stream_id = ossl_quic_tserver_pop_incoming_stream(ACQUIRE_S());
1562             if (new_stream_id == UINT64_MAX)
1563                 S_SPIN_AGAIN();
1564 
1565             if (!TEST_true(helper_set_s_stream(h, op->stream_name, new_stream_id)))
1566                 goto out;
1567         } break;
1568 
1569         case OPK_C_ACCEPT_STREAM_NONE: {
1570             SSL *c_stream;
1571 
1572             if (!TEST_ptr_null(c_stream = SSL_accept_stream(h->c_conn,
1573                                    SSL_ACCEPT_STREAM_NO_BLOCK))) {
1574                 SSL_free(c_stream);
1575                 goto out;
1576             }
1577         } break;
1578 
1579         case OPK_C_FREE_STREAM: {
1580             if (!TEST_ptr(c_tgt)
1581                 || !TEST_true(!SSL_is_connection(c_tgt)))
1582                 goto out;
1583 
1584             if (!TEST_ptr(op->stream_name))
1585                 goto out;
1586 
1587             if (!TEST_true(helper_local_set_c_stream(hl, op->stream_name, NULL)))
1588                 goto out;
1589 
1590             SSL_free(c_tgt);
1591             c_tgt = NULL;
1592         } break;
1593 
1594         case OPK_C_SET_DEFAULT_STREAM_MODE: {
1595             if (!TEST_ptr(c_tgt))
1596                 goto out;
1597 
1598             if (!TEST_true(SSL_set_default_stream_mode(c_tgt, op->arg1)))
1599                 goto out;
1600         } break;
1601 
1602         case OPK_C_SET_INCOMING_STREAM_POLICY: {
1603             if (!TEST_ptr(c_tgt))
1604                 goto out;
1605 
1606             if (!TEST_true(SSL_set_incoming_stream_policy(c_tgt,
1607                     op->arg1, 0)))
1608                 goto out;
1609         } break;
1610 
1611         case OPK_C_SHUTDOWN_WAIT: {
1612             int ret;
1613             QUIC_CHANNEL *ch = ossl_quic_conn_get_channel(h->c_conn);
1614             SSL_SHUTDOWN_EX_ARGS args = { 0 };
1615 
1616             ossl_quic_engine_set_inhibit_tick(ossl_quic_channel_get0_engine(ch), 0);
1617 
1618             if (!TEST_ptr(c_tgt))
1619                 goto out;
1620 
1621             args.quic_reason = (const char *)op->arg0;
1622 
1623             ret = SSL_shutdown_ex(c_tgt, op->arg1, &args, sizeof(args));
1624             if (!TEST_int_ge(ret, 0))
1625                 goto out;
1626 
1627             if (ret == 0)
1628                 C_SPIN_AGAIN();
1629         } break;
1630 
1631         case OPK_S_SHUTDOWN: {
1632             ossl_quic_tserver_shutdown(ACQUIRE_S(), op->arg1);
1633         } break;
1634 
1635         case OPK_C_EXPECT_CONN_CLOSE_INFO: {
1636             SSL_CONN_CLOSE_INFO cc_info = { 0 };
1637             int expect_app = (op->arg1 & EXPECT_CONN_CLOSE_APP) != 0;
1638             int expect_remote = (op->arg1 & EXPECT_CONN_CLOSE_REMOTE) != 0;
1639             uint64_t error_code = op->arg2;
1640 
1641             if (!TEST_ptr(c_tgt))
1642                 goto out;
1643 
1644             if (h->blocking
1645                 && !TEST_true(SSL_shutdown_ex(c_tgt,
1646                     SSL_SHUTDOWN_FLAG_WAIT_PEER,
1647                     NULL, 0)))
1648                 goto out;
1649 
1650             if (!SSL_get_conn_close_info(c_tgt, &cc_info, sizeof(cc_info)))
1651                 C_SPIN_AGAIN();
1652 
1653             if (!TEST_int_eq(expect_app,
1654                     (cc_info.flags
1655                         & SSL_CONN_CLOSE_FLAG_TRANSPORT)
1656                         == 0)
1657                 || !TEST_int_eq(expect_remote,
1658                     (cc_info.flags
1659                         & SSL_CONN_CLOSE_FLAG_LOCAL)
1660                         == 0)
1661                 || !TEST_uint64_t_eq(error_code, cc_info.error_code)) {
1662                 TEST_info("Connection close reason: %s", cc_info.reason);
1663                 goto out;
1664             }
1665         } break;
1666 
1667         case OPK_S_EXPECT_CONN_CLOSE_INFO: {
1668             const QUIC_TERMINATE_CAUSE *tc;
1669             int expect_app = (op->arg1 & EXPECT_CONN_CLOSE_APP) != 0;
1670             int expect_remote = (op->arg1 & EXPECT_CONN_CLOSE_REMOTE) != 0;
1671             uint64_t error_code = op->arg2;
1672 
1673             if (!ossl_quic_tserver_is_term_any(ACQUIRE_S())) {
1674                 ossl_quic_tserver_ping(ACQUIRE_S());
1675                 S_SPIN_AGAIN();
1676             }
1677 
1678             if (!TEST_ptr(tc = ossl_quic_tserver_get_terminate_cause(ACQUIRE_S())))
1679                 goto out;
1680 
1681             if (!TEST_uint64_t_eq(error_code, tc->error_code)
1682                 || !TEST_int_eq(expect_app, tc->app)
1683                 || !TEST_int_eq(expect_remote, tc->remote))
1684                 goto out;
1685         } break;
1686 
1687         case OPK_S_BIND_STREAM_ID: {
1688             if (!TEST_uint64_t_eq(s_stream_id, UINT64_MAX))
1689                 goto out;
1690 
1691             if (!TEST_ptr(op->stream_name))
1692                 goto out;
1693 
1694             if (!TEST_true(helper_set_s_stream(h, op->stream_name, op->arg2)))
1695                 goto out;
1696         } break;
1697 
1698         case OPK_S_UNBIND_STREAM_ID: {
1699             if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
1700                 goto out;
1701 
1702             if (!TEST_ptr(op->stream_name))
1703                 goto out;
1704 
1705             if (!TEST_true(helper_set_s_stream(h, op->stream_name, UINT64_MAX)))
1706                 goto out;
1707         } break;
1708 
1709         case OPK_C_WRITE_FAIL: {
1710             size_t bytes_written = 0;
1711             int r;
1712 
1713             if (!TEST_ptr(c_tgt))
1714                 goto out;
1715 
1716             r = SSL_write_ex(c_tgt, "apple", 5, &bytes_written);
1717             if (!TEST_false(r)
1718                 || !check_consistent_want(c_tgt, r))
1719                 goto out;
1720         } break;
1721 
1722         case OPK_S_WRITE_FAIL: {
1723             size_t bytes_written = 0;
1724 
1725             if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
1726                 goto out;
1727 
1728             if (!TEST_false(ossl_quic_tserver_write(ACQUIRE_S(), s_stream_id,
1729                     (const unsigned char *)"apple", 5,
1730                     &bytes_written)))
1731                 goto out;
1732         } break;
1733 
1734         case OPK_C_READ_FAIL: {
1735             size_t bytes_read = 0;
1736             char buf[1];
1737             int r;
1738 
1739             if (!TEST_ptr(c_tgt))
1740                 goto out;
1741 
1742             r = SSL_read_ex(c_tgt, buf, sizeof(buf), &bytes_read);
1743             if (!TEST_false(r))
1744                 goto out;
1745             if (!check_consistent_want(c_tgt, r))
1746                 goto out;
1747         } break;
1748 
1749         case OPK_C_READ_FAIL_WAIT: {
1750             size_t bytes_read = 0;
1751             char buf[1];
1752             int r;
1753 
1754             if (!TEST_ptr(c_tgt))
1755                 goto out;
1756 
1757             r = SSL_read_ex(c_tgt, buf, sizeof(buf), &bytes_read);
1758             if (!TEST_false(r))
1759                 goto out;
1760             if (!check_consistent_want(c_tgt, r))
1761                 goto out;
1762 
1763             if (is_want(c_tgt, 0))
1764                 C_SPIN_AGAIN();
1765         } break;
1766 
1767         case OPK_S_READ_FAIL: {
1768             int ret;
1769             size_t bytes_read = 0;
1770             unsigned char buf[1];
1771 
1772             if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
1773                 goto out;
1774 
1775             ret = ossl_quic_tserver_read(ACQUIRE_S(), s_stream_id,
1776                 buf, sizeof(buf),
1777                 &bytes_read);
1778             if (!TEST_true(ret == 0 || (op->arg1 && bytes_read == 0)))
1779                 goto out;
1780         } break;
1781 
1782         case OPK_C_STREAM_RESET:
1783         case OPK_C_STREAM_RESET_FAIL: {
1784             SSL_STREAM_RESET_ARGS args = { 0 };
1785 
1786             if (!TEST_ptr(c_tgt))
1787                 goto out;
1788 
1789             args.quic_error_code = op->arg2;
1790             if (op->op == OPK_C_STREAM_RESET) {
1791                 if (!TEST_true(SSL_stream_reset(c_tgt, &args, sizeof(args))))
1792                     goto out;
1793             } else {
1794                 if (!TEST_false(SSL_stream_reset(c_tgt, &args, sizeof(args))))
1795                     goto out;
1796             }
1797         } break;
1798 
1799         case OPK_NEW_THREAD: {
1800 #if !defined(OPENSSL_THREADS)
1801             /*
1802              * If this test script requires threading and we do not have
1803              * support for it, skip the rest of it.
1804              */
1805             TEST_skip("threading not supported, skipping");
1806             testresult = 1;
1807             goto out;
1808 #else
1809             size_t i;
1810 
1811             if (!TEST_ptr_null(h->threads)) {
1812                 TEST_error("max one NEW_THREAD operation per script");
1813                 goto out;
1814             }
1815 
1816             h->threads = OPENSSL_zalloc(op->arg1 * sizeof(struct child_thread_args));
1817             if (!TEST_ptr(h->threads))
1818                 goto out;
1819 
1820             h->num_threads = op->arg1;
1821 
1822             for (i = 0; i < op->arg1; ++i) {
1823                 h->threads[i].h = h;
1824                 h->threads[i].script = op->arg0;
1825                 h->threads[i].script_name = script_name;
1826                 h->threads[i].thread_idx = i;
1827 
1828                 h->threads[i].m = ossl_crypto_mutex_new();
1829                 if (!TEST_ptr(h->threads[i].m))
1830                     goto out;
1831 
1832                 h->threads[i].t
1833                     = ossl_crypto_thread_native_start(run_script_child_thread,
1834                         &h->threads[i], 1);
1835                 if (!TEST_ptr(h->threads[i].t))
1836                     goto out;
1837             }
1838 #endif
1839         } break;
1840 
1841         case OPK_C_CLOSE_SOCKET: {
1842             BIO_closesocket(h->c_fd);
1843             h->c_fd = -1;
1844         } break;
1845 
1846         case OPK_C_EXPECT_SSL_ERR: {
1847             if (!TEST_size_t_eq((size_t)SSL_get_error(c_tgt, 0), op->arg1))
1848                 goto out;
1849             if (!TEST_int_eq(SSL_want(c_tgt), SSL_NOTHING))
1850                 goto out;
1851         } break;
1852 
1853         case OPK_EXPECT_ERR_REASON: {
1854             if (!TEST_size_t_eq((size_t)ERR_GET_REASON(ERR_peek_last_error()), op->arg1))
1855                 goto out;
1856         } break;
1857 
1858         case OPK_EXPECT_ERR_LIB: {
1859             if (!TEST_size_t_eq((size_t)ERR_GET_LIB(ERR_peek_last_error()), op->arg1))
1860                 goto out;
1861         } break;
1862 
1863         case OPK_POP_ERR:
1864             ERR_pop();
1865             break;
1866 
1867         case OPK_SLEEP: {
1868             OSSL_sleep(op->arg2);
1869         } break;
1870 
1871         case OPK_S_SET_INJECT_PLAIN:
1872             h->qtf_packet_plain_cb = op->qtf_packet_plain_cb;
1873 
1874             if (!TEST_true(qtest_fault_set_packet_plain_listener(h->qtf,
1875                     h->qtf_packet_plain_cb != NULL ? helper_packet_plain_listener : NULL,
1876                     h)))
1877                 goto out;
1878 
1879             break;
1880 
1881         case OPK_S_SET_INJECT_HANDSHAKE:
1882             h->qtf_handshake_cb = op->qtf_handshake_cb;
1883 
1884             if (!TEST_true(qtest_fault_set_handshake_listener(h->qtf,
1885                     h->qtf_handshake_cb != NULL ? helper_handshake_listener : NULL,
1886                     h)))
1887                 goto out;
1888 
1889             break;
1890 
1891         case OPK_S_SET_INJECT_DATAGRAM:
1892             h->qtf_datagram_cb = op->qtf_datagram_cb;
1893 
1894             if (!TEST_true(qtest_fault_set_datagram_listener(h->qtf,
1895                     h->qtf_datagram_cb != NULL ? helper_datagram_listener : NULL,
1896                     h)))
1897                 goto out;
1898 
1899             break;
1900 
1901         case OPK_SET_INJECT_WORD:
1902             /*
1903              * Must hold server tick lock - callbacks can be called from other
1904              * thread when running test in blocking mode (tsan).
1905              */
1906             ACQUIRE_S();
1907             h->inject_word0 = op->arg1;
1908             h->inject_word1 = op->arg2;
1909             break;
1910 
1911         case OPK_C_INHIBIT_TICK: {
1912             QUIC_CHANNEL *ch = ossl_quic_conn_get_channel(h->c_conn);
1913 
1914             ossl_quic_engine_set_inhibit_tick(ossl_quic_channel_get0_engine(ch),
1915                 op->arg1);
1916         } break;
1917 
1918         case OPK_C_SET_WRITE_BUF_SIZE:
1919             if (!TEST_ptr(c_tgt))
1920                 goto out;
1921 
1922             if (!TEST_true(ossl_quic_set_write_buffer_size(c_tgt, op->arg1)))
1923                 goto out;
1924 
1925             break;
1926 
1927         case OPK_S_NEW_TICKET:
1928             if (!TEST_true(ossl_quic_tserver_new_ticket(ACQUIRE_S())))
1929                 goto out;
1930             break;
1931 
1932         default:
1933             TEST_error("unknown op");
1934             goto out;
1935         }
1936     }
1937 
1938 out:
1939     s_unlock(h, hl); /* idempotent */
1940     if (!testresult) {
1941         size_t i;
1942         const QUIC_TERMINATE_CAUSE *tcause;
1943         const char *e_str, *f_str;
1944 
1945         TEST_error("failed in script \"%s\" at op %zu, thread %d\n",
1946             script_name, op_idx + 1, thread_idx);
1947 
1948         for (i = 0; i < repeat_stack_len; ++i)
1949             TEST_info("while repeating, iteration %zu of %zu, starting at script op %zu",
1950                 repeat_stack_done[i],
1951                 repeat_stack_limit[i],
1952                 repeat_stack_idx[i]);
1953 
1954         ERR_print_errors_fp(stderr);
1955 
1956         if (h->c_conn != NULL) {
1957             SSL_CONN_CLOSE_INFO cc_info = { 0 };
1958 
1959             if (SSL_get_conn_close_info(h->c_conn, &cc_info, sizeof(cc_info))) {
1960                 e_str = ossl_quic_err_to_string(cc_info.error_code);
1961                 f_str = ossl_quic_frame_type_to_string(cc_info.frame_type);
1962 
1963                 if (e_str == NULL)
1964                     e_str = "?";
1965                 if (f_str == NULL)
1966                     f_str = "?";
1967 
1968                 TEST_info("client side is closed: %llu(%s)/%llu(%s), "
1969                           "%s, %s, reason: \"%s\"",
1970                     (unsigned long long)cc_info.error_code,
1971                     e_str,
1972                     (unsigned long long)cc_info.frame_type,
1973                     f_str,
1974                     (cc_info.flags & SSL_CONN_CLOSE_FLAG_LOCAL) != 0
1975                         ? "local"
1976                         : "remote",
1977                     (cc_info.flags & SSL_CONN_CLOSE_FLAG_TRANSPORT) != 0
1978                         ? "transport"
1979                         : "app",
1980                     cc_info.reason != NULL ? cc_info.reason : "-");
1981             }
1982         }
1983 
1984         tcause = (h->s != NULL
1985                 ? ossl_quic_tserver_get_terminate_cause(h->s)
1986                 : NULL);
1987         if (tcause != NULL) {
1988             e_str = ossl_quic_err_to_string(tcause->error_code);
1989             f_str = ossl_quic_frame_type_to_string(tcause->frame_type);
1990 
1991             if (e_str == NULL)
1992                 e_str = "?";
1993             if (f_str == NULL)
1994                 f_str = "?";
1995 
1996             TEST_info("server side is closed: %llu(%s)/%llu(%s), "
1997                       "%s, %s, reason: \"%s\"",
1998                 (unsigned long long)tcause->error_code,
1999                 e_str,
2000                 (unsigned long long)tcause->frame_type,
2001                 f_str,
2002                 tcause->remote ? "remote" : "local",
2003                 tcause->app ? "app" : "transport",
2004                 tcause->reason != NULL ? tcause->reason : "-");
2005         }
2006     }
2007 
2008     OPENSSL_free(tmp_buf);
2009     helper_local_cleanup(hl);
2010     return testresult;
2011 }
2012 
run_script(const struct script_op * script,const char * script_name,int free_order,int blocking)2013 static int run_script(const struct script_op *script,
2014     const char *script_name,
2015     int free_order,
2016     int blocking)
2017 {
2018     int testresult = 0;
2019     struct helper h;
2020 
2021     if (!TEST_true(helper_init(&h, script_name,
2022             free_order, blocking, 1)))
2023         goto out;
2024 
2025     if (!TEST_true(run_script_worker(&h, script, script_name, -1)))
2026         goto out;
2027 
2028 #if defined(OPENSSL_THREADS)
2029     if (!TEST_true(join_threads(h.threads, h.num_threads)))
2030         goto out;
2031 #endif
2032 
2033     testresult = 1;
2034 out:
2035     helper_cleanup(&h);
2036     return testresult;
2037 }
2038 
2039 #if defined(OPENSSL_THREADS)
run_script_child_thread(void * arg)2040 static CRYPTO_THREAD_RETVAL run_script_child_thread(void *arg)
2041 {
2042     int testresult;
2043     struct child_thread_args *args = arg;
2044 
2045     testresult = run_script_worker(args->h, args->script,
2046         args->script_name,
2047         args->thread_idx);
2048 
2049     ossl_crypto_mutex_lock(args->m);
2050     args->testresult = testresult;
2051     args->done = 1;
2052     ossl_crypto_mutex_unlock(args->m);
2053     return 1;
2054 }
2055 #endif
2056 
2057 /* 1. Simple single-stream test */
2058 static const struct script_op script_1[] = {
2059     OP_C_SET_ALPN("ossltest")
2060         OP_C_CONNECT_WAIT()
2061             OP_C_WRITE(DEFAULT, "apple", 5)
2062                 OP_C_CONCLUDE(DEFAULT)
2063                     OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
2064                         OP_S_READ_EXPECT(a, "apple", 5)
2065                             OP_S_EXPECT_FIN(a)
2066                                 OP_S_WRITE(a, "orange", 6)
2067                                     OP_S_CONCLUDE(a)
2068                                         OP_C_READ_EXPECT(DEFAULT, "orange", 6)
2069                                             OP_C_EXPECT_FIN(DEFAULT)
2070                                                 OP_END
2071 };
2072 
2073 /* 2. Multi-stream test */
2074 static const struct script_op script_2[] = {
2075     OP_C_SET_ALPN("ossltest")
2076         OP_C_CONNECT_WAIT()
2077             OP_C_SET_INCOMING_STREAM_POLICY(SSL_INCOMING_STREAM_POLICY_ACCEPT)
2078                 OP_C_WRITE(DEFAULT, "apple", 5)
2079                     OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
2080                         OP_S_READ_EXPECT(a, "apple", 5)
2081                             OP_S_WRITE(a, "orange", 6)
2082                                 OP_C_READ_EXPECT(DEFAULT, "orange", 6)
2083 
2084                                     OP_C_NEW_STREAM_BIDI(b, C_BIDI_ID(1))
2085                                         OP_C_WRITE(b, "flamingo", 8)
2086                                             OP_C_CONCLUDE(b)
2087                                                 OP_S_BIND_STREAM_ID(b, C_BIDI_ID(1))
2088                                                     OP_S_READ_EXPECT(b, "flamingo", 8)
2089                                                         OP_S_EXPECT_FIN(b)
2090                                                             OP_S_WRITE(b, "gargoyle", 8)
2091                                                                 OP_S_CONCLUDE(b)
2092                                                                     OP_C_READ_EXPECT(b, "gargoyle", 8)
2093                                                                         OP_C_EXPECT_FIN(b)
2094 
2095                                                                             OP_C_NEW_STREAM_UNI(c, C_UNI_ID(0))
2096                                                                                 OP_C_WRITE(c, "elephant", 8)
2097                                                                                     OP_C_CONCLUDE(c)
2098                                                                                         OP_S_BIND_STREAM_ID(c, C_UNI_ID(0))
2099                                                                                             OP_S_READ_EXPECT(c, "elephant", 8)
2100                                                                                                 OP_S_EXPECT_FIN(c)
2101                                                                                                     OP_S_WRITE_FAIL(c)
2102 
2103                                                                                                         OP_C_ACCEPT_STREAM_NONE()
2104 
2105                                                                                                             OP_S_NEW_STREAM_BIDI(d, S_BIDI_ID(0))
2106                                                                                                                 OP_S_WRITE(d, "frog", 4)
2107                                                                                                                     OP_S_CONCLUDE(d)
2108 
2109                                                                                                                         OP_C_ACCEPT_STREAM_WAIT(d)
2110                                                                                                                             OP_C_ACCEPT_STREAM_NONE()
2111                                                                                                                                 OP_C_READ_EXPECT(d, "frog", 4)
2112                                                                                                                                     OP_C_EXPECT_FIN(d)
2113 
2114                                                                                                                                         OP_S_NEW_STREAM_BIDI(e, S_BIDI_ID(1))
2115                                                                                                                                             OP_S_WRITE(e, "mixture", 7)
2116                                                                                                                                                 OP_S_CONCLUDE(e)
2117 
2118                                                                                                                                                     OP_C_ACCEPT_STREAM_WAIT(e)
2119                                                                                                                                                         OP_C_READ_EXPECT(e, "mixture", 7)
2120                                                                                                                                                             OP_C_EXPECT_FIN(e)
2121                                                                                                                                                                 OP_C_WRITE(e, "ramble", 6)
2122                                                                                                                                                                     OP_S_READ_EXPECT(e, "ramble", 6)
2123                                                                                                                                                                         OP_C_CONCLUDE(e)
2124                                                                                                                                                                             OP_S_EXPECT_FIN(e)
2125 
2126                                                                                                                                                                                 OP_S_NEW_STREAM_UNI(f, S_UNI_ID(0))
2127                                                                                                                                                                                     OP_S_WRITE(f, "yonder", 6)
2128                                                                                                                                                                                         OP_S_CONCLUDE(f)
2129 
2130                                                                                                                                                                                             OP_C_ACCEPT_STREAM_WAIT(f)
2131                                                                                                                                                                                                 OP_C_ACCEPT_STREAM_NONE()
2132                                                                                                                                                                                                     OP_C_READ_EXPECT(f, "yonder", 6)
2133                                                                                                                                                                                                         OP_C_EXPECT_FIN(f)
2134                                                                                                                                                                                                             OP_C_WRITE_FAIL(f)
2135 
2136                                                                                                                                                                                                                 OP_C_SET_INCOMING_STREAM_POLICY(SSL_INCOMING_STREAM_POLICY_REJECT)
2137                                                                                                                                                                                                                     OP_S_NEW_STREAM_BIDI(g, S_BIDI_ID(2))
2138                                                                                                                                                                                                                         OP_S_WRITE(g, "unseen", 6)
2139                                                                                                                                                                                                                             OP_S_CONCLUDE(g)
2140 
2141                                                                                                                                                                                                                                 OP_C_ACCEPT_STREAM_NONE()
2142 
2143                                                                                                                                                                                                                                     OP_C_SET_INCOMING_STREAM_POLICY(SSL_INCOMING_STREAM_POLICY_AUTO)
2144                                                                                                                                                                                                                                         OP_S_NEW_STREAM_BIDI(h, S_BIDI_ID(3))
2145                                                                                                                                                                                                                                             OP_S_WRITE(h, "UNSEEN", 6)
2146                                                                                                                                                                                                                                                 OP_S_CONCLUDE(h)
2147 
2148                                                                                                                                                                                                                                                     OP_C_ACCEPT_STREAM_NONE()
2149 
2150     /*
2151      * Streams g, h should have been rejected, so server should have got
2152      * STOP_SENDING/RESET_STREAM.
2153      */
2154     OP_CHECK(check_rejected, S_BIDI_ID(2))
2155         OP_CHECK(check_rejected, S_BIDI_ID(3))
2156 
2157             OP_END
2158 };
2159 
2160 /* 3. Default stream detach/reattach test */
2161 static const struct script_op script_3[] = {
2162     OP_C_SET_ALPN("ossltest")
2163         OP_C_CONNECT_WAIT()
2164 
2165             OP_C_WRITE(DEFAULT, "apple", 5)
2166                 OP_C_DETACH(a) /* DEFAULT becomes stream 'a' */
2167     OP_C_WRITE_FAIL(DEFAULT)
2168 
2169         OP_C_WRITE(a, "by", 2)
2170 
2171             OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
2172                 OP_S_READ_EXPECT(a, "appleby", 7)
2173 
2174                     OP_S_WRITE(a, "hello", 5)
2175                         OP_C_READ_EXPECT(a, "hello", 5)
2176 
2177                             OP_C_WRITE_FAIL(DEFAULT)
2178                                 OP_C_ATTACH(a)
2179                                     OP_C_WRITE(DEFAULT, "is here", 7)
2180                                         OP_S_READ_EXPECT(a, "is here", 7)
2181 
2182                                             OP_C_DETACH(a)
2183                                                 OP_C_CONCLUDE(a)
2184                                                     OP_S_EXPECT_FIN(a)
2185 
2186                                                         OP_END
2187 };
2188 
2189 /* 4. Default stream mode test */
2190 static const struct script_op script_4[] = {
2191     OP_C_SET_ALPN("ossltest")
2192         OP_C_CONNECT_WAIT()
2193 
2194             OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2195                 OP_C_WRITE_FAIL(DEFAULT)
2196 
2197                     OP_S_NEW_STREAM_BIDI(a, S_BIDI_ID(0))
2198                         OP_S_WRITE(a, "apple", 5)
2199 
2200                             OP_C_READ_FAIL(DEFAULT)
2201 
2202                                 OP_C_ACCEPT_STREAM_WAIT(a)
2203                                     OP_C_READ_EXPECT(a, "apple", 5)
2204 
2205                                         OP_C_ATTACH(a)
2206                                             OP_C_WRITE(DEFAULT, "orange", 6)
2207                                                 OP_S_READ_EXPECT(a, "orange", 6)
2208 
2209                                                     OP_END
2210 };
2211 
2212 /* 5. Test stream reset functionality */
2213 static const struct script_op script_5[] = {
2214     OP_C_SET_ALPN("ossltest")
2215         OP_C_CONNECT_WAIT()
2216 
2217             OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2218                 OP_C_NEW_STREAM_BIDI(a, C_BIDI_ID(0))
2219                     OP_C_NEW_STREAM_BIDI(b, C_BIDI_ID(1))
2220 
2221                         OP_C_WRITE(a, "apple", 5)
2222                             OP_C_STREAM_RESET(a, 42)
2223 
2224                                 OP_C_WRITE(b, "strawberry", 10)
2225 
2226                                     OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
2227                                         OP_S_BIND_STREAM_ID(b, C_BIDI_ID(1))
2228                                             OP_S_READ_EXPECT(b, "strawberry", 10)
2229     /* Reset disrupts read of already sent data */
2230     OP_S_READ_FAIL(a, 0)
2231         OP_CHECK(check_stream_reset, C_BIDI_ID(0))
2232 
2233             OP_END
2234 };
2235 
2236 /* 6. Test STOP_SENDING functionality */
2237 static const struct script_op script_6[] = {
2238     OP_C_SET_ALPN("ossltest")
2239         OP_C_CONNECT_WAIT()
2240 
2241             OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2242                 OP_S_NEW_STREAM_BIDI(a, S_BIDI_ID(0))
2243                     OP_S_WRITE(a, "apple", 5)
2244 
2245                         OP_C_ACCEPT_STREAM_WAIT(a)
2246                             OP_C_FREE_STREAM(a)
2247                                 OP_C_ACCEPT_STREAM_NONE()
2248 
2249                                     OP_CHECK(check_stream_stopped, S_BIDI_ID(0))
2250 
2251                                         OP_END
2252 };
2253 
2254 /* 7. Unidirectional default stream mode test (client sends first) */
2255 static const struct script_op script_7[] = {
2256     OP_C_SET_ALPN("ossltest")
2257         OP_C_CONNECT_WAIT()
2258 
2259             OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
2260                 OP_C_WRITE(DEFAULT, "apple", 5)
2261 
2262                     OP_S_BIND_STREAM_ID(a, C_UNI_ID(0))
2263                         OP_S_READ_EXPECT(a, "apple", 5)
2264                             OP_S_WRITE_FAIL(a)
2265 
2266                                 OP_END
2267 };
2268 
2269 /* 8. Unidirectional default stream mode test (server sends first) */
2270 static const struct script_op script_8[] = {
2271     OP_C_SET_ALPN("ossltest")
2272         OP_C_CONNECT_WAIT()
2273 
2274             OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
2275                 OP_S_NEW_STREAM_UNI(a, S_UNI_ID(0))
2276                     OP_S_WRITE(a, "apple", 5)
2277                         OP_C_READ_EXPECT(DEFAULT, "apple", 5)
2278                             OP_C_WRITE_FAIL(DEFAULT)
2279 
2280                                 OP_END
2281 };
2282 
2283 /* 9. Unidirectional default stream mode test (server sends first on bidi) */
2284 static const struct script_op script_9[] = {
2285     OP_C_SET_ALPN("ossltest")
2286         OP_C_CONNECT_WAIT()
2287 
2288             OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
2289                 OP_S_NEW_STREAM_BIDI(a, S_BIDI_ID(0))
2290                     OP_S_WRITE(a, "apple", 5)
2291                         OP_C_READ_EXPECT(DEFAULT, "apple", 5)
2292                             OP_C_WRITE(DEFAULT, "orange", 6)
2293                                 OP_S_READ_EXPECT(a, "orange", 6)
2294 
2295                                     OP_END
2296 };
2297 
2298 /* 10. Shutdown */
2299 static const struct script_op script_10[] = {
2300     OP_C_SET_ALPN("ossltest")
2301         OP_C_CONNECT_WAIT()
2302 
2303             OP_C_WRITE(DEFAULT, "apple", 5)
2304                 OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
2305                     OP_S_READ_EXPECT(a, "apple", 5)
2306 
2307                         OP_C_SHUTDOWN_WAIT(NULL, 0)
2308                             OP_C_EXPECT_CONN_CLOSE_INFO(0, 1, 0)
2309                                 OP_S_EXPECT_CONN_CLOSE_INFO(0, 1, 1)
2310 
2311                                     OP_END
2312 };
2313 
2314 /* 11. Many threads accepted on the same client connection */
2315 static const struct script_op script_11_child[] = {
2316     OP_C_ACCEPT_STREAM_WAIT(a)
2317         OP_C_READ_EXPECT(a, "foo", 3)
2318             OP_SLEEP(10)
2319                 OP_C_EXPECT_FIN(a)
2320 
2321                     OP_END
2322 };
2323 
2324 static const struct script_op script_11[] = {
2325     OP_C_SET_ALPN("ossltest")
2326         OP_C_CONNECT_WAIT()
2327             OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2328 
2329                 OP_NEW_THREAD(5, script_11_child)
2330 
2331                     OP_S_NEW_STREAM_BIDI(a, ANY_ID)
2332                         OP_S_WRITE(a, "foo", 3)
2333                             OP_S_CONCLUDE(a)
2334 
2335                                 OP_S_NEW_STREAM_BIDI(b, ANY_ID)
2336                                     OP_S_WRITE(b, "foo", 3)
2337                                         OP_S_CONCLUDE(b)
2338 
2339                                             OP_S_NEW_STREAM_BIDI(c, ANY_ID)
2340                                                 OP_S_WRITE(c, "foo", 3)
2341                                                     OP_S_CONCLUDE(c)
2342 
2343                                                         OP_S_NEW_STREAM_BIDI(d, ANY_ID)
2344                                                             OP_S_WRITE(d, "foo", 3)
2345                                                                 OP_S_CONCLUDE(d)
2346 
2347                                                                     OP_S_NEW_STREAM_BIDI(e, ANY_ID)
2348                                                                         OP_S_WRITE(e, "foo", 3)
2349                                                                             OP_S_CONCLUDE(e)
2350 
2351                                                                                 OP_END
2352 };
2353 
2354 /* 12. Many threads initiated on the same client connection */
2355 static const struct script_op script_12_child[] = {
2356     OP_C_NEW_STREAM_BIDI(a, ANY_ID)
2357         OP_C_WRITE(a, "foo", 3)
2358             OP_C_CONCLUDE(a)
2359                 OP_C_FREE_STREAM(a)
2360 
2361                     OP_END
2362 };
2363 
2364 static const struct script_op script_12[] = {
2365     OP_C_SET_ALPN("ossltest")
2366         OP_C_CONNECT_WAIT()
2367             OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2368 
2369                 OP_NEW_THREAD(5, script_12_child)
2370 
2371                     OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
2372                         OP_S_READ_EXPECT(a, "foo", 3)
2373                             OP_S_EXPECT_FIN(a)
2374                                 OP_S_BIND_STREAM_ID(b, C_BIDI_ID(1))
2375                                     OP_S_READ_EXPECT(b, "foo", 3)
2376                                         OP_S_EXPECT_FIN(b)
2377                                             OP_S_BIND_STREAM_ID(c, C_BIDI_ID(2))
2378                                                 OP_S_READ_EXPECT(c, "foo", 3)
2379                                                     OP_S_EXPECT_FIN(c)
2380                                                         OP_S_BIND_STREAM_ID(d, C_BIDI_ID(3))
2381                                                             OP_S_READ_EXPECT(d, "foo", 3)
2382                                                                 OP_S_EXPECT_FIN(d)
2383                                                                     OP_S_BIND_STREAM_ID(e, C_BIDI_ID(4))
2384                                                                         OP_S_READ_EXPECT(e, "foo", 3)
2385                                                                             OP_S_EXPECT_FIN(e)
2386 
2387                                                                                 OP_END
2388 };
2389 
2390 /* 13. Many threads accepted on the same client connection (stress test) */
2391 static const struct script_op script_13_child[] = {
2392     OP_BEGIN_REPEAT(10)
2393 
2394         OP_C_ACCEPT_STREAM_WAIT(a)
2395             OP_C_READ_EXPECT(a, "foo", 3)
2396                 OP_C_EXPECT_FIN(a)
2397                     OP_C_FREE_STREAM(a)
2398 
2399                         OP_END_REPEAT()
2400 
2401                             OP_END
2402 };
2403 
2404 static const struct script_op script_13[] = {
2405     OP_C_SET_ALPN("ossltest")
2406         OP_C_CONNECT_WAIT()
2407             OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2408 
2409                 OP_NEW_THREAD(5, script_13_child)
2410 
2411                     OP_BEGIN_REPEAT(50)
2412 
2413                         OP_S_NEW_STREAM_BIDI(a, ANY_ID)
2414                             OP_S_WRITE(a, "foo", 3)
2415                                 OP_S_CONCLUDE(a)
2416                                     OP_S_UNBIND_STREAM_ID(a)
2417 
2418                                         OP_END_REPEAT()
2419 
2420                                             OP_END
2421 };
2422 
2423 /* 14. Many threads initiating on the same client connection (stress test) */
2424 static const struct script_op script_14_child[] = {
2425     OP_BEGIN_REPEAT(10)
2426 
2427         OP_C_NEW_STREAM_BIDI(a, ANY_ID)
2428             OP_C_WRITE(a, "foo", 3)
2429                 OP_C_CONCLUDE(a)
2430                     OP_C_FREE_STREAM(a)
2431 
2432                         OP_END_REPEAT()
2433 
2434                             OP_END
2435 };
2436 
2437 static const struct script_op script_14[] = {
2438     OP_C_SET_ALPN("ossltest")
2439         OP_C_CONNECT_WAIT()
2440             OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2441 
2442                 OP_NEW_THREAD(5, script_14_child)
2443 
2444                     OP_BEGIN_REPEAT(50)
2445 
2446                         OP_S_ACCEPT_STREAM_WAIT(a)
2447                             OP_S_READ_EXPECT(a, "foo", 3)
2448                                 OP_S_EXPECT_FIN(a)
2449                                     OP_S_UNBIND_STREAM_ID(a)
2450 
2451                                         OP_END_REPEAT()
2452 
2453                                             OP_END
2454 };
2455 
2456 /* 15. Client sending large number of streams, MAX_STREAMS test */
2457 static const struct script_op script_15[] = {
2458     OP_C_SET_ALPN("ossltest")
2459         OP_C_CONNECT_WAIT()
2460             OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2461 
2462     /*
2463      * This will cause a protocol violation to be raised by the server if we are
2464      * not handling the stream limit correctly on the TX side.
2465      */
2466     OP_BEGIN_REPEAT(200)
2467 
2468         OP_C_NEW_STREAM_BIDI_EX(a, ANY_ID, SSL_STREAM_FLAG_ADVANCE)
2469             OP_C_WRITE(a, "foo", 3)
2470                 OP_C_CONCLUDE(a)
2471                     OP_C_FREE_STREAM(a)
2472 
2473                         OP_END_REPEAT()
2474 
2475     /* Prove the connection is still good. */
2476     OP_S_NEW_STREAM_BIDI(a, S_BIDI_ID(0))
2477         OP_S_WRITE(a, "bar", 3)
2478             OP_S_CONCLUDE(a)
2479 
2480                 OP_C_ACCEPT_STREAM_WAIT(a)
2481                     OP_C_READ_EXPECT(a, "bar", 3)
2482                         OP_C_EXPECT_FIN(a)
2483 
2484     /*
2485      * Drain the queue of incoming streams. We should be able to get all 200
2486      * even though only 100 can be initiated at a time.
2487      */
2488     OP_BEGIN_REPEAT(200)
2489 
2490         OP_S_ACCEPT_STREAM_WAIT(b)
2491             OP_S_READ_EXPECT(b, "foo", 3)
2492                 OP_S_EXPECT_FIN(b)
2493                     OP_S_UNBIND_STREAM_ID(b)
2494 
2495                         OP_END_REPEAT()
2496 
2497                             OP_END
2498 };
2499 
2500 /* 16. Server sending large number of streams, MAX_STREAMS test */
2501 static const struct script_op script_16[] = {
2502     OP_C_SET_ALPN("ossltest")
2503         OP_C_CONNECT_WAIT()
2504             OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2505 
2506     /*
2507      * This will cause a protocol violation to be raised by the client if we are
2508      * not handling the stream limit correctly on the TX side.
2509      */
2510     OP_BEGIN_REPEAT(200)
2511 
2512         OP_S_NEW_STREAM_BIDI(a, ANY_ID)
2513             OP_S_WRITE(a, "foo", 3)
2514                 OP_S_CONCLUDE(a)
2515                     OP_S_UNBIND_STREAM_ID(a)
2516 
2517                         OP_END_REPEAT()
2518 
2519     /* Prove that the connection is still good. */
2520     OP_C_NEW_STREAM_BIDI(a, ANY_ID)
2521         OP_C_WRITE(a, "bar", 3)
2522             OP_C_CONCLUDE(a)
2523 
2524                 OP_S_ACCEPT_STREAM_WAIT(b)
2525                     OP_S_READ_EXPECT(b, "bar", 3)
2526                         OP_S_EXPECT_FIN(b)
2527 
2528     /* Drain the queue of incoming streams. */
2529     OP_BEGIN_REPEAT(200)
2530 
2531         OP_C_ACCEPT_STREAM_WAIT(b)
2532             OP_C_READ_EXPECT(b, "foo", 3)
2533                 OP_C_EXPECT_FIN(b)
2534                     OP_C_FREE_STREAM(b)
2535 
2536                         OP_END_REPEAT()
2537 
2538                             OP_END
2539 };
2540 
2541 /* 17. Key update test - unlimited */
2542 static const struct script_op script_17[] = {
2543     OP_C_SET_ALPN("ossltest")
2544         OP_C_CONNECT_WAIT()
2545 
2546             OP_C_WRITE(DEFAULT, "apple", 5)
2547 
2548                 OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
2549                     OP_S_READ_EXPECT(a, "apple", 5)
2550 
2551                         OP_CHECK(override_key_update, 1)
2552 
2553                             OP_BEGIN_REPEAT(200)
2554 
2555                                 OP_C_WRITE(DEFAULT, "apple", 5)
2556                                     OP_S_READ_EXPECT(a, "apple", 5)
2557 
2558     /*
2559      * TXKU frequency is bounded by RTT because a previous TXKU needs to be
2560      * acknowledged by the peer first before another one can be begin. By
2561      * waiting this long, we eliminate any such concern and ensure as many key
2562      * updates as possible can occur for the purposes of this test.
2563      */
2564     OP_CHECK(skip_time_ms, 100)
2565 
2566         OP_END_REPEAT()
2567 
2568     /* At least 5 RXKUs detected */
2569     OP_CHECK(check_key_update_ge, 5)
2570 
2571     /*
2572      * Prove the connection is still healthy by sending something in both
2573      * directions.
2574      */
2575     OP_C_WRITE(DEFAULT, "xyzzy", 5)
2576         OP_S_READ_EXPECT(a, "xyzzy", 5)
2577 
2578             OP_S_WRITE(a, "plugh", 5)
2579                 OP_C_READ_EXPECT(DEFAULT, "plugh", 5)
2580 
2581                     OP_END
2582 };
2583 
2584 /* 18. Key update test - RTT-bounded */
2585 static const struct script_op script_18[] = {
2586     OP_C_SET_ALPN("ossltest")
2587         OP_C_CONNECT_WAIT()
2588 
2589             OP_C_WRITE(DEFAULT, "apple", 5)
2590 
2591                 OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
2592                     OP_S_READ_EXPECT(a, "apple", 5)
2593 
2594                         OP_CHECK(override_key_update, 1)
2595 
2596                             OP_BEGIN_REPEAT(200)
2597 
2598                                 OP_C_WRITE(DEFAULT, "apple", 5)
2599                                     OP_S_READ_EXPECT(a, "apple", 5)
2600                                         OP_CHECK(skip_time_ms, 8)
2601 
2602                                             OP_END_REPEAT()
2603 
2604     /*
2605      * This time we simulate far less time passing between writes, so there are
2606      * fewer opportunities to initiate TXKUs. Note that we ask for a TXKU every
2607      * 1 packet above, which is absurd; thus this ensures we only actually
2608      * generate TXKUs when we are allowed to.
2609      */
2610     OP_CHECK(check_key_update_lt, 240)
2611 
2612     /*
2613      * Prove the connection is still healthy by sending something in both
2614      * directions.
2615      */
2616     OP_C_WRITE(DEFAULT, "xyzzy", 5)
2617         OP_S_READ_EXPECT(a, "xyzzy", 5)
2618 
2619             OP_S_WRITE(a, "plugh", 5)
2620                 OP_C_READ_EXPECT(DEFAULT, "plugh", 5)
2621 
2622                     OP_END
2623 };
2624 
2625 /* 19. Key update test - artificially triggered */
2626 static const struct script_op script_19[] = {
2627     OP_C_SET_ALPN("ossltest")
2628         OP_C_CONNECT_WAIT()
2629 
2630             OP_C_WRITE(DEFAULT, "apple", 5)
2631 
2632                 OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
2633                     OP_S_READ_EXPECT(a, "apple", 5)
2634 
2635                         OP_C_WRITE(DEFAULT, "orange", 6)
2636                             OP_S_READ_EXPECT(a, "orange", 6)
2637 
2638                                 OP_S_WRITE(a, "strawberry", 10)
2639                                     OP_C_READ_EXPECT(DEFAULT, "strawberry", 10)
2640 
2641                                         OP_CHECK(check_key_update_lt, 1)
2642                                             OP_CHECK(trigger_key_update, 0)
2643 
2644                                                 OP_C_WRITE(DEFAULT, "orange", 6)
2645                                                     OP_S_READ_EXPECT(a, "orange", 6)
2646                                                         OP_S_WRITE(a, "ok", 2)
2647 
2648                                                             OP_C_READ_EXPECT(DEFAULT, "ok", 2)
2649                                                                 OP_CHECK(check_key_update_ge, 1)
2650 
2651                                                                     OP_END
2652 };
2653 
2654 /* 20. Multiple threads accept stream with socket forcibly closed (error test) */
script_20_trigger(struct helper * h,volatile uint64_t * counter)2655 static int script_20_trigger(struct helper *h, volatile uint64_t *counter)
2656 {
2657 #if defined(OPENSSL_THREADS)
2658     ossl_crypto_mutex_lock(h->misc_m);
2659     ++*counter;
2660     ossl_crypto_condvar_broadcast(h->misc_cv);
2661     ossl_crypto_mutex_unlock(h->misc_m);
2662 #endif
2663     return 1;
2664 }
2665 
script_20_wait(struct helper * h,volatile uint64_t * counter,uint64_t threshold)2666 static int script_20_wait(struct helper *h, volatile uint64_t *counter, uint64_t threshold)
2667 {
2668 #if defined(OPENSSL_THREADS)
2669     int stop = 0;
2670 
2671     ossl_crypto_mutex_lock(h->misc_m);
2672     while (!stop) {
2673         stop = (*counter >= threshold);
2674         if (stop)
2675             break;
2676 
2677         ossl_crypto_condvar_wait(h->misc_cv, h->misc_m);
2678     }
2679 
2680     ossl_crypto_mutex_unlock(h->misc_m);
2681 #endif
2682     return 1;
2683 }
2684 
script_20_trigger1(struct helper * h,struct helper_local * hl)2685 static int script_20_trigger1(struct helper *h, struct helper_local *hl)
2686 {
2687     return script_20_trigger(h, &h->scratch0);
2688 }
2689 
script_20_wait1(struct helper * h,struct helper_local * hl)2690 static int script_20_wait1(struct helper *h, struct helper_local *hl)
2691 {
2692     return script_20_wait(h, &h->scratch0, hl->check_op->arg2);
2693 }
2694 
script_20_trigger2(struct helper * h,struct helper_local * hl)2695 static int script_20_trigger2(struct helper *h, struct helper_local *hl)
2696 {
2697     return script_20_trigger(h, &h->scratch1);
2698 }
2699 
script_20_wait2(struct helper * h,struct helper_local * hl)2700 static int script_20_wait2(struct helper *h, struct helper_local *hl)
2701 {
2702     return script_20_wait(h, &h->scratch1, hl->check_op->arg2);
2703 }
2704 
2705 static const struct script_op script_20_child[] = {
2706     OP_C_ACCEPT_STREAM_WAIT(a)
2707         OP_C_READ_EXPECT(a, "foo", 3)
2708 
2709             OP_CHECK(script_20_trigger1, 0)
2710                 OP_CHECK(script_20_wait2, 1)
2711 
2712                     OP_C_READ_FAIL_WAIT(a)
2713                         OP_C_EXPECT_SSL_ERR(a, SSL_ERROR_SYSCALL)
2714 
2715                             OP_EXPECT_ERR_LIB(ERR_LIB_SSL)
2716                                 OP_EXPECT_ERR_REASON(SSL_R_PROTOCOL_IS_SHUTDOWN)
2717 
2718                                     OP_POP_ERR()
2719                                         OP_EXPECT_ERR_LIB(ERR_LIB_SSL)
2720                                             OP_EXPECT_ERR_REASON(SSL_R_QUIC_NETWORK_ERROR)
2721 
2722                                                 OP_C_FREE_STREAM(a)
2723 
2724                                                     OP_END
2725 };
2726 
2727 static const struct script_op script_20[] = {
2728     OP_C_SET_ALPN("ossltest")
2729         OP_C_CONNECT_WAIT()
2730             OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2731 
2732                 OP_NEW_THREAD(5, script_20_child)
2733 
2734                     OP_BEGIN_REPEAT(5)
2735 
2736                         OP_S_NEW_STREAM_BIDI(a, ANY_ID)
2737                             OP_S_WRITE(a, "foo", 3)
2738                                 OP_S_UNBIND_STREAM_ID(a)
2739 
2740                                     OP_END_REPEAT()
2741 
2742                                         OP_CHECK(script_20_wait1, 5)
2743 
2744                                             OP_C_CLOSE_SOCKET()
2745                                                 OP_CHECK(script_20_trigger2, 0)
2746 
2747                                                     OP_END
2748 };
2749 
2750 /* 21. Fault injection - unknown frame in 1-RTT packet */
script_21_inject_plain(struct helper * h,QUIC_PKT_HDR * hdr,unsigned char * buf,size_t len)2751 static int script_21_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
2752     unsigned char *buf, size_t len)
2753 {
2754     int ok = 0;
2755     WPACKET wpkt;
2756     unsigned char frame_buf[21];
2757     size_t written;
2758 
2759     if (h->inject_word0 == 0 || hdr->type != h->inject_word0)
2760         return 1;
2761 
2762     if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
2763             sizeof(frame_buf), 0)))
2764         return 0;
2765 
2766     if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1)))
2767         goto err;
2768 
2769     switch (h->inject_word1) {
2770     case OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE:
2771     case OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE:
2772     case OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID:
2773         /*
2774          * These cases to be formatted properly need a single uint64_t
2775          */
2776         if (!TEST_true(WPACKET_put_bytes_u64(&wpkt, (uint64_t)0)))
2777             goto err;
2778         break;
2779     case OSSL_QUIC_FRAME_TYPE_MAX_DATA:
2780     case OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI:
2781     case OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI:
2782     case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI:
2783     case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI:
2784     case OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED:
2785         /*
2786          * These cases require a single vlint
2787          */
2788         if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, (uint64_t)0)))
2789             goto err;
2790         break;
2791     case OSSL_QUIC_FRAME_TYPE_STOP_SENDING:
2792     case OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA:
2793     case OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED:
2794         /*
2795          * These cases require 2 variable integers
2796          */
2797         if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, (uint64_t)0)))
2798             goto err;
2799         if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, (uint64_t)0)))
2800             goto err;
2801         break;
2802     case OSSL_QUIC_FRAME_TYPE_STREAM:
2803     case OSSL_QUIC_FRAME_TYPE_RESET_STREAM:
2804     case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP:
2805         /*
2806          * These cases require 3 variable integers
2807          */
2808         if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, (uint64_t)0)))
2809             goto err;
2810         if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, (uint64_t)0)))
2811             goto err;
2812         if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, (uint64_t)0)))
2813             goto err;
2814         break;
2815     case OSSL_QUIC_FRAME_TYPE_NEW_TOKEN:
2816         /*
2817          * Special case for new token
2818          */
2819 
2820         /* New token length, cannot be zero */
2821         if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, (uint64_t)1)))
2822             goto err;
2823 
2824         /* 1 bytes of token data, to match the above length */
2825         if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, (uint8_t)0)))
2826             goto err;
2827         break;
2828     case OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID:
2829         /*
2830          * Special case for New Connection ids, has a combination
2831          * of vlints and fixed width values
2832          */
2833 
2834         /* seq number */
2835         if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, (uint64_t)0)))
2836             goto err;
2837 
2838         /* retire prior to */
2839         if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, (uint64_t)0)))
2840             goto err;
2841 
2842         /* Connection id length, arbitrary at 1 bytes */
2843         if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, (uint8_t)1)))
2844             goto err;
2845 
2846         /* The connection id, to match the above length */
2847         if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, (uint8_t)0)))
2848             goto err;
2849 
2850         /* 16 bytes total for the SRT */
2851         if (!TEST_true(WPACKET_memset(&wpkt, 0, 16)))
2852             goto err;
2853 
2854         break;
2855     }
2856 
2857     if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
2858         goto err;
2859 
2860     if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
2861         goto err;
2862 
2863     ok = 1;
2864 err:
2865     if (ok)
2866         WPACKET_finish(&wpkt);
2867     else
2868         WPACKET_cleanup(&wpkt);
2869     return ok;
2870 }
2871 
2872 static const struct script_op script_21[] = {
2873     OP_S_SET_INJECT_PLAIN(script_21_inject_plain)
2874         OP_C_SET_ALPN("ossltest")
2875             OP_C_CONNECT_WAIT()
2876 
2877                 OP_C_WRITE(DEFAULT, "apple", 5)
2878                     OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
2879                         OP_S_READ_EXPECT(a, "apple", 5)
2880 
2881                             OP_SET_INJECT_WORD(QUIC_PKT_TYPE_1RTT, OSSL_QUIC_VLINT_MAX)
2882 
2883                                 OP_S_WRITE(a, "orange", 6)
2884 
2885                                     OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR, 0, 0)
2886 
2887                                         OP_END
2888 };
2889 
2890 /* 22. Fault injection - non-zero packet header reserved bits */
script_22_inject_plain(struct helper * h,QUIC_PKT_HDR * hdr,unsigned char * buf,size_t len)2891 static int script_22_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
2892     unsigned char *buf, size_t len)
2893 {
2894     if (h->inject_word0 == 0)
2895         return 1;
2896 
2897     hdr->reserved = 1;
2898     return 1;
2899 }
2900 
2901 static const struct script_op script_22[] = {
2902     OP_S_SET_INJECT_PLAIN(script_22_inject_plain)
2903         OP_C_SET_ALPN("ossltest")
2904             OP_C_CONNECT_WAIT()
2905 
2906                 OP_C_WRITE(DEFAULT, "apple", 5)
2907                     OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
2908                         OP_S_READ_EXPECT(a, "apple", 5)
2909 
2910                             OP_SET_INJECT_WORD(1, 0)
2911 
2912                                 OP_S_WRITE(a, "orange", 6)
2913 
2914                                     OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_PROTOCOL_VIOLATION, 0, 0)
2915 
2916                                         OP_END
2917 };
2918 
2919 /* 23. Fault injection - empty NEW_TOKEN */
script_23_inject_plain(struct helper * h,QUIC_PKT_HDR * hdr,unsigned char * buf,size_t len)2920 static int script_23_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
2921     unsigned char *buf, size_t len)
2922 {
2923     int ok = 0;
2924     WPACKET wpkt;
2925     unsigned char frame_buf[16];
2926     size_t written;
2927 
2928     if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT)
2929         return 1;
2930 
2931     if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
2932             sizeof(frame_buf), 0)))
2933         return 0;
2934 
2935     if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_NEW_TOKEN))
2936         || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 0)))
2937         goto err;
2938 
2939     if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
2940         goto err;
2941 
2942     if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
2943         goto err;
2944 
2945     ok = 1;
2946 err:
2947     if (ok)
2948         WPACKET_finish(&wpkt);
2949     else
2950         WPACKET_cleanup(&wpkt);
2951     return ok;
2952 }
2953 
2954 static const struct script_op script_23[] = {
2955     OP_S_SET_INJECT_PLAIN(script_23_inject_plain)
2956         OP_C_SET_ALPN("ossltest")
2957             OP_C_CONNECT_WAIT()
2958 
2959                 OP_C_WRITE(DEFAULT, "apple", 5)
2960                     OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
2961                         OP_S_READ_EXPECT(a, "apple", 5)
2962 
2963                             OP_SET_INJECT_WORD(1, 0)
2964 
2965                                 OP_S_WRITE(a, "orange", 6)
2966 
2967                                     OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR, 0, 0)
2968 
2969                                         OP_END
2970 };
2971 
2972 /* 24. Fault injection - excess value of MAX_STREAMS_BIDI */
script_24_inject_plain(struct helper * h,QUIC_PKT_HDR * hdr,unsigned char * buf,size_t len)2973 static int script_24_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
2974     unsigned char *buf, size_t len)
2975 {
2976     int ok = 0;
2977     WPACKET wpkt;
2978     unsigned char frame_buf[16];
2979     size_t written;
2980 
2981     if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT)
2982         return 1;
2983 
2984     if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
2985             sizeof(frame_buf), 0)))
2986         return 0;
2987 
2988     if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1))
2989         || !TEST_true(WPACKET_quic_write_vlint(&wpkt, (((uint64_t)1) << 60) + 1)))
2990         goto err;
2991 
2992     if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
2993         goto err;
2994 
2995     if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
2996         goto err;
2997 
2998     ok = 1;
2999 err:
3000     if (ok)
3001         WPACKET_finish(&wpkt);
3002     else
3003         WPACKET_cleanup(&wpkt);
3004     return ok;
3005 }
3006 
3007 static const struct script_op script_24[] = {
3008     OP_S_SET_INJECT_PLAIN(script_24_inject_plain)
3009         OP_C_SET_ALPN("ossltest")
3010             OP_C_CONNECT_WAIT()
3011 
3012                 OP_C_WRITE(DEFAULT, "apple", 5)
3013                     OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
3014                         OP_S_READ_EXPECT(a, "apple", 5)
3015 
3016                             OP_SET_INJECT_WORD(1, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI)
3017 
3018                                 OP_S_WRITE(a, "orange", 6)
3019 
3020                                     OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR, 0, 0)
3021 
3022                                         OP_END
3023 };
3024 
3025 /* 25. Fault injection - excess value of MAX_STREAMS_UNI */
3026 static const struct script_op script_25[] = {
3027     OP_S_SET_INJECT_PLAIN(script_24_inject_plain)
3028         OP_C_SET_ALPN("ossltest")
3029             OP_C_CONNECT_WAIT()
3030 
3031                 OP_C_WRITE(DEFAULT, "apple", 5)
3032                     OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
3033                         OP_S_READ_EXPECT(a, "apple", 5)
3034 
3035                             OP_SET_INJECT_WORD(1, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI)
3036 
3037                                 OP_S_WRITE(a, "orange", 6)
3038 
3039                                     OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR, 0, 0)
3040 
3041                                         OP_END
3042 };
3043 
3044 /* 26. Fault injection - excess value of STREAMS_BLOCKED_BIDI */
3045 static const struct script_op script_26[] = {
3046     OP_S_SET_INJECT_PLAIN(script_24_inject_plain)
3047         OP_C_SET_ALPN("ossltest")
3048             OP_C_CONNECT_WAIT()
3049 
3050                 OP_C_WRITE(DEFAULT, "apple", 5)
3051                     OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
3052                         OP_S_READ_EXPECT(a, "apple", 5)
3053 
3054                             OP_SET_INJECT_WORD(1, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI)
3055 
3056                                 OP_S_WRITE(a, "orange", 6)
3057 
3058                                     OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_LIMIT_ERROR, 0, 0)
3059 
3060                                         OP_END
3061 };
3062 
3063 /* 27. Fault injection - excess value of STREAMS_BLOCKED_UNI */
3064 static const struct script_op script_27[] = {
3065     OP_S_SET_INJECT_PLAIN(script_24_inject_plain)
3066         OP_C_SET_ALPN("ossltest")
3067             OP_C_CONNECT_WAIT()
3068 
3069                 OP_C_WRITE(DEFAULT, "apple", 5)
3070                     OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
3071                         OP_S_READ_EXPECT(a, "apple", 5)
3072 
3073                             OP_SET_INJECT_WORD(1, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI)
3074 
3075                                 OP_S_WRITE(a, "orange", 6)
3076 
3077                                     OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_LIMIT_ERROR, 0, 0)
3078 
3079                                         OP_END
3080 };
3081 
3082 /* 28. Fault injection - received RESET_STREAM for send-only stream */
script_28_inject_plain(struct helper * h,QUIC_PKT_HDR * hdr,unsigned char * buf,size_t len)3083 static int script_28_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
3084     unsigned char *buf, size_t len)
3085 {
3086     int ok = 0;
3087     WPACKET wpkt;
3088     unsigned char frame_buf[32];
3089     size_t written;
3090 
3091     if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT)
3092         return 1;
3093 
3094     if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
3095             sizeof(frame_buf), 0)))
3096         return 0;
3097 
3098     if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1))
3099         || !TEST_true(WPACKET_quic_write_vlint(&wpkt, /* stream ID */
3100             h->inject_word0 - 1))
3101         || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 123))
3102         || (h->inject_word1 == OSSL_QUIC_FRAME_TYPE_RESET_STREAM
3103             && !TEST_true(WPACKET_quic_write_vlint(&wpkt, 5)))) /* final size */
3104         goto err;
3105 
3106     if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
3107         goto err;
3108 
3109     if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
3110         goto err;
3111 
3112     ok = 1;
3113 err:
3114     if (ok)
3115         WPACKET_finish(&wpkt);
3116     else
3117         WPACKET_cleanup(&wpkt);
3118     return ok;
3119 }
3120 
3121 static const struct script_op script_28[] = {
3122     OP_S_SET_INJECT_PLAIN(script_28_inject_plain)
3123         OP_C_SET_ALPN("ossltest")
3124             OP_C_CONNECT_WAIT()
3125                 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
3126 
3127                     OP_C_NEW_STREAM_BIDI(a, C_BIDI_ID(0))
3128                         OP_C_WRITE(a, "orange", 6)
3129 
3130                             OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
3131                                 OP_S_READ_EXPECT(a, "orange", 6)
3132 
3133                                     OP_C_NEW_STREAM_UNI(b, C_UNI_ID(0))
3134                                         OP_C_WRITE(b, "apple", 5)
3135 
3136                                             OP_S_BIND_STREAM_ID(b, C_UNI_ID(0))
3137                                                 OP_S_READ_EXPECT(b, "apple", 5)
3138 
3139                                                     OP_SET_INJECT_WORD(C_UNI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_RESET_STREAM)
3140                                                         OP_S_WRITE(a, "fruit", 5)
3141 
3142                                                             OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR, 0, 0)
3143 
3144                                                                 OP_END
3145 };
3146 
3147 /* 29. Fault injection - received RESET_STREAM for nonexistent send-only stream */
3148 static const struct script_op script_29[] = {
3149     OP_S_SET_INJECT_PLAIN(script_28_inject_plain)
3150         OP_C_SET_ALPN("ossltest")
3151             OP_C_CONNECT_WAIT()
3152                 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
3153 
3154                     OP_C_NEW_STREAM_BIDI(a, C_BIDI_ID(0))
3155                         OP_C_WRITE(a, "orange", 6)
3156 
3157                             OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
3158                                 OP_S_READ_EXPECT(a, "orange", 6)
3159 
3160                                     OP_C_NEW_STREAM_UNI(b, C_UNI_ID(0))
3161                                         OP_C_WRITE(b, "apple", 5)
3162 
3163                                             OP_S_BIND_STREAM_ID(b, C_UNI_ID(0))
3164                                                 OP_S_READ_EXPECT(b, "apple", 5)
3165 
3166                                                     OP_SET_INJECT_WORD(C_UNI_ID(1) + 1, OSSL_QUIC_FRAME_TYPE_RESET_STREAM)
3167                                                         OP_S_WRITE(a, "fruit", 5)
3168 
3169                                                             OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR, 0, 0)
3170 
3171                                                                 OP_END
3172 };
3173 
3174 /* 30. Fault injection - received STOP_SENDING for receive-only stream */
3175 static const struct script_op script_30[] = {
3176     OP_S_SET_INJECT_PLAIN(script_28_inject_plain)
3177         OP_C_SET_ALPN("ossltest")
3178             OP_C_CONNECT_WAIT()
3179                 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
3180 
3181                     OP_S_NEW_STREAM_UNI(a, S_UNI_ID(0))
3182                         OP_S_WRITE(a, "apple", 5)
3183 
3184                             OP_C_ACCEPT_STREAM_WAIT(a)
3185                                 OP_C_READ_EXPECT(a, "apple", 5)
3186 
3187                                     OP_SET_INJECT_WORD(S_UNI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_STOP_SENDING)
3188                                         OP_S_WRITE(a, "orange", 6)
3189 
3190                                             OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR, 0, 0)
3191 
3192                                                 OP_END
3193 };
3194 
3195 /* 31. Fault injection - received STOP_SENDING for nonexistent receive-only stream */
3196 static const struct script_op script_31[] = {
3197     OP_S_SET_INJECT_PLAIN(script_28_inject_plain)
3198         OP_C_SET_ALPN("ossltest")
3199             OP_C_CONNECT_WAIT()
3200                 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
3201 
3202                     OP_S_NEW_STREAM_UNI(a, S_UNI_ID(0))
3203                         OP_S_WRITE(a, "apple", 5)
3204 
3205                             OP_C_ACCEPT_STREAM_WAIT(a)
3206                                 OP_C_READ_EXPECT(a, "apple", 5)
3207 
3208                                     OP_SET_INJECT_WORD(C_UNI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_STOP_SENDING)
3209                                         OP_S_WRITE(a, "orange", 6)
3210 
3211                                             OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR, 0, 0)
3212 
3213                                                 OP_END
3214 };
3215 
3216 /* 32. Fault injection - STREAM frame for nonexistent stream */
script_32_inject_plain(struct helper * h,QUIC_PKT_HDR * hdr,unsigned char * buf,size_t len)3217 static int script_32_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
3218     unsigned char *buf, size_t len)
3219 {
3220     int ok = 0;
3221     WPACKET wpkt;
3222     unsigned char frame_buf[64];
3223     size_t written;
3224     uint64_t type = OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN, offset, flen, i;
3225 
3226     if (hdr->type != QUIC_PKT_TYPE_1RTT)
3227         return 1;
3228 
3229     switch (h->inject_word1) {
3230     default:
3231         return 0;
3232     case 0:
3233         return 1;
3234     case 1:
3235         offset = 0;
3236         flen = 0;
3237         break;
3238     case 2:
3239         offset = (((uint64_t)1) << 62) - 1;
3240         flen = 5;
3241         break;
3242     case 3:
3243         offset = 1 * 1024 * 1024 * 1024; /* 1G */
3244         flen = 5;
3245         break;
3246     case 4:
3247         offset = 0;
3248         flen = 1;
3249         break;
3250     }
3251 
3252     if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
3253             sizeof(frame_buf), 0)))
3254         return 0;
3255 
3256     if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, type))
3257         || !TEST_true(WPACKET_quic_write_vlint(&wpkt, /* stream ID */
3258             h->inject_word0 - 1))
3259         || !TEST_true(WPACKET_quic_write_vlint(&wpkt, offset))
3260         || !TEST_true(WPACKET_quic_write_vlint(&wpkt, flen)))
3261         goto err;
3262 
3263     for (i = 0; i < flen; ++i)
3264         if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x42)))
3265             goto err;
3266 
3267     if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
3268         goto err;
3269 
3270     if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
3271         goto err;
3272 
3273     ok = 1;
3274 err:
3275     if (ok)
3276         WPACKET_finish(&wpkt);
3277     else
3278         WPACKET_cleanup(&wpkt);
3279     return ok;
3280 }
3281 
3282 static const struct script_op script_32[] = {
3283     OP_S_SET_INJECT_PLAIN(script_32_inject_plain)
3284         OP_C_SET_ALPN("ossltest")
3285             OP_C_CONNECT_WAIT()
3286                 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
3287 
3288                     OP_S_NEW_STREAM_UNI(a, S_UNI_ID(0))
3289                         OP_S_WRITE(a, "apple", 5)
3290 
3291                             OP_C_ACCEPT_STREAM_WAIT(a)
3292                                 OP_C_READ_EXPECT(a, "apple", 5)
3293 
3294                                     OP_SET_INJECT_WORD(C_UNI_ID(0) + 1, 1)
3295                                         OP_S_WRITE(a, "orange", 6)
3296 
3297                                             OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR, 0, 0)
3298 
3299                                                 OP_END
3300 };
3301 
3302 /* 33. Fault injection - STREAM frame with illegal offset */
3303 static const struct script_op script_33[] = {
3304     OP_S_SET_INJECT_PLAIN(script_32_inject_plain)
3305         OP_C_SET_ALPN("ossltest")
3306             OP_C_CONNECT_WAIT()
3307                 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
3308 
3309                     OP_C_NEW_STREAM_BIDI(a, C_BIDI_ID(0))
3310                         OP_C_WRITE(a, "apple", 5)
3311 
3312                             OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
3313                                 OP_S_READ_EXPECT(a, "apple", 5)
3314 
3315                                     OP_SET_INJECT_WORD(C_BIDI_ID(0) + 1, 2)
3316                                         OP_S_WRITE(a, "orange", 6)
3317 
3318                                             OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR, 0, 0)
3319 
3320                                                 OP_END
3321 };
3322 
3323 /* 34. Fault injection - STREAM frame which exceeds FC */
3324 static const struct script_op script_34[] = {
3325     OP_S_SET_INJECT_PLAIN(script_32_inject_plain)
3326         OP_C_SET_ALPN("ossltest")
3327             OP_C_CONNECT_WAIT()
3328                 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
3329 
3330                     OP_C_NEW_STREAM_BIDI(a, C_BIDI_ID(0))
3331                         OP_C_WRITE(a, "apple", 5)
3332 
3333                             OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
3334                                 OP_S_READ_EXPECT(a, "apple", 5)
3335 
3336                                     OP_SET_INJECT_WORD(C_BIDI_ID(0) + 1, 3)
3337                                         OP_S_WRITE(a, "orange", 6)
3338 
3339                                             OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FLOW_CONTROL_ERROR, 0, 0)
3340 
3341                                                 OP_END
3342 };
3343 
3344 /* 35. Fault injection - MAX_STREAM_DATA for receive-only stream */
3345 static const struct script_op script_35[] = {
3346     OP_S_SET_INJECT_PLAIN(script_28_inject_plain)
3347         OP_C_SET_ALPN("ossltest")
3348             OP_C_CONNECT_WAIT()
3349                 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
3350 
3351                     OP_S_NEW_STREAM_UNI(a, S_UNI_ID(0))
3352                         OP_S_WRITE(a, "apple", 5)
3353 
3354                             OP_C_ACCEPT_STREAM_WAIT(a)
3355                                 OP_C_READ_EXPECT(a, "apple", 5)
3356 
3357                                     OP_SET_INJECT_WORD(S_UNI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA)
3358                                         OP_S_WRITE(a, "orange", 6)
3359 
3360                                             OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR, 0, 0)
3361 
3362                                                 OP_END
3363 };
3364 
3365 /* 36. Fault injection - MAX_STREAM_DATA for nonexistent stream */
3366 static const struct script_op script_36[] = {
3367     OP_S_SET_INJECT_PLAIN(script_28_inject_plain)
3368         OP_C_SET_ALPN("ossltest")
3369             OP_C_CONNECT_WAIT()
3370                 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
3371 
3372                     OP_S_NEW_STREAM_UNI(a, S_UNI_ID(0))
3373                         OP_S_WRITE(a, "apple", 5)
3374 
3375                             OP_C_ACCEPT_STREAM_WAIT(a)
3376                                 OP_C_READ_EXPECT(a, "apple", 5)
3377 
3378                                     OP_SET_INJECT_WORD(C_BIDI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA)
3379                                         OP_S_WRITE(a, "orange", 6)
3380 
3381                                             OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR, 0, 0)
3382 
3383                                                 OP_END
3384 };
3385 
3386 /* 37. Fault injection - STREAM_DATA_BLOCKED for send-only stream */
3387 static const struct script_op script_37[] = {
3388     OP_S_SET_INJECT_PLAIN(script_28_inject_plain)
3389         OP_C_SET_ALPN("ossltest")
3390             OP_C_CONNECT_WAIT()
3391                 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
3392 
3393                     OP_C_NEW_STREAM_UNI(a, C_UNI_ID(0))
3394                         OP_C_WRITE(a, "apple", 5)
3395 
3396                             OP_S_BIND_STREAM_ID(a, C_UNI_ID(0))
3397                                 OP_S_READ_EXPECT(a, "apple", 5)
3398 
3399                                     OP_S_NEW_STREAM_UNI(b, S_UNI_ID(0))
3400                                         OP_SET_INJECT_WORD(C_UNI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED)
3401                                             OP_S_WRITE(b, "orange", 5)
3402 
3403                                                 OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR, 0, 0)
3404 
3405                                                     OP_END
3406 };
3407 
3408 /* 38. Fault injection - STREAM_DATA_BLOCKED for non-existent stream */
3409 static const struct script_op script_38[] = {
3410     OP_S_SET_INJECT_PLAIN(script_28_inject_plain)
3411         OP_C_SET_ALPN("ossltest")
3412             OP_C_CONNECT_WAIT()
3413                 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
3414 
3415                     OP_C_NEW_STREAM_UNI(a, C_UNI_ID(0))
3416                         OP_C_WRITE(a, "apple", 5)
3417 
3418                             OP_S_BIND_STREAM_ID(a, C_UNI_ID(0))
3419                                 OP_S_READ_EXPECT(a, "apple", 5)
3420 
3421                                     OP_SET_INJECT_WORD(C_BIDI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED)
3422 
3423                                         OP_S_NEW_STREAM_UNI(b, S_UNI_ID(0))
3424                                             OP_S_WRITE(b, "orange", 5)
3425 
3426                                                 OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR, 0, 0)
3427 
3428                                                     OP_END
3429 };
3430 
3431 /* 39. Fault injection - NEW_CONN_ID with zero-len CID */
script_39_inject_plain(struct helper * h,QUIC_PKT_HDR * hdr,unsigned char * buf,size_t len)3432 static int script_39_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
3433     unsigned char *buf, size_t len)
3434 {
3435     int ok = 0;
3436     WPACKET wpkt;
3437     unsigned char frame_buf[64];
3438     size_t i, written;
3439     uint64_t seq_no = 0, retire_prior_to = 0;
3440     QUIC_CONN_ID new_cid = { 0 };
3441     QUIC_CHANNEL *ch = ossl_quic_tserver_get_channel(h->s_priv);
3442 
3443     if (hdr->type != QUIC_PKT_TYPE_1RTT)
3444         return 1;
3445 
3446     switch (h->inject_word1) {
3447     case 0:
3448         return 1;
3449     case 1:
3450         new_cid.id_len = 0;
3451         break;
3452     case 2:
3453         new_cid.id_len = 21;
3454         break;
3455     case 3:
3456         new_cid.id_len = 1;
3457         new_cid.id[0] = 0x55;
3458 
3459         seq_no = 0;
3460         retire_prior_to = 1;
3461         break;
3462     case 4:
3463         /* Use our actual CID so we don't break connectivity. */
3464         ossl_quic_channel_get_diag_local_cid(ch, &new_cid);
3465 
3466         seq_no = 2;
3467         retire_prior_to = 2;
3468         break;
3469     case 5:
3470         /*
3471          * Use a bogus CID which will need to be ignored if connectivity is to
3472          * be continued.
3473          */
3474         new_cid.id_len = 8;
3475         new_cid.id[0] = 0x55;
3476 
3477         seq_no = 1;
3478         retire_prior_to = 1;
3479         break;
3480     }
3481 
3482     if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
3483             sizeof(frame_buf), 0)))
3484         return 0;
3485 
3486     if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID))
3487         || !TEST_true(WPACKET_quic_write_vlint(&wpkt, seq_no)) /* seq no */
3488         || !TEST_true(WPACKET_quic_write_vlint(&wpkt, retire_prior_to)) /* retire prior to */
3489         || !TEST_true(WPACKET_put_bytes_u8(&wpkt, new_cid.id_len))) /* len */
3490         goto err;
3491 
3492     for (i = 0; i < new_cid.id_len && i < OSSL_NELEM(new_cid.id); ++i)
3493         if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, new_cid.id[i])))
3494             goto err;
3495 
3496     for (; i < new_cid.id_len; ++i)
3497         if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x55)))
3498             goto err;
3499 
3500     for (i = 0; i < QUIC_STATELESS_RESET_TOKEN_LEN; ++i)
3501         if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x42)))
3502             goto err;
3503 
3504     if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
3505         goto err;
3506 
3507     if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
3508         goto err;
3509 
3510     ok = 1;
3511 err:
3512     if (ok)
3513         WPACKET_finish(&wpkt);
3514     else
3515         WPACKET_cleanup(&wpkt);
3516     return ok;
3517 }
3518 
3519 static const struct script_op script_39[] = {
3520     OP_S_SET_INJECT_PLAIN(script_39_inject_plain)
3521         OP_C_SET_ALPN("ossltest")
3522             OP_C_CONNECT_WAIT()
3523                 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
3524 
3525                     OP_C_NEW_STREAM_BIDI(a, C_BIDI_ID(0))
3526                         OP_C_WRITE(a, "apple", 5)
3527                             OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
3528                                 OP_S_READ_EXPECT(a, "apple", 5)
3529 
3530                                     OP_SET_INJECT_WORD(0, 1)
3531                                         OP_S_WRITE(a, "orange", 5)
3532 
3533                                             OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR, 0, 0)
3534 
3535                                                 OP_END
3536 };
3537 
3538 /* 40. Shutdown flush test */
3539 static const unsigned char script_40_data[1024] = "strawberry";
3540 
3541 static const struct script_op script_40[] = {
3542     OP_C_SET_ALPN("ossltest")
3543         OP_C_CONNECT_WAIT()
3544             OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
3545 
3546                 OP_C_NEW_STREAM_BIDI(a, C_BIDI_ID(0))
3547                     OP_C_WRITE(a, "apple", 5)
3548 
3549                         OP_C_INHIBIT_TICK(1)
3550                             OP_C_SET_WRITE_BUF_SIZE(a, 1024 * 100 * 3)
3551 
3552                                 OP_BEGIN_REPEAT(100)
3553 
3554                                     OP_C_WRITE(a, script_40_data, sizeof(script_40_data))
3555 
3556                                         OP_END_REPEAT()
3557 
3558                                             OP_C_CONCLUDE(a)
3559                                                 OP_C_SHUTDOWN_WAIT(NULL, 0) /* disengages tick inhibition */
3560 
3561     OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
3562         OP_S_READ_EXPECT(a, "apple", 5)
3563 
3564             OP_BEGIN_REPEAT(100)
3565 
3566                 OP_S_READ_EXPECT(a, script_40_data, sizeof(script_40_data))
3567 
3568                     OP_END_REPEAT()
3569 
3570                         OP_S_EXPECT_FIN(a)
3571 
3572                             OP_C_EXPECT_CONN_CLOSE_INFO(0, 1, 0)
3573                                 OP_S_EXPECT_CONN_CLOSE_INFO(0, 1, 1)
3574 
3575                                     OP_END
3576 };
3577 
3578 /* 41. Fault injection - PATH_CHALLENGE yields PATH_RESPONSE */
3579 static const uint64_t path_challenge = UINT64_C(0xbdeb9451169c83aa);
3580 
script_41_inject_plain(struct helper * h,QUIC_PKT_HDR * hdr,unsigned char * buf,size_t len)3581 static int script_41_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
3582     unsigned char *buf, size_t len)
3583 {
3584     int ok = 0;
3585     WPACKET wpkt;
3586     unsigned char frame_buf[16];
3587     size_t written;
3588 
3589     if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT)
3590         return 1;
3591 
3592     if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
3593             sizeof(frame_buf), 0)))
3594         return 0;
3595 
3596     if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1))
3597         || !TEST_true(WPACKET_put_bytes_u64(&wpkt, path_challenge)))
3598         goto err;
3599 
3600     if (!TEST_true(WPACKET_get_total_written(&wpkt, &written))
3601         || !TEST_size_t_eq(written, 9))
3602         goto err;
3603 
3604     if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
3605         goto err;
3606 
3607     --h->inject_word0;
3608     ok = 1;
3609 err:
3610     if (ok)
3611         WPACKET_finish(&wpkt);
3612     else
3613         WPACKET_cleanup(&wpkt);
3614     return ok;
3615 }
3616 
script_41_trace(int write_p,int version,int content_type,const void * buf,size_t len,SSL * ssl,void * arg)3617 static void script_41_trace(int write_p, int version, int content_type,
3618     const void *buf, size_t len, SSL *ssl, void *arg)
3619 {
3620     uint64_t frame_type, frame_data;
3621     int was_minimal;
3622     struct helper *h = arg;
3623     PACKET pkt;
3624 
3625     if (version != OSSL_QUIC1_VERSION
3626         || content_type != SSL3_RT_QUIC_FRAME_FULL
3627         || len < 1)
3628         return;
3629 
3630     if (!TEST_true(PACKET_buf_init(&pkt, buf, len))) {
3631         ++h->scratch1;
3632         return;
3633     }
3634 
3635     if (!TEST_true(ossl_quic_wire_peek_frame_header(&pkt, &frame_type,
3636             &was_minimal))) {
3637         ++h->scratch1;
3638         return;
3639     }
3640 
3641     if (frame_type != OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE)
3642         return;
3643 
3644     if (!TEST_true(ossl_quic_wire_decode_frame_path_response(&pkt, &frame_data))
3645         || !TEST_uint64_t_eq(frame_data, path_challenge)) {
3646         ++h->scratch1;
3647         return;
3648     }
3649 
3650     ++h->scratch0;
3651 }
3652 
script_41_setup(struct helper * h,struct helper_local * hl)3653 static int script_41_setup(struct helper *h, struct helper_local *hl)
3654 {
3655     ossl_quic_tserver_set_msg_callback(ACQUIRE_S(), script_41_trace, h);
3656     return 1;
3657 }
3658 
script_41_check(struct helper * h,struct helper_local * hl)3659 static int script_41_check(struct helper *h, struct helper_local *hl)
3660 {
3661     /* At least one valid challenge/response echo? */
3662     if (!TEST_uint64_t_gt(h->scratch0, 0))
3663         return 0;
3664 
3665     /* No failed tests? */
3666     if (!TEST_uint64_t_eq(h->scratch1, 0))
3667         return 0;
3668 
3669     return 1;
3670 }
3671 
3672 static const struct script_op script_41[] = {
3673     OP_S_SET_INJECT_PLAIN(script_41_inject_plain)
3674         OP_C_SET_ALPN("ossltest")
3675             OP_C_CONNECT_WAIT()
3676                 OP_CHECK(script_41_setup, 0)
3677 
3678                     OP_C_WRITE(DEFAULT, "apple", 5)
3679                         OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
3680                             OP_S_READ_EXPECT(a, "apple", 5)
3681 
3682                                 OP_SET_INJECT_WORD(1, OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE)
3683 
3684                                     OP_S_WRITE(a, "orange", 6)
3685                                         OP_C_READ_EXPECT(DEFAULT, "orange", 6)
3686 
3687                                             OP_C_WRITE(DEFAULT, "strawberry", 10)
3688                                                 OP_S_READ_EXPECT(a, "strawberry", 10)
3689 
3690                                                     OP_CHECK(script_41_check, 0)
3691                                                         OP_END
3692 };
3693 
3694 /* 42. Fault injection - CRYPTO frame with illegal offset */
script_42_inject_plain(struct helper * h,QUIC_PKT_HDR * hdr,unsigned char * buf,size_t len)3695 static int script_42_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
3696     unsigned char *buf, size_t len)
3697 {
3698     int ok = 0;
3699     unsigned char frame_buf[64];
3700     size_t written;
3701     WPACKET wpkt;
3702 
3703     if (h->inject_word0 == 0)
3704         return 1;
3705 
3706     --h->inject_word0;
3707 
3708     if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
3709             sizeof(frame_buf), 0)))
3710         return 0;
3711 
3712     if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_CRYPTO))
3713         || !TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1))
3714         || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 1))
3715         || !TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x42)))
3716         goto err;
3717 
3718     if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
3719         goto err;
3720 
3721     if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
3722         goto err;
3723 
3724     ok = 1;
3725 err:
3726     if (ok)
3727         WPACKET_finish(&wpkt);
3728     else
3729         WPACKET_cleanup(&wpkt);
3730     return ok;
3731 }
3732 
3733 static const struct script_op script_42[] = {
3734     OP_S_SET_INJECT_PLAIN(script_42_inject_plain)
3735         OP_C_SET_ALPN("ossltest")
3736             OP_C_CONNECT_WAIT()
3737                 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
3738 
3739                     OP_C_NEW_STREAM_BIDI(a, C_BIDI_ID(0))
3740                         OP_C_WRITE(a, "apple", 5)
3741 
3742                             OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
3743                                 OP_S_READ_EXPECT(a, "apple", 5)
3744 
3745                                     OP_SET_INJECT_WORD(1, (((uint64_t)1) << 62) - 1)
3746                                         OP_S_WRITE(a, "orange", 6)
3747 
3748                                             OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR, 0, 0)
3749 
3750                                                 OP_END
3751 };
3752 
3753 /* 43. Fault injection - CRYPTO frame exceeding FC */
3754 static const struct script_op script_43[] = {
3755     OP_S_SET_INJECT_PLAIN(script_42_inject_plain)
3756         OP_C_SET_ALPN("ossltest")
3757             OP_C_CONNECT_WAIT()
3758                 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
3759 
3760                     OP_C_NEW_STREAM_BIDI(a, C_BIDI_ID(0))
3761                         OP_C_WRITE(a, "apple", 5)
3762 
3763                             OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
3764                                 OP_S_READ_EXPECT(a, "apple", 5)
3765 
3766                                     OP_SET_INJECT_WORD(1, 0x100000 /* 1 MiB */)
3767                                         OP_S_WRITE(a, "orange", 6)
3768 
3769                                             OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_CRYPTO_BUFFER_EXCEEDED, 0, 0)
3770 
3771                                                 OP_END
3772 };
3773 
3774 /* 44. Fault injection - PADDING */
script_44_inject_plain(struct helper * h,QUIC_PKT_HDR * hdr,unsigned char * buf,size_t len)3775 static int script_44_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
3776     unsigned char *buf, size_t len)
3777 {
3778     int ok = 0;
3779     WPACKET wpkt;
3780     unsigned char frame_buf[16];
3781     size_t written;
3782 
3783     if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT)
3784         return 1;
3785 
3786     if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
3787             sizeof(frame_buf), 0)))
3788         return 0;
3789 
3790     if (!TEST_true(ossl_quic_wire_encode_padding(&wpkt, 1)))
3791         goto err;
3792 
3793     if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
3794         goto err;
3795 
3796     if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
3797         goto err;
3798 
3799     ok = 1;
3800 err:
3801     if (ok)
3802         WPACKET_finish(&wpkt);
3803     else
3804         WPACKET_cleanup(&wpkt);
3805     return ok;
3806 }
3807 
3808 static const struct script_op script_44[] = {
3809     OP_S_SET_INJECT_PLAIN(script_44_inject_plain)
3810         OP_C_SET_ALPN("ossltest")
3811             OP_C_CONNECT_WAIT()
3812 
3813                 OP_C_WRITE(DEFAULT, "apple", 5)
3814                     OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
3815                         OP_S_READ_EXPECT(a, "apple", 5)
3816 
3817                             OP_SET_INJECT_WORD(1, 0)
3818 
3819                                 OP_S_WRITE(a, "Strawberry", 10)
3820                                     OP_C_READ_EXPECT(DEFAULT, "Strawberry", 10)
3821 
3822                                         OP_END
3823 };
3824 
3825 /* 45. PING must generate ACK */
force_ping(struct helper * h,struct helper_local * hl)3826 static int force_ping(struct helper *h, struct helper_local *hl)
3827 {
3828     QUIC_CHANNEL *ch = ossl_quic_tserver_get_channel(ACQUIRE_S());
3829 
3830     h->scratch0 = ossl_quic_channel_get_diag_num_rx_ack(ch);
3831 
3832     if (!TEST_true(ossl_quic_tserver_ping(ACQUIRE_S())))
3833         return 0;
3834 
3835     return 1;
3836 }
3837 
wait_incoming_acks_increased(struct helper * h,struct helper_local * hl)3838 static int wait_incoming_acks_increased(struct helper *h, struct helper_local *hl)
3839 {
3840     QUIC_CHANNEL *ch = ossl_quic_tserver_get_channel(ACQUIRE_S());
3841     uint16_t count;
3842 
3843     count = ossl_quic_channel_get_diag_num_rx_ack(ch);
3844 
3845     if (count == h->scratch0) {
3846         h->check_spin_again = 1;
3847         return 0;
3848     }
3849 
3850     return 1;
3851 }
3852 
3853 static const struct script_op script_45[] = {
3854     OP_C_SET_ALPN("ossltest")
3855         OP_C_CONNECT_WAIT()
3856 
3857             OP_C_WRITE(DEFAULT, "apple", 5)
3858                 OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
3859                     OP_S_READ_EXPECT(a, "apple", 5)
3860 
3861                         OP_BEGIN_REPEAT(2)
3862 
3863                             OP_CHECK(force_ping, 0)
3864                                 OP_CHECK(wait_incoming_acks_increased, 0)
3865 
3866                                     OP_END_REPEAT()
3867 
3868                                         OP_S_WRITE(a, "Strawberry", 10)
3869                                             OP_C_READ_EXPECT(DEFAULT, "Strawberry", 10)
3870 
3871                                                 OP_END
3872 };
3873 
3874 /* 46. Fault injection - ACK - malformed initial range */
script_46_inject_plain(struct helper * h,QUIC_PKT_HDR * hdr,unsigned char * buf,size_t len)3875 static int script_46_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
3876     unsigned char *buf, size_t len)
3877 {
3878     int ok = 0;
3879     WPACKET wpkt;
3880     unsigned char frame_buf[16];
3881     size_t written;
3882     uint64_t type = 0, largest_acked = 0, first_range = 0, range_count = 0;
3883     uint64_t agap = 0, alen = 0;
3884     uint64_t ect0 = 0, ect1 = 0, ecnce = 0;
3885 
3886     if (h->inject_word0 == 0)
3887         return 1;
3888 
3889     if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
3890             sizeof(frame_buf), 0)))
3891         return 0;
3892 
3893     type = OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN;
3894 
3895     switch (h->inject_word0) {
3896     case 1:
3897         largest_acked = 100;
3898         first_range = 101;
3899         range_count = 0;
3900         break;
3901     case 2:
3902         largest_acked = 100;
3903         first_range = 80;
3904         /* [20..100]; [0..18]  */
3905         range_count = 1;
3906         agap = 0;
3907         alen = 19;
3908         break;
3909     case 3:
3910         largest_acked = 100;
3911         first_range = 80;
3912         range_count = 1;
3913         agap = 18;
3914         alen = 1;
3915         break;
3916     case 4:
3917         type = OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN;
3918         largest_acked = 100;
3919         first_range = 1;
3920         range_count = 0;
3921         break;
3922     case 5:
3923         type = OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN;
3924         largest_acked = 0;
3925         first_range = 0;
3926         range_count = 0;
3927         ect0 = 0;
3928         ect1 = 50;
3929         ecnce = 200;
3930         break;
3931     }
3932 
3933     h->inject_word0 = 0;
3934 
3935     if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, type))
3936         || !TEST_true(WPACKET_quic_write_vlint(&wpkt, largest_acked))
3937         || !TEST_true(WPACKET_quic_write_vlint(&wpkt, /*ack_delay=*/0))
3938         || !TEST_true(WPACKET_quic_write_vlint(&wpkt, /*ack_range_count=*/range_count))
3939         || !TEST_true(WPACKET_quic_write_vlint(&wpkt, /*first_ack_range=*/first_range)))
3940         goto err;
3941 
3942     if (range_count > 0)
3943         if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, /*range[0].gap=*/agap))
3944             || !TEST_true(WPACKET_quic_write_vlint(&wpkt, /*range[0].len=*/alen)))
3945             goto err;
3946 
3947     if (type == OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN)
3948         if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, ect0))
3949             || !TEST_true(WPACKET_quic_write_vlint(&wpkt, ect1))
3950             || !TEST_true(WPACKET_quic_write_vlint(&wpkt, ecnce)))
3951             goto err;
3952 
3953     if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
3954         goto err;
3955 
3956     if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
3957         goto err;
3958 
3959     ok = 1;
3960 err:
3961     if (ok)
3962         WPACKET_finish(&wpkt);
3963     else
3964         WPACKET_cleanup(&wpkt);
3965     return ok;
3966 }
3967 
3968 static const struct script_op script_46[] = {
3969     OP_S_SET_INJECT_PLAIN(script_46_inject_plain)
3970         OP_C_SET_ALPN("ossltest")
3971             OP_C_CONNECT_WAIT()
3972 
3973                 OP_C_WRITE(DEFAULT, "apple", 5)
3974                     OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
3975                         OP_S_READ_EXPECT(a, "apple", 5)
3976 
3977                             OP_SET_INJECT_WORD(1, 0)
3978 
3979                                 OP_S_WRITE(a, "Strawberry", 10)
3980 
3981                                     OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR, 0, 0)
3982 
3983                                         OP_END
3984 };
3985 
3986 /* 47. Fault injection - ACK - malformed subsequent range */
3987 static const struct script_op script_47[] = {
3988     OP_S_SET_INJECT_PLAIN(script_46_inject_plain)
3989         OP_C_SET_ALPN("ossltest")
3990             OP_C_CONNECT_WAIT()
3991 
3992                 OP_C_WRITE(DEFAULT, "apple", 5)
3993                     OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
3994                         OP_S_READ_EXPECT(a, "apple", 5)
3995 
3996                             OP_SET_INJECT_WORD(2, 0)
3997 
3998                                 OP_S_WRITE(a, "Strawberry", 10)
3999 
4000                                     OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR, 0, 0)
4001 
4002                                         OP_END
4003 };
4004 
4005 /* 48. Fault injection - ACK - malformed subsequent range */
4006 static const struct script_op script_48[] = {
4007     OP_S_SET_INJECT_PLAIN(script_46_inject_plain)
4008         OP_C_SET_ALPN("ossltest")
4009             OP_C_CONNECT_WAIT()
4010 
4011                 OP_C_WRITE(DEFAULT, "apple", 5)
4012                     OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
4013                         OP_S_READ_EXPECT(a, "apple", 5)
4014 
4015                             OP_SET_INJECT_WORD(3, 0)
4016 
4017                                 OP_S_WRITE(a, "Strawberry", 10)
4018 
4019                                     OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR, 0, 0)
4020 
4021                                         OP_END
4022 };
4023 
4024 /* 49. Fault injection - ACK - fictional PN */
4025 static const struct script_op script_49[] = {
4026     OP_S_SET_INJECT_PLAIN(script_46_inject_plain)
4027         OP_C_SET_ALPN("ossltest")
4028             OP_C_CONNECT_WAIT()
4029 
4030                 OP_C_WRITE(DEFAULT, "apple", 5)
4031                     OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
4032                         OP_S_READ_EXPECT(a, "apple", 5)
4033 
4034                             OP_SET_INJECT_WORD(4, 0)
4035 
4036                                 OP_S_WRITE(a, "Strawberry", 10)
4037     /*
4038      * The injected ACK acknowledges a packet number we have not sent, which the
4039      * peer is expected to treat as a PROTOCOL_VIOLATION, so the connection is
4040      * closed rather than the stream data being delivered.
4041      */
4042     OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_PROTOCOL_VIOLATION, 0, 0)
4043 
4044         OP_END
4045 };
4046 
4047 /* 50. Fault injection - ACK - duplicate PN */
4048 static const struct script_op script_50[] = {
4049     OP_S_SET_INJECT_PLAIN(script_46_inject_plain)
4050         OP_C_SET_ALPN("ossltest")
4051             OP_C_CONNECT_WAIT()
4052 
4053                 OP_C_WRITE(DEFAULT, "apple", 5)
4054                     OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
4055                         OP_S_READ_EXPECT(a, "apple", 5)
4056 
4057                             OP_BEGIN_REPEAT(2)
4058 
4059                                 OP_SET_INJECT_WORD(5, 0)
4060 
4061                                     OP_S_WRITE(a, "Strawberry", 10)
4062                                         OP_C_READ_EXPECT(DEFAULT, "Strawberry", 10)
4063 
4064                                             OP_END_REPEAT()
4065 
4066                                                 OP_END
4067 };
4068 
4069 /* 51. Fault injection - PATH_RESPONSE is ignored */
4070 static const struct script_op script_51[] = {
4071     OP_S_SET_INJECT_PLAIN(script_41_inject_plain)
4072         OP_C_SET_ALPN("ossltest")
4073             OP_C_CONNECT_WAIT()
4074 
4075                 OP_C_WRITE(DEFAULT, "apple", 5)
4076                     OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
4077                         OP_S_READ_EXPECT(a, "apple", 5)
4078 
4079                             OP_SET_INJECT_WORD(1, OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE)
4080 
4081                                 OP_S_WRITE(a, "orange", 6)
4082                                     OP_C_READ_EXPECT(DEFAULT, "orange", 6)
4083 
4084                                         OP_C_WRITE(DEFAULT, "Strawberry", 10)
4085                                             OP_S_READ_EXPECT(a, "Strawberry", 10)
4086 
4087                                                 OP_END
4088 };
4089 
4090 /* 52. Fault injection - ignore BLOCKED frames with bogus values */
script_52_inject_plain(struct helper * h,QUIC_PKT_HDR * hdr,unsigned char * buf,size_t len)4091 static int script_52_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
4092     unsigned char *buf, size_t len)
4093 {
4094     int ok = 0;
4095     unsigned char frame_buf[64];
4096     size_t written;
4097     WPACKET wpkt;
4098     uint64_t type = h->inject_word1;
4099 
4100     if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT)
4101         return 1;
4102 
4103     --h->inject_word0;
4104 
4105     if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
4106             sizeof(frame_buf), 0)))
4107         return 0;
4108 
4109     if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, type)))
4110         goto err;
4111 
4112     if (type == OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED)
4113         if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, C_BIDI_ID(0))))
4114             goto err;
4115 
4116     if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, 0xFFFFFF)))
4117         goto err;
4118 
4119     if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
4120         goto err;
4121 
4122     if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
4123         goto err;
4124 
4125     ok = 1;
4126 err:
4127     if (ok)
4128         WPACKET_finish(&wpkt);
4129     else
4130         WPACKET_cleanup(&wpkt);
4131     return ok;
4132 }
4133 
4134 static const struct script_op script_52[] = {
4135     OP_S_SET_INJECT_PLAIN(script_52_inject_plain)
4136         OP_C_SET_ALPN("ossltest")
4137             OP_C_CONNECT_WAIT()
4138 
4139                 OP_C_WRITE(DEFAULT, "apple", 5)
4140                     OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
4141                         OP_S_READ_EXPECT(a, "apple", 5)
4142 
4143                             OP_SET_INJECT_WORD(1, OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED)
4144 
4145                                 OP_S_WRITE(a, "orange", 6)
4146                                     OP_C_READ_EXPECT(DEFAULT, "orange", 6)
4147 
4148                                         OP_C_WRITE(DEFAULT, "Strawberry", 10)
4149                                             OP_S_READ_EXPECT(a, "Strawberry", 10)
4150 
4151                                                 OP_SET_INJECT_WORD(1, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED)
4152 
4153                                                     OP_S_WRITE(a, "orange", 6)
4154                                                         OP_C_READ_EXPECT(DEFAULT, "orange", 6)
4155 
4156                                                             OP_C_WRITE(DEFAULT, "Strawberry", 10)
4157                                                                 OP_S_READ_EXPECT(a, "Strawberry", 10)
4158 
4159                                                                     OP_SET_INJECT_WORD(1, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI)
4160 
4161                                                                         OP_S_WRITE(a, "orange", 6)
4162                                                                             OP_C_READ_EXPECT(DEFAULT, "orange", 6)
4163 
4164                                                                                 OP_C_WRITE(DEFAULT, "Strawberry", 10)
4165                                                                                     OP_S_READ_EXPECT(a, "Strawberry", 10)
4166 
4167                                                                                         OP_SET_INJECT_WORD(1, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI)
4168 
4169                                                                                             OP_S_WRITE(a, "orange", 6)
4170                                                                                                 OP_C_READ_EXPECT(DEFAULT, "orange", 6)
4171 
4172                                                                                                     OP_C_WRITE(DEFAULT, "Strawberry", 10)
4173                                                                                                         OP_S_READ_EXPECT(a, "Strawberry", 10)
4174 
4175                                                                                                             OP_END
4176 };
4177 
4178 /* 53. Fault injection - excess CRYPTO buffer size */
script_53_inject_plain(struct helper * h,QUIC_PKT_HDR * hdr,unsigned char * buf,size_t len)4179 static int script_53_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
4180     unsigned char *buf, size_t len)
4181 {
4182     int ok = 0;
4183     size_t written;
4184     WPACKET wpkt;
4185     uint64_t offset = 0, data_len = 100;
4186     unsigned char *frame_buf = NULL;
4187     size_t frame_len, i;
4188 
4189     if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT)
4190         return 1;
4191 
4192     h->inject_word0 = 0;
4193 
4194     switch (h->inject_word1) {
4195     case 0:
4196         /*
4197          * Far out offset which will not have been reached during handshake.
4198          * This will not be delivered to the QUIC_TLS instance since it will be
4199          * waiting for in-order delivery of previous bytes. This tests our flow
4200          * control on CRYPTO stream buffering.
4201          */
4202         offset = 100000;
4203         data_len = 1;
4204         break;
4205     }
4206 
4207     frame_len = 1 + 8 + 8 + (size_t)data_len;
4208     if (!TEST_ptr(frame_buf = OPENSSL_malloc(frame_len)))
4209         return 0;
4210 
4211     if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf, frame_len, 0)))
4212         goto err;
4213 
4214     if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_CRYPTO))
4215         || !TEST_true(WPACKET_quic_write_vlint(&wpkt, offset))
4216         || !TEST_true(WPACKET_quic_write_vlint(&wpkt, data_len)))
4217         goto err;
4218 
4219     for (i = 0; i < data_len; ++i)
4220         if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x42)))
4221             goto err;
4222 
4223     if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
4224         goto err;
4225 
4226     if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
4227         goto err;
4228 
4229     ok = 1;
4230 err:
4231     if (ok)
4232         WPACKET_finish(&wpkt);
4233     else
4234         WPACKET_cleanup(&wpkt);
4235     OPENSSL_free(frame_buf);
4236     return ok;
4237 }
4238 
4239 static const struct script_op script_53[] = {
4240     OP_S_SET_INJECT_PLAIN(script_53_inject_plain)
4241         OP_C_SET_ALPN("ossltest")
4242             OP_C_CONNECT_WAIT()
4243 
4244                 OP_C_WRITE(DEFAULT, "apple", 5)
4245                     OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
4246                         OP_S_READ_EXPECT(a, "apple", 5)
4247 
4248                             OP_SET_INJECT_WORD(1, 0)
4249                                 OP_S_WRITE(a, "Strawberry", 10)
4250 
4251                                     OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_CRYPTO_BUFFER_EXCEEDED, 0, 0)
4252 
4253                                         OP_END
4254 };
4255 
4256 /* 54. Fault injection - corrupted crypto stream data */
script_54_inject_handshake(struct helper * h,unsigned char * buf,size_t buf_len)4257 static int script_54_inject_handshake(struct helper *h,
4258     unsigned char *buf, size_t buf_len)
4259 {
4260     size_t i;
4261 
4262     for (i = 0; i < buf_len; ++i)
4263         buf[i] ^= 0xff;
4264 
4265     return 1;
4266 }
4267 
4268 static const struct script_op script_54[] = {
4269     OP_S_SET_INJECT_HANDSHAKE(script_54_inject_handshake)
4270         OP_C_SET_ALPN("ossltest")
4271             OP_C_CONNECT_WAIT_OR_FAIL()
4272 
4273                 OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_CRYPTO_UNEXPECTED_MESSAGE, 0, 0)
4274 
4275                     OP_END
4276 };
4277 
4278 /* 55. Fault injection - NEW_CONN_ID with >20 byte CID */
4279 static const struct script_op script_55[] = {
4280     OP_S_SET_INJECT_PLAIN(script_39_inject_plain)
4281         OP_C_SET_ALPN("ossltest")
4282             OP_C_CONNECT_WAIT()
4283                 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4284 
4285                     OP_C_NEW_STREAM_BIDI(a, C_BIDI_ID(0))
4286                         OP_C_WRITE(a, "apple", 5)
4287                             OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
4288                                 OP_S_READ_EXPECT(a, "apple", 5)
4289 
4290                                     OP_SET_INJECT_WORD(0, 2)
4291                                         OP_S_WRITE(a, "orange", 5)
4292 
4293                                             OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR, 0, 0)
4294 
4295                                                 OP_END
4296 };
4297 
4298 /* 56. Fault injection - NEW_CONN_ID with seq no < retire prior to */
4299 static const struct script_op script_56[] = {
4300     OP_S_SET_INJECT_PLAIN(script_39_inject_plain)
4301         OP_C_SET_ALPN("ossltest")
4302             OP_C_CONNECT_WAIT()
4303                 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4304 
4305                     OP_C_NEW_STREAM_BIDI(a, C_BIDI_ID(0))
4306                         OP_C_WRITE(a, "apple", 5)
4307                             OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
4308                                 OP_S_READ_EXPECT(a, "apple", 5)
4309 
4310                                     OP_SET_INJECT_WORD(0, 3)
4311                                         OP_S_WRITE(a, "orange", 5)
4312 
4313                                             OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR, 0, 0)
4314 
4315                                                 OP_END
4316 };
4317 
4318 /* 57. Fault injection - NEW_CONN_ID with lower seq so ignored */
4319 static const struct script_op script_57[] = {
4320     OP_S_SET_INJECT_PLAIN(script_39_inject_plain)
4321         OP_C_SET_ALPN("ossltest")
4322             OP_C_CONNECT_WAIT()
4323                 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4324 
4325                     OP_C_NEW_STREAM_BIDI(a, C_BIDI_ID(0))
4326                         OP_C_WRITE(a, "apple", 5)
4327                             OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
4328                                 OP_S_READ_EXPECT(a, "apple", 5)
4329 
4330                                     OP_SET_INJECT_WORD(0, 4)
4331                                         OP_S_WRITE(a, "orange", 5)
4332                                             OP_C_READ_EXPECT(a, "orange", 5)
4333 
4334                                                 OP_C_WRITE(a, "Strawberry", 10)
4335                                                     OP_S_READ_EXPECT(a, "Strawberry", 10)
4336 
4337     /*
4338      * Now we send a NEW_CONN_ID with a bogus CID. However the sequence number
4339      * is old so it should be ignored and we should still be able to
4340      * communicate.
4341      */
4342     OP_SET_INJECT_WORD(0, 5)
4343         OP_S_WRITE(a, "raspberry", 9)
4344             OP_C_READ_EXPECT(a, "raspberry", 9)
4345 
4346                 OP_C_WRITE(a, "peach", 5)
4347                     OP_S_READ_EXPECT(a, "peach", 5)
4348 
4349                         OP_END
4350 };
4351 
4352 /* 58. Fault injection - repeated HANDSHAKE_DONE */
script_58_inject_plain(struct helper * h,QUIC_PKT_HDR * hdr,unsigned char * buf,size_t len)4353 static int script_58_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
4354     unsigned char *buf, size_t len)
4355 {
4356     int ok = 0;
4357     unsigned char frame_buf[64];
4358     size_t written;
4359     WPACKET wpkt;
4360 
4361     if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT)
4362         return 1;
4363 
4364     if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
4365             sizeof(frame_buf), 0)))
4366         return 0;
4367 
4368     if (h->inject_word0 == 1) {
4369         if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE)))
4370             goto err;
4371     } else {
4372         /* Needless multi-byte encoding */
4373         if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x40))
4374             || !TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x1E)))
4375             goto err;
4376     }
4377 
4378     if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
4379         goto err;
4380 
4381     if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
4382         goto err;
4383 
4384     ok = 1;
4385 err:
4386     if (ok)
4387         WPACKET_finish(&wpkt);
4388     else
4389         WPACKET_cleanup(&wpkt);
4390     return ok;
4391 }
4392 
4393 static const struct script_op script_58[] = {
4394     OP_S_SET_INJECT_PLAIN(script_58_inject_plain)
4395         OP_C_SET_ALPN("ossltest")
4396             OP_C_CONNECT_WAIT()
4397 
4398                 OP_C_WRITE(DEFAULT, "apple", 5)
4399                     OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
4400                         OP_S_READ_EXPECT(a, "apple", 5)
4401 
4402                             OP_SET_INJECT_WORD(1, 0)
4403 
4404                                 OP_S_WRITE(a, "orange", 6)
4405                                     OP_C_READ_EXPECT(DEFAULT, "orange", 6)
4406 
4407                                         OP_C_WRITE(DEFAULT, "Strawberry", 10)
4408                                             OP_S_READ_EXPECT(a, "Strawberry", 10)
4409 
4410                                                 OP_END
4411 };
4412 
4413 /* 59. Fault injection - multi-byte frame encoding */
4414 static const struct script_op script_59[] = {
4415     OP_S_SET_INJECT_PLAIN(script_58_inject_plain)
4416         OP_C_SET_ALPN("ossltest")
4417             OP_C_CONNECT_WAIT()
4418 
4419                 OP_C_WRITE(DEFAULT, "apple", 5)
4420                     OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
4421                         OP_S_READ_EXPECT(a, "apple", 5)
4422 
4423                             OP_SET_INJECT_WORD(2, 0)
4424 
4425                                 OP_S_WRITE(a, "orange", 6)
4426 
4427                                     OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_PROTOCOL_VIOLATION, 0, 0)
4428 
4429                                         OP_END
4430 };
4431 
4432 /* 60. Connection close reason truncation */
4433 static char long_reason[2048];
4434 
init_reason(struct helper * h,struct helper_local * hl)4435 static int init_reason(struct helper *h, struct helper_local *hl)
4436 {
4437     memset(long_reason, '~', sizeof(long_reason));
4438     memcpy(long_reason, "This is a long reason string.", 29);
4439     long_reason[OSSL_NELEM(long_reason) - 1] = '\0';
4440     return 1;
4441 }
4442 
check_shutdown_reason(struct helper * h,struct helper_local * hl)4443 static int check_shutdown_reason(struct helper *h, struct helper_local *hl)
4444 {
4445     const QUIC_TERMINATE_CAUSE *tc = ossl_quic_tserver_get_terminate_cause(ACQUIRE_S());
4446 
4447     if (tc == NULL) {
4448         h->check_spin_again = 1;
4449         return 0;
4450     }
4451 
4452     if (!TEST_size_t_ge(tc->reason_len, 50)
4453         || !TEST_mem_eq(long_reason, tc->reason_len,
4454             tc->reason, tc->reason_len))
4455         return 0;
4456 
4457     return 1;
4458 }
4459 
4460 static const struct script_op script_60[] = {
4461     OP_C_SET_ALPN("ossltest")
4462         OP_C_CONNECT_WAIT()
4463 
4464             OP_C_WRITE(DEFAULT, "apple", 5)
4465                 OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
4466                     OP_S_READ_EXPECT(a, "apple", 5)
4467 
4468                         OP_CHECK(init_reason, 0)
4469                             OP_C_SHUTDOWN_WAIT(long_reason, 0)
4470                                 OP_CHECK(check_shutdown_reason, 0)
4471 
4472                                     OP_END
4473 };
4474 
4475 /* 61. Fault injection - RESET_STREAM exceeding stream count FC */
script_61_inject_plain(struct helper * h,QUIC_PKT_HDR * hdr,unsigned char * buf,size_t len)4476 static int script_61_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
4477     unsigned char *buf, size_t len)
4478 {
4479     int ok = 0;
4480     WPACKET wpkt;
4481     unsigned char frame_buf[32];
4482     size_t written;
4483 
4484     if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT)
4485         return 1;
4486 
4487     if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
4488             sizeof(frame_buf), 0)))
4489         return 0;
4490 
4491     if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word0))
4492         || !TEST_true(WPACKET_quic_write_vlint(&wpkt, /* stream ID */
4493             h->inject_word1))
4494         || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 123))
4495         || (h->inject_word0 == OSSL_QUIC_FRAME_TYPE_RESET_STREAM
4496             && !TEST_true(WPACKET_quic_write_vlint(&wpkt, 0)))) /* final size */
4497         goto err;
4498 
4499     if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
4500         goto err;
4501 
4502     if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
4503         goto err;
4504 
4505     ok = 1;
4506 err:
4507     if (ok)
4508         WPACKET_finish(&wpkt);
4509     else
4510         WPACKET_cleanup(&wpkt);
4511     return ok;
4512 }
4513 
4514 static const struct script_op script_61[] = {
4515     OP_S_SET_INJECT_PLAIN(script_61_inject_plain)
4516         OP_C_SET_ALPN("ossltest")
4517             OP_C_CONNECT_WAIT()
4518                 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4519 
4520                     OP_C_NEW_STREAM_BIDI(a, C_BIDI_ID(0))
4521                         OP_C_WRITE(a, "orange", 6)
4522 
4523                             OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
4524                                 OP_S_READ_EXPECT(a, "orange", 6)
4525 
4526                                     OP_SET_INJECT_WORD(OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
4527                                         S_BIDI_ID(OSSL_QUIC_VLINT_MAX / 4))
4528                                         OP_S_WRITE(a, "fruit", 5)
4529 
4530                                             OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_LIMIT_ERROR, 0, 0)
4531 
4532                                                 OP_END
4533 };
4534 
4535 /* 62. Fault injection - STOP_SENDING with high ID */
4536 static const struct script_op script_62[] = {
4537     OP_S_SET_INJECT_PLAIN(script_61_inject_plain)
4538         OP_C_SET_ALPN("ossltest")
4539             OP_C_CONNECT_WAIT()
4540                 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4541 
4542                     OP_C_NEW_STREAM_BIDI(a, C_BIDI_ID(0))
4543                         OP_C_WRITE(a, "orange", 6)
4544 
4545                             OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
4546                                 OP_S_READ_EXPECT(a, "orange", 6)
4547 
4548                                     OP_SET_INJECT_WORD(OSSL_QUIC_FRAME_TYPE_STOP_SENDING,
4549                                         C_BIDI_ID(OSSL_QUIC_VLINT_MAX / 4))
4550                                         OP_S_WRITE(a, "fruit", 5)
4551 
4552                                             OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR, 0, 0)
4553 
4554                                                 OP_END
4555 };
4556 
4557 /* 63. Fault injection - STREAM frame exceeding stream limit */
4558 static const struct script_op script_63[] = {
4559     OP_S_SET_INJECT_PLAIN(script_32_inject_plain)
4560         OP_C_SET_ALPN("ossltest")
4561             OP_C_CONNECT_WAIT()
4562                 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4563 
4564                     OP_C_NEW_STREAM_BIDI(a, C_BIDI_ID(0))
4565                         OP_C_WRITE(a, "apple", 5)
4566 
4567                             OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
4568                                 OP_S_READ_EXPECT(a, "apple", 5)
4569 
4570                                     OP_SET_INJECT_WORD(S_BIDI_ID(5000) + 1, 4)
4571                                         OP_S_WRITE(a, "orange", 6)
4572 
4573                                             OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_LIMIT_ERROR, 0, 0)
4574 
4575                                                 OP_END
4576 };
4577 
4578 /* 64. Fault injection - STREAM - zero-length no-FIN is accepted */
4579 static const struct script_op script_64[] = {
4580     OP_S_SET_INJECT_PLAIN(script_32_inject_plain)
4581         OP_C_SET_ALPN("ossltest")
4582             OP_C_CONNECT_WAIT()
4583                 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4584 
4585                     OP_S_NEW_STREAM_UNI(a, S_UNI_ID(0))
4586                         OP_S_WRITE(a, "apple", 5)
4587 
4588                             OP_C_ACCEPT_STREAM_WAIT(a)
4589                                 OP_C_READ_EXPECT(a, "apple", 5)
4590 
4591                                     OP_SET_INJECT_WORD(S_BIDI_ID(20) + 1, 1)
4592                                         OP_S_WRITE(a, "orange", 6)
4593                                             OP_C_READ_EXPECT(a, "orange", 6)
4594 
4595                                                 OP_END
4596 };
4597 
4598 /* 65. Fault injection - CRYPTO - zero-length is accepted */
script_65_inject_plain(struct helper * h,QUIC_PKT_HDR * hdr,unsigned char * buf,size_t len)4599 static int script_65_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
4600     unsigned char *buf, size_t len)
4601 {
4602     int ok = 0;
4603     unsigned char frame_buf[64];
4604     size_t written;
4605     WPACKET wpkt;
4606 
4607     if (h->inject_word0 == 0)
4608         return 1;
4609 
4610     --h->inject_word0;
4611 
4612     if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
4613             sizeof(frame_buf), 0)))
4614         return 0;
4615 
4616     if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_CRYPTO))
4617         || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 0))
4618         || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 0)))
4619         goto err;
4620 
4621     if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
4622         goto err;
4623 
4624     if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
4625         goto err;
4626 
4627     ok = 1;
4628 err:
4629     if (ok)
4630         WPACKET_finish(&wpkt);
4631     else
4632         WPACKET_cleanup(&wpkt);
4633     return ok;
4634 }
4635 
4636 static const struct script_op script_65[] = {
4637     OP_S_SET_INJECT_PLAIN(script_65_inject_plain)
4638         OP_C_SET_ALPN("ossltest")
4639             OP_C_CONNECT_WAIT()
4640                 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4641 
4642                     OP_C_NEW_STREAM_BIDI(a, C_BIDI_ID(0))
4643                         OP_C_WRITE(a, "apple", 5)
4644 
4645                             OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
4646                                 OP_S_READ_EXPECT(a, "apple", 5)
4647 
4648                                     OP_SET_INJECT_WORD(1, 0)
4649                                         OP_S_WRITE(a, "orange", 6)
4650                                             OP_C_READ_EXPECT(a, "orange", 6)
4651 
4652                                                 OP_END
4653 };
4654 
4655 /* 66. Fault injection - large MAX_STREAM_DATA */
script_66_inject_plain(struct helper * h,QUIC_PKT_HDR * hdr,unsigned char * buf,size_t len)4656 static int script_66_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
4657     unsigned char *buf, size_t len)
4658 {
4659     int ok = 0;
4660     WPACKET wpkt;
4661     unsigned char frame_buf[64];
4662     size_t written;
4663 
4664     if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT)
4665         return 1;
4666 
4667     if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
4668             sizeof(frame_buf), 0)))
4669         return 0;
4670 
4671     if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1)))
4672         goto err;
4673 
4674     if (h->inject_word1 == OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA)
4675         if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, /* stream ID */
4676                 h->inject_word0 - 1)))
4677             goto err;
4678 
4679     if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_VLINT_MAX)))
4680         goto err;
4681 
4682     if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
4683         goto err;
4684 
4685     if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
4686         goto err;
4687 
4688     ok = 1;
4689 err:
4690     if (ok)
4691         WPACKET_finish(&wpkt);
4692     else
4693         WPACKET_cleanup(&wpkt);
4694     return ok;
4695 }
4696 
4697 static const struct script_op script_66[] = {
4698     OP_S_SET_INJECT_PLAIN(script_66_inject_plain)
4699         OP_C_SET_ALPN("ossltest")
4700             OP_C_CONNECT_WAIT()
4701                 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4702 
4703                     OP_S_NEW_STREAM_BIDI(a, S_BIDI_ID(0))
4704                         OP_S_WRITE(a, "apple", 5)
4705 
4706                             OP_C_ACCEPT_STREAM_WAIT(a)
4707                                 OP_C_READ_EXPECT(a, "apple", 5)
4708 
4709                                     OP_SET_INJECT_WORD(S_BIDI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA)
4710                                         OP_S_WRITE(a, "orange", 6)
4711                                             OP_C_READ_EXPECT(a, "orange", 6)
4712                                                 OP_C_WRITE(a, "Strawberry", 10)
4713                                                     OP_S_READ_EXPECT(a, "Strawberry", 10)
4714 
4715                                                         OP_END
4716 };
4717 
4718 /* 67. Fault injection - large MAX_DATA */
4719 static const struct script_op script_67[] = {
4720     OP_S_SET_INJECT_PLAIN(script_66_inject_plain)
4721         OP_C_SET_ALPN("ossltest")
4722             OP_C_CONNECT_WAIT()
4723                 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4724 
4725                     OP_S_NEW_STREAM_BIDI(a, S_BIDI_ID(0))
4726                         OP_S_WRITE(a, "apple", 5)
4727 
4728                             OP_C_ACCEPT_STREAM_WAIT(a)
4729                                 OP_C_READ_EXPECT(a, "apple", 5)
4730 
4731                                     OP_SET_INJECT_WORD(1, OSSL_QUIC_FRAME_TYPE_MAX_DATA)
4732                                         OP_S_WRITE(a, "orange", 6)
4733                                             OP_C_READ_EXPECT(a, "orange", 6)
4734                                                 OP_C_WRITE(a, "Strawberry", 10)
4735                                                     OP_S_READ_EXPECT(a, "Strawberry", 10)
4736 
4737                                                         OP_END
4738 };
4739 
4740 /* 68. Fault injection - Unexpected TLS messages */
script_68_inject_handshake(struct helper * h,unsigned char * msg,size_t msglen)4741 static int script_68_inject_handshake(struct helper *h, unsigned char *msg,
4742     size_t msglen)
4743 {
4744     const unsigned char *data;
4745     size_t datalen;
4746     const unsigned char certreq[] = {
4747         SSL3_MT_CERTIFICATE_REQUEST, /* CertificateRequest message */
4748         0, 0, 12, /* Length of message */
4749         1, 1, /* certificate_request_context */
4750         0, 8, /* Extensions block length */
4751         0, TLSEXT_TYPE_signature_algorithms, /* sig_algs extension*/
4752         0, 4, /* 4 bytes of sig algs extension*/
4753         0, 2, /* sigalgs list is 2 bytes long */
4754         8, 4 /* rsa_pss_rsae_sha256 */
4755     };
4756     const unsigned char keyupdate[] = {
4757         SSL3_MT_KEY_UPDATE, /* KeyUpdate message */
4758         0, 0, 1, /* Length of message */
4759         SSL_KEY_UPDATE_NOT_REQUESTED /* update_not_requested */
4760     };
4761 
4762     /* We transform the NewSessionTicket message into something else */
4763     switch (h->inject_word0) {
4764     case 0:
4765         return 1;
4766 
4767     case 1:
4768         /* CertificateRequest message */
4769         data = certreq;
4770         datalen = sizeof(certreq);
4771         break;
4772 
4773     case 2:
4774         /* KeyUpdate message */
4775         data = keyupdate;
4776         datalen = sizeof(keyupdate);
4777         break;
4778 
4779     default:
4780         return 0;
4781     }
4782 
4783     if (!TEST_true(qtest_fault_resize_message(h->qtf,
4784             datalen - SSL3_HM_HEADER_LENGTH)))
4785         return 0;
4786 
4787     memcpy(msg, data, datalen);
4788 
4789     return 1;
4790 }
4791 
4792 /* Send a CerticateRequest message post-handshake */
4793 static const struct script_op script_68[] = {
4794     OP_S_SET_INJECT_HANDSHAKE(script_68_inject_handshake)
4795         OP_C_SET_ALPN("ossltest")
4796             OP_C_CONNECT_WAIT()
4797                 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4798 
4799                     OP_C_NEW_STREAM_BIDI(a, C_BIDI_ID(0))
4800                         OP_C_WRITE(a, "apple", 5)
4801                             OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
4802                                 OP_S_READ_EXPECT(a, "apple", 5)
4803 
4804                                     OP_SET_INJECT_WORD(1, 0)
4805                                         OP_S_NEW_TICKET()
4806                                             OP_S_WRITE(a, "orange", 6)
4807 
4808                                                 OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_PROTOCOL_VIOLATION, 0, 0)
4809 
4810                                                     OP_END
4811 };
4812 
4813 /* 69. Send a TLS KeyUpdate message post-handshake */
4814 static const struct script_op script_69[] = {
4815     OP_S_SET_INJECT_HANDSHAKE(script_68_inject_handshake)
4816         OP_C_SET_ALPN("ossltest")
4817             OP_C_CONNECT_WAIT()
4818                 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4819 
4820                     OP_C_NEW_STREAM_BIDI(a, C_BIDI_ID(0))
4821                         OP_C_WRITE(a, "apple", 5)
4822                             OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
4823                                 OP_S_READ_EXPECT(a, "apple", 5)
4824 
4825                                     OP_SET_INJECT_WORD(2, 0)
4826                                         OP_S_NEW_TICKET()
4827                                             OP_S_WRITE(a, "orange", 6)
4828 
4829                                                 OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_CRYPTO_ERR_BEGIN
4830                                                         + SSL_AD_UNEXPECTED_MESSAGE,
4831                                                     0, 0)
4832 
4833                                                     OP_END
4834 };
4835 
set_max_early_data(struct helper * h,struct helper_local * hl)4836 static int set_max_early_data(struct helper *h, struct helper_local *hl)
4837 {
4838 
4839     if (!TEST_true(ossl_quic_tserver_set_max_early_data(ACQUIRE_S(),
4840             (uint32_t)hl->check_op->arg2)))
4841         return 0;
4842 
4843     return 1;
4844 }
4845 
4846 /* 70. Send a TLS NewSessionTicket message with invalid max_early_data */
4847 static const struct script_op script_70[] = {
4848     OP_C_SET_ALPN("ossltest")
4849         OP_C_CONNECT_WAIT()
4850             OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4851 
4852                 OP_C_NEW_STREAM_BIDI(a, C_BIDI_ID(0))
4853                     OP_C_WRITE(a, "apple", 5)
4854                         OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
4855                             OP_S_READ_EXPECT(a, "apple", 5)
4856 
4857                                 OP_CHECK(set_max_early_data, 0xfffffffe)
4858                                     OP_S_NEW_TICKET()
4859                                         OP_S_WRITE(a, "orange", 6)
4860 
4861                                             OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_PROTOCOL_VIOLATION, 0, 0)
4862 
4863                                                 OP_END
4864 };
4865 
4866 /* 71. Send a TLS NewSessionTicket message with valid max_early_data */
4867 static const struct script_op script_71[] = {
4868     OP_C_SET_ALPN("ossltest")
4869         OP_C_CONNECT_WAIT()
4870             OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4871 
4872                 OP_C_NEW_STREAM_BIDI(a, C_BIDI_ID(0))
4873                     OP_C_WRITE(a, "apple", 5)
4874                         OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
4875                             OP_S_READ_EXPECT(a, "apple", 5)
4876 
4877                                 OP_CHECK(set_max_early_data, 0xffffffff)
4878                                     OP_S_NEW_TICKET()
4879                                         OP_S_WRITE(a, "orange", 6)
4880                                             OP_C_READ_EXPECT(a, "orange", 6)
4881 
4882                                                 OP_END
4883 };
4884 
4885 /* 72. Test that APL stops handing out streams after limit reached (bidi) */
script_72_check(struct helper * h,struct helper_local * hl)4886 static int script_72_check(struct helper *h, struct helper_local *hl)
4887 {
4888     if (!TEST_uint64_t_ge(h->fail_count, 50))
4889         return 0;
4890 
4891     return 1;
4892 }
4893 
4894 static const struct script_op script_72[] = {
4895     OP_C_SET_ALPN("ossltest")
4896         OP_C_CONNECT_WAIT()
4897             OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4898 
4899     /*
4900      * Request more streams than a server will initially hand out and test that
4901      * they fail properly.
4902      */
4903     OP_BEGIN_REPEAT(200)
4904 
4905         OP_C_NEW_STREAM_BIDI_EX(a, ANY_ID, ALLOW_FAIL | SSL_STREAM_FLAG_NO_BLOCK)
4906             OP_C_SKIP_IF_UNBOUND(a, 2)
4907                 OP_C_WRITE(a, "apple", 5)
4908                     OP_C_FREE_STREAM(a)
4909 
4910                         OP_END_REPEAT()
4911 
4912                             OP_CHECK(script_72_check, 0)
4913 
4914                                 OP_END
4915 };
4916 
4917 /* 73. Test that APL stops handing out streams after limit reached (uni) */
4918 static const struct script_op script_73[] = {
4919     OP_C_SET_ALPN("ossltest")
4920         OP_C_CONNECT_WAIT()
4921             OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4922 
4923     /*
4924      * Request more streams than a server will initially hand out and test that
4925      * they fail properly.
4926      */
4927     OP_BEGIN_REPEAT(200)
4928 
4929         OP_C_NEW_STREAM_UNI_EX(a, ANY_ID, ALLOW_FAIL | SSL_STREAM_FLAG_NO_BLOCK)
4930             OP_C_SKIP_IF_UNBOUND(a, 2)
4931                 OP_C_WRITE(a, "apple", 5)
4932                     OP_C_FREE_STREAM(a)
4933 
4934                         OP_END_REPEAT()
4935 
4936                             OP_CHECK(script_72_check, 0)
4937 
4938                                 OP_END
4939 };
4940 
4941 /* 74. Version negotiation: QUIC_VERSION_1 ignored */
generate_version_neg(WPACKET * wpkt,uint32_t version)4942 static int generate_version_neg(WPACKET *wpkt, uint32_t version)
4943 {
4944     QUIC_PKT_HDR hdr = { 0 };
4945 
4946     hdr.type = QUIC_PKT_TYPE_VERSION_NEG;
4947     hdr.version = 0;
4948     hdr.fixed = 1;
4949     hdr.dst_conn_id.id_len = 0;
4950     hdr.src_conn_id.id_len = 8;
4951     memset(hdr.src_conn_id.id, 0x55, 8);
4952 
4953     if (!TEST_true(ossl_quic_wire_encode_pkt_hdr(wpkt, 0, &hdr, NULL)))
4954         return 0;
4955 
4956     if (!TEST_true(WPACKET_put_bytes_u32(wpkt, version)))
4957         return 0;
4958 
4959     return 1;
4960 }
4961 
server_gen_version_neg(struct helper * h,BIO_MSG * msg,size_t stride)4962 static int server_gen_version_neg(struct helper *h, BIO_MSG *msg, size_t stride)
4963 {
4964     int rc = 0, have_wpkt = 0;
4965     size_t l;
4966     WPACKET wpkt;
4967     BUF_MEM *buf = NULL;
4968     uint32_t version;
4969 
4970     switch (h->inject_word0) {
4971     case 0:
4972         return 1;
4973     case 1:
4974         version = QUIC_VERSION_1;
4975         break;
4976     default:
4977         version = 0x5432abcd;
4978         break;
4979     }
4980 
4981     if (!TEST_ptr(buf = BUF_MEM_new()))
4982         goto err;
4983 
4984     if (!TEST_true(WPACKET_init(&wpkt, buf)))
4985         goto err;
4986 
4987     have_wpkt = 1;
4988 
4989     generate_version_neg(&wpkt, version);
4990 
4991     if (!TEST_true(WPACKET_get_total_written(&wpkt, &l)))
4992         goto err;
4993 
4994     if (!TEST_true(qtest_fault_resize_datagram(h->qtf, l)))
4995         return 0;
4996 
4997     memcpy(msg->data, buf->data, l);
4998     h->inject_word0 = 0;
4999 
5000     rc = 1;
5001 err:
5002     if (have_wpkt)
5003         WPACKET_finish(&wpkt);
5004 
5005     BUF_MEM_free(buf);
5006     return rc;
5007 }
5008 
5009 static int do_mutation = 0;
5010 static QUIC_PKT_HDR *hdr_to_free = NULL;
5011 
5012 /*
5013  * Check packets to transmit, if we have an initial packet
5014  * Modify the version number to something incorrect
5015  * so that we trigger a version negotiation
5016  * Note, this is a use once function, it will only modify the
5017  * first INITIAL packet it sees, after which it needs to be
5018  * armed again
5019  */
script_74_alter_version(const QUIC_PKT_HDR * hdrin,const OSSL_QTX_IOVEC * iovecin,size_t numin,QUIC_PKT_HDR ** hdrout,const OSSL_QTX_IOVEC ** iovecout,size_t * numout,void * arg)5020 static int script_74_alter_version(const QUIC_PKT_HDR *hdrin,
5021     const OSSL_QTX_IOVEC *iovecin, size_t numin,
5022     QUIC_PKT_HDR **hdrout,
5023     const OSSL_QTX_IOVEC **iovecout,
5024     size_t *numout,
5025     void *arg)
5026 {
5027     *hdrout = OPENSSL_memdup(hdrin, sizeof(QUIC_PKT_HDR));
5028     *iovecout = iovecin;
5029     *numout = numin;
5030     hdr_to_free = *hdrout;
5031 
5032     if (do_mutation == 0)
5033         return 1;
5034     do_mutation = 0;
5035 
5036     if (hdrin->type == QUIC_PKT_TYPE_INITIAL)
5037         (*hdrout)->version = 0xdeadbeef;
5038     return 1;
5039 }
5040 
script_74_finish_mutation(void * arg)5041 static void script_74_finish_mutation(void *arg)
5042 {
5043     OPENSSL_free(hdr_to_free);
5044 }
5045 
5046 /*
5047  * Enable the packet mutator for the client channel
5048  * So that when we send a Initial packet
5049  * We modify the version to be something invalid
5050  * to force a version negotiation
5051  */
script_74_arm_packet_mutator(struct helper * h,struct helper_local * hl)5052 static int script_74_arm_packet_mutator(struct helper *h,
5053     struct helper_local *hl)
5054 {
5055     QUIC_CHANNEL *ch = ossl_quic_conn_get_channel(h->c_conn);
5056 
5057     do_mutation = 1;
5058     if (!ossl_quic_channel_set_mutator(ch, script_74_alter_version,
5059             script_74_finish_mutation,
5060             NULL))
5061         return 0;
5062     return 1;
5063 }
5064 
5065 static const struct script_op script_74[] = {
5066     OP_CHECK(script_74_arm_packet_mutator, 0)
5067         OP_C_SET_ALPN("ossltest")
5068             OP_C_CONNECT_WAIT()
5069 
5070                 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
5071 
5072                     OP_C_NEW_STREAM_BIDI(a, C_BIDI_ID(0))
5073                         OP_C_WRITE(a, "apple", 5)
5074                             OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
5075                                 OP_S_READ_EXPECT(a, "apple", 5)
5076 
5077                                     OP_END
5078 };
5079 
5080 /* 75. Version negotiation: Unknown version causes connection abort */
5081 static const struct script_op script_75[] = {
5082     OP_S_SET_INJECT_DATAGRAM(server_gen_version_neg)
5083         OP_SET_INJECT_WORD(2, 0)
5084 
5085             OP_C_SET_ALPN("ossltest")
5086                 OP_C_CONNECT_WAIT_OR_FAIL()
5087 
5088                     OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_CONNECTION_REFUSED, 0, 0)
5089 
5090                         OP_END
5091 };
5092 
5093 /* 76. Test peer-initiated shutdown wait */
script_76_check(struct helper * h,struct helper_local * hl)5094 static int script_76_check(struct helper *h, struct helper_local *hl)
5095 {
5096     if (!TEST_false(SSL_shutdown_ex(h->c_conn,
5097             SSL_SHUTDOWN_FLAG_WAIT_PEER
5098                 | SSL_SHUTDOWN_FLAG_NO_BLOCK,
5099             NULL, 0)))
5100         return 0;
5101 
5102     return 1;
5103 }
5104 
5105 static const struct script_op script_76[] = {
5106     OP_C_SET_ALPN("ossltest")
5107         OP_C_CONNECT_WAIT()
5108             OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
5109 
5110                 OP_C_NEW_STREAM_BIDI(a, C_BIDI_ID(0))
5111                     OP_C_WRITE(a, "apple", 5)
5112 
5113                         OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
5114                             OP_S_READ_EXPECT(a, "apple", 5)
5115 
5116     /* Check a WAIT_PEER call doesn't succeed yet. */
5117     OP_CHECK(script_76_check, 0)
5118         OP_S_SHUTDOWN(42)
5119 
5120             OP_C_SHUTDOWN_WAIT(NULL, SSL_SHUTDOWN_FLAG_WAIT_PEER)
5121                 OP_C_EXPECT_CONN_CLOSE_INFO(42, 1, 1)
5122 
5123                     OP_END
5124 };
5125 
5126 /* 77. Ensure default stream popping operates correctly */
5127 static const struct script_op script_77[] = {
5128     OP_C_SET_ALPN("ossltest")
5129         OP_C_CONNECT_WAIT()
5130 
5131             OP_C_SET_INCOMING_STREAM_POLICY(SSL_INCOMING_STREAM_POLICY_ACCEPT)
5132 
5133                 OP_S_NEW_STREAM_BIDI(a, S_BIDI_ID(0))
5134                     OP_S_WRITE(a, "Strawberry", 10)
5135 
5136                         OP_C_READ_EXPECT(DEFAULT, "Strawberry", 10)
5137 
5138                             OP_S_NEW_STREAM_BIDI(b, S_BIDI_ID(1))
5139                                 OP_S_WRITE(b, "xyz", 3)
5140 
5141                                     OP_C_ACCEPT_STREAM_WAIT(b)
5142                                         OP_C_READ_EXPECT(b, "xyz", 3)
5143 
5144                                             OP_END
5145 };
5146 
5147 /* 78. Post-connection session ticket handling */
5148 static size_t new_session_count;
5149 
on_new_session(SSL * s,SSL_SESSION * sess)5150 static int on_new_session(SSL *s, SSL_SESSION *sess)
5151 {
5152     ++new_session_count;
5153     return 0; /* do not ref session, we aren't keeping it */
5154 }
5155 
setup_session(struct helper * h,struct helper_local * hl)5156 static int setup_session(struct helper *h, struct helper_local *hl)
5157 {
5158     SSL_CTX_set_session_cache_mode(h->c_ctx, SSL_SESS_CACHE_BOTH);
5159     SSL_CTX_sess_set_new_cb(h->c_ctx, on_new_session);
5160     return 1;
5161 }
5162 
trigger_late_session_ticket(struct helper * h,struct helper_local * hl)5163 static int trigger_late_session_ticket(struct helper *h, struct helper_local *hl)
5164 {
5165     new_session_count = 0;
5166 
5167     if (!TEST_true(ossl_quic_tserver_new_ticket(ACQUIRE_S())))
5168         return 0;
5169 
5170     return 1;
5171 }
5172 
check_got_session_ticket(struct helper * h,struct helper_local * hl)5173 static int check_got_session_ticket(struct helper *h, struct helper_local *hl)
5174 {
5175     if (!TEST_size_t_gt(new_session_count, 0))
5176         return 0;
5177 
5178     return 1;
5179 }
5180 
5181 static int check_idle_timeout(struct helper *h, struct helper_local *hl);
5182 
5183 static const struct script_op script_78[] = {
5184     OP_C_SET_ALPN("ossltest")
5185         OP_CHECK(setup_session, 0)
5186             OP_C_CONNECT_WAIT()
5187 
5188                 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
5189 
5190                     OP_C_NEW_STREAM_BIDI(a, C_BIDI_ID(0))
5191                         OP_C_WRITE(a, "apple", 5)
5192 
5193                             OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
5194                                 OP_S_READ_EXPECT(a, "apple", 5)
5195 
5196                                     OP_S_WRITE(a, "orange", 6)
5197                                         OP_C_READ_EXPECT(a, "orange", 6)
5198 
5199                                             OP_CHECK(trigger_late_session_ticket, 0)
5200 
5201                                                 OP_S_WRITE(a, "Strawberry", 10)
5202                                                     OP_C_READ_EXPECT(a, "Strawberry", 10)
5203 
5204                                                         OP_CHECK(check_got_session_ticket, 0)
5205                                                             OP_CHECK2(check_idle_timeout,
5206                                                                 SSL_VALUE_CLASS_FEATURE_NEGOTIATED, 30000)
5207 
5208                                                                 OP_END
5209 };
5210 
5211 /* 79. Optimised FIN test */
5212 static const struct script_op script_79[] = {
5213     OP_C_SET_ALPN("ossltest")
5214         OP_C_CONNECT_WAIT()
5215             OP_C_WRITE_EX2(DEFAULT, "apple", 5, SSL_WRITE_FLAG_CONCLUDE)
5216                 OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
5217                     OP_S_READ_EXPECT(a, "apple", 5)
5218                         OP_S_EXPECT_FIN(a)
5219                             OP_S_WRITE(a, "orange", 6)
5220                                 OP_S_CONCLUDE(a)
5221                                     OP_C_READ_EXPECT(DEFAULT, "orange", 6)
5222                                         OP_C_EXPECT_FIN(DEFAULT)
5223                                             OP_END
5224 };
5225 
5226 /* 80. Stateless reset detection test */
5227 static QUIC_STATELESS_RESET_TOKEN test_reset_token = {
5228     { 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
5229         0xde, 0xad, 0xbe, 0xef }
5230 };
5231 
5232 /*
5233  * Generate a packet in the following format:
5234  * https://www.rfc-editor.org/rfc/rfc9000.html#name-stateless-reset
5235  * Stateless Reset {
5236  *  Fixed Bits (2): 1
5237  *  Unpredictable bits (38..)
5238  *  Stateless reset token (128)
5239  *  }
5240  */
script_80_send_stateless_reset(struct helper * h,QUIC_PKT_HDR * hdr,unsigned char * buf,size_t len)5241 static int script_80_send_stateless_reset(struct helper *h, QUIC_PKT_HDR *hdr,
5242     unsigned char *buf, size_t len)
5243 {
5244     unsigned char databuf[64];
5245 
5246     if (h->inject_word1 == 0)
5247         return 1;
5248 
5249     h->inject_word1 = 0;
5250 
5251     fprintf(stderr, "Sending stateless reset\n");
5252 
5253     RAND_bytes(databuf, 64);
5254     databuf[0] = 0x40;
5255     memcpy(&databuf[48], test_reset_token.token,
5256         sizeof(test_reset_token.token));
5257 
5258     if (!TEST_int_eq(SSL_inject_net_dgram(h->c_conn, databuf, sizeof(databuf),
5259                          NULL, h->s_net_bio_addr),
5260             1))
5261         return 0;
5262 
5263     return 1;
5264 }
5265 
script_80_gen_new_conn_id(struct helper * h,QUIC_PKT_HDR * hdr,unsigned char * buf,size_t len)5266 static int script_80_gen_new_conn_id(struct helper *h, QUIC_PKT_HDR *hdr,
5267     unsigned char *buf, size_t len)
5268 {
5269     int rc = 0;
5270     size_t l;
5271     unsigned char frame_buf[64];
5272     WPACKET wpkt;
5273     QUIC_CONN_ID new_cid = { 0 };
5274     OSSL_QUIC_FRAME_NEW_CONN_ID ncid = { 0 };
5275     QUIC_CHANNEL *ch = ossl_quic_tserver_get_channel(ACQUIRE_S_NOHL());
5276 
5277     if (h->inject_word0 == 0)
5278         return 1;
5279 
5280     h->inject_word0 = 0;
5281 
5282     fprintf(stderr, "sending new conn id\n");
5283     if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
5284             sizeof(frame_buf), 0)))
5285         return 0;
5286 
5287     ossl_quic_channel_get_diag_local_cid(ch, &new_cid);
5288 
5289     ncid.seq_num = 2;
5290     ncid.retire_prior_to = 2;
5291     ncid.conn_id = new_cid;
5292     memcpy(ncid.stateless_reset.token, test_reset_token.token,
5293         sizeof(test_reset_token.token));
5294 
5295     if (!TEST_true(ossl_quic_wire_encode_frame_new_conn_id(&wpkt, &ncid)))
5296         goto err;
5297 
5298     if (!TEST_true(WPACKET_get_total_written(&wpkt, &l)))
5299         goto err;
5300 
5301     if (!qtest_fault_prepend_frame(h->qtf, frame_buf, l))
5302         goto err;
5303 
5304     rc = 1;
5305 err:
5306     if (rc)
5307         WPACKET_finish(&wpkt);
5308     else
5309         WPACKET_cleanup(&wpkt);
5310 
5311     return rc;
5312 }
5313 
script_80_inject_pkt(struct helper * h,QUIC_PKT_HDR * hdr,unsigned char * buf,size_t len)5314 static int script_80_inject_pkt(struct helper *h, QUIC_PKT_HDR *hdr,
5315     unsigned char *buf, size_t len)
5316 {
5317     if (h->inject_word1 == 1)
5318         return script_80_send_stateless_reset(h, hdr, buf, len);
5319     else if (h->inject_word0 == 1)
5320         return script_80_gen_new_conn_id(h, hdr, buf, len);
5321 
5322     return 1;
5323 }
5324 
5325 static const struct script_op script_80[] = {
5326     OP_S_SET_INJECT_PLAIN(script_80_inject_pkt)
5327         OP_C_SET_ALPN("ossltest")
5328             OP_C_CONNECT_WAIT()
5329                 OP_C_WRITE(DEFAULT, "apple", 5)
5330                     OP_C_CONCLUDE(DEFAULT)
5331                         OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
5332                             OP_S_READ_EXPECT(a, "apple", 5)
5333                                 OP_SET_INJECT_WORD(1, 0)
5334                                     OP_S_WRITE(a, "apple", 5)
5335                                         OP_C_READ_EXPECT(DEFAULT, "apple", 5)
5336                                             OP_SET_INJECT_WORD(0, 1)
5337                                                 OP_S_WRITE(a, "apple", 5)
5338                                                     OP_C_EXPECT_CONN_CLOSE_INFO(0, 0, 1)
5339                                                         OP_END
5340 };
5341 
5342 /* 81. Idle timeout configuration */
modify_idle_timeout(struct helper * h,struct helper_local * hl)5343 static int modify_idle_timeout(struct helper *h, struct helper_local *hl)
5344 {
5345     uint64_t v = 0;
5346 
5347     /* Test bad value is rejected. */
5348     if (!TEST_false(SSL_set_feature_request_uint(h->c_conn,
5349             SSL_VALUE_QUIC_IDLE_TIMEOUT,
5350             (1ULL << 62))))
5351         return 0;
5352 
5353     /* Set value. */
5354     if (!TEST_true(SSL_set_feature_request_uint(h->c_conn,
5355             SSL_VALUE_QUIC_IDLE_TIMEOUT,
5356             hl->check_op->arg2)))
5357         return 0;
5358 
5359     if (!TEST_true(SSL_get_feature_request_uint(h->c_conn,
5360             SSL_VALUE_QUIC_IDLE_TIMEOUT,
5361             &v)))
5362         return 0;
5363 
5364     if (!TEST_uint64_t_eq(v, hl->check_op->arg2))
5365         return 0;
5366 
5367     return 1;
5368 }
5369 
check_idle_timeout(struct helper * h,struct helper_local * hl)5370 static int check_idle_timeout(struct helper *h, struct helper_local *hl)
5371 {
5372     uint64_t v = 0;
5373 
5374     if (!TEST_true(SSL_get_value_uint(h->c_conn, hl->check_op->arg1,
5375             SSL_VALUE_QUIC_IDLE_TIMEOUT,
5376             &v)))
5377         return 0;
5378 
5379     if (!TEST_uint64_t_eq(v, hl->check_op->arg2))
5380         return 0;
5381 
5382     return 1;
5383 }
5384 
5385 static const struct script_op script_81[] = {
5386     OP_C_SET_ALPN("ossltest")
5387         OP_CHECK(modify_idle_timeout, 25000)
5388             OP_C_CONNECT_WAIT()
5389 
5390                 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
5391 
5392                     OP_CHECK2(check_idle_timeout,
5393                         SSL_VALUE_CLASS_FEATURE_PEER_REQUEST, 30000)
5394                         OP_CHECK2(check_idle_timeout,
5395                             SSL_VALUE_CLASS_FEATURE_NEGOTIATED, 25000)
5396 
5397                             OP_END
5398 };
5399 
5400 /* 82. Negotiated default idle timeout if not configured */
5401 static const struct script_op script_82[] = {
5402     OP_C_SET_ALPN("ossltest")
5403         OP_C_CONNECT_WAIT()
5404 
5405             OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
5406 
5407                 OP_CHECK2(check_idle_timeout,
5408                     SSL_VALUE_CLASS_FEATURE_PEER_REQUEST, 30000)
5409                     OP_CHECK2(check_idle_timeout,
5410                         SSL_VALUE_CLASS_FEATURE_NEGOTIATED, 30000)
5411 
5412                         OP_END
5413 };
5414 
5415 /* 83. No late changes to idle timeout */
cannot_change_idle_timeout(struct helper * h,struct helper_local * hl)5416 static int cannot_change_idle_timeout(struct helper *h, struct helper_local *hl)
5417 {
5418     uint64_t v = 0;
5419 
5420     if (!TEST_true(SSL_get_feature_request_uint(h->c_conn,
5421             SSL_VALUE_QUIC_IDLE_TIMEOUT,
5422             &v)))
5423         return 0;
5424 
5425     if (!TEST_uint64_t_eq(v, 30000))
5426         return 0;
5427 
5428     if (!TEST_false(SSL_set_feature_request_uint(h->c_conn,
5429             SSL_VALUE_QUIC_IDLE_TIMEOUT,
5430             5000)))
5431         return 0;
5432 
5433     return 1;
5434 }
5435 
5436 static const struct script_op script_83[] = {
5437     OP_C_SET_ALPN("ossltest")
5438         OP_C_CONNECT_WAIT()
5439 
5440             OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
5441 
5442                 OP_CHECK(cannot_change_idle_timeout, 0)
5443                     OP_CHECK2(check_idle_timeout,
5444                         SSL_VALUE_CLASS_FEATURE_PEER_REQUEST, 30000)
5445                         OP_CHECK2(check_idle_timeout,
5446                             SSL_VALUE_CLASS_FEATURE_NEGOTIATED, 30000)
5447 
5448                             OP_END
5449 };
5450 
5451 /* 84. Test query of available streams */
check_avail_streams(struct helper * h,struct helper_local * hl)5452 static int check_avail_streams(struct helper *h, struct helper_local *hl)
5453 {
5454     uint64_t v = 0;
5455 
5456     switch (hl->check_op->arg1) {
5457     case 0:
5458         if (!TEST_true(SSL_get_quic_stream_bidi_local_avail(h->c_conn, &v)))
5459             return 0;
5460         break;
5461     case 1:
5462         if (!TEST_true(SSL_get_quic_stream_bidi_remote_avail(h->c_conn, &v)))
5463             return 0;
5464         break;
5465     case 2:
5466         if (!TEST_true(SSL_get_quic_stream_uni_local_avail(h->c_conn, &v)))
5467             return 0;
5468         break;
5469     case 3:
5470         if (!TEST_true(SSL_get_quic_stream_uni_remote_avail(h->c_conn, &v)))
5471             return 0;
5472         break;
5473     default:
5474         return 0;
5475     }
5476 
5477     if (!TEST_uint64_t_eq(v, hl->check_op->arg2))
5478         return 0;
5479 
5480     return 1;
5481 }
5482 
5483 static int set_event_handling_mode_conn(struct helper *h, struct helper_local *hl);
5484 static int reenable_test_event_handling(struct helper *h, struct helper_local *hl);
5485 
check_write_buf_stat(struct helper * h,struct helper_local * hl)5486 static int check_write_buf_stat(struct helper *h, struct helper_local *hl)
5487 {
5488     SSL *c_a;
5489     uint64_t size, used, avail;
5490 
5491     if (!TEST_ptr(c_a = helper_local_get_c_stream(hl, "a")))
5492         return 0;
5493 
5494     if (!TEST_true(SSL_get_stream_write_buf_size(c_a, &size))
5495         || !TEST_true(SSL_get_stream_write_buf_used(c_a, &used))
5496         || !TEST_true(SSL_get_stream_write_buf_avail(c_a, &avail))
5497         || !TEST_uint64_t_ge(size, avail)
5498         || !TEST_uint64_t_ge(size, used)
5499         || !TEST_uint64_t_eq(avail + used, size))
5500         return 0;
5501 
5502     if (!TEST_uint64_t_eq(used, hl->check_op->arg1))
5503         return 0;
5504 
5505     return 1;
5506 }
5507 
5508 static const struct script_op script_84[] = {
5509     OP_C_SET_ALPN("ossltest")
5510         OP_C_CONNECT_WAIT()
5511 
5512             OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
5513 
5514                 OP_CHECK2(check_avail_streams, 0, 100)
5515                     OP_CHECK2(check_avail_streams, 1, 100)
5516                         OP_CHECK2(check_avail_streams, 2, 100)
5517                             OP_CHECK2(check_avail_streams, 3, 100)
5518 
5519                                 OP_C_NEW_STREAM_BIDI(a, C_BIDI_ID(0))
5520 
5521                                     OP_CHECK2(check_avail_streams, 0, 99)
5522                                         OP_CHECK2(check_avail_streams, 1, 100)
5523                                             OP_CHECK2(check_avail_streams, 2, 100)
5524                                                 OP_CHECK2(check_avail_streams, 3, 100)
5525 
5526                                                     OP_C_NEW_STREAM_UNI(b, C_UNI_ID(0))
5527 
5528                                                         OP_CHECK2(check_avail_streams, 0, 99)
5529                                                             OP_CHECK2(check_avail_streams, 1, 100)
5530                                                                 OP_CHECK2(check_avail_streams, 2, 99)
5531                                                                     OP_CHECK2(check_avail_streams, 3, 100)
5532 
5533                                                                         OP_S_NEW_STREAM_BIDI(c, S_BIDI_ID(0))
5534                                                                             OP_S_WRITE(c, "x", 1)
5535 
5536                                                                                 OP_C_ACCEPT_STREAM_WAIT(c)
5537                                                                                     OP_C_READ_EXPECT(c, "x", 1)
5538 
5539                                                                                         OP_CHECK2(check_avail_streams, 0, 99)
5540                                                                                             OP_CHECK2(check_avail_streams, 1, 99)
5541                                                                                                 OP_CHECK2(check_avail_streams, 2, 99)
5542                                                                                                     OP_CHECK2(check_avail_streams, 3, 100)
5543 
5544                                                                                                         OP_S_NEW_STREAM_UNI(d, S_UNI_ID(0))
5545                                                                                                             OP_S_WRITE(d, "x", 1)
5546 
5547                                                                                                                 OP_C_ACCEPT_STREAM_WAIT(d)
5548                                                                                                                     OP_C_READ_EXPECT(d, "x", 1)
5549 
5550                                                                                                                         OP_CHECK2(check_avail_streams, 0, 99)
5551                                                                                                                             OP_CHECK2(check_avail_streams, 1, 99)
5552                                                                                                                                 OP_CHECK2(check_avail_streams, 2, 99)
5553                                                                                                                                     OP_CHECK2(check_avail_streams, 3, 99)
5554 
5555                                                                                                                                         OP_CHECK2(check_write_buf_stat, 0, 0)
5556                                                                                                                                             OP_CHECK(set_event_handling_mode_conn,
5557                                                                                                                                                 SSL_VALUE_EVENT_HANDLING_MODE_EXPLICIT)
5558                                                                                                                                                 OP_C_WRITE(a, "apple", 5)
5559                                                                                                                                                     OP_CHECK2(check_write_buf_stat, 5, 0)
5560 
5561                                                                                                                                                         OP_CHECK(reenable_test_event_handling, 0)
5562 
5563                                                                                                                                                             OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
5564                                                                                                                                                                 OP_S_READ_EXPECT(a, "apple", 5)
5565                                                                                                                                                                     OP_S_WRITE(a, "orange", 6)
5566                                                                                                                                                                         OP_C_READ_EXPECT(a, "orange", 6)
5567 
5568                                                                                                                                                                             OP_END
5569 };
5570 
5571 /* 85. Test SSL_poll (lite, non-blocking) */
script_85_poll(struct helper * h,struct helper_local * hl)5572 ossl_unused static int script_85_poll(struct helper *h, struct helper_local *hl)
5573 {
5574     int ok = 1, ret, expected_ret = 1;
5575     static const struct timeval timeout = { 0 };
5576     size_t result_count, expected_result_count = 0;
5577     SSL_POLL_ITEM items[5] = { 0 }, *item = items;
5578     SSL *c_a, *c_b, *c_c, *c_d;
5579     size_t i;
5580     uint64_t mode, expected_revents[5] = { 0 };
5581 
5582     if (!TEST_ptr(c_a = helper_local_get_c_stream(hl, "a"))
5583         || !TEST_ptr(c_b = helper_local_get_c_stream(hl, "b"))
5584         || !TEST_ptr(c_c = helper_local_get_c_stream(hl, "c"))
5585         || !TEST_ptr(c_d = helper_local_get_c_stream(hl, "d")))
5586         return 0;
5587 
5588     item->desc = SSL_as_poll_descriptor(c_a);
5589     item->events = UINT64_MAX;
5590     item->revents = UINT64_MAX;
5591     ++item;
5592 
5593     item->desc = SSL_as_poll_descriptor(c_b);
5594     item->events = UINT64_MAX;
5595     item->revents = UINT64_MAX;
5596     ++item;
5597 
5598     item->desc = SSL_as_poll_descriptor(c_c);
5599     item->events = UINT64_MAX;
5600     item->revents = UINT64_MAX;
5601     ++item;
5602 
5603     item->desc = SSL_as_poll_descriptor(c_d);
5604     item->events = UINT64_MAX;
5605     item->revents = UINT64_MAX;
5606     ++item;
5607 
5608     item->desc = SSL_as_poll_descriptor(h->c_conn);
5609     item->events = UINT64_MAX;
5610     item->revents = UINT64_MAX;
5611     ++item;
5612 
5613     result_count = SIZE_MAX;
5614     ret = SSL_poll(items, OSSL_NELEM(items), sizeof(SSL_POLL_ITEM),
5615         &timeout, 0,
5616         &result_count);
5617 
5618     mode = hl->check_op->arg2;
5619     switch (mode) {
5620     case 0:
5621         /* No incoming data yet */
5622         expected_revents[0] = SSL_POLL_EVENT_W;
5623         expected_revents[1] = SSL_POLL_EVENT_W;
5624         expected_revents[2] = SSL_POLL_EVENT_W;
5625         expected_revents[3] = SSL_POLL_EVENT_W;
5626         expected_revents[4] = SSL_POLL_EVENT_OS;
5627         expected_result_count = 5;
5628         break;
5629     case 1:
5630         /* Expect more events */
5631         expected_revents[0] = SSL_POLL_EVENT_W | SSL_POLL_EVENT_R;
5632         expected_revents[1] = SSL_POLL_EVENT_W | SSL_POLL_EVENT_ER;
5633         expected_revents[2] = SSL_POLL_EVENT_EW;
5634         expected_revents[3] = SSL_POLL_EVENT_W;
5635         expected_revents[4] = SSL_POLL_EVENT_OS | SSL_POLL_EVENT_ISB;
5636         expected_result_count = 5;
5637         break;
5638     default:
5639         return 0;
5640     }
5641 
5642     if (!TEST_int_eq(ret, expected_ret)
5643         || !TEST_size_t_eq(result_count, expected_result_count))
5644         ok = 0;
5645 
5646     for (i = 0; i < OSSL_NELEM(items); ++i)
5647         if (!TEST_uint64_t_eq(items[i].revents, expected_revents[i])) {
5648             TEST_error("mismatch at index %zu in poll results, mode %d",
5649                 i, (int)mode);
5650             ok = 0;
5651         }
5652 
5653     return ok;
5654 }
5655 
5656 static const struct script_op script_85[] = {
5657     OP_C_SET_ALPN("ossltest")
5658         OP_C_CONNECT_WAIT()
5659 
5660             OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
5661 
5662                 OP_C_NEW_STREAM_BIDI(a, C_BIDI_ID(0))
5663                     OP_C_WRITE(a, "flamingo", 8)
5664 
5665                         OP_C_NEW_STREAM_BIDI(b, C_BIDI_ID(1))
5666                             OP_C_WRITE(b, "orange", 6)
5667 
5668                                 OP_C_NEW_STREAM_BIDI(c, C_BIDI_ID(2))
5669                                     OP_C_WRITE(c, "Strawberry", 10)
5670 
5671                                         OP_C_NEW_STREAM_BIDI(d, C_BIDI_ID(3))
5672                                             OP_C_WRITE(d, "sync", 4)
5673 
5674                                                 OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
5675                                                     OP_S_BIND_STREAM_ID(b, C_BIDI_ID(1))
5676                                                         OP_S_BIND_STREAM_ID(c, C_BIDI_ID(2))
5677                                                             OP_S_BIND_STREAM_ID(d, C_BIDI_ID(3))
5678 
5679     /* Check nothing readable yet. */
5680     OP_CHECK(script_85_poll, 0)
5681 
5682     /* Send something that will make client sockets readable. */
5683     OP_S_READ_EXPECT(a, "flamingo", 8)
5684         OP_S_WRITE(a, "herringbone", 11)
5685 
5686     /* Send something that will make 'b' reset. */
5687     OP_S_SET_INJECT_PLAIN(script_28_inject_plain)
5688         OP_SET_INJECT_WORD(C_BIDI_ID(1) + 1, OSSL_QUIC_FRAME_TYPE_RESET_STREAM)
5689 
5690     /* Ensure sync. */
5691     OP_S_READ_EXPECT(d, "sync", 4)
5692         OP_S_WRITE(d, "x", 1)
5693             OP_C_READ_EXPECT(d, "x", 1)
5694 
5695     /* Send something that will make 'c' reset. */
5696     OP_S_SET_INJECT_PLAIN(script_28_inject_plain)
5697         OP_SET_INJECT_WORD(C_BIDI_ID(2) + 1, OSSL_QUIC_FRAME_TYPE_STOP_SENDING)
5698 
5699             OP_S_NEW_STREAM_BIDI(z, S_BIDI_ID(0))
5700                 OP_S_WRITE(z, "z", 1)
5701 
5702     /* Ensure sync. */
5703     OP_S_WRITE(d, "x", 1)
5704         OP_C_READ_EXPECT(d, "x", 1)
5705 
5706     /* Check a is now readable. */
5707     OP_CHECK(script_85_poll, 1)
5708 
5709         OP_END
5710 };
5711 
5712 /* 86. Event Handling Mode Configuration */
set_event_handling_mode_conn(struct helper * h,struct helper_local * hl)5713 static int set_event_handling_mode_conn(struct helper *h, struct helper_local *hl)
5714 {
5715     hl->explicit_event_handling = 1;
5716     return SSL_set_event_handling_mode(h->c_conn, hl->check_op->arg2);
5717 }
5718 
reenable_test_event_handling(struct helper * h,struct helper_local * hl)5719 static int reenable_test_event_handling(struct helper *h, struct helper_local *hl)
5720 {
5721     hl->explicit_event_handling = 0;
5722     return 1;
5723 }
5724 
set_event_handling_mode_stream(struct helper * h,struct helper_local * hl)5725 static ossl_unused int set_event_handling_mode_stream(struct helper *h, struct helper_local *hl)
5726 {
5727     SSL *ssl = helper_local_get_c_stream(hl, "a");
5728 
5729     if (!TEST_ptr(ssl))
5730         return 0;
5731 
5732     return SSL_set_event_handling_mode(ssl, hl->check_op->arg2);
5733 }
5734 
5735 static const struct script_op script_86[] = {
5736     OP_SKIP_IF_BLOCKING(23)
5737 
5738         OP_C_SET_ALPN("ossltest")
5739             OP_C_CONNECT_WAIT()
5740 
5741                 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
5742 
5743     /* Turn on explicit handling mode. */
5744     OP_CHECK(set_event_handling_mode_conn,
5745         SSL_VALUE_EVENT_HANDLING_MODE_EXPLICIT)
5746 
5747     /*
5748      * Create a new stream and write data. This won't get sent
5749      * to the network net because we are in explicit mode
5750      * and we haven't called SSL_handle_events().
5751      */
5752     OP_C_NEW_STREAM_BIDI(a, C_BIDI_ID(0))
5753         OP_C_WRITE(a, "apple", 5)
5754 
5755     /* Put connection back into implicit handling mode. */
5756     OP_CHECK(set_event_handling_mode_conn,
5757         SSL_VALUE_EVENT_HANDLING_MODE_IMPLICIT)
5758 
5759     /* Override at stream level. */
5760     OP_CHECK(set_event_handling_mode_stream,
5761         SSL_VALUE_EVENT_HANDLING_MODE_EXPLICIT)
5762         OP_C_WRITE(a, "orange", 6)
5763             OP_C_CONCLUDE(a)
5764 
5765     /*
5766      * Confirm the data isn't going to arrive. OP_SLEEP is always undesirable
5767      * but we have no reasonable way to synchronise on something not arriving
5768      * given all network traffic is essentially stopped and there are no other
5769      * signals arriving from the peer which could be used for synchronisation.
5770      * Slow OSes will pass this anyway (fail-open).
5771      */
5772     OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
5773 
5774         OP_BEGIN_REPEAT(20)
5775             OP_S_READ_FAIL(a, 1)
5776                 OP_SLEEP(10)
5777                     OP_END_REPEAT()
5778 
5779     /* Now let the data arrive and confirm it arrives. */
5780     OP_CHECK(reenable_test_event_handling, 0)
5781         OP_S_READ_EXPECT(a, "appleorange", 11)
5782             OP_S_EXPECT_FIN(a)
5783 
5784     /* Back into explicit mode. */
5785     OP_CHECK(set_event_handling_mode_conn,
5786         SSL_VALUE_EVENT_HANDLING_MODE_EXPLICIT)
5787         OP_S_WRITE(a, "ok", 2)
5788             OP_C_READ_FAIL(a)
5789 
5790     /* Works once event handling is done. */
5791     OP_CHECK(reenable_test_event_handling, 0)
5792         OP_C_READ_EXPECT(a, "ok", 2)
5793 
5794             OP_END
5795 };
5796 
5797 /* 87. Test stream reset functionality */
5798 static const struct script_op script_87[] = {
5799     OP_C_SET_ALPN("ossltest")
5800         OP_C_CONNECT_WAIT()
5801             OP_C_NEW_STREAM_BIDI(a, C_BIDI_ID(0))
5802                 OP_C_WRITE(a, "apple", 5)
5803                     OP_C_CONCLUDE(a)
5804                         OP_S_BIND_STREAM_ID(a, C_BIDI_ID(0))
5805                             OP_S_READ_EXPECT(a, "apple", 5)
5806                                 OP_S_EXPECT_FIN(a)
5807                                     OP_S_WRITE(a, "orange", 6)
5808                                         OP_C_READ_EXPECT(a, "orange", 6)
5809                                             OP_S_CONCLUDE(a)
5810                                                 OP_C_EXPECT_FIN(a)
5811                                                     OP_SLEEP(1000)
5812                                                         OP_C_STREAM_RESET_FAIL(a, 42)
5813                                                             OP_END
5814 };
5815 
5816 static const struct script_op *const scripts[] = {
5817     script_1,
5818     script_2,
5819     script_3,
5820     script_4,
5821     script_5,
5822     script_6,
5823     script_7,
5824     script_8,
5825     script_9,
5826     script_10,
5827     script_11,
5828     script_12,
5829     script_13,
5830     script_14,
5831     script_15,
5832     script_16,
5833     script_17,
5834     script_18,
5835     script_19,
5836     script_20,
5837     script_21,
5838     script_22,
5839     script_23,
5840     script_24,
5841     script_25,
5842     script_26,
5843     script_27,
5844     script_28,
5845     script_29,
5846     script_30,
5847     script_31,
5848     script_32,
5849     script_33,
5850     script_34,
5851     script_35,
5852     script_36,
5853     script_37,
5854     script_38,
5855     script_39,
5856     script_40,
5857     script_41,
5858     script_42,
5859     script_43,
5860     script_44,
5861     script_45,
5862     script_46,
5863     script_47,
5864     script_48,
5865     script_49,
5866     script_50,
5867     script_51,
5868     script_52,
5869     script_53,
5870     script_54,
5871     script_55,
5872     script_56,
5873     script_57,
5874     script_58,
5875     script_59,
5876     script_60,
5877     script_61,
5878     script_62,
5879     script_63,
5880     script_64,
5881     script_65,
5882     script_66,
5883     script_67,
5884     script_68,
5885     script_69,
5886     script_70,
5887     script_71,
5888     script_72,
5889     script_73,
5890     script_74,
5891     script_75,
5892     script_76,
5893     script_77,
5894     script_78,
5895     script_79,
5896     script_80,
5897     script_81,
5898     script_82,
5899     script_83,
5900     script_84,
5901     script_85,
5902     script_86,
5903     script_87
5904 };
5905 
test_script(int idx)5906 static int test_script(int idx)
5907 {
5908     int script_idx, free_order, blocking;
5909     char script_name[64];
5910 
5911     free_order = idx % 2;
5912     idx /= 2;
5913 
5914     blocking = idx % 2;
5915     idx /= 2;
5916 
5917     script_idx = idx;
5918 
5919     if (blocking && free_order)
5920         return 1; /* don't need to test free_order twice */
5921 
5922 #if !defined(OPENSSL_THREADS)
5923     if (blocking) {
5924         TEST_skip("cannot test in blocking mode without threads");
5925         return 1;
5926     }
5927 #endif
5928 
5929     BIO_snprintf(script_name, sizeof(script_name), "script %d", script_idx + 1);
5930 
5931     TEST_info("Running script %d (order=%d, blocking=%d)", script_idx + 1,
5932         free_order, blocking);
5933     return run_script(scripts[script_idx], script_name, free_order, blocking);
5934 }
5935 
5936 /* Dynamically generated tests. */
5937 static struct script_op dyn_frame_types_script[] = {
5938     OP_S_SET_INJECT_PLAIN(script_21_inject_plain)
5939         OP_SET_INJECT_WORD(0, 0) /* dynamic */
5940 
5941     OP_C_SET_ALPN("ossltest")
5942         OP_C_CONNECT_WAIT_OR_FAIL()
5943 
5944             OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR, 0, 0)
5945 
5946                 OP_END
5947 };
5948 
5949 struct forbidden_frame_type {
5950     uint64_t pkt_type, frame_type, expected_err;
5951 };
5952 
5953 static const struct forbidden_frame_type forbidden_frame_types[] = {
5954     { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_VLINT_MAX, OSSL_QUIC_ERR_FRAME_ENCODING_ERROR },
5955     { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_VLINT_MAX, OSSL_QUIC_ERR_FRAME_ENCODING_ERROR },
5956     { QUIC_PKT_TYPE_1RTT, OSSL_QUIC_VLINT_MAX, OSSL_QUIC_ERR_FRAME_ENCODING_ERROR },
5957 
5958     { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_STREAM, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5959     { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_RESET_STREAM, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5960     { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_STOP_SENDING, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5961     { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_NEW_TOKEN, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5962     { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_MAX_DATA, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5963     { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5964     { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5965     { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5966     { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5967     { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5968     { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5969     { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5970     { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5971     { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5972     { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5973     { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5974     { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5975     { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5976 
5977     { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_STREAM, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5978     { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_RESET_STREAM, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5979     { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_STOP_SENDING, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5980     { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_NEW_TOKEN, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5981     { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_MAX_DATA, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5982     { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5983     { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5984     { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5985     { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5986     { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5987     { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5988     { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5989     { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5990     { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5991     { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5992     { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5993     { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5994     { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5995 
5996     /* Client uses a zero-length CID so this is not allowed. */
5997     { QUIC_PKT_TYPE_1RTT, OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
5998 };
5999 
test_dyn_frame_types(int idx)6000 static ossl_unused int test_dyn_frame_types(int idx)
6001 {
6002     size_t i;
6003     char script_name[64];
6004     struct script_op *s = dyn_frame_types_script;
6005 
6006     for (i = 0; i < OSSL_NELEM(dyn_frame_types_script); ++i)
6007         if (s[i].op == OPK_SET_INJECT_WORD) {
6008             s[i].arg1 = (size_t)forbidden_frame_types[idx].pkt_type;
6009             s[i].arg2 = forbidden_frame_types[idx].frame_type;
6010         } else if (s[i].op == OPK_C_EXPECT_CONN_CLOSE_INFO) {
6011             s[i].arg2 = forbidden_frame_types[idx].expected_err;
6012         }
6013 
6014     BIO_snprintf(script_name, sizeof(script_name),
6015         "dyn script %d", idx);
6016 
6017     return run_script(dyn_frame_types_script, script_name, 0, 0);
6018 }
6019 
6020 OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
6021 
setup_tests(void)6022 int setup_tests(void)
6023 {
6024 #if defined(_PUT_MODEL_)
6025     return TEST_skip("QUIC is not supported by this build");
6026 #endif
6027 
6028     if (!test_skip_common_options()) {
6029         TEST_error("Error parsing test options\n");
6030         return 0;
6031     }
6032 
6033     if (!TEST_ptr(certfile = test_get_argument(0))
6034         || !TEST_ptr(keyfile = test_get_argument(1)))
6035         return 0;
6036 
6037     ADD_ALL_TESTS(test_dyn_frame_types, OSSL_NELEM(forbidden_frame_types));
6038     ADD_ALL_TESTS(test_script, OSSL_NELEM(scripts) * 2 * 2);
6039     return 1;
6040 }
6041