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