2
0

ssltestlib.c 28 KB

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