bio_enc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * Copyright 1995-2023 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. return 0;
  62. ctx->cipher = EVP_CIPHER_CTX_new();
  63. if (ctx->cipher == NULL) {
  64. OPENSSL_free(ctx);
  65. return 0;
  66. }
  67. ctx->cont = 1;
  68. ctx->ok = 1;
  69. ctx->read_end = ctx->read_start = &(ctx->buf[BUF_OFFSET]);
  70. BIO_set_data(bi, ctx);
  71. BIO_set_init(bi, 1);
  72. return 1;
  73. }
  74. static int enc_free(BIO *a)
  75. {
  76. BIO_ENC_CTX *b;
  77. if (a == NULL)
  78. return 0;
  79. b = BIO_get_data(a);
  80. if (b == NULL)
  81. return 0;
  82. EVP_CIPHER_CTX_free(b->cipher);
  83. OPENSSL_clear_free(b, sizeof(BIO_ENC_CTX));
  84. BIO_set_data(a, NULL);
  85. BIO_set_init(a, 0);
  86. return 1;
  87. }
  88. static int enc_read(BIO *b, char *out, int outl)
  89. {
  90. int ret = 0, i, blocksize;
  91. BIO_ENC_CTX *ctx;
  92. BIO *next;
  93. if (out == NULL)
  94. return 0;
  95. ctx = BIO_get_data(b);
  96. next = BIO_next(b);
  97. if ((ctx == NULL) || (next == NULL))
  98. return 0;
  99. /* First check if there are bytes decoded/encoded */
  100. if (ctx->buf_len > 0) {
  101. i = ctx->buf_len - ctx->buf_off;
  102. if (i > outl)
  103. i = outl;
  104. memcpy(out, &(ctx->buf[ctx->buf_off]), i);
  105. ret = i;
  106. out += i;
  107. outl -= i;
  108. ctx->buf_off += i;
  109. if (ctx->buf_len == ctx->buf_off) {
  110. ctx->buf_len = 0;
  111. ctx->buf_off = 0;
  112. }
  113. }
  114. blocksize = EVP_CIPHER_CTX_get_block_size(ctx->cipher);
  115. if (blocksize == 1)
  116. blocksize = 0;
  117. /*
  118. * At this point, we have room of outl bytes and an empty buffer, so we
  119. * should read in some more.
  120. */
  121. while (outl > 0) {
  122. if (ctx->cont <= 0)
  123. break;
  124. if (ctx->read_start == ctx->read_end) { /* time to read more data */
  125. ctx->read_end = ctx->read_start = &(ctx->buf[BUF_OFFSET]);
  126. i = BIO_read(next, ctx->read_start, ENC_BLOCK_SIZE);
  127. if (i > 0)
  128. ctx->read_end += i;
  129. } else {
  130. i = ctx->read_end - ctx->read_start;
  131. }
  132. if (i <= 0) {
  133. /* Should be continue next time we are called? */
  134. if (!BIO_should_retry(next)) {
  135. ctx->cont = i;
  136. i = EVP_CipherFinal_ex(ctx->cipher,
  137. ctx->buf, &(ctx->buf_len));
  138. ctx->ok = i;
  139. ctx->buf_off = 0;
  140. } else {
  141. ret = (ret == 0) ? i : ret;
  142. break;
  143. }
  144. } else {
  145. if (outl > ENC_MIN_CHUNK) {
  146. /*
  147. * Depending on flags block cipher decrypt can write
  148. * one extra block and then back off, i.e. output buffer
  149. * has to accommodate extra block...
  150. */
  151. int j = outl - blocksize, buf_len;
  152. if (!EVP_CipherUpdate(ctx->cipher,
  153. (unsigned char *)out, &buf_len,
  154. ctx->read_start, i > j ? j : i)) {
  155. BIO_clear_retry_flags(b);
  156. return 0;
  157. }
  158. ret += buf_len;
  159. out += buf_len;
  160. outl -= buf_len;
  161. if ((i -= j) <= 0) {
  162. ctx->read_start = ctx->read_end;
  163. continue;
  164. }
  165. ctx->read_start += j;
  166. }
  167. if (i > ENC_MIN_CHUNK)
  168. i = ENC_MIN_CHUNK;
  169. if (!EVP_CipherUpdate(ctx->cipher,
  170. ctx->buf, &ctx->buf_len,
  171. ctx->read_start, i)) {
  172. BIO_clear_retry_flags(b);
  173. ctx->ok = 0;
  174. return 0;
  175. }
  176. ctx->read_start += i;
  177. ctx->cont = 1;
  178. /*
  179. * Note: it is possible for EVP_CipherUpdate to decrypt zero
  180. * bytes because this is or looks like the final block: if this
  181. * happens we should retry and either read more data or decrypt
  182. * the final block
  183. */
  184. if (ctx->buf_len == 0)
  185. continue;
  186. }
  187. if (ctx->buf_len <= outl)
  188. i = ctx->buf_len;
  189. else
  190. i = outl;
  191. if (i <= 0)
  192. break;
  193. memcpy(out, ctx->buf, i);
  194. ret += i;
  195. ctx->buf_off = i;
  196. outl -= i;
  197. out += i;
  198. }
  199. BIO_clear_retry_flags(b);
  200. BIO_copy_next_retry(b);
  201. return ((ret == 0) ? ctx->cont : ret);
  202. }
  203. static int enc_write(BIO *b, const char *in, int inl)
  204. {
  205. int ret = 0, n, i;
  206. BIO_ENC_CTX *ctx;
  207. BIO *next;
  208. ctx = BIO_get_data(b);
  209. next = BIO_next(b);
  210. if ((ctx == NULL) || (next == NULL))
  211. return 0;
  212. ret = inl;
  213. BIO_clear_retry_flags(b);
  214. n = ctx->buf_len - ctx->buf_off;
  215. while (n > 0) {
  216. i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
  217. if (i <= 0) {
  218. BIO_copy_next_retry(b);
  219. return i;
  220. }
  221. ctx->buf_off += i;
  222. n -= i;
  223. }
  224. /* at this point all pending data has been written */
  225. if ((in == NULL) || (inl <= 0))
  226. return 0;
  227. ctx->buf_off = 0;
  228. while (inl > 0) {
  229. n = (inl > ENC_BLOCK_SIZE) ? ENC_BLOCK_SIZE : inl;
  230. if (!EVP_CipherUpdate(ctx->cipher,
  231. ctx->buf, &ctx->buf_len,
  232. (const unsigned char *)in, n)) {
  233. BIO_clear_retry_flags(b);
  234. ctx->ok = 0;
  235. return 0;
  236. }
  237. inl -= n;
  238. in += n;
  239. ctx->buf_off = 0;
  240. n = ctx->buf_len;
  241. while (n > 0) {
  242. i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
  243. if (i <= 0) {
  244. BIO_copy_next_retry(b);
  245. return (ret == inl) ? i : ret - inl;
  246. }
  247. n -= i;
  248. ctx->buf_off += i;
  249. }
  250. ctx->buf_len = 0;
  251. ctx->buf_off = 0;
  252. }
  253. BIO_copy_next_retry(b);
  254. return ret;
  255. }
  256. static long enc_ctrl(BIO *b, int cmd, long num, void *ptr)
  257. {
  258. BIO *dbio;
  259. BIO_ENC_CTX *ctx, *dctx;
  260. long ret = 1;
  261. int i;
  262. EVP_CIPHER_CTX **c_ctx;
  263. BIO *next;
  264. int pend;
  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_is_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. pend = ctx->buf_len - ctx->buf_off;
  299. i = enc_write(b, NULL, 0);
  300. /*
  301. * i should never be > 0 here because we didn't ask to write any
  302. * new data. We stop if we get an error or we failed to make any
  303. * progress writing pending data.
  304. */
  305. if (i < 0 || (ctx->buf_len - ctx->buf_off) == pend)
  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(next, cmd, num, ptr);
  322. BIO_copy_next_retry(b);
  323. break;
  324. case BIO_C_GET_CIPHER_STATUS:
  325. ret = (long)ctx->ok;
  326. break;
  327. case BIO_C_DO_STATE_MACHINE:
  328. BIO_clear_retry_flags(b);
  329. ret = BIO_ctrl(next, cmd, num, ptr);
  330. BIO_copy_next_retry(b);
  331. break;
  332. case BIO_C_GET_CIPHER_CTX:
  333. c_ctx = (EVP_CIPHER_CTX **)ptr;
  334. *c_ctx = ctx->cipher;
  335. BIO_set_init(b, 1);
  336. break;
  337. case BIO_CTRL_DUP:
  338. dbio = (BIO *)ptr;
  339. dctx = BIO_get_data(dbio);
  340. dctx->cipher = EVP_CIPHER_CTX_new();
  341. if (dctx->cipher == NULL)
  342. return 0;
  343. ret = EVP_CIPHER_CTX_copy(dctx->cipher, ctx->cipher);
  344. if (ret)
  345. BIO_set_init(dbio, 1);
  346. break;
  347. default:
  348. ret = BIO_ctrl(next, cmd, num, ptr);
  349. break;
  350. }
  351. return ret;
  352. }
  353. static long enc_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  354. {
  355. BIO *next = BIO_next(b);
  356. if (next == NULL)
  357. return 0;
  358. return BIO_callback_ctrl(next, cmd, fp);
  359. }
  360. int BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k,
  361. const unsigned char *i, int e)
  362. {
  363. BIO_ENC_CTX *ctx;
  364. BIO_callback_fn_ex callback_ex;
  365. #ifndef OPENSSL_NO_DEPRECATED_3_0
  366. long (*callback) (struct bio_st *, int, const char *, int, long, long) = NULL;
  367. #endif
  368. ctx = BIO_get_data(b);
  369. if (ctx == NULL)
  370. return 0;
  371. if ((callback_ex = BIO_get_callback_ex(b)) != NULL) {
  372. if (callback_ex(b, BIO_CB_CTRL, (const char *)c, 0, BIO_CTRL_SET,
  373. e, 1, NULL) <= 0)
  374. return 0;
  375. }
  376. #ifndef OPENSSL_NO_DEPRECATED_3_0
  377. else {
  378. callback = BIO_get_callback(b);
  379. if ((callback != NULL) &&
  380. (callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e,
  381. 0L) <= 0))
  382. return 0;
  383. }
  384. #endif
  385. BIO_set_init(b, 1);
  386. if (!EVP_CipherInit_ex(ctx->cipher, c, NULL, k, i, e))
  387. return 0;
  388. if (callback_ex != NULL)
  389. return callback_ex(b, BIO_CB_CTRL | BIO_CB_RETURN, (const char *)c, 0,
  390. BIO_CTRL_SET, e, 1, NULL);
  391. #ifndef OPENSSL_NO_DEPRECATED_3_0
  392. else if (callback != NULL)
  393. return callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 1L);
  394. #endif
  395. return 1;
  396. }