ssltestlib.c 43 KB

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