1 /*
2 * Copyright 2016-2025 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 /*
11 * We need access to the deprecated low level ENGINE APIs for legacy purposes
12 * when the deprecated calls are not hidden
13 */
14 #ifndef OPENSSL_NO_DEPRECATED_3_0
15 #define OPENSSL_SUPPRESS_DEPRECATED
16 #endif
17
18 #include <string.h>
19
20 #include <openssl/engine.h>
21 #include "internal/e_os.h"
22 #include "internal/nelem.h"
23 #include "ssltestlib.h"
24 #include "../testutil.h"
25
26 #if (!defined(OPENSSL_NO_KTLS) || !defined(OPENSSL_NO_QUIC)) && !defined(OPENSSL_NO_POSIX_IO) && !defined(OPENSSL_NO_SOCK)
27 #define OSSL_USE_SOCKETS 1
28 #include "internal/e_winsock.h"
29 #include "internal/sockets.h"
30 #include <openssl/bio.h>
31 #endif
32
33 static int tls_dump_new(BIO *bi);
34 static int tls_dump_free(BIO *a);
35 static int tls_dump_read(BIO *b, char *out, int outl);
36 static int tls_dump_write(BIO *b, const char *in, int inl);
37 static long tls_dump_ctrl(BIO *b, int cmd, long num, void *ptr);
38 static int tls_dump_gets(BIO *bp, char *buf, int size);
39 static int tls_dump_puts(BIO *bp, const char *str);
40
41 /* Choose a sufficiently large type likely to be unused for this custom BIO */
42 #define BIO_TYPE_TLS_DUMP_FILTER (0x80 | BIO_TYPE_FILTER)
43 #define BIO_TYPE_MEMPACKET_TEST 0x81
44 #define BIO_TYPE_ALWAYS_RETRY 0x82
45 #define BIO_TYPE_MAYBE_RETRY (0x83 | BIO_TYPE_FILTER)
46
47 static BIO_METHOD *method_tls_dump = NULL;
48 static BIO_METHOD *meth_mem = NULL;
49 static BIO_METHOD *meth_always_retry = NULL;
50 static BIO_METHOD *meth_maybe_retry = NULL;
51 static int retry_err = -1;
52
53 /* Note: Not thread safe! */
bio_f_tls_dump_filter(void)54 const BIO_METHOD *bio_f_tls_dump_filter(void)
55 {
56 if (method_tls_dump == NULL) {
57 method_tls_dump = BIO_meth_new(BIO_TYPE_TLS_DUMP_FILTER,
58 "TLS dump filter");
59 if (method_tls_dump == NULL
60 || !BIO_meth_set_write(method_tls_dump, tls_dump_write)
61 || !BIO_meth_set_read(method_tls_dump, tls_dump_read)
62 || !BIO_meth_set_puts(method_tls_dump, tls_dump_puts)
63 || !BIO_meth_set_gets(method_tls_dump, tls_dump_gets)
64 || !BIO_meth_set_ctrl(method_tls_dump, tls_dump_ctrl)
65 || !BIO_meth_set_create(method_tls_dump, tls_dump_new)
66 || !BIO_meth_set_destroy(method_tls_dump, tls_dump_free))
67 return NULL;
68 }
69 return method_tls_dump;
70 }
71
bio_f_tls_dump_filter_free(void)72 void bio_f_tls_dump_filter_free(void)
73 {
74 BIO_meth_free(method_tls_dump);
75 }
76
tls_dump_new(BIO * bio)77 static int tls_dump_new(BIO *bio)
78 {
79 BIO_set_init(bio, 1);
80 return 1;
81 }
82
tls_dump_free(BIO * bio)83 static int tls_dump_free(BIO *bio)
84 {
85 BIO_set_init(bio, 0);
86
87 return 1;
88 }
89
copy_flags(BIO * bio)90 static void copy_flags(BIO *bio)
91 {
92 int flags;
93 BIO *next = BIO_next(bio);
94
95 flags = BIO_test_flags(next, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS);
96 BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS);
97 BIO_set_flags(bio, flags);
98 }
99
100 #define RECORD_CONTENT_TYPE 0
101 #define RECORD_VERSION_HI 1
102 #define RECORD_VERSION_LO 2
103 #define RECORD_EPOCH_HI 3
104 #define RECORD_EPOCH_LO 4
105 #define RECORD_SEQUENCE_START 5
106 #define RECORD_SEQUENCE_END 10
107 #define RECORD_LEN_HI 11
108 #define RECORD_LEN_LO 12
109
110 #define MSG_TYPE 0
111 #define MSG_LEN_HI 1
112 #define MSG_LEN_MID 2
113 #define MSG_LEN_LO 3
114 #define MSG_SEQ_HI 4
115 #define MSG_SEQ_LO 5
116 #define MSG_FRAG_OFF_HI 6
117 #define MSG_FRAG_OFF_MID 7
118 #define MSG_FRAG_OFF_LO 8
119 #define MSG_FRAG_LEN_HI 9
120 #define MSG_FRAG_LEN_MID 10
121 #define MSG_FRAG_LEN_LO 11
122
dump_data(const char * data,int len)123 static void dump_data(const char *data, int len)
124 {
125 int rem, i, content, reclen, msglen, fragoff, fraglen, epoch;
126 unsigned char *rec;
127
128 printf("---- START OF PACKET ----\n");
129
130 rem = len;
131 rec = (unsigned char *)data;
132
133 while (rem > 0) {
134 if (rem != len)
135 printf("*\n");
136 printf("*---- START OF RECORD ----\n");
137 if (rem < DTLS1_RT_HEADER_LENGTH) {
138 printf("*---- RECORD TRUNCATED ----\n");
139 break;
140 }
141 content = rec[RECORD_CONTENT_TYPE];
142 printf("** Record Content-type: %d\n", content);
143 printf("** Record Version: %02x%02x\n",
144 rec[RECORD_VERSION_HI], rec[RECORD_VERSION_LO]);
145 epoch = (rec[RECORD_EPOCH_HI] << 8) | rec[RECORD_EPOCH_LO];
146 printf("** Record Epoch: %d\n", epoch);
147 printf("** Record Sequence: ");
148 for (i = RECORD_SEQUENCE_START; i <= RECORD_SEQUENCE_END; i++)
149 printf("%02x", rec[i]);
150 reclen = (rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO];
151 printf("\n** Record Length: %d\n", reclen);
152
153 /* Now look at message */
154 rec += DTLS1_RT_HEADER_LENGTH;
155 rem -= DTLS1_RT_HEADER_LENGTH;
156 if (content == SSL3_RT_HANDSHAKE) {
157 printf("**---- START OF HANDSHAKE MESSAGE FRAGMENT ----\n");
158 if (epoch > 0) {
159 printf("**---- HANDSHAKE MESSAGE FRAGMENT ENCRYPTED ----\n");
160 } else if (rem < DTLS1_HM_HEADER_LENGTH
161 || reclen < DTLS1_HM_HEADER_LENGTH) {
162 printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n");
163 } else {
164 printf("*** Message Type: %d\n", rec[MSG_TYPE]);
165 msglen = (rec[MSG_LEN_HI] << 16) | (rec[MSG_LEN_MID] << 8)
166 | rec[MSG_LEN_LO];
167 printf("*** Message Length: %d\n", msglen);
168 printf("*** Message sequence: %d\n",
169 (rec[MSG_SEQ_HI] << 8) | rec[MSG_SEQ_LO]);
170 fragoff = (rec[MSG_FRAG_OFF_HI] << 16)
171 | (rec[MSG_FRAG_OFF_MID] << 8)
172 | rec[MSG_FRAG_OFF_LO];
173 printf("*** Message Fragment offset: %d\n", fragoff);
174 fraglen = (rec[MSG_FRAG_LEN_HI] << 16)
175 | (rec[MSG_FRAG_LEN_MID] << 8)
176 | rec[MSG_FRAG_LEN_LO];
177 printf("*** Message Fragment len: %d\n", fraglen);
178 if (fragoff + fraglen > msglen)
179 printf("***---- HANDSHAKE MESSAGE FRAGMENT INVALID ----\n");
180 else if (reclen < fraglen)
181 printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n");
182 else
183 printf("**---- END OF HANDSHAKE MESSAGE FRAGMENT ----\n");
184 }
185 }
186 if (rem < reclen) {
187 printf("*---- RECORD TRUNCATED ----\n");
188 rem = 0;
189 } else {
190 rec += reclen;
191 rem -= reclen;
192 printf("*---- END OF RECORD ----\n");
193 }
194 }
195 printf("---- END OF PACKET ----\n\n");
196 fflush(stdout);
197 }
198
tls_dump_read(BIO * bio,char * out,int outl)199 static int tls_dump_read(BIO *bio, char *out, int outl)
200 {
201 int ret;
202 BIO *next = BIO_next(bio);
203
204 ret = BIO_read(next, out, outl);
205 copy_flags(bio);
206
207 if (ret > 0) {
208 dump_data(out, ret);
209 }
210
211 return ret;
212 }
213
tls_dump_write(BIO * bio,const char * in,int inl)214 static int tls_dump_write(BIO *bio, const char *in, int inl)
215 {
216 int ret;
217 BIO *next = BIO_next(bio);
218
219 ret = BIO_write(next, in, inl);
220 copy_flags(bio);
221
222 return ret;
223 }
224
tls_dump_ctrl(BIO * bio,int cmd,long num,void * ptr)225 static long tls_dump_ctrl(BIO *bio, int cmd, long num, void *ptr)
226 {
227 long ret;
228 BIO *next = BIO_next(bio);
229
230 if (next == NULL)
231 return 0;
232
233 switch (cmd) {
234 case BIO_CTRL_DUP:
235 ret = 0L;
236 break;
237 default:
238 ret = BIO_ctrl(next, cmd, num, ptr);
239 break;
240 }
241 return ret;
242 }
243
tls_dump_gets(BIO * bio,char * buf,int size)244 static int tls_dump_gets(BIO *bio, char *buf, int size)
245 {
246 /* We don't support this - not needed anyway */
247 return -1;
248 }
249
tls_dump_puts(BIO * bio,const char * str)250 static int tls_dump_puts(BIO *bio, const char *str)
251 {
252 return tls_dump_write(bio, str, strlen(str));
253 }
254
255 struct mempacket_st {
256 unsigned char *data;
257 int len;
258 unsigned int num;
259 unsigned int type;
260 };
261
mempacket_free(MEMPACKET * pkt)262 static void mempacket_free(MEMPACKET *pkt)
263 {
264 if (pkt->data != NULL)
265 OPENSSL_free(pkt->data);
266 OPENSSL_free(pkt);
267 }
268
269 typedef struct mempacket_test_ctx_st {
270 STACK_OF(MEMPACKET) *pkts;
271 uint16_t epoch;
272 unsigned int currrec;
273 unsigned int currpkt;
274 unsigned int lastpkt;
275 unsigned int injected;
276 unsigned int noinject;
277 unsigned int dropepoch;
278 int droprec;
279 int duprec;
280 } MEMPACKET_TEST_CTX;
281
282 static int mempacket_test_new(BIO *bi);
283 static int mempacket_test_free(BIO *a);
284 static int mempacket_test_read(BIO *b, char *out, int outl);
285 static int mempacket_test_write(BIO *b, const char *in, int inl);
286 static long mempacket_test_ctrl(BIO *b, int cmd, long num, void *ptr);
287 static int mempacket_test_gets(BIO *bp, char *buf, int size);
288 static int mempacket_test_puts(BIO *bp, const char *str);
289
bio_s_mempacket_test(void)290 const BIO_METHOD *bio_s_mempacket_test(void)
291 {
292 if (meth_mem == NULL) {
293 if (!TEST_ptr(meth_mem = BIO_meth_new(BIO_TYPE_MEMPACKET_TEST,
294 "Mem Packet Test"))
295 || !TEST_true(BIO_meth_set_write(meth_mem, mempacket_test_write))
296 || !TEST_true(BIO_meth_set_read(meth_mem, mempacket_test_read))
297 || !TEST_true(BIO_meth_set_puts(meth_mem, mempacket_test_puts))
298 || !TEST_true(BIO_meth_set_gets(meth_mem, mempacket_test_gets))
299 || !TEST_true(BIO_meth_set_ctrl(meth_mem, mempacket_test_ctrl))
300 || !TEST_true(BIO_meth_set_create(meth_mem, mempacket_test_new))
301 || !TEST_true(BIO_meth_set_destroy(meth_mem, mempacket_test_free)))
302 return NULL;
303 }
304 return meth_mem;
305 }
306
bio_s_mempacket_test_free(void)307 void bio_s_mempacket_test_free(void)
308 {
309 BIO_meth_free(meth_mem);
310 }
311
mempacket_test_new(BIO * bio)312 static int mempacket_test_new(BIO *bio)
313 {
314 MEMPACKET_TEST_CTX *ctx;
315
316 if (!TEST_ptr(ctx = OPENSSL_zalloc(sizeof(*ctx))))
317 return 0;
318 if (!TEST_ptr(ctx->pkts = sk_MEMPACKET_new_null())) {
319 OPENSSL_free(ctx);
320 return 0;
321 }
322 ctx->dropepoch = 0;
323 ctx->droprec = -1;
324 BIO_set_init(bio, 1);
325 BIO_set_data(bio, ctx);
326 return 1;
327 }
328
mempacket_test_free(BIO * bio)329 static int mempacket_test_free(BIO *bio)
330 {
331 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
332
333 sk_MEMPACKET_pop_free(ctx->pkts, mempacket_free);
334 OPENSSL_free(ctx);
335 BIO_set_data(bio, NULL);
336 BIO_set_init(bio, 0);
337 return 1;
338 }
339
340 /* Record Header values */
341 #define EPOCH_HI 3
342 #define EPOCH_LO 4
343 #define RECORD_SEQUENCE 10
344 #define RECORD_LEN_HI 11
345 #define RECORD_LEN_LO 12
346
347 #define STANDARD_PACKET 0
348
mempacket_test_read(BIO * bio,char * out,int outl)349 static int mempacket_test_read(BIO *bio, char *out, int outl)
350 {
351 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
352 MEMPACKET *thispkt;
353 unsigned char *rec;
354 int rem;
355 unsigned int seq, offset, len, epoch;
356
357 BIO_clear_retry_flags(bio);
358 if ((thispkt = sk_MEMPACKET_value(ctx->pkts, 0)) == NULL
359 || thispkt->num != ctx->currpkt) {
360 /* Probably run out of data */
361 BIO_set_retry_read(bio);
362 return -1;
363 }
364 (void)sk_MEMPACKET_shift(ctx->pkts);
365 ctx->currpkt++;
366
367 if (outl > thispkt->len)
368 outl = thispkt->len;
369
370 if (thispkt->type != INJECT_PACKET_IGNORE_REC_SEQ
371 && (ctx->injected || ctx->droprec >= 0)) {
372 /*
373 * Overwrite the record sequence number. We strictly number them in
374 * the order received. Since we are actually a reliable transport
375 * we know that there won't be any re-ordering. We overwrite to deal
376 * with any packets that have been injected
377 */
378 for (rem = thispkt->len, rec = thispkt->data; rem > 0; rem -= len) {
379 if (rem < DTLS1_RT_HEADER_LENGTH)
380 return -1;
381 epoch = (rec[EPOCH_HI] << 8) | rec[EPOCH_LO];
382 if (epoch != ctx->epoch) {
383 ctx->epoch = epoch;
384 ctx->currrec = 0;
385 }
386 seq = ctx->currrec;
387 offset = 0;
388 do {
389 rec[RECORD_SEQUENCE - offset] = seq & 0xFF;
390 seq >>= 8;
391 offset++;
392 } while (seq > 0);
393
394 len = ((rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO])
395 + DTLS1_RT_HEADER_LENGTH;
396 if (rem < (int)len)
397 return -1;
398 if (ctx->droprec == (int)ctx->currrec && ctx->dropepoch == epoch) {
399 if (rem > (int)len)
400 memmove(rec, rec + len, rem - len);
401 outl -= len;
402 ctx->droprec = -1;
403 if (outl == 0)
404 BIO_set_retry_read(bio);
405 } else {
406 rec += len;
407 }
408
409 ctx->currrec++;
410 }
411 }
412
413 memcpy(out, thispkt->data, outl);
414 mempacket_free(thispkt);
415 return outl;
416 }
417
418 /*
419 * Look for records from different epochs in the last datagram and swap them
420 * around
421 */
mempacket_swap_epoch(BIO * bio)422 int mempacket_swap_epoch(BIO *bio)
423 {
424 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
425 MEMPACKET *thispkt;
426 int rem, len, prevlen = 0, pktnum;
427 unsigned char *rec, *prevrec = NULL, *tmp;
428 unsigned int epoch;
429 int numpkts = sk_MEMPACKET_num(ctx->pkts);
430
431 if (numpkts <= 0)
432 return 0;
433
434 /*
435 * If there are multiple packets we only look in the last one. This should
436 * always be the one where any epoch change occurs.
437 */
438 thispkt = sk_MEMPACKET_value(ctx->pkts, numpkts - 1);
439 if (thispkt == NULL)
440 return 0;
441
442 for (rem = thispkt->len, rec = thispkt->data; rem > 0; rem -= len, rec += len) {
443 if (rem < DTLS1_RT_HEADER_LENGTH)
444 return 0;
445 epoch = (rec[EPOCH_HI] << 8) | rec[EPOCH_LO];
446 len = ((rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO])
447 + DTLS1_RT_HEADER_LENGTH;
448 if (rem < len)
449 return 0;
450
451 /* Assumes the epoch change does not happen on the first record */
452 if (epoch != ctx->epoch) {
453 if (prevrec == NULL)
454 return 0;
455
456 /*
457 * We found 2 records with different epochs. Take a copy of the
458 * earlier record
459 */
460 tmp = OPENSSL_malloc(prevlen);
461 if (tmp == NULL)
462 return 0;
463
464 memcpy(tmp, prevrec, prevlen);
465 /*
466 * Move everything from this record onwards, including any trailing
467 * records, and overwrite the earlier record
468 */
469 memmove(prevrec, rec, rem);
470 thispkt->len -= prevlen;
471 pktnum = thispkt->num;
472
473 /*
474 * Create a new packet for the earlier record that we took out and
475 * add it to the end of the packet list.
476 */
477 thispkt = OPENSSL_malloc(sizeof(*thispkt));
478 if (thispkt == NULL) {
479 OPENSSL_free(tmp);
480 return 0;
481 }
482 thispkt->type = INJECT_PACKET;
483 thispkt->data = tmp;
484 thispkt->len = prevlen;
485 thispkt->num = pktnum + 1;
486 if (sk_MEMPACKET_insert(ctx->pkts, thispkt, numpkts) <= 0) {
487 OPENSSL_free(tmp);
488 OPENSSL_free(thispkt);
489 return 0;
490 }
491
492 return 1;
493 }
494 prevrec = rec;
495 prevlen = len;
496 }
497
498 return 0;
499 }
500
501 /* Move packet from position s to position d in the list (d < s) */
mempacket_move_packet(BIO * bio,int d,int s)502 int mempacket_move_packet(BIO *bio, int d, int s)
503 {
504 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
505 MEMPACKET *thispkt;
506 int numpkts = sk_MEMPACKET_num(ctx->pkts);
507 int i;
508
509 if (d >= s)
510 return 0;
511
512 /* We need at least s + 1 packets to be able to swap them */
513 if (numpkts <= s)
514 return 0;
515
516 /* Get the packet at position s */
517 thispkt = sk_MEMPACKET_value(ctx->pkts, s);
518 if (thispkt == NULL)
519 return 0;
520
521 /* Remove and re-add it */
522 if (sk_MEMPACKET_delete(ctx->pkts, s) != thispkt)
523 return 0;
524
525 thispkt->num -= (s - d);
526 if (sk_MEMPACKET_insert(ctx->pkts, thispkt, d) <= 0)
527 return 0;
528
529 /* Increment the packet numbers for moved packets */
530 for (i = d + 1; i <= s; i++) {
531 thispkt = sk_MEMPACKET_value(ctx->pkts, i);
532 thispkt->num++;
533 }
534 return 1;
535 }
536
mempacket_dup_last_packet(BIO * bio)537 int mempacket_dup_last_packet(BIO *bio)
538 {
539 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
540 MEMPACKET *thispkt, *duppkt;
541 int numpkts = sk_MEMPACKET_num(ctx->pkts);
542
543 /* We can only duplicate a packet if there is at least 1 pending */
544 if (numpkts <= 0)
545 return 0;
546
547 /* Get the last packet */
548 thispkt = sk_MEMPACKET_value(ctx->pkts, numpkts - 1);
549 if (thispkt == NULL)
550 return 0;
551
552 duppkt = OPENSSL_malloc(sizeof(*duppkt));
553 if (duppkt == NULL)
554 return 0;
555
556 *duppkt = *thispkt;
557 duppkt->data = OPENSSL_memdup(thispkt->data, thispkt->len);
558 if (duppkt->data == NULL) {
559 mempacket_free(duppkt);
560 return 0;
561 }
562 duppkt->num++;
563 if (sk_MEMPACKET_insert(ctx->pkts, duppkt, numpkts) <= 0) {
564 mempacket_free(duppkt);
565 return 0;
566 }
567
568 return 1;
569 }
570
mempacket_test_inject(BIO * bio,const char * in,int inl,int pktnum,int type)571 int mempacket_test_inject(BIO *bio, const char *in, int inl, int pktnum,
572 int type)
573 {
574 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
575 MEMPACKET *thispkt = NULL, *looppkt, *nextpkt, *allpkts[3];
576 int i, duprec;
577 const unsigned char *inu = (const unsigned char *)in;
578 size_t len = ((inu[RECORD_LEN_HI] << 8) | inu[RECORD_LEN_LO])
579 + DTLS1_RT_HEADER_LENGTH;
580
581 if (ctx == NULL)
582 return -1;
583
584 if ((size_t)inl < len)
585 return -1;
586
587 if ((size_t)inl == len)
588 duprec = 0;
589 else
590 duprec = ctx->duprec > 0;
591
592 /* We don't support arbitrary injection when duplicating records */
593 if (duprec && pktnum != -1)
594 return -1;
595
596 /* We only allow injection before we've started writing any data */
597 if (pktnum >= 0) {
598 if (ctx->noinject)
599 return -1;
600 ctx->injected = 1;
601 } else {
602 ctx->noinject = 1;
603 }
604
605 for (i = 0; i < (duprec ? 3 : 1); i++) {
606 if (!TEST_ptr(allpkts[i] = OPENSSL_malloc(sizeof(*thispkt))))
607 goto err;
608 thispkt = allpkts[i];
609
610 if (!TEST_ptr(thispkt->data = OPENSSL_malloc(inl)))
611 goto err;
612 /*
613 * If we are duplicating the packet, we duplicate it three times. The
614 * first two times we drop the first record if there are more than one.
615 * In this way we know that libssl will not be able to make progress
616 * until it receives the last packet, and hence will be forced to
617 * buffer these records.
618 */
619 if (duprec && i != 2) {
620 memcpy(thispkt->data, in + len, inl - len);
621 thispkt->len = inl - len;
622 } else {
623 memcpy(thispkt->data, in, inl);
624 thispkt->len = inl;
625 }
626 thispkt->num = (pktnum >= 0) ? (unsigned int)pktnum : ctx->lastpkt + i;
627 thispkt->type = type;
628 }
629
630 for (i = 0; i < sk_MEMPACKET_num(ctx->pkts); i++) {
631 if (!TEST_ptr(looppkt = sk_MEMPACKET_value(ctx->pkts, i)))
632 goto err;
633 /* Check if we found the right place to insert this packet */
634 if (looppkt->num > thispkt->num) {
635 if (sk_MEMPACKET_insert(ctx->pkts, thispkt, i) == 0)
636 goto err;
637 /* If we're doing up front injection then we're done */
638 if (pktnum >= 0)
639 return inl;
640 /*
641 * We need to do some accounting on lastpkt. We increment it first,
642 * but it might now equal the value of injected packets, so we need
643 * to skip over those
644 */
645 ctx->lastpkt++;
646 do {
647 i++;
648 nextpkt = sk_MEMPACKET_value(ctx->pkts, i);
649 if (nextpkt != NULL && nextpkt->num == ctx->lastpkt)
650 ctx->lastpkt++;
651 else
652 return inl;
653 } while (1);
654 } else if (looppkt->num == thispkt->num) {
655 if (!ctx->noinject) {
656 /* We injected two packets with the same packet number! */
657 goto err;
658 }
659 ctx->lastpkt++;
660 thispkt->num++;
661 }
662 }
663 /*
664 * We didn't find any packets with a packet number equal to or greater than
665 * this one, so we just add it onto the end
666 */
667 for (i = 0; i < (duprec ? 3 : 1); i++) {
668 thispkt = allpkts[i];
669 if (!sk_MEMPACKET_push(ctx->pkts, thispkt))
670 goto err;
671
672 if (pktnum < 0)
673 ctx->lastpkt++;
674 }
675
676 return inl;
677
678 err:
679 for (i = 0; i < (ctx->duprec > 0 ? 3 : 1); i++)
680 mempacket_free(allpkts[i]);
681 return -1;
682 }
683
mempacket_test_write(BIO * bio,const char * in,int inl)684 static int mempacket_test_write(BIO *bio, const char *in, int inl)
685 {
686 return mempacket_test_inject(bio, in, inl, -1, STANDARD_PACKET);
687 }
688
mempacket_test_ctrl(BIO * bio,int cmd,long num,void * ptr)689 static long mempacket_test_ctrl(BIO *bio, int cmd, long num, void *ptr)
690 {
691 long ret = 1;
692 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
693 MEMPACKET *thispkt;
694
695 switch (cmd) {
696 case BIO_CTRL_EOF:
697 ret = (long)(sk_MEMPACKET_num(ctx->pkts) == 0);
698 break;
699 case BIO_CTRL_GET_CLOSE:
700 ret = BIO_get_shutdown(bio);
701 break;
702 case BIO_CTRL_SET_CLOSE:
703 BIO_set_shutdown(bio, (int)num);
704 break;
705 case BIO_CTRL_WPENDING:
706 ret = 0L;
707 break;
708 case BIO_CTRL_PENDING:
709 thispkt = sk_MEMPACKET_value(ctx->pkts, 0);
710 if (thispkt == NULL)
711 ret = 0;
712 else
713 ret = thispkt->len;
714 break;
715 case BIO_CTRL_FLUSH:
716 ret = 1;
717 break;
718 case MEMPACKET_CTRL_SET_DROP_EPOCH:
719 ctx->dropepoch = (unsigned int)num;
720 break;
721 case MEMPACKET_CTRL_SET_DROP_REC:
722 ctx->droprec = (int)num;
723 break;
724 case MEMPACKET_CTRL_GET_DROP_REC:
725 ret = ctx->droprec;
726 break;
727 case MEMPACKET_CTRL_SET_DUPLICATE_REC:
728 ctx->duprec = (int)num;
729 break;
730 case BIO_CTRL_RESET:
731 case BIO_CTRL_DUP:
732 case BIO_CTRL_PUSH:
733 case BIO_CTRL_POP:
734 default:
735 ret = 0;
736 break;
737 }
738 return ret;
739 }
740
mempacket_test_gets(BIO * bio,char * buf,int size)741 static int mempacket_test_gets(BIO *bio, char *buf, int size)
742 {
743 /* We don't support this - not needed anyway */
744 return -1;
745 }
746
mempacket_test_puts(BIO * bio,const char * str)747 static int mempacket_test_puts(BIO *bio, const char *str)
748 {
749 return mempacket_test_write(bio, str, strlen(str));
750 }
751
752 static int always_retry_new(BIO *bi);
753 static int always_retry_free(BIO *a);
754 static int always_retry_read(BIO *b, char *out, int outl);
755 static int always_retry_write(BIO *b, const char *in, int inl);
756 static long always_retry_ctrl(BIO *b, int cmd, long num, void *ptr);
757 static int always_retry_gets(BIO *bp, char *buf, int size);
758 static int always_retry_puts(BIO *bp, const char *str);
759
bio_s_always_retry(void)760 const BIO_METHOD *bio_s_always_retry(void)
761 {
762 if (meth_always_retry == NULL) {
763 if (!TEST_ptr(meth_always_retry = BIO_meth_new(BIO_TYPE_ALWAYS_RETRY,
764 "Always Retry"))
765 || !TEST_true(BIO_meth_set_write(meth_always_retry,
766 always_retry_write))
767 || !TEST_true(BIO_meth_set_read(meth_always_retry,
768 always_retry_read))
769 || !TEST_true(BIO_meth_set_puts(meth_always_retry,
770 always_retry_puts))
771 || !TEST_true(BIO_meth_set_gets(meth_always_retry,
772 always_retry_gets))
773 || !TEST_true(BIO_meth_set_ctrl(meth_always_retry,
774 always_retry_ctrl))
775 || !TEST_true(BIO_meth_set_create(meth_always_retry,
776 always_retry_new))
777 || !TEST_true(BIO_meth_set_destroy(meth_always_retry,
778 always_retry_free)))
779 return NULL;
780 }
781 return meth_always_retry;
782 }
783
bio_s_always_retry_free(void)784 void bio_s_always_retry_free(void)
785 {
786 BIO_meth_free(meth_always_retry);
787 }
788
always_retry_new(BIO * bio)789 static int always_retry_new(BIO *bio)
790 {
791 BIO_set_init(bio, 1);
792 return 1;
793 }
794
always_retry_free(BIO * bio)795 static int always_retry_free(BIO *bio)
796 {
797 BIO_set_data(bio, NULL);
798 BIO_set_init(bio, 0);
799 return 1;
800 }
801
set_always_retry_err_val(int err)802 void set_always_retry_err_val(int err)
803 {
804 retry_err = err;
805 }
806
always_retry_read(BIO * bio,char * out,int outl)807 static int always_retry_read(BIO *bio, char *out, int outl)
808 {
809 BIO_set_retry_read(bio);
810 return retry_err;
811 }
812
always_retry_write(BIO * bio,const char * in,int inl)813 static int always_retry_write(BIO *bio, const char *in, int inl)
814 {
815 BIO_set_retry_write(bio);
816 return retry_err;
817 }
818
always_retry_ctrl(BIO * bio,int cmd,long num,void * ptr)819 static long always_retry_ctrl(BIO *bio, int cmd, long num, void *ptr)
820 {
821 long ret = 1;
822
823 switch (cmd) {
824 case BIO_CTRL_FLUSH:
825 BIO_set_retry_write(bio);
826 /* fall through */
827 case BIO_CTRL_EOF:
828 case BIO_CTRL_RESET:
829 case BIO_CTRL_DUP:
830 case BIO_CTRL_PUSH:
831 case BIO_CTRL_POP:
832 default:
833 ret = 0;
834 break;
835 }
836 return ret;
837 }
838
always_retry_gets(BIO * bio,char * buf,int size)839 static int always_retry_gets(BIO *bio, char *buf, int size)
840 {
841 BIO_set_retry_read(bio);
842 return retry_err;
843 }
844
always_retry_puts(BIO * bio,const char * str)845 static int always_retry_puts(BIO *bio, const char *str)
846 {
847 BIO_set_retry_write(bio);
848 return retry_err;
849 }
850
851 struct maybe_retry_data_st {
852 unsigned int retrycnt;
853 };
854
855 static int maybe_retry_new(BIO *bi);
856 static int maybe_retry_free(BIO *a);
857 static int maybe_retry_write(BIO *b, const char *in, int inl);
858 static long maybe_retry_ctrl(BIO *b, int cmd, long num, void *ptr);
859
bio_s_maybe_retry(void)860 const BIO_METHOD *bio_s_maybe_retry(void)
861 {
862 if (meth_maybe_retry == NULL) {
863 if (!TEST_ptr(meth_maybe_retry = BIO_meth_new(BIO_TYPE_MAYBE_RETRY,
864 "Maybe Retry"))
865 || !TEST_true(BIO_meth_set_write(meth_maybe_retry,
866 maybe_retry_write))
867 || !TEST_true(BIO_meth_set_ctrl(meth_maybe_retry,
868 maybe_retry_ctrl))
869 || !TEST_true(BIO_meth_set_create(meth_maybe_retry,
870 maybe_retry_new))
871 || !TEST_true(BIO_meth_set_destroy(meth_maybe_retry,
872 maybe_retry_free)))
873 return NULL;
874 }
875 return meth_maybe_retry;
876 }
877
bio_s_maybe_retry_free(void)878 void bio_s_maybe_retry_free(void)
879 {
880 BIO_meth_free(meth_maybe_retry);
881 }
882
maybe_retry_new(BIO * bio)883 static int maybe_retry_new(BIO *bio)
884 {
885 struct maybe_retry_data_st *data = OPENSSL_zalloc(sizeof(*data));
886
887 if (data == NULL)
888 return 0;
889
890 BIO_set_data(bio, data);
891 BIO_set_init(bio, 1);
892 return 1;
893 }
894
maybe_retry_free(BIO * bio)895 static int maybe_retry_free(BIO *bio)
896 {
897 struct maybe_retry_data_st *data = BIO_get_data(bio);
898
899 OPENSSL_free(data);
900 BIO_set_data(bio, NULL);
901 BIO_set_init(bio, 0);
902 return 1;
903 }
904
maybe_retry_write(BIO * bio,const char * in,int inl)905 static int maybe_retry_write(BIO *bio, const char *in, int inl)
906 {
907 struct maybe_retry_data_st *data = BIO_get_data(bio);
908
909 if (data == NULL)
910 return -1;
911
912 if (data->retrycnt == 0) {
913 BIO_set_retry_write(bio);
914 return -1;
915 }
916 data->retrycnt--;
917
918 return BIO_write(BIO_next(bio), in, inl);
919 }
920
maybe_retry_ctrl(BIO * bio,int cmd,long num,void * ptr)921 static long maybe_retry_ctrl(BIO *bio, int cmd, long num, void *ptr)
922 {
923 struct maybe_retry_data_st *data = BIO_get_data(bio);
924
925 if (data == NULL)
926 return 0;
927
928 switch (cmd) {
929 case MAYBE_RETRY_CTRL_SET_RETRY_AFTER_CNT:
930 data->retrycnt = num;
931 return 1;
932
933 case BIO_CTRL_FLUSH:
934 if (data->retrycnt == 0) {
935 BIO_set_retry_write(bio);
936 return -1;
937 }
938 data->retrycnt--;
939 /* fall through */
940 default:
941 return BIO_ctrl(BIO_next(bio), cmd, num, ptr);
942 }
943 }
944
create_ssl_ctx_pair(OSSL_LIB_CTX * libctx,const SSL_METHOD * sm,const SSL_METHOD * cm,int min_proto_version,int max_proto_version,SSL_CTX ** sctx,SSL_CTX ** cctx,char * certfile,char * privkeyfile)945 int create_ssl_ctx_pair(OSSL_LIB_CTX *libctx, const SSL_METHOD *sm,
946 const SSL_METHOD *cm, int min_proto_version,
947 int max_proto_version, SSL_CTX **sctx, SSL_CTX **cctx,
948 char *certfile, char *privkeyfile)
949 {
950 SSL_CTX *serverctx = NULL;
951 SSL_CTX *clientctx = NULL;
952
953 if (sctx != NULL) {
954 if (*sctx != NULL)
955 serverctx = *sctx;
956 else if (!TEST_ptr(serverctx = SSL_CTX_new_ex(libctx, NULL, sm))
957 || !TEST_true(SSL_CTX_set_options(serverctx,
958 SSL_OP_ALLOW_CLIENT_RENEGOTIATION)))
959 goto err;
960 }
961
962 if (cctx != NULL) {
963 if (*cctx != NULL)
964 clientctx = *cctx;
965 else if (!TEST_ptr(clientctx = SSL_CTX_new_ex(libctx, NULL, cm)))
966 goto err;
967 }
968
969 #if !defined(OPENSSL_NO_TLS1_3) \
970 && defined(OPENSSL_NO_EC) \
971 && defined(OPENSSL_NO_DH)
972 /*
973 * There are no usable built-in TLSv1.3 groups if ec and dh are both
974 * disabled
975 */
976 if (max_proto_version == 0
977 && (sm == TLS_server_method() || cm == TLS_client_method()))
978 max_proto_version = TLS1_2_VERSION;
979 #endif
980
981 if (serverctx != NULL
982 && ((min_proto_version > 0
983 && !TEST_true(SSL_CTX_set_min_proto_version(serverctx,
984 min_proto_version)))
985 || (max_proto_version > 0
986 && !TEST_true(SSL_CTX_set_max_proto_version(serverctx,
987 max_proto_version)))))
988 goto err;
989 if (clientctx != NULL
990 && ((min_proto_version > 0
991 && !TEST_true(SSL_CTX_set_min_proto_version(clientctx,
992 min_proto_version)))
993 || (max_proto_version > 0
994 && !TEST_true(SSL_CTX_set_max_proto_version(clientctx,
995 max_proto_version)))))
996 goto err;
997
998 if (serverctx != NULL && certfile != NULL && privkeyfile != NULL) {
999 if (!TEST_int_eq(SSL_CTX_use_certificate_file(serverctx, certfile,
1000 SSL_FILETYPE_PEM),
1001 1)
1002 || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(serverctx,
1003 privkeyfile,
1004 SSL_FILETYPE_PEM),
1005 1)
1006 || !TEST_int_eq(SSL_CTX_check_private_key(serverctx), 1))
1007 goto err;
1008 }
1009
1010 if (sctx != NULL)
1011 *sctx = serverctx;
1012 if (cctx != NULL)
1013 *cctx = clientctx;
1014 return 1;
1015
1016 err:
1017 if (sctx != NULL && *sctx == NULL)
1018 SSL_CTX_free(serverctx);
1019 if (cctx != NULL && *cctx == NULL)
1020 SSL_CTX_free(clientctx);
1021 return 0;
1022 }
1023
1024 #define MAXLOOPS 1000000
1025
1026 #if defined(OSSL_USE_SOCKETS)
1027
wait_until_sock_readable(int sock)1028 int wait_until_sock_readable(int sock)
1029 {
1030 fd_set readfds;
1031 struct timeval timeout;
1032 int width;
1033
1034 width = sock + 1;
1035 FD_ZERO(&readfds);
1036 openssl_fdset(sock, &readfds);
1037 timeout.tv_sec = 10; /* give up after 10 seconds */
1038 timeout.tv_usec = 0;
1039
1040 select(width, &readfds, NULL, NULL, &timeout);
1041
1042 return FD_ISSET(sock, &readfds);
1043 }
1044
create_test_sockets(int * cfdp,int * sfdp,int socktype,BIO_ADDR * saddr)1045 int create_test_sockets(int *cfdp, int *sfdp, int socktype, BIO_ADDR *saddr)
1046 {
1047 struct sockaddr_in sin;
1048 const char *host = "127.0.0.1";
1049 int cfd_connected = 0, ret = 0;
1050 socklen_t slen = sizeof(sin);
1051 int afd = -1, cfd = -1, sfd = -1;
1052
1053 memset((char *)&sin, 0, sizeof(sin));
1054 sin.sin_family = AF_INET;
1055 sin.sin_addr.s_addr = inet_addr(host);
1056
1057 afd = BIO_socket(AF_INET, socktype,
1058 socktype == SOCK_STREAM ? IPPROTO_TCP : IPPROTO_UDP, 0);
1059 if (afd == INVALID_SOCKET)
1060 return 0;
1061
1062 if (bind(afd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
1063 goto out;
1064
1065 if (getsockname(afd, (struct sockaddr *)&sin, &slen) < 0)
1066 goto out;
1067
1068 if (saddr != NULL
1069 && !BIO_ADDR_rawmake(saddr, sin.sin_family, &sin.sin_addr,
1070 sizeof(sin.sin_addr), sin.sin_port))
1071 goto out;
1072
1073 if (socktype == SOCK_STREAM && listen(afd, 1) < 0)
1074 goto out;
1075
1076 cfd = BIO_socket(AF_INET, socktype,
1077 socktype == SOCK_STREAM ? IPPROTO_TCP : IPPROTO_UDP, 0);
1078 if (cfd == INVALID_SOCKET)
1079 goto out;
1080
1081 if (!BIO_socket_nbio(afd, 1))
1082 goto out;
1083
1084 /*
1085 * If a DGRAM socket then we don't call "accept" or "connect" - so act like
1086 * we already called them.
1087 */
1088 if (socktype == SOCK_DGRAM) {
1089 cfd_connected = 1;
1090 sfd = afd;
1091 afd = -1;
1092 }
1093
1094 while (sfd == -1 || !cfd_connected) {
1095 sfd = accept(afd, NULL, 0);
1096 if (sfd == -1 && errno != EAGAIN)
1097 goto out;
1098
1099 if (!cfd_connected && connect(cfd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
1100 goto out;
1101 else
1102 cfd_connected = 1;
1103 }
1104
1105 if (!BIO_socket_nbio(cfd, 1) || !BIO_socket_nbio(sfd, 1))
1106 goto out;
1107 ret = 1;
1108 *cfdp = cfd;
1109 *sfdp = sfd;
1110 goto success;
1111
1112 out:
1113 if (cfd != -1)
1114 close(cfd);
1115 if (sfd != -1)
1116 close(sfd);
1117 success:
1118 if (afd != -1)
1119 close(afd);
1120 return ret;
1121 }
1122
create_ssl_objects2(SSL_CTX * serverctx,SSL_CTX * clientctx,SSL ** sssl,SSL ** cssl,int sfd,int cfd)1123 int create_ssl_objects2(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl,
1124 SSL **cssl, int sfd, int cfd)
1125 {
1126 SSL *serverssl = NULL, *clientssl = NULL;
1127 BIO *s_to_c_bio = NULL, *c_to_s_bio = NULL;
1128 BIO_POLL_DESCRIPTOR rdesc = { 0 }, wdesc = { 0 };
1129
1130 if (*sssl != NULL)
1131 serverssl = *sssl;
1132 else if (!TEST_ptr(serverssl = SSL_new(serverctx)))
1133 goto error;
1134 if (*cssl != NULL)
1135 clientssl = *cssl;
1136 else if (!TEST_ptr(clientssl = SSL_new(clientctx)))
1137 goto error;
1138
1139 if (!TEST_ptr(s_to_c_bio = BIO_new_socket(sfd, BIO_NOCLOSE))
1140 || !TEST_ptr(c_to_s_bio = BIO_new_socket(cfd, BIO_NOCLOSE)))
1141 goto error;
1142
1143 if (!TEST_false(SSL_get_rpoll_descriptor(clientssl, &rdesc)
1144 || !TEST_false(SSL_get_wpoll_descriptor(clientssl, &wdesc))))
1145 goto error;
1146
1147 SSL_set_bio(clientssl, c_to_s_bio, c_to_s_bio);
1148 SSL_set_bio(serverssl, s_to_c_bio, s_to_c_bio);
1149
1150 if (!TEST_true(SSL_get_rpoll_descriptor(clientssl, &rdesc))
1151 || !TEST_true(SSL_get_wpoll_descriptor(clientssl, &wdesc))
1152 || !TEST_int_eq(rdesc.type, BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD)
1153 || !TEST_int_eq(wdesc.type, BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD)
1154 || !TEST_int_eq(rdesc.value.fd, cfd)
1155 || !TEST_int_eq(wdesc.value.fd, cfd))
1156 goto error;
1157
1158 if (!TEST_true(SSL_get_rpoll_descriptor(serverssl, &rdesc))
1159 || !TEST_true(SSL_get_wpoll_descriptor(serverssl, &wdesc))
1160 || !TEST_int_eq(rdesc.type, BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD)
1161 || !TEST_int_eq(wdesc.type, BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD)
1162 || !TEST_int_eq(rdesc.value.fd, sfd)
1163 || !TEST_int_eq(wdesc.value.fd, sfd))
1164 goto error;
1165
1166 *sssl = serverssl;
1167 *cssl = clientssl;
1168 return 1;
1169
1170 error:
1171 SSL_free(serverssl);
1172 SSL_free(clientssl);
1173 BIO_free(s_to_c_bio);
1174 BIO_free(c_to_s_bio);
1175 return 0;
1176 }
1177
1178 #else
1179
wait_until_sock_readable(int sock)1180 int wait_until_sock_readable(int sock)
1181 {
1182 return 0;
1183 }
1184
1185 #endif /* defined(OSSL_USE_SOCKETS) */
1186
1187 /*
1188 * NOTE: Transfers control of the BIOs - this function will free them on error
1189 */
create_ssl_objects(SSL_CTX * serverctx,SSL_CTX * clientctx,SSL ** sssl,SSL ** cssl,BIO * s_to_c_fbio,BIO * c_to_s_fbio)1190 int create_ssl_objects(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl,
1191 SSL **cssl, BIO *s_to_c_fbio, BIO *c_to_s_fbio)
1192 {
1193 SSL *serverssl = NULL, *clientssl = NULL;
1194 BIO *s_to_c_bio = NULL, *c_to_s_bio = NULL;
1195
1196 if (*sssl != NULL)
1197 serverssl = *sssl;
1198 else if (!TEST_ptr(serverssl = SSL_new(serverctx)))
1199 goto error;
1200 if (*cssl != NULL)
1201 clientssl = *cssl;
1202 else if (!TEST_ptr(clientssl = SSL_new(clientctx)))
1203 goto error;
1204
1205 if (SSL_is_dtls(clientssl)) {
1206 if (!TEST_ptr(s_to_c_bio = BIO_new(bio_s_mempacket_test()))
1207 || !TEST_ptr(c_to_s_bio = BIO_new(bio_s_mempacket_test())))
1208 goto error;
1209 } else {
1210 if (!TEST_ptr(s_to_c_bio = BIO_new(BIO_s_mem()))
1211 || !TEST_ptr(c_to_s_bio = BIO_new(BIO_s_mem())))
1212 goto error;
1213 }
1214
1215 if (s_to_c_fbio != NULL
1216 && !TEST_ptr(s_to_c_bio = BIO_push(s_to_c_fbio, s_to_c_bio)))
1217 goto error;
1218 if (c_to_s_fbio != NULL
1219 && !TEST_ptr(c_to_s_bio = BIO_push(c_to_s_fbio, c_to_s_bio)))
1220 goto error;
1221
1222 /* Set Non-blocking IO behaviour */
1223 BIO_set_mem_eof_return(s_to_c_bio, -1);
1224 BIO_set_mem_eof_return(c_to_s_bio, -1);
1225
1226 /* Up ref these as we are passing them to two SSL objects */
1227 if (!BIO_up_ref(s_to_c_bio))
1228 goto error;
1229 if (!BIO_up_ref(c_to_s_bio)) {
1230 BIO_free(s_to_c_bio);
1231 goto error;
1232 }
1233
1234 SSL_set_bio(serverssl, c_to_s_bio, s_to_c_bio);
1235 SSL_set_bio(clientssl, s_to_c_bio, c_to_s_bio);
1236 *sssl = serverssl;
1237 *cssl = clientssl;
1238 return 1;
1239
1240 error:
1241 SSL_free(serverssl);
1242 SSL_free(clientssl);
1243 BIO_free(s_to_c_bio);
1244 BIO_free(c_to_s_bio);
1245 BIO_free(s_to_c_fbio);
1246 BIO_free(c_to_s_fbio);
1247
1248 return 0;
1249 }
1250
1251 /*
1252 * Create an SSL connection, but does not read any post-handshake
1253 * NewSessionTicket messages.
1254 * If |read| is set and we're using DTLS then we will attempt to SSL_read on
1255 * the connection once we've completed one half of it, to ensure any retransmits
1256 * get triggered.
1257 * We stop the connection attempt (and return a failure value) if either peer
1258 * has SSL_get_error() return the value in the |want| parameter. The connection
1259 * attempt could be restarted by a subsequent call to this function.
1260 */
create_bare_ssl_connection_ex(SSL * serverssl,SSL * clientssl,int want,int read,int listen,int * cm_count,int * sm_count)1261 int create_bare_ssl_connection_ex(SSL *serverssl, SSL *clientssl, int want,
1262 int read, int listen, int *cm_count, int *sm_count)
1263 {
1264 int retc = -1, rets = -1, err, abortctr = 0, ret = 0;
1265 int clienterr = 0, servererr = 0;
1266 int isdtls = SSL_is_dtls(serverssl);
1267 int icm_count = 0, ism_count = 0;
1268 #ifndef OPENSSL_NO_SOCK
1269 BIO_ADDR *peer = NULL;
1270
1271 if (listen) {
1272 if (!isdtls) {
1273 TEST_error("DTLSv1_listen requested for non-DTLS object\n");
1274 return 0;
1275 }
1276 peer = BIO_ADDR_new();
1277 if (!TEST_ptr(peer))
1278 return 0;
1279 }
1280 #else
1281 if (listen) {
1282 TEST_error("DTLSv1_listen requested in a no-sock build\n");
1283 return 0;
1284 }
1285 #endif
1286
1287 do {
1288 err = SSL_ERROR_WANT_WRITE;
1289 while (!clienterr && retc <= 0 && err == SSL_ERROR_WANT_WRITE) {
1290 retc = SSL_connect(clientssl);
1291 if (retc <= 0)
1292 err = SSL_get_error(clientssl, retc);
1293 icm_count++;
1294 }
1295
1296 if (!clienterr && retc <= 0 && err != SSL_ERROR_WANT_READ) {
1297 TEST_info("SSL_connect() failed %d, %d", retc, err);
1298 if (want != SSL_ERROR_SSL)
1299 TEST_openssl_errors();
1300 clienterr = 1;
1301 }
1302 if (want != SSL_ERROR_NONE && err == want)
1303 goto err;
1304
1305 err = SSL_ERROR_WANT_WRITE;
1306 while (!servererr && rets <= 0 && err == SSL_ERROR_WANT_WRITE) {
1307 #ifndef OPENSSL_NO_SOCK
1308 if (listen) {
1309 rets = DTLSv1_listen(serverssl, peer);
1310 if (rets < 0) {
1311 err = SSL_ERROR_SSL;
1312 } else if (rets == 0) {
1313 err = SSL_ERROR_WANT_READ;
1314 } else {
1315 /* Success - stop listening and call SSL_accept from now on */
1316 listen = 0;
1317 rets = 0;
1318 }
1319 ism_count++;
1320 } else
1321 #endif
1322 {
1323 rets = SSL_accept(serverssl);
1324 if (rets <= 0)
1325 err = SSL_get_error(serverssl, rets);
1326 ism_count++;
1327 }
1328 }
1329
1330 if (!servererr && rets <= 0
1331 && err != SSL_ERROR_WANT_READ
1332 && err != SSL_ERROR_WANT_X509_LOOKUP) {
1333 TEST_info("SSL_accept() failed %d, %d", rets, err);
1334 if (want != SSL_ERROR_SSL)
1335 TEST_openssl_errors();
1336 servererr = 1;
1337 }
1338 if (want != SSL_ERROR_NONE && err == want)
1339 goto err;
1340 if (clienterr && servererr)
1341 goto err;
1342 if (isdtls && read) {
1343 unsigned char buf[20];
1344
1345 /* Trigger any retransmits that may be appropriate */
1346 if (rets > 0 && retc <= 0) {
1347 if (SSL_read(serverssl, buf, sizeof(buf)) > 0) {
1348 /* We don't expect this to succeed! */
1349 TEST_info("Unexpected SSL_read() success!");
1350 goto err;
1351 }
1352 ism_count++;
1353 }
1354 if (retc > 0 && rets <= 0) {
1355 if (SSL_read(clientssl, buf, sizeof(buf)) > 0) {
1356 /* We don't expect this to succeed! */
1357 TEST_info("Unexpected SSL_read() success!");
1358 goto err;
1359 }
1360 icm_count++;
1361 }
1362 }
1363 if (++abortctr == MAXLOOPS) {
1364 TEST_info("No progress made");
1365 goto err;
1366 }
1367 if (isdtls && abortctr <= 50 && (abortctr % 10) == 0) {
1368 /*
1369 * It looks like we're just spinning. Pause for a short period to
1370 * give the DTLS timer a chance to do something. We only do this for
1371 * the first few times to prevent hangs.
1372 */
1373 OSSL_sleep(50);
1374 }
1375 } while (retc <= 0 || rets <= 0);
1376
1377 ret = 1;
1378 err:
1379 if (cm_count != NULL)
1380 *cm_count = icm_count;
1381 if (sm_count != NULL)
1382 *sm_count = ism_count;
1383 #ifndef OPENSSL_NO_SOCK
1384 BIO_ADDR_free(peer);
1385 #endif
1386 return ret;
1387 }
1388
create_bare_ssl_connection(SSL * serverssl,SSL * clientssl,int want,int read,int listen)1389 int create_bare_ssl_connection(SSL *serverssl, SSL *clientssl, int want,
1390 int read, int listen)
1391 {
1392 return create_bare_ssl_connection_ex(serverssl, clientssl, want, read,
1393 listen, NULL, NULL);
1394 }
1395
1396 /*
1397 * Create an SSL connection including any post handshake NewSessionTicket
1398 * messages.
1399 */
create_ssl_connection_ex(SSL * serverssl,SSL * clientssl,int want,int * cm_count,int * sm_count)1400 int create_ssl_connection_ex(SSL *serverssl, SSL *clientssl, int want,
1401 int *cm_count, int *sm_count)
1402 {
1403 int i;
1404 unsigned char buf;
1405 size_t readbytes;
1406
1407 if (!create_bare_ssl_connection_ex(serverssl, clientssl, want, 1, 0,
1408 cm_count, sm_count))
1409 return 0;
1410
1411 /*
1412 * We attempt to read some data on the client side which we expect to fail.
1413 * This will ensure we have received the NewSessionTicket in TLSv1.3 where
1414 * appropriate. We do this twice because there are 2 NewSessionTickets.
1415 */
1416 for (i = 0; i < 2; i++) {
1417 if (SSL_read_ex(clientssl, &buf, sizeof(buf), &readbytes) > 0) {
1418 if (!TEST_ulong_eq(readbytes, 0))
1419 return 0;
1420 } else if (!TEST_int_eq(SSL_get_error(clientssl, 0),
1421 SSL_ERROR_WANT_READ)) {
1422 return 0;
1423 }
1424 if (cm_count != NULL)
1425 (*cm_count)++;
1426 }
1427
1428 return 1;
1429 }
1430
create_ssl_connection(SSL * serverssl,SSL * clientssl,int want)1431 int create_ssl_connection(SSL *serverssl, SSL *clientssl, int want)
1432 {
1433 return create_ssl_connection_ex(serverssl, clientssl, want, NULL, NULL);
1434 }
1435
shutdown_ssl_connection(SSL * serverssl,SSL * clientssl)1436 void shutdown_ssl_connection(SSL *serverssl, SSL *clientssl)
1437 {
1438 SSL_shutdown(clientssl);
1439 SSL_shutdown(serverssl);
1440 SSL_free(serverssl);
1441 SSL_free(clientssl);
1442 }
1443
create_a_psk(SSL * ssl,size_t mdsize)1444 SSL_SESSION *create_a_psk(SSL *ssl, size_t mdsize)
1445 {
1446 const SSL_CIPHER *cipher = NULL;
1447 const unsigned char key[SHA384_DIGEST_LENGTH] = {
1448 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
1449 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
1450 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
1451 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
1452 0x2c, 0x2d, 0x2e, 0x2f
1453 };
1454 SSL_SESSION *sess = NULL;
1455
1456 if (mdsize == SHA384_DIGEST_LENGTH) {
1457 cipher = SSL_CIPHER_find(ssl, TLS13_AES_256_GCM_SHA384_BYTES);
1458 } else if (mdsize == SHA256_DIGEST_LENGTH) {
1459 /*
1460 * Any ciphersuite using SHA256 will do - it will be compatible with
1461 * the actual ciphersuite selected as long as it too is based on SHA256
1462 */
1463 cipher = SSL_CIPHER_find(ssl, TLS13_AES_128_GCM_SHA256_BYTES);
1464 } else {
1465 /* Should not happen */
1466 return NULL;
1467 }
1468 sess = SSL_SESSION_new();
1469 if (!TEST_ptr(sess)
1470 || !TEST_ptr(cipher)
1471 || !TEST_true(SSL_SESSION_set1_master_key(sess, key, mdsize))
1472 || !TEST_true(SSL_SESSION_set_cipher(sess, cipher))
1473 || !TEST_true(
1474 SSL_SESSION_set_protocol_version(sess,
1475 TLS1_3_VERSION))) {
1476 SSL_SESSION_free(sess);
1477 return NULL;
1478 }
1479 return sess;
1480 }
1481
1482 #define NUM_EXTRA_CERTS 40
1483
ssl_ctx_add_large_cert_chain(OSSL_LIB_CTX * libctx,SSL_CTX * sctx,const char * cert_file)1484 int ssl_ctx_add_large_cert_chain(OSSL_LIB_CTX *libctx, SSL_CTX *sctx,
1485 const char *cert_file)
1486 {
1487 BIO *certbio = NULL;
1488 X509 *chaincert = NULL;
1489 int certlen;
1490 int ret = 0;
1491 int i;
1492
1493 if (!TEST_ptr(certbio = BIO_new_file(cert_file, "r")))
1494 goto end;
1495
1496 if (!TEST_ptr(chaincert = X509_new_ex(libctx, NULL)))
1497 goto end;
1498
1499 if (PEM_read_bio_X509(certbio, &chaincert, NULL, NULL) == NULL)
1500 goto end;
1501 BIO_free(certbio);
1502 certbio = NULL;
1503
1504 /*
1505 * We assume the supplied certificate is big enough so that if we add
1506 * NUM_EXTRA_CERTS it will make the overall message large enough. The
1507 * default buffer size is requested to be 16k, but due to the way BUF_MEM
1508 * works, it ends up allocating a little over 21k (16 * 4/3). So, in this
1509 * test we need to have a message larger than that.
1510 */
1511 certlen = i2d_X509(chaincert, NULL);
1512 OPENSSL_assert(certlen * NUM_EXTRA_CERTS > (SSL3_RT_MAX_PLAIN_LENGTH * 4) / 3);
1513 for (i = 0; i < NUM_EXTRA_CERTS; i++) {
1514 if (!X509_up_ref(chaincert))
1515 goto end;
1516 if (!SSL_CTX_add_extra_chain_cert(sctx, chaincert)) {
1517 X509_free(chaincert);
1518 goto end;
1519 }
1520 }
1521
1522 ret = 1;
1523 end:
1524 BIO_free(certbio);
1525 X509_free(chaincert);
1526 return ret;
1527 }
1528
load_dasync(void)1529 ENGINE *load_dasync(void)
1530 {
1531 #if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_DYNAMIC_ENGINE)
1532 ENGINE *e;
1533
1534 if (!TEST_ptr(e = ENGINE_by_id("dasync")))
1535 return NULL;
1536
1537 if (!TEST_true(ENGINE_init(e))) {
1538 ENGINE_free(e);
1539 return NULL;
1540 }
1541
1542 if (!TEST_true(ENGINE_register_ciphers(e))) {
1543 ENGINE_free(e);
1544 return NULL;
1545 }
1546
1547 return e;
1548 #else
1549 return NULL;
1550 #endif
1551 }
1552