bio_ssl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /* ssl/bio_ssl.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <stdio.h>
  59. #include <stdlib.h>
  60. #include <string.h>
  61. #include <errno.h>
  62. #include <openssl/crypto.h>
  63. #include <openssl/bio.h>
  64. #include <openssl/err.h>
  65. #include <openssl/ssl.h>
  66. static int ssl_write(BIO *h, const char *buf, int num);
  67. static int ssl_read(BIO *h, char *buf, int size);
  68. static int ssl_puts(BIO *h, const char *str);
  69. static long ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  70. static int ssl_new(BIO *h);
  71. static int ssl_free(BIO *data);
  72. static long ssl_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
  73. typedef struct bio_ssl_st
  74. {
  75. SSL *ssl; /* The ssl handle :-) */
  76. /* re-negotiate every time the total number of bytes is this size */
  77. int num_renegotiates;
  78. unsigned long renegotiate_count;
  79. unsigned long byte_count;
  80. unsigned long renegotiate_timeout;
  81. unsigned long last_time;
  82. } BIO_SSL;
  83. static BIO_METHOD methods_sslp=
  84. {
  85. BIO_TYPE_SSL,"ssl",
  86. ssl_write,
  87. ssl_read,
  88. ssl_puts,
  89. NULL, /* ssl_gets, */
  90. ssl_ctrl,
  91. ssl_new,
  92. ssl_free,
  93. ssl_callback_ctrl,
  94. };
  95. BIO_METHOD *BIO_f_ssl(void)
  96. {
  97. return(&methods_sslp);
  98. }
  99. static int ssl_new(BIO *bi)
  100. {
  101. BIO_SSL *bs;
  102. bs=(BIO_SSL *)OPENSSL_malloc(sizeof(BIO_SSL));
  103. if (bs == NULL)
  104. {
  105. BIOerr(BIO_F_SSL_NEW,ERR_R_MALLOC_FAILURE);
  106. return(0);
  107. }
  108. memset(bs,0,sizeof(BIO_SSL));
  109. bi->init=0;
  110. bi->ptr=(char *)bs;
  111. bi->flags=0;
  112. return(1);
  113. }
  114. static int ssl_free(BIO *a)
  115. {
  116. BIO_SSL *bs;
  117. if (a == NULL) return(0);
  118. bs=(BIO_SSL *)a->ptr;
  119. if (bs->ssl != NULL) SSL_shutdown(bs->ssl);
  120. if (a->shutdown)
  121. {
  122. if (a->init && (bs->ssl != NULL))
  123. SSL_free(bs->ssl);
  124. a->init=0;
  125. a->flags=0;
  126. }
  127. if (a->ptr != NULL)
  128. OPENSSL_free(a->ptr);
  129. return(1);
  130. }
  131. static int ssl_read(BIO *b, char *out, int outl)
  132. {
  133. int ret=1;
  134. BIO_SSL *sb;
  135. SSL *ssl;
  136. int retry_reason=0;
  137. int r=0;
  138. if (out == NULL) return(0);
  139. sb=(BIO_SSL *)b->ptr;
  140. ssl=sb->ssl;
  141. BIO_clear_retry_flags(b);
  142. #if 0
  143. if (!SSL_is_init_finished(ssl))
  144. {
  145. /* ret=SSL_do_handshake(ssl); */
  146. if (ret > 0)
  147. {
  148. outflags=(BIO_FLAGS_READ|BIO_FLAGS_SHOULD_RETRY);
  149. ret= -1;
  150. goto end;
  151. }
  152. }
  153. #endif
  154. /* if (ret > 0) */
  155. ret=SSL_read(ssl,out,outl);
  156. switch (SSL_get_error(ssl,ret))
  157. {
  158. case SSL_ERROR_NONE:
  159. if (ret <= 0) break;
  160. if (sb->renegotiate_count > 0)
  161. {
  162. sb->byte_count+=ret;
  163. if (sb->byte_count > sb->renegotiate_count)
  164. {
  165. sb->byte_count=0;
  166. sb->num_renegotiates++;
  167. SSL_renegotiate(ssl);
  168. r=1;
  169. }
  170. }
  171. if ((sb->renegotiate_timeout > 0) && (!r))
  172. {
  173. unsigned long tm;
  174. tm=(unsigned long)time(NULL);
  175. if (tm > sb->last_time+sb->renegotiate_timeout)
  176. {
  177. sb->last_time=tm;
  178. sb->num_renegotiates++;
  179. SSL_renegotiate(ssl);
  180. }
  181. }
  182. break;
  183. case SSL_ERROR_WANT_READ:
  184. BIO_set_retry_read(b);
  185. break;
  186. case SSL_ERROR_WANT_WRITE:
  187. BIO_set_retry_write(b);
  188. break;
  189. case SSL_ERROR_WANT_X509_LOOKUP:
  190. BIO_set_retry_special(b);
  191. retry_reason=BIO_RR_SSL_X509_LOOKUP;
  192. break;
  193. case SSL_ERROR_WANT_ACCEPT:
  194. BIO_set_retry_special(b);
  195. retry_reason=BIO_RR_ACCEPT;
  196. break;
  197. case SSL_ERROR_WANT_CONNECT:
  198. BIO_set_retry_special(b);
  199. retry_reason=BIO_RR_CONNECT;
  200. break;
  201. case SSL_ERROR_SYSCALL:
  202. case SSL_ERROR_SSL:
  203. case SSL_ERROR_ZERO_RETURN:
  204. default:
  205. break;
  206. }
  207. b->retry_reason=retry_reason;
  208. return(ret);
  209. }
  210. static int ssl_write(BIO *b, const char *out, int outl)
  211. {
  212. int ret,r=0;
  213. int retry_reason=0;
  214. SSL *ssl;
  215. BIO_SSL *bs;
  216. if (out == NULL) return(0);
  217. bs=(BIO_SSL *)b->ptr;
  218. ssl=bs->ssl;
  219. BIO_clear_retry_flags(b);
  220. /* ret=SSL_do_handshake(ssl);
  221. if (ret > 0) */
  222. ret=SSL_write(ssl,out,outl);
  223. switch (SSL_get_error(ssl,ret))
  224. {
  225. case SSL_ERROR_NONE:
  226. if (ret <= 0) break;
  227. if (bs->renegotiate_count > 0)
  228. {
  229. bs->byte_count+=ret;
  230. if (bs->byte_count > bs->renegotiate_count)
  231. {
  232. bs->byte_count=0;
  233. bs->num_renegotiates++;
  234. SSL_renegotiate(ssl);
  235. r=1;
  236. }
  237. }
  238. if ((bs->renegotiate_timeout > 0) && (!r))
  239. {
  240. unsigned long tm;
  241. tm=(unsigned long)time(NULL);
  242. if (tm > bs->last_time+bs->renegotiate_timeout)
  243. {
  244. bs->last_time=tm;
  245. bs->num_renegotiates++;
  246. SSL_renegotiate(ssl);
  247. }
  248. }
  249. break;
  250. case SSL_ERROR_WANT_WRITE:
  251. BIO_set_retry_write(b);
  252. break;
  253. case SSL_ERROR_WANT_READ:
  254. BIO_set_retry_read(b);
  255. break;
  256. case SSL_ERROR_WANT_X509_LOOKUP:
  257. BIO_set_retry_special(b);
  258. retry_reason=BIO_RR_SSL_X509_LOOKUP;
  259. break;
  260. case SSL_ERROR_WANT_CONNECT:
  261. BIO_set_retry_special(b);
  262. retry_reason=BIO_RR_CONNECT;
  263. case SSL_ERROR_SYSCALL:
  264. case SSL_ERROR_SSL:
  265. default:
  266. break;
  267. }
  268. b->retry_reason=retry_reason;
  269. return(ret);
  270. }
  271. static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr)
  272. {
  273. SSL **sslp,*ssl;
  274. BIO_SSL *bs;
  275. BIO *dbio,*bio;
  276. long ret=1;
  277. bs=(BIO_SSL *)b->ptr;
  278. ssl=bs->ssl;
  279. if ((ssl == NULL) && (cmd != BIO_C_SET_SSL))
  280. return(0);
  281. switch (cmd)
  282. {
  283. case BIO_CTRL_RESET:
  284. SSL_shutdown(ssl);
  285. if (ssl->handshake_func == ssl->method->ssl_connect)
  286. SSL_set_connect_state(ssl);
  287. else if (ssl->handshake_func == ssl->method->ssl_accept)
  288. SSL_set_accept_state(ssl);
  289. SSL_clear(ssl);
  290. if (b->next_bio != NULL)
  291. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  292. else if (ssl->rbio != NULL)
  293. ret=BIO_ctrl(ssl->rbio,cmd,num,ptr);
  294. else
  295. ret=1;
  296. break;
  297. case BIO_CTRL_INFO:
  298. ret=0;
  299. break;
  300. case BIO_C_SSL_MODE:
  301. if (num) /* client mode */
  302. SSL_set_connect_state(ssl);
  303. else
  304. SSL_set_accept_state(ssl);
  305. break;
  306. case BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT:
  307. ret=bs->renegotiate_timeout;
  308. if (num < 60) num=5;
  309. bs->renegotiate_timeout=(unsigned long)num;
  310. bs->last_time=(unsigned long)time(NULL);
  311. break;
  312. case BIO_C_SET_SSL_RENEGOTIATE_BYTES:
  313. ret=bs->renegotiate_count;
  314. if ((long)num >=512)
  315. bs->renegotiate_count=(unsigned long)num;
  316. break;
  317. case BIO_C_GET_SSL_NUM_RENEGOTIATES:
  318. ret=bs->num_renegotiates;
  319. break;
  320. case BIO_C_SET_SSL:
  321. if (ssl != NULL)
  322. ssl_free(b);
  323. b->shutdown=(int)num;
  324. ssl=(SSL *)ptr;
  325. ((BIO_SSL *)b->ptr)->ssl=ssl;
  326. bio=SSL_get_rbio(ssl);
  327. if (bio != NULL)
  328. {
  329. if (b->next_bio != NULL)
  330. BIO_push(bio,b->next_bio);
  331. b->next_bio=bio;
  332. CRYPTO_add(&bio->references,1,CRYPTO_LOCK_BIO);
  333. }
  334. b->init=1;
  335. break;
  336. case BIO_C_GET_SSL:
  337. if (ptr != NULL)
  338. {
  339. sslp=(SSL **)ptr;
  340. *sslp=ssl;
  341. }
  342. else
  343. ret=0;
  344. break;
  345. case BIO_CTRL_GET_CLOSE:
  346. ret=b->shutdown;
  347. break;
  348. case BIO_CTRL_SET_CLOSE:
  349. b->shutdown=(int)num;
  350. break;
  351. case BIO_CTRL_WPENDING:
  352. ret=BIO_ctrl(ssl->wbio,cmd,num,ptr);
  353. break;
  354. case BIO_CTRL_PENDING:
  355. ret=SSL_pending(ssl);
  356. if (ret == 0)
  357. ret=BIO_pending(ssl->rbio);
  358. break;
  359. case BIO_CTRL_FLUSH:
  360. BIO_clear_retry_flags(b);
  361. ret=BIO_ctrl(ssl->wbio,cmd,num,ptr);
  362. BIO_copy_next_retry(b);
  363. break;
  364. case BIO_CTRL_PUSH:
  365. if ((b->next_bio != NULL) && (b->next_bio != ssl->rbio))
  366. {
  367. SSL_set_bio(ssl,b->next_bio,b->next_bio);
  368. CRYPTO_add(&b->next_bio->references,1,CRYPTO_LOCK_BIO);
  369. }
  370. break;
  371. case BIO_CTRL_POP:
  372. /* ugly bit of a hack */
  373. if (ssl->rbio != ssl->wbio) /* we are in trouble :-( */
  374. {
  375. BIO_free_all(ssl->wbio);
  376. }
  377. ssl->wbio=NULL;
  378. ssl->rbio=NULL;
  379. break;
  380. case BIO_C_DO_STATE_MACHINE:
  381. BIO_clear_retry_flags(b);
  382. b->retry_reason=0;
  383. ret=(int)SSL_do_handshake(ssl);
  384. switch (SSL_get_error(ssl,(int)ret))
  385. {
  386. case SSL_ERROR_WANT_READ:
  387. BIO_set_flags(b,
  388. BIO_FLAGS_READ|BIO_FLAGS_SHOULD_RETRY);
  389. break;
  390. case SSL_ERROR_WANT_WRITE:
  391. BIO_set_flags(b,
  392. BIO_FLAGS_WRITE|BIO_FLAGS_SHOULD_RETRY);
  393. break;
  394. case SSL_ERROR_WANT_CONNECT:
  395. BIO_set_flags(b,
  396. BIO_FLAGS_IO_SPECIAL|BIO_FLAGS_SHOULD_RETRY);
  397. b->retry_reason=b->next_bio->retry_reason;
  398. break;
  399. default:
  400. break;
  401. }
  402. break;
  403. case BIO_CTRL_DUP:
  404. dbio=(BIO *)ptr;
  405. if (((BIO_SSL *)dbio->ptr)->ssl != NULL)
  406. SSL_free(((BIO_SSL *)dbio->ptr)->ssl);
  407. ((BIO_SSL *)dbio->ptr)->ssl=SSL_dup(ssl);
  408. ((BIO_SSL *)dbio->ptr)->renegotiate_count=
  409. ((BIO_SSL *)b->ptr)->renegotiate_count;
  410. ((BIO_SSL *)dbio->ptr)->byte_count=
  411. ((BIO_SSL *)b->ptr)->byte_count;
  412. ((BIO_SSL *)dbio->ptr)->renegotiate_timeout=
  413. ((BIO_SSL *)b->ptr)->renegotiate_timeout;
  414. ((BIO_SSL *)dbio->ptr)->last_time=
  415. ((BIO_SSL *)b->ptr)->last_time;
  416. ret=(((BIO_SSL *)dbio->ptr)->ssl != NULL);
  417. break;
  418. case BIO_C_GET_FD:
  419. ret=BIO_ctrl(ssl->rbio,cmd,num,ptr);
  420. break;
  421. case BIO_CTRL_SET_CALLBACK:
  422. {
  423. #if 0 /* FIXME: Should this be used? -- Richard Levitte */
  424. BIOerr(SSL_F_SSL_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  425. ret = -1;
  426. #else
  427. ret=0;
  428. #endif
  429. }
  430. break;
  431. case BIO_CTRL_GET_CALLBACK:
  432. {
  433. void (**fptr)();
  434. fptr=(void (**)())ptr;
  435. *fptr=SSL_get_info_callback(ssl);
  436. }
  437. break;
  438. default:
  439. ret=BIO_ctrl(ssl->rbio,cmd,num,ptr);
  440. break;
  441. }
  442. return(ret);
  443. }
  444. static long ssl_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
  445. {
  446. SSL *ssl;
  447. BIO_SSL *bs;
  448. long ret=1;
  449. bs=(BIO_SSL *)b->ptr;
  450. ssl=bs->ssl;
  451. switch (cmd)
  452. {
  453. case BIO_CTRL_SET_CALLBACK:
  454. {
  455. SSL_set_info_callback(ssl,fp);
  456. }
  457. break;
  458. default:
  459. ret=BIO_callback_ctrl(ssl->rbio,cmd,fp);
  460. break;
  461. }
  462. return(ret);
  463. }
  464. static int ssl_puts(BIO *bp, const char *str)
  465. {
  466. int n,ret;
  467. n=strlen(str);
  468. ret=BIO_write(bp,str,n);
  469. return(ret);
  470. }
  471. BIO *BIO_new_buffer_ssl_connect(SSL_CTX *ctx)
  472. {
  473. BIO *ret=NULL,*buf=NULL,*ssl=NULL;
  474. if ((buf=BIO_new(BIO_f_buffer())) == NULL)
  475. return(NULL);
  476. if ((ssl=BIO_new_ssl_connect(ctx)) == NULL)
  477. goto err;
  478. if ((ret=BIO_push(buf,ssl)) == NULL)
  479. goto err;
  480. return(ret);
  481. err:
  482. if (buf != NULL) BIO_free(buf);
  483. if (ssl != NULL) BIO_free(ssl);
  484. return(NULL);
  485. }
  486. BIO *BIO_new_ssl_connect(SSL_CTX *ctx)
  487. {
  488. BIO *ret=NULL,*con=NULL,*ssl=NULL;
  489. if ((con=BIO_new(BIO_s_connect())) == NULL)
  490. return(NULL);
  491. if ((ssl=BIO_new_ssl(ctx,1)) == NULL)
  492. goto err;
  493. if ((ret=BIO_push(ssl,con)) == NULL)
  494. goto err;
  495. return(ret);
  496. err:
  497. if (con != NULL) BIO_free(con);
  498. if (ret != NULL) BIO_free(ret);
  499. return(NULL);
  500. }
  501. BIO *BIO_new_ssl(SSL_CTX *ctx, int client)
  502. {
  503. BIO *ret;
  504. SSL *ssl;
  505. if ((ret=BIO_new(BIO_f_ssl())) == NULL)
  506. return(NULL);
  507. if ((ssl=SSL_new(ctx)) == NULL)
  508. {
  509. BIO_free(ret);
  510. return(NULL);
  511. }
  512. if (client)
  513. SSL_set_connect_state(ssl);
  514. else
  515. SSL_set_accept_state(ssl);
  516. BIO_set_ssl(ret,ssl,BIO_CLOSE);
  517. return(ret);
  518. }
  519. int BIO_ssl_copy_session_id(BIO *t, BIO *f)
  520. {
  521. t=BIO_find_type(t,BIO_TYPE_SSL);
  522. f=BIO_find_type(f,BIO_TYPE_SSL);
  523. if ((t == NULL) || (f == NULL))
  524. return(0);
  525. if ( (((BIO_SSL *)t->ptr)->ssl == NULL) ||
  526. (((BIO_SSL *)f->ptr)->ssl == NULL))
  527. return(0);
  528. SSL_copy_session_id(((BIO_SSL *)t->ptr)->ssl,((BIO_SSL *)f->ptr)->ssl);
  529. return(1);
  530. }
  531. void BIO_ssl_shutdown(BIO *b)
  532. {
  533. SSL *s;
  534. while (b != NULL)
  535. {
  536. if (b->method->type == BIO_TYPE_SSL)
  537. {
  538. s=((BIO_SSL *)b->ptr)->ssl;
  539. SSL_shutdown(s);
  540. break;
  541. }
  542. b=b->next_bio;
  543. }
  544. }