2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
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
15 I wrote function which took BIO* as argument, read data from it
16 and processed it. Then I wanted to store the input file in
17 encrypted form. OK I pushed BIO_f_cipher to the BIO stack
18 and everything was OK. BUT if user types wrong password
19 BIO_f_cipher outputs only garbage and my function crashes. Yes
20 I can and I should fix my function, but BIO_f_cipher is
21 easy way to add encryption support to many existing applications
22 and it's hard to debug and fix them all.
24 So I wanted another BIO which would catch the incorrect passwords and
25 file damages which cause garbage on BIO_f_cipher's output.
27 The easy way is to push the BIO_f_md and save the checksum at
28 the end of the file. However there are several problems with this
31 1) you must somehow separate checksum from actual data.
32 2) you need lot's of memory when reading the file, because you
33 must read to the end of the file and verify the checksum before
34 letting the application to read the data.
36 BIO_f_reliable tries to solve both problems, so that you can
37 read and write arbitrary long streams using only fixed amount
40 BIO_f_reliable splits data stream into blocks. Each block is prefixed
41 with it's length and suffixed with it's digest. So you need only
42 several Kbytes of memory to buffer single block before verifying
45 BIO_f_reliable goes further and adds several important capabilities:
47 1) the digest of the block is computed over the whole stream
48 -- so nobody can rearrange the blocks or remove or replace them.
50 2) to detect invalid passwords right at the start BIO_f_reliable
51 adds special prefix to the stream. In order to avoid known plain-text
52 attacks this prefix is generated as follows:
54 *) digest is initialized with random seed instead of
56 *) same seed is written to output
57 *) well-known text is then hashed and the output
58 of the digest is also written to output.
60 reader can now read the seed from stream, hash the same string
61 and then compare the digest output.
63 Bad things: BIO_f_reliable knows what's going on in EVP_Digest. I
64 initially wrote and tested this code on x86 machine and wrote the
65 digests out in machine-dependent order :( There are people using
66 this code and I cannot change this easily without making existing
67 data files unreadable.
74 #include "internal/cryptlib.h"
75 #include <openssl/buffer.h>
76 #include "internal/bio.h"
77 #include <openssl/evp.h>
78 #include <openssl/rand.h>
79 #include "internal/evp_int.h"
81 static int ok_write(BIO *h, const char *buf, int num);
82 static int ok_read(BIO *h, char *buf, int size);
83 static long ok_ctrl(BIO *h, int cmd, long arg1, void *arg2);
84 static int ok_new(BIO *h);
85 static int ok_free(BIO *data);
86 static long ok_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
88 static __owur int sig_out(BIO *b);
89 static __owur int sig_in(BIO *b);
90 static __owur int block_out(BIO *b);
91 static __owur int block_in(BIO *b);
92 #define OK_BLOCK_SIZE (1024*4)
93 #define OK_BLOCK_BLOCK 4
94 #define IOBS (OK_BLOCK_SIZE+ OK_BLOCK_BLOCK+ 3*EVP_MAX_MD_SIZE)
95 #define WELLKNOWN "The quick brown fox jumped over the lazy dog's back."
97 typedef struct ok_struct {
102 int cont; /* <= 0 when finished */
105 int blockout; /* output block is ready */
106 int sigio; /* must process signature */
107 unsigned char buf[IOBS];
110 static const BIO_METHOD methods_ok = {
113 /* TODO: Convert to new style write function */
116 /* TODO: Convert to new style read function */
127 const BIO_METHOD *BIO_f_reliable(void)
132 static int ok_new(BIO *bi)
136 ctx = OPENSSL_zalloc(sizeof(*ctx));
142 ctx->md = EVP_MD_CTX_new();
143 if (ctx->md == NULL) {
148 BIO_set_data(bi, ctx);
153 static int ok_free(BIO *a)
160 ctx = BIO_get_data(a);
162 EVP_MD_CTX_free(ctx->md);
163 OPENSSL_clear_free(ctx, sizeof(BIO_OK_CTX));
164 BIO_set_data(a, NULL);
170 static int ok_read(BIO *b, char *out, int outl)
179 ctx = BIO_get_data(b);
182 if ((ctx == NULL) || (next == NULL) || (BIO_get_init(b) == 0))
187 /* copy clean bytes to output buffer */
189 i = ctx->buf_len - ctx->buf_off;
192 memcpy(out, &(ctx->buf[ctx->buf_off]), i);
198 /* all clean bytes are out */
199 if (ctx->buf_len == ctx->buf_off) {
203 * copy start of the next block into proper place
205 if (ctx->buf_len_save - ctx->buf_off_save > 0) {
206 ctx->buf_len = ctx->buf_len_save - ctx->buf_off_save;
207 memmove(ctx->buf, &(ctx->buf[ctx->buf_off_save]),
216 /* output buffer full -- cancel */
220 /* no clean bytes in buffer -- fill it */
221 n = IOBS - ctx->buf_len;
222 i = BIO_read(next, &(ctx->buf[ctx->buf_len]), n);
225 break; /* nothing new */
229 /* no signature yet -- check if we got one */
230 if (ctx->sigio == 1) {
232 BIO_clear_retry_flags(b);
237 /* signature ok -- check if we got block */
238 if (ctx->sigio == 0) {
240 BIO_clear_retry_flags(b);
245 /* invalid block -- cancel */
251 BIO_clear_retry_flags(b);
252 BIO_copy_next_retry(b);
256 static int ok_write(BIO *b, const char *in, int inl)
265 ctx = BIO_get_data(b);
269 if ((ctx == NULL) || (next == NULL) || (BIO_get_init(b) == 0))
272 if (ctx->sigio && !sig_out(b))
276 BIO_clear_retry_flags(b);
277 n = ctx->buf_len - ctx->buf_off;
278 while (ctx->blockout && n > 0) {
279 i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
281 BIO_copy_next_retry(b);
282 if (!BIO_should_retry(b))
290 /* at this point all pending data has been written */
292 if (ctx->buf_len == ctx->buf_off) {
293 ctx->buf_len = OK_BLOCK_BLOCK;
297 if ((in == NULL) || (inl <= 0))
300 n = (inl + ctx->buf_len > OK_BLOCK_SIZE + OK_BLOCK_BLOCK) ?
301 (int)(OK_BLOCK_SIZE + OK_BLOCK_BLOCK - ctx->buf_len) : inl;
303 memcpy(&ctx->buf[ctx->buf_len], in, n);
308 if (ctx->buf_len >= OK_BLOCK_SIZE + OK_BLOCK_BLOCK) {
310 BIO_clear_retry_flags(b);
316 BIO_clear_retry_flags(b);
317 BIO_copy_next_retry(b);
321 static long ok_ctrl(BIO *b, int cmd, long num, void *ptr)
330 ctx = BIO_get_data(b);
337 ctx->buf_len_save = 0;
338 ctx->buf_off_save = 0;
343 ret = BIO_ctrl(next, cmd, num, ptr);
345 case BIO_CTRL_EOF: /* More to read */
349 ret = BIO_ctrl(next, cmd, num, ptr);
351 case BIO_CTRL_PENDING: /* More to read in buffer */
352 case BIO_CTRL_WPENDING: /* More to read in buffer */
353 ret = ctx->blockout ? ctx->buf_len - ctx->buf_off : 0;
355 ret = BIO_ctrl(next, cmd, num, ptr);
358 /* do a final write */
359 if (ctx->blockout == 0)
363 while (ctx->blockout) {
364 i = ok_write(b, NULL, 0);
372 ctx->buf_off = ctx->buf_len = 0;
373 ctx->cont = (int)ret;
375 /* Finally flush the underlying BIO */
376 ret = BIO_ctrl(next, cmd, num, ptr);
378 case BIO_C_DO_STATE_MACHINE:
379 BIO_clear_retry_flags(b);
380 ret = BIO_ctrl(next, cmd, num, ptr);
381 BIO_copy_next_retry(b);
384 ret = (long)ctx->cont;
388 if (!EVP_DigestInit_ex(ctx->md, md, NULL))
393 if (BIO_get_init(b)) {
395 *ppmd = EVP_MD_CTX_md(ctx->md);
400 ret = BIO_ctrl(next, cmd, num, ptr);
406 static long ok_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
418 ret = BIO_callback_ctrl(next, cmd, fp);
425 static void longswap(void *_ptr, size_t len)
434 if (is_endian.little) {
436 unsigned char *p = _ptr, c;
438 for (i = 0; i < len; i += 4) {
439 c = p[0], p[0] = p[3], p[3] = c;
440 c = p[1], p[1] = p[2], p[2] = c;
445 static int sig_out(BIO *b)
449 const EVP_MD *digest;
453 ctx = BIO_get_data(b);
455 digest = EVP_MD_CTX_md(md);
456 md_size = EVP_MD_size(digest);
457 md_data = EVP_MD_CTX_md_data(md);
459 if (ctx->buf_len + 2 * md_size > OK_BLOCK_SIZE)
462 if (!EVP_DigestInit_ex(md, digest, NULL))
465 * FIXME: there's absolutely no guarantee this makes any sense at all,
466 * particularly now EVP_MD_CTX has been restructured.
468 if (RAND_bytes(md_data, md_size) <= 0)
470 memcpy(&(ctx->buf[ctx->buf_len]), md_data, md_size);
471 longswap(&(ctx->buf[ctx->buf_len]), md_size);
472 ctx->buf_len += md_size;
474 if (!EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN)))
476 if (!EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL))
478 ctx->buf_len += md_size;
483 BIO_clear_retry_flags(b);
487 static int sig_in(BIO *b)
491 unsigned char tmp[EVP_MAX_MD_SIZE];
493 const EVP_MD *digest;
497 ctx = BIO_get_data(b);
499 digest = EVP_MD_CTX_md(md);
500 md_size = EVP_MD_size(digest);
501 md_data = EVP_MD_CTX_md_data(md);
503 if ((int)(ctx->buf_len - ctx->buf_off) < 2 * md_size)
506 if (!EVP_DigestInit_ex(md, digest, NULL))
508 memcpy(md_data, &(ctx->buf[ctx->buf_off]), md_size);
509 longswap(md_data, md_size);
510 ctx->buf_off += md_size;
512 if (!EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN)))
514 if (!EVP_DigestFinal_ex(md, tmp, NULL))
516 ret = memcmp(&(ctx->buf[ctx->buf_off]), tmp, md_size) == 0;
517 ctx->buf_off += md_size;
520 if (ctx->buf_len != ctx->buf_off) {
521 memmove(ctx->buf, &(ctx->buf[ctx->buf_off]),
522 ctx->buf_len - ctx->buf_off);
524 ctx->buf_len -= ctx->buf_off;
531 BIO_clear_retry_flags(b);
535 static int block_out(BIO *b)
540 const EVP_MD *digest;
543 ctx = BIO_get_data(b);
545 digest = EVP_MD_CTX_md(md);
546 md_size = EVP_MD_size(digest);
548 tl = ctx->buf_len - OK_BLOCK_BLOCK;
549 ctx->buf[0] = (unsigned char)(tl >> 24);
550 ctx->buf[1] = (unsigned char)(tl >> 16);
551 ctx->buf[2] = (unsigned char)(tl >> 8);
552 ctx->buf[3] = (unsigned char)(tl);
553 if (!EVP_DigestUpdate(md,
554 (unsigned char *)&(ctx->buf[OK_BLOCK_BLOCK]), tl))
556 if (!EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL))
558 ctx->buf_len += md_size;
562 BIO_clear_retry_flags(b);
566 static int block_in(BIO *b)
570 unsigned long tl = 0;
571 unsigned char tmp[EVP_MAX_MD_SIZE];
574 ctx = BIO_get_data(b);
576 md_size = EVP_MD_size(EVP_MD_CTX_md(md));
578 assert(sizeof(tl) >= OK_BLOCK_BLOCK); /* always true */
587 if (ctx->buf_len < tl + OK_BLOCK_BLOCK + md_size)
590 if (!EVP_DigestUpdate(md,
591 (unsigned char *)&(ctx->buf[OK_BLOCK_BLOCK]), tl))
593 if (!EVP_DigestFinal_ex(md, tmp, NULL))
595 if (memcmp(&(ctx->buf[tl + OK_BLOCK_BLOCK]), tmp, md_size) == 0) {
596 /* there might be parts from next block lurking around ! */
597 ctx->buf_off_save = tl + OK_BLOCK_BLOCK + md_size;
598 ctx->buf_len_save = ctx->buf_len;
599 ctx->buf_off = OK_BLOCK_BLOCK;
600 ctx->buf_len = tl + OK_BLOCK_BLOCK;
607 BIO_clear_retry_flags(b);