1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
59 From: Arne Ansper <arne@cyber.ee>
63 I wrote function which took BIO* as argument, read data from it
64 and processed it. Then I wanted to store the input file in
65 encrypted form. OK I pushed BIO_f_cipher to the BIO stack
66 and everything was OK. BUT if user types wrong password
67 BIO_f_cipher outputs only garbage and my function crashes. Yes
68 I can and I should fix my function, but BIO_f_cipher is
69 easy way to add encryption support to many existing applications
70 and it's hard to debug and fix them all.
72 So I wanted another BIO which would catch the incorrect passwords and
73 file damages which cause garbage on BIO_f_cipher's output.
75 The easy way is to push the BIO_f_md and save the checksum at
76 the end of the file. However there are several problems with this
79 1) you must somehow separate checksum from actual data.
80 2) you need lot's of memory when reading the file, because you
81 must read to the end of the file and verify the checksum before
82 letting the application to read the data.
84 BIO_f_reliable tries to solve both problems, so that you can
85 read and write arbitrary long streams using only fixed amount
88 BIO_f_reliable splits data stream into blocks. Each block is prefixed
89 with it's length and suffixed with it's digest. So you need only
90 several Kbytes of memory to buffer single block before verifying
93 BIO_f_reliable goes further and adds several important capabilities:
95 1) the digest of the block is computed over the whole stream
96 -- so nobody can rearrange the blocks or remove or replace them.
98 2) to detect invalid passwords right at the start BIO_f_reliable
99 adds special prefix to the stream. In order to avoid known plain-text
100 attacks this prefix is generated as follows:
102 *) digest is initialized with random seed instead of
104 *) same seed is written to output
105 *) well-known text is then hashed and the output
106 of the digest is also written to output.
108 reader can now read the seed from stream, hash the same string
109 and then compare the digest output.
111 Bad things: BIO_f_reliable knows what's going on in EVP_Digest. I
112 initially wrote and tested this code on x86 machine and wrote the
113 digests out in machine-dependent order :( There are people using
114 this code and I cannot change this easily without making existing
115 data files unreadable.
122 #include "internal/cryptlib.h"
123 #include <openssl/buffer.h>
124 #include "internal/bio.h"
125 #include <openssl/evp.h>
126 #include <openssl/rand.h>
127 #include "internal/evp_int.h"
129 static int ok_write(BIO *h, const char *buf, int num);
130 static int ok_read(BIO *h, char *buf, int size);
131 static long ok_ctrl(BIO *h, int cmd, long arg1, void *arg2);
132 static int ok_new(BIO *h);
133 static int ok_free(BIO *data);
134 static long ok_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
136 static __owur int sig_out(BIO *b);
137 static __owur int sig_in(BIO *b);
138 static __owur int block_out(BIO *b);
139 static __owur int block_in(BIO *b);
140 #define OK_BLOCK_SIZE (1024*4)
141 #define OK_BLOCK_BLOCK 4
142 #define IOBS (OK_BLOCK_SIZE+ OK_BLOCK_BLOCK+ 3*EVP_MAX_MD_SIZE)
143 #define WELLKNOWN "The quick brown fox jumped over the lazy dog's back."
145 typedef struct ok_struct {
150 int cont; /* <= 0 when finished */
153 int blockout; /* output block is ready */
154 int sigio; /* must process signature */
155 unsigned char buf[IOBS];
158 static const BIO_METHOD methods_ok = {
159 BIO_TYPE_CIPHER, "reliable",
170 const BIO_METHOD *BIO_f_reliable(void)
172 return (&methods_ok);
175 static int ok_new(BIO *bi)
179 ctx = OPENSSL_zalloc(sizeof(*ctx));
185 ctx->md = EVP_MD_CTX_new();
186 if (ctx->md == NULL) {
191 BIO_set_data(bi, ctx);
196 static int ok_free(BIO *a)
203 ctx = BIO_get_data(a);
205 EVP_MD_CTX_free(ctx->md);
206 OPENSSL_clear_free(ctx, sizeof(BIO_OK_CTX));
207 BIO_set_data(a, NULL);
213 static int ok_read(BIO *b, char *out, int outl)
222 ctx = BIO_get_data(b);
225 if ((ctx == NULL) || (next == NULL) || (BIO_get_init(b) == 0))
230 /* copy clean bytes to output buffer */
232 i = ctx->buf_len - ctx->buf_off;
235 memcpy(out, &(ctx->buf[ctx->buf_off]), i);
241 /* all clean bytes are out */
242 if (ctx->buf_len == ctx->buf_off) {
246 * copy start of the next block into proper place
248 if (ctx->buf_len_save - ctx->buf_off_save > 0) {
249 ctx->buf_len = ctx->buf_len_save - ctx->buf_off_save;
250 memmove(ctx->buf, &(ctx->buf[ctx->buf_off_save]),
259 /* output buffer full -- cancel */
263 /* no clean bytes in buffer -- fill it */
264 n = IOBS - ctx->buf_len;
265 i = BIO_read(next, &(ctx->buf[ctx->buf_len]), n);
268 break; /* nothing new */
272 /* no signature yet -- check if we got one */
273 if (ctx->sigio == 1) {
275 BIO_clear_retry_flags(b);
280 /* signature ok -- check if we got block */
281 if (ctx->sigio == 0) {
283 BIO_clear_retry_flags(b);
288 /* invalid block -- cancel */
294 BIO_clear_retry_flags(b);
295 BIO_copy_next_retry(b);
299 static int ok_write(BIO *b, const char *in, int inl)
308 ctx = BIO_get_data(b);
312 if ((ctx == NULL) || (next == NULL) || (BIO_get_init(b) == 0))
315 if (ctx->sigio && !sig_out(b))
319 BIO_clear_retry_flags(b);
320 n = ctx->buf_len - ctx->buf_off;
321 while (ctx->blockout && n > 0) {
322 i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
324 BIO_copy_next_retry(b);
325 if (!BIO_should_retry(b))
333 /* at this point all pending data has been written */
335 if (ctx->buf_len == ctx->buf_off) {
336 ctx->buf_len = OK_BLOCK_BLOCK;
340 if ((in == NULL) || (inl <= 0))
343 n = (inl + ctx->buf_len > OK_BLOCK_SIZE + OK_BLOCK_BLOCK) ?
344 (int)(OK_BLOCK_SIZE + OK_BLOCK_BLOCK - ctx->buf_len) : inl;
346 memcpy(&ctx->buf[ctx->buf_len], in, n);
351 if (ctx->buf_len >= OK_BLOCK_SIZE + OK_BLOCK_BLOCK) {
353 BIO_clear_retry_flags(b);
359 BIO_clear_retry_flags(b);
360 BIO_copy_next_retry(b);
364 static long ok_ctrl(BIO *b, int cmd, long num, void *ptr)
373 ctx = BIO_get_data(b);
380 ctx->buf_len_save = 0;
381 ctx->buf_off_save = 0;
386 ret = BIO_ctrl(next, cmd, num, ptr);
388 case BIO_CTRL_EOF: /* More to read */
392 ret = BIO_ctrl(next, cmd, num, ptr);
394 case BIO_CTRL_PENDING: /* More to read in buffer */
395 case BIO_CTRL_WPENDING: /* More to read in buffer */
396 ret = ctx->blockout ? ctx->buf_len - ctx->buf_off : 0;
398 ret = BIO_ctrl(next, cmd, num, ptr);
401 /* do a final write */
402 if (ctx->blockout == 0)
406 while (ctx->blockout) {
407 i = ok_write(b, NULL, 0);
415 ctx->buf_off = ctx->buf_len = 0;
416 ctx->cont = (int)ret;
418 /* Finally flush the underlying BIO */
419 ret = BIO_ctrl(next, cmd, num, ptr);
421 case BIO_C_DO_STATE_MACHINE:
422 BIO_clear_retry_flags(b);
423 ret = BIO_ctrl(next, cmd, num, ptr);
424 BIO_copy_next_retry(b);
427 ret = (long)ctx->cont;
431 if (!EVP_DigestInit_ex(ctx->md, md, NULL))
436 if (BIO_get_init(b)) {
438 *ppmd = EVP_MD_CTX_md(ctx->md);
443 ret = BIO_ctrl(next, cmd, num, ptr);
449 static long ok_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
461 ret = BIO_callback_ctrl(next, cmd, fp);
468 static void longswap(void *_ptr, size_t len)
477 if (is_endian.little) {
479 unsigned char *p = _ptr, c;
481 for (i = 0; i < len; i += 4) {
482 c = p[0], p[0] = p[3], p[3] = c;
483 c = p[1], p[1] = p[2], p[2] = c;
488 static int sig_out(BIO *b)
492 const EVP_MD *digest;
496 ctx = BIO_get_data(b);
498 digest = EVP_MD_CTX_md(md);
499 md_size = EVP_MD_size(digest);
500 md_data = EVP_MD_CTX_md_data(md);
502 if (ctx->buf_len + 2 * md_size > OK_BLOCK_SIZE)
505 if (!EVP_DigestInit_ex(md, digest, NULL))
508 * FIXME: there's absolutely no guarantee this makes any sense at all,
509 * particularly now EVP_MD_CTX has been restructured.
511 if (RAND_bytes(md_data, md_size) <= 0)
513 memcpy(&(ctx->buf[ctx->buf_len]), md_data, md_size);
514 longswap(&(ctx->buf[ctx->buf_len]), md_size);
515 ctx->buf_len += md_size;
517 if (!EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN)))
519 if (!EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL))
521 ctx->buf_len += md_size;
526 BIO_clear_retry_flags(b);
530 static int sig_in(BIO *b)
534 unsigned char tmp[EVP_MAX_MD_SIZE];
536 const EVP_MD *digest;
540 ctx = BIO_get_data(b);
542 digest = EVP_MD_CTX_md(md);
543 md_size = EVP_MD_size(digest);
544 md_data = EVP_MD_CTX_md_data(md);
546 if ((int)(ctx->buf_len - ctx->buf_off) < 2 * md_size)
549 if (!EVP_DigestInit_ex(md, digest, NULL))
551 memcpy(md_data, &(ctx->buf[ctx->buf_off]), md_size);
552 longswap(md_data, md_size);
553 ctx->buf_off += md_size;
555 if (!EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN)))
557 if (!EVP_DigestFinal_ex(md, tmp, NULL))
559 ret = memcmp(&(ctx->buf[ctx->buf_off]), tmp, md_size) == 0;
560 ctx->buf_off += md_size;
563 if (ctx->buf_len != ctx->buf_off) {
564 memmove(ctx->buf, &(ctx->buf[ctx->buf_off]),
565 ctx->buf_len - ctx->buf_off);
567 ctx->buf_len -= ctx->buf_off;
574 BIO_clear_retry_flags(b);
578 static int block_out(BIO *b)
583 const EVP_MD *digest;
586 ctx = BIO_get_data(b);
588 digest = EVP_MD_CTX_md(md);
589 md_size = EVP_MD_size(digest);
591 tl = ctx->buf_len - OK_BLOCK_BLOCK;
592 ctx->buf[0] = (unsigned char)(tl >> 24);
593 ctx->buf[1] = (unsigned char)(tl >> 16);
594 ctx->buf[2] = (unsigned char)(tl >> 8);
595 ctx->buf[3] = (unsigned char)(tl);
596 if (!EVP_DigestUpdate(md,
597 (unsigned char *)&(ctx->buf[OK_BLOCK_BLOCK]), tl))
599 if (!EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL))
601 ctx->buf_len += md_size;
605 BIO_clear_retry_flags(b);
609 static int block_in(BIO *b)
613 unsigned long tl = 0;
614 unsigned char tmp[EVP_MAX_MD_SIZE];
617 ctx = BIO_get_data(b);
619 md_size = EVP_MD_size(EVP_MD_CTX_md(md));
621 assert(sizeof(tl) >= OK_BLOCK_BLOCK); /* always true */
630 if (ctx->buf_len < tl + OK_BLOCK_BLOCK + md_size)
633 if (!EVP_DigestUpdate(md,
634 (unsigned char *)&(ctx->buf[OK_BLOCK_BLOCK]), tl))
636 if (!EVP_DigestFinal_ex(md, tmp, NULL))
638 if (memcmp(&(ctx->buf[tl + OK_BLOCK_BLOCK]), tmp, md_size) == 0) {
639 /* there might be parts from next block lurking around ! */
640 ctx->buf_off_save = tl + OK_BLOCK_BLOCK + md_size;
641 ctx->buf_len_save = ctx->buf_len;
642 ctx->buf_off = OK_BLOCK_BLOCK;
643 ctx->buf_len = tl + OK_BLOCK_BLOCK;
650 BIO_clear_retry_flags(b);