2
0

bio_enc.c 12 KB

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