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