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