Skip to content

Commit

Permalink
Give BIO an ex_data
Browse files Browse the repository at this point in the history
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
Homebrew/homebrew-core#155651

Change-Id: I5bf226cc3506a114cd62f885a8c15006512dfc65
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/64227
Auto-Submit: David Benjamin <davidben@google.com>
Reviewed-by: Bob Beck <bbe@google.com>
Commit-Queue: Bob Beck <bbe@google.com>
(cherry picked from commit 2139aba2e3e28cd1cdefbd9b48e2c31a75441203)
  • Loading branch information
davidben authored and samuel40791765 committed Nov 29, 2023
1 parent b8f1f94 commit e851068
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
25 changes: 25 additions & 0 deletions crypto/bio/bio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
24 changes: 24 additions & 0 deletions include/openssl/bio.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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|,
Expand Down

0 comments on commit e851068

Please sign in to comment.