ssltestlib.c 43 KB

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