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