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 switch (ctx->operation) {
184 case EVP_PKEY_OP_PARAMGEN:
185 ret = ctx->pmeth->paramgen(ctx, *ppkey);
187 case EVP_PKEY_OP_KEYGEN:
188 ret = ctx->pmeth->keygen(ctx, *ppkey);
197 if (allocated_pkey != NULL)
199 EVP_PKEY_free(allocated_pkey);
204 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
208 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
213 int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
215 if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {
216 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
219 return EVP_PKEY_gen(ctx, ppkey);
222 int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
224 if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
225 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
228 return EVP_PKEY_gen(ctx, ppkey);
231 void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb)
233 ctx->pkey_gencb = cb;
236 EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx)
238 return ctx->pkey_gencb;
242 * "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB style
246 static int trans_cb(int a, int b, BN_GENCB *gcb)
248 EVP_PKEY_CTX *ctx = BN_GENCB_get_arg(gcb);
249 ctx->keygen_info[0] = a;
250 ctx->keygen_info[1] = b;
251 return ctx->pkey_gencb(ctx);
254 void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx)
256 BN_GENCB_set(cb, trans_cb, ctx);
259 int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx)
262 return ctx->keygen_info_count;
263 if (idx < 0 || idx > ctx->keygen_info_count)
265 return ctx->keygen_info[idx];
270 EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
271 const unsigned char *key, int keylen)
273 EVP_PKEY_CTX *mac_ctx = NULL;
274 EVP_PKEY *mac_key = NULL;
275 mac_ctx = EVP_PKEY_CTX_new_id(type, e);
278 if (EVP_PKEY_keygen_init(mac_ctx) <= 0)
280 if (EVP_PKEY_CTX_set_mac_key(mac_ctx, key, keylen) <= 0)
282 if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0)
285 EVP_PKEY_CTX_free(mac_ctx);
289 #endif /* FIPS_MODE */
291 /*- All methods below can also be used in FIPS_MODE */
293 static int fromdata_init(EVP_PKEY_CTX *ctx, int operation)
295 if (ctx == NULL || ctx->keytype == NULL)
298 evp_pkey_ctx_free_old_ops(ctx);
299 if (ctx->keymgmt == NULL)
302 ctx->operation = operation;
306 ctx->operation = EVP_PKEY_OP_UNDEFINED;
307 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
311 int EVP_PKEY_param_fromdata_init(EVP_PKEY_CTX *ctx)
313 return fromdata_init(ctx, EVP_PKEY_OP_PARAMFROMDATA);
316 int EVP_PKEY_key_fromdata_init(EVP_PKEY_CTX *ctx)
318 return fromdata_init(ctx, EVP_PKEY_OP_KEYFROMDATA);
321 int EVP_PKEY_fromdata(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey, OSSL_PARAM params[])
323 void *keydata = NULL;
326 if (ctx == NULL || (ctx->operation & EVP_PKEY_OP_TYPE_FROMDATA) == 0) {
327 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
335 *ppkey = EVP_PKEY_new();
337 if (*ppkey == NULL) {
338 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
342 if (ctx->operation == EVP_PKEY_OP_PARAMFROMDATA)
343 selection = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
345 selection = OSSL_KEYMGMT_SELECT_ALL;
346 keydata = evp_keymgmt_util_fromdata(*ppkey, ctx->keymgmt, selection,
351 /* keydata is cached in *ppkey, so we need not bother with it further */
356 * TODO(3.0) Re-evaluate the names, it's possible that we find these to be
359 * EVP_PKEY_param_settable()
360 * EVP_PKEY_param_gettable()
362 const OSSL_PARAM *EVP_PKEY_param_fromdata_settable(EVP_PKEY_CTX *ctx)
364 /* We call fromdata_init to get ctx->keymgmt populated */
365 if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED))
366 return evp_keymgmt_import_types(ctx->keymgmt,
367 OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
371 const OSSL_PARAM *EVP_PKEY_key_fromdata_settable(EVP_PKEY_CTX *ctx)
373 /* We call fromdata_init to get ctx->keymgmt populated */
374 if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED))
375 return evp_keymgmt_import_types(ctx->keymgmt,
376 OSSL_KEYMGMT_SELECT_ALL);