bio_enc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <errno.h>
  11. #include "internal/cryptlib.h"
  12. #include <openssl/buffer.h>
  13. #include <openssl/evp.h>
  14. #include "internal/bio.h"
  15. static int enc_write(BIO *h, const char *buf, int num);
  16. static int enc_read(BIO *h, char *buf, int size);
  17. static long enc_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  18. static int enc_new(BIO *h);
  19. static int enc_free(BIO *data);
  20. static long enc_callback_ctrl(BIO *h, int cmd, bio_info_cb *fps);
  21. #define ENC_BLOCK_SIZE (1024*4)
  22. #define ENC_MIN_CHUNK (256)
  23. #define BUF_OFFSET (ENC_MIN_CHUNK + EVP_MAX_BLOCK_LENGTH)
  24. typedef struct enc_struct {
  25. int buf_len;
  26. int buf_off;
  27. int cont; /* <= 0 when finished */
  28. int finished;
  29. int ok; /* bad decrypt */
  30. EVP_CIPHER_CTX *cipher;
  31. unsigned char *read_start, *read_end;
  32. /*
  33. * buf is larger than ENC_BLOCK_SIZE because EVP_DecryptUpdate can return
  34. * up to a block more data than is presented to it
  35. */
  36. unsigned char buf[BUF_OFFSET + ENC_BLOCK_SIZE];
  37. } BIO_ENC_CTX;
  38. static const BIO_METHOD methods_enc = {
  39. BIO_TYPE_CIPHER, "cipher",
  40. /* TODO: Convert to new style write function */
  41. bwrite_conv,
  42. enc_write,
  43. /* TODO: Convert to new style read function */
  44. bread_conv,
  45. enc_read,
  46. NULL, /* enc_puts, */
  47. NULL, /* enc_gets, */
  48. enc_ctrl,
  49. enc_new,
  50. enc_free,
  51. enc_callback_ctrl,
  52. };
  53. const BIO_METHOD *BIO_f_cipher(void)
  54. {
  55. return (&methods_enc);
  56. }
  57. static int enc_new(BIO *bi)
  58. {
  59. BIO_ENC_CTX *ctx;
  60. ctx = OPENSSL_zalloc(sizeof(*ctx));
  61. if (ctx == NULL)
  62. return 0;
  63. ctx->cipher = EVP_CIPHER_CTX_new();
  64. if (ctx->cipher == NULL) {
  65. OPENSSL_free(ctx);
  66. return 0;
  67. }
  68. ctx->cont = 1;
  69. ctx->ok = 1;
  70. ctx->read_end = ctx->read_start = &(ctx->buf[BUF_OFFSET]);
  71. BIO_set_data(bi, ctx);
  72. BIO_set_init(bi, 1);
  73. return 1;
  74. }
  75. static int enc_free(BIO *a)
  76. {
  77. BIO_ENC_CTX *b;
  78. if (a == NULL)
  79. return 0;
  80. b = BIO_get_data(a);
  81. if (b == NULL)
  82. return 0;
  83. EVP_CIPHER_CTX_free(b->cipher);
  84. OPENSSL_clear_free(b, sizeof(BIO_ENC_CTX));
  85. BIO_set_data(a, NULL);
  86. BIO_set_init(a, 0);
  87. return 1;
  88. }
  89. static int enc_read(BIO *b, char *out, int outl)
  90. {
  91. int ret = 0, i, blocksize;
  92. BIO_ENC_CTX *ctx;
  93. BIO *next;
  94. if (out == NULL)
  95. return (0);
  96. ctx = BIO_get_data(b);
  97. next = BIO_next(b);
  98. if ((ctx == NULL) || (next == NULL))
  99. return 0;
  100. /* First check if there are bytes decoded/encoded */
  101. if (ctx->buf_len > 0) {
  102. i = ctx->buf_len - ctx->buf_off;
  103. if (i > outl)
  104. i = outl;
  105. memcpy(out, &(ctx->buf[ctx->buf_off]), i);
  106. ret = i;
  107. out += i;
  108. outl -= i;
  109. ctx->buf_off += i;
  110. if (ctx->buf_len == ctx->buf_off) {
  111. ctx->buf_len = 0;
  112. ctx->buf_off = 0;
  113. }
  114. }
  115. blocksize = EVP_CIPHER_CTX_block_size(ctx->cipher);
  116. if (blocksize == 1)
  117. blocksize = 0;
  118. /*
  119. * At this point, we have room of outl bytes and an empty buffer, so we
  120. * should read in some more.
  121. */
  122. while (outl > 0) {
  123. if (ctx->cont <= 0)
  124. break;
  125. if (ctx->read_start == ctx->read_end) { /* time to read more data */
  126. ctx->read_end = ctx->read_start = &(ctx->buf[BUF_OFFSET]);
  127. i = BIO_read(next, ctx->read_start, ENC_BLOCK_SIZE);
  128. if (i > 0)
  129. ctx->read_end += i;
  130. } else {
  131. i = ctx->read_end - ctx->read_start;
  132. }
  133. if (i <= 0) {
  134. /* Should be continue next time we are called? */
  135. if (!BIO_should_retry(next)) {
  136. ctx->cont = i;
  137. i = EVP_CipherFinal_ex(ctx->cipher,
  138. ctx->buf, &(ctx->buf_len));
  139. ctx->ok = i;
  140. ctx->buf_off = 0;
  141. } else {
  142. ret = (ret == 0) ? i : ret;
  143. break;
  144. }
  145. } else {
  146. if (outl > ENC_MIN_CHUNK) {
  147. /*
  148. * Depending on flags block cipher decrypt can write
  149. * one extra block and then back off, i.e. output buffer
  150. * has to accommodate extra block...
  151. */
  152. int j = outl - blocksize, buf_len;
  153. if (!EVP_CipherUpdate(ctx->cipher,
  154. (unsigned char *)out, &buf_len,
  155. ctx->read_start, i > j ? j : i)) {
  156. BIO_clear_retry_flags(b);
  157. return 0;
  158. }
  159. ret += buf_len;
  160. out += buf_len;
  161. outl -= buf_len;
  162. if ((i -= j) <= 0) {
  163. ctx->read_start = ctx->read_end;
  164. continue;
  165. }
  166. ctx->read_start += j;
  167. }
  168. if (i > ENC_MIN_CHUNK)
  169. i = ENC_MIN_CHUNK;
  170. if (!EVP_CipherUpdate(ctx->cipher,
  171. ctx->buf, &ctx->buf_len,
  172. ctx->read_start, i)) {
  173. BIO_clear_retry_flags(b);
  174. ctx->ok = 0;
  175. return 0;
  176. }
  177. ctx->read_start += i;
  178. ctx->cont = 1;
  179. /*
  180. * Note: it is possible for EVP_CipherUpdate to decrypt zero
  181. * bytes because this is or looks like the final block: if this
  182. * happens we should retry and either read more data or decrypt
  183. * the final block
  184. */
  185. if (ctx->buf_len == 0)
  186. continue;
  187. }
  188. if (ctx->buf_len <= outl)
  189. i = ctx->buf_len;
  190. else
  191. i = outl;
  192. if (i <= 0)
  193. break;
  194. memcpy(out, ctx->buf, i);
  195. ret += i;
  196. ctx->buf_off = i;
  197. outl -= i;
  198. out += i;
  199. }
  200. BIO_clear_retry_flags(b);
  201. BIO_copy_next_retry(b);
  202. return ((ret == 0) ? ctx->cont : ret);
  203. }
  204. static int enc_write(BIO *b, const char *in, int inl)
  205. {
  206. int ret = 0, n, i;
  207. BIO_ENC_CTX *ctx;
  208. BIO *next;
  209. ctx = BIO_get_data(b);
  210. next = BIO_next(b);
  211. if ((ctx == NULL) || (next == NULL))
  212. return 0;
  213. ret = inl;
  214. BIO_clear_retry_flags(b);
  215. n = ctx->buf_len - ctx->buf_off;
  216. while (n > 0) {
  217. i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
  218. if (i <= 0) {
  219. BIO_copy_next_retry(b);
  220. return (i);
  221. }
  222. ctx->buf_off += i;
  223. n -= i;
  224. }
  225. /* at this point all pending data has been written */
  226. if ((in == NULL) || (inl <= 0))
  227. return (0);
  228. ctx->buf_off = 0;
  229. while (inl > 0) {
  230. n = (inl > ENC_BLOCK_SIZE) ? ENC_BLOCK_SIZE : inl;
  231. if (!EVP_CipherUpdate(ctx->cipher,
  232. ctx->buf, &ctx->buf_len,
  233. (const unsigned char *)in, n)) {
  234. BIO_clear_retry_flags(b);
  235. ctx->ok = 0;
  236. return 0;
  237. }
  238. inl -= n;
  239. in += n;
  240. ctx->buf_off = 0;
  241. n = ctx->buf_len;
  242. while (n > 0) {
  243. i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
  244. if (i <= 0) {
  245. BIO_copy_next_retry(b);
  246. return (ret == inl) ? i : ret - inl;
  247. }
  248. n -= i;
  249. ctx->buf_off += i;
  250. }
  251. ctx->buf_len = 0;
  252. ctx->buf_off = 0;
  253. }
  254. BIO_copy_next_retry(b);
  255. return (ret);
  256. }
  257. static long enc_ctrl(BIO *b, int cmd, long num, void *ptr)
  258. {
  259. BIO *dbio;
  260. BIO_ENC_CTX *ctx, *dctx;
  261. long ret = 1;
  262. int i;
  263. EVP_CIPHER_CTX **c_ctx;
  264. BIO *next;
  265. ctx = BIO_get_data(b);
  266. next = BIO_next(b);
  267. if (ctx == NULL)
  268. return 0;
  269. switch (cmd) {
  270. case BIO_CTRL_RESET:
  271. ctx->ok = 1;
  272. ctx->finished = 0;
  273. if (!EVP_CipherInit_ex(ctx->cipher, NULL, NULL, NULL, NULL,
  274. EVP_CIPHER_CTX_encrypting(ctx->cipher)))
  275. return 0;
  276. ret = BIO_ctrl(next, cmd, num, ptr);
  277. break;
  278. case BIO_CTRL_EOF: /* More to read */
  279. if (ctx->cont <= 0)
  280. ret = 1;
  281. else
  282. ret = BIO_ctrl(next, cmd, num, ptr);
  283. break;
  284. case BIO_CTRL_WPENDING:
  285. ret = ctx->buf_len - ctx->buf_off;
  286. if (ret <= 0)
  287. ret = BIO_ctrl(next, cmd, num, ptr);
  288. break;
  289. case BIO_CTRL_PENDING: /* More to read in buffer */
  290. ret = ctx->buf_len - ctx->buf_off;
  291. if (ret <= 0)
  292. ret = BIO_ctrl(next, cmd, num, ptr);
  293. break;
  294. case BIO_CTRL_FLUSH:
  295. /* do a final write */
  296. again:
  297. while (ctx->buf_len != ctx->buf_off) {
  298. i = enc_write(b, NULL, 0);
  299. if (i < 0)
  300. return i;
  301. }
  302. if (!ctx->finished) {
  303. ctx->finished = 1;
  304. ctx->buf_off = 0;
  305. ret = EVP_CipherFinal_ex(ctx->cipher,
  306. (unsigned char *)ctx->buf,
  307. &(ctx->buf_len));
  308. ctx->ok = (int)ret;
  309. if (ret <= 0)
  310. break;
  311. /* push out the bytes */
  312. goto again;
  313. }
  314. /* Finally flush the underlying BIO */
  315. ret = BIO_ctrl(next, cmd, num, ptr);
  316. break;
  317. case BIO_C_GET_CIPHER_STATUS:
  318. ret = (long)ctx->ok;
  319. break;
  320. case BIO_C_DO_STATE_MACHINE:
  321. BIO_clear_retry_flags(b);
  322. ret = BIO_ctrl(next, cmd, num, ptr);
  323. BIO_copy_next_retry(b);
  324. break;
  325. case BIO_C_GET_CIPHER_CTX:
  326. c_ctx = (EVP_CIPHER_CTX **)ptr;
  327. *c_ctx = ctx->cipher;
  328. BIO_set_init(b, 1);
  329. break;
  330. case BIO_CTRL_DUP:
  331. dbio = (BIO *)ptr;
  332. dctx = BIO_get_data(dbio);
  333. dctx->cipher = EVP_CIPHER_CTX_new();
  334. if (dctx->cipher == NULL)
  335. return 0;
  336. ret = EVP_CIPHER_CTX_copy(dctx->cipher, ctx->cipher);
  337. if (ret)
  338. BIO_set_init(dbio, 1);
  339. break;
  340. default:
  341. ret = BIO_ctrl(next, cmd, num, ptr);
  342. break;
  343. }
  344. return (ret);
  345. }
  346. static long enc_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
  347. {
  348. long ret = 1;
  349. BIO *next = BIO_next(b);
  350. if (next == NULL)
  351. return (0);
  352. switch (cmd) {
  353. default:
  354. ret = BIO_callback_ctrl(next, cmd, fp);
  355. break;
  356. }
  357. return (ret);
  358. }
  359. int BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k,
  360. const unsigned char *i, int e)
  361. {
  362. BIO_ENC_CTX *ctx;
  363. long (*callback) (struct bio_st *, int, const char *, int, long, long);
  364. ctx = BIO_get_data(b);
  365. if (ctx == NULL)
  366. return 0;
  367. callback = BIO_get_callback(b);
  368. if ((callback != NULL) &&
  369. (callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e,
  370. 0L) <= 0))
  371. return 0;
  372. BIO_set_init(b, 1);
  373. if (!EVP_CipherInit_ex(ctx->cipher, c, NULL, k, i, e))
  374. return 0;
  375. if (callback != NULL)
  376. return callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 1L);
  377. return 1;
  378. }