bio_ok.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. /*-
  10. From: Arne Ansper
  11. Why BIO_f_reliable?
  12. I wrote function which took BIO* as argument, read data from it
  13. and processed it. Then I wanted to store the input file in
  14. encrypted form. OK I pushed BIO_f_cipher to the BIO stack
  15. and everything was OK. BUT if user types wrong password
  16. BIO_f_cipher outputs only garbage and my function crashes. Yes
  17. I can and I should fix my function, but BIO_f_cipher is
  18. easy way to add encryption support to many existing applications
  19. and it's hard to debug and fix them all.
  20. So I wanted another BIO which would catch the incorrect passwords and
  21. file damages which cause garbage on BIO_f_cipher's output.
  22. The easy way is to push the BIO_f_md and save the checksum at
  23. the end of the file. However there are several problems with this
  24. approach:
  25. 1) you must somehow separate checksum from actual data.
  26. 2) you need lot's of memory when reading the file, because you
  27. must read to the end of the file and verify the checksum before
  28. letting the application to read the data.
  29. BIO_f_reliable tries to solve both problems, so that you can
  30. read and write arbitrary long streams using only fixed amount
  31. of memory.
  32. BIO_f_reliable splits data stream into blocks. Each block is prefixed
  33. with its length and suffixed with its digest. So you need only
  34. several Kbytes of memory to buffer single block before verifying
  35. its digest.
  36. BIO_f_reliable goes further and adds several important capabilities:
  37. 1) the digest of the block is computed over the whole stream
  38. -- so nobody can rearrange the blocks or remove or replace them.
  39. 2) to detect invalid passwords right at the start BIO_f_reliable
  40. adds special prefix to the stream. In order to avoid known plain-text
  41. attacks this prefix is generated as follows:
  42. *) digest is initialized with random seed instead of
  43. standardized one.
  44. *) same seed is written to output
  45. *) well-known text is then hashed and the output
  46. of the digest is also written to output.
  47. reader can now read the seed from stream, hash the same string
  48. and then compare the digest output.
  49. Bad things: BIO_f_reliable knows what's going on in EVP_Digest. I
  50. initially wrote and tested this code on x86 machine and wrote the
  51. digests out in machine-dependent order :( There are people using
  52. this code and I cannot change this easily without making existing
  53. data files unreadable.
  54. */
  55. #include <stdio.h>
  56. #include <errno.h>
  57. #include <assert.h>
  58. #include "internal/cryptlib.h"
  59. #include <openssl/buffer.h>
  60. #include "internal/bio.h"
  61. #include <openssl/evp.h>
  62. #include <openssl/rand.h>
  63. #include "internal/endian.h"
  64. #include "crypto/evp.h"
  65. static int ok_write(BIO *h, const char *buf, int num);
  66. static int ok_read(BIO *h, char *buf, int size);
  67. static long ok_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  68. static int ok_new(BIO *h);
  69. static int ok_free(BIO *data);
  70. static long ok_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
  71. static __owur int sig_out(BIO *b);
  72. static __owur int sig_in(BIO *b);
  73. static __owur int block_out(BIO *b);
  74. static __owur int block_in(BIO *b);
  75. #define OK_BLOCK_SIZE (1024*4)
  76. #define OK_BLOCK_BLOCK 4
  77. #define IOBS (OK_BLOCK_SIZE+ OK_BLOCK_BLOCK+ 3*EVP_MAX_MD_SIZE)
  78. #define WELLKNOWN "The quick brown fox jumped over the lazy dog's back."
  79. typedef struct ok_struct {
  80. size_t buf_len;
  81. size_t buf_off;
  82. size_t buf_len_save;
  83. size_t buf_off_save;
  84. int cont; /* <= 0 when finished */
  85. int finished;
  86. EVP_MD_CTX *md;
  87. int blockout; /* output block is ready */
  88. int sigio; /* must process signature */
  89. unsigned char buf[IOBS];
  90. } BIO_OK_CTX;
  91. static const BIO_METHOD methods_ok = {
  92. BIO_TYPE_CIPHER,
  93. "reliable",
  94. bwrite_conv,
  95. ok_write,
  96. bread_conv,
  97. ok_read,
  98. NULL, /* ok_puts, */
  99. NULL, /* ok_gets, */
  100. ok_ctrl,
  101. ok_new,
  102. ok_free,
  103. ok_callback_ctrl,
  104. };
  105. const BIO_METHOD *BIO_f_reliable(void)
  106. {
  107. return &methods_ok;
  108. }
  109. static int ok_new(BIO *bi)
  110. {
  111. BIO_OK_CTX *ctx;
  112. if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
  113. return 0;
  114. ctx->cont = 1;
  115. ctx->sigio = 1;
  116. ctx->md = EVP_MD_CTX_new();
  117. if (ctx->md == NULL) {
  118. OPENSSL_free(ctx);
  119. return 0;
  120. }
  121. BIO_set_init(bi, 0);
  122. BIO_set_data(bi, ctx);
  123. return 1;
  124. }
  125. static int ok_free(BIO *a)
  126. {
  127. BIO_OK_CTX *ctx;
  128. if (a == NULL)
  129. return 0;
  130. ctx = BIO_get_data(a);
  131. EVP_MD_CTX_free(ctx->md);
  132. OPENSSL_clear_free(ctx, sizeof(BIO_OK_CTX));
  133. BIO_set_data(a, NULL);
  134. BIO_set_init(a, 0);
  135. return 1;
  136. }
  137. static int ok_read(BIO *b, char *out, int outl)
  138. {
  139. int ret = 0, i, n;
  140. BIO_OK_CTX *ctx;
  141. BIO *next;
  142. if (out == NULL)
  143. return 0;
  144. ctx = BIO_get_data(b);
  145. next = BIO_next(b);
  146. if ((ctx == NULL) || (next == NULL) || (BIO_get_init(b) == 0))
  147. return 0;
  148. while (outl > 0) {
  149. /* copy clean bytes to output buffer */
  150. if (ctx->blockout) {
  151. i = ctx->buf_len - ctx->buf_off;
  152. if (i > outl)
  153. i = outl;
  154. memcpy(out, &(ctx->buf[ctx->buf_off]), i);
  155. ret += i;
  156. out += i;
  157. outl -= i;
  158. ctx->buf_off += i;
  159. /* all clean bytes are out */
  160. if (ctx->buf_len == ctx->buf_off) {
  161. ctx->buf_off = 0;
  162. /*
  163. * copy start of the next block into proper place
  164. */
  165. if (ctx->buf_len_save > ctx->buf_off_save) {
  166. ctx->buf_len = ctx->buf_len_save - ctx->buf_off_save;
  167. memmove(ctx->buf, &(ctx->buf[ctx->buf_off_save]),
  168. ctx->buf_len);
  169. } else {
  170. ctx->buf_len = 0;
  171. }
  172. ctx->blockout = 0;
  173. }
  174. }
  175. /* output buffer full -- cancel */
  176. if (outl == 0)
  177. break;
  178. /* no clean bytes in buffer -- fill it */
  179. n = IOBS - ctx->buf_len;
  180. i = BIO_read(next, &(ctx->buf[ctx->buf_len]), n);
  181. if (i <= 0)
  182. break; /* nothing new */
  183. ctx->buf_len += i;
  184. /* no signature yet -- check if we got one */
  185. if (ctx->sigio == 1) {
  186. if (!sig_in(b)) {
  187. BIO_clear_retry_flags(b);
  188. return 0;
  189. }
  190. }
  191. /* signature ok -- check if we got block */
  192. if (ctx->sigio == 0) {
  193. if (!block_in(b)) {
  194. BIO_clear_retry_flags(b);
  195. return 0;
  196. }
  197. }
  198. /* invalid block -- cancel */
  199. if (ctx->cont <= 0)
  200. break;
  201. }
  202. BIO_clear_retry_flags(b);
  203. BIO_copy_next_retry(b);
  204. return ret;
  205. }
  206. static int ok_write(BIO *b, const char *in, int inl)
  207. {
  208. int ret = 0, n, i;
  209. BIO_OK_CTX *ctx;
  210. BIO *next;
  211. if (inl <= 0)
  212. return inl;
  213. ctx = BIO_get_data(b);
  214. next = BIO_next(b);
  215. ret = inl;
  216. if ((ctx == NULL) || (next == NULL) || (BIO_get_init(b) == 0))
  217. return 0;
  218. if (ctx->sigio && !sig_out(b))
  219. return 0;
  220. do {
  221. BIO_clear_retry_flags(b);
  222. n = ctx->buf_len - ctx->buf_off;
  223. while (ctx->blockout && n > 0) {
  224. i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
  225. if (i <= 0) {
  226. BIO_copy_next_retry(b);
  227. if (!BIO_should_retry(b))
  228. ctx->cont = 0;
  229. return i;
  230. }
  231. ctx->buf_off += i;
  232. n -= i;
  233. }
  234. /* at this point all pending data has been written */
  235. ctx->blockout = 0;
  236. if (ctx->buf_len == ctx->buf_off) {
  237. ctx->buf_len = OK_BLOCK_BLOCK;
  238. ctx->buf_off = 0;
  239. }
  240. if ((in == NULL) || (inl <= 0))
  241. return 0;
  242. n = (inl + ctx->buf_len > OK_BLOCK_SIZE + OK_BLOCK_BLOCK) ?
  243. (int)(OK_BLOCK_SIZE + OK_BLOCK_BLOCK - ctx->buf_len) : inl;
  244. memcpy(&ctx->buf[ctx->buf_len], in, n);
  245. ctx->buf_len += n;
  246. inl -= n;
  247. in += n;
  248. if (ctx->buf_len >= OK_BLOCK_SIZE + OK_BLOCK_BLOCK) {
  249. if (!block_out(b)) {
  250. BIO_clear_retry_flags(b);
  251. return 0;
  252. }
  253. }
  254. } while (inl > 0);
  255. BIO_clear_retry_flags(b);
  256. BIO_copy_next_retry(b);
  257. return ret;
  258. }
  259. static long ok_ctrl(BIO *b, int cmd, long num, void *ptr)
  260. {
  261. BIO_OK_CTX *ctx;
  262. EVP_MD *md;
  263. const EVP_MD **ppmd;
  264. long ret = 1;
  265. int i;
  266. BIO *next;
  267. ctx = BIO_get_data(b);
  268. next = BIO_next(b);
  269. switch (cmd) {
  270. case BIO_CTRL_RESET:
  271. ctx->buf_len = 0;
  272. ctx->buf_off = 0;
  273. ctx->buf_len_save = 0;
  274. ctx->buf_off_save = 0;
  275. ctx->cont = 1;
  276. ctx->finished = 0;
  277. ctx->blockout = 0;
  278. ctx->sigio = 1;
  279. ret = BIO_ctrl(next, cmd, num, ptr);
  280. break;
  281. case BIO_CTRL_EOF: /* More to read */
  282. if (ctx->cont <= 0)
  283. ret = 1;
  284. else
  285. ret = BIO_ctrl(next, cmd, num, ptr);
  286. break;
  287. case BIO_CTRL_PENDING: /* More to read in buffer */
  288. case BIO_CTRL_WPENDING: /* More to read in buffer */
  289. ret = ctx->blockout ? ctx->buf_len - ctx->buf_off : 0;
  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. if (ctx->blockout == 0)
  296. if (!block_out(b))
  297. return 0;
  298. while (ctx->blockout) {
  299. i = ok_write(b, NULL, 0);
  300. if (i < 0) {
  301. ret = i;
  302. break;
  303. }
  304. }
  305. ctx->finished = 1;
  306. ctx->buf_off = ctx->buf_len = 0;
  307. ctx->cont = (int)ret;
  308. /* Finally flush the underlying BIO */
  309. ret = BIO_ctrl(next, cmd, num, ptr);
  310. break;
  311. case BIO_C_DO_STATE_MACHINE:
  312. BIO_clear_retry_flags(b);
  313. ret = BIO_ctrl(next, cmd, num, ptr);
  314. BIO_copy_next_retry(b);
  315. break;
  316. case BIO_CTRL_INFO:
  317. ret = (long)ctx->cont;
  318. break;
  319. case BIO_C_SET_MD:
  320. md = ptr;
  321. if (!EVP_DigestInit_ex(ctx->md, md, NULL))
  322. return 0;
  323. BIO_set_init(b, 1);
  324. break;
  325. case BIO_C_GET_MD:
  326. if (BIO_get_init(b)) {
  327. ppmd = ptr;
  328. *ppmd = EVP_MD_CTX_get0_md(ctx->md);
  329. } else
  330. ret = 0;
  331. break;
  332. default:
  333. ret = BIO_ctrl(next, cmd, num, ptr);
  334. break;
  335. }
  336. return ret;
  337. }
  338. static long ok_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  339. {
  340. BIO *next;
  341. next = BIO_next(b);
  342. if (next == NULL)
  343. return 0;
  344. return BIO_callback_ctrl(next, cmd, fp);
  345. }
  346. static void longswap(void *_ptr, size_t len)
  347. {
  348. DECLARE_IS_ENDIAN;
  349. if (IS_LITTLE_ENDIAN) {
  350. size_t i;
  351. unsigned char *p = _ptr, c;
  352. for (i = 0; i < len; i += 4) {
  353. c = p[0], p[0] = p[3], p[3] = c;
  354. c = p[1], p[1] = p[2], p[2] = c;
  355. }
  356. }
  357. }
  358. static int sig_out(BIO *b)
  359. {
  360. BIO_OK_CTX *ctx;
  361. EVP_MD_CTX *md;
  362. const EVP_MD *digest;
  363. int md_size;
  364. void *md_data;
  365. ctx = BIO_get_data(b);
  366. md = ctx->md;
  367. digest = EVP_MD_CTX_get0_md(md);
  368. md_size = EVP_MD_get_size(digest);
  369. md_data = EVP_MD_CTX_get0_md_data(md);
  370. if (ctx->buf_len + 2 * md_size > OK_BLOCK_SIZE)
  371. return 1;
  372. if (!EVP_DigestInit_ex(md, digest, NULL))
  373. goto berr;
  374. /*
  375. * FIXME: there's absolutely no guarantee this makes any sense at all,
  376. * particularly now EVP_MD_CTX has been restructured.
  377. */
  378. if (RAND_bytes(md_data, md_size) <= 0)
  379. goto berr;
  380. memcpy(&(ctx->buf[ctx->buf_len]), md_data, md_size);
  381. longswap(&(ctx->buf[ctx->buf_len]), md_size);
  382. ctx->buf_len += md_size;
  383. if (!EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN)))
  384. goto berr;
  385. if (!EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL))
  386. goto berr;
  387. ctx->buf_len += md_size;
  388. ctx->blockout = 1;
  389. ctx->sigio = 0;
  390. return 1;
  391. berr:
  392. BIO_clear_retry_flags(b);
  393. return 0;
  394. }
  395. static int sig_in(BIO *b)
  396. {
  397. BIO_OK_CTX *ctx;
  398. EVP_MD_CTX *md;
  399. unsigned char tmp[EVP_MAX_MD_SIZE];
  400. int ret = 0;
  401. const EVP_MD *digest;
  402. int md_size;
  403. void *md_data;
  404. ctx = BIO_get_data(b);
  405. if ((md = ctx->md) == NULL)
  406. goto berr;
  407. digest = EVP_MD_CTX_get0_md(md);
  408. if ((md_size = EVP_MD_get_size(digest)) < 0)
  409. goto berr;
  410. md_data = EVP_MD_CTX_get0_md_data(md);
  411. if ((int)(ctx->buf_len - ctx->buf_off) < 2 * md_size)
  412. return 1;
  413. if (!EVP_DigestInit_ex(md, digest, NULL))
  414. goto berr;
  415. memcpy(md_data, &(ctx->buf[ctx->buf_off]), md_size);
  416. longswap(md_data, md_size);
  417. ctx->buf_off += md_size;
  418. if (!EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN)))
  419. goto berr;
  420. if (!EVP_DigestFinal_ex(md, tmp, NULL))
  421. goto berr;
  422. ret = memcmp(&(ctx->buf[ctx->buf_off]), tmp, md_size) == 0;
  423. ctx->buf_off += md_size;
  424. if (ret == 1) {
  425. ctx->sigio = 0;
  426. if (ctx->buf_len != ctx->buf_off) {
  427. memmove(ctx->buf, &(ctx->buf[ctx->buf_off]),
  428. ctx->buf_len - ctx->buf_off);
  429. }
  430. ctx->buf_len -= ctx->buf_off;
  431. ctx->buf_off = 0;
  432. } else {
  433. ctx->cont = 0;
  434. }
  435. return 1;
  436. berr:
  437. BIO_clear_retry_flags(b);
  438. return 0;
  439. }
  440. static int block_out(BIO *b)
  441. {
  442. BIO_OK_CTX *ctx;
  443. EVP_MD_CTX *md;
  444. unsigned long tl;
  445. const EVP_MD *digest;
  446. int md_size;
  447. ctx = BIO_get_data(b);
  448. md = ctx->md;
  449. digest = EVP_MD_CTX_get0_md(md);
  450. md_size = EVP_MD_get_size(digest);
  451. tl = ctx->buf_len - OK_BLOCK_BLOCK;
  452. ctx->buf[0] = (unsigned char)(tl >> 24);
  453. ctx->buf[1] = (unsigned char)(tl >> 16);
  454. ctx->buf[2] = (unsigned char)(tl >> 8);
  455. ctx->buf[3] = (unsigned char)(tl);
  456. if (!EVP_DigestUpdate(md,
  457. (unsigned char *)&(ctx->buf[OK_BLOCK_BLOCK]), tl))
  458. goto berr;
  459. if (!EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL))
  460. goto berr;
  461. ctx->buf_len += md_size;
  462. ctx->blockout = 1;
  463. return 1;
  464. berr:
  465. BIO_clear_retry_flags(b);
  466. return 0;
  467. }
  468. static int block_in(BIO *b)
  469. {
  470. BIO_OK_CTX *ctx;
  471. EVP_MD_CTX *md;
  472. unsigned long tl = 0;
  473. unsigned char tmp[EVP_MAX_MD_SIZE];
  474. int md_size;
  475. ctx = BIO_get_data(b);
  476. md = ctx->md;
  477. md_size = EVP_MD_get_size(EVP_MD_CTX_get0_md(md));
  478. if (md_size < 0)
  479. goto berr;
  480. assert(sizeof(tl) >= OK_BLOCK_BLOCK); /* always true */
  481. tl = ctx->buf[0];
  482. tl <<= 8;
  483. tl |= ctx->buf[1];
  484. tl <<= 8;
  485. tl |= ctx->buf[2];
  486. tl <<= 8;
  487. tl |= ctx->buf[3];
  488. if (ctx->buf_len < tl + OK_BLOCK_BLOCK + md_size)
  489. return 1;
  490. if (!EVP_DigestUpdate(md,
  491. (unsigned char *)&(ctx->buf[OK_BLOCK_BLOCK]), tl))
  492. goto berr;
  493. if (!EVP_DigestFinal_ex(md, tmp, NULL))
  494. goto berr;
  495. if (memcmp(&(ctx->buf[tl + OK_BLOCK_BLOCK]), tmp, md_size) == 0) {
  496. /* there might be parts from next block lurking around ! */
  497. ctx->buf_off_save = tl + OK_BLOCK_BLOCK + md_size;
  498. ctx->buf_len_save = ctx->buf_len;
  499. ctx->buf_off = OK_BLOCK_BLOCK;
  500. ctx->buf_len = tl + OK_BLOCK_BLOCK;
  501. ctx->blockout = 1;
  502. } else {
  503. ctx->cont = 0;
  504. }
  505. return 1;
  506. berr:
  507. BIO_clear_retry_flags(b);
  508. return 0;
  509. }