Redesign the KEYMGMT libcrypto <-> provider interface - the basics
[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 #endif /* FIPS_MODE */
175
176 /*- All methods below can also be used in FIPS_MODE */
177
178 static int fromdata_init(EVP_PKEY_CTX *ctx, int operation)
179 {
180     if (ctx == NULL || ctx->keytype == NULL)
181         goto not_supported;
182
183     evp_pkey_ctx_free_old_ops(ctx);
184     ctx->operation = operation;
185     if (ctx->keymgmt == NULL)
186         ctx->keymgmt = EVP_KEYMGMT_fetch(ctx->libctx, ctx->keytype,
187                                          ctx->propquery);
188     if (ctx->keymgmt == NULL)
189         goto not_supported;
190
191     return 1;
192
193  not_supported:
194     ctx->operation = EVP_PKEY_OP_UNDEFINED;
195     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
196     return -2;
197 }
198
199 int EVP_PKEY_param_fromdata_init(EVP_PKEY_CTX *ctx)
200 {
201     return fromdata_init(ctx, EVP_PKEY_OP_PARAMFROMDATA);
202 }
203
204 int EVP_PKEY_key_fromdata_init(EVP_PKEY_CTX *ctx)
205 {
206     return fromdata_init(ctx, EVP_PKEY_OP_KEYFROMDATA);
207 }
208
209 int EVP_PKEY_fromdata(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey, OSSL_PARAM params[])
210 {
211     void *keydata = NULL;
212     int selection;
213
214     if (ctx == NULL || (ctx->operation & EVP_PKEY_OP_TYPE_FROMDATA) == 0) {
215         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
216         return -2;
217     }
218
219     if (ppkey == NULL)
220         return -1;
221
222     if (*ppkey == NULL)
223         *ppkey = EVP_PKEY_new();
224
225     if (*ppkey == NULL) {
226         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
227         return -1;
228     }
229
230     if (ctx->operation == EVP_PKEY_OP_PARAMFROMDATA)
231         selection = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
232     else
233         selection = OSSL_KEYMGMT_SELECT_KEYPAIR;
234     keydata = evp_keymgmt_util_fromdata(*ppkey, ctx->keymgmt, selection,
235                                         params);
236
237     if (keydata == NULL)
238         return 0;
239     /* keydata is cached in *ppkey, so we need not bother with it further */
240     return 1;
241 }
242
243 /*
244  * TODO(3.0) Re-evaluate the names, it's possible that we find these to be
245  * better:
246  *
247  * EVP_PKEY_param_settable()
248  * EVP_PKEY_param_gettable()
249  */
250 const OSSL_PARAM *EVP_PKEY_param_fromdata_settable(EVP_PKEY_CTX *ctx)
251 {
252     /* We call fromdata_init to get ctx->keymgmt populated */
253     if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED))
254         return evp_keymgmt_import_types(ctx->keymgmt,
255                                         OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
256     return NULL;
257 }
258
259 const OSSL_PARAM *EVP_PKEY_key_fromdata_settable(EVP_PKEY_CTX *ctx)
260 {
261     /* We call fromdata_init to get ctx->keymgmt populated */
262     if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED))
263         return evp_keymgmt_import_types(ctx->keymgmt,
264                                         OSSL_KEYMGMT_SELECT_KEYPAIR);
265     return NULL;
266 }