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->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);
80 ctx->operation = EVP_PKEY_OP_UNDEFINED;
84 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
89 int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx)
91 return gen_init(ctx, EVP_PKEY_OP_PARAMGEN);
94 int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx)
96 return gen_init(ctx, EVP_PKEY_OP_KEYGEN);
99 static int ossl_callback_to_pkey_gencb(const OSSL_PARAM params[], void *arg)
101 EVP_PKEY_CTX *ctx = arg;
102 const OSSL_PARAM *param = NULL;
105 if (ctx->pkey_gencb == NULL)
106 return 1; /* No callback? That's fine */
108 if ((param = OSSL_PARAM_locate_const(params, OSSL_GEN_PARAM_POTENTIAL))
110 || !OSSL_PARAM_get_int(param, &p))
112 if ((param = OSSL_PARAM_locate_const(params, OSSL_GEN_PARAM_ITERATION))
114 || !OSSL_PARAM_get_int(param, &n))
117 ctx->keygen_info[0] = p;
118 ctx->keygen_info[1] = n;
120 return ctx->pkey_gencb(ctx);
123 int EVP_PKEY_gen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
127 EVP_PKEY *allocated_pkey = NULL;
135 if ((ctx->operation & EVP_PKEY_OP_TYPE_GEN) == 0)
136 goto not_initialized;
139 *ppkey = allocated_pkey = EVP_PKEY_new();
141 if (*ppkey == NULL) {
142 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
146 if (ctx->keymgmt == NULL || ctx->op.keymgmt.genctx == NULL)
150 if (ctx->pkey != NULL) {
151 EVP_KEYMGMT *tmp_keymgmt = ctx->keymgmt;
153 evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
154 &tmp_keymgmt, ctx->propquery);
158 ret = evp_keymgmt_gen_set_template(ctx->keymgmt,
159 ctx->op.keymgmt.genctx, keydata);
163 * the returned value from evp_keymgmt_util_gen() is cached in *ppkey,
164 * so we so not need to save it, just check it.
167 && (evp_keymgmt_util_gen(*ppkey, ctx->keymgmt, ctx->op.keymgmt.genctx,
168 ossl_callback_to_pkey_gencb, ctx)
172 /* In case |*ppkey| was originally a legacy key */
174 evp_pkey_free_legacy(*ppkey);
183 if (ctx->pkey && !evp_pkey_downgrade(ctx->pkey))
185 switch (ctx->operation) {
186 case EVP_PKEY_OP_PARAMGEN:
187 ret = ctx->pmeth->paramgen(ctx, *ppkey);
189 case EVP_PKEY_OP_KEYGEN:
190 ret = ctx->pmeth->keygen(ctx, *ppkey);
199 if (allocated_pkey != NULL)
201 EVP_PKEY_free(allocated_pkey);
206 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
210 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
215 ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_DOMAIN_PARAMETERS);
221 int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
223 if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {
224 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
227 return EVP_PKEY_gen(ctx, ppkey);
230 int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
232 if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
233 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
236 return EVP_PKEY_gen(ctx, ppkey);
239 void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb)
241 ctx->pkey_gencb = cb;
244 EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx)
246 return ctx->pkey_gencb;
250 * "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB style
254 static int trans_cb(int a, int b, BN_GENCB *gcb)
256 EVP_PKEY_CTX *ctx = BN_GENCB_get_arg(gcb);
257 ctx->keygen_info[0] = a;
258 ctx->keygen_info[1] = b;
259 return ctx->pkey_gencb(ctx);
262 void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx)
264 BN_GENCB_set(cb, trans_cb, ctx);
267 int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx)
270 return ctx->keygen_info_count;
271 if (idx < 0 || idx > ctx->keygen_info_count)
273 return ctx->keygen_info[idx];
278 EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
279 const unsigned char *key, int keylen)
281 EVP_PKEY_CTX *mac_ctx = NULL;
282 EVP_PKEY *mac_key = NULL;
283 mac_ctx = EVP_PKEY_CTX_new_id(type, e);
286 if (EVP_PKEY_keygen_init(mac_ctx) <= 0)
288 if (EVP_PKEY_CTX_set_mac_key(mac_ctx, key, keylen) <= 0)
290 if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0)
293 EVP_PKEY_CTX_free(mac_ctx);
297 #endif /* FIPS_MODE */
299 /*- All methods below can also be used in FIPS_MODE */
301 static int fromdata_init(EVP_PKEY_CTX *ctx, int operation)
303 if (ctx == NULL || ctx->keytype == NULL)
306 evp_pkey_ctx_free_old_ops(ctx);
307 if (ctx->keymgmt == NULL)
310 ctx->operation = operation;
314 ctx->operation = EVP_PKEY_OP_UNDEFINED;
315 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
319 int EVP_PKEY_param_fromdata_init(EVP_PKEY_CTX *ctx)
321 return fromdata_init(ctx, EVP_PKEY_OP_PARAMFROMDATA);
324 int EVP_PKEY_key_fromdata_init(EVP_PKEY_CTX *ctx)
326 return fromdata_init(ctx, EVP_PKEY_OP_KEYFROMDATA);
329 int EVP_PKEY_fromdata(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey, OSSL_PARAM params[])
331 void *keydata = NULL;
334 if (ctx == NULL || (ctx->operation & EVP_PKEY_OP_TYPE_FROMDATA) == 0) {
335 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
343 *ppkey = EVP_PKEY_new();
345 if (*ppkey == NULL) {
346 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
350 if (ctx->operation == EVP_PKEY_OP_PARAMFROMDATA)
351 selection = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
353 selection = OSSL_KEYMGMT_SELECT_ALL;
354 keydata = evp_keymgmt_util_fromdata(*ppkey, ctx->keymgmt, selection,
359 /* keydata is cached in *ppkey, so we need not bother with it further */
364 * TODO(3.0) Re-evaluate the names, it's possible that we find these to be
367 * EVP_PKEY_param_settable()
368 * EVP_PKEY_param_gettable()
370 const OSSL_PARAM *EVP_PKEY_param_fromdata_settable(EVP_PKEY_CTX *ctx)
372 /* We call fromdata_init to get ctx->keymgmt populated */
373 if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED))
374 return evp_keymgmt_import_types(ctx->keymgmt,
375 OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
379 const OSSL_PARAM *EVP_PKEY_key_fromdata_settable(EVP_PKEY_CTX *ctx)
381 /* We call fromdata_init to get ctx->keymgmt populated */
382 if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED))
383 return evp_keymgmt_import_types(ctx->keymgmt,
384 OSSL_KEYMGMT_SELECT_ALL);