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 <openssl/serializer.h>
31 # include "internal/sizes.h"
34 static int gen_init(EVP_PKEY_CTX
*ctx
, int operation
)
41 evp_pkey_ctx_free_old_ops(ctx
);
42 ctx
->operation
= operation
;
44 if (ctx
->keymgmt
== NULL
|| ctx
->keymgmt
->gen_init
== NULL
)
47 /* TODO remove when provider SM2 key generation is implemented */
49 if (ctx
->pmeth
!= NULL
&& ctx
->pmeth
->pkey_id
== EVP_PKEY_SM2
)
54 case EVP_PKEY_OP_PARAMGEN
:
55 ctx
->op
.keymgmt
.genctx
=
56 evp_keymgmt_gen_init(ctx
->keymgmt
,
57 OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
);
59 case EVP_PKEY_OP_KEYGEN
:
60 ctx
->op
.keymgmt
.genctx
=
61 evp_keymgmt_gen_init(ctx
->keymgmt
, OSSL_KEYMGMT_SELECT_KEYPAIR
);
65 if (ctx
->op
.keymgmt
.genctx
== NULL
)
66 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
75 if (ctx
->pmeth
== NULL
76 || (operation
== EVP_PKEY_OP_PARAMGEN
77 && ctx
->pmeth
->paramgen
== NULL
)
78 || (operation
== EVP_PKEY_OP_KEYGEN
79 && ctx
->pmeth
->keygen
== NULL
))
84 case EVP_PKEY_OP_PARAMGEN
:
85 if (ctx
->pmeth
->paramgen_init
!= NULL
)
86 ret
= ctx
->pmeth
->paramgen_init(ctx
);
88 case EVP_PKEY_OP_KEYGEN
:
89 if (ctx
->pmeth
->keygen_init
!= NULL
)
90 ret
= ctx
->pmeth
->keygen_init(ctx
);
96 if (ret
<= 0 && ctx
!= NULL
) {
97 evp_pkey_ctx_free_old_ops(ctx
);
98 ctx
->operation
= EVP_PKEY_OP_UNDEFINED
;
103 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
108 int EVP_PKEY_paramgen_init(EVP_PKEY_CTX
*ctx
)
110 return gen_init(ctx
, EVP_PKEY_OP_PARAMGEN
);
113 int EVP_PKEY_keygen_init(EVP_PKEY_CTX
*ctx
)
115 return gen_init(ctx
, EVP_PKEY_OP_KEYGEN
);
118 static int ossl_callback_to_pkey_gencb(const OSSL_PARAM params
[], void *arg
)
120 EVP_PKEY_CTX
*ctx
= arg
;
121 const OSSL_PARAM
*param
= NULL
;
124 if (ctx
->pkey_gencb
== NULL
)
125 return 1; /* No callback? That's fine */
127 if ((param
= OSSL_PARAM_locate_const(params
, OSSL_GEN_PARAM_POTENTIAL
))
129 || !OSSL_PARAM_get_int(param
, &p
))
131 if ((param
= OSSL_PARAM_locate_const(params
, OSSL_GEN_PARAM_ITERATION
))
133 || !OSSL_PARAM_get_int(param
, &n
))
136 ctx
->keygen_info
[0] = p
;
137 ctx
->keygen_info
[1] = n
;
139 return ctx
->pkey_gencb(ctx
);
142 int EVP_PKEY_gen(EVP_PKEY_CTX
*ctx
, EVP_PKEY
**ppkey
)
146 EVP_PKEY
*allocated_pkey
= NULL
;
147 /* Legacy compatible keygen callback info, only used with provider impls */
156 if ((ctx
->operation
& EVP_PKEY_OP_TYPE_GEN
) == 0)
157 goto not_initialized
;
160 *ppkey
= allocated_pkey
= EVP_PKEY_new();
162 if (*ppkey
== NULL
) {
163 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
167 if (ctx
->op
.keymgmt
.genctx
== NULL
)
171 * Asssigning gentmp to ctx->keygen_info is something our legacy
172 * implementations do. Because the provider implementations aren't
173 * allowed to reach into our EVP_PKEY_CTX, we need to provide similar
174 * space for backward compatibility. It's ok that we attach a local
175 * variable, as it should only be useful in the calls down from here.
176 * This is cleared as soon as it isn't useful any more, i.e. directly
177 * after the evp_keymgmt_util_gen() call.
179 ctx
->keygen_info
= gentmp
;
180 ctx
->keygen_info_count
= 2;
183 if (ctx
->pkey
!= NULL
) {
184 EVP_KEYMGMT
*tmp_keymgmt
= ctx
->keymgmt
;
186 evp_pkey_export_to_provider(ctx
->pkey
, ctx
->libctx
,
187 &tmp_keymgmt
, ctx
->propquery
);
189 if (tmp_keymgmt
== NULL
)
192 * It's ok if keydata is NULL here. The backend is expected to deal
193 * with that as it sees fit.
195 ret
= evp_keymgmt_gen_set_template(ctx
->keymgmt
,
196 ctx
->op
.keymgmt
.genctx
, keydata
);
200 * the returned value from evp_keymgmt_util_gen() is cached in *ppkey,
201 * so we so not need to save it, just check it.
204 && (evp_keymgmt_util_gen(*ppkey
, ctx
->keymgmt
, ctx
->op
.keymgmt
.genctx
,
205 ossl_callback_to_pkey_gencb
, ctx
)
208 ctx
->keygen_info
= NULL
;
211 /* In case |*ppkey| was originally a legacy key */
213 evp_pkey_free_legacy(*ppkey
);
216 /* TODO remove when SM2 key have been cleanly separated from EC keys */
219 * Legacy SM2 keys are implemented as EC_KEY with a twist. The legacy
220 * key generation detects the SM2 curve and "magically" changes the pkey
222 * Since we don't have SM2 in the provider implementation, we need to
223 * downgrade the generated provider side key to a legacy one under the
226 * THIS IS AN UGLY BUT TEMPORARY HACK
229 char curve_name
[OSSL_MAX_NAME_SIZE
] = "";
231 if (!EVP_PKEY_get_utf8_string_param(*ppkey
, OSSL_PKEY_PARAM_EC_NAME
,
232 curve_name
, sizeof(curve_name
),
234 || strcmp(curve_name
, "SM2") != 0)
238 if (!evp_pkey_downgrade(*ppkey
)
239 || !EVP_PKEY_set_alias_type(*ppkey
, EVP_PKEY_SM2
))
248 if (ctx
->pkey
&& !evp_pkey_downgrade(ctx
->pkey
))
250 switch (ctx
->operation
) {
251 case EVP_PKEY_OP_PARAMGEN
:
252 ret
= ctx
->pmeth
->paramgen(ctx
, *ppkey
);
254 case EVP_PKEY_OP_KEYGEN
:
255 ret
= ctx
->pmeth
->keygen(ctx
, *ppkey
);
264 if (allocated_pkey
!= NULL
)
266 EVP_PKEY_free(allocated_pkey
);
271 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
275 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATON_NOT_INITIALIZED
);
280 ERR_raise(ERR_LIB_EVP
, EVP_R_INACCESSIBLE_DOMAIN_PARAMETERS
);
286 int EVP_PKEY_paramgen(EVP_PKEY_CTX
*ctx
, EVP_PKEY
**ppkey
)
288 if (ctx
->operation
!= EVP_PKEY_OP_PARAMGEN
) {
289 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATON_NOT_INITIALIZED
);
292 return EVP_PKEY_gen(ctx
, ppkey
);
295 int EVP_PKEY_keygen(EVP_PKEY_CTX
*ctx
, EVP_PKEY
**ppkey
)
297 if (ctx
->operation
!= EVP_PKEY_OP_KEYGEN
) {
298 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATON_NOT_INITIALIZED
);
301 return EVP_PKEY_gen(ctx
, ppkey
);
304 void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX
*ctx
, EVP_PKEY_gen_cb
*cb
)
306 ctx
->pkey_gencb
= cb
;
309 EVP_PKEY_gen_cb
*EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX
*ctx
)
311 return ctx
->pkey_gencb
;
315 * "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB style
319 static int trans_cb(int a
, int b
, BN_GENCB
*gcb
)
321 EVP_PKEY_CTX
*ctx
= BN_GENCB_get_arg(gcb
);
322 ctx
->keygen_info
[0] = a
;
323 ctx
->keygen_info
[1] = b
;
324 return ctx
->pkey_gencb(ctx
);
327 void evp_pkey_set_cb_translate(BN_GENCB
*cb
, EVP_PKEY_CTX
*ctx
)
329 BN_GENCB_set(cb
, trans_cb
, ctx
);
332 int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX
*ctx
, int idx
)
335 return ctx
->keygen_info_count
;
336 if (idx
< 0 || idx
> ctx
->keygen_info_count
)
338 return ctx
->keygen_info
[idx
];
343 EVP_PKEY
*EVP_PKEY_new_mac_key(int type
, ENGINE
*e
,
344 const unsigned char *key
, int keylen
)
346 EVP_PKEY_CTX
*mac_ctx
= NULL
;
347 EVP_PKEY
*mac_key
= NULL
;
348 mac_ctx
= EVP_PKEY_CTX_new_id(type
, e
);
351 if (EVP_PKEY_keygen_init(mac_ctx
) <= 0)
353 if (EVP_PKEY_CTX_set_mac_key(mac_ctx
, key
, keylen
) <= 0)
355 if (EVP_PKEY_keygen(mac_ctx
, &mac_key
) <= 0)
358 EVP_PKEY_CTX_free(mac_ctx
);
362 #endif /* FIPS_MODULE */
364 /*- All methods below can also be used in FIPS_MODULE */
366 static int fromdata_init(EVP_PKEY_CTX
*ctx
, int operation
)
368 if (ctx
== NULL
|| ctx
->keytype
== NULL
)
371 evp_pkey_ctx_free_old_ops(ctx
);
372 if (ctx
->keymgmt
== NULL
)
375 ctx
->operation
= operation
;
379 ctx
->operation
= EVP_PKEY_OP_UNDEFINED
;
380 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
384 int EVP_PKEY_param_fromdata_init(EVP_PKEY_CTX
*ctx
)
386 return fromdata_init(ctx
, EVP_PKEY_OP_PARAMFROMDATA
);
389 int EVP_PKEY_key_fromdata_init(EVP_PKEY_CTX
*ctx
)
391 return fromdata_init(ctx
, EVP_PKEY_OP_KEYFROMDATA
);
394 int EVP_PKEY_fromdata(EVP_PKEY_CTX
*ctx
, EVP_PKEY
**ppkey
, OSSL_PARAM params
[])
396 void *keydata
= NULL
;
399 if (ctx
== NULL
|| (ctx
->operation
& EVP_PKEY_OP_TYPE_FROMDATA
) == 0) {
400 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
408 *ppkey
= EVP_PKEY_new();
410 if (*ppkey
== NULL
) {
411 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
415 if (ctx
->operation
== EVP_PKEY_OP_PARAMFROMDATA
)
416 selection
= OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
;
418 selection
= OSSL_KEYMGMT_SELECT_ALL
;
419 keydata
= evp_keymgmt_util_fromdata(*ppkey
, ctx
->keymgmt
, selection
,
424 /* keydata is cached in *ppkey, so we need not bother with it further */
429 * TODO(3.0) Re-evaluate the names, it's possible that we find these to be
432 * EVP_PKEY_param_settable()
433 * EVP_PKEY_param_gettable()
435 const OSSL_PARAM
*EVP_PKEY_param_fromdata_settable(EVP_PKEY_CTX
*ctx
)
437 /* We call fromdata_init to get ctx->keymgmt populated */
438 if (fromdata_init(ctx
, EVP_PKEY_OP_UNDEFINED
))
439 return evp_keymgmt_import_types(ctx
->keymgmt
,
440 OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
);
444 const OSSL_PARAM
*EVP_PKEY_key_fromdata_settable(EVP_PKEY_CTX
*ctx
)
446 /* We call fromdata_init to get ctx->keymgmt populated */
447 if (fromdata_init(ctx
, EVP_PKEY_OP_UNDEFINED
))
448 return evp_keymgmt_import_types(ctx
->keymgmt
,
449 OSSL_KEYMGMT_SELECT_ALL
);