14c5fd4b99d393d4b8f1f0709fc8c40fb1640b95
[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 "internal/cryptlib.h"
13 #include <openssl/objects.h>
14 #include <openssl/evp.h>
15 #include "crypto/bn.h"
16 #include "crypto/asn1.h"
17 #include "crypto/evp.h"
18 #include "evp_local.h"
19
20 #ifndef FIPS_MODE
21 int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx)
22 {
23     int ret;
24     if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
25         EVPerr(EVP_F_EVP_PKEY_PARAMGEN_INIT,
26                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
27         return -2;
28     }
29     ctx->operation = EVP_PKEY_OP_PARAMGEN;
30     if (!ctx->pmeth->paramgen_init)
31         return 1;
32     ret = ctx->pmeth->paramgen_init(ctx);
33     if (ret <= 0)
34         ctx->operation = EVP_PKEY_OP_UNDEFINED;
35     return ret;
36 }
37
38 int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
39 {
40     int ret;
41     if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
42         EVPerr(EVP_F_EVP_PKEY_PARAMGEN,
43                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
44         return -2;
45     }
46
47     if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {
48         EVPerr(EVP_F_EVP_PKEY_PARAMGEN, EVP_R_OPERATON_NOT_INITIALIZED);
49         return -1;
50     }
51
52     if (ppkey == NULL)
53         return -1;
54
55     if (*ppkey == NULL)
56         *ppkey = EVP_PKEY_new();
57
58     if (*ppkey == NULL) {
59         EVPerr(EVP_F_EVP_PKEY_PARAMGEN, ERR_R_MALLOC_FAILURE);
60         return -1;
61     }
62
63     ret = ctx->pmeth->paramgen(ctx, *ppkey);
64     if (ret <= 0) {
65         EVP_PKEY_free(*ppkey);
66         *ppkey = NULL;
67     }
68     return ret;
69 }
70
71 int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx)
72 {
73     int ret;
74     if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
75         EVPerr(EVP_F_EVP_PKEY_KEYGEN_INIT,
76                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
77         return -2;
78     }
79     ctx->operation = EVP_PKEY_OP_KEYGEN;
80     if (!ctx->pmeth->keygen_init)
81         return 1;
82     ret = ctx->pmeth->keygen_init(ctx);
83     if (ret <= 0)
84         ctx->operation = EVP_PKEY_OP_UNDEFINED;
85     return ret;
86 }
87
88 int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
89 {
90     int ret;
91
92     if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
93         EVPerr(EVP_F_EVP_PKEY_KEYGEN,
94                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
95         return -2;
96     }
97     if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
98         EVPerr(EVP_F_EVP_PKEY_KEYGEN, EVP_R_OPERATON_NOT_INITIALIZED);
99         return -1;
100     }
101
102     if (ppkey == NULL)
103         return -1;
104
105     if (*ppkey == NULL)
106         *ppkey = EVP_PKEY_new();
107     if (*ppkey == NULL)
108         return -1;
109
110     ret = ctx->pmeth->keygen(ctx, *ppkey);
111     if (ret <= 0) {
112         EVP_PKEY_free(*ppkey);
113         *ppkey = NULL;
114     }
115     return ret;
116 }
117
118 void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb)
119 {
120     ctx->pkey_gencb = cb;
121 }
122
123 EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx)
124 {
125     return ctx->pkey_gencb;
126 }
127
128 /*
129  * "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB style
130  * callbacks.
131  */
132
133 static int trans_cb(int a, int b, BN_GENCB *gcb)
134 {
135     EVP_PKEY_CTX *ctx = BN_GENCB_get_arg(gcb);
136     ctx->keygen_info[0] = a;
137     ctx->keygen_info[1] = b;
138     return ctx->pkey_gencb(ctx);
139 }
140
141 void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx)
142 {
143     BN_GENCB_set(cb, trans_cb, ctx);
144 }
145
146 int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx)
147 {
148     if (idx == -1)
149         return ctx->keygen_info_count;
150     if (idx < 0 || idx > ctx->keygen_info_count)
151         return 0;
152     return ctx->keygen_info[idx];
153 }
154
155 EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
156                                const unsigned char *key, int keylen)
157 {
158     EVP_PKEY_CTX *mac_ctx = NULL;
159     EVP_PKEY *mac_key = NULL;
160     mac_ctx = EVP_PKEY_CTX_new_id(type, e);
161     if (!mac_ctx)
162         return NULL;
163     if (EVP_PKEY_keygen_init(mac_ctx) <= 0)
164         goto merr;
165     if (EVP_PKEY_CTX_set_mac_key(mac_ctx, key, keylen) <= 0)
166         goto merr;
167     if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0)
168         goto merr;
169  merr:
170     EVP_PKEY_CTX_free(mac_ctx);
171     return mac_key;
172 }
173
174 int EVP_PKEY_check(EVP_PKEY_CTX *ctx)
175 {
176     EVP_PKEY *pkey = ctx->pkey;
177
178     if (pkey == NULL) {
179         EVPerr(EVP_F_EVP_PKEY_CHECK, EVP_R_NO_KEY_SET);
180         return 0;
181     }
182
183     /* call customized check function first */
184     if (ctx->pmeth->check != NULL)
185         return ctx->pmeth->check(pkey);
186
187     /* use default check function in ameth */
188     if (pkey->ameth == NULL || pkey->ameth->pkey_check == NULL) {
189         EVPerr(EVP_F_EVP_PKEY_CHECK,
190                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
191         return -2;
192     }
193
194     return pkey->ameth->pkey_check(pkey);
195 }
196
197 int EVP_PKEY_public_check(EVP_PKEY_CTX *ctx)
198 {
199     EVP_PKEY *pkey = ctx->pkey;
200
201     if (pkey == NULL) {
202         EVPerr(EVP_F_EVP_PKEY_PUBLIC_CHECK, EVP_R_NO_KEY_SET);
203         return 0;
204     }
205
206     /* call customized public key check function first */
207     if (ctx->pmeth->public_check != NULL)
208         return ctx->pmeth->public_check(pkey);
209
210     /* use default public key check function in ameth */
211     if (pkey->ameth == NULL || pkey->ameth->pkey_public_check == NULL) {
212         EVPerr(EVP_F_EVP_PKEY_PUBLIC_CHECK,
213                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
214         return -2;
215     }
216
217     return pkey->ameth->pkey_public_check(pkey);
218 }
219
220 int EVP_PKEY_param_check(EVP_PKEY_CTX *ctx)
221 {
222     EVP_PKEY *pkey = ctx->pkey;
223
224     if (pkey == NULL) {
225         EVPerr(EVP_F_EVP_PKEY_PARAM_CHECK, EVP_R_NO_KEY_SET);
226         return 0;
227     }
228
229     /* call customized param check function first */
230     if (ctx->pmeth->param_check != NULL)
231         return ctx->pmeth->param_check(pkey);
232
233     /* use default param check function in ameth */
234     if (pkey->ameth == NULL || pkey->ameth->pkey_param_check == NULL) {
235         EVPerr(EVP_F_EVP_PKEY_PARAM_CHECK,
236                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
237         return -2;
238     }
239
240     return pkey->ameth->pkey_param_check(pkey);
241 }
242
243 #endif /* FIPS_MODE */
244
245 /*- All methods below can also be used in FIPS_MODE */
246
247 static int fromdata_init(EVP_PKEY_CTX *ctx, int operation)
248 {
249     if (ctx == NULL || ctx->keytype == NULL)
250         goto not_supported;
251
252     evp_pkey_ctx_free_old_ops(ctx);
253     ctx->operation = operation;
254     if (ctx->keymgmt == NULL)
255         ctx->keymgmt = EVP_KEYMGMT_fetch(ctx->libctx, ctx->keytype,
256                                          ctx->propquery);
257     if (ctx->keymgmt == NULL)
258         goto not_supported;
259
260     return 1;
261
262  not_supported:
263     ctx->operation = EVP_PKEY_OP_UNDEFINED;
264     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
265     return -2;
266 }
267
268 int EVP_PKEY_param_fromdata_init(EVP_PKEY_CTX *ctx)
269 {
270     return fromdata_init(ctx, EVP_PKEY_OP_PARAMFROMDATA);
271 }
272
273 int EVP_PKEY_key_fromdata_init(EVP_PKEY_CTX *ctx)
274 {
275     return fromdata_init(ctx, EVP_PKEY_OP_KEYFROMDATA);
276 }
277
278 int EVP_PKEY_fromdata(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey, OSSL_PARAM params[])
279 {
280     void *provdata = NULL;
281
282     if (ctx == NULL || (ctx->operation & EVP_PKEY_OP_TYPE_FROMDATA) == 0) {
283         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
284         return -2;
285     }
286
287     if (ppkey == NULL)
288         return -1;
289
290     if (*ppkey == NULL)
291         *ppkey = EVP_PKEY_new();
292
293     if (*ppkey == NULL) {
294         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
295         return -1;
296     }
297
298     provdata =
299         evp_keymgmt_fromdata(*ppkey, ctx->keymgmt, params,
300                              ctx->operation == EVP_PKEY_OP_PARAMFROMDATA);
301
302     if (provdata == NULL)
303         return 0;
304     /* provdata is cached in *ppkey, so we need not bother with it further */
305     return 1;
306 }
307
308 /*
309  * TODO(3.0) Re-evaluate the names, it's possible that we find these to be
310  * better:
311  *
312  * EVP_PKEY_param_settable()
313  * EVP_PKEY_param_gettable()
314  */
315 const OSSL_PARAM *EVP_PKEY_param_fromdata_settable(EVP_PKEY_CTX *ctx)
316 {
317     /* We call fromdata_init to get ctx->keymgmt populated */
318     if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED))
319         return evp_keymgmt_importdomparam_types(ctx->keymgmt);
320     return NULL;
321 }
322
323 const OSSL_PARAM *EVP_PKEY_key_fromdata_settable(EVP_PKEY_CTX *ctx)
324 {
325     /* We call fromdata_init to get ctx->keymgmt populated */
326     if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED))
327         return evp_keymgmt_importdomparam_types(ctx->keymgmt);
328     return NULL;
329 }
330
331