ssltestlib.c 31 KB

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