2 * Copyright 2006-2021 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
12 #include <openssl/core.h>
13 #include <openssl/core_names.h>
14 #include "internal/cryptlib.h"
15 #include "internal/core.h"
16 #include <openssl/objects.h>
17 #include <openssl/evp.h>
18 #include "crypto/bn.h"
19 #include "crypto/asn1.h"
20 #include "crypto/evp.h"
21 #include "evp_local.h"
23 static int gen_init(EVP_PKEY_CTX *ctx, int operation)
30 evp_pkey_ctx_free_old_ops(ctx);
31 ctx->operation = operation;
33 if (ctx->keymgmt == NULL || ctx->keymgmt->gen_init == NULL)
37 case EVP_PKEY_OP_PARAMGEN:
38 ctx->op.keymgmt.genctx =
39 evp_keymgmt_gen_init(ctx->keymgmt,
40 OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
42 case EVP_PKEY_OP_KEYGEN:
43 ctx->op.keymgmt.genctx =
44 evp_keymgmt_gen_init(ctx->keymgmt, OSSL_KEYMGMT_SELECT_KEYPAIR);
48 if (ctx->op.keymgmt.genctx == NULL)
49 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
58 if (ctx->pmeth == NULL
59 || (operation == EVP_PKEY_OP_PARAMGEN
60 && ctx->pmeth->paramgen == NULL)
61 || (operation == EVP_PKEY_OP_KEYGEN
62 && ctx->pmeth->keygen == NULL))
67 case EVP_PKEY_OP_PARAMGEN:
68 if (ctx->pmeth->paramgen_init != NULL)
69 ret = ctx->pmeth->paramgen_init(ctx);
71 case EVP_PKEY_OP_KEYGEN:
72 if (ctx->pmeth->keygen_init != NULL)
73 ret = ctx->pmeth->keygen_init(ctx);
79 if (ret <= 0 && ctx != NULL) {
80 evp_pkey_ctx_free_old_ops(ctx);
81 ctx->operation = EVP_PKEY_OP_UNDEFINED;
86 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
91 int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx)
93 return gen_init(ctx, EVP_PKEY_OP_PARAMGEN);
96 int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx)
98 return gen_init(ctx, EVP_PKEY_OP_KEYGEN);
101 static int ossl_callback_to_pkey_gencb(const OSSL_PARAM params[], void *arg)
103 EVP_PKEY_CTX *ctx = arg;
104 const OSSL_PARAM *param = NULL;
107 if (ctx->pkey_gencb == NULL)
108 return 1; /* No callback? That's fine */
110 if ((param = OSSL_PARAM_locate_const(params, OSSL_GEN_PARAM_POTENTIAL))
112 || !OSSL_PARAM_get_int(param, &p))
114 if ((param = OSSL_PARAM_locate_const(params, OSSL_GEN_PARAM_ITERATION))
116 || !OSSL_PARAM_get_int(param, &n))
119 ctx->keygen_info[0] = p;
120 ctx->keygen_info[1] = n;
122 return ctx->pkey_gencb(ctx);
125 int EVP_PKEY_gen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
129 EVP_PKEY *allocated_pkey = NULL;
130 /* Legacy compatible keygen callback info, only used with provider impls */
139 if ((ctx->operation & EVP_PKEY_OP_TYPE_GEN) == 0)
140 goto not_initialized;
143 *ppkey = allocated_pkey = EVP_PKEY_new();
145 if (*ppkey == NULL) {
146 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
150 if (ctx->op.keymgmt.genctx == NULL)
154 * Asssigning gentmp to ctx->keygen_info is something our legacy
155 * implementations do. Because the provider implementations aren't
156 * allowed to reach into our EVP_PKEY_CTX, we need to provide similar
157 * space for backward compatibility. It's ok that we attach a local
158 * variable, as it should only be useful in the calls down from here.
159 * This is cleared as soon as it isn't useful any more, i.e. directly
160 * after the evp_keymgmt_util_gen() call.
162 ctx->keygen_info = gentmp;
163 ctx->keygen_info_count = 2;
166 if (ctx->pkey != NULL) {
167 EVP_KEYMGMT *tmp_keymgmt = ctx->keymgmt;
169 evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
170 &tmp_keymgmt, ctx->propquery);
172 if (tmp_keymgmt == NULL)
175 * It's ok if keydata is NULL here. The backend is expected to deal
176 * with that as it sees fit.
178 ret = evp_keymgmt_gen_set_template(ctx->keymgmt,
179 ctx->op.keymgmt.genctx, keydata);
183 * the returned value from evp_keymgmt_util_gen() is cached in *ppkey,
184 * so we do not need to save it, just check it.
187 && (evp_keymgmt_util_gen(*ppkey, ctx->keymgmt, ctx->op.keymgmt.genctx,
188 ossl_callback_to_pkey_gencb, ctx)
191 ctx->keygen_info = NULL;
194 /* In case |*ppkey| was originally a legacy key */
196 evp_pkey_free_legacy(*ppkey);
200 * Because we still have legacy keys
201 * TODO remove this #legacy internal keys are gone
203 (*ppkey)->type = ctx->legacy_keytype;
212 * If we get here then we're using legacy paramgen/keygen. In that case
213 * the pkey in ctx (if there is one) had better not be provided (because the
214 * legacy methods may not know how to handle it). However we can only get
215 * here if ctx->op.keymgmt.genctx == NULL, but that should never be the case
216 * if ctx->pkey is provided because we don't allow this when we initialise
219 if (ctx->pkey != NULL && !ossl_assert(!evp_pkey_is_provided(ctx->pkey)))
222 switch (ctx->operation) {
223 case EVP_PKEY_OP_PARAMGEN:
224 ret = ctx->pmeth->paramgen(ctx, *ppkey);
226 case EVP_PKEY_OP_KEYGEN:
227 ret = ctx->pmeth->keygen(ctx, *ppkey);
236 if (allocated_pkey != NULL)
238 EVP_PKEY_free(allocated_pkey);
243 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
247 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
252 ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_DOMAIN_PARAMETERS);
258 int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
260 if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {
261 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
264 return EVP_PKEY_gen(ctx, ppkey);
267 int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
269 if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
270 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
273 return EVP_PKEY_gen(ctx, ppkey);
276 void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb)
278 ctx->pkey_gencb = cb;
281 EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx)
283 return ctx->pkey_gencb;
287 * "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB style
291 static int trans_cb(int a, int b, BN_GENCB *gcb)
293 EVP_PKEY_CTX *ctx = BN_GENCB_get_arg(gcb);
294 ctx->keygen_info[0] = a;
295 ctx->keygen_info[1] = b;
296 return ctx->pkey_gencb(ctx);
299 void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx)
301 BN_GENCB_set(cb, trans_cb, ctx);
304 int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx)
307 return ctx->keygen_info_count;
308 if (idx < 0 || idx > ctx->keygen_info_count)
310 return ctx->keygen_info[idx];
315 EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
316 const unsigned char *key, int keylen)
318 EVP_PKEY_CTX *mac_ctx = NULL;
319 EVP_PKEY *mac_key = NULL;
320 mac_ctx = EVP_PKEY_CTX_new_id(type, e);
323 if (EVP_PKEY_keygen_init(mac_ctx) <= 0)
325 if (EVP_PKEY_CTX_set_mac_key(mac_ctx, key, keylen) <= 0)
327 if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0)
330 EVP_PKEY_CTX_free(mac_ctx);
334 #endif /* FIPS_MODULE */
336 /*- All methods below can also be used in FIPS_MODULE */
338 static int fromdata_init(EVP_PKEY_CTX *ctx, int operation)
340 if (ctx == NULL || ctx->keytype == NULL)
343 evp_pkey_ctx_free_old_ops(ctx);
344 if (ctx->keymgmt == NULL)
347 ctx->operation = operation;
352 ctx->operation = EVP_PKEY_OP_UNDEFINED;
353 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
357 int EVP_PKEY_fromdata_init(EVP_PKEY_CTX *ctx)
359 return fromdata_init(ctx, EVP_PKEY_OP_FROMDATA);
362 int EVP_PKEY_fromdata(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey, int selection,
365 void *keydata = NULL;
367 if (ctx == NULL || (ctx->operation & EVP_PKEY_OP_FROMDATA) == 0) {
368 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
376 *ppkey = EVP_PKEY_new();
378 if (*ppkey == NULL) {
379 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
383 keydata = evp_keymgmt_util_fromdata(*ppkey, ctx->keymgmt, selection, params);
386 /* keydata is cached in *ppkey, so we need not bother with it further */
390 const OSSL_PARAM *EVP_PKEY_fromdata_settable(EVP_PKEY_CTX *ctx, int selection)
392 /* We call fromdata_init to get ctx->keymgmt populated */
393 if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED) == 1)
394 return evp_keymgmt_import_types(ctx->keymgmt, selection);