* This package is an SSL implementation written
* by Eric Young (eay@cryptsoft.com).
* The implementation was written so as to conform with Netscapes SSL.
- *
+ *
* This library is free for commercial and non-commercial use as long as
* the following conditions are aheared to. The following conditions
* apply to all code found in this distribution, be it the RC4, RSA,
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
* included with this distribution is covered by the same copyright terms
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
- *
+ *
* Copyright remains Eric Young's, and as such any Copyright notices in
* the code are not to be removed.
* If this package is used in a product, Eric Young should be given attribution
* as the author of the parts of the library used.
* This can be in the form of a textual message at program startup or
* in documentation (online or textual) provided with the package.
- *
+ *
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* Eric Young (eay@cryptsoft.com)"
* The word 'cryptographic' can be left out if the rouines from the library
* being used are not cryptographic related :-).
- * 4. If you include any Windows specific code (or a derivative thereof) from
+ * 4. If you include any Windows specific code (or a derivative thereof) from
* the apps directory (application code) you must include an acknowledgement:
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
- *
+ *
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
- *
+ *
* The licence and distribution terms for any publically available version or
* derivative of this code cannot be changed. i.e. this code cannot simply be
* copied and put under another distribution licence
static long b64_ctrl(BIO *h, int cmd, long arg1, void *arg2);
static int b64_new(BIO *h);
static int b64_free(BIO *data);
-static long b64_callback_ctrl(BIO *h,int cmd,bio_info_cb *fp);
+static long b64_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
#define B64_BLOCK_SIZE 1024
#define B64_BLOCK_SIZE2 768
#define B64_NONE 0
#define B64_ENCODE 1
#define B64_DECODE 2
-typedef struct b64_struct
- {
+typedef struct b64_struct {
/*BIO *bio; moved to the BIO structure */
int buf_len;
int buf_off;
int start; /* have we started decoding yet? */
int cont; /* <= 0 when finished */
EVP_ENCODE_CTX base64;
- char buf[EVP_ENCODE_LENGTH(B64_BLOCK_SIZE)+10];
+ char buf[EVP_ENCODE_LENGTH(B64_BLOCK_SIZE) + 10];
char tmp[B64_BLOCK_SIZE];
- } BIO_B64_CTX;
+} BIO_B64_CTX;
-static BIO_METHOD methods_b64= {
+static BIO_METHOD methods_b64 = {
.type = BIO_TYPE_BASE64,
.name = "base64 encoding",
.bwrite = b64_write,
.callback_ctrl = b64_callback_ctrl
};
-BIO_METHOD *BIO_f_base64(void)
- {
- return(&methods_b64);
- }
+BIO_METHOD *
+BIO_f_base64(void)
+{
+ return (&methods_b64);
+}
-static int b64_new(BIO *bi)
- {
+static int
+b64_new(BIO *bi)
+{
BIO_B64_CTX *ctx;
- ctx=(BIO_B64_CTX *)malloc(sizeof(BIO_B64_CTX));
- if (ctx == NULL) return(0);
-
- ctx->buf_len=0;
- ctx->tmp_len=0;
- ctx->tmp_nl=0;
- ctx->buf_off=0;
- ctx->cont=1;
- ctx->start=1;
- ctx->encode=0;
-
- bi->init=1;
- bi->ptr=(char *)ctx;
- bi->flags=0;
+ ctx = (BIO_B64_CTX *)malloc(sizeof(BIO_B64_CTX));
+ if (ctx == NULL)
+ return (0);
+
+ ctx->buf_len = 0;
+ ctx->tmp_len = 0;
+ ctx->tmp_nl = 0;
+ ctx->buf_off = 0;
+ ctx->cont = 1;
+ ctx->start = 1;
+ ctx->encode = 0;
+
+ bi->init = 1;
+ bi->ptr = (char *)ctx;
+ bi->flags = 0;
bi->num = 0;
- return(1);
- }
-
-static int b64_free(BIO *a)
- {
- if (a == NULL) return(0);
+ return (1);
+}
+
+static int
+b64_free(BIO *a)
+{
+ if (a == NULL)
+ return (0);
free(a->ptr);
- a->ptr=NULL;
- a->init=0;
- a->flags=0;
- return(1);
- }
-
-static int b64_read(BIO *b, char *out, int outl)
- {
- int ret=0,i,ii,j,k,x,n,num,ret_code=0;
+ a->ptr = NULL;
+ a->init = 0;
+ a->flags = 0;
+ return (1);
+}
+
+static int
+b64_read(BIO *b, char *out, int outl)
+{
+ int ret = 0, i, ii, j,k, x,n, num, ret_code = 0;
BIO_B64_CTX *ctx;
- unsigned char *p,*q;
+ unsigned char *p, *q;
- if (out == NULL) return(0);
- ctx=(BIO_B64_CTX *)b->ptr;
+ if (out == NULL)
+ return (0);
+ ctx = (BIO_B64_CTX *)b->ptr;
- if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
+ if ((ctx == NULL) || (b->next_bio == NULL))
+ return (0);
BIO_clear_retry_flags(b);
- if (ctx->encode != B64_DECODE)
- {
- ctx->encode=B64_DECODE;
- ctx->buf_len=0;
- ctx->buf_off=0;
- ctx->tmp_len=0;
+ if (ctx->encode != B64_DECODE) {
+ ctx->encode = B64_DECODE;
+ ctx->buf_len = 0;
+ ctx->buf_off = 0;
+ ctx->tmp_len = 0;
EVP_DecodeInit(&(ctx->base64));
- }
+ }
/* First check if there are bytes decoded/encoded */
- if (ctx->buf_len > 0)
- {
+ if (ctx->buf_len > 0) {
OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
- i=ctx->buf_len-ctx->buf_off;
- if (i > outl) i=outl;
- OPENSSL_assert(ctx->buf_off+i < (int)sizeof(ctx->buf));
- memcpy(out,&(ctx->buf[ctx->buf_off]),i);
- ret=i;
- out+=i;
- outl-=i;
- ctx->buf_off+=i;
- if (ctx->buf_len == ctx->buf_off)
- {
- ctx->buf_len=0;
- ctx->buf_off=0;
- }
+ i = ctx->buf_len - ctx->buf_off;
+ if (i > outl)
+ i = outl;
+ OPENSSL_assert(ctx->buf_off + i < (int)sizeof(ctx->buf));
+ memcpy(out, &(ctx->buf[ctx->buf_off]), i);
+ ret = i;
+ out += i;
+ outl -= i;
+ ctx->buf_off += i;
+ if (ctx->buf_len == ctx->buf_off) {
+ ctx->buf_len = 0;
+ ctx->buf_off = 0;
}
+ }
/* At this point, we have room of outl bytes and an empty
* buffer, so we should read in some more. */
- ret_code=0;
- while (outl > 0)
- {
+ ret_code = 0;
+ while (outl > 0) {
if (ctx->cont <= 0)
break;
- i=BIO_read(b->next_bio,&(ctx->tmp[ctx->tmp_len]),
- B64_BLOCK_SIZE-ctx->tmp_len);
+ i = BIO_read(b->next_bio, &(ctx->tmp[ctx->tmp_len]),
+ B64_BLOCK_SIZE - ctx->tmp_len);
- if (i <= 0)
- {
- ret_code=i;
+ if (i <= 0) {
+ ret_code = i;
/* Should we continue next time we are called? */
- if (!BIO_should_retry(b->next_bio))
- {
- ctx->cont=i;
+ if (!BIO_should_retry(b->next_bio)) {
+ ctx->cont = i;
/* If buffer empty break */
- if(ctx->tmp_len == 0)
+ if (ctx->tmp_len == 0)
break;
/* Fall through and process what we have */
else
i = 0;
- }
+ }
/* else we retry and add more data to buffer */
else
break;
- }
- i+=ctx->tmp_len;
+ }
+ i += ctx->tmp_len;
ctx->tmp_len = i;
/* We need to scan, a line at a time until we
* have a valid line if we are starting. */
- if (ctx->start && (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL))
- {
+ if (ctx->start && (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)) {
/* ctx->start=1; */
- ctx->tmp_len=0;
- }
- else if (ctx->start)
- {
- q=p=(unsigned char *)ctx->tmp;
- for (j=0; j<i; j++)
- {
- if (*(q++) != '\n') continue;
+ ctx->tmp_len = 0;
+ } else if (ctx->start) {
+ q = p=(unsigned char *)ctx->tmp;
+ for (j = 0; j < i; j++) {
+ if (*(q++) != '\n')
+ continue;
/* due to a previous very long line,
* we need to keep on scanning for a '\n'
* before we even start looking for
* base64 encoded stuff. */
- if (ctx->tmp_nl)
- {
- p=q;
- ctx->tmp_nl=0;
+ if (ctx->tmp_nl) {
+ p = q;
+ ctx->tmp_nl = 0;
continue;
- }
+ }
- k=EVP_DecodeUpdate(&(ctx->base64),
- (unsigned char *)ctx->buf,
- &num,p,q-p);
+ k = EVP_DecodeUpdate(&(ctx->base64),
+ (unsigned char *)ctx->buf,
+ &num, p, q - p);
if ((k <= 0) && (num == 0) && (ctx->start))
EVP_DecodeInit(&ctx->base64);
- else
- {
+ else {
if (p != (unsigned char *)
- &(ctx->tmp[0]))
- {
- i-=(p- (unsigned char *)
- &(ctx->tmp[0]));
- for (x=0; x < i; x++)
- ctx->tmp[x]=p[x];
- }
+ &(ctx->tmp[0])) {
+ i -= (p - (unsigned char *)
+ &(ctx->tmp[0]));
+ for (x = 0; x < i; x++)
+ ctx->tmp[x] = p[x];
+ }
EVP_DecodeInit(&ctx->base64);
- ctx->start=0;
+ ctx->start = 0;
break;
- }
- p=q;
}
+ p = q;
+ }
/* we fell off the end without starting */
- if ((j == i) && (num == 0))
- {
+ if ((j == i) && (num == 0)) {
/* Is this is one long chunk?, if so, keep on
* reading until a new line. */
- if (p == (unsigned char *)&(ctx->tmp[0]))
- {
+ if (p == (unsigned char *)&(ctx->tmp[0])) {
/* Check buffer full */
- if (i == B64_BLOCK_SIZE)
- {
- ctx->tmp_nl=1;
- ctx->tmp_len=0;
- }
+ if (i == B64_BLOCK_SIZE) {
+ ctx->tmp_nl = 1;
+ ctx->tmp_len = 0;
}
+ }
else if (p != q) /* finished on a '\n' */
- {
- n=q-p;
- for (ii=0; ii<n; ii++)
- ctx->tmp[ii]=p[ii];
- ctx->tmp_len=n;
- }
+ {
+ n = q - p;
+ for (ii = 0; ii < n; ii++)
+ ctx->tmp[ii] = p[ii];
+ ctx->tmp_len = n;
+ }
/* else finished on a '\n' */
continue;
- }
- else
- {
- ctx->tmp_len=0;
+ } else {
+ ctx->tmp_len = 0;
}
- }
- else if ((i < B64_BLOCK_SIZE) && (ctx->cont > 0))
- {
+ } else if ((i < B64_BLOCK_SIZE) && (ctx->cont > 0)) {
/* If buffer isn't full and we can retry then
* restart to read in more data.
*/
continue;
}
- if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)
- {
- int z,jj;
+ if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
+ int z, jj;
#if 0
- jj=(i>>2)<<2;
+ jj = (i >> 2) << 2;
#else
jj = i & ~3; /* process per 4 */
#endif
- z=EVP_DecodeBlock((unsigned char *)ctx->buf,
- (unsigned char *)ctx->tmp,jj);
- if (jj > 2)
- {
- if (ctx->tmp[jj-1] == '=')
- {
+ z = EVP_DecodeBlock((unsigned char *)ctx->buf,
+ (unsigned char *)ctx->tmp, jj);
+ if (jj > 2) {
+ if (ctx->tmp[jj-1] == '=') {
z--;
if (ctx->tmp[jj-2] == '=')
z--;
- }
}
+ }
/* z is now number of output bytes and jj is the
* number consumed */
- if (jj != i)
- {
- memmove(ctx->tmp, &ctx->tmp[jj], i-jj);
- ctx->tmp_len=i-jj;
- }
- ctx->buf_len=0;
- if (z > 0)
- {
- ctx->buf_len=z;
- }
- i=z;
+ if (jj != i) {
+ memmove(ctx->tmp, &ctx->tmp[jj], i - jj);
+ ctx->tmp_len = i - jj;
}
- else
- {
- i=EVP_DecodeUpdate(&(ctx->base64),
- (unsigned char *)ctx->buf,&ctx->buf_len,
- (unsigned char *)ctx->tmp,i);
- ctx->tmp_len = 0;
+ ctx->buf_len = 0;
+ if (z > 0) {
+ ctx->buf_len = z;
}
- ctx->buf_off=0;
- if (i < 0)
- {
- ret_code=0;
- ctx->buf_len=0;
+ i = z;
+ } else {
+ i = EVP_DecodeUpdate(&(ctx->base64),
+ (unsigned char *)ctx->buf, &ctx->buf_len,
+ (unsigned char *)ctx->tmp, i);
+ ctx->tmp_len = 0;
+ }
+ ctx->buf_off = 0;
+ if (i < 0) {
+ ret_code = 0;
+ ctx->buf_len = 0;
break;
- }
+ }
if (ctx->buf_len <= outl)
- i=ctx->buf_len;
+ i = ctx->buf_len;
else
- i=outl;
-
- memcpy(out,ctx->buf,i);
- ret+=i;
- ctx->buf_off=i;
- if (ctx->buf_off == ctx->buf_len)
- {
- ctx->buf_len=0;
- ctx->buf_off=0;
- }
- outl-=i;
- out+=i;
+ i = outl;
+
+ memcpy(out, ctx->buf, i);
+ ret += i;
+ ctx->buf_off = i;
+ if (ctx->buf_off == ctx->buf_len) {
+ ctx->buf_len = 0;
+ ctx->buf_off = 0;
}
+ outl -= i;
+ out += i;
+ }
/* BIO_clear_retry_flags(b); */
BIO_copy_next_retry(b);
- return((ret == 0)?ret_code:ret);
- }
+ return ((ret == 0) ? ret_code : ret);
+}
-static int b64_write(BIO *b, const char *in, int inl)
- {
- int ret=0;
+static int
+b64_write(BIO *b, const char *in, int inl)
+{
+ int ret = 0;
int n;
int i;
BIO_B64_CTX *ctx;
- ctx=(BIO_B64_CTX *)b->ptr;
+ ctx = (BIO_B64_CTX *)b->ptr;
BIO_clear_retry_flags(b);
- if (ctx->encode != B64_ENCODE)
- {
- ctx->encode=B64_ENCODE;
- ctx->buf_len=0;
- ctx->buf_off=0;
- ctx->tmp_len=0;
+ if (ctx->encode != B64_ENCODE) {
+ ctx->encode = B64_ENCODE;
+ ctx->buf_len = 0;
+ ctx->buf_off = 0;
+ ctx->tmp_len = 0;
EVP_EncodeInit(&(ctx->base64));
- }
+ }
OPENSSL_assert(ctx->buf_off < (int)sizeof(ctx->buf));
OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
- n=ctx->buf_len-ctx->buf_off;
- while (n > 0)
- {
- i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
- if (i <= 0)
- {
+ n = ctx->buf_len - ctx->buf_off;
+ while (n > 0) {
+ i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n);
+ if (i <= 0) {
BIO_copy_next_retry(b);
- return(i);
- }
+ return (i);
+ }
OPENSSL_assert(i <= n);
- ctx->buf_off+=i;
+ ctx->buf_off += i;
OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
- n-=i;
- }
+ n -= i;
+ }
/* at this point all pending data has been written */
- ctx->buf_off=0;
- ctx->buf_len=0;
+ ctx->buf_off = 0;
+ ctx->buf_len = 0;
- if ((in == NULL) || (inl <= 0)) return(0);
+ if ((in == NULL) || (inl <= 0))
+ return (0);
- while (inl > 0)
- {
- n=(inl > B64_BLOCK_SIZE)?B64_BLOCK_SIZE:inl;
+ while (inl > 0) {
+ n = (inl > B64_BLOCK_SIZE) ? B64_BLOCK_SIZE : inl;
- if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)
- {
- if (ctx->tmp_len > 0)
- {
+ if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
+ if (ctx->tmp_len > 0) {
OPENSSL_assert(ctx->tmp_len <= 3);
- n=3-ctx->tmp_len;
+ n = 3 - ctx->tmp_len;
/* There's a theoretical possibility for this */
- if (n > inl)
- n=inl;
- memcpy(&(ctx->tmp[ctx->tmp_len]),in,n);
- ctx->tmp_len+=n;
+ if (n > inl)
+ n = inl;
+ memcpy(&(ctx->tmp[ctx->tmp_len]), in, n);
+ ctx->tmp_len += n;
ret += n;
if (ctx->tmp_len < 3)
break;
- ctx->buf_len=EVP_EncodeBlock((unsigned char *)ctx->buf,(unsigned char *)ctx->tmp,ctx->tmp_len);
- OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
+ ctx->buf_len = EVP_EncodeBlock(
+ (unsigned char *)ctx->buf,
+ (unsigned char *)ctx->tmp, ctx->tmp_len);
+ OPENSSL_assert(ctx->buf_len <=
+ (int)sizeof(ctx->buf));
OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
/* Since we're now done using the temporary
buffer, the length should be 0'd */
- ctx->tmp_len=0;
- }
- else
- {
- if (n < 3)
- {
- memcpy(ctx->tmp,in,n);
- ctx->tmp_len=n;
+ ctx->tmp_len = 0;
+ } else {
+ if (n < 3) {
+ memcpy(ctx->tmp, in, n);
+ ctx->tmp_len = n;
ret += n;
break;
- }
- n-=n%3;
- ctx->buf_len=EVP_EncodeBlock((unsigned char *)ctx->buf,(const unsigned char *)in,n);
- OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
+ }
+ n -= n % 3;
+ ctx->buf_len = EVP_EncodeBlock(
+ (unsigned char *)ctx->buf,
+ (const unsigned char *)in, n);
+ OPENSSL_assert(ctx->buf_len <=
+ (int)sizeof(ctx->buf));
OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
ret += n;
- }
}
- else
- {
+ } else {
EVP_EncodeUpdate(&(ctx->base64),
- (unsigned char *)ctx->buf,&ctx->buf_len,
- (unsigned char *)in,n);
+ (unsigned char *)ctx->buf, &ctx->buf_len,
+ (unsigned char *)in, n);
OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
ret += n;
- }
- inl-=n;
- in+=n;
-
- ctx->buf_off=0;
- n=ctx->buf_len;
- while (n > 0)
- {
- i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
- if (i <= 0)
- {
+ }
+ inl -= n;
+ in += n;
+
+ ctx->buf_off = 0;
+ n = ctx->buf_len;
+ while (n > 0) {
+ i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n);
+ if (i <= 0) {
BIO_copy_next_retry(b);
- return((ret == 0)?i:ret);
- }
+ return ((ret == 0) ? i : ret);
+ }
OPENSSL_assert(i <= n);
- n-=i;
- ctx->buf_off+=i;
+ n -= i;
+ ctx->buf_off += i;
OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
- }
- ctx->buf_len=0;
- ctx->buf_off=0;
}
- return(ret);
+ ctx->buf_len = 0;
+ ctx->buf_off = 0;
}
+ return (ret);
+}
-static long b64_ctrl(BIO *b, int cmd, long num, void *ptr)
- {
+static long
+b64_ctrl(BIO *b, int cmd, long num, void *ptr)
+{
BIO_B64_CTX *ctx;
- long ret=1;
+ long ret = 1;
int i;
- ctx=(BIO_B64_CTX *)b->ptr;
+ ctx = (BIO_B64_CTX *)b->ptr;
- switch (cmd)
- {
+ switch (cmd) {
case BIO_CTRL_RESET:
- ctx->cont=1;
- ctx->start=1;
- ctx->encode=B64_NONE;
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ctx->cont = 1;
+ ctx->start = 1;
+ ctx->encode = B64_NONE;
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
case BIO_CTRL_EOF: /* More to read */
if (ctx->cont <= 0)
- ret=1;
+ ret = 1;
else
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
case BIO_CTRL_WPENDING: /* More to write in buffer */
OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
- ret=ctx->buf_len-ctx->buf_off;
- if ((ret == 0) && (ctx->encode != B64_NONE)
- && (ctx->base64.num != 0))
- ret=1;
+ ret = ctx->buf_len - ctx->buf_off;
+ if ((ret == 0) && (ctx->encode != B64_NONE) &&
+ (ctx->base64.num != 0))
+ ret = 1;
else if (ret <= 0)
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
case BIO_CTRL_PENDING: /* More to read in buffer */
OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
- ret=ctx->buf_len-ctx->buf_off;
+ ret = ctx->buf_len - ctx->buf_off;
if (ret <= 0)
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
case BIO_CTRL_FLUSH:
/* do a final write */
again:
- while (ctx->buf_len != ctx->buf_off)
- {
- i=b64_write(b,NULL,0);
+ while (ctx->buf_len != ctx->buf_off) {
+ i = b64_write(b, NULL, 0);
if (i < 0)
return i;
- }
- if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)
- {
- if (ctx->tmp_len != 0)
- {
- ctx->buf_len=EVP_EncodeBlock(
- (unsigned char *)ctx->buf,
- (unsigned char *)ctx->tmp,
- ctx->tmp_len);
- ctx->buf_off=0;
- ctx->tmp_len=0;
+ }
+ if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
+ if (ctx->tmp_len != 0) {
+ ctx->buf_len = EVP_EncodeBlock(
+ (unsigned char *)ctx->buf,
+ (unsigned char *)ctx->tmp,
+ ctx->tmp_len);
+ ctx->buf_off = 0;
+ ctx->tmp_len = 0;
goto again;
- }
}
- else if (ctx->encode != B64_NONE && ctx->base64.num != 0)
- {
- ctx->buf_off=0;
+ } else if (ctx->encode != B64_NONE && ctx->base64.num != 0) {
+ ctx->buf_off = 0;
EVP_EncodeFinal(&(ctx->base64),
- (unsigned char *)ctx->buf,
- &(ctx->buf_len));
+ (unsigned char *)ctx->buf,
+ &(ctx->buf_len));
/* push out the bytes */
goto again;
- }
+ }
/* Finally flush the underlying BIO */
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
case BIO_C_DO_STATE_MACHINE:
BIO_clear_retry_flags(b);
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
BIO_copy_next_retry(b);
break;
case BIO_CTRL_GET:
case BIO_CTRL_SET:
default:
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
- }
- return(ret);
}
+ return (ret);
+}
-static long b64_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
- {
- long ret=1;
+static long
+b64_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
+{
+ long ret = 1;
- if (b->next_bio == NULL) return(0);
- switch (cmd)
- {
+ if (b->next_bio == NULL)
+ return (0);
+ switch (cmd) {
default:
- ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
+ ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
break;
- }
- return(ret);
- }
-
-static int b64_puts(BIO *b, const char *str)
- {
- return b64_write(b,str,strlen(str));
}
+ return (ret);
+}
+
+static int
+b64_puts(BIO *b, const char *str)
+{
+ return b64_write(b, str, strlen(str));
+}
* This package is an SSL implementation written
* by Eric Young (eay@cryptsoft.com).
* The implementation was written so as to conform with Netscapes SSL.
- *
+ *
* This library is free for commercial and non-commercial use as long as
* the following conditions are aheared to. The following conditions
* apply to all code found in this distribution, be it the RC4, RSA,
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
* included with this distribution is covered by the same copyright terms
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
- *
+ *
* Copyright remains Eric Young's, and as such any Copyright notices in
* the code are not to be removed.
* If this package is used in a product, Eric Young should be given attribution
* as the author of the parts of the library used.
* This can be in the form of a textual message at program startup or
* in documentation (online or textual) provided with the package.
- *
+ *
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* Eric Young (eay@cryptsoft.com)"
* The word 'cryptographic' can be left out if the rouines from the library
* being used are not cryptographic related :-).
- * 4. If you include any Windows specific code (or a derivative thereof) from
+ * 4. If you include any Windows specific code (or a derivative thereof) from
* the apps directory (application code) you must include an acknowledgement:
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
- *
+ *
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
- *
+ *
* The licence and distribution terms for any publically available version or
* derivative of this code cannot be changed. i.e. this code cannot simply be
* copied and put under another distribution licence
#define ENC_BLOCK_SIZE (1024*4)
#define BUF_OFFSET (EVP_MAX_BLOCK_LENGTH*2)
-typedef struct enc_struct
- {
+typedef struct enc_struct {
int buf_len;
int buf_off;
int cont; /* <= 0 when finished */
/* buf is larger than ENC_BLOCK_SIZE because EVP_DecryptUpdate
* can return up to a block more data than is presented to it
*/
- char buf[ENC_BLOCK_SIZE+BUF_OFFSET+2];
- } BIO_ENC_CTX;
+ char buf[ENC_BLOCK_SIZE + BUF_OFFSET + 2];
+} BIO_ENC_CTX;
-static BIO_METHOD methods_enc= {
+static BIO_METHOD methods_enc = {
.type = BIO_TYPE_CIPHER,
.name = "cipher",
.bwrite = enc_write,
.callback_ctrl = enc_callback_ctrl
};
-BIO_METHOD *BIO_f_cipher(void)
- {
- return(&methods_enc);
- }
+BIO_METHOD *
+BIO_f_cipher(void)
+{
+ return (&methods_enc);
+}
-static int enc_new(BIO *bi)
- {
+static int
+enc_new(BIO *bi)
+{
BIO_ENC_CTX *ctx;
- ctx=(BIO_ENC_CTX *)malloc(sizeof(BIO_ENC_CTX));
- if (ctx == NULL) return(0);
+ ctx = (BIO_ENC_CTX *)malloc(sizeof(BIO_ENC_CTX));
+ if (ctx == NULL)
+ return (0);
EVP_CIPHER_CTX_init(&ctx->cipher);
- ctx->buf_len=0;
- ctx->buf_off=0;
- ctx->cont=1;
- ctx->finished=0;
- ctx->ok=1;
-
- bi->init=0;
- bi->ptr=(char *)ctx;
- bi->flags=0;
- return(1);
- }
-
-static int enc_free(BIO *a)
- {
+ ctx->buf_len = 0;
+ ctx->buf_off = 0;
+ ctx->cont = 1;
+ ctx->finished = 0;
+ ctx->ok = 1;
+
+ bi->init = 0;
+ bi->ptr = (char *)ctx;
+ bi->flags = 0;
+ return (1);
+}
+
+static int
+enc_free(BIO *a)
+{
BIO_ENC_CTX *b;
- if (a == NULL) return(0);
- b=(BIO_ENC_CTX *)a->ptr;
+ if (a == NULL)
+ return (0);
+ b = (BIO_ENC_CTX *)a->ptr;
EVP_CIPHER_CTX_cleanup(&(b->cipher));
- OPENSSL_cleanse(a->ptr,sizeof(BIO_ENC_CTX));
+ OPENSSL_cleanse(a->ptr, sizeof(BIO_ENC_CTX));
free(a->ptr);
- a->ptr=NULL;
- a->init=0;
- a->flags=0;
- return(1);
- }
-
-static int enc_read(BIO *b, char *out, int outl)
- {
- int ret=0,i;
+ a->ptr = NULL;
+ a->init = 0;
+ a->flags = 0;
+ return (1);
+}
+
+static int
+enc_read(BIO *b, char *out, int outl)
+{
+ int ret = 0, i;
BIO_ENC_CTX *ctx;
- if (out == NULL) return(0);
- ctx=(BIO_ENC_CTX *)b->ptr;
+ if (out == NULL)
+ return (0);
+ ctx = (BIO_ENC_CTX *)b->ptr;
- if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
+ if ((ctx == NULL) || (b->next_bio == NULL))
+ return (0);
/* First check if there are bytes decoded/encoded */
- if (ctx->buf_len > 0)
- {
- i=ctx->buf_len-ctx->buf_off;
- if (i > outl) i=outl;
- memcpy(out,&(ctx->buf[ctx->buf_off]),i);
- ret=i;
- out+=i;
- outl-=i;
- ctx->buf_off+=i;
- if (ctx->buf_len == ctx->buf_off)
- {
- ctx->buf_len=0;
- ctx->buf_off=0;
- }
+ if (ctx->buf_len > 0) {
+ i = ctx->buf_len - ctx->buf_off;
+ if (i > outl)
+ i = outl;
+ memcpy(out, &(ctx->buf[ctx->buf_off]), i);
+ ret = i;
+ out += i;
+ outl -= i;
+ ctx->buf_off += i;
+ if (ctx->buf_len == ctx->buf_off) {
+ ctx->buf_len = 0;
+ ctx->buf_off = 0;
}
+ }
/* At this point, we have room of outl bytes and an empty
* buffer, so we should read in some more. */
- while (outl > 0)
- {
- if (ctx->cont <= 0) break;
+ while (outl > 0) {
+ if (ctx->cont <= 0)
+ break;
/* read in at IV offset, read the EVP_Cipher
* documentation about why */
- i=BIO_read(b->next_bio,&(ctx->buf[BUF_OFFSET]),ENC_BLOCK_SIZE);
+ i = BIO_read(b->next_bio, &(ctx->buf[BUF_OFFSET]), ENC_BLOCK_SIZE);
- if (i <= 0)
- {
+ if (i <= 0) {
/* Should be continue next time we are called? */
- if (!BIO_should_retry(b->next_bio))
- {
- ctx->cont=i;
- i=EVP_CipherFinal_ex(&(ctx->cipher),
- (unsigned char *)ctx->buf,
- &(ctx->buf_len));
- ctx->ok=i;
- ctx->buf_off=0;
- }
- else
- {
- ret=(ret == 0)?i:ret;
+ if (!BIO_should_retry(b->next_bio)) {
+ ctx->cont = i;
+ i = EVP_CipherFinal_ex(&(ctx->cipher),
+ (unsigned char *)ctx->buf,
+ &(ctx->buf_len));
+ ctx->ok = i;
+ ctx->buf_off = 0;
+ } else {
+ ret = (ret == 0) ? i : ret;
break;
- }
}
- else
- {
+ } else {
EVP_CipherUpdate(&(ctx->cipher),
- (unsigned char *)ctx->buf,&ctx->buf_len,
- (unsigned char *)&(ctx->buf[BUF_OFFSET]),i);
- ctx->cont=1;
+ (unsigned char *)ctx->buf, &ctx->buf_len,
+ (unsigned char *)&(ctx->buf[BUF_OFFSET]), i);
+ ctx->cont = 1;
/* Note: it is possible for EVP_CipherUpdate to
* decrypt zero bytes because this is or looks like
* the final block: if this happens we should retry
* and either read more data or decrypt the final
* block
*/
- if(ctx->buf_len == 0) continue;
- }
+ if (ctx->buf_len == 0)
+ continue;
+ }
if (ctx->buf_len <= outl)
- i=ctx->buf_len;
+ i = ctx->buf_len;
else
- i=outl;
- if (i <= 0) break;
- memcpy(out,ctx->buf,i);
- ret+=i;
- ctx->buf_off=i;
- outl-=i;
- out+=i;
- }
+ i = outl;
+ if (i <= 0)
+ break;
+ memcpy(out, ctx->buf, i);
+ ret += i;
+ ctx->buf_off = i;
+ outl -= i;
+ out += i;
+ }
BIO_clear_retry_flags(b);
BIO_copy_next_retry(b);
- return((ret == 0)?ctx->cont:ret);
- }
+ return ((ret == 0) ? ctx->cont : ret);
+}
-static int enc_write(BIO *b, const char *in, int inl)
- {
- int ret=0,n,i;
+static int
+enc_write(BIO *b, const char *in, int inl)
+{
+ int ret = 0, n, i;
BIO_ENC_CTX *ctx;
- ctx=(BIO_ENC_CTX *)b->ptr;
- ret=inl;
+ ctx = (BIO_ENC_CTX *)b->ptr;
+ ret = inl;
BIO_clear_retry_flags(b);
- n=ctx->buf_len-ctx->buf_off;
- while (n > 0)
- {
- i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
- if (i <= 0)
- {
+ n = ctx->buf_len - ctx->buf_off;
+ while (n > 0) {
+ i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n);
+ if (i <= 0) {
BIO_copy_next_retry(b);
- return(i);
- }
- ctx->buf_off+=i;
- n-=i;
+ return (i);
}
+ ctx->buf_off += i;
+ n -= i;
+ }
/* at this point all pending data has been written */
- if ((in == NULL) || (inl <= 0)) return(0);
+ if ((in == NULL) || (inl <= 0))
+ return (0);
- ctx->buf_off=0;
- while (inl > 0)
- {
- n=(inl > ENC_BLOCK_SIZE)?ENC_BLOCK_SIZE:inl;
+ ctx->buf_off = 0;
+ while (inl > 0) {
+ n = (inl > ENC_BLOCK_SIZE) ? ENC_BLOCK_SIZE : inl;
EVP_CipherUpdate(&(ctx->cipher),
- (unsigned char *)ctx->buf,&ctx->buf_len,
- (unsigned char *)in,n);
- inl-=n;
- in+=n;
-
- ctx->buf_off=0;
- n=ctx->buf_len;
- while (n > 0)
- {
- i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
- if (i <= 0)
- {
+ (unsigned char *)ctx->buf, &ctx->buf_len,
+ (unsigned char *)in, n);
+ inl -= n;
+ in += n;
+
+ ctx->buf_off = 0;
+ n = ctx->buf_len;
+ while (n > 0) {
+ i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n);
+ if (i <= 0) {
BIO_copy_next_retry(b);
return (ret == inl) ? i : ret - inl;
- }
- n-=i;
- ctx->buf_off+=i;
}
- ctx->buf_len=0;
- ctx->buf_off=0;
+ n -= i;
+ ctx->buf_off += i;
}
- BIO_copy_next_retry(b);
- return(ret);
+ ctx->buf_len = 0;
+ ctx->buf_off = 0;
}
+ BIO_copy_next_retry(b);
+ return (ret);
+}
-static long enc_ctrl(BIO *b, int cmd, long num, void *ptr)
- {
+static long
+enc_ctrl(BIO *b, int cmd, long num, void *ptr)
+{
BIO *dbio;
- BIO_ENC_CTX *ctx,*dctx;
- long ret=1;
+ BIO_ENC_CTX *ctx, *dctx;
+ long ret = 1;
int i;
EVP_CIPHER_CTX **c_ctx;
- ctx=(BIO_ENC_CTX *)b->ptr;
+ ctx = (BIO_ENC_CTX *)b->ptr;
- switch (cmd)
- {
+ switch (cmd) {
case BIO_CTRL_RESET:
- ctx->ok=1;
- ctx->finished=0;
- EVP_CipherInit_ex(&(ctx->cipher),NULL,NULL,NULL,NULL,
- ctx->cipher.encrypt);
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ctx->ok = 1;
+ ctx->finished = 0;
+ EVP_CipherInit_ex(&(ctx->cipher), NULL, NULL, NULL, NULL,
+ ctx->cipher.encrypt);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
case BIO_CTRL_EOF: /* More to read */
if (ctx->cont <= 0)
- ret=1;
+ ret = 1;
else
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
case BIO_CTRL_WPENDING:
- ret=ctx->buf_len-ctx->buf_off;
+ ret = ctx->buf_len - ctx->buf_off;
if (ret <= 0)
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
case BIO_CTRL_PENDING: /* More to read in buffer */
- ret=ctx->buf_len-ctx->buf_off;
+ ret = ctx->buf_len - ctx->buf_off;
if (ret <= 0)
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
case BIO_CTRL_FLUSH:
/* do a final write */
again:
- while (ctx->buf_len != ctx->buf_off)
- {
- i=enc_write(b,NULL,0);
+ while (ctx->buf_len != ctx->buf_off) {
+ i = enc_write(b, NULL, 0);
if (i < 0)
return i;
- }
+ }
- if (!ctx->finished)
- {
- ctx->finished=1;
- ctx->buf_off=0;
- ret=EVP_CipherFinal_ex(&(ctx->cipher),
- (unsigned char *)ctx->buf,
- &(ctx->buf_len));
- ctx->ok=(int)ret;
- if (ret <= 0) break;
+ if (!ctx->finished) {
+ ctx->finished = 1;
+ ctx->buf_off = 0;
+ ret = EVP_CipherFinal_ex(&(ctx->cipher),
+ (unsigned char *)ctx->buf,
+ &(ctx->buf_len));
+ ctx->ok = (int)ret;
+ if (ret <= 0)
+ break;
/* push out the bytes */
goto again;
- }
-
+ }
+
/* Finally flush the underlying BIO */
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
case BIO_C_GET_CIPHER_STATUS:
- ret=(long)ctx->ok;
+ ret = (long)ctx->ok;
break;
case BIO_C_DO_STATE_MACHINE:
BIO_clear_retry_flags(b);
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
BIO_copy_next_retry(b);
break;
case BIO_C_GET_CIPHER_CTX:
- c_ctx=(EVP_CIPHER_CTX **)ptr;
- (*c_ctx)= &(ctx->cipher);
- b->init=1;
+ c_ctx = (EVP_CIPHER_CTX **)ptr;
+ (*c_ctx) = &(ctx->cipher);
+ b->init = 1;
break;
case BIO_CTRL_DUP:
- dbio=(BIO *)ptr;
- dctx=(BIO_ENC_CTX *)dbio->ptr;
+ dbio = (BIO *)ptr;
+ dctx = (BIO_ENC_CTX *)dbio->ptr;
EVP_CIPHER_CTX_init(&dctx->cipher);
- ret = EVP_CIPHER_CTX_copy(&dctx->cipher,&ctx->cipher);
+ ret = EVP_CIPHER_CTX_copy(&dctx->cipher, &ctx->cipher);
if (ret)
- dbio->init=1;
+ dbio->init = 1;
break;
default:
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
- }
- return(ret);
}
+ return (ret);
+}
-static long enc_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
- {
- long ret=1;
+static long
+enc_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
+{
+ long ret = 1;
- if (b->next_bio == NULL) return(0);
- switch (cmd)
- {
+ if (b->next_bio == NULL)
+ return (0);
+ switch (cmd) {
default:
- ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
+ ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
break;
- }
- return(ret);
}
+ return (ret);
+}
/*
void BIO_set_cipher_ctx(b,c)
b->init=1;
ctx=(BIO_ENC_CTX *)b->ptr;
memcpy(ctx->cipher,c,sizeof(EVP_CIPHER_CTX));
-
+
if (b->callback != NULL)
b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L);
}
*/
-void BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k,
- const unsigned char *i, int e)
- {
+void
+BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k,
+ const unsigned char *i, int e)
+{
BIO_ENC_CTX *ctx;
- if (b == NULL) return;
+ if (b == NULL)
+ return;
if ((b->callback != NULL) &&
- (b->callback(b,BIO_CB_CTRL,(const char *)c,BIO_CTRL_SET,e,0L) <= 0))
+ (b->callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 0L) <= 0))
return;
- b->init=1;
- ctx=(BIO_ENC_CTX *)b->ptr;
- EVP_CipherInit_ex(&(ctx->cipher),c,NULL, k,i,e);
-
- if (b->callback != NULL)
- b->callback(b,BIO_CB_CTRL,(const char *)c,BIO_CTRL_SET,e,1L);
- }
+ b->init = 1;
+ ctx = (BIO_ENC_CTX *)b->ptr;
+ EVP_CipherInit_ex(&(ctx->cipher), c,NULL, k, i, e);
+ if (b->callback != NULL)
+ b->callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 1L);
+}
* This package is an SSL implementation written
* by Eric Young (eay@cryptsoft.com).
* The implementation was written so as to conform with Netscapes SSL.
- *
+ *
* This library is free for commercial and non-commercial use as long as
* the following conditions are aheared to. The following conditions
* apply to all code found in this distribution, be it the RC4, RSA,
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
* included with this distribution is covered by the same copyright terms
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
- *
+ *
* Copyright remains Eric Young's, and as such any Copyright notices in
* the code are not to be removed.
* If this package is used in a product, Eric Young should be given attribution
* as the author of the parts of the library used.
* This can be in the form of a textual message at program startup or
* in documentation (online or textual) provided with the package.
- *
+ *
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* Eric Young (eay@cryptsoft.com)"
* The word 'cryptographic' can be left out if the rouines from the library
* being used are not cryptographic related :-).
- * 4. If you include any Windows specific code (or a derivative thereof) from
+ * 4. If you include any Windows specific code (or a derivative thereof) from
* the apps directory (application code) you must include an acknowledgement:
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
- *
+ *
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
- *
+ *
* The licence and distribution terms for any publically available version or
* derivative of this code cannot be changed. i.e. this code cannot simply be
* copied and put under another distribution licence
static long md_ctrl(BIO *h, int cmd, long arg1, void *arg2);
static int md_new(BIO *h);
static int md_free(BIO *data);
-static long md_callback_ctrl(BIO *h,int cmd,bio_info_cb *fp);
+static long md_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
static BIO_METHOD methods_md = {
.type = BIO_TYPE_MD,
.callback_ctrl = md_callback_ctrl
};
-BIO_METHOD *BIO_f_md(void)
- {
- return(&methods_md);
- }
+BIO_METHOD *
+BIO_f_md(void)
+{
+ return (&methods_md);
+}
-static int md_new(BIO *bi)
- {
+static int
+md_new(BIO *bi)
+{
EVP_MD_CTX *ctx;
- ctx=EVP_MD_CTX_create();
- if (ctx == NULL) return(0);
+ ctx = EVP_MD_CTX_create();
+ if (ctx == NULL)
+ return (0);
- bi->init=0;
- bi->ptr=(char *)ctx;
- bi->flags=0;
- return(1);
- }
+ bi->init = 0;
+ bi->ptr = (char *)ctx;
+ bi->flags = 0;
+ return (1);
+}
-static int md_free(BIO *a)
- {
- if (a == NULL) return(0);
+static int
+md_free(BIO *a)
+{
+ if (a == NULL)
+ return (0);
EVP_MD_CTX_destroy(a->ptr);
- a->ptr=NULL;
- a->init=0;
- a->flags=0;
- return(1);
- }
-
-static int md_read(BIO *b, char *out, int outl)
- {
- int ret=0;
+ a->ptr = NULL;
+ a->init = 0;
+ a->flags = 0;
+ return (1);
+}
+
+static int
+md_read(BIO *b, char *out, int outl)
+{
+ int ret = 0;
EVP_MD_CTX *ctx;
- if (out == NULL) return(0);
- ctx=b->ptr;
+ if (out == NULL)
+ return (0);
+ ctx = b->ptr;
- if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
+ if ((ctx == NULL) || (b->next_bio == NULL))
+ return (0);
- ret=BIO_read(b->next_bio,out,outl);
- if (b->init)
- {
- if (ret > 0)
- {
- if (EVP_DigestUpdate(ctx,(unsigned char *)out,
- (unsigned int)ret)<=0) return (-1);
- }
+ ret = BIO_read(b->next_bio, out, outl);
+ if (b->init) {
+ if (ret > 0) {
+ if (EVP_DigestUpdate(ctx, (unsigned char *)out,
+ (unsigned int)ret) <= 0)
+ return (-1);
}
+ }
BIO_clear_retry_flags(b);
BIO_copy_next_retry(b);
- return(ret);
- }
+ return (ret);
+}
-static int md_write(BIO *b, const char *in, int inl)
- {
- int ret=0;
+static int
+md_write(BIO *b, const char *in, int inl)
+{
+ int ret = 0;
EVP_MD_CTX *ctx;
- if ((in == NULL) || (inl <= 0)) return(0);
- ctx=b->ptr;
+ if ((in == NULL) || (inl <= 0))
+ return (0);
+ ctx = b->ptr;
if ((ctx != NULL) && (b->next_bio != NULL))
- ret=BIO_write(b->next_bio,in,inl);
- if (b->init)
- {
- if (ret > 0)
- {
- if (!EVP_DigestUpdate(ctx,(const unsigned char *)in,
- (unsigned int)ret))
- {
+ ret = BIO_write(b->next_bio, in, inl);
+ if (b->init) {
+ if (ret > 0) {
+ if (!EVP_DigestUpdate(ctx, (const unsigned char *)in,
+ (unsigned int)ret)) {
BIO_clear_retry_flags(b);
return 0;
- }
}
}
- if(b->next_bio != NULL)
- {
+ }
+ if (b->next_bio != NULL) {
BIO_clear_retry_flags(b);
BIO_copy_next_retry(b);
- }
- return(ret);
}
+ return (ret);
+}
-static long md_ctrl(BIO *b, int cmd, long num, void *ptr)
- {
- EVP_MD_CTX *ctx,*dctx,**pctx;
+static long
+md_ctrl(BIO *b, int cmd, long num, void *ptr)
+{
+ EVP_MD_CTX *ctx, *dctx, **pctx;
const EVP_MD **ppmd;
EVP_MD *md;
- long ret=1;
+ long ret = 1;
BIO *dbio;
- ctx=b->ptr;
+ ctx = b->ptr;
- switch (cmd)
- {
+ switch (cmd) {
case BIO_CTRL_RESET:
if (b->init)
- ret = EVP_DigestInit_ex(ctx,ctx->digest, NULL);
+ ret = EVP_DigestInit_ex(ctx, ctx->digest, NULL);
else
- ret=0;
+ ret = 0;
if (ret > 0)
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
case BIO_C_GET_MD:
- if (b->init)
- {
- ppmd=ptr;
- *ppmd=ctx->digest;
- }
- else
- ret=0;
+ if (b->init) {
+ ppmd = ptr;
+ *ppmd = ctx->digest;
+ } else
+ ret = 0;
break;
case BIO_C_GET_MD_CTX:
- pctx=ptr;
- *pctx=ctx;
+ pctx = ptr;
+ *pctx = ctx;
b->init = 1;
break;
case BIO_C_SET_MD_CTX:
if (b->init)
- b->ptr=ptr;
+ b->ptr = ptr;
else
- ret=0;
+ ret = 0;
break;
case BIO_C_DO_STATE_MACHINE:
BIO_clear_retry_flags(b);
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
BIO_copy_next_retry(b);
break;
case BIO_C_SET_MD:
- md=ptr;
- ret = EVP_DigestInit_ex(ctx,md, NULL);
+ md = ptr;
+ ret = EVP_DigestInit_ex(ctx, md, NULL);
if (ret > 0)
- b->init=1;
+ b->init = 1;
break;
case BIO_CTRL_DUP:
- dbio=ptr;
- dctx=dbio->ptr;
- if (!EVP_MD_CTX_copy_ex(dctx,ctx))
+ dbio = ptr;
+ dctx = dbio->ptr;
+ if (!EVP_MD_CTX_copy_ex(dctx, ctx))
return 0;
- b->init=1;
+ b->init = 1;
break;
default:
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
- }
- return(ret);
}
+ return (ret);
+}
-static long md_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
- {
- long ret=1;
+static long
+md_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
+{
+ long ret = 1;
- if (b->next_bio == NULL) return(0);
- switch (cmd)
- {
+ if (b->next_bio == NULL)
+ return (0);
+ switch (cmd) {
default:
- ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
+ ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
break;
- }
- return(ret);
}
+ return (ret);
+}
-static int md_gets(BIO *bp, char *buf, int size)
- {
+static int
+md_gets(BIO *bp, char *buf, int size)
+{
EVP_MD_CTX *ctx;
unsigned int ret;
-
- ctx=bp->ptr;
+ ctx = bp->ptr;
if (size < ctx->digest->md_size)
- return(0);
- if (EVP_DigestFinal_ex(ctx,(unsigned char *)buf,&ret)<=0)
+ return (0);
+ if (EVP_DigestFinal_ex(ctx, (unsigned char *)buf, &ret) <= 0)
return -1;
-
- return((int)ret);
- }
+
+ return ((int)ret);
+}
/*
static int md_puts(bp,str)
return(-1);
}
*/
-
* This package is an SSL implementation written
* by Eric Young (eay@cryptsoft.com).
* The implementation was written so as to conform with Netscapes SSL.
- *
+ *
* This library is free for commercial and non-commercial use as long as
* the following conditions are aheared to. The following conditions
* apply to all code found in this distribution, be it the RC4, RSA,
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
* included with this distribution is covered by the same copyright terms
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
- *
+ *
* Copyright remains Eric Young's, and as such any Copyright notices in
* the code are not to be removed.
* If this package is used in a product, Eric Young should be given attribution
* as the author of the parts of the library used.
* This can be in the form of a textual message at program startup or
* in documentation (online or textual) provided with the package.
- *
+ *
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* Eric Young (eay@cryptsoft.com)"
* The word 'cryptographic' can be left out if the rouines from the library
* being used are not cryptographic related :-).
- * 4. If you include any Windows specific code (or a derivative thereof) from
+ * 4. If you include any Windows specific code (or a derivative thereof) from
* the apps directory (application code) you must include an acknowledgement:
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
- *
+ *
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
- *
+ *
* The licence and distribution terms for any publically available version or
* derivative of this code cannot be changed. i.e. this code cannot simply be
* copied and put under another distribution licence
Why BIO_f_reliable?
I wrote function which took BIO* as argument, read data from it
- and processed it. Then I wanted to store the input file in
+ and processed it. Then I wanted to store the input file in
encrypted form. OK I pushed BIO_f_cipher to the BIO stack
- and everything was OK. BUT if user types wrong password
+ and everything was OK. BUT if user types wrong password
BIO_f_cipher outputs only garbage and my function crashes. Yes
- I can and I should fix my function, but BIO_f_cipher is
+ I can and I should fix my function, but BIO_f_cipher is
easy way to add encryption support to many existing applications
- and it's hard to debug and fix them all.
+ and it's hard to debug and fix them all.
So I wanted another BIO which would catch the incorrect passwords and
- file damages which cause garbage on BIO_f_cipher's output.
+ file damages which cause garbage on BIO_f_cipher's output.
- The easy way is to push the BIO_f_md and save the checksum at
+ The easy way is to push the BIO_f_md and save the checksum at
the end of the file. However there are several problems with this
approach:
- 1) you must somehow separate checksum from actual data.
- 2) you need lot's of memory when reading the file, because you
+ 1) you must somehow separate checksum from actual data.
+ 2) you need lot's of memory when reading the file, because you
must read to the end of the file and verify the checksum before
- letting the application to read the data.
-
- BIO_f_reliable tries to solve both problems, so that you can
+ letting the application to read the data.
+
+ BIO_f_reliable tries to solve both problems, so that you can
read and write arbitrary long streams using only fixed amount
of memory.
BIO_f_reliable splits data stream into blocks. Each block is prefixed
- with it's length and suffixed with it's digest. So you need only
- several Kbytes of memory to buffer single block before verifying
- it's digest.
+ with it's length and suffixed with it's digest. So you need only
+ several Kbytes of memory to buffer single block before verifying
+ it's digest.
BIO_f_reliable goes further and adds several important capabilities:
- 1) the digest of the block is computed over the whole stream
+ 1) the digest of the block is computed over the whole stream
-- so nobody can rearrange the blocks or remove or replace them.
- 2) to detect invalid passwords right at the start BIO_f_reliable
+ 2) to detect invalid passwords right at the start BIO_f_reliable
adds special prefix to the stream. In order to avoid known plain-text
attacks this prefix is generated as follows:
- *) digest is initialized with random seed instead of
+ *) digest is initialized with random seed instead of
standardized one.
*) same seed is written to output
- *) well-known text is then hashed and the output
+ *) well-known text is then hashed and the output
of the digest is also written to output.
reader can now read the seed from stream, hash the same string
and then compare the digest output.
- Bad things: BIO_f_reliable knows what's going on in EVP_Digest. I
+ Bad things: BIO_f_reliable knows what's going on in EVP_Digest. I
initially wrote and tested this code on x86 machine and wrote the
digests out in machine-dependent order :( There are people using
this code and I cannot change this easily without making existing
#define IOBS (OK_BLOCK_SIZE+ OK_BLOCK_BLOCK+ 3*EVP_MAX_MD_SIZE)
#define WELLKNOWN "The quick brown fox jumped over the lazy dog's back."
-typedef struct ok_struct
- {
+typedef struct ok_struct {
size_t buf_len;
size_t buf_off;
size_t buf_len_save;
int cont; /* <= 0 when finished */
int finished;
EVP_MD_CTX md;
- int blockout; /* output block is ready */
+ int blockout; /* output block is ready */
int sigio; /* must process signature */
unsigned char buf[IOBS];
- } BIO_OK_CTX;
+} BIO_OK_CTX;
static BIO_METHOD methods_ok = {
.type = BIO_TYPE_CIPHER,
.callback_ctrl = ok_callback_ctrl
};
-BIO_METHOD *BIO_f_reliable(void)
- {
- return(&methods_ok);
- }
+BIO_METHOD *
+BIO_f_reliable(void)
+{
+ return (&methods_ok);
+}
-static int ok_new(BIO *bi)
- {
+static int
+ok_new(BIO *bi)
+{
BIO_OK_CTX *ctx;
- ctx=(BIO_OK_CTX *)malloc(sizeof(BIO_OK_CTX));
- if (ctx == NULL) return(0);
+ ctx = (BIO_OK_CTX *)malloc(sizeof(BIO_OK_CTX));
+ if (ctx == NULL)
+ return (0);
- ctx->buf_len=0;
- ctx->buf_off=0;
- ctx->buf_len_save=0;
- ctx->buf_off_save=0;
- ctx->cont=1;
- ctx->finished=0;
- ctx->blockout= 0;
- ctx->sigio=1;
+ ctx->buf_len = 0;
+ ctx->buf_off = 0;
+ ctx->buf_len_save = 0;
+ ctx->buf_off_save = 0;
+ ctx->cont = 1;
+ ctx->finished = 0;
+ ctx->blockout = 0;
+ ctx->sigio = 1;
EVP_MD_CTX_init(&ctx->md);
- bi->init=0;
- bi->ptr=(char *)ctx;
- bi->flags=0;
- return(1);
- }
+ bi->init = 0;
+ bi->ptr = (char *)ctx;
+ bi->flags = 0;
+ return (1);
+}
-static int ok_free(BIO *a)
- {
- if (a == NULL) return(0);
+static int
+ok_free(BIO *a)
+{
+ if (a == NULL)
+ return (0);
EVP_MD_CTX_cleanup(&((BIO_OK_CTX *)a->ptr)->md);
- OPENSSL_cleanse(a->ptr,sizeof(BIO_OK_CTX));
+ OPENSSL_cleanse(a->ptr, sizeof(BIO_OK_CTX));
free(a->ptr);
- a->ptr=NULL;
- a->init=0;
- a->flags=0;
- return(1);
- }
-
-static int ok_read(BIO *b, char *out, int outl)
- {
- int ret=0,i,n;
+ a->ptr = NULL;
+ a->init = 0;
+ a->flags = 0;
+ return (1);
+}
+
+static int
+ok_read(BIO *b, char *out, int outl)
+{
+ int ret = 0, i, n;
BIO_OK_CTX *ctx;
- if (out == NULL) return(0);
- ctx=(BIO_OK_CTX *)b->ptr;
+ if (out == NULL)
+ return (0);
+ ctx = (BIO_OK_CTX *)b->ptr;
- if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0)) return(0);
+ if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0))
+ return (0);
- while(outl > 0)
- {
+ while (outl > 0) {
/* copy clean bytes to output buffer */
- if (ctx->blockout)
- {
- i=ctx->buf_len-ctx->buf_off;
- if (i > outl) i=outl;
- memcpy(out,&(ctx->buf[ctx->buf_off]),i);
- ret+=i;
- out+=i;
- outl-=i;
- ctx->buf_off+=i;
+ if (ctx->blockout) {
+ i = ctx->buf_len - ctx->buf_off;
+ if (i > outl)
+ i = outl;
+ memcpy(out, &(ctx->buf[ctx->buf_off]), i);
+ ret += i;
+ out += i;
+ outl -= i;
+ ctx->buf_off += i;
/* all clean bytes are out */
- if (ctx->buf_len == ctx->buf_off)
- {
- ctx->buf_off=0;
+ if (ctx->buf_len == ctx->buf_off) {
+ ctx->buf_off = 0;
/* copy start of the next block into proper place */
- if(ctx->buf_len_save- ctx->buf_off_save > 0)
- {
- ctx->buf_len= ctx->buf_len_save- ctx->buf_off_save;
+ if (ctx->buf_len_save - ctx->buf_off_save > 0) {
+ ctx->buf_len = ctx->buf_len_save - ctx->buf_off_save;
memmove(ctx->buf, &(ctx->buf[ctx->buf_off_save]),
- ctx->buf_len);
- }
- else
- {
- ctx->buf_len=0;
- }
- ctx->blockout= 0;
+ ctx->buf_len);
+ } else {
+ ctx->buf_len = 0;
}
+ ctx->blockout = 0;
}
-
+ }
+
/* output buffer full -- cancel */
- if (outl == 0) break;
+ if (outl == 0)
+ break;
/* no clean bytes in buffer -- fill it */
- n=IOBS- ctx->buf_len;
- i=BIO_read(b->next_bio,&(ctx->buf[ctx->buf_len]),n);
+ n = IOBS - ctx->buf_len;
+ i = BIO_read(b->next_bio, &(ctx->buf[ctx->buf_len]), n);
if (i <= 0) break; /* nothing new */
- ctx->buf_len+= i;
+ ctx->buf_len += i;
/* no signature yet -- check if we got one */
- if (ctx->sigio == 1)
- {
- if (!sig_in(b))
- {
+ if (ctx->sigio == 1) {
+ if (!sig_in(b)) {
BIO_clear_retry_flags(b);
return 0;
- }
}
+ }
/* signature ok -- check if we got block */
- if (ctx->sigio == 0)
- {
- if (!block_in(b))
- {
+ if (ctx->sigio == 0) {
+ if (!block_in(b)) {
BIO_clear_retry_flags(b);
return 0;
- }
}
+ }
/* invalid block -- cancel */
- if (ctx->cont <= 0) break;
+ if (ctx->cont <= 0)
+ break;
- }
+ }
BIO_clear_retry_flags(b);
BIO_copy_next_retry(b);
- return(ret);
- }
+ return (ret);
+}
-static int ok_write(BIO *b, const char *in, int inl)
- {
- int ret=0,n,i;
+static int
+ok_write(BIO *b, const char *in, int inl)
+{
+ int ret = 0, n, i;
BIO_OK_CTX *ctx;
- if (inl <= 0) return inl;
+ if (inl <= 0)
+ return inl;
- ctx=(BIO_OK_CTX *)b->ptr;
- ret=inl;
+ ctx = (BIO_OK_CTX *)b->ptr;
+ ret = inl;
- if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0)) return(0);
+ if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0))
+ return (0);
- if(ctx->sigio && !sig_out(b))
+ if (ctx->sigio && !sig_out(b))
return 0;
- do{
+ do {
BIO_clear_retry_flags(b);
- n=ctx->buf_len-ctx->buf_off;
- while (ctx->blockout && n > 0)
- {
- i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
- if (i <= 0)
- {
+ n = ctx->buf_len - ctx->buf_off;
+ while (ctx->blockout && n > 0) {
+ i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n);
+ if (i <= 0) {
BIO_copy_next_retry(b);
- if(!BIO_should_retry(b))
- ctx->cont= 0;
- return(i);
- }
- ctx->buf_off+=i;
- n-=i;
+ if (!BIO_should_retry(b))
+ ctx->cont = 0;
+ return (i);
}
+ ctx->buf_off += i;
+ n -= i;
+ }
/* at this point all pending data has been written */
- ctx->blockout= 0;
- if (ctx->buf_len == ctx->buf_off)
- {
- ctx->buf_len=OK_BLOCK_BLOCK;
- ctx->buf_off=0;
- }
-
- if ((in == NULL) || (inl <= 0)) return(0);
+ ctx->blockout = 0;
+ if (ctx->buf_len == ctx->buf_off) {
+ ctx->buf_len = OK_BLOCK_BLOCK;
+ ctx->buf_off = 0;
+ }
- n= (inl+ ctx->buf_len > OK_BLOCK_SIZE+ OK_BLOCK_BLOCK) ?
- (int)(OK_BLOCK_SIZE+OK_BLOCK_BLOCK-ctx->buf_len) : inl;
+ if ((in == NULL) || (inl <= 0))
+ return (0);
- memcpy((unsigned char *)(&(ctx->buf[ctx->buf_len])),(unsigned char *)in,n);
- ctx->buf_len+= n;
- inl-=n;
- in+=n;
+ n = (inl + ctx->buf_len > OK_BLOCK_SIZE + OK_BLOCK_BLOCK) ?
+ (int)(OK_BLOCK_SIZE + OK_BLOCK_BLOCK - ctx->buf_len) : inl;
- if(ctx->buf_len >= OK_BLOCK_SIZE+ OK_BLOCK_BLOCK)
- {
- if (!block_out(b))
- {
+ memcpy((unsigned char *)(&(ctx->buf[ctx->buf_len])),
+ (unsigned char *)in, n);
+ ctx->buf_len += n;
+ inl -= n;
+ in += n;
+
+ if (ctx->buf_len >= OK_BLOCK_SIZE + OK_BLOCK_BLOCK) {
+ if (!block_out(b)) {
BIO_clear_retry_flags(b);
return 0;
- }
}
- }while(inl > 0);
+ }
+ } while (inl > 0);
BIO_clear_retry_flags(b);
BIO_copy_next_retry(b);
- return(ret);
- }
+ return (ret);
+}
-static long ok_ctrl(BIO *b, int cmd, long num, void *ptr)
- {
+static long
+ok_ctrl(BIO *b, int cmd, long num, void *ptr)
+{
BIO_OK_CTX *ctx;
EVP_MD *md;
const EVP_MD **ppmd;
- long ret=1;
+ long ret = 1;
int i;
- ctx=b->ptr;
+ ctx = b->ptr;
- switch (cmd)
- {
+ switch (cmd) {
case BIO_CTRL_RESET:
- ctx->buf_len=0;
- ctx->buf_off=0;
- ctx->buf_len_save=0;
- ctx->buf_off_save=0;
- ctx->cont=1;
- ctx->finished=0;
- ctx->blockout= 0;
- ctx->sigio=1;
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ctx->buf_len = 0;
+ ctx->buf_off = 0;
+ ctx->buf_len_save = 0;
+ ctx->buf_off_save = 0;
+ ctx->cont = 1;
+ ctx->finished = 0;
+ ctx->blockout = 0;
+ ctx->sigio = 1;
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
case BIO_CTRL_EOF: /* More to read */
if (ctx->cont <= 0)
- ret=1;
+ ret = 1;
else
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
case BIO_CTRL_PENDING: /* More to read in buffer */
case BIO_CTRL_WPENDING: /* More to read in buffer */
- ret=ctx->blockout ? ctx->buf_len-ctx->buf_off : 0;
+ ret = ctx->blockout ? ctx->buf_len - ctx->buf_off : 0;
if (ret <= 0)
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
case BIO_CTRL_FLUSH:
/* do a final write */
- if(ctx->blockout == 0)
+ if (ctx->blockout == 0)
if (!block_out(b))
return 0;
- while (ctx->blockout)
- {
- i=ok_write(b,NULL,0);
- if (i < 0)
- {
- ret=i;
+ while (ctx->blockout) {
+ i = ok_write(b, NULL, 0);
+ if (i < 0) {
+ ret = i;
break;
- }
}
+ }
+
+ ctx->finished = 1;
+ ctx->buf_off = ctx->buf_len = 0;
+ ctx->cont = (int)ret;
- ctx->finished=1;
- ctx->buf_off=ctx->buf_len=0;
- ctx->cont=(int)ret;
-
/* Finally flush the underlying BIO */
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
case BIO_C_DO_STATE_MACHINE:
BIO_clear_retry_flags(b);
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
BIO_copy_next_retry(b);
break;
case BIO_CTRL_INFO:
- ret=(long)ctx->cont;
+ ret = (long)ctx->cont;
break;
case BIO_C_SET_MD:
- md=ptr;
+ md = ptr;
if (!EVP_DigestInit_ex(&ctx->md, md, NULL))
return 0;
- b->init=1;
+ b->init = 1;
break;
case BIO_C_GET_MD:
- if (b->init)
- {
- ppmd=ptr;
- *ppmd=ctx->md.digest;
- }
- else
- ret=0;
+ if (b->init) {
+ ppmd = ptr;
+ *ppmd = ctx->md.digest;
+ } else
+ ret = 0;
break;
default:
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
- }
- return(ret);
}
+ return (ret);
+}
-static long ok_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
- {
- long ret=1;
+static long
+ok_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
+{
+ long ret = 1;
- if (b->next_bio == NULL) return(0);
- switch (cmd)
- {
+ if (b->next_bio == NULL)
+ return (0);
+ switch (cmd) {
default:
- ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
+ ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
break;
- }
- return(ret);
}
+ return (ret);
+}
-static void longswap(void *_ptr, size_t len)
+static void
+longswap(void *_ptr, size_t len)
{
if (_BYTE_ORDER == _LITTLE_ENDIAN) {
size_t i;
- unsigned char *p=_ptr,c;
+ unsigned char *p = _ptr, c;
- for(i= 0;i < len;i+= 4) {
- c=p[0],p[0]=p[3],p[3]=c;
- c=p[1],p[1]=p[2],p[2]=c;
+ for (i = 0; i < len; i += 4) {
+ c = p[0], p[0] = p[3], p[3] = c;
+ c = p[1], p[1] = p[2], p[2] = c;
}
}
}
-static int sig_out(BIO* b)
- {
+static int
+sig_out(BIO* b)
+{
BIO_OK_CTX *ctx;
EVP_MD_CTX *md;
- ctx=b->ptr;
- md=&ctx->md;
+ ctx = b->ptr;
+ md = &ctx->md;
- if(ctx->buf_len+ 2* md->digest->md_size > OK_BLOCK_SIZE) return 1;
+ if (ctx->buf_len + 2* md->digest->md_size > OK_BLOCK_SIZE)
+ return 1;
if (!EVP_DigestInit_ex(md, md->digest, NULL))
goto berr;
RAND_pseudo_bytes(md->md_data, md->digest->md_size);
memcpy(&(ctx->buf[ctx->buf_len]), md->md_data, md->digest->md_size);
longswap(&(ctx->buf[ctx->buf_len]), md->digest->md_size);
- ctx->buf_len+= md->digest->md_size;
+ ctx->buf_len += md->digest->md_size;
if (!EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN)))
goto berr;
if (!EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL))
goto berr;
- ctx->buf_len+= md->digest->md_size;
- ctx->blockout= 1;
- ctx->sigio= 0;
+ ctx->buf_len += md->digest->md_size;
+ ctx->blockout = 1;
+ ctx->sigio = 0;
return 1;
- berr:
+berr:
BIO_clear_retry_flags(b);
return 0;
- }
+}
-static int sig_in(BIO* b)
- {
+static int
+sig_in(BIO* b)
+{
BIO_OK_CTX *ctx;
EVP_MD_CTX *md;
unsigned char tmp[EVP_MAX_MD_SIZE];
- int ret= 0;
+ int ret = 0;
- ctx=b->ptr;
- md=&ctx->md;
+ ctx = b->ptr;
+ md = &ctx->md;
- if((int)(ctx->buf_len-ctx->buf_off) < 2*md->digest->md_size) return 1;
+ if ((int)(ctx->buf_len - ctx->buf_off) < 2*md->digest->md_size)
+ return 1;
if (!EVP_DigestInit_ex(md, md->digest, NULL))
goto berr;
memcpy(md->md_data, &(ctx->buf[ctx->buf_off]), md->digest->md_size);
longswap(md->md_data, md->digest->md_size);
- ctx->buf_off+= md->digest->md_size;
+ ctx->buf_off += md->digest->md_size;
if (!EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN)))
goto berr;
if (!EVP_DigestFinal_ex(md, tmp, NULL))
goto berr;
- ret= memcmp(&(ctx->buf[ctx->buf_off]), tmp, md->digest->md_size) == 0;
- ctx->buf_off+= md->digest->md_size;
- if(ret == 1)
- {
- ctx->sigio= 0;
- if(ctx->buf_len != ctx->buf_off)
- {
- memmove(ctx->buf, &(ctx->buf[ctx->buf_off]), ctx->buf_len- ctx->buf_off);
- }
- ctx->buf_len-= ctx->buf_off;
- ctx->buf_off= 0;
- }
- else
- {
- ctx->cont= 0;
+ ret = memcmp(&(ctx->buf[ctx->buf_off]), tmp, md->digest->md_size) == 0;
+ ctx->buf_off += md->digest->md_size;
+ if (ret == 1) {
+ ctx->sigio = 0;
+ if (ctx->buf_len != ctx->buf_off) {
+ memmove(ctx->buf, &(ctx->buf[ctx->buf_off]),
+ ctx->buf_len - ctx->buf_off);
}
+ ctx->buf_len -= ctx->buf_off;
+ ctx->buf_off = 0;
+ } else {
+ ctx->cont = 0;
+ }
return 1;
- berr:
+berr:
BIO_clear_retry_flags(b);
return 0;
- }
+}
-static int block_out(BIO* b)
- {
+static int
+block_out(BIO* b)
+{
BIO_OK_CTX *ctx;
EVP_MD_CTX *md;
unsigned long tl;
- ctx=b->ptr;
- md=&ctx->md;
+ ctx = b->ptr;
+ md = &ctx->md;
- tl= ctx->buf_len- OK_BLOCK_BLOCK;
- ctx->buf[0]=(unsigned char)(tl>>24);
- ctx->buf[1]=(unsigned char)(tl>>16);
- ctx->buf[2]=(unsigned char)(tl>>8);
- ctx->buf[3]=(unsigned char)(tl);
+ tl = ctx->buf_len - OK_BLOCK_BLOCK;
+ ctx->buf[0] = (unsigned char)(tl >> 24);
+ ctx->buf[1] = (unsigned char)(tl >> 16);
+ ctx->buf[2] = (unsigned char)(tl >> 8);
+ ctx->buf[3] = (unsigned char)(tl);
if (!EVP_DigestUpdate(md,
- (unsigned char*) &(ctx->buf[OK_BLOCK_BLOCK]), tl))
+ (unsigned char*) &(ctx->buf[OK_BLOCK_BLOCK]), tl))
goto berr;
if (!EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL))
goto berr;
- ctx->buf_len+= md->digest->md_size;
- ctx->blockout= 1;
+ ctx->buf_len += md->digest->md_size;
+ ctx->blockout = 1;
return 1;
- berr:
+berr:
BIO_clear_retry_flags(b);
return 0;
- }
+}
-static int block_in(BIO* b)
- {
+static int
+block_in(BIO* b)
+{
BIO_OK_CTX *ctx;
EVP_MD_CTX *md;
- unsigned long tl= 0;
+ unsigned long tl = 0;
unsigned char tmp[EVP_MAX_MD_SIZE];
- ctx=b->ptr;
- md=&ctx->md;
+ ctx = b->ptr;
+ md = &ctx->md;
- assert(sizeof(tl)>=OK_BLOCK_BLOCK); /* always true */
- tl =ctx->buf[0]; tl<<=8;
- tl|=ctx->buf[1]; tl<<=8;
- tl|=ctx->buf[2]; tl<<=8;
- tl|=ctx->buf[3];
+ assert(sizeof(tl) >= OK_BLOCK_BLOCK); /* always true */
+ tl = ctx->buf[0];
+ tl <<= 8;
+ tl |= ctx->buf[1];
+ tl <<= 8;
+ tl |= ctx->buf[2];
+ tl <<= 8;
+ tl |= ctx->buf[3];
+
+ if (ctx->buf_len < tl + OK_BLOCK_BLOCK + md->digest->md_size)
+ return 1;
- if (ctx->buf_len < tl+ OK_BLOCK_BLOCK+ md->digest->md_size) return 1;
-
if (!EVP_DigestUpdate(md,
- (unsigned char*) &(ctx->buf[OK_BLOCK_BLOCK]), tl))
+ (unsigned char*) &(ctx->buf[OK_BLOCK_BLOCK]), tl))
goto berr;
if (!EVP_DigestFinal_ex(md, tmp, NULL))
goto berr;
- if(memcmp(&(ctx->buf[tl+ OK_BLOCK_BLOCK]), tmp, md->digest->md_size) == 0)
- {
+ if (memcmp(&(ctx->buf[tl + OK_BLOCK_BLOCK]), tmp,
+ md->digest->md_size) == 0) {
/* there might be parts from next block lurking around ! */
- ctx->buf_off_save= tl+ OK_BLOCK_BLOCK+ md->digest->md_size;
- ctx->buf_len_save= ctx->buf_len;
- ctx->buf_off= OK_BLOCK_BLOCK;
- ctx->buf_len= tl+ OK_BLOCK_BLOCK;
- ctx->blockout= 1;
- }
- else
- {
- ctx->cont= 0;
- }
+ ctx->buf_off_save = tl + OK_BLOCK_BLOCK + md->digest->md_size;
+ ctx->buf_len_save = ctx->buf_len;
+ ctx->buf_off = OK_BLOCK_BLOCK;
+ ctx->buf_len = tl + OK_BLOCK_BLOCK;
+ ctx->blockout = 1;
+ } else {
+ ctx->cont = 0;
+ }
return 1;
- berr:
+
+berr:
BIO_clear_retry_flags(b);
return 0;
- }
-
+}
* This package is an SSL implementation written
* by Eric Young (eay@cryptsoft.com).
* The implementation was written so as to conform with Netscapes SSL.
- *
+ *
* This library is free for commercial and non-commercial use as long as
* the following conditions are aheared to. The following conditions
* apply to all code found in this distribution, be it the RC4, RSA,
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
* included with this distribution is covered by the same copyright terms
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
- *
+ *
* Copyright remains Eric Young's, and as such any Copyright notices in
* the code are not to be removed.
* If this package is used in a product, Eric Young should be given attribution
* as the author of the parts of the library used.
* This can be in the form of a textual message at program startup or
* in documentation (online or textual) provided with the package.
- *
+ *
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* Eric Young (eay@cryptsoft.com)"
* The word 'cryptographic' can be left out if the rouines from the library
* being used are not cryptographic related :-).
- * 4. If you include any Windows specific code (or a derivative thereof) from
+ * 4. If you include any Windows specific code (or a derivative thereof) from
* the apps directory (application code) you must include an acknowledgement:
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
- *
+ *
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
- *
+ *
* The licence and distribution terms for any publically available version or
* derivative of this code cannot be changed. i.e. this code cannot simply be
* copied and put under another distribution licence
static long b64_ctrl(BIO *h, int cmd, long arg1, void *arg2);
static int b64_new(BIO *h);
static int b64_free(BIO *data);
-static long b64_callback_ctrl(BIO *h,int cmd,bio_info_cb *fp);
+static long b64_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
#define B64_BLOCK_SIZE 1024
#define B64_BLOCK_SIZE2 768
#define B64_NONE 0
#define B64_ENCODE 1
#define B64_DECODE 2
-typedef struct b64_struct
- {
+typedef struct b64_struct {
/*BIO *bio; moved to the BIO structure */
int buf_len;
int buf_off;
int start; /* have we started decoding yet? */
int cont; /* <= 0 when finished */
EVP_ENCODE_CTX base64;
- char buf[EVP_ENCODE_LENGTH(B64_BLOCK_SIZE)+10];
+ char buf[EVP_ENCODE_LENGTH(B64_BLOCK_SIZE) + 10];
char tmp[B64_BLOCK_SIZE];
- } BIO_B64_CTX;
+} BIO_B64_CTX;
-static BIO_METHOD methods_b64= {
+static BIO_METHOD methods_b64 = {
.type = BIO_TYPE_BASE64,
.name = "base64 encoding",
.bwrite = b64_write,
.callback_ctrl = b64_callback_ctrl
};
-BIO_METHOD *BIO_f_base64(void)
- {
- return(&methods_b64);
- }
+BIO_METHOD *
+BIO_f_base64(void)
+{
+ return (&methods_b64);
+}
-static int b64_new(BIO *bi)
- {
+static int
+b64_new(BIO *bi)
+{
BIO_B64_CTX *ctx;
- ctx=(BIO_B64_CTX *)malloc(sizeof(BIO_B64_CTX));
- if (ctx == NULL) return(0);
-
- ctx->buf_len=0;
- ctx->tmp_len=0;
- ctx->tmp_nl=0;
- ctx->buf_off=0;
- ctx->cont=1;
- ctx->start=1;
- ctx->encode=0;
-
- bi->init=1;
- bi->ptr=(char *)ctx;
- bi->flags=0;
+ ctx = (BIO_B64_CTX *)malloc(sizeof(BIO_B64_CTX));
+ if (ctx == NULL)
+ return (0);
+
+ ctx->buf_len = 0;
+ ctx->tmp_len = 0;
+ ctx->tmp_nl = 0;
+ ctx->buf_off = 0;
+ ctx->cont = 1;
+ ctx->start = 1;
+ ctx->encode = 0;
+
+ bi->init = 1;
+ bi->ptr = (char *)ctx;
+ bi->flags = 0;
bi->num = 0;
- return(1);
- }
-
-static int b64_free(BIO *a)
- {
- if (a == NULL) return(0);
+ return (1);
+}
+
+static int
+b64_free(BIO *a)
+{
+ if (a == NULL)
+ return (0);
free(a->ptr);
- a->ptr=NULL;
- a->init=0;
- a->flags=0;
- return(1);
- }
-
-static int b64_read(BIO *b, char *out, int outl)
- {
- int ret=0,i,ii,j,k,x,n,num,ret_code=0;
+ a->ptr = NULL;
+ a->init = 0;
+ a->flags = 0;
+ return (1);
+}
+
+static int
+b64_read(BIO *b, char *out, int outl)
+{
+ int ret = 0, i, ii, j,k, x,n, num, ret_code = 0;
BIO_B64_CTX *ctx;
- unsigned char *p,*q;
+ unsigned char *p, *q;
- if (out == NULL) return(0);
- ctx=(BIO_B64_CTX *)b->ptr;
+ if (out == NULL)
+ return (0);
+ ctx = (BIO_B64_CTX *)b->ptr;
- if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
+ if ((ctx == NULL) || (b->next_bio == NULL))
+ return (0);
BIO_clear_retry_flags(b);
- if (ctx->encode != B64_DECODE)
- {
- ctx->encode=B64_DECODE;
- ctx->buf_len=0;
- ctx->buf_off=0;
- ctx->tmp_len=0;
+ if (ctx->encode != B64_DECODE) {
+ ctx->encode = B64_DECODE;
+ ctx->buf_len = 0;
+ ctx->buf_off = 0;
+ ctx->tmp_len = 0;
EVP_DecodeInit(&(ctx->base64));
- }
+ }
/* First check if there are bytes decoded/encoded */
- if (ctx->buf_len > 0)
- {
+ if (ctx->buf_len > 0) {
OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
- i=ctx->buf_len-ctx->buf_off;
- if (i > outl) i=outl;
- OPENSSL_assert(ctx->buf_off+i < (int)sizeof(ctx->buf));
- memcpy(out,&(ctx->buf[ctx->buf_off]),i);
- ret=i;
- out+=i;
- outl-=i;
- ctx->buf_off+=i;
- if (ctx->buf_len == ctx->buf_off)
- {
- ctx->buf_len=0;
- ctx->buf_off=0;
- }
+ i = ctx->buf_len - ctx->buf_off;
+ if (i > outl)
+ i = outl;
+ OPENSSL_assert(ctx->buf_off + i < (int)sizeof(ctx->buf));
+ memcpy(out, &(ctx->buf[ctx->buf_off]), i);
+ ret = i;
+ out += i;
+ outl -= i;
+ ctx->buf_off += i;
+ if (ctx->buf_len == ctx->buf_off) {
+ ctx->buf_len = 0;
+ ctx->buf_off = 0;
}
+ }
/* At this point, we have room of outl bytes and an empty
* buffer, so we should read in some more. */
- ret_code=0;
- while (outl > 0)
- {
+ ret_code = 0;
+ while (outl > 0) {
if (ctx->cont <= 0)
break;
- i=BIO_read(b->next_bio,&(ctx->tmp[ctx->tmp_len]),
- B64_BLOCK_SIZE-ctx->tmp_len);
+ i = BIO_read(b->next_bio, &(ctx->tmp[ctx->tmp_len]),
+ B64_BLOCK_SIZE - ctx->tmp_len);
- if (i <= 0)
- {
- ret_code=i;
+ if (i <= 0) {
+ ret_code = i;
/* Should we continue next time we are called? */
- if (!BIO_should_retry(b->next_bio))
- {
- ctx->cont=i;
+ if (!BIO_should_retry(b->next_bio)) {
+ ctx->cont = i;
/* If buffer empty break */
- if(ctx->tmp_len == 0)
+ if (ctx->tmp_len == 0)
break;
/* Fall through and process what we have */
else
i = 0;
- }
+ }
/* else we retry and add more data to buffer */
else
break;
- }
- i+=ctx->tmp_len;
+ }
+ i += ctx->tmp_len;
ctx->tmp_len = i;
/* We need to scan, a line at a time until we
* have a valid line if we are starting. */
- if (ctx->start && (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL))
- {
+ if (ctx->start && (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)) {
/* ctx->start=1; */
- ctx->tmp_len=0;
- }
- else if (ctx->start)
- {
- q=p=(unsigned char *)ctx->tmp;
- for (j=0; j<i; j++)
- {
- if (*(q++) != '\n') continue;
+ ctx->tmp_len = 0;
+ } else if (ctx->start) {
+ q = p=(unsigned char *)ctx->tmp;
+ for (j = 0; j < i; j++) {
+ if (*(q++) != '\n')
+ continue;
/* due to a previous very long line,
* we need to keep on scanning for a '\n'
* before we even start looking for
* base64 encoded stuff. */
- if (ctx->tmp_nl)
- {
- p=q;
- ctx->tmp_nl=0;
+ if (ctx->tmp_nl) {
+ p = q;
+ ctx->tmp_nl = 0;
continue;
- }
+ }
- k=EVP_DecodeUpdate(&(ctx->base64),
- (unsigned char *)ctx->buf,
- &num,p,q-p);
+ k = EVP_DecodeUpdate(&(ctx->base64),
+ (unsigned char *)ctx->buf,
+ &num, p, q - p);
if ((k <= 0) && (num == 0) && (ctx->start))
EVP_DecodeInit(&ctx->base64);
- else
- {
+ else {
if (p != (unsigned char *)
- &(ctx->tmp[0]))
- {
- i-=(p- (unsigned char *)
- &(ctx->tmp[0]));
- for (x=0; x < i; x++)
- ctx->tmp[x]=p[x];
- }
+ &(ctx->tmp[0])) {
+ i -= (p - (unsigned char *)
+ &(ctx->tmp[0]));
+ for (x = 0; x < i; x++)
+ ctx->tmp[x] = p[x];
+ }
EVP_DecodeInit(&ctx->base64);
- ctx->start=0;
+ ctx->start = 0;
break;
- }
- p=q;
}
+ p = q;
+ }
/* we fell off the end without starting */
- if ((j == i) && (num == 0))
- {
+ if ((j == i) && (num == 0)) {
/* Is this is one long chunk?, if so, keep on
* reading until a new line. */
- if (p == (unsigned char *)&(ctx->tmp[0]))
- {
+ if (p == (unsigned char *)&(ctx->tmp[0])) {
/* Check buffer full */
- if (i == B64_BLOCK_SIZE)
- {
- ctx->tmp_nl=1;
- ctx->tmp_len=0;
- }
+ if (i == B64_BLOCK_SIZE) {
+ ctx->tmp_nl = 1;
+ ctx->tmp_len = 0;
}
+ }
else if (p != q) /* finished on a '\n' */
- {
- n=q-p;
- for (ii=0; ii<n; ii++)
- ctx->tmp[ii]=p[ii];
- ctx->tmp_len=n;
- }
+ {
+ n = q - p;
+ for (ii = 0; ii < n; ii++)
+ ctx->tmp[ii] = p[ii];
+ ctx->tmp_len = n;
+ }
/* else finished on a '\n' */
continue;
- }
- else
- {
- ctx->tmp_len=0;
+ } else {
+ ctx->tmp_len = 0;
}
- }
- else if ((i < B64_BLOCK_SIZE) && (ctx->cont > 0))
- {
+ } else if ((i < B64_BLOCK_SIZE) && (ctx->cont > 0)) {
/* If buffer isn't full and we can retry then
* restart to read in more data.
*/
continue;
}
- if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)
- {
- int z,jj;
+ if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
+ int z, jj;
#if 0
- jj=(i>>2)<<2;
+ jj = (i >> 2) << 2;
#else
jj = i & ~3; /* process per 4 */
#endif
- z=EVP_DecodeBlock((unsigned char *)ctx->buf,
- (unsigned char *)ctx->tmp,jj);
- if (jj > 2)
- {
- if (ctx->tmp[jj-1] == '=')
- {
+ z = EVP_DecodeBlock((unsigned char *)ctx->buf,
+ (unsigned char *)ctx->tmp, jj);
+ if (jj > 2) {
+ if (ctx->tmp[jj-1] == '=') {
z--;
if (ctx->tmp[jj-2] == '=')
z--;
- }
}
+ }
/* z is now number of output bytes and jj is the
* number consumed */
- if (jj != i)
- {
- memmove(ctx->tmp, &ctx->tmp[jj], i-jj);
- ctx->tmp_len=i-jj;
- }
- ctx->buf_len=0;
- if (z > 0)
- {
- ctx->buf_len=z;
- }
- i=z;
+ if (jj != i) {
+ memmove(ctx->tmp, &ctx->tmp[jj], i - jj);
+ ctx->tmp_len = i - jj;
}
- else
- {
- i=EVP_DecodeUpdate(&(ctx->base64),
- (unsigned char *)ctx->buf,&ctx->buf_len,
- (unsigned char *)ctx->tmp,i);
- ctx->tmp_len = 0;
+ ctx->buf_len = 0;
+ if (z > 0) {
+ ctx->buf_len = z;
}
- ctx->buf_off=0;
- if (i < 0)
- {
- ret_code=0;
- ctx->buf_len=0;
+ i = z;
+ } else {
+ i = EVP_DecodeUpdate(&(ctx->base64),
+ (unsigned char *)ctx->buf, &ctx->buf_len,
+ (unsigned char *)ctx->tmp, i);
+ ctx->tmp_len = 0;
+ }
+ ctx->buf_off = 0;
+ if (i < 0) {
+ ret_code = 0;
+ ctx->buf_len = 0;
break;
- }
+ }
if (ctx->buf_len <= outl)
- i=ctx->buf_len;
+ i = ctx->buf_len;
else
- i=outl;
-
- memcpy(out,ctx->buf,i);
- ret+=i;
- ctx->buf_off=i;
- if (ctx->buf_off == ctx->buf_len)
- {
- ctx->buf_len=0;
- ctx->buf_off=0;
- }
- outl-=i;
- out+=i;
+ i = outl;
+
+ memcpy(out, ctx->buf, i);
+ ret += i;
+ ctx->buf_off = i;
+ if (ctx->buf_off == ctx->buf_len) {
+ ctx->buf_len = 0;
+ ctx->buf_off = 0;
}
+ outl -= i;
+ out += i;
+ }
/* BIO_clear_retry_flags(b); */
BIO_copy_next_retry(b);
- return((ret == 0)?ret_code:ret);
- }
+ return ((ret == 0) ? ret_code : ret);
+}
-static int b64_write(BIO *b, const char *in, int inl)
- {
- int ret=0;
+static int
+b64_write(BIO *b, const char *in, int inl)
+{
+ int ret = 0;
int n;
int i;
BIO_B64_CTX *ctx;
- ctx=(BIO_B64_CTX *)b->ptr;
+ ctx = (BIO_B64_CTX *)b->ptr;
BIO_clear_retry_flags(b);
- if (ctx->encode != B64_ENCODE)
- {
- ctx->encode=B64_ENCODE;
- ctx->buf_len=0;
- ctx->buf_off=0;
- ctx->tmp_len=0;
+ if (ctx->encode != B64_ENCODE) {
+ ctx->encode = B64_ENCODE;
+ ctx->buf_len = 0;
+ ctx->buf_off = 0;
+ ctx->tmp_len = 0;
EVP_EncodeInit(&(ctx->base64));
- }
+ }
OPENSSL_assert(ctx->buf_off < (int)sizeof(ctx->buf));
OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
- n=ctx->buf_len-ctx->buf_off;
- while (n > 0)
- {
- i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
- if (i <= 0)
- {
+ n = ctx->buf_len - ctx->buf_off;
+ while (n > 0) {
+ i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n);
+ if (i <= 0) {
BIO_copy_next_retry(b);
- return(i);
- }
+ return (i);
+ }
OPENSSL_assert(i <= n);
- ctx->buf_off+=i;
+ ctx->buf_off += i;
OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
- n-=i;
- }
+ n -= i;
+ }
/* at this point all pending data has been written */
- ctx->buf_off=0;
- ctx->buf_len=0;
+ ctx->buf_off = 0;
+ ctx->buf_len = 0;
- if ((in == NULL) || (inl <= 0)) return(0);
+ if ((in == NULL) || (inl <= 0))
+ return (0);
- while (inl > 0)
- {
- n=(inl > B64_BLOCK_SIZE)?B64_BLOCK_SIZE:inl;
+ while (inl > 0) {
+ n = (inl > B64_BLOCK_SIZE) ? B64_BLOCK_SIZE : inl;
- if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)
- {
- if (ctx->tmp_len > 0)
- {
+ if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
+ if (ctx->tmp_len > 0) {
OPENSSL_assert(ctx->tmp_len <= 3);
- n=3-ctx->tmp_len;
+ n = 3 - ctx->tmp_len;
/* There's a theoretical possibility for this */
- if (n > inl)
- n=inl;
- memcpy(&(ctx->tmp[ctx->tmp_len]),in,n);
- ctx->tmp_len+=n;
+ if (n > inl)
+ n = inl;
+ memcpy(&(ctx->tmp[ctx->tmp_len]), in, n);
+ ctx->tmp_len += n;
ret += n;
if (ctx->tmp_len < 3)
break;
- ctx->buf_len=EVP_EncodeBlock((unsigned char *)ctx->buf,(unsigned char *)ctx->tmp,ctx->tmp_len);
- OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
+ ctx->buf_len = EVP_EncodeBlock(
+ (unsigned char *)ctx->buf,
+ (unsigned char *)ctx->tmp, ctx->tmp_len);
+ OPENSSL_assert(ctx->buf_len <=
+ (int)sizeof(ctx->buf));
OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
/* Since we're now done using the temporary
buffer, the length should be 0'd */
- ctx->tmp_len=0;
- }
- else
- {
- if (n < 3)
- {
- memcpy(ctx->tmp,in,n);
- ctx->tmp_len=n;
+ ctx->tmp_len = 0;
+ } else {
+ if (n < 3) {
+ memcpy(ctx->tmp, in, n);
+ ctx->tmp_len = n;
ret += n;
break;
- }
- n-=n%3;
- ctx->buf_len=EVP_EncodeBlock((unsigned char *)ctx->buf,(const unsigned char *)in,n);
- OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
+ }
+ n -= n % 3;
+ ctx->buf_len = EVP_EncodeBlock(
+ (unsigned char *)ctx->buf,
+ (const unsigned char *)in, n);
+ OPENSSL_assert(ctx->buf_len <=
+ (int)sizeof(ctx->buf));
OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
ret += n;
- }
}
- else
- {
+ } else {
EVP_EncodeUpdate(&(ctx->base64),
- (unsigned char *)ctx->buf,&ctx->buf_len,
- (unsigned char *)in,n);
+ (unsigned char *)ctx->buf, &ctx->buf_len,
+ (unsigned char *)in, n);
OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
ret += n;
- }
- inl-=n;
- in+=n;
-
- ctx->buf_off=0;
- n=ctx->buf_len;
- while (n > 0)
- {
- i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
- if (i <= 0)
- {
+ }
+ inl -= n;
+ in += n;
+
+ ctx->buf_off = 0;
+ n = ctx->buf_len;
+ while (n > 0) {
+ i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n);
+ if (i <= 0) {
BIO_copy_next_retry(b);
- return((ret == 0)?i:ret);
- }
+ return ((ret == 0) ? i : ret);
+ }
OPENSSL_assert(i <= n);
- n-=i;
- ctx->buf_off+=i;
+ n -= i;
+ ctx->buf_off += i;
OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
- }
- ctx->buf_len=0;
- ctx->buf_off=0;
}
- return(ret);
+ ctx->buf_len = 0;
+ ctx->buf_off = 0;
}
+ return (ret);
+}
-static long b64_ctrl(BIO *b, int cmd, long num, void *ptr)
- {
+static long
+b64_ctrl(BIO *b, int cmd, long num, void *ptr)
+{
BIO_B64_CTX *ctx;
- long ret=1;
+ long ret = 1;
int i;
- ctx=(BIO_B64_CTX *)b->ptr;
+ ctx = (BIO_B64_CTX *)b->ptr;
- switch (cmd)
- {
+ switch (cmd) {
case BIO_CTRL_RESET:
- ctx->cont=1;
- ctx->start=1;
- ctx->encode=B64_NONE;
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ctx->cont = 1;
+ ctx->start = 1;
+ ctx->encode = B64_NONE;
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
case BIO_CTRL_EOF: /* More to read */
if (ctx->cont <= 0)
- ret=1;
+ ret = 1;
else
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
case BIO_CTRL_WPENDING: /* More to write in buffer */
OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
- ret=ctx->buf_len-ctx->buf_off;
- if ((ret == 0) && (ctx->encode != B64_NONE)
- && (ctx->base64.num != 0))
- ret=1;
+ ret = ctx->buf_len - ctx->buf_off;
+ if ((ret == 0) && (ctx->encode != B64_NONE) &&
+ (ctx->base64.num != 0))
+ ret = 1;
else if (ret <= 0)
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
case BIO_CTRL_PENDING: /* More to read in buffer */
OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
- ret=ctx->buf_len-ctx->buf_off;
+ ret = ctx->buf_len - ctx->buf_off;
if (ret <= 0)
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
case BIO_CTRL_FLUSH:
/* do a final write */
again:
- while (ctx->buf_len != ctx->buf_off)
- {
- i=b64_write(b,NULL,0);
+ while (ctx->buf_len != ctx->buf_off) {
+ i = b64_write(b, NULL, 0);
if (i < 0)
return i;
- }
- if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)
- {
- if (ctx->tmp_len != 0)
- {
- ctx->buf_len=EVP_EncodeBlock(
- (unsigned char *)ctx->buf,
- (unsigned char *)ctx->tmp,
- ctx->tmp_len);
- ctx->buf_off=0;
- ctx->tmp_len=0;
+ }
+ if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
+ if (ctx->tmp_len != 0) {
+ ctx->buf_len = EVP_EncodeBlock(
+ (unsigned char *)ctx->buf,
+ (unsigned char *)ctx->tmp,
+ ctx->tmp_len);
+ ctx->buf_off = 0;
+ ctx->tmp_len = 0;
goto again;
- }
}
- else if (ctx->encode != B64_NONE && ctx->base64.num != 0)
- {
- ctx->buf_off=0;
+ } else if (ctx->encode != B64_NONE && ctx->base64.num != 0) {
+ ctx->buf_off = 0;
EVP_EncodeFinal(&(ctx->base64),
- (unsigned char *)ctx->buf,
- &(ctx->buf_len));
+ (unsigned char *)ctx->buf,
+ &(ctx->buf_len));
/* push out the bytes */
goto again;
- }
+ }
/* Finally flush the underlying BIO */
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
case BIO_C_DO_STATE_MACHINE:
BIO_clear_retry_flags(b);
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
BIO_copy_next_retry(b);
break;
case BIO_CTRL_GET:
case BIO_CTRL_SET:
default:
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
- }
- return(ret);
}
+ return (ret);
+}
-static long b64_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
- {
- long ret=1;
+static long
+b64_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
+{
+ long ret = 1;
- if (b->next_bio == NULL) return(0);
- switch (cmd)
- {
+ if (b->next_bio == NULL)
+ return (0);
+ switch (cmd) {
default:
- ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
+ ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
break;
- }
- return(ret);
- }
-
-static int b64_puts(BIO *b, const char *str)
- {
- return b64_write(b,str,strlen(str));
}
+ return (ret);
+}
+
+static int
+b64_puts(BIO *b, const char *str)
+{
+ return b64_write(b, str, strlen(str));
+}
* This package is an SSL implementation written
* by Eric Young (eay@cryptsoft.com).
* The implementation was written so as to conform with Netscapes SSL.
- *
+ *
* This library is free for commercial and non-commercial use as long as
* the following conditions are aheared to. The following conditions
* apply to all code found in this distribution, be it the RC4, RSA,
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
* included with this distribution is covered by the same copyright terms
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
- *
+ *
* Copyright remains Eric Young's, and as such any Copyright notices in
* the code are not to be removed.
* If this package is used in a product, Eric Young should be given attribution
* as the author of the parts of the library used.
* This can be in the form of a textual message at program startup or
* in documentation (online or textual) provided with the package.
- *
+ *
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* Eric Young (eay@cryptsoft.com)"
* The word 'cryptographic' can be left out if the rouines from the library
* being used are not cryptographic related :-).
- * 4. If you include any Windows specific code (or a derivative thereof) from
+ * 4. If you include any Windows specific code (or a derivative thereof) from
* the apps directory (application code) you must include an acknowledgement:
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
- *
+ *
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
- *
+ *
* The licence and distribution terms for any publically available version or
* derivative of this code cannot be changed. i.e. this code cannot simply be
* copied and put under another distribution licence
#define ENC_BLOCK_SIZE (1024*4)
#define BUF_OFFSET (EVP_MAX_BLOCK_LENGTH*2)
-typedef struct enc_struct
- {
+typedef struct enc_struct {
int buf_len;
int buf_off;
int cont; /* <= 0 when finished */
/* buf is larger than ENC_BLOCK_SIZE because EVP_DecryptUpdate
* can return up to a block more data than is presented to it
*/
- char buf[ENC_BLOCK_SIZE+BUF_OFFSET+2];
- } BIO_ENC_CTX;
+ char buf[ENC_BLOCK_SIZE + BUF_OFFSET + 2];
+} BIO_ENC_CTX;
-static BIO_METHOD methods_enc= {
+static BIO_METHOD methods_enc = {
.type = BIO_TYPE_CIPHER,
.name = "cipher",
.bwrite = enc_write,
.callback_ctrl = enc_callback_ctrl
};
-BIO_METHOD *BIO_f_cipher(void)
- {
- return(&methods_enc);
- }
+BIO_METHOD *
+BIO_f_cipher(void)
+{
+ return (&methods_enc);
+}
-static int enc_new(BIO *bi)
- {
+static int
+enc_new(BIO *bi)
+{
BIO_ENC_CTX *ctx;
- ctx=(BIO_ENC_CTX *)malloc(sizeof(BIO_ENC_CTX));
- if (ctx == NULL) return(0);
+ ctx = (BIO_ENC_CTX *)malloc(sizeof(BIO_ENC_CTX));
+ if (ctx == NULL)
+ return (0);
EVP_CIPHER_CTX_init(&ctx->cipher);
- ctx->buf_len=0;
- ctx->buf_off=0;
- ctx->cont=1;
- ctx->finished=0;
- ctx->ok=1;
-
- bi->init=0;
- bi->ptr=(char *)ctx;
- bi->flags=0;
- return(1);
- }
-
-static int enc_free(BIO *a)
- {
+ ctx->buf_len = 0;
+ ctx->buf_off = 0;
+ ctx->cont = 1;
+ ctx->finished = 0;
+ ctx->ok = 1;
+
+ bi->init = 0;
+ bi->ptr = (char *)ctx;
+ bi->flags = 0;
+ return (1);
+}
+
+static int
+enc_free(BIO *a)
+{
BIO_ENC_CTX *b;
- if (a == NULL) return(0);
- b=(BIO_ENC_CTX *)a->ptr;
+ if (a == NULL)
+ return (0);
+ b = (BIO_ENC_CTX *)a->ptr;
EVP_CIPHER_CTX_cleanup(&(b->cipher));
- OPENSSL_cleanse(a->ptr,sizeof(BIO_ENC_CTX));
+ OPENSSL_cleanse(a->ptr, sizeof(BIO_ENC_CTX));
free(a->ptr);
- a->ptr=NULL;
- a->init=0;
- a->flags=0;
- return(1);
- }
-
-static int enc_read(BIO *b, char *out, int outl)
- {
- int ret=0,i;
+ a->ptr = NULL;
+ a->init = 0;
+ a->flags = 0;
+ return (1);
+}
+
+static int
+enc_read(BIO *b, char *out, int outl)
+{
+ int ret = 0, i;
BIO_ENC_CTX *ctx;
- if (out == NULL) return(0);
- ctx=(BIO_ENC_CTX *)b->ptr;
+ if (out == NULL)
+ return (0);
+ ctx = (BIO_ENC_CTX *)b->ptr;
- if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
+ if ((ctx == NULL) || (b->next_bio == NULL))
+ return (0);
/* First check if there are bytes decoded/encoded */
- if (ctx->buf_len > 0)
- {
- i=ctx->buf_len-ctx->buf_off;
- if (i > outl) i=outl;
- memcpy(out,&(ctx->buf[ctx->buf_off]),i);
- ret=i;
- out+=i;
- outl-=i;
- ctx->buf_off+=i;
- if (ctx->buf_len == ctx->buf_off)
- {
- ctx->buf_len=0;
- ctx->buf_off=0;
- }
+ if (ctx->buf_len > 0) {
+ i = ctx->buf_len - ctx->buf_off;
+ if (i > outl)
+ i = outl;
+ memcpy(out, &(ctx->buf[ctx->buf_off]), i);
+ ret = i;
+ out += i;
+ outl -= i;
+ ctx->buf_off += i;
+ if (ctx->buf_len == ctx->buf_off) {
+ ctx->buf_len = 0;
+ ctx->buf_off = 0;
}
+ }
/* At this point, we have room of outl bytes and an empty
* buffer, so we should read in some more. */
- while (outl > 0)
- {
- if (ctx->cont <= 0) break;
+ while (outl > 0) {
+ if (ctx->cont <= 0)
+ break;
/* read in at IV offset, read the EVP_Cipher
* documentation about why */
- i=BIO_read(b->next_bio,&(ctx->buf[BUF_OFFSET]),ENC_BLOCK_SIZE);
+ i = BIO_read(b->next_bio, &(ctx->buf[BUF_OFFSET]), ENC_BLOCK_SIZE);
- if (i <= 0)
- {
+ if (i <= 0) {
/* Should be continue next time we are called? */
- if (!BIO_should_retry(b->next_bio))
- {
- ctx->cont=i;
- i=EVP_CipherFinal_ex(&(ctx->cipher),
- (unsigned char *)ctx->buf,
- &(ctx->buf_len));
- ctx->ok=i;
- ctx->buf_off=0;
- }
- else
- {
- ret=(ret == 0)?i:ret;
+ if (!BIO_should_retry(b->next_bio)) {
+ ctx->cont = i;
+ i = EVP_CipherFinal_ex(&(ctx->cipher),
+ (unsigned char *)ctx->buf,
+ &(ctx->buf_len));
+ ctx->ok = i;
+ ctx->buf_off = 0;
+ } else {
+ ret = (ret == 0) ? i : ret;
break;
- }
}
- else
- {
+ } else {
EVP_CipherUpdate(&(ctx->cipher),
- (unsigned char *)ctx->buf,&ctx->buf_len,
- (unsigned char *)&(ctx->buf[BUF_OFFSET]),i);
- ctx->cont=1;
+ (unsigned char *)ctx->buf, &ctx->buf_len,
+ (unsigned char *)&(ctx->buf[BUF_OFFSET]), i);
+ ctx->cont = 1;
/* Note: it is possible for EVP_CipherUpdate to
* decrypt zero bytes because this is or looks like
* the final block: if this happens we should retry
* and either read more data or decrypt the final
* block
*/
- if(ctx->buf_len == 0) continue;
- }
+ if (ctx->buf_len == 0)
+ continue;
+ }
if (ctx->buf_len <= outl)
- i=ctx->buf_len;
+ i = ctx->buf_len;
else
- i=outl;
- if (i <= 0) break;
- memcpy(out,ctx->buf,i);
- ret+=i;
- ctx->buf_off=i;
- outl-=i;
- out+=i;
- }
+ i = outl;
+ if (i <= 0)
+ break;
+ memcpy(out, ctx->buf, i);
+ ret += i;
+ ctx->buf_off = i;
+ outl -= i;
+ out += i;
+ }
BIO_clear_retry_flags(b);
BIO_copy_next_retry(b);
- return((ret == 0)?ctx->cont:ret);
- }
+ return ((ret == 0) ? ctx->cont : ret);
+}
-static int enc_write(BIO *b, const char *in, int inl)
- {
- int ret=0,n,i;
+static int
+enc_write(BIO *b, const char *in, int inl)
+{
+ int ret = 0, n, i;
BIO_ENC_CTX *ctx;
- ctx=(BIO_ENC_CTX *)b->ptr;
- ret=inl;
+ ctx = (BIO_ENC_CTX *)b->ptr;
+ ret = inl;
BIO_clear_retry_flags(b);
- n=ctx->buf_len-ctx->buf_off;
- while (n > 0)
- {
- i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
- if (i <= 0)
- {
+ n = ctx->buf_len - ctx->buf_off;
+ while (n > 0) {
+ i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n);
+ if (i <= 0) {
BIO_copy_next_retry(b);
- return(i);
- }
- ctx->buf_off+=i;
- n-=i;
+ return (i);
}
+ ctx->buf_off += i;
+ n -= i;
+ }
/* at this point all pending data has been written */
- if ((in == NULL) || (inl <= 0)) return(0);
+ if ((in == NULL) || (inl <= 0))
+ return (0);
- ctx->buf_off=0;
- while (inl > 0)
- {
- n=(inl > ENC_BLOCK_SIZE)?ENC_BLOCK_SIZE:inl;
+ ctx->buf_off = 0;
+ while (inl > 0) {
+ n = (inl > ENC_BLOCK_SIZE) ? ENC_BLOCK_SIZE : inl;
EVP_CipherUpdate(&(ctx->cipher),
- (unsigned char *)ctx->buf,&ctx->buf_len,
- (unsigned char *)in,n);
- inl-=n;
- in+=n;
-
- ctx->buf_off=0;
- n=ctx->buf_len;
- while (n > 0)
- {
- i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
- if (i <= 0)
- {
+ (unsigned char *)ctx->buf, &ctx->buf_len,
+ (unsigned char *)in, n);
+ inl -= n;
+ in += n;
+
+ ctx->buf_off = 0;
+ n = ctx->buf_len;
+ while (n > 0) {
+ i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n);
+ if (i <= 0) {
BIO_copy_next_retry(b);
return (ret == inl) ? i : ret - inl;
- }
- n-=i;
- ctx->buf_off+=i;
}
- ctx->buf_len=0;
- ctx->buf_off=0;
+ n -= i;
+ ctx->buf_off += i;
}
- BIO_copy_next_retry(b);
- return(ret);
+ ctx->buf_len = 0;
+ ctx->buf_off = 0;
}
+ BIO_copy_next_retry(b);
+ return (ret);
+}
-static long enc_ctrl(BIO *b, int cmd, long num, void *ptr)
- {
+static long
+enc_ctrl(BIO *b, int cmd, long num, void *ptr)
+{
BIO *dbio;
- BIO_ENC_CTX *ctx,*dctx;
- long ret=1;
+ BIO_ENC_CTX *ctx, *dctx;
+ long ret = 1;
int i;
EVP_CIPHER_CTX **c_ctx;
- ctx=(BIO_ENC_CTX *)b->ptr;
+ ctx = (BIO_ENC_CTX *)b->ptr;
- switch (cmd)
- {
+ switch (cmd) {
case BIO_CTRL_RESET:
- ctx->ok=1;
- ctx->finished=0;
- EVP_CipherInit_ex(&(ctx->cipher),NULL,NULL,NULL,NULL,
- ctx->cipher.encrypt);
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ctx->ok = 1;
+ ctx->finished = 0;
+ EVP_CipherInit_ex(&(ctx->cipher), NULL, NULL, NULL, NULL,
+ ctx->cipher.encrypt);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
case BIO_CTRL_EOF: /* More to read */
if (ctx->cont <= 0)
- ret=1;
+ ret = 1;
else
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
case BIO_CTRL_WPENDING:
- ret=ctx->buf_len-ctx->buf_off;
+ ret = ctx->buf_len - ctx->buf_off;
if (ret <= 0)
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
case BIO_CTRL_PENDING: /* More to read in buffer */
- ret=ctx->buf_len-ctx->buf_off;
+ ret = ctx->buf_len - ctx->buf_off;
if (ret <= 0)
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
case BIO_CTRL_FLUSH:
/* do a final write */
again:
- while (ctx->buf_len != ctx->buf_off)
- {
- i=enc_write(b,NULL,0);
+ while (ctx->buf_len != ctx->buf_off) {
+ i = enc_write(b, NULL, 0);
if (i < 0)
return i;
- }
+ }
- if (!ctx->finished)
- {
- ctx->finished=1;
- ctx->buf_off=0;
- ret=EVP_CipherFinal_ex(&(ctx->cipher),
- (unsigned char *)ctx->buf,
- &(ctx->buf_len));
- ctx->ok=(int)ret;
- if (ret <= 0) break;
+ if (!ctx->finished) {
+ ctx->finished = 1;
+ ctx->buf_off = 0;
+ ret = EVP_CipherFinal_ex(&(ctx->cipher),
+ (unsigned char *)ctx->buf,
+ &(ctx->buf_len));
+ ctx->ok = (int)ret;
+ if (ret <= 0)
+ break;
/* push out the bytes */
goto again;
- }
-
+ }
+
/* Finally flush the underlying BIO */
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
case BIO_C_GET_CIPHER_STATUS:
- ret=(long)ctx->ok;
+ ret = (long)ctx->ok;
break;
case BIO_C_DO_STATE_MACHINE:
BIO_clear_retry_flags(b);
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
BIO_copy_next_retry(b);
break;
case BIO_C_GET_CIPHER_CTX:
- c_ctx=(EVP_CIPHER_CTX **)ptr;
- (*c_ctx)= &(ctx->cipher);
- b->init=1;
+ c_ctx = (EVP_CIPHER_CTX **)ptr;
+ (*c_ctx) = &(ctx->cipher);
+ b->init = 1;
break;
case BIO_CTRL_DUP:
- dbio=(BIO *)ptr;
- dctx=(BIO_ENC_CTX *)dbio->ptr;
+ dbio = (BIO *)ptr;
+ dctx = (BIO_ENC_CTX *)dbio->ptr;
EVP_CIPHER_CTX_init(&dctx->cipher);
- ret = EVP_CIPHER_CTX_copy(&dctx->cipher,&ctx->cipher);
+ ret = EVP_CIPHER_CTX_copy(&dctx->cipher, &ctx->cipher);
if (ret)
- dbio->init=1;
+ dbio->init = 1;
break;
default:
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
- }
- return(ret);
}
+ return (ret);
+}
-static long enc_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
- {
- long ret=1;
+static long
+enc_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
+{
+ long ret = 1;
- if (b->next_bio == NULL) return(0);
- switch (cmd)
- {
+ if (b->next_bio == NULL)
+ return (0);
+ switch (cmd) {
default:
- ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
+ ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
break;
- }
- return(ret);
}
+ return (ret);
+}
/*
void BIO_set_cipher_ctx(b,c)
b->init=1;
ctx=(BIO_ENC_CTX *)b->ptr;
memcpy(ctx->cipher,c,sizeof(EVP_CIPHER_CTX));
-
+
if (b->callback != NULL)
b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L);
}
*/
-void BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k,
- const unsigned char *i, int e)
- {
+void
+BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k,
+ const unsigned char *i, int e)
+{
BIO_ENC_CTX *ctx;
- if (b == NULL) return;
+ if (b == NULL)
+ return;
if ((b->callback != NULL) &&
- (b->callback(b,BIO_CB_CTRL,(const char *)c,BIO_CTRL_SET,e,0L) <= 0))
+ (b->callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 0L) <= 0))
return;
- b->init=1;
- ctx=(BIO_ENC_CTX *)b->ptr;
- EVP_CipherInit_ex(&(ctx->cipher),c,NULL, k,i,e);
-
- if (b->callback != NULL)
- b->callback(b,BIO_CB_CTRL,(const char *)c,BIO_CTRL_SET,e,1L);
- }
+ b->init = 1;
+ ctx = (BIO_ENC_CTX *)b->ptr;
+ EVP_CipherInit_ex(&(ctx->cipher), c,NULL, k, i, e);
+ if (b->callback != NULL)
+ b->callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 1L);
+}
* This package is an SSL implementation written
* by Eric Young (eay@cryptsoft.com).
* The implementation was written so as to conform with Netscapes SSL.
- *
+ *
* This library is free for commercial and non-commercial use as long as
* the following conditions are aheared to. The following conditions
* apply to all code found in this distribution, be it the RC4, RSA,
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
* included with this distribution is covered by the same copyright terms
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
- *
+ *
* Copyright remains Eric Young's, and as such any Copyright notices in
* the code are not to be removed.
* If this package is used in a product, Eric Young should be given attribution
* as the author of the parts of the library used.
* This can be in the form of a textual message at program startup or
* in documentation (online or textual) provided with the package.
- *
+ *
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* Eric Young (eay@cryptsoft.com)"
* The word 'cryptographic' can be left out if the rouines from the library
* being used are not cryptographic related :-).
- * 4. If you include any Windows specific code (or a derivative thereof) from
+ * 4. If you include any Windows specific code (or a derivative thereof) from
* the apps directory (application code) you must include an acknowledgement:
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
- *
+ *
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
- *
+ *
* The licence and distribution terms for any publically available version or
* derivative of this code cannot be changed. i.e. this code cannot simply be
* copied and put under another distribution licence
static long md_ctrl(BIO *h, int cmd, long arg1, void *arg2);
static int md_new(BIO *h);
static int md_free(BIO *data);
-static long md_callback_ctrl(BIO *h,int cmd,bio_info_cb *fp);
+static long md_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
static BIO_METHOD methods_md = {
.type = BIO_TYPE_MD,
.callback_ctrl = md_callback_ctrl
};
-BIO_METHOD *BIO_f_md(void)
- {
- return(&methods_md);
- }
+BIO_METHOD *
+BIO_f_md(void)
+{
+ return (&methods_md);
+}
-static int md_new(BIO *bi)
- {
+static int
+md_new(BIO *bi)
+{
EVP_MD_CTX *ctx;
- ctx=EVP_MD_CTX_create();
- if (ctx == NULL) return(0);
+ ctx = EVP_MD_CTX_create();
+ if (ctx == NULL)
+ return (0);
- bi->init=0;
- bi->ptr=(char *)ctx;
- bi->flags=0;
- return(1);
- }
+ bi->init = 0;
+ bi->ptr = (char *)ctx;
+ bi->flags = 0;
+ return (1);
+}
-static int md_free(BIO *a)
- {
- if (a == NULL) return(0);
+static int
+md_free(BIO *a)
+{
+ if (a == NULL)
+ return (0);
EVP_MD_CTX_destroy(a->ptr);
- a->ptr=NULL;
- a->init=0;
- a->flags=0;
- return(1);
- }
-
-static int md_read(BIO *b, char *out, int outl)
- {
- int ret=0;
+ a->ptr = NULL;
+ a->init = 0;
+ a->flags = 0;
+ return (1);
+}
+
+static int
+md_read(BIO *b, char *out, int outl)
+{
+ int ret = 0;
EVP_MD_CTX *ctx;
- if (out == NULL) return(0);
- ctx=b->ptr;
+ if (out == NULL)
+ return (0);
+ ctx = b->ptr;
- if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
+ if ((ctx == NULL) || (b->next_bio == NULL))
+ return (0);
- ret=BIO_read(b->next_bio,out,outl);
- if (b->init)
- {
- if (ret > 0)
- {
- if (EVP_DigestUpdate(ctx,(unsigned char *)out,
- (unsigned int)ret)<=0) return (-1);
- }
+ ret = BIO_read(b->next_bio, out, outl);
+ if (b->init) {
+ if (ret > 0) {
+ if (EVP_DigestUpdate(ctx, (unsigned char *)out,
+ (unsigned int)ret) <= 0)
+ return (-1);
}
+ }
BIO_clear_retry_flags(b);
BIO_copy_next_retry(b);
- return(ret);
- }
+ return (ret);
+}
-static int md_write(BIO *b, const char *in, int inl)
- {
- int ret=0;
+static int
+md_write(BIO *b, const char *in, int inl)
+{
+ int ret = 0;
EVP_MD_CTX *ctx;
- if ((in == NULL) || (inl <= 0)) return(0);
- ctx=b->ptr;
+ if ((in == NULL) || (inl <= 0))
+ return (0);
+ ctx = b->ptr;
if ((ctx != NULL) && (b->next_bio != NULL))
- ret=BIO_write(b->next_bio,in,inl);
- if (b->init)
- {
- if (ret > 0)
- {
- if (!EVP_DigestUpdate(ctx,(const unsigned char *)in,
- (unsigned int)ret))
- {
+ ret = BIO_write(b->next_bio, in, inl);
+ if (b->init) {
+ if (ret > 0) {
+ if (!EVP_DigestUpdate(ctx, (const unsigned char *)in,
+ (unsigned int)ret)) {
BIO_clear_retry_flags(b);
return 0;
- }
}
}
- if(b->next_bio != NULL)
- {
+ }
+ if (b->next_bio != NULL) {
BIO_clear_retry_flags(b);
BIO_copy_next_retry(b);
- }
- return(ret);
}
+ return (ret);
+}
-static long md_ctrl(BIO *b, int cmd, long num, void *ptr)
- {
- EVP_MD_CTX *ctx,*dctx,**pctx;
+static long
+md_ctrl(BIO *b, int cmd, long num, void *ptr)
+{
+ EVP_MD_CTX *ctx, *dctx, **pctx;
const EVP_MD **ppmd;
EVP_MD *md;
- long ret=1;
+ long ret = 1;
BIO *dbio;
- ctx=b->ptr;
+ ctx = b->ptr;
- switch (cmd)
- {
+ switch (cmd) {
case BIO_CTRL_RESET:
if (b->init)
- ret = EVP_DigestInit_ex(ctx,ctx->digest, NULL);
+ ret = EVP_DigestInit_ex(ctx, ctx->digest, NULL);
else
- ret=0;
+ ret = 0;
if (ret > 0)
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
case BIO_C_GET_MD:
- if (b->init)
- {
- ppmd=ptr;
- *ppmd=ctx->digest;
- }
- else
- ret=0;
+ if (b->init) {
+ ppmd = ptr;
+ *ppmd = ctx->digest;
+ } else
+ ret = 0;
break;
case BIO_C_GET_MD_CTX:
- pctx=ptr;
- *pctx=ctx;
+ pctx = ptr;
+ *pctx = ctx;
b->init = 1;
break;
case BIO_C_SET_MD_CTX:
if (b->init)
- b->ptr=ptr;
+ b->ptr = ptr;
else
- ret=0;
+ ret = 0;
break;
case BIO_C_DO_STATE_MACHINE:
BIO_clear_retry_flags(b);
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
BIO_copy_next_retry(b);
break;
case BIO_C_SET_MD:
- md=ptr;
- ret = EVP_DigestInit_ex(ctx,md, NULL);
+ md = ptr;
+ ret = EVP_DigestInit_ex(ctx, md, NULL);
if (ret > 0)
- b->init=1;
+ b->init = 1;
break;
case BIO_CTRL_DUP:
- dbio=ptr;
- dctx=dbio->ptr;
- if (!EVP_MD_CTX_copy_ex(dctx,ctx))
+ dbio = ptr;
+ dctx = dbio->ptr;
+ if (!EVP_MD_CTX_copy_ex(dctx, ctx))
return 0;
- b->init=1;
+ b->init = 1;
break;
default:
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
- }
- return(ret);
}
+ return (ret);
+}
-static long md_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
- {
- long ret=1;
+static long
+md_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
+{
+ long ret = 1;
- if (b->next_bio == NULL) return(0);
- switch (cmd)
- {
+ if (b->next_bio == NULL)
+ return (0);
+ switch (cmd) {
default:
- ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
+ ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
break;
- }
- return(ret);
}
+ return (ret);
+}
-static int md_gets(BIO *bp, char *buf, int size)
- {
+static int
+md_gets(BIO *bp, char *buf, int size)
+{
EVP_MD_CTX *ctx;
unsigned int ret;
-
- ctx=bp->ptr;
+ ctx = bp->ptr;
if (size < ctx->digest->md_size)
- return(0);
- if (EVP_DigestFinal_ex(ctx,(unsigned char *)buf,&ret)<=0)
+ return (0);
+ if (EVP_DigestFinal_ex(ctx, (unsigned char *)buf, &ret) <= 0)
return -1;
-
- return((int)ret);
- }
+
+ return ((int)ret);
+}
/*
static int md_puts(bp,str)
return(-1);
}
*/
-
* This package is an SSL implementation written
* by Eric Young (eay@cryptsoft.com).
* The implementation was written so as to conform with Netscapes SSL.
- *
+ *
* This library is free for commercial and non-commercial use as long as
* the following conditions are aheared to. The following conditions
* apply to all code found in this distribution, be it the RC4, RSA,
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
* included with this distribution is covered by the same copyright terms
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
- *
+ *
* Copyright remains Eric Young's, and as such any Copyright notices in
* the code are not to be removed.
* If this package is used in a product, Eric Young should be given attribution
* as the author of the parts of the library used.
* This can be in the form of a textual message at program startup or
* in documentation (online or textual) provided with the package.
- *
+ *
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* Eric Young (eay@cryptsoft.com)"
* The word 'cryptographic' can be left out if the rouines from the library
* being used are not cryptographic related :-).
- * 4. If you include any Windows specific code (or a derivative thereof) from
+ * 4. If you include any Windows specific code (or a derivative thereof) from
* the apps directory (application code) you must include an acknowledgement:
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
- *
+ *
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
- *
+ *
* The licence and distribution terms for any publically available version or
* derivative of this code cannot be changed. i.e. this code cannot simply be
* copied and put under another distribution licence
Why BIO_f_reliable?
I wrote function which took BIO* as argument, read data from it
- and processed it. Then I wanted to store the input file in
+ and processed it. Then I wanted to store the input file in
encrypted form. OK I pushed BIO_f_cipher to the BIO stack
- and everything was OK. BUT if user types wrong password
+ and everything was OK. BUT if user types wrong password
BIO_f_cipher outputs only garbage and my function crashes. Yes
- I can and I should fix my function, but BIO_f_cipher is
+ I can and I should fix my function, but BIO_f_cipher is
easy way to add encryption support to many existing applications
- and it's hard to debug and fix them all.
+ and it's hard to debug and fix them all.
So I wanted another BIO which would catch the incorrect passwords and
- file damages which cause garbage on BIO_f_cipher's output.
+ file damages which cause garbage on BIO_f_cipher's output.
- The easy way is to push the BIO_f_md and save the checksum at
+ The easy way is to push the BIO_f_md and save the checksum at
the end of the file. However there are several problems with this
approach:
- 1) you must somehow separate checksum from actual data.
- 2) you need lot's of memory when reading the file, because you
+ 1) you must somehow separate checksum from actual data.
+ 2) you need lot's of memory when reading the file, because you
must read to the end of the file and verify the checksum before
- letting the application to read the data.
-
- BIO_f_reliable tries to solve both problems, so that you can
+ letting the application to read the data.
+
+ BIO_f_reliable tries to solve both problems, so that you can
read and write arbitrary long streams using only fixed amount
of memory.
BIO_f_reliable splits data stream into blocks. Each block is prefixed
- with it's length and suffixed with it's digest. So you need only
- several Kbytes of memory to buffer single block before verifying
- it's digest.
+ with it's length and suffixed with it's digest. So you need only
+ several Kbytes of memory to buffer single block before verifying
+ it's digest.
BIO_f_reliable goes further and adds several important capabilities:
- 1) the digest of the block is computed over the whole stream
+ 1) the digest of the block is computed over the whole stream
-- so nobody can rearrange the blocks or remove or replace them.
- 2) to detect invalid passwords right at the start BIO_f_reliable
+ 2) to detect invalid passwords right at the start BIO_f_reliable
adds special prefix to the stream. In order to avoid known plain-text
attacks this prefix is generated as follows:
- *) digest is initialized with random seed instead of
+ *) digest is initialized with random seed instead of
standardized one.
*) same seed is written to output
- *) well-known text is then hashed and the output
+ *) well-known text is then hashed and the output
of the digest is also written to output.
reader can now read the seed from stream, hash the same string
and then compare the digest output.
- Bad things: BIO_f_reliable knows what's going on in EVP_Digest. I
+ Bad things: BIO_f_reliable knows what's going on in EVP_Digest. I
initially wrote and tested this code on x86 machine and wrote the
digests out in machine-dependent order :( There are people using
this code and I cannot change this easily without making existing
#define IOBS (OK_BLOCK_SIZE+ OK_BLOCK_BLOCK+ 3*EVP_MAX_MD_SIZE)
#define WELLKNOWN "The quick brown fox jumped over the lazy dog's back."
-typedef struct ok_struct
- {
+typedef struct ok_struct {
size_t buf_len;
size_t buf_off;
size_t buf_len_save;
int cont; /* <= 0 when finished */
int finished;
EVP_MD_CTX md;
- int blockout; /* output block is ready */
+ int blockout; /* output block is ready */
int sigio; /* must process signature */
unsigned char buf[IOBS];
- } BIO_OK_CTX;
+} BIO_OK_CTX;
static BIO_METHOD methods_ok = {
.type = BIO_TYPE_CIPHER,
.callback_ctrl = ok_callback_ctrl
};
-BIO_METHOD *BIO_f_reliable(void)
- {
- return(&methods_ok);
- }
+BIO_METHOD *
+BIO_f_reliable(void)
+{
+ return (&methods_ok);
+}
-static int ok_new(BIO *bi)
- {
+static int
+ok_new(BIO *bi)
+{
BIO_OK_CTX *ctx;
- ctx=(BIO_OK_CTX *)malloc(sizeof(BIO_OK_CTX));
- if (ctx == NULL) return(0);
+ ctx = (BIO_OK_CTX *)malloc(sizeof(BIO_OK_CTX));
+ if (ctx == NULL)
+ return (0);
- ctx->buf_len=0;
- ctx->buf_off=0;
- ctx->buf_len_save=0;
- ctx->buf_off_save=0;
- ctx->cont=1;
- ctx->finished=0;
- ctx->blockout= 0;
- ctx->sigio=1;
+ ctx->buf_len = 0;
+ ctx->buf_off = 0;
+ ctx->buf_len_save = 0;
+ ctx->buf_off_save = 0;
+ ctx->cont = 1;
+ ctx->finished = 0;
+ ctx->blockout = 0;
+ ctx->sigio = 1;
EVP_MD_CTX_init(&ctx->md);
- bi->init=0;
- bi->ptr=(char *)ctx;
- bi->flags=0;
- return(1);
- }
+ bi->init = 0;
+ bi->ptr = (char *)ctx;
+ bi->flags = 0;
+ return (1);
+}
-static int ok_free(BIO *a)
- {
- if (a == NULL) return(0);
+static int
+ok_free(BIO *a)
+{
+ if (a == NULL)
+ return (0);
EVP_MD_CTX_cleanup(&((BIO_OK_CTX *)a->ptr)->md);
- OPENSSL_cleanse(a->ptr,sizeof(BIO_OK_CTX));
+ OPENSSL_cleanse(a->ptr, sizeof(BIO_OK_CTX));
free(a->ptr);
- a->ptr=NULL;
- a->init=0;
- a->flags=0;
- return(1);
- }
-
-static int ok_read(BIO *b, char *out, int outl)
- {
- int ret=0,i,n;
+ a->ptr = NULL;
+ a->init = 0;
+ a->flags = 0;
+ return (1);
+}
+
+static int
+ok_read(BIO *b, char *out, int outl)
+{
+ int ret = 0, i, n;
BIO_OK_CTX *ctx;
- if (out == NULL) return(0);
- ctx=(BIO_OK_CTX *)b->ptr;
+ if (out == NULL)
+ return (0);
+ ctx = (BIO_OK_CTX *)b->ptr;
- if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0)) return(0);
+ if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0))
+ return (0);
- while(outl > 0)
- {
+ while (outl > 0) {
/* copy clean bytes to output buffer */
- if (ctx->blockout)
- {
- i=ctx->buf_len-ctx->buf_off;
- if (i > outl) i=outl;
- memcpy(out,&(ctx->buf[ctx->buf_off]),i);
- ret+=i;
- out+=i;
- outl-=i;
- ctx->buf_off+=i;
+ if (ctx->blockout) {
+ i = ctx->buf_len - ctx->buf_off;
+ if (i > outl)
+ i = outl;
+ memcpy(out, &(ctx->buf[ctx->buf_off]), i);
+ ret += i;
+ out += i;
+ outl -= i;
+ ctx->buf_off += i;
/* all clean bytes are out */
- if (ctx->buf_len == ctx->buf_off)
- {
- ctx->buf_off=0;
+ if (ctx->buf_len == ctx->buf_off) {
+ ctx->buf_off = 0;
/* copy start of the next block into proper place */
- if(ctx->buf_len_save- ctx->buf_off_save > 0)
- {
- ctx->buf_len= ctx->buf_len_save- ctx->buf_off_save;
+ if (ctx->buf_len_save - ctx->buf_off_save > 0) {
+ ctx->buf_len = ctx->buf_len_save - ctx->buf_off_save;
memmove(ctx->buf, &(ctx->buf[ctx->buf_off_save]),
- ctx->buf_len);
- }
- else
- {
- ctx->buf_len=0;
- }
- ctx->blockout= 0;
+ ctx->buf_len);
+ } else {
+ ctx->buf_len = 0;
}
+ ctx->blockout = 0;
}
-
+ }
+
/* output buffer full -- cancel */
- if (outl == 0) break;
+ if (outl == 0)
+ break;
/* no clean bytes in buffer -- fill it */
- n=IOBS- ctx->buf_len;
- i=BIO_read(b->next_bio,&(ctx->buf[ctx->buf_len]),n);
+ n = IOBS - ctx->buf_len;
+ i = BIO_read(b->next_bio, &(ctx->buf[ctx->buf_len]), n);
if (i <= 0) break; /* nothing new */
- ctx->buf_len+= i;
+ ctx->buf_len += i;
/* no signature yet -- check if we got one */
- if (ctx->sigio == 1)
- {
- if (!sig_in(b))
- {
+ if (ctx->sigio == 1) {
+ if (!sig_in(b)) {
BIO_clear_retry_flags(b);
return 0;
- }
}
+ }
/* signature ok -- check if we got block */
- if (ctx->sigio == 0)
- {
- if (!block_in(b))
- {
+ if (ctx->sigio == 0) {
+ if (!block_in(b)) {
BIO_clear_retry_flags(b);
return 0;
- }
}
+ }
/* invalid block -- cancel */
- if (ctx->cont <= 0) break;
+ if (ctx->cont <= 0)
+ break;
- }
+ }
BIO_clear_retry_flags(b);
BIO_copy_next_retry(b);
- return(ret);
- }
+ return (ret);
+}
-static int ok_write(BIO *b, const char *in, int inl)
- {
- int ret=0,n,i;
+static int
+ok_write(BIO *b, const char *in, int inl)
+{
+ int ret = 0, n, i;
BIO_OK_CTX *ctx;
- if (inl <= 0) return inl;
+ if (inl <= 0)
+ return inl;
- ctx=(BIO_OK_CTX *)b->ptr;
- ret=inl;
+ ctx = (BIO_OK_CTX *)b->ptr;
+ ret = inl;
- if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0)) return(0);
+ if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0))
+ return (0);
- if(ctx->sigio && !sig_out(b))
+ if (ctx->sigio && !sig_out(b))
return 0;
- do{
+ do {
BIO_clear_retry_flags(b);
- n=ctx->buf_len-ctx->buf_off;
- while (ctx->blockout && n > 0)
- {
- i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
- if (i <= 0)
- {
+ n = ctx->buf_len - ctx->buf_off;
+ while (ctx->blockout && n > 0) {
+ i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n);
+ if (i <= 0) {
BIO_copy_next_retry(b);
- if(!BIO_should_retry(b))
- ctx->cont= 0;
- return(i);
- }
- ctx->buf_off+=i;
- n-=i;
+ if (!BIO_should_retry(b))
+ ctx->cont = 0;
+ return (i);
}
+ ctx->buf_off += i;
+ n -= i;
+ }
/* at this point all pending data has been written */
- ctx->blockout= 0;
- if (ctx->buf_len == ctx->buf_off)
- {
- ctx->buf_len=OK_BLOCK_BLOCK;
- ctx->buf_off=0;
- }
-
- if ((in == NULL) || (inl <= 0)) return(0);
+ ctx->blockout = 0;
+ if (ctx->buf_len == ctx->buf_off) {
+ ctx->buf_len = OK_BLOCK_BLOCK;
+ ctx->buf_off = 0;
+ }
- n= (inl+ ctx->buf_len > OK_BLOCK_SIZE+ OK_BLOCK_BLOCK) ?
- (int)(OK_BLOCK_SIZE+OK_BLOCK_BLOCK-ctx->buf_len) : inl;
+ if ((in == NULL) || (inl <= 0))
+ return (0);
- memcpy((unsigned char *)(&(ctx->buf[ctx->buf_len])),(unsigned char *)in,n);
- ctx->buf_len+= n;
- inl-=n;
- in+=n;
+ n = (inl + ctx->buf_len > OK_BLOCK_SIZE + OK_BLOCK_BLOCK) ?
+ (int)(OK_BLOCK_SIZE + OK_BLOCK_BLOCK - ctx->buf_len) : inl;
- if(ctx->buf_len >= OK_BLOCK_SIZE+ OK_BLOCK_BLOCK)
- {
- if (!block_out(b))
- {
+ memcpy((unsigned char *)(&(ctx->buf[ctx->buf_len])),
+ (unsigned char *)in, n);
+ ctx->buf_len += n;
+ inl -= n;
+ in += n;
+
+ if (ctx->buf_len >= OK_BLOCK_SIZE + OK_BLOCK_BLOCK) {
+ if (!block_out(b)) {
BIO_clear_retry_flags(b);
return 0;
- }
}
- }while(inl > 0);
+ }
+ } while (inl > 0);
BIO_clear_retry_flags(b);
BIO_copy_next_retry(b);
- return(ret);
- }
+ return (ret);
+}
-static long ok_ctrl(BIO *b, int cmd, long num, void *ptr)
- {
+static long
+ok_ctrl(BIO *b, int cmd, long num, void *ptr)
+{
BIO_OK_CTX *ctx;
EVP_MD *md;
const EVP_MD **ppmd;
- long ret=1;
+ long ret = 1;
int i;
- ctx=b->ptr;
+ ctx = b->ptr;
- switch (cmd)
- {
+ switch (cmd) {
case BIO_CTRL_RESET:
- ctx->buf_len=0;
- ctx->buf_off=0;
- ctx->buf_len_save=0;
- ctx->buf_off_save=0;
- ctx->cont=1;
- ctx->finished=0;
- ctx->blockout= 0;
- ctx->sigio=1;
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ctx->buf_len = 0;
+ ctx->buf_off = 0;
+ ctx->buf_len_save = 0;
+ ctx->buf_off_save = 0;
+ ctx->cont = 1;
+ ctx->finished = 0;
+ ctx->blockout = 0;
+ ctx->sigio = 1;
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
case BIO_CTRL_EOF: /* More to read */
if (ctx->cont <= 0)
- ret=1;
+ ret = 1;
else
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
case BIO_CTRL_PENDING: /* More to read in buffer */
case BIO_CTRL_WPENDING: /* More to read in buffer */
- ret=ctx->blockout ? ctx->buf_len-ctx->buf_off : 0;
+ ret = ctx->blockout ? ctx->buf_len - ctx->buf_off : 0;
if (ret <= 0)
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
case BIO_CTRL_FLUSH:
/* do a final write */
- if(ctx->blockout == 0)
+ if (ctx->blockout == 0)
if (!block_out(b))
return 0;
- while (ctx->blockout)
- {
- i=ok_write(b,NULL,0);
- if (i < 0)
- {
- ret=i;
+ while (ctx->blockout) {
+ i = ok_write(b, NULL, 0);
+ if (i < 0) {
+ ret = i;
break;
- }
}
+ }
+
+ ctx->finished = 1;
+ ctx->buf_off = ctx->buf_len = 0;
+ ctx->cont = (int)ret;
- ctx->finished=1;
- ctx->buf_off=ctx->buf_len=0;
- ctx->cont=(int)ret;
-
/* Finally flush the underlying BIO */
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
case BIO_C_DO_STATE_MACHINE:
BIO_clear_retry_flags(b);
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
BIO_copy_next_retry(b);
break;
case BIO_CTRL_INFO:
- ret=(long)ctx->cont;
+ ret = (long)ctx->cont;
break;
case BIO_C_SET_MD:
- md=ptr;
+ md = ptr;
if (!EVP_DigestInit_ex(&ctx->md, md, NULL))
return 0;
- b->init=1;
+ b->init = 1;
break;
case BIO_C_GET_MD:
- if (b->init)
- {
- ppmd=ptr;
- *ppmd=ctx->md.digest;
- }
- else
- ret=0;
+ if (b->init) {
+ ppmd = ptr;
+ *ppmd = ctx->md.digest;
+ } else
+ ret = 0;
break;
default:
- ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+ ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
- }
- return(ret);
}
+ return (ret);
+}
-static long ok_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
- {
- long ret=1;
+static long
+ok_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
+{
+ long ret = 1;
- if (b->next_bio == NULL) return(0);
- switch (cmd)
- {
+ if (b->next_bio == NULL)
+ return (0);
+ switch (cmd) {
default:
- ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
+ ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
break;
- }
- return(ret);
}
+ return (ret);
+}
-static void longswap(void *_ptr, size_t len)
+static void
+longswap(void *_ptr, size_t len)
{
if (_BYTE_ORDER == _LITTLE_ENDIAN) {
size_t i;
- unsigned char *p=_ptr,c;
+ unsigned char *p = _ptr, c;
- for(i= 0;i < len;i+= 4) {
- c=p[0],p[0]=p[3],p[3]=c;
- c=p[1],p[1]=p[2],p[2]=c;
+ for (i = 0; i < len; i += 4) {
+ c = p[0], p[0] = p[3], p[3] = c;
+ c = p[1], p[1] = p[2], p[2] = c;
}
}
}
-static int sig_out(BIO* b)
- {
+static int
+sig_out(BIO* b)
+{
BIO_OK_CTX *ctx;
EVP_MD_CTX *md;
- ctx=b->ptr;
- md=&ctx->md;
+ ctx = b->ptr;
+ md = &ctx->md;
- if(ctx->buf_len+ 2* md->digest->md_size > OK_BLOCK_SIZE) return 1;
+ if (ctx->buf_len + 2* md->digest->md_size > OK_BLOCK_SIZE)
+ return 1;
if (!EVP_DigestInit_ex(md, md->digest, NULL))
goto berr;
RAND_pseudo_bytes(md->md_data, md->digest->md_size);
memcpy(&(ctx->buf[ctx->buf_len]), md->md_data, md->digest->md_size);
longswap(&(ctx->buf[ctx->buf_len]), md->digest->md_size);
- ctx->buf_len+= md->digest->md_size;
+ ctx->buf_len += md->digest->md_size;
if (!EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN)))
goto berr;
if (!EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL))
goto berr;
- ctx->buf_len+= md->digest->md_size;
- ctx->blockout= 1;
- ctx->sigio= 0;
+ ctx->buf_len += md->digest->md_size;
+ ctx->blockout = 1;
+ ctx->sigio = 0;
return 1;
- berr:
+berr:
BIO_clear_retry_flags(b);
return 0;
- }
+}
-static int sig_in(BIO* b)
- {
+static int
+sig_in(BIO* b)
+{
BIO_OK_CTX *ctx;
EVP_MD_CTX *md;
unsigned char tmp[EVP_MAX_MD_SIZE];
- int ret= 0;
+ int ret = 0;
- ctx=b->ptr;
- md=&ctx->md;
+ ctx = b->ptr;
+ md = &ctx->md;
- if((int)(ctx->buf_len-ctx->buf_off) < 2*md->digest->md_size) return 1;
+ if ((int)(ctx->buf_len - ctx->buf_off) < 2*md->digest->md_size)
+ return 1;
if (!EVP_DigestInit_ex(md, md->digest, NULL))
goto berr;
memcpy(md->md_data, &(ctx->buf[ctx->buf_off]), md->digest->md_size);
longswap(md->md_data, md->digest->md_size);
- ctx->buf_off+= md->digest->md_size;
+ ctx->buf_off += md->digest->md_size;
if (!EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN)))
goto berr;
if (!EVP_DigestFinal_ex(md, tmp, NULL))
goto berr;
- ret= memcmp(&(ctx->buf[ctx->buf_off]), tmp, md->digest->md_size) == 0;
- ctx->buf_off+= md->digest->md_size;
- if(ret == 1)
- {
- ctx->sigio= 0;
- if(ctx->buf_len != ctx->buf_off)
- {
- memmove(ctx->buf, &(ctx->buf[ctx->buf_off]), ctx->buf_len- ctx->buf_off);
- }
- ctx->buf_len-= ctx->buf_off;
- ctx->buf_off= 0;
- }
- else
- {
- ctx->cont= 0;
+ ret = memcmp(&(ctx->buf[ctx->buf_off]), tmp, md->digest->md_size) == 0;
+ ctx->buf_off += md->digest->md_size;
+ if (ret == 1) {
+ ctx->sigio = 0;
+ if (ctx->buf_len != ctx->buf_off) {
+ memmove(ctx->buf, &(ctx->buf[ctx->buf_off]),
+ ctx->buf_len - ctx->buf_off);
}
+ ctx->buf_len -= ctx->buf_off;
+ ctx->buf_off = 0;
+ } else {
+ ctx->cont = 0;
+ }
return 1;
- berr:
+berr:
BIO_clear_retry_flags(b);
return 0;
- }
+}
-static int block_out(BIO* b)
- {
+static int
+block_out(BIO* b)
+{
BIO_OK_CTX *ctx;
EVP_MD_CTX *md;
unsigned long tl;
- ctx=b->ptr;
- md=&ctx->md;
+ ctx = b->ptr;
+ md = &ctx->md;
- tl= ctx->buf_len- OK_BLOCK_BLOCK;
- ctx->buf[0]=(unsigned char)(tl>>24);
- ctx->buf[1]=(unsigned char)(tl>>16);
- ctx->buf[2]=(unsigned char)(tl>>8);
- ctx->buf[3]=(unsigned char)(tl);
+ tl = ctx->buf_len - OK_BLOCK_BLOCK;
+ ctx->buf[0] = (unsigned char)(tl >> 24);
+ ctx->buf[1] = (unsigned char)(tl >> 16);
+ ctx->buf[2] = (unsigned char)(tl >> 8);
+ ctx->buf[3] = (unsigned char)(tl);
if (!EVP_DigestUpdate(md,
- (unsigned char*) &(ctx->buf[OK_BLOCK_BLOCK]), tl))
+ (unsigned char*) &(ctx->buf[OK_BLOCK_BLOCK]), tl))
goto berr;
if (!EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL))
goto berr;
- ctx->buf_len+= md->digest->md_size;
- ctx->blockout= 1;
+ ctx->buf_len += md->digest->md_size;
+ ctx->blockout = 1;
return 1;
- berr:
+berr:
BIO_clear_retry_flags(b);
return 0;
- }
+}
-static int block_in(BIO* b)
- {
+static int
+block_in(BIO* b)
+{
BIO_OK_CTX *ctx;
EVP_MD_CTX *md;
- unsigned long tl= 0;
+ unsigned long tl = 0;
unsigned char tmp[EVP_MAX_MD_SIZE];
- ctx=b->ptr;
- md=&ctx->md;
+ ctx = b->ptr;
+ md = &ctx->md;
- assert(sizeof(tl)>=OK_BLOCK_BLOCK); /* always true */
- tl =ctx->buf[0]; tl<<=8;
- tl|=ctx->buf[1]; tl<<=8;
- tl|=ctx->buf[2]; tl<<=8;
- tl|=ctx->buf[3];
+ assert(sizeof(tl) >= OK_BLOCK_BLOCK); /* always true */
+ tl = ctx->buf[0];
+ tl <<= 8;
+ tl |= ctx->buf[1];
+ tl <<= 8;
+ tl |= ctx->buf[2];
+ tl <<= 8;
+ tl |= ctx->buf[3];
+
+ if (ctx->buf_len < tl + OK_BLOCK_BLOCK + md->digest->md_size)
+ return 1;
- if (ctx->buf_len < tl+ OK_BLOCK_BLOCK+ md->digest->md_size) return 1;
-
if (!EVP_DigestUpdate(md,
- (unsigned char*) &(ctx->buf[OK_BLOCK_BLOCK]), tl))
+ (unsigned char*) &(ctx->buf[OK_BLOCK_BLOCK]), tl))
goto berr;
if (!EVP_DigestFinal_ex(md, tmp, NULL))
goto berr;
- if(memcmp(&(ctx->buf[tl+ OK_BLOCK_BLOCK]), tmp, md->digest->md_size) == 0)
- {
+ if (memcmp(&(ctx->buf[tl + OK_BLOCK_BLOCK]), tmp,
+ md->digest->md_size) == 0) {
/* there might be parts from next block lurking around ! */
- ctx->buf_off_save= tl+ OK_BLOCK_BLOCK+ md->digest->md_size;
- ctx->buf_len_save= ctx->buf_len;
- ctx->buf_off= OK_BLOCK_BLOCK;
- ctx->buf_len= tl+ OK_BLOCK_BLOCK;
- ctx->blockout= 1;
- }
- else
- {
- ctx->cont= 0;
- }
+ ctx->buf_off_save = tl + OK_BLOCK_BLOCK + md->digest->md_size;
+ ctx->buf_len_save = ctx->buf_len;
+ ctx->buf_off = OK_BLOCK_BLOCK;
+ ctx->buf_len = tl + OK_BLOCK_BLOCK;
+ ctx->blockout = 1;
+ } else {
+ ctx->cont = 0;
+ }
return 1;
- berr:
+
+berr:
BIO_clear_retry_flags(b);
return 0;
- }
-
+}