ssltestlib.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. /*
  2. * Copyright 2016-2019 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. int mempacket_test_inject(BIO *bio, const char *in, int inl, int pktnum,
  382. int type)
  383. {
  384. MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
  385. MEMPACKET *thispkt = NULL, *looppkt, *nextpkt, *allpkts[3];
  386. int i, duprec;
  387. const unsigned char *inu = (const unsigned char *)in;
  388. size_t len = ((inu[RECORD_LEN_HI] << 8) | inu[RECORD_LEN_LO])
  389. + DTLS1_RT_HEADER_LENGTH;
  390. if (ctx == NULL)
  391. return -1;
  392. if ((size_t)inl < len)
  393. return -1;
  394. if ((size_t)inl == len)
  395. duprec = 0;
  396. else
  397. duprec = ctx->duprec > 0;
  398. /* We don't support arbitrary injection when duplicating records */
  399. if (duprec && pktnum != -1)
  400. return -1;
  401. /* We only allow injection before we've started writing any data */
  402. if (pktnum >= 0) {
  403. if (ctx->noinject)
  404. return -1;
  405. ctx->injected = 1;
  406. } else {
  407. ctx->noinject = 1;
  408. }
  409. for (i = 0; i < (duprec ? 3 : 1); i++) {
  410. if (!TEST_ptr(allpkts[i] = OPENSSL_malloc(sizeof(*thispkt))))
  411. goto err;
  412. thispkt = allpkts[i];
  413. if (!TEST_ptr(thispkt->data = OPENSSL_malloc(inl)))
  414. goto err;
  415. /*
  416. * If we are duplicating the packet, we duplicate it three times. The
  417. * first two times we drop the first record if there are more than one.
  418. * In this way we know that libssl will not be able to make progress
  419. * until it receives the last packet, and hence will be forced to
  420. * buffer these records.
  421. */
  422. if (duprec && i != 2) {
  423. memcpy(thispkt->data, in + len, inl - len);
  424. thispkt->len = inl - len;
  425. } else {
  426. memcpy(thispkt->data, in, inl);
  427. thispkt->len = inl;
  428. }
  429. thispkt->num = (pktnum >= 0) ? (unsigned int)pktnum : ctx->lastpkt + i;
  430. thispkt->type = type;
  431. }
  432. for(i = 0; (looppkt = sk_MEMPACKET_value(ctx->pkts, i)) != NULL; i++) {
  433. /* Check if we found the right place to insert this packet */
  434. if (looppkt->num > thispkt->num) {
  435. if (sk_MEMPACKET_insert(ctx->pkts, thispkt, i) == 0)
  436. goto err;
  437. /* If we're doing up front injection then we're done */
  438. if (pktnum >= 0)
  439. return inl;
  440. /*
  441. * We need to do some accounting on lastpkt. We increment it first,
  442. * but it might now equal the value of injected packets, so we need
  443. * to skip over those
  444. */
  445. ctx->lastpkt++;
  446. do {
  447. i++;
  448. nextpkt = sk_MEMPACKET_value(ctx->pkts, i);
  449. if (nextpkt != NULL && nextpkt->num == ctx->lastpkt)
  450. ctx->lastpkt++;
  451. else
  452. return inl;
  453. } while(1);
  454. } else if (looppkt->num == thispkt->num) {
  455. if (!ctx->noinject) {
  456. /* We injected two packets with the same packet number! */
  457. goto err;
  458. }
  459. ctx->lastpkt++;
  460. thispkt->num++;
  461. }
  462. }
  463. /*
  464. * We didn't find any packets with a packet number equal to or greater than
  465. * this one, so we just add it onto the end
  466. */
  467. for (i = 0; i < (duprec ? 3 : 1); i++) {
  468. thispkt = allpkts[i];
  469. if (!sk_MEMPACKET_push(ctx->pkts, thispkt))
  470. goto err;
  471. if (pktnum < 0)
  472. ctx->lastpkt++;
  473. }
  474. return inl;
  475. err:
  476. for (i = 0; i < (ctx->duprec > 0 ? 3 : 1); i++)
  477. mempacket_free(allpkts[i]);
  478. return -1;
  479. }
  480. static int mempacket_test_write(BIO *bio, const char *in, int inl)
  481. {
  482. return mempacket_test_inject(bio, in, inl, -1, STANDARD_PACKET);
  483. }
  484. static long mempacket_test_ctrl(BIO *bio, int cmd, long num, void *ptr)
  485. {
  486. long ret = 1;
  487. MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
  488. MEMPACKET *thispkt;
  489. switch (cmd) {
  490. case BIO_CTRL_EOF:
  491. ret = (long)(sk_MEMPACKET_num(ctx->pkts) == 0);
  492. break;
  493. case BIO_CTRL_GET_CLOSE:
  494. ret = BIO_get_shutdown(bio);
  495. break;
  496. case BIO_CTRL_SET_CLOSE:
  497. BIO_set_shutdown(bio, (int)num);
  498. break;
  499. case BIO_CTRL_WPENDING:
  500. ret = 0L;
  501. break;
  502. case BIO_CTRL_PENDING:
  503. thispkt = sk_MEMPACKET_value(ctx->pkts, 0);
  504. if (thispkt == NULL)
  505. ret = 0;
  506. else
  507. ret = thispkt->len;
  508. break;
  509. case BIO_CTRL_FLUSH:
  510. ret = 1;
  511. break;
  512. case MEMPACKET_CTRL_SET_DROP_EPOCH:
  513. ctx->dropepoch = (unsigned int)num;
  514. break;
  515. case MEMPACKET_CTRL_SET_DROP_REC:
  516. ctx->droprec = (int)num;
  517. break;
  518. case MEMPACKET_CTRL_GET_DROP_REC:
  519. ret = ctx->droprec;
  520. break;
  521. case MEMPACKET_CTRL_SET_DUPLICATE_REC:
  522. ctx->duprec = (int)num;
  523. break;
  524. case BIO_CTRL_RESET:
  525. case BIO_CTRL_DUP:
  526. case BIO_CTRL_PUSH:
  527. case BIO_CTRL_POP:
  528. default:
  529. ret = 0;
  530. break;
  531. }
  532. return ret;
  533. }
  534. static int mempacket_test_gets(BIO *bio, char *buf, int size)
  535. {
  536. /* We don't support this - not needed anyway */
  537. return -1;
  538. }
  539. static int mempacket_test_puts(BIO *bio, const char *str)
  540. {
  541. return mempacket_test_write(bio, str, strlen(str));
  542. }
  543. static int always_retry_new(BIO *bi);
  544. static int always_retry_free(BIO *a);
  545. static int always_retry_read(BIO *b, char *out, int outl);
  546. static int always_retry_write(BIO *b, const char *in, int inl);
  547. static long always_retry_ctrl(BIO *b, int cmd, long num, void *ptr);
  548. static int always_retry_gets(BIO *bp, char *buf, int size);
  549. static int always_retry_puts(BIO *bp, const char *str);
  550. const BIO_METHOD *bio_s_always_retry(void)
  551. {
  552. if (meth_always_retry == NULL) {
  553. if (!TEST_ptr(meth_always_retry = BIO_meth_new(BIO_TYPE_ALWAYS_RETRY,
  554. "Always Retry"))
  555. || !TEST_true(BIO_meth_set_write(meth_always_retry,
  556. always_retry_write))
  557. || !TEST_true(BIO_meth_set_read(meth_always_retry,
  558. always_retry_read))
  559. || !TEST_true(BIO_meth_set_puts(meth_always_retry,
  560. always_retry_puts))
  561. || !TEST_true(BIO_meth_set_gets(meth_always_retry,
  562. always_retry_gets))
  563. || !TEST_true(BIO_meth_set_ctrl(meth_always_retry,
  564. always_retry_ctrl))
  565. || !TEST_true(BIO_meth_set_create(meth_always_retry,
  566. always_retry_new))
  567. || !TEST_true(BIO_meth_set_destroy(meth_always_retry,
  568. always_retry_free)))
  569. return NULL;
  570. }
  571. return meth_always_retry;
  572. }
  573. void bio_s_always_retry_free(void)
  574. {
  575. BIO_meth_free(meth_always_retry);
  576. }
  577. static int always_retry_new(BIO *bio)
  578. {
  579. BIO_set_init(bio, 1);
  580. return 1;
  581. }
  582. static int always_retry_free(BIO *bio)
  583. {
  584. BIO_set_data(bio, NULL);
  585. BIO_set_init(bio, 0);
  586. return 1;
  587. }
  588. static int always_retry_read(BIO *bio, char *out, int outl)
  589. {
  590. BIO_set_retry_read(bio);
  591. return -1;
  592. }
  593. static int always_retry_write(BIO *bio, const char *in, int inl)
  594. {
  595. BIO_set_retry_write(bio);
  596. return -1;
  597. }
  598. static long always_retry_ctrl(BIO *bio, int cmd, long num, void *ptr)
  599. {
  600. long ret = 1;
  601. switch (cmd) {
  602. case BIO_CTRL_FLUSH:
  603. BIO_set_retry_write(bio);
  604. /* fall through */
  605. case BIO_CTRL_EOF:
  606. case BIO_CTRL_RESET:
  607. case BIO_CTRL_DUP:
  608. case BIO_CTRL_PUSH:
  609. case BIO_CTRL_POP:
  610. default:
  611. ret = 0;
  612. break;
  613. }
  614. return ret;
  615. }
  616. static int always_retry_gets(BIO *bio, char *buf, int size)
  617. {
  618. BIO_set_retry_read(bio);
  619. return -1;
  620. }
  621. static int always_retry_puts(BIO *bio, const char *str)
  622. {
  623. BIO_set_retry_write(bio);
  624. return -1;
  625. }
  626. int create_ssl_ctx_pair(const SSL_METHOD *sm, const SSL_METHOD *cm,
  627. int min_proto_version, int max_proto_version,
  628. SSL_CTX **sctx, SSL_CTX **cctx, char *certfile,
  629. char *privkeyfile)
  630. {
  631. SSL_CTX *serverctx = NULL;
  632. SSL_CTX *clientctx = NULL;
  633. if (!TEST_ptr(serverctx = SSL_CTX_new(sm))
  634. || (cctx != NULL && !TEST_ptr(clientctx = SSL_CTX_new(cm))))
  635. goto err;
  636. if ((min_proto_version > 0
  637. && !TEST_true(SSL_CTX_set_min_proto_version(serverctx,
  638. min_proto_version)))
  639. || (max_proto_version > 0
  640. && !TEST_true(SSL_CTX_set_max_proto_version(serverctx,
  641. max_proto_version))))
  642. goto err;
  643. if (clientctx != NULL
  644. && ((min_proto_version > 0
  645. && !TEST_true(SSL_CTX_set_min_proto_version(clientctx,
  646. min_proto_version)))
  647. || (max_proto_version > 0
  648. && !TEST_true(SSL_CTX_set_max_proto_version(clientctx,
  649. max_proto_version)))))
  650. goto err;
  651. if (certfile != NULL && privkeyfile != NULL) {
  652. if (!TEST_int_eq(SSL_CTX_use_certificate_file(serverctx, certfile,
  653. SSL_FILETYPE_PEM), 1)
  654. || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(serverctx,
  655. privkeyfile,
  656. SSL_FILETYPE_PEM), 1)
  657. || !TEST_int_eq(SSL_CTX_check_private_key(serverctx), 1))
  658. goto err;
  659. }
  660. #ifndef OPENSSL_NO_DH
  661. SSL_CTX_set_dh_auto(serverctx, 1);
  662. #endif
  663. *sctx = serverctx;
  664. if (cctx != NULL)
  665. *cctx = clientctx;
  666. return 1;
  667. err:
  668. SSL_CTX_free(serverctx);
  669. SSL_CTX_free(clientctx);
  670. return 0;
  671. }
  672. #define MAXLOOPS 1000000
  673. /*
  674. * NOTE: Transfers control of the BIOs - this function will free them on error
  675. */
  676. int create_ssl_objects(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl,
  677. SSL **cssl, BIO *s_to_c_fbio, BIO *c_to_s_fbio)
  678. {
  679. SSL *serverssl = NULL, *clientssl = NULL;
  680. BIO *s_to_c_bio = NULL, *c_to_s_bio = NULL;
  681. if (*sssl != NULL)
  682. serverssl = *sssl;
  683. else if (!TEST_ptr(serverssl = SSL_new(serverctx)))
  684. goto error;
  685. if (*cssl != NULL)
  686. clientssl = *cssl;
  687. else if (!TEST_ptr(clientssl = SSL_new(clientctx)))
  688. goto error;
  689. if (SSL_is_dtls(clientssl)) {
  690. if (!TEST_ptr(s_to_c_bio = BIO_new(bio_s_mempacket_test()))
  691. || !TEST_ptr(c_to_s_bio = BIO_new(bio_s_mempacket_test())))
  692. goto error;
  693. } else {
  694. if (!TEST_ptr(s_to_c_bio = BIO_new(BIO_s_mem()))
  695. || !TEST_ptr(c_to_s_bio = BIO_new(BIO_s_mem())))
  696. goto error;
  697. }
  698. if (s_to_c_fbio != NULL
  699. && !TEST_ptr(s_to_c_bio = BIO_push(s_to_c_fbio, s_to_c_bio)))
  700. goto error;
  701. if (c_to_s_fbio != NULL
  702. && !TEST_ptr(c_to_s_bio = BIO_push(c_to_s_fbio, c_to_s_bio)))
  703. goto error;
  704. /* Set Non-blocking IO behaviour */
  705. BIO_set_mem_eof_return(s_to_c_bio, -1);
  706. BIO_set_mem_eof_return(c_to_s_bio, -1);
  707. /* Up ref these as we are passing them to two SSL objects */
  708. SSL_set_bio(serverssl, c_to_s_bio, s_to_c_bio);
  709. BIO_up_ref(s_to_c_bio);
  710. BIO_up_ref(c_to_s_bio);
  711. SSL_set_bio(clientssl, s_to_c_bio, c_to_s_bio);
  712. *sssl = serverssl;
  713. *cssl = clientssl;
  714. return 1;
  715. error:
  716. SSL_free(serverssl);
  717. SSL_free(clientssl);
  718. BIO_free(s_to_c_bio);
  719. BIO_free(c_to_s_bio);
  720. BIO_free(s_to_c_fbio);
  721. BIO_free(c_to_s_fbio);
  722. return 0;
  723. }
  724. /*
  725. * Create an SSL connection, but does not ready any post-handshake
  726. * NewSessionTicket messages.
  727. * If |read| is set and we're using DTLS then we will attempt to SSL_read on
  728. * the connection once we've completed one half of it, to ensure any retransmits
  729. * get triggered.
  730. */
  731. int create_bare_ssl_connection(SSL *serverssl, SSL *clientssl, int want,
  732. int read)
  733. {
  734. int retc = -1, rets = -1, err, abortctr = 0;
  735. int clienterr = 0, servererr = 0;
  736. int isdtls = SSL_is_dtls(serverssl);
  737. do {
  738. err = SSL_ERROR_WANT_WRITE;
  739. while (!clienterr && retc <= 0 && err == SSL_ERROR_WANT_WRITE) {
  740. retc = SSL_connect(clientssl);
  741. if (retc <= 0)
  742. err = SSL_get_error(clientssl, retc);
  743. }
  744. if (!clienterr && retc <= 0 && err != SSL_ERROR_WANT_READ) {
  745. TEST_info("SSL_connect() failed %d, %d", retc, err);
  746. clienterr = 1;
  747. }
  748. if (want != SSL_ERROR_NONE && err == want)
  749. return 0;
  750. err = SSL_ERROR_WANT_WRITE;
  751. while (!servererr && rets <= 0 && err == SSL_ERROR_WANT_WRITE) {
  752. rets = SSL_accept(serverssl);
  753. if (rets <= 0)
  754. err = SSL_get_error(serverssl, rets);
  755. }
  756. if (!servererr && rets <= 0
  757. && err != SSL_ERROR_WANT_READ
  758. && err != SSL_ERROR_WANT_X509_LOOKUP) {
  759. TEST_info("SSL_accept() failed %d, %d", rets, err);
  760. servererr = 1;
  761. }
  762. if (want != SSL_ERROR_NONE && err == want)
  763. return 0;
  764. if (clienterr && servererr)
  765. return 0;
  766. if (isdtls && read) {
  767. unsigned char buf[20];
  768. /* Trigger any retransmits that may be appropriate */
  769. if (rets > 0 && retc <= 0) {
  770. if (SSL_read(serverssl, buf, sizeof(buf)) > 0) {
  771. /* We don't expect this to succeed! */
  772. TEST_info("Unexpected SSL_read() success!");
  773. return 0;
  774. }
  775. }
  776. if (retc > 0 && rets <= 0) {
  777. if (SSL_read(clientssl, buf, sizeof(buf)) > 0) {
  778. /* We don't expect this to succeed! */
  779. TEST_info("Unexpected SSL_read() success!");
  780. return 0;
  781. }
  782. }
  783. }
  784. if (++abortctr == MAXLOOPS) {
  785. TEST_info("No progress made");
  786. return 0;
  787. }
  788. if (isdtls && abortctr <= 50 && (abortctr % 10) == 0) {
  789. /*
  790. * It looks like we're just spinning. Pause for a short period to
  791. * give the DTLS timer a chance to do something. We only do this for
  792. * the first few times to prevent hangs.
  793. */
  794. ossl_sleep(50);
  795. }
  796. } while (retc <=0 || rets <= 0);
  797. return 1;
  798. }
  799. /*
  800. * Create an SSL connection including any post handshake NewSessionTicket
  801. * messages.
  802. */
  803. int create_ssl_connection(SSL *serverssl, SSL *clientssl, int want)
  804. {
  805. int i;
  806. unsigned char buf;
  807. size_t readbytes;
  808. if (!create_bare_ssl_connection(serverssl, clientssl, want, 1))
  809. return 0;
  810. /*
  811. * We attempt to read some data on the client side which we expect to fail.
  812. * This will ensure we have received the NewSessionTicket in TLSv1.3 where
  813. * appropriate. We do this twice because there are 2 NewSessionTickets.
  814. */
  815. for (i = 0; i < 2; i++) {
  816. if (SSL_read_ex(clientssl, &buf, sizeof(buf), &readbytes) > 0) {
  817. if (!TEST_ulong_eq(readbytes, 0))
  818. return 0;
  819. } else if (!TEST_int_eq(SSL_get_error(clientssl, 0),
  820. SSL_ERROR_WANT_READ)) {
  821. return 0;
  822. }
  823. }
  824. return 1;
  825. }
  826. void shutdown_ssl_connection(SSL *serverssl, SSL *clientssl)
  827. {
  828. SSL_shutdown(clientssl);
  829. SSL_shutdown(serverssl);
  830. SSL_free(serverssl);
  831. SSL_free(clientssl);
  832. }