bio_enc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * Copyright 1995-2018 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,
  40. "cipher",
  41. /* TODO: Convert to new style write function */
  42. bwrite_conv,
  43. enc_write,
  44. /* TODO: Convert to new style read function */
  45. bread_conv,
  46. enc_read,
  47. NULL, /* enc_puts, */
  48. NULL, /* enc_gets, */
  49. enc_ctrl,
  50. enc_new,
  51. enc_free,
  52. enc_callback_ctrl,
  53. };
  54. const BIO_METHOD *BIO_f_cipher(void)
  55. {
  56. return &methods_enc;
  57. }
  58. static int enc_new(BIO *bi)
  59. {
  60. BIO_ENC_CTX *ctx;
  61. if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
  62. EVPerr(EVP_F_ENC_NEW, ERR_R_MALLOC_FAILURE);
  63. return 0;
  64. }
  65. ctx->cipher = EVP_CIPHER_CTX_new();
  66. if (ctx->cipher == NULL) {
  67. OPENSSL_free(ctx);
  68. return 0;
  69. }
  70. ctx->cont = 1;
  71. ctx->ok = 1;
  72. ctx->read_end = ctx->read_start = &(ctx->buf[BUF_OFFSET]);
  73. BIO_set_data(bi, ctx);
  74. BIO_set_init(bi, 1);
  75. return 1;
  76. }
  77. static int enc_free(BIO *a)
  78. {
  79. BIO_ENC_CTX *b;
  80. if (a == NULL)
  81. return 0;
  82. b = BIO_get_data(a);
  83. if (b == NULL)
  84. return 0;
  85. EVP_CIPHER_CTX_free(b->cipher);
  86. OPENSSL_clear_free(b, sizeof(BIO_ENC_CTX));
  87. BIO_set_data(a, NULL);
  88. BIO_set_init(a, 0);
  89. return 1;
  90. }
  91. static int enc_read(BIO *b, char *out, int outl)
  92. {
  93. int ret = 0, i, blocksize;
  94. BIO_ENC_CTX *ctx;
  95. BIO *next;
  96. if (out == NULL)
  97. return 0;
  98. ctx = BIO_get_data(b);
  99. next = BIO_next(b);
  100. if ((ctx == NULL) || (next == NULL))
  101. return 0;
  102. /* First check if there are bytes decoded/encoded */
  103. if (ctx->buf_len > 0) {
  104. i = ctx->buf_len - ctx->buf_off;
  105. if (i > outl)
  106. i = outl;
  107. memcpy(out, &(ctx->buf[ctx->buf_off]), i);
  108. ret = i;
  109. out += i;
  110. outl -= i;
  111. ctx->buf_off += i;
  112. if (ctx->buf_len == ctx->buf_off) {
  113. ctx->buf_len = 0;
  114. ctx->buf_off = 0;
  115. }
  116. }
  117. blocksize = EVP_CIPHER_CTX_block_size(ctx->cipher);
  118. if (blocksize == 1)
  119. blocksize = 0;
  120. /*
  121. * At this point, we have room of outl bytes and an empty buffer, so we
  122. * should read in some more.
  123. */
  124. while (outl > 0) {
  125. if (ctx->cont <= 0)
  126. break;
  127. if (ctx->read_start == ctx->read_end) { /* time to read more data */
  128. ctx->read_end = ctx->read_start = &(ctx->buf[BUF_OFFSET]);
  129. i = BIO_read(next, ctx->read_start, ENC_BLOCK_SIZE);
  130. if (i > 0)
  131. ctx->read_end += i;
  132. } else {
  133. i = ctx->read_end - ctx->read_start;
  134. }
  135. if (i <= 0) {
  136. /* Should be continue next time we are called? */
  137. if (!BIO_should_retry(next)) {
  138. ctx->cont = i;
  139. i = EVP_CipherFinal_ex(ctx->cipher,
  140. ctx->buf, &(ctx->buf_len));
  141. ctx->ok = i;
  142. ctx->buf_off = 0;
  143. } else {
  144. ret = (ret == 0) ? i : ret;
  145. break;
  146. }
  147. } else {
  148. if (outl > ENC_MIN_CHUNK) {
  149. /*
  150. * Depending on flags block cipher decrypt can write
  151. * one extra block and then back off, i.e. output buffer
  152. * has to accommodate extra block...
  153. */
  154. int j = outl - blocksize, buf_len;
  155. if (!EVP_CipherUpdate(ctx->cipher,
  156. (unsigned char *)out, &buf_len,
  157. ctx->read_start, i > j ? j : i)) {
  158. BIO_clear_retry_flags(b);
  159. return 0;
  160. }
  161. ret += buf_len;
  162. out += buf_len;
  163. outl -= buf_len;
  164. if ((i -= j) <= 0) {
  165. ctx->read_start = ctx->read_end;
  166. continue;
  167. }
  168. ctx->read_start += j;
  169. }
  170. if (i > ENC_MIN_CHUNK)
  171. i = ENC_MIN_CHUNK;
  172. if (!EVP_CipherUpdate(ctx->cipher,
  173. ctx->buf, &ctx->buf_len,
  174. ctx->read_start, i)) {
  175. BIO_clear_retry_flags(b);
  176. ctx->ok = 0;
  177. return 0;
  178. }
  179. ctx->read_start += i;
  180. ctx->cont = 1;
  181. /*
  182. * Note: it is possible for EVP_CipherUpdate to decrypt zero
  183. * bytes because this is or looks like the final block: if this
  184. * happens we should retry and either read more data or decrypt
  185. * the final block
  186. */
  187. if (ctx->buf_len == 0)
  188. continue;
  189. }
  190. if (ctx->buf_len <= outl)
  191. i = ctx->buf_len;
  192. else
  193. i = outl;
  194. if (i <= 0)
  195. break;
  196. memcpy(out, ctx->buf, i);
  197. ret += i;
  198. ctx->buf_off = i;
  199. outl -= i;
  200. out += i;
  201. }
  202. BIO_clear_retry_flags(b);
  203. BIO_copy_next_retry(b);
  204. return ((ret == 0) ? ctx->cont : ret);
  205. }
  206. static int enc_write(BIO *b, const char *in, int inl)
  207. {
  208. int ret = 0, n, i;
  209. BIO_ENC_CTX *ctx;
  210. BIO *next;
  211. ctx = BIO_get_data(b);
  212. next = BIO_next(b);
  213. if ((ctx == NULL) || (next == NULL))
  214. return 0;
  215. ret = inl;
  216. BIO_clear_retry_flags(b);
  217. n = ctx->buf_len - ctx->buf_off;
  218. while (n > 0) {
  219. i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
  220. if (i <= 0) {
  221. BIO_copy_next_retry(b);
  222. return i;
  223. }
  224. ctx->buf_off += i;
  225. n -= i;
  226. }
  227. /* at this point all pending data has been written */
  228. if ((in == NULL) || (inl <= 0))
  229. return 0;
  230. ctx->buf_off = 0;
  231. while (inl > 0) {
  232. n = (inl > ENC_BLOCK_SIZE) ? ENC_BLOCK_SIZE : inl;
  233. if (!EVP_CipherUpdate(ctx->cipher,
  234. ctx->buf, &ctx->buf_len,
  235. (const unsigned char *)in, n)) {
  236. BIO_clear_retry_flags(b);
  237. ctx->ok = 0;
  238. return 0;
  239. }
  240. inl -= n;
  241. in += n;
  242. ctx->buf_off = 0;
  243. n = ctx->buf_len;
  244. while (n > 0) {
  245. i = BIO_write(next, &(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. BIO *next;
  267. int pend;
  268. ctx = BIO_get_data(b);
  269. next = BIO_next(b);
  270. if (ctx == NULL)
  271. return 0;
  272. switch (cmd) {
  273. case BIO_CTRL_RESET:
  274. ctx->ok = 1;
  275. ctx->finished = 0;
  276. if (!EVP_CipherInit_ex(ctx->cipher, NULL, NULL, NULL, NULL,
  277. EVP_CIPHER_CTX_encrypting(ctx->cipher)))
  278. return 0;
  279. ret = BIO_ctrl(next, cmd, num, ptr);
  280. break;
  281. case BIO_CTRL_EOF: /* More to read */
  282. if (ctx->cont <= 0)
  283. ret = 1;
  284. else
  285. ret = BIO_ctrl(next, cmd, num, ptr);
  286. break;
  287. case BIO_CTRL_WPENDING:
  288. ret = ctx->buf_len - ctx->buf_off;
  289. if (ret <= 0)
  290. ret = BIO_ctrl(next, cmd, num, ptr);
  291. break;
  292. case BIO_CTRL_PENDING: /* More to read in buffer */
  293. ret = ctx->buf_len - ctx->buf_off;
  294. if (ret <= 0)
  295. ret = BIO_ctrl(next, cmd, num, ptr);
  296. break;
  297. case BIO_CTRL_FLUSH:
  298. /* do a final write */
  299. again:
  300. while (ctx->buf_len != ctx->buf_off) {
  301. pend = ctx->buf_len - ctx->buf_off;
  302. i = enc_write(b, NULL, 0);
  303. /*
  304. * i should never be > 0 here because we didn't ask to write any
  305. * new data. We stop if we get an error or we failed to make any
  306. * progress writing pending data.
  307. */
  308. if (i < 0 || (ctx->buf_len - ctx->buf_off) == pend)
  309. return i;
  310. }
  311. if (!ctx->finished) {
  312. ctx->finished = 1;
  313. ctx->buf_off = 0;
  314. ret = EVP_CipherFinal_ex(ctx->cipher,
  315. (unsigned char *)ctx->buf,
  316. &(ctx->buf_len));
  317. ctx->ok = (int)ret;
  318. if (ret <= 0)
  319. break;
  320. /* push out the bytes */
  321. goto again;
  322. }
  323. /* Finally flush the underlying BIO */
  324. ret = BIO_ctrl(next, cmd, num, ptr);
  325. break;
  326. case BIO_C_GET_CIPHER_STATUS:
  327. ret = (long)ctx->ok;
  328. break;
  329. case BIO_C_DO_STATE_MACHINE:
  330. BIO_clear_retry_flags(b);
  331. ret = BIO_ctrl(next, cmd, num, ptr);
  332. BIO_copy_next_retry(b);
  333. break;
  334. case BIO_C_GET_CIPHER_CTX:
  335. c_ctx = (EVP_CIPHER_CTX **)ptr;
  336. *c_ctx = ctx->cipher;
  337. BIO_set_init(b, 1);
  338. break;
  339. case BIO_CTRL_DUP:
  340. dbio = (BIO *)ptr;
  341. dctx = BIO_get_data(dbio);
  342. dctx->cipher = EVP_CIPHER_CTX_new();
  343. if (dctx->cipher == NULL)
  344. return 0;
  345. ret = EVP_CIPHER_CTX_copy(dctx->cipher, ctx->cipher);
  346. if (ret)
  347. BIO_set_init(dbio, 1);
  348. break;
  349. default:
  350. ret = BIO_ctrl(next, cmd, num, ptr);
  351. break;
  352. }
  353. return ret;
  354. }
  355. static long enc_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  356. {
  357. long ret = 1;
  358. BIO *next = BIO_next(b);
  359. if (next == NULL)
  360. return 0;
  361. switch (cmd) {
  362. default:
  363. ret = BIO_callback_ctrl(next, cmd, fp);
  364. break;
  365. }
  366. return ret;
  367. }
  368. int 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. long (*callback) (struct bio_st *, int, const char *, int, long, long);
  373. ctx = BIO_get_data(b);
  374. if (ctx == NULL)
  375. return 0;
  376. callback = BIO_get_callback(b);
  377. if ((callback != NULL) &&
  378. (callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e,
  379. 0L) <= 0))
  380. return 0;
  381. BIO_set_init(b, 1);
  382. if (!EVP_CipherInit_ex(ctx->cipher, c, NULL, k, i, e))
  383. return 0;
  384. if (callback != NULL)
  385. return callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 1L);
  386. return 1;
  387. }