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