ssltestlib.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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. } MEMPACKET_TEST_CTX;
  248. static int mempacket_test_new(BIO *bi);
  249. static int mempacket_test_free(BIO *a);
  250. static int mempacket_test_read(BIO *b, char *out, int outl);
  251. static int mempacket_test_write(BIO *b, const char *in, int inl);
  252. static long mempacket_test_ctrl(BIO *b, int cmd, long num, void *ptr);
  253. static int mempacket_test_gets(BIO *bp, char *buf, int size);
  254. static int mempacket_test_puts(BIO *bp, const char *str);
  255. const BIO_METHOD *bio_s_mempacket_test(void)
  256. {
  257. if (meth_mem == NULL) {
  258. if (!TEST_ptr(meth_mem = BIO_meth_new(BIO_TYPE_MEMPACKET_TEST,
  259. "Mem Packet Test"))
  260. || !TEST_true(BIO_meth_set_write(meth_mem, mempacket_test_write))
  261. || !TEST_true(BIO_meth_set_read(meth_mem, mempacket_test_read))
  262. || !TEST_true(BIO_meth_set_puts(meth_mem, mempacket_test_puts))
  263. || !TEST_true(BIO_meth_set_gets(meth_mem, mempacket_test_gets))
  264. || !TEST_true(BIO_meth_set_ctrl(meth_mem, mempacket_test_ctrl))
  265. || !TEST_true(BIO_meth_set_create(meth_mem, mempacket_test_new))
  266. || !TEST_true(BIO_meth_set_destroy(meth_mem, mempacket_test_free)))
  267. return NULL;
  268. }
  269. return meth_mem;
  270. }
  271. void bio_s_mempacket_test_free(void)
  272. {
  273. BIO_meth_free(meth_mem);
  274. }
  275. static int mempacket_test_new(BIO *bio)
  276. {
  277. MEMPACKET_TEST_CTX *ctx;
  278. if (!TEST_ptr(ctx = OPENSSL_zalloc(sizeof(*ctx))))
  279. return 0;
  280. if (!TEST_ptr(ctx->pkts = sk_MEMPACKET_new_null())) {
  281. OPENSSL_free(ctx);
  282. return 0;
  283. }
  284. ctx->dropepoch = 0;
  285. ctx->droprec = -1;
  286. BIO_set_init(bio, 1);
  287. BIO_set_data(bio, ctx);
  288. return 1;
  289. }
  290. static int mempacket_test_free(BIO *bio)
  291. {
  292. MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
  293. sk_MEMPACKET_pop_free(ctx->pkts, mempacket_free);
  294. OPENSSL_free(ctx);
  295. BIO_set_data(bio, NULL);
  296. BIO_set_init(bio, 0);
  297. return 1;
  298. }
  299. /* Record Header values */
  300. #define EPOCH_HI 3
  301. #define EPOCH_LO 4
  302. #define RECORD_SEQUENCE 10
  303. #define RECORD_LEN_HI 11
  304. #define RECORD_LEN_LO 12
  305. #define STANDARD_PACKET 0
  306. static int mempacket_test_read(BIO *bio, char *out, int outl)
  307. {
  308. MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
  309. MEMPACKET *thispkt;
  310. unsigned char *rec;
  311. int rem;
  312. unsigned int seq, offset, len, epoch;
  313. BIO_clear_retry_flags(bio);
  314. thispkt = sk_MEMPACKET_value(ctx->pkts, 0);
  315. if (thispkt == NULL || thispkt->num != ctx->currpkt) {
  316. /* Probably run out of data */
  317. BIO_set_retry_read(bio);
  318. return -1;
  319. }
  320. (void)sk_MEMPACKET_shift(ctx->pkts);
  321. ctx->currpkt++;
  322. if (outl > thispkt->len)
  323. outl = thispkt->len;
  324. if (thispkt->type != INJECT_PACKET_IGNORE_REC_SEQ
  325. && (ctx->injected || ctx->droprec >= 0)) {
  326. /*
  327. * Overwrite the record sequence number. We strictly number them in
  328. * the order received. Since we are actually a reliable transport
  329. * we know that there won't be any re-ordering. We overwrite to deal
  330. * with any packets that have been injected
  331. */
  332. for (rem = thispkt->len, rec = thispkt->data; rem > 0; rem -= len) {
  333. if (rem < DTLS1_RT_HEADER_LENGTH)
  334. return -1;
  335. epoch = (rec[EPOCH_HI] << 8) | rec[EPOCH_LO];
  336. if (epoch != ctx->epoch) {
  337. ctx->epoch = epoch;
  338. ctx->currrec = 0;
  339. }
  340. seq = ctx->currrec;
  341. offset = 0;
  342. do {
  343. rec[RECORD_SEQUENCE - offset] = seq & 0xFF;
  344. seq >>= 8;
  345. offset++;
  346. } while (seq > 0);
  347. len = ((rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO])
  348. + DTLS1_RT_HEADER_LENGTH;
  349. if (rem < (int)len)
  350. return -1;
  351. if (ctx->droprec == (int)ctx->currrec && ctx->dropepoch == epoch) {
  352. if (rem > (int)len)
  353. memmove(rec, rec + len, rem - len);
  354. outl -= len;
  355. ctx->droprec = -1;
  356. if (outl == 0)
  357. BIO_set_retry_read(bio);
  358. } else {
  359. rec += len;
  360. }
  361. ctx->currrec++;
  362. }
  363. }
  364. memcpy(out, thispkt->data, outl);
  365. mempacket_free(thispkt);
  366. return outl;
  367. }
  368. int mempacket_test_inject(BIO *bio, const char *in, int inl, int pktnum,
  369. int type)
  370. {
  371. MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
  372. MEMPACKET *thispkt, *looppkt, *nextpkt;
  373. int i;
  374. if (ctx == NULL)
  375. return -1;
  376. /* We only allow injection before we've started writing any data */
  377. if (pktnum >= 0) {
  378. if (ctx->noinject)
  379. return -1;
  380. ctx->injected = 1;
  381. } else {
  382. ctx->noinject = 1;
  383. }
  384. if (!TEST_ptr(thispkt = OPENSSL_malloc(sizeof(*thispkt))))
  385. return -1;
  386. if (!TEST_ptr(thispkt->data = OPENSSL_malloc(inl))) {
  387. mempacket_free(thispkt);
  388. return -1;
  389. }
  390. memcpy(thispkt->data, in, inl);
  391. thispkt->len = inl;
  392. thispkt->num = (pktnum >= 0) ? (unsigned int)pktnum : ctx->lastpkt;
  393. thispkt->type = type;
  394. for(i = 0; (looppkt = sk_MEMPACKET_value(ctx->pkts, i)) != NULL; i++) {
  395. /* Check if we found the right place to insert this packet */
  396. if (looppkt->num > thispkt->num) {
  397. if (sk_MEMPACKET_insert(ctx->pkts, thispkt, i) == 0) {
  398. mempacket_free(thispkt);
  399. return -1;
  400. }
  401. /* If we're doing up front injection then we're done */
  402. if (pktnum >= 0)
  403. return inl;
  404. /*
  405. * We need to do some accounting on lastpkt. We increment it first,
  406. * but it might now equal the value of injected packets, so we need
  407. * to skip over those
  408. */
  409. ctx->lastpkt++;
  410. do {
  411. i++;
  412. nextpkt = sk_MEMPACKET_value(ctx->pkts, i);
  413. if (nextpkt != NULL && nextpkt->num == ctx->lastpkt)
  414. ctx->lastpkt++;
  415. else
  416. return inl;
  417. } while(1);
  418. } else if (looppkt->num == thispkt->num) {
  419. if (!ctx->noinject) {
  420. /* We injected two packets with the same packet number! */
  421. return -1;
  422. }
  423. ctx->lastpkt++;
  424. thispkt->num++;
  425. }
  426. }
  427. /*
  428. * We didn't find any packets with a packet number equal to or greater than
  429. * this one, so we just add it onto the end
  430. */
  431. if (!sk_MEMPACKET_push(ctx->pkts, thispkt)) {
  432. mempacket_free(thispkt);
  433. return -1;
  434. }
  435. if (pktnum < 0)
  436. ctx->lastpkt++;
  437. return inl;
  438. }
  439. static int mempacket_test_write(BIO *bio, const char *in, int inl)
  440. {
  441. return mempacket_test_inject(bio, in, inl, -1, STANDARD_PACKET);
  442. }
  443. static long mempacket_test_ctrl(BIO *bio, int cmd, long num, void *ptr)
  444. {
  445. long ret = 1;
  446. MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
  447. MEMPACKET *thispkt;
  448. switch (cmd) {
  449. case BIO_CTRL_EOF:
  450. ret = (long)(sk_MEMPACKET_num(ctx->pkts) == 0);
  451. break;
  452. case BIO_CTRL_GET_CLOSE:
  453. ret = BIO_get_shutdown(bio);
  454. break;
  455. case BIO_CTRL_SET_CLOSE:
  456. BIO_set_shutdown(bio, (int)num);
  457. break;
  458. case BIO_CTRL_WPENDING:
  459. ret = 0L;
  460. break;
  461. case BIO_CTRL_PENDING:
  462. thispkt = sk_MEMPACKET_value(ctx->pkts, 0);
  463. if (thispkt == NULL)
  464. ret = 0;
  465. else
  466. ret = thispkt->len;
  467. break;
  468. case BIO_CTRL_FLUSH:
  469. ret = 1;
  470. break;
  471. case MEMPACKET_CTRL_SET_DROP_EPOCH:
  472. ctx->dropepoch = (unsigned int)num;
  473. break;
  474. case MEMPACKET_CTRL_SET_DROP_REC:
  475. ctx->droprec = (int)num;
  476. break;
  477. case MEMPACKET_CTRL_GET_DROP_REC:
  478. ret = ctx->droprec;
  479. break;
  480. case BIO_CTRL_RESET:
  481. case BIO_CTRL_DUP:
  482. case BIO_CTRL_PUSH:
  483. case BIO_CTRL_POP:
  484. default:
  485. ret = 0;
  486. break;
  487. }
  488. return ret;
  489. }
  490. static int mempacket_test_gets(BIO *bio, char *buf, int size)
  491. {
  492. /* We don't support this - not needed anyway */
  493. return -1;
  494. }
  495. static int mempacket_test_puts(BIO *bio, const char *str)
  496. {
  497. return mempacket_test_write(bio, str, strlen(str));
  498. }
  499. int create_ssl_ctx_pair(const SSL_METHOD *sm, const SSL_METHOD *cm,
  500. int min_proto_version, int max_proto_version,
  501. SSL_CTX **sctx, SSL_CTX **cctx, char *certfile,
  502. char *privkeyfile)
  503. {
  504. SSL_CTX *serverctx = NULL;
  505. SSL_CTX *clientctx = NULL;
  506. if (!TEST_ptr(serverctx = SSL_CTX_new(sm))
  507. || (cctx != NULL && !TEST_ptr(clientctx = SSL_CTX_new(cm))))
  508. goto err;
  509. if ((min_proto_version > 0
  510. && !TEST_true(SSL_CTX_set_min_proto_version(serverctx,
  511. min_proto_version)))
  512. || (max_proto_version > 0
  513. && !TEST_true(SSL_CTX_set_max_proto_version(serverctx,
  514. max_proto_version))))
  515. goto err;
  516. if (clientctx != NULL
  517. && ((min_proto_version > 0
  518. && !TEST_true(SSL_CTX_set_min_proto_version(clientctx,
  519. min_proto_version)))
  520. || (max_proto_version > 0
  521. && !TEST_true(SSL_CTX_set_max_proto_version(clientctx,
  522. max_proto_version)))))
  523. goto err;
  524. if (certfile != NULL && privkeyfile != NULL) {
  525. if (!TEST_int_eq(SSL_CTX_use_certificate_file(serverctx, certfile,
  526. SSL_FILETYPE_PEM), 1)
  527. || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(serverctx,
  528. privkeyfile,
  529. SSL_FILETYPE_PEM), 1)
  530. || !TEST_int_eq(SSL_CTX_check_private_key(serverctx), 1))
  531. goto err;
  532. }
  533. #ifndef OPENSSL_NO_DH
  534. SSL_CTX_set_dh_auto(serverctx, 1);
  535. #endif
  536. *sctx = serverctx;
  537. if (cctx != NULL)
  538. *cctx = clientctx;
  539. return 1;
  540. err:
  541. SSL_CTX_free(serverctx);
  542. SSL_CTX_free(clientctx);
  543. return 0;
  544. }
  545. #define MAXLOOPS 1000000
  546. /*
  547. * NOTE: Transfers control of the BIOs - this function will free them on error
  548. */
  549. int create_ssl_objects(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl,
  550. SSL **cssl, BIO *s_to_c_fbio, BIO *c_to_s_fbio)
  551. {
  552. SSL *serverssl = NULL, *clientssl = NULL;
  553. BIO *s_to_c_bio = NULL, *c_to_s_bio = NULL;
  554. if (*sssl != NULL)
  555. serverssl = *sssl;
  556. else if (!TEST_ptr(serverssl = SSL_new(serverctx)))
  557. goto error;
  558. if (*cssl != NULL)
  559. clientssl = *cssl;
  560. else if (!TEST_ptr(clientssl = SSL_new(clientctx)))
  561. goto error;
  562. if (SSL_is_dtls(clientssl)) {
  563. if (!TEST_ptr(s_to_c_bio = BIO_new(bio_s_mempacket_test()))
  564. || !TEST_ptr(c_to_s_bio = BIO_new(bio_s_mempacket_test())))
  565. goto error;
  566. } else {
  567. if (!TEST_ptr(s_to_c_bio = BIO_new(BIO_s_mem()))
  568. || !TEST_ptr(c_to_s_bio = BIO_new(BIO_s_mem())))
  569. goto error;
  570. }
  571. if (s_to_c_fbio != NULL
  572. && !TEST_ptr(s_to_c_bio = BIO_push(s_to_c_fbio, s_to_c_bio)))
  573. goto error;
  574. if (c_to_s_fbio != NULL
  575. && !TEST_ptr(c_to_s_bio = BIO_push(c_to_s_fbio, c_to_s_bio)))
  576. goto error;
  577. /* Set Non-blocking IO behaviour */
  578. BIO_set_mem_eof_return(s_to_c_bio, -1);
  579. BIO_set_mem_eof_return(c_to_s_bio, -1);
  580. /* Up ref these as we are passing them to two SSL objects */
  581. SSL_set_bio(serverssl, c_to_s_bio, s_to_c_bio);
  582. BIO_up_ref(s_to_c_bio);
  583. BIO_up_ref(c_to_s_bio);
  584. SSL_set_bio(clientssl, s_to_c_bio, c_to_s_bio);
  585. *sssl = serverssl;
  586. *cssl = clientssl;
  587. return 1;
  588. error:
  589. SSL_free(serverssl);
  590. SSL_free(clientssl);
  591. BIO_free(s_to_c_bio);
  592. BIO_free(c_to_s_bio);
  593. BIO_free(s_to_c_fbio);
  594. BIO_free(c_to_s_fbio);
  595. return 0;
  596. }
  597. int create_ssl_connection(SSL *serverssl, SSL *clientssl, int want)
  598. {
  599. int retc = -1, rets = -1, err, abortctr = 0, i;
  600. int clienterr = 0, servererr = 0;
  601. unsigned char buf;
  602. size_t readbytes;
  603. int isdtls = SSL_is_dtls(serverssl);
  604. do {
  605. err = SSL_ERROR_WANT_WRITE;
  606. while (!clienterr && retc <= 0 && err == SSL_ERROR_WANT_WRITE) {
  607. retc = SSL_connect(clientssl);
  608. if (retc <= 0)
  609. err = SSL_get_error(clientssl, retc);
  610. }
  611. if (!clienterr && retc <= 0 && err != SSL_ERROR_WANT_READ) {
  612. TEST_info("SSL_connect() failed %d, %d", retc, err);
  613. clienterr = 1;
  614. }
  615. if (want != SSL_ERROR_NONE && err == want)
  616. return 0;
  617. err = SSL_ERROR_WANT_WRITE;
  618. while (!servererr && rets <= 0 && err == SSL_ERROR_WANT_WRITE) {
  619. rets = SSL_accept(serverssl);
  620. if (rets <= 0)
  621. err = SSL_get_error(serverssl, rets);
  622. }
  623. if (!servererr && rets <= 0 && err != SSL_ERROR_WANT_READ) {
  624. TEST_info("SSL_accept() failed %d, %d", rets, err);
  625. servererr = 1;
  626. }
  627. if (want != SSL_ERROR_NONE && err == want)
  628. return 0;
  629. if (clienterr && servererr)
  630. return 0;
  631. if (isdtls) {
  632. if (rets > 0 && retc <= 0)
  633. DTLSv1_handle_timeout(serverssl);
  634. if (retc > 0 && rets <= 0)
  635. DTLSv1_handle_timeout(clientssl);
  636. }
  637. if (++abortctr == MAXLOOPS) {
  638. TEST_info("No progress made");
  639. return 0;
  640. }
  641. if (isdtls && abortctr <= 50 && (abortctr % 10) == 0) {
  642. /*
  643. * It looks like we're just spinning. Pause for a short period to
  644. * give the DTLS timer a chance to do something. We only do this for
  645. * the first few times to prevent hangs.
  646. */
  647. ossl_sleep(50);
  648. }
  649. } while (retc <=0 || rets <= 0);
  650. /*
  651. * We attempt to read some data on the client side which we expect to fail.
  652. * This will ensure we have received the NewSessionTicket in TLSv1.3 where
  653. * appropriate. We do this twice because there are 2 NewSesionTickets.
  654. */
  655. for (i = 0; i < 2; i++) {
  656. if (SSL_read_ex(clientssl, &buf, sizeof(buf), &readbytes) > 0) {
  657. if (!TEST_ulong_eq(readbytes, 0))
  658. return 0;
  659. } else if (!TEST_int_eq(SSL_get_error(clientssl, 0),
  660. SSL_ERROR_WANT_READ)) {
  661. return 0;
  662. }
  663. }
  664. return 1;
  665. }
  666. void shutdown_ssl_connection(SSL *serverssl, SSL *clientssl)
  667. {
  668. SSL_shutdown(clientssl);
  669. SSL_shutdown(serverssl);
  670. SSL_free(serverssl);
  671. SSL_free(clientssl);
  672. }