From 4861a03c3f18b876ad1c5fbb7a2676e1e4efc34d Mon Sep 17 00:00:00 2001 From: David Benjamin Date: Tue, 28 Nov 2023 09:40:56 -0500 Subject: [PATCH] Give BIO an ex_data It has one in upstream OpenSSL. The most recent OpenSSL release is hitting a compatibility issue with postgres, which seems like it'll get fixed by postgres using BIO_get_app_data. Add it on our end too. https://www.postgresql.org/message-id/CAN55FZ1eDDYsYaL7mv%2BoSLUij2h_u6hvD4Qmv-7PK7jkji0uyQ%40mail.gmail.com https://github.com/Homebrew/homebrew-core/issues/155651 Change-Id: I5bf226cc3506a114cd62f885a8c15006512dfc65 Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/64227 Auto-Submit: David Benjamin Reviewed-by: Bob Beck Commit-Queue: Bob Beck (cherry picked from commit 2139aba2e3e28cd1cdefbd9b48e2c31a75441203) --- crypto/bio/bio.c | 25 +++++++++++++++++++++++++ include/openssl/bio.h | 24 ++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/crypto/bio/bio.c b/crypto/bio/bio.c index c2eff5e4e9..d179c507eb 100644 --- a/crypto/bio/bio.c +++ b/crypto/bio/bio.c @@ -94,6 +94,9 @@ static int call_bio_callback_with_processed(BIO *bio, const int oper, return ret; } +static CRYPTO_EX_DATA_CLASS g_ex_data_class = + CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA; + BIO *BIO_new(const BIO_METHOD *method) { BIO *ret = OPENSSL_malloc(sizeof(BIO)); if (ret == NULL) { @@ -105,6 +108,7 @@ BIO *BIO_new(const BIO_METHOD *method) { ret->shutdown = 1; ret->references = 1; ret->callback_ex = NULL; + CRYPTO_new_ex_data(&ret->ex_data); if (method->create != NULL && !method->create(ret)) { OPENSSL_free(ret); @@ -134,6 +138,7 @@ int BIO_free(BIO *bio) { } } + CRYPTO_free_ex_data(&g_ex_data_class, bio, &bio->ex_data); OPENSSL_free(bio); } return 1; @@ -803,3 +808,23 @@ void BIO_set_callback_arg(BIO *bio, char *arg) { char *BIO_get_callback_arg(const BIO *bio) { return bio->cb_arg; } + +int BIO_get_ex_new_index(long argl, void *argp, + CRYPTO_EX_unused *unused, + CRYPTO_EX_dup *dup_unused, + CRYPTO_EX_free *free_func) { + int index; + if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, + free_func)) { + return -1; + } + return index; +} + +int BIO_set_ex_data(BIO *bio, int idx, void *data) { + return CRYPTO_set_ex_data(&bio->ex_data, idx, data); +} + +void *BIO_get_ex_data(const BIO *bio, int idx) { + return CRYPTO_get_ex_data(&bio->ex_data, idx); +} diff --git a/include/openssl/bio.h b/include/openssl/bio.h index 5b9608bc37..93e64c33ff 100644 --- a/include/openssl/bio.h +++ b/include/openssl/bio.h @@ -755,9 +755,17 @@ OPENSSL_EXPORT long (*BIO_meth_get_callback_ctrl(const BIO_METHOD *method)) (BIO // BIO_set_data sets custom data on |bio|. It may be retried with // |BIO_get_data|. +// +// This function should only be called by the implementation of a custom |BIO|. +// In particular, the data pointer of a built-in |BIO| is private to the +// library. For other uses, see |BIO_set_ex_data| and |BIO_set_app_data|. OPENSSL_EXPORT void BIO_set_data(BIO *bio, void *ptr); // BIO_get_data returns custom data on |bio| set by |BIO_get_data|. +// +// This function should only be called by the implementation of a custom |BIO|. +// In particular, the data pointer of a built-in |BIO| is private to the +// library. For other uses, see |BIO_get_ex_data| and |BIO_get_app_data|. OPENSSL_EXPORT void *BIO_get_data(BIO *bio); // BIO_set_init sets whether |bio| has been fully initialized. Until fully @@ -813,6 +821,21 @@ OPENSSL_EXPORT int BIO_get_init(BIO *bio); #define BIO_CTRL_SET_FILENAME 30 +// ex_data functions. +// +// See |ex_data.h| for details. + +OPENSSL_EXPORT int BIO_get_ex_new_index(long argl, void *argp, + CRYPTO_EX_unused *unused, + CRYPTO_EX_dup *dup_unused, + CRYPTO_EX_free *free_func); +OPENSSL_EXPORT int BIO_set_ex_data(BIO *bio, int idx, void *arg); +OPENSSL_EXPORT void *BIO_get_ex_data(const BIO *bio, int idx); + +#define BIO_set_app_data(bio, arg) (BIO_set_ex_data(bio, 0, (char *)(arg))) +#define BIO_get_app_data(bio) (BIO_get_ex_data(bio, 0)) + + // Deprecated functions. // BIO_f_base64 returns a filter |BIO| that base64-encodes data written into @@ -906,6 +929,7 @@ struct bio_method_st { struct bio_st { const BIO_METHOD *method; + CRYPTO_EX_DATA ex_data; // If set, |BIO_read|, |BIO_write|, and |BIO_free| execute |callback_ex|. // Callbacks are only called with for the following events: |BIO_CB_READ|,