]>
| Commit | Line | Data |
|---|---|---|
| d0308923 | 1 | /* |
| e6633241 | 2 | * Copyright 2019-2025 The OpenSSL Project Authors. All Rights Reserved. |
| d0308923 MC |
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> | |
| 23c48d94 | 13 | #include <openssl/core_dispatch.h> |
| d0308923 | 14 | #include <openssl/core_names.h> |
| 8c2e588b | 15 | #include <openssl/err.h> |
| d0308923 | 16 | #include <openssl/params.h> |
| fdaad3f1 | 17 | #include "prov/provider_ctx.h" |
| af3e7e1b | 18 | #include "prov/implementations.h" |
| e2f5df36 | 19 | #include "prov/names.h" |
| eab7b424 | 20 | #include "prov/providercommon.h" |
| d0308923 | 21 | |
| fdaad3f1 RL |
22 | /* |
| 23 | * Forward declarations to ensure that interface functions are correctly | |
| 24 | * defined. | |
| 25 | */ | |
| 363b1e5d DMSP |
26 | static OSSL_FUNC_provider_gettable_params_fn legacy_gettable_params; |
| 27 | static OSSL_FUNC_provider_get_params_fn legacy_get_params; | |
| 28 | static OSSL_FUNC_provider_query_operation_fn legacy_query; | |
| fdaad3f1 | 29 | |
| f5056577 SL |
30 | #define ALG(NAMES, FUNC) { NAMES, "provider=legacy", FUNC } |
| 31 | ||
| 318e074e RL |
32 | #ifdef STATIC_LEGACY |
| 33 | OSSL_provider_init_fn ossl_legacy_provider_init; | |
| 2fab90bb | 34 | #define OSSL_provider_init ossl_legacy_provider_init |
| 318e074e RL |
35 | #endif |
| 36 | ||
| 8c2e588b RL |
37 | #ifndef STATIC_LEGACY |
| 38 | /* | |
| 39 | * Should these function pointers be stored in the provider side provctx? | |
| 40 | * Could they ever be different from one init to the next? We assume not for | |
| 41 | * now. | |
| 42 | */ | |
| 43 | ||
| 44 | /* Functions provided by the core */ | |
| 45 | static OSSL_FUNC_core_new_error_fn *c_new_error; | |
| 46 | static OSSL_FUNC_core_set_error_debug_fn *c_set_error_debug; | |
| 47 | static OSSL_FUNC_core_vset_error_fn *c_vset_error; | |
| 48 | static OSSL_FUNC_core_set_error_mark_fn *c_set_error_mark; | |
| 49 | static OSSL_FUNC_core_clear_last_error_mark_fn *c_clear_last_error_mark; | |
| 50 | static OSSL_FUNC_core_pop_error_to_mark_fn *c_pop_error_to_mark; | |
| f77fafd1 | 51 | static OSSL_FUNC_core_count_to_mark_fn *c_count_to_mark; |
| 8c2e588b RL |
52 | #endif |
| 53 | ||
| d0308923 | 54 | /* Parameters we provide to the core */ |
| fdaad3f1 RL |
55 | static const OSSL_PARAM legacy_param_types[] = { |
| 56 | OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0), | |
| 57 | OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0), | |
| 58 | OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0), | |
| eab7b424 | 59 | OSSL_PARAM_DEFN(OSSL_PROV_PARAM_STATUS, OSSL_PARAM_INTEGER, NULL, 0), |
| fdaad3f1 | 60 | OSSL_PARAM_END |
| d0308923 MC |
61 | }; |
| 62 | ||
| fdaad3f1 | 63 | static const OSSL_PARAM *legacy_gettable_params(void *provctx) |
| d0308923 MC |
64 | { |
| 65 | return legacy_param_types; | |
| 66 | } | |
| 67 | ||
| fdaad3f1 | 68 | static int legacy_get_params(void *provctx, OSSL_PARAM params[]) |
| d0308923 | 69 | { |
| 4e7991b4 | 70 | OSSL_PARAM *p; |
| d0308923 MC |
71 | |
| 72 | p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME); | |
| 73 | if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL Legacy Provider")) | |
| 74 | return 0; | |
| 75 | p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION); | |
| 76 | if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR)) | |
| 77 | return 0; | |
| 78 | p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO); | |
| 79 | if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR)) | |
| 80 | return 0; | |
| 04cb5ec0 | 81 | p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_STATUS); |
| eab7b424 | 82 | if (p != NULL && !OSSL_PARAM_set_int(p, ossl_prov_is_running())) |
| 04cb5ec0 | 83 | return 0; |
| d0308923 MC |
84 | return 1; |
| 85 | } | |
| 86 | ||
| d0308923 MC |
87 | static const OSSL_ALGORITHM legacy_digests[] = { |
| 88 | #ifndef OPENSSL_NO_MD2 | |
| e2f5df36 | 89 | ALG(PROV_NAMES_MD2, ossl_md2_functions), |
| d0308923 | 90 | #endif |
| d5e5e2ff | 91 | #ifndef OPENSSL_NO_MD4 |
| e2f5df36 | 92 | ALG(PROV_NAMES_MD4, ossl_md4_functions), |
| d5e5e2ff | 93 | #endif |
| d5e5e2ff | 94 | #ifndef OPENSSL_NO_MDC2 |
| e2f5df36 | 95 | ALG(PROV_NAMES_MDC2, ossl_mdc2_functions), |
| d5e5e2ff | 96 | #endif /* OPENSSL_NO_MDC2 */ |
| d5e5e2ff | 97 | #ifndef OPENSSL_NO_WHIRLPOOL |
| e2f5df36 | 98 | ALG(PROV_NAMES_WHIRLPOOL, ossl_wp_functions), |
| d5e5e2ff | 99 | #endif /* OPENSSL_NO_WHIRLPOOL */ |
| d5e5e2ff | 100 | #ifndef OPENSSL_NO_RMD160 |
| e2f5df36 | 101 | ALG(PROV_NAMES_RIPEMD_160, ossl_ripemd160_functions), |
| d5e5e2ff | 102 | #endif /* OPENSSL_NO_RMD160 */ |
| f5056577 SL |
103 | { NULL, NULL, NULL } |
| 104 | }; | |
| d5e5e2ff | 105 | |
| f5056577 SL |
106 | static const OSSL_ALGORITHM legacy_ciphers[] = { |
| 107 | #ifndef OPENSSL_NO_CAST | |
| e2f5df36 RL |
108 | ALG(PROV_NAMES_CAST5_ECB, ossl_cast5128ecb_functions), |
| 109 | ALG(PROV_NAMES_CAST5_CBC, ossl_cast5128cbc_functions), | |
| 110 | ALG(PROV_NAMES_CAST5_OFB, ossl_cast5128ofb64_functions), | |
| 111 | ALG(PROV_NAMES_CAST5_CFB, ossl_cast5128cfb64_functions), | |
| f5056577 SL |
112 | #endif /* OPENSSL_NO_CAST */ |
| 113 | #ifndef OPENSSL_NO_BF | |
| e2f5df36 RL |
114 | ALG(PROV_NAMES_BF_ECB, ossl_blowfish128ecb_functions), |
| 115 | ALG(PROV_NAMES_BF_CBC, ossl_blowfish128cbc_functions), | |
| 7a9e93dd TM |
116 | ALG(PROV_NAMES_BF_OFB, ossl_blowfish128ofb64_functions), |
| 117 | ALG(PROV_NAMES_BF_CFB, ossl_blowfish128cfb64_functions), | |
| f5056577 SL |
118 | #endif /* OPENSSL_NO_BF */ |
| 119 | #ifndef OPENSSL_NO_IDEA | |
| e2f5df36 RL |
120 | ALG(PROV_NAMES_IDEA_ECB, ossl_idea128ecb_functions), |
| 121 | ALG(PROV_NAMES_IDEA_CBC, ossl_idea128cbc_functions), | |
| 122 | ALG(PROV_NAMES_IDEA_OFB, ossl_idea128ofb64_functions), | |
| 123 | ALG(PROV_NAMES_IDEA_CFB, ossl_idea128cfb64_functions), | |
| f5056577 SL |
124 | #endif /* OPENSSL_NO_IDEA */ |
| 125 | #ifndef OPENSSL_NO_SEED | |
| e2f5df36 RL |
126 | ALG(PROV_NAMES_SEED_ECB, ossl_seed128ecb_functions), |
| 127 | ALG(PROV_NAMES_SEED_CBC, ossl_seed128cbc_functions), | |
| 128 | ALG(PROV_NAMES_SEED_OFB, ossl_seed128ofb128_functions), | |
| 129 | ALG(PROV_NAMES_SEED_CFB, ossl_seed128cfb128_functions), | |
| f5056577 SL |
130 | #endif /* OPENSSL_NO_SEED */ |
| 131 | #ifndef OPENSSL_NO_RC2 | |
| e2f5df36 RL |
132 | ALG(PROV_NAMES_RC2_ECB, ossl_rc2128ecb_functions), |
| 133 | ALG(PROV_NAMES_RC2_CBC, ossl_rc2128cbc_functions), | |
| 134 | ALG(PROV_NAMES_RC2_40_CBC, ossl_rc240cbc_functions), | |
| 135 | ALG(PROV_NAMES_RC2_64_CBC, ossl_rc264cbc_functions), | |
| 136 | ALG(PROV_NAMES_RC2_CFB, ossl_rc2128cfb128_functions), | |
| 137 | ALG(PROV_NAMES_RC2_OFB, ossl_rc2128ofb128_functions), | |
| f5056577 SL |
138 | #endif /* OPENSSL_NO_RC2 */ |
| 139 | #ifndef OPENSSL_NO_RC4 | |
| e2f5df36 RL |
140 | ALG(PROV_NAMES_RC4, ossl_rc4128_functions), |
| 141 | ALG(PROV_NAMES_RC4_40, ossl_rc440_functions), | |
| 2fab90bb | 142 | #ifndef OPENSSL_NO_MD5 |
| e2f5df36 | 143 | ALG(PROV_NAMES_RC4_HMAC_MD5, ossl_rc4_hmac_ossl_md5_functions), |
| 2fab90bb | 144 | #endif /* OPENSSL_NO_MD5 */ |
| f5056577 SL |
145 | #endif /* OPENSSL_NO_RC4 */ |
| 146 | #ifndef OPENSSL_NO_RC5 | |
| e2f5df36 RL |
147 | ALG(PROV_NAMES_RC5_ECB, ossl_rc5128ecb_functions), |
| 148 | ALG(PROV_NAMES_RC5_CBC, ossl_rc5128cbc_functions), | |
| 149 | ALG(PROV_NAMES_RC5_OFB, ossl_rc5128ofb64_functions), | |
| 150 | ALG(PROV_NAMES_RC5_CFB, ossl_rc5128cfb64_functions), | |
| f5056577 SL |
151 | #endif /* OPENSSL_NO_RC5 */ |
| 152 | #ifndef OPENSSL_NO_DES | |
| e2f5df36 RL |
153 | ALG(PROV_NAMES_DESX_CBC, ossl_tdes_desx_cbc_functions), |
| 154 | ALG(PROV_NAMES_DES_ECB, ossl_des_ecb_functions), | |
| 155 | ALG(PROV_NAMES_DES_CBC, ossl_des_cbc_functions), | |
| 156 | ALG(PROV_NAMES_DES_OFB, ossl_des_ofb64_functions), | |
| 157 | ALG(PROV_NAMES_DES_CFB, ossl_des_cfb64_functions), | |
| 158 | ALG(PROV_NAMES_DES_CFB1, ossl_des_cfb1_functions), | |
| 159 | ALG(PROV_NAMES_DES_CFB8, ossl_des_cfb8_functions), | |
| f5056577 | 160 | #endif /* OPENSSL_NO_DES */ |
| d0308923 MC |
161 | { NULL, NULL, NULL } |
| 162 | }; | |
| 163 | ||
| 0f183675 | 164 | static const OSSL_ALGORITHM legacy_kdfs[] = { |
| 56cd5dc7 | 165 | ALG(PROV_NAMES_PBKDF1, ossl_kdf_pbkdf1_functions), |
| 722fe8ed | 166 | ALG(PROV_NAMES_PVKKDF, ossl_kdf_pvk_functions), |
| 0f183675 JS |
167 | { NULL, NULL, NULL } |
| 168 | }; | |
| 169 | ||
| 8c3c2f5c DB |
170 | static const OSSL_ALGORITHM legacy_skeymgmt[] = { |
| 171 | ALG(PROV_NAMES_GENERIC, ossl_generic_skeymgmt_functions), | |
| 172 | { NULL, NULL, NULL } | |
| 173 | }; | |
| 174 | ||
| fdaad3f1 | 175 | static const OSSL_ALGORITHM *legacy_query(void *provctx, int operation_id, |
| 2fab90bb | 176 | int *no_cache) |
| d0308923 MC |
177 | { |
| 178 | *no_cache = 0; | |
| 179 | switch (operation_id) { | |
| 180 | case OSSL_OP_DIGEST: | |
| 181 | return legacy_digests; | |
| f5056577 SL |
182 | case OSSL_OP_CIPHER: |
| 183 | return legacy_ciphers; | |
| 0f183675 JS |
184 | case OSSL_OP_KDF: |
| 185 | return legacy_kdfs; | |
| 8c3c2f5c DB |
186 | case OSSL_OP_SKEYMGMT: |
| 187 | return legacy_skeymgmt; | |
| d0308923 MC |
188 | } |
| 189 | return NULL; | |
| 190 | } | |
| 191 | ||
| 78906fff RL |
192 | static void legacy_teardown(void *provctx) |
| 193 | { | |
| a829b735 | 194 | OSSL_LIB_CTX_free(PROV_LIBCTX_OF(provctx)); |
| 7d6766cb | 195 | ossl_prov_ctx_free(provctx); |
| 78906fff RL |
196 | } |
| 197 | ||
| d0308923 MC |
198 | /* Functions we provide to the core */ |
| 199 | static const OSSL_DISPATCH legacy_dispatch_table[] = { | |
| 78906fff | 200 | { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))legacy_teardown }, |
| dca97d00 | 201 | { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))legacy_gettable_params }, |
| d0308923 MC |
202 | { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))legacy_get_params }, |
| 203 | { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))legacy_query }, | |
| 1e6bd31e | 204 | OSSL_DISPATCH_END |
| d0308923 MC |
205 | }; |
| 206 | ||
| d40b42ab | 207 | int OSSL_provider_init(const OSSL_CORE_HANDLE *handle, |
| 2fab90bb BB |
208 | const OSSL_DISPATCH *in, |
| 209 | const OSSL_DISPATCH **out, | |
| 210 | void **provctx) | |
| d0308923 | 211 | { |
| b4250010 | 212 | OSSL_LIB_CTX *libctx = NULL; |
| 8c2e588b RL |
213 | #ifndef STATIC_LEGACY |
| 214 | const OSSL_DISPATCH *tmp; | |
| 215 | #endif | |
| 216 | ||
| 217 | #ifndef STATIC_LEGACY | |
| 218 | for (tmp = in; tmp->function_id != 0; tmp++) { | |
| 219 | /* | |
| 220 | * We do not support the scenario of an application linked against | |
| 221 | * multiple versions of libcrypto (e.g. one static and one dynamic), | |
| 222 | * but sharing a single legacy.so. We do a simple sanity check here. | |
| 223 | */ | |
| 2fab90bb BB |
224 | #define set_func(c, f) \ |
| 225 | if (c == NULL) \ | |
| 226 | c = f; \ | |
| 227 | else if (c != f) \ | |
| 228 | return 0; | |
| 8c2e588b RL |
229 | switch (tmp->function_id) { |
| 230 | case OSSL_FUNC_CORE_NEW_ERROR: | |
| 231 | set_func(c_new_error, OSSL_FUNC_core_new_error(tmp)); | |
| 232 | break; | |
| 233 | case OSSL_FUNC_CORE_SET_ERROR_DEBUG: | |
| 234 | set_func(c_set_error_debug, OSSL_FUNC_core_set_error_debug(tmp)); | |
| 235 | break; | |
| 236 | case OSSL_FUNC_CORE_VSET_ERROR: | |
| 237 | set_func(c_vset_error, OSSL_FUNC_core_vset_error(tmp)); | |
| 238 | break; | |
| 239 | case OSSL_FUNC_CORE_SET_ERROR_MARK: | |
| 240 | set_func(c_set_error_mark, OSSL_FUNC_core_set_error_mark(tmp)); | |
| 241 | break; | |
| 242 | case OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK: | |
| 243 | set_func(c_clear_last_error_mark, | |
| 2fab90bb | 244 | OSSL_FUNC_core_clear_last_error_mark(tmp)); |
| 8c2e588b RL |
245 | break; |
| 246 | case OSSL_FUNC_CORE_POP_ERROR_TO_MARK: | |
| 247 | set_func(c_pop_error_to_mark, OSSL_FUNC_core_pop_error_to_mark(tmp)); | |
| 248 | break; | |
| f77fafd1 IF |
249 | case OSSL_FUNC_CORE_COUNT_TO_MARK: |
| 250 | set_func(c_count_to_mark, OSSL_FUNC_core_count_to_mark(in)); | |
| 251 | break; | |
| 8c2e588b RL |
252 | } |
| 253 | } | |
| 254 | #endif | |
| 8013a933 | 255 | |
| 7d6766cb | 256 | if ((*provctx = ossl_prov_ctx_new()) == NULL |
| d0efad48 | 257 | || (libctx = OSSL_LIB_CTX_new_child(handle, in)) == NULL) { |
| b4250010 | 258 | OSSL_LIB_CTX_free(libctx); |
| 78906fff RL |
259 | legacy_teardown(*provctx); |
| 260 | *provctx = NULL; | |
| 261 | return 0; | |
| 262 | } | |
| a829b735 | 263 | ossl_prov_ctx_set0_libctx(*provctx, libctx); |
| 7d6766cb | 264 | ossl_prov_ctx_set0_handle(*provctx, handle); |
| 78906fff | 265 | |
| d0308923 | 266 | *out = legacy_dispatch_table; |
| 8013a933 | 267 | |
| d0308923 MC |
268 | return 1; |
| 269 | } | |
| 8c2e588b RL |
270 | |
| 271 | #ifndef STATIC_LEGACY | |
| 272 | /* | |
| 273 | * Provider specific implementation of libcrypto functions in terms of | |
| 274 | * upcalls. | |
| 275 | */ | |
| 276 | ||
| 277 | /* | |
| 278 | * For ERR functions, we pass a NULL context. This is valid to do as long | |
| 279 | * as only error codes that the calling libcrypto supports are used. | |
| 280 | */ | |
| 281 | void ERR_new(void) | |
| 282 | { | |
| 283 | c_new_error(NULL); | |
| 284 | } | |
| 285 | ||
| 286 | void ERR_set_debug(const char *file, int line, const char *func) | |
| 287 | { | |
| 288 | c_set_error_debug(NULL, file, line, func); | |
| 289 | } | |
| 290 | ||
| 291 | void ERR_set_error(int lib, int reason, const char *fmt, ...) | |
| 292 | { | |
| 293 | va_list args; | |
| 294 | ||
| 295 | va_start(args, fmt); | |
| 296 | c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args); | |
| 297 | va_end(args); | |
| 298 | } | |
| 299 | ||
| 300 | void ERR_vset_error(int lib, int reason, const char *fmt, va_list args) | |
| 301 | { | |
| 302 | c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args); | |
| 303 | } | |
| 304 | ||
| 305 | int ERR_set_mark(void) | |
| 306 | { | |
| 307 | return c_set_error_mark(NULL); | |
| 308 | } | |
| 309 | ||
| 310 | int ERR_clear_last_mark(void) | |
| 311 | { | |
| 312 | return c_clear_last_error_mark(NULL); | |
| 313 | } | |
| 314 | ||
| 315 | int ERR_pop_to_mark(void) | |
| 316 | { | |
| 317 | return c_pop_error_to_mark(NULL); | |
| 318 | } | |
| f77fafd1 IF |
319 | |
| 320 | int ERR_count_to_mark(void) | |
| 321 | { | |
| 322 | return c_count_to_mark != NULL ? c_count_to_mark(NULL) : 0; | |
| 323 | } | |
| 8c2e588b | 324 | #endif |