Make BIO opaque

Move the the BIO_METHOD and BIO structures into internal header files,
provide appropriate accessor methods and update all internal code to use
the new accessors where appropriate.

Reviewed-by: Richard Levitte <levitte@openssl.org>
This commit is contained in:
Matt Caswell
2016-03-22 09:21:29 +00:00
parent f334461fac
commit a146ae55ba
22 changed files with 534 additions and 316 deletions

View File

@@ -60,6 +60,7 @@
#include <openssl/asn1.h> #include <openssl/asn1.h>
#include <openssl/asn1t.h> #include <openssl/asn1t.h>
#include "internal/evp_int.h" #include "internal/evp_int.h"
#include "internal/bio.h"
#include "asn1_locl.h" #include "asn1_locl.h"
/* /*

View File

@@ -63,7 +63,7 @@
*/ */
#include <string.h> #include <string.h>
#include <openssl/bio.h> #include <internal/bio.h>
#include <openssl/asn1.h> #include <openssl/asn1.h>
/* Must be large enough for biggest tag+length */ /* Must be large enough for biggest tag+length */
@@ -152,9 +152,9 @@ static int asn1_bio_new(BIO *b)
OPENSSL_free(ctx); OPENSSL_free(ctx);
return 0; return 0;
} }
b->init = 1; BIO_set_data(b, ctx);
b->ptr = (char *)ctx; BIO_set_init(b, 1);
b->flags = 0;
return 1; return 1;
} }
@@ -178,15 +178,20 @@ static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size)
static int asn1_bio_free(BIO *b) static int asn1_bio_free(BIO *b)
{ {
BIO_ASN1_BUF_CTX *ctx = (BIO_ASN1_BUF_CTX *)b->ptr; BIO_ASN1_BUF_CTX *ctx;
if (b == NULL)
return 0;
ctx = BIO_get_data(b);
if (ctx == NULL) if (ctx == NULL)
return 0; return 0;
OPENSSL_free(ctx->buf); OPENSSL_free(ctx->buf);
OPENSSL_free(ctx); OPENSSL_free(ctx);
b->init = 0; BIO_set_data(b, NULL);
b->ptr = NULL; BIO_set_init(b, 0);
b->flags = 0;
return 1; return 1;
} }
@@ -195,10 +200,11 @@ static int asn1_bio_write(BIO *b, const char *in, int inl)
BIO_ASN1_BUF_CTX *ctx; BIO_ASN1_BUF_CTX *ctx;
int wrmax, wrlen, ret; int wrmax, wrlen, ret;
unsigned char *p; unsigned char *p;
if (!in || (inl < 0) || (b->next_bio == NULL)) BIO *next;
return 0;
ctx = (BIO_ASN1_BUF_CTX *)b->ptr; ctx = BIO_get_data(b);
if (ctx == NULL) next = BIO_next(b);
if (in == NULL || inl < 0 || ctx == NULL || next == NULL)
return 0; return 0;
wrlen = 0; wrlen = 0;
@@ -236,7 +242,7 @@ static int asn1_bio_write(BIO *b, const char *in, int inl)
break; break;
case ASN1_STATE_HEADER_COPY: case ASN1_STATE_HEADER_COPY:
ret = BIO_write(b->next_bio, ctx->buf + ctx->bufpos, ctx->buflen); ret = BIO_write(next, ctx->buf + ctx->bufpos, ctx->buflen);
if (ret <= 0) if (ret <= 0)
goto done; goto done;
@@ -256,7 +262,7 @@ static int asn1_bio_write(BIO *b, const char *in, int inl)
wrmax = ctx->copylen; wrmax = ctx->copylen;
else else
wrmax = inl; wrmax = inl;
ret = BIO_write(b->next_bio, in, wrmax); ret = BIO_write(next, in, wrmax);
if (ret <= 0) if (ret <= 0)
break; break;
wrlen += ret; wrlen += ret;
@@ -292,10 +298,11 @@ static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
asn1_ps_func *cleanup, asn1_bio_state_t next) asn1_ps_func *cleanup, asn1_bio_state_t next)
{ {
int ret; int ret;
if (ctx->ex_len <= 0) if (ctx->ex_len <= 0)
return 1; return 1;
for (;;) { for (;;) {
ret = BIO_write(b->next_bio, ctx->ex_buf + ctx->ex_pos, ctx->ex_len); ret = BIO_write(BIO_next(b), ctx->ex_buf + ctx->ex_pos, ctx->ex_len);
if (ret <= 0) if (ret <= 0)
break; break;
ctx->ex_len -= ret; ctx->ex_len -= ret;
@@ -330,9 +337,10 @@ static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
static int asn1_bio_read(BIO *b, char *in, int inl) static int asn1_bio_read(BIO *b, char *in, int inl)
{ {
if (!b->next_bio) BIO *next = BIO_next(b);
if (next == NULL)
return 0; return 0;
return BIO_read(b->next_bio, in, inl); return BIO_read(next, in, inl);
} }
static int asn1_bio_puts(BIO *b, const char *str) static int asn1_bio_puts(BIO *b, const char *str)
@@ -342,16 +350,18 @@ static int asn1_bio_puts(BIO *b, const char *str)
static int asn1_bio_gets(BIO *b, char *str, int size) static int asn1_bio_gets(BIO *b, char *str, int size)
{ {
if (!b->next_bio) BIO *next = BIO_next(b);
if (next == NULL)
return 0; return 0;
return BIO_gets(b->next_bio, str, size); return BIO_gets(next, str, size);
} }
static long asn1_bio_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) static long asn1_bio_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
{ {
if (b->next_bio == NULL) BIO *next = BIO_next(b);
return (0); if (next == NULL)
return BIO_callback_ctrl(b->next_bio, cmd, fp); return 0;
return BIO_callback_ctrl(next, cmd, fp);
} }
static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2) static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
@@ -359,9 +369,12 @@ static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
BIO_ASN1_BUF_CTX *ctx; BIO_ASN1_BUF_CTX *ctx;
BIO_ASN1_EX_FUNCS *ex_func; BIO_ASN1_EX_FUNCS *ex_func;
long ret = 1; long ret = 1;
ctx = (BIO_ASN1_BUF_CTX *)b->ptr; BIO *next;
ctx = BIO_get_data(b);
if (ctx == NULL) if (ctx == NULL)
return 0; return 0;
next = BIO_next(b);
switch (cmd) { switch (cmd) {
case BIO_C_SET_PREFIX: case BIO_C_SET_PREFIX:
@@ -397,7 +410,7 @@ static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
break; break;
case BIO_CTRL_FLUSH: case BIO_CTRL_FLUSH:
if (!b->next_bio) if (next == NULL)
return 0; return 0;
/* Call post function if possible */ /* Call post function if possible */
@@ -415,16 +428,16 @@ static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
} }
if (ctx->state == ASN1_STATE_DONE) if (ctx->state == ASN1_STATE_DONE)
return BIO_ctrl(b->next_bio, cmd, arg1, arg2); return BIO_ctrl(next, cmd, arg1, arg2);
else { else {
BIO_clear_retry_flags(b); BIO_clear_retry_flags(b);
return 0; return 0;
} }
default: default:
if (!b->next_bio) if (next == NULL)
return 0; return 0;
return BIO_ctrl(b->next_bio, cmd, arg1, arg2); return BIO_ctrl(next, cmd, arg1, arg2);
} }

View File

@@ -57,8 +57,8 @@
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include "bio_lcl.h"
#include "internal/cryptlib.h" #include "internal/cryptlib.h"
#include <openssl/bio.h>
static int buffer_write(BIO *h, const char *buf, int num); static int buffer_write(BIO *h, const char *buf, int num);
static int buffer_read(BIO *h, char *buf, int size); static int buffer_read(BIO *h, char *buf, int size);

View File

@@ -57,9 +57,9 @@
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include "bio_lcl.h"
#include "internal/cryptlib.h" #include "internal/cryptlib.h"
#include <openssl/rand.h> #include <openssl/rand.h>
#include <openssl/bio.h>
/* /*
* BIO_put and BIO_get both add to the digest, BIO_gets returns the digest * BIO_put and BIO_get both add to the digest, BIO_gets returns the digest

View File

@@ -57,8 +57,8 @@
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include "bio_lcl.h"
#include "internal/cryptlib.h" #include "internal/cryptlib.h"
#include <openssl/bio.h>
/* /*
* BIO_put and BIO_get both add to the digest, BIO_gets returns the digest * BIO_put and BIO_get both add to the digest, BIO_gets returns the digest

View File

@@ -58,8 +58,8 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include "bio_lcl.h"
#include "internal/cryptlib.h" #include "internal/cryptlib.h"
#include <openssl/bio.h>
#include <openssl/err.h> #include <openssl/err.h>
long BIO_debug_callback(BIO *bio, int cmd, const char *argp, long BIO_debug_callback(BIO *bio, int cmd, const char *argp,

View File

@@ -65,7 +65,50 @@ union bio_addr_st {
/* END BIO_ADDRINFO/BIO_ADDR stuff. */ /* END BIO_ADDRINFO/BIO_ADDR stuff. */
#include "internal/cryptlib.h" #include "internal/cryptlib.h"
#include <openssl/bio.h> #include <internal/bio.h>
typedef struct bio_f_buffer_ctx_struct {
/*-
* Buffers are setup like this:
*
* <---------------------- size ----------------------->
* +---------------------------------------------------+
* | consumed | remaining | free space |
* +---------------------------------------------------+
* <-- off --><------- len ------->
*/
/*- BIO *bio; *//*
* this is now in the BIO struct
*/
int ibuf_size; /* how big is the input buffer */
int obuf_size; /* how big is the output buffer */
char *ibuf; /* the char array */
int ibuf_len; /* how many bytes are in it */
int ibuf_off; /* write/read offset */
char *obuf; /* the char array */
int obuf_len; /* how many bytes are in it */
int obuf_off; /* write/read offset */
} BIO_F_BUFFER_CTX;
struct bio_st {
const BIO_METHOD *method;
/* bio, mode, argp, argi, argl, ret */
long (*callback) (struct bio_st *, int, const char *, int, long, long);
char *cb_arg; /* first argument for the callback */
int init;
int shutdown;
int flags; /* extra storage */
int retry_reason;
int num;
void *ptr;
struct bio_st *next_bio; /* used by filter BIOs */
struct bio_st *prev_bio; /* used by filter BIOs */
int references;
uint64_t num_read;
uint64_t num_write;
CRYPTO_EX_DATA ex_data;
CRYPTO_RWLOCK *lock;
};
#ifndef OPENSSL_NO_SOCK #ifndef OPENSSL_NO_SOCK
# ifdef OPENSSL_SYS_VMS # ifdef OPENSSL_SYS_VMS

View File

@@ -58,8 +58,8 @@
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include <openssl/crypto.h> #include <openssl/crypto.h>
#include "bio_lcl.h"
#include "internal/cryptlib.h" #include "internal/cryptlib.h"
#include <openssl/bio.h>
#include <openssl/stack.h> #include <openssl/stack.h>
BIO *BIO_new(const BIO_METHOD *method) BIO *BIO_new(const BIO_METHOD *method)
@@ -142,6 +142,36 @@ int BIO_free(BIO *a)
return 1; return 1;
} }
void BIO_set_data(BIO *a, void *ptr)
{
a->ptr = ptr;
}
void *BIO_get_data(BIO *a)
{
return a->ptr;
}
void BIO_set_init(BIO *a, int init)
{
a->init = init;
}
int BIO_get_init(BIO *a)
{
return a->init;
}
void BIO_set_shutdown(BIO *a, int shut)
{
a->shutdown = shut;
}
int BIO_get_shutdown(BIO *a)
{
return a->shutdown;
}
void BIO_vfree(BIO *a) void BIO_vfree(BIO *a)
{ {
BIO_free(a); BIO_free(a);
@@ -487,6 +517,11 @@ int BIO_get_retry_reason(BIO *bio)
return (bio->retry_reason); return (bio->retry_reason);
} }
void BIO_set_retry_reason(BIO *bio, int reason)
{
bio->retry_reason = reason;
}
BIO *BIO_find_type(BIO *bio, int type) BIO *BIO_find_type(BIO *bio, int type)
{ {
int mt, mask; int mt, mask;
@@ -516,6 +551,11 @@ BIO *BIO_next(BIO *b)
return b->next_bio; return b->next_bio;
} }
void BIO_set_next(BIO *b, BIO *next)
{
b->next_bio = next;
}
void BIO_free_all(BIO *bio) void BIO_free_all(BIO *bio)
{ {
BIO *b; BIO *b;

View File

@@ -82,13 +82,13 @@ int BIO_meth_set_write(BIO_METHOD *biom,
return 1; return 1;
} }
int (*BIO_meth_get_read(BIO_METHOD *biom)) (BIO *, const char *, int) int (*BIO_meth_get_read(BIO_METHOD *biom)) (BIO *, char *, int)
{ {
return biom->bread; return biom->bread;
} }
int BIO_meth_set_read(BIO_METHOD *biom, int BIO_meth_set_read(BIO_METHOD *biom,
int (*read) (BIO *, const char *, int)) int (*read) (BIO *, char *, int))
{ {
biom->bread = read; biom->bread = read;
return 1; return 1;
@@ -108,7 +108,7 @@ int BIO_meth_set_puts(BIO_METHOD *biom,
int (*BIO_meth_get_gets(BIO_METHOD *biom)) (BIO *, char *, int) int (*BIO_meth_get_gets(BIO_METHOD *biom)) (BIO *, char *, int)
{ {
return biom->gets; return biom->bgets;
} }
int BIO_meth_set_gets(BIO_METHOD *biom, int BIO_meth_set_gets(BIO_METHOD *biom,
@@ -130,7 +130,7 @@ int BIO_meth_set_ctrl(BIO_METHOD *biom,
return 1; return 1;
} }
int (*BIO_meth_get_create(BIO_METHOD *bion)) (BIO *) int (*BIO_meth_get_create(BIO_METHOD *biom)) (BIO *)
{ {
return biom->create; return biom->create;
} }

View File

@@ -65,7 +65,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <openssl/bio.h> #include "bio_lcl.h"
#include <openssl/err.h> #include <openssl/err.h>
#include <openssl/crypto.h> #include <openssl/crypto.h>

View File

@@ -64,6 +64,7 @@
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include "bio_lcl.h"
#include "internal/cryptlib.h" #include "internal/cryptlib.h"
#if defined(OPENSSL_SYS_WINCE) #if defined(OPENSSL_SYS_WINCE)

View File

@@ -57,8 +57,8 @@
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include "bio_lcl.h"
#include "internal/cryptlib.h" #include "internal/cryptlib.h"
#include <openssl/bio.h>
static int mem_write(BIO *h, const char *buf, int num); static int mem_write(BIO *h, const char *buf, int num);
static int mem_read(BIO *h, char *buf, int size); static int mem_read(BIO *h, char *buf, int size);

View File

@@ -57,8 +57,8 @@
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include "bio_lcl.h"
#include "internal/cryptlib.h" #include "internal/cryptlib.h"
#include <openssl/bio.h>
static int null_write(BIO *h, const char *buf, int num); static int null_write(BIO *h, const char *buf, int num);
static int null_read(BIO *h, char *buf, int size); static int null_read(BIO *h, char *buf, int size);

View File

@@ -58,6 +58,7 @@
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#define USE_SOCKETS #define USE_SOCKETS
#include "bio_lcl.h"
#include "internal/cryptlib.h" #include "internal/cryptlib.h"
#ifndef OPENSSL_NO_SOCK #ifndef OPENSSL_NO_SOCK

View File

@@ -60,6 +60,7 @@
#include "internal/cryptlib.h" #include "internal/cryptlib.h"
#include <openssl/buffer.h> #include <openssl/buffer.h>
#include <openssl/evp.h> #include <openssl/evp.h>
#include "internal/bio.h"
static int b64_write(BIO *h, const char *buf, int num); static int b64_write(BIO *h, const char *buf, int num);
static int b64_read(BIO *h, char *buf, int size); static int b64_read(BIO *h, char *buf, int size);
@@ -105,9 +106,10 @@ static const BIO_METHOD methods_b64 = {
b64_callback_ctrl, b64_callback_ctrl,
}; };
const BIO_METHOD *BIO_f_base64(void) const BIO_METHOD *BIO_f_base64(void)
{ {
return (&methods_b64); return &methods_b64;
} }
static int b64_new(BIO *bi) static int b64_new(BIO *bi)
@@ -121,23 +123,28 @@ static int b64_new(BIO *bi)
ctx->cont = 1; ctx->cont = 1;
ctx->start = 1; ctx->start = 1;
ctx->base64 = EVP_ENCODE_CTX_new(); ctx->base64 = EVP_ENCODE_CTX_new();
bi->init = 1; BIO_set_data(bi, ctx);
bi->ptr = (char *)ctx; BIO_set_init(bi, 1);
bi->flags = 0;
bi->num = 0; return 1;
return (1);
} }
static int b64_free(BIO *a) static int b64_free(BIO *a)
{ {
BIO_B64_CTX *ctx;
if (a == NULL) if (a == NULL)
return (0); return 0;
EVP_ENCODE_CTX_free(((BIO_B64_CTX *)a->ptr)->base64);
OPENSSL_free(a->ptr); ctx = BIO_get_data(a);
a->ptr = NULL; if (ctx == NULL)
a->init = 0; return 0;
a->flags = 0;
return (1); EVP_ENCODE_CTX_free(ctx->base64);
OPENSSL_free(ctx);
BIO_set_data(a, NULL);
BIO_set_init(a, 0);
return 1;
} }
static int b64_read(BIO *b, char *out, int outl) static int b64_read(BIO *b, char *out, int outl)
@@ -145,13 +152,15 @@ static int b64_read(BIO *b, char *out, int outl)
int ret = 0, i, ii, j, k, x, n, num, ret_code = 0; int ret = 0, i, ii, j, k, x, n, num, ret_code = 0;
BIO_B64_CTX *ctx; BIO_B64_CTX *ctx;
unsigned char *p, *q; unsigned char *p, *q;
BIO *next;
if (out == NULL) if (out == NULL)
return (0); return (0);
ctx = (BIO_B64_CTX *)b->ptr; ctx = (BIO_B64_CTX *)BIO_get_data(b);
if ((ctx == NULL) || (b->next_bio == NULL)) next = BIO_next(b);
return (0); if ((ctx == NULL) || (next == NULL))
return 0;
BIO_clear_retry_flags(b); BIO_clear_retry_flags(b);
@@ -191,14 +200,14 @@ static int b64_read(BIO *b, char *out, int outl)
if (ctx->cont <= 0) if (ctx->cont <= 0)
break; break;
i = BIO_read(b->next_bio, &(ctx->tmp[ctx->tmp_len]), i = BIO_read(next, &(ctx->tmp[ctx->tmp_len]),
B64_BLOCK_SIZE - ctx->tmp_len); B64_BLOCK_SIZE - ctx->tmp_len);
if (i <= 0) { if (i <= 0) {
ret_code = i; ret_code = i;
/* Should we continue next time we are called? */ /* Should we continue next time we are called? */
if (!BIO_should_retry(b->next_bio)) { if (!BIO_should_retry(next)) {
ctx->cont = i; ctx->cont = i;
/* If buffer empty break */ /* If buffer empty break */
if (ctx->tmp_len == 0) if (ctx->tmp_len == 0)
@@ -354,8 +363,13 @@ static int b64_write(BIO *b, const char *in, int inl)
int n; int n;
int i; int i;
BIO_B64_CTX *ctx; BIO_B64_CTX *ctx;
BIO *next;
ctx = (BIO_B64_CTX *)BIO_get_data(b);
next = BIO_next(b);
if ((ctx == NULL) || (next == NULL))
return 0;
ctx = (BIO_B64_CTX *)b->ptr;
BIO_clear_retry_flags(b); BIO_clear_retry_flags(b);
if (ctx->encode != B64_ENCODE) { if (ctx->encode != B64_ENCODE) {
@@ -371,7 +385,7 @@ static int b64_write(BIO *b, const char *in, int inl)
OPENSSL_assert(ctx->buf_len >= ctx->buf_off); OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
n = ctx->buf_len - ctx->buf_off; n = ctx->buf_len - ctx->buf_off;
while (n > 0) { while (n > 0) {
i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n); i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
if (i <= 0) { if (i <= 0) {
BIO_copy_next_retry(b); BIO_copy_next_retry(b);
return (i); return (i);
@@ -445,7 +459,7 @@ static int b64_write(BIO *b, const char *in, int inl)
ctx->buf_off = 0; ctx->buf_off = 0;
n = ctx->buf_len; n = ctx->buf_len;
while (n > 0) { while (n > 0) {
i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n); i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
if (i <= 0) { if (i <= 0) {
BIO_copy_next_retry(b); BIO_copy_next_retry(b);
return ((ret == 0) ? i : ret); return ((ret == 0) ? i : ret);
@@ -467,21 +481,25 @@ static long b64_ctrl(BIO *b, int cmd, long num, void *ptr)
BIO_B64_CTX *ctx; BIO_B64_CTX *ctx;
long ret = 1; long ret = 1;
int i; int i;
BIO *next;
ctx = (BIO_B64_CTX *)b->ptr; ctx = (BIO_B64_CTX *)BIO_get_data(b);
next = BIO_next(b);
if ((ctx == NULL) || (next == NULL))
return 0;
switch (cmd) { switch (cmd) {
case BIO_CTRL_RESET: case BIO_CTRL_RESET:
ctx->cont = 1; ctx->cont = 1;
ctx->start = 1; ctx->start = 1;
ctx->encode = B64_NONE; ctx->encode = B64_NONE;
ret = BIO_ctrl(b->next_bio, cmd, num, ptr); ret = BIO_ctrl(next, cmd, num, ptr);
break; break;
case BIO_CTRL_EOF: /* More to read */ case BIO_CTRL_EOF: /* More to read */
if (ctx->cont <= 0) if (ctx->cont <= 0)
ret = 1; ret = 1;
else else
ret = BIO_ctrl(b->next_bio, cmd, num, ptr); ret = BIO_ctrl(next, cmd, num, ptr);
break; break;
case BIO_CTRL_WPENDING: /* More to write in buffer */ case BIO_CTRL_WPENDING: /* More to write in buffer */
OPENSSL_assert(ctx->buf_len >= ctx->buf_off); OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
@@ -490,13 +508,13 @@ static long b64_ctrl(BIO *b, int cmd, long num, void *ptr)
&& (EVP_ENCODE_CTX_num(ctx->base64) != 0)) && (EVP_ENCODE_CTX_num(ctx->base64) != 0))
ret = 1; ret = 1;
else if (ret <= 0) else if (ret <= 0)
ret = BIO_ctrl(b->next_bio, cmd, num, ptr); ret = BIO_ctrl(next, cmd, num, ptr);
break; break;
case BIO_CTRL_PENDING: /* More to read in buffer */ case BIO_CTRL_PENDING: /* More to read in buffer */
OPENSSL_assert(ctx->buf_len >= ctx->buf_off); 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) if (ret <= 0)
ret = BIO_ctrl(b->next_bio, cmd, num, ptr); ret = BIO_ctrl(next, cmd, num, ptr);
break; break;
case BIO_CTRL_FLUSH: case BIO_CTRL_FLUSH:
/* do a final write */ /* do a final write */
@@ -524,12 +542,12 @@ static long b64_ctrl(BIO *b, int cmd, long num, void *ptr)
goto again; goto again;
} }
/* Finally flush the underlying BIO */ /* Finally flush the underlying BIO */
ret = BIO_ctrl(b->next_bio, cmd, num, ptr); ret = BIO_ctrl(next, cmd, num, ptr);
break; break;
case BIO_C_DO_STATE_MACHINE: case BIO_C_DO_STATE_MACHINE:
BIO_clear_retry_flags(b); BIO_clear_retry_flags(b);
ret = BIO_ctrl(b->next_bio, cmd, num, ptr); ret = BIO_ctrl(next, cmd, num, ptr);
BIO_copy_next_retry(b); BIO_copy_next_retry(b);
break; break;
@@ -539,21 +557,22 @@ static long b64_ctrl(BIO *b, int cmd, long num, void *ptr)
case BIO_CTRL_GET: case BIO_CTRL_GET:
case BIO_CTRL_SET: case BIO_CTRL_SET:
default: default:
ret = BIO_ctrl(b->next_bio, cmd, num, ptr); ret = BIO_ctrl(next, cmd, num, ptr);
break; break;
} }
return (ret); return ret;
} }
static long b64_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) static long b64_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
{ {
long ret = 1; long ret = 1;
BIO *next = BIO_next(b);
if (b->next_bio == NULL) if (next == NULL)
return (0); return 0;
switch (cmd) { switch (cmd) {
default: default:
ret = BIO_callback_ctrl(b->next_bio, cmd, fp); ret = BIO_callback_ctrl(next, cmd, fp);
break; break;
} }
return (ret); return (ret);

View File

@@ -60,6 +60,7 @@
#include "internal/cryptlib.h" #include "internal/cryptlib.h"
#include <openssl/buffer.h> #include <openssl/buffer.h>
#include <openssl/evp.h> #include <openssl/evp.h>
#include "internal/bio.h"
static int enc_write(BIO *h, const char *buf, int num); static int enc_write(BIO *h, const char *buf, int num);
static int enc_read(BIO *h, char *buf, int size); static int enc_read(BIO *h, char *buf, int size);
@@ -122,9 +123,9 @@ static int enc_new(BIO *bi)
} }
ctx->cont = 1; ctx->cont = 1;
ctx->ok = 1; ctx->ok = 1;
bi->init = 0; BIO_set_data(bi, ctx);
bi->ptr = (char *)ctx; BIO_set_init(bi, 1);
bi->flags = 0;
return 1; return 1;
} }
@@ -133,27 +134,33 @@ static int enc_free(BIO *a)
BIO_ENC_CTX *b; BIO_ENC_CTX *b;
if (a == NULL) if (a == NULL)
return (0); return 0;
b = (BIO_ENC_CTX *)a->ptr;
b = BIO_get_data(a);
if (b == NULL)
return 0;
EVP_CIPHER_CTX_free(b->cipher); EVP_CIPHER_CTX_free(b->cipher);
OPENSSL_clear_free(a->ptr, sizeof(BIO_ENC_CTX)); OPENSSL_clear_free(b, sizeof(BIO_ENC_CTX));
a->ptr = NULL; BIO_set_data(a, NULL);
a->init = 0; BIO_set_init(a, 0);
a->flags = 0;
return (1); return 1;
} }
static int enc_read(BIO *b, char *out, int outl) static int enc_read(BIO *b, char *out, int outl)
{ {
int ret = 0, i; int ret = 0, i;
BIO_ENC_CTX *ctx; BIO_ENC_CTX *ctx;
BIO *next;
if (out == NULL) if (out == NULL)
return (0); return (0);
ctx = (BIO_ENC_CTX *)b->ptr; ctx = BIO_get_data(b);
if ((ctx == NULL) || (b->next_bio == NULL)) next = BIO_next(b);
return (0); if ((ctx == NULL) || (next == NULL))
return 0;
/* First check if there are bytes decoded/encoded */ /* First check if there are bytes decoded/encoded */
if (ctx->buf_len > 0) { if (ctx->buf_len > 0) {
@@ -183,11 +190,11 @@ static int enc_read(BIO *b, char *out, int outl)
/* /*
* read in at IV offset, read the EVP_Cipher documentation about why * 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(next, &(ctx->buf[BUF_OFFSET]), ENC_BLOCK_SIZE);
if (i <= 0) { if (i <= 0) {
/* Should be continue next time we are called? */ /* Should be continue next time we are called? */
if (!BIO_should_retry(b->next_bio)) { if (!BIO_should_retry(next)) {
ctx->cont = i; ctx->cont = i;
i = EVP_CipherFinal_ex(ctx->cipher, i = EVP_CipherFinal_ex(ctx->cipher,
(unsigned char *)ctx->buf, (unsigned char *)ctx->buf,
@@ -239,14 +246,19 @@ static int enc_write(BIO *b, const char *in, int inl)
{ {
int ret = 0, n, i; int ret = 0, n, i;
BIO_ENC_CTX *ctx; BIO_ENC_CTX *ctx;
BIO *next;
ctx = BIO_get_data(b);
next = BIO_next(b);
if ((ctx == NULL) || (next == NULL))
return 0;
ctx = (BIO_ENC_CTX *)b->ptr;
ret = inl; ret = inl;
BIO_clear_retry_flags(b); BIO_clear_retry_flags(b);
n = ctx->buf_len - ctx->buf_off; n = ctx->buf_len - ctx->buf_off;
while (n > 0) { while (n > 0) {
i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n); i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
if (i <= 0) { if (i <= 0) {
BIO_copy_next_retry(b); BIO_copy_next_retry(b);
return (i); return (i);
@@ -274,7 +286,7 @@ static int enc_write(BIO *b, const char *in, int inl)
ctx->buf_off = 0; ctx->buf_off = 0;
n = ctx->buf_len; n = ctx->buf_len;
while (n > 0) { while (n > 0) {
i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n); i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
if (i <= 0) { if (i <= 0) {
BIO_copy_next_retry(b); BIO_copy_next_retry(b);
return (ret == inl) ? i : ret - inl; return (ret == inl) ? i : ret - inl;
@@ -296,8 +308,12 @@ static long enc_ctrl(BIO *b, int cmd, long num, void *ptr)
long ret = 1; long ret = 1;
int i; int i;
EVP_CIPHER_CTX **c_ctx; EVP_CIPHER_CTX **c_ctx;
BIO *next;
ctx = (BIO_ENC_CTX *)b->ptr; ctx = BIO_get_data(b);
next = BIO_next(b);
if (ctx == NULL)
return 0;
switch (cmd) { switch (cmd) {
case BIO_CTRL_RESET: case BIO_CTRL_RESET:
@@ -306,23 +322,23 @@ static long enc_ctrl(BIO *b, int cmd, long num, void *ptr)
if (!EVP_CipherInit_ex(ctx->cipher, NULL, NULL, NULL, NULL, if (!EVP_CipherInit_ex(ctx->cipher, NULL, NULL, NULL, NULL,
EVP_CIPHER_CTX_encrypting(ctx->cipher))) EVP_CIPHER_CTX_encrypting(ctx->cipher)))
return 0; return 0;
ret = BIO_ctrl(b->next_bio, cmd, num, ptr); ret = BIO_ctrl(next, cmd, num, ptr);
break; break;
case BIO_CTRL_EOF: /* More to read */ case BIO_CTRL_EOF: /* More to read */
if (ctx->cont <= 0) if (ctx->cont <= 0)
ret = 1; ret = 1;
else else
ret = BIO_ctrl(b->next_bio, cmd, num, ptr); ret = BIO_ctrl(next, cmd, num, ptr);
break; break;
case BIO_CTRL_WPENDING: case BIO_CTRL_WPENDING:
ret = ctx->buf_len - ctx->buf_off; ret = ctx->buf_len - ctx->buf_off;
if (ret <= 0) if (ret <= 0)
ret = BIO_ctrl(b->next_bio, cmd, num, ptr); ret = BIO_ctrl(next, cmd, num, ptr);
break; break;
case BIO_CTRL_PENDING: /* More to read in buffer */ 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) if (ret <= 0)
ret = BIO_ctrl(b->next_bio, cmd, num, ptr); ret = BIO_ctrl(next, cmd, num, ptr);
break; break;
case BIO_CTRL_FLUSH: case BIO_CTRL_FLUSH:
/* do a final write */ /* do a final write */
@@ -348,33 +364,33 @@ static long enc_ctrl(BIO *b, int cmd, long num, void *ptr)
} }
/* Finally flush the underlying BIO */ /* Finally flush the underlying BIO */
ret = BIO_ctrl(b->next_bio, cmd, num, ptr); ret = BIO_ctrl(next, cmd, num, ptr);
break; break;
case BIO_C_GET_CIPHER_STATUS: case BIO_C_GET_CIPHER_STATUS:
ret = (long)ctx->ok; ret = (long)ctx->ok;
break; break;
case BIO_C_DO_STATE_MACHINE: case BIO_C_DO_STATE_MACHINE:
BIO_clear_retry_flags(b); BIO_clear_retry_flags(b);
ret = BIO_ctrl(b->next_bio, cmd, num, ptr); ret = BIO_ctrl(next, cmd, num, ptr);
BIO_copy_next_retry(b); BIO_copy_next_retry(b);
break; break;
case BIO_C_GET_CIPHER_CTX: case BIO_C_GET_CIPHER_CTX:
c_ctx = (EVP_CIPHER_CTX **)ptr; c_ctx = (EVP_CIPHER_CTX **)ptr;
*c_ctx = ctx->cipher; *c_ctx = ctx->cipher;
b->init = 1; BIO_set_init(b, 1);
break; break;
case BIO_CTRL_DUP: case BIO_CTRL_DUP:
dbio = (BIO *)ptr; dbio = (BIO *)ptr;
dctx = (BIO_ENC_CTX *)dbio->ptr; dctx = BIO_get_data(dbio);
dctx->cipher = EVP_CIPHER_CTX_new(); dctx->cipher = EVP_CIPHER_CTX_new();
if (dctx->cipher == NULL) if (dctx->cipher == NULL)
return 0; return 0;
ret = EVP_CIPHER_CTX_copy(dctx->cipher, ctx->cipher); ret = EVP_CIPHER_CTX_copy(dctx->cipher, ctx->cipher);
if (ret) if (ret)
dbio->init = 1; BIO_set_init(dbio, 1);
break; break;
default: default:
ret = BIO_ctrl(b->next_bio, cmd, num, ptr); ret = BIO_ctrl(next, cmd, num, ptr);
break; break;
} }
return (ret); return (ret);
@@ -383,12 +399,13 @@ static long enc_ctrl(BIO *b, int cmd, long num, void *ptr)
static long enc_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) static long enc_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
{ {
long ret = 1; long ret = 1;
BIO *next = BIO_next(b);
if (b->next_bio == NULL) if (next == NULL)
return (0); return (0);
switch (cmd) { switch (cmd) {
default: default:
ret = BIO_callback_ctrl(b->next_bio, cmd, fp); ret = BIO_callback_ctrl(next, cmd, fp);
break; break;
} }
return (ret); return (ret);
@@ -418,22 +435,25 @@ int BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k,
const unsigned char *i, int e) const unsigned char *i, int e)
{ {
BIO_ENC_CTX *ctx; BIO_ENC_CTX *ctx;
long (*callback) (struct bio_st *, int, const char *, int, long, long);
if (b == NULL) ctx = BIO_get_data(b);
if (ctx == NULL)
return 0; return 0;
if ((b->callback != NULL) && callback = BIO_get_callback(b);
(b->callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 0L) <=
0)) if ((callback != NULL) &&
(callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e,
0L) <= 0))
return 0; return 0;
b->init = 1; BIO_set_init(b, 1);
ctx = (BIO_ENC_CTX *)b->ptr;
if (!EVP_CipherInit_ex(ctx->cipher, c, NULL, k, i, e)) if (!EVP_CipherInit_ex(ctx->cipher, c, NULL, k, i, e))
return 0; return 0;
if (b->callback != NULL) if (callback != NULL)
return b->callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, return callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 1L);
1L);
return 1; return 1;
} }

View File

@@ -62,6 +62,7 @@
#include <openssl/evp.h> #include <openssl/evp.h>
#include "internal/evp_int.h" #include "internal/evp_int.h"
#include "evp_locl.h" #include "evp_locl.h"
#include "internal/bio.h"
/* /*
* BIO_put and BIO_get both add to the digest, BIO_gets returns the digest * BIO_put and BIO_get both add to the digest, BIO_gets returns the digest
@@ -103,37 +104,40 @@ static int md_new(BIO *bi)
if (ctx == NULL) if (ctx == NULL)
return (0); return (0);
bi->init = 0; BIO_set_init(bi, 1);
bi->ptr = (char *)ctx; BIO_set_data(bi, ctx);
bi->flags = 0;
return (1); return 1;
} }
static int md_free(BIO *a) static int md_free(BIO *a)
{ {
if (a == NULL) if (a == NULL)
return (0); return (0);
EVP_MD_CTX_free(a->ptr); EVP_MD_CTX_free(BIO_get_data(a));
a->ptr = NULL; BIO_set_data(a, NULL);
a->init = 0; BIO_set_init(a, 0);
a->flags = 0;
return (1); return 1;
} }
static int md_read(BIO *b, char *out, int outl) static int md_read(BIO *b, char *out, int outl)
{ {
int ret = 0; int ret = 0;
EVP_MD_CTX *ctx; EVP_MD_CTX *ctx;
BIO *next;
if (out == NULL) if (out == NULL)
return (0); return (0);
ctx = b->ptr;
if ((ctx == NULL) || (b->next_bio == NULL)) ctx = BIO_get_data(b);
next = BIO_next(b);
if ((ctx == NULL) || (next == NULL))
return (0); return (0);
ret = BIO_read(b->next_bio, out, outl); ret = BIO_read(next, out, outl);
if (b->init) { if (BIO_get_init(b)) {
if (ret > 0) { if (ret > 0) {
if (EVP_DigestUpdate(ctx, (unsigned char *)out, if (EVP_DigestUpdate(ctx, (unsigned char *)out,
(unsigned int)ret) <= 0) (unsigned int)ret) <= 0)
@@ -149,14 +153,17 @@ static int md_write(BIO *b, const char *in, int inl)
{ {
int ret = 0; int ret = 0;
EVP_MD_CTX *ctx; EVP_MD_CTX *ctx;
BIO *next;
if ((in == NULL) || (inl <= 0)) if ((in == NULL) || (inl <= 0))
return (0); return 0;
ctx = b->ptr;
if ((ctx != NULL) && (b->next_bio != NULL)) ctx = BIO_get_data(b);
ret = BIO_write(b->next_bio, in, inl); next = BIO_next(b);
if (b->init) { if ((ctx != NULL) && (next != NULL))
ret = BIO_write(next, in, inl);
if (BIO_get_init(b)) {
if (ret > 0) { if (ret > 0) {
if (!EVP_DigestUpdate(ctx, (const unsigned char *)in, if (!EVP_DigestUpdate(ctx, (const unsigned char *)in,
(unsigned int)ret)) { (unsigned int)ret)) {
@@ -165,11 +172,11 @@ static int md_write(BIO *b, const char *in, int inl)
} }
} }
} }
if (b->next_bio != NULL) { if (next != NULL) {
BIO_clear_retry_flags(b); BIO_clear_retry_flags(b);
BIO_copy_next_retry(b); BIO_copy_next_retry(b);
} }
return (ret); return ret;
} }
static long md_ctrl(BIO *b, int cmd, long num, void *ptr) static long md_ctrl(BIO *b, int cmd, long num, void *ptr)
@@ -178,21 +185,23 @@ static long md_ctrl(BIO *b, int cmd, long num, void *ptr)
const EVP_MD **ppmd; const EVP_MD **ppmd;
EVP_MD *md; EVP_MD *md;
long ret = 1; long ret = 1;
BIO *dbio; BIO *dbio, *next;
ctx = b->ptr;
ctx = BIO_get_data(b);
next = BIO_next(b);
switch (cmd) { switch (cmd) {
case BIO_CTRL_RESET: case BIO_CTRL_RESET:
if (b->init) if (BIO_get_init(b))
ret = EVP_DigestInit_ex(ctx, ctx->digest, NULL); ret = EVP_DigestInit_ex(ctx, ctx->digest, NULL);
else else
ret = 0; ret = 0;
if (ret > 0) if (ret > 0)
ret = BIO_ctrl(b->next_bio, cmd, num, ptr); ret = BIO_ctrl(next, cmd, num, ptr);
break; break;
case BIO_C_GET_MD: case BIO_C_GET_MD:
if (b->init) { if (BIO_get_init(b)) {
ppmd = ptr; ppmd = ptr;
*ppmd = ctx->digest; *ppmd = ctx->digest;
} else } else
@@ -201,17 +210,17 @@ static long md_ctrl(BIO *b, int cmd, long num, void *ptr)
case BIO_C_GET_MD_CTX: case BIO_C_GET_MD_CTX:
pctx = ptr; pctx = ptr;
*pctx = ctx; *pctx = ctx;
b->init = 1; BIO_set_init(b, 1);
break; break;
case BIO_C_SET_MD_CTX: case BIO_C_SET_MD_CTX:
if (b->init) if (BIO_get_init(b))
b->ptr = ptr; BIO_set_data(b, ptr);
else else
ret = 0; ret = 0;
break; break;
case BIO_C_DO_STATE_MACHINE: case BIO_C_DO_STATE_MACHINE:
BIO_clear_retry_flags(b); BIO_clear_retry_flags(b);
ret = BIO_ctrl(b->next_bio, cmd, num, ptr); ret = BIO_ctrl(next, cmd, num, ptr);
BIO_copy_next_retry(b); BIO_copy_next_retry(b);
break; break;
@@ -219,17 +228,17 @@ static long md_ctrl(BIO *b, int cmd, long num, void *ptr)
md = ptr; md = ptr;
ret = EVP_DigestInit_ex(ctx, md, NULL); ret = EVP_DigestInit_ex(ctx, md, NULL);
if (ret > 0) if (ret > 0)
b->init = 1; BIO_set_init(b, 1);
break; break;
case BIO_CTRL_DUP: case BIO_CTRL_DUP:
dbio = ptr; dbio = ptr;
dctx = dbio->ptr; dctx = BIO_get_data(dbio);
if (!EVP_MD_CTX_copy_ex(dctx, ctx)) if (!EVP_MD_CTX_copy_ex(dctx, ctx))
return 0; return 0;
b->init = 1; BIO_set_init(b, 1);
break; break;
default: default:
ret = BIO_ctrl(b->next_bio, cmd, num, ptr); ret = BIO_ctrl(next, cmd, num, ptr);
break; break;
} }
return (ret); return (ret);
@@ -238,12 +247,16 @@ static long md_ctrl(BIO *b, int cmd, long num, void *ptr)
static long md_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) static long md_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
{ {
long ret = 1; long ret = 1;
BIO *next;
next = BIO_next(b);
if (next == NULL)
return 0;
if (b->next_bio == NULL)
return (0);
switch (cmd) { switch (cmd) {
default: default:
ret = BIO_callback_ctrl(b->next_bio, cmd, fp); ret = BIO_callback_ctrl(next, cmd, fp);
break; break;
} }
return (ret); return (ret);
@@ -254,20 +267,13 @@ static int md_gets(BIO *bp, char *buf, int size)
EVP_MD_CTX *ctx; EVP_MD_CTX *ctx;
unsigned int ret; unsigned int ret;
ctx = bp->ptr; ctx = BIO_get_data(bp);
if (size < ctx->digest->md_size) if (size < ctx->digest->md_size)
return (0); return 0;
if (EVP_DigestFinal_ex(ctx, (unsigned char *)buf, &ret) <= 0) if (EVP_DigestFinal_ex(ctx, (unsigned char *)buf, &ret) <= 0)
return -1; return -1;
return ((int)ret); return ((int)ret);
} }
/*-
static int md_puts(bp,str)
BIO *bp;
char *str;
{
return(-1);
}
*/

View File

@@ -121,7 +121,7 @@
#include <assert.h> #include <assert.h>
#include "internal/cryptlib.h" #include "internal/cryptlib.h"
#include <openssl/buffer.h> #include <openssl/buffer.h>
#include <openssl/bio.h> #include "internal/bio.h"
#include <openssl/evp.h> #include <openssl/evp.h>
#include <openssl/rand.h> #include <openssl/rand.h>
#include "internal/evp_int.h" #include "internal/evp_int.h"
@@ -178,40 +178,48 @@ static int ok_new(BIO *bi)
ctx = OPENSSL_zalloc(sizeof(*ctx)); ctx = OPENSSL_zalloc(sizeof(*ctx));
if (ctx == NULL) if (ctx == NULL)
return (0); return 0;
ctx->cont = 1; ctx->cont = 1;
ctx->sigio = 1; ctx->sigio = 1;
ctx->md = EVP_MD_CTX_new(); ctx->md = EVP_MD_CTX_new();
bi->init = 0; BIO_set_init(bi, 0);
bi->ptr = (char *)ctx; BIO_set_data(bi, ctx);
bi->flags = 0;
return (1); return 1;
} }
static int ok_free(BIO *a) static int ok_free(BIO *a)
{ {
BIO_OK_CTX *ctx;
if (a == NULL) if (a == NULL)
return (0); return 0;
EVP_MD_CTX_free(((BIO_OK_CTX *)a->ptr)->md);
OPENSSL_clear_free(a->ptr, sizeof(BIO_OK_CTX)); ctx = BIO_get_data(a);
a->ptr = NULL;
a->init = 0; EVP_MD_CTX_free(ctx->md);
a->flags = 0; OPENSSL_clear_free(ctx, sizeof(BIO_OK_CTX));
return (1); BIO_set_data(a, NULL);
BIO_set_init(a, 0);
return 1;
} }
static int ok_read(BIO *b, char *out, int outl) static int ok_read(BIO *b, char *out, int outl)
{ {
int ret = 0, i, n; int ret = 0, i, n;
BIO_OK_CTX *ctx; BIO_OK_CTX *ctx;
BIO *next;
if (out == NULL) if (out == NULL)
return (0); return 0;
ctx = (BIO_OK_CTX *)b->ptr;
if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0)) ctx = BIO_get_data(b);
return (0); next = BIO_next(b);
if ((ctx == NULL) || (next == NULL) || (BIO_get_init(b) == 0))
return 0;
while (outl > 0) { while (outl > 0) {
@@ -250,7 +258,7 @@ static int ok_read(BIO *b, char *out, int outl)
/* no clean bytes in buffer -- fill it */ /* no clean bytes in buffer -- fill it */
n = IOBS - ctx->buf_len; n = IOBS - ctx->buf_len;
i = BIO_read(b->next_bio, &(ctx->buf[ctx->buf_len]), n); i = BIO_read(next, &(ctx->buf[ctx->buf_len]), n);
if (i <= 0) if (i <= 0)
break; /* nothing new */ break; /* nothing new */
@@ -281,21 +289,23 @@ static int ok_read(BIO *b, char *out, int outl)
BIO_clear_retry_flags(b); BIO_clear_retry_flags(b);
BIO_copy_next_retry(b); BIO_copy_next_retry(b);
return (ret); return ret;
} }
static int ok_write(BIO *b, const char *in, int inl) static int ok_write(BIO *b, const char *in, int inl)
{ {
int ret = 0, n, i; int ret = 0, n, i;
BIO_OK_CTX *ctx; BIO_OK_CTX *ctx;
BIO *next;
if (inl <= 0) if (inl <= 0)
return inl; return inl;
ctx = (BIO_OK_CTX *)b->ptr; ctx = BIO_get_data(b);
next = BIO_next(b);
ret = inl; ret = inl;
if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0)) if ((ctx == NULL) || (next == NULL) || (BIO_get_init(b) == 0))
return (0); return (0);
if (ctx->sigio && !sig_out(b)) if (ctx->sigio && !sig_out(b))
@@ -305,7 +315,7 @@ static int ok_write(BIO *b, const char *in, int inl)
BIO_clear_retry_flags(b); BIO_clear_retry_flags(b);
n = ctx->buf_len - ctx->buf_off; n = ctx->buf_len - ctx->buf_off;
while (ctx->blockout && n > 0) { while (ctx->blockout && n > 0) {
i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n); i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
if (i <= 0) { if (i <= 0) {
BIO_copy_next_retry(b); BIO_copy_next_retry(b);
if (!BIO_should_retry(b)) if (!BIO_should_retry(b))
@@ -354,8 +364,10 @@ static long ok_ctrl(BIO *b, int cmd, long num, void *ptr)
const EVP_MD **ppmd; const EVP_MD **ppmd;
long ret = 1; long ret = 1;
int i; int i;
BIO *next;
ctx = b->ptr; ctx = BIO_get_data(b);
next = BIO_next(b);
switch (cmd) { switch (cmd) {
case BIO_CTRL_RESET: case BIO_CTRL_RESET:
@@ -367,19 +379,19 @@ static long ok_ctrl(BIO *b, int cmd, long num, void *ptr)
ctx->finished = 0; ctx->finished = 0;
ctx->blockout = 0; ctx->blockout = 0;
ctx->sigio = 1; ctx->sigio = 1;
ret = BIO_ctrl(b->next_bio, cmd, num, ptr); ret = BIO_ctrl(next, cmd, num, ptr);
break; break;
case BIO_CTRL_EOF: /* More to read */ case BIO_CTRL_EOF: /* More to read */
if (ctx->cont <= 0) if (ctx->cont <= 0)
ret = 1; ret = 1;
else else
ret = BIO_ctrl(b->next_bio, cmd, num, ptr); ret = BIO_ctrl(next, cmd, num, ptr);
break; break;
case BIO_CTRL_PENDING: /* More to read in buffer */ case BIO_CTRL_PENDING: /* More to read in buffer */
case BIO_CTRL_WPENDING: /* 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) if (ret <= 0)
ret = BIO_ctrl(b->next_bio, cmd, num, ptr); ret = BIO_ctrl(next, cmd, num, ptr);
break; break;
case BIO_CTRL_FLUSH: case BIO_CTRL_FLUSH:
/* do a final write */ /* do a final write */
@@ -400,11 +412,11 @@ static long ok_ctrl(BIO *b, int cmd, long num, void *ptr)
ctx->cont = (int)ret; ctx->cont = (int)ret;
/* Finally flush the underlying BIO */ /* Finally flush the underlying BIO */
ret = BIO_ctrl(b->next_bio, cmd, num, ptr); ret = BIO_ctrl(next, cmd, num, ptr);
break; break;
case BIO_C_DO_STATE_MACHINE: case BIO_C_DO_STATE_MACHINE:
BIO_clear_retry_flags(b); BIO_clear_retry_flags(b);
ret = BIO_ctrl(b->next_bio, cmd, num, ptr); ret = BIO_ctrl(next, cmd, num, ptr);
BIO_copy_next_retry(b); BIO_copy_next_retry(b);
break; break;
case BIO_CTRL_INFO: case BIO_CTRL_INFO:
@@ -414,34 +426,39 @@ static long ok_ctrl(BIO *b, int cmd, long num, void *ptr)
md = ptr; md = ptr;
if (!EVP_DigestInit_ex(ctx->md, md, NULL)) if (!EVP_DigestInit_ex(ctx->md, md, NULL))
return 0; return 0;
b->init = 1; BIO_set_init(b, 1);
break; break;
case BIO_C_GET_MD: case BIO_C_GET_MD:
if (b->init) { if (BIO_get_init(b)) {
ppmd = ptr; ppmd = ptr;
*ppmd = EVP_MD_CTX_md(ctx->md); *ppmd = EVP_MD_CTX_md(ctx->md);
} else } else
ret = 0; ret = 0;
break; break;
default: default:
ret = BIO_ctrl(b->next_bio, cmd, num, ptr); ret = BIO_ctrl(next, cmd, num, ptr);
break; break;
} }
return (ret); return ret;
} }
static long ok_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) static long ok_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
{ {
long ret = 1; long ret = 1;
BIO *next;
next = BIO_next(b);
if (next == NULL)
return 0;
if (b->next_bio == NULL)
return (0);
switch (cmd) { switch (cmd) {
default: default:
ret = BIO_callback_ctrl(b->next_bio, cmd, fp); ret = BIO_callback_ctrl(next, cmd, fp);
break; break;
} }
return (ret);
return ret;
} }
static void longswap(void *_ptr, size_t len) static void longswap(void *_ptr, size_t len)
@@ -472,7 +489,7 @@ static int sig_out(BIO *b)
int md_size; int md_size;
void *md_data; void *md_data;
ctx = b->ptr; ctx = BIO_get_data(b);
md = ctx->md; md = ctx->md;
digest = EVP_MD_CTX_md(md); digest = EVP_MD_CTX_md(md);
md_size = EVP_MD_size(digest); md_size = EVP_MD_size(digest);
@@ -516,7 +533,7 @@ static int sig_in(BIO *b)
int md_size; int md_size;
void *md_data; void *md_data;
ctx = b->ptr; ctx = BIO_get_data(b);
md = ctx->md; md = ctx->md;
digest = EVP_MD_CTX_md(md); digest = EVP_MD_CTX_md(md);
md_size = EVP_MD_size(digest); md_size = EVP_MD_size(digest);
@@ -562,7 +579,7 @@ static int block_out(BIO *b)
const EVP_MD *digest; const EVP_MD *digest;
int md_size; int md_size;
ctx = b->ptr; ctx = BIO_get_data(b);
md = ctx->md; md = ctx->md;
digest = EVP_MD_CTX_md(md); digest = EVP_MD_CTX_md(md);
md_size = EVP_MD_size(digest); md_size = EVP_MD_size(digest);
@@ -593,7 +610,7 @@ static int block_in(BIO *b)
unsigned char tmp[EVP_MAX_MD_SIZE]; unsigned char tmp[EVP_MAX_MD_SIZE];
int md_size; int md_size;
ctx = b->ptr; ctx = BIO_get_data(b);
md = ctx->md; md = ctx->md;
md_size = EVP_MD_size(EVP_MD_CTX_md(md)); md_size = EVP_MD_size(EVP_MD_CTX_md(md));

71
include/internal/bio.h Normal file
View File

@@ -0,0 +1,71 @@
/* ====================================================================
* Copyright (c) 2016 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* licensing@OpenSSL.org.
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT 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.
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* (eay@cryptsoft.com). This product includes software written by Tim
* Hudson (tjh@cryptsoft.com).
*
*/
#include <openssl/bio.h>
struct bio_method_st {
int type;
const char *name;
int (*bwrite) (BIO *, const char *, int);
int (*bread) (BIO *, char *, int);
int (*bputs) (BIO *, const char *);
int (*bgets) (BIO *, char *, int);
long (*ctrl) (BIO *, int, long, void *);
int (*create) (BIO *);
int (*destroy) (BIO *);
long (*callback_ctrl) (BIO *, int, bio_info_cb *);
};

View File

@@ -290,70 +290,16 @@ void BIO_set_callback(BIO *b,
char *BIO_get_callback_arg(const BIO *b); char *BIO_get_callback_arg(const BIO *b);
void BIO_set_callback_arg(BIO *b, char *arg); void BIO_set_callback_arg(BIO *b, char *arg);
typedef struct bio_method_st BIO_METHOD;
const char *BIO_method_name(const BIO *b); const char *BIO_method_name(const BIO *b);
int BIO_method_type(const BIO *b); int BIO_method_type(const BIO *b);
typedef void bio_info_cb (struct bio_st *, int, const char *, int, long, typedef void bio_info_cb (struct bio_st *, int, const char *, int, long,
long); long);
typedef struct bio_method_st {
int type;
const char *name;
int (*bwrite) (BIO *, const char *, int);
int (*bread) (BIO *, char *, int);
int (*bputs) (BIO *, const char *);
int (*bgets) (BIO *, char *, int);
long (*ctrl) (BIO *, int, long, void *);
int (*create) (BIO *);
int (*destroy) (BIO *);
long (*callback_ctrl) (BIO *, int, bio_info_cb *);
} BIO_METHOD;
struct bio_st {
const BIO_METHOD *method;
/* bio, mode, argp, argi, argl, ret */
long (*callback) (struct bio_st *, int, const char *, int, long, long);
char *cb_arg; /* first argument for the callback */
int init;
int shutdown;
int flags; /* extra storage */
int retry_reason;
int num;
void *ptr;
struct bio_st *next_bio; /* used by filter BIOs */
struct bio_st *prev_bio; /* used by filter BIOs */
int references;
uint64_t num_read;
uint64_t num_write;
CRYPTO_EX_DATA ex_data;
CRYPTO_RWLOCK *lock;
};
DEFINE_STACK_OF(BIO) DEFINE_STACK_OF(BIO)
typedef struct bio_f_buffer_ctx_struct {
/*-
* Buffers are setup like this:
*
* <---------------------- size ----------------------->
* +---------------------------------------------------+
* | consumed | remaining | free space |
* +---------------------------------------------------+
* <-- off --><------- len ------->
*/
/*- BIO *bio; *//*
* this is now in the BIO struct
*/
int ibuf_size; /* how big is the input buffer */
int obuf_size; /* how big is the output buffer */
char *ibuf; /* the char array */
int ibuf_len; /* how many bytes are in it */
int ibuf_off; /* write/read offset */
char *obuf; /* the char array */
int obuf_len; /* how many bytes are in it */
int obuf_off; /* write/read offset */
} BIO_F_BUFFER_CTX;
/* Prefix and suffix callback in ASN1 BIO */ /* Prefix and suffix callback in ASN1 BIO */
typedef int asn1_ps_func (BIO *b, unsigned char **pbuf, int *plen, typedef int asn1_ps_func (BIO *b, unsigned char **pbuf, int *plen,
void *parg); void *parg);
@@ -635,6 +581,12 @@ BIO *BIO_new_fp(FILE *stream, int close_flag);
BIO *BIO_new(const BIO_METHOD *type); BIO *BIO_new(const BIO_METHOD *type);
int BIO_set(BIO *a, const BIO_METHOD *type); int BIO_set(BIO *a, const BIO_METHOD *type);
int BIO_free(BIO *a); int BIO_free(BIO *a);
void BIO_set_data(BIO *a, void *ptr);
void *BIO_get_data(BIO *a);
void BIO_set_init(BIO *a, int init);
int BIO_get_init(BIO *a);
void BIO_set_shutdown(BIO *a, int shut);
int BIO_get_shutdown(BIO *a);
void BIO_vfree(BIO *a); void BIO_vfree(BIO *a);
int BIO_up_ref(BIO *a); int BIO_up_ref(BIO *a);
int BIO_read(BIO *b, void *data, int len); int BIO_read(BIO *b, void *data, int len);
@@ -653,8 +605,10 @@ BIO *BIO_pop(BIO *b);
void BIO_free_all(BIO *a); void BIO_free_all(BIO *a);
BIO *BIO_find_type(BIO *b, int bio_type); BIO *BIO_find_type(BIO *b, int bio_type);
BIO *BIO_next(BIO *b); BIO *BIO_next(BIO *b);
void BIO_set_next(BIO *b, BIO *next);
BIO *BIO_get_retry_BIO(BIO *bio, int *reason); BIO *BIO_get_retry_BIO(BIO *bio, int *reason);
int BIO_get_retry_reason(BIO *bio); int BIO_get_retry_reason(BIO *bio);
void BIO_set_retry_reason(BIO *bio, int reason);
BIO *BIO_dup_chain(BIO *in); BIO *BIO_dup_chain(BIO *in);
int BIO_nread0(BIO *bio, char **buf); int BIO_nread0(BIO *bio, char **buf);
@@ -818,6 +772,34 @@ int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)
__bio_h__attr__((__format__(__printf__, 3, 0))); __bio_h__attr__((__format__(__printf__, 3, 0)));
# undef __bio_h__attr__ # undef __bio_h__attr__
BIO_METHOD *BIO_meth_new(int type, const char *name);
void BIO_meth_free(BIO_METHOD *biom);
int (*BIO_meth_get_write(BIO_METHOD *biom)) (BIO *, const char *, int);
int BIO_meth_set_write(BIO_METHOD *biom,
int (*write) (BIO *, const char *, int));
int (*BIO_meth_get_read(BIO_METHOD *biom)) (BIO *, char *, int);
int BIO_meth_set_read(BIO_METHOD *biom,
int (*read) (BIO *, char *, int));
int (*BIO_meth_get_puts(BIO_METHOD *biom)) (BIO *, const char *);
int BIO_meth_set_puts(BIO_METHOD *biom,
int (*puts) (BIO *, const char *));
int (*BIO_meth_get_gets(BIO_METHOD *biom)) (BIO *, char *, int);
int BIO_meth_set_gets(BIO_METHOD *biom,
int (*gets) (BIO *, char *, int));
long (*BIO_meth_get_ctrl(BIO_METHOD *biom)) (BIO *, int, long, void *);
int BIO_meth_set_ctrl(BIO_METHOD *biom,
long (*ctrl) (BIO *, int, long, void *));
int (*BIO_meth_get_create(BIO_METHOD *bion)) (BIO *);
int BIO_meth_set_create(BIO_METHOD *biom, int (*create) (BIO *));
int (*BIO_meth_get_destroy(BIO_METHOD *biom)) (BIO *);
int BIO_meth_set_destroy(BIO_METHOD *biom, int (*destroy) (BIO *));
long (*BIO_meth_get_callback_ctrl(BIO_METHOD *biom))
(BIO *, int, bio_info_cb *);
int BIO_meth_set_callback_ctrl(BIO_METHOD *biom,
long (*callback_ctrl) (BIO *, int,
bio_info_cb *));
/* BEGIN ERROR CODES */ /* BEGIN ERROR CODES */
/* /*
* The following lines are auto generated by the script mkerr.pl. Any changes * The following lines are auto generated by the script mkerr.pl. Any changes

View File

@@ -60,7 +60,7 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <openssl/crypto.h> #include <openssl/crypto.h>
#include <openssl/bio.h> #include "internal/bio.h"
#include <openssl/err.h> #include <openssl/err.h>
#include "ssl_locl.h" #include "ssl_locl.h"
@@ -106,10 +106,12 @@ static int ssl_new(BIO *bi)
BIOerr(BIO_F_SSL_NEW, ERR_R_MALLOC_FAILURE); BIOerr(BIO_F_SSL_NEW, ERR_R_MALLOC_FAILURE);
return (0); return (0);
} }
bi->init = 0; BIO_set_init(bi, 0);
bi->ptr = (char *)bs; BIO_set_data(bi, bs);
bi->flags = 0; /* Clear all flags */
return (1); BIO_clear_flags(bi, ~0);
return 1;
} }
static int ssl_free(BIO *a) static int ssl_free(BIO *a)
@@ -118,17 +120,18 @@ static int ssl_free(BIO *a)
if (a == NULL) if (a == NULL)
return (0); return (0);
bs = (BIO_SSL *)a->ptr; bs = BIO_get_data(a);
if (bs->ssl != NULL) if (bs->ssl != NULL)
SSL_shutdown(bs->ssl); SSL_shutdown(bs->ssl);
if (a->shutdown) { if (BIO_get_shutdown(a)) {
if (a->init) if (BIO_get_init(a))
SSL_free(bs->ssl); SSL_free(bs->ssl);
a->init = 0; /* Clear all flags */
a->flags = 0; BIO_clear_flags(a, ~0);
BIO_set_init(a, 0);
} }
OPENSSL_free(a->ptr); OPENSSL_free(bs);
return (1); return 1;
} }
static int ssl_read(BIO *b, char *out, int outl) static int ssl_read(BIO *b, char *out, int outl)
@@ -141,7 +144,7 @@ static int ssl_read(BIO *b, char *out, int outl)
if (out == NULL) if (out == NULL)
return (0); return (0);
sb = (BIO_SSL *)b->ptr; sb = BIO_get_data(b);
ssl = sb->ssl; ssl = sb->ssl;
BIO_clear_retry_flags(b); BIO_clear_retry_flags(b);
@@ -198,7 +201,7 @@ static int ssl_read(BIO *b, char *out, int outl)
break; break;
} }
b->retry_reason = retry_reason; BIO_set_retry_reason(b, retry_reason);
return (ret); return (ret);
} }
@@ -211,7 +214,7 @@ static int ssl_write(BIO *b, const char *out, int outl)
if (out == NULL) if (out == NULL)
return (0); return (0);
bs = (BIO_SSL *)b->ptr; bs = BIO_get_data(b);
ssl = bs->ssl; ssl = bs->ssl;
BIO_clear_retry_flags(b); BIO_clear_retry_flags(b);
@@ -264,18 +267,20 @@ static int ssl_write(BIO *b, const char *out, int outl)
break; break;
} }
b->retry_reason = retry_reason; BIO_set_retry_reason(b, retry_reason);
return (ret); return ret;
} }
static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr) static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr)
{ {
SSL **sslp, *ssl; SSL **sslp, *ssl;
BIO_SSL *bs; BIO_SSL *bs, *dbs;
BIO *dbio, *bio; BIO *dbio, *bio;
long ret = 1; long ret = 1;
BIO *next;
bs = (BIO_SSL *)b->ptr; bs = BIO_get_data(b);
next = BIO_next(b);
ssl = bs->ssl; ssl = bs->ssl;
if ((ssl == NULL) && (cmd != BIO_C_SET_SSL)) if ((ssl == NULL) && (cmd != BIO_C_SET_SSL))
return (0); return (0);
@@ -293,8 +298,8 @@ static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr)
break; break;
} }
if (b->next_bio != NULL) if (next != NULL)
ret = BIO_ctrl(b->next_bio, cmd, num, ptr); ret = BIO_ctrl(next, cmd, num, ptr);
else if (ssl->rbio != NULL) else if (ssl->rbio != NULL)
ret = BIO_ctrl(ssl->rbio, cmd, num, ptr); ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);
else else
@@ -330,17 +335,17 @@ static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr)
if (!ssl_new(b)) if (!ssl_new(b))
return 0; return 0;
} }
b->shutdown = (int)num; BIO_set_shutdown(b, num);
ssl = (SSL *)ptr; ssl = (SSL *)ptr;
((BIO_SSL *)b->ptr)->ssl = ssl; bs->ssl = ssl;
bio = SSL_get_rbio(ssl); bio = SSL_get_rbio(ssl);
if (bio != NULL) { if (bio != NULL) {
if (b->next_bio != NULL) if (next != NULL)
BIO_push(bio, b->next_bio); BIO_push(bio, next);
b->next_bio = bio; BIO_set_next(b, bio);
BIO_up_ref(bio); BIO_up_ref(bio);
} }
b->init = 1; BIO_set_init(b, 1);
break; break;
case BIO_C_GET_SSL: case BIO_C_GET_SSL:
if (ptr != NULL) { if (ptr != NULL) {
@@ -350,10 +355,10 @@ static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr)
ret = 0; ret = 0;
break; break;
case BIO_CTRL_GET_CLOSE: case BIO_CTRL_GET_CLOSE:
ret = b->shutdown; ret = BIO_get_shutdown(b);
break; break;
case BIO_CTRL_SET_CLOSE: case BIO_CTRL_SET_CLOSE:
b->shutdown = (int)num; BIO_set_shutdown(b, (int)num);
break; break;
case BIO_CTRL_WPENDING: case BIO_CTRL_WPENDING:
ret = BIO_ctrl(ssl->wbio, cmd, num, ptr); ret = BIO_ctrl(ssl->wbio, cmd, num, ptr);
@@ -369,8 +374,8 @@ static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr)
BIO_copy_next_retry(b); BIO_copy_next_retry(b);
break; break;
case BIO_CTRL_PUSH: case BIO_CTRL_PUSH:
if ((b->next_bio != NULL) && (b->next_bio != ssl->rbio)) { if ((next != NULL) && (next != ssl->rbio)) {
SSL_set_bio(ssl, b->next_bio, b->next_bio); SSL_set_bio(ssl, next, next);
BIO_up_ref(b); BIO_up_ref(b);
} }
break; break;
@@ -383,8 +388,8 @@ static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr)
*/ */
if (ssl->rbio != ssl->wbio) if (ssl->rbio != ssl->wbio)
BIO_free_all(ssl->wbio); BIO_free_all(ssl->wbio);
if (b->next_bio != NULL) if (next != NULL)
BIO_free(b->next_bio); BIO_free(next);
ssl->wbio = NULL; ssl->wbio = NULL;
ssl->rbio = NULL; ssl->rbio = NULL;
} }
@@ -392,7 +397,7 @@ static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr)
case BIO_C_DO_STATE_MACHINE: case BIO_C_DO_STATE_MACHINE:
BIO_clear_retry_flags(b); BIO_clear_retry_flags(b);
b->retry_reason = 0; BIO_set_retry_reason(b, 0);
ret = (int)SSL_do_handshake(ssl); ret = (int)SSL_do_handshake(ssl);
switch (SSL_get_error(ssl, (int)ret)) { switch (SSL_get_error(ssl, (int)ret)) {
@@ -404,11 +409,11 @@ static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr)
break; break;
case SSL_ERROR_WANT_CONNECT: case SSL_ERROR_WANT_CONNECT:
BIO_set_flags(b, BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY); BIO_set_flags(b, BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY);
b->retry_reason = b->next_bio->retry_reason; BIO_set_retry_reason(b, BIO_get_retry_reason(next));
break; break;
case SSL_ERROR_WANT_X509_LOOKUP: case SSL_ERROR_WANT_X509_LOOKUP:
BIO_set_retry_special(b); BIO_set_retry_special(b);
b->retry_reason = BIO_RR_SSL_X509_LOOKUP; BIO_set_retry_reason(b, BIO_RR_SSL_X509_LOOKUP);
break; break;
default: default:
break; break;
@@ -416,15 +421,14 @@ static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr)
break; break;
case BIO_CTRL_DUP: case BIO_CTRL_DUP:
dbio = (BIO *)ptr; dbio = (BIO *)ptr;
SSL_free(((BIO_SSL *)dbio->ptr)->ssl); dbs = BIO_get_data(dbio);
((BIO_SSL *)dbio->ptr)->ssl = SSL_dup(ssl); SSL_free(dbs->ssl);
((BIO_SSL *)dbio->ptr)->renegotiate_count = dbs->ssl = SSL_dup(ssl);
((BIO_SSL *)b->ptr)->renegotiate_count; dbs->renegotiate_count = dbs->renegotiate_count;
((BIO_SSL *)dbio->ptr)->byte_count = ((BIO_SSL *)b->ptr)->byte_count; dbs->byte_count = dbs->byte_count;
((BIO_SSL *)dbio->ptr)->renegotiate_timeout = dbs->renegotiate_timeout = dbs->renegotiate_timeout;
((BIO_SSL *)b->ptr)->renegotiate_timeout; dbs->last_time = dbs->last_time;
((BIO_SSL *)dbio->ptr)->last_time = ((BIO_SSL *)b->ptr)->last_time; ret = (dbs->ssl != NULL);
ret = (((BIO_SSL *)dbio->ptr)->ssl != NULL);
break; break;
case BIO_C_GET_FD: case BIO_C_GET_FD:
ret = BIO_ctrl(ssl->rbio, cmd, num, ptr); ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);
@@ -461,7 +465,7 @@ static long ssl_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
BIO_SSL *bs; BIO_SSL *bs;
long ret = 1; long ret = 1;
bs = (BIO_SSL *)b->ptr; bs = BIO_get_data(b);
ssl = bs->ssl; ssl = bs->ssl;
switch (cmd) { switch (cmd) {
case BIO_CTRL_SET_CALLBACK: case BIO_CTRL_SET_CALLBACK:
@@ -548,14 +552,16 @@ BIO *BIO_new_ssl(SSL_CTX *ctx, int client)
int BIO_ssl_copy_session_id(BIO *t, BIO *f) int BIO_ssl_copy_session_id(BIO *t, BIO *f)
{ {
BIO_SSL *tdata, *fdata;
t = BIO_find_type(t, BIO_TYPE_SSL); t = BIO_find_type(t, BIO_TYPE_SSL);
f = BIO_find_type(f, BIO_TYPE_SSL); f = BIO_find_type(f, BIO_TYPE_SSL);
if ((t == NULL) || (f == NULL)) if ((t == NULL) || (f == NULL))
return 0;
tdata = BIO_get_data(t);
fdata = BIO_get_data(f);
if ((tdata->ssl == NULL) || (fdata->ssl == NULL))
return (0); return (0);
if ((((BIO_SSL *)t->ptr)->ssl == NULL) || if (!SSL_copy_session_id(tdata->ssl, (fdata->ssl)))
(((BIO_SSL *)f->ptr)->ssl == NULL))
return (0);
if (!SSL_copy_session_id(((BIO_SSL *)t->ptr)->ssl, ((BIO_SSL *)f->ptr)->ssl))
return 0; return 0;
return (1); return (1);
} }
@@ -564,12 +570,10 @@ void BIO_ssl_shutdown(BIO *b)
{ {
SSL *s; SSL *s;
while (b != NULL) { b = BIO_find_type(b, BIO_TYPE_SSL);
if (b->method->type == BIO_TYPE_SSL) { if (b == NULL)
s = ((BIO_SSL *)b->ptr)->ssl; return;
SSL_shutdown(s);
break; s = BIO_get_data(b);
} SSL_shutdown(s);
b = b->next_bio;
}
} }

View File

@@ -1105,8 +1105,8 @@ void SSL_set_wbio(SSL *s, BIO *wbio)
*/ */
if (s->bbio != NULL) { if (s->bbio != NULL) {
if (s->wbio == s->bbio) { if (s->wbio == s->bbio) {
s->wbio = s->wbio->next_bio; s->wbio = BIO_next(s->wbio);
s->bbio->next_bio = NULL; BIO_set_next(s->bbio, NULL);
} }
} }
if (s->wbio != wbio && s->rbio != s->wbio) if (s->wbio != wbio && s->rbio != s->wbio)