bio_ok.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /*
  2. * Copyright 1995-2020 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. /* TODO: Convert to new style write function */
  95. bwrite_conv,
  96. ok_write,
  97. /* TODO: Convert to new style read function */
  98. bread_conv,
  99. ok_read,
  100. NULL, /* ok_puts, */
  101. NULL, /* ok_gets, */
  102. ok_ctrl,
  103. ok_new,
  104. ok_free,
  105. ok_callback_ctrl,
  106. };
  107. const BIO_METHOD *BIO_f_reliable(void)
  108. {
  109. return &methods_ok;
  110. }
  111. static int ok_new(BIO *bi)
  112. {
  113. BIO_OK_CTX *ctx;
  114. if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
  115. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  116. return 0;
  117. }
  118. ctx->cont = 1;
  119. ctx->sigio = 1;
  120. ctx->md = EVP_MD_CTX_new();
  121. if (ctx->md == NULL) {
  122. OPENSSL_free(ctx);
  123. return 0;
  124. }
  125. BIO_set_init(bi, 0);
  126. BIO_set_data(bi, ctx);
  127. return 1;
  128. }
  129. static int ok_free(BIO *a)
  130. {
  131. BIO_OK_CTX *ctx;
  132. if (a == NULL)
  133. return 0;
  134. ctx = BIO_get_data(a);
  135. EVP_MD_CTX_free(ctx->md);
  136. OPENSSL_clear_free(ctx, sizeof(BIO_OK_CTX));
  137. BIO_set_data(a, NULL);
  138. BIO_set_init(a, 0);
  139. return 1;
  140. }
  141. static int ok_read(BIO *b, char *out, int outl)
  142. {
  143. int ret = 0, i, n;
  144. BIO_OK_CTX *ctx;
  145. BIO *next;
  146. if (out == NULL)
  147. return 0;
  148. ctx = BIO_get_data(b);
  149. next = BIO_next(b);
  150. if ((ctx == NULL) || (next == NULL) || (BIO_get_init(b) == 0))
  151. return 0;
  152. while (outl > 0) {
  153. /* copy clean bytes to output buffer */
  154. if (ctx->blockout) {
  155. i = ctx->buf_len - ctx->buf_off;
  156. if (i > outl)
  157. i = outl;
  158. memcpy(out, &(ctx->buf[ctx->buf_off]), i);
  159. ret += i;
  160. out += i;
  161. outl -= i;
  162. ctx->buf_off += i;
  163. /* all clean bytes are out */
  164. if (ctx->buf_len == ctx->buf_off) {
  165. ctx->buf_off = 0;
  166. /*
  167. * copy start of the next block into proper place
  168. */
  169. if (ctx->buf_len_save > ctx->buf_off_save) {
  170. ctx->buf_len = ctx->buf_len_save - ctx->buf_off_save;
  171. memmove(ctx->buf, &(ctx->buf[ctx->buf_off_save]),
  172. ctx->buf_len);
  173. } else {
  174. ctx->buf_len = 0;
  175. }
  176. ctx->blockout = 0;
  177. }
  178. }
  179. /* output buffer full -- cancel */
  180. if (outl == 0)
  181. break;
  182. /* no clean bytes in buffer -- fill it */
  183. n = IOBS - ctx->buf_len;
  184. i = BIO_read(next, &(ctx->buf[ctx->buf_len]), n);
  185. if (i <= 0)
  186. break; /* nothing new */
  187. ctx->buf_len += i;
  188. /* no signature yet -- check if we got one */
  189. if (ctx->sigio == 1) {
  190. if (!sig_in(b)) {
  191. BIO_clear_retry_flags(b);
  192. return 0;
  193. }
  194. }
  195. /* signature ok -- check if we got block */
  196. if (ctx->sigio == 0) {
  197. if (!block_in(b)) {
  198. BIO_clear_retry_flags(b);
  199. return 0;
  200. }
  201. }
  202. /* invalid block -- cancel */
  203. if (ctx->cont <= 0)
  204. break;
  205. }
  206. BIO_clear_retry_flags(b);
  207. BIO_copy_next_retry(b);
  208. return ret;
  209. }
  210. static int ok_write(BIO *b, const char *in, int inl)
  211. {
  212. int ret = 0, n, i;
  213. BIO_OK_CTX *ctx;
  214. BIO *next;
  215. if (inl <= 0)
  216. return inl;
  217. ctx = BIO_get_data(b);
  218. next = BIO_next(b);
  219. ret = inl;
  220. if ((ctx == NULL) || (next == NULL) || (BIO_get_init(b) == 0))
  221. return 0;
  222. if (ctx->sigio && !sig_out(b))
  223. return 0;
  224. do {
  225. BIO_clear_retry_flags(b);
  226. n = ctx->buf_len - ctx->buf_off;
  227. while (ctx->blockout && n > 0) {
  228. i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
  229. if (i <= 0) {
  230. BIO_copy_next_retry(b);
  231. if (!BIO_should_retry(b))
  232. ctx->cont = 0;
  233. return i;
  234. }
  235. ctx->buf_off += i;
  236. n -= i;
  237. }
  238. /* at this point all pending data has been written */
  239. ctx->blockout = 0;
  240. if (ctx->buf_len == ctx->buf_off) {
  241. ctx->buf_len = OK_BLOCK_BLOCK;
  242. ctx->buf_off = 0;
  243. }
  244. if ((in == NULL) || (inl <= 0))
  245. return 0;
  246. n = (inl + ctx->buf_len > OK_BLOCK_SIZE + OK_BLOCK_BLOCK) ?
  247. (int)(OK_BLOCK_SIZE + OK_BLOCK_BLOCK - ctx->buf_len) : inl;
  248. memcpy(&ctx->buf[ctx->buf_len], in, n);
  249. ctx->buf_len += n;
  250. inl -= n;
  251. in += n;
  252. if (ctx->buf_len >= OK_BLOCK_SIZE + OK_BLOCK_BLOCK) {
  253. if (!block_out(b)) {
  254. BIO_clear_retry_flags(b);
  255. return 0;
  256. }
  257. }
  258. } while (inl > 0);
  259. BIO_clear_retry_flags(b);
  260. BIO_copy_next_retry(b);
  261. return ret;
  262. }
  263. static long ok_ctrl(BIO *b, int cmd, long num, void *ptr)
  264. {
  265. BIO_OK_CTX *ctx;
  266. EVP_MD *md;
  267. const EVP_MD **ppmd;
  268. long ret = 1;
  269. int i;
  270. BIO *next;
  271. ctx = BIO_get_data(b);
  272. next = BIO_next(b);
  273. switch (cmd) {
  274. case BIO_CTRL_RESET:
  275. ctx->buf_len = 0;
  276. ctx->buf_off = 0;
  277. ctx->buf_len_save = 0;
  278. ctx->buf_off_save = 0;
  279. ctx->cont = 1;
  280. ctx->finished = 0;
  281. ctx->blockout = 0;
  282. ctx->sigio = 1;
  283. ret = BIO_ctrl(next, cmd, num, ptr);
  284. break;
  285. case BIO_CTRL_EOF: /* More to read */
  286. if (ctx->cont <= 0)
  287. ret = 1;
  288. else
  289. ret = BIO_ctrl(next, cmd, num, ptr);
  290. break;
  291. case BIO_CTRL_PENDING: /* More to read in buffer */
  292. case BIO_CTRL_WPENDING: /* More to read in buffer */
  293. ret = ctx->blockout ? ctx->buf_len - ctx->buf_off : 0;
  294. if (ret <= 0)
  295. ret = BIO_ctrl(next, cmd, num, ptr);
  296. break;
  297. case BIO_CTRL_FLUSH:
  298. /* do a final write */
  299. if (ctx->blockout == 0)
  300. if (!block_out(b))
  301. return 0;
  302. while (ctx->blockout) {
  303. i = ok_write(b, NULL, 0);
  304. if (i < 0) {
  305. ret = i;
  306. break;
  307. }
  308. }
  309. ctx->finished = 1;
  310. ctx->buf_off = ctx->buf_len = 0;
  311. ctx->cont = (int)ret;
  312. /* Finally flush the underlying BIO */
  313. ret = BIO_ctrl(next, cmd, num, ptr);
  314. break;
  315. case BIO_C_DO_STATE_MACHINE:
  316. BIO_clear_retry_flags(b);
  317. ret = BIO_ctrl(next, cmd, num, ptr);
  318. BIO_copy_next_retry(b);
  319. break;
  320. case BIO_CTRL_INFO:
  321. ret = (long)ctx->cont;
  322. break;
  323. case BIO_C_SET_MD:
  324. md = ptr;
  325. if (!EVP_DigestInit_ex(ctx->md, md, NULL))
  326. return 0;
  327. BIO_set_init(b, 1);
  328. break;
  329. case BIO_C_GET_MD:
  330. if (BIO_get_init(b)) {
  331. ppmd = ptr;
  332. *ppmd = EVP_MD_CTX_md(ctx->md);
  333. } else
  334. ret = 0;
  335. break;
  336. default:
  337. ret = BIO_ctrl(next, cmd, num, ptr);
  338. break;
  339. }
  340. return ret;
  341. }
  342. static long ok_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  343. {
  344. BIO *next;
  345. next = BIO_next(b);
  346. if (next == NULL)
  347. return 0;
  348. return BIO_callback_ctrl(next, cmd, fp);
  349. }
  350. static void longswap(void *_ptr, size_t len)
  351. {
  352. DECLARE_IS_ENDIAN;
  353. if (IS_LITTLE_ENDIAN) {
  354. size_t i;
  355. unsigned char *p = _ptr, c;
  356. for (i = 0; i < len; i += 4) {
  357. c = p[0], p[0] = p[3], p[3] = c;
  358. c = p[1], p[1] = p[2], p[2] = c;
  359. }
  360. }
  361. }
  362. static int sig_out(BIO *b)
  363. {
  364. BIO_OK_CTX *ctx;
  365. EVP_MD_CTX *md;
  366. const EVP_MD *digest;
  367. int md_size;
  368. void *md_data;
  369. ctx = BIO_get_data(b);
  370. md = ctx->md;
  371. digest = EVP_MD_CTX_md(md);
  372. md_size = EVP_MD_size(digest);
  373. md_data = EVP_MD_CTX_md_data(md);
  374. if (ctx->buf_len + 2 * md_size > OK_BLOCK_SIZE)
  375. return 1;
  376. if (!EVP_DigestInit_ex(md, digest, NULL))
  377. goto berr;
  378. /*
  379. * FIXME: there's absolutely no guarantee this makes any sense at all,
  380. * particularly now EVP_MD_CTX has been restructured.
  381. */
  382. if (RAND_bytes(md_data, md_size) <= 0)
  383. goto berr;
  384. memcpy(&(ctx->buf[ctx->buf_len]), md_data, md_size);
  385. longswap(&(ctx->buf[ctx->buf_len]), md_size);
  386. ctx->buf_len += md_size;
  387. if (!EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN)))
  388. goto berr;
  389. if (!EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL))
  390. goto berr;
  391. ctx->buf_len += md_size;
  392. ctx->blockout = 1;
  393. ctx->sigio = 0;
  394. return 1;
  395. berr:
  396. BIO_clear_retry_flags(b);
  397. return 0;
  398. }
  399. static int sig_in(BIO *b)
  400. {
  401. BIO_OK_CTX *ctx;
  402. EVP_MD_CTX *md;
  403. unsigned char tmp[EVP_MAX_MD_SIZE];
  404. int ret = 0;
  405. const EVP_MD *digest;
  406. int md_size;
  407. void *md_data;
  408. ctx = BIO_get_data(b);
  409. md = ctx->md;
  410. digest = EVP_MD_CTX_md(md);
  411. md_size = EVP_MD_size(digest);
  412. md_data = EVP_MD_CTX_md_data(md);
  413. if ((int)(ctx->buf_len - ctx->buf_off) < 2 * md_size)
  414. return 1;
  415. if (!EVP_DigestInit_ex(md, digest, NULL))
  416. goto berr;
  417. memcpy(md_data, &(ctx->buf[ctx->buf_off]), md_size);
  418. longswap(md_data, md_size);
  419. ctx->buf_off += md_size;
  420. if (!EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN)))
  421. goto berr;
  422. if (!EVP_DigestFinal_ex(md, tmp, NULL))
  423. goto berr;
  424. ret = memcmp(&(ctx->buf[ctx->buf_off]), tmp, md_size) == 0;
  425. ctx->buf_off += md_size;
  426. if (ret == 1) {
  427. ctx->sigio = 0;
  428. if (ctx->buf_len != ctx->buf_off) {
  429. memmove(ctx->buf, &(ctx->buf[ctx->buf_off]),
  430. ctx->buf_len - ctx->buf_off);
  431. }
  432. ctx->buf_len -= ctx->buf_off;
  433. ctx->buf_off = 0;
  434. } else {
  435. ctx->cont = 0;
  436. }
  437. return 1;
  438. berr:
  439. BIO_clear_retry_flags(b);
  440. return 0;
  441. }
  442. static int block_out(BIO *b)
  443. {
  444. BIO_OK_CTX *ctx;
  445. EVP_MD_CTX *md;
  446. unsigned long tl;
  447. const EVP_MD *digest;
  448. int md_size;
  449. ctx = BIO_get_data(b);
  450. md = ctx->md;
  451. digest = EVP_MD_CTX_md(md);
  452. md_size = EVP_MD_size(digest);
  453. tl = ctx->buf_len - OK_BLOCK_BLOCK;
  454. ctx->buf[0] = (unsigned char)(tl >> 24);
  455. ctx->buf[1] = (unsigned char)(tl >> 16);
  456. ctx->buf[2] = (unsigned char)(tl >> 8);
  457. ctx->buf[3] = (unsigned char)(tl);
  458. if (!EVP_DigestUpdate(md,
  459. (unsigned char *)&(ctx->buf[OK_BLOCK_BLOCK]), tl))
  460. goto berr;
  461. if (!EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL))
  462. goto berr;
  463. ctx->buf_len += md_size;
  464. ctx->blockout = 1;
  465. return 1;
  466. berr:
  467. BIO_clear_retry_flags(b);
  468. return 0;
  469. }
  470. static int block_in(BIO *b)
  471. {
  472. BIO_OK_CTX *ctx;
  473. EVP_MD_CTX *md;
  474. unsigned long tl = 0;
  475. unsigned char tmp[EVP_MAX_MD_SIZE];
  476. int md_size;
  477. ctx = BIO_get_data(b);
  478. md = ctx->md;
  479. md_size = EVP_MD_size(EVP_MD_CTX_md(md));
  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. }