ssltestlib.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. /*
  2. * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 "e_os.h"
  11. #include "ssltestlib.h"
  12. static int tls_dump_new(BIO *bi);
  13. static int tls_dump_free(BIO *a);
  14. static int tls_dump_read(BIO *b, char *out, int outl);
  15. static int tls_dump_write(BIO *b, const char *in, int inl);
  16. static long tls_dump_ctrl(BIO *b, int cmd, long num, void *ptr);
  17. static int tls_dump_gets(BIO *bp, char *buf, int size);
  18. static int tls_dump_puts(BIO *bp, const char *str);
  19. /* Choose a sufficiently large type likely to be unused for this custom BIO */
  20. # define BIO_TYPE_TLS_DUMP_FILTER (0x80 | BIO_TYPE_FILTER)
  21. # define BIO_TYPE_MEMPACKET_TEST 0x81
  22. static BIO_METHOD *method_tls_dump = NULL;
  23. static BIO_METHOD *method_mempacket_test = NULL;
  24. /* Note: Not thread safe! */
  25. const BIO_METHOD *bio_f_tls_dump_filter(void)
  26. {
  27. if (method_tls_dump == NULL) {
  28. method_tls_dump = BIO_meth_new(BIO_TYPE_TLS_DUMP_FILTER,
  29. "TLS dump filter");
  30. if ( method_tls_dump == NULL
  31. || !BIO_meth_set_write(method_tls_dump, tls_dump_write)
  32. || !BIO_meth_set_read(method_tls_dump, tls_dump_read)
  33. || !BIO_meth_set_puts(method_tls_dump, tls_dump_puts)
  34. || !BIO_meth_set_gets(method_tls_dump, tls_dump_gets)
  35. || !BIO_meth_set_ctrl(method_tls_dump, tls_dump_ctrl)
  36. || !BIO_meth_set_create(method_tls_dump, tls_dump_new)
  37. || !BIO_meth_set_destroy(method_tls_dump, tls_dump_free))
  38. return NULL;
  39. }
  40. return method_tls_dump;
  41. }
  42. void bio_f_tls_dump_filter_free(void)
  43. {
  44. BIO_meth_free(method_tls_dump);
  45. }
  46. static int tls_dump_new(BIO *bio)
  47. {
  48. BIO_set_init(bio, 1);
  49. return 1;
  50. }
  51. static int tls_dump_free(BIO *bio)
  52. {
  53. BIO_set_init(bio, 0);
  54. return 1;
  55. }
  56. static void copy_flags(BIO *bio)
  57. {
  58. int flags;
  59. BIO *next = BIO_next(bio);
  60. flags = BIO_test_flags(next, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS);
  61. BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS);
  62. BIO_set_flags(bio, flags);
  63. }
  64. #define RECORD_CONTENT_TYPE 0
  65. #define RECORD_VERSION_HI 1
  66. #define RECORD_VERSION_LO 2
  67. #define RECORD_EPOCH_HI 3
  68. #define RECORD_EPOCH_LO 4
  69. #define RECORD_SEQUENCE_START 5
  70. #define RECORD_SEQUENCE_END 10
  71. #define RECORD_LEN_HI 11
  72. #define RECORD_LEN_LO 12
  73. #define MSG_TYPE 0
  74. #define MSG_LEN_HI 1
  75. #define MSG_LEN_MID 2
  76. #define MSG_LEN_LO 3
  77. #define MSG_SEQ_HI 4
  78. #define MSG_SEQ_LO 5
  79. #define MSG_FRAG_OFF_HI 6
  80. #define MSG_FRAG_OFF_MID 7
  81. #define MSG_FRAG_OFF_LO 8
  82. #define MSG_FRAG_LEN_HI 9
  83. #define MSG_FRAG_LEN_MID 10
  84. #define MSG_FRAG_LEN_LO 11
  85. static void dump_data(const char *data, int len)
  86. {
  87. int rem, i, content, reclen, msglen, fragoff, fraglen, epoch;
  88. unsigned char *rec;
  89. printf("---- START OF PACKET ----\n");
  90. rem = len;
  91. rec = (unsigned char *)data;
  92. while (rem > 0) {
  93. if (rem != len)
  94. printf("*\n");
  95. printf("*---- START OF RECORD ----\n");
  96. if (rem < DTLS1_RT_HEADER_LENGTH) {
  97. printf("*---- RECORD TRUNCATED ----\n");
  98. break;
  99. }
  100. content = rec[RECORD_CONTENT_TYPE];
  101. printf("** Record Content-type: %d\n", content);
  102. printf("** Record Version: %02x%02x\n",
  103. rec[RECORD_VERSION_HI], rec[RECORD_VERSION_LO]);
  104. epoch = (rec[RECORD_EPOCH_HI] << 8) | rec[RECORD_EPOCH_LO];
  105. printf("** Record Epoch: %d\n", epoch);
  106. printf("** Record Sequence: ");
  107. for (i = RECORD_SEQUENCE_START; i <= RECORD_SEQUENCE_END; i++)
  108. printf("%02x", rec[i]);
  109. reclen = (rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO];
  110. printf("\n** Record Length: %d\n", reclen);
  111. /* Now look at message */
  112. rec += DTLS1_RT_HEADER_LENGTH;
  113. rem -= DTLS1_RT_HEADER_LENGTH;
  114. if (content == SSL3_RT_HANDSHAKE) {
  115. printf("**---- START OF HANDSHAKE MESSAGE FRAGMENT ----\n");
  116. if (epoch > 0) {
  117. printf("**---- HANDSHAKE MESSAGE FRAGMENT ENCRYPTED ----\n");
  118. } else if (rem < DTLS1_HM_HEADER_LENGTH
  119. || reclen < DTLS1_HM_HEADER_LENGTH) {
  120. printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n");
  121. } else {
  122. printf("*** Message Type: %d\n", rec[MSG_TYPE]);
  123. msglen = (rec[MSG_LEN_HI] << 16) | (rec[MSG_LEN_MID] << 8)
  124. | rec[MSG_LEN_LO];
  125. printf("*** Message Length: %d\n", msglen);
  126. printf("*** Message sequence: %d\n",
  127. (rec[MSG_SEQ_HI] << 8) | rec[MSG_SEQ_LO]);
  128. fragoff = (rec[MSG_FRAG_OFF_HI] << 16)
  129. | (rec[MSG_FRAG_OFF_MID] << 8)
  130. | rec[MSG_FRAG_OFF_LO];
  131. printf("*** Message Fragment offset: %d\n", fragoff);
  132. fraglen = (rec[MSG_FRAG_LEN_HI] << 16)
  133. | (rec[MSG_FRAG_LEN_MID] << 8)
  134. | rec[MSG_FRAG_LEN_LO];
  135. printf("*** Message Fragment len: %d\n", fraglen);
  136. if (fragoff + fraglen > msglen)
  137. printf("***---- HANDSHAKE MESSAGE FRAGMENT INVALID ----\n");
  138. else if(reclen < fraglen)
  139. printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n");
  140. else
  141. printf("**---- END OF HANDSHAKE MESSAGE FRAGMENT ----\n");
  142. }
  143. }
  144. if (rem < reclen) {
  145. printf("*---- RECORD TRUNCATED ----\n");
  146. rem = 0;
  147. } else {
  148. rec += reclen;
  149. rem -= reclen;
  150. printf("*---- END OF RECORD ----\n");
  151. }
  152. }
  153. printf("---- END OF PACKET ----\n\n");
  154. fflush(stdout);
  155. }
  156. static int tls_dump_read(BIO *bio, char *out, int outl)
  157. {
  158. int ret;
  159. BIO *next = BIO_next(bio);
  160. ret = BIO_read(next, out, outl);
  161. copy_flags(bio);
  162. if (ret > 0) {
  163. dump_data(out, ret);
  164. }
  165. return ret;
  166. }
  167. static int tls_dump_write(BIO *bio, const char *in, int inl)
  168. {
  169. int ret;
  170. BIO *next = BIO_next(bio);
  171. ret = BIO_write(next, in, inl);
  172. copy_flags(bio);
  173. return ret;
  174. }
  175. static long tls_dump_ctrl(BIO *bio, int cmd, long num, void *ptr)
  176. {
  177. long ret;
  178. BIO *next = BIO_next(bio);
  179. if (next == NULL)
  180. return 0;
  181. switch (cmd) {
  182. case BIO_CTRL_DUP:
  183. ret = 0L;
  184. break;
  185. default:
  186. ret = BIO_ctrl(next, cmd, num, ptr);
  187. break;
  188. }
  189. return ret;
  190. }
  191. static int tls_dump_gets(BIO *bio, char *buf, int size)
  192. {
  193. /* We don't support this - not needed anyway */
  194. return -1;
  195. }
  196. static int tls_dump_puts(BIO *bio, const char *str)
  197. {
  198. return tls_dump_write(bio, str, strlen(str));
  199. }
  200. struct mempacket_st {
  201. unsigned char *data;
  202. int len;
  203. unsigned int num;
  204. unsigned int type;
  205. };
  206. static void mempacket_free(MEMPACKET *pkt)
  207. {
  208. if (pkt->data != NULL)
  209. OPENSSL_free(pkt->data);
  210. OPENSSL_free(pkt);
  211. }
  212. typedef struct mempacket_test_ctx_st {
  213. STACK_OF(MEMPACKET) *pkts;
  214. unsigned int epoch;
  215. unsigned int currrec;
  216. unsigned int currpkt;
  217. unsigned int lastpkt;
  218. unsigned int noinject;
  219. } MEMPACKET_TEST_CTX;
  220. static int mempacket_test_new(BIO *bi);
  221. static int mempacket_test_free(BIO *a);
  222. static int mempacket_test_read(BIO *b, char *out, int outl);
  223. static int mempacket_test_write(BIO *b, const char *in, int inl);
  224. static long mempacket_test_ctrl(BIO *b, int cmd, long num, void *ptr);
  225. static int mempacket_test_gets(BIO *bp, char *buf, int size);
  226. static int mempacket_test_puts(BIO *bp, const char *str);
  227. const BIO_METHOD *bio_s_mempacket_test(void)
  228. {
  229. if (method_mempacket_test == NULL) {
  230. method_mempacket_test = BIO_meth_new(BIO_TYPE_MEMPACKET_TEST,
  231. "Mem Packet Test");
  232. if ( method_mempacket_test == NULL
  233. || !BIO_meth_set_write(method_mempacket_test, mempacket_test_write)
  234. || !BIO_meth_set_read(method_mempacket_test, mempacket_test_read)
  235. || !BIO_meth_set_puts(method_mempacket_test, mempacket_test_puts)
  236. || !BIO_meth_set_gets(method_mempacket_test, mempacket_test_gets)
  237. || !BIO_meth_set_ctrl(method_mempacket_test, mempacket_test_ctrl)
  238. || !BIO_meth_set_create(method_mempacket_test, mempacket_test_new)
  239. || !BIO_meth_set_destroy(method_mempacket_test, mempacket_test_free))
  240. return NULL;
  241. }
  242. return method_mempacket_test;
  243. }
  244. void bio_s_mempacket_test_free(void)
  245. {
  246. BIO_meth_free(method_mempacket_test);
  247. }
  248. static int mempacket_test_new(BIO *bio)
  249. {
  250. MEMPACKET_TEST_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
  251. if (ctx == NULL)
  252. return 0;
  253. ctx->pkts = sk_MEMPACKET_new_null();
  254. if (ctx->pkts == NULL) {
  255. OPENSSL_free(ctx);
  256. return 0;
  257. }
  258. BIO_set_init(bio, 1);
  259. BIO_set_data(bio, ctx);
  260. return 1;
  261. }
  262. static int mempacket_test_free(BIO *bio)
  263. {
  264. MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
  265. sk_MEMPACKET_pop_free(ctx->pkts, mempacket_free);
  266. OPENSSL_free(ctx);
  267. BIO_set_data(bio, NULL);
  268. BIO_set_init(bio, 0);
  269. return 1;
  270. }
  271. /* Record Header values */
  272. #define EPOCH_HI 4
  273. #define EPOCH_LO 5
  274. #define RECORD_SEQUENCE 10
  275. #define RECORD_LEN_HI 11
  276. #define RECORD_LEN_LO 12
  277. #define STANDARD_PACKET 0
  278. static int mempacket_test_read(BIO *bio, char *out, int outl)
  279. {
  280. MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
  281. MEMPACKET *thispkt;
  282. unsigned char *rec;
  283. int rem;
  284. unsigned int seq, offset, len, epoch;
  285. BIO_clear_retry_flags(bio);
  286. thispkt = sk_MEMPACKET_value(ctx->pkts, 0);
  287. if (thispkt == NULL || thispkt->num != ctx->currpkt) {
  288. /* Probably run out of data */
  289. BIO_set_retry_read(bio);
  290. return -1;
  291. }
  292. (void)sk_MEMPACKET_shift(ctx->pkts);
  293. ctx->currpkt++;
  294. if (outl > thispkt->len)
  295. outl = thispkt->len;
  296. if (thispkt->type != INJECT_PACKET_IGNORE_REC_SEQ) {
  297. /*
  298. * Overwrite the record sequence number. We strictly number them in
  299. * the order received. Since we are actually a reliable transport
  300. * we know that there won't be any re-ordering. We overwrite to deal
  301. * with any packets that have been injected
  302. */
  303. rem = thispkt->len;
  304. rec = thispkt->data;
  305. while (rem > 0) {
  306. if (rem < DTLS1_RT_HEADER_LENGTH) {
  307. return -1;
  308. }
  309. epoch = (rec[EPOCH_HI] << 8) | rec[EPOCH_LO];
  310. if (epoch != ctx->epoch) {
  311. ctx->epoch = epoch;
  312. ctx->currrec = 0;
  313. }
  314. seq = ctx->currrec;
  315. offset = 0;
  316. do {
  317. rec[RECORD_SEQUENCE - offset] = seq & 0xFF;
  318. seq >>= 8;
  319. offset++;
  320. } while (seq > 0);
  321. ctx->currrec++;
  322. len = ((rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO])
  323. + DTLS1_RT_HEADER_LENGTH;
  324. rec += len;
  325. rem -= len;
  326. }
  327. }
  328. memcpy(out, thispkt->data, outl);
  329. mempacket_free(thispkt);
  330. return outl;
  331. }
  332. int mempacket_test_inject(BIO *bio, const char *in, int inl, int pktnum,
  333. int type)
  334. {
  335. MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
  336. MEMPACKET *thispkt, *looppkt, *nextpkt;
  337. int i;
  338. if (ctx == NULL)
  339. return -1;
  340. /* We only allow injection before we've started writing any data */
  341. if (pktnum >= 0) {
  342. if (ctx->noinject)
  343. return -1;
  344. } else {
  345. ctx->noinject = 1;
  346. }
  347. thispkt = OPENSSL_malloc(sizeof(MEMPACKET));
  348. if (thispkt == NULL)
  349. return -1;
  350. thispkt->data = OPENSSL_malloc(inl);
  351. if (thispkt->data == NULL) {
  352. mempacket_free(thispkt);
  353. return -1;
  354. }
  355. memcpy(thispkt->data, in, inl);
  356. thispkt->len = inl;
  357. thispkt->num = (pktnum >= 0) ? (unsigned int)pktnum : ctx->lastpkt;
  358. thispkt->type = type;
  359. for(i = 0; (looppkt = sk_MEMPACKET_value(ctx->pkts, i)) != NULL; i++) {
  360. /* Check if we found the right place to insert this packet */
  361. if (looppkt->num > thispkt->num) {
  362. if (sk_MEMPACKET_insert(ctx->pkts, thispkt, i) == 0) {
  363. mempacket_free(thispkt);
  364. return -1;
  365. }
  366. /* If we're doing up front injection then we're done */
  367. if (pktnum >= 0)
  368. return inl;
  369. /*
  370. * We need to do some accounting on lastpkt. We increment it first,
  371. * but it might now equal the value of injected packets, so we need
  372. * to skip over those
  373. */
  374. ctx->lastpkt++;
  375. do {
  376. i++;
  377. nextpkt = sk_MEMPACKET_value(ctx->pkts, i);
  378. if (nextpkt != NULL && nextpkt->num == ctx->lastpkt)
  379. ctx->lastpkt++;
  380. else
  381. return inl;
  382. } while(1);
  383. } else if(looppkt->num == thispkt->num) {
  384. if (!ctx->noinject) {
  385. /* We injected two packets with the same packet number! */
  386. return -1;
  387. }
  388. ctx->lastpkt++;
  389. thispkt->num++;
  390. }
  391. }
  392. /*
  393. * We didn't find any packets with a packet number equal to or greater than
  394. * this one, so we just add it onto the end
  395. */
  396. if (!sk_MEMPACKET_push(ctx->pkts, thispkt)) {
  397. mempacket_free(thispkt);
  398. return -1;
  399. }
  400. if (pktnum < 0)
  401. ctx->lastpkt++;
  402. return inl;
  403. }
  404. static int mempacket_test_write(BIO *bio, const char *in, int inl)
  405. {
  406. return mempacket_test_inject(bio, in, inl, -1, STANDARD_PACKET);
  407. }
  408. static long mempacket_test_ctrl(BIO *bio, int cmd, long num, void *ptr)
  409. {
  410. long ret = 1;
  411. MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
  412. MEMPACKET *thispkt;
  413. switch (cmd) {
  414. case BIO_CTRL_EOF:
  415. ret = (long)(sk_MEMPACKET_num(ctx->pkts) == 0);
  416. break;
  417. case BIO_CTRL_GET_CLOSE:
  418. ret = BIO_get_shutdown(bio);
  419. break;
  420. case BIO_CTRL_SET_CLOSE:
  421. BIO_set_shutdown(bio, (int)num);
  422. break;
  423. case BIO_CTRL_WPENDING:
  424. ret = 0L;
  425. break;
  426. case BIO_CTRL_PENDING:
  427. thispkt = sk_MEMPACKET_value(ctx->pkts, 0);
  428. if (thispkt == NULL)
  429. ret = 0;
  430. else
  431. ret = thispkt->len;
  432. break;
  433. case BIO_CTRL_FLUSH:
  434. ret = 1;
  435. break;
  436. case BIO_CTRL_RESET:
  437. case BIO_CTRL_DUP:
  438. case BIO_CTRL_PUSH:
  439. case BIO_CTRL_POP:
  440. default:
  441. ret = 0;
  442. break;
  443. }
  444. return ret;
  445. }
  446. static int mempacket_test_gets(BIO *bio, char *buf, int size)
  447. {
  448. /* We don't support this - not needed anyway */
  449. return -1;
  450. }
  451. static int mempacket_test_puts(BIO *bio, const char *str)
  452. {
  453. return mempacket_test_write(bio, str, strlen(str));
  454. }
  455. int create_ssl_ctx_pair(const SSL_METHOD *sm, const SSL_METHOD *cm,
  456. int min_proto_version, int max_proto_version,
  457. SSL_CTX **sctx, SSL_CTX **cctx, char *certfile,
  458. char *privkeyfile)
  459. {
  460. SSL_CTX *serverctx = NULL;
  461. SSL_CTX *clientctx = NULL;
  462. serverctx = SSL_CTX_new(sm);
  463. if (cctx != NULL)
  464. clientctx = SSL_CTX_new(cm);
  465. if (serverctx == NULL || (cctx != NULL && clientctx == NULL)) {
  466. printf("Failed to create SSL_CTX\n");
  467. goto err;
  468. }
  469. if (min_proto_version > 0
  470. && !SSL_CTX_set_min_proto_version(serverctx, min_proto_version)) {
  471. printf("Unable to set server min protocol versions\n");
  472. goto err;
  473. }
  474. if (max_proto_version > 0
  475. && !SSL_CTX_set_max_proto_version(serverctx, max_proto_version)) {
  476. printf("Unable to set server max protocol versions\n");
  477. goto err;
  478. }
  479. if (clientctx != NULL) {
  480. if (min_proto_version > 0
  481. && !SSL_CTX_set_max_proto_version(clientctx, max_proto_version)) {
  482. printf("Unable to set client max protocol versions\n");
  483. goto err;
  484. }
  485. if (max_proto_version > 0
  486. && !SSL_CTX_set_min_proto_version(clientctx, min_proto_version)) {
  487. printf("Unable to set client min protocol versions\n");
  488. goto err;
  489. }
  490. }
  491. if (SSL_CTX_use_certificate_file(serverctx, certfile,
  492. SSL_FILETYPE_PEM) <= 0) {
  493. printf("Failed to load server certificate\n");
  494. goto err;
  495. }
  496. if (SSL_CTX_use_PrivateKey_file(serverctx, privkeyfile,
  497. SSL_FILETYPE_PEM) <= 0) {
  498. printf("Failed to load server private key\n");
  499. }
  500. if (SSL_CTX_check_private_key(serverctx) <= 0) {
  501. printf("Failed to check private key\n");
  502. goto err;
  503. }
  504. #ifndef OPENSSL_NO_DH
  505. SSL_CTX_set_dh_auto(serverctx, 1);
  506. #endif
  507. *sctx = serverctx;
  508. if (cctx != NULL)
  509. *cctx = clientctx;
  510. return 1;
  511. err:
  512. SSL_CTX_free(serverctx);
  513. SSL_CTX_free(clientctx);
  514. return 0;
  515. }
  516. #define MAXLOOPS 100000
  517. /*
  518. * NOTE: Transfers control of the BIOs - this function will free them on error
  519. */
  520. int create_ssl_objects(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl,
  521. SSL **cssl, BIO *s_to_c_fbio, BIO *c_to_s_fbio)
  522. {
  523. SSL *serverssl, *clientssl;
  524. BIO *s_to_c_bio = NULL, *c_to_s_bio = NULL;
  525. if (*sssl == NULL)
  526. serverssl = SSL_new(serverctx);
  527. else
  528. serverssl = *sssl;
  529. if (*cssl == NULL)
  530. clientssl = SSL_new(clientctx);
  531. else
  532. clientssl = *cssl;
  533. if (serverssl == NULL || clientssl == NULL) {
  534. printf("Failed to create SSL object\n");
  535. goto error;
  536. }
  537. if (SSL_is_dtls(clientssl)) {
  538. s_to_c_bio = BIO_new(bio_s_mempacket_test());
  539. c_to_s_bio = BIO_new(bio_s_mempacket_test());
  540. } else {
  541. s_to_c_bio = BIO_new(BIO_s_mem());
  542. c_to_s_bio = BIO_new(BIO_s_mem());
  543. }
  544. if (s_to_c_bio == NULL || c_to_s_bio == NULL) {
  545. printf("Failed to create mem BIOs\n");
  546. goto error;
  547. }
  548. if (s_to_c_fbio != NULL)
  549. s_to_c_bio = BIO_push(s_to_c_fbio, s_to_c_bio);
  550. if (c_to_s_fbio != NULL)
  551. c_to_s_bio = BIO_push(c_to_s_fbio, c_to_s_bio);
  552. if (s_to_c_bio == NULL || c_to_s_bio == NULL) {
  553. printf("Failed to create chained BIOs\n");
  554. goto error;
  555. }
  556. /* Set Non-blocking IO behaviour */
  557. BIO_set_mem_eof_return(s_to_c_bio, -1);
  558. BIO_set_mem_eof_return(c_to_s_bio, -1);
  559. /* Up ref these as we are passing them to two SSL objects */
  560. BIO_up_ref(s_to_c_bio);
  561. BIO_up_ref(c_to_s_bio);
  562. SSL_set_bio(serverssl, c_to_s_bio, s_to_c_bio);
  563. SSL_set_bio(clientssl, s_to_c_bio, c_to_s_bio);
  564. /* BIOs will now be freed when SSL objects are freed */
  565. s_to_c_bio = c_to_s_bio = NULL;
  566. s_to_c_fbio = c_to_s_fbio = NULL;
  567. *sssl = serverssl;
  568. *cssl = clientssl;
  569. return 1;
  570. error:
  571. SSL_free(serverssl);
  572. SSL_free(clientssl);
  573. BIO_free(s_to_c_bio);
  574. BIO_free(c_to_s_bio);
  575. BIO_free(s_to_c_fbio);
  576. BIO_free(c_to_s_fbio);
  577. return 0;
  578. }
  579. int create_ssl_connection(SSL *serverssl, SSL *clientssl)
  580. {
  581. int retc = -1, rets = -1, err, abortctr = 0;
  582. int clienterr = 0, servererr = 0;
  583. do {
  584. err = SSL_ERROR_WANT_WRITE;
  585. while (!clienterr && retc <= 0 && err == SSL_ERROR_WANT_WRITE) {
  586. retc = SSL_connect(clientssl);
  587. if (retc <= 0)
  588. err = SSL_get_error(clientssl, retc);
  589. }
  590. if (!clienterr && retc <= 0 && err != SSL_ERROR_WANT_READ) {
  591. printf("SSL_connect() failed %d, %d\n", retc, err);
  592. clienterr = 1;
  593. }
  594. err = SSL_ERROR_WANT_WRITE;
  595. while (!servererr && rets <= 0 && err == SSL_ERROR_WANT_WRITE) {
  596. rets = SSL_accept(serverssl);
  597. if (rets <= 0)
  598. err = SSL_get_error(serverssl, rets);
  599. }
  600. if (!servererr && rets <= 0 && err != SSL_ERROR_WANT_READ) {
  601. printf("SSL_accept() failed %d, %d\n", retc, err);
  602. servererr = 1;
  603. }
  604. if (clienterr && servererr)
  605. return 0;
  606. if (++abortctr == MAXLOOPS) {
  607. printf("No progress made\n");
  608. return 0;
  609. }
  610. } while (retc <=0 || rets <= 0);
  611. return 1;
  612. }