ssltestlib.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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(OPENSSL_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_with_libctx(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_with_libctx(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. #ifndef OPENSSL_NO_DH
  648. SSL_CTX_set_dh_auto(serverctx, 1);
  649. #endif
  650. *sctx = serverctx;
  651. if (cctx != NULL)
  652. *cctx = clientctx;
  653. return 1;
  654. err:
  655. if (*sctx == NULL)
  656. SSL_CTX_free(serverctx);
  657. if (cctx != NULL && *cctx == NULL)
  658. SSL_CTX_free(clientctx);
  659. return 0;
  660. }
  661. #define MAXLOOPS 1000000
  662. #if !defined(OPENSSL_NO_KTLS) && !defined(OPENSSL_NO_SOCK)
  663. static int set_nb(int fd)
  664. {
  665. int flags;
  666. flags = fcntl(fd,F_GETFL,0);
  667. if (flags == -1)
  668. return flags;
  669. flags = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
  670. return flags;
  671. }
  672. int create_test_sockets(int *cfd, int *sfd)
  673. {
  674. struct sockaddr_in sin;
  675. const char *host = "127.0.0.1";
  676. int cfd_connected = 0, ret = 0;
  677. socklen_t slen = sizeof(sin);
  678. int afd = -1;
  679. *cfd = -1;
  680. *sfd = -1;
  681. memset ((char *) &sin, 0, sizeof(sin));
  682. sin.sin_family = AF_INET;
  683. sin.sin_addr.s_addr = inet_addr(host);
  684. afd = socket(AF_INET, SOCK_STREAM, 0);
  685. if (afd < 0)
  686. return 0;
  687. if (bind(afd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
  688. goto out;
  689. if (getsockname(afd, (struct sockaddr*)&sin, &slen) < 0)
  690. goto out;
  691. if (listen(afd, 1) < 0)
  692. goto out;
  693. *cfd = socket(AF_INET, SOCK_STREAM, 0);
  694. if (*cfd < 0)
  695. goto out;
  696. if (set_nb(afd) == -1)
  697. goto out;
  698. while (*sfd == -1 || !cfd_connected ) {
  699. *sfd = accept(afd, NULL, 0);
  700. if (*sfd == -1 && errno != EAGAIN)
  701. goto out;
  702. if (!cfd_connected && connect(*cfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
  703. goto out;
  704. else
  705. cfd_connected = 1;
  706. }
  707. if (set_nb(*cfd) == -1 || set_nb(*sfd) == -1)
  708. goto out;
  709. ret = 1;
  710. goto success;
  711. out:
  712. if (*cfd != -1)
  713. close(*cfd);
  714. if (*sfd != -1)
  715. close(*sfd);
  716. success:
  717. if (afd != -1)
  718. close(afd);
  719. return ret;
  720. }
  721. int create_ssl_objects2(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl,
  722. SSL **cssl, int sfd, int cfd)
  723. {
  724. SSL *serverssl = NULL, *clientssl = NULL;
  725. BIO *s_to_c_bio = NULL, *c_to_s_bio = NULL;
  726. if (*sssl != NULL)
  727. serverssl = *sssl;
  728. else if (!TEST_ptr(serverssl = SSL_new(serverctx)))
  729. goto error;
  730. if (*cssl != NULL)
  731. clientssl = *cssl;
  732. else if (!TEST_ptr(clientssl = SSL_new(clientctx)))
  733. goto error;
  734. if (!TEST_ptr(s_to_c_bio = BIO_new_socket(sfd, BIO_NOCLOSE))
  735. || !TEST_ptr(c_to_s_bio = BIO_new_socket(cfd, BIO_NOCLOSE)))
  736. goto error;
  737. SSL_set_bio(clientssl, c_to_s_bio, c_to_s_bio);
  738. SSL_set_bio(serverssl, s_to_c_bio, s_to_c_bio);
  739. *sssl = serverssl;
  740. *cssl = clientssl;
  741. return 1;
  742. error:
  743. SSL_free(serverssl);
  744. SSL_free(clientssl);
  745. BIO_free(s_to_c_bio);
  746. BIO_free(c_to_s_bio);
  747. return 0;
  748. }
  749. #endif
  750. /*
  751. * NOTE: Transfers control of the BIOs - this function will free them on error
  752. */
  753. int create_ssl_objects(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl,
  754. SSL **cssl, BIO *s_to_c_fbio, BIO *c_to_s_fbio)
  755. {
  756. SSL *serverssl = NULL, *clientssl = NULL;
  757. BIO *s_to_c_bio = NULL, *c_to_s_bio = NULL;
  758. if (*sssl != NULL)
  759. serverssl = *sssl;
  760. else if (!TEST_ptr(serverssl = SSL_new(serverctx)))
  761. goto error;
  762. if (*cssl != NULL)
  763. clientssl = *cssl;
  764. else if (!TEST_ptr(clientssl = SSL_new(clientctx)))
  765. goto error;
  766. if (SSL_is_dtls(clientssl)) {
  767. if (!TEST_ptr(s_to_c_bio = BIO_new(bio_s_mempacket_test()))
  768. || !TEST_ptr(c_to_s_bio = BIO_new(bio_s_mempacket_test())))
  769. goto error;
  770. } else {
  771. if (!TEST_ptr(s_to_c_bio = BIO_new(BIO_s_mem()))
  772. || !TEST_ptr(c_to_s_bio = BIO_new(BIO_s_mem())))
  773. goto error;
  774. }
  775. if (s_to_c_fbio != NULL
  776. && !TEST_ptr(s_to_c_bio = BIO_push(s_to_c_fbio, s_to_c_bio)))
  777. goto error;
  778. if (c_to_s_fbio != NULL
  779. && !TEST_ptr(c_to_s_bio = BIO_push(c_to_s_fbio, c_to_s_bio)))
  780. goto error;
  781. /* Set Non-blocking IO behaviour */
  782. BIO_set_mem_eof_return(s_to_c_bio, -1);
  783. BIO_set_mem_eof_return(c_to_s_bio, -1);
  784. /* Up ref these as we are passing them to two SSL objects */
  785. SSL_set_bio(serverssl, c_to_s_bio, s_to_c_bio);
  786. BIO_up_ref(s_to_c_bio);
  787. BIO_up_ref(c_to_s_bio);
  788. SSL_set_bio(clientssl, s_to_c_bio, c_to_s_bio);
  789. *sssl = serverssl;
  790. *cssl = clientssl;
  791. return 1;
  792. error:
  793. SSL_free(serverssl);
  794. SSL_free(clientssl);
  795. BIO_free(s_to_c_bio);
  796. BIO_free(c_to_s_bio);
  797. BIO_free(s_to_c_fbio);
  798. BIO_free(c_to_s_fbio);
  799. return 0;
  800. }
  801. /*
  802. * Create an SSL connection, but does not read any post-handshake
  803. * NewSessionTicket messages.
  804. * If |read| is set and we're using DTLS then we will attempt to SSL_read on
  805. * the connection once we've completed one half of it, to ensure any retransmits
  806. * get triggered.
  807. * We stop the connection attempt (and return a failure value) if either peer
  808. * has SSL_get_error() return the value in the |want| parameter. The connection
  809. * attempt could be restarted by a subsequent call to this function.
  810. */
  811. int create_bare_ssl_connection(SSL *serverssl, SSL *clientssl, int want,
  812. int read)
  813. {
  814. int retc = -1, rets = -1, err, abortctr = 0;
  815. int clienterr = 0, servererr = 0;
  816. int isdtls = SSL_is_dtls(serverssl);
  817. do {
  818. err = SSL_ERROR_WANT_WRITE;
  819. while (!clienterr && retc <= 0 && err == SSL_ERROR_WANT_WRITE) {
  820. retc = SSL_connect(clientssl);
  821. if (retc <= 0)
  822. err = SSL_get_error(clientssl, retc);
  823. }
  824. if (!clienterr && retc <= 0 && err != SSL_ERROR_WANT_READ) {
  825. TEST_info("SSL_connect() failed %d, %d", retc, err);
  826. if (want != SSL_ERROR_SSL)
  827. TEST_openssl_errors();
  828. clienterr = 1;
  829. }
  830. if (want != SSL_ERROR_NONE && err == want)
  831. return 0;
  832. err = SSL_ERROR_WANT_WRITE;
  833. while (!servererr && rets <= 0 && err == SSL_ERROR_WANT_WRITE) {
  834. rets = SSL_accept(serverssl);
  835. if (rets <= 0)
  836. err = SSL_get_error(serverssl, rets);
  837. }
  838. if (!servererr && rets <= 0
  839. && err != SSL_ERROR_WANT_READ
  840. && err != SSL_ERROR_WANT_X509_LOOKUP) {
  841. TEST_info("SSL_accept() failed %d, %d", rets, err);
  842. if (want != SSL_ERROR_SSL)
  843. TEST_openssl_errors();
  844. servererr = 1;
  845. }
  846. if (want != SSL_ERROR_NONE && err == want)
  847. return 0;
  848. if (clienterr && servererr)
  849. return 0;
  850. if (isdtls && read) {
  851. unsigned char buf[20];
  852. /* Trigger any retransmits that may be appropriate */
  853. if (rets > 0 && retc <= 0) {
  854. if (SSL_read(serverssl, buf, sizeof(buf)) > 0) {
  855. /* We don't expect this to succeed! */
  856. TEST_info("Unexpected SSL_read() success!");
  857. return 0;
  858. }
  859. }
  860. if (retc > 0 && rets <= 0) {
  861. if (SSL_read(clientssl, buf, sizeof(buf)) > 0) {
  862. /* We don't expect this to succeed! */
  863. TEST_info("Unexpected SSL_read() success!");
  864. return 0;
  865. }
  866. }
  867. }
  868. if (++abortctr == MAXLOOPS) {
  869. TEST_info("No progress made");
  870. return 0;
  871. }
  872. if (isdtls && abortctr <= 50 && (abortctr % 10) == 0) {
  873. /*
  874. * It looks like we're just spinning. Pause for a short period to
  875. * give the DTLS timer a chance to do something. We only do this for
  876. * the first few times to prevent hangs.
  877. */
  878. ossl_sleep(50);
  879. }
  880. } while (retc <=0 || rets <= 0);
  881. return 1;
  882. }
  883. /*
  884. * Create an SSL connection including any post handshake NewSessionTicket
  885. * messages.
  886. */
  887. int create_ssl_connection(SSL *serverssl, SSL *clientssl, int want)
  888. {
  889. int i;
  890. unsigned char buf;
  891. size_t readbytes;
  892. if (!create_bare_ssl_connection(serverssl, clientssl, want, 1))
  893. return 0;
  894. /*
  895. * We attempt to read some data on the client side which we expect to fail.
  896. * This will ensure we have received the NewSessionTicket in TLSv1.3 where
  897. * appropriate. We do this twice because there are 2 NewSessionTickets.
  898. */
  899. for (i = 0; i < 2; i++) {
  900. if (SSL_read_ex(clientssl, &buf, sizeof(buf), &readbytes) > 0) {
  901. if (!TEST_ulong_eq(readbytes, 0))
  902. return 0;
  903. } else if (!TEST_int_eq(SSL_get_error(clientssl, 0),
  904. SSL_ERROR_WANT_READ)) {
  905. return 0;
  906. }
  907. }
  908. return 1;
  909. }
  910. void shutdown_ssl_connection(SSL *serverssl, SSL *clientssl)
  911. {
  912. SSL_shutdown(clientssl);
  913. SSL_shutdown(serverssl);
  914. SSL_free(serverssl);
  915. SSL_free(clientssl);
  916. }