ssltestlib.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. /*
  2. * Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 <string.h>
  10. #include "internal/nelem.h"
  11. #include "ssltestlib.h"
  12. #include "testutil.h"
  13. #include "e_os.h"
  14. #ifdef OPENSSL_SYS_UNIX
  15. # include <unistd.h>
  16. static ossl_inline void ossl_sleep(unsigned int millis)
  17. {
  18. # ifdef OPENSSL_SYS_VXWORKS
  19. struct timespec ts;
  20. ts.tv_sec = (long int) (millis / 1000);
  21. ts.tv_nsec = (long int) (millis % 1000) * 1000000ul;
  22. nanosleep(&ts, NULL);
  23. # else
  24. usleep(millis * 1000);
  25. # endif
  26. }
  27. #elif defined(_WIN32)
  28. # include <windows.h>
  29. static ossl_inline void ossl_sleep(unsigned int millis)
  30. {
  31. Sleep(millis);
  32. }
  33. #else
  34. /* Fallback to a busy wait */
  35. static ossl_inline void ossl_sleep(unsigned int millis)
  36. {
  37. struct timeval start, now;
  38. unsigned int elapsedms;
  39. gettimeofday(&start, NULL);
  40. do {
  41. gettimeofday(&now, NULL);
  42. elapsedms = (((now.tv_sec - start.tv_sec) * 1000000)
  43. + now.tv_usec - start.tv_usec) / 1000;
  44. } while (elapsedms < millis);
  45. }
  46. #endif
  47. static int tls_dump_new(BIO *bi);
  48. static int tls_dump_free(BIO *a);
  49. static int tls_dump_read(BIO *b, char *out, int outl);
  50. static int tls_dump_write(BIO *b, const char *in, int inl);
  51. static long tls_dump_ctrl(BIO *b, int cmd, long num, void *ptr);
  52. static int tls_dump_gets(BIO *bp, char *buf, int size);
  53. static int tls_dump_puts(BIO *bp, const char *str);
  54. /* Choose a sufficiently large type likely to be unused for this custom BIO */
  55. #define BIO_TYPE_TLS_DUMP_FILTER (0x80 | BIO_TYPE_FILTER)
  56. #define BIO_TYPE_MEMPACKET_TEST 0x81
  57. #define BIO_TYPE_ALWAYS_RETRY 0x82
  58. static BIO_METHOD *method_tls_dump = NULL;
  59. static BIO_METHOD *meth_mem = NULL;
  60. static BIO_METHOD *meth_always_retry = NULL;
  61. /* Note: Not thread safe! */
  62. const BIO_METHOD *bio_f_tls_dump_filter(void)
  63. {
  64. if (method_tls_dump == NULL) {
  65. method_tls_dump = BIO_meth_new(BIO_TYPE_TLS_DUMP_FILTER,
  66. "TLS dump filter");
  67. if ( method_tls_dump == NULL
  68. || !BIO_meth_set_write(method_tls_dump, tls_dump_write)
  69. || !BIO_meth_set_read(method_tls_dump, tls_dump_read)
  70. || !BIO_meth_set_puts(method_tls_dump, tls_dump_puts)
  71. || !BIO_meth_set_gets(method_tls_dump, tls_dump_gets)
  72. || !BIO_meth_set_ctrl(method_tls_dump, tls_dump_ctrl)
  73. || !BIO_meth_set_create(method_tls_dump, tls_dump_new)
  74. || !BIO_meth_set_destroy(method_tls_dump, tls_dump_free))
  75. return NULL;
  76. }
  77. return method_tls_dump;
  78. }
  79. void bio_f_tls_dump_filter_free(void)
  80. {
  81. BIO_meth_free(method_tls_dump);
  82. }
  83. static int tls_dump_new(BIO *bio)
  84. {
  85. BIO_set_init(bio, 1);
  86. return 1;
  87. }
  88. static int tls_dump_free(BIO *bio)
  89. {
  90. BIO_set_init(bio, 0);
  91. return 1;
  92. }
  93. static void copy_flags(BIO *bio)
  94. {
  95. int flags;
  96. BIO *next = BIO_next(bio);
  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. #define RECORD_CONTENT_TYPE 0
  102. #define RECORD_VERSION_HI 1
  103. #define RECORD_VERSION_LO 2
  104. #define RECORD_EPOCH_HI 3
  105. #define RECORD_EPOCH_LO 4
  106. #define RECORD_SEQUENCE_START 5
  107. #define RECORD_SEQUENCE_END 10
  108. #define RECORD_LEN_HI 11
  109. #define RECORD_LEN_LO 12
  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. static void dump_data(const char *data, int len)
  123. {
  124. int rem, i, content, reclen, msglen, fragoff, fraglen, epoch;
  125. unsigned char *rec;
  126. printf("---- START OF PACKET ----\n");
  127. rem = len;
  128. rec = (unsigned char *)data;
  129. while (rem > 0) {
  130. if (rem != len)
  131. printf("*\n");
  132. printf("*---- START OF RECORD ----\n");
  133. if (rem < DTLS1_RT_HEADER_LENGTH) {
  134. printf("*---- RECORD TRUNCATED ----\n");
  135. break;
  136. }
  137. content = rec[RECORD_CONTENT_TYPE];
  138. printf("** Record Content-type: %d\n", content);
  139. printf("** Record Version: %02x%02x\n",
  140. rec[RECORD_VERSION_HI], rec[RECORD_VERSION_LO]);
  141. epoch = (rec[RECORD_EPOCH_HI] << 8) | rec[RECORD_EPOCH_LO];
  142. printf("** Record Epoch: %d\n", epoch);
  143. printf("** Record Sequence: ");
  144. for (i = RECORD_SEQUENCE_START; i <= RECORD_SEQUENCE_END; i++)
  145. printf("%02x", rec[i]);
  146. reclen = (rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO];
  147. printf("\n** Record Length: %d\n", reclen);
  148. /* Now look at message */
  149. rec += DTLS1_RT_HEADER_LENGTH;
  150. rem -= DTLS1_RT_HEADER_LENGTH;
  151. if (content == SSL3_RT_HANDSHAKE) {
  152. printf("**---- START OF HANDSHAKE MESSAGE FRAGMENT ----\n");
  153. if (epoch > 0) {
  154. printf("**---- HANDSHAKE MESSAGE FRAGMENT ENCRYPTED ----\n");
  155. } else if (rem < DTLS1_HM_HEADER_LENGTH
  156. || reclen < DTLS1_HM_HEADER_LENGTH) {
  157. printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n");
  158. } else {
  159. printf("*** Message Type: %d\n", rec[MSG_TYPE]);
  160. msglen = (rec[MSG_LEN_HI] << 16) | (rec[MSG_LEN_MID] << 8)
  161. | rec[MSG_LEN_LO];
  162. printf("*** Message Length: %d\n", msglen);
  163. printf("*** Message sequence: %d\n",
  164. (rec[MSG_SEQ_HI] << 8) | rec[MSG_SEQ_LO]);
  165. fragoff = (rec[MSG_FRAG_OFF_HI] << 16)
  166. | (rec[MSG_FRAG_OFF_MID] << 8)
  167. | rec[MSG_FRAG_OFF_LO];
  168. printf("*** Message Fragment offset: %d\n", fragoff);
  169. fraglen = (rec[MSG_FRAG_LEN_HI] << 16)
  170. | (rec[MSG_FRAG_LEN_MID] << 8)
  171. | rec[MSG_FRAG_LEN_LO];
  172. printf("*** Message Fragment len: %d\n", fraglen);
  173. if (fragoff + fraglen > msglen)
  174. printf("***---- HANDSHAKE MESSAGE FRAGMENT INVALID ----\n");
  175. else if (reclen < fraglen)
  176. printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n");
  177. else
  178. printf("**---- END OF HANDSHAKE MESSAGE FRAGMENT ----\n");
  179. }
  180. }
  181. if (rem < reclen) {
  182. printf("*---- RECORD TRUNCATED ----\n");
  183. rem = 0;
  184. } else {
  185. rec += reclen;
  186. rem -= reclen;
  187. printf("*---- END OF RECORD ----\n");
  188. }
  189. }
  190. printf("---- END OF PACKET ----\n\n");
  191. fflush(stdout);
  192. }
  193. static int tls_dump_read(BIO *bio, char *out, int outl)
  194. {
  195. int ret;
  196. BIO *next = BIO_next(bio);
  197. ret = BIO_read(next, out, outl);
  198. copy_flags(bio);
  199. if (ret > 0) {
  200. dump_data(out, ret);
  201. }
  202. return ret;
  203. }
  204. static int tls_dump_write(BIO *bio, const char *in, int inl)
  205. {
  206. int ret;
  207. BIO *next = BIO_next(bio);
  208. ret = BIO_write(next, in, inl);
  209. copy_flags(bio);
  210. return ret;
  211. }
  212. static long tls_dump_ctrl(BIO *bio, int cmd, long num, void *ptr)
  213. {
  214. long ret;
  215. BIO *next = BIO_next(bio);
  216. if (next == NULL)
  217. return 0;
  218. switch (cmd) {
  219. case BIO_CTRL_DUP:
  220. ret = 0L;
  221. break;
  222. default:
  223. ret = BIO_ctrl(next, cmd, num, ptr);
  224. break;
  225. }
  226. return ret;
  227. }
  228. static int tls_dump_gets(BIO *bio, char *buf, int size)
  229. {
  230. /* We don't support this - not needed anyway */
  231. return -1;
  232. }
  233. static int tls_dump_puts(BIO *bio, const char *str)
  234. {
  235. return tls_dump_write(bio, str, strlen(str));
  236. }
  237. struct mempacket_st {
  238. unsigned char *data;
  239. int len;
  240. unsigned int num;
  241. unsigned int type;
  242. };
  243. static void mempacket_free(MEMPACKET *pkt)
  244. {
  245. if (pkt->data != NULL)
  246. OPENSSL_free(pkt->data);
  247. OPENSSL_free(pkt);
  248. }
  249. typedef struct mempacket_test_ctx_st {
  250. STACK_OF(MEMPACKET) *pkts;
  251. unsigned int epoch;
  252. unsigned int currrec;
  253. unsigned int currpkt;
  254. unsigned int lastpkt;
  255. unsigned int injected;
  256. unsigned int noinject;
  257. unsigned int dropepoch;
  258. int droprec;
  259. int duprec;
  260. } MEMPACKET_TEST_CTX;
  261. static int mempacket_test_new(BIO *bi);
  262. static int mempacket_test_free(BIO *a);
  263. static int mempacket_test_read(BIO *b, char *out, int outl);
  264. static int mempacket_test_write(BIO *b, const char *in, int inl);
  265. static long mempacket_test_ctrl(BIO *b, int cmd, long num, void *ptr);
  266. static int mempacket_test_gets(BIO *bp, char *buf, int size);
  267. static int mempacket_test_puts(BIO *bp, const char *str);
  268. const BIO_METHOD *bio_s_mempacket_test(void)
  269. {
  270. if (meth_mem == NULL) {
  271. if (!TEST_ptr(meth_mem = BIO_meth_new(BIO_TYPE_MEMPACKET_TEST,
  272. "Mem Packet Test"))
  273. || !TEST_true(BIO_meth_set_write(meth_mem, mempacket_test_write))
  274. || !TEST_true(BIO_meth_set_read(meth_mem, mempacket_test_read))
  275. || !TEST_true(BIO_meth_set_puts(meth_mem, mempacket_test_puts))
  276. || !TEST_true(BIO_meth_set_gets(meth_mem, mempacket_test_gets))
  277. || !TEST_true(BIO_meth_set_ctrl(meth_mem, mempacket_test_ctrl))
  278. || !TEST_true(BIO_meth_set_create(meth_mem, mempacket_test_new))
  279. || !TEST_true(BIO_meth_set_destroy(meth_mem, mempacket_test_free)))
  280. return NULL;
  281. }
  282. return meth_mem;
  283. }
  284. void bio_s_mempacket_test_free(void)
  285. {
  286. BIO_meth_free(meth_mem);
  287. }
  288. static int mempacket_test_new(BIO *bio)
  289. {
  290. MEMPACKET_TEST_CTX *ctx;
  291. if (!TEST_ptr(ctx = OPENSSL_zalloc(sizeof(*ctx))))
  292. return 0;
  293. if (!TEST_ptr(ctx->pkts = sk_MEMPACKET_new_null())) {
  294. OPENSSL_free(ctx);
  295. return 0;
  296. }
  297. ctx->dropepoch = 0;
  298. ctx->droprec = -1;
  299. BIO_set_init(bio, 1);
  300. BIO_set_data(bio, ctx);
  301. return 1;
  302. }
  303. static int mempacket_test_free(BIO *bio)
  304. {
  305. MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
  306. sk_MEMPACKET_pop_free(ctx->pkts, mempacket_free);
  307. OPENSSL_free(ctx);
  308. BIO_set_data(bio, NULL);
  309. BIO_set_init(bio, 0);
  310. return 1;
  311. }
  312. /* Record Header values */
  313. #define EPOCH_HI 3
  314. #define EPOCH_LO 4
  315. #define RECORD_SEQUENCE 10
  316. #define RECORD_LEN_HI 11
  317. #define RECORD_LEN_LO 12
  318. #define STANDARD_PACKET 0
  319. static int mempacket_test_read(BIO *bio, char *out, int outl)
  320. {
  321. MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
  322. MEMPACKET *thispkt;
  323. unsigned char *rec;
  324. int rem;
  325. unsigned int seq, offset, len, epoch;
  326. BIO_clear_retry_flags(bio);
  327. thispkt = sk_MEMPACKET_value(ctx->pkts, 0);
  328. if (thispkt == NULL || thispkt->num != ctx->currpkt) {
  329. /* Probably run out of data */
  330. BIO_set_retry_read(bio);
  331. return -1;
  332. }
  333. (void)sk_MEMPACKET_shift(ctx->pkts);
  334. ctx->currpkt++;
  335. if (outl > thispkt->len)
  336. outl = thispkt->len;
  337. if (thispkt->type != INJECT_PACKET_IGNORE_REC_SEQ
  338. && (ctx->injected || ctx->droprec >= 0)) {
  339. /*
  340. * Overwrite the record sequence number. We strictly number them in
  341. * the order received. Since we are actually a reliable transport
  342. * we know that there won't be any re-ordering. We overwrite to deal
  343. * with any packets that have been injected
  344. */
  345. for (rem = thispkt->len, rec = thispkt->data; rem > 0; rem -= len) {
  346. if (rem < DTLS1_RT_HEADER_LENGTH)
  347. return -1;
  348. epoch = (rec[EPOCH_HI] << 8) | rec[EPOCH_LO];
  349. if (epoch != ctx->epoch) {
  350. ctx->epoch = epoch;
  351. ctx->currrec = 0;
  352. }
  353. seq = ctx->currrec;
  354. offset = 0;
  355. do {
  356. rec[RECORD_SEQUENCE - offset] = seq & 0xFF;
  357. seq >>= 8;
  358. offset++;
  359. } while (seq > 0);
  360. len = ((rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO])
  361. + DTLS1_RT_HEADER_LENGTH;
  362. if (rem < (int)len)
  363. return -1;
  364. if (ctx->droprec == (int)ctx->currrec && ctx->dropepoch == epoch) {
  365. if (rem > (int)len)
  366. memmove(rec, rec + len, rem - len);
  367. outl -= len;
  368. ctx->droprec = -1;
  369. if (outl == 0)
  370. BIO_set_retry_read(bio);
  371. } else {
  372. rec += len;
  373. }
  374. ctx->currrec++;
  375. }
  376. }
  377. memcpy(out, thispkt->data, outl);
  378. mempacket_free(thispkt);
  379. return outl;
  380. }
  381. /* Take the last and penultimate packets and swap them around */
  382. int mempacket_swap_recent(BIO *bio)
  383. {
  384. MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
  385. MEMPACKET *thispkt;
  386. int numpkts = sk_MEMPACKET_num(ctx->pkts);
  387. /* We need at least 2 packets to be able to swap them */
  388. if (numpkts <= 1)
  389. return 0;
  390. /* Get the penultimate packet */
  391. thispkt = sk_MEMPACKET_value(ctx->pkts, numpkts - 2);
  392. if (thispkt == NULL)
  393. return 0;
  394. if (sk_MEMPACKET_delete(ctx->pkts, numpkts - 2) != thispkt)
  395. return 0;
  396. /* Re-add it to the end of the list */
  397. thispkt->num++;
  398. if (sk_MEMPACKET_insert(ctx->pkts, thispkt, numpkts - 1) <= 0)
  399. return 0;
  400. /* We also have to adjust the packet number of the other packet */
  401. thispkt = sk_MEMPACKET_value(ctx->pkts, numpkts - 2);
  402. if (thispkt == NULL)
  403. return 0;
  404. thispkt->num--;
  405. return 1;
  406. }
  407. int mempacket_test_inject(BIO *bio, const char *in, int inl, int pktnum,
  408. int type)
  409. {
  410. MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
  411. MEMPACKET *thispkt = NULL, *looppkt, *nextpkt, *allpkts[3];
  412. int i, duprec;
  413. const unsigned char *inu = (const unsigned char *)in;
  414. size_t len = ((inu[RECORD_LEN_HI] << 8) | inu[RECORD_LEN_LO])
  415. + DTLS1_RT_HEADER_LENGTH;
  416. if (ctx == NULL)
  417. return -1;
  418. if ((size_t)inl < len)
  419. return -1;
  420. if ((size_t)inl == len)
  421. duprec = 0;
  422. else
  423. duprec = ctx->duprec > 0;
  424. /* We don't support arbitrary injection when duplicating records */
  425. if (duprec && pktnum != -1)
  426. return -1;
  427. /* We only allow injection before we've started writing any data */
  428. if (pktnum >= 0) {
  429. if (ctx->noinject)
  430. return -1;
  431. ctx->injected = 1;
  432. } else {
  433. ctx->noinject = 1;
  434. }
  435. for (i = 0; i < (duprec ? 3 : 1); i++) {
  436. if (!TEST_ptr(allpkts[i] = OPENSSL_malloc(sizeof(*thispkt))))
  437. goto err;
  438. thispkt = allpkts[i];
  439. if (!TEST_ptr(thispkt->data = OPENSSL_malloc(inl)))
  440. goto err;
  441. /*
  442. * If we are duplicating the packet, we duplicate it three times. The
  443. * first two times we drop the first record if there are more than one.
  444. * In this way we know that libssl will not be able to make progress
  445. * until it receives the last packet, and hence will be forced to
  446. * buffer these records.
  447. */
  448. if (duprec && i != 2) {
  449. memcpy(thispkt->data, in + len, inl - len);
  450. thispkt->len = inl - len;
  451. } else {
  452. memcpy(thispkt->data, in, inl);
  453. thispkt->len = inl;
  454. }
  455. thispkt->num = (pktnum >= 0) ? (unsigned int)pktnum : ctx->lastpkt + i;
  456. thispkt->type = type;
  457. }
  458. for(i = 0; (looppkt = sk_MEMPACKET_value(ctx->pkts, i)) != NULL; i++) {
  459. /* Check if we found the right place to insert this packet */
  460. if (looppkt->num > thispkt->num) {
  461. if (sk_MEMPACKET_insert(ctx->pkts, thispkt, i) == 0)
  462. goto err;
  463. /* If we're doing up front injection then we're done */
  464. if (pktnum >= 0)
  465. return inl;
  466. /*
  467. * We need to do some accounting on lastpkt. We increment it first,
  468. * but it might now equal the value of injected packets, so we need
  469. * to skip over those
  470. */
  471. ctx->lastpkt++;
  472. do {
  473. i++;
  474. nextpkt = sk_MEMPACKET_value(ctx->pkts, i);
  475. if (nextpkt != NULL && nextpkt->num == ctx->lastpkt)
  476. ctx->lastpkt++;
  477. else
  478. return inl;
  479. } while(1);
  480. } else if (looppkt->num == thispkt->num) {
  481. if (!ctx->noinject) {
  482. /* We injected two packets with the same packet number! */
  483. goto err;
  484. }
  485. ctx->lastpkt++;
  486. thispkt->num++;
  487. }
  488. }
  489. /*
  490. * We didn't find any packets with a packet number equal to or greater than
  491. * this one, so we just add it onto the end
  492. */
  493. for (i = 0; i < (duprec ? 3 : 1); i++) {
  494. thispkt = allpkts[i];
  495. if (!sk_MEMPACKET_push(ctx->pkts, thispkt))
  496. goto err;
  497. if (pktnum < 0)
  498. ctx->lastpkt++;
  499. }
  500. return inl;
  501. err:
  502. for (i = 0; i < (ctx->duprec > 0 ? 3 : 1); i++)
  503. mempacket_free(allpkts[i]);
  504. return -1;
  505. }
  506. static int mempacket_test_write(BIO *bio, const char *in, int inl)
  507. {
  508. return mempacket_test_inject(bio, in, inl, -1, STANDARD_PACKET);
  509. }
  510. static long mempacket_test_ctrl(BIO *bio, int cmd, long num, void *ptr)
  511. {
  512. long ret = 1;
  513. MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
  514. MEMPACKET *thispkt;
  515. switch (cmd) {
  516. case BIO_CTRL_EOF:
  517. ret = (long)(sk_MEMPACKET_num(ctx->pkts) == 0);
  518. break;
  519. case BIO_CTRL_GET_CLOSE:
  520. ret = BIO_get_shutdown(bio);
  521. break;
  522. case BIO_CTRL_SET_CLOSE:
  523. BIO_set_shutdown(bio, (int)num);
  524. break;
  525. case BIO_CTRL_WPENDING:
  526. ret = 0L;
  527. break;
  528. case BIO_CTRL_PENDING:
  529. thispkt = sk_MEMPACKET_value(ctx->pkts, 0);
  530. if (thispkt == NULL)
  531. ret = 0;
  532. else
  533. ret = thispkt->len;
  534. break;
  535. case BIO_CTRL_FLUSH:
  536. ret = 1;
  537. break;
  538. case MEMPACKET_CTRL_SET_DROP_EPOCH:
  539. ctx->dropepoch = (unsigned int)num;
  540. break;
  541. case MEMPACKET_CTRL_SET_DROP_REC:
  542. ctx->droprec = (int)num;
  543. break;
  544. case MEMPACKET_CTRL_GET_DROP_REC:
  545. ret = ctx->droprec;
  546. break;
  547. case MEMPACKET_CTRL_SET_DUPLICATE_REC:
  548. ctx->duprec = (int)num;
  549. break;
  550. case BIO_CTRL_RESET:
  551. case BIO_CTRL_DUP:
  552. case BIO_CTRL_PUSH:
  553. case BIO_CTRL_POP:
  554. default:
  555. ret = 0;
  556. break;
  557. }
  558. return ret;
  559. }
  560. static int mempacket_test_gets(BIO *bio, char *buf, int size)
  561. {
  562. /* We don't support this - not needed anyway */
  563. return -1;
  564. }
  565. static int mempacket_test_puts(BIO *bio, const char *str)
  566. {
  567. return mempacket_test_write(bio, str, strlen(str));
  568. }
  569. static int always_retry_new(BIO *bi);
  570. static int always_retry_free(BIO *a);
  571. static int always_retry_read(BIO *b, char *out, int outl);
  572. static int always_retry_write(BIO *b, const char *in, int inl);
  573. static long always_retry_ctrl(BIO *b, int cmd, long num, void *ptr);
  574. static int always_retry_gets(BIO *bp, char *buf, int size);
  575. static int always_retry_puts(BIO *bp, const char *str);
  576. const BIO_METHOD *bio_s_always_retry(void)
  577. {
  578. if (meth_always_retry == NULL) {
  579. if (!TEST_ptr(meth_always_retry = BIO_meth_new(BIO_TYPE_ALWAYS_RETRY,
  580. "Always Retry"))
  581. || !TEST_true(BIO_meth_set_write(meth_always_retry,
  582. always_retry_write))
  583. || !TEST_true(BIO_meth_set_read(meth_always_retry,
  584. always_retry_read))
  585. || !TEST_true(BIO_meth_set_puts(meth_always_retry,
  586. always_retry_puts))
  587. || !TEST_true(BIO_meth_set_gets(meth_always_retry,
  588. always_retry_gets))
  589. || !TEST_true(BIO_meth_set_ctrl(meth_always_retry,
  590. always_retry_ctrl))
  591. || !TEST_true(BIO_meth_set_create(meth_always_retry,
  592. always_retry_new))
  593. || !TEST_true(BIO_meth_set_destroy(meth_always_retry,
  594. always_retry_free)))
  595. return NULL;
  596. }
  597. return meth_always_retry;
  598. }
  599. void bio_s_always_retry_free(void)
  600. {
  601. BIO_meth_free(meth_always_retry);
  602. }
  603. static int always_retry_new(BIO *bio)
  604. {
  605. BIO_set_init(bio, 1);
  606. return 1;
  607. }
  608. static int always_retry_free(BIO *bio)
  609. {
  610. BIO_set_data(bio, NULL);
  611. BIO_set_init(bio, 0);
  612. return 1;
  613. }
  614. static int always_retry_read(BIO *bio, char *out, int outl)
  615. {
  616. BIO_set_retry_read(bio);
  617. return -1;
  618. }
  619. static int always_retry_write(BIO *bio, const char *in, int inl)
  620. {
  621. BIO_set_retry_write(bio);
  622. return -1;
  623. }
  624. static long always_retry_ctrl(BIO *bio, int cmd, long num, void *ptr)
  625. {
  626. long ret = 1;
  627. switch (cmd) {
  628. case BIO_CTRL_FLUSH:
  629. BIO_set_retry_write(bio);
  630. /* fall through */
  631. case BIO_CTRL_EOF:
  632. case BIO_CTRL_RESET:
  633. case BIO_CTRL_DUP:
  634. case BIO_CTRL_PUSH:
  635. case BIO_CTRL_POP:
  636. default:
  637. ret = 0;
  638. break;
  639. }
  640. return ret;
  641. }
  642. static int always_retry_gets(BIO *bio, char *buf, int size)
  643. {
  644. BIO_set_retry_read(bio);
  645. return -1;
  646. }
  647. static int always_retry_puts(BIO *bio, const char *str)
  648. {
  649. BIO_set_retry_write(bio);
  650. return -1;
  651. }
  652. int create_ssl_ctx_pair(const SSL_METHOD *sm, const SSL_METHOD *cm,
  653. int min_proto_version, int max_proto_version,
  654. SSL_CTX **sctx, SSL_CTX **cctx, char *certfile,
  655. char *privkeyfile)
  656. {
  657. SSL_CTX *serverctx = NULL;
  658. SSL_CTX *clientctx = NULL;
  659. if (!TEST_ptr(serverctx = SSL_CTX_new(sm))
  660. || (cctx != NULL && !TEST_ptr(clientctx = SSL_CTX_new(cm))))
  661. goto err;
  662. if ((min_proto_version > 0
  663. && !TEST_true(SSL_CTX_set_min_proto_version(serverctx,
  664. min_proto_version)))
  665. || (max_proto_version > 0
  666. && !TEST_true(SSL_CTX_set_max_proto_version(serverctx,
  667. max_proto_version))))
  668. goto err;
  669. if (clientctx != NULL
  670. && ((min_proto_version > 0
  671. && !TEST_true(SSL_CTX_set_min_proto_version(clientctx,
  672. min_proto_version)))
  673. || (max_proto_version > 0
  674. && !TEST_true(SSL_CTX_set_max_proto_version(clientctx,
  675. max_proto_version)))))
  676. goto err;
  677. if (certfile != NULL && privkeyfile != NULL) {
  678. if (!TEST_int_eq(SSL_CTX_use_certificate_file(serverctx, certfile,
  679. SSL_FILETYPE_PEM), 1)
  680. || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(serverctx,
  681. privkeyfile,
  682. SSL_FILETYPE_PEM), 1)
  683. || !TEST_int_eq(SSL_CTX_check_private_key(serverctx), 1))
  684. goto err;
  685. }
  686. #ifndef OPENSSL_NO_DH
  687. SSL_CTX_set_dh_auto(serverctx, 1);
  688. #endif
  689. *sctx = serverctx;
  690. if (cctx != NULL)
  691. *cctx = clientctx;
  692. return 1;
  693. err:
  694. SSL_CTX_free(serverctx);
  695. SSL_CTX_free(clientctx);
  696. return 0;
  697. }
  698. #define MAXLOOPS 1000000
  699. /*
  700. * NOTE: Transfers control of the BIOs - this function will free them on error
  701. */
  702. int create_ssl_objects(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl,
  703. SSL **cssl, BIO *s_to_c_fbio, BIO *c_to_s_fbio)
  704. {
  705. SSL *serverssl = NULL, *clientssl = NULL;
  706. BIO *s_to_c_bio = NULL, *c_to_s_bio = NULL;
  707. if (*sssl != NULL)
  708. serverssl = *sssl;
  709. else if (!TEST_ptr(serverssl = SSL_new(serverctx)))
  710. goto error;
  711. if (*cssl != NULL)
  712. clientssl = *cssl;
  713. else if (!TEST_ptr(clientssl = SSL_new(clientctx)))
  714. goto error;
  715. if (SSL_is_dtls(clientssl)) {
  716. if (!TEST_ptr(s_to_c_bio = BIO_new(bio_s_mempacket_test()))
  717. || !TEST_ptr(c_to_s_bio = BIO_new(bio_s_mempacket_test())))
  718. goto error;
  719. } else {
  720. if (!TEST_ptr(s_to_c_bio = BIO_new(BIO_s_mem()))
  721. || !TEST_ptr(c_to_s_bio = BIO_new(BIO_s_mem())))
  722. goto error;
  723. }
  724. if (s_to_c_fbio != NULL
  725. && !TEST_ptr(s_to_c_bio = BIO_push(s_to_c_fbio, s_to_c_bio)))
  726. goto error;
  727. if (c_to_s_fbio != NULL
  728. && !TEST_ptr(c_to_s_bio = BIO_push(c_to_s_fbio, c_to_s_bio)))
  729. goto error;
  730. /* Set Non-blocking IO behaviour */
  731. BIO_set_mem_eof_return(s_to_c_bio, -1);
  732. BIO_set_mem_eof_return(c_to_s_bio, -1);
  733. /* Up ref these as we are passing them to two SSL objects */
  734. SSL_set_bio(serverssl, c_to_s_bio, s_to_c_bio);
  735. BIO_up_ref(s_to_c_bio);
  736. BIO_up_ref(c_to_s_bio);
  737. SSL_set_bio(clientssl, s_to_c_bio, c_to_s_bio);
  738. *sssl = serverssl;
  739. *cssl = clientssl;
  740. return 1;
  741. error:
  742. SSL_free(serverssl);
  743. SSL_free(clientssl);
  744. BIO_free(s_to_c_bio);
  745. BIO_free(c_to_s_bio);
  746. BIO_free(s_to_c_fbio);
  747. BIO_free(c_to_s_fbio);
  748. return 0;
  749. }
  750. /*
  751. * Create an SSL connection, but does not ready any post-handshake
  752. * NewSessionTicket messages.
  753. * If |read| is set and we're using DTLS then we will attempt to SSL_read on
  754. * the connection once we've completed one half of it, to ensure any retransmits
  755. * get triggered.
  756. */
  757. int create_bare_ssl_connection(SSL *serverssl, SSL *clientssl, int want,
  758. int read)
  759. {
  760. int retc = -1, rets = -1, err, abortctr = 0;
  761. int clienterr = 0, servererr = 0;
  762. int isdtls = SSL_is_dtls(serverssl);
  763. do {
  764. err = SSL_ERROR_WANT_WRITE;
  765. while (!clienterr && retc <= 0 && err == SSL_ERROR_WANT_WRITE) {
  766. retc = SSL_connect(clientssl);
  767. if (retc <= 0)
  768. err = SSL_get_error(clientssl, retc);
  769. }
  770. if (!clienterr && retc <= 0 && err != SSL_ERROR_WANT_READ) {
  771. TEST_info("SSL_connect() failed %d, %d", retc, err);
  772. clienterr = 1;
  773. }
  774. if (want != SSL_ERROR_NONE && err == want)
  775. return 0;
  776. err = SSL_ERROR_WANT_WRITE;
  777. while (!servererr && rets <= 0 && err == SSL_ERROR_WANT_WRITE) {
  778. rets = SSL_accept(serverssl);
  779. if (rets <= 0)
  780. err = SSL_get_error(serverssl, rets);
  781. }
  782. if (!servererr && rets <= 0
  783. && err != SSL_ERROR_WANT_READ
  784. && err != SSL_ERROR_WANT_X509_LOOKUP) {
  785. TEST_info("SSL_accept() failed %d, %d", rets, err);
  786. servererr = 1;
  787. }
  788. if (want != SSL_ERROR_NONE && err == want)
  789. return 0;
  790. if (clienterr && servererr)
  791. return 0;
  792. if (isdtls && read) {
  793. unsigned char buf[20];
  794. /* Trigger any retransmits that may be appropriate */
  795. if (rets > 0 && retc <= 0) {
  796. if (SSL_read(serverssl, buf, sizeof(buf)) > 0) {
  797. /* We don't expect this to succeed! */
  798. TEST_info("Unexpected SSL_read() success!");
  799. return 0;
  800. }
  801. }
  802. if (retc > 0 && rets <= 0) {
  803. if (SSL_read(clientssl, buf, sizeof(buf)) > 0) {
  804. /* We don't expect this to succeed! */
  805. TEST_info("Unexpected SSL_read() success!");
  806. return 0;
  807. }
  808. }
  809. }
  810. if (++abortctr == MAXLOOPS) {
  811. TEST_info("No progress made");
  812. return 0;
  813. }
  814. if (isdtls && abortctr <= 50 && (abortctr % 10) == 0) {
  815. /*
  816. * It looks like we're just spinning. Pause for a short period to
  817. * give the DTLS timer a chance to do something. We only do this for
  818. * the first few times to prevent hangs.
  819. */
  820. ossl_sleep(50);
  821. }
  822. } while (retc <=0 || rets <= 0);
  823. return 1;
  824. }
  825. /*
  826. * Create an SSL connection including any post handshake NewSessionTicket
  827. * messages.
  828. */
  829. int create_ssl_connection(SSL *serverssl, SSL *clientssl, int want)
  830. {
  831. int i;
  832. unsigned char buf;
  833. size_t readbytes;
  834. if (!create_bare_ssl_connection(serverssl, clientssl, want, 1))
  835. return 0;
  836. /*
  837. * We attempt to read some data on the client side which we expect to fail.
  838. * This will ensure we have received the NewSessionTicket in TLSv1.3 where
  839. * appropriate. We do this twice because there are 2 NewSessionTickets.
  840. */
  841. for (i = 0; i < 2; i++) {
  842. if (SSL_read_ex(clientssl, &buf, sizeof(buf), &readbytes) > 0) {
  843. if (!TEST_ulong_eq(readbytes, 0))
  844. return 0;
  845. } else if (!TEST_int_eq(SSL_get_error(clientssl, 0),
  846. SSL_ERROR_WANT_READ)) {
  847. return 0;
  848. }
  849. }
  850. return 1;
  851. }
  852. void shutdown_ssl_connection(SSL *serverssl, SSL *clientssl)
  853. {
  854. SSL_shutdown(clientssl);
  855. SSL_shutdown(serverssl);
  856. SSL_free(serverssl);
  857. SSL_free(clientssl);
  858. }