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