bio_enc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. ctx = BIO_get_data(b);
  268. next = BIO_next(b);
  269. if (ctx == NULL)
  270. return 0;
  271. switch (cmd) {
  272. case BIO_CTRL_RESET:
  273. ctx->ok = 1;
  274. ctx->finished = 0;
  275. if (!EVP_CipherInit_ex(ctx->cipher, NULL, NULL, NULL, NULL,
  276. EVP_CIPHER_CTX_encrypting(ctx->cipher)))
  277. return 0;
  278. ret = BIO_ctrl(next, cmd, num, ptr);
  279. break;
  280. case BIO_CTRL_EOF: /* More to read */
  281. if (ctx->cont <= 0)
  282. ret = 1;
  283. else
  284. ret = BIO_ctrl(next, cmd, num, ptr);
  285. break;
  286. case BIO_CTRL_WPENDING:
  287. ret = ctx->buf_len - ctx->buf_off;
  288. if (ret <= 0)
  289. ret = BIO_ctrl(next, cmd, num, ptr);
  290. break;
  291. case BIO_CTRL_PENDING: /* More to read in buffer */
  292. ret = ctx->buf_len - ctx->buf_off;
  293. if (ret <= 0)
  294. ret = BIO_ctrl(next, cmd, num, ptr);
  295. break;
  296. case BIO_CTRL_FLUSH:
  297. /* do a final write */
  298. again:
  299. while (ctx->buf_len != ctx->buf_off) {
  300. i = enc_write(b, NULL, 0);
  301. if (i < 0)
  302. return i;
  303. }
  304. if (!ctx->finished) {
  305. ctx->finished = 1;
  306. ctx->buf_off = 0;
  307. ret = EVP_CipherFinal_ex(ctx->cipher,
  308. (unsigned char *)ctx->buf,
  309. &(ctx->buf_len));
  310. ctx->ok = (int)ret;
  311. if (ret <= 0)
  312. break;
  313. /* push out the bytes */
  314. goto again;
  315. }
  316. /* Finally flush the underlying BIO */
  317. ret = BIO_ctrl(next, cmd, num, ptr);
  318. break;
  319. case BIO_C_GET_CIPHER_STATUS:
  320. ret = (long)ctx->ok;
  321. break;
  322. case BIO_C_DO_STATE_MACHINE:
  323. BIO_clear_retry_flags(b);
  324. ret = BIO_ctrl(next, cmd, num, ptr);
  325. BIO_copy_next_retry(b);
  326. break;
  327. case BIO_C_GET_CIPHER_CTX:
  328. c_ctx = (EVP_CIPHER_CTX **)ptr;
  329. *c_ctx = ctx->cipher;
  330. BIO_set_init(b, 1);
  331. break;
  332. case BIO_CTRL_DUP:
  333. dbio = (BIO *)ptr;
  334. dctx = BIO_get_data(dbio);
  335. dctx->cipher = EVP_CIPHER_CTX_new();
  336. if (dctx->cipher == NULL)
  337. return 0;
  338. ret = EVP_CIPHER_CTX_copy(dctx->cipher, ctx->cipher);
  339. if (ret)
  340. BIO_set_init(dbio, 1);
  341. break;
  342. default:
  343. ret = BIO_ctrl(next, cmd, num, ptr);
  344. break;
  345. }
  346. return ret;
  347. }
  348. static long enc_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  349. {
  350. long ret = 1;
  351. BIO *next = BIO_next(b);
  352. if (next == NULL)
  353. return 0;
  354. switch (cmd) {
  355. default:
  356. ret = BIO_callback_ctrl(next, cmd, fp);
  357. break;
  358. }
  359. return ret;
  360. }
  361. int BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k,
  362. const unsigned char *i, int e)
  363. {
  364. BIO_ENC_CTX *ctx;
  365. long (*callback) (struct bio_st *, int, const char *, int, long, long);
  366. ctx = BIO_get_data(b);
  367. if (ctx == NULL)
  368. return 0;
  369. callback = BIO_get_callback(b);
  370. if ((callback != NULL) &&
  371. (callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e,
  372. 0L) <= 0))
  373. return 0;
  374. BIO_set_init(b, 1);
  375. if (!EVP_CipherInit_ex(ctx->cipher, c, NULL, k, i, e))
  376. return 0;
  377. if (callback != NULL)
  378. return callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 1L);
  379. return 1;
  380. }