ssltestlib.c 36 KB

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