bio_enc.c 13 KB

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