bio_enc.c 12 KB

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