2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
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
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 its length and suffixed with its 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/endian.h"
80 #include "crypto/evp.h"
82 static int ok_write(BIO *h, const char *buf, int num);
83 static int ok_read(BIO *h, char *buf, int size);
84 static long ok_ctrl(BIO *h, int cmd, long arg1, void *arg2);
85 static int ok_new(BIO *h);
86 static int ok_free(BIO *data);
87 static long ok_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
89 static __owur int sig_out(BIO *b);
90 static __owur int sig_in(BIO *b);
91 static __owur int block_out(BIO *b);
92 static __owur int block_in(BIO *b);
93 #define OK_BLOCK_SIZE (1024*4)
94 #define OK_BLOCK_BLOCK 4
95 #define IOBS (OK_BLOCK_SIZE+ OK_BLOCK_BLOCK+ 3*EVP_MAX_MD_SIZE)
96 #define WELLKNOWN "The quick brown fox jumped over the lazy dog's back."
98 typedef struct ok_struct {
103 int cont; /* <= 0 when finished */
106 int blockout; /* output block is ready */
107 int sigio; /* must process signature */
108 unsigned char buf[IOBS];
111 static const BIO_METHOD methods_ok = {
114 /* TODO: Convert to new style write function */
117 /* TODO: Convert to new style read function */
128 const BIO_METHOD *BIO_f_reliable(void)
133 static int ok_new(BIO *bi)
137 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
138 EVPerr(EVP_F_OK_NEW, ERR_R_MALLOC_FAILURE);
144 ctx->md = EVP_MD_CTX_new();
145 if (ctx->md == NULL) {
150 BIO_set_data(bi, ctx);
155 static int ok_free(BIO *a)
162 ctx = BIO_get_data(a);
164 EVP_MD_CTX_free(ctx->md);
165 OPENSSL_clear_free(ctx, sizeof(BIO_OK_CTX));
166 BIO_set_data(a, NULL);
172 static int ok_read(BIO *b, char *out, int outl)
181 ctx = BIO_get_data(b);
184 if ((ctx == NULL) || (next == NULL) || (BIO_get_init(b) == 0))
189 /* copy clean bytes to output buffer */
191 i = ctx->buf_len - ctx->buf_off;
194 memcpy(out, &(ctx->buf[ctx->buf_off]), i);
200 /* all clean bytes are out */
201 if (ctx->buf_len == ctx->buf_off) {
205 * copy start of the next block into proper place
207 if (ctx->buf_len_save - ctx->buf_off_save > 0) {
208 ctx->buf_len = ctx->buf_len_save - ctx->buf_off_save;
209 memmove(ctx->buf, &(ctx->buf[ctx->buf_off_save]),
218 /* output buffer full -- cancel */
222 /* no clean bytes in buffer -- fill it */
223 n = IOBS - ctx->buf_len;
224 i = BIO_read(next, &(ctx->buf[ctx->buf_len]), n);
227 break; /* nothing new */
231 /* no signature yet -- check if we got one */
232 if (ctx->sigio == 1) {
234 BIO_clear_retry_flags(b);
239 /* signature ok -- check if we got block */
240 if (ctx->sigio == 0) {
242 BIO_clear_retry_flags(b);
247 /* invalid block -- cancel */
253 BIO_clear_retry_flags(b);
254 BIO_copy_next_retry(b);
258 static int ok_write(BIO *b, const char *in, int inl)
267 ctx = BIO_get_data(b);
271 if ((ctx == NULL) || (next == NULL) || (BIO_get_init(b) == 0))
274 if (ctx->sigio && !sig_out(b))
278 BIO_clear_retry_flags(b);
279 n = ctx->buf_len - ctx->buf_off;
280 while (ctx->blockout && n > 0) {
281 i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
283 BIO_copy_next_retry(b);
284 if (!BIO_should_retry(b))
292 /* at this point all pending data has been written */
294 if (ctx->buf_len == ctx->buf_off) {
295 ctx->buf_len = OK_BLOCK_BLOCK;
299 if ((in == NULL) || (inl <= 0))
302 n = (inl + ctx->buf_len > OK_BLOCK_SIZE + OK_BLOCK_BLOCK) ?
303 (int)(OK_BLOCK_SIZE + OK_BLOCK_BLOCK - ctx->buf_len) : inl;
305 memcpy(&ctx->buf[ctx->buf_len], in, n);
310 if (ctx->buf_len >= OK_BLOCK_SIZE + OK_BLOCK_BLOCK) {
312 BIO_clear_retry_flags(b);
318 BIO_clear_retry_flags(b);
319 BIO_copy_next_retry(b);
323 static long ok_ctrl(BIO *b, int cmd, long num, void *ptr)
332 ctx = BIO_get_data(b);
339 ctx->buf_len_save = 0;
340 ctx->buf_off_save = 0;
345 ret = BIO_ctrl(next, cmd, num, ptr);
347 case BIO_CTRL_EOF: /* More to read */
351 ret = BIO_ctrl(next, cmd, num, ptr);
353 case BIO_CTRL_PENDING: /* More to read in buffer */
354 case BIO_CTRL_WPENDING: /* More to read in buffer */
355 ret = ctx->blockout ? ctx->buf_len - ctx->buf_off : 0;
357 ret = BIO_ctrl(next, cmd, num, ptr);
360 /* do a final write */
361 if (ctx->blockout == 0)
365 while (ctx->blockout) {
366 i = ok_write(b, NULL, 0);
374 ctx->buf_off = ctx->buf_len = 0;
375 ctx->cont = (int)ret;
377 /* Finally flush the underlying BIO */
378 ret = BIO_ctrl(next, cmd, num, ptr);
380 case BIO_C_DO_STATE_MACHINE:
381 BIO_clear_retry_flags(b);
382 ret = BIO_ctrl(next, cmd, num, ptr);
383 BIO_copy_next_retry(b);
386 ret = (long)ctx->cont;
390 if (!EVP_DigestInit_ex(ctx->md, md, NULL))
395 if (BIO_get_init(b)) {
397 *ppmd = EVP_MD_CTX_md(ctx->md);
402 ret = BIO_ctrl(next, cmd, num, ptr);
408 static long ok_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
417 return BIO_callback_ctrl(next, cmd, fp);
420 static void longswap(void *_ptr, size_t len)
424 if (IS_LITTLE_ENDIAN) {
426 unsigned char *p = _ptr, c;
428 for (i = 0; i < len; i += 4) {
429 c = p[0], p[0] = p[3], p[3] = c;
430 c = p[1], p[1] = p[2], p[2] = c;
435 static int sig_out(BIO *b)
439 const EVP_MD *digest;
443 ctx = BIO_get_data(b);
445 digest = EVP_MD_CTX_md(md);
446 md_size = EVP_MD_size(digest);
447 md_data = EVP_MD_CTX_md_data(md);
449 if (ctx->buf_len + 2 * md_size > OK_BLOCK_SIZE)
452 if (!EVP_DigestInit_ex(md, digest, NULL))
455 * FIXME: there's absolutely no guarantee this makes any sense at all,
456 * particularly now EVP_MD_CTX has been restructured.
458 if (RAND_bytes(md_data, md_size) <= 0)
460 memcpy(&(ctx->buf[ctx->buf_len]), md_data, md_size);
461 longswap(&(ctx->buf[ctx->buf_len]), md_size);
462 ctx->buf_len += md_size;
464 if (!EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN)))
466 if (!EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL))
468 ctx->buf_len += md_size;
473 BIO_clear_retry_flags(b);
477 static int sig_in(BIO *b)
481 unsigned char tmp[EVP_MAX_MD_SIZE];
483 const EVP_MD *digest;
487 ctx = BIO_get_data(b);
489 digest = EVP_MD_CTX_md(md);
490 md_size = EVP_MD_size(digest);
491 md_data = EVP_MD_CTX_md_data(md);
493 if ((int)(ctx->buf_len - ctx->buf_off) < 2 * md_size)
496 if (!EVP_DigestInit_ex(md, digest, NULL))
498 memcpy(md_data, &(ctx->buf[ctx->buf_off]), md_size);
499 longswap(md_data, md_size);
500 ctx->buf_off += md_size;
502 if (!EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN)))
504 if (!EVP_DigestFinal_ex(md, tmp, NULL))
506 ret = memcmp(&(ctx->buf[ctx->buf_off]), tmp, md_size) == 0;
507 ctx->buf_off += md_size;
510 if (ctx->buf_len != ctx->buf_off) {
511 memmove(ctx->buf, &(ctx->buf[ctx->buf_off]),
512 ctx->buf_len - ctx->buf_off);
514 ctx->buf_len -= ctx->buf_off;
521 BIO_clear_retry_flags(b);
525 static int block_out(BIO *b)
530 const EVP_MD *digest;
533 ctx = BIO_get_data(b);
535 digest = EVP_MD_CTX_md(md);
536 md_size = EVP_MD_size(digest);
538 tl = ctx->buf_len - OK_BLOCK_BLOCK;
539 ctx->buf[0] = (unsigned char)(tl >> 24);
540 ctx->buf[1] = (unsigned char)(tl >> 16);
541 ctx->buf[2] = (unsigned char)(tl >> 8);
542 ctx->buf[3] = (unsigned char)(tl);
543 if (!EVP_DigestUpdate(md,
544 (unsigned char *)&(ctx->buf[OK_BLOCK_BLOCK]), tl))
546 if (!EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL))
548 ctx->buf_len += md_size;
552 BIO_clear_retry_flags(b);
556 static int block_in(BIO *b)
560 unsigned long tl = 0;
561 unsigned char tmp[EVP_MAX_MD_SIZE];
564 ctx = BIO_get_data(b);
566 md_size = EVP_MD_size(EVP_MD_CTX_md(md));
568 assert(sizeof(tl) >= OK_BLOCK_BLOCK); /* always true */
577 if (ctx->buf_len < tl + OK_BLOCK_BLOCK + md_size)
580 if (!EVP_DigestUpdate(md,
581 (unsigned char *)&(ctx->buf[OK_BLOCK_BLOCK]), tl))
583 if (!EVP_DigestFinal_ex(md, tmp, NULL))
585 if (memcmp(&(ctx->buf[tl + OK_BLOCK_BLOCK]), tmp, md_size) == 0) {
586 /* there might be parts from next block lurking around ! */
587 ctx->buf_off_save = tl + OK_BLOCK_BLOCK + md_size;
588 ctx->buf_len_save = ctx->buf_len;
589 ctx->buf_off = OK_BLOCK_BLOCK;
590 ctx->buf_len = tl + OK_BLOCK_BLOCK;
597 BIO_clear_retry_flags(b);