diff options
author | Tomas Mraz <tomas@openssl.org> | 2021-05-24 18:15:57 +0200 |
---|---|---|
committer | Tomas Mraz <tomas@openssl.org> | 2021-05-26 17:18:34 +0200 |
commit | 0800318a0c1f80ed838838951b0478cb977d40a6 (patch) | |
tree | 686267909ca13431b484c0c8e5a9a09fcc1b4537 /crypto | |
parent | 022411112dd19420f0df51818322a216d914a0fb (diff) |
Deprecate old style BIO callback calls
New style BIO_debug_callback_ex() function added to provide
replacement for BIO_debug_callback().
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15440)
Diffstat (limited to 'crypto')
-rw-r--r-- | crypto/bio/bio_cb.c | 70 | ||||
-rw-r--r-- | crypto/bio/bio_lib.c | 44 | ||||
-rw-r--r-- | crypto/bio/bio_local.h | 2 | ||||
-rw-r--r-- | crypto/bio/bss_acpt.c | 6 | ||||
-rw-r--r-- | crypto/evp/bio_enc.c | 31 |
5 files changed, 104 insertions, 49 deletions
diff --git a/crypto/bio/bio_cb.c b/crypto/bio/bio_cb.c index 154fb5c9f0..dfe15c5f24 100644 --- a/crypto/bio/bio_cb.c +++ b/crypto/bio/bio_cb.c @@ -7,6 +7,8 @@ * https://www.openssl.org/source/license.html */ +#define OPENSSL_SUPPRESS_DEPRECATED + #include <stdio.h> #include <string.h> #include <stdlib.h> @@ -14,25 +16,25 @@ #include "internal/cryptlib.h" #include <openssl/err.h> -long BIO_debug_callback(BIO *bio, int cmd, const char *argp, - int argi, long argl, long ret) +long BIO_debug_callback_ex(BIO *bio, int cmd, const char *argp, size_t len, + int argi, long argl, int ret, size_t *processed) { BIO *b; char buf[256]; char *p; - long r = 1; - int len, left; + int left; + size_t l = 0; - if (BIO_CB_RETURN & cmd) - r = ret; + if (processed != NULL) + l = *processed; - len = BIO_snprintf(buf, sizeof(buf), "BIO[%p]: ", (void *)bio); + left = BIO_snprintf(buf, sizeof(buf), "BIO[%p]: ", (void *)bio); /* Ignore errors and continue printing the other information. */ - if (len < 0) - len = 0; - p = buf + len; - left = sizeof(buf) - len; + if (left < 0) + left = 0; + p = buf + left; + left = sizeof(buf) - left; switch (cmd) { case BIO_CB_FREE: @@ -40,47 +42,47 @@ long BIO_debug_callback(BIO *bio, int cmd, const char *argp, break; case BIO_CB_READ: if (bio->method->type & BIO_TYPE_DESCRIPTOR) - BIO_snprintf(p, left, "read(%d,%lu) - %s fd=%d\n", - bio->num, (unsigned long)argi, + BIO_snprintf(p, left, "read(%d,%zu) - %s fd=%d\n", + bio->num, len, bio->method->name, bio->num); else - BIO_snprintf(p, left, "read(%d,%lu) - %s\n", - bio->num, (unsigned long)argi, bio->method->name); + BIO_snprintf(p, left, "read(%d,%zu) - %s\n", + bio->num, len, bio->method->name); break; case BIO_CB_WRITE: if (bio->method->type & BIO_TYPE_DESCRIPTOR) - BIO_snprintf(p, left, "write(%d,%lu) - %s fd=%d\n", - bio->num, (unsigned long)argi, + BIO_snprintf(p, left, "write(%d,%zu) - %s fd=%d\n", + bio->num, len, bio->method->name, bio->num); else - BIO_snprintf(p, left, "write(%d,%lu) - %s\n", - bio->num, (unsigned long)argi, bio->method->name); + BIO_snprintf(p, left, "write(%d,%zu) - %s\n", + bio->num, len, bio->method->name); break; case BIO_CB_PUTS: BIO_snprintf(p, left, "puts() - %s\n", bio->method->name); break; case BIO_CB_GETS: - BIO_snprintf(p, left, "gets(%lu) - %s\n", (unsigned long)argi, + BIO_snprintf(p, left, "gets(%zu) - %s\n", len, bio->method->name); break; case BIO_CB_CTRL: - BIO_snprintf(p, left, "ctrl(%lu) - %s\n", (unsigned long)argi, + BIO_snprintf(p, left, "ctrl(%d) - %s\n", argi, bio->method->name); break; case BIO_CB_RETURN | BIO_CB_READ: - BIO_snprintf(p, left, "read return %ld\n", ret); + BIO_snprintf(p, left, "read return %d processed: %zu\n", ret, l); break; case BIO_CB_RETURN | BIO_CB_WRITE: - BIO_snprintf(p, left, "write return %ld\n", ret); + BIO_snprintf(p, left, "write return %d processed: %zu\n", ret, l); break; case BIO_CB_RETURN | BIO_CB_GETS: - BIO_snprintf(p, left, "gets return %ld\n", ret); + BIO_snprintf(p, left, "gets return %d processed: %zu\n", ret, l); break; case BIO_CB_RETURN | BIO_CB_PUTS: - BIO_snprintf(p, left, "puts return %ld\n", ret); + BIO_snprintf(p, left, "puts return %d processed: %zu\n", ret, l); break; case BIO_CB_RETURN | BIO_CB_CTRL: - BIO_snprintf(p, left, "ctrl return %ld\n", ret); + BIO_snprintf(p, left, "ctrl return %d\n", ret); break; default: BIO_snprintf(p, left, "bio callback - unknown type (%d)\n", cmd); @@ -94,5 +96,19 @@ long BIO_debug_callback(BIO *bio, int cmd, const char *argp, else fputs(buf, stderr); #endif - return r; + return ret; +} + +#ifndef OPENSSL_NO_DEPRECATED_3_0 +long BIO_debug_callback(BIO *bio, int cmd, const char *argp, + int argi, long argl, long ret) +{ + size_t processed = 0; + + if (ret > 0) + processed = (size_t)ret; + BIO_debug_callback_ex(bio, cmd, argp, (size_t)argi, + argi, argl, ret > 0 ? 1 : (int)ret, &processed); + return ret; } +#endif diff --git a/crypto/bio/bio_lib.c b/crypto/bio/bio_lib.c index 9f25376e95..91db2290c4 100644 --- a/crypto/bio/bio_lib.c +++ b/crypto/bio/bio_lib.c @@ -7,6 +7,8 @@ * https://www.openssl.org/source/license.html */ +#define OPENSSL_SUPPRESS_DEPRECATED + #include <stdio.h> #include <errno.h> #include <openssl/crypto.h> @@ -19,6 +21,11 @@ #define HAS_LEN_OPER(o) ((o) == BIO_CB_READ || (o) == BIO_CB_WRITE \ || (o) == BIO_CB_GETS) +#ifndef OPENSSL_NO_DEPRECATED_3_0 +# define HAS_CALLBACK(b) ((b)->callback != NULL || (b)->callback_ex != NULL) +#else +# define HAS_CALLBACK(b) ((b)->callback_ex != NULL) +#endif /* * Helper function to work out whether to call the new style callback or the old * one, and translate between the two. @@ -30,12 +37,15 @@ static long bio_call_callback(BIO *b, int oper, const char *argp, size_t len, int argi, long argl, long inret, size_t *processed) { - long ret; + long ret = inret; +#ifndef OPENSSL_NO_DEPRECATED_3_0 int bareoper; if (b->callback_ex != NULL) +#endif return b->callback_ex(b, oper, argp, len, argi, argl, inret, processed); +#ifndef OPENSSL_NO_DEPRECATED_3_0 /* Strip off any BIO_CB_RETURN flag */ bareoper = oper & ~BIO_CB_RETURN; @@ -63,7 +73,7 @@ static long bio_call_callback(BIO *b, int oper, const char *argp, size_t len, *processed = (size_t)ret; ret = 1; } - +#endif return ret; } @@ -127,7 +137,7 @@ int BIO_free(BIO *a) return 1; REF_ASSERT_ISNT(ret < 0); - if (a->callback != NULL || a->callback_ex != NULL) { + if (HAS_CALLBACK(a)) { ret = (int)bio_call_callback(a, BIO_CB_FREE, NULL, 0, 0, 0L, 1L, NULL); if (ret <= 0) return ret; @@ -207,6 +217,7 @@ void BIO_set_flags(BIO *b, int flags) b->flags |= flags; } +#ifndef OPENSSL_NO_DEPRECATED_3_0 BIO_callback_fn BIO_get_callback(const BIO *b) { return b->callback; @@ -216,6 +227,7 @@ void BIO_set_callback(BIO *b, BIO_callback_fn cb) { b->callback = cb; } +#endif BIO_callback_fn_ex BIO_get_callback_ex(const BIO *b) { @@ -266,7 +278,7 @@ static int bio_read_intern(BIO *b, void *data, size_t dlen, size_t *readbytes) return -2; } - if ((b->callback != NULL || b->callback_ex != NULL) && + if (HAS_CALLBACK(b) && ((ret = (int)bio_call_callback(b, BIO_CB_READ, data, dlen, 0, 0L, 1L, NULL)) <= 0)) return ret; @@ -281,7 +293,7 @@ static int bio_read_intern(BIO *b, void *data, size_t dlen, size_t *readbytes) if (ret > 0) b->num_read += (uint64_t)*readbytes; - if (b->callback != NULL || b->callback_ex != NULL) + if (HAS_CALLBACK(b)) ret = (int)bio_call_callback(b, BIO_CB_READ | BIO_CB_RETURN, data, dlen, 0, 0L, ret, readbytes); @@ -331,7 +343,7 @@ static int bio_write_intern(BIO *b, const void *data, size_t dlen, return -2; } - if ((b->callback != NULL || b->callback_ex != NULL) && + if (HAS_CALLBACK(b) && ((ret = (int)bio_call_callback(b, BIO_CB_WRITE, data, dlen, 0, 0L, 1L, NULL)) <= 0)) return ret; @@ -346,7 +358,7 @@ static int bio_write_intern(BIO *b, const void *data, size_t dlen, if (ret > 0) b->num_write += (uint64_t)*written; - if (b->callback != NULL || b->callback_ex != NULL) + if (HAS_CALLBACK(b)) ret = (int)bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN, data, dlen, 0, 0L, ret, written); @@ -390,7 +402,7 @@ int BIO_puts(BIO *b, const char *buf) return -2; } - if (b->callback != NULL || b->callback_ex != NULL) { + if (HAS_CALLBACK(b)) { ret = (int)bio_call_callback(b, BIO_CB_PUTS, buf, 0, 0, 0L, 1L, NULL); if (ret <= 0) return ret; @@ -409,7 +421,7 @@ int BIO_puts(BIO *b, const char *buf) ret = 1; } - if (b->callback != NULL || b->callback_ex != NULL) + if (HAS_CALLBACK(b)) ret = (int)bio_call_callback(b, BIO_CB_PUTS | BIO_CB_RETURN, buf, 0, 0, 0L, ret, &written); @@ -444,7 +456,7 @@ int BIO_gets(BIO *b, char *buf, int size) return -1; } - if (b->callback != NULL || b->callback_ex != NULL) { + if (HAS_CALLBACK(b)) { ret = (int)bio_call_callback(b, BIO_CB_GETS, buf, size, 0, 0L, 1, NULL); if (ret <= 0) return ret; @@ -462,7 +474,7 @@ int BIO_gets(BIO *b, char *buf, int size) ret = 1; } - if (b->callback != NULL || b->callback_ex != NULL) + if (HAS_CALLBACK(b)) ret = (int)bio_call_callback(b, BIO_CB_GETS | BIO_CB_RETURN, buf, size, 0, 0L, ret, &readbytes); @@ -551,7 +563,7 @@ long BIO_ctrl(BIO *b, int cmd, long larg, void *parg) return -2; } - if (b->callback != NULL || b->callback_ex != NULL) { + if (HAS_CALLBACK(b)) { ret = bio_call_callback(b, BIO_CB_CTRL, parg, 0, cmd, larg, 1L, NULL); if (ret <= 0) return ret; @@ -559,7 +571,7 @@ long BIO_ctrl(BIO *b, int cmd, long larg, void *parg) ret = b->method->ctrl(b, cmd, larg, parg); - if (b->callback != NULL || b->callback_ex != NULL) + if (HAS_CALLBACK(b)) ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, 0, cmd, larg, ret, NULL); @@ -580,7 +592,7 @@ long BIO_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp) return -2; } - if (b->callback != NULL || b->callback_ex != NULL) { + if (HAS_CALLBACK(b)) { ret = bio_call_callback(b, BIO_CB_CTRL, (void *)&fp, 0, cmd, 0, 1L, NULL); if (ret <= 0) @@ -589,7 +601,7 @@ long BIO_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp) ret = b->method->callback_ctrl(b, cmd, fp); - if (b->callback != NULL || b->callback_ex != NULL) + if (HAS_CALLBACK(b)) ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, 0, cmd, 0, ret, NULL); @@ -742,7 +754,9 @@ BIO *BIO_dup_chain(BIO *in) for (bio = in; bio != NULL; bio = bio->next_bio) { if ((new_bio = BIO_new(bio->method)) == NULL) goto err; +#ifndef OPENSSL_NO_DEPRECATED_3_0 new_bio->callback = bio->callback; +#endif new_bio->callback_ex = bio->callback_ex; new_bio->cb_arg = bio->cb_arg; new_bio->init = bio->init; diff --git a/crypto/bio/bio_local.h b/crypto/bio/bio_local.h index 581b19c0c1..134ad748a1 100644 --- a/crypto/bio/bio_local.h +++ b/crypto/bio/bio_local.h @@ -116,7 +116,9 @@ struct bio_st { OSSL_LIB_CTX *libctx; const BIO_METHOD *method; /* bio, mode, argp, argi, argl, ret */ +#ifndef OPENSSL_NO_DEPRECATED_3_0 BIO_callback_fn callback; +#endif BIO_callback_fn_ex callback_ex; char *cb_arg; /* first argument for the callback */ int init; diff --git a/crypto/bio/bss_acpt.c b/crypto/bio/bss_acpt.c index 5b776224d6..aff92223af 100644 --- a/crypto/bio/bss_acpt.c +++ b/crypto/bio/bss_acpt.c @@ -7,6 +7,8 @@ * https://www.openssl.org/source/license.html */ +#define OPENSSL_SUPPRESS_DEPRECATED + #include <stdio.h> #include <errno.h> #include "bio_local.h" @@ -305,9 +307,11 @@ static int acpt_state(BIO *b, BIO_ACCEPT *c) if (bio == NULL) goto exit_loop; + BIO_set_callback_ex(bio, BIO_get_callback_ex(b)); +#ifndef OPENSSL_NO_DEPRECATED_3_0 BIO_set_callback(bio, BIO_get_callback(b)); +#endif BIO_set_callback_arg(bio, BIO_get_callback_arg(b)); - /* * If the accept BIO has an bio_chain, we dup it and put the new * socket at the end. diff --git a/crypto/evp/bio_enc.c b/crypto/evp/bio_enc.c index df669245f3..9d7a9eafef 100644 --- a/crypto/evp/bio_enc.c +++ b/crypto/evp/bio_enc.c @@ -7,6 +7,8 @@ * https://www.openssl.org/source/license.html */ +#define OPENSSL_SUPPRESS_DEPRECATED /* for BIO_get_callback */ + #include <stdio.h> #include <errno.h> #include "internal/cryptlib.h" @@ -392,7 +394,7 @@ static long enc_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp) if (next == NULL) return 0; - + return BIO_callback_ctrl(next, cmd, fp); } @@ -400,25 +402,42 @@ 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); + BIO_callback_fn_ex callback_ex; +#ifndef OPENSSL_NO_DEPRECATED_3_0 + long (*callback) (struct bio_st *, int, const char *, int, long, long) = NULL; +#endif ctx = BIO_get_data(b); if (ctx == NULL) return 0; - callback = BIO_get_callback(b); + if ((callback_ex = BIO_get_callback_ex(b)) != NULL) { + if (callback_ex(b, BIO_CB_CTRL, (const char *)c, 0, BIO_CTRL_SET, + e, 1, NULL) <= 0) + return 0; + } +#ifndef OPENSSL_NO_DEPRECATED_3_0 + else { + callback = BIO_get_callback(b); - if ((callback != NULL) && + if ((callback != NULL) && (callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 0L) <= 0)) - return 0; + return 0; + } +#endif BIO_set_init(b, 1); if (!EVP_CipherInit_ex(ctx->cipher, c, NULL, k, i, e)) return 0; - if (callback != NULL) + if (callback_ex != NULL) + return callback_ex(b, BIO_CB_CTRL | BIO_CB_RETURN, (const char *)c, 0, + BIO_CTRL_SET, e, 1, NULL); +#ifndef OPENSSL_NO_DEPRECATED_3_0 + else if (callback != NULL) return callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 1L); +#endif return 1; } |