2
0

bio_ok.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /* crypto/evp/bio_ok.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. /*
  59. From: Arne Ansper <arne@cyber.ee>
  60. Why BIO_f_reliable?
  61. I wrote function which took BIO* as argument, read data from it
  62. and processed it. Then I wanted to store the input file in
  63. encrypted form. OK I pushed BIO_f_cipher to the BIO stack
  64. and everything was OK. BUT if user types wrong password
  65. BIO_f_cipher outputs only garbage and my function crashes. Yes
  66. I can and I should fix my function, but BIO_f_cipher is
  67. easy way to add encryption support to many existing applications
  68. and it's hard to debug and fix them all.
  69. So I wanted another BIO which would catch the incorrect passwords and
  70. file damages which cause garbage on BIO_f_cipher's output.
  71. The easy way is to push the BIO_f_md and save the checksum at
  72. the end of the file. However there are several problems with this
  73. approach:
  74. 1) you must somehow separate checksum from actual data.
  75. 2) you need lot's of memory when reading the file, because you
  76. must read to the end of the file and verify the checksum before
  77. letting the application to read the data.
  78. BIO_f_reliable tries to solve both problems, so that you can
  79. read and write arbitrary long streams using only fixed amount
  80. of memory.
  81. BIO_f_reliable splits data stream into blocks. Each block is prefixed
  82. with it's length and suffixed with it's digest. So you need only
  83. several Kbytes of memory to buffer single block before verifying
  84. it's digest.
  85. BIO_f_reliable goes further and adds several important capabilities:
  86. 1) the digest of the block is computed over the whole stream
  87. -- so nobody can rearrange the blocks or remove or replace them.
  88. 2) to detect invalid passwords right at the start BIO_f_reliable
  89. adds special prefix to the stream. In order to avoid known plain-text
  90. attacks this prefix is generated as follows:
  91. *) digest is initialized with random seed instead of
  92. standardized one.
  93. *) same seed is written to output
  94. *) well-known text is then hashed and the output
  95. of the digest is also written to output.
  96. reader can now read the seed from stream, hash the same string
  97. and then compare the digest output.
  98. Bad things: BIO_f_reliable knows what's going on in EVP_Digest. I
  99. initially wrote and tested this code on x86 machine and wrote the
  100. digests out in machine-dependent order :( There are people using
  101. this code and I cannot change this easily without making existing
  102. data files unreadable.
  103. */
  104. #include <stdio.h>
  105. #include <errno.h>
  106. #include <assert.h>
  107. #include "cryptlib.h"
  108. #include <openssl/buffer.h>
  109. #include <openssl/bio.h>
  110. #include <openssl/evp.h>
  111. #include <openssl/rand.h>
  112. static int ok_write(BIO *h, const char *buf, int num);
  113. static int ok_read(BIO *h, char *buf, int size);
  114. static long ok_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  115. static int ok_new(BIO *h);
  116. static int ok_free(BIO *data);
  117. static long ok_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
  118. static void sig_out(BIO* b);
  119. static void sig_in(BIO* b);
  120. static void block_out(BIO* b);
  121. static void block_in(BIO* b);
  122. #define OK_BLOCK_SIZE (1024*4)
  123. #define OK_BLOCK_BLOCK 4
  124. #define IOBS (OK_BLOCK_SIZE+ OK_BLOCK_BLOCK+ 3*EVP_MAX_MD_SIZE)
  125. #define WELLKNOWN "The quick brown fox jumped over the lazy dog's back."
  126. typedef struct ok_struct
  127. {
  128. size_t buf_len;
  129. size_t buf_off;
  130. size_t buf_len_save;
  131. size_t buf_off_save;
  132. int cont; /* <= 0 when finished */
  133. int finished;
  134. EVP_MD_CTX md;
  135. int blockout; /* output block is ready */
  136. int sigio; /* must process signature */
  137. unsigned char buf[IOBS];
  138. } BIO_OK_CTX;
  139. static BIO_METHOD methods_ok=
  140. {
  141. BIO_TYPE_CIPHER,"reliable",
  142. ok_write,
  143. ok_read,
  144. NULL, /* ok_puts, */
  145. NULL, /* ok_gets, */
  146. ok_ctrl,
  147. ok_new,
  148. ok_free,
  149. ok_callback_ctrl,
  150. };
  151. BIO_METHOD *BIO_f_reliable(void)
  152. {
  153. return(&methods_ok);
  154. }
  155. static int ok_new(BIO *bi)
  156. {
  157. BIO_OK_CTX *ctx;
  158. ctx=(BIO_OK_CTX *)OPENSSL_malloc(sizeof(BIO_OK_CTX));
  159. if (ctx == NULL) return(0);
  160. ctx->buf_len=0;
  161. ctx->buf_off=0;
  162. ctx->buf_len_save=0;
  163. ctx->buf_off_save=0;
  164. ctx->cont=1;
  165. ctx->finished=0;
  166. ctx->blockout= 0;
  167. ctx->sigio=1;
  168. EVP_MD_CTX_init(&ctx->md);
  169. bi->init=0;
  170. bi->ptr=(char *)ctx;
  171. bi->flags=0;
  172. return(1);
  173. }
  174. static int ok_free(BIO *a)
  175. {
  176. if (a == NULL) return(0);
  177. EVP_MD_CTX_cleanup(&((BIO_OK_CTX *)a->ptr)->md);
  178. OPENSSL_cleanse(a->ptr,sizeof(BIO_OK_CTX));
  179. OPENSSL_free(a->ptr);
  180. a->ptr=NULL;
  181. a->init=0;
  182. a->flags=0;
  183. return(1);
  184. }
  185. static int ok_read(BIO *b, char *out, int outl)
  186. {
  187. int ret=0,i,n;
  188. BIO_OK_CTX *ctx;
  189. if (out == NULL) return(0);
  190. ctx=(BIO_OK_CTX *)b->ptr;
  191. if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0)) return(0);
  192. while(outl > 0)
  193. {
  194. /* copy clean bytes to output buffer */
  195. if (ctx->blockout)
  196. {
  197. i=ctx->buf_len-ctx->buf_off;
  198. if (i > outl) i=outl;
  199. memcpy(out,&(ctx->buf[ctx->buf_off]),i);
  200. ret+=i;
  201. out+=i;
  202. outl-=i;
  203. ctx->buf_off+=i;
  204. /* all clean bytes are out */
  205. if (ctx->buf_len == ctx->buf_off)
  206. {
  207. ctx->buf_off=0;
  208. /* copy start of the next block into proper place */
  209. if(ctx->buf_len_save- ctx->buf_off_save > 0)
  210. {
  211. ctx->buf_len= ctx->buf_len_save- ctx->buf_off_save;
  212. memmove(ctx->buf, &(ctx->buf[ctx->buf_off_save]),
  213. ctx->buf_len);
  214. }
  215. else
  216. {
  217. ctx->buf_len=0;
  218. }
  219. ctx->blockout= 0;
  220. }
  221. }
  222. /* output buffer full -- cancel */
  223. if (outl == 0) break;
  224. /* no clean bytes in buffer -- fill it */
  225. n=IOBS- ctx->buf_len;
  226. i=BIO_read(b->next_bio,&(ctx->buf[ctx->buf_len]),n);
  227. if (i <= 0) break; /* nothing new */
  228. ctx->buf_len+= i;
  229. /* no signature yet -- check if we got one */
  230. if (ctx->sigio == 1) sig_in(b);
  231. /* signature ok -- check if we got block */
  232. if (ctx->sigio == 0) block_in(b);
  233. /* invalid block -- cancel */
  234. if (ctx->cont <= 0) break;
  235. }
  236. BIO_clear_retry_flags(b);
  237. BIO_copy_next_retry(b);
  238. return(ret);
  239. }
  240. static int ok_write(BIO *b, const char *in, int inl)
  241. {
  242. int ret=0,n,i;
  243. BIO_OK_CTX *ctx;
  244. if (inl <= 0) return inl;
  245. ctx=(BIO_OK_CTX *)b->ptr;
  246. ret=inl;
  247. if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0)) return(0);
  248. if(ctx->sigio) sig_out(b);
  249. do{
  250. BIO_clear_retry_flags(b);
  251. n=ctx->buf_len-ctx->buf_off;
  252. while (ctx->blockout && n > 0)
  253. {
  254. i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
  255. if (i <= 0)
  256. {
  257. BIO_copy_next_retry(b);
  258. if(!BIO_should_retry(b))
  259. ctx->cont= 0;
  260. return(i);
  261. }
  262. ctx->buf_off+=i;
  263. n-=i;
  264. }
  265. /* at this point all pending data has been written */
  266. ctx->blockout= 0;
  267. if (ctx->buf_len == ctx->buf_off)
  268. {
  269. ctx->buf_len=OK_BLOCK_BLOCK;
  270. ctx->buf_off=0;
  271. }
  272. if ((in == NULL) || (inl <= 0)) return(0);
  273. n= (inl+ ctx->buf_len > OK_BLOCK_SIZE+ OK_BLOCK_BLOCK) ?
  274. (int)(OK_BLOCK_SIZE+OK_BLOCK_BLOCK-ctx->buf_len) : inl;
  275. memcpy((unsigned char *)(&(ctx->buf[ctx->buf_len])),(unsigned char *)in,n);
  276. ctx->buf_len+= n;
  277. inl-=n;
  278. in+=n;
  279. if(ctx->buf_len >= OK_BLOCK_SIZE+ OK_BLOCK_BLOCK)
  280. {
  281. block_out(b);
  282. }
  283. }while(inl > 0);
  284. BIO_clear_retry_flags(b);
  285. BIO_copy_next_retry(b);
  286. return(ret);
  287. }
  288. static long ok_ctrl(BIO *b, int cmd, long num, void *ptr)
  289. {
  290. BIO_OK_CTX *ctx;
  291. EVP_MD *md;
  292. const EVP_MD **ppmd;
  293. long ret=1;
  294. int i;
  295. ctx=b->ptr;
  296. switch (cmd)
  297. {
  298. case BIO_CTRL_RESET:
  299. ctx->buf_len=0;
  300. ctx->buf_off=0;
  301. ctx->buf_len_save=0;
  302. ctx->buf_off_save=0;
  303. ctx->cont=1;
  304. ctx->finished=0;
  305. ctx->blockout= 0;
  306. ctx->sigio=1;
  307. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  308. break;
  309. case BIO_CTRL_EOF: /* More to read */
  310. if (ctx->cont <= 0)
  311. ret=1;
  312. else
  313. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  314. break;
  315. case BIO_CTRL_PENDING: /* More to read in buffer */
  316. case BIO_CTRL_WPENDING: /* More to read in buffer */
  317. ret=ctx->blockout ? ctx->buf_len-ctx->buf_off : 0;
  318. if (ret <= 0)
  319. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  320. break;
  321. case BIO_CTRL_FLUSH:
  322. /* do a final write */
  323. if(ctx->blockout == 0)
  324. block_out(b);
  325. while (ctx->blockout)
  326. {
  327. i=ok_write(b,NULL,0);
  328. if (i < 0)
  329. {
  330. ret=i;
  331. break;
  332. }
  333. }
  334. ctx->finished=1;
  335. ctx->buf_off=ctx->buf_len=0;
  336. ctx->cont=(int)ret;
  337. /* Finally flush the underlying BIO */
  338. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  339. break;
  340. case BIO_C_DO_STATE_MACHINE:
  341. BIO_clear_retry_flags(b);
  342. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  343. BIO_copy_next_retry(b);
  344. break;
  345. case BIO_CTRL_INFO:
  346. ret=(long)ctx->cont;
  347. break;
  348. case BIO_C_SET_MD:
  349. md=ptr;
  350. EVP_DigestInit_ex(&ctx->md, md, NULL);
  351. b->init=1;
  352. break;
  353. case BIO_C_GET_MD:
  354. if (b->init)
  355. {
  356. ppmd=ptr;
  357. *ppmd=ctx->md.digest;
  358. }
  359. else
  360. ret=0;
  361. break;
  362. default:
  363. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  364. break;
  365. }
  366. return(ret);
  367. }
  368. static long ok_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
  369. {
  370. long ret=1;
  371. if (b->next_bio == NULL) return(0);
  372. switch (cmd)
  373. {
  374. default:
  375. ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
  376. break;
  377. }
  378. return(ret);
  379. }
  380. static void longswap(void *_ptr, size_t len)
  381. { const union { long one; char little; } is_endian = {1};
  382. if (is_endian.little) {
  383. size_t i;
  384. unsigned char *p=_ptr,c;
  385. for(i= 0;i < len;i+= 4) {
  386. c=p[0],p[0]=p[3],p[3]=c;
  387. c=p[1],p[1]=p[2],p[2]=c;
  388. }
  389. }
  390. }
  391. static void sig_out(BIO* b)
  392. {
  393. BIO_OK_CTX *ctx;
  394. EVP_MD_CTX *md;
  395. ctx=b->ptr;
  396. md=&ctx->md;
  397. if(ctx->buf_len+ 2* md->digest->md_size > OK_BLOCK_SIZE) return;
  398. EVP_DigestInit_ex(md, md->digest, NULL);
  399. /* FIXME: there's absolutely no guarantee this makes any sense at all,
  400. * particularly now EVP_MD_CTX has been restructured.
  401. */
  402. RAND_pseudo_bytes(md->md_data, md->digest->md_size);
  403. memcpy(&(ctx->buf[ctx->buf_len]), md->md_data, md->digest->md_size);
  404. longswap(&(ctx->buf[ctx->buf_len]), md->digest->md_size);
  405. ctx->buf_len+= md->digest->md_size;
  406. EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN));
  407. EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL);
  408. ctx->buf_len+= md->digest->md_size;
  409. ctx->blockout= 1;
  410. ctx->sigio= 0;
  411. }
  412. static void sig_in(BIO* b)
  413. {
  414. BIO_OK_CTX *ctx;
  415. EVP_MD_CTX *md;
  416. unsigned char tmp[EVP_MAX_MD_SIZE];
  417. int ret= 0;
  418. ctx=b->ptr;
  419. md=&ctx->md;
  420. if((int)(ctx->buf_len-ctx->buf_off) < 2*md->digest->md_size) return;
  421. EVP_DigestInit_ex(md, md->digest, NULL);
  422. memcpy(md->md_data, &(ctx->buf[ctx->buf_off]), md->digest->md_size);
  423. longswap(md->md_data, md->digest->md_size);
  424. ctx->buf_off+= md->digest->md_size;
  425. EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN));
  426. EVP_DigestFinal_ex(md, tmp, NULL);
  427. ret= memcmp(&(ctx->buf[ctx->buf_off]), tmp, md->digest->md_size) == 0;
  428. ctx->buf_off+= md->digest->md_size;
  429. if(ret == 1)
  430. {
  431. ctx->sigio= 0;
  432. if(ctx->buf_len != ctx->buf_off)
  433. {
  434. memmove(ctx->buf, &(ctx->buf[ctx->buf_off]), ctx->buf_len- ctx->buf_off);
  435. }
  436. ctx->buf_len-= ctx->buf_off;
  437. ctx->buf_off= 0;
  438. }
  439. else
  440. {
  441. ctx->cont= 0;
  442. }
  443. }
  444. static void block_out(BIO* b)
  445. {
  446. BIO_OK_CTX *ctx;
  447. EVP_MD_CTX *md;
  448. unsigned long tl;
  449. ctx=b->ptr;
  450. md=&ctx->md;
  451. tl= ctx->buf_len- OK_BLOCK_BLOCK;
  452. ctx->buf[0]=(unsigned char)(tl>>24);
  453. ctx->buf[1]=(unsigned char)(tl>>16);
  454. ctx->buf[2]=(unsigned char)(tl>>8);
  455. ctx->buf[3]=(unsigned char)(tl);
  456. EVP_DigestUpdate(md, (unsigned char*) &(ctx->buf[OK_BLOCK_BLOCK]), tl);
  457. EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL);
  458. ctx->buf_len+= md->digest->md_size;
  459. ctx->blockout= 1;
  460. }
  461. static void block_in(BIO* b)
  462. {
  463. BIO_OK_CTX *ctx;
  464. EVP_MD_CTX *md;
  465. unsigned long tl= 0;
  466. unsigned char tmp[EVP_MAX_MD_SIZE];
  467. ctx=b->ptr;
  468. md=&ctx->md;
  469. assert(sizeof(tl)>=OK_BLOCK_BLOCK); /* always true */
  470. tl =ctx->buf[0]; tl<<=8;
  471. tl|=ctx->buf[1]; tl<<=8;
  472. tl|=ctx->buf[2]; tl<<=8;
  473. tl|=ctx->buf[3];
  474. if (ctx->buf_len < tl+ OK_BLOCK_BLOCK+ md->digest->md_size) return;
  475. EVP_DigestUpdate(md, (unsigned char*) &(ctx->buf[OK_BLOCK_BLOCK]), tl);
  476. EVP_DigestFinal_ex(md, tmp, NULL);
  477. if(memcmp(&(ctx->buf[tl+ OK_BLOCK_BLOCK]), tmp, md->digest->md_size) == 0)
  478. {
  479. /* there might be parts from next block lurking around ! */
  480. ctx->buf_off_save= tl+ OK_BLOCK_BLOCK+ md->digest->md_size;
  481. ctx->buf_len_save= ctx->buf_len;
  482. ctx->buf_off= OK_BLOCK_BLOCK;
  483. ctx->buf_len= tl+ OK_BLOCK_BLOCK;
  484. ctx->blockout= 1;
  485. }
  486. else
  487. {
  488. ctx->cont= 0;
  489. }
  490. }