]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/bio_ok.c
Change functions to ANSI C.
[thirdparty/openssl.git] / crypto / evp / bio_ok.c
CommitLineData
1745a3fb
BL
1/* crypto/evp/bio_ok.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/*
60 From: Arne Ansper <arne@cyber.ee>
61
62 Why BIO_f_reliable?
63
64 I wrote function which took BIO* as argument, read data from it
65 and processed it. Then I wanted to store the input file in
66 encrypted form. OK I pushed BIO_f_cipher to the BIO stack
67 and everything was OK. BUT if user types wrong password
68 BIO_f_cipher outputs only garbage and my function crashes. Yes
69 I can and I should fix my function, but BIO_f_cipher is
70 easy way to add encryption support to many exisiting applications
71 and it's hard to debug and fix them all.
72
73 So I wanted another BIO which would catch the incorrect passwords and
74 file damages which cause garbage on BIO_f_cipher's output.
75
76 The easy way is to push the BIO_f_md and save the checksum at
77 the end of the file. However there are several problems with this
78 approach:
79
80 1) you must somehow separate checksum from actual data.
81 2) you need lot's of memory when reading the file, because you
82 must read to the end of the file and verify the checksum before
83 leting the application to read the data.
84
85 BIO_f_reliable tries to solve both problems, so that you can
86 read and write arbitraly long streams using only fixed amount
87 of memory.
88
89 BIO_f_reliable splits data stream into blocks. Each block is prefixed
90 with it's length and suffixed with it's digest. So you need only
91 several Kbytes of memory to buffer single block before verifying
92 it's digest.
93
94 BIO_f_reliable goes futher and adds several important capabilities:
95
96 1) the digest of the block is computed over the whole stream
97 -- so nobody can rearrange the blocks or remove or replace them.
98
99 2) to detect invalid passwords right at the start BIO_f_reliable
100 adds special prefix to the stream. In order to avoid known plain-text
101 attacks this prefix is generated as follows:
102
103 *) digest is initialized with random seed instead of
104 standardized one.
105 *) same seed is written to ouput
106 *) well-known text is then hashed and the output
107 of the digest is also written to output.
108
109 reader can now read the seed from stream, hash the same string
110 and then compare the digest output.
111
112 Bad things: BIO_f_reliable knows what's going on in EVP_Digest. I
113 initialy wrote and tested this code on x86 machine and wrote the
114 digests out in machine-dependent order :( There are people using
115 this code and I cannot change this easily without making existing
116 data files unreadable.
117
118*/
119
120#include <stdio.h>
121#include <errno.h>
122#include "cryptlib.h"
123#include "buffer.h"
124#include "bio.h"
125#include "evp.h"
126#include "rand.h"
127
128#ifndef NOPROTO
129static int ok_write(BIO *h,char *buf,int num);
130static int ok_read(BIO *h,char *buf,int size);
131static long ok_ctrl(BIO *h,int cmd,long arg1,char *arg2);
132static int ok_new(BIO *h);
133static int ok_free(BIO *data);
134static void sig_out(BIO* b);
135static void sig_in(BIO* b);
136static void block_out(BIO* b);
137static void block_in(BIO* b);
138#else
139static int ok_write();
140static int ok_read();
141static long ok_ctrl();
142static int ok_new();
143static int ok_free();
144static void sig_out();
145static void sig_in();
146static void block_out();
147static void block_in();
148#endif
149
150#define OK_BLOCK_SIZE (1024*4)
151#define OK_BLOCK_BLOCK 4
152#define IOBS (OK_BLOCK_SIZE+ OK_BLOCK_BLOCK+ 3*EVP_MAX_MD_SIZE)
153#define WELLKNOWN "The quick brown fox jumped over the lazy dog's back."
154
155#ifndef L_ENDIAN
156#define swapem(x) \
157 ((unsigned long int)((((unsigned long int)(x) & 0x000000ffU) << 24) | \
158 (((unsigned long int)(x) & 0x0000ff00U) << 8) | \
159 (((unsigned long int)(x) & 0x00ff0000U) >> 8) | \
160 (((unsigned long int)(x) & 0xff000000U) >> 24)))
161#else
162#define swapem(x) (x)
163#endif
164
165typedef struct ok_struct
166 {
167 int buf_len;
168 int buf_off;
169 int buf_len_save;
170 int buf_off_save;
171 int cont; /* <= 0 when finished */
172 int finished;
173 EVP_MD_CTX md;
174 int blockout; /* output block is ready */
175 int sigio; /* must process signature */
176 char buf[IOBS];
177 } BIO_OK_CTX;
178
179static BIO_METHOD methods_ok=
180 {
181 BIO_TYPE_CIPHER,"reliable",
182 ok_write,
183 ok_read,
184 NULL, /* ok_puts, */
185 NULL, /* ok_gets, */
186 ok_ctrl,
187 ok_new,
188 ok_free,
189 };
190
6b691a5c 191BIO_METHOD *BIO_f_reliable(void)
1745a3fb
BL
192 {
193 return(&methods_ok);
194 }
195
6b691a5c 196static int ok_new(BIO *bi)
1745a3fb
BL
197 {
198 BIO_OK_CTX *ctx;
199
200 ctx=(BIO_OK_CTX *)Malloc(sizeof(BIO_OK_CTX));
201 if (ctx == NULL) return(0);
202
203 ctx->buf_len=0;
204 ctx->buf_off=0;
205 ctx->buf_len_save=0;
206 ctx->buf_off_save=0;
207 ctx->cont=1;
208 ctx->finished=0;
209 ctx->blockout= 0;
210 ctx->sigio=1;
211
212 bi->init=0;
213 bi->ptr=(char *)ctx;
214 bi->flags=0;
215 return(1);
216 }
217
6b691a5c 218static int ok_free(BIO *a)
1745a3fb
BL
219 {
220 if (a == NULL) return(0);
221 memset(a->ptr,0,sizeof(BIO_OK_CTX));
222 Free(a->ptr);
223 a->ptr=NULL;
224 a->init=0;
225 a->flags=0;
226 return(1);
227 }
228
6b691a5c 229static int ok_read(BIO *b, char *out, int outl)
1745a3fb
BL
230 {
231 int ret=0,i,n;
232 BIO_OK_CTX *ctx;
233
234 if (out == NULL) return(0);
235 ctx=(BIO_OK_CTX *)b->ptr;
236
237 if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0)) return(0);
238
239 while(outl > 0)
240 {
241
242 /* copy clean bytes to output buffer */
243 if (ctx->blockout)
244 {
245 i=ctx->buf_len-ctx->buf_off;
246 if (i > outl) i=outl;
247 memcpy(out,&(ctx->buf[ctx->buf_off]),i);
248 ret+=i;
249 out+=i;
250 outl-=i;
251 ctx->buf_off+=i;
252
253 /* all clean bytes are out */
254 if (ctx->buf_len == ctx->buf_off)
255 {
256 ctx->buf_off=0;
257
258 /* copy start of the next block into proper place */
259 if(ctx->buf_len_save- ctx->buf_off_save > 0)
260 {
261 ctx->buf_len= ctx->buf_len_save- ctx->buf_off_save;
262 memmove(ctx->buf, &(ctx->buf[ctx->buf_off_save]),
263 ctx->buf_len);
264 }
265 else
266 {
267 ctx->buf_len=0;
268 }
269 ctx->blockout= 0;
270 }
271 }
272
273 /* output buffer full -- cancel */
274 if (outl == 0) break;
275
276 /* no clean bytes in buffer -- fill it */
277 n=IOBS- ctx->buf_len;
278 i=BIO_read(b->next_bio,&(ctx->buf[ctx->buf_len]),n);
279
280 if (i <= 0) break; /* nothing new */
281
282 ctx->buf_len+= i;
283
284 /* no signature yet -- check if we got one */
285 if (ctx->sigio == 1) sig_in(b);
286
287 /* signature ok -- check if we got block */
288 if (ctx->sigio == 0) block_in(b);
289
290 /* invalid block -- cancel */
291 if (ctx->cont <= 0) break;
292
293 }
294
295 BIO_clear_retry_flags(b);
296 BIO_copy_next_retry(b);
297 return(ret);
298 }
299
6b691a5c 300static int ok_write(BIO *b, char *in, int inl)
1745a3fb
BL
301 {
302 int ret=0,n,i;
303 BIO_OK_CTX *ctx;
304
305 ctx=(BIO_OK_CTX *)b->ptr;
306 ret=inl;
307
308 if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0)) return(0);
309
310 if(ctx->sigio) sig_out(b);
311
312 do{
313 BIO_clear_retry_flags(b);
314 n=ctx->buf_len-ctx->buf_off;
315 while (ctx->blockout && n > 0)
316 {
317 i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
318 if (i <= 0)
319 {
320 BIO_copy_next_retry(b);
321 if(!BIO_should_retry(b))
322 ctx->cont= 0;
323 return(i);
324 }
325 ctx->buf_off+=i;
326 n-=i;
327 }
328
329 /* at this point all pending data has been written */
330 ctx->blockout= 0;
331 if (ctx->buf_len == ctx->buf_off)
332 {
333 ctx->buf_len=OK_BLOCK_BLOCK;
334 ctx->buf_off=0;
335 }
336
337 if ((in == NULL) || (inl <= 0)) return(0);
338
339 n= (inl+ ctx->buf_len > OK_BLOCK_SIZE+ OK_BLOCK_BLOCK) ?
340 OK_BLOCK_SIZE+ OK_BLOCK_BLOCK- ctx->buf_len : inl;
341
342 memcpy((unsigned char *)(&(ctx->buf[ctx->buf_len])),(unsigned char *)in,n);
343 ctx->buf_len+= n;
344 inl-=n;
345 in+=n;
346
347 if(ctx->buf_len >= OK_BLOCK_SIZE+ OK_BLOCK_BLOCK)
348 {
349 block_out(b);
350 }
351 }while(inl > 0);
352
353 BIO_clear_retry_flags(b);
354 BIO_copy_next_retry(b);
355 return(ret);
356 }
357
6b691a5c 358static long ok_ctrl(BIO *b, int cmd, long num, char *ptr)
1745a3fb
BL
359 {
360 BIO_OK_CTX *ctx;
361 EVP_MD *md;
e778802f 362 const EVP_MD **ppmd;
1745a3fb
BL
363 long ret=1;
364 int i;
365
366 ctx=(BIO_OK_CTX *)b->ptr;
367
368 switch (cmd)
369 {
370 case BIO_CTRL_RESET:
371 ctx->buf_len=0;
372 ctx->buf_off=0;
373 ctx->buf_len_save=0;
374 ctx->buf_off_save=0;
375 ctx->cont=1;
376 ctx->finished=0;
377 ctx->blockout= 0;
378 ctx->sigio=1;
379 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
380 break;
381 case BIO_CTRL_EOF: /* More to read */
382 if (ctx->cont <= 0)
383 ret=1;
384 else
385 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
386 break;
387 case BIO_CTRL_PENDING: /* More to read in buffer */
388 case BIO_CTRL_WPENDING: /* More to read in buffer */
389 ret=ctx->blockout ? ctx->buf_len-ctx->buf_off : 0;
390 if (ret <= 0)
391 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
392 break;
393 case BIO_CTRL_FLUSH:
394 /* do a final write */
395 if(ctx->blockout == 0)
396 block_out(b);
397
398 while (ctx->blockout)
399 {
400 i=ok_write(b,NULL,0);
401 if (i < 0)
402 {
403 ret=i;
404 break;
405 }
406 }
407
408 ctx->finished=1;
409 ctx->buf_off=ctx->buf_len=0;
410 ctx->cont=(int)ret;
411
412 /* Finally flush the underlying BIO */
413 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
414 break;
415 case BIO_C_DO_STATE_MACHINE:
416 BIO_clear_retry_flags(b);
417 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
418 BIO_copy_next_retry(b);
419 break;
420 case BIO_CTRL_INFO:
421 ret=(long)ctx->cont;
422 break;
423 case BIO_C_SET_MD:
424 md=(EVP_MD *)ptr;
425 EVP_DigestInit(&(ctx->md),md);
426 b->init=1;
427 break;
428 case BIO_C_GET_MD:
429 if (b->init)
430 {
e778802f 431 ppmd=(const EVP_MD **)ptr;
1745a3fb
BL
432 *ppmd=ctx->md.digest;
433 }
434 else
435 ret=0;
436 break;
437 default:
438 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
439 break;
440 }
441 return(ret);
442 }
443
444static void longswap(char* ptr, int len)
445{
446#ifndef L_ENDIAN
447 int i;
448
449 for(i= 0;i < len;i+= 4){
450 *((unsigned long *)&(ptr[i]))= swapem(*((unsigned long *)&(ptr[i])));
451 }
452#endif
453}
454
455static void sig_out(BIO* b)
456 {
457 BIO_OK_CTX *ctx;
458 EVP_MD_CTX *md;
459
460 ctx=(BIO_OK_CTX *)b->ptr;
461 md= &(ctx->md);
462
463 if(ctx->buf_len+ 2* md->digest->md_size > OK_BLOCK_SIZE) return;
464
465 EVP_DigestInit(md, md->digest);
466 RAND_bytes(&(md->md.base[0]), md->digest->md_size);
467 memcpy(&(ctx->buf[ctx->buf_len]), &(md->md.base[0]), md->digest->md_size);
468 longswap(&(ctx->buf[ctx->buf_len]), md->digest->md_size);
469 ctx->buf_len+= md->digest->md_size;
470
471 EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN));
472 md->digest->final(&(ctx->buf[ctx->buf_len]), &(md->md.base[0]));
473 ctx->buf_len+= md->digest->md_size;
474 ctx->blockout= 1;
475 ctx->sigio= 0;
476 }
477
478static void sig_in(BIO* b)
479 {
480 BIO_OK_CTX *ctx;
481 EVP_MD_CTX *md;
482 unsigned char tmp[EVP_MAX_MD_SIZE];
483 int ret= 0;
484
485 ctx=(BIO_OK_CTX *)b->ptr;
486 md= &(ctx->md);
487
488 if(ctx->buf_len- ctx->buf_off < 2* md->digest->md_size) return;
489
490 EVP_DigestInit(md, md->digest);
491 memcpy(&(md->md.base[0]), &(ctx->buf[ctx->buf_off]), md->digest->md_size);
492 longswap(&(md->md.base[0]), md->digest->md_size);
493 ctx->buf_off+= md->digest->md_size;
494
495 EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN));
496 md->digest->final(tmp, &(md->md.base[0]));
497 ret= memcmp(&(ctx->buf[ctx->buf_off]), tmp, md->digest->md_size) == 0;
498 ctx->buf_off+= md->digest->md_size;
499 if(ret == 1)
500 {
501 ctx->sigio= 0;
502 if(ctx->buf_len != ctx->buf_off)
503 {
504 memmove(ctx->buf, &(ctx->buf[ctx->buf_off]), ctx->buf_len- ctx->buf_off);
505 }
506 ctx->buf_len-= ctx->buf_off;
507 ctx->buf_off= 0;
508 }
509 else
510 {
511 ctx->cont= 0;
512 }
513 }
514
515static void block_out(BIO* b)
516 {
517 BIO_OK_CTX *ctx;
518 EVP_MD_CTX *md;
519 unsigned long tl;
520
521 ctx=(BIO_OK_CTX *)b->ptr;
522 md= &(ctx->md);
523
524 tl= ctx->buf_len- OK_BLOCK_BLOCK;
525 tl= swapem(tl);
526 memcpy(ctx->buf, &tl, OK_BLOCK_BLOCK);
527 tl= swapem(tl);
528 EVP_DigestUpdate(md, &(ctx->buf[OK_BLOCK_BLOCK]), tl);
529 md->digest->final(&(ctx->buf[ctx->buf_len]), &(md->md.base[0]));
530 ctx->buf_len+= md->digest->md_size;
531 ctx->blockout= 1;
532 }
533
534static void block_in(BIO* b)
535 {
536 BIO_OK_CTX *ctx;
537 EVP_MD_CTX *md;
06c68491 538 long tl= 0;
1745a3fb
BL
539 unsigned char tmp[EVP_MAX_MD_SIZE];
540
541 ctx=(BIO_OK_CTX *)b->ptr;
542 md= &(ctx->md);
543
544 memcpy(&tl, ctx->buf, OK_BLOCK_BLOCK);
545 tl= swapem(tl);
546 if (ctx->buf_len < tl+ OK_BLOCK_BLOCK+ md->digest->md_size) return;
547
548 EVP_DigestUpdate(md, &(ctx->buf[OK_BLOCK_BLOCK]), tl);
549 md->digest->final(tmp, &(md->md.base[0]));
550 if(memcmp(&(ctx->buf[tl+ OK_BLOCK_BLOCK]), tmp, md->digest->md_size) == 0)
551 {
552 /* there might be parts from next block lurking around ! */
553 ctx->buf_off_save= tl+ OK_BLOCK_BLOCK+ md->digest->md_size;
554 ctx->buf_len_save= ctx->buf_len;
555 ctx->buf_off= OK_BLOCK_BLOCK;
556 ctx->buf_len= tl+ OK_BLOCK_BLOCK;
557 ctx->blockout= 1;
558 }
559 else
560 {
561 ctx->cont= 0;
562 }
563 }
564