bio_enc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * Copyright 1995-2024 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 == 0)
  116. return 0;
  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. int pend;
  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_is_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. pend = ctx->buf_len - ctx->buf_off;
  301. i = enc_write(b, NULL, 0);
  302. /*
  303. * i should never be > 0 here because we didn't ask to write any
  304. * new data. We stop if we get an error or we failed to make any
  305. * progress writing pending data.
  306. */
  307. if (i < 0 || (ctx->buf_len - ctx->buf_off) == pend)
  308. return i;
  309. }
  310. if (!ctx->finished) {
  311. ctx->finished = 1;
  312. ctx->buf_off = 0;
  313. ret = EVP_CipherFinal_ex(ctx->cipher,
  314. (unsigned char *)ctx->buf,
  315. &(ctx->buf_len));
  316. ctx->ok = (int)ret;
  317. if (ret <= 0)
  318. break;
  319. /* push out the bytes */
  320. goto again;
  321. }
  322. /* Finally flush the underlying BIO */
  323. ret = BIO_ctrl(next, cmd, num, ptr);
  324. BIO_copy_next_retry(b);
  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. BIO *next = BIO_next(b);
  358. if (next == NULL)
  359. return 0;
  360. return BIO_callback_ctrl(next, cmd, fp);
  361. }
  362. int BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k,
  363. const unsigned char *i, int e)
  364. {
  365. BIO_ENC_CTX *ctx;
  366. BIO_callback_fn_ex callback_ex;
  367. #ifndef OPENSSL_NO_DEPRECATED_3_0
  368. long (*callback) (struct bio_st *, int, const char *, int, long, long) = NULL;
  369. #endif
  370. ctx = BIO_get_data(b);
  371. if (ctx == NULL)
  372. return 0;
  373. if ((callback_ex = BIO_get_callback_ex(b)) != NULL) {
  374. if (callback_ex(b, BIO_CB_CTRL, (const char *)c, 0, BIO_CTRL_SET,
  375. e, 1, NULL) <= 0)
  376. return 0;
  377. }
  378. #ifndef OPENSSL_NO_DEPRECATED_3_0
  379. else {
  380. callback = BIO_get_callback(b);
  381. if ((callback != NULL) &&
  382. (callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e,
  383. 0L) <= 0))
  384. return 0;
  385. }
  386. #endif
  387. BIO_set_init(b, 1);
  388. if (!EVP_CipherInit_ex(ctx->cipher, c, NULL, k, i, e))
  389. return 0;
  390. if (callback_ex != NULL)
  391. return callback_ex(b, BIO_CB_CTRL | BIO_CB_RETURN, (const char *)c, 0,
  392. BIO_CTRL_SET, e, 1, NULL);
  393. #ifndef OPENSSL_NO_DEPRECATED_3_0
  394. else if (callback != NULL)
  395. return callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 1L);
  396. #endif
  397. return 1;
  398. }