bio_enc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /* crypto/evp/bio_enc.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 "cryptlib.h"
  61. #include <openssl/buffer.h>
  62. #include <openssl/evp.h>
  63. static int enc_write(BIO *h, const char *buf, int num);
  64. static int enc_read(BIO *h, char *buf, int size);
  65. /*static int enc_puts(BIO *h, const char *str); */
  66. /*static int enc_gets(BIO *h, char *str, int size); */
  67. static long enc_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  68. static int enc_new(BIO *h);
  69. static int enc_free(BIO *data);
  70. static long enc_callback_ctrl(BIO *h, int cmd, bio_info_cb *fps);
  71. #define ENC_BLOCK_SIZE (1024*4)
  72. #define BUF_OFFSET (EVP_MAX_BLOCK_LENGTH*2)
  73. typedef struct enc_struct
  74. {
  75. int buf_len;
  76. int buf_off;
  77. int cont; /* <= 0 when finished */
  78. int finished;
  79. int ok; /* bad decrypt */
  80. EVP_CIPHER_CTX cipher;
  81. /* buf is larger than ENC_BLOCK_SIZE because EVP_DecryptUpdate
  82. * can return up to a block more data than is presented to it
  83. */
  84. char buf[ENC_BLOCK_SIZE+BUF_OFFSET+2];
  85. } BIO_ENC_CTX;
  86. static BIO_METHOD methods_enc=
  87. {
  88. BIO_TYPE_CIPHER,"cipher",
  89. enc_write,
  90. enc_read,
  91. NULL, /* enc_puts, */
  92. NULL, /* enc_gets, */
  93. enc_ctrl,
  94. enc_new,
  95. enc_free,
  96. enc_callback_ctrl,
  97. };
  98. BIO_METHOD *BIO_f_cipher(void)
  99. {
  100. return(&methods_enc);
  101. }
  102. static int enc_new(BIO *bi)
  103. {
  104. BIO_ENC_CTX *ctx;
  105. ctx=(BIO_ENC_CTX *)OPENSSL_malloc(sizeof(BIO_ENC_CTX));
  106. if (ctx == NULL) return(0);
  107. EVP_CIPHER_CTX_init(&ctx->cipher);
  108. ctx->buf_len=0;
  109. ctx->buf_off=0;
  110. ctx->cont=1;
  111. ctx->finished=0;
  112. ctx->ok=1;
  113. bi->init=0;
  114. bi->ptr=(char *)ctx;
  115. bi->flags=0;
  116. return(1);
  117. }
  118. static int enc_free(BIO *a)
  119. {
  120. BIO_ENC_CTX *b;
  121. if (a == NULL) return(0);
  122. b=(BIO_ENC_CTX *)a->ptr;
  123. EVP_CIPHER_CTX_cleanup(&(b->cipher));
  124. OPENSSL_cleanse(a->ptr,sizeof(BIO_ENC_CTX));
  125. OPENSSL_free(a->ptr);
  126. a->ptr=NULL;
  127. a->init=0;
  128. a->flags=0;
  129. return(1);
  130. }
  131. static int enc_read(BIO *b, char *out, int outl)
  132. {
  133. int ret=0,i;
  134. BIO_ENC_CTX *ctx;
  135. if (out == NULL) return(0);
  136. ctx=(BIO_ENC_CTX *)b->ptr;
  137. if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
  138. /* First check if there are bytes decoded/encoded */
  139. if (ctx->buf_len > 0)
  140. {
  141. i=ctx->buf_len-ctx->buf_off;
  142. if (i > outl) i=outl;
  143. memcpy(out,&(ctx->buf[ctx->buf_off]),i);
  144. ret=i;
  145. out+=i;
  146. outl-=i;
  147. ctx->buf_off+=i;
  148. if (ctx->buf_len == ctx->buf_off)
  149. {
  150. ctx->buf_len=0;
  151. ctx->buf_off=0;
  152. }
  153. }
  154. /* At this point, we have room of outl bytes and an empty
  155. * buffer, so we should read in some more. */
  156. while (outl > 0)
  157. {
  158. if (ctx->cont <= 0) break;
  159. /* read in at IV offset, read the EVP_Cipher
  160. * documentation about why */
  161. i=BIO_read(b->next_bio,&(ctx->buf[BUF_OFFSET]),ENC_BLOCK_SIZE);
  162. if (i <= 0)
  163. {
  164. /* Should be continue next time we are called? */
  165. if (!BIO_should_retry(b->next_bio))
  166. {
  167. ctx->cont=i;
  168. i=EVP_CipherFinal_ex(&(ctx->cipher),
  169. (unsigned char *)ctx->buf,
  170. &(ctx->buf_len));
  171. ctx->ok=i;
  172. ctx->buf_off=0;
  173. }
  174. else
  175. {
  176. ret=(ret == 0)?i:ret;
  177. break;
  178. }
  179. }
  180. else
  181. {
  182. if (!EVP_CipherUpdate(&(ctx->cipher),
  183. (unsigned char *)ctx->buf,&ctx->buf_len,
  184. (unsigned char *)&(ctx->buf[BUF_OFFSET]),i))
  185. {
  186. BIO_clear_retry_flags(b);
  187. return 0;
  188. }
  189. ctx->cont=1;
  190. /* Note: it is possible for EVP_CipherUpdate to
  191. * decrypt zero bytes because this is or looks like
  192. * the final block: if this happens we should retry
  193. * and either read more data or decrypt the final
  194. * block
  195. */
  196. if(ctx->buf_len == 0) continue;
  197. }
  198. if (ctx->buf_len <= outl)
  199. i=ctx->buf_len;
  200. else
  201. i=outl;
  202. if (i <= 0) break;
  203. memcpy(out,ctx->buf,i);
  204. ret+=i;
  205. ctx->buf_off=i;
  206. outl-=i;
  207. out+=i;
  208. }
  209. BIO_clear_retry_flags(b);
  210. BIO_copy_next_retry(b);
  211. return((ret == 0)?ctx->cont:ret);
  212. }
  213. static int enc_write(BIO *b, const char *in, int inl)
  214. {
  215. int ret=0,n,i;
  216. BIO_ENC_CTX *ctx;
  217. ctx=(BIO_ENC_CTX *)b->ptr;
  218. ret=inl;
  219. BIO_clear_retry_flags(b);
  220. n=ctx->buf_len-ctx->buf_off;
  221. while (n > 0)
  222. {
  223. i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
  224. if (i <= 0)
  225. {
  226. BIO_copy_next_retry(b);
  227. return(i);
  228. }
  229. ctx->buf_off+=i;
  230. n-=i;
  231. }
  232. /* at this point all pending data has been written */
  233. if ((in == NULL) || (inl <= 0)) return(0);
  234. ctx->buf_off=0;
  235. while (inl > 0)
  236. {
  237. n=(inl > ENC_BLOCK_SIZE)?ENC_BLOCK_SIZE:inl;
  238. if (!EVP_CipherUpdate(&(ctx->cipher),
  239. (unsigned char *)ctx->buf,&ctx->buf_len,
  240. (unsigned char *)in,n))
  241. {
  242. BIO_clear_retry_flags(b);
  243. return 0;
  244. }
  245. inl-=n;
  246. in+=n;
  247. ctx->buf_off=0;
  248. n=ctx->buf_len;
  249. while (n > 0)
  250. {
  251. i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
  252. if (i <= 0)
  253. {
  254. BIO_copy_next_retry(b);
  255. return (ret == inl) ? i : ret - inl;
  256. }
  257. n-=i;
  258. ctx->buf_off+=i;
  259. }
  260. ctx->buf_len=0;
  261. ctx->buf_off=0;
  262. }
  263. BIO_copy_next_retry(b);
  264. return(ret);
  265. }
  266. static long enc_ctrl(BIO *b, int cmd, long num, void *ptr)
  267. {
  268. BIO *dbio;
  269. BIO_ENC_CTX *ctx,*dctx;
  270. long ret=1;
  271. int i;
  272. EVP_CIPHER_CTX **c_ctx;
  273. ctx=(BIO_ENC_CTX *)b->ptr;
  274. switch (cmd)
  275. {
  276. case BIO_CTRL_RESET:
  277. ctx->ok=1;
  278. ctx->finished=0;
  279. if (!EVP_CipherInit_ex(&(ctx->cipher),NULL,NULL,NULL,NULL,
  280. ctx->cipher.encrypt))
  281. return 0;
  282. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  283. break;
  284. case BIO_CTRL_EOF: /* More to read */
  285. if (ctx->cont <= 0)
  286. ret=1;
  287. else
  288. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  289. break;
  290. case BIO_CTRL_WPENDING:
  291. ret=ctx->buf_len-ctx->buf_off;
  292. if (ret <= 0)
  293. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  294. break;
  295. case BIO_CTRL_PENDING: /* More to read in buffer */
  296. ret=ctx->buf_len-ctx->buf_off;
  297. if (ret <= 0)
  298. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  299. break;
  300. case BIO_CTRL_FLUSH:
  301. /* do a final write */
  302. again:
  303. while (ctx->buf_len != ctx->buf_off)
  304. {
  305. i=enc_write(b,NULL,0);
  306. if (i < 0)
  307. return i;
  308. }
  309. if (!ctx->finished)
  310. {
  311. ctx->finished=1;
  312. ctx->buf_off=0;
  313. ret=EVP_CipherFinal_ex(&(ctx->cipher),
  314. (unsigned char *)ctx->buf,
  315. &(ctx->buf_len));
  316. ctx->ok=(int)ret;
  317. if (ret <= 0) break;
  318. /* push out the bytes */
  319. goto again;
  320. }
  321. /* Finally flush the underlying BIO */
  322. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  323. break;
  324. case BIO_C_GET_CIPHER_STATUS:
  325. ret=(long)ctx->ok;
  326. break;
  327. case BIO_C_DO_STATE_MACHINE:
  328. BIO_clear_retry_flags(b);
  329. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  330. BIO_copy_next_retry(b);
  331. break;
  332. case BIO_C_GET_CIPHER_CTX:
  333. c_ctx=(EVP_CIPHER_CTX **)ptr;
  334. (*c_ctx)= &(ctx->cipher);
  335. b->init=1;
  336. break;
  337. case BIO_CTRL_DUP:
  338. dbio=(BIO *)ptr;
  339. dctx=(BIO_ENC_CTX *)dbio->ptr;
  340. EVP_CIPHER_CTX_init(&dctx->cipher);
  341. ret = EVP_CIPHER_CTX_copy(&dctx->cipher,&ctx->cipher);
  342. if (ret)
  343. dbio->init=1;
  344. break;
  345. default:
  346. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  347. break;
  348. }
  349. return(ret);
  350. }
  351. static long enc_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
  352. {
  353. long ret=1;
  354. if (b->next_bio == NULL) return(0);
  355. switch (cmd)
  356. {
  357. default:
  358. ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
  359. break;
  360. }
  361. return(ret);
  362. }
  363. /*
  364. void BIO_set_cipher_ctx(b,c)
  365. BIO *b;
  366. EVP_CIPHER_ctx *c;
  367. {
  368. if (b == NULL) return;
  369. if ((b->callback != NULL) &&
  370. (b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0))
  371. return;
  372. b->init=1;
  373. ctx=(BIO_ENC_CTX *)b->ptr;
  374. memcpy(ctx->cipher,c,sizeof(EVP_CIPHER_CTX));
  375. if (b->callback != NULL)
  376. b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L);
  377. }
  378. */
  379. int BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k,
  380. const unsigned char *i, int e)
  381. {
  382. BIO_ENC_CTX *ctx;
  383. if (b == NULL) return 0;
  384. if ((b->callback != NULL) &&
  385. (b->callback(b,BIO_CB_CTRL,(const char *)c,BIO_CTRL_SET,e,0L) <= 0))
  386. return 0;
  387. b->init=1;
  388. ctx=(BIO_ENC_CTX *)b->ptr;
  389. if (!EVP_CipherInit_ex(&(ctx->cipher),c,NULL, k,i,e))
  390. return 0;
  391. if (b->callback != NULL)
  392. return b->callback(b,BIO_CB_CTRL,(const char *)c,BIO_CTRL_SET,e,1L);
  393. return 1;
  394. }