/src/openssl30/crypto/ec/ec_asn1.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2002-2022 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 | | /* |
11 | | * ECDSA low level APIs are deprecated for public use, but still ok for |
12 | | * internal use. |
13 | | */ |
14 | | #include "internal/deprecated.h" |
15 | | |
16 | | #include <string.h> |
17 | | #include "ec_local.h" |
18 | | #include <openssl/err.h> |
19 | | #include <openssl/asn1t.h> |
20 | | #include <openssl/objects.h> |
21 | | #include "internal/nelem.h" |
22 | | #include "crypto/asn1_dsa.h" |
23 | | |
24 | | #ifndef FIPS_MODULE |
25 | | |
26 | | /* some structures needed for the asn1 encoding */ |
27 | | typedef struct x9_62_pentanomial_st { |
28 | | int32_t k1; |
29 | | int32_t k2; |
30 | | int32_t k3; |
31 | | } X9_62_PENTANOMIAL; |
32 | | |
33 | | typedef struct x9_62_characteristic_two_st { |
34 | | int32_t m; |
35 | | ASN1_OBJECT *type; |
36 | | union { |
37 | | char *ptr; |
38 | | /* NID_X9_62_onBasis */ |
39 | | ASN1_NULL *onBasis; |
40 | | /* NID_X9_62_tpBasis */ |
41 | | ASN1_INTEGER *tpBasis; |
42 | | /* NID_X9_62_ppBasis */ |
43 | | X9_62_PENTANOMIAL *ppBasis; |
44 | | /* anything else */ |
45 | | ASN1_TYPE *other; |
46 | | } p; |
47 | | } X9_62_CHARACTERISTIC_TWO; |
48 | | |
49 | | typedef struct x9_62_fieldid_st { |
50 | | ASN1_OBJECT *fieldType; |
51 | | union { |
52 | | char *ptr; |
53 | | /* NID_X9_62_prime_field */ |
54 | | ASN1_INTEGER *prime; |
55 | | /* NID_X9_62_characteristic_two_field */ |
56 | | X9_62_CHARACTERISTIC_TWO *char_two; |
57 | | /* anything else */ |
58 | | ASN1_TYPE *other; |
59 | | } p; |
60 | | } X9_62_FIELDID; |
61 | | |
62 | | typedef struct x9_62_curve_st { |
63 | | ASN1_OCTET_STRING *a; |
64 | | ASN1_OCTET_STRING *b; |
65 | | ASN1_BIT_STRING *seed; |
66 | | } X9_62_CURVE; |
67 | | |
68 | | struct ec_parameters_st { |
69 | | int32_t version; |
70 | | X9_62_FIELDID *fieldID; |
71 | | X9_62_CURVE *curve; |
72 | | ASN1_OCTET_STRING *base; |
73 | | ASN1_INTEGER *order; |
74 | | ASN1_INTEGER *cofactor; |
75 | | } /* ECPARAMETERS */; |
76 | | |
77 | | typedef enum { |
78 | | ECPKPARAMETERS_TYPE_NAMED = 0, |
79 | | ECPKPARAMETERS_TYPE_EXPLICIT, |
80 | | ECPKPARAMETERS_TYPE_IMPLICIT |
81 | | } ecpk_parameters_type_t; |
82 | | |
83 | | struct ecpk_parameters_st { |
84 | | int type; |
85 | | union { |
86 | | ASN1_OBJECT *named_curve; |
87 | | ECPARAMETERS *parameters; |
88 | | ASN1_NULL *implicitlyCA; |
89 | | } value; |
90 | | } /* ECPKPARAMETERS */; |
91 | | |
92 | | /* SEC1 ECPrivateKey */ |
93 | | typedef struct ec_privatekey_st { |
94 | | int32_t version; |
95 | | ASN1_OCTET_STRING *privateKey; |
96 | | ECPKPARAMETERS *parameters; |
97 | | ASN1_BIT_STRING *publicKey; |
98 | | } EC_PRIVATEKEY; |
99 | | |
100 | | /* the OpenSSL ASN.1 definitions */ |
101 | | ASN1_SEQUENCE(X9_62_PENTANOMIAL) = { |
102 | | ASN1_EMBED(X9_62_PENTANOMIAL, k1, INT32), |
103 | | ASN1_EMBED(X9_62_PENTANOMIAL, k2, INT32), |
104 | | ASN1_EMBED(X9_62_PENTANOMIAL, k3, INT32) |
105 | 352 | } static_ASN1_SEQUENCE_END(X9_62_PENTANOMIAL) |
106 | 352 | |
107 | 352 | DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL) |
108 | 352 | IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL) |
109 | 352 | |
110 | 352 | ASN1_ADB_TEMPLATE(char_two_def) = ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.other, ASN1_ANY); |
111 | 352 | |
112 | 352 | ASN1_ADB(X9_62_CHARACTERISTIC_TWO) = { |
113 | 352 | ADB_ENTRY(NID_X9_62_onBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.onBasis, ASN1_NULL)), |
114 | 352 | ADB_ENTRY(NID_X9_62_tpBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.tpBasis, ASN1_INTEGER)), |
115 | 352 | ADB_ENTRY(NID_X9_62_ppBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.ppBasis, X9_62_PENTANOMIAL)) |
116 | 5.99k | } ASN1_ADB_END(X9_62_CHARACTERISTIC_TWO, 0, type, 0, &char_two_def_tt, NULL); |
117 | | |
118 | | ASN1_SEQUENCE(X9_62_CHARACTERISTIC_TWO) = { |
119 | | ASN1_EMBED(X9_62_CHARACTERISTIC_TWO, m, INT32), |
120 | | ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, type, ASN1_OBJECT), |
121 | | ASN1_ADB_OBJECT(X9_62_CHARACTERISTIC_TWO) |
122 | 4.06k | } static_ASN1_SEQUENCE_END(X9_62_CHARACTERISTIC_TWO) |
123 | 4.06k | |
124 | 4.06k | DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO) |
125 | 4.06k | IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO) |
126 | 4.06k | |
127 | 4.06k | ASN1_ADB_TEMPLATE(fieldID_def) = ASN1_SIMPLE(X9_62_FIELDID, p.other, ASN1_ANY); |
128 | 4.06k | |
129 | 4.06k | ASN1_ADB(X9_62_FIELDID) = { |
130 | 4.06k | ADB_ENTRY(NID_X9_62_prime_field, ASN1_SIMPLE(X9_62_FIELDID, p.prime, ASN1_INTEGER)), |
131 | 4.06k | ADB_ENTRY(NID_X9_62_characteristic_two_field, ASN1_SIMPLE(X9_62_FIELDID, p.char_two, X9_62_CHARACTERISTIC_TWO)) |
132 | 866k | } ASN1_ADB_END(X9_62_FIELDID, 0, fieldType, 0, &fieldID_def_tt, NULL); |
133 | | |
134 | | ASN1_SEQUENCE(X9_62_FIELDID) = { |
135 | | ASN1_SIMPLE(X9_62_FIELDID, fieldType, ASN1_OBJECT), |
136 | | ASN1_ADB_OBJECT(X9_62_FIELDID) |
137 | 877k | } static_ASN1_SEQUENCE_END(X9_62_FIELDID) |
138 | 877k | |
139 | 877k | ASN1_SEQUENCE(X9_62_CURVE) |
140 | 877k | = { ASN1_SIMPLE(X9_62_CURVE, a, ASN1_OCTET_STRING), ASN1_SIMPLE(X9_62_CURVE, b, ASN1_OCTET_STRING), ASN1_OPT(X9_62_CURVE, seed, ASN1_BIT_STRING) } static_ASN1_SEQUENCE_END(X9_62_CURVE) |
141 | 744k | |
142 | 744k | ASN1_SEQUENCE(ECPARAMETERS) |
143 | 744k | = { ASN1_EMBED(ECPARAMETERS, version, INT32), ASN1_SIMPLE(ECPARAMETERS, fieldID, X9_62_FIELDID), ASN1_SIMPLE(ECPARAMETERS, curve, X9_62_CURVE), ASN1_SIMPLE(ECPARAMETERS, base, ASN1_OCTET_STRING), ASN1_SIMPLE(ECPARAMETERS, order, ASN1_INTEGER), ASN1_OPT(ECPARAMETERS, cofactor, ASN1_INTEGER) } ASN1_SEQUENCE_END(ECPARAMETERS) |
144 | 694k | |
145 | 694k | DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS) |
146 | 694k | IMPLEMENT_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS) |
147 | 694k | |
148 | 694k | ASN1_CHOICE(ECPKPARAMETERS) = { |
149 | 694k | ASN1_SIMPLE(ECPKPARAMETERS, value.named_curve, ASN1_OBJECT), |
150 | 694k | ASN1_SIMPLE(ECPKPARAMETERS, value.parameters, ECPARAMETERS), |
151 | 694k | ASN1_SIMPLE(ECPKPARAMETERS, value.implicitlyCA, ASN1_NULL) |
152 | 3.36M | } ASN1_CHOICE_END(ECPKPARAMETERS) |
153 | 3.36M | |
154 | 3.36M | DECLARE_ASN1_FUNCTIONS(ECPKPARAMETERS) |
155 | 3.36M | DECLARE_ASN1_ENCODE_FUNCTIONS_name(ECPKPARAMETERS, ECPKPARAMETERS) |
156 | 3.36M | IMPLEMENT_ASN1_FUNCTIONS(ECPKPARAMETERS) |
157 | 3.36M | |
158 | 3.36M | ASN1_SEQUENCE(EC_PRIVATEKEY) = { |
159 | 3.36M | ASN1_EMBED(EC_PRIVATEKEY, version, INT32), |
160 | 3.36M | ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING), |
161 | 3.36M | ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0), |
162 | 3.36M | ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1) |
163 | 3.36M | } static_ASN1_SEQUENCE_END(EC_PRIVATEKEY) |
164 | 499k | |
165 | 499k | DECLARE_ASN1_FUNCTIONS(EC_PRIVATEKEY) |
166 | 499k | DECLARE_ASN1_ENCODE_FUNCTIONS_name(EC_PRIVATEKEY, EC_PRIVATEKEY) |
167 | 499k | IMPLEMENT_ASN1_FUNCTIONS(EC_PRIVATEKEY) |
168 | 499k | |
169 | 499k | /* some declarations of internal function */ |
170 | 499k | |
171 | 499k | /* ec_asn1_group2field() sets the values in a X9_62_FIELDID object */ |
172 | 499k | static int ec_asn1_group2fieldid(const EC_GROUP *, X9_62_FIELDID *); |
173 | 499k | /* ec_asn1_group2curve() sets the values in a X9_62_CURVE object */ |
174 | 499k | static int ec_asn1_group2curve(const EC_GROUP *, X9_62_CURVE *); |
175 | 499k | |
176 | 499k | /* the function definitions */ |
177 | 499k | |
178 | 499k | static int ec_asn1_group2fieldid(const EC_GROUP *group, X9_62_FIELDID *field) |
179 | 499k | { |
180 | 1.62k | int ok = 0, nid; |
181 | 1.62k | BIGNUM *tmp = NULL; |
182 | | |
183 | 1.62k | if (group == NULL || field == NULL) |
184 | 0 | return 0; |
185 | | |
186 | | /* clear the old values (if necessary) */ |
187 | 1.62k | ASN1_OBJECT_free(field->fieldType); |
188 | 1.62k | ASN1_TYPE_free(field->p.other); |
189 | | |
190 | 1.62k | nid = EC_GROUP_get_field_type(group); |
191 | | /* set OID for the field */ |
192 | 1.62k | if ((field->fieldType = OBJ_nid2obj(nid)) == NULL) { |
193 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_OBJ_LIB); |
194 | 0 | goto err; |
195 | 0 | } |
196 | | |
197 | 1.62k | if (nid == NID_X9_62_prime_field) { |
198 | 1.62k | if ((tmp = BN_new()) == NULL) { |
199 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
200 | 0 | goto err; |
201 | 0 | } |
202 | | /* the parameters are specified by the prime number p */ |
203 | 1.62k | if (!EC_GROUP_get_curve(group, tmp, NULL, NULL, NULL)) { |
204 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
205 | 0 | goto err; |
206 | 0 | } |
207 | | /* set the prime number */ |
208 | 1.62k | field->p.prime = BN_to_ASN1_INTEGER(tmp, NULL); |
209 | 1.62k | if (field->p.prime == NULL) { |
210 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
211 | 0 | goto err; |
212 | 0 | } |
213 | 1.62k | } else if (nid == NID_X9_62_characteristic_two_field) |
214 | | #ifdef OPENSSL_NO_EC2M |
215 | | { |
216 | | ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED); |
217 | | goto err; |
218 | | } |
219 | | #else |
220 | 0 | { |
221 | 0 | int field_type; |
222 | 0 | X9_62_CHARACTERISTIC_TWO *char_two; |
223 | |
|
224 | 0 | field->p.char_two = X9_62_CHARACTERISTIC_TWO_new(); |
225 | 0 | char_two = field->p.char_two; |
226 | |
|
227 | 0 | if (char_two == NULL) { |
228 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
229 | 0 | goto err; |
230 | 0 | } |
231 | | |
232 | 0 | char_two->m = (long)EC_GROUP_get_degree(group); |
233 | |
|
234 | 0 | field_type = EC_GROUP_get_basis_type(group); |
235 | |
|
236 | 0 | if (field_type == 0) { |
237 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
238 | 0 | goto err; |
239 | 0 | } |
240 | | /* set base type OID */ |
241 | 0 | if ((char_two->type = OBJ_nid2obj(field_type)) == NULL) { |
242 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_OBJ_LIB); |
243 | 0 | goto err; |
244 | 0 | } |
245 | | |
246 | 0 | if (field_type == NID_X9_62_tpBasis) { |
247 | 0 | unsigned int k; |
248 | |
|
249 | 0 | if (!EC_GROUP_get_trinomial_basis(group, &k)) |
250 | 0 | goto err; |
251 | | |
252 | 0 | char_two->p.tpBasis = ASN1_INTEGER_new(); |
253 | 0 | if (char_two->p.tpBasis == NULL) { |
254 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
255 | 0 | goto err; |
256 | 0 | } |
257 | 0 | if (!ASN1_INTEGER_set(char_two->p.tpBasis, (long)k)) { |
258 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
259 | 0 | goto err; |
260 | 0 | } |
261 | 0 | } else if (field_type == NID_X9_62_ppBasis) { |
262 | 0 | unsigned int k1, k2, k3; |
263 | |
|
264 | 0 | if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3)) |
265 | 0 | goto err; |
266 | | |
267 | 0 | char_two->p.ppBasis = X9_62_PENTANOMIAL_new(); |
268 | 0 | if (char_two->p.ppBasis == NULL) { |
269 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
270 | 0 | goto err; |
271 | 0 | } |
272 | | |
273 | | /* set k? values */ |
274 | 0 | char_two->p.ppBasis->k1 = (long)k1; |
275 | 0 | char_two->p.ppBasis->k2 = (long)k2; |
276 | 0 | char_two->p.ppBasis->k3 = (long)k3; |
277 | 0 | } else { /* field_type == NID_X9_62_onBasis */ |
278 | | |
279 | | /* for ONB the parameters are (asn1) NULL */ |
280 | 0 | char_two->p.onBasis = ASN1_NULL_new(); |
281 | 0 | if (char_two->p.onBasis == NULL) { |
282 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
283 | 0 | goto err; |
284 | 0 | } |
285 | 0 | } |
286 | 0 | } |
287 | 0 | #endif |
288 | 0 | else { |
289 | 0 | ERR_raise(ERR_LIB_EC, EC_R_UNSUPPORTED_FIELD); |
290 | 0 | goto err; |
291 | 0 | } |
292 | | |
293 | 1.62k | ok = 1; |
294 | | |
295 | 1.62k | err: |
296 | 1.62k | BN_free(tmp); |
297 | 1.62k | return ok; |
298 | 1.62k | } |
299 | | |
300 | | static int ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve) |
301 | 1.62k | { |
302 | 1.62k | int ok = 0; |
303 | 1.62k | BIGNUM *tmp_1 = NULL, *tmp_2 = NULL; |
304 | 1.62k | unsigned char *a_buf = NULL, *b_buf = NULL; |
305 | 1.62k | size_t len; |
306 | | |
307 | 1.62k | if (!group || !curve || !curve->a || !curve->b) |
308 | 0 | return 0; |
309 | | |
310 | 1.62k | if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL) { |
311 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
312 | 0 | goto err; |
313 | 0 | } |
314 | | |
315 | | /* get a and b */ |
316 | 1.62k | if (!EC_GROUP_get_curve(group, NULL, tmp_1, tmp_2, NULL)) { |
317 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
318 | 0 | goto err; |
319 | 0 | } |
320 | | |
321 | | /* |
322 | | * Per SEC 1, the curve coefficients must be padded up to size. See C.2's |
323 | | * definition of Curve, C.1's definition of FieldElement, and 2.3.5's |
324 | | * definition of how to encode the field elements. |
325 | | */ |
326 | 1.62k | len = ((size_t)EC_GROUP_get_degree(group) + 7) / 8; |
327 | 1.62k | if ((a_buf = OPENSSL_malloc(len)) == NULL |
328 | 1.62k | || (b_buf = OPENSSL_malloc(len)) == NULL) { |
329 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
330 | 0 | goto err; |
331 | 0 | } |
332 | 1.62k | if (BN_bn2binpad(tmp_1, a_buf, len) < 0 |
333 | 1.62k | || BN_bn2binpad(tmp_2, b_buf, len) < 0) { |
334 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB); |
335 | 0 | goto err; |
336 | 0 | } |
337 | | |
338 | | /* set a and b */ |
339 | 1.62k | if (!ASN1_OCTET_STRING_set(curve->a, a_buf, len) |
340 | 1.62k | || !ASN1_OCTET_STRING_set(curve->b, b_buf, len)) { |
341 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
342 | 0 | goto err; |
343 | 0 | } |
344 | | |
345 | | /* set the seed (optional) */ |
346 | 1.62k | if (group->seed) { |
347 | 487 | if (!curve->seed) |
348 | 487 | if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) { |
349 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
350 | 0 | goto err; |
351 | 0 | } |
352 | 487 | curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); |
353 | 487 | curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT; |
354 | 487 | if (!ASN1_BIT_STRING_set(curve->seed, group->seed, |
355 | 487 | (int)group->seed_len)) { |
356 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
357 | 0 | goto err; |
358 | 0 | } |
359 | 1.13k | } else { |
360 | 1.13k | ASN1_BIT_STRING_free(curve->seed); |
361 | 1.13k | curve->seed = NULL; |
362 | 1.13k | } |
363 | | |
364 | 1.62k | ok = 1; |
365 | | |
366 | 1.62k | err: |
367 | 1.62k | OPENSSL_free(a_buf); |
368 | 1.62k | OPENSSL_free(b_buf); |
369 | 1.62k | BN_free(tmp_1); |
370 | 1.62k | BN_free(tmp_2); |
371 | 1.62k | return ok; |
372 | 1.62k | } |
373 | | |
374 | | ECPARAMETERS *EC_GROUP_get_ecparameters(const EC_GROUP *group, |
375 | | ECPARAMETERS *params) |
376 | 1.12k | { |
377 | 1.12k | size_t len = 0; |
378 | 1.12k | ECPARAMETERS *ret = NULL; |
379 | 1.12k | const BIGNUM *tmp; |
380 | 1.12k | unsigned char *buffer = NULL; |
381 | 1.12k | const EC_POINT *point = NULL; |
382 | 1.12k | point_conversion_form_t form; |
383 | 1.12k | ASN1_INTEGER *orig; |
384 | | |
385 | 1.12k | if (params == NULL) { |
386 | 1.12k | if ((ret = ECPARAMETERS_new()) == NULL) { |
387 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
388 | 0 | goto err; |
389 | 0 | } |
390 | 1.12k | } else |
391 | 0 | ret = params; |
392 | | |
393 | | /* set the version (always one) */ |
394 | 1.12k | ret->version = (long)0x1; |
395 | | |
396 | | /* set the fieldID */ |
397 | 1.12k | if (!ec_asn1_group2fieldid(group, ret->fieldID)) { |
398 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
399 | 0 | goto err; |
400 | 0 | } |
401 | | |
402 | | /* set the curve */ |
403 | 1.12k | if (!ec_asn1_group2curve(group, ret->curve)) { |
404 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
405 | 0 | goto err; |
406 | 0 | } |
407 | | |
408 | | /* set the base point */ |
409 | 1.12k | if ((point = EC_GROUP_get0_generator(group)) == NULL) { |
410 | 0 | ERR_raise(ERR_LIB_EC, EC_R_UNDEFINED_GENERATOR); |
411 | 0 | goto err; |
412 | 0 | } |
413 | | |
414 | 1.12k | form = EC_GROUP_get_point_conversion_form(group); |
415 | | |
416 | 1.12k | len = EC_POINT_point2buf(group, point, form, &buffer, NULL); |
417 | 1.12k | if (len == 0) { |
418 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
419 | 0 | goto err; |
420 | 0 | } |
421 | 1.12k | if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) { |
422 | 0 | OPENSSL_free(buffer); |
423 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
424 | 0 | goto err; |
425 | 0 | } |
426 | 1.12k | ASN1_STRING_set0(ret->base, buffer, len); |
427 | | |
428 | | /* set the order */ |
429 | 1.12k | tmp = EC_GROUP_get0_order(group); |
430 | 1.12k | if (tmp == NULL) { |
431 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
432 | 0 | goto err; |
433 | 0 | } |
434 | 1.12k | ret->order = BN_to_ASN1_INTEGER(tmp, orig = ret->order); |
435 | 1.12k | if (ret->order == NULL) { |
436 | 0 | ret->order = orig; |
437 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
438 | 0 | goto err; |
439 | 0 | } |
440 | | |
441 | | /* set the cofactor (optional) */ |
442 | 1.12k | tmp = EC_GROUP_get0_cofactor(group); |
443 | 1.12k | if (tmp != NULL) { |
444 | 1.12k | ret->cofactor = BN_to_ASN1_INTEGER(tmp, orig = ret->cofactor); |
445 | 1.12k | if (ret->cofactor == NULL) { |
446 | 0 | ret->cofactor = orig; |
447 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
448 | 0 | goto err; |
449 | 0 | } |
450 | 1.12k | } |
451 | | |
452 | 1.12k | return ret; |
453 | | |
454 | 0 | err: |
455 | 0 | if (params == NULL) |
456 | 0 | ECPARAMETERS_free(ret); |
457 | 0 | return NULL; |
458 | 1.12k | } |
459 | | |
460 | | ECPKPARAMETERS *EC_GROUP_get_ecpkparameters(const EC_GROUP *group, |
461 | | ECPKPARAMETERS *params) |
462 | 8.73k | { |
463 | 8.73k | int ok = 1, tmp; |
464 | 8.73k | ECPKPARAMETERS *ret = params; |
465 | | |
466 | 8.73k | if (ret == NULL) { |
467 | 8.73k | if ((ret = ECPKPARAMETERS_new()) == NULL) { |
468 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
469 | 0 | return NULL; |
470 | 0 | } |
471 | 8.73k | } else { |
472 | 0 | if (ret->type == ECPKPARAMETERS_TYPE_NAMED) |
473 | 0 | ASN1_OBJECT_free(ret->value.named_curve); |
474 | 0 | else if (ret->type == ECPKPARAMETERS_TYPE_EXPLICIT |
475 | 0 | && ret->value.parameters != NULL) |
476 | 0 | ECPARAMETERS_free(ret->value.parameters); |
477 | 0 | } |
478 | | |
479 | 8.73k | if (EC_GROUP_get_asn1_flag(group) == OPENSSL_EC_NAMED_CURVE) { |
480 | | /* |
481 | | * use the asn1 OID to describe the elliptic curve parameters |
482 | | */ |
483 | 7.11k | tmp = EC_GROUP_get_curve_name(group); |
484 | 7.11k | if (tmp) { |
485 | 7.11k | ASN1_OBJECT *asn1obj = OBJ_nid2obj(tmp); |
486 | | |
487 | 7.11k | if (asn1obj == NULL || OBJ_length(asn1obj) == 0) { |
488 | 0 | ASN1_OBJECT_free(asn1obj); |
489 | 0 | ERR_raise(ERR_LIB_EC, EC_R_MISSING_OID); |
490 | 0 | ok = 0; |
491 | 7.11k | } else { |
492 | 7.11k | ret->type = ECPKPARAMETERS_TYPE_NAMED; |
493 | 7.11k | ret->value.named_curve = asn1obj; |
494 | 7.11k | } |
495 | 7.11k | } else |
496 | | /* we don't know the nid => ERROR */ |
497 | 0 | ok = 0; |
498 | 7.11k | } else { |
499 | | /* use the ECPARAMETERS structure */ |
500 | 1.62k | ret->type = ECPKPARAMETERS_TYPE_EXPLICIT; |
501 | 1.62k | if ((ret->value.parameters = EC_GROUP_get_ecparameters(group, NULL)) == NULL) |
502 | 0 | ok = 0; |
503 | 1.62k | } |
504 | | |
505 | 8.73k | if (!ok) { |
506 | 0 | ECPKPARAMETERS_free(ret); |
507 | 0 | return NULL; |
508 | 0 | } |
509 | 8.73k | return ret; |
510 | 8.73k | } |
511 | | |
512 | | EC_GROUP *EC_GROUP_new_from_ecparameters(const ECPARAMETERS *params) |
513 | 23.9k | { |
514 | 23.9k | int ok = 0, tmp; |
515 | 23.9k | EC_GROUP *ret = NULL, *dup = NULL; |
516 | 23.9k | BIGNUM *p = NULL, *a = NULL, *b = NULL; |
517 | 23.9k | EC_POINT *point = NULL; |
518 | 23.9k | long field_bits; |
519 | 23.9k | int curve_name = NID_undef; |
520 | 23.9k | BN_CTX *ctx = NULL; |
521 | | |
522 | 23.9k | if (params->fieldID == NULL |
523 | 23.9k | || params->fieldID->fieldType == NULL |
524 | 23.9k | || params->fieldID->p.ptr == NULL) { |
525 | 0 | ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); |
526 | 0 | goto err; |
527 | 0 | } |
528 | | |
529 | | /* |
530 | | * Now extract the curve parameters a and b. Note that, although SEC 1 |
531 | | * specifies the length of their encodings, historical versions of OpenSSL |
532 | | * encoded them incorrectly, so we must accept any length for backwards |
533 | | * compatibility. |
534 | | */ |
535 | 23.9k | if (params->curve == NULL |
536 | 23.9k | || params->curve->a == NULL || params->curve->a->data == NULL |
537 | 23.9k | || params->curve->b == NULL || params->curve->b->data == NULL) { |
538 | 0 | ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); |
539 | 0 | goto err; |
540 | 0 | } |
541 | 23.9k | a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL); |
542 | 23.9k | if (a == NULL) { |
543 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB); |
544 | 0 | goto err; |
545 | 0 | } |
546 | 23.9k | b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL); |
547 | 23.9k | if (b == NULL) { |
548 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB); |
549 | 0 | goto err; |
550 | 0 | } |
551 | | |
552 | | /* get the field parameters */ |
553 | 23.9k | tmp = OBJ_obj2nid(params->fieldID->fieldType); |
554 | 23.9k | if (tmp == NID_X9_62_characteristic_two_field) |
555 | | #ifdef OPENSSL_NO_EC2M |
556 | | { |
557 | | ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED); |
558 | | goto err; |
559 | | } |
560 | | #else |
561 | 0 | { |
562 | 0 | X9_62_CHARACTERISTIC_TWO *char_two; |
563 | |
|
564 | 0 | char_two = params->fieldID->p.char_two; |
565 | |
|
566 | 0 | field_bits = char_two->m; |
567 | 0 | if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) { |
568 | 0 | ERR_raise(ERR_LIB_EC, EC_R_FIELD_TOO_LARGE); |
569 | 0 | goto err; |
570 | 0 | } |
571 | | |
572 | 0 | if ((p = BN_new()) == NULL) { |
573 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
574 | 0 | goto err; |
575 | 0 | } |
576 | | |
577 | | /* get the base type */ |
578 | 0 | tmp = OBJ_obj2nid(char_two->type); |
579 | |
|
580 | 0 | if (tmp == NID_X9_62_tpBasis) { |
581 | 0 | long tmp_long; |
582 | |
|
583 | 0 | if (!char_two->p.tpBasis) { |
584 | 0 | ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); |
585 | 0 | goto err; |
586 | 0 | } |
587 | | |
588 | 0 | tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis); |
589 | |
|
590 | 0 | if (!(char_two->m > tmp_long && tmp_long > 0)) { |
591 | 0 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_TRINOMIAL_BASIS); |
592 | 0 | goto err; |
593 | 0 | } |
594 | | |
595 | | /* create the polynomial */ |
596 | 0 | if (!BN_set_bit(p, (int)char_two->m)) |
597 | 0 | goto err; |
598 | 0 | if (!BN_set_bit(p, (int)tmp_long)) |
599 | 0 | goto err; |
600 | 0 | if (!BN_set_bit(p, 0)) |
601 | 0 | goto err; |
602 | 0 | } else if (tmp == NID_X9_62_ppBasis) { |
603 | 0 | X9_62_PENTANOMIAL *penta; |
604 | |
|
605 | 0 | penta = char_two->p.ppBasis; |
606 | 0 | if (penta == NULL) { |
607 | 0 | ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); |
608 | 0 | goto err; |
609 | 0 | } |
610 | | |
611 | 0 | if (!(char_two->m > penta->k3 && penta->k3 > penta->k2 |
612 | 0 | && penta->k2 > penta->k1 && penta->k1 > 0)) { |
613 | 0 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_PENTANOMIAL_BASIS); |
614 | 0 | goto err; |
615 | 0 | } |
616 | | |
617 | | /* create the polynomial */ |
618 | 0 | if (!BN_set_bit(p, (int)char_two->m)) |
619 | 0 | goto err; |
620 | 0 | if (!BN_set_bit(p, (int)penta->k1)) |
621 | 0 | goto err; |
622 | 0 | if (!BN_set_bit(p, (int)penta->k2)) |
623 | 0 | goto err; |
624 | 0 | if (!BN_set_bit(p, (int)penta->k3)) |
625 | 0 | goto err; |
626 | 0 | if (!BN_set_bit(p, 0)) |
627 | 0 | goto err; |
628 | 0 | } else if (tmp == NID_X9_62_onBasis) { |
629 | 0 | ERR_raise(ERR_LIB_EC, EC_R_NOT_IMPLEMENTED); |
630 | 0 | goto err; |
631 | 0 | } else { /* error */ |
632 | |
|
633 | 0 | ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); |
634 | 0 | goto err; |
635 | 0 | } |
636 | | |
637 | | /* create the EC_GROUP structure */ |
638 | 0 | ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL); |
639 | 0 | } |
640 | 23.9k | #endif |
641 | 23.9k | else if (tmp == NID_X9_62_prime_field) { |
642 | | /* we have a curve over a prime field */ |
643 | | /* extract the prime number */ |
644 | 23.6k | if (params->fieldID->p.prime == NULL) { |
645 | 0 | ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); |
646 | 0 | goto err; |
647 | 0 | } |
648 | 23.6k | p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL); |
649 | 23.6k | if (p == NULL) { |
650 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
651 | 0 | goto err; |
652 | 0 | } |
653 | | |
654 | 23.6k | if (BN_is_negative(p) || BN_is_zero(p)) { |
655 | 247 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD); |
656 | 247 | goto err; |
657 | 247 | } |
658 | | |
659 | 23.3k | field_bits = BN_num_bits(p); |
660 | 23.3k | if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) { |
661 | 0 | ERR_raise(ERR_LIB_EC, EC_R_FIELD_TOO_LARGE); |
662 | 0 | goto err; |
663 | 0 | } |
664 | | |
665 | | /* create the EC_GROUP structure */ |
666 | 23.3k | ret = EC_GROUP_new_curve_GFp(p, a, b, NULL); |
667 | 23.3k | } else { |
668 | 349 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD); |
669 | 349 | goto err; |
670 | 349 | } |
671 | | |
672 | 23.3k | if (ret == NULL) { |
673 | 3.66k | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
674 | 3.66k | goto err; |
675 | 3.66k | } |
676 | | |
677 | | /* extract seed (optional) */ |
678 | 19.7k | if (params->curve->seed != NULL) { |
679 | | /* |
680 | | * This happens for instance with |
681 | | * fuzz/corpora/asn1/65cf44e85614c62f10cf3b7a7184c26293a19e4a |
682 | | * and causes the OPENSSL_malloc below to choke on the |
683 | | * zero length allocation request. |
684 | | */ |
685 | 13.8k | if (params->curve->seed->length == 0) { |
686 | 61 | ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); |
687 | 61 | goto err; |
688 | 61 | } |
689 | 13.7k | OPENSSL_free(ret->seed); |
690 | 13.7k | if ((ret->seed = OPENSSL_malloc(params->curve->seed->length)) == NULL) { |
691 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
692 | 0 | goto err; |
693 | 0 | } |
694 | 13.7k | memcpy(ret->seed, params->curve->seed->data, |
695 | 13.7k | params->curve->seed->length); |
696 | 13.7k | ret->seed_len = params->curve->seed->length; |
697 | 13.7k | } |
698 | | |
699 | 19.6k | if (params->order == NULL |
700 | 19.6k | || params->base == NULL |
701 | 19.6k | || params->base->data == NULL |
702 | 19.6k | || params->base->length == 0) { |
703 | 137 | ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); |
704 | 137 | goto err; |
705 | 137 | } |
706 | | |
707 | 19.5k | if ((point = EC_POINT_new(ret)) == NULL) |
708 | 0 | goto err; |
709 | | |
710 | | /* set the point conversion form */ |
711 | 19.5k | EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)(params->base->data[0] & ~0x01)); |
712 | | |
713 | | /* extract the ec point */ |
714 | 19.5k | if (!EC_POINT_oct2point(ret, point, params->base->data, |
715 | 19.5k | params->base->length, NULL)) { |
716 | 6.47k | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
717 | 6.47k | goto err; |
718 | 6.47k | } |
719 | | |
720 | | /* extract the order */ |
721 | 13.0k | if (ASN1_INTEGER_to_BN(params->order, a) == NULL) { |
722 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
723 | 0 | goto err; |
724 | 0 | } |
725 | 13.0k | if (BN_is_negative(a) || BN_is_zero(a)) { |
726 | 547 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER); |
727 | 547 | goto err; |
728 | 547 | } |
729 | 12.4k | if (BN_num_bits(a) > (int)field_bits + 1) { /* Hasse bound */ |
730 | 347 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER); |
731 | 347 | goto err; |
732 | 347 | } |
733 | | |
734 | | /* extract the cofactor (optional) */ |
735 | 12.1k | if (params->cofactor == NULL) { |
736 | 4.68k | BN_free(b); |
737 | 4.68k | b = NULL; |
738 | 7.45k | } else if (ASN1_INTEGER_to_BN(params->cofactor, b) == NULL) { |
739 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
740 | 0 | goto err; |
741 | 0 | } |
742 | | /* set the generator, order and cofactor (if present) */ |
743 | 12.1k | if (!EC_GROUP_set_generator(ret, point, a, b)) { |
744 | 170 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
745 | 170 | goto err; |
746 | 170 | } |
747 | | |
748 | | /* |
749 | | * Check if the explicit parameters group just created matches one of the |
750 | | * built-in curves. |
751 | | * |
752 | | * We create a copy of the group just built, so that we can remove optional |
753 | | * fields for the lookup: we do this to avoid the possibility that one of |
754 | | * the optional parameters is used to force the library into using a less |
755 | | * performant and less secure EC_METHOD instead of the specialized one. |
756 | | * In any case, `seed` is not really used in any computation, while a |
757 | | * cofactor different from the one in the built-in table is just |
758 | | * mathematically wrong anyway and should not be used. |
759 | | */ |
760 | 11.9k | if ((ctx = BN_CTX_new()) == NULL) { |
761 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB); |
762 | 0 | goto err; |
763 | 0 | } |
764 | 11.9k | if ((dup = EC_GROUP_dup(ret)) == NULL |
765 | 11.9k | || EC_GROUP_set_seed(dup, NULL, 0) != 1 |
766 | 11.9k | || !EC_GROUP_set_generator(dup, point, a, NULL)) { |
767 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
768 | 0 | goto err; |
769 | 0 | } |
770 | 11.9k | if ((curve_name = ossl_ec_curve_nid_from_params(dup, ctx)) != NID_undef) { |
771 | | /* |
772 | | * The input explicit parameters successfully matched one of the |
773 | | * built-in curves: often for built-in curves we have specialized |
774 | | * methods with better performance and hardening. |
775 | | * |
776 | | * In this case we replace the `EC_GROUP` created through explicit |
777 | | * parameters with one created from a named group. |
778 | | */ |
779 | 1.53k | EC_GROUP *named_group = NULL; |
780 | | |
781 | 1.53k | #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128 |
782 | | /* |
783 | | * NID_wap_wsg_idm_ecid_wtls12 and NID_secp224r1 are both aliases for |
784 | | * the same curve, we prefer the SECP nid when matching explicit |
785 | | * parameters as that is associated with a specialized EC_METHOD. |
786 | | */ |
787 | 1.53k | if (curve_name == NID_wap_wsg_idm_ecid_wtls12) |
788 | 0 | curve_name = NID_secp224r1; |
789 | 1.53k | #endif /* !def(OPENSSL_NO_EC_NISTP_64_GCC_128) */ |
790 | | |
791 | 1.53k | if ((named_group = EC_GROUP_new_by_curve_name(curve_name)) == NULL) { |
792 | 1.30k | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
793 | 1.30k | goto err; |
794 | 1.30k | } |
795 | 234 | EC_GROUP_free(ret); |
796 | 234 | ret = named_group; |
797 | | |
798 | | /* |
799 | | * Set the flag so that EC_GROUPs created from explicit parameters are |
800 | | * serialized using explicit parameters by default. |
801 | | */ |
802 | 234 | EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_EXPLICIT_CURVE); |
803 | | |
804 | | /* |
805 | | * If the input params do not contain the optional seed field we make |
806 | | * sure it is not added to the returned group. |
807 | | * |
808 | | * The seed field is not really used inside libcrypto anyway, and |
809 | | * adding it to parsed explicit parameter keys would alter their DER |
810 | | * encoding output (because of the extra field) which could impact |
811 | | * applications fingerprinting keys by their DER encoding. |
812 | | */ |
813 | 234 | if (params->curve->seed == NULL) { |
814 | 71 | if (EC_GROUP_set_seed(ret, NULL, 0) != 1) |
815 | 0 | goto err; |
816 | 71 | } |
817 | 234 | } |
818 | | |
819 | 10.6k | ok = 1; |
820 | | |
821 | 23.9k | err: |
822 | 23.9k | if (!ok) { |
823 | 13.3k | EC_GROUP_free(ret); |
824 | 13.3k | ret = NULL; |
825 | 13.3k | } |
826 | 23.9k | EC_GROUP_free(dup); |
827 | | |
828 | 23.9k | BN_free(p); |
829 | 23.9k | BN_free(a); |
830 | 23.9k | BN_free(b); |
831 | 23.9k | EC_POINT_free(point); |
832 | | |
833 | 23.9k | BN_CTX_free(ctx); |
834 | | |
835 | 23.9k | return ret; |
836 | 10.6k | } |
837 | | |
838 | | EC_GROUP *EC_GROUP_new_from_ecpkparameters(const ECPKPARAMETERS *params) |
839 | 113k | { |
840 | 113k | EC_GROUP *ret = NULL; |
841 | 113k | int tmp = 0; |
842 | | |
843 | 113k | if (params == NULL) { |
844 | 0 | ERR_raise(ERR_LIB_EC, EC_R_MISSING_PARAMETERS); |
845 | 0 | return NULL; |
846 | 0 | } |
847 | | |
848 | 113k | if (params->type == ECPKPARAMETERS_TYPE_NAMED) { |
849 | | /* the curve is given by an OID */ |
850 | 89.0k | tmp = OBJ_obj2nid(params->value.named_curve); |
851 | 89.0k | if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) { |
852 | 13.2k | ERR_raise(ERR_LIB_EC, EC_R_EC_GROUP_NEW_BY_NAME_FAILURE); |
853 | 13.2k | return NULL; |
854 | 13.2k | } |
855 | 75.7k | EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE); |
856 | 75.7k | } else if (params->type == ECPKPARAMETERS_TYPE_EXPLICIT) { |
857 | | /* the parameters are given by an ECPARAMETERS structure */ |
858 | 23.9k | ret = EC_GROUP_new_from_ecparameters(params->value.parameters); |
859 | 23.9k | if (!ret) { |
860 | 13.3k | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
861 | 13.3k | return NULL; |
862 | 13.3k | } |
863 | 10.6k | EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_EXPLICIT_CURVE); |
864 | 10.6k | } else if (params->type == ECPKPARAMETERS_TYPE_IMPLICIT) { |
865 | | /* implicit parameters inherited from CA - unsupported */ |
866 | 101 | return NULL; |
867 | 101 | } else { |
868 | 0 | ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); |
869 | 0 | return NULL; |
870 | 0 | } |
871 | | |
872 | 86.4k | return ret; |
873 | 113k | } |
874 | | |
875 | | /* EC_GROUP <-> DER encoding of ECPKPARAMETERS */ |
876 | | |
877 | | EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len) |
878 | 271k | { |
879 | 271k | EC_GROUP *group = NULL; |
880 | 271k | ECPKPARAMETERS *params = NULL; |
881 | 271k | const unsigned char *p = *in; |
882 | | |
883 | 271k | if ((params = d2i_ECPKPARAMETERS(NULL, &p, len)) == NULL) { |
884 | 238k | ECPKPARAMETERS_free(params); |
885 | 238k | return NULL; |
886 | 238k | } |
887 | | |
888 | 33.1k | if ((group = EC_GROUP_new_from_ecpkparameters(params)) == NULL) { |
889 | 24.3k | ECPKPARAMETERS_free(params); |
890 | 24.3k | return NULL; |
891 | 24.3k | } |
892 | | |
893 | 8.75k | if (params->type == ECPKPARAMETERS_TYPE_EXPLICIT) |
894 | 7.39k | group->decoded_from_explicit_params = 1; |
895 | | |
896 | 8.75k | if (a) { |
897 | 5.05k | EC_GROUP_free(*a); |
898 | 5.05k | *a = group; |
899 | 5.05k | } |
900 | | |
901 | 8.75k | ECPKPARAMETERS_free(params); |
902 | 8.75k | *in = p; |
903 | 8.75k | return group; |
904 | 33.1k | } |
905 | | |
906 | | int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out) |
907 | 1.26k | { |
908 | 1.26k | int ret = 0; |
909 | 1.26k | ECPKPARAMETERS *tmp = EC_GROUP_get_ecpkparameters(a, NULL); |
910 | 1.26k | if (tmp == NULL) { |
911 | 0 | ERR_raise(ERR_LIB_EC, EC_R_GROUP2PKPARAMETERS_FAILURE); |
912 | 0 | return 0; |
913 | 0 | } |
914 | 1.26k | if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) { |
915 | 0 | ERR_raise(ERR_LIB_EC, EC_R_I2D_ECPKPARAMETERS_FAILURE); |
916 | 0 | ECPKPARAMETERS_free(tmp); |
917 | 0 | return 0; |
918 | 0 | } |
919 | 1.26k | ECPKPARAMETERS_free(tmp); |
920 | 1.26k | return ret; |
921 | 1.26k | } |
922 | | |
923 | | /* some EC_KEY functions */ |
924 | | |
925 | | EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len) |
926 | 386k | { |
927 | 386k | EC_KEY *ret = NULL; |
928 | 386k | EC_PRIVATEKEY *priv_key = NULL; |
929 | 386k | const unsigned char *p = *in; |
930 | | |
931 | 386k | if ((priv_key = d2i_EC_PRIVATEKEY(NULL, &p, len)) == NULL) |
932 | 305k | return NULL; |
933 | | |
934 | 81.5k | if (a == NULL || *a == NULL) { |
935 | 80.2k | if ((ret = EC_KEY_new()) == NULL) { |
936 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
937 | 0 | goto err; |
938 | 0 | } |
939 | 80.2k | } else |
940 | 1.36k | ret = *a; |
941 | | |
942 | 81.5k | if (priv_key->parameters) { |
943 | 80.0k | EC_GROUP_free(ret->group); |
944 | 80.0k | ret->group = EC_GROUP_new_from_ecpkparameters(priv_key->parameters); |
945 | 80.0k | if (ret->group != NULL |
946 | 77.6k | && priv_key->parameters->type == ECPKPARAMETERS_TYPE_EXPLICIT) |
947 | 3.28k | ret->group->decoded_from_explicit_params = 1; |
948 | 80.0k | } |
949 | | |
950 | 81.5k | if (ret->group == NULL) { |
951 | 2.71k | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
952 | 2.71k | goto err; |
953 | 2.71k | } |
954 | | |
955 | 78.8k | ret->version = priv_key->version; |
956 | | |
957 | 78.8k | if (priv_key->privateKey) { |
958 | 78.8k | ASN1_OCTET_STRING *pkey = priv_key->privateKey; |
959 | 78.8k | if (EC_KEY_oct2priv(ret, ASN1_STRING_get0_data(pkey), |
960 | 78.8k | ASN1_STRING_length(pkey)) |
961 | 78.8k | == 0) |
962 | 0 | goto err; |
963 | 78.8k | } else { |
964 | 0 | ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY); |
965 | 0 | goto err; |
966 | 0 | } |
967 | | |
968 | 78.8k | if (EC_GROUP_get_curve_name(ret->group) == NID_sm2) |
969 | 129 | EC_KEY_set_flags(ret, EC_FLAG_SM2_RANGE); |
970 | | |
971 | 78.8k | EC_POINT_clear_free(ret->pub_key); |
972 | 78.8k | ret->pub_key = EC_POINT_new(ret->group); |
973 | 78.8k | if (ret->pub_key == NULL) { |
974 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
975 | 0 | goto err; |
976 | 0 | } |
977 | | |
978 | 78.8k | if (priv_key->publicKey) { |
979 | 61.3k | const unsigned char *pub_oct; |
980 | 61.3k | int pub_oct_len; |
981 | | |
982 | 61.3k | pub_oct = ASN1_STRING_get0_data(priv_key->publicKey); |
983 | 61.3k | pub_oct_len = ASN1_STRING_length(priv_key->publicKey); |
984 | 61.3k | if (!EC_KEY_oct2key(ret, pub_oct, pub_oct_len, NULL)) { |
985 | 5.04k | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
986 | 5.04k | goto err; |
987 | 5.04k | } |
988 | 61.3k | } else { |
989 | 17.5k | if (ret->group->meth->keygenpub == NULL |
990 | 17.5k | || ret->group->meth->keygenpub(ret) == 0) |
991 | 0 | goto err; |
992 | | /* Remember the original private-key-only encoding. */ |
993 | 17.5k | ret->enc_flag |= EC_PKEY_NO_PUBKEY; |
994 | 17.5k | } |
995 | | |
996 | 73.8k | if (a) |
997 | 1.00k | *a = ret; |
998 | 73.8k | EC_PRIVATEKEY_free(priv_key); |
999 | 73.8k | *in = p; |
1000 | 73.8k | ret->dirty_cnt++; |
1001 | 73.8k | return ret; |
1002 | | |
1003 | 7.75k | err: |
1004 | 7.75k | if (a == NULL || *a != ret) |
1005 | 7.39k | EC_KEY_free(ret); |
1006 | 7.75k | EC_PRIVATEKEY_free(priv_key); |
1007 | 7.75k | return NULL; |
1008 | 78.8k | } |
1009 | | |
1010 | | int i2d_ECPrivateKey(const EC_KEY *a, unsigned char **out) |
1011 | 7.93k | { |
1012 | 7.93k | int ret = 0, ok = 0; |
1013 | 7.93k | unsigned char *priv = NULL, *pub = NULL; |
1014 | 7.93k | size_t privlen = 0, publen = 0; |
1015 | | |
1016 | 7.93k | EC_PRIVATEKEY *priv_key = NULL; |
1017 | | |
1018 | 7.93k | if (a == NULL || a->group == NULL || (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key == NULL)) { |
1019 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); |
1020 | 0 | goto err; |
1021 | 0 | } |
1022 | | |
1023 | 7.93k | if ((priv_key = EC_PRIVATEKEY_new()) == NULL) { |
1024 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
1025 | 0 | goto err; |
1026 | 0 | } |
1027 | | |
1028 | 7.93k | priv_key->version = a->version; |
1029 | | |
1030 | 7.93k | privlen = EC_KEY_priv2buf(a, &priv); |
1031 | | |
1032 | 7.93k | if (privlen == 0) { |
1033 | 2.67k | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
1034 | 2.67k | goto err; |
1035 | 2.67k | } |
1036 | | |
1037 | 5.26k | ASN1_STRING_set0(priv_key->privateKey, priv, privlen); |
1038 | 5.26k | priv = NULL; |
1039 | | |
1040 | 5.26k | if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) { |
1041 | 5.24k | if ((priv_key->parameters = EC_GROUP_get_ecpkparameters(a->group, |
1042 | 5.24k | priv_key->parameters)) |
1043 | 5.24k | == NULL) { |
1044 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
1045 | 0 | goto err; |
1046 | 0 | } |
1047 | 5.24k | } |
1048 | | |
1049 | 5.26k | if (!(a->enc_flag & EC_PKEY_NO_PUBKEY)) { |
1050 | 1.59k | priv_key->publicKey = ASN1_BIT_STRING_new(); |
1051 | 1.59k | if (priv_key->publicKey == NULL) { |
1052 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
1053 | 0 | goto err; |
1054 | 0 | } |
1055 | | |
1056 | 1.59k | publen = EC_KEY_key2buf(a, a->conv_form, &pub, NULL); |
1057 | | |
1058 | 1.59k | if (publen == 0) { |
1059 | 49 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
1060 | 49 | goto err; |
1061 | 49 | } |
1062 | | |
1063 | 1.54k | priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); |
1064 | 1.54k | priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT; |
1065 | 1.54k | ASN1_STRING_set0(priv_key->publicKey, pub, publen); |
1066 | 1.54k | pub = NULL; |
1067 | 1.54k | } |
1068 | | |
1069 | 5.21k | if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) { |
1070 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
1071 | 0 | goto err; |
1072 | 0 | } |
1073 | 5.21k | ok = 1; |
1074 | 7.93k | err: |
1075 | 7.93k | OPENSSL_clear_free(priv, privlen); |
1076 | 7.93k | OPENSSL_free(pub); |
1077 | 7.93k | EC_PRIVATEKEY_free(priv_key); |
1078 | 7.93k | return (ok ? ret : 0); |
1079 | 5.21k | } |
1080 | | |
1081 | | int i2d_ECParameters(const EC_KEY *a, unsigned char **out) |
1082 | 760 | { |
1083 | 760 | if (a == NULL) { |
1084 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); |
1085 | 0 | return 0; |
1086 | 0 | } |
1087 | 760 | return i2d_ECPKParameters(a->group, out); |
1088 | 760 | } |
1089 | | |
1090 | | EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len) |
1091 | 167k | { |
1092 | 167k | EC_KEY *ret; |
1093 | | |
1094 | 167k | if (in == NULL || *in == NULL) { |
1095 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); |
1096 | 0 | return NULL; |
1097 | 0 | } |
1098 | | |
1099 | 167k | if (a == NULL || *a == NULL) { |
1100 | 135k | if ((ret = EC_KEY_new()) == NULL) { |
1101 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
1102 | 0 | return NULL; |
1103 | 0 | } |
1104 | 135k | } else |
1105 | 32.6k | ret = *a; |
1106 | | |
1107 | 167k | if (!d2i_ECPKParameters(&ret->group, in, len)) { |
1108 | 162k | if (a == NULL || *a != ret) |
1109 | 133k | EC_KEY_free(ret); |
1110 | 29.2k | else |
1111 | 29.2k | ret->dirty_cnt++; |
1112 | 162k | return NULL; |
1113 | 162k | } |
1114 | | |
1115 | 5.05k | if (EC_GROUP_get_curve_name(ret->group) == NID_sm2) |
1116 | 11 | EC_KEY_set_flags(ret, EC_FLAG_SM2_RANGE); |
1117 | | |
1118 | 5.05k | ret->dirty_cnt++; |
1119 | | |
1120 | 5.05k | if (a) |
1121 | 3.33k | *a = ret; |
1122 | | |
1123 | 5.05k | return ret; |
1124 | 167k | } |
1125 | | |
1126 | | EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len) |
1127 | 558k | { |
1128 | 558k | EC_KEY *ret = NULL; |
1129 | | |
1130 | 558k | if (a == NULL || (*a) == NULL || (*a)->group == NULL) { |
1131 | | /* |
1132 | | * sorry, but a EC_GROUP-structure is necessary to set the public key |
1133 | | */ |
1134 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); |
1135 | 0 | return 0; |
1136 | 0 | } |
1137 | 558k | ret = *a; |
1138 | | /* EC_KEY_opt2key updates dirty_cnt */ |
1139 | 558k | if (!EC_KEY_oct2key(ret, *in, len, NULL)) { |
1140 | 257k | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
1141 | 257k | return 0; |
1142 | 257k | } |
1143 | 301k | *in += len; |
1144 | 301k | return ret; |
1145 | 558k | } |
1146 | | |
1147 | | int i2o_ECPublicKey(const EC_KEY *a, unsigned char **out) |
1148 | 0 | { |
1149 | 0 | size_t buf_len = 0; |
1150 | 0 | int new_buffer = 0; |
1151 | |
|
1152 | 0 | if (a == NULL || a->pub_key == NULL) { |
1153 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); |
1154 | 0 | return 0; |
1155 | 0 | } |
1156 | | |
1157 | 0 | buf_len = EC_POINT_point2oct(a->group, a->pub_key, |
1158 | 0 | a->conv_form, NULL, 0, NULL); |
1159 | |
|
1160 | 0 | if (out == NULL || buf_len == 0) |
1161 | | /* out == NULL => just return the length of the octet string */ |
1162 | 0 | return buf_len; |
1163 | | |
1164 | 0 | if (*out == NULL) { |
1165 | 0 | if ((*out = OPENSSL_malloc(buf_len)) == NULL) { |
1166 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
1167 | 0 | return 0; |
1168 | 0 | } |
1169 | 0 | new_buffer = 1; |
1170 | 0 | } |
1171 | 0 | if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form, |
1172 | 0 | *out, buf_len, NULL)) { |
1173 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
1174 | 0 | if (new_buffer) { |
1175 | 0 | OPENSSL_free(*out); |
1176 | 0 | *out = NULL; |
1177 | 0 | } |
1178 | 0 | return 0; |
1179 | 0 | } |
1180 | 0 | if (!new_buffer) |
1181 | 0 | *out += buf_len; |
1182 | 0 | return buf_len; |
1183 | 0 | } |
1184 | | |
1185 | | DECLARE_ASN1_FUNCTIONS(ECDSA_SIG) |
1186 | | DECLARE_ASN1_ENCODE_FUNCTIONS_name(ECDSA_SIG, ECDSA_SIG) |
1187 | | |
1188 | | #endif /* FIPS_MODULE */ |
1189 | | |
1190 | | ECDSA_SIG *ECDSA_SIG_new(void) |
1191 | 14.1k | { |
1192 | 14.1k | ECDSA_SIG *sig = OPENSSL_zalloc(sizeof(*sig)); |
1193 | 14.1k | if (sig == NULL) |
1194 | 14.1k | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
1195 | 14.1k | return sig; |
1196 | 14.1k | } |
1197 | | |
1198 | | void ECDSA_SIG_free(ECDSA_SIG *sig) |
1199 | 80.7k | { |
1200 | 80.7k | if (sig == NULL) |
1201 | 0 | return; |
1202 | 80.7k | BN_clear_free(sig->r); |
1203 | 80.7k | BN_clear_free(sig->s); |
1204 | 80.7k | OPENSSL_free(sig); |
1205 | 80.7k | } |
1206 | | |
1207 | | ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **psig, const unsigned char **ppin, long len) |
1208 | 74.0k | { |
1209 | 74.0k | ECDSA_SIG *sig; |
1210 | | |
1211 | 74.0k | if (len < 0) |
1212 | 0 | return NULL; |
1213 | 74.0k | if (psig != NULL && *psig != NULL) { |
1214 | 2.60k | sig = *psig; |
1215 | 71.4k | } else { |
1216 | 71.4k | sig = ECDSA_SIG_new(); |
1217 | 71.4k | if (sig == NULL) |
1218 | 0 | return NULL; |
1219 | 71.4k | } |
1220 | 74.0k | if (sig->r == NULL) |
1221 | 74.0k | sig->r = BN_new(); |
1222 | 74.0k | if (sig->s == NULL) |
1223 | 74.0k | sig->s = BN_new(); |
1224 | 74.0k | if (sig->r == NULL || sig->s == NULL |
1225 | 74.0k | || ossl_decode_der_dsa_sig(sig->r, sig->s, ppin, (size_t)len) == 0) { |
1226 | 72.2k | if (psig == NULL || *psig == NULL) |
1227 | 71.2k | ECDSA_SIG_free(sig); |
1228 | 72.2k | return NULL; |
1229 | 72.2k | } |
1230 | 1.74k | if (psig != NULL && *psig == NULL) |
1231 | 0 | *psig = sig; |
1232 | 1.74k | return sig; |
1233 | 74.0k | } |
1234 | | |
1235 | | int i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **ppout) |
1236 | 406k | { |
1237 | 406k | BUF_MEM *buf = NULL; |
1238 | 406k | size_t encoded_len; |
1239 | 406k | WPACKET pkt; |
1240 | | |
1241 | 406k | if (ppout == NULL) { |
1242 | 398k | if (!WPACKET_init_null(&pkt, 0)) |
1243 | 0 | return -1; |
1244 | 398k | } else if (*ppout == NULL) { |
1245 | 1.74k | if ((buf = BUF_MEM_new()) == NULL |
1246 | 1.74k | || !WPACKET_init_len(&pkt, buf, 0)) { |
1247 | 0 | BUF_MEM_free(buf); |
1248 | 0 | return -1; |
1249 | 0 | } |
1250 | 6.75k | } else { |
1251 | 6.75k | if (!WPACKET_init_static_len(&pkt, *ppout, SIZE_MAX, 0)) |
1252 | 0 | return -1; |
1253 | 6.75k | } |
1254 | | |
1255 | 406k | if (!ossl_encode_der_dsa_sig(&pkt, sig->r, sig->s) |
1256 | 406k | || !WPACKET_get_total_written(&pkt, &encoded_len) |
1257 | 406k | || !WPACKET_finish(&pkt)) { |
1258 | 0 | BUF_MEM_free(buf); |
1259 | 0 | WPACKET_cleanup(&pkt); |
1260 | 0 | return -1; |
1261 | 0 | } |
1262 | | |
1263 | 406k | if (ppout != NULL) { |
1264 | 8.49k | if (*ppout == NULL) { |
1265 | 1.74k | *ppout = (unsigned char *)buf->data; |
1266 | 1.74k | buf->data = NULL; |
1267 | 1.74k | BUF_MEM_free(buf); |
1268 | 6.75k | } else { |
1269 | 6.75k | *ppout += encoded_len; |
1270 | 6.75k | } |
1271 | 8.49k | } |
1272 | | |
1273 | 406k | return (int)encoded_len; |
1274 | 406k | } |
1275 | | |
1276 | | void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps) |
1277 | 0 | { |
1278 | 0 | if (pr != NULL) |
1279 | 0 | *pr = sig->r; |
1280 | 0 | if (ps != NULL) |
1281 | 0 | *ps = sig->s; |
1282 | 0 | } |
1283 | | |
1284 | | const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig) |
1285 | 0 | { |
1286 | 0 | return sig->r; |
1287 | 0 | } |
1288 | | |
1289 | | const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig) |
1290 | 0 | { |
1291 | 0 | return sig->s; |
1292 | 0 | } |
1293 | | |
1294 | | int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s) |
1295 | 0 | { |
1296 | 0 | if (r == NULL || s == NULL) |
1297 | 0 | return 0; |
1298 | 0 | BN_clear_free(sig->r); |
1299 | 0 | BN_clear_free(sig->s); |
1300 | 0 | sig->r = r; |
1301 | 0 | sig->s = s; |
1302 | 0 | return 1; |
1303 | 0 | } |
1304 | | |
1305 | | int ECDSA_size(const EC_KEY *ec) |
1306 | 398k | { |
1307 | 398k | int ret; |
1308 | 398k | ECDSA_SIG sig; |
1309 | 398k | const EC_GROUP *group; |
1310 | 398k | const BIGNUM *bn; |
1311 | | |
1312 | 398k | if (ec == NULL) |
1313 | 0 | return 0; |
1314 | 398k | group = EC_KEY_get0_group(ec); |
1315 | 398k | if (group == NULL) |
1316 | 0 | return 0; |
1317 | | |
1318 | 398k | bn = EC_GROUP_get0_order(group); |
1319 | 398k | if (bn == NULL) |
1320 | 0 | return 0; |
1321 | | |
1322 | 398k | sig.r = sig.s = (BIGNUM *)bn; |
1323 | 398k | ret = i2d_ECDSA_SIG(&sig, NULL); |
1324 | | |
1325 | 398k | if (ret < 0) |
1326 | 0 | ret = 0; |
1327 | 398k | return ret; |
1328 | 398k | } |