ssltestlib.c 20 KB

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