Path: blob/main/crypto/openssl/providers/baseprov.c
48150 views
/*1* Copyright 2020-2025 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 <string.h>10#include <stdio.h>11#include <openssl/opensslconf.h>12#include <openssl/core.h>13#include <openssl/core_dispatch.h>14#include <openssl/core_names.h>15#include <openssl/params.h>16#include "prov/bio.h"17#include "prov/provider_ctx.h"18#include "prov/providercommon.h"19#include "prov/implementations.h"20#include "prov/provider_util.h"21#include "prov/names.h"2223/*24* Forward declarations to ensure that interface functions are correctly25* defined.26*/27static OSSL_FUNC_provider_gettable_params_fn base_gettable_params;28static OSSL_FUNC_provider_get_params_fn base_get_params;29static OSSL_FUNC_provider_query_operation_fn base_query;3031/* Functions provided by the core */32static OSSL_FUNC_core_gettable_params_fn *c_gettable_params = NULL;33static OSSL_FUNC_core_get_params_fn *c_get_params = NULL;3435/* Parameters we provide to the core */36static const OSSL_PARAM base_param_types[] = {37OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),38OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),39OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0),40OSSL_PARAM_DEFN(OSSL_PROV_PARAM_STATUS, OSSL_PARAM_INTEGER, NULL, 0),41OSSL_PARAM_END42};4344static const OSSL_PARAM *base_gettable_params(void *provctx)45{46return base_param_types;47}4849static int base_get_params(void *provctx, OSSL_PARAM params[])50{51OSSL_PARAM *p;5253p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);54if (p != NULL55&& !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL Base Provider"))56return 0;57p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);58if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))59return 0;60p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);61if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))62return 0;63p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_STATUS);64if (p != NULL && !OSSL_PARAM_set_int(p, ossl_prov_is_running()))65return 0;6667return 1;68}6970static const OSSL_ALGORITHM base_encoder[] = {71#define ENCODER_PROVIDER "base"72#include "encoders.inc"73{ NULL, NULL, NULL }74#undef ENCODER_PROVIDER75};7677static const OSSL_ALGORITHM base_decoder[] = {78#define DECODER_PROVIDER "base"79#include "decoders.inc"80{ NULL, NULL, NULL }81#undef DECODER_PROVIDER82};8384static const OSSL_ALGORITHM base_store[] = {85#define STORE(name, _fips, func_table) \86{ name, "provider=base,fips=" _fips, (func_table) },8788#include "stores.inc"89{ NULL, NULL, NULL }90#undef STORE91};9293static const OSSL_ALGORITHM base_rands[] = {94{ PROV_NAMES_SEED_SRC, "provider=base", ossl_seed_src_functions },95#ifndef OPENSSL_NO_JITTER96{ PROV_NAMES_JITTER, "provider=base", ossl_jitter_functions },97#endif98{ NULL, NULL, NULL }99};100101static const OSSL_ALGORITHM *base_query(void *provctx, int operation_id,102int *no_cache)103{104*no_cache = 0;105switch (operation_id) {106case OSSL_OP_ENCODER:107return base_encoder;108case OSSL_OP_DECODER:109return base_decoder;110case OSSL_OP_STORE:111return base_store;112case OSSL_OP_RAND:113return base_rands;114}115return NULL;116}117118static void base_teardown(void *provctx)119{120BIO_meth_free(ossl_prov_ctx_get0_core_bio_method(provctx));121ossl_prov_ctx_free(provctx);122}123124/* Functions we provide to the core */125static const OSSL_DISPATCH base_dispatch_table[] = {126{ OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))base_teardown },127{ OSSL_FUNC_PROVIDER_GETTABLE_PARAMS,128(void (*)(void))base_gettable_params },129{ OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))base_get_params },130{ OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))base_query },131OSSL_DISPATCH_END132};133134OSSL_provider_init_fn ossl_base_provider_init;135136int ossl_base_provider_init(const OSSL_CORE_HANDLE *handle,137const OSSL_DISPATCH *in, const OSSL_DISPATCH **out,138void **provctx)139{140OSSL_FUNC_core_get_libctx_fn *c_get_libctx = NULL;141BIO_METHOD *corebiometh;142143if (!ossl_prov_bio_from_dispatch(in))144return 0;145for (; in->function_id != 0; in++) {146switch (in->function_id) {147case OSSL_FUNC_CORE_GETTABLE_PARAMS:148c_gettable_params = OSSL_FUNC_core_gettable_params(in);149break;150case OSSL_FUNC_CORE_GET_PARAMS:151c_get_params = OSSL_FUNC_core_get_params(in);152break;153case OSSL_FUNC_CORE_GET_LIBCTX:154c_get_libctx = OSSL_FUNC_core_get_libctx(in);155break;156default:157/* Just ignore anything we don't understand */158break;159}160}161162if (c_get_libctx == NULL)163return 0;164165/*166* We want to make sure that all calls from this provider that requires167* a library context use the same context as the one used to call our168* functions. We do that by passing it along in the provider context.169*170* This only works for built-in providers. Most providers should171* create their own library context.172*/173if ((*provctx = ossl_prov_ctx_new()) == NULL174|| (corebiometh = ossl_bio_prov_init_bio_method()) == NULL) {175ossl_prov_ctx_free(*provctx);176*provctx = NULL;177return 0;178}179ossl_prov_ctx_set0_libctx(*provctx,180(OSSL_LIB_CTX *)c_get_libctx(handle));181ossl_prov_ctx_set0_handle(*provctx, handle);182ossl_prov_ctx_set0_core_bio_method(*provctx, corebiometh);183ossl_prov_ctx_set0_core_get_params(*provctx, c_get_params);184185*out = base_dispatch_table;186187return 1;188}189190191