ssltestlib.c 24 KB

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