Handle mdname in legacy EVP_DigestSignInit_ex codepaths
[openssl.git] / crypto / evp / pmeth_gn.c
1 /*
2  * Copyright 2006-2016 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 static int gen_init(EVP_PKEY_CTX *ctx, int operation)
24 {
25     int ret = 0;
26
27     if (ctx == NULL)
28         goto not_supported;
29
30     evp_pkey_ctx_free_old_ops(ctx);
31     ctx->operation = operation;
32
33     if (ctx->keymgmt == NULL || ctx->keymgmt->gen_init == NULL)
34         goto legacy;
35
36     switch (operation) {
37     case EVP_PKEY_OP_PARAMGEN:
38         ctx->op.keymgmt.genctx =
39             evp_keymgmt_gen_init(ctx->keymgmt,
40                                  OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
41         break;
42     case EVP_PKEY_OP_KEYGEN:
43         ctx->op.keymgmt.genctx =
44             evp_keymgmt_gen_init(ctx->keymgmt, OSSL_KEYMGMT_SELECT_KEYPAIR);
45         break;
46     }
47
48     if (ctx->op.keymgmt.genctx == NULL)
49         ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
50     else
51         ret = 1;
52     goto end;
53
54  legacy:
55 #ifdef FIPS_MODE
56     goto not_supported;
57 #else
58     if (ctx->pmeth == NULL
59         || (operation == EVP_PKEY_OP_PARAMGEN
60             && ctx->pmeth->paramgen == NULL)
61         || (operation == EVP_PKEY_OP_KEYGEN
62             && ctx->pmeth->keygen == NULL))
63         goto not_supported;
64
65     ret = 1;
66     switch (operation) {
67     case EVP_PKEY_OP_PARAMGEN:
68         if (ctx->pmeth->paramgen_init != NULL)
69             ret = ctx->pmeth->paramgen_init(ctx);
70         break;
71     case EVP_PKEY_OP_KEYGEN:
72         if (ctx->pmeth->keygen_init != NULL)
73             ret = ctx->pmeth->keygen_init(ctx);
74         break;
75     }
76 #endif
77
78  end:
79     if (ret <= 0)
80         ctx->operation = EVP_PKEY_OP_UNDEFINED;
81     return ret;
82
83  not_supported:
84     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
85     ret = -2;
86     goto end;
87 }
88
89 int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx)
90 {
91     return gen_init(ctx, EVP_PKEY_OP_PARAMGEN);
92 }
93
94 int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx)
95 {
96     return gen_init(ctx, EVP_PKEY_OP_KEYGEN);
97 }
98
99 static int ossl_callback_to_pkey_gencb(const OSSL_PARAM params[], void *arg)
100 {
101     EVP_PKEY_CTX *ctx = arg;
102     const OSSL_PARAM *param = NULL;
103     int p = -1, n = -1;
104
105     if (ctx->pkey_gencb == NULL)
106         return 1;                /* No callback?  That's fine */
107
108     if ((param = OSSL_PARAM_locate_const(params, OSSL_GEN_PARAM_POTENTIAL))
109         == NULL
110         || !OSSL_PARAM_get_int(param, &p))
111         return 0;
112     if ((param = OSSL_PARAM_locate_const(params, OSSL_GEN_PARAM_ITERATION))
113         == NULL
114         || !OSSL_PARAM_get_int(param, &n))
115         return 0;
116
117     ctx->keygen_info[0] = p;
118     ctx->keygen_info[1] = n;
119
120     return ctx->pkey_gencb(ctx);
121 }
122
123 int EVP_PKEY_gen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
124 {
125     int ret = 0;
126     OSSL_CALLBACK cb;
127     EVP_PKEY *allocated_pkey = NULL;
128
129     if (ppkey == NULL)
130         return -1;
131
132     if (ctx == NULL)
133         goto not_supported;
134
135     if ((ctx->operation & EVP_PKEY_OP_TYPE_GEN) == 0)
136         goto not_initialized;
137
138     if (*ppkey == NULL)
139         *ppkey = allocated_pkey = EVP_PKEY_new();
140
141     if (*ppkey == NULL) {
142         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
143         return -1;
144     }
145
146     if (ctx->keymgmt == NULL || ctx->op.keymgmt.genctx == NULL)
147         goto legacy;
148
149     ret = 1;
150     if (ctx->pkey != NULL) {
151         EVP_KEYMGMT *tmp_keymgmt = ctx->keymgmt;
152         void *keydata =
153             evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
154                                         &tmp_keymgmt, ctx->propquery);
155
156         if (keydata == NULL)
157             goto not_supported;
158         ret = evp_keymgmt_gen_set_template(ctx->keymgmt,
159                                            ctx->op.keymgmt.genctx, keydata);
160     }
161
162     /*
163      * the returned value from evp_keymgmt_util_gen() is cached in *ppkey,
164      * so we so not need to save it, just check it.
165      */
166     ret = ret
167         && (evp_keymgmt_util_gen(*ppkey, ctx->keymgmt, ctx->op.keymgmt.genctx,
168                                  ossl_callback_to_pkey_gencb, ctx)
169             != NULL);
170
171 #ifndef FIPS_MODE
172     /* In case |*ppkey| was originally a legacy key */
173     if (ret)
174         evp_pkey_free_legacy(*ppkey);
175 #endif
176
177     goto end;
178
179  legacy:
180 #ifdef FIPS_MODE
181     goto not_supported;
182 #else
183     switch (ctx->operation) {
184     case EVP_PKEY_OP_PARAMGEN:
185         ret = ctx->pmeth->paramgen(ctx, *ppkey);
186         break;
187     case EVP_PKEY_OP_KEYGEN:
188         ret = ctx->pmeth->keygen(ctx, *ppkey);
189         break;
190     default:
191         goto not_supported;
192     }
193 #endif
194
195  end:
196     if (ret <= 0) {
197         if (allocated_pkey != NULL)
198             *ppkey = NULL;
199         EVP_PKEY_free(allocated_pkey);
200     }
201     return ret;
202
203  not_supported:
204     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
205     ret = -2;
206     goto end;
207  not_initialized:
208     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
209     ret = -1;
210     goto end;
211 }
212
213 int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
214 {
215     if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {
216         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
217         return -1;
218     }
219     return EVP_PKEY_gen(ctx, ppkey);
220 }
221
222 int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
223 {
224     if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
225         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
226         return -1;
227     }
228     return EVP_PKEY_gen(ctx, ppkey);
229 }
230
231 void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb)
232 {
233     ctx->pkey_gencb = cb;
234 }
235
236 EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx)
237 {
238     return ctx->pkey_gencb;
239 }
240
241 /*
242  * "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB style
243  * callbacks.
244  */
245
246 static int trans_cb(int a, int b, BN_GENCB *gcb)
247 {
248     EVP_PKEY_CTX *ctx = BN_GENCB_get_arg(gcb);
249     ctx->keygen_info[0] = a;
250     ctx->keygen_info[1] = b;
251     return ctx->pkey_gencb(ctx);
252 }
253
254 void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx)
255 {
256     BN_GENCB_set(cb, trans_cb, ctx);
257 }
258
259 int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx)
260 {
261     if (idx == -1)
262         return ctx->keygen_info_count;
263     if (idx < 0 || idx > ctx->keygen_info_count)
264         return 0;
265     return ctx->keygen_info[idx];
266 }
267
268 #ifndef FIPS_MODE
269
270 EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
271                                const unsigned char *key, int keylen)
272 {
273     EVP_PKEY_CTX *mac_ctx = NULL;
274     EVP_PKEY *mac_key = NULL;
275     mac_ctx = EVP_PKEY_CTX_new_id(type, e);
276     if (!mac_ctx)
277         return NULL;
278     if (EVP_PKEY_keygen_init(mac_ctx) <= 0)
279         goto merr;
280     if (EVP_PKEY_CTX_set_mac_key(mac_ctx, key, keylen) <= 0)
281         goto merr;
282     if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0)
283         goto merr;
284  merr:
285     EVP_PKEY_CTX_free(mac_ctx);
286     return mac_key;
287 }
288
289 #endif /* FIPS_MODE */
290
291 /*- All methods below can also be used in FIPS_MODE */
292
293 static int fromdata_init(EVP_PKEY_CTX *ctx, int operation)
294 {
295     if (ctx == NULL || ctx->keytype == NULL)
296         goto not_supported;
297
298     evp_pkey_ctx_free_old_ops(ctx);
299     if (ctx->keymgmt == NULL)
300         goto not_supported;
301
302     ctx->operation = operation;
303     return 1;
304
305  not_supported:
306     ctx->operation = EVP_PKEY_OP_UNDEFINED;
307     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
308     return -2;
309 }
310
311 int EVP_PKEY_param_fromdata_init(EVP_PKEY_CTX *ctx)
312 {
313     return fromdata_init(ctx, EVP_PKEY_OP_PARAMFROMDATA);
314 }
315
316 int EVP_PKEY_key_fromdata_init(EVP_PKEY_CTX *ctx)
317 {
318     return fromdata_init(ctx, EVP_PKEY_OP_KEYFROMDATA);
319 }
320
321 int EVP_PKEY_fromdata(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey, OSSL_PARAM params[])
322 {
323     void *keydata = NULL;
324     int selection;
325
326     if (ctx == NULL || (ctx->operation & EVP_PKEY_OP_TYPE_FROMDATA) == 0) {
327         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
328         return -2;
329     }
330
331     if (ppkey == NULL)
332         return -1;
333
334     if (*ppkey == NULL)
335         *ppkey = EVP_PKEY_new();
336
337     if (*ppkey == NULL) {
338         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
339         return -1;
340     }
341
342     if (ctx->operation == EVP_PKEY_OP_PARAMFROMDATA)
343         selection = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
344     else
345         selection = OSSL_KEYMGMT_SELECT_ALL;
346     keydata = evp_keymgmt_util_fromdata(*ppkey, ctx->keymgmt, selection,
347                                         params);
348
349     if (keydata == NULL)
350         return 0;
351     /* keydata is cached in *ppkey, so we need not bother with it further */
352     return 1;
353 }
354
355 /*
356  * TODO(3.0) Re-evaluate the names, it's possible that we find these to be
357  * better:
358  *
359  * EVP_PKEY_param_settable()
360  * EVP_PKEY_param_gettable()
361  */
362 const OSSL_PARAM *EVP_PKEY_param_fromdata_settable(EVP_PKEY_CTX *ctx)
363 {
364     /* We call fromdata_init to get ctx->keymgmt populated */
365     if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED))
366         return evp_keymgmt_import_types(ctx->keymgmt,
367                                         OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
368     return NULL;
369 }
370
371 const OSSL_PARAM *EVP_PKEY_key_fromdata_settable(EVP_PKEY_CTX *ctx)
372 {
373     /* We call fromdata_init to get ctx->keymgmt populated */
374     if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED))
375         return evp_keymgmt_import_types(ctx->keymgmt,
376                                         OSSL_KEYMGMT_SELECT_ALL);
377     return NULL;
378 }