This commit was generated by cvs2svn to track changes on a CVS vendor
[openssl.git] / crypto / pkcs7 / bio_ber.c
1 /* crypto/evp/bio_ber.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 #include <stdio.h>
60 #include <errno.h>
61 #include "cryptlib.h"
62 #include "buffer.h"
63 #include "evp.h"
64
65 #ifndef NOPROTO
66 static int ber_write(BIO *h,char *buf,int num);
67 static int ber_read(BIO *h,char *buf,int size);
68 /*static int ber_puts(BIO *h,char *str); */
69 /*static int ber_gets(BIO *h,char *str,int size); */
70 static long ber_ctrl(BIO *h,int cmd,long arg1,char *arg2);
71 static int ber_new(BIO *h);
72 static int ber_free(BIO *data);
73 #else
74 static int ber_write();
75 static int ber_read();
76 /*static int ber_puts(); */
77 /*static int ber_gets(); */
78 static long ber_ctrl();
79 static int ber_new();
80 static int ber_free();
81 #endif
82
83 #define BER_BUF_SIZE    (32)
84
85 /* This is used to hold the state of the BER objects being read. */
86 typedef struct ber_struct
87         {
88         int tag;
89         int class;
90         long length;
91         int inf;
92         int num_left;
93         int depth;
94         } BER_CTX;
95
96 typedef struct bio_ber_struct
97         {
98         int tag;
99         int class;
100         long length;
101         int inf;
102
103         /* most of the following are used when doing non-blocking IO */
104         /* reading */
105         long num_left;  /* number of bytes still to read/write in block */
106         int depth;      /* used with idefinite encoding. */
107         int finished;   /* No more read data */
108
109         /* writting */ 
110         char *w_addr;
111         int w_offset;
112         int w_left;
113
114         int buf_len;
115         int buf_off;
116         unsigned char buf[BER_BUF_SIZE];
117         } BIO_BER_CTX;
118
119 static BIO_METHOD methods_ber=
120         {
121         BIO_TYPE_CIPHER,"cipher",
122         ber_write,
123         ber_read,
124         NULL, /* ber_puts, */
125         NULL, /* ber_gets, */
126         ber_ctrl,
127         ber_new,
128         ber_free,
129         };
130
131 BIO_METHOD *BIO_f_ber()
132         {
133         return(&methods_ber);
134         }
135
136 static int ber_new(bi)
137 BIO *bi;
138         {
139         BIO_BER_CTX *ctx;
140
141         ctx=(BIO_BER_CTX *)Malloc(sizeof(BIO_BER_CTX));
142         if (ctx == NULL) return(0);
143
144         memset((char *)ctx,0,sizeof(BIO_BER_CTX));
145
146         bi->init=0;
147         bi->ptr=(char *)ctx;
148         bi->flags=0;
149         return(1);
150         }
151
152 static int ber_free(a)
153 BIO *a;
154         {
155         BIO_BER_CTX *b;
156
157         if (a == NULL) return(0);
158         b=(BIO_BER_CTX *)a->ptr;
159         memset(a->ptr,0,sizeof(BIO_BER_CTX));
160         Free(a->ptr);
161         a->ptr=NULL;
162         a->init=0;
163         a->flags=0;
164         return(1);
165         }
166
167 int bio_ber_get_header(bio,ctx)
168 BIO *bio;
169 BIO_BER_CTX *ctx;
170         {
171         char buf[64];
172         int i,j,n;
173         int ret;
174         unsigned char *p;
175         unsigned long length
176         int tag;
177         int class;
178         long max;
179
180         BIO_clear_retry_flags(b);
181
182         /* Pack the buffer down if there is a hole at the front */
183         if (ctx->buf_off != 0)
184                 {
185                 p=ctx->buf;
186                 j=ctx->buf_off;
187                 n=ctx->buf_len-j;
188                 for (i=0; i<n; i++)
189                         {
190                         p[0]=p[j];
191                         p++;
192                         }
193                 ctx->buf_len-j;
194                 ctx->buf_off=0;
195                 }
196
197         /* If there is more room, read some more data */
198         i=BER_BUF_SIZE-ctx->buf_len;
199         if (i)
200                 {
201                 i=BIO_read(bio->next_bio,&(ctx->buf[ctx->buf_len]),i);
202                 if (i <= 0)
203                         {
204                         BIO_copy_next_retry(b);
205                         return(i);
206                         }
207                 else
208                         ctx->buf_len+=i;
209                 }
210
211         max=ctx->buf_len;
212         p=ctx->buf;
213         ret=ASN1_get_object(&p,&length,&tag,&class,max);
214
215         if (ret & 0x80)
216                 {
217                 if ((ctx->buf_len < BER_BUF_SIZE) &&
218                         (ERR_GET_REASON(ERR_peek_error()) == ASN1_R_TOO_LONG))
219                         {
220                         ERR_get_error(); /* clear the error */
221                         BIO_set_retry_read(b);
222                         }
223                 return(-1);
224                 }
225
226         /* We have no error, we have a header, so make use of it */
227
228         if ((ctx->tag  >= 0) && (ctx->tag != tag))
229                 {
230                 BIOerr(BIO_F_BIO_BER_GET_HEADER,BIO_R_TAG_MISMATCH);
231                 sprintf(buf,"tag=%d, got %d",ctx->tag,tag);
232                 ERR_add_error_data(1,buf);
233                 return(-1);
234                 }
235         if (ret & 0x01)
236         if (ret & V_ASN1_CONSTRUCTED)
237         }
238         
239 static int ber_read(b,out,outl)
240 BIO *b;
241 char *out;
242 int outl;
243         {
244         int ret=0,i,n;
245         BIO_BER_CTX *ctx;
246
247         BIO_clear_retry_flags(b);
248
249         if (out == NULL) return(0);
250         ctx=(BIO_BER_CTX *)b->ptr;
251
252         if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
253
254         if (ctx->finished) return(0);
255
256 again:
257         /* First see if we are half way through reading a block */
258         if (ctx->num_left > 0)
259                 {
260                 if (ctx->num_left < outl)
261                         n=ctx->num_left;
262                 else
263                         n=outl;
264                 i=BIO_read(b->next_bio,out,n);
265                 if (i <= 0)
266                         {
267                         BIO_copy_next_retry(b);
268                         return(i);
269                         }
270                 ctx->num_left-=i;
271                 outl-=i;
272                 ret+=i;
273                 if (ctx->num_left <= 0)
274                         {
275                         ctx->depth--;
276                         if (ctx->depth <= 0)
277                                 ctx->finished=1;
278                         }
279                 if (outl <= 0)
280                         return(ret);
281                 else
282                         goto again;
283                 }
284         else    /* we need to read another BER header */
285                 {
286                 }
287         }
288
289 static int ber_write(b,in,inl)
290 BIO *b;
291 char *in;
292 int inl;
293         {
294         int ret=0,n,i;
295         BIO_ENC_CTX *ctx;
296
297         ctx=(BIO_ENC_CTX *)b->ptr;
298         ret=inl;
299
300         BIO_clear_retry_flags(b);
301         n=ctx->buf_len-ctx->buf_off;
302         while (n > 0)
303                 {
304                 i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
305                 if (i <= 0)
306                         {
307                         BIO_copy_next_retry(b);
308                         return(i);
309                         }
310                 ctx->buf_off+=i;
311                 n-=i;
312                 }
313         /* at this point all pending data has been written */
314
315         if ((in == NULL) || (inl <= 0)) return(0);
316
317         ctx->buf_off=0;
318         while (inl > 0)
319                 {
320                 n=(inl > ENC_BLOCK_SIZE)?ENC_BLOCK_SIZE:inl;
321                 EVP_CipherUpdate(&(ctx->cipher),
322                         (unsigned char *)ctx->buf,&ctx->buf_len,
323                         (unsigned char *)in,n);
324                 inl-=n;
325                 in+=n;
326
327                 ctx->buf_off=0;
328                 n=ctx->buf_len;
329                 while (n > 0)
330                         {
331                         i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
332                         if (i <= 0)
333                                 {
334                                 BIO_copy_next_retry(b);
335                                 return(i);
336                                 }
337                         n-=i;
338                         ctx->buf_off+=i;
339                         }
340                 ctx->buf_len=0;
341                 ctx->buf_off=0;
342                 }
343         BIO_copy_next_retry(b);
344         return(ret);
345         }
346
347 static long ber_ctrl(b,cmd,num,ptr)
348 BIO *b;
349 int cmd;
350 long num;
351 char *ptr;
352         {
353         BIO *dbio;
354         BIO_ENC_CTX *ctx,*dctx;
355         long ret=1;
356         int i;
357
358         ctx=(BIO_ENC_CTX *)b->ptr;
359
360         switch (cmd)
361                 {
362         case BIO_CTRL_RESET:
363                 ctx->ok=1;
364                 ctx->finished=0;
365                 EVP_CipherInit(&(ctx->cipher),NULL,NULL,NULL,
366                         ctx->cipher.berrypt);
367                 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
368                 break;
369         case BIO_CTRL_EOF:      /* More to read */
370                 if (ctx->cont <= 0)
371                         ret=1;
372                 else
373                         ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
374                 break;
375         case BIO_CTRL_WPENDING:
376                 ret=ctx->buf_len-ctx->buf_off;
377                 if (ret <= 0)
378                         ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
379                 break;
380         case BIO_CTRL_PENDING: /* More to read in buffer */
381                 ret=ctx->buf_len-ctx->buf_off;
382                 if (ret <= 0)
383                         ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
384                 break;
385         case BIO_CTRL_FLUSH:
386                 /* do a final write */
387 again:
388                 while (ctx->buf_len != ctx->buf_off)
389                         {
390                         i=ber_write(b,NULL,0);
391                         if (i < 0)
392                                 {
393                                 ret=i;
394                                 break;
395                                 }
396                         }
397
398                 if (!ctx->finished)
399                         {
400                         ctx->finished=1;
401                         ctx->buf_off=0;
402                         ret=EVP_CipherFinal(&(ctx->cipher),
403                                 (unsigned char *)ctx->buf,
404                                 &(ctx->buf_len));
405                         ctx->ok=(int)ret;
406                         if (ret <= 0) break;
407
408                         /* push out the bytes */
409                         goto again;
410                         }
411                 
412                 /* Finally flush the underlying BIO */
413                 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
414                 break;
415         case BIO_C_GET_CIPHER_STATUS:
416                 ret=(long)ctx->ok;
417                 break;
418         case BIO_C_DO_STATE_MACHINE:
419                 BIO_clear_retry_flags(b);
420                 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
421                 BIO_copy_next_retry(b);
422                 break;
423
424         case BIO_CTRL_DUP:
425                 dbio=(BIO *)ptr;
426                 dctx=(BIO_ENC_CTX *)dbio->ptr;
427                 memcpy(&(dctx->cipher),&(ctx->cipher),sizeof(ctx->cipher));
428                 dbio->init=1;
429                 break;
430         default:
431                 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
432                 break;
433                 }
434         return(ret);
435         }
436
437 /*
438 void BIO_set_cipher_ctx(b,c)
439 BIO *b;
440 EVP_CIPHER_ctx *c;
441         {
442         if (b == NULL) return;
443
444         if ((b->callback != NULL) &&
445                 (b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0))
446                 return;
447
448         b->init=1;
449         ctx=(BIO_ENC_CTX *)b->ptr;
450         memcpy(ctx->cipher,c,sizeof(EVP_CIPHER_CTX));
451         
452         if (b->callback != NULL)
453                 b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L);
454         }
455 */
456
457 void BIO_set_cipher(b,c,k,i,e)
458 BIO *b;
459 EVP_CIPHER *c;
460 unsigned char *k;
461 unsigned char *i;
462 int e;
463         {
464         BIO_ENC_CTX *ctx;
465
466         if (b == NULL) return;
467
468         if ((b->callback != NULL) &&
469                 (b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0))
470                 return;
471
472         b->init=1;
473         ctx=(BIO_ENC_CTX *)b->ptr;
474         EVP_CipherInit(&(ctx->cipher),c,k,i,e);
475         
476         if (b->callback != NULL)
477                 b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L);
478         }
479