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