2
0

bio_enc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. EVP_CipherUpdate(&(ctx->cipher),
  183. (unsigned char *)ctx->buf,&ctx->buf_len,
  184. (unsigned char *)&(ctx->buf[BUF_OFFSET]),i);
  185. ctx->cont=1;
  186. /* Note: it is possible for EVP_CipherUpdate to
  187. * decrypt zero bytes because this is or looks like
  188. * the final block: if this happens we should retry
  189. * and either read more data or decrypt the final
  190. * block
  191. */
  192. if(ctx->buf_len == 0) continue;
  193. }
  194. if (ctx->buf_len <= outl)
  195. i=ctx->buf_len;
  196. else
  197. i=outl;
  198. if (i <= 0) break;
  199. memcpy(out,ctx->buf,i);
  200. ret+=i;
  201. ctx->buf_off=i;
  202. outl-=i;
  203. out+=i;
  204. }
  205. BIO_clear_retry_flags(b);
  206. BIO_copy_next_retry(b);
  207. return((ret == 0)?ctx->cont:ret);
  208. }
  209. static int enc_write(BIO *b, const char *in, int inl)
  210. {
  211. int ret=0,n,i;
  212. BIO_ENC_CTX *ctx;
  213. ctx=(BIO_ENC_CTX *)b->ptr;
  214. ret=inl;
  215. BIO_clear_retry_flags(b);
  216. n=ctx->buf_len-ctx->buf_off;
  217. while (n > 0)
  218. {
  219. i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
  220. if (i <= 0)
  221. {
  222. BIO_copy_next_retry(b);
  223. return(i);
  224. }
  225. ctx->buf_off+=i;
  226. n-=i;
  227. }
  228. /* at this point all pending data has been written */
  229. if ((in == NULL) || (inl <= 0)) return(0);
  230. ctx->buf_off=0;
  231. while (inl > 0)
  232. {
  233. n=(inl > ENC_BLOCK_SIZE)?ENC_BLOCK_SIZE:inl;
  234. EVP_CipherUpdate(&(ctx->cipher),
  235. (unsigned char *)ctx->buf,&ctx->buf_len,
  236. (unsigned char *)in,n);
  237. inl-=n;
  238. in+=n;
  239. ctx->buf_off=0;
  240. n=ctx->buf_len;
  241. while (n > 0)
  242. {
  243. i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
  244. if (i <= 0)
  245. {
  246. BIO_copy_next_retry(b);
  247. return (ret == inl) ? i : ret - inl;
  248. }
  249. n-=i;
  250. ctx->buf_off+=i;
  251. }
  252. ctx->buf_len=0;
  253. ctx->buf_off=0;
  254. }
  255. BIO_copy_next_retry(b);
  256. return(ret);
  257. }
  258. static long enc_ctrl(BIO *b, int cmd, long num, void *ptr)
  259. {
  260. BIO *dbio;
  261. BIO_ENC_CTX *ctx,*dctx;
  262. long ret=1;
  263. int i;
  264. EVP_CIPHER_CTX **c_ctx;
  265. ctx=(BIO_ENC_CTX *)b->ptr;
  266. switch (cmd)
  267. {
  268. case BIO_CTRL_RESET:
  269. ctx->ok=1;
  270. ctx->finished=0;
  271. EVP_CipherInit_ex(&(ctx->cipher),NULL,NULL,NULL,NULL,
  272. ctx->cipher.encrypt);
  273. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  274. break;
  275. case BIO_CTRL_EOF: /* More to read */
  276. if (ctx->cont <= 0)
  277. ret=1;
  278. else
  279. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  280. break;
  281. case BIO_CTRL_WPENDING:
  282. ret=ctx->buf_len-ctx->buf_off;
  283. if (ret <= 0)
  284. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  285. break;
  286. case BIO_CTRL_PENDING: /* More to read in buffer */
  287. ret=ctx->buf_len-ctx->buf_off;
  288. if (ret <= 0)
  289. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  290. break;
  291. case BIO_CTRL_FLUSH:
  292. /* do a final write */
  293. again:
  294. while (ctx->buf_len != ctx->buf_off)
  295. {
  296. i=enc_write(b,NULL,0);
  297. if (i < 0)
  298. return i;
  299. }
  300. if (!ctx->finished)
  301. {
  302. ctx->finished=1;
  303. ctx->buf_off=0;
  304. ret=EVP_CipherFinal_ex(&(ctx->cipher),
  305. (unsigned char *)ctx->buf,
  306. &(ctx->buf_len));
  307. ctx->ok=(int)ret;
  308. if (ret <= 0) break;
  309. /* push out the bytes */
  310. goto again;
  311. }
  312. /* Finally flush the underlying BIO */
  313. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  314. break;
  315. case BIO_C_GET_CIPHER_STATUS:
  316. ret=(long)ctx->ok;
  317. break;
  318. case BIO_C_DO_STATE_MACHINE:
  319. BIO_clear_retry_flags(b);
  320. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  321. BIO_copy_next_retry(b);
  322. break;
  323. case BIO_C_GET_CIPHER_CTX:
  324. c_ctx=(EVP_CIPHER_CTX **)ptr;
  325. (*c_ctx)= &(ctx->cipher);
  326. b->init=1;
  327. break;
  328. case BIO_CTRL_DUP:
  329. dbio=(BIO *)ptr;
  330. dctx=(BIO_ENC_CTX *)dbio->ptr;
  331. memcpy(&(dctx->cipher),&(ctx->cipher),sizeof(ctx->cipher));
  332. dbio->init=1;
  333. break;
  334. default:
  335. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  336. break;
  337. }
  338. return(ret);
  339. }
  340. static long enc_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
  341. {
  342. long ret=1;
  343. if (b->next_bio == NULL) return(0);
  344. switch (cmd)
  345. {
  346. default:
  347. ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
  348. break;
  349. }
  350. return(ret);
  351. }
  352. /*
  353. void BIO_set_cipher_ctx(b,c)
  354. BIO *b;
  355. EVP_CIPHER_ctx *c;
  356. {
  357. if (b == NULL) return;
  358. if ((b->callback != NULL) &&
  359. (b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0))
  360. return;
  361. b->init=1;
  362. ctx=(BIO_ENC_CTX *)b->ptr;
  363. memcpy(ctx->cipher,c,sizeof(EVP_CIPHER_CTX));
  364. if (b->callback != NULL)
  365. b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L);
  366. }
  367. */
  368. void BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k,
  369. const unsigned char *i, int e)
  370. {
  371. BIO_ENC_CTX *ctx;
  372. if (b == NULL) return;
  373. if ((b->callback != NULL) &&
  374. (b->callback(b,BIO_CB_CTRL,(const char *)c,BIO_CTRL_SET,e,0L) <= 0))
  375. return;
  376. b->init=1;
  377. ctx=(BIO_ENC_CTX *)b->ptr;
  378. EVP_CipherInit_ex(&(ctx->cipher),c,NULL, k,i,e);
  379. if (b->callback != NULL)
  380. b->callback(b,BIO_CB_CTRL,(const char *)c,BIO_CTRL_SET,e,1L);
  381. }