/src/openssl41/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 | if (!ASN1_BIT_STRING_set1(curve->seed, group->seed, |
352 | 276 | (int)group->seed_len, 0)) { |
353 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
354 | 0 | goto err; |
355 | 0 | } |
356 | 699 | } else { |
357 | 699 | ASN1_BIT_STRING_free(curve->seed); |
358 | 699 | curve->seed = NULL; |
359 | 699 | } |
360 | | |
361 | 975 | ok = 1; |
362 | | |
363 | 975 | err: |
364 | 975 | OPENSSL_free(a_buf); |
365 | 975 | OPENSSL_free(b_buf); |
366 | 975 | BN_free(tmp_1); |
367 | 975 | BN_free(tmp_2); |
368 | 975 | return ok; |
369 | 975 | } |
370 | | |
371 | | ECPARAMETERS *EC_GROUP_get_ecparameters(const EC_GROUP *group, |
372 | | ECPARAMETERS *params) |
373 | 363 | { |
374 | 363 | size_t len = 0; |
375 | 363 | ECPARAMETERS *ret = NULL; |
376 | 363 | const BIGNUM *tmp; |
377 | 363 | unsigned char *buffer = NULL; |
378 | 363 | const EC_POINT *point = NULL; |
379 | 363 | point_conversion_form_t form; |
380 | 363 | ASN1_INTEGER *orig; |
381 | | |
382 | 363 | if (params == NULL) { |
383 | 363 | if ((ret = ECPARAMETERS_new()) == NULL) { |
384 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
385 | 0 | goto err; |
386 | 0 | } |
387 | 363 | } else |
388 | 0 | ret = params; |
389 | | |
390 | | /* set the version (always one) */ |
391 | 363 | ret->version = (long)0x1; |
392 | | |
393 | | /* set the fieldID */ |
394 | 363 | if (!ec_asn1_group2fieldid(group, ret->fieldID)) { |
395 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
396 | 0 | goto err; |
397 | 0 | } |
398 | | |
399 | | /* set the curve */ |
400 | 363 | if (!ec_asn1_group2curve(group, ret->curve)) { |
401 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
402 | 0 | goto err; |
403 | 0 | } |
404 | | |
405 | | /* set the base point */ |
406 | 363 | if ((point = EC_GROUP_get0_generator(group)) == NULL) { |
407 | 0 | ERR_raise(ERR_LIB_EC, EC_R_UNDEFINED_GENERATOR); |
408 | 0 | goto err; |
409 | 0 | } |
410 | | |
411 | 363 | form = EC_GROUP_get_point_conversion_form(group); |
412 | | |
413 | 363 | len = EC_POINT_point2buf(group, point, form, &buffer, NULL); |
414 | 363 | if (len == 0 || len > INT_MAX) { |
415 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
416 | 0 | goto err; |
417 | 0 | } |
418 | 363 | if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) { |
419 | 0 | OPENSSL_free(buffer); |
420 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
421 | 0 | goto err; |
422 | 0 | } |
423 | 363 | ASN1_STRING_set0(ret->base, buffer, (int)len); |
424 | | |
425 | | /* set the order */ |
426 | 363 | tmp = EC_GROUP_get0_order(group); |
427 | 363 | if (tmp == NULL) { |
428 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
429 | 0 | goto err; |
430 | 0 | } |
431 | 363 | ret->order = BN_to_ASN1_INTEGER(tmp, orig = ret->order); |
432 | 363 | if (ret->order == NULL) { |
433 | 0 | ret->order = orig; |
434 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
435 | 0 | goto err; |
436 | 0 | } |
437 | | |
438 | | /* set the cofactor (optional) */ |
439 | 363 | tmp = EC_GROUP_get0_cofactor(group); |
440 | 363 | if (tmp != NULL) { |
441 | 363 | ret->cofactor = BN_to_ASN1_INTEGER(tmp, orig = ret->cofactor); |
442 | 363 | if (ret->cofactor == NULL) { |
443 | 0 | ret->cofactor = orig; |
444 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
445 | 0 | goto err; |
446 | 0 | } |
447 | 363 | } |
448 | | |
449 | 363 | return ret; |
450 | | |
451 | 0 | err: |
452 | 0 | if (params == NULL) |
453 | 0 | ECPARAMETERS_free(ret); |
454 | 0 | return NULL; |
455 | 363 | } |
456 | | |
457 | | ECPKPARAMETERS *EC_GROUP_get_ecpkparameters(const EC_GROUP *group, |
458 | | ECPKPARAMETERS *params) |
459 | 7.24k | { |
460 | 7.24k | int ok = 1, tmp; |
461 | 7.24k | ECPKPARAMETERS *ret = params; |
462 | | |
463 | 7.24k | if (ret == NULL) { |
464 | 7.24k | if ((ret = ECPKPARAMETERS_new()) == NULL) { |
465 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
466 | 0 | return NULL; |
467 | 0 | } |
468 | 7.24k | } else { |
469 | 0 | if (ret->type == ECPKPARAMETERS_TYPE_NAMED) { |
470 | 0 | ASN1_OBJECT_free(ret->value.named_curve); |
471 | 0 | ret->value.named_curve = NULL; |
472 | 0 | } else if (ret->type == ECPKPARAMETERS_TYPE_EXPLICIT |
473 | 0 | && ret->value.parameters != NULL) { |
474 | 0 | ECPARAMETERS_free(ret->value.parameters); |
475 | 0 | ret->value.parameters = NULL; |
476 | 0 | } |
477 | 0 | } |
478 | | |
479 | 7.24k | 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 | 6.26k | tmp = EC_GROUP_get_curve_name(group); |
484 | 6.26k | if (tmp) { |
485 | 6.26k | ASN1_OBJECT *asn1obj = OBJ_nid2obj(tmp); |
486 | | |
487 | 6.26k | 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 | 6.26k | } else { |
492 | 6.26k | ret->type = ECPKPARAMETERS_TYPE_NAMED; |
493 | 6.26k | ret->value.named_curve = asn1obj; |
494 | 6.26k | } |
495 | 6.26k | } else |
496 | | /* we don't know the nid => ERROR */ |
497 | 0 | ok = 0; |
498 | 6.26k | } else { |
499 | | /* use the ECPARAMETERS structure */ |
500 | 975 | ret->type = ECPKPARAMETERS_TYPE_EXPLICIT; |
501 | 975 | if ((ret->value.parameters = EC_GROUP_get_ecparameters(group, NULL)) == NULL) |
502 | 0 | ok = 0; |
503 | 975 | } |
504 | | |
505 | 7.24k | if (!ok) { |
506 | 0 | ECPKPARAMETERS_free(ret); |
507 | 0 | return NULL; |
508 | 0 | } |
509 | 7.24k | return ret; |
510 | 7.24k | } |
511 | | |
512 | | EC_GROUP *EC_GROUP_new_from_ecparameters(const ECPARAMETERS *params) |
513 | 14.9k | { |
514 | 14.9k | int ok = 0, tmp; |
515 | 14.9k | EC_GROUP *ret = NULL, *dup = NULL; |
516 | 14.9k | BIGNUM *p = NULL, *a = NULL, *b = NULL; |
517 | 14.9k | EC_POINT *point = NULL; |
518 | 14.9k | long field_bits; |
519 | 14.9k | int curve_name = NID_undef; |
520 | 14.9k | BN_CTX *ctx = NULL; |
521 | | |
522 | 14.9k | if (params->fieldID == NULL |
523 | 14.9k | || params->fieldID->fieldType == NULL |
524 | 14.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 | 14.9k | if (params->curve == NULL |
536 | 14.9k | || params->curve->a == NULL || params->curve->a->data == NULL |
537 | 14.9k | || params->curve->b == NULL || params->curve->b->data == NULL) { |
538 | 43 | ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); |
539 | 43 | goto err; |
540 | 43 | } |
541 | 14.8k | a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL); |
542 | 14.8k | if (a == NULL) { |
543 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB); |
544 | 0 | goto err; |
545 | 0 | } |
546 | 14.8k | b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL); |
547 | 14.8k | 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 | 14.8k | tmp = OBJ_obj2nid(params->fieldID->fieldType); |
554 | 14.8k | 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_BN_LIB); |
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 | 14.8k | #endif |
641 | 14.8k | else if (tmp == NID_X9_62_prime_field) { |
642 | | /* we have a curve over a prime field */ |
643 | | /* extract the prime number */ |
644 | 14.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 | 14.6k | p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL); |
649 | 14.6k | if (p == NULL) { |
650 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
651 | 0 | goto err; |
652 | 0 | } |
653 | | |
654 | 14.6k | if (BN_is_negative(p) || BN_is_zero(p)) { |
655 | 168 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD); |
656 | 168 | goto err; |
657 | 168 | } |
658 | | |
659 | 14.4k | field_bits = BN_num_bits(p); |
660 | 14.4k | 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 | 14.4k | ret = EC_GROUP_new_curve_GFp(p, a, b, NULL); |
667 | 14.4k | } else { |
668 | 224 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD); |
669 | 224 | goto err; |
670 | 224 | } |
671 | | |
672 | 14.4k | if (ret == NULL) { |
673 | 2.07k | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
674 | 2.07k | goto err; |
675 | 2.07k | } |
676 | | |
677 | | /* extract seed (optional) */ |
678 | 12.4k | 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 | 6.81k | if (params->curve->seed->length == 0) { |
686 | 70 | ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); |
687 | 70 | goto err; |
688 | 70 | } |
689 | 6.74k | OPENSSL_free(ret->seed); |
690 | 6.74k | if ((ret->seed = OPENSSL_malloc(params->curve->seed->length)) == NULL) |
691 | 0 | goto err; |
692 | 6.74k | memcpy(ret->seed, params->curve->seed->data, |
693 | 6.74k | params->curve->seed->length); |
694 | 6.74k | ret->seed_len = params->curve->seed->length; |
695 | 6.74k | } |
696 | | |
697 | 12.3k | if (params->order == NULL |
698 | 12.3k | || params->base == NULL |
699 | 12.3k | || params->base->data == NULL |
700 | 12.3k | || params->base->length == 0) { |
701 | 173 | ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); |
702 | 173 | goto err; |
703 | 173 | } |
704 | | |
705 | 12.1k | if ((point = EC_POINT_new(ret)) == NULL) |
706 | 0 | goto err; |
707 | | |
708 | | /* set the point conversion form */ |
709 | 12.1k | EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)(params->base->data[0] & ~0x01)); |
710 | | |
711 | | /* extract the ec point */ |
712 | 12.1k | if (!EC_POINT_oct2point(ret, point, params->base->data, |
713 | 12.1k | params->base->length, NULL)) { |
714 | 4.75k | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
715 | 4.75k | goto err; |
716 | 4.75k | } |
717 | | |
718 | | /* extract the order */ |
719 | 7.41k | if (ASN1_INTEGER_to_BN(params->order, a) == NULL) { |
720 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
721 | 0 | goto err; |
722 | 0 | } |
723 | 7.41k | if (BN_is_negative(a) || BN_is_zero(a)) { |
724 | 417 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER); |
725 | 417 | goto err; |
726 | 417 | } |
727 | 6.99k | if (BN_num_bits(a) > (int)field_bits + 1) { /* Hasse bound */ |
728 | 195 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER); |
729 | 195 | goto err; |
730 | 195 | } |
731 | | |
732 | | /* extract the cofactor (optional) */ |
733 | 6.80k | if (params->cofactor == NULL) { |
734 | 3.59k | BN_free(b); |
735 | 3.59k | b = NULL; |
736 | 3.59k | } else if (ASN1_INTEGER_to_BN(params->cofactor, b) == NULL) { |
737 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
738 | 0 | goto err; |
739 | 0 | } |
740 | | /* set the generator, order and cofactor (if present) */ |
741 | 6.80k | if (!EC_GROUP_set_generator(ret, point, a, b)) { |
742 | 154 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
743 | 154 | goto err; |
744 | 154 | } |
745 | | |
746 | | /* |
747 | | * Check if the explicit parameters group just created matches one of the |
748 | | * built-in curves. |
749 | | * |
750 | | * We create a copy of the group just built, so that we can remove optional |
751 | | * fields for the lookup: we do this to avoid the possibility that one of |
752 | | * the optional parameters is used to force the library into using a less |
753 | | * performant and less secure EC_METHOD instead of the specialized one. |
754 | | * In any case, `seed` is not really used in any computation, while a |
755 | | * cofactor different from the one in the built-in table is just |
756 | | * mathematically wrong anyway and should not be used. |
757 | | */ |
758 | 6.64k | if ((ctx = BN_CTX_new()) == NULL) { |
759 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB); |
760 | 0 | goto err; |
761 | 0 | } |
762 | 6.64k | if ((dup = EC_GROUP_dup(ret)) == NULL |
763 | 6.64k | || EC_GROUP_set_seed(dup, NULL, 0) != 1 |
764 | 6.64k | || !EC_GROUP_set_generator(dup, point, a, NULL)) { |
765 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
766 | 0 | goto err; |
767 | 0 | } |
768 | 6.64k | if ((curve_name = ossl_ec_curve_nid_from_params(dup, ctx)) != NID_undef) { |
769 | | /* |
770 | | * The input explicit parameters successfully matched one of the |
771 | | * built-in curves: often for built-in curves we have specialized |
772 | | * methods with better performance and hardening. |
773 | | * |
774 | | * In this case we replace the `EC_GROUP` created through explicit |
775 | | * parameters with one created from a named group. |
776 | | */ |
777 | 1.82k | EC_GROUP *named_group = NULL; |
778 | | |
779 | 1.82k | #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128 |
780 | | /* |
781 | | * NID_wap_wsg_idm_ecid_wtls12 and NID_secp224r1 are both aliases for |
782 | | * the same curve, we prefer the SECP nid when matching explicit |
783 | | * parameters as that is associated with a specialized EC_METHOD. |
784 | | */ |
785 | 1.82k | if (curve_name == NID_wap_wsg_idm_ecid_wtls12) |
786 | 0 | curve_name = NID_secp224r1; |
787 | 1.82k | #endif /* !def(OPENSSL_NO_EC_NISTP_64_GCC_128) */ |
788 | | |
789 | 1.82k | if ((named_group = EC_GROUP_new_by_curve_name(curve_name)) == NULL) { |
790 | 1.52k | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
791 | 1.52k | goto err; |
792 | 1.52k | } |
793 | 302 | EC_GROUP_free(ret); |
794 | 302 | ret = named_group; |
795 | | |
796 | | /* |
797 | | * Set the flag so that EC_GROUPs created from explicit parameters are |
798 | | * serialized using explicit parameters by default. |
799 | | */ |
800 | 302 | EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_EXPLICIT_CURVE); |
801 | | |
802 | | /* |
803 | | * If the input params do not contain the optional seed field we make |
804 | | * sure it is not added to the returned group. |
805 | | * |
806 | | * The seed field is not really used inside libcrypto anyway, and |
807 | | * adding it to parsed explicit parameter keys would alter their DER |
808 | | * encoding output (because of the extra field) which could impact |
809 | | * applications fingerprinting keys by their DER encoding. |
810 | | */ |
811 | 302 | if (params->curve->seed == NULL) { |
812 | 103 | if (EC_GROUP_set_seed(ret, NULL, 0) != 1) |
813 | 0 | goto err; |
814 | 103 | } |
815 | 302 | } |
816 | | |
817 | 5.12k | ok = 1; |
818 | | |
819 | 14.9k | err: |
820 | 14.9k | if (!ok) { |
821 | 9.79k | EC_GROUP_free(ret); |
822 | 9.79k | ret = NULL; |
823 | 9.79k | } |
824 | 14.9k | EC_GROUP_free(dup); |
825 | | |
826 | 14.9k | BN_free(p); |
827 | 14.9k | BN_free(a); |
828 | 14.9k | BN_free(b); |
829 | 14.9k | EC_POINT_free(point); |
830 | | |
831 | 14.9k | BN_CTX_free(ctx); |
832 | | |
833 | 14.9k | return ret; |
834 | 5.12k | } |
835 | | |
836 | | EC_GROUP *EC_GROUP_new_from_ecpkparameters(const ECPKPARAMETERS *params) |
837 | 101k | { |
838 | 101k | EC_GROUP *ret = NULL; |
839 | 101k | int tmp = 0; |
840 | | |
841 | 101k | if (params == NULL) { |
842 | 0 | ERR_raise(ERR_LIB_EC, EC_R_MISSING_PARAMETERS); |
843 | 0 | return NULL; |
844 | 0 | } |
845 | | |
846 | 101k | if (params->type == ECPKPARAMETERS_TYPE_NAMED) { |
847 | | /* the curve is given by an OID */ |
848 | 86.1k | tmp = OBJ_obj2nid(params->value.named_curve); |
849 | 86.1k | if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) { |
850 | 12.3k | ERR_raise(ERR_LIB_EC, EC_R_EC_GROUP_NEW_BY_NAME_FAILURE); |
851 | 12.3k | return NULL; |
852 | 12.3k | } |
853 | 73.7k | EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE); |
854 | 73.7k | } else if (params->type == ECPKPARAMETERS_TYPE_EXPLICIT) { |
855 | | /* the parameters are given by an ECPARAMETERS structure */ |
856 | 14.9k | ret = EC_GROUP_new_from_ecparameters(params->value.parameters); |
857 | 14.9k | if (!ret) { |
858 | 9.79k | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
859 | 9.79k | return NULL; |
860 | 9.79k | } |
861 | 5.12k | EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_EXPLICIT_CURVE); |
862 | 5.12k | } else if (params->type == ECPKPARAMETERS_TYPE_IMPLICIT) { |
863 | | /* implicit parameters inherited from CA - unsupported */ |
864 | 86 | return NULL; |
865 | 86 | } else { |
866 | 0 | ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); |
867 | 0 | return NULL; |
868 | 0 | } |
869 | | |
870 | 78.8k | return ret; |
871 | 101k | } |
872 | | |
873 | | /* EC_GROUP <-> DER encoding of ECPKPARAMETERS */ |
874 | | |
875 | | EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len) |
876 | 119k | { |
877 | 119k | EC_GROUP *group = NULL; |
878 | 119k | ECPKPARAMETERS *params = NULL; |
879 | 119k | const unsigned char *p = *in; |
880 | | |
881 | 119k | if ((params = d2i_ECPKPARAMETERS(NULL, &p, len)) == NULL) { |
882 | 108k | ECPKPARAMETERS_free(params); |
883 | 108k | return NULL; |
884 | 108k | } |
885 | | |
886 | 11.3k | if ((group = EC_GROUP_new_from_ecpkparameters(params)) == NULL) { |
887 | 9.90k | ECPKPARAMETERS_free(params); |
888 | 9.90k | return NULL; |
889 | 9.90k | } |
890 | | |
891 | 1.48k | if (params->type == ECPKPARAMETERS_TYPE_EXPLICIT) |
892 | 783 | group->decoded_from_explicit_params = 1; |
893 | 1.48k | #ifdef OPENSSL_NO_EC_EXPLICIT_CURVES |
894 | 1.48k | if (EC_GROUP_check_named_curve(group, 0, NULL) == NID_undef) { |
895 | 624 | EC_GROUP_free(group); |
896 | 624 | ECPKPARAMETERS_free(params); |
897 | 624 | ERR_raise(ERR_LIB_EC, EC_R_UNKNOWN_GROUP); |
898 | 624 | return NULL; |
899 | 624 | } |
900 | 857 | #endif |
901 | | |
902 | 857 | if (a) { |
903 | 640 | EC_GROUP_free(*a); |
904 | 640 | *a = group; |
905 | 640 | } |
906 | | |
907 | 857 | ECPKPARAMETERS_free(params); |
908 | 857 | *in = p; |
909 | 857 | return group; |
910 | 1.48k | } |
911 | | |
912 | | int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out) |
913 | 1.00k | { |
914 | 1.00k | int ret = 0; |
915 | 1.00k | ECPKPARAMETERS *tmp = EC_GROUP_get_ecpkparameters(a, NULL); |
916 | 1.00k | if (tmp == NULL) { |
917 | 0 | ERR_raise(ERR_LIB_EC, EC_R_GROUP2PKPARAMETERS_FAILURE); |
918 | 0 | return 0; |
919 | 0 | } |
920 | 1.00k | if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) { |
921 | 0 | ERR_raise(ERR_LIB_EC, EC_R_I2D_ECPKPARAMETERS_FAILURE); |
922 | 0 | ECPKPARAMETERS_free(tmp); |
923 | 0 | return 0; |
924 | 0 | } |
925 | 1.00k | ECPKPARAMETERS_free(tmp); |
926 | 1.00k | return ret; |
927 | 1.00k | } |
928 | | |
929 | | /* some EC_KEY functions */ |
930 | | |
931 | | EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len) |
932 | 104k | { |
933 | 104k | EC_KEY *ret = NULL; |
934 | 104k | EC_PRIVATEKEY *priv_key = NULL; |
935 | 104k | const unsigned char *p = *in; |
936 | | |
937 | 104k | if ((priv_key = d2i_EC_PRIVATEKEY(NULL, &p, len)) == NULL) |
938 | 79.0k | return NULL; |
939 | | |
940 | 25.0k | if (a == NULL || *a == NULL) { |
941 | 24.6k | if ((ret = EC_KEY_new()) == NULL) { |
942 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
943 | 0 | goto err; |
944 | 0 | } |
945 | 24.6k | } else |
946 | 345 | ret = *a; |
947 | | |
948 | 25.0k | if (priv_key->parameters) { |
949 | 24.6k | EC_GROUP_free(ret->group); |
950 | 24.6k | ret->group = EC_GROUP_new_from_ecpkparameters(priv_key->parameters); |
951 | 24.6k | if (ret->group != NULL |
952 | 24.1k | && priv_key->parameters->type == ECPKPARAMETERS_TYPE_EXPLICIT) |
953 | 223 | ret->group->decoded_from_explicit_params = 1; |
954 | 24.6k | } |
955 | | |
956 | 25.0k | if (ret->group == NULL) { |
957 | 591 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
958 | 591 | goto err; |
959 | 591 | } |
960 | | |
961 | 24.4k | #ifdef OPENSSL_NO_EC_EXPLICIT_CURVES |
962 | 24.4k | if (EC_GROUP_check_named_curve(ret->group, 0, NULL) == NID_undef) { |
963 | 219 | ERR_raise(ERR_LIB_EC, EC_R_UNKNOWN_GROUP); |
964 | 219 | goto err; |
965 | 219 | } |
966 | 24.2k | #endif |
967 | | |
968 | 24.2k | ret->version = priv_key->version; |
969 | | |
970 | 24.2k | if (priv_key->privateKey) { |
971 | 24.2k | ASN1_OCTET_STRING *pkey = priv_key->privateKey; |
972 | 24.2k | size_t pkey_len = ASN1_STRING_get_length(pkey); |
973 | 24.2k | if (pkey_len > INT_MAX) |
974 | 0 | goto err; |
975 | 24.2k | if (EC_KEY_oct2priv(ret, ASN1_STRING_get0_data(pkey), (int)pkey_len) == 0) |
976 | 0 | goto err; |
977 | 24.2k | } else { |
978 | 0 | ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY); |
979 | 0 | goto err; |
980 | 0 | } |
981 | | |
982 | 24.2k | if (EC_GROUP_get_curve_name(ret->group) == NID_sm2) |
983 | 77 | EC_KEY_set_flags(ret, EC_FLAG_SM2_RANGE); |
984 | | |
985 | 24.2k | EC_POINT_clear_free(ret->pub_key); |
986 | 24.2k | ret->pub_key = EC_POINT_new(ret->group); |
987 | 24.2k | if (ret->pub_key == NULL) { |
988 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
989 | 0 | goto err; |
990 | 0 | } |
991 | | |
992 | 24.2k | if (priv_key->publicKey) { |
993 | 19.9k | const unsigned char *pub_oct; |
994 | 19.9k | size_t pub_oct_len; |
995 | | |
996 | 19.9k | pub_oct = ASN1_STRING_get0_data(priv_key->publicKey); |
997 | 19.9k | pub_oct_len = ASN1_STRING_get_length(priv_key->publicKey); |
998 | 19.9k | if (pub_oct_len > INT_MAX) |
999 | 0 | goto err; |
1000 | 19.9k | if (!EC_KEY_oct2key(ret, pub_oct, (int)pub_oct_len, NULL)) { |
1001 | 1.23k | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
1002 | 1.23k | goto err; |
1003 | 1.23k | } |
1004 | 19.9k | } else { |
1005 | 4.28k | if (ret->group->meth->keygenpub == NULL |
1006 | 4.28k | || ret->group->meth->keygenpub(ret) == 0) |
1007 | 0 | goto err; |
1008 | | /* Remember the original private-key-only encoding. */ |
1009 | 4.28k | ret->enc_flag |= EC_PKEY_NO_PUBKEY; |
1010 | 4.28k | } |
1011 | | |
1012 | 22.9k | if (a) |
1013 | 273 | *a = ret; |
1014 | 22.9k | EC_PRIVATEKEY_free(priv_key); |
1015 | 22.9k | *in = p; |
1016 | 22.9k | ret->dirty_cnt++; |
1017 | 22.9k | return ret; |
1018 | | |
1019 | 2.04k | err: |
1020 | 2.04k | if (a == NULL || *a != ret) |
1021 | 1.96k | EC_KEY_free(ret); |
1022 | 2.04k | EC_PRIVATEKEY_free(priv_key); |
1023 | 2.04k | return NULL; |
1024 | 24.2k | } |
1025 | | |
1026 | | int i2d_ECPrivateKey(const EC_KEY *a, unsigned char **out) |
1027 | 6.13k | { |
1028 | 6.13k | int ret = 0, ok = 0; |
1029 | 6.13k | unsigned char *priv = NULL, *pub = NULL; |
1030 | 6.13k | size_t privlen = 0, publen = 0; |
1031 | | |
1032 | 6.13k | EC_PRIVATEKEY *priv_key = NULL; |
1033 | | |
1034 | 6.13k | if (a == NULL || a->group == NULL || (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key == NULL)) { |
1035 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); |
1036 | 0 | goto err; |
1037 | 0 | } |
1038 | | |
1039 | 6.13k | if ((priv_key = EC_PRIVATEKEY_new()) == NULL) { |
1040 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
1041 | 0 | goto err; |
1042 | 0 | } |
1043 | | |
1044 | 6.13k | priv_key->version = a->version; |
1045 | | |
1046 | 6.13k | privlen = EC_KEY_priv2buf(a, &priv); |
1047 | | |
1048 | 6.13k | if (privlen == 0 || privlen > INT_MAX) { |
1049 | 2.31k | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
1050 | 2.31k | goto err; |
1051 | 2.31k | } |
1052 | | |
1053 | 3.82k | ASN1_STRING_set0(priv_key->privateKey, priv, (int)privlen); |
1054 | 3.82k | priv = NULL; |
1055 | | |
1056 | 3.82k | if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) { |
1057 | 3.80k | if ((priv_key->parameters = EC_GROUP_get_ecpkparameters(a->group, |
1058 | 3.80k | priv_key->parameters)) |
1059 | 3.80k | == NULL) { |
1060 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
1061 | 0 | goto err; |
1062 | 0 | } |
1063 | 3.80k | } |
1064 | | |
1065 | 3.82k | if (!(a->enc_flag & EC_PKEY_NO_PUBKEY)) { |
1066 | 1.27k | priv_key->publicKey = ASN1_BIT_STRING_new(); |
1067 | 1.27k | if (priv_key->publicKey == NULL) { |
1068 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
1069 | 0 | goto err; |
1070 | 0 | } |
1071 | | |
1072 | 1.27k | publen = EC_KEY_key2buf(a, EC_KEY_get_conv_form(a), &pub, NULL); |
1073 | | |
1074 | 1.27k | if (publen == 0 || publen > INT_MAX) { |
1075 | 67 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
1076 | 67 | goto err; |
1077 | 67 | } |
1078 | | |
1079 | 1.21k | ossl_asn1_bit_string_set_unused_bits(priv_key->publicKey, 0); |
1080 | 1.21k | ASN1_STRING_set0(priv_key->publicKey, pub, (int)publen); |
1081 | 1.21k | pub = NULL; |
1082 | 1.21k | } |
1083 | | |
1084 | 3.75k | if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) { |
1085 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
1086 | 0 | goto err; |
1087 | 0 | } |
1088 | 3.75k | ok = 1; |
1089 | 6.13k | err: |
1090 | 6.13k | OPENSSL_clear_free(priv, privlen); |
1091 | 6.13k | OPENSSL_free(pub); |
1092 | 6.13k | EC_PRIVATEKEY_free(priv_key); |
1093 | 6.13k | return ok ? ret : 0; |
1094 | 3.75k | } |
1095 | | |
1096 | | int i2d_ECParameters(const EC_KEY *a, unsigned char **out) |
1097 | 576 | { |
1098 | 576 | if (a == NULL) { |
1099 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); |
1100 | 0 | return 0; |
1101 | 0 | } |
1102 | 576 | return i2d_ECPKParameters(a->group, out); |
1103 | 576 | } |
1104 | | |
1105 | | EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len) |
1106 | 171k | { |
1107 | 171k | EC_KEY *ret; |
1108 | | |
1109 | 171k | if (in == NULL || *in == NULL) { |
1110 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); |
1111 | 0 | return NULL; |
1112 | 0 | } |
1113 | | |
1114 | 171k | if (a == NULL || *a == NULL) { |
1115 | 137k | if ((ret = EC_KEY_new()) == NULL) { |
1116 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
1117 | 0 | return NULL; |
1118 | 0 | } |
1119 | 137k | } else |
1120 | 33.8k | ret = *a; |
1121 | | |
1122 | 171k | if (!d2i_ECPKParameters(&ret->group, in, len)) { |
1123 | 168k | if (a == NULL || *a != ret) |
1124 | 135k | EC_KEY_free(ret); |
1125 | 33.0k | else |
1126 | 33.0k | ret->dirty_cnt++; |
1127 | 168k | return NULL; |
1128 | 168k | } |
1129 | | |
1130 | 2.52k | if (EC_GROUP_get_curve_name(ret->group) == NID_sm2) |
1131 | 13 | EC_KEY_set_flags(ret, EC_FLAG_SM2_RANGE); |
1132 | | |
1133 | 2.52k | ret->dirty_cnt++; |
1134 | | |
1135 | 2.52k | if (a) |
1136 | 845 | *a = ret; |
1137 | | |
1138 | 2.52k | return ret; |
1139 | 171k | } |
1140 | | |
1141 | | EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len) |
1142 | 570k | { |
1143 | 570k | EC_KEY *ret = NULL; |
1144 | | |
1145 | 570k | if (a == NULL || (*a) == NULL || (*a)->group == NULL) { |
1146 | | /* |
1147 | | * sorry, but a EC_GROUP-structure is necessary to set the public key |
1148 | | */ |
1149 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); |
1150 | 0 | return 0; |
1151 | 0 | } |
1152 | 570k | ret = *a; |
1153 | | /* EC_KEY_opt2key updates dirty_cnt */ |
1154 | 570k | if (!EC_KEY_oct2key(ret, *in, len, NULL)) { |
1155 | 273k | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
1156 | 273k | return 0; |
1157 | 273k | } |
1158 | 296k | *in += len; |
1159 | 296k | return ret; |
1160 | 570k | } |
1161 | | |
1162 | | int i2o_ECPublicKey(const EC_KEY *a, unsigned char **out) |
1163 | 0 | { |
1164 | 0 | size_t buf_len = 0; |
1165 | 0 | int new_buffer = 0; |
1166 | |
|
1167 | 0 | if (a == NULL || a->pub_key == NULL) { |
1168 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); |
1169 | 0 | return 0; |
1170 | 0 | } |
1171 | | |
1172 | 0 | buf_len = EC_POINT_point2oct(a->group, a->pub_key, |
1173 | 0 | EC_KEY_get_conv_form(a), NULL, 0, NULL); |
1174 | |
|
1175 | 0 | if (buf_len > INT_MAX) { |
1176 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_PASSED_INVALID_ARGUMENT); |
1177 | 0 | return 0; |
1178 | 0 | } |
1179 | 0 | if (out == NULL || buf_len == 0) |
1180 | | /* out == NULL => just return the length of the octet string */ |
1181 | 0 | return (int)buf_len; |
1182 | | |
1183 | 0 | if (*out == NULL) { |
1184 | 0 | if ((*out = OPENSSL_malloc(buf_len)) == NULL) |
1185 | 0 | return 0; |
1186 | 0 | new_buffer = 1; |
1187 | 0 | } |
1188 | 0 | if (!EC_POINT_point2oct(a->group, a->pub_key, EC_KEY_get_conv_form(a), |
1189 | 0 | *out, buf_len, NULL)) { |
1190 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
1191 | 0 | if (new_buffer) { |
1192 | 0 | OPENSSL_free(*out); |
1193 | 0 | *out = NULL; |
1194 | 0 | } |
1195 | 0 | return 0; |
1196 | 0 | } |
1197 | 0 | if (!new_buffer) |
1198 | 0 | *out += buf_len; |
1199 | 0 | return (int)buf_len; |
1200 | 0 | } |
1201 | | |
1202 | | DECLARE_ASN1_FUNCTIONS(ECDSA_SIG) |
1203 | | DECLARE_ASN1_ENCODE_FUNCTIONS_name(ECDSA_SIG, ECDSA_SIG) |
1204 | | |
1205 | | #endif /* FIPS_MODULE */ |
1206 | | |
1207 | | ECDSA_SIG *ECDSA_SIG_new(void) |
1208 | 77.0k | { |
1209 | 77.0k | ECDSA_SIG *sig = OPENSSL_zalloc(sizeof(*sig)); |
1210 | | |
1211 | 77.0k | return sig; |
1212 | 77.0k | } |
1213 | | |
1214 | | void ECDSA_SIG_free(ECDSA_SIG *sig) |
1215 | 77.0k | { |
1216 | 77.0k | if (sig == NULL) |
1217 | 0 | return; |
1218 | 77.0k | BN_clear_free(sig->r); |
1219 | 77.0k | BN_clear_free(sig->s); |
1220 | 77.0k | OPENSSL_free(sig); |
1221 | 77.0k | } |
1222 | | |
1223 | | ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **psig, const unsigned char **ppin, long len) |
1224 | 70.8k | { |
1225 | 70.8k | ECDSA_SIG *sig; |
1226 | | |
1227 | 70.8k | if (len < 0) |
1228 | 0 | return NULL; |
1229 | 70.8k | if (psig != NULL && *psig != NULL) { |
1230 | 3.54k | sig = *psig; |
1231 | 67.3k | } else { |
1232 | 67.3k | sig = ECDSA_SIG_new(); |
1233 | 67.3k | if (sig == NULL) |
1234 | 0 | return NULL; |
1235 | 67.3k | } |
1236 | 70.8k | if (sig->r == NULL) |
1237 | 70.8k | sig->r = BN_new(); |
1238 | 70.8k | if (sig->s == NULL) |
1239 | 70.8k | sig->s = BN_new(); |
1240 | 70.8k | if (sig->r == NULL || sig->s == NULL |
1241 | 70.8k | || ossl_decode_der_dsa_sig(sig->r, sig->s, ppin, (size_t)len) == 0) { |
1242 | 68.1k | if (psig == NULL || *psig == NULL) |
1243 | 67.1k | ECDSA_SIG_free(sig); |
1244 | 68.1k | return NULL; |
1245 | 68.1k | } |
1246 | 2.66k | if (psig != NULL && *psig == NULL) |
1247 | 0 | *psig = sig; |
1248 | 2.66k | return sig; |
1249 | 70.8k | } |
1250 | | |
1251 | | int i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **ppout) |
1252 | 400k | { |
1253 | 400k | BUF_MEM *buf = NULL; |
1254 | 400k | size_t encoded_len; |
1255 | 400k | WPACKET pkt; |
1256 | | |
1257 | 400k | if (ppout == NULL) { |
1258 | 391k | if (!WPACKET_init_null(&pkt, 0)) |
1259 | 0 | return -1; |
1260 | 391k | } else if (*ppout == NULL) { |
1261 | 2.66k | if ((buf = BUF_MEM_new()) == NULL |
1262 | 2.66k | || !WPACKET_init_len(&pkt, buf, 0)) { |
1263 | 0 | BUF_MEM_free(buf); |
1264 | 0 | return -1; |
1265 | 0 | } |
1266 | 6.17k | } else { |
1267 | 6.17k | if (!WPACKET_init_static_len(&pkt, *ppout, SIZE_MAX, 0)) |
1268 | 0 | return -1; |
1269 | 6.17k | } |
1270 | | |
1271 | 400k | if (!ossl_encode_der_dsa_sig(&pkt, sig->r, sig->s) |
1272 | 400k | || !WPACKET_get_total_written(&pkt, &encoded_len) |
1273 | 400k | || !WPACKET_finish(&pkt)) { |
1274 | 0 | BUF_MEM_free(buf); |
1275 | 0 | WPACKET_cleanup(&pkt); |
1276 | 0 | return -1; |
1277 | 0 | } |
1278 | | |
1279 | 400k | if (ppout != NULL) { |
1280 | 8.83k | if (*ppout == NULL) { |
1281 | 2.66k | *ppout = (unsigned char *)buf->data; |
1282 | 2.66k | buf->data = NULL; |
1283 | 2.66k | BUF_MEM_free(buf); |
1284 | 6.17k | } else { |
1285 | 6.17k | *ppout += encoded_len; |
1286 | 6.17k | } |
1287 | 8.83k | } |
1288 | | |
1289 | 400k | return (int)encoded_len; |
1290 | 400k | } |
1291 | | |
1292 | | void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps) |
1293 | 62 | { |
1294 | 62 | if (pr != NULL) |
1295 | 62 | *pr = sig->r; |
1296 | 62 | if (ps != NULL) |
1297 | 62 | *ps = sig->s; |
1298 | 62 | } |
1299 | | |
1300 | | const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig) |
1301 | 0 | { |
1302 | 0 | return sig->r; |
1303 | 0 | } |
1304 | | |
1305 | | const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig) |
1306 | 0 | { |
1307 | 0 | return sig->s; |
1308 | 0 | } |
1309 | | |
1310 | | int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s) |
1311 | 0 | { |
1312 | 0 | if (r == NULL || s == NULL) |
1313 | 0 | return 0; |
1314 | 0 | BN_clear_free(sig->r); |
1315 | 0 | BN_clear_free(sig->s); |
1316 | 0 | sig->r = r; |
1317 | 0 | sig->s = s; |
1318 | 0 | return 1; |
1319 | 0 | } |
1320 | | |
1321 | | int ECDSA_size(const EC_KEY *ec) |
1322 | 391k | { |
1323 | 391k | int ret; |
1324 | 391k | ECDSA_SIG sig; |
1325 | 391k | const EC_GROUP *group; |
1326 | 391k | const BIGNUM *bn; |
1327 | | |
1328 | 391k | if (ec == NULL) |
1329 | 0 | return 0; |
1330 | 391k | group = EC_KEY_get0_group(ec); |
1331 | 391k | if (group == NULL) |
1332 | 0 | return 0; |
1333 | | |
1334 | 391k | bn = EC_GROUP_get0_order(group); |
1335 | 391k | if (bn == NULL) |
1336 | 0 | return 0; |
1337 | | |
1338 | 391k | sig.r = sig.s = (BIGNUM *)bn; |
1339 | 391k | ret = i2d_ECDSA_SIG(&sig, NULL); |
1340 | | |
1341 | 391k | if (ret < 0) |
1342 | 0 | ret = 0; |
1343 | 391k | return ret; |
1344 | 391k | } |