e4b9db03cf7368066734fb8d8b45345661f96229
[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 <openssl/buffer.h>
63 #include <openssl/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(void)
132         {
133         return(&methods_ber);
134         }
135
136 static int ber_new(BIO *bi)
137         {
138         BIO_BER_CTX *ctx;
139
140         ctx=(BIO_BER_CTX *)Malloc(sizeof(BIO_BER_CTX));
141         if (ctx == NULL) return(0);
142
143         memset((char *)ctx,0,sizeof(BIO_BER_CTX));
144
145         bi->init=0;
146         bi->ptr=(char *)ctx;
147         bi->flags=0;
148         return(1);
149         }
150
151 static int ber_free(BIO *a)
152         {
153         BIO_BER_CTX *b;
154
155         if (a == NULL) return(0);
156         b=(BIO_BER_CTX *)a->ptr;
157         memset(a->ptr,0,sizeof(BIO_BER_CTX));
158         Free(a->ptr);
159         a->ptr=NULL;
160         a->init=0;
161         a->flags=0;
162         return(1);
163         }
164
165 int bio_ber_get_header(BIO *bio, BIO_BER_CTX *ctx)
166         {
167         char buf[64];
168         int i,j,n;
169         int ret;
170         unsigned char *p;
171         unsigned long length
172         int tag;
173         int class;
174         long max;
175
176         BIO_clear_retry_flags(b);
177
178         /* Pack the buffer down if there is a hole at the front */
179         if (ctx->buf_off != 0)
180                 {
181                 p=ctx->buf;
182                 j=ctx->buf_off;
183                 n=ctx->buf_len-j;
184                 for (i=0; i<n; i++)
185                         {
186                         p[0]=p[j];
187                         p++;
188                         }
189                 ctx->buf_len-j;
190                 ctx->buf_off=0;
191                 }
192
193         /* If there is more room, read some more data */
194         i=BER_BUF_SIZE-ctx->buf_len;
195         if (i)
196                 {
197                 i=BIO_read(bio->next_bio,&(ctx->buf[ctx->buf_len]),i);
198                 if (i <= 0)
199                         {
200                         BIO_copy_next_retry(b);
201                         return(i);
202                         }
203                 else
204                         ctx->buf_len+=i;
205                 }
206
207         max=ctx->buf_len;
208         p=ctx->buf;
209         ret=ASN1_get_object(&p,&length,&tag,&class,max);
210
211         if (ret & 0x80)
212                 {
213                 if ((ctx->buf_len < BER_BUF_SIZE) &&
214                         (ERR_GET_REASON(ERR_peek_error()) == ASN1_R_TOO_LONG))
215                         {
216                         ERR_get_error(); /* clear the error */
217                         BIO_set_retry_read(b);
218                         }
219                 return(-1);
220                 }
221
222         /* We have no error, we have a header, so make use of it */
223
224         if ((ctx->tag  >= 0) && (ctx->tag != tag))
225                 {
226                 BIOerr(BIO_F_BIO_BER_GET_HEADER,BIO_R_TAG_MISMATCH);
227                 sprintf(buf,"tag=%d, got %d",ctx->tag,tag);
228                 ERR_add_error_data(1,buf);
229                 return(-1);
230                 }
231         if (ret & 0x01)
232         if (ret & V_ASN1_CONSTRUCTED)
233         }
234         
235 static int ber_read(BIO *b, char *out, int outl)
236         {
237         int ret=0,i,n;
238         BIO_BER_CTX *ctx;
239
240         BIO_clear_retry_flags(b);
241
242         if (out == NULL) return(0);
243         ctx=(BIO_BER_CTX *)b->ptr;
244
245         if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
246
247         if (ctx->finished) return(0);
248
249 again:
250         /* First see if we are half way through reading a block */
251         if (ctx->num_left > 0)
252                 {
253                 if (ctx->num_left < outl)
254                         n=ctx->num_left;
255                 else
256                         n=outl;
257                 i=BIO_read(b->next_bio,out,n);
258                 if (i <= 0)
259                         {
260                         BIO_copy_next_retry(b);
261                         return(i);
262                         }
263                 ctx->num_left-=i;
264                 outl-=i;
265                 ret+=i;
266                 if (ctx->num_left <= 0)
267                         {
268                         ctx->depth--;
269                         if (ctx->depth <= 0)
270                                 ctx->finished=1;
271                         }
272                 if (outl <= 0)
273                         return(ret);
274                 else
275                         goto again;
276                 }
277         else    /* we need to read another BER header */
278                 {
279                 }
280         }
281
282 static int ber_write(BIO *b, char *in, int inl)
283         {
284         int ret=0,n,i;
285         BIO_ENC_CTX *ctx;
286
287         ctx=(BIO_ENC_CTX *)b->ptr;
288         ret=inl;
289
290         BIO_clear_retry_flags(b);
291         n=ctx->buf_len-ctx->buf_off;
292         while (n > 0)
293                 {
294                 i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
295                 if (i <= 0)
296                         {
297                         BIO_copy_next_retry(b);
298                         return(i);
299                         }
300                 ctx->buf_off+=i;
301                 n-=i;
302                 }
303         /* at this point all pending data has been written */
304
305         if ((in == NULL) || (inl <= 0)) return(0);
306
307         ctx->buf_off=0;
308         while (inl > 0)
309                 {
310                 n=(inl > ENC_BLOCK_SIZE)?ENC_BLOCK_SIZE:inl;
311                 EVP_CipherUpdate(&(ctx->cipher),
312                         (unsigned char *)ctx->buf,&ctx->buf_len,
313                         (unsigned char *)in,n);
314                 inl-=n;
315                 in+=n;
316
317                 ctx->buf_off=0;
318                 n=ctx->buf_len;
319                 while (n > 0)
320                         {
321                         i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
322                         if (i <= 0)
323                                 {
324                                 BIO_copy_next_retry(b);
325                                 return(i);
326                                 }
327                         n-=i;
328                         ctx->buf_off+=i;
329                         }
330                 ctx->buf_len=0;
331                 ctx->buf_off=0;
332                 }
333         BIO_copy_next_retry(b);
334         return(ret);
335         }
336
337 static long ber_ctrl(BIO *b, int cmd, long num, char *ptr)
338         {
339         BIO *dbio;
340         BIO_ENC_CTX *ctx,*dctx;
341         long ret=1;
342         int i;
343
344         ctx=(BIO_ENC_CTX *)b->ptr;
345
346         switch (cmd)
347                 {
348         case BIO_CTRL_RESET:
349                 ctx->ok=1;
350                 ctx->finished=0;
351                 EVP_CipherInit(&(ctx->cipher),NULL,NULL,NULL,
352                         ctx->cipher.berrypt);
353                 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
354                 break;
355         case BIO_CTRL_EOF:      /* More to read */
356                 if (ctx->cont <= 0)
357                         ret=1;
358                 else
359                         ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
360                 break;
361         case BIO_CTRL_WPENDING:
362                 ret=ctx->buf_len-ctx->buf_off;
363                 if (ret <= 0)
364                         ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
365                 break;
366         case BIO_CTRL_PENDING: /* More to read in buffer */
367                 ret=ctx->buf_len-ctx->buf_off;
368                 if (ret <= 0)
369                         ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
370                 break;
371         case BIO_CTRL_FLUSH:
372                 /* do a final write */
373 again:
374                 while (ctx->buf_len != ctx->buf_off)
375                         {
376                         i=ber_write(b,NULL,0);
377                         if (i < 0)
378                                 {
379                                 ret=i;
380                                 break;
381                                 }
382                         }
383
384                 if (!ctx->finished)
385                         {
386                         ctx->finished=1;
387                         ctx->buf_off=0;
388                         ret=EVP_CipherFinal(&(ctx->cipher),
389                                 (unsigned char *)ctx->buf,
390                                 &(ctx->buf_len));
391                         ctx->ok=(int)ret;
392                         if (ret <= 0) break;
393
394                         /* push out the bytes */
395                         goto again;
396                         }
397                 
398                 /* Finally flush the underlying BIO */
399                 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
400                 break;
401         case BIO_C_GET_CIPHER_STATUS:
402                 ret=(long)ctx->ok;
403                 break;
404         case BIO_C_DO_STATE_MACHINE:
405                 BIO_clear_retry_flags(b);
406                 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
407                 BIO_copy_next_retry(b);
408                 break;
409
410         case BIO_CTRL_DUP:
411                 dbio=(BIO *)ptr;
412                 dctx=(BIO_ENC_CTX *)dbio->ptr;
413                 memcpy(&(dctx->cipher),&(ctx->cipher),sizeof(ctx->cipher));
414                 dbio->init=1;
415                 break;
416         default:
417                 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
418                 break;
419                 }
420         return(ret);
421         }
422
423 /*
424 void BIO_set_cipher_ctx(b,c)
425 BIO *b;
426 EVP_CIPHER_ctx *c;
427         {
428         if (b == NULL) return;
429
430         if ((b->callback != NULL) &&
431                 (b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0))
432                 return;
433
434         b->init=1;
435         ctx=(BIO_ENC_CTX *)b->ptr;
436         memcpy(ctx->cipher,c,sizeof(EVP_CIPHER_CTX));
437         
438         if (b->callback != NULL)
439                 b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L);
440         }
441 */
442
443 void BIO_set_cipher(BIO *b, EVP_CIPHER *c, unsigned char *k, unsigned char *i,
444              int e)
445         {
446         BIO_ENC_CTX *ctx;
447
448         if (b == NULL) return;
449
450         if ((b->callback != NULL) &&
451                 (b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0))
452                 return;
453
454         b->init=1;
455         ctx=(BIO_ENC_CTX *)b->ptr;
456         EVP_CipherInit(&(ctx->cipher),c,k,i,e);
457         
458         if (b->callback != NULL)
459                 b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L);
460         }
461