From a146ae55ba479a5c7aa2a6afba1b2b93102a152c Mon Sep 17 00:00:00 2001 From: Matt Caswell Date: Tue, 22 Mar 2016 09:21:29 +0000 Subject: 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 +++++++++++++++++++++++++++-------------------- 18 files changed, 361 insertions(+), 200 deletions(-) (limited to 'crypto') 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)); -- cgit v1.2.3