Put an error on the stack in the event of a fetch failure
[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     if (ctx->pkey && !evp_pkey_downgrade(ctx->pkey))
184         goto not_accessible;
185     switch (ctx->operation) {
186     case EVP_PKEY_OP_PARAMGEN:
187         ret = ctx->pmeth->paramgen(ctx, *ppkey);
188         break;
189     case EVP_PKEY_OP_KEYGEN:
190         ret = ctx->pmeth->keygen(ctx, *ppkey);
191         break;
192     default:
193         goto not_supported;
194     }
195 #endif
196
197  end:
198     if (ret <= 0) {
199         if (allocated_pkey != NULL)
200             *ppkey = NULL;
201         EVP_PKEY_free(allocated_pkey);
202     }
203     return ret;
204
205  not_supported:
206     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
207     ret = -2;
208     goto end;
209  not_initialized:
210     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
211     ret = -1;
212     goto end;
213 #ifndef FIPS_MODE
214  not_accessible:
215     ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_DOMAIN_PARAMETERS);
216     ret = -1;
217     goto end;
218 #endif
219 }
220
221 int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
222 {
223     if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {
224         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
225         return -1;
226     }
227     return EVP_PKEY_gen(ctx, ppkey);
228 }
229
230 int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
231 {
232     if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
233         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
234         return -1;
235     }
236     return EVP_PKEY_gen(ctx, ppkey);
237 }
238
239 void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb)
240 {
241     ctx->pkey_gencb = cb;
242 }
243
244 EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx)
245 {
246     return ctx->pkey_gencb;
247 }
248
249 /*
250  * "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB style
251  * callbacks.
252  */
253
254 static int trans_cb(int a, int b, BN_GENCB *gcb)
255 {
256     EVP_PKEY_CTX *ctx = BN_GENCB_get_arg(gcb);
257     ctx->keygen_info[0] = a;
258     ctx->keygen_info[1] = b;
259     return ctx->pkey_gencb(ctx);
260 }
261
262 void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx)
263 {
264     BN_GENCB_set(cb, trans_cb, ctx);
265 }
266
267 int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx)
268 {
269     if (idx == -1)
270         return ctx->keygen_info_count;
271     if (idx < 0 || idx > ctx->keygen_info_count)
272         return 0;
273     return ctx->keygen_info[idx];
274 }
275
276 #ifndef FIPS_MODE
277
278 EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
279                                const unsigned char *key, int keylen)
280 {
281     EVP_PKEY_CTX *mac_ctx = NULL;
282     EVP_PKEY *mac_key = NULL;
283     mac_ctx = EVP_PKEY_CTX_new_id(type, e);
284     if (!mac_ctx)
285         return NULL;
286     if (EVP_PKEY_keygen_init(mac_ctx) <= 0)
287         goto merr;
288     if (EVP_PKEY_CTX_set_mac_key(mac_ctx, key, keylen) <= 0)
289         goto merr;
290     if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0)
291         goto merr;
292  merr:
293     EVP_PKEY_CTX_free(mac_ctx);
294     return mac_key;
295 }
296
297 #endif /* FIPS_MODE */
298
299 /*- All methods below can also be used in FIPS_MODE */
300
301 static int fromdata_init(EVP_PKEY_CTX *ctx, int operation)
302 {
303     if (ctx == NULL || ctx->keytype == NULL)
304         goto not_supported;
305
306     evp_pkey_ctx_free_old_ops(ctx);
307     if (ctx->keymgmt == NULL)
308         goto not_supported;
309
310     ctx->operation = operation;
311     return 1;
312
313  not_supported:
314     ctx->operation = EVP_PKEY_OP_UNDEFINED;
315     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
316     return -2;
317 }
318
319 int EVP_PKEY_param_fromdata_init(EVP_PKEY_CTX *ctx)
320 {
321     return fromdata_init(ctx, EVP_PKEY_OP_PARAMFROMDATA);
322 }
323
324 int EVP_PKEY_key_fromdata_init(EVP_PKEY_CTX *ctx)
325 {
326     return fromdata_init(ctx, EVP_PKEY_OP_KEYFROMDATA);
327 }
328
329 int EVP_PKEY_fromdata(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey, OSSL_PARAM params[])
330 {
331     void *keydata = NULL;
332     int selection;
333
334     if (ctx == NULL || (ctx->operation & EVP_PKEY_OP_TYPE_FROMDATA) == 0) {
335         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
336         return -2;
337     }
338
339     if (ppkey == NULL)
340         return -1;
341
342     if (*ppkey == NULL)
343         *ppkey = EVP_PKEY_new();
344
345     if (*ppkey == NULL) {
346         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
347         return -1;
348     }
349
350     if (ctx->operation == EVP_PKEY_OP_PARAMFROMDATA)
351         selection = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
352     else
353         selection = OSSL_KEYMGMT_SELECT_ALL;
354     keydata = evp_keymgmt_util_fromdata(*ppkey, ctx->keymgmt, selection,
355                                         params);
356
357     if (keydata == NULL)
358         return 0;
359     /* keydata is cached in *ppkey, so we need not bother with it further */
360     return 1;
361 }
362
363 /*
364  * TODO(3.0) Re-evaluate the names, it's possible that we find these to be
365  * better:
366  *
367  * EVP_PKEY_param_settable()
368  * EVP_PKEY_param_gettable()
369  */
370 const OSSL_PARAM *EVP_PKEY_param_fromdata_settable(EVP_PKEY_CTX *ctx)
371 {
372     /* We call fromdata_init to get ctx->keymgmt populated */
373     if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED))
374         return evp_keymgmt_import_types(ctx->keymgmt,
375                                         OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
376     return NULL;
377 }
378
379 const OSSL_PARAM *EVP_PKEY_key_fromdata_settable(EVP_PKEY_CTX *ctx)
380 {
381     /* We call fromdata_init to get ctx->keymgmt populated */
382     if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED))
383         return evp_keymgmt_import_types(ctx->keymgmt,
384                                         OSSL_KEYMGMT_SELECT_ALL);
385     return NULL;
386 }