1
/* crypto/evp/bio_ber.c */
2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
23
* Redistribution and use in source and binary forms, with or without
24
* modification, are permitted provided that the following conditions
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)"
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
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.]
62
#include <openssl/buffer.h>
63
#include <openssl/evp.h>
65
static int ber_write(BIO *h,char *buf,int num);
66
static int ber_read(BIO *h,char *buf,int size);
67
/*static int ber_puts(BIO *h,char *str); */
68
/*static int ber_gets(BIO *h,char *str,int size); */
69
static long ber_ctrl(BIO *h,int cmd,long arg1,char *arg2);
70
static int ber_new(BIO *h);
71
static int ber_free(BIO *data);
72
static long ber_callback_ctrl(BIO *h,int cmd,void *(*fp)());
73
#define BER_BUF_SIZE (32)
75
/* This is used to hold the state of the BER objects being read. */
76
typedef struct ber_struct
86
typedef struct bio_ber_struct
93
/* most of the following are used when doing non-blocking IO */
95
long num_left; /* number of bytes still to read/write in block */
96
int depth; /* used with indefinite encoding. */
97
int finished; /* No more read data */
106
unsigned char buf[BER_BUF_SIZE];
109
static BIO_METHOD methods_ber=
111
BIO_TYPE_CIPHER,"cipher",
114
NULL, /* ber_puts, */
115
NULL, /* ber_gets, */
122
BIO_METHOD *BIO_f_ber(void)
124
return(&methods_ber);
127
static int ber_new(BIO *bi)
131
ctx=(BIO_BER_CTX *)OPENSSL_malloc(sizeof(BIO_BER_CTX));
132
if (ctx == NULL) return(0);
134
memset((char *)ctx,0,sizeof(BIO_BER_CTX));
142
static int ber_free(BIO *a)
146
if (a == NULL) return(0);
147
b=(BIO_BER_CTX *)a->ptr;
148
OPENSSL_cleanse(a->ptr,sizeof(BIO_BER_CTX));
149
OPENSSL_free(a->ptr);
156
int bio_ber_get_header(BIO *bio, BIO_BER_CTX *ctx)
167
BIO_clear_retry_flags(b);
169
/* Pack the buffer down if there is a hole at the front */
170
if (ctx->buf_off != 0)
184
/* If there is more room, read some more data */
185
i=BER_BUF_SIZE-ctx->buf_len;
188
i=BIO_read(bio->next_bio,&(ctx->buf[ctx->buf_len]),i);
191
BIO_copy_next_retry(b);
200
ret=ASN1_get_object(&p,&length,&tag,&class,max);
204
if ((ctx->buf_len < BER_BUF_SIZE) &&
205
(ERR_GET_REASON(ERR_peek_error()) == ASN1_R_TOO_LONG))
207
ERR_get_error(); /* clear the error */
208
BIO_set_retry_read(b);
213
/* We have no error, we have a header, so make use of it */
215
if ((ctx->tag >= 0) && (ctx->tag != tag))
217
BIOerr(BIO_F_BIO_BER_GET_HEADER,BIO_R_TAG_MISMATCH);
218
sprintf(buf,"tag=%d, got %d",ctx->tag,tag);
219
ERR_add_error_data(1,buf);
223
if (ret & V_ASN1_CONSTRUCTED)
226
static int ber_read(BIO *b, char *out, int outl)
231
BIO_clear_retry_flags(b);
233
if (out == NULL) return(0);
234
ctx=(BIO_BER_CTX *)b->ptr;
236
if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
238
if (ctx->finished) return(0);
241
/* First see if we are half way through reading a block */
242
if (ctx->num_left > 0)
244
if (ctx->num_left < outl)
248
i=BIO_read(b->next_bio,out,n);
251
BIO_copy_next_retry(b);
257
if (ctx->num_left <= 0)
268
else /* we need to read another BER header */
273
static int ber_write(BIO *b, char *in, int inl)
278
ctx=(BIO_ENC_CTX *)b->ptr;
281
BIO_clear_retry_flags(b);
282
n=ctx->buf_len-ctx->buf_off;
285
i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
288
BIO_copy_next_retry(b);
294
/* at this point all pending data has been written */
296
if ((in == NULL) || (inl <= 0)) return(0);
301
n=(inl > ENC_BLOCK_SIZE)?ENC_BLOCK_SIZE:inl;
302
EVP_CipherUpdate(&(ctx->cipher),
303
(unsigned char *)ctx->buf,&ctx->buf_len,
304
(unsigned char *)in,n);
312
i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
315
BIO_copy_next_retry(b);
324
BIO_copy_next_retry(b);
328
static long ber_ctrl(BIO *b, int cmd, long num, char *ptr)
331
BIO_ENC_CTX *ctx,*dctx;
335
ctx=(BIO_ENC_CTX *)b->ptr;
342
EVP_CipherInit_ex(&(ctx->cipher),NULL,NULL,NULL,NULL,
343
ctx->cipher.berrypt);
344
ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
346
case BIO_CTRL_EOF: /* More to read */
350
ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
352
case BIO_CTRL_WPENDING:
353
ret=ctx->buf_len-ctx->buf_off;
355
ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
357
case BIO_CTRL_PENDING: /* More to read in buffer */
358
ret=ctx->buf_len-ctx->buf_off;
360
ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
363
/* do a final write */
365
while (ctx->buf_len != ctx->buf_off)
367
i=ber_write(b,NULL,0);
379
ret=EVP_CipherFinal_ex(&(ctx->cipher),
380
(unsigned char *)ctx->buf,
385
/* push out the bytes */
389
/* Finally flush the underlying BIO */
390
ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
392
case BIO_C_GET_CIPHER_STATUS:
395
case BIO_C_DO_STATE_MACHINE:
396
BIO_clear_retry_flags(b);
397
ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
398
BIO_copy_next_retry(b);
403
dctx=(BIO_ENC_CTX *)dbio->ptr;
404
memcpy(&(dctx->cipher),&(ctx->cipher),sizeof(ctx->cipher));
408
ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
414
static long ber_callback_ctrl(BIO *b, int cmd, void *(*fp)())
418
if (b->next_bio == NULL) return(0);
422
ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
429
void BIO_set_cipher_ctx(b,c)
433
if (b == NULL) return;
435
if ((b->callback != NULL) &&
436
(b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0))
440
ctx=(BIO_ENC_CTX *)b->ptr;
441
memcpy(ctx->cipher,c,sizeof(EVP_CIPHER_CTX));
443
if (b->callback != NULL)
444
b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L);
448
void BIO_set_cipher(BIO *b, EVP_CIPHER *c, unsigned char *k, unsigned char *i,
453
if (b == NULL) return;
455
if ((b->callback != NULL) &&
456
(b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0))
460
ctx=(BIO_ENC_CTX *)b->ptr;
461
EVP_CipherInit_ex(&(ctx->cipher),c,NULL,k,i,e);
463
if (b->callback != NULL)
464
b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L);