]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
33388b44 | 2 | * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved. |
f5cda4cb | 3 | * |
4a8b0c55 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
62867571 RS |
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 | |
f5cda4cb DSH |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
11 | #include <stdlib.h> | |
62924755 RL |
12 | #include <openssl/core.h> |
13 | #include <openssl/core_names.h> | |
b39fc560 | 14 | #include "internal/cryptlib.h" |
62924755 | 15 | #include "internal/core.h" |
c20276e4 | 16 | #include <openssl/objects.h> |
f5cda4cb | 17 | #include <openssl/evp.h> |
25f2138b DMSP |
18 | #include "crypto/bn.h" |
19 | #include "crypto/asn1.h" | |
20 | #include "crypto/evp.h" | |
46e2dd05 RL |
21 | #include "evp_local.h" |
22 | ||
813d3171 RL |
23 | #if !defined(FIPS_MODE) && !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 | ||
62924755 | 34 | static int gen_init(EVP_PKEY_CTX *ctx, int operation) |
0f113f3e | 35 | { |
62924755 RL |
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 | ||
4b9e90f4 | 44 | if (ctx->keymgmt == NULL || ctx->keymgmt->gen_init == NULL) |
62924755 RL |
45 | goto legacy; |
46 | ||
813d3171 RL |
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 | ||
62924755 RL |
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_MODE | |
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; | |
0f113f3e | 92 | } |
62924755 RL |
93 | #endif |
94 | ||
95 | end: | |
0f113f3e MC |
96 | if (ret <= 0) |
97 | ctx->operation = EVP_PKEY_OP_UNDEFINED; | |
98 | return ret; | |
62924755 RL |
99 | |
100 | not_supported: | |
101 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
102 | ret = -2; | |
103 | goto end; | |
0f113f3e | 104 | } |
f5cda4cb | 105 | |
62924755 | 106 | int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx) |
0f113f3e | 107 | { |
62924755 RL |
108 | return gen_init(ctx, EVP_PKEY_OP_PARAMGEN); |
109 | } | |
0f113f3e | 110 | |
62924755 RL |
111 | int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx) |
112 | { | |
113 | return gen_init(ctx, EVP_PKEY_OP_KEYGEN); | |
114 | } | |
115 | ||
116 | static int ossl_callback_to_pkey_gencb(const OSSL_PARAM params[], void *arg) | |
117 | { | |
118 | EVP_PKEY_CTX *ctx = arg; | |
119 | const OSSL_PARAM *param = NULL; | |
120 | int p = -1, n = -1; | |
121 | ||
122 | if (ctx->pkey_gencb == NULL) | |
123 | return 1; /* No callback? That's fine */ | |
124 | ||
125 | if ((param = OSSL_PARAM_locate_const(params, OSSL_GEN_PARAM_POTENTIAL)) | |
126 | == NULL | |
127 | || !OSSL_PARAM_get_int(param, &p)) | |
128 | return 0; | |
129 | if ((param = OSSL_PARAM_locate_const(params, OSSL_GEN_PARAM_ITERATION)) | |
130 | == NULL | |
131 | || !OSSL_PARAM_get_int(param, &n)) | |
132 | return 0; | |
133 | ||
134 | ctx->keygen_info[0] = p; | |
135 | ctx->keygen_info[1] = n; | |
136 | ||
137 | return ctx->pkey_gencb(ctx); | |
138 | } | |
139 | ||
140 | int EVP_PKEY_gen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey) | |
141 | { | |
142 | int ret = 0; | |
143 | OSSL_CALLBACK cb; | |
144 | EVP_PKEY *allocated_pkey = NULL; | |
0f113f3e | 145 | |
e34c66c6 | 146 | if (ppkey == NULL) |
0f113f3e MC |
147 | return -1; |
148 | ||
62924755 RL |
149 | if (ctx == NULL) |
150 | goto not_supported; | |
151 | ||
152 | if ((ctx->operation & EVP_PKEY_OP_TYPE_GEN) == 0) | |
153 | goto not_initialized; | |
154 | ||
e34c66c6 | 155 | if (*ppkey == NULL) |
62924755 | 156 | *ppkey = allocated_pkey = EVP_PKEY_new(); |
0f113f3e | 157 | |
e34c66c6 | 158 | if (*ppkey == NULL) { |
62924755 | 159 | ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); |
e34c66c6 EK |
160 | return -1; |
161 | } | |
162 | ||
813d3171 | 163 | if (ctx->op.keymgmt.genctx == NULL) |
62924755 RL |
164 | goto legacy; |
165 | ||
166 | ret = 1; | |
167 | if (ctx->pkey != NULL) { | |
168 | EVP_KEYMGMT *tmp_keymgmt = ctx->keymgmt; | |
169 | void *keydata = | |
170 | evp_pkey_export_to_provider(ctx->pkey, ctx->libctx, | |
171 | &tmp_keymgmt, ctx->propquery); | |
172 | ||
d0ddf9b4 | 173 | if (tmp_keymgmt == NULL) |
62924755 | 174 | goto not_supported; |
d0ddf9b4 RL |
175 | /* |
176 | * It's ok if keydata is NULL here. The backend is expected to deal | |
177 | * with that as it sees fit. | |
178 | */ | |
62924755 RL |
179 | ret = evp_keymgmt_gen_set_template(ctx->keymgmt, |
180 | ctx->op.keymgmt.genctx, keydata); | |
181 | } | |
182 | ||
183 | /* | |
184 | * the returned value from evp_keymgmt_util_gen() is cached in *ppkey, | |
185 | * so we so not need to save it, just check it. | |
186 | */ | |
187 | ret = ret | |
188 | && (evp_keymgmt_util_gen(*ppkey, ctx->keymgmt, ctx->op.keymgmt.genctx, | |
189 | ossl_callback_to_pkey_gencb, ctx) | |
190 | != NULL); | |
191 | ||
192 | #ifndef FIPS_MODE | |
193 | /* In case |*ppkey| was originally a legacy key */ | |
194 | if (ret) | |
195 | evp_pkey_free_legacy(*ppkey); | |
196 | #endif | |
197 | ||
813d3171 RL |
198 | /* TODO remove when SM2 key have been cleanly separated from EC keys */ |
199 | #ifdef TMP_SM2_HACK | |
200 | /* | |
201 | * Legacy SM2 keys are implemented as EC_KEY with a twist. The legacy | |
202 | * key generation detects the SM2 curve and "magically" changes the pkey | |
203 | * id accordingly. | |
204 | * Since we don't have SM2 in the provider implementation, we need to | |
205 | * downgrade the generated provider side key to a legacy one under the | |
206 | * same conditions. | |
207 | * | |
208 | * THIS IS AN UGLY BUT TEMPORARY HACK | |
209 | */ | |
210 | { | |
211 | char curve_name[OSSL_MAX_NAME_SIZE] = ""; | |
212 | ||
213 | if (EVP_PKEY_CTX_get_ec_paramgen_curve_name(ctx, curve_name, | |
214 | sizeof(curve_name)) < 1 | |
215 | || strcmp(curve_name, "SM2") != 0) | |
216 | goto end; | |
217 | } | |
218 | ||
219 | if (!evp_pkey_downgrade(*ppkey) | |
220 | || !EVP_PKEY_set_alias_type(*ppkey, EVP_PKEY_SM2)) | |
221 | ret = 0; | |
222 | #endif | |
62924755 RL |
223 | goto end; |
224 | ||
225 | legacy: | |
226 | #ifdef FIPS_MODE | |
227 | goto not_supported; | |
228 | #else | |
acb90ba8 RL |
229 | if (ctx->pkey && !evp_pkey_downgrade(ctx->pkey)) |
230 | goto not_accessible; | |
62924755 RL |
231 | switch (ctx->operation) { |
232 | case EVP_PKEY_OP_PARAMGEN: | |
233 | ret = ctx->pmeth->paramgen(ctx, *ppkey); | |
234 | break; | |
235 | case EVP_PKEY_OP_KEYGEN: | |
236 | ret = ctx->pmeth->keygen(ctx, *ppkey); | |
237 | break; | |
238 | default: | |
239 | goto not_supported; | |
240 | } | |
241 | #endif | |
242 | ||
243 | end: | |
0f113f3e | 244 | if (ret <= 0) { |
62924755 RL |
245 | if (allocated_pkey != NULL) |
246 | *ppkey = NULL; | |
247 | EVP_PKEY_free(allocated_pkey); | |
0f113f3e MC |
248 | } |
249 | return ret; | |
62924755 RL |
250 | |
251 | not_supported: | |
252 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
253 | ret = -2; | |
254 | goto end; | |
255 | not_initialized: | |
256 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED); | |
257 | ret = -1; | |
258 | goto end; | |
acb90ba8 RL |
259 | #ifndef FIPS_MODE |
260 | not_accessible: | |
261 | ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_DOMAIN_PARAMETERS); | |
262 | ret = -1; | |
263 | goto end; | |
264 | #endif | |
0f113f3e | 265 | } |
f5cda4cb | 266 | |
62924755 | 267 | int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey) |
0f113f3e | 268 | { |
62924755 RL |
269 | if (ctx->operation != EVP_PKEY_OP_PARAMGEN) { |
270 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED); | |
271 | return -1; | |
0f113f3e | 272 | } |
62924755 | 273 | return EVP_PKEY_gen(ctx, ppkey); |
0f113f3e | 274 | } |
f5cda4cb DSH |
275 | |
276 | int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey) | |
0f113f3e | 277 | { |
0f113f3e | 278 | if (ctx->operation != EVP_PKEY_OP_KEYGEN) { |
62924755 | 279 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED); |
0f113f3e MC |
280 | return -1; |
281 | } | |
62924755 | 282 | return EVP_PKEY_gen(ctx, ppkey); |
0f113f3e | 283 | } |
f5cda4cb DSH |
284 | |
285 | void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb) | |
0f113f3e MC |
286 | { |
287 | ctx->pkey_gencb = cb; | |
288 | } | |
f5cda4cb | 289 | |
b28dea4e | 290 | EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx) |
0f113f3e MC |
291 | { |
292 | return ctx->pkey_gencb; | |
293 | } | |
b28dea4e | 294 | |
0f113f3e MC |
295 | /* |
296 | * "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB style | |
297 | * callbacks. | |
f5cda4cb DSH |
298 | */ |
299 | ||
300 | static int trans_cb(int a, int b, BN_GENCB *gcb) | |
0f113f3e MC |
301 | { |
302 | EVP_PKEY_CTX *ctx = BN_GENCB_get_arg(gcb); | |
303 | ctx->keygen_info[0] = a; | |
304 | ctx->keygen_info[1] = b; | |
305 | return ctx->pkey_gencb(ctx); | |
306 | } | |
f5cda4cb DSH |
307 | |
308 | void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx) | |
0f113f3e MC |
309 | { |
310 | BN_GENCB_set(cb, trans_cb, ctx); | |
311 | } | |
f5cda4cb DSH |
312 | |
313 | int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx) | |
0f113f3e MC |
314 | { |
315 | if (idx == -1) | |
316 | return ctx->keygen_info_count; | |
317 | if (idx < 0 || idx > ctx->keygen_info_count) | |
318 | return 0; | |
319 | return ctx->keygen_info[idx]; | |
320 | } | |
2022cfe0 | 321 | |
62924755 RL |
322 | #ifndef FIPS_MODE |
323 | ||
2022cfe0 | 324 | EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e, |
0f113f3e MC |
325 | const unsigned char *key, int keylen) |
326 | { | |
327 | EVP_PKEY_CTX *mac_ctx = NULL; | |
328 | EVP_PKEY *mac_key = NULL; | |
329 | mac_ctx = EVP_PKEY_CTX_new_id(type, e); | |
330 | if (!mac_ctx) | |
331 | return NULL; | |
332 | if (EVP_PKEY_keygen_init(mac_ctx) <= 0) | |
333 | goto merr; | |
eff1a4d2 | 334 | if (EVP_PKEY_CTX_set_mac_key(mac_ctx, key, keylen) <= 0) |
0f113f3e MC |
335 | goto merr; |
336 | if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0) | |
337 | goto merr; | |
338 | merr: | |
c5ba2d99 | 339 | EVP_PKEY_CTX_free(mac_ctx); |
0f113f3e MC |
340 | return mac_key; |
341 | } | |
2aee35d3 | 342 | |
e683582b SL |
343 | #endif /* FIPS_MODE */ |
344 | ||
345 | /*- All methods below can also be used in FIPS_MODE */ | |
346 | ||
347 | static int fromdata_init(EVP_PKEY_CTX *ctx, int operation) | |
348 | { | |
349 | if (ctx == NULL || ctx->keytype == NULL) | |
350 | goto not_supported; | |
351 | ||
352 | evp_pkey_ctx_free_old_ops(ctx); | |
e683582b SL |
353 | if (ctx->keymgmt == NULL) |
354 | goto not_supported; | |
355 | ||
4b9e90f4 | 356 | ctx->operation = operation; |
e683582b SL |
357 | return 1; |
358 | ||
359 | not_supported: | |
360 | ctx->operation = EVP_PKEY_OP_UNDEFINED; | |
361 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
362 | return -2; | |
363 | } | |
364 | ||
365 | int EVP_PKEY_param_fromdata_init(EVP_PKEY_CTX *ctx) | |
366 | { | |
367 | return fromdata_init(ctx, EVP_PKEY_OP_PARAMFROMDATA); | |
368 | } | |
369 | ||
370 | int EVP_PKEY_key_fromdata_init(EVP_PKEY_CTX *ctx) | |
371 | { | |
372 | return fromdata_init(ctx, EVP_PKEY_OP_KEYFROMDATA); | |
373 | } | |
374 | ||
375 | int EVP_PKEY_fromdata(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey, OSSL_PARAM params[]) | |
376 | { | |
b305452f RL |
377 | void *keydata = NULL; |
378 | int selection; | |
e683582b SL |
379 | |
380 | if (ctx == NULL || (ctx->operation & EVP_PKEY_OP_TYPE_FROMDATA) == 0) { | |
381 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
382 | return -2; | |
383 | } | |
384 | ||
385 | if (ppkey == NULL) | |
386 | return -1; | |
387 | ||
388 | if (*ppkey == NULL) | |
389 | *ppkey = EVP_PKEY_new(); | |
390 | ||
391 | if (*ppkey == NULL) { | |
392 | ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); | |
393 | return -1; | |
394 | } | |
395 | ||
b305452f RL |
396 | if (ctx->operation == EVP_PKEY_OP_PARAMFROMDATA) |
397 | selection = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS; | |
398 | else | |
f552d900 | 399 | selection = OSSL_KEYMGMT_SELECT_ALL; |
b305452f RL |
400 | keydata = evp_keymgmt_util_fromdata(*ppkey, ctx->keymgmt, selection, |
401 | params); | |
e683582b | 402 | |
b305452f | 403 | if (keydata == NULL) |
e683582b | 404 | return 0; |
b305452f | 405 | /* keydata is cached in *ppkey, so we need not bother with it further */ |
e683582b SL |
406 | return 1; |
407 | } | |
408 | ||
409 | /* | |
410 | * TODO(3.0) Re-evaluate the names, it's possible that we find these to be | |
411 | * better: | |
412 | * | |
413 | * EVP_PKEY_param_settable() | |
414 | * EVP_PKEY_param_gettable() | |
415 | */ | |
416 | const OSSL_PARAM *EVP_PKEY_param_fromdata_settable(EVP_PKEY_CTX *ctx) | |
417 | { | |
418 | /* We call fromdata_init to get ctx->keymgmt populated */ | |
419 | if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED)) | |
b305452f RL |
420 | return evp_keymgmt_import_types(ctx->keymgmt, |
421 | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS); | |
e683582b SL |
422 | return NULL; |
423 | } | |
424 | ||
425 | const OSSL_PARAM *EVP_PKEY_key_fromdata_settable(EVP_PKEY_CTX *ctx) | |
426 | { | |
427 | /* We call fromdata_init to get ctx->keymgmt populated */ | |
428 | if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED)) | |
b305452f | 429 | return evp_keymgmt_import_types(ctx->keymgmt, |
f552d900 | 430 | OSSL_KEYMGMT_SELECT_ALL); |
e683582b SL |
431 | return NULL; |
432 | } |