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