diff options
author | Matt Caswell <matt@openssl.org> | 2016-03-22 09:21:29 +0000 |
---|---|---|
committer | Matt Caswell <matt@openssl.org> | 2016-03-29 17:40:54 +0100 |
commit | a146ae55ba479a5c7aa2a6afba1b2b93102a152c (patch) | |
tree | 4c0803ebfed56f2a4ff59725a763cdc78bcf0379 /crypto | |
parent | f334461facc4078adbad6552563f77799c174cab (diff) |
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>
Diffstat (limited to 'crypto')
-rw-r--r-- | crypto/asn1/asn_mime.c | 1 | ||||
-rw-r--r-- | crypto/asn1/bio_asn1.c | 67 | ||||
-rw-r--r-- | crypto/bio/bf_buff.c | 2 | ||||
-rw-r--r-- | crypto/bio/bf_nbio.c | 2 | ||||
-rw-r--r-- | crypto/bio/bf_null.c | 2 | ||||
-rw-r--r-- | crypto/bio/bio_cb.c | 2 | ||||
-rw-r--r-- | crypto/bio/bio_lcl.h | 45 | ||||
-rw-r--r-- | crypto/bio/bio_lib.c | 42 | ||||
-rw-r--r-- | crypto/bio/bio_meth.c | 8 | ||||
-rw-r--r-- | crypto/bio/bss_bio.c | 2 | ||||
-rw-r--r-- | crypto/bio/bss_log.c | 1 | ||||
-rw-r--r-- | crypto/bio/bss_mem.c | 2 | ||||
-rw-r--r-- | crypto/bio/bss_null.c | 2 | ||||
-rw-r--r-- | crypto/bio/bss_sock.c | 1 | ||||
-rw-r--r-- | crypto/evp/bio_b64.c | 85 | ||||
-rw-r--r-- | crypto/evp/bio_enc.c | 100 | ||||
-rw-r--r-- | crypto/evp/bio_md.c | 100 | ||||
-rw-r--r-- | crypto/evp/bio_ok.c | 97 |
18 files changed, 361 insertions, 200 deletions
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 <openssl/asn1.h> #include <openssl/asn1t.h> #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 <string.h> -#include <openssl/bio.h> +#include <internal/bio.h> #include <openssl/asn1.h> /* 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 <stdio.h> #include <errno.h> +#include "bio_lcl.h" #include "internal/cryptlib.h" -#include <openssl/bio.h> 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 <stdio.h> #include <errno.h> +#include "bio_lcl.h" #include "internal/cryptlib.h" #include <openssl/rand.h> -#include <openssl/bio.h> /* * 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 <stdio.h> #include <errno.h> +#include "bio_lcl.h" #include "internal/cryptlib.h" -#include <openssl/bio.h> /* * 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 <stdio.h> #include <string.h> #include <stdlib.h> +#include "bio_lcl.h" #include "internal/cryptlib.h" -#include <openssl/bio.h> #include <openssl/err.h> 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 <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 # 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 <stdio.h> #include <errno.h> #include <openssl/crypto.h> +#include "bio_lcl.h" #include "internal/cryptlib.h" -#include <openssl/bio.h> #include <openssl/stack.h> 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 <stdlib.h> #include <string.h> -#include <openssl/bio.h> +#include "bio_lcl.h" #include <openssl/err.h> #include <openssl/crypto.h> 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 <stdio.h> #include <errno.h> +#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 <stdio.h> #include <errno.h> +#include "bio_lcl.h" #include "internal/cryptlib.h" -#include <openssl/bio.h> 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 <stdio.h> #include <errno.h> +#include "bio_lcl.h" #include "internal/cryptlib.h" -#include <openssl/bio.h> 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 <stdio.h> #include <errno.h> #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 <openssl/buffer.h> #include <openssl/evp.h> +#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 <openssl/buffer.h> #include <openssl/evp.h> +#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: |