Path: blob/main/crypto/openssl/providers/legacyprov.c
48148 views
/*1* Copyright 2019-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/core.h>12#include <openssl/core_dispatch.h>13#include <openssl/core_names.h>14#include <openssl/err.h>15#include <openssl/params.h>16#include "prov/provider_ctx.h"17#include "prov/implementations.h"18#include "prov/names.h"19#include "prov/providercommon.h"2021/*22* Forward declarations to ensure that interface functions are correctly23* defined.24*/25static OSSL_FUNC_provider_gettable_params_fn legacy_gettable_params;26static OSSL_FUNC_provider_get_params_fn legacy_get_params;27static OSSL_FUNC_provider_query_operation_fn legacy_query;2829#define ALG(NAMES, FUNC) { NAMES, "provider=legacy", FUNC }3031#ifdef STATIC_LEGACY32OSSL_provider_init_fn ossl_legacy_provider_init;33# define OSSL_provider_init ossl_legacy_provider_init34#endif3536#ifndef STATIC_LEGACY37/*38* Should these function pointers be stored in the provider side provctx?39* Could they ever be different from one init to the next? We assume not for40* now.41*/4243/* Functions provided by the core */44static OSSL_FUNC_core_new_error_fn *c_new_error;45static OSSL_FUNC_core_set_error_debug_fn *c_set_error_debug;46static OSSL_FUNC_core_vset_error_fn *c_vset_error;47static OSSL_FUNC_core_set_error_mark_fn *c_set_error_mark;48static OSSL_FUNC_core_clear_last_error_mark_fn *c_clear_last_error_mark;49static OSSL_FUNC_core_pop_error_to_mark_fn *c_pop_error_to_mark;50static OSSL_FUNC_core_count_to_mark_fn *c_count_to_mark;51#endif5253/* Parameters we provide to the core */54static const OSSL_PARAM legacy_param_types[] = {55OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),56OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),57OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0),58OSSL_PARAM_DEFN(OSSL_PROV_PARAM_STATUS, OSSL_PARAM_INTEGER, NULL, 0),59OSSL_PARAM_END60};6162static const OSSL_PARAM *legacy_gettable_params(void *provctx)63{64return legacy_param_types;65}6667static int legacy_get_params(void *provctx, OSSL_PARAM params[])68{69OSSL_PARAM *p;7071p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);72if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL Legacy Provider"))73return 0;74p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);75if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))76return 0;77p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);78if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))79return 0;80p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_STATUS);81if (p != NULL && !OSSL_PARAM_set_int(p, ossl_prov_is_running()))82return 0;83return 1;84}8586static const OSSL_ALGORITHM legacy_digests[] = {87#ifndef OPENSSL_NO_MD288ALG(PROV_NAMES_MD2, ossl_md2_functions),89#endif90#ifndef OPENSSL_NO_MD491ALG(PROV_NAMES_MD4, ossl_md4_functions),92#endif93#ifndef OPENSSL_NO_MDC294ALG(PROV_NAMES_MDC2, ossl_mdc2_functions),95#endif /* OPENSSL_NO_MDC2 */96#ifndef OPENSSL_NO_WHIRLPOOL97ALG(PROV_NAMES_WHIRLPOOL, ossl_wp_functions),98#endif /* OPENSSL_NO_WHIRLPOOL */99#ifndef OPENSSL_NO_RMD160100ALG(PROV_NAMES_RIPEMD_160, ossl_ripemd160_functions),101#endif /* OPENSSL_NO_RMD160 */102{ NULL, NULL, NULL }103};104105static const OSSL_ALGORITHM legacy_ciphers[] = {106#ifndef OPENSSL_NO_CAST107ALG(PROV_NAMES_CAST5_ECB, ossl_cast5128ecb_functions),108ALG(PROV_NAMES_CAST5_CBC, ossl_cast5128cbc_functions),109ALG(PROV_NAMES_CAST5_OFB, ossl_cast5128ofb64_functions),110ALG(PROV_NAMES_CAST5_CFB, ossl_cast5128cfb64_functions),111#endif /* OPENSSL_NO_CAST */112#ifndef OPENSSL_NO_BF113ALG(PROV_NAMES_BF_ECB, ossl_blowfish128ecb_functions),114ALG(PROV_NAMES_BF_CBC, ossl_blowfish128cbc_functions),115ALG(PROV_NAMES_BF_OFB, ossl_blowfish128ofb64_functions),116ALG(PROV_NAMES_BF_CFB, ossl_blowfish128cfb64_functions),117#endif /* OPENSSL_NO_BF */118#ifndef OPENSSL_NO_IDEA119ALG(PROV_NAMES_IDEA_ECB, ossl_idea128ecb_functions),120ALG(PROV_NAMES_IDEA_CBC, ossl_idea128cbc_functions),121ALG(PROV_NAMES_IDEA_OFB, ossl_idea128ofb64_functions),122ALG(PROV_NAMES_IDEA_CFB, ossl_idea128cfb64_functions),123#endif /* OPENSSL_NO_IDEA */124#ifndef OPENSSL_NO_SEED125ALG(PROV_NAMES_SEED_ECB, ossl_seed128ecb_functions),126ALG(PROV_NAMES_SEED_CBC, ossl_seed128cbc_functions),127ALG(PROV_NAMES_SEED_OFB, ossl_seed128ofb128_functions),128ALG(PROV_NAMES_SEED_CFB, ossl_seed128cfb128_functions),129#endif /* OPENSSL_NO_SEED */130#ifndef OPENSSL_NO_RC2131ALG(PROV_NAMES_RC2_ECB, ossl_rc2128ecb_functions),132ALG(PROV_NAMES_RC2_CBC, ossl_rc2128cbc_functions),133ALG(PROV_NAMES_RC2_40_CBC, ossl_rc240cbc_functions),134ALG(PROV_NAMES_RC2_64_CBC, ossl_rc264cbc_functions),135ALG(PROV_NAMES_RC2_CFB, ossl_rc2128cfb128_functions),136ALG(PROV_NAMES_RC2_OFB, ossl_rc2128ofb128_functions),137#endif /* OPENSSL_NO_RC2 */138#ifndef OPENSSL_NO_RC4139ALG(PROV_NAMES_RC4, ossl_rc4128_functions),140ALG(PROV_NAMES_RC4_40, ossl_rc440_functions),141# ifndef OPENSSL_NO_MD5142ALG(PROV_NAMES_RC4_HMAC_MD5, ossl_rc4_hmac_ossl_md5_functions),143# endif /* OPENSSL_NO_MD5 */144#endif /* OPENSSL_NO_RC4 */145#ifndef OPENSSL_NO_RC5146ALG(PROV_NAMES_RC5_ECB, ossl_rc5128ecb_functions),147ALG(PROV_NAMES_RC5_CBC, ossl_rc5128cbc_functions),148ALG(PROV_NAMES_RC5_OFB, ossl_rc5128ofb64_functions),149ALG(PROV_NAMES_RC5_CFB, ossl_rc5128cfb64_functions),150#endif /* OPENSSL_NO_RC5 */151#ifndef OPENSSL_NO_DES152ALG(PROV_NAMES_DESX_CBC, ossl_tdes_desx_cbc_functions),153ALG(PROV_NAMES_DES_ECB, ossl_des_ecb_functions),154ALG(PROV_NAMES_DES_CBC, ossl_des_cbc_functions),155ALG(PROV_NAMES_DES_OFB, ossl_des_ofb64_functions),156ALG(PROV_NAMES_DES_CFB, ossl_des_cfb64_functions),157ALG(PROV_NAMES_DES_CFB1, ossl_des_cfb1_functions),158ALG(PROV_NAMES_DES_CFB8, ossl_des_cfb8_functions),159#endif /* OPENSSL_NO_DES */160{ NULL, NULL, NULL }161};162163static const OSSL_ALGORITHM legacy_kdfs[] = {164ALG(PROV_NAMES_PBKDF1, ossl_kdf_pbkdf1_functions),165ALG(PROV_NAMES_PVKKDF, ossl_kdf_pvk_functions),166{ NULL, NULL, NULL }167};168169static const OSSL_ALGORITHM *legacy_query(void *provctx, int operation_id,170int *no_cache)171{172*no_cache = 0;173switch (operation_id) {174case OSSL_OP_DIGEST:175return legacy_digests;176case OSSL_OP_CIPHER:177return legacy_ciphers;178case OSSL_OP_KDF:179return legacy_kdfs;180}181return NULL;182}183184static void legacy_teardown(void *provctx)185{186OSSL_LIB_CTX_free(PROV_LIBCTX_OF(provctx));187ossl_prov_ctx_free(provctx);188}189190/* Functions we provide to the core */191static const OSSL_DISPATCH legacy_dispatch_table[] = {192{ OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))legacy_teardown },193{ OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))legacy_gettable_params },194{ OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))legacy_get_params },195{ OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))legacy_query },196OSSL_DISPATCH_END197};198199int OSSL_provider_init(const OSSL_CORE_HANDLE *handle,200const OSSL_DISPATCH *in,201const OSSL_DISPATCH **out,202void **provctx)203{204OSSL_LIB_CTX *libctx = NULL;205#ifndef STATIC_LEGACY206const OSSL_DISPATCH *tmp;207#endif208209#ifndef STATIC_LEGACY210for (tmp = in; tmp->function_id != 0; tmp++) {211/*212* We do not support the scenario of an application linked against213* multiple versions of libcrypto (e.g. one static and one dynamic),214* but sharing a single legacy.so. We do a simple sanity check here.215*/216#define set_func(c, f) if (c == NULL) c = f; else if (c != f) return 0;217switch (tmp->function_id) {218case OSSL_FUNC_CORE_NEW_ERROR:219set_func(c_new_error, OSSL_FUNC_core_new_error(tmp));220break;221case OSSL_FUNC_CORE_SET_ERROR_DEBUG:222set_func(c_set_error_debug, OSSL_FUNC_core_set_error_debug(tmp));223break;224case OSSL_FUNC_CORE_VSET_ERROR:225set_func(c_vset_error, OSSL_FUNC_core_vset_error(tmp));226break;227case OSSL_FUNC_CORE_SET_ERROR_MARK:228set_func(c_set_error_mark, OSSL_FUNC_core_set_error_mark(tmp));229break;230case OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK:231set_func(c_clear_last_error_mark,232OSSL_FUNC_core_clear_last_error_mark(tmp));233break;234case OSSL_FUNC_CORE_POP_ERROR_TO_MARK:235set_func(c_pop_error_to_mark, OSSL_FUNC_core_pop_error_to_mark(tmp));236break;237case OSSL_FUNC_CORE_COUNT_TO_MARK:238set_func(c_count_to_mark, OSSL_FUNC_core_count_to_mark(in));239break;240}241}242#endif243244if ((*provctx = ossl_prov_ctx_new()) == NULL245|| (libctx = OSSL_LIB_CTX_new_child(handle, in)) == NULL) {246OSSL_LIB_CTX_free(libctx);247legacy_teardown(*provctx);248*provctx = NULL;249return 0;250}251ossl_prov_ctx_set0_libctx(*provctx, libctx);252ossl_prov_ctx_set0_handle(*provctx, handle);253254*out = legacy_dispatch_table;255256return 1;257}258259#ifndef STATIC_LEGACY260/*261* Provider specific implementation of libcrypto functions in terms of262* upcalls.263*/264265/*266* For ERR functions, we pass a NULL context. This is valid to do as long267* as only error codes that the calling libcrypto supports are used.268*/269void ERR_new(void)270{271c_new_error(NULL);272}273274void ERR_set_debug(const char *file, int line, const char *func)275{276c_set_error_debug(NULL, file, line, func);277}278279void ERR_set_error(int lib, int reason, const char *fmt, ...)280{281va_list args;282283va_start(args, fmt);284c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);285va_end(args);286}287288void ERR_vset_error(int lib, int reason, const char *fmt, va_list args)289{290c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);291}292293int ERR_set_mark(void)294{295return c_set_error_mark(NULL);296}297298int ERR_clear_last_mark(void)299{300return c_clear_last_error_mark(NULL);301}302303int ERR_pop_to_mark(void)304{305return c_pop_error_to_mark(NULL);306}307308int ERR_count_to_mark(void)309{310return c_count_to_mark != NULL ? c_count_to_mark(NULL) : 0;311}312#endif313314315