summaryrefslogtreecommitdiffstats
path: root/crypto/bio
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/bio')
-rw-r--r--crypto/bio/bio_lib.c8
-rw-r--r--crypto/bio/bio_local.h1
-rw-r--r--crypto/bio/bss_core.c170
-rw-r--r--crypto/bio/build.info2
4 files changed, 179 insertions, 2 deletions
diff --git a/crypto/bio/bio_lib.c b/crypto/bio/bio_lib.c
index 6d360b62ed..5cdd6d7cfd 100644
--- a/crypto/bio/bio_lib.c
+++ b/crypto/bio/bio_lib.c
@@ -68,7 +68,7 @@ static long bio_call_callback(BIO *b, int oper, const char *argp, size_t len,
return ret;
}
-BIO *BIO_new(const BIO_METHOD *method)
+BIO *BIO_new_ex(OSSL_LIB_CTX *libctx, const BIO_METHOD *method)
{
BIO *bio = OPENSSL_zalloc(sizeof(*bio));
@@ -77,6 +77,7 @@ BIO *BIO_new(const BIO_METHOD *method)
return NULL;
}
+ bio->libctx = libctx;
bio->method = method;
bio->shutdown = 1;
bio->references = 1;
@@ -107,6 +108,11 @@ err:
return NULL;
}
+BIO *BIO_new(const BIO_METHOD *method)
+{
+ return BIO_new_ex(NULL, method);
+}
+
int BIO_free(BIO *a)
{
int ret;
diff --git a/crypto/bio/bio_local.h b/crypto/bio/bio_local.h
index 30e56cba8d..3d9afe0760 100644
--- a/crypto/bio/bio_local.h
+++ b/crypto/bio/bio_local.h
@@ -113,6 +113,7 @@ typedef struct bio_f_buffer_ctx_struct {
} BIO_F_BUFFER_CTX;
struct bio_st {
+ OSSL_LIB_CTX *libctx;
const BIO_METHOD *method;
/* bio, mode, argp, argi, argl, ret */
BIO_callback_fn callback;
diff --git a/crypto/bio/bss_core.c b/crypto/bio/bss_core.c
new file mode 100644
index 0000000000..2baabe614e
--- /dev/null
+++ b/crypto/bio/bss_core.c
@@ -0,0 +1,170 @@
+/*
+ * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
+ *
+ * Licensed under the Apache License 2.0 (the "License"). You may not use
+ * this file except in compliance with the License. You can obtain a copy
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
+ */
+
+#include <openssl/core_dispatch.h>
+#include "bio_local.h"
+#include "internal/cryptlib.h"
+
+typedef struct {
+ OSSL_FUNC_BIO_read_ex_fn *c_bio_read_ex;
+ OSSL_FUNC_BIO_write_ex_fn *c_bio_write_ex;
+ OSSL_FUNC_BIO_gets_fn *c_bio_gets;
+ OSSL_FUNC_BIO_puts_fn *c_bio_puts;
+ OSSL_FUNC_BIO_ctrl_fn *c_bio_ctrl;
+} BIO_CORE_GLOBALS;
+
+static void bio_core_globals_free(void *vbcg)
+{
+ OPENSSL_free(vbcg);
+}
+
+static void *bio_core_globals_new(OSSL_LIB_CTX *ctx)
+{
+ return OPENSSL_zalloc(sizeof(BIO_CORE_GLOBALS));
+}
+
+static const OSSL_LIB_CTX_METHOD bio_core_globals_method = {
+ bio_core_globals_new,
+ bio_core_globals_free,
+};
+
+static ossl_inline BIO_CORE_GLOBALS *get_globals(OSSL_LIB_CTX *libctx)
+{
+ return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_BIO_CORE_INDEX,
+ &bio_core_globals_method);
+}
+
+static int bio_core_read_ex(BIO *bio, char *data, size_t data_len,
+ size_t *bytes_read)
+{
+ BIO_CORE_GLOBALS *bcgbl = get_globals(bio->libctx);
+
+ if (bcgbl->c_bio_read_ex == NULL)
+ return 0;
+ return bcgbl->c_bio_read_ex(BIO_get_data(bio), data, data_len, bytes_read);
+}
+
+static int bio_core_write_ex(BIO *bio, const char *data, size_t data_len,
+ size_t *written)
+{
+ BIO_CORE_GLOBALS *bcgbl = get_globals(bio->libctx);
+
+ if (bcgbl->c_bio_write_ex == NULL)
+ return 0;
+ return bcgbl->c_bio_write_ex(BIO_get_data(bio), data, data_len, written);
+}
+
+static long bio_core_ctrl(BIO *bio, int cmd, long num, void *ptr)
+{
+ BIO_CORE_GLOBALS *bcgbl = get_globals(bio->libctx);
+
+ if (bcgbl->c_bio_ctrl == NULL)
+ return -1;
+ return bcgbl->c_bio_ctrl(BIO_get_data(bio), cmd, num, ptr);
+}
+
+static int bio_core_gets(BIO *bio, char *buf, int size)
+{
+ BIO_CORE_GLOBALS *bcgbl = get_globals(bio->libctx);
+
+ if (bcgbl->c_bio_gets == NULL)
+ return -1;
+ return bcgbl->c_bio_gets(BIO_get_data(bio), buf, size);
+}
+
+static int bio_core_puts(BIO *bio, const char *str)
+{
+ BIO_CORE_GLOBALS *bcgbl = get_globals(bio->libctx);
+
+ if (bcgbl->c_bio_puts == NULL)
+ return -1;
+ return bcgbl->c_bio_puts(BIO_get_data(bio), str);
+}
+
+static int bio_core_new(BIO *bio)
+{
+ BIO_set_init(bio, 1);
+
+ return 1;
+}
+
+static int bio_core_free(BIO *bio)
+{
+ BIO_set_init(bio, 0);
+
+ return 1;
+}
+
+static const BIO_METHOD corebiometh = {
+ BIO_TYPE_CORE_TO_PROV,
+ "BIO to Core filter",
+ bio_core_write_ex,
+ NULL,
+ bio_core_read_ex,
+ NULL,
+ bio_core_puts,
+ bio_core_gets,
+ bio_core_ctrl,
+ bio_core_new,
+ bio_core_free,
+ NULL,
+};
+
+const BIO_METHOD *BIO_s_core(void)
+{
+ return &corebiometh;
+}
+
+BIO *BIO_new_from_core_bio(OSSL_LIB_CTX *libctx, OSSL_CORE_BIO *corebio)
+{
+ BIO *outbio;
+ BIO_CORE_GLOBALS *bcgbl = get_globals(libctx);
+
+ /* Check the library context has been initialised with the callbacks */
+ if (bcgbl->c_bio_write_ex == NULL && bcgbl->c_bio_read_ex == NULL)
+ return NULL;
+
+ if ((outbio = BIO_new_ex(libctx, BIO_s_core())) == NULL)
+ return NULL;
+
+ BIO_set_data(outbio, corebio);
+ return outbio;
+}
+
+int ossl_bio_init_core(OSSL_LIB_CTX *libctx, const OSSL_DISPATCH *fns)
+{
+ BIO_CORE_GLOBALS *bcgbl = get_globals(libctx);
+
+ for (; fns->function_id != 0; fns++) {
+ switch (fns->function_id) {
+ case OSSL_FUNC_BIO_READ_EX:
+ if (bcgbl->c_bio_read_ex == NULL)
+ bcgbl->c_bio_read_ex = OSSL_FUNC_BIO_read_ex(fns);
+ break;
+ case OSSL_FUNC_BIO_WRITE_EX:
+ if (bcgbl->c_bio_write_ex == NULL)
+ bcgbl->c_bio_write_ex = OSSL_FUNC_BIO_write_ex(fns);
+ break;
+ case OSSL_FUNC_BIO_GETS:
+ if (bcgbl->c_bio_gets == NULL)
+ bcgbl->c_bio_gets = OSSL_FUNC_BIO_gets(fns);
+ break;
+ case OSSL_FUNC_BIO_PUTS:
+ if (bcgbl->c_bio_puts == NULL)
+ bcgbl->c_bio_puts = OSSL_FUNC_BIO_puts(fns);
+ break;
+ case OSSL_FUNC_BIO_CTRL:
+ if (bcgbl->c_bio_ctrl == NULL)
+ bcgbl->c_bio_ctrl = OSSL_FUNC_BIO_ctrl(fns);
+ break;
+ }
+ }
+
+ return 1;
+}
diff --git a/crypto/bio/build.info b/crypto/bio/build.info
index 2bee64fc62..ba7e358c29 100644
--- a/crypto/bio/build.info
+++ b/crypto/bio/build.info
@@ -11,7 +11,7 @@ SOURCE[../../libcrypto]=\
SOURCE[../../libcrypto]=\
bss_null.c bss_mem.c bss_bio.c bss_fd.c bss_file.c \
bss_sock.c bss_conn.c bss_acpt.c bss_dgram.c \
- bss_log.c
+ bss_log.c bss_core.c
# Filters
SOURCE[../../libcrypto]=\