Path: blob/main/crypto/openssl/providers/common/bio_prov.c
48261 views
/*1* Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#include <assert.h>10#include <openssl/core_dispatch.h>11#include "internal/cryptlib.h"12#include "prov/bio.h"1314static OSSL_FUNC_BIO_new_file_fn *c_bio_new_file = NULL;15static OSSL_FUNC_BIO_new_membuf_fn *c_bio_new_membuf = NULL;16static OSSL_FUNC_BIO_read_ex_fn *c_bio_read_ex = NULL;17static OSSL_FUNC_BIO_write_ex_fn *c_bio_write_ex = NULL;18static OSSL_FUNC_BIO_gets_fn *c_bio_gets = NULL;19static OSSL_FUNC_BIO_puts_fn *c_bio_puts = NULL;20static OSSL_FUNC_BIO_ctrl_fn *c_bio_ctrl = NULL;21static OSSL_FUNC_BIO_up_ref_fn *c_bio_up_ref = NULL;22static OSSL_FUNC_BIO_free_fn *c_bio_free = NULL;23static OSSL_FUNC_BIO_vprintf_fn *c_bio_vprintf = NULL;2425int ossl_prov_bio_from_dispatch(const OSSL_DISPATCH *fns)26{27for (; fns->function_id != 0; fns++) {28switch (fns->function_id) {29case OSSL_FUNC_BIO_NEW_FILE:30if (c_bio_new_file == NULL)31c_bio_new_file = OSSL_FUNC_BIO_new_file(fns);32break;33case OSSL_FUNC_BIO_NEW_MEMBUF:34if (c_bio_new_membuf == NULL)35c_bio_new_membuf = OSSL_FUNC_BIO_new_membuf(fns);36break;37case OSSL_FUNC_BIO_READ_EX:38if (c_bio_read_ex == NULL)39c_bio_read_ex = OSSL_FUNC_BIO_read_ex(fns);40break;41case OSSL_FUNC_BIO_WRITE_EX:42if (c_bio_write_ex == NULL)43c_bio_write_ex = OSSL_FUNC_BIO_write_ex(fns);44break;45case OSSL_FUNC_BIO_GETS:46if (c_bio_gets == NULL)47c_bio_gets = OSSL_FUNC_BIO_gets(fns);48break;49case OSSL_FUNC_BIO_PUTS:50if (c_bio_puts == NULL)51c_bio_puts = OSSL_FUNC_BIO_puts(fns);52break;53case OSSL_FUNC_BIO_CTRL:54if (c_bio_ctrl == NULL)55c_bio_ctrl = OSSL_FUNC_BIO_ctrl(fns);56break;57case OSSL_FUNC_BIO_UP_REF:58if (c_bio_up_ref == NULL)59c_bio_up_ref = OSSL_FUNC_BIO_up_ref(fns);60break;61case OSSL_FUNC_BIO_FREE:62if (c_bio_free == NULL)63c_bio_free = OSSL_FUNC_BIO_free(fns);64break;65case OSSL_FUNC_BIO_VPRINTF:66if (c_bio_vprintf == NULL)67c_bio_vprintf = OSSL_FUNC_BIO_vprintf(fns);68break;69}70}7172return 1;73}7475OSSL_CORE_BIO *ossl_prov_bio_new_file(const char *filename, const char *mode)76{77if (c_bio_new_file == NULL)78return NULL;79return c_bio_new_file(filename, mode);80}8182OSSL_CORE_BIO *ossl_prov_bio_new_membuf(const char *filename, int len)83{84if (c_bio_new_membuf == NULL)85return NULL;86return c_bio_new_membuf(filename, len);87}8889int ossl_prov_bio_read_ex(OSSL_CORE_BIO *bio, void *data, size_t data_len,90size_t *bytes_read)91{92if (c_bio_read_ex == NULL)93return 0;94return c_bio_read_ex(bio, data, data_len, bytes_read);95}9697int ossl_prov_bio_write_ex(OSSL_CORE_BIO *bio, const void *data, size_t data_len,98size_t *written)99{100if (c_bio_write_ex == NULL)101return 0;102return c_bio_write_ex(bio, data, data_len, written);103}104105int ossl_prov_bio_gets(OSSL_CORE_BIO *bio, char *buf, int size)106{107if (c_bio_gets == NULL)108return -1;109return c_bio_gets(bio, buf, size);110}111112int ossl_prov_bio_puts(OSSL_CORE_BIO *bio, const char *str)113{114if (c_bio_puts == NULL)115return -1;116return c_bio_puts(bio, str);117}118119int ossl_prov_bio_ctrl(OSSL_CORE_BIO *bio, int cmd, long num, void *ptr)120{121if (c_bio_ctrl == NULL)122return -1;123return c_bio_ctrl(bio, cmd, num, ptr);124}125126int ossl_prov_bio_up_ref(OSSL_CORE_BIO *bio)127{128if (c_bio_up_ref == NULL)129return 0;130return c_bio_up_ref(bio);131}132133int ossl_prov_bio_free(OSSL_CORE_BIO *bio)134{135if (c_bio_free == NULL)136return 0;137return c_bio_free(bio);138}139140int ossl_prov_bio_vprintf(OSSL_CORE_BIO *bio, const char *format, va_list ap)141{142if (c_bio_vprintf == NULL)143return -1;144return c_bio_vprintf(bio, format, ap);145}146147int ossl_prov_bio_printf(OSSL_CORE_BIO *bio, const char *format, ...)148{149va_list ap;150int ret;151152va_start(ap, format);153ret = ossl_prov_bio_vprintf(bio, format, ap);154va_end(ap);155156return ret;157}158159#ifndef FIPS_MODULE160161/* No direct BIO support in the FIPS module */162163static int bio_core_read_ex(BIO *bio, char *data, size_t data_len,164size_t *bytes_read)165{166return ossl_prov_bio_read_ex(BIO_get_data(bio), data, data_len, bytes_read);167}168169static int bio_core_write_ex(BIO *bio, const char *data, size_t data_len,170size_t *written)171{172return ossl_prov_bio_write_ex(BIO_get_data(bio), data, data_len, written);173}174175static long bio_core_ctrl(BIO *bio, int cmd, long num, void *ptr)176{177return ossl_prov_bio_ctrl(BIO_get_data(bio), cmd, num, ptr);178}179180static int bio_core_gets(BIO *bio, char *buf, int size)181{182return ossl_prov_bio_gets(BIO_get_data(bio), buf, size);183}184185static int bio_core_puts(BIO *bio, const char *str)186{187return ossl_prov_bio_puts(BIO_get_data(bio), str);188}189190static int bio_core_new(BIO *bio)191{192BIO_set_init(bio, 1);193194return 1;195}196197static int bio_core_free(BIO *bio)198{199BIO_set_init(bio, 0);200ossl_prov_bio_free(BIO_get_data(bio));201202return 1;203}204205BIO_METHOD *ossl_bio_prov_init_bio_method(void)206{207BIO_METHOD *corebiometh = NULL;208209corebiometh = BIO_meth_new(BIO_TYPE_CORE_TO_PROV, "BIO to Core filter");210if (corebiometh == NULL211|| !BIO_meth_set_write_ex(corebiometh, bio_core_write_ex)212|| !BIO_meth_set_read_ex(corebiometh, bio_core_read_ex)213|| !BIO_meth_set_puts(corebiometh, bio_core_puts)214|| !BIO_meth_set_gets(corebiometh, bio_core_gets)215|| !BIO_meth_set_ctrl(corebiometh, bio_core_ctrl)216|| !BIO_meth_set_create(corebiometh, bio_core_new)217|| !BIO_meth_set_destroy(corebiometh, bio_core_free)) {218BIO_meth_free(corebiometh);219return NULL;220}221222return corebiometh;223}224225BIO *ossl_bio_new_from_core_bio(PROV_CTX *provctx, OSSL_CORE_BIO *corebio)226{227BIO *outbio;228BIO_METHOD *corebiometh = ossl_prov_ctx_get0_core_bio_method(provctx);229230if (corebiometh == NULL)231return NULL;232233if ((outbio = BIO_new(corebiometh)) == NULL)234return NULL;235if (!ossl_prov_bio_up_ref(corebio)) {236BIO_free(outbio);237return NULL;238}239BIO_set_data(outbio, corebio);240return outbio;241}242243#endif244245246