1*b0d17251Schristos /*
2*b0d17251Schristos * Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
3*b0d17251Schristos *
4*b0d17251Schristos * Licensed under the Apache License 2.0 (the "License"). You may not use
5*b0d17251Schristos * this file except in compliance with the License. You can obtain a copy
6*b0d17251Schristos * in the file LICENSE in the source distribution or at
7*b0d17251Schristos * https://www.openssl.org/source/license.html
8*b0d17251Schristos */
9*b0d17251Schristos
10*b0d17251Schristos #include <string.h>
11*b0d17251Schristos #include <stdio.h>
12*b0d17251Schristos #include <openssl/core.h>
13*b0d17251Schristos #include <openssl/core_dispatch.h>
14*b0d17251Schristos #include <openssl/core_names.h>
15*b0d17251Schristos #include <openssl/params.h>
16*b0d17251Schristos #include "prov/provider_ctx.h"
17*b0d17251Schristos #include "prov/implementations.h"
18*b0d17251Schristos #include "prov/names.h"
19*b0d17251Schristos #include "prov/providercommon.h"
20*b0d17251Schristos
21*b0d17251Schristos /*
22*b0d17251Schristos * Forward declarations to ensure that interface functions are correctly
23*b0d17251Schristos * defined.
24*b0d17251Schristos */
25*b0d17251Schristos static OSSL_FUNC_provider_gettable_params_fn legacy_gettable_params;
26*b0d17251Schristos static OSSL_FUNC_provider_get_params_fn legacy_get_params;
27*b0d17251Schristos static OSSL_FUNC_provider_query_operation_fn legacy_query;
28*b0d17251Schristos
29*b0d17251Schristos #define ALG(NAMES, FUNC) { NAMES, "provider=legacy", FUNC }
30*b0d17251Schristos
31*b0d17251Schristos #ifdef STATIC_LEGACY
32*b0d17251Schristos OSSL_provider_init_fn ossl_legacy_provider_init;
33*b0d17251Schristos # define OSSL_provider_init ossl_legacy_provider_init
34*b0d17251Schristos #endif
35*b0d17251Schristos
36*b0d17251Schristos /* Parameters we provide to the core */
37*b0d17251Schristos static const OSSL_PARAM legacy_param_types[] = {
38*b0d17251Schristos OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
39*b0d17251Schristos OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
40*b0d17251Schristos OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0),
41*b0d17251Schristos OSSL_PARAM_DEFN(OSSL_PROV_PARAM_STATUS, OSSL_PARAM_INTEGER, NULL, 0),
42*b0d17251Schristos OSSL_PARAM_END
43*b0d17251Schristos };
44*b0d17251Schristos
legacy_gettable_params(void * provctx)45*b0d17251Schristos static const OSSL_PARAM *legacy_gettable_params(void *provctx)
46*b0d17251Schristos {
47*b0d17251Schristos return legacy_param_types;
48*b0d17251Schristos }
49*b0d17251Schristos
legacy_get_params(void * provctx,OSSL_PARAM params[])50*b0d17251Schristos static int legacy_get_params(void *provctx, OSSL_PARAM params[])
51*b0d17251Schristos {
52*b0d17251Schristos OSSL_PARAM *p;
53*b0d17251Schristos
54*b0d17251Schristos p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
55*b0d17251Schristos if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL Legacy Provider"))
56*b0d17251Schristos return 0;
57*b0d17251Schristos p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
58*b0d17251Schristos if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
59*b0d17251Schristos return 0;
60*b0d17251Schristos p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
61*b0d17251Schristos if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
62*b0d17251Schristos return 0;
63*b0d17251Schristos p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_STATUS);
64*b0d17251Schristos if (p != NULL && !OSSL_PARAM_set_int(p, ossl_prov_is_running()))
65*b0d17251Schristos return 0;
66*b0d17251Schristos return 1;
67*b0d17251Schristos }
68*b0d17251Schristos
69*b0d17251Schristos static const OSSL_ALGORITHM legacy_digests[] = {
70*b0d17251Schristos #ifndef OPENSSL_NO_MD2
71*b0d17251Schristos ALG(PROV_NAMES_MD2, ossl_md2_functions),
72*b0d17251Schristos #endif
73*b0d17251Schristos #ifndef OPENSSL_NO_MD4
74*b0d17251Schristos ALG(PROV_NAMES_MD4, ossl_md4_functions),
75*b0d17251Schristos #endif
76*b0d17251Schristos #ifndef OPENSSL_NO_MDC2
77*b0d17251Schristos ALG(PROV_NAMES_MDC2, ossl_mdc2_functions),
78*b0d17251Schristos #endif /* OPENSSL_NO_MDC2 */
79*b0d17251Schristos #ifndef OPENSSL_NO_WHIRLPOOL
80*b0d17251Schristos ALG(PROV_NAMES_WHIRLPOOL, ossl_wp_functions),
81*b0d17251Schristos #endif /* OPENSSL_NO_WHIRLPOOL */
82*b0d17251Schristos #ifndef OPENSSL_NO_RMD160
83*b0d17251Schristos ALG(PROV_NAMES_RIPEMD_160, ossl_ripemd160_functions),
84*b0d17251Schristos #endif /* OPENSSL_NO_RMD160 */
85*b0d17251Schristos { NULL, NULL, NULL }
86*b0d17251Schristos };
87*b0d17251Schristos
88*b0d17251Schristos static const OSSL_ALGORITHM legacy_ciphers[] = {
89*b0d17251Schristos #ifndef OPENSSL_NO_CAST
90*b0d17251Schristos ALG(PROV_NAMES_CAST5_ECB, ossl_cast5128ecb_functions),
91*b0d17251Schristos ALG(PROV_NAMES_CAST5_CBC, ossl_cast5128cbc_functions),
92*b0d17251Schristos ALG(PROV_NAMES_CAST5_OFB, ossl_cast5128ofb64_functions),
93*b0d17251Schristos ALG(PROV_NAMES_CAST5_CFB, ossl_cast5128cfb64_functions),
94*b0d17251Schristos #endif /* OPENSSL_NO_CAST */
95*b0d17251Schristos #ifndef OPENSSL_NO_BF
96*b0d17251Schristos ALG(PROV_NAMES_BF_ECB, ossl_blowfish128ecb_functions),
97*b0d17251Schristos ALG(PROV_NAMES_BF_CBC, ossl_blowfish128cbc_functions),
98*b0d17251Schristos ALG(PROV_NAMES_BF_OFB, ossl_blowfish128ofb64_functions),
99*b0d17251Schristos ALG(PROV_NAMES_BF_CFB, ossl_blowfish128cfb64_functions),
100*b0d17251Schristos #endif /* OPENSSL_NO_BF */
101*b0d17251Schristos #ifndef OPENSSL_NO_IDEA
102*b0d17251Schristos ALG(PROV_NAMES_IDEA_ECB, ossl_idea128ecb_functions),
103*b0d17251Schristos ALG(PROV_NAMES_IDEA_CBC, ossl_idea128cbc_functions),
104*b0d17251Schristos ALG(PROV_NAMES_IDEA_OFB, ossl_idea128ofb64_functions),
105*b0d17251Schristos ALG(PROV_NAMES_IDEA_CFB, ossl_idea128cfb64_functions),
106*b0d17251Schristos #endif /* OPENSSL_NO_IDEA */
107*b0d17251Schristos #ifndef OPENSSL_NO_SEED
108*b0d17251Schristos ALG(PROV_NAMES_SEED_ECB, ossl_seed128ecb_functions),
109*b0d17251Schristos ALG(PROV_NAMES_SEED_CBC, ossl_seed128cbc_functions),
110*b0d17251Schristos ALG(PROV_NAMES_SEED_OFB, ossl_seed128ofb128_functions),
111*b0d17251Schristos ALG(PROV_NAMES_SEED_CFB, ossl_seed128cfb128_functions),
112*b0d17251Schristos #endif /* OPENSSL_NO_SEED */
113*b0d17251Schristos #ifndef OPENSSL_NO_RC2
114*b0d17251Schristos ALG(PROV_NAMES_RC2_ECB, ossl_rc2128ecb_functions),
115*b0d17251Schristos ALG(PROV_NAMES_RC2_CBC, ossl_rc2128cbc_functions),
116*b0d17251Schristos ALG(PROV_NAMES_RC2_40_CBC, ossl_rc240cbc_functions),
117*b0d17251Schristos ALG(PROV_NAMES_RC2_64_CBC, ossl_rc264cbc_functions),
118*b0d17251Schristos ALG(PROV_NAMES_RC2_CFB, ossl_rc2128cfb128_functions),
119*b0d17251Schristos ALG(PROV_NAMES_RC2_OFB, ossl_rc2128ofb128_functions),
120*b0d17251Schristos #endif /* OPENSSL_NO_RC2 */
121*b0d17251Schristos #ifndef OPENSSL_NO_RC4
122*b0d17251Schristos ALG(PROV_NAMES_RC4, ossl_rc4128_functions),
123*b0d17251Schristos ALG(PROV_NAMES_RC4_40, ossl_rc440_functions),
124*b0d17251Schristos # ifndef OPENSSL_NO_MD5
125*b0d17251Schristos ALG(PROV_NAMES_RC4_HMAC_MD5, ossl_rc4_hmac_ossl_md5_functions),
126*b0d17251Schristos # endif /* OPENSSL_NO_MD5 */
127*b0d17251Schristos #endif /* OPENSSL_NO_RC4 */
128*b0d17251Schristos #ifndef OPENSSL_NO_RC5
129*b0d17251Schristos ALG(PROV_NAMES_RC5_ECB, ossl_rc5128ecb_functions),
130*b0d17251Schristos ALG(PROV_NAMES_RC5_CBC, ossl_rc5128cbc_functions),
131*b0d17251Schristos ALG(PROV_NAMES_RC5_OFB, ossl_rc5128ofb64_functions),
132*b0d17251Schristos ALG(PROV_NAMES_RC5_CFB, ossl_rc5128cfb64_functions),
133*b0d17251Schristos #endif /* OPENSSL_NO_RC5 */
134*b0d17251Schristos #ifndef OPENSSL_NO_DES
135*b0d17251Schristos ALG(PROV_NAMES_DESX_CBC, ossl_tdes_desx_cbc_functions),
136*b0d17251Schristos ALG(PROV_NAMES_DES_ECB, ossl_des_ecb_functions),
137*b0d17251Schristos ALG(PROV_NAMES_DES_CBC, ossl_des_cbc_functions),
138*b0d17251Schristos ALG(PROV_NAMES_DES_OFB, ossl_des_ofb64_functions),
139*b0d17251Schristos ALG(PROV_NAMES_DES_CFB, ossl_des_cfb64_functions),
140*b0d17251Schristos ALG(PROV_NAMES_DES_CFB1, ossl_des_cfb1_functions),
141*b0d17251Schristos ALG(PROV_NAMES_DES_CFB8, ossl_des_cfb8_functions),
142*b0d17251Schristos #endif /* OPENSSL_NO_DES */
143*b0d17251Schristos { NULL, NULL, NULL }
144*b0d17251Schristos };
145*b0d17251Schristos
146*b0d17251Schristos static const OSSL_ALGORITHM legacy_kdfs[] = {
147*b0d17251Schristos ALG(PROV_NAMES_PBKDF1, ossl_kdf_pbkdf1_functions),
148*b0d17251Schristos { NULL, NULL, NULL }
149*b0d17251Schristos };
150*b0d17251Schristos
legacy_query(void * provctx,int operation_id,int * no_cache)151*b0d17251Schristos static const OSSL_ALGORITHM *legacy_query(void *provctx, int operation_id,
152*b0d17251Schristos int *no_cache)
153*b0d17251Schristos {
154*b0d17251Schristos *no_cache = 0;
155*b0d17251Schristos switch (operation_id) {
156*b0d17251Schristos case OSSL_OP_DIGEST:
157*b0d17251Schristos return legacy_digests;
158*b0d17251Schristos case OSSL_OP_CIPHER:
159*b0d17251Schristos return legacy_ciphers;
160*b0d17251Schristos case OSSL_OP_KDF:
161*b0d17251Schristos return legacy_kdfs;
162*b0d17251Schristos }
163*b0d17251Schristos return NULL;
164*b0d17251Schristos }
165*b0d17251Schristos
legacy_teardown(void * provctx)166*b0d17251Schristos static void legacy_teardown(void *provctx)
167*b0d17251Schristos {
168*b0d17251Schristos OSSL_LIB_CTX_free(PROV_LIBCTX_OF(provctx));
169*b0d17251Schristos ossl_prov_ctx_free(provctx);
170*b0d17251Schristos }
171*b0d17251Schristos
172*b0d17251Schristos /* Functions we provide to the core */
173*b0d17251Schristos static const OSSL_DISPATCH legacy_dispatch_table[] = {
174*b0d17251Schristos { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))legacy_teardown },
175*b0d17251Schristos { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))legacy_gettable_params },
176*b0d17251Schristos { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))legacy_get_params },
177*b0d17251Schristos { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))legacy_query },
178*b0d17251Schristos { 0, NULL }
179*b0d17251Schristos };
180*b0d17251Schristos
OSSL_provider_init(const OSSL_CORE_HANDLE * handle,const OSSL_DISPATCH * in,const OSSL_DISPATCH ** out,void ** provctx)181*b0d17251Schristos int OSSL_provider_init(const OSSL_CORE_HANDLE *handle,
182*b0d17251Schristos const OSSL_DISPATCH *in,
183*b0d17251Schristos const OSSL_DISPATCH **out,
184*b0d17251Schristos void **provctx)
185*b0d17251Schristos {
186*b0d17251Schristos OSSL_LIB_CTX *libctx = NULL;
187*b0d17251Schristos
188*b0d17251Schristos if ((*provctx = ossl_prov_ctx_new()) == NULL
189*b0d17251Schristos || (libctx = OSSL_LIB_CTX_new_child(handle, in)) == NULL) {
190*b0d17251Schristos OSSL_LIB_CTX_free(libctx);
191*b0d17251Schristos legacy_teardown(*provctx);
192*b0d17251Schristos *provctx = NULL;
193*b0d17251Schristos return 0;
194*b0d17251Schristos }
195*b0d17251Schristos ossl_prov_ctx_set0_libctx(*provctx, libctx);
196*b0d17251Schristos ossl_prov_ctx_set0_handle(*provctx, handle);
197*b0d17251Schristos
198*b0d17251Schristos *out = legacy_dispatch_table;
199*b0d17251Schristos
200*b0d17251Schristos return 1;
201*b0d17251Schristos }
202