TianoCore EDK2 master
Loading...
Searching...
No Matches
uefiprov.c
Go to the documentation of this file.
1
9#include <string.h>
10#include <stdio.h>
11#include <openssl/opensslconf.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/bio.h"
17#include "prov/provider_ctx.h"
18#include "prov/providercommon.h"
19#include "prov/implementations.h"
20#include "prov/names.h"
21#include "prov/provider_util.h"
22#include "prov/seeding.h"
23#include "internal/nelem.h"
24#include "provider_local.h"
25
26OSSL_provider_init_fn ossl_uefi_provider_init;
27const OSSL_PROVIDER_INFO ossl_predefined_providers[] = {
28 { "default", NULL, ossl_uefi_provider_init, NULL, 1 },
29 { NULL, NULL, NULL, NULL, 0 }
30};
31
32/*
33 * Forward declarations to ensure that interface functions are correctly
34 * defined.
35 */
36static OSSL_FUNC_provider_gettable_params_fn deflt_gettable_params;
37static OSSL_FUNC_provider_get_params_fn deflt_get_params;
38static OSSL_FUNC_provider_query_operation_fn deflt_query;
39
40#define ALGC(NAMES, FUNC, CHECK) { { NAMES, "provider=default", FUNC }, CHECK }
41#define ALG(NAMES, FUNC) ALGC(NAMES, FUNC, NULL)
42
43/* Functions provided by the core */
44static OSSL_FUNC_core_gettable_params_fn *c_gettable_params = NULL;
45static OSSL_FUNC_core_get_params_fn *c_get_params = NULL;
46
47/* Parameters we provide to the core */
48static const OSSL_PARAM deflt_param_types[] = {
49 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
50 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
51 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0),
52 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_STATUS, OSSL_PARAM_INTEGER, NULL, 0),
53 OSSL_PARAM_END
54};
55
56static const OSSL_PARAM *deflt_gettable_params(void *provctx)
57{
58 return deflt_param_types;
59}
60
61static int deflt_get_params(void *provctx, OSSL_PARAM params[])
62{
63 OSSL_PARAM *p;
64
65 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
66 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL Default Provider"))
67 return 0;
68 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
69 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
70 return 0;
71 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
72 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
73 return 0;
74 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_STATUS);
75 if (p != NULL && !OSSL_PARAM_set_int(p, ossl_prov_is_running()))
76 return 0;
77 return 1;
78}
79
80/*
81 * For the algorithm names, we use the following formula for our primary
82 * names:
83 *
84 * ALGNAME[VERSION?][-SUBNAME[VERSION?]?][-SIZE?][-MODE?]
85 *
86 * VERSION is only present if there are multiple versions of
87 * an alg (MD2, MD4, MD5). It may be omitted if there is only
88 * one version (if a subsequent version is released in the future,
89 * we can always change the canonical name, and add the old name
90 * as an alias).
91 *
92 * SUBNAME may be present where we are combining multiple
93 * algorithms together, e.g. MD5-SHA1.
94 *
95 * SIZE is only present if multiple versions of an algorithm exist
96 * with different sizes (e.g. AES-128-CBC, AES-256-CBC)
97 *
98 * MODE is only present where applicable.
99 *
100 * We add diverse other names where applicable, such as the names that
101 * NIST uses, or that are used for ASN.1 OBJECT IDENTIFIERs, or names
102 * we have used historically.
103 *
104 * Algorithm names are case insensitive, but we use all caps in our "canonical"
105 * names for consistency.
106 */
107static const OSSL_ALGORITHM deflt_digests[] = {
108 /* Our primary name:NIST name[:our older names] */
109 { PROV_NAMES_SHA1, "provider=default", ossl_sha1_functions },
110 { PROV_NAMES_SHA2_224, "provider=default", ossl_sha224_functions },
111 { PROV_NAMES_SHA2_256, "provider=default", ossl_sha256_functions },
112 { PROV_NAMES_SHA2_384, "provider=default", ossl_sha384_functions },
113 { PROV_NAMES_SHA2_512, "provider=default", ossl_sha512_functions },
114
115#ifndef OPENSSL_NO_SM3
116 { PROV_NAMES_SM3, "provider=default", ossl_sm3_functions },
117#endif /* OPENSSL_NO_SM3 */
118
119#ifndef OPENSSL_NO_MD5
120 { PROV_NAMES_MD5, "provider=default", ossl_md5_functions },
121#endif /* OPENSSL_NO_MD5 */
122
123 { PROV_NAMES_NULL, "provider=default", ossl_nullmd_functions },
124 { NULL, NULL, NULL }
125};
126
127static const OSSL_ALGORITHM_CAPABLE deflt_ciphers[] = {
128 ALG(PROV_NAMES_NULL, ossl_null_functions),
129 ALG(PROV_NAMES_AES_256_ECB, ossl_aes256ecb_functions),
130 ALG(PROV_NAMES_AES_192_ECB, ossl_aes192ecb_functions),
131 ALG(PROV_NAMES_AES_128_ECB, ossl_aes128ecb_functions),
132 ALG(PROV_NAMES_AES_256_CBC, ossl_aes256cbc_functions),
133 ALG(PROV_NAMES_AES_192_CBC, ossl_aes192cbc_functions),
134 ALG(PROV_NAMES_AES_128_CBC, ossl_aes128cbc_functions),
135
136 ALG(PROV_NAMES_AES_256_CTR, ossl_aes256ctr_functions),
137 ALG(PROV_NAMES_AES_192_CTR, ossl_aes192ctr_functions),
138 ALG(PROV_NAMES_AES_128_CTR, ossl_aes128ctr_functions),
139
140 ALG(PROV_NAMES_AES_256_GCM, ossl_aes256gcm_functions),
141 ALG(PROV_NAMES_AES_192_GCM, ossl_aes192gcm_functions),
142 ALG(PROV_NAMES_AES_128_GCM, ossl_aes128gcm_functions),
143
144 ALGC (
145 PROV_NAMES_AES_128_CBC_HMAC_SHA256,
146 ossl_aes128cbc_hmac_sha256_functions,
147 ossl_cipher_capable_aes_cbc_hmac_sha256
148 ),
149 ALGC (
150 PROV_NAMES_AES_256_CBC_HMAC_SHA256,
151 ossl_aes256cbc_hmac_sha256_functions,
152 ossl_cipher_capable_aes_cbc_hmac_sha256
153 ),
154
155 { { NULL, NULL, NULL }, NULL }
156};
157static OSSL_ALGORITHM exported_ciphers[OSSL_NELEM(deflt_ciphers)];
158
159static const OSSL_ALGORITHM deflt_macs[] = {
160 { PROV_NAMES_HMAC, "provider=default", ossl_hmac_functions },
161 { NULL, NULL, NULL }
162};
163
164static const OSSL_ALGORITHM deflt_kdfs[] = {
165 { PROV_NAMES_HKDF, "provider=default", ossl_kdf_hkdf_functions },
166 { PROV_NAMES_SSKDF, "provider=default", ossl_kdf_sskdf_functions },
167 { PROV_NAMES_PBKDF2, "provider=default", ossl_kdf_pbkdf2_functions },
168 { PROV_NAMES_SSHKDF, "provider=default", ossl_kdf_sshkdf_functions },
169 { PROV_NAMES_TLS1_PRF, "provider=default", ossl_kdf_tls1_prf_functions },
170 { NULL, NULL, NULL }
171};
172
173static const OSSL_ALGORITHM deflt_keyexch[] = {
174#ifndef OPENSSL_NO_DH
175 { PROV_NAMES_DH, "provider=default", ossl_dh_keyexch_functions },
176#endif
177#ifndef OPENSSL_NO_EC
178 { PROV_NAMES_ECDH, "provider=default", ossl_ecdh_keyexch_functions },
179#endif
180 { PROV_NAMES_TLS1_PRF, "provider=default", ossl_kdf_tls1_prf_keyexch_functions },
181 { PROV_NAMES_HKDF, "provider=default", ossl_kdf_hkdf_keyexch_functions },
182 { NULL, NULL, NULL }
183};
184
185static const OSSL_ALGORITHM deflt_rands[] = {
186 { PROV_NAMES_CTR_DRBG, "provider=default", ossl_drbg_ctr_functions },
187 { PROV_NAMES_HASH_DRBG, "provider=default", ossl_drbg_hash_functions },
188 { NULL, NULL, NULL }
189};
190
191static const OSSL_ALGORITHM deflt_signature[] = {
192 { PROV_NAMES_RSA, "provider=default", ossl_rsa_signature_functions },
193#ifndef OPENSSL_NO_EC
194 { PROV_NAMES_ECDSA, "provider=default", ossl_ecdsa_signature_functions },
195#endif
196
197 { NULL, NULL, NULL }
198};
199
200static const OSSL_ALGORITHM deflt_asym_cipher[] = {
201 { PROV_NAMES_RSA, "provider=default", ossl_rsa_asym_cipher_functions },
202 { NULL, NULL, NULL }
203};
204
205static const OSSL_ALGORITHM deflt_keymgmt[] = {
206#ifndef OPENSSL_NO_DH
207 { PROV_NAMES_DH, "provider=default", ossl_dh_keymgmt_functions,
208 PROV_DESCS_DH },
209 { PROV_NAMES_DHX, "provider=default", ossl_dhx_keymgmt_functions,
210 PROV_DESCS_DHX },
211#endif
212
213 { PROV_NAMES_RSA, "provider=default", ossl_rsa_keymgmt_functions,
214 PROV_DESCS_RSA },
215 { PROV_NAMES_RSA_PSS, "provider=default", ossl_rsapss_keymgmt_functions,
216 PROV_DESCS_RSA_PSS },
217#ifndef OPENSSL_NO_EC
218 { PROV_NAMES_EC, "provider=default", ossl_ec_keymgmt_functions,
219 PROV_DESCS_EC },
220#endif
221 { PROV_NAMES_TLS1_PRF, "provider=default", ossl_kdf_keymgmt_functions,
222 PROV_DESCS_TLS1_PRF_SIGN },
223 { PROV_NAMES_HKDF, "provider=default", ossl_kdf_keymgmt_functions,
224 PROV_DESCS_HKDF_SIGN },
225
226 { NULL, NULL, NULL }
227};
228
229static const OSSL_ALGORITHM deflt_decoder[] = {
230#define DECODER_PROVIDER "default"
231#include "decoders.inc"
232 { NULL, NULL, NULL }
233#undef DECODER_PROVIDER
234};
235
236static const OSSL_ALGORITHM *deflt_query(void *provctx, int operation_id,
237 int *no_cache)
238{
239 *no_cache = 0;
240 switch (operation_id) {
241 case OSSL_OP_DIGEST:
242 return deflt_digests;
243 case OSSL_OP_CIPHER:
244 return exported_ciphers;
245 case OSSL_OP_MAC:
246 return deflt_macs;
247 case OSSL_OP_KDF:
248 return deflt_kdfs;
249 case OSSL_OP_RAND:
250 return deflt_rands;
251 case OSSL_OP_KEYMGMT:
252 return deflt_keymgmt;
253 case OSSL_OP_KEYEXCH:
254 return deflt_keyexch;
255 case OSSL_OP_SIGNATURE:
256 return deflt_signature;
257 case OSSL_OP_ASYM_CIPHER:
258 return deflt_asym_cipher;
259 case OSSL_OP_DECODER:
260 return deflt_decoder;
261 }
262 return NULL;
263}
264
265
266static void deflt_teardown(void *provctx)
267{
268 BIO_meth_free(ossl_prov_ctx_get0_core_bio_method(provctx));
269 ossl_prov_ctx_free(provctx);
270}
271
272/* Functions we provide to the core */
273static const OSSL_DISPATCH deflt_dispatch_table[] = {
274 { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))deflt_teardown },
275 { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))deflt_gettable_params },
276 { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))deflt_get_params },
277 { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))deflt_query },
278 { OSSL_FUNC_PROVIDER_GET_CAPABILITIES,
279 (void (*)(void))ossl_prov_get_capabilities },
280 { 0, NULL }
281};
282
283OSSL_provider_init_fn ossl_uefi_provider_init;
284
285int ossl_uefi_provider_init(const OSSL_CORE_HANDLE *handle,
286 const OSSL_DISPATCH *in,
287 const OSSL_DISPATCH **out,
288 void **provctx)
289{
290 OSSL_FUNC_core_get_libctx_fn *c_get_libctx = NULL;
291 BIO_METHOD *corebiometh;
292
293 if (!ossl_prov_bio_from_dispatch(in)
294 || !ossl_prov_seeding_from_dispatch(in))
295 return 0;
296 for (; in->function_id != 0; in++) {
297 switch (in->function_id) {
298 case OSSL_FUNC_CORE_GETTABLE_PARAMS:
299 c_gettable_params = OSSL_FUNC_core_gettable_params(in);
300 break;
301 case OSSL_FUNC_CORE_GET_PARAMS:
302 c_get_params = OSSL_FUNC_core_get_params(in);
303 break;
304 case OSSL_FUNC_CORE_GET_LIBCTX:
305 c_get_libctx = OSSL_FUNC_core_get_libctx(in);
306 break;
307 default:
308 /* Just ignore anything we don't understand */
309 break;
310 }
311 }
312
313 if (c_get_libctx == NULL)
314 return 0;
315
316 /*
317 * We want to make sure that all calls from this provider that requires
318 * a library context use the same context as the one used to call our
319 * functions. We do that by passing it along in the provider context.
320 *
321 * This only works for built-in providers. Most providers should
322 * create their own library context.
323 */
324 if ((*provctx = ossl_prov_ctx_new()) == NULL
325 || (corebiometh = ossl_bio_prov_init_bio_method()) == NULL) {
326 ossl_prov_ctx_free(*provctx);
327 *provctx = NULL;
328 return 0;
329 }
330 ossl_prov_ctx_set0_libctx(*provctx,
331 (OSSL_LIB_CTX *)c_get_libctx(handle));
332 ossl_prov_ctx_set0_handle(*provctx, handle);
333 ossl_prov_ctx_set0_core_bio_method(*provctx, corebiometh);
334
335 *out = deflt_dispatch_table;
336 ossl_prov_cache_exported_algorithms(deflt_ciphers, exported_ciphers);
337
338 return 1;
339}
#define NULL
Definition: Base.h:319