2 * Copyright 2006-2020 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 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_EC)
27 /* TODO(3.0) remove when provider SM2 key generation is implemented */
29 # include <openssl/ec.h>
30 # include "internal/sizes.h"
33 static int gen_init(EVP_PKEY_CTX *ctx, int operation)
40 evp_pkey_ctx_free_old_ops(ctx);
41 ctx->operation = operation;
43 if (ctx->keymgmt == NULL || ctx->keymgmt->gen_init == NULL)
46 /* TODO remove when provider SM2 key generation is implemented */
48 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id == EVP_PKEY_SM2)
53 case EVP_PKEY_OP_PARAMGEN:
54 ctx->op.keymgmt.genctx =
55 evp_keymgmt_gen_init(ctx->keymgmt,
56 OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
58 case EVP_PKEY_OP_KEYGEN:
59 ctx->op.keymgmt.genctx =
60 evp_keymgmt_gen_init(ctx->keymgmt, OSSL_KEYMGMT_SELECT_KEYPAIR);
64 if (ctx->op.keymgmt.genctx == NULL)
65 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
74 if (ctx->pmeth == NULL
75 || (operation == EVP_PKEY_OP_PARAMGEN
76 && ctx->pmeth->paramgen == NULL)
77 || (operation == EVP_PKEY_OP_KEYGEN
78 && ctx->pmeth->keygen == NULL))
83 case EVP_PKEY_OP_PARAMGEN:
84 if (ctx->pmeth->paramgen_init != NULL)
85 ret = ctx->pmeth->paramgen_init(ctx);
87 case EVP_PKEY_OP_KEYGEN:
88 if (ctx->pmeth->keygen_init != NULL)
89 ret = ctx->pmeth->keygen_init(ctx);
95 if (ret <= 0 && ctx != NULL) {
96 evp_pkey_ctx_free_old_ops(ctx);
97 ctx->operation = EVP_PKEY_OP_UNDEFINED;
102 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
107 int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx)
109 return gen_init(ctx, EVP_PKEY_OP_PARAMGEN);
112 int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx)
114 return gen_init(ctx, EVP_PKEY_OP_KEYGEN);
117 static int ossl_callback_to_pkey_gencb(const OSSL_PARAM params[], void *arg)
119 EVP_PKEY_CTX *ctx = arg;
120 const OSSL_PARAM *param = NULL;
123 if (ctx->pkey_gencb == NULL)
124 return 1; /* No callback? That's fine */
126 if ((param = OSSL_PARAM_locate_const(params, OSSL_GEN_PARAM_POTENTIAL))
128 || !OSSL_PARAM_get_int(param, &p))
130 if ((param = OSSL_PARAM_locate_const(params, OSSL_GEN_PARAM_ITERATION))
132 || !OSSL_PARAM_get_int(param, &n))
135 ctx->keygen_info[0] = p;
136 ctx->keygen_info[1] = n;
138 return ctx->pkey_gencb(ctx);
141 int EVP_PKEY_gen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
145 EVP_PKEY *allocated_pkey = NULL;
146 /* Legacy compatible keygen callback info, only used with provider impls */
155 if ((ctx->operation & EVP_PKEY_OP_TYPE_GEN) == 0)
156 goto not_initialized;
159 *ppkey = allocated_pkey = EVP_PKEY_new();
161 if (*ppkey == NULL) {
162 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
166 if (ctx->op.keymgmt.genctx == NULL)
170 * Asssigning gentmp to ctx->keygen_info is something our legacy
171 * implementations do. Because the provider implementations aren't
172 * allowed to reach into our EVP_PKEY_CTX, we need to provide similar
173 * space for backward compatibility. It's ok that we attach a local
174 * variable, as it should only be useful in the calls down from here.
175 * This is cleared as soon as it isn't useful any more, i.e. directly
176 * after the evp_keymgmt_util_gen() call.
178 ctx->keygen_info = gentmp;
179 ctx->keygen_info_count = 2;
182 if (ctx->pkey != NULL) {
183 EVP_KEYMGMT *tmp_keymgmt = ctx->keymgmt;
185 evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
186 &tmp_keymgmt, ctx->propquery);
188 if (tmp_keymgmt == NULL)
191 * It's ok if keydata is NULL here. The backend is expected to deal
192 * with that as it sees fit.
194 ret = evp_keymgmt_gen_set_template(ctx->keymgmt,
195 ctx->op.keymgmt.genctx, keydata);
199 * the returned value from evp_keymgmt_util_gen() is cached in *ppkey,
200 * so we so not need to save it, just check it.
203 && (evp_keymgmt_util_gen(*ppkey, ctx->keymgmt, ctx->op.keymgmt.genctx,
204 ossl_callback_to_pkey_gencb, ctx)
207 ctx->keygen_info = NULL;
210 /* In case |*ppkey| was originally a legacy key */
212 evp_pkey_free_legacy(*ppkey);
216 * Because we still have legacy keys, and evp_pkey_downgrade()
217 * TODO remove this #legacy internal keys are gone
219 (*ppkey)->type = ctx->legacy_keytype;
221 /* TODO remove when SM2 key have been cleanly separated from EC keys */
224 * Legacy SM2 keys are implemented as EC_KEY with a twist. The legacy
225 * key generation detects the SM2 curve and "magically" changes the pkey
227 * Since we don't have SM2 in the provider implementation, we need to
228 * downgrade the generated provider side key to a legacy one under the
231 * THIS IS AN UGLY BUT TEMPORARY HACK
234 char curve_name[OSSL_MAX_NAME_SIZE] = "";
236 if (!EVP_PKEY_get_utf8_string_param(*ppkey, OSSL_PKEY_PARAM_GROUP_NAME,
237 curve_name, sizeof(curve_name),
239 || strcmp(curve_name, "SM2") != 0)
243 if (!evp_pkey_downgrade(*ppkey)
244 || !EVP_PKEY_set_alias_type(*ppkey, EVP_PKEY_SM2))
253 if (ctx->pkey && !evp_pkey_downgrade(ctx->pkey))
255 switch (ctx->operation) {
256 case EVP_PKEY_OP_PARAMGEN:
257 ret = ctx->pmeth->paramgen(ctx, *ppkey);
259 case EVP_PKEY_OP_KEYGEN:
260 ret = ctx->pmeth->keygen(ctx, *ppkey);
269 if (allocated_pkey != NULL)
271 EVP_PKEY_free(allocated_pkey);
276 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
280 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
285 ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_DOMAIN_PARAMETERS);
291 int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
293 if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {
294 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
297 return EVP_PKEY_gen(ctx, ppkey);
300 int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
302 if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
303 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
306 return EVP_PKEY_gen(ctx, ppkey);
309 void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb)
311 ctx->pkey_gencb = cb;
314 EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx)
316 return ctx->pkey_gencb;
320 * "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB style
324 static int trans_cb(int a, int b, BN_GENCB *gcb)
326 EVP_PKEY_CTX *ctx = BN_GENCB_get_arg(gcb);
327 ctx->keygen_info[0] = a;
328 ctx->keygen_info[1] = b;
329 return ctx->pkey_gencb(ctx);
332 void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx)
334 BN_GENCB_set(cb, trans_cb, ctx);
337 int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx)
340 return ctx->keygen_info_count;
341 if (idx < 0 || idx > ctx->keygen_info_count)
343 return ctx->keygen_info[idx];
348 EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
349 const unsigned char *key, int keylen)
351 EVP_PKEY_CTX *mac_ctx = NULL;
352 EVP_PKEY *mac_key = NULL;
353 mac_ctx = EVP_PKEY_CTX_new_id(type, e);
356 if (EVP_PKEY_keygen_init(mac_ctx) <= 0)
358 if (EVP_PKEY_CTX_set_mac_key(mac_ctx, key, keylen) <= 0)
360 if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0)
363 EVP_PKEY_CTX_free(mac_ctx);
367 #endif /* FIPS_MODULE */
369 /*- All methods below can also be used in FIPS_MODULE */
371 static int fromdata_init(EVP_PKEY_CTX *ctx, int operation)
373 if (ctx == NULL || ctx->keytype == NULL)
376 evp_pkey_ctx_free_old_ops(ctx);
377 if (ctx->keymgmt == NULL)
380 ctx->operation = operation;
385 ctx->operation = EVP_PKEY_OP_UNDEFINED;
386 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
390 int EVP_PKEY_param_fromdata_init(EVP_PKEY_CTX *ctx)
392 return fromdata_init(ctx, EVP_PKEY_OP_PARAMFROMDATA);
395 int EVP_PKEY_key_fromdata_init(EVP_PKEY_CTX *ctx)
397 return fromdata_init(ctx, EVP_PKEY_OP_KEYFROMDATA);
400 int EVP_PKEY_fromdata(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey, OSSL_PARAM params[])
402 void *keydata = NULL;
405 if (ctx == NULL || (ctx->operation & EVP_PKEY_OP_TYPE_FROMDATA) == 0) {
406 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
414 *ppkey = EVP_PKEY_new();
416 if (*ppkey == NULL) {
417 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
421 if (ctx->operation == EVP_PKEY_OP_PARAMFROMDATA)
422 selection = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
424 selection = OSSL_KEYMGMT_SELECT_ALL;
425 keydata = evp_keymgmt_util_fromdata(*ppkey, ctx->keymgmt, selection,
430 /* keydata is cached in *ppkey, so we need not bother with it further */
435 * TODO(3.0) Re-evaluate the names, it's possible that we find these to be
438 * EVP_PKEY_param_settable()
439 * EVP_PKEY_param_gettable()
441 const OSSL_PARAM *EVP_PKEY_param_fromdata_settable(EVP_PKEY_CTX *ctx)
443 /* We call fromdata_init to get ctx->keymgmt populated */
444 if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED))
445 return evp_keymgmt_import_types(ctx->keymgmt,
446 OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
450 const OSSL_PARAM *EVP_PKEY_key_fromdata_settable(EVP_PKEY_CTX *ctx)
452 /* We call fromdata_init to get ctx->keymgmt populated */
453 if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED))
454 return evp_keymgmt_import_types(ctx->keymgmt,
455 OSSL_KEYMGMT_SELECT_ALL);