2 * Copyright 2006-2016 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->engine != NULL || ctx->keytype == NULL)
36 if (ctx->keymgmt == NULL) {
38 EVP_KEYMGMT_fetch(ctx->libctx, ctx->keytype, ctx->propquery);
39 if (ctx->keymgmt == NULL
40 || ctx->keymgmt->gen_init == NULL) {
41 EVP_KEYMGMT_free(ctx->keymgmt);
46 if (ctx->keymgmt->gen_init == NULL)
50 case EVP_PKEY_OP_PARAMGEN:
51 ctx->op.keymgmt.genctx =
52 evp_keymgmt_gen_init(ctx->keymgmt,
53 OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
55 case EVP_PKEY_OP_KEYGEN:
56 ctx->op.keymgmt.genctx =
57 evp_keymgmt_gen_init(ctx->keymgmt, OSSL_KEYMGMT_SELECT_KEYPAIR);
61 if (ctx->op.keymgmt.genctx == NULL)
62 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
71 if (ctx->pmeth == NULL
72 || (operation == EVP_PKEY_OP_PARAMGEN
73 && ctx->pmeth->paramgen == NULL)
74 || (operation == EVP_PKEY_OP_KEYGEN
75 && ctx->pmeth->keygen == NULL))
80 case EVP_PKEY_OP_PARAMGEN:
81 if (ctx->pmeth->paramgen_init != NULL)
82 ret = ctx->pmeth->paramgen_init(ctx);
84 case EVP_PKEY_OP_KEYGEN:
85 if (ctx->pmeth->keygen_init != NULL)
86 ret = ctx->pmeth->keygen_init(ctx);
93 ctx->operation = EVP_PKEY_OP_UNDEFINED;
97 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
102 int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx)
104 return gen_init(ctx, EVP_PKEY_OP_PARAMGEN);
107 int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx)
109 return gen_init(ctx, EVP_PKEY_OP_KEYGEN);
112 static int ossl_callback_to_pkey_gencb(const OSSL_PARAM params[], void *arg)
114 EVP_PKEY_CTX *ctx = arg;
115 const OSSL_PARAM *param = NULL;
118 if (ctx->pkey_gencb == NULL)
119 return 1; /* No callback? That's fine */
121 if ((param = OSSL_PARAM_locate_const(params, OSSL_GEN_PARAM_POTENTIAL))
123 || !OSSL_PARAM_get_int(param, &p))
125 if ((param = OSSL_PARAM_locate_const(params, OSSL_GEN_PARAM_ITERATION))
127 || !OSSL_PARAM_get_int(param, &n))
130 ctx->keygen_info[0] = p;
131 ctx->keygen_info[1] = n;
133 return ctx->pkey_gencb(ctx);
136 int EVP_PKEY_gen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
140 EVP_PKEY *allocated_pkey = NULL;
148 if ((ctx->operation & EVP_PKEY_OP_TYPE_GEN) == 0)
149 goto not_initialized;
152 *ppkey = allocated_pkey = EVP_PKEY_new();
154 if (*ppkey == NULL) {
155 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
159 if (ctx->keymgmt == NULL)
163 if (ctx->pkey != NULL) {
164 EVP_KEYMGMT *tmp_keymgmt = ctx->keymgmt;
166 evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
167 &tmp_keymgmt, ctx->propquery);
171 ret = evp_keymgmt_gen_set_template(ctx->keymgmt,
172 ctx->op.keymgmt.genctx, keydata);
176 * the returned value from evp_keymgmt_util_gen() is cached in *ppkey,
177 * so we so not need to save it, just check it.
180 && (evp_keymgmt_util_gen(*ppkey, ctx->keymgmt, ctx->op.keymgmt.genctx,
181 ossl_callback_to_pkey_gencb, ctx)
185 /* In case |*ppkey| was originally a legacy key */
187 evp_pkey_free_legacy(*ppkey);
196 switch (ctx->operation) {
197 case EVP_PKEY_OP_PARAMGEN:
198 ret = ctx->pmeth->paramgen(ctx, *ppkey);
200 case EVP_PKEY_OP_KEYGEN:
201 ret = ctx->pmeth->keygen(ctx, *ppkey);
210 if (allocated_pkey != NULL)
212 EVP_PKEY_free(allocated_pkey);
217 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
221 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
226 int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
228 if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {
229 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
232 return EVP_PKEY_gen(ctx, ppkey);
235 int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
237 if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
238 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
241 return EVP_PKEY_gen(ctx, ppkey);
244 void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb)
246 ctx->pkey_gencb = cb;
249 EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx)
251 return ctx->pkey_gencb;
255 * "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB style
259 static int trans_cb(int a, int b, BN_GENCB *gcb)
261 EVP_PKEY_CTX *ctx = BN_GENCB_get_arg(gcb);
262 ctx->keygen_info[0] = a;
263 ctx->keygen_info[1] = b;
264 return ctx->pkey_gencb(ctx);
267 void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx)
269 BN_GENCB_set(cb, trans_cb, ctx);
272 int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx)
275 return ctx->keygen_info_count;
276 if (idx < 0 || idx > ctx->keygen_info_count)
278 return ctx->keygen_info[idx];
283 EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
284 const unsigned char *key, int keylen)
286 EVP_PKEY_CTX *mac_ctx = NULL;
287 EVP_PKEY *mac_key = NULL;
288 mac_ctx = EVP_PKEY_CTX_new_id(type, e);
291 if (EVP_PKEY_keygen_init(mac_ctx) <= 0)
293 if (EVP_PKEY_CTX_set_mac_key(mac_ctx, key, keylen) <= 0)
295 if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0)
298 EVP_PKEY_CTX_free(mac_ctx);
302 #endif /* FIPS_MODE */
304 /*- All methods below can also be used in FIPS_MODE */
306 static int fromdata_init(EVP_PKEY_CTX *ctx, int operation)
308 if (ctx == NULL || ctx->keytype == NULL)
311 evp_pkey_ctx_free_old_ops(ctx);
312 ctx->operation = operation;
313 if (ctx->keymgmt == NULL)
314 ctx->keymgmt = EVP_KEYMGMT_fetch(ctx->libctx, ctx->keytype,
316 if (ctx->keymgmt == NULL)
322 ctx->operation = EVP_PKEY_OP_UNDEFINED;
323 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
327 int EVP_PKEY_param_fromdata_init(EVP_PKEY_CTX *ctx)
329 return fromdata_init(ctx, EVP_PKEY_OP_PARAMFROMDATA);
332 int EVP_PKEY_key_fromdata_init(EVP_PKEY_CTX *ctx)
334 return fromdata_init(ctx, EVP_PKEY_OP_KEYFROMDATA);
337 int EVP_PKEY_fromdata(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey, OSSL_PARAM params[])
339 void *keydata = NULL;
342 if (ctx == NULL || (ctx->operation & EVP_PKEY_OP_TYPE_FROMDATA) == 0) {
343 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
351 *ppkey = EVP_PKEY_new();
353 if (*ppkey == NULL) {
354 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
358 if (ctx->operation == EVP_PKEY_OP_PARAMFROMDATA)
359 selection = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
361 selection = OSSL_KEYMGMT_SELECT_ALL;
362 keydata = evp_keymgmt_util_fromdata(*ppkey, ctx->keymgmt, selection,
367 /* keydata is cached in *ppkey, so we need not bother with it further */
372 * TODO(3.0) Re-evaluate the names, it's possible that we find these to be
375 * EVP_PKEY_param_settable()
376 * EVP_PKEY_param_gettable()
378 const OSSL_PARAM *EVP_PKEY_param_fromdata_settable(EVP_PKEY_CTX *ctx)
380 /* We call fromdata_init to get ctx->keymgmt populated */
381 if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED))
382 return evp_keymgmt_import_types(ctx->keymgmt,
383 OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
387 const OSSL_PARAM *EVP_PKEY_key_fromdata_settable(EVP_PKEY_CTX *ctx)
389 /* We call fromdata_init to get ctx->keymgmt populated */
390 if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED))
391 return evp_keymgmt_import_types(ctx->keymgmt,
392 OSSL_KEYMGMT_SELECT_ALL);