From a146ae55ba479a5c7aa2a6afba1b2b93102a152c Mon Sep 17 00:00:00 2001 From: Matt Caswell Date: Tue, 22 Mar 2016 09:21:29 +0000 Subject: [PATCH] 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 --- crypto/asn1/asn_mime.c | 1 + crypto/asn1/bio_asn1.c | 67 +++++++++++++---------- crypto/bio/bf_buff.c | 2 +- crypto/bio/bf_nbio.c | 2 +- crypto/bio/bf_null.c | 2 +- crypto/bio/bio_cb.c | 2 +- crypto/bio/bio_lcl.h | 45 +++++++++++++++- crypto/bio/bio_lib.c | 42 ++++++++++++++- crypto/bio/bio_meth.c | 8 +-- crypto/bio/bss_bio.c | 2 +- crypto/bio/bss_log.c | 1 + crypto/bio/bss_mem.c | 2 +- crypto/bio/bss_null.c | 2 +- crypto/bio/bss_sock.c | 1 + crypto/evp/bio_b64.c | 85 +++++++++++++++++------------ crypto/evp/bio_enc.c | 100 ++++++++++++++++++++-------------- crypto/evp/bio_md.c | 100 ++++++++++++++++++---------------- crypto/evp/bio_ok.c | 97 +++++++++++++++++++-------------- include/internal/bio.h | 71 ++++++++++++++++++++++++ include/openssl/bio.h | 94 +++++++++++++------------------- ssl/bio_ssl.c | 120 +++++++++++++++++++++-------------------- ssl/ssl_lib.c | 4 +- 22 files changed, 534 insertions(+), 316 deletions(-) create mode 100644 include/internal/bio.h diff --git a/crypto/asn1/asn_mime.c b/crypto/asn1/asn_mime.c index 6ac9b2c8e2..851fb91e8c 100644 --- a/crypto/asn1/asn_mime.c +++ b/crypto/asn1/asn_mime.c @@ -60,6 +60,7 @@ #include #include #include "internal/evp_int.h" +#include "internal/bio.h" #include "asn1_locl.h" /* diff --git a/crypto/asn1/bio_asn1.c b/crypto/asn1/bio_asn1.c index 80206aa4f6..1657ac72cf 100644 --- a/crypto/asn1/bio_asn1.c +++ b/crypto/asn1/bio_asn1.c @@ -63,7 +63,7 @@ */ #include -#include +#include #include /* Must be large enough for biggest tag+length */ @@ -152,9 +152,9 @@ static int asn1_bio_new(BIO *b) OPENSSL_free(ctx); return 0; } - b->init = 1; - b->ptr = (char *)ctx; - b->flags = 0; + BIO_set_data(b, ctx); + BIO_set_init(b, 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) { - 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) return 0; + OPENSSL_free(ctx->buf); OPENSSL_free(ctx); - b->init = 0; - b->ptr = NULL; - b->flags = 0; + BIO_set_data(b, NULL); + BIO_set_init(b, 0); + return 1; } @@ -195,10 +200,11 @@ static int asn1_bio_write(BIO *b, const char *in, int inl) BIO_ASN1_BUF_CTX *ctx; int wrmax, wrlen, ret; unsigned char *p; - if (!in || (inl < 0) || (b->next_bio == NULL)) - return 0; - ctx = (BIO_ASN1_BUF_CTX *)b->ptr; - if (ctx == NULL) + BIO *next; + + ctx = BIO_get_data(b); + next = BIO_next(b); + if (in == NULL || inl < 0 || ctx == NULL || next == NULL) return 0; wrlen = 0; @@ -236,7 +242,7 @@ static int asn1_bio_write(BIO *b, const char *in, int inl) break; 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) goto done; @@ -256,7 +262,7 @@ static int asn1_bio_write(BIO *b, const char *in, int inl) wrmax = ctx->copylen; else wrmax = inl; - ret = BIO_write(b->next_bio, in, wrmax); + ret = BIO_write(next, in, wrmax); if (ret <= 0) break; 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) { int ret; + if (ctx->ex_len <= 0) return 1; 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) break; 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) { - if (!b->next_bio) + BIO *next = BIO_next(b); + if (next == NULL) 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) @@ -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) { - if (!b->next_bio) + BIO *next = BIO_next(b); + if (next == NULL) 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) { - if (b->next_bio == NULL) - return (0); - return BIO_callback_ctrl(b->next_bio, cmd, fp); + BIO *next = BIO_next(b); + if (next == NULL) + return 0; + return BIO_callback_ctrl(next, cmd, fp); } 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_EX_FUNCS *ex_func; long ret = 1; - ctx = (BIO_ASN1_BUF_CTX *)b->ptr; + BIO *next; + + ctx = BIO_get_data(b); if (ctx == NULL) return 0; + next = BIO_next(b); switch (cmd) { case BIO_C_SET_PREFIX: @@ -397,7 +410,7 @@ static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2) break; case BIO_CTRL_FLUSH: - if (!b->next_bio) + if (next == NULL) return 0; /* 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) - return BIO_ctrl(b->next_bio, cmd, arg1, arg2); + return BIO_ctrl(next, cmd, arg1, arg2); else { BIO_clear_retry_flags(b); return 0; } default: - if (!b->next_bio) + if (next == NULL) return 0; - return BIO_ctrl(b->next_bio, cmd, arg1, arg2); + return BIO_ctrl(next, cmd, arg1, arg2); } diff --git a/crypto/bio/bf_buff.c b/crypto/bio/bf_buff.c index 6040c85119..361d26a5b7 100644 --- a/crypto/bio/bf_buff.c +++ b/crypto/bio/bf_buff.c @@ -57,8 +57,8 @@ #include #include +#include "bio_lcl.h" #include "internal/cryptlib.h" -#include static int buffer_write(BIO *h, const char *buf, int num); static int buffer_read(BIO *h, char *buf, int size); diff --git a/crypto/bio/bf_nbio.c b/crypto/bio/bf_nbio.c index c8bf580e1d..cefd35f984 100644 --- a/crypto/bio/bf_nbio.c +++ b/crypto/bio/bf_nbio.c @@ -57,9 +57,9 @@ #include #include +#include "bio_lcl.h" #include "internal/cryptlib.h" #include -#include /* * BIO_put and BIO_get both add to the digest, BIO_gets returns the digest diff --git a/crypto/bio/bf_null.c b/crypto/bio/bf_null.c index e3b87d24e5..162e250f9b 100644 --- a/crypto/bio/bf_null.c +++ b/crypto/bio/bf_null.c @@ -57,8 +57,8 @@ #include #include +#include "bio_lcl.h" #include "internal/cryptlib.h" -#include /* * BIO_put and BIO_get both add to the digest, BIO_gets returns the digest diff --git a/crypto/bio/bio_cb.c b/crypto/bio/bio_cb.c index ec484b697b..552b66eca0 100644 --- a/crypto/bio/bio_cb.c +++ b/crypto/bio/bio_cb.c @@ -58,8 +58,8 @@ #include #include #include +#include "bio_lcl.h" #include "internal/cryptlib.h" -#include #include long BIO_debug_callback(BIO *bio, int cmd, const char *argp, diff --git a/crypto/bio/bio_lcl.h b/crypto/bio/bio_lcl.h index 24f8b18595..1e409f8e13 100644 --- a/crypto/bio/bio_lcl.h +++ b/crypto/bio/bio_lcl.h @@ -65,7 +65,50 @@ union bio_addr_st { /* END BIO_ADDRINFO/BIO_ADDR stuff. */ #include "internal/cryptlib.h" -#include +#include + +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 # ifdef OPENSSL_SYS_VMS diff --git a/crypto/bio/bio_lib.c b/crypto/bio/bio_lib.c index 9357553d36..ac98cf2402 100644 --- a/crypto/bio/bio_lib.c +++ b/crypto/bio/bio_lib.c @@ -58,8 +58,8 @@ #include #include #include +#include "bio_lcl.h" #include "internal/cryptlib.h" -#include #include BIO *BIO_new(const BIO_METHOD *method) @@ -142,6 +142,36 @@ int BIO_free(BIO *a) 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) { BIO_free(a); @@ -487,6 +517,11 @@ int BIO_get_retry_reason(BIO *bio) 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) { int mt, mask; @@ -516,6 +551,11 @@ BIO *BIO_next(BIO *b) return b->next_bio; } +void BIO_set_next(BIO *b, BIO *next) +{ + b->next_bio = next; +} + void BIO_free_all(BIO *bio) { BIO *b; diff --git a/crypto/bio/bio_meth.c b/crypto/bio/bio_meth.c index 3d337e91f3..134c448976 100644 --- a/crypto/bio/bio_meth.c +++ b/crypto/bio/bio_meth.c @@ -82,13 +82,13 @@ int BIO_meth_set_write(BIO_METHOD *biom, 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; } int BIO_meth_set_read(BIO_METHOD *biom, - int (*read) (BIO *, const char *, int)) + int (*read) (BIO *, char *, int)) { biom->bread = read; 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) { - return biom->gets; + return biom->bgets; } int BIO_meth_set_gets(BIO_METHOD *biom, @@ -130,7 +130,7 @@ int BIO_meth_set_ctrl(BIO_METHOD *biom, return 1; } -int (*BIO_meth_get_create(BIO_METHOD *bion)) (BIO *) +int (*BIO_meth_get_create(BIO_METHOD *biom)) (BIO *) { return biom->create; } diff --git a/crypto/bio/bss_bio.c b/crypto/bio/bss_bio.c index 518fa35c2f..2991c3afed 100644 --- a/crypto/bio/bss_bio.c +++ b/crypto/bio/bss_bio.c @@ -65,7 +65,7 @@ #include #include -#include +#include "bio_lcl.h" #include #include diff --git a/crypto/bio/bss_log.c b/crypto/bio/bss_log.c index a6bc0e779a..c2c8c79097 100644 --- a/crypto/bio/bss_log.c +++ b/crypto/bio/bss_log.c @@ -64,6 +64,7 @@ #include #include +#include "bio_lcl.h" #include "internal/cryptlib.h" #if defined(OPENSSL_SYS_WINCE) diff --git a/crypto/bio/bss_mem.c b/crypto/bio/bss_mem.c index 68ac90d0af..460e070a7d 100644 --- a/crypto/bio/bss_mem.c +++ b/crypto/bio/bss_mem.c @@ -57,8 +57,8 @@ #include #include +#include "bio_lcl.h" #include "internal/cryptlib.h" -#include static int mem_write(BIO *h, const char *buf, int num); static int mem_read(BIO *h, char *buf, int size); diff --git a/crypto/bio/bss_null.c b/crypto/bio/bss_null.c index c5e24844d1..29561c7326 100644 --- a/crypto/bio/bss_null.c +++ b/crypto/bio/bss_null.c @@ -57,8 +57,8 @@ #include #include +#include "bio_lcl.h" #include "internal/cryptlib.h" -#include static int null_write(BIO *h, const char *buf, int num); static int null_read(BIO *h, char *buf, int size); diff --git a/crypto/bio/bss_sock.c b/crypto/bio/bss_sock.c index 85d7d661fb..c1f76a24a7 100644 --- a/crypto/bio/bss_sock.c +++ b/crypto/bio/bss_sock.c @@ -58,6 +58,7 @@ #include #include #define USE_SOCKETS +#include "bio_lcl.h" #include "internal/cryptlib.h" #ifndef OPENSSL_NO_SOCK diff --git a/crypto/evp/bio_b64.c b/crypto/evp/bio_b64.c index 93e4166b48..cdb50b4584 100644 --- a/crypto/evp/bio_b64.c +++ b/crypto/evp/bio_b64.c @@ -60,6 +60,7 @@ #include "internal/cryptlib.h" #include #include +#include "internal/bio.h" static int b64_write(BIO *h, const char *buf, int num); static int b64_read(BIO *h, char *buf, int size); @@ -105,9 +106,10 @@ static const BIO_METHOD methods_b64 = { b64_callback_ctrl, }; + const BIO_METHOD *BIO_f_base64(void) { - return (&methods_b64); + return &methods_b64; } static int b64_new(BIO *bi) @@ -121,23 +123,28 @@ static int b64_new(BIO *bi) ctx->cont = 1; ctx->start = 1; ctx->base64 = EVP_ENCODE_CTX_new(); - bi->init = 1; - bi->ptr = (char *)ctx; - bi->flags = 0; - bi->num = 0; - return (1); + BIO_set_data(bi, ctx); + BIO_set_init(bi, 1); + + return 1; } static int b64_free(BIO *a) { + BIO_B64_CTX *ctx; if (a == NULL) - return (0); - EVP_ENCODE_CTX_free(((BIO_B64_CTX *)a->ptr)->base64); - OPENSSL_free(a->ptr); - a->ptr = NULL; - a->init = 0; - a->flags = 0; - return (1); + return 0; + + ctx = BIO_get_data(a); + if (ctx == NULL) + return 0; + + 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) @@ -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; BIO_B64_CTX *ctx; unsigned char *p, *q; + BIO *next; if (out == NULL) return (0); - ctx = (BIO_B64_CTX *)b->ptr; + ctx = (BIO_B64_CTX *)BIO_get_data(b); - if ((ctx == NULL) || (b->next_bio == NULL)) - return (0); + next = BIO_next(b); + if ((ctx == NULL) || (next == NULL)) + return 0; BIO_clear_retry_flags(b); @@ -191,14 +200,14 @@ static int b64_read(BIO *b, char *out, int outl) if (ctx->cont <= 0) 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); if (i <= 0) { ret_code = i; /* Should we continue next time we are called? */ - if (!BIO_should_retry(b->next_bio)) { + if (!BIO_should_retry(next)) { ctx->cont = i; /* If buffer empty break */ if (ctx->tmp_len == 0) @@ -354,8 +363,13 @@ static int b64_write(BIO *b, const char *in, int inl) int n; int i; 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); 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); n = ctx->buf_len - ctx->buf_off; 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) { BIO_copy_next_retry(b); return (i); @@ -445,7 +459,7 @@ static int b64_write(BIO *b, const char *in, int inl) ctx->buf_off = 0; n = ctx->buf_len; 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) { BIO_copy_next_retry(b); 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; long ret = 1; 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) { case BIO_CTRL_RESET: ctx->cont = 1; ctx->start = 1; ctx->encode = B64_NONE; - ret = BIO_ctrl(b->next_bio, cmd, num, ptr); + ret = BIO_ctrl(next, cmd, num, ptr); break; case BIO_CTRL_EOF: /* More to read */ if (ctx->cont <= 0) ret = 1; else - ret = BIO_ctrl(b->next_bio, cmd, num, ptr); + ret = BIO_ctrl(next, cmd, num, ptr); break; case BIO_CTRL_WPENDING: /* More to write in buffer */ 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)) ret = 1; else if (ret <= 0) - ret = BIO_ctrl(b->next_bio, cmd, num, ptr); + ret = BIO_ctrl(next, 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; if (ret <= 0) - ret = BIO_ctrl(b->next_bio, cmd, num, ptr); + ret = BIO_ctrl(next, cmd, num, ptr); break; case BIO_CTRL_FLUSH: /* do a final write */ @@ -524,12 +542,12 @@ static long b64_ctrl(BIO *b, int cmd, long num, void *ptr) goto again; } /* Finally flush the underlying BIO */ - ret = BIO_ctrl(b->next_bio, cmd, num, ptr); + ret = BIO_ctrl(next, 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(next, cmd, num, ptr); BIO_copy_next_retry(b); break; @@ -539,21 +557,22 @@ static long b64_ctrl(BIO *b, int cmd, long num, void *ptr) case BIO_CTRL_GET: case BIO_CTRL_SET: default: - ret = BIO_ctrl(b->next_bio, cmd, num, ptr); + ret = BIO_ctrl(next, cmd, num, ptr); break; } - return (ret); + return ret; } static long b64_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) { long ret = 1; + BIO *next = BIO_next(b); - if (b->next_bio == NULL) - return (0); + if (next == NULL) + return 0; switch (cmd) { default: - ret = BIO_callback_ctrl(b->next_bio, cmd, fp); + ret = BIO_callback_ctrl(next, cmd, fp); break; } return (ret); diff --git a/crypto/evp/bio_enc.c b/crypto/evp/bio_enc.c index e89c1df098..ad94ba4e5c 100644 --- a/crypto/evp/bio_enc.c +++ b/crypto/evp/bio_enc.c @@ -60,6 +60,7 @@ #include "internal/cryptlib.h" #include #include +#include "internal/bio.h" static int enc_write(BIO *h, const char *buf, int num); static int enc_read(BIO *h, char *buf, int size); @@ -122,9 +123,9 @@ static int enc_new(BIO *bi) } ctx->cont = 1; ctx->ok = 1; - bi->init = 0; - bi->ptr = (char *)ctx; - bi->flags = 0; + BIO_set_data(bi, ctx); + BIO_set_init(bi, 1); + return 1; } @@ -133,27 +134,33 @@ static int enc_free(BIO *a) BIO_ENC_CTX *b; if (a == NULL) - return (0); - b = (BIO_ENC_CTX *)a->ptr; + return 0; + + b = BIO_get_data(a); + if (b == NULL) + return 0; + EVP_CIPHER_CTX_free(b->cipher); - OPENSSL_clear_free(a->ptr, sizeof(BIO_ENC_CTX)); - a->ptr = NULL; - a->init = 0; - a->flags = 0; - return (1); + OPENSSL_clear_free(b, sizeof(BIO_ENC_CTX)); + BIO_set_data(a, NULL); + BIO_set_init(a, 0); + + return 1; } static int enc_read(BIO *b, char *out, int outl) { int ret = 0, i; BIO_ENC_CTX *ctx; + BIO *next; if (out == NULL) return (0); - ctx = (BIO_ENC_CTX *)b->ptr; + ctx = BIO_get_data(b); - if ((ctx == NULL) || (b->next_bio == NULL)) - return (0); + next = BIO_next(b); + if ((ctx == NULL) || (next == NULL)) + return 0; /* First check if there are bytes decoded/encoded */ 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 */ - 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) { /* Should be continue next time we are called? */ - if (!BIO_should_retry(b->next_bio)) { + if (!BIO_should_retry(next)) { ctx->cont = i; i = EVP_CipherFinal_ex(ctx->cipher, (unsigned char *)ctx->buf, @@ -239,14 +246,19 @@ static int enc_write(BIO *b, const char *in, int inl) { int ret = 0, n, i; 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; 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); + i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n); if (i <= 0) { BIO_copy_next_retry(b); return (i); @@ -274,7 +286,7 @@ static int enc_write(BIO *b, const char *in, int inl) ctx->buf_off = 0; n = ctx->buf_len; 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) { BIO_copy_next_retry(b); 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; int i; 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) { 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, EVP_CIPHER_CTX_encrypting(ctx->cipher))) return 0; - ret = BIO_ctrl(b->next_bio, cmd, num, ptr); + ret = BIO_ctrl(next, cmd, num, ptr); break; case BIO_CTRL_EOF: /* More to read */ if (ctx->cont <= 0) ret = 1; else - ret = BIO_ctrl(b->next_bio, cmd, num, ptr); + ret = BIO_ctrl(next, cmd, num, ptr); break; case BIO_CTRL_WPENDING: ret = ctx->buf_len - ctx->buf_off; if (ret <= 0) - ret = BIO_ctrl(b->next_bio, cmd, num, ptr); + ret = BIO_ctrl(next, cmd, num, ptr); break; case BIO_CTRL_PENDING: /* More to read in buffer */ ret = ctx->buf_len - ctx->buf_off; if (ret <= 0) - ret = BIO_ctrl(b->next_bio, cmd, num, ptr); + ret = BIO_ctrl(next, cmd, num, ptr); break; case BIO_CTRL_FLUSH: /* 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 */ - ret = BIO_ctrl(b->next_bio, cmd, num, ptr); + ret = BIO_ctrl(next, cmd, num, ptr); break; case BIO_C_GET_CIPHER_STATUS: 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(next, 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; + BIO_set_init(b, 1); break; case BIO_CTRL_DUP: dbio = (BIO *)ptr; - dctx = (BIO_ENC_CTX *)dbio->ptr; + dctx = BIO_get_data(dbio); dctx->cipher = EVP_CIPHER_CTX_new(); if (dctx->cipher == NULL) return 0; ret = EVP_CIPHER_CTX_copy(dctx->cipher, ctx->cipher); if (ret) - dbio->init = 1; + BIO_set_init(dbio, 1); break; default: - ret = BIO_ctrl(b->next_bio, cmd, num, ptr); + ret = BIO_ctrl(next, cmd, num, ptr); break; } 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) { long ret = 1; + BIO *next = BIO_next(b); - if (b->next_bio == NULL) + if (next == NULL) return (0); switch (cmd) { default: - ret = BIO_callback_ctrl(b->next_bio, cmd, fp); + ret = BIO_callback_ctrl(next, cmd, fp); break; } 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) { 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; - if ((b->callback != NULL) && - (b->callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 0L) <= - 0)) + callback = BIO_get_callback(b); + + if ((callback != NULL) && + (callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, + 0L) <= 0)) return 0; - b->init = 1; - ctx = (BIO_ENC_CTX *)b->ptr; + BIO_set_init(b, 1); + if (!EVP_CipherInit_ex(ctx->cipher, c, NULL, k, i, e)) return 0; - if (b->callback != NULL) - return b->callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, - 1L); + if (callback != NULL) + return callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 1L); return 1; } diff --git a/crypto/evp/bio_md.c b/crypto/evp/bio_md.c index 90dffa1313..46c5583351 100644 --- a/crypto/evp/bio_md.c +++ b/crypto/evp/bio_md.c @@ -62,6 +62,7 @@ #include #include "internal/evp_int.h" #include "evp_locl.h" +#include "internal/bio.h" /* * 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) return (0); - bi->init = 0; - bi->ptr = (char *)ctx; - bi->flags = 0; - return (1); + BIO_set_init(bi, 1); + BIO_set_data(bi, ctx); + + return 1; } static int md_free(BIO *a) { if (a == NULL) return (0); - EVP_MD_CTX_free(a->ptr); - a->ptr = NULL; - a->init = 0; - a->flags = 0; - return (1); + EVP_MD_CTX_free(BIO_get_data(a)); + BIO_set_data(a, NULL); + BIO_set_init(a, 0); + + return 1; } static int md_read(BIO *b, char *out, int outl) { int ret = 0; EVP_MD_CTX *ctx; + BIO *next; if (out == NULL) 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); - ret = BIO_read(b->next_bio, out, outl); - if (b->init) { + ret = BIO_read(next, out, outl); + if (BIO_get_init(b)) { if (ret > 0) { if (EVP_DigestUpdate(ctx, (unsigned char *)out, (unsigned int)ret) <= 0) @@ -149,14 +153,17 @@ static int md_write(BIO *b, const char *in, int inl) { int ret = 0; EVP_MD_CTX *ctx; + BIO *next; if ((in == NULL) || (inl <= 0)) - return (0); - ctx = b->ptr; + return 0; - if ((ctx != NULL) && (b->next_bio != NULL)) - ret = BIO_write(b->next_bio, in, inl); - if (b->init) { + ctx = BIO_get_data(b); + next = BIO_next(b); + if ((ctx != NULL) && (next != NULL)) + ret = BIO_write(next, in, inl); + + if (BIO_get_init(b)) { if (ret > 0) { if (!EVP_DigestUpdate(ctx, (const unsigned char *)in, (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_copy_next_retry(b); } - return (ret); + return ret; } 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; EVP_MD *md; long ret = 1; - BIO *dbio; + BIO *dbio, *next; - ctx = b->ptr; + + ctx = BIO_get_data(b); + next = BIO_next(b); switch (cmd) { case BIO_CTRL_RESET: - if (b->init) + if (BIO_get_init(b)) ret = EVP_DigestInit_ex(ctx, ctx->digest, NULL); else ret = 0; if (ret > 0) - ret = BIO_ctrl(b->next_bio, cmd, num, ptr); + ret = BIO_ctrl(next, cmd, num, ptr); break; case BIO_C_GET_MD: - if (b->init) { + if (BIO_get_init(b)) { ppmd = ptr; *ppmd = ctx->digest; } else @@ -201,17 +210,17 @@ static long md_ctrl(BIO *b, int cmd, long num, void *ptr) case BIO_C_GET_MD_CTX: pctx = ptr; *pctx = ctx; - b->init = 1; + BIO_set_init(b, 1); break; case BIO_C_SET_MD_CTX: - if (b->init) - b->ptr = ptr; + if (BIO_get_init(b)) + BIO_set_data(b, ptr); else 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(next, cmd, num, ptr); BIO_copy_next_retry(b); break; @@ -219,17 +228,17 @@ static long md_ctrl(BIO *b, int cmd, long num, void *ptr) md = ptr; ret = EVP_DigestInit_ex(ctx, md, NULL); if (ret > 0) - b->init = 1; + BIO_set_init(b, 1); break; case BIO_CTRL_DUP: dbio = ptr; - dctx = dbio->ptr; + dctx = BIO_get_data(dbio); if (!EVP_MD_CTX_copy_ex(dctx, ctx)) return 0; - b->init = 1; + BIO_set_init(b, 1); break; default: - ret = BIO_ctrl(b->next_bio, cmd, num, ptr); + ret = BIO_ctrl(next, cmd, num, ptr); break; } 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) { long ret = 1; + BIO *next; + + next = BIO_next(b); + + if (next == NULL) + return 0; - if (b->next_bio == NULL) - return (0); switch (cmd) { default: - ret = BIO_callback_ctrl(b->next_bio, cmd, fp); + ret = BIO_callback_ctrl(next, cmd, fp); break; } return (ret); @@ -254,20 +267,13 @@ static int md_gets(BIO *bp, char *buf, int size) EVP_MD_CTX *ctx; unsigned int ret; - ctx = bp->ptr; + ctx = BIO_get_data(bp); + if (size < ctx->digest->md_size) - return (0); + return 0; + if (EVP_DigestFinal_ex(ctx, (unsigned char *)buf, &ret) <= 0) return -1; return ((int)ret); } - -/*- -static int md_puts(bp,str) -BIO *bp; -char *str; - { - return(-1); - } -*/ diff --git a/crypto/evp/bio_ok.c b/crypto/evp/bio_ok.c index a29777ced0..0ac1a31a63 100644 --- a/crypto/evp/bio_ok.c +++ b/crypto/evp/bio_ok.c @@ -121,7 +121,7 @@ #include #include "internal/cryptlib.h" #include -#include +#include "internal/bio.h" #include #include #include "internal/evp_int.h" @@ -178,40 +178,48 @@ static int ok_new(BIO *bi) ctx = OPENSSL_zalloc(sizeof(*ctx)); if (ctx == NULL) - return (0); + return 0; ctx->cont = 1; ctx->sigio = 1; ctx->md = EVP_MD_CTX_new(); - bi->init = 0; - bi->ptr = (char *)ctx; - bi->flags = 0; - return (1); + BIO_set_init(bi, 0); + BIO_set_data(bi, ctx); + + return 1; } static int ok_free(BIO *a) { + BIO_OK_CTX *ctx; + if (a == NULL) - return (0); - EVP_MD_CTX_free(((BIO_OK_CTX *)a->ptr)->md); - OPENSSL_clear_free(a->ptr, sizeof(BIO_OK_CTX)); - a->ptr = NULL; - a->init = 0; - a->flags = 0; - return (1); + return 0; + + ctx = BIO_get_data(a); + + EVP_MD_CTX_free(ctx->md); + OPENSSL_clear_free(ctx, sizeof(BIO_OK_CTX)); + BIO_set_data(a, NULL); + BIO_set_init(a, 0); + + return 1; } static int ok_read(BIO *b, char *out, int outl) { int ret = 0, i, n; BIO_OK_CTX *ctx; + BIO *next; if (out == NULL) - return (0); - ctx = (BIO_OK_CTX *)b->ptr; + return 0; - if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0)) - return (0); + ctx = BIO_get_data(b); + next = BIO_next(b); + + if ((ctx == NULL) || (next == NULL) || (BIO_get_init(b) == 0)) + return 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 */ 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) break; /* nothing new */ @@ -281,21 +289,23 @@ static int ok_read(BIO *b, char *out, int outl) 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; BIO_OK_CTX *ctx; + BIO *next; if (inl <= 0) return inl; - ctx = (BIO_OK_CTX *)b->ptr; + ctx = BIO_get_data(b); + next = BIO_next(b); ret = inl; - if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0)) + if ((ctx == NULL) || (next == NULL) || (BIO_get_init(b) == 0)) return (0); 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); n = ctx->buf_len - ctx->buf_off; 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) { BIO_copy_next_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; long ret = 1; int i; + BIO *next; - ctx = b->ptr; + ctx = BIO_get_data(b); + next = BIO_next(b); switch (cmd) { case BIO_CTRL_RESET: @@ -367,19 +379,19 @@ static long ok_ctrl(BIO *b, int cmd, long num, void *ptr) ctx->finished = 0; ctx->blockout = 0; ctx->sigio = 1; - ret = BIO_ctrl(b->next_bio, cmd, num, ptr); + ret = BIO_ctrl(next, cmd, num, ptr); break; case BIO_CTRL_EOF: /* More to read */ if (ctx->cont <= 0) ret = 1; else - ret = BIO_ctrl(b->next_bio, cmd, num, ptr); + ret = BIO_ctrl(next, 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; if (ret <= 0) - ret = BIO_ctrl(b->next_bio, cmd, num, ptr); + ret = BIO_ctrl(next, cmd, num, ptr); break; case BIO_CTRL_FLUSH: /* do a final write */ @@ -400,11 +412,11 @@ static long ok_ctrl(BIO *b, int cmd, long num, void *ptr) ctx->cont = (int)ret; /* Finally flush the underlying BIO */ - ret = BIO_ctrl(b->next_bio, cmd, num, ptr); + ret = BIO_ctrl(next, 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(next, cmd, num, ptr); BIO_copy_next_retry(b); break; case BIO_CTRL_INFO: @@ -414,34 +426,39 @@ static long ok_ctrl(BIO *b, int cmd, long num, void *ptr) md = ptr; if (!EVP_DigestInit_ex(ctx->md, md, NULL)) return 0; - b->init = 1; + BIO_set_init(b, 1); break; case BIO_C_GET_MD: - if (b->init) { + if (BIO_get_init(b)) { ppmd = ptr; *ppmd = EVP_MD_CTX_md(ctx->md); } else ret = 0; break; default: - ret = BIO_ctrl(b->next_bio, cmd, num, ptr); + ret = BIO_ctrl(next, cmd, num, ptr); break; } - return (ret); + return ret; } static long ok_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) { long ret = 1; + BIO *next; + + next = BIO_next(b); + + if (next == NULL) + return 0; - if (b->next_bio == NULL) - return (0); switch (cmd) { default: - ret = BIO_callback_ctrl(b->next_bio, cmd, fp); + ret = BIO_callback_ctrl(next, cmd, fp); break; } - return (ret); + + return ret; } static void longswap(void *_ptr, size_t len) @@ -472,7 +489,7 @@ static int sig_out(BIO *b) int md_size; void *md_data; - ctx = b->ptr; + ctx = BIO_get_data(b); md = ctx->md; digest = EVP_MD_CTX_md(md); md_size = EVP_MD_size(digest); @@ -516,7 +533,7 @@ static int sig_in(BIO *b) int md_size; void *md_data; - ctx = b->ptr; + ctx = BIO_get_data(b); md = ctx->md; digest = EVP_MD_CTX_md(md); md_size = EVP_MD_size(digest); @@ -562,7 +579,7 @@ static int block_out(BIO *b) const EVP_MD *digest; int md_size; - ctx = b->ptr; + ctx = BIO_get_data(b); md = ctx->md; digest = EVP_MD_CTX_md(md); md_size = EVP_MD_size(digest); @@ -593,7 +610,7 @@ static int block_in(BIO *b) unsigned char tmp[EVP_MAX_MD_SIZE]; int md_size; - ctx = b->ptr; + ctx = BIO_get_data(b); md = ctx->md; md_size = EVP_MD_size(EVP_MD_CTX_md(md)); diff --git a/include/internal/bio.h b/include/internal/bio.h new file mode 100644 index 0000000000..b3352ce80f --- /dev/null +++ b/include/internal/bio.h @@ -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 + +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 *); +}; + + + diff --git a/include/openssl/bio.h b/include/openssl/bio.h index 5389770b6a..09aeea1840 100644 --- a/include/openssl/bio.h +++ b/include/openssl/bio.h @@ -290,70 +290,16 @@ void BIO_set_callback(BIO *b, char *BIO_get_callback_arg(const BIO *b); void BIO_set_callback_arg(BIO *b, char *arg); +typedef struct bio_method_st BIO_METHOD; + const char *BIO_method_name(const BIO *b); int BIO_method_type(const BIO *b); typedef void bio_info_cb (struct bio_st *, int, const char *, int, 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) -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 */ typedef int asn1_ps_func (BIO *b, unsigned char **pbuf, int *plen, void *parg); @@ -635,6 +581,12 @@ BIO *BIO_new_fp(FILE *stream, int close_flag); BIO *BIO_new(const BIO_METHOD *type); int BIO_set(BIO *a, const BIO_METHOD *type); 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); int BIO_up_ref(BIO *a); int BIO_read(BIO *b, void *data, int len); @@ -653,8 +605,10 @@ BIO *BIO_pop(BIO *b); void BIO_free_all(BIO *a); BIO *BIO_find_type(BIO *b, int bio_type); BIO *BIO_next(BIO *b); +void BIO_set_next(BIO *b, BIO *next); BIO *BIO_get_retry_BIO(BIO *bio, int *reason); int BIO_get_retry_reason(BIO *bio); +void BIO_set_retry_reason(BIO *bio, int reason); BIO *BIO_dup_chain(BIO *in); 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))); # 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 */ /* * The following lines are auto generated by the script mkerr.pl. Any changes diff --git a/ssl/bio_ssl.c b/ssl/bio_ssl.c index c3afc57f5f..e3deebc08e 100644 --- a/ssl/bio_ssl.c +++ b/ssl/bio_ssl.c @@ -60,7 +60,7 @@ #include #include #include -#include +#include "internal/bio.h" #include #include "ssl_locl.h" @@ -106,10 +106,12 @@ static int ssl_new(BIO *bi) BIOerr(BIO_F_SSL_NEW, ERR_R_MALLOC_FAILURE); return (0); } - bi->init = 0; - bi->ptr = (char *)bs; - bi->flags = 0; - return (1); + BIO_set_init(bi, 0); + BIO_set_data(bi, bs); + /* Clear all flags */ + BIO_clear_flags(bi, ~0); + + return 1; } static int ssl_free(BIO *a) @@ -118,17 +120,18 @@ static int ssl_free(BIO *a) if (a == NULL) return (0); - bs = (BIO_SSL *)a->ptr; + bs = BIO_get_data(a); if (bs->ssl != NULL) SSL_shutdown(bs->ssl); - if (a->shutdown) { - if (a->init) + if (BIO_get_shutdown(a)) { + if (BIO_get_init(a)) SSL_free(bs->ssl); - a->init = 0; - a->flags = 0; + /* Clear all flags */ + BIO_clear_flags(a, ~0); + BIO_set_init(a, 0); } - OPENSSL_free(a->ptr); - return (1); + OPENSSL_free(bs); + return 1; } 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) return (0); - sb = (BIO_SSL *)b->ptr; + sb = BIO_get_data(b); ssl = sb->ssl; BIO_clear_retry_flags(b); @@ -198,7 +201,7 @@ static int ssl_read(BIO *b, char *out, int outl) break; } - b->retry_reason = retry_reason; + BIO_set_retry_reason(b, retry_reason); return (ret); } @@ -211,7 +214,7 @@ static int ssl_write(BIO *b, const char *out, int outl) if (out == NULL) return (0); - bs = (BIO_SSL *)b->ptr; + bs = BIO_get_data(b); ssl = bs->ssl; BIO_clear_retry_flags(b); @@ -264,18 +267,20 @@ static int ssl_write(BIO *b, const char *out, int outl) break; } - b->retry_reason = retry_reason; - return (ret); + BIO_set_retry_reason(b, retry_reason); + return ret; } static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr) { SSL **sslp, *ssl; - BIO_SSL *bs; + BIO_SSL *bs, *dbs; BIO *dbio, *bio; long ret = 1; + BIO *next; - bs = (BIO_SSL *)b->ptr; + bs = BIO_get_data(b); + next = BIO_next(b); ssl = bs->ssl; if ((ssl == NULL) && (cmd != BIO_C_SET_SSL)) return (0); @@ -293,8 +298,8 @@ static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr) break; } - if (b->next_bio != NULL) - ret = BIO_ctrl(b->next_bio, cmd, num, ptr); + if (next != NULL) + ret = BIO_ctrl(next, cmd, num, ptr); else if (ssl->rbio != NULL) ret = BIO_ctrl(ssl->rbio, cmd, num, ptr); else @@ -330,17 +335,17 @@ static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr) if (!ssl_new(b)) return 0; } - b->shutdown = (int)num; + BIO_set_shutdown(b, num); ssl = (SSL *)ptr; - ((BIO_SSL *)b->ptr)->ssl = ssl; + bs->ssl = ssl; bio = SSL_get_rbio(ssl); if (bio != NULL) { - if (b->next_bio != NULL) - BIO_push(bio, b->next_bio); - b->next_bio = bio; + if (next != NULL) + BIO_push(bio, next); + BIO_set_next(b, bio); BIO_up_ref(bio); } - b->init = 1; + BIO_set_init(b, 1); break; case BIO_C_GET_SSL: if (ptr != NULL) { @@ -350,10 +355,10 @@ static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr) ret = 0; break; case BIO_CTRL_GET_CLOSE: - ret = b->shutdown; + ret = BIO_get_shutdown(b); break; case BIO_CTRL_SET_CLOSE: - b->shutdown = (int)num; + BIO_set_shutdown(b, (int)num); break; case BIO_CTRL_WPENDING: 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); break; case BIO_CTRL_PUSH: - if ((b->next_bio != NULL) && (b->next_bio != ssl->rbio)) { - SSL_set_bio(ssl, b->next_bio, b->next_bio); + if ((next != NULL) && (next != ssl->rbio)) { + SSL_set_bio(ssl, next, next); BIO_up_ref(b); } break; @@ -383,8 +388,8 @@ static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr) */ if (ssl->rbio != ssl->wbio) BIO_free_all(ssl->wbio); - if (b->next_bio != NULL) - BIO_free(b->next_bio); + if (next != NULL) + BIO_free(next); ssl->wbio = 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: BIO_clear_retry_flags(b); - b->retry_reason = 0; + BIO_set_retry_reason(b, 0); ret = (int)SSL_do_handshake(ssl); switch (SSL_get_error(ssl, (int)ret)) { @@ -404,11 +409,11 @@ static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr) break; case SSL_ERROR_WANT_CONNECT: 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; case SSL_ERROR_WANT_X509_LOOKUP: BIO_set_retry_special(b); - b->retry_reason = BIO_RR_SSL_X509_LOOKUP; + BIO_set_retry_reason(b, BIO_RR_SSL_X509_LOOKUP); break; default: break; @@ -416,15 +421,14 @@ static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr) break; case BIO_CTRL_DUP: dbio = (BIO *)ptr; - SSL_free(((BIO_SSL *)dbio->ptr)->ssl); - ((BIO_SSL *)dbio->ptr)->ssl = SSL_dup(ssl); - ((BIO_SSL *)dbio->ptr)->renegotiate_count = - ((BIO_SSL *)b->ptr)->renegotiate_count; - ((BIO_SSL *)dbio->ptr)->byte_count = ((BIO_SSL *)b->ptr)->byte_count; - ((BIO_SSL *)dbio->ptr)->renegotiate_timeout = - ((BIO_SSL *)b->ptr)->renegotiate_timeout; - ((BIO_SSL *)dbio->ptr)->last_time = ((BIO_SSL *)b->ptr)->last_time; - ret = (((BIO_SSL *)dbio->ptr)->ssl != NULL); + dbs = BIO_get_data(dbio); + SSL_free(dbs->ssl); + dbs->ssl = SSL_dup(ssl); + dbs->renegotiate_count = dbs->renegotiate_count; + dbs->byte_count = dbs->byte_count; + dbs->renegotiate_timeout = dbs->renegotiate_timeout; + dbs->last_time = dbs->last_time; + ret = (dbs->ssl != NULL); break; case BIO_C_GET_FD: 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; long ret = 1; - bs = (BIO_SSL *)b->ptr; + bs = BIO_get_data(b); ssl = bs->ssl; switch (cmd) { 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) { + BIO_SSL *tdata, *fdata; t = BIO_find_type(t, BIO_TYPE_SSL); f = BIO_find_type(f, BIO_TYPE_SSL); 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); - if ((((BIO_SSL *)t->ptr)->ssl == NULL) || - (((BIO_SSL *)f->ptr)->ssl == NULL)) - return (0); - if (!SSL_copy_session_id(((BIO_SSL *)t->ptr)->ssl, ((BIO_SSL *)f->ptr)->ssl)) + if (!SSL_copy_session_id(tdata->ssl, (fdata->ssl))) return 0; return (1); } @@ -564,12 +570,10 @@ void BIO_ssl_shutdown(BIO *b) { SSL *s; - while (b != NULL) { - if (b->method->type == BIO_TYPE_SSL) { - s = ((BIO_SSL *)b->ptr)->ssl; - SSL_shutdown(s); - break; - } - b = b->next_bio; - } + b = BIO_find_type(b, BIO_TYPE_SSL); + if (b == NULL) + return; + + s = BIO_get_data(b); + SSL_shutdown(s); } diff --git a/ssl/ssl_lib.c b/ssl/ssl_lib.c index 3385bc1508..e6511895ce 100644 --- a/ssl/ssl_lib.c +++ b/ssl/ssl_lib.c @@ -1105,8 +1105,8 @@ void SSL_set_wbio(SSL *s, BIO *wbio) */ if (s->bbio != NULL) { if (s->wbio == s->bbio) { - s->wbio = s->wbio->next_bio; - s->bbio->next_bio = NULL; + s->wbio = BIO_next(s->wbio); + BIO_set_next(s->bbio, NULL); } } if (s->wbio != wbio && s->rbio != s->wbio) -- 2.25.1