TLSv1.3: additional checks in SSL_set_record_padding_callback
[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
148     if (ppkey == NULL)
149         return -1;
150
151     if (ctx == NULL)
152         goto not_supported;
153
154     if ((ctx->operation & EVP_PKEY_OP_TYPE_GEN) == 0)
155         goto not_initialized;
156
157     if (*ppkey == NULL)
158         *ppkey = allocated_pkey = EVP_PKEY_new();
159
160     if (*ppkey == NULL) {
161         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
162         return -1;
163     }
164
165     if (ctx->op.keymgmt.genctx == NULL)
166         goto legacy;
167
168     ret = 1;
169     if (ctx->pkey != NULL) {
170         EVP_KEYMGMT *tmp_keymgmt = ctx->keymgmt;
171         void *keydata =
172             evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
173                                         &tmp_keymgmt, ctx->propquery);
174
175         if (tmp_keymgmt == NULL)
176             goto not_supported;
177         /*
178          * It's ok if keydata is NULL here.  The backend is expected to deal
179          * with that as it sees fit.
180          */
181         ret = evp_keymgmt_gen_set_template(ctx->keymgmt,
182                                            ctx->op.keymgmt.genctx, keydata);
183     }
184
185     /*
186      * the returned value from evp_keymgmt_util_gen() is cached in *ppkey,
187      * so we so not need to save it, just check it.
188      */
189     ret = ret
190         && (evp_keymgmt_util_gen(*ppkey, ctx->keymgmt, ctx->op.keymgmt.genctx,
191                                  ossl_callback_to_pkey_gencb, ctx)
192             != NULL);
193
194 #ifndef FIPS_MODULE
195     /* In case |*ppkey| was originally a legacy key */
196     if (ret)
197         evp_pkey_free_legacy(*ppkey);
198 #endif
199
200 /* TODO remove when SM2 key have been cleanly separated from EC keys */
201 #ifdef TMP_SM2_HACK
202     /*
203      * Legacy SM2 keys are implemented as EC_KEY with a twist.  The legacy
204      * key generation detects the SM2 curve and "magically" changes the pkey
205      * id accordingly.
206      * Since we don't have SM2 in the provider implementation, we need to
207      * downgrade the generated provider side key to a legacy one under the
208      * same conditions.
209      *
210      * THIS IS AN UGLY BUT TEMPORARY HACK
211      */
212     {
213         char curve_name[OSSL_MAX_NAME_SIZE] = "";
214
215         if (!EVP_PKEY_get_utf8_string_param(*ppkey, OSSL_PKEY_PARAM_EC_NAME,
216                                             curve_name, sizeof(curve_name),
217                                             NULL)
218             || strcmp(curve_name, "SM2") != 0)
219             goto end;
220     }
221
222     if (!evp_pkey_downgrade(*ppkey)
223         || !EVP_PKEY_set_alias_type(*ppkey, EVP_PKEY_SM2))
224         ret = 0;
225 #endif
226     goto end;
227
228  legacy:
229 #ifdef FIPS_MODULE
230     goto not_supported;
231 #else
232     if (ctx->pkey && !evp_pkey_downgrade(ctx->pkey))
233         goto not_accessible;
234     switch (ctx->operation) {
235     case EVP_PKEY_OP_PARAMGEN:
236         ret = ctx->pmeth->paramgen(ctx, *ppkey);
237         break;
238     case EVP_PKEY_OP_KEYGEN:
239         ret = ctx->pmeth->keygen(ctx, *ppkey);
240         break;
241     default:
242         goto not_supported;
243     }
244 #endif
245
246  end:
247     if (ret <= 0) {
248         if (allocated_pkey != NULL)
249             *ppkey = NULL;
250         EVP_PKEY_free(allocated_pkey);
251     }
252     return ret;
253
254  not_supported:
255     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
256     ret = -2;
257     goto end;
258  not_initialized:
259     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
260     ret = -1;
261     goto end;
262 #ifndef FIPS_MODULE
263  not_accessible:
264     ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_DOMAIN_PARAMETERS);
265     ret = -1;
266     goto end;
267 #endif
268 }
269
270 int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
271 {
272     if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {
273         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
274         return -1;
275     }
276     return EVP_PKEY_gen(ctx, ppkey);
277 }
278
279 int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
280 {
281     if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
282         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
283         return -1;
284     }
285     return EVP_PKEY_gen(ctx, ppkey);
286 }
287
288 void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb)
289 {
290     ctx->pkey_gencb = cb;
291 }
292
293 EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx)
294 {
295     return ctx->pkey_gencb;
296 }
297
298 /*
299  * "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB style
300  * callbacks.
301  */
302
303 static int trans_cb(int a, int b, BN_GENCB *gcb)
304 {
305     EVP_PKEY_CTX *ctx = BN_GENCB_get_arg(gcb);
306     ctx->keygen_info[0] = a;
307     ctx->keygen_info[1] = b;
308     return ctx->pkey_gencb(ctx);
309 }
310
311 void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx)
312 {
313     BN_GENCB_set(cb, trans_cb, ctx);
314 }
315
316 int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx)
317 {
318     if (idx == -1)
319         return ctx->keygen_info_count;
320     if (idx < 0 || idx > ctx->keygen_info_count)
321         return 0;
322     return ctx->keygen_info[idx];
323 }
324
325 #ifndef FIPS_MODULE
326
327 EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
328                                const unsigned char *key, int keylen)
329 {
330     EVP_PKEY_CTX *mac_ctx = NULL;
331     EVP_PKEY *mac_key = NULL;
332     mac_ctx = EVP_PKEY_CTX_new_id(type, e);
333     if (!mac_ctx)
334         return NULL;
335     if (EVP_PKEY_keygen_init(mac_ctx) <= 0)
336         goto merr;
337     if (EVP_PKEY_CTX_set_mac_key(mac_ctx, key, keylen) <= 0)
338         goto merr;
339     if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0)
340         goto merr;
341  merr:
342     EVP_PKEY_CTX_free(mac_ctx);
343     return mac_key;
344 }
345
346 #endif /* FIPS_MODULE */
347
348 /*- All methods below can also be used in FIPS_MODULE */
349
350 static int fromdata_init(EVP_PKEY_CTX *ctx, int operation)
351 {
352     if (ctx == NULL || ctx->keytype == NULL)
353         goto not_supported;
354
355     evp_pkey_ctx_free_old_ops(ctx);
356     if (ctx->keymgmt == NULL)
357         goto not_supported;
358
359     ctx->operation = operation;
360     return 1;
361
362  not_supported:
363     ctx->operation = EVP_PKEY_OP_UNDEFINED;
364     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
365     return -2;
366 }
367
368 int EVP_PKEY_param_fromdata_init(EVP_PKEY_CTX *ctx)
369 {
370     return fromdata_init(ctx, EVP_PKEY_OP_PARAMFROMDATA);
371 }
372
373 int EVP_PKEY_key_fromdata_init(EVP_PKEY_CTX *ctx)
374 {
375     return fromdata_init(ctx, EVP_PKEY_OP_KEYFROMDATA);
376 }
377
378 int EVP_PKEY_fromdata(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey, OSSL_PARAM params[])
379 {
380     void *keydata = NULL;
381     int selection;
382
383     if (ctx == NULL || (ctx->operation & EVP_PKEY_OP_TYPE_FROMDATA) == 0) {
384         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
385         return -2;
386     }
387
388     if (ppkey == NULL)
389         return -1;
390
391     if (*ppkey == NULL)
392         *ppkey = EVP_PKEY_new();
393
394     if (*ppkey == NULL) {
395         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
396         return -1;
397     }
398
399     if (ctx->operation == EVP_PKEY_OP_PARAMFROMDATA)
400         selection = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
401     else
402         selection = OSSL_KEYMGMT_SELECT_ALL;
403     keydata = evp_keymgmt_util_fromdata(*ppkey, ctx->keymgmt, selection,
404                                         params);
405
406     if (keydata == NULL)
407         return 0;
408     /* keydata is cached in *ppkey, so we need not bother with it further */
409     return 1;
410 }
411
412 /*
413  * TODO(3.0) Re-evaluate the names, it's possible that we find these to be
414  * better:
415  *
416  * EVP_PKEY_param_settable()
417  * EVP_PKEY_param_gettable()
418  */
419 const OSSL_PARAM *EVP_PKEY_param_fromdata_settable(EVP_PKEY_CTX *ctx)
420 {
421     /* We call fromdata_init to get ctx->keymgmt populated */
422     if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED))
423         return evp_keymgmt_import_types(ctx->keymgmt,
424                                         OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
425     return NULL;
426 }
427
428 const OSSL_PARAM *EVP_PKEY_key_fromdata_settable(EVP_PKEY_CTX *ctx)
429 {
430     /* We call fromdata_init to get ctx->keymgmt populated */
431     if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED))
432         return evp_keymgmt_import_types(ctx->keymgmt,
433                                         OSSL_KEYMGMT_SELECT_ALL);
434     return NULL;
435 }