bio_ok.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 it's length and suffixed with it's digest. So you need only
  34. several Kbytes of memory to buffer single block before verifying
  35. it's 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/evp_int.h"
  64. static int ok_write(BIO *h, const char *buf, int num);
  65. static int ok_read(BIO *h, char *buf, int size);
  66. static long ok_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  67. static int ok_new(BIO *h);
  68. static int ok_free(BIO *data);
  69. static long ok_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
  70. static __owur int sig_out(BIO *b);
  71. static __owur int sig_in(BIO *b);
  72. static __owur int block_out(BIO *b);
  73. static __owur int block_in(BIO *b);
  74. #define OK_BLOCK_SIZE (1024*4)
  75. #define OK_BLOCK_BLOCK 4
  76. #define IOBS (OK_BLOCK_SIZE+ OK_BLOCK_BLOCK+ 3*EVP_MAX_MD_SIZE)
  77. #define WELLKNOWN "The quick brown fox jumped over the lazy dog's back."
  78. typedef struct ok_struct {
  79. size_t buf_len;
  80. size_t buf_off;
  81. size_t buf_len_save;
  82. size_t buf_off_save;
  83. int cont; /* <= 0 when finished */
  84. int finished;
  85. EVP_MD_CTX *md;
  86. int blockout; /* output block is ready */
  87. int sigio; /* must process signature */
  88. unsigned char buf[IOBS];
  89. } BIO_OK_CTX;
  90. static const BIO_METHOD methods_ok = {
  91. BIO_TYPE_CIPHER,
  92. "reliable",
  93. /* TODO: Convert to new style write function */
  94. bwrite_conv,
  95. ok_write,
  96. /* TODO: Convert to new style read function */
  97. bread_conv,
  98. ok_read,
  99. NULL, /* ok_puts, */
  100. NULL, /* ok_gets, */
  101. ok_ctrl,
  102. ok_new,
  103. ok_free,
  104. ok_callback_ctrl,
  105. };
  106. const BIO_METHOD *BIO_f_reliable(void)
  107. {
  108. return &methods_ok;
  109. }
  110. static int ok_new(BIO *bi)
  111. {
  112. BIO_OK_CTX *ctx;
  113. if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
  114. EVPerr(EVP_F_OK_NEW, ERR_R_MALLOC_FAILURE);
  115. return 0;
  116. }
  117. ctx->cont = 1;
  118. ctx->sigio = 1;
  119. ctx->md = EVP_MD_CTX_new();
  120. if (ctx->md == NULL) {
  121. OPENSSL_free(ctx);
  122. return 0;
  123. }
  124. BIO_set_init(bi, 0);
  125. BIO_set_data(bi, ctx);
  126. return 1;
  127. }
  128. static int ok_free(BIO *a)
  129. {
  130. BIO_OK_CTX *ctx;
  131. if (a == NULL)
  132. return 0;
  133. ctx = BIO_get_data(a);
  134. EVP_MD_CTX_free(ctx->md);
  135. OPENSSL_clear_free(ctx, sizeof(BIO_OK_CTX));
  136. BIO_set_data(a, NULL);
  137. BIO_set_init(a, 0);
  138. return 1;
  139. }
  140. static int ok_read(BIO *b, char *out, int outl)
  141. {
  142. int ret = 0, i, n;
  143. BIO_OK_CTX *ctx;
  144. BIO *next;
  145. if (out == NULL)
  146. return 0;
  147. ctx = BIO_get_data(b);
  148. next = BIO_next(b);
  149. if ((ctx == NULL) || (next == NULL) || (BIO_get_init(b) == 0))
  150. return 0;
  151. while (outl > 0) {
  152. /* copy clean bytes to output buffer */
  153. if (ctx->blockout) {
  154. i = ctx->buf_len - ctx->buf_off;
  155. if (i > outl)
  156. i = outl;
  157. memcpy(out, &(ctx->buf[ctx->buf_off]), i);
  158. ret += i;
  159. out += i;
  160. outl -= i;
  161. ctx->buf_off += i;
  162. /* all clean bytes are out */
  163. if (ctx->buf_len == ctx->buf_off) {
  164. ctx->buf_off = 0;
  165. /*
  166. * copy start of the next block into proper place
  167. */
  168. if (ctx->buf_len_save - ctx->buf_off_save > 0) {
  169. ctx->buf_len = ctx->buf_len_save - ctx->buf_off_save;
  170. memmove(ctx->buf, &(ctx->buf[ctx->buf_off_save]),
  171. ctx->buf_len);
  172. } else {
  173. ctx->buf_len = 0;
  174. }
  175. ctx->blockout = 0;
  176. }
  177. }
  178. /* output buffer full -- cancel */
  179. if (outl == 0)
  180. break;
  181. /* no clean bytes in buffer -- fill it */
  182. n = IOBS - ctx->buf_len;
  183. i = BIO_read(next, &(ctx->buf[ctx->buf_len]), n);
  184. if (i <= 0)
  185. break; /* nothing new */
  186. ctx->buf_len += i;
  187. /* no signature yet -- check if we got one */
  188. if (ctx->sigio == 1) {
  189. if (!sig_in(b)) {
  190. BIO_clear_retry_flags(b);
  191. return 0;
  192. }
  193. }
  194. /* signature ok -- check if we got block */
  195. if (ctx->sigio == 0) {
  196. if (!block_in(b)) {
  197. BIO_clear_retry_flags(b);
  198. return 0;
  199. }
  200. }
  201. /* invalid block -- cancel */
  202. if (ctx->cont <= 0)
  203. break;
  204. }
  205. BIO_clear_retry_flags(b);
  206. BIO_copy_next_retry(b);
  207. return ret;
  208. }
  209. static int ok_write(BIO *b, const char *in, int inl)
  210. {
  211. int ret = 0, n, i;
  212. BIO_OK_CTX *ctx;
  213. BIO *next;
  214. if (inl <= 0)
  215. return inl;
  216. ctx = BIO_get_data(b);
  217. next = BIO_next(b);
  218. ret = inl;
  219. if ((ctx == NULL) || (next == NULL) || (BIO_get_init(b) == 0))
  220. return 0;
  221. if (ctx->sigio && !sig_out(b))
  222. return 0;
  223. do {
  224. BIO_clear_retry_flags(b);
  225. n = ctx->buf_len - ctx->buf_off;
  226. while (ctx->blockout && n > 0) {
  227. i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
  228. if (i <= 0) {
  229. BIO_copy_next_retry(b);
  230. if (!BIO_should_retry(b))
  231. ctx->cont = 0;
  232. return i;
  233. }
  234. ctx->buf_off += i;
  235. n -= i;
  236. }
  237. /* at this point all pending data has been written */
  238. ctx->blockout = 0;
  239. if (ctx->buf_len == ctx->buf_off) {
  240. ctx->buf_len = OK_BLOCK_BLOCK;
  241. ctx->buf_off = 0;
  242. }
  243. if ((in == NULL) || (inl <= 0))
  244. return 0;
  245. n = (inl + ctx->buf_len > OK_BLOCK_SIZE + OK_BLOCK_BLOCK) ?
  246. (int)(OK_BLOCK_SIZE + OK_BLOCK_BLOCK - ctx->buf_len) : inl;
  247. memcpy(&ctx->buf[ctx->buf_len], in, n);
  248. ctx->buf_len += n;
  249. inl -= n;
  250. in += n;
  251. if (ctx->buf_len >= OK_BLOCK_SIZE + OK_BLOCK_BLOCK) {
  252. if (!block_out(b)) {
  253. BIO_clear_retry_flags(b);
  254. return 0;
  255. }
  256. }
  257. } while (inl > 0);
  258. BIO_clear_retry_flags(b);
  259. BIO_copy_next_retry(b);
  260. return ret;
  261. }
  262. static long ok_ctrl(BIO *b, int cmd, long num, void *ptr)
  263. {
  264. BIO_OK_CTX *ctx;
  265. EVP_MD *md;
  266. const EVP_MD **ppmd;
  267. long ret = 1;
  268. int i;
  269. BIO *next;
  270. ctx = BIO_get_data(b);
  271. next = BIO_next(b);
  272. switch (cmd) {
  273. case BIO_CTRL_RESET:
  274. ctx->buf_len = 0;
  275. ctx->buf_off = 0;
  276. ctx->buf_len_save = 0;
  277. ctx->buf_off_save = 0;
  278. ctx->cont = 1;
  279. ctx->finished = 0;
  280. ctx->blockout = 0;
  281. ctx->sigio = 1;
  282. ret = BIO_ctrl(next, cmd, num, ptr);
  283. break;
  284. case BIO_CTRL_EOF: /* More to read */
  285. if (ctx->cont <= 0)
  286. ret = 1;
  287. else
  288. ret = BIO_ctrl(next, cmd, num, ptr);
  289. break;
  290. case BIO_CTRL_PENDING: /* More to read in buffer */
  291. case BIO_CTRL_WPENDING: /* More to read in buffer */
  292. ret = ctx->blockout ? ctx->buf_len - ctx->buf_off : 0;
  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. if (ctx->blockout == 0)
  299. if (!block_out(b))
  300. return 0;
  301. while (ctx->blockout) {
  302. i = ok_write(b, NULL, 0);
  303. if (i < 0) {
  304. ret = i;
  305. break;
  306. }
  307. }
  308. ctx->finished = 1;
  309. ctx->buf_off = ctx->buf_len = 0;
  310. ctx->cont = (int)ret;
  311. /* Finally flush the underlying BIO */
  312. ret = BIO_ctrl(next, cmd, num, ptr);
  313. break;
  314. case BIO_C_DO_STATE_MACHINE:
  315. BIO_clear_retry_flags(b);
  316. ret = BIO_ctrl(next, cmd, num, ptr);
  317. BIO_copy_next_retry(b);
  318. break;
  319. case BIO_CTRL_INFO:
  320. ret = (long)ctx->cont;
  321. break;
  322. case BIO_C_SET_MD:
  323. md = ptr;
  324. if (!EVP_DigestInit_ex(ctx->md, md, NULL))
  325. return 0;
  326. BIO_set_init(b, 1);
  327. break;
  328. case BIO_C_GET_MD:
  329. if (BIO_get_init(b)) {
  330. ppmd = ptr;
  331. *ppmd = EVP_MD_CTX_md(ctx->md);
  332. } else
  333. ret = 0;
  334. break;
  335. default:
  336. ret = BIO_ctrl(next, cmd, num, ptr);
  337. break;
  338. }
  339. return ret;
  340. }
  341. static long ok_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  342. {
  343. long ret = 1;
  344. BIO *next;
  345. next = BIO_next(b);
  346. if (next == NULL)
  347. return 0;
  348. switch (cmd) {
  349. default:
  350. ret = BIO_callback_ctrl(next, cmd, fp);
  351. break;
  352. }
  353. return ret;
  354. }
  355. static void longswap(void *_ptr, size_t len)
  356. {
  357. const union {
  358. long one;
  359. char little;
  360. } is_endian = {
  361. 1
  362. };
  363. if (is_endian.little) {
  364. size_t i;
  365. unsigned char *p = _ptr, c;
  366. for (i = 0; i < len; i += 4) {
  367. c = p[0], p[0] = p[3], p[3] = c;
  368. c = p[1], p[1] = p[2], p[2] = c;
  369. }
  370. }
  371. }
  372. static int sig_out(BIO *b)
  373. {
  374. BIO_OK_CTX *ctx;
  375. EVP_MD_CTX *md;
  376. const EVP_MD *digest;
  377. int md_size;
  378. void *md_data;
  379. ctx = BIO_get_data(b);
  380. md = ctx->md;
  381. digest = EVP_MD_CTX_md(md);
  382. md_size = EVP_MD_size(digest);
  383. md_data = EVP_MD_CTX_md_data(md);
  384. if (ctx->buf_len + 2 * md_size > OK_BLOCK_SIZE)
  385. return 1;
  386. if (!EVP_DigestInit_ex(md, digest, NULL))
  387. goto berr;
  388. /*
  389. * FIXME: there's absolutely no guarantee this makes any sense at all,
  390. * particularly now EVP_MD_CTX has been restructured.
  391. */
  392. if (RAND_bytes(md_data, md_size) <= 0)
  393. goto berr;
  394. memcpy(&(ctx->buf[ctx->buf_len]), md_data, md_size);
  395. longswap(&(ctx->buf[ctx->buf_len]), md_size);
  396. ctx->buf_len += md_size;
  397. if (!EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN)))
  398. goto berr;
  399. if (!EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL))
  400. goto berr;
  401. ctx->buf_len += md_size;
  402. ctx->blockout = 1;
  403. ctx->sigio = 0;
  404. return 1;
  405. berr:
  406. BIO_clear_retry_flags(b);
  407. return 0;
  408. }
  409. static int sig_in(BIO *b)
  410. {
  411. BIO_OK_CTX *ctx;
  412. EVP_MD_CTX *md;
  413. unsigned char tmp[EVP_MAX_MD_SIZE];
  414. int ret = 0;
  415. const EVP_MD *digest;
  416. int md_size;
  417. void *md_data;
  418. ctx = BIO_get_data(b);
  419. md = ctx->md;
  420. digest = EVP_MD_CTX_md(md);
  421. md_size = EVP_MD_size(digest);
  422. md_data = EVP_MD_CTX_md_data(md);
  423. if ((int)(ctx->buf_len - ctx->buf_off) < 2 * md_size)
  424. return 1;
  425. if (!EVP_DigestInit_ex(md, digest, NULL))
  426. goto berr;
  427. memcpy(md_data, &(ctx->buf[ctx->buf_off]), md_size);
  428. longswap(md_data, md_size);
  429. ctx->buf_off += md_size;
  430. if (!EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN)))
  431. goto berr;
  432. if (!EVP_DigestFinal_ex(md, tmp, NULL))
  433. goto berr;
  434. ret = memcmp(&(ctx->buf[ctx->buf_off]), tmp, md_size) == 0;
  435. ctx->buf_off += md_size;
  436. if (ret == 1) {
  437. ctx->sigio = 0;
  438. if (ctx->buf_len != ctx->buf_off) {
  439. memmove(ctx->buf, &(ctx->buf[ctx->buf_off]),
  440. ctx->buf_len - ctx->buf_off);
  441. }
  442. ctx->buf_len -= ctx->buf_off;
  443. ctx->buf_off = 0;
  444. } else {
  445. ctx->cont = 0;
  446. }
  447. return 1;
  448. berr:
  449. BIO_clear_retry_flags(b);
  450. return 0;
  451. }
  452. static int block_out(BIO *b)
  453. {
  454. BIO_OK_CTX *ctx;
  455. EVP_MD_CTX *md;
  456. unsigned long tl;
  457. const EVP_MD *digest;
  458. int md_size;
  459. ctx = BIO_get_data(b);
  460. md = ctx->md;
  461. digest = EVP_MD_CTX_md(md);
  462. md_size = EVP_MD_size(digest);
  463. tl = ctx->buf_len - OK_BLOCK_BLOCK;
  464. ctx->buf[0] = (unsigned char)(tl >> 24);
  465. ctx->buf[1] = (unsigned char)(tl >> 16);
  466. ctx->buf[2] = (unsigned char)(tl >> 8);
  467. ctx->buf[3] = (unsigned char)(tl);
  468. if (!EVP_DigestUpdate(md,
  469. (unsigned char *)&(ctx->buf[OK_BLOCK_BLOCK]), tl))
  470. goto berr;
  471. if (!EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL))
  472. goto berr;
  473. ctx->buf_len += md_size;
  474. ctx->blockout = 1;
  475. return 1;
  476. berr:
  477. BIO_clear_retry_flags(b);
  478. return 0;
  479. }
  480. static int block_in(BIO *b)
  481. {
  482. BIO_OK_CTX *ctx;
  483. EVP_MD_CTX *md;
  484. unsigned long tl = 0;
  485. unsigned char tmp[EVP_MAX_MD_SIZE];
  486. int md_size;
  487. ctx = BIO_get_data(b);
  488. md = ctx->md;
  489. md_size = EVP_MD_size(EVP_MD_CTX_md(md));
  490. assert(sizeof(tl) >= OK_BLOCK_BLOCK); /* always true */
  491. tl = ctx->buf[0];
  492. tl <<= 8;
  493. tl |= ctx->buf[1];
  494. tl <<= 8;
  495. tl |= ctx->buf[2];
  496. tl <<= 8;
  497. tl |= ctx->buf[3];
  498. if (ctx->buf_len < tl + OK_BLOCK_BLOCK + md_size)
  499. return 1;
  500. if (!EVP_DigestUpdate(md,
  501. (unsigned char *)&(ctx->buf[OK_BLOCK_BLOCK]), tl))
  502. goto berr;
  503. if (!EVP_DigestFinal_ex(md, tmp, NULL))
  504. goto berr;
  505. if (memcmp(&(ctx->buf[tl + OK_BLOCK_BLOCK]), tmp, md_size) == 0) {
  506. /* there might be parts from next block lurking around ! */
  507. ctx->buf_off_save = tl + OK_BLOCK_BLOCK + md_size;
  508. ctx->buf_len_save = ctx->buf_len;
  509. ctx->buf_off = OK_BLOCK_BLOCK;
  510. ctx->buf_len = tl + OK_BLOCK_BLOCK;
  511. ctx->blockout = 1;
  512. } else {
  513. ctx->cont = 0;
  514. }
  515. return 1;
  516. berr:
  517. BIO_clear_retry_flags(b);
  518. return 0;
  519. }