Make EVP_PKEY_CTX_[get|set]_group_name work for DH too
[openssl.git] / crypto / evp / pmeth_gn.c
1 /*
2  * Copyright 2006-2020 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 #include "crypto/asn1.h"
20 #include "crypto/evp.h"
21 #include "evp_local.h"
22
23 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_EC)
24 # define TMP_SM2_HACK
25 #endif
26
27 /* TODO(3.0) remove when provider SM2 key generation is implemented */
28 #ifdef TMP_SM2_HACK
29 # include <openssl/ec.h>
30 # include <openssl/serializer.h>
31 # include "internal/sizes.h"
32 #endif
33
34 static int gen_init(EVP_PKEY_CTX *ctx, int operation)
35 {
36     int ret = 0;
37
38     if (ctx == NULL)
39         goto not_supported;
40
41     evp_pkey_ctx_free_old_ops(ctx);
42     ctx->operation = operation;
43
44     if (ctx->keymgmt == NULL || ctx->keymgmt->gen_init == NULL)
45         goto legacy;
46
47 /* TODO remove when provider SM2 key generation is implemented */
48 #ifdef TMP_SM2_HACK
49     if (ctx->pmeth != NULL && ctx->pmeth->pkey_id == EVP_PKEY_SM2)
50         goto legacy;
51 #endif
52
53     switch (operation) {
54     case EVP_PKEY_OP_PARAMGEN:
55         ctx->op.keymgmt.genctx =
56             evp_keymgmt_gen_init(ctx->keymgmt,
57                                  OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
58         break;
59     case EVP_PKEY_OP_KEYGEN:
60         ctx->op.keymgmt.genctx =
61             evp_keymgmt_gen_init(ctx->keymgmt, OSSL_KEYMGMT_SELECT_KEYPAIR);
62         break;
63     }
64
65     if (ctx->op.keymgmt.genctx == NULL)
66         ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
67     else
68         ret = 1;
69     goto end;
70
71  legacy:
72 #ifdef FIPS_MODULE
73     goto not_supported;
74 #else
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))
80         goto not_supported;
81
82     ret = 1;
83     switch (operation) {
84     case EVP_PKEY_OP_PARAMGEN:
85         if (ctx->pmeth->paramgen_init != NULL)
86             ret = ctx->pmeth->paramgen_init(ctx);
87         break;
88     case EVP_PKEY_OP_KEYGEN:
89         if (ctx->pmeth->keygen_init != NULL)
90             ret = ctx->pmeth->keygen_init(ctx);
91         break;
92     }
93 #endif
94
95  end:
96     if (ret <= 0 && ctx != NULL) {
97         evp_pkey_ctx_free_old_ops(ctx);
98         ctx->operation = EVP_PKEY_OP_UNDEFINED;
99     }
100     return ret;
101
102  not_supported:
103     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
104     ret = -2;
105     goto end;
106 }
107
108 int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx)
109 {
110     return gen_init(ctx, EVP_PKEY_OP_PARAMGEN);
111 }
112
113 int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx)
114 {
115     return gen_init(ctx, EVP_PKEY_OP_KEYGEN);
116 }
117
118 static int ossl_callback_to_pkey_gencb(const OSSL_PARAM params[], void *arg)
119 {
120     EVP_PKEY_CTX *ctx = arg;
121     const OSSL_PARAM *param = NULL;
122     int p = -1, n = -1;
123
124     if (ctx->pkey_gencb == NULL)
125         return 1;                /* No callback?  That's fine */
126
127     if ((param = OSSL_PARAM_locate_const(params, OSSL_GEN_PARAM_POTENTIAL))
128         == NULL
129         || !OSSL_PARAM_get_int(param, &p))
130         return 0;
131     if ((param = OSSL_PARAM_locate_const(params, OSSL_GEN_PARAM_ITERATION))
132         == NULL
133         || !OSSL_PARAM_get_int(param, &n))
134         return 0;
135
136     ctx->keygen_info[0] = p;
137     ctx->keygen_info[1] = n;
138
139     return ctx->pkey_gencb(ctx);
140 }
141
142 int EVP_PKEY_gen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
143 {
144     int ret = 0;
145     OSSL_CALLBACK cb;
146     EVP_PKEY *allocated_pkey = NULL;
147     /* Legacy compatible keygen callback info, only used with provider impls */
148     int gentmp[2];
149
150     if (ppkey == NULL)
151         return -1;
152
153     if (ctx == NULL)
154         goto not_supported;
155
156     if ((ctx->operation & EVP_PKEY_OP_TYPE_GEN) == 0)
157         goto not_initialized;
158
159     if (*ppkey == NULL)
160         *ppkey = allocated_pkey = EVP_PKEY_new();
161
162     if (*ppkey == NULL) {
163         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
164         return -1;
165     }
166
167     if (ctx->op.keymgmt.genctx == NULL)
168         goto legacy;
169
170     /*
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.
178      */
179     ctx->keygen_info = gentmp;
180     ctx->keygen_info_count = 2;
181
182     ret = 1;
183     if (ctx->pkey != NULL) {
184         EVP_KEYMGMT *tmp_keymgmt = ctx->keymgmt;
185         void *keydata =
186             evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
187                                         &tmp_keymgmt, ctx->propquery);
188
189         if (tmp_keymgmt == NULL)
190             goto not_supported;
191         /*
192          * It's ok if keydata is NULL here.  The backend is expected to deal
193          * with that as it sees fit.
194          */
195         ret = evp_keymgmt_gen_set_template(ctx->keymgmt,
196                                            ctx->op.keymgmt.genctx, keydata);
197     }
198
199     /*
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.
202      */
203     ret = ret
204         && (evp_keymgmt_util_gen(*ppkey, ctx->keymgmt, ctx->op.keymgmt.genctx,
205                                  ossl_callback_to_pkey_gencb, ctx)
206             != NULL);
207
208     ctx->keygen_info = NULL;
209
210 #ifndef FIPS_MODULE
211     /* In case |*ppkey| was originally a legacy key */
212     if (ret)
213         evp_pkey_free_legacy(*ppkey);
214 #endif
215
216 /* TODO remove when SM2 key have been cleanly separated from EC keys */
217 #ifdef TMP_SM2_HACK
218     /*
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
221      * id accordingly.
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
224      * same conditions.
225      *
226      * THIS IS AN UGLY BUT TEMPORARY HACK
227      */
228     {
229         char curve_name[OSSL_MAX_NAME_SIZE] = "";
230
231         if (!EVP_PKEY_get_utf8_string_param(*ppkey, OSSL_PKEY_PARAM_GROUP_NAME,
232                                             curve_name, sizeof(curve_name),
233                                             NULL)
234             || strcmp(curve_name, "SM2") != 0)
235             goto end;
236     }
237
238     if (!evp_pkey_downgrade(*ppkey)
239         || !EVP_PKEY_set_alias_type(*ppkey, EVP_PKEY_SM2))
240         ret = 0;
241 #endif
242     goto end;
243
244  legacy:
245 #ifdef FIPS_MODULE
246     goto not_supported;
247 #else
248     if (ctx->pkey && !evp_pkey_downgrade(ctx->pkey))
249         goto not_accessible;
250     switch (ctx->operation) {
251     case EVP_PKEY_OP_PARAMGEN:
252         ret = ctx->pmeth->paramgen(ctx, *ppkey);
253         break;
254     case EVP_PKEY_OP_KEYGEN:
255         ret = ctx->pmeth->keygen(ctx, *ppkey);
256         break;
257     default:
258         goto not_supported;
259     }
260 #endif
261
262  end:
263     if (ret <= 0) {
264         if (allocated_pkey != NULL)
265             *ppkey = NULL;
266         EVP_PKEY_free(allocated_pkey);
267     }
268     return ret;
269
270  not_supported:
271     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
272     ret = -2;
273     goto end;
274  not_initialized:
275     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
276     ret = -1;
277     goto end;
278 #ifndef FIPS_MODULE
279  not_accessible:
280     ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_DOMAIN_PARAMETERS);
281     ret = -1;
282     goto end;
283 #endif
284 }
285
286 int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
287 {
288     if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {
289         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
290         return -1;
291     }
292     return EVP_PKEY_gen(ctx, ppkey);
293 }
294
295 int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
296 {
297     if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
298         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
299         return -1;
300     }
301     return EVP_PKEY_gen(ctx, ppkey);
302 }
303
304 void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb)
305 {
306     ctx->pkey_gencb = cb;
307 }
308
309 EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx)
310 {
311     return ctx->pkey_gencb;
312 }
313
314 /*
315  * "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB style
316  * callbacks.
317  */
318
319 static int trans_cb(int a, int b, BN_GENCB *gcb)
320 {
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);
325 }
326
327 void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx)
328 {
329     BN_GENCB_set(cb, trans_cb, ctx);
330 }
331
332 int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx)
333 {
334     if (idx == -1)
335         return ctx->keygen_info_count;
336     if (idx < 0 || idx > ctx->keygen_info_count)
337         return 0;
338     return ctx->keygen_info[idx];
339 }
340
341 #ifndef FIPS_MODULE
342
343 EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
344                                const unsigned char *key, int keylen)
345 {
346     EVP_PKEY_CTX *mac_ctx = NULL;
347     EVP_PKEY *mac_key = NULL;
348     mac_ctx = EVP_PKEY_CTX_new_id(type, e);
349     if (!mac_ctx)
350         return NULL;
351     if (EVP_PKEY_keygen_init(mac_ctx) <= 0)
352         goto merr;
353     if (EVP_PKEY_CTX_set_mac_key(mac_ctx, key, keylen) <= 0)
354         goto merr;
355     if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0)
356         goto merr;
357  merr:
358     EVP_PKEY_CTX_free(mac_ctx);
359     return mac_key;
360 }
361
362 #endif /* FIPS_MODULE */
363
364 /*- All methods below can also be used in FIPS_MODULE */
365
366 static int fromdata_init(EVP_PKEY_CTX *ctx, int operation)
367 {
368     if (ctx == NULL || ctx->keytype == NULL)
369         goto not_supported;
370
371     evp_pkey_ctx_free_old_ops(ctx);
372     if (ctx->keymgmt == NULL)
373         goto not_supported;
374
375     ctx->operation = operation;
376     return 1;
377
378  not_supported:
379     ctx->operation = EVP_PKEY_OP_UNDEFINED;
380     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
381     return -2;
382 }
383
384 int EVP_PKEY_param_fromdata_init(EVP_PKEY_CTX *ctx)
385 {
386     return fromdata_init(ctx, EVP_PKEY_OP_PARAMFROMDATA);
387 }
388
389 int EVP_PKEY_key_fromdata_init(EVP_PKEY_CTX *ctx)
390 {
391     return fromdata_init(ctx, EVP_PKEY_OP_KEYFROMDATA);
392 }
393
394 int EVP_PKEY_fromdata(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey, OSSL_PARAM params[])
395 {
396     void *keydata = NULL;
397     int selection;
398
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);
401         return -2;
402     }
403
404     if (ppkey == NULL)
405         return -1;
406
407     if (*ppkey == NULL)
408         *ppkey = EVP_PKEY_new();
409
410     if (*ppkey == NULL) {
411         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
412         return -1;
413     }
414
415     if (ctx->operation == EVP_PKEY_OP_PARAMFROMDATA)
416         selection = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
417     else
418         selection = OSSL_KEYMGMT_SELECT_ALL;
419     keydata = evp_keymgmt_util_fromdata(*ppkey, ctx->keymgmt, selection,
420                                         params);
421
422     if (keydata == NULL)
423         return 0;
424     /* keydata is cached in *ppkey, so we need not bother with it further */
425     return 1;
426 }
427
428 /*
429  * TODO(3.0) Re-evaluate the names, it's possible that we find these to be
430  * better:
431  *
432  * EVP_PKEY_param_settable()
433  * EVP_PKEY_param_gettable()
434  */
435 const OSSL_PARAM *EVP_PKEY_param_fromdata_settable(EVP_PKEY_CTX *ctx)
436 {
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);
441     return NULL;
442 }
443
444 const OSSL_PARAM *EVP_PKEY_key_fromdata_settable(EVP_PKEY_CTX *ctx)
445 {
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);
450     return NULL;
451 }