]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/pmeth_gn.c
Update copyright year
[thirdparty/openssl.git] / crypto / evp / pmeth_gn.c
CommitLineData
0f113f3e 1/*
fecb3aae 2 * Copyright 2006-2022 The OpenSSL Project Authors. All Rights Reserved.
f5cda4cb 3 *
4a8b0c55 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
62867571
RS
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
f5cda4cb
DSH
8 */
9
10#include <stdio.h>
11#include <stdlib.h>
62924755
RL
12#include <openssl/core.h>
13#include <openssl/core_names.h>
b39fc560 14#include "internal/cryptlib.h"
62924755 15#include "internal/core.h"
c20276e4 16#include <openssl/objects.h>
f5cda4cb 17#include <openssl/evp.h>
25f2138b 18#include "crypto/bn.h"
3f773c91
TM
19#ifndef FIPS_MODULE
20# include "crypto/asn1.h"
21#endif
25f2138b 22#include "crypto/evp.h"
46e2dd05
RL
23#include "evp_local.h"
24
62924755 25static int gen_init(EVP_PKEY_CTX *ctx, int operation)
0f113f3e 26{
62924755
RL
27 int ret = 0;
28
29 if (ctx == NULL)
30 goto not_supported;
31
32 evp_pkey_ctx_free_old_ops(ctx);
33 ctx->operation = operation;
34
4b9e90f4 35 if (ctx->keymgmt == NULL || ctx->keymgmt->gen_init == NULL)
62924755
RL
36 goto legacy;
37
62924755
RL
38 switch (operation) {
39 case EVP_PKEY_OP_PARAMGEN:
40 ctx->op.keymgmt.genctx =
41 evp_keymgmt_gen_init(ctx->keymgmt,
1be63b3e 42 OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, NULL);
62924755
RL
43 break;
44 case EVP_PKEY_OP_KEYGEN:
45 ctx->op.keymgmt.genctx =
1be63b3e
P
46 evp_keymgmt_gen_init(ctx->keymgmt, OSSL_KEYMGMT_SELECT_KEYPAIR,
47 NULL);
62924755
RL
48 break;
49 }
50
51 if (ctx->op.keymgmt.genctx == NULL)
52 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
53 else
54 ret = 1;
55 goto end;
56
57 legacy:
f844f9eb 58#ifdef FIPS_MODULE
62924755
RL
59 goto not_supported;
60#else
61 if (ctx->pmeth == NULL
62 || (operation == EVP_PKEY_OP_PARAMGEN
63 && ctx->pmeth->paramgen == NULL)
64 || (operation == EVP_PKEY_OP_KEYGEN
65 && ctx->pmeth->keygen == NULL))
66 goto not_supported;
67
68 ret = 1;
69 switch (operation) {
70 case EVP_PKEY_OP_PARAMGEN:
71 if (ctx->pmeth->paramgen_init != NULL)
72 ret = ctx->pmeth->paramgen_init(ctx);
73 break;
74 case EVP_PKEY_OP_KEYGEN:
75 if (ctx->pmeth->keygen_init != NULL)
76 ret = ctx->pmeth->keygen_init(ctx);
77 break;
0f113f3e 78 }
62924755
RL
79#endif
80
81 end:
c7fa9297
RL
82 if (ret <= 0 && ctx != NULL) {
83 evp_pkey_ctx_free_old_ops(ctx);
0f113f3e 84 ctx->operation = EVP_PKEY_OP_UNDEFINED;
c7fa9297 85 }
0f113f3e 86 return ret;
62924755
RL
87
88 not_supported:
89 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
90 ret = -2;
91 goto end;
0f113f3e 92}
f5cda4cb 93
62924755 94int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx)
0f113f3e 95{
62924755
RL
96 return gen_init(ctx, EVP_PKEY_OP_PARAMGEN);
97}
0f113f3e 98
62924755
RL
99int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx)
100{
101 return gen_init(ctx, EVP_PKEY_OP_KEYGEN);
102}
103
104static int ossl_callback_to_pkey_gencb(const OSSL_PARAM params[], void *arg)
105{
106 EVP_PKEY_CTX *ctx = arg;
107 const OSSL_PARAM *param = NULL;
108 int p = -1, n = -1;
109
110 if (ctx->pkey_gencb == NULL)
111 return 1; /* No callback? That's fine */
112
113 if ((param = OSSL_PARAM_locate_const(params, OSSL_GEN_PARAM_POTENTIAL))
114 == NULL
115 || !OSSL_PARAM_get_int(param, &p))
116 return 0;
117 if ((param = OSSL_PARAM_locate_const(params, OSSL_GEN_PARAM_ITERATION))
118 == NULL
119 || !OSSL_PARAM_get_int(param, &n))
120 return 0;
121
122 ctx->keygen_info[0] = p;
123 ctx->keygen_info[1] = n;
124
125 return ctx->pkey_gencb(ctx);
126}
127
f9253152 128int EVP_PKEY_generate(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
62924755
RL
129{
130 int ret = 0;
62924755 131 EVP_PKEY *allocated_pkey = NULL;
4ec1463d
RL
132 /* Legacy compatible keygen callback info, only used with provider impls */
133 int gentmp[2];
0f113f3e 134
e34c66c6 135 if (ppkey == NULL)
0f113f3e
MC
136 return -1;
137
62924755
RL
138 if (ctx == NULL)
139 goto not_supported;
140
141 if ((ctx->operation & EVP_PKEY_OP_TYPE_GEN) == 0)
142 goto not_initialized;
143
e34c66c6 144 if (*ppkey == NULL)
62924755 145 *ppkey = allocated_pkey = EVP_PKEY_new();
0f113f3e 146
e34c66c6 147 if (*ppkey == NULL) {
62924755 148 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
e34c66c6
EK
149 return -1;
150 }
151
813d3171 152 if (ctx->op.keymgmt.genctx == NULL)
62924755
RL
153 goto legacy;
154
4ec1463d
RL
155 /*
156 * Asssigning gentmp to ctx->keygen_info is something our legacy
157 * implementations do. Because the provider implementations aren't
158 * allowed to reach into our EVP_PKEY_CTX, we need to provide similar
159 * space for backward compatibility. It's ok that we attach a local
160 * variable, as it should only be useful in the calls down from here.
161 * This is cleared as soon as it isn't useful any more, i.e. directly
162 * after the evp_keymgmt_util_gen() call.
163 */
164 ctx->keygen_info = gentmp;
165 ctx->keygen_info_count = 2;
166
62924755
RL
167 ret = 1;
168 if (ctx->pkey != NULL) {
169 EVP_KEYMGMT *tmp_keymgmt = ctx->keymgmt;
170 void *keydata =
171 evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
172 &tmp_keymgmt, ctx->propquery);
173
d0ddf9b4 174 if (tmp_keymgmt == NULL)
62924755 175 goto not_supported;
d0ddf9b4
RL
176 /*
177 * It's ok if keydata is NULL here. The backend is expected to deal
178 * with that as it sees fit.
179 */
62924755
RL
180 ret = evp_keymgmt_gen_set_template(ctx->keymgmt,
181 ctx->op.keymgmt.genctx, keydata);
182 }
183
184 /*
185 * the returned value from evp_keymgmt_util_gen() is cached in *ppkey,
d1ca3911 186 * so we do not need to save it, just check it.
62924755
RL
187 */
188 ret = ret
189 && (evp_keymgmt_util_gen(*ppkey, ctx->keymgmt, ctx->op.keymgmt.genctx,
190 ossl_callback_to_pkey_gencb, ctx)
191 != NULL);
192
4ec1463d
RL
193 ctx->keygen_info = NULL;
194
f844f9eb 195#ifndef FIPS_MODULE
62924755
RL
196 /* In case |*ppkey| was originally a legacy key */
197 if (ret)
198 evp_pkey_free_legacy(*ppkey);
199#endif
200
50914496 201 /*
b574c6a9 202 * Because we still have legacy keys
50914496
RL
203 */
204 (*ppkey)->type = ctx->legacy_keytype;
205
62924755
RL
206 goto end;
207
208 legacy:
f844f9eb 209#ifdef FIPS_MODULE
62924755
RL
210 goto not_supported;
211#else
b574c6a9
MC
212 /*
213 * If we get here then we're using legacy paramgen/keygen. In that case
214 * the pkey in ctx (if there is one) had better not be provided (because the
215 * legacy methods may not know how to handle it). However we can only get
216 * here if ctx->op.keymgmt.genctx == NULL, but that should never be the case
217 * if ctx->pkey is provided because we don't allow this when we initialise
218 * the ctx.
219 */
220 if (ctx->pkey != NULL && !ossl_assert(!evp_pkey_is_provided(ctx->pkey)))
acb90ba8 221 goto not_accessible;
b574c6a9 222
62924755
RL
223 switch (ctx->operation) {
224 case EVP_PKEY_OP_PARAMGEN:
225 ret = ctx->pmeth->paramgen(ctx, *ppkey);
226 break;
227 case EVP_PKEY_OP_KEYGEN:
228 ret = ctx->pmeth->keygen(ctx, *ppkey);
229 break;
230 default:
231 goto not_supported;
232 }
233#endif
234
235 end:
0f113f3e 236 if (ret <= 0) {
62924755
RL
237 if (allocated_pkey != NULL)
238 *ppkey = NULL;
239 EVP_PKEY_free(allocated_pkey);
0f113f3e
MC
240 }
241 return ret;
62924755
RL
242
243 not_supported:
244 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
245 ret = -2;
246 goto end;
247 not_initialized:
bf23b9a1 248 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
62924755
RL
249 ret = -1;
250 goto end;
f844f9eb 251#ifndef FIPS_MODULE
acb90ba8
RL
252 not_accessible:
253 ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_DOMAIN_PARAMETERS);
254 ret = -1;
255 goto end;
256#endif
0f113f3e 257}
f5cda4cb 258
62924755 259int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
0f113f3e 260{
62924755 261 if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {
bf23b9a1 262 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
62924755 263 return -1;
0f113f3e 264 }
f9253152 265 return EVP_PKEY_generate(ctx, ppkey);
0f113f3e 266}
f5cda4cb
DSH
267
268int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
0f113f3e 269{
0f113f3e 270 if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
bf23b9a1 271 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
0f113f3e
MC
272 return -1;
273 }
f9253152 274 return EVP_PKEY_generate(ctx, ppkey);
0f113f3e 275}
f5cda4cb
DSH
276
277void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb)
0f113f3e
MC
278{
279 ctx->pkey_gencb = cb;
280}
f5cda4cb 281
b28dea4e 282EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx)
0f113f3e
MC
283{
284 return ctx->pkey_gencb;
285}
b28dea4e 286
0f113f3e
MC
287/*
288 * "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB style
289 * callbacks.
f5cda4cb
DSH
290 */
291
292static int trans_cb(int a, int b, BN_GENCB *gcb)
0f113f3e
MC
293{
294 EVP_PKEY_CTX *ctx = BN_GENCB_get_arg(gcb);
295 ctx->keygen_info[0] = a;
296 ctx->keygen_info[1] = b;
297 return ctx->pkey_gencb(ctx);
298}
f5cda4cb
DSH
299
300void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx)
0f113f3e
MC
301{
302 BN_GENCB_set(cb, trans_cb, ctx);
303}
f5cda4cb
DSH
304
305int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx)
0f113f3e
MC
306{
307 if (idx == -1)
308 return ctx->keygen_info_count;
309 if (idx < 0 || idx > ctx->keygen_info_count)
310 return 0;
311 return ctx->keygen_info[idx];
312}
2022cfe0 313
f844f9eb 314#ifndef FIPS_MODULE
62924755 315
2022cfe0 316EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
0f113f3e
MC
317 const unsigned char *key, int keylen)
318{
319 EVP_PKEY_CTX *mac_ctx = NULL;
320 EVP_PKEY *mac_key = NULL;
321 mac_ctx = EVP_PKEY_CTX_new_id(type, e);
322 if (!mac_ctx)
323 return NULL;
324 if (EVP_PKEY_keygen_init(mac_ctx) <= 0)
325 goto merr;
eff1a4d2 326 if (EVP_PKEY_CTX_set_mac_key(mac_ctx, key, keylen) <= 0)
0f113f3e
MC
327 goto merr;
328 if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0)
329 goto merr;
330 merr:
c5ba2d99 331 EVP_PKEY_CTX_free(mac_ctx);
0f113f3e
MC
332 return mac_key;
333}
2aee35d3 334
f844f9eb 335#endif /* FIPS_MODULE */
e683582b 336
f844f9eb 337/*- All methods below can also be used in FIPS_MODULE */
e683582b
SL
338
339static int fromdata_init(EVP_PKEY_CTX *ctx, int operation)
340{
341 if (ctx == NULL || ctx->keytype == NULL)
342 goto not_supported;
343
344 evp_pkey_ctx_free_old_ops(ctx);
e683582b
SL
345 if (ctx->keymgmt == NULL)
346 goto not_supported;
347
4b9e90f4 348 ctx->operation = operation;
e683582b
SL
349 return 1;
350
351 not_supported:
10ead938
SL
352 if (ctx != NULL)
353 ctx->operation = EVP_PKEY_OP_UNDEFINED;
e683582b
SL
354 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
355 return -2;
356}
357
2db985b7 358int EVP_PKEY_fromdata_init(EVP_PKEY_CTX *ctx)
e683582b 359{
2db985b7 360 return fromdata_init(ctx, EVP_PKEY_OP_FROMDATA);
e683582b
SL
361}
362
2db985b7
SL
363int EVP_PKEY_fromdata(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey, int selection,
364 OSSL_PARAM params[])
e683582b 365{
b305452f 366 void *keydata = NULL;
5b03b89f 367 EVP_PKEY *allocated_pkey = NULL;
e683582b 368
2db985b7 369 if (ctx == NULL || (ctx->operation & EVP_PKEY_OP_FROMDATA) == 0) {
e683582b
SL
370 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
371 return -2;
372 }
373
374 if (ppkey == NULL)
375 return -1;
376
377 if (*ppkey == NULL)
5b03b89f 378 allocated_pkey = *ppkey = EVP_PKEY_new();
e683582b
SL
379
380 if (*ppkey == NULL) {
381 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
382 return -1;
383 }
384
2db985b7 385 keydata = evp_keymgmt_util_fromdata(*ppkey, ctx->keymgmt, selection, params);
5b03b89f
TM
386 if (keydata == NULL) {
387 if (allocated_pkey != NULL) {
388 *ppkey = NULL;
389 EVP_PKEY_free(allocated_pkey);
390 }
e683582b 391 return 0;
5b03b89f 392 }
b305452f 393 /* keydata is cached in *ppkey, so we need not bother with it further */
e683582b
SL
394 return 1;
395}
396
2db985b7 397const OSSL_PARAM *EVP_PKEY_fromdata_settable(EVP_PKEY_CTX *ctx, int selection)
e683582b
SL
398{
399 /* We call fromdata_init to get ctx->keymgmt populated */
2db985b7
SL
400 if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED) == 1)
401 return evp_keymgmt_import_types(ctx->keymgmt, selection);
e683582b
SL
402 return NULL;
403}
a732a4c3
SL
404
405static OSSL_CALLBACK ossl_pkey_todata_cb;
406
407static int ossl_pkey_todata_cb(const OSSL_PARAM params[], void *arg)
408{
409 OSSL_PARAM **ret = arg;
410
411 *ret = OSSL_PARAM_dup(params);
412 return 1;
413}
414
415int EVP_PKEY_todata(const EVP_PKEY *pkey, int selection, OSSL_PARAM **params)
416{
417 if (params == NULL)
418 return 0;
419 return EVP_PKEY_export(pkey, selection, ossl_pkey_todata_cb, params);
420}
421
f33c04b8
RL
422#ifndef FIPS_MODULE
423struct fake_import_data_st {
424 OSSL_CALLBACK *export_cb;
425 void *export_cbarg;
426};
427
428static OSSL_FUNC_keymgmt_import_fn pkey_fake_import;
429static int pkey_fake_import(void *fake_keydata, int ignored_selection,
430 const OSSL_PARAM params[])
431{
432 struct fake_import_data_st *data = fake_keydata;
433
434 return data->export_cb(params, data->export_cbarg);
435}
436#endif
437
a732a4c3
SL
438int EVP_PKEY_export(const EVP_PKEY *pkey, int selection,
439 OSSL_CALLBACK *export_cb, void *export_cbarg)
440{
f33c04b8
RL
441 if (pkey == NULL) {
442 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
443 return 0;
444 }
445#ifndef FIPS_MODULE
446 if (evp_pkey_is_legacy(pkey)) {
447 struct fake_import_data_st data;
448
449 data.export_cb = export_cb;
450 data.export_cbarg = export_cbarg;
451
452 /*
453 * We don't need to care about libctx or propq here, as we're only
454 * interested in the resulting OSSL_PARAM array.
455 */
456 return pkey->ameth->export_to(pkey, &data, pkey_fake_import,
457 NULL, NULL);
458 }
459#endif
a732a4c3
SL
460 return evp_keymgmt_util_export(pkey, selection, export_cb, export_cbarg);
461}