ssltestlib.c 31 KB

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