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 "cryptlib.h"
  107. #include <openssl/buffer.h>
  108. #include <openssl/bio.h>
  109. #include <openssl/evp.h>
  110. #include <openssl/rand.h>
  111. static int ok_write(BIO *h, const char *buf, int num);
  112. static int ok_read(BIO *h, char *buf, int size);
  113. static long ok_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  114. static int ok_new(BIO *h);
  115. static int ok_free(BIO *data);
  116. static long ok_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
  117. static void sig_out(BIO* b);
  118. static void sig_in(BIO* b);
  119. static void block_out(BIO* b);
  120. static void block_in(BIO* b);
  121. #define OK_BLOCK_SIZE (1024*4)
  122. #define OK_BLOCK_BLOCK 4
  123. #define IOBS (OK_BLOCK_SIZE+ OK_BLOCK_BLOCK+ 3*EVP_MAX_MD_SIZE)
  124. #define WELLKNOWN "The quick brown fox jumped over the lazy dog's back."
  125. #ifndef L_ENDIAN
  126. #define swapem(x) \
  127. ((unsigned long int)((((unsigned long int)(x) & 0x000000ffU) << 24) | \
  128. (((unsigned long int)(x) & 0x0000ff00U) << 8) | \
  129. (((unsigned long int)(x) & 0x00ff0000U) >> 8) | \
  130. (((unsigned long int)(x) & 0xff000000U) >> 24)))
  131. #else
  132. #define swapem(x) (x)
  133. #endif
  134. typedef struct ok_struct
  135. {
  136. int buf_len;
  137. int buf_off;
  138. int buf_len_save;
  139. int buf_off_save;
  140. int cont; /* <= 0 when finished */
  141. int finished;
  142. EVP_MD_CTX md;
  143. int blockout; /* output block is ready */
  144. int sigio; /* must process signature */
  145. unsigned char buf[IOBS];
  146. } BIO_OK_CTX;
  147. static BIO_METHOD methods_ok=
  148. {
  149. BIO_TYPE_CIPHER,"reliable",
  150. ok_write,
  151. ok_read,
  152. NULL, /* ok_puts, */
  153. NULL, /* ok_gets, */
  154. ok_ctrl,
  155. ok_new,
  156. ok_free,
  157. ok_callback_ctrl,
  158. };
  159. BIO_METHOD *BIO_f_reliable(void)
  160. {
  161. return(&methods_ok);
  162. }
  163. static int ok_new(BIO *bi)
  164. {
  165. BIO_OK_CTX *ctx;
  166. ctx=(BIO_OK_CTX *)OPENSSL_malloc(sizeof(BIO_OK_CTX));
  167. if (ctx == NULL) return(0);
  168. ctx->buf_len=0;
  169. ctx->buf_off=0;
  170. ctx->buf_len_save=0;
  171. ctx->buf_off_save=0;
  172. ctx->cont=1;
  173. ctx->finished=0;
  174. ctx->blockout= 0;
  175. ctx->sigio=1;
  176. EVP_MD_CTX_init(&ctx->md);
  177. bi->init=0;
  178. bi->ptr=(char *)ctx;
  179. bi->flags=0;
  180. return(1);
  181. }
  182. static int ok_free(BIO *a)
  183. {
  184. if (a == NULL) return(0);
  185. EVP_MD_CTX_cleanup(&((BIO_OK_CTX *)a->ptr)->md);
  186. OPENSSL_cleanse(a->ptr,sizeof(BIO_OK_CTX));
  187. OPENSSL_free(a->ptr);
  188. a->ptr=NULL;
  189. a->init=0;
  190. a->flags=0;
  191. return(1);
  192. }
  193. static int ok_read(BIO *b, char *out, int outl)
  194. {
  195. int ret=0,i,n;
  196. BIO_OK_CTX *ctx;
  197. if (out == NULL) return(0);
  198. ctx=(BIO_OK_CTX *)b->ptr;
  199. if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0)) return(0);
  200. while(outl > 0)
  201. {
  202. /* copy clean bytes to output buffer */
  203. if (ctx->blockout)
  204. {
  205. i=ctx->buf_len-ctx->buf_off;
  206. if (i > outl) i=outl;
  207. memcpy(out,&(ctx->buf[ctx->buf_off]),i);
  208. ret+=i;
  209. out+=i;
  210. outl-=i;
  211. ctx->buf_off+=i;
  212. /* all clean bytes are out */
  213. if (ctx->buf_len == ctx->buf_off)
  214. {
  215. ctx->buf_off=0;
  216. /* copy start of the next block into proper place */
  217. if(ctx->buf_len_save- ctx->buf_off_save > 0)
  218. {
  219. ctx->buf_len= ctx->buf_len_save- ctx->buf_off_save;
  220. memmove(ctx->buf, &(ctx->buf[ctx->buf_off_save]),
  221. ctx->buf_len);
  222. }
  223. else
  224. {
  225. ctx->buf_len=0;
  226. }
  227. ctx->blockout= 0;
  228. }
  229. }
  230. /* output buffer full -- cancel */
  231. if (outl == 0) break;
  232. /* no clean bytes in buffer -- fill it */
  233. n=IOBS- ctx->buf_len;
  234. i=BIO_read(b->next_bio,&(ctx->buf[ctx->buf_len]),n);
  235. if (i <= 0) break; /* nothing new */
  236. ctx->buf_len+= i;
  237. /* no signature yet -- check if we got one */
  238. if (ctx->sigio == 1) sig_in(b);
  239. /* signature ok -- check if we got block */
  240. if (ctx->sigio == 0) block_in(b);
  241. /* invalid block -- cancel */
  242. if (ctx->cont <= 0) break;
  243. }
  244. BIO_clear_retry_flags(b);
  245. BIO_copy_next_retry(b);
  246. return(ret);
  247. }
  248. static int ok_write(BIO *b, const char *in, int inl)
  249. {
  250. int ret=0,n,i;
  251. BIO_OK_CTX *ctx;
  252. ctx=(BIO_OK_CTX *)b->ptr;
  253. ret=inl;
  254. if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0)) return(0);
  255. if(ctx->sigio) sig_out(b);
  256. do{
  257. BIO_clear_retry_flags(b);
  258. n=ctx->buf_len-ctx->buf_off;
  259. while (ctx->blockout && n > 0)
  260. {
  261. i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
  262. if (i <= 0)
  263. {
  264. BIO_copy_next_retry(b);
  265. if(!BIO_should_retry(b))
  266. ctx->cont= 0;
  267. return(i);
  268. }
  269. ctx->buf_off+=i;
  270. n-=i;
  271. }
  272. /* at this point all pending data has been written */
  273. ctx->blockout= 0;
  274. if (ctx->buf_len == ctx->buf_off)
  275. {
  276. ctx->buf_len=OK_BLOCK_BLOCK;
  277. ctx->buf_off=0;
  278. }
  279. if ((in == NULL) || (inl <= 0)) return(0);
  280. n= (inl+ ctx->buf_len > OK_BLOCK_SIZE+ OK_BLOCK_BLOCK) ?
  281. OK_BLOCK_SIZE+ OK_BLOCK_BLOCK- ctx->buf_len : inl;
  282. memcpy((unsigned char *)(&(ctx->buf[ctx->buf_len])),(unsigned char *)in,n);
  283. ctx->buf_len+= n;
  284. inl-=n;
  285. in+=n;
  286. if(ctx->buf_len >= OK_BLOCK_SIZE+ OK_BLOCK_BLOCK)
  287. {
  288. block_out(b);
  289. }
  290. }while(inl > 0);
  291. BIO_clear_retry_flags(b);
  292. BIO_copy_next_retry(b);
  293. return(ret);
  294. }
  295. static long ok_ctrl(BIO *b, int cmd, long num, void *ptr)
  296. {
  297. BIO_OK_CTX *ctx;
  298. EVP_MD *md;
  299. const EVP_MD **ppmd;
  300. long ret=1;
  301. int i;
  302. ctx=b->ptr;
  303. switch (cmd)
  304. {
  305. case BIO_CTRL_RESET:
  306. ctx->buf_len=0;
  307. ctx->buf_off=0;
  308. ctx->buf_len_save=0;
  309. ctx->buf_off_save=0;
  310. ctx->cont=1;
  311. ctx->finished=0;
  312. ctx->blockout= 0;
  313. ctx->sigio=1;
  314. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  315. break;
  316. case BIO_CTRL_EOF: /* More to read */
  317. if (ctx->cont <= 0)
  318. ret=1;
  319. else
  320. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  321. break;
  322. case BIO_CTRL_PENDING: /* More to read in buffer */
  323. case BIO_CTRL_WPENDING: /* More to read in buffer */
  324. ret=ctx->blockout ? ctx->buf_len-ctx->buf_off : 0;
  325. if (ret <= 0)
  326. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  327. break;
  328. case BIO_CTRL_FLUSH:
  329. /* do a final write */
  330. if(ctx->blockout == 0)
  331. block_out(b);
  332. while (ctx->blockout)
  333. {
  334. i=ok_write(b,NULL,0);
  335. if (i < 0)
  336. {
  337. ret=i;
  338. break;
  339. }
  340. }
  341. ctx->finished=1;
  342. ctx->buf_off=ctx->buf_len=0;
  343. ctx->cont=(int)ret;
  344. /* Finally flush the underlying BIO */
  345. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  346. break;
  347. case BIO_C_DO_STATE_MACHINE:
  348. BIO_clear_retry_flags(b);
  349. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  350. BIO_copy_next_retry(b);
  351. break;
  352. case BIO_CTRL_INFO:
  353. ret=(long)ctx->cont;
  354. break;
  355. case BIO_C_SET_MD:
  356. md=ptr;
  357. EVP_DigestInit_ex(&ctx->md, md, NULL);
  358. b->init=1;
  359. break;
  360. case BIO_C_GET_MD:
  361. if (b->init)
  362. {
  363. ppmd=ptr;
  364. *ppmd=ctx->md.digest;
  365. }
  366. else
  367. ret=0;
  368. break;
  369. default:
  370. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  371. break;
  372. }
  373. return(ret);
  374. }
  375. static long ok_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
  376. {
  377. long ret=1;
  378. if (b->next_bio == NULL) return(0);
  379. switch (cmd)
  380. {
  381. default:
  382. ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
  383. break;
  384. }
  385. return(ret);
  386. }
  387. static void longswap(void *_ptr, int len)
  388. {
  389. #ifndef L_ENDIAN
  390. int i;
  391. char *ptr=_ptr;
  392. for(i= 0;i < len;i+= 4){
  393. *((unsigned long *)&(ptr[i]))= swapem(*((unsigned long *)&(ptr[i])));
  394. }
  395. #endif
  396. }
  397. static void sig_out(BIO* b)
  398. {
  399. BIO_OK_CTX *ctx;
  400. EVP_MD_CTX *md;
  401. ctx=b->ptr;
  402. md=&ctx->md;
  403. if(ctx->buf_len+ 2* md->digest->md_size > OK_BLOCK_SIZE) return;
  404. EVP_DigestInit_ex(md, md->digest, NULL);
  405. /* FIXME: there's absolutely no guarantee this makes any sense at all,
  406. * particularly now EVP_MD_CTX has been restructured.
  407. */
  408. RAND_pseudo_bytes(md->md_data, md->digest->md_size);
  409. memcpy(&(ctx->buf[ctx->buf_len]), md->md_data, md->digest->md_size);
  410. longswap(&(ctx->buf[ctx->buf_len]), md->digest->md_size);
  411. ctx->buf_len+= md->digest->md_size;
  412. EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN));
  413. EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL);
  414. ctx->buf_len+= md->digest->md_size;
  415. ctx->blockout= 1;
  416. ctx->sigio= 0;
  417. }
  418. static void sig_in(BIO* b)
  419. {
  420. BIO_OK_CTX *ctx;
  421. EVP_MD_CTX *md;
  422. unsigned char tmp[EVP_MAX_MD_SIZE];
  423. int ret= 0;
  424. ctx=b->ptr;
  425. md=&ctx->md;
  426. if(ctx->buf_len- ctx->buf_off < 2* md->digest->md_size) return;
  427. EVP_DigestInit_ex(md, md->digest, NULL);
  428. memcpy(md->md_data, &(ctx->buf[ctx->buf_off]), md->digest->md_size);
  429. longswap(md->md_data, md->digest->md_size);
  430. ctx->buf_off+= md->digest->md_size;
  431. EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN));
  432. EVP_DigestFinal_ex(md, tmp, NULL);
  433. ret= memcmp(&(ctx->buf[ctx->buf_off]), tmp, md->digest->md_size) == 0;
  434. ctx->buf_off+= md->digest->md_size;
  435. if(ret == 1)
  436. {
  437. ctx->sigio= 0;
  438. if(ctx->buf_len != ctx->buf_off)
  439. {
  440. memmove(ctx->buf, &(ctx->buf[ctx->buf_off]), ctx->buf_len- ctx->buf_off);
  441. }
  442. ctx->buf_len-= ctx->buf_off;
  443. ctx->buf_off= 0;
  444. }
  445. else
  446. {
  447. ctx->cont= 0;
  448. }
  449. }
  450. static void block_out(BIO* b)
  451. {
  452. BIO_OK_CTX *ctx;
  453. EVP_MD_CTX *md;
  454. unsigned long tl;
  455. ctx=b->ptr;
  456. md=&ctx->md;
  457. tl= ctx->buf_len- OK_BLOCK_BLOCK;
  458. tl= swapem(tl);
  459. memcpy(ctx->buf, &tl, OK_BLOCK_BLOCK);
  460. tl= swapem(tl);
  461. EVP_DigestUpdate(md, (unsigned char*) &(ctx->buf[OK_BLOCK_BLOCK]), tl);
  462. EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL);
  463. ctx->buf_len+= md->digest->md_size;
  464. ctx->blockout= 1;
  465. }
  466. static void block_in(BIO* b)
  467. {
  468. BIO_OK_CTX *ctx;
  469. EVP_MD_CTX *md;
  470. long tl= 0;
  471. unsigned char tmp[EVP_MAX_MD_SIZE];
  472. ctx=b->ptr;
  473. md=&ctx->md;
  474. memcpy(&tl, ctx->buf, OK_BLOCK_BLOCK);
  475. tl= swapem(tl);
  476. if (ctx->buf_len < tl+ OK_BLOCK_BLOCK+ md->digest->md_size) return;
  477. EVP_DigestUpdate(md, (unsigned char*) &(ctx->buf[OK_BLOCK_BLOCK]), tl);
  478. EVP_DigestFinal_ex(md, tmp, NULL);
  479. if(memcmp(&(ctx->buf[tl+ OK_BLOCK_BLOCK]), tmp, md->digest->md_size) == 0)
  480. {
  481. /* there might be parts from next block lurking around ! */
  482. ctx->buf_off_save= tl+ OK_BLOCK_BLOCK+ md->digest->md_size;
  483. ctx->buf_len_save= ctx->buf_len;
  484. ctx->buf_off= OK_BLOCK_BLOCK;
  485. ctx->buf_len= tl+ OK_BLOCK_BLOCK;
  486. ctx->blockout= 1;
  487. }
  488. else
  489. {
  490. ctx->cont= 0;
  491. }
  492. }