2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include "internal/cryptlib.h"
11 #include "internal/nelem.h"
12 #include "crypto/evp.h"
13 #include "crypto/asn1.h"
14 #include "internal/provider.h"
15 #include "evp_local.h"
17 static OSSL_PARAM *paramdefs_to_params(const OSSL_PARAM *paramdefs)
21 OSSL_PARAM *params, *q;
23 for (cnt = 1, p = paramdefs; p->key != NULL; p++, cnt++)
26 params = OPENSSL_zalloc(cnt * sizeof(*params));
28 for (p = paramdefs, q = params; ; p++, q++) {
33 q->data = NULL; /* In case the provider used it */
40 typedef union align_block_un {
44 #define ALIGN_SIZE sizeof(ALIGN_BLOCK)
46 static void *allocate_params_space(OSSL_PARAM *params)
48 unsigned char *data = NULL;
52 for (space = 0, p = params; p->key != NULL; p++)
53 space += ((p->return_size + ALIGN_SIZE - 1) / ALIGN_SIZE) * ALIGN_SIZE;
58 data = OPENSSL_zalloc(space);
60 for (space = 0, p = params; p->key != NULL; p++) {
61 p->data = data + space;
62 space += ((p->return_size + ALIGN_SIZE - 1) / ALIGN_SIZE) * ALIGN_SIZE;
68 void *evp_keymgmt_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt,
69 int want_domainparams)
71 void *provdata = NULL;
75 * If there is an underlying legacy key and it has changed, invalidate
76 * the cache of provider keys.
78 if (pk->pkey.ptr != NULL) {
80 * If there is no dirty counter, this key can't be used with
83 if (pk->ameth->dirty_cnt == NULL)
86 if (pk->ameth->dirty_cnt(pk) != pk->dirty_cnt_copy)
87 evp_keymgmt_clear_pkey_cache(pk);
91 * See if we have exported to this provider already.
92 * If we have, return immediately.
95 i < OSSL_NELEM(pk->pkeys) && pk->pkeys[i].keymgmt != NULL;
97 if (keymgmt == pk->pkeys[i].keymgmt
98 && want_domainparams == pk->pkeys[i].domainparams)
99 return pk->pkeys[i].provdata;
102 if (pk->pkey.ptr != NULL) {
103 /* There is a legacy key, try to export that one to the provider */
105 /* If the legacy key doesn't have an export function, give up */
106 if (pk->ameth->export_to == NULL)
109 /* Otherwise, simply use it. */
110 provdata = pk->ameth->export_to(pk, keymgmt, want_domainparams);
112 /* Synchronize the dirty count, but only if we exported successfully */
113 if (provdata != NULL)
114 pk->dirty_cnt_copy = pk->ameth->dirty_cnt(pk);
118 * Here, there is no legacy key, so we look at the already cached
119 * provider keys, and import from the first that supports it
120 * (i.e. use its export function), and export the imported data to
124 void *(*importfn)(void *provctx, const OSSL_PARAM params[]) =
125 want_domainparams ? keymgmt->importdomparams : keymgmt->importkey;
128 * If the given keymgmt doesn't have an import function, give up
130 if (importfn == NULL)
133 for (j = 0; j < i && pk->pkeys[j].keymgmt != NULL; j++) {
134 if (pk->pkeys[j].keymgmt->exportkey != NULL) {
135 const OSSL_PARAM *paramdefs = NULL;
136 OSSL_PARAM *params = NULL;
139 ossl_provider_ctx(EVP_KEYMGMT_provider(keymgmt));
140 int (*exportfn)(void *provctx, OSSL_PARAM params[]) = NULL;
142 if (pk->pkeys[j].domainparams != want_domainparams)
145 exportfn = want_domainparams
146 ? pk->pkeys[j].keymgmt->exportdomparams
147 : pk->pkeys[j].keymgmt->exportkey;
149 paramdefs = pk->pkeys[j].keymgmt->exportkey_types();
151 * All params have 'data' set to NULL. In that case,
152 * the exportkey call should just fill in 'return_size'
153 * in all applicable params.
155 params = paramdefs_to_params(paramdefs);
156 /* Get 'return_size' filled */
157 exportfn(pk->pkeys[j].provdata, params);
160 * Allocate space and assign 'data' to point into the
162 * If something goes wrong, go to the next cached key.
164 if ((data = allocate_params_space(params)) == NULL)
168 * Call the exportkey function a second time, to get
170 * If something goes wrong, go to the next cached key.
172 if (!exportfn(pk->pkeys[j].provdata, params))
176 * We should have all the data at this point, so import
177 * into the new provider and hope to get a key back.
179 provdata = importfn(provctx, params);
182 OPENSSL_free(params);
185 if (provdata != NULL)
192 * TODO(3.0) Right now, we assume we have ample space. We will
193 * have to think about a cache aging scheme, though, if |i| indexes
196 j = ossl_assert(i < OSSL_NELEM(pk->pkeys));
198 if (provdata != NULL) {
199 EVP_KEYMGMT_up_ref(keymgmt);
200 pk->pkeys[i].keymgmt = keymgmt;
201 pk->pkeys[i].provdata = provdata;
202 pk->pkeys[i].domainparams = want_domainparams;
208 void evp_keymgmt_clear_pkey_cache(EVP_PKEY *pk)
214 i < OSSL_NELEM(pk->pkeys) && pk->pkeys[i].keymgmt != NULL;
216 EVP_KEYMGMT *keymgmt = pk->pkeys[i].keymgmt;
217 void *provdata = pk->pkeys[i].provdata;
219 pk->pkeys[i].keymgmt = NULL;
220 pk->pkeys[i].provdata = NULL;
221 if (pk->pkeys[i].domainparams)
222 keymgmt->freedomparams(provdata);
224 keymgmt->freekey(provdata);
225 EVP_KEYMGMT_free(keymgmt);
231 /* internal functions */
232 /* TODO(3.0) decide if these should be public or internal */
233 void *evp_keymgmt_importdomparams(const EVP_KEYMGMT *keymgmt,
234 const OSSL_PARAM params[])
236 void *provctx = ossl_provider_ctx(EVP_KEYMGMT_provider(keymgmt));
238 return keymgmt->importdomparams(provctx, params);
241 void *evp_keymgmt_gendomparams(const EVP_KEYMGMT *keymgmt,
242 const OSSL_PARAM params[])
244 void *provctx = ossl_provider_ctx(EVP_KEYMGMT_provider(keymgmt));
246 return keymgmt->gendomparams(provctx, params);
249 void evp_keymgmt_freedomparams(const EVP_KEYMGMT *keymgmt,
252 keymgmt->freedomparams(provdomparams);
255 int evp_keymgmt_exportdomparams(const EVP_KEYMGMT *keymgmt,
256 void *provdomparams, OSSL_PARAM params[])
258 return keymgmt->exportdomparams(provdomparams, params);
261 const OSSL_PARAM *evp_keymgmt_importdomparam_types(const EVP_KEYMGMT *keymgmt)
263 return keymgmt->importdomparam_types();
266 const OSSL_PARAM *evp_keymgmt_exportdomparam_types(const EVP_KEYMGMT *keymgmt)
268 return keymgmt->exportdomparam_types();
272 void *evp_keymgmt_importkey(const EVP_KEYMGMT *keymgmt,
273 const OSSL_PARAM params[])
275 void *provctx = ossl_provider_ctx(EVP_KEYMGMT_provider(keymgmt));
277 return keymgmt->importkey(provctx, params);
280 void *evp_keymgmt_genkey(const EVP_KEYMGMT *keymgmt, void *domparams,
281 const OSSL_PARAM params[])
283 void *provctx = ossl_provider_ctx(EVP_KEYMGMT_provider(keymgmt));
285 return keymgmt->genkey(provctx, domparams, params);
288 void *evp_keymgmt_loadkey(const EVP_KEYMGMT *keymgmt,
289 void *id, size_t idlen)
291 void *provctx = ossl_provider_ctx(EVP_KEYMGMT_provider(keymgmt));
293 return keymgmt->loadkey(provctx, id, idlen);
296 void evp_keymgmt_freekey(const EVP_KEYMGMT *keymgmt, void *provkey)
298 keymgmt->freekey(provkey);
301 int evp_keymgmt_exportkey(const EVP_KEYMGMT *keymgmt, void *provkey,
304 return keymgmt->exportkey(provkey, params);
307 const OSSL_PARAM *evp_keymgmt_importkey_types(const EVP_KEYMGMT *keymgmt)
309 return keymgmt->importkey_types();
312 const OSSL_PARAM *evp_keymgmt_exportkey_types(const EVP_KEYMGMT *keymgmt)
314 return keymgmt->exportkey_types();