bio_lib.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /* crypto/bio/bio_lib.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 <errno.h>
  60. #include <openssl/crypto.h>
  61. #include "cryptlib.h"
  62. #include <openssl/bio.h>
  63. #include <openssl/stack.h>
  64. BIO *BIO_new(BIO_METHOD *method)
  65. {
  66. BIO *ret=NULL;
  67. ret=(BIO *)OPENSSL_malloc(sizeof(BIO));
  68. if (ret == NULL)
  69. {
  70. BIOerr(BIO_F_BIO_NEW,ERR_R_MALLOC_FAILURE);
  71. return(NULL);
  72. }
  73. if (!BIO_set(ret,method))
  74. {
  75. OPENSSL_free(ret);
  76. ret=NULL;
  77. }
  78. return(ret);
  79. }
  80. int BIO_set(BIO *bio, BIO_METHOD *method)
  81. {
  82. bio->method=method;
  83. bio->callback=NULL;
  84. bio->cb_arg=NULL;
  85. bio->init=0;
  86. bio->shutdown=1;
  87. bio->flags=0;
  88. bio->retry_reason=0;
  89. bio->num=0;
  90. bio->ptr=NULL;
  91. bio->prev_bio=NULL;
  92. bio->next_bio=NULL;
  93. bio->references=1;
  94. bio->num_read=0L;
  95. bio->num_write=0L;
  96. CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
  97. if (method->create != NULL)
  98. if (!method->create(bio))
  99. {
  100. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio,
  101. &bio->ex_data);
  102. return(0);
  103. }
  104. return(1);
  105. }
  106. int BIO_free(BIO *a)
  107. {
  108. int ret=0,i;
  109. if (a == NULL) return(0);
  110. i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_BIO);
  111. #ifdef REF_PRINT
  112. REF_PRINT("BIO",a);
  113. #endif
  114. if (i > 0) return(1);
  115. #ifdef REF_CHECK
  116. if (i < 0)
  117. {
  118. fprintf(stderr,"BIO_free, bad reference count\n");
  119. abort();
  120. }
  121. #endif
  122. if ((a->callback != NULL) &&
  123. ((i=(int)a->callback(a,BIO_CB_FREE,NULL,0,0L,1L)) <= 0))
  124. return(i);
  125. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
  126. if ((a->method == NULL) || (a->method->destroy == NULL)) return(1);
  127. ret=a->method->destroy(a);
  128. OPENSSL_free(a);
  129. return(1);
  130. }
  131. void BIO_vfree(BIO *a)
  132. { BIO_free(a); }
  133. void BIO_clear_flags(BIO *b, int flags)
  134. {
  135. b->flags &= ~flags;
  136. }
  137. int BIO_test_flags(const BIO *b, int flags)
  138. {
  139. return (b->flags & flags);
  140. }
  141. void BIO_set_flags(BIO *b, int flags)
  142. {
  143. b->flags |= flags;
  144. }
  145. long (*BIO_get_callback(const BIO *b))(struct bio_st *,int,const char *,int, long,long)
  146. {
  147. return b->callback;
  148. }
  149. void BIO_set_callback(BIO *b, long (*cb)(struct bio_st *,int,const char *,int, long,long))
  150. {
  151. b->callback = cb;
  152. }
  153. void BIO_set_callback_arg(BIO *b, char *arg)
  154. {
  155. b->cb_arg = arg;
  156. }
  157. char * BIO_get_callback_arg(const BIO *b)
  158. {
  159. return b->cb_arg;
  160. }
  161. const char * BIO_method_name(const BIO *b)
  162. {
  163. return b->method->name;
  164. }
  165. int BIO_method_type(const BIO *b)
  166. {
  167. return b->method->type;
  168. }
  169. int BIO_read(BIO *b, void *out, int outl)
  170. {
  171. int i;
  172. long (*cb)(BIO *,int,const char *,int,long,long);
  173. if ((b == NULL) || (b->method == NULL) || (b->method->bread == NULL))
  174. {
  175. BIOerr(BIO_F_BIO_READ,BIO_R_UNSUPPORTED_METHOD);
  176. return(-2);
  177. }
  178. cb=b->callback;
  179. if ((cb != NULL) &&
  180. ((i=(int)cb(b,BIO_CB_READ,out,outl,0L,1L)) <= 0))
  181. return(i);
  182. if (!b->init)
  183. {
  184. BIOerr(BIO_F_BIO_READ,BIO_R_UNINITIALIZED);
  185. return(-2);
  186. }
  187. i=b->method->bread(b,out,outl);
  188. if (i > 0) b->num_read+=(unsigned long)i;
  189. if (cb != NULL)
  190. i=(int)cb(b,BIO_CB_READ|BIO_CB_RETURN,out,outl,
  191. 0L,(long)i);
  192. return(i);
  193. }
  194. int BIO_write(BIO *b, const void *in, int inl)
  195. {
  196. int i;
  197. long (*cb)(BIO *,int,const char *,int,long,long);
  198. if (b == NULL)
  199. return(0);
  200. cb=b->callback;
  201. if ((b->method == NULL) || (b->method->bwrite == NULL))
  202. {
  203. BIOerr(BIO_F_BIO_WRITE,BIO_R_UNSUPPORTED_METHOD);
  204. return(-2);
  205. }
  206. if ((cb != NULL) &&
  207. ((i=(int)cb(b,BIO_CB_WRITE,in,inl,0L,1L)) <= 0))
  208. return(i);
  209. if (!b->init)
  210. {
  211. BIOerr(BIO_F_BIO_WRITE,BIO_R_UNINITIALIZED);
  212. return(-2);
  213. }
  214. i=b->method->bwrite(b,in,inl);
  215. if (i > 0) b->num_write+=(unsigned long)i;
  216. if (cb != NULL)
  217. i=(int)cb(b,BIO_CB_WRITE|BIO_CB_RETURN,in,inl,
  218. 0L,(long)i);
  219. return(i);
  220. }
  221. int BIO_puts(BIO *b, const char *in)
  222. {
  223. int i;
  224. long (*cb)(BIO *,int,const char *,int,long,long);
  225. if ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL))
  226. {
  227. BIOerr(BIO_F_BIO_PUTS,BIO_R_UNSUPPORTED_METHOD);
  228. return(-2);
  229. }
  230. cb=b->callback;
  231. if ((cb != NULL) &&
  232. ((i=(int)cb(b,BIO_CB_PUTS,in,0,0L,1L)) <= 0))
  233. return(i);
  234. if (!b->init)
  235. {
  236. BIOerr(BIO_F_BIO_PUTS,BIO_R_UNINITIALIZED);
  237. return(-2);
  238. }
  239. i=b->method->bputs(b,in);
  240. if (i > 0) b->num_write+=(unsigned long)i;
  241. if (cb != NULL)
  242. i=(int)cb(b,BIO_CB_PUTS|BIO_CB_RETURN,in,0,
  243. 0L,(long)i);
  244. return(i);
  245. }
  246. int BIO_gets(BIO *b, char *in, int inl)
  247. {
  248. int i;
  249. long (*cb)(BIO *,int,const char *,int,long,long);
  250. if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL))
  251. {
  252. BIOerr(BIO_F_BIO_GETS,BIO_R_UNSUPPORTED_METHOD);
  253. return(-2);
  254. }
  255. cb=b->callback;
  256. if ((cb != NULL) &&
  257. ((i=(int)cb(b,BIO_CB_GETS,in,inl,0L,1L)) <= 0))
  258. return(i);
  259. if (!b->init)
  260. {
  261. BIOerr(BIO_F_BIO_GETS,BIO_R_UNINITIALIZED);
  262. return(-2);
  263. }
  264. i=b->method->bgets(b,in,inl);
  265. if (cb != NULL)
  266. i=(int)cb(b,BIO_CB_GETS|BIO_CB_RETURN,in,inl,
  267. 0L,(long)i);
  268. return(i);
  269. }
  270. int BIO_indent(BIO *b,int indent,int max)
  271. {
  272. if(indent < 0)
  273. indent=0;
  274. if(indent > max)
  275. indent=max;
  276. while(indent--)
  277. if(BIO_puts(b," ") != 1)
  278. return 0;
  279. return 1;
  280. }
  281. long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
  282. {
  283. int i;
  284. i=iarg;
  285. return(BIO_ctrl(b,cmd,larg,(char *)&i));
  286. }
  287. char *BIO_ptr_ctrl(BIO *b, int cmd, long larg)
  288. {
  289. char *p=NULL;
  290. if (BIO_ctrl(b,cmd,larg,(char *)&p) <= 0)
  291. return(NULL);
  292. else
  293. return(p);
  294. }
  295. long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
  296. {
  297. long ret;
  298. long (*cb)(BIO *,int,const char *,int,long,long);
  299. if (b == NULL) return(0);
  300. if ((b->method == NULL) || (b->method->ctrl == NULL))
  301. {
  302. BIOerr(BIO_F_BIO_CTRL,BIO_R_UNSUPPORTED_METHOD);
  303. return(-2);
  304. }
  305. cb=b->callback;
  306. if ((cb != NULL) &&
  307. ((ret=cb(b,BIO_CB_CTRL,parg,cmd,larg,1L)) <= 0))
  308. return(ret);
  309. ret=b->method->ctrl(b,cmd,larg,parg);
  310. if (cb != NULL)
  311. ret=cb(b,BIO_CB_CTRL|BIO_CB_RETURN,parg,cmd,
  312. larg,ret);
  313. return(ret);
  314. }
  315. long BIO_callback_ctrl(BIO *b, int cmd, void (*fp)(struct bio_st *, int, const char *, int, long, long))
  316. {
  317. long ret;
  318. long (*cb)(BIO *,int,const char *,int,long,long);
  319. if (b == NULL) return(0);
  320. if ((b->method == NULL) || (b->method->callback_ctrl == NULL))
  321. {
  322. BIOerr(BIO_F_BIO_CALLBACK_CTRL,BIO_R_UNSUPPORTED_METHOD);
  323. return(-2);
  324. }
  325. cb=b->callback;
  326. if ((cb != NULL) &&
  327. ((ret=cb(b,BIO_CB_CTRL,(void *)&fp,cmd,0,1L)) <= 0))
  328. return(ret);
  329. ret=b->method->callback_ctrl(b,cmd,fp);
  330. if (cb != NULL)
  331. ret=cb(b,BIO_CB_CTRL|BIO_CB_RETURN,(void *)&fp,cmd,
  332. 0,ret);
  333. return(ret);
  334. }
  335. /* It is unfortunate to duplicate in functions what the BIO_(w)pending macros
  336. * do; but those macros have inappropriate return type, and for interfacing
  337. * from other programming languages, C macros aren't much of a help anyway. */
  338. size_t BIO_ctrl_pending(BIO *bio)
  339. {
  340. return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
  341. }
  342. size_t BIO_ctrl_wpending(BIO *bio)
  343. {
  344. return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL);
  345. }
  346. /* put the 'bio' on the end of b's list of operators */
  347. BIO *BIO_push(BIO *b, BIO *bio)
  348. {
  349. BIO *lb;
  350. if (b == NULL) return(bio);
  351. lb=b;
  352. while (lb->next_bio != NULL)
  353. lb=lb->next_bio;
  354. lb->next_bio=bio;
  355. if (bio != NULL)
  356. bio->prev_bio=lb;
  357. /* called to do internal processing */
  358. BIO_ctrl(b,BIO_CTRL_PUSH,0,NULL);
  359. return(b);
  360. }
  361. /* Remove the first and return the rest */
  362. BIO *BIO_pop(BIO *b)
  363. {
  364. BIO *ret;
  365. if (b == NULL) return(NULL);
  366. ret=b->next_bio;
  367. BIO_ctrl(b,BIO_CTRL_POP,0,NULL);
  368. if (b->prev_bio != NULL)
  369. b->prev_bio->next_bio=b->next_bio;
  370. if (b->next_bio != NULL)
  371. b->next_bio->prev_bio=b->prev_bio;
  372. b->next_bio=NULL;
  373. b->prev_bio=NULL;
  374. return(ret);
  375. }
  376. BIO *BIO_get_retry_BIO(BIO *bio, int *reason)
  377. {
  378. BIO *b,*last;
  379. b=last=bio;
  380. for (;;)
  381. {
  382. if (!BIO_should_retry(b)) break;
  383. last=b;
  384. b=b->next_bio;
  385. if (b == NULL) break;
  386. }
  387. if (reason != NULL) *reason=last->retry_reason;
  388. return(last);
  389. }
  390. int BIO_get_retry_reason(BIO *bio)
  391. {
  392. return(bio->retry_reason);
  393. }
  394. BIO *BIO_find_type(BIO *bio, int type)
  395. {
  396. int mt,mask;
  397. if(!bio) return NULL;
  398. mask=type&0xff;
  399. do {
  400. if (bio->method != NULL)
  401. {
  402. mt=bio->method->type;
  403. if (!mask)
  404. {
  405. if (mt & type) return(bio);
  406. }
  407. else if (mt == type)
  408. return(bio);
  409. }
  410. bio=bio->next_bio;
  411. } while (bio != NULL);
  412. return(NULL);
  413. }
  414. BIO *BIO_next(BIO *b)
  415. {
  416. if(!b) return NULL;
  417. return b->next_bio;
  418. }
  419. void BIO_free_all(BIO *bio)
  420. {
  421. BIO *b;
  422. int ref;
  423. while (bio != NULL)
  424. {
  425. b=bio;
  426. ref=b->references;
  427. bio=bio->next_bio;
  428. BIO_free(b);
  429. /* Since ref count > 1, don't free anyone else. */
  430. if (ref > 1) break;
  431. }
  432. }
  433. BIO *BIO_dup_chain(BIO *in)
  434. {
  435. BIO *ret=NULL,*eoc=NULL,*bio,*new;
  436. for (bio=in; bio != NULL; bio=bio->next_bio)
  437. {
  438. if ((new=BIO_new(bio->method)) == NULL) goto err;
  439. new->callback=bio->callback;
  440. new->cb_arg=bio->cb_arg;
  441. new->init=bio->init;
  442. new->shutdown=bio->shutdown;
  443. new->flags=bio->flags;
  444. /* This will let SSL_s_sock() work with stdin/stdout */
  445. new->num=bio->num;
  446. if (!BIO_dup_state(bio,(char *)new))
  447. {
  448. BIO_free(new);
  449. goto err;
  450. }
  451. /* copy app data */
  452. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new->ex_data,
  453. &bio->ex_data))
  454. goto err;
  455. if (ret == NULL)
  456. {
  457. eoc=new;
  458. ret=eoc;
  459. }
  460. else
  461. {
  462. BIO_push(eoc,new);
  463. eoc=new;
  464. }
  465. }
  466. return(ret);
  467. err:
  468. if (ret != NULL)
  469. BIO_free(ret);
  470. return(NULL);
  471. }
  472. void BIO_copy_next_retry(BIO *b)
  473. {
  474. BIO_set_flags(b,BIO_get_retry_flags(b->next_bio));
  475. b->retry_reason=b->next_bio->retry_reason;
  476. }
  477. int BIO_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
  478. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
  479. {
  480. return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_BIO, argl, argp,
  481. new_func, dup_func, free_func);
  482. }
  483. int BIO_set_ex_data(BIO *bio, int idx, void *data)
  484. {
  485. return(CRYPTO_set_ex_data(&(bio->ex_data),idx,data));
  486. }
  487. void *BIO_get_ex_data(BIO *bio, int idx)
  488. {
  489. return(CRYPTO_get_ex_data(&(bio->ex_data),idx));
  490. }
  491. unsigned long BIO_number_read(BIO *bio)
  492. {
  493. if(bio) return bio->num_read;
  494. return 0;
  495. }
  496. unsigned long BIO_number_written(BIO *bio)
  497. {
  498. if(bio) return bio->num_write;
  499. return 0;
  500. }
  501. IMPLEMENT_STACK_OF(BIO)