]>
Commit | Line | Data |
---|---|---|
d0308923 | 1 | /* |
33388b44 | 2 | * Copyright 2019-2020 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 MC |
14 | #include <openssl/core_names.h> |
15 | #include <openssl/params.h> | |
fdaad3f1 | 16 | #include "prov/provider_ctx.h" |
af3e7e1b | 17 | #include "prov/implementations.h" |
eab7b424 | 18 | #include "prov/providercommon.h" |
d0308923 | 19 | |
fdaad3f1 RL |
20 | /* |
21 | * Forward declarations to ensure that interface functions are correctly | |
22 | * defined. | |
23 | */ | |
363b1e5d DMSP |
24 | static OSSL_FUNC_provider_gettable_params_fn legacy_gettable_params; |
25 | static OSSL_FUNC_provider_get_params_fn legacy_get_params; | |
26 | static OSSL_FUNC_provider_query_operation_fn legacy_query; | |
fdaad3f1 | 27 | |
f5056577 SL |
28 | #define ALG(NAMES, FUNC) { NAMES, "provider=legacy", FUNC } |
29 | ||
318e074e RL |
30 | #ifdef STATIC_LEGACY |
31 | OSSL_provider_init_fn ossl_legacy_provider_init; | |
32 | # define OSSL_provider_init ossl_legacy_provider_init | |
33 | #endif | |
34 | ||
d0308923 | 35 | /* Functions provided by the core */ |
363b1e5d DMSP |
36 | static OSSL_FUNC_core_gettable_params_fn *c_gettable_params = NULL; |
37 | static OSSL_FUNC_core_get_params_fn *c_get_params = NULL; | |
d0308923 MC |
38 | |
39 | /* Parameters we provide to the core */ | |
fdaad3f1 RL |
40 | static const OSSL_PARAM legacy_param_types[] = { |
41 | OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0), | |
42 | OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0), | |
43 | OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0), | |
eab7b424 | 44 | OSSL_PARAM_DEFN(OSSL_PROV_PARAM_STATUS, OSSL_PARAM_INTEGER, NULL, 0), |
fdaad3f1 | 45 | OSSL_PARAM_END |
d0308923 MC |
46 | }; |
47 | ||
fdaad3f1 | 48 | static const OSSL_PARAM *legacy_gettable_params(void *provctx) |
d0308923 MC |
49 | { |
50 | return legacy_param_types; | |
51 | } | |
52 | ||
fdaad3f1 | 53 | static int legacy_get_params(void *provctx, OSSL_PARAM params[]) |
d0308923 | 54 | { |
4e7991b4 | 55 | OSSL_PARAM *p; |
d0308923 MC |
56 | |
57 | p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME); | |
58 | if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL Legacy Provider")) | |
59 | return 0; | |
60 | p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION); | |
61 | if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR)) | |
62 | return 0; | |
63 | p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO); | |
64 | if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR)) | |
65 | return 0; | |
04cb5ec0 | 66 | p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_STATUS); |
eab7b424 | 67 | if (p != NULL && !OSSL_PARAM_set_int(p, ossl_prov_is_running())) |
04cb5ec0 | 68 | return 0; |
d0308923 MC |
69 | return 1; |
70 | } | |
71 | ||
d0308923 MC |
72 | static const OSSL_ALGORITHM legacy_digests[] = { |
73 | #ifndef OPENSSL_NO_MD2 | |
1be63951 | 74 | ALG("MD2", ossl_md2_functions), |
d0308923 | 75 | #endif |
d5e5e2ff | 76 | #ifndef OPENSSL_NO_MD4 |
1be63951 | 77 | ALG("MD4", ossl_md4_functions), |
d5e5e2ff | 78 | #endif |
d5e5e2ff | 79 | #ifndef OPENSSL_NO_MDC2 |
1be63951 | 80 | ALG("MDC2", ossl_mdc2_functions), |
d5e5e2ff | 81 | #endif /* OPENSSL_NO_MDC2 */ |
d5e5e2ff | 82 | #ifndef OPENSSL_NO_WHIRLPOOL |
1be63951 | 83 | ALG("WHIRLPOOL", ossl_wp_functions), |
d5e5e2ff | 84 | #endif /* OPENSSL_NO_WHIRLPOOL */ |
d5e5e2ff | 85 | #ifndef OPENSSL_NO_RMD160 |
1be63951 | 86 | ALG("RIPEMD-160:RIPEMD160:RIPEMD:RMD160", ossl_ripemd160_functions), |
d5e5e2ff | 87 | #endif /* OPENSSL_NO_RMD160 */ |
f5056577 SL |
88 | { NULL, NULL, NULL } |
89 | }; | |
d5e5e2ff | 90 | |
f5056577 SL |
91 | static const OSSL_ALGORITHM legacy_ciphers[] = { |
92 | #ifndef OPENSSL_NO_CAST | |
1be63951 P |
93 | ALG("CAST5-ECB", ossl_cast5128ecb_functions), |
94 | ALG("CAST5-CBC:CAST-CBC:CAST", ossl_cast5128cbc_functions), | |
95 | ALG("CAST5-OFB", ossl_cast5128ofb64_functions), | |
96 | ALG("CAST5-CFB", ossl_cast5128cfb64_functions), | |
f5056577 SL |
97 | #endif /* OPENSSL_NO_CAST */ |
98 | #ifndef OPENSSL_NO_BF | |
1be63951 P |
99 | ALG("BF-ECB", ossl_blowfish128ecb_functions), |
100 | ALG("BF-CBC:BF:BLOWFISH", ossl_blowfish128cbc_functions), | |
101 | ALG("BF-OFB", ossl_blowfish64ofb64_functions), | |
102 | ALG("BF-CFB", ossl_blowfish64cfb64_functions), | |
f5056577 SL |
103 | #endif /* OPENSSL_NO_BF */ |
104 | #ifndef OPENSSL_NO_IDEA | |
1be63951 P |
105 | ALG("IDEA-ECB", ossl_idea128ecb_functions), |
106 | ALG("IDEA-CBC:IDEA", ossl_idea128cbc_functions), | |
107 | ALG("IDEA-OFB:IDEA-OFB64", ossl_idea128ofb64_functions), | |
108 | ALG("IDEA-CFB:IDEA-CFB64", ossl_idea128cfb64_functions), | |
f5056577 SL |
109 | #endif /* OPENSSL_NO_IDEA */ |
110 | #ifndef OPENSSL_NO_SEED | |
1be63951 P |
111 | ALG("SEED-ECB", ossl_seed128ecb_functions), |
112 | ALG("SEED-CBC:SEED", ossl_seed128cbc_functions), | |
113 | ALG("SEED-OFB:SEED-OFB128", ossl_seed128ofb128_functions), | |
114 | ALG("SEED-CFB:SEED-CFB128", ossl_seed128cfb128_functions), | |
f5056577 SL |
115 | #endif /* OPENSSL_NO_SEED */ |
116 | #ifndef OPENSSL_NO_RC2 | |
1be63951 P |
117 | ALG("RC2-ECB", ossl_rc2128ecb_functions), |
118 | ALG("RC2-CBC:RC2:RC2-128", ossl_rc2128cbc_functions), | |
119 | ALG("RC2-40-CBC:RC2-40", ossl_rc240cbc_functions), | |
120 | ALG("RC2-64-CBC:RC2-64", ossl_rc264cbc_functions), | |
121 | ALG("RC2-CFB", ossl_rc2128cfb128_functions), | |
122 | ALG("RC2-OFB", ossl_rc2128ofb128_functions), | |
f5056577 SL |
123 | #endif /* OPENSSL_NO_RC2 */ |
124 | #ifndef OPENSSL_NO_RC4 | |
1be63951 P |
125 | ALG("RC4", ossl_rc4128_functions), |
126 | ALG("RC4-40", ossl_rc440_functions), | |
f5056577 | 127 | # ifndef OPENSSL_NO_MD5 |
1be63951 | 128 | ALG("RC4-HMAC-MD5", ossl_rc4_hmac_ossl_md5_functions), |
f5056577 SL |
129 | # endif /* OPENSSL_NO_MD5 */ |
130 | #endif /* OPENSSL_NO_RC4 */ | |
131 | #ifndef OPENSSL_NO_RC5 | |
1be63951 P |
132 | ALG("RC5-ECB", ossl_rc5128ecb_functions), |
133 | ALG("RC5-CBC:RC5", ossl_rc5128cbc_functions), | |
134 | ALG("RC5-OFB", ossl_rc5128ofb64_functions), | |
135 | ALG("RC5-CFB", ossl_rc5128cfb64_functions), | |
f5056577 SL |
136 | #endif /* OPENSSL_NO_RC5 */ |
137 | #ifndef OPENSSL_NO_DES | |
1be63951 P |
138 | ALG("DESX-CBC:DESX", ossl_tdes_desx_cbc_functions), |
139 | ALG("DES-ECB", ossl_des_ecb_functions), | |
140 | ALG("DES-CBC:DES", ossl_des_cbc_functions), | |
141 | ALG("DES-OFB", ossl_des_ofb64_functions), | |
142 | ALG("DES-CFB", ossl_des_cfb64_functions), | |
143 | ALG("DES-CFB1", ossl_des_cfb1_functions), | |
144 | ALG("DES-CFB8", ossl_des_cfb8_functions), | |
f5056577 | 145 | #endif /* OPENSSL_NO_DES */ |
d0308923 MC |
146 | { NULL, NULL, NULL } |
147 | }; | |
148 | ||
fdaad3f1 | 149 | static const OSSL_ALGORITHM *legacy_query(void *provctx, int operation_id, |
d0308923 MC |
150 | int *no_cache) |
151 | { | |
152 | *no_cache = 0; | |
153 | switch (operation_id) { | |
154 | case OSSL_OP_DIGEST: | |
155 | return legacy_digests; | |
f5056577 SL |
156 | case OSSL_OP_CIPHER: |
157 | return legacy_ciphers; | |
d0308923 MC |
158 | } |
159 | return NULL; | |
160 | } | |
161 | ||
78906fff RL |
162 | static void legacy_teardown(void *provctx) |
163 | { | |
a829b735 | 164 | OSSL_LIB_CTX_free(PROV_LIBCTX_OF(provctx)); |
7d6766cb | 165 | ossl_prov_ctx_free(provctx); |
78906fff RL |
166 | } |
167 | ||
d0308923 MC |
168 | /* Functions we provide to the core */ |
169 | static const OSSL_DISPATCH legacy_dispatch_table[] = { | |
78906fff | 170 | { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))legacy_teardown }, |
dca97d00 | 171 | { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))legacy_gettable_params }, |
d0308923 MC |
172 | { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))legacy_get_params }, |
173 | { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))legacy_query }, | |
174 | { 0, NULL } | |
175 | }; | |
176 | ||
d40b42ab | 177 | int OSSL_provider_init(const OSSL_CORE_HANDLE *handle, |
d0308923 | 178 | const OSSL_DISPATCH *in, |
a39eb840 RL |
179 | const OSSL_DISPATCH **out, |
180 | void **provctx) | |
d0308923 | 181 | { |
a829b735 | 182 | OSSL_FUNC_core_get_libctx_fn *c_get_libctx = NULL; |
b4250010 | 183 | OSSL_LIB_CTX *libctx = NULL; |
8013a933 | 184 | |
d0308923 MC |
185 | for (; in->function_id != 0; in++) { |
186 | switch (in->function_id) { | |
dca97d00 | 187 | case OSSL_FUNC_CORE_GETTABLE_PARAMS: |
363b1e5d | 188 | c_gettable_params = OSSL_FUNC_core_gettable_params(in); |
d0308923 MC |
189 | break; |
190 | case OSSL_FUNC_CORE_GET_PARAMS: | |
363b1e5d | 191 | c_get_params = OSSL_FUNC_core_get_params(in); |
d0308923 | 192 | break; |
a829b735 DMSP |
193 | case OSSL_FUNC_CORE_GET_LIBCTX: |
194 | c_get_libctx = OSSL_FUNC_core_get_libctx(in); | |
8013a933 | 195 | break; |
d0308923 MC |
196 | /* Just ignore anything we don't understand */ |
197 | default: | |
198 | break; | |
199 | } | |
200 | } | |
201 | ||
8013a933 RL |
202 | if (c_get_libctx == NULL) |
203 | return 0; | |
204 | ||
7d6766cb | 205 | if ((*provctx = ossl_prov_ctx_new()) == NULL |
b4250010 DMSP |
206 | || (libctx = OSSL_LIB_CTX_new()) == NULL) { |
207 | OSSL_LIB_CTX_free(libctx); | |
78906fff RL |
208 | legacy_teardown(*provctx); |
209 | *provctx = NULL; | |
210 | return 0; | |
211 | } | |
a829b735 | 212 | ossl_prov_ctx_set0_libctx(*provctx, libctx); |
7d6766cb | 213 | ossl_prov_ctx_set0_handle(*provctx, handle); |
78906fff | 214 | |
d0308923 | 215 | *out = legacy_dispatch_table; |
8013a933 | 216 | |
d0308923 MC |
217 | return 1; |
218 | } |