]>
Commit | Line | Data |
---|---|---|
0abae163 | 1 | /* |
4333b89f | 2 | * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. |
0abae163 RL |
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 | ||
5b5eea4b SL |
10 | /* |
11 | * Low level APIs related to EC_KEY are deprecated for public use, | |
12 | * but still ok for internal use. | |
13 | */ | |
14 | #include "internal/deprecated.h" | |
15 | ||
0abae163 RL |
16 | #include <openssl/core_names.h> |
17 | #include <openssl/objects.h> | |
18 | #include <openssl/params.h> | |
c0f39ded | 19 | #include <openssl/err.h> |
b4f447c0 | 20 | #include <openssl/engine.h> |
0abae163 RL |
21 | #include "crypto/bn.h" |
22 | #include "crypto/ec.h" | |
c0f39ded SL |
23 | #include "ec_local.h" |
24 | #include "e_os.h" | |
25 | #include "internal/param_build_set.h" | |
26 | ||
27 | /* Mapping between a flag and a name */ | |
28 | static const OSSL_ITEM encoding_nameid_map[] = { | |
29 | { OPENSSL_EC_EXPLICIT_CURVE, OSSL_PKEY_EC_ENCODING_EXPLICIT }, | |
30 | { OPENSSL_EC_NAMED_CURVE, OSSL_PKEY_EC_ENCODING_GROUP }, | |
31 | }; | |
32 | ||
5b5eea4b SL |
33 | static const OSSL_ITEM check_group_type_nameid_map[] = { |
34 | { 0, OSSL_PKEY_EC_GROUP_CHECK_DEFAULT }, | |
35 | { EC_FLAG_CHECK_NAMED_GROUP, OSSL_PKEY_EC_GROUP_CHECK_NAMED }, | |
36 | { EC_FLAG_CHECK_NAMED_GROUP_NIST, OSSL_PKEY_EC_GROUP_CHECK_NAMED_NIST }, | |
37 | }; | |
38 | ||
39 | static const OSSL_ITEM format_nameid_map[] = { | |
40 | { (int)POINT_CONVERSION_UNCOMPRESSED, OSSL_PKEY_EC_POINT_CONVERSION_FORMAT_UNCOMPRESSED }, | |
41 | { (int)POINT_CONVERSION_COMPRESSED, OSSL_PKEY_EC_POINT_CONVERSION_FORMAT_COMPRESSED }, | |
42 | { (int)POINT_CONVERSION_HYBRID, OSSL_PKEY_EC_POINT_CONVERSION_FORMAT_HYBRID }, | |
43 | }; | |
44 | ||
32ab57cb | 45 | int ossl_ec_encoding_name2id(const char *name) |
c0f39ded SL |
46 | { |
47 | size_t i, sz; | |
48 | ||
49 | /* Return the default value if there is no name */ | |
50 | if (name == NULL) | |
51 | return OPENSSL_EC_NAMED_CURVE; | |
52 | ||
53 | for (i = 0, sz = OSSL_NELEM(encoding_nameid_map); i < sz; i++) { | |
54 | if (strcasecmp(name, encoding_nameid_map[i].ptr) == 0) | |
55 | return encoding_nameid_map[i].id; | |
56 | } | |
57 | return -1; | |
58 | } | |
59 | ||
60 | static char *ec_param_encoding_id2name(int id) | |
61 | { | |
62 | size_t i, sz; | |
63 | ||
64 | for (i = 0, sz = OSSL_NELEM(encoding_nameid_map); i < sz; i++) { | |
65 | if (id == (int)encoding_nameid_map[i].id) | |
66 | return encoding_nameid_map[i].ptr; | |
67 | } | |
68 | return NULL; | |
69 | } | |
70 | ||
32ab57cb | 71 | char *ossl_ec_check_group_type_id2name(int id) |
5b5eea4b SL |
72 | { |
73 | size_t i, sz; | |
74 | ||
75 | for (i = 0, sz = OSSL_NELEM(check_group_type_nameid_map); i < sz; i++) { | |
76 | if (id == (int)check_group_type_nameid_map[i].id) | |
77 | return check_group_type_nameid_map[i].ptr; | |
78 | } | |
79 | return NULL; | |
80 | } | |
81 | ||
82 | static int ec_check_group_type_name2id(const char *name) | |
83 | { | |
84 | size_t i, sz; | |
85 | ||
86 | /* Return the default value if there is no name */ | |
87 | if (name == NULL) | |
88 | return 0; | |
89 | ||
90 | for (i = 0, sz = OSSL_NELEM(check_group_type_nameid_map); i < sz; i++) { | |
91 | if (strcasecmp(name, check_group_type_nameid_map[i].ptr) == 0) | |
92 | return check_group_type_nameid_map[i].id; | |
93 | } | |
94 | return -1; | |
95 | } | |
96 | ||
32ab57cb | 97 | int ossl_ec_set_check_group_type_from_name(EC_KEY *ec, const char *name) |
5b5eea4b SL |
98 | { |
99 | int flags = ec_check_group_type_name2id(name); | |
100 | ||
101 | if (flags == -1) | |
102 | return 0; | |
103 | EC_KEY_clear_flags(ec, EC_FLAG_CHECK_NAMED_GROUP_MASK); | |
104 | EC_KEY_set_flags(ec, flags); | |
105 | return 1; | |
106 | } | |
107 | ||
108 | static int ec_set_check_group_type_from_param(EC_KEY *ec, const OSSL_PARAM *p) | |
109 | { | |
110 | const char *name = NULL; | |
111 | int status = 0; | |
112 | ||
113 | switch (p->data_type) { | |
114 | case OSSL_PARAM_UTF8_STRING: | |
115 | name = p->data; | |
116 | status = (name != NULL); | |
117 | break; | |
118 | case OSSL_PARAM_UTF8_PTR: | |
119 | status = OSSL_PARAM_get_utf8_ptr(p, &name); | |
120 | break; | |
121 | } | |
122 | if (status) | |
32ab57cb | 123 | return ossl_ec_set_check_group_type_from_name(ec, name); |
5b5eea4b SL |
124 | return 0; |
125 | } | |
126 | ||
32ab57cb | 127 | int ossl_ec_pt_format_name2id(const char *name) |
5b5eea4b SL |
128 | { |
129 | size_t i, sz; | |
130 | ||
131 | /* Return the default value if there is no name */ | |
132 | if (name == NULL) | |
133 | return (int)POINT_CONVERSION_UNCOMPRESSED; | |
134 | ||
135 | for (i = 0, sz = OSSL_NELEM(format_nameid_map); i < sz; i++) { | |
136 | if (strcasecmp(name, format_nameid_map[i].ptr) == 0) | |
137 | return format_nameid_map[i].id; | |
138 | } | |
139 | return -1; | |
140 | } | |
141 | ||
32ab57cb | 142 | char *ossl_ec_pt_format_id2name(int id) |
5b5eea4b SL |
143 | { |
144 | size_t i, sz; | |
145 | ||
146 | for (i = 0, sz = OSSL_NELEM(format_nameid_map); i < sz; i++) { | |
147 | if (id == (int)format_nameid_map[i].id) | |
148 | return format_nameid_map[i].ptr; | |
149 | } | |
150 | return NULL; | |
151 | } | |
152 | ||
f71a7453 JS |
153 | static int ec_group_explicit_todata(const EC_GROUP *group, OSSL_PARAM_BLD *tmpl, |
154 | OSSL_PARAM params[], BN_CTX *bnctx, | |
155 | unsigned char **genbuf) | |
c0f39ded | 156 | { |
f71a7453 JS |
157 | int ret = 0, fid; |
158 | const char *field_type; | |
159 | const OSSL_PARAM *param = NULL; | |
160 | const OSSL_PARAM *param_p = NULL; | |
161 | const OSSL_PARAM *param_a = NULL; | |
162 | const OSSL_PARAM *param_b = NULL; | |
163 | ||
164 | fid = EC_GROUP_get_field_type(group); | |
165 | ||
166 | if (fid == NID_X9_62_prime_field) { | |
167 | field_type = SN_X9_62_prime_field; | |
168 | } else if (fid == NID_X9_62_characteristic_two_field) { | |
169 | #ifdef OPENSSL_NO_EC2M | |
170 | ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED); | |
171 | goto err; | |
172 | #else | |
173 | field_type = SN_X9_62_characteristic_two_field; | |
174 | #endif | |
175 | } else { | |
176 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD); | |
c0f39ded SL |
177 | return 0; |
178 | } | |
179 | ||
f71a7453 JS |
180 | param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_P); |
181 | param_a = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_A); | |
182 | param_b = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_B); | |
183 | if (tmpl != NULL || param_p != NULL || param_a != NULL || param_b != NULL) | |
184 | { | |
185 | BIGNUM *p = BN_CTX_get(bnctx); | |
186 | BIGNUM *a = BN_CTX_get(bnctx); | |
187 | BIGNUM *b = BN_CTX_get(bnctx); | |
c0f39ded | 188 | |
c0f39ded | 189 | if (b == NULL) { |
9311d0c4 | 190 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
c0f39ded SL |
191 | goto err; |
192 | } | |
193 | ||
194 | if (!EC_GROUP_get_curve(group, p, a, b, bnctx)) { | |
9311d0c4 | 195 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE); |
c0f39ded SL |
196 | goto err; |
197 | } | |
f71a7453 JS |
198 | if (!ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_P, p) |
199 | || !ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_A, a) | |
200 | || !ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_B, b)) { | |
201 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); | |
202 | goto err; | |
203 | } | |
204 | } | |
205 | ||
206 | param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ORDER); | |
207 | if (tmpl != NULL || param != NULL) { | |
208 | const BIGNUM *order = EC_GROUP_get0_order(group); | |
c0f39ded | 209 | |
c0f39ded | 210 | if (order == NULL) { |
9311d0c4 | 211 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER); |
c0f39ded SL |
212 | goto err; |
213 | } | |
f71a7453 JS |
214 | if (!ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_ORDER, |
215 | order)) { | |
216 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); | |
217 | goto err; | |
218 | } | |
219 | } | |
220 | ||
221 | param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_FIELD_TYPE); | |
222 | if (tmpl != NULL || param != NULL) { | |
223 | if (!ossl_param_build_set_utf8_string(tmpl, params, | |
224 | OSSL_PKEY_PARAM_EC_FIELD_TYPE, | |
225 | field_type)) { | |
226 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); | |
227 | goto err; | |
228 | } | |
229 | } | |
230 | ||
231 | param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_GENERATOR); | |
232 | if (tmpl != NULL || param != NULL) { | |
233 | size_t genbuf_len; | |
234 | const EC_POINT *genpt = EC_GROUP_get0_generator(group); | |
235 | point_conversion_form_t genform = EC_GROUP_get_point_conversion_form(group); | |
236 | ||
c0f39ded | 237 | if (genpt == NULL) { |
9311d0c4 | 238 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR); |
c0f39ded SL |
239 | goto err; |
240 | } | |
c0f39ded SL |
241 | genbuf_len = EC_POINT_point2buf(group, genpt, genform, genbuf, bnctx); |
242 | if (genbuf_len == 0) { | |
9311d0c4 | 243 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR); |
c0f39ded SL |
244 | goto err; |
245 | } | |
f71a7453 JS |
246 | if (!ossl_param_build_set_octet_string(tmpl, params, |
247 | OSSL_PKEY_PARAM_EC_GENERATOR, | |
248 | *genbuf, genbuf_len)) { | |
9311d0c4 | 249 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
c0f39ded SL |
250 | goto err; |
251 | } | |
f71a7453 JS |
252 | } |
253 | ||
254 | param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_COFACTOR); | |
255 | if (tmpl != NULL || param != NULL) { | |
256 | const BIGNUM *cofactor = EC_GROUP_get0_cofactor(group); | |
c0f39ded | 257 | |
c0f39ded SL |
258 | if (cofactor != NULL |
259 | && !ossl_param_build_set_bn(tmpl, params, | |
260 | OSSL_PKEY_PARAM_EC_COFACTOR, cofactor)) { | |
9311d0c4 | 261 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
c0f39ded SL |
262 | goto err; |
263 | } | |
f71a7453 JS |
264 | } |
265 | ||
266 | param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_SEED); | |
267 | if (tmpl != NULL || param != NULL) { | |
268 | unsigned char *seed = EC_GROUP_get0_seed(group); | |
269 | size_t seed_len = EC_GROUP_get_seed_len(group); | |
c0f39ded | 270 | |
c0f39ded SL |
271 | if (seed != NULL |
272 | && seed_len > 0 | |
273 | && !ossl_param_build_set_octet_string(tmpl, params, | |
274 | OSSL_PKEY_PARAM_EC_SEED, | |
275 | seed, seed_len)) { | |
9311d0c4 | 276 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
c0f39ded SL |
277 | goto err; |
278 | } | |
f71a7453 JS |
279 | } |
280 | ret = 1; | |
281 | err: | |
282 | return ret; | |
283 | } | |
284 | ||
285 | int ossl_ec_group_todata(const EC_GROUP *group, OSSL_PARAM_BLD *tmpl, | |
286 | OSSL_PARAM params[], OSSL_LIB_CTX *libctx, | |
287 | const char *propq, | |
288 | BN_CTX *bnctx, unsigned char **genbuf) | |
289 | { | |
290 | int ret = 0, curve_nid, encoding_flag; | |
291 | const char *encoding_name, *pt_form_name; | |
292 | point_conversion_form_t genform; | |
293 | ||
294 | if (group == NULL) { | |
295 | ERR_raise(ERR_LIB_EC,EC_R_PASSED_NULL_PARAMETER); | |
296 | return 0; | |
297 | } | |
298 | ||
299 | genform = EC_GROUP_get_point_conversion_form(group); | |
300 | pt_form_name = ossl_ec_pt_format_id2name(genform); | |
301 | if (pt_form_name == NULL | |
302 | || !ossl_param_build_set_utf8_string( | |
303 | tmpl, params, | |
304 | OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, pt_form_name)) { | |
305 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM); | |
306 | return 0; | |
307 | } | |
308 | encoding_flag = EC_GROUP_get_asn1_flag(group) & OPENSSL_EC_NAMED_CURVE; | |
309 | encoding_name = ec_param_encoding_id2name(encoding_flag); | |
310 | if (encoding_name == NULL | |
311 | || !ossl_param_build_set_utf8_string(tmpl, params, | |
312 | OSSL_PKEY_PARAM_EC_ENCODING, | |
313 | encoding_name)) { | |
314 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING); | |
315 | return 0; | |
316 | } | |
317 | ||
318 | curve_nid = EC_GROUP_get_curve_name(group); | |
319 | ||
320 | /* | |
321 | * Get the explicit parameters in these two cases: | |
322 | * - We do not have a template, i.e. specific parameters are requested | |
323 | * - The curve is not a named curve | |
324 | */ | |
325 | if (tmpl == NULL || curve_nid == NID_undef) | |
326 | if (!ec_group_explicit_todata(group, tmpl, params, bnctx, genbuf)) | |
c0f39ded | 327 | goto err; |
f71a7453 JS |
328 | |
329 | if (curve_nid != NID_undef) { | |
330 | /* Named curve */ | |
f9253152 | 331 | const char *curve_name = OSSL_EC_curve_nid2name(curve_nid); |
c0f39ded SL |
332 | |
333 | if (curve_name == NULL | |
334 | || !ossl_param_build_set_utf8_string(tmpl, params, | |
335 | OSSL_PKEY_PARAM_GROUP_NAME, | |
336 | curve_name)) { | |
9311d0c4 | 337 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE); |
c0f39ded SL |
338 | goto err; |
339 | } | |
340 | } | |
341 | ret = 1; | |
342 | err: | |
343 | return ret; | |
344 | } | |
0abae163 RL |
345 | |
346 | /* | |
347 | * The intention with the "backend" source file is to offer backend support | |
348 | * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider | |
349 | * implementations alike. | |
350 | */ | |
32ab57cb | 351 | int ossl_ec_set_ecdh_cofactor_mode(EC_KEY *ec, int mode) |
0abae163 RL |
352 | { |
353 | const EC_GROUP *ecg = EC_KEY_get0_group(ec); | |
354 | const BIGNUM *cofactor; | |
0abae163 RL |
355 | /* |
356 | * mode can be only 0 for disable, or 1 for enable here. | |
357 | * | |
358 | * This is in contrast with the same parameter on an ECDH EVP_PKEY_CTX that | |
359 | * also supports mode == -1 with the meaning of "reset to the default for | |
360 | * the associated key". | |
361 | */ | |
362 | if (mode < 0 || mode > 1) | |
363 | return 0; | |
364 | ||
365 | if ((cofactor = EC_GROUP_get0_cofactor(ecg)) == NULL ) | |
366 | return 0; | |
367 | ||
368 | /* ECDH cofactor mode has no effect if cofactor is 1 */ | |
369 | if (BN_is_one(cofactor)) | |
370 | return 1; | |
371 | ||
372 | if (mode == 1) | |
373 | EC_KEY_set_flags(ec, EC_FLAG_COFACTOR_ECDH); | |
374 | else if (mode == 0) | |
375 | EC_KEY_clear_flags(ec, EC_FLAG_COFACTOR_ECDH); | |
376 | ||
377 | return 1; | |
378 | } | |
379 | ||
380 | /* | |
32ab57cb | 381 | * Callers of ossl_ec_key_fromdata MUST make sure that ec_key_params_fromdata has |
0abae163 RL |
382 | * been called before! |
383 | * | |
384 | * This function only gets the bare keypair, domain parameters and other | |
385 | * parameters are treated separately, and domain parameters are required to | |
386 | * define a keypair. | |
387 | */ | |
32ab57cb | 388 | int ossl_ec_key_fromdata(EC_KEY *ec, const OSSL_PARAM params[], int include_private) |
0abae163 | 389 | { |
9e2c0358 | 390 | const OSSL_PARAM *param_priv_key = NULL, *param_pub_key = NULL; |
0abae163 RL |
391 | BN_CTX *ctx = NULL; |
392 | BIGNUM *priv_key = NULL; | |
393 | unsigned char *pub_key = NULL; | |
394 | size_t pub_key_len; | |
395 | const EC_GROUP *ecg = NULL; | |
396 | EC_POINT *pub_point = NULL; | |
397 | int ok = 0; | |
398 | ||
399 | ecg = EC_KEY_get0_group(ec); | |
400 | if (ecg == NULL) | |
401 | return 0; | |
402 | ||
0abae163 RL |
403 | param_pub_key = |
404 | OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY); | |
9e2c0358 RL |
405 | if (include_private) |
406 | param_priv_key = | |
407 | OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY); | |
0abae163 | 408 | |
32ab57cb | 409 | ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(ec)); |
0abae163 RL |
410 | if (ctx == NULL) |
411 | goto err; | |
9e2c0358 | 412 | |
9e2c0358 RL |
413 | if (param_pub_key != NULL) |
414 | if (!OSSL_PARAM_get_octet_string(param_pub_key, | |
415 | (void **)&pub_key, 0, &pub_key_len) | |
0abae163 | 416 | || (pub_point = EC_POINT_new(ecg)) == NULL |
9e2c0358 | 417 | || !EC_POINT_oct2point(ecg, pub_point, pub_key, pub_key_len, ctx)) |
0abae163 RL |
418 | goto err; |
419 | ||
420 | if (param_priv_key != NULL && include_private) { | |
421 | int fixed_words; | |
422 | const BIGNUM *order; | |
423 | ||
424 | /* | |
425 | * Key import/export should never leak the bit length of the secret | |
426 | * scalar in the key. | |
427 | * | |
428 | * For this reason, on export we use padded BIGNUMs with fixed length. | |
429 | * | |
430 | * When importing we also should make sure that, even if short lived, | |
431 | * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as | |
432 | * soon as possible, so that any processing of this BIGNUM might opt for | |
433 | * constant time implementations in the backend. | |
434 | * | |
435 | * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have | |
436 | * to preallocate the BIGNUM internal buffer to a fixed public size big | |
437 | * enough that operations performed during the processing never trigger | |
438 | * a realloc which would leak the size of the scalar through memory | |
439 | * accesses. | |
440 | * | |
441 | * Fixed Length | |
442 | * ------------ | |
443 | * | |
444 | * The order of the large prime subgroup of the curve is our choice for | |
445 | * a fixed public size, as that is generally the upper bound for | |
446 | * generating a private key in EC cryptosystems and should fit all valid | |
447 | * secret scalars. | |
448 | * | |
449 | * For padding on export we just use the bit length of the order | |
450 | * converted to bytes (rounding up). | |
451 | * | |
452 | * For preallocating the BIGNUM storage we look at the number of "words" | |
453 | * required for the internal representation of the order, and we | |
454 | * preallocate 2 extra "words" in case any of the subsequent processing | |
455 | * might temporarily overflow the order length. | |
456 | */ | |
457 | order = EC_GROUP_get0_order(ecg); | |
458 | if (order == NULL || BN_is_zero(order)) | |
459 | goto err; | |
460 | ||
461 | fixed_words = bn_get_top(order) + 2; | |
462 | ||
463 | if ((priv_key = BN_secure_new()) == NULL) | |
464 | goto err; | |
465 | if (bn_wexpand(priv_key, fixed_words) == NULL) | |
466 | goto err; | |
467 | BN_set_flags(priv_key, BN_FLG_CONSTTIME); | |
468 | ||
469 | if (!OSSL_PARAM_get_BN(param_priv_key, &priv_key)) | |
470 | goto err; | |
471 | } | |
472 | ||
473 | if (priv_key != NULL | |
9e2c0358 | 474 | && !EC_KEY_set_private_key(ec, priv_key)) |
0abae163 RL |
475 | goto err; |
476 | ||
9e2c0358 RL |
477 | if (pub_point != NULL |
478 | && !EC_KEY_set_public_key(ec, pub_point)) | |
0abae163 RL |
479 | goto err; |
480 | ||
481 | ok = 1; | |
482 | ||
483 | err: | |
484 | BN_CTX_free(ctx); | |
485 | BN_clear_free(priv_key); | |
486 | OPENSSL_free(pub_key); | |
487 | EC_POINT_free(pub_point); | |
488 | return ok; | |
489 | } | |
490 | ||
32ab57cb | 491 | int ossl_ec_group_fromdata(EC_KEY *ec, const OSSL_PARAM params[]) |
0abae163 | 492 | { |
0abae163 | 493 | int ok = 0; |
c0f39ded | 494 | EC_GROUP *group = NULL; |
0abae163 RL |
495 | |
496 | if (ec == NULL) | |
497 | return 0; | |
498 | ||
32ab57cb SL |
499 | group = EC_GROUP_new_from_params(params, ossl_ec_key_get_libctx(ec), |
500 | ossl_ec_key_get0_propq(ec)); | |
0abae163 | 501 | |
c0f39ded | 502 | if (!EC_KEY_set_group(ec, group)) |
0abae163 | 503 | goto err; |
0abae163 | 504 | ok = 1; |
c0f39ded SL |
505 | err: |
506 | EC_GROUP_free(group); | |
0abae163 RL |
507 | return ok; |
508 | } | |
509 | ||
5b5eea4b SL |
510 | static int ec_key_point_format_fromdata(EC_KEY *ec, const OSSL_PARAM params[]) |
511 | { | |
512 | const OSSL_PARAM *p; | |
513 | int format = -1; | |
514 | ||
515 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT); | |
516 | if (p != NULL) { | |
32ab57cb | 517 | if (!ossl_ec_pt_format_param2id(p, &format)) { |
5b5eea4b SL |
518 | ECerr(0, EC_R_INVALID_FORM); |
519 | return 0; | |
520 | } | |
521 | EC_KEY_set_conv_form(ec, format); | |
522 | } | |
523 | return 1; | |
524 | } | |
525 | ||
526 | static int ec_key_group_check_fromdata(EC_KEY *ec, const OSSL_PARAM params[]) | |
527 | { | |
528 | const OSSL_PARAM *p; | |
529 | ||
530 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE); | |
531 | if (p != NULL) | |
532 | return ec_set_check_group_type_from_param(ec, p); | |
533 | return 1; | |
534 | } | |
535 | ||
536 | static int ec_set_include_public(EC_KEY *ec, int include) | |
537 | { | |
538 | int flags = EC_KEY_get_enc_flags(ec); | |
539 | ||
540 | if (!include) | |
541 | flags |= EC_PKEY_NO_PUBKEY; | |
542 | else | |
543 | flags &= ~EC_PKEY_NO_PUBKEY; | |
544 | EC_KEY_set_enc_flags(ec, flags); | |
545 | return 1; | |
546 | } | |
547 | ||
32ab57cb | 548 | int ossl_ec_key_otherparams_fromdata(EC_KEY *ec, const OSSL_PARAM params[]) |
0abae163 RL |
549 | { |
550 | const OSSL_PARAM *p; | |
551 | ||
552 | if (ec == NULL) | |
553 | return 0; | |
554 | ||
555 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH); | |
b8086652 SL |
556 | if (p != NULL) { |
557 | int mode; | |
0abae163 | 558 | |
b8086652 | 559 | if (!OSSL_PARAM_get_int(p, &mode) |
32ab57cb | 560 | || !ossl_ec_set_ecdh_cofactor_mode(ec, mode)) |
b8086652 SL |
561 | return 0; |
562 | } | |
c0f39ded | 563 | |
5b5eea4b SL |
564 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC); |
565 | if (p != NULL) { | |
566 | int include = 1; | |
567 | ||
568 | if (!OSSL_PARAM_get_int(p, &include) | |
569 | || !ec_set_include_public(ec, include)) | |
570 | return 0; | |
571 | } | |
572 | if (!ec_key_point_format_fromdata(ec, params)) | |
573 | return 0; | |
574 | if (!ec_key_group_check_fromdata(ec, params)) | |
575 | return 0; | |
0abae163 RL |
576 | return 1; |
577 | } | |
5b5eea4b | 578 | |
b247113c TM |
579 | int ossl_ec_key_is_foreign(const EC_KEY *ec) |
580 | { | |
581 | #ifndef FIPS_MODULE | |
582 | if (ec->engine != NULL || EC_KEY_get_method(ec) != EC_KEY_OpenSSL()) | |
583 | return 1; | |
584 | #endif | |
585 | return 0; | |
586 | ||
587 | } | |
588 | ||
b4f447c0 TM |
589 | EC_KEY *ossl_ec_key_dup(const EC_KEY *src, int selection) |
590 | { | |
db6b1266 | 591 | EC_KEY *ret; |
b4f447c0 TM |
592 | |
593 | if (src == NULL) { | |
594 | ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); | |
db6b1266 | 595 | return NULL; |
b4f447c0 TM |
596 | } |
597 | ||
db6b1266 TM |
598 | if ((ret = ossl_ec_key_new_method_int(src->libctx, src->propq, |
599 | src->engine)) == NULL) | |
600 | return NULL; | |
601 | ||
b4f447c0 TM |
602 | /* copy the parameters */ |
603 | if (src->group != NULL | |
604 | && (selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) { | |
605 | ret->group = ossl_ec_group_new_ex(src->libctx, src->propq, | |
606 | src->group->meth); | |
607 | if (ret->group == NULL | |
608 | || !EC_GROUP_copy(ret->group, src->group)) | |
609 | goto err; | |
610 | ||
611 | if (src->meth != NULL) { | |
612 | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) | |
613 | if (src->engine != NULL && ENGINE_init(src->engine) == 0) | |
614 | goto err; | |
615 | ret->engine = src->engine; | |
616 | #endif | |
617 | ret->meth = src->meth; | |
618 | } | |
619 | } | |
620 | ||
621 | /* copy the public key */ | |
622 | if (src->pub_key != NULL | |
623 | && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) { | |
624 | if (ret->group == NULL) | |
625 | /* no parameter-less keys allowed */ | |
626 | goto err; | |
627 | ret->pub_key = EC_POINT_new(ret->group); | |
628 | if (ret->pub_key == NULL | |
629 | || !EC_POINT_copy(ret->pub_key, src->pub_key)) | |
630 | goto err; | |
631 | } | |
632 | ||
633 | /* copy the private key */ | |
634 | if (src->priv_key != NULL | |
635 | && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { | |
636 | if (ret->group == NULL) | |
637 | /* no parameter-less keys allowed */ | |
638 | goto err; | |
639 | ret->priv_key = BN_new(); | |
640 | if (ret->priv_key == NULL || !BN_copy(ret->priv_key, src->priv_key)) | |
641 | goto err; | |
642 | if (ret->group->meth->keycopy | |
643 | && ret->group->meth->keycopy(ret, src) == 0) | |
644 | goto err; | |
645 | } | |
646 | ||
647 | /* copy the rest */ | |
648 | if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) { | |
649 | ret->enc_flag = src->enc_flag; | |
650 | ret->conv_form = src->conv_form; | |
651 | } | |
652 | ||
653 | ret->version = src->version; | |
654 | ret->flags = src->flags; | |
655 | ||
656 | #ifndef FIPS_MODULE | |
657 | if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY, | |
658 | &ret->ex_data, &src->ex_data)) | |
659 | goto err; | |
660 | #endif | |
661 | ||
662 | if (ret->meth != NULL && ret->meth->copy != NULL) { | |
663 | if ((selection | |
664 | & OSSL_KEYMGMT_SELECT_KEYPAIR) != OSSL_KEYMGMT_SELECT_KEYPAIR) | |
665 | goto err; | |
666 | if (ret->meth->copy(ret, src) == 0) | |
667 | goto err; | |
668 | } | |
669 | ||
670 | return ret; | |
671 | err: | |
672 | EC_KEY_free(ret); | |
673 | return NULL; | |
674 | } | |
675 | ||
32ab57cb | 676 | int ossl_ec_encoding_param2id(const OSSL_PARAM *p, int *id) |
5b5eea4b SL |
677 | { |
678 | const char *name = NULL; | |
679 | int status = 0; | |
680 | ||
681 | switch (p->data_type) { | |
682 | case OSSL_PARAM_UTF8_STRING: | |
683 | /* The OSSL_PARAM functions have no support for this */ | |
684 | name = p->data; | |
685 | status = (name != NULL); | |
686 | break; | |
687 | case OSSL_PARAM_UTF8_PTR: | |
688 | status = OSSL_PARAM_get_utf8_ptr(p, &name); | |
689 | break; | |
690 | } | |
691 | if (status) { | |
32ab57cb | 692 | int i = ossl_ec_encoding_name2id(name); |
5b5eea4b SL |
693 | |
694 | if (i >= 0) { | |
695 | *id = i; | |
696 | return 1; | |
697 | } | |
698 | } | |
699 | return 0; | |
700 | } | |
701 | ||
32ab57cb | 702 | int ossl_ec_pt_format_param2id(const OSSL_PARAM *p, int *id) |
5b5eea4b SL |
703 | { |
704 | const char *name = NULL; | |
705 | int status = 0; | |
706 | ||
707 | switch (p->data_type) { | |
708 | case OSSL_PARAM_UTF8_STRING: | |
709 | /* The OSSL_PARAM functions have no support for this */ | |
710 | name = p->data; | |
711 | status = (name != NULL); | |
712 | break; | |
713 | case OSSL_PARAM_UTF8_PTR: | |
714 | status = OSSL_PARAM_get_utf8_ptr(p, &name); | |
715 | break; | |
716 | } | |
717 | if (status) { | |
32ab57cb | 718 | int i = ossl_ec_pt_format_name2id(name); |
5b5eea4b SL |
719 | |
720 | if (i >= 0) { | |
721 | *id = i; | |
722 | return 1; | |
723 | } | |
724 | } | |
725 | return 0; | |
726 | } | |
cf333799 RL |
727 | |
728 | #ifndef FIPS_MODULE | |
8c7c1c84 MC |
729 | int ossl_x509_algor_is_sm2(const X509_ALGOR *palg) |
730 | { | |
731 | int ptype = 0; | |
732 | const void *pval = NULL; | |
733 | ||
734 | X509_ALGOR_get0(NULL, &ptype, &pval, palg); | |
735 | ||
736 | if (ptype == V_ASN1_OBJECT) | |
737 | return OBJ_obj2nid((ASN1_OBJECT *)pval) == NID_sm2; | |
738 | ||
739 | if (ptype == V_ASN1_SEQUENCE) { | |
740 | const ASN1_STRING *str = pval; | |
741 | const unsigned char *der = str->data; | |
742 | int derlen = str->length; | |
743 | EC_GROUP *group; | |
744 | int ret; | |
745 | ||
746 | if ((group = d2i_ECPKParameters(NULL, &der, derlen)) == NULL) | |
747 | ret = 0; | |
748 | else | |
749 | ret = (EC_GROUP_get_curve_name(group) == NID_sm2); | |
750 | ||
751 | EC_GROUP_free(group); | |
752 | return ret; | |
753 | } | |
754 | ||
755 | return 0; | |
756 | } | |
757 | ||
cf333799 RL |
758 | EC_KEY *ossl_ec_key_param_from_x509_algor(const X509_ALGOR *palg, |
759 | OSSL_LIB_CTX *libctx, const char *propq) | |
760 | { | |
761 | int ptype = 0; | |
762 | const void *pval = NULL; | |
763 | EC_KEY *eckey = NULL; | |
764 | EC_GROUP *group = NULL; | |
765 | ||
766 | X509_ALGOR_get0(NULL, &ptype, &pval, palg); | |
767 | if ((eckey = EC_KEY_new_ex(libctx, propq)) == NULL) { | |
768 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); | |
769 | goto ecerr; | |
770 | } | |
771 | ||
772 | if (ptype == V_ASN1_SEQUENCE) { | |
773 | const ASN1_STRING *pstr = pval; | |
774 | const unsigned char *pm = pstr->data; | |
775 | int pmlen = pstr->length; | |
776 | ||
777 | ||
778 | if (d2i_ECParameters(&eckey, &pm, pmlen) == NULL) { | |
779 | ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR); | |
780 | goto ecerr; | |
781 | } | |
782 | } else if (ptype == V_ASN1_OBJECT) { | |
783 | const ASN1_OBJECT *poid = pval; | |
784 | ||
785 | /* | |
786 | * type == V_ASN1_OBJECT => the parameters are given by an asn1 OID | |
787 | */ | |
788 | ||
789 | group = EC_GROUP_new_by_curve_name_ex(libctx, propq, OBJ_obj2nid(poid)); | |
790 | if (group == NULL) | |
791 | goto ecerr; | |
792 | EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE); | |
793 | if (EC_KEY_set_group(eckey, group) == 0) | |
794 | goto ecerr; | |
795 | EC_GROUP_free(group); | |
796 | } else { | |
797 | ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR); | |
798 | goto ecerr; | |
799 | } | |
800 | ||
801 | return eckey; | |
802 | ||
803 | ecerr: | |
804 | EC_KEY_free(eckey); | |
805 | EC_GROUP_free(group); | |
806 | return NULL; | |
807 | } | |
808 | ||
809 | EC_KEY *ossl_ec_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf, | |
810 | OSSL_LIB_CTX *libctx, const char *propq) | |
811 | { | |
812 | const unsigned char *p = NULL; | |
813 | int pklen; | |
814 | EC_KEY *eckey = NULL; | |
815 | const X509_ALGOR *palg; | |
816 | ||
817 | if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8inf)) | |
818 | return 0; | |
819 | eckey = ossl_ec_key_param_from_x509_algor(palg, libctx, propq); | |
820 | if (eckey == NULL) | |
821 | goto err; | |
822 | ||
823 | /* We have parameters now set private key */ | |
824 | if (!d2i_ECPrivateKey(&eckey, &p, pklen)) { | |
825 | ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR); | |
826 | goto err; | |
827 | } | |
828 | ||
829 | return eckey; | |
830 | err: | |
831 | EC_KEY_free(eckey); | |
832 | return NULL; | |
833 | } | |
834 | #endif |