/src/openssl/crypto/ec/ec_asn1.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2002-2018 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_lcl.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 | 0 |
|
21 | 0 | if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) != |
22 | 0 | NID_X9_62_characteristic_two_field) |
23 | 0 | /* everything else is currently not supported */ |
24 | 0 | return 0; |
25 | 0 | |
26 | 0 | /* 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 | 0 |
|
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 | 0 | /* 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 | 0 | |
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 | 0 |
|
56 | 0 | if (k) |
57 | 0 | *k = group->poly[1]; |
58 | 0 |
|
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 | 0 | |
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 | 0 |
|
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 | 0 |
|
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 | | struct ecpk_parameters_st { |
141 | | int type; |
142 | | union { |
143 | | ASN1_OBJECT *named_curve; |
144 | | ECPARAMETERS *parameters; |
145 | | ASN1_NULL *implicitlyCA; |
146 | | } value; |
147 | | } /* ECPKPARAMETERS */ ; |
148 | | |
149 | | /* SEC1 ECPrivateKey */ |
150 | | typedef struct ec_privatekey_st { |
151 | | int32_t version; |
152 | | ASN1_OCTET_STRING *privateKey; |
153 | | ECPKPARAMETERS *parameters; |
154 | | ASN1_BIT_STRING *publicKey; |
155 | | } EC_PRIVATEKEY; |
156 | | |
157 | | /* the OpenSSL ASN.1 definitions */ |
158 | | ASN1_SEQUENCE(X9_62_PENTANOMIAL) = { |
159 | | ASN1_EMBED(X9_62_PENTANOMIAL, k1, INT32), |
160 | | ASN1_EMBED(X9_62_PENTANOMIAL, k2, INT32), |
161 | | ASN1_EMBED(X9_62_PENTANOMIAL, k3, INT32) |
162 | | } static_ASN1_SEQUENCE_END(X9_62_PENTANOMIAL) |
163 | | |
164 | | DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL) |
165 | | IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL) |
166 | | |
167 | | ASN1_ADB_TEMPLATE(char_two_def) = ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.other, ASN1_ANY); |
168 | | |
169 | | ASN1_ADB(X9_62_CHARACTERISTIC_TWO) = { |
170 | | ADB_ENTRY(NID_X9_62_onBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.onBasis, ASN1_NULL)), |
171 | | ADB_ENTRY(NID_X9_62_tpBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.tpBasis, ASN1_INTEGER)), |
172 | | ADB_ENTRY(NID_X9_62_ppBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.ppBasis, X9_62_PENTANOMIAL)) |
173 | | } ASN1_ADB_END(X9_62_CHARACTERISTIC_TWO, 0, type, 0, &char_two_def_tt, NULL); |
174 | | |
175 | | ASN1_SEQUENCE(X9_62_CHARACTERISTIC_TWO) = { |
176 | | ASN1_EMBED(X9_62_CHARACTERISTIC_TWO, m, INT32), |
177 | | ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, type, ASN1_OBJECT), |
178 | | ASN1_ADB_OBJECT(X9_62_CHARACTERISTIC_TWO) |
179 | | } static_ASN1_SEQUENCE_END(X9_62_CHARACTERISTIC_TWO) |
180 | | |
181 | | DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO) |
182 | | IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO) |
183 | | |
184 | | ASN1_ADB_TEMPLATE(fieldID_def) = ASN1_SIMPLE(X9_62_FIELDID, p.other, ASN1_ANY); |
185 | | |
186 | | ASN1_ADB(X9_62_FIELDID) = { |
187 | | ADB_ENTRY(NID_X9_62_prime_field, ASN1_SIMPLE(X9_62_FIELDID, p.prime, ASN1_INTEGER)), |
188 | | ADB_ENTRY(NID_X9_62_characteristic_two_field, ASN1_SIMPLE(X9_62_FIELDID, p.char_two, X9_62_CHARACTERISTIC_TWO)) |
189 | | } ASN1_ADB_END(X9_62_FIELDID, 0, fieldType, 0, &fieldID_def_tt, NULL); |
190 | | |
191 | | ASN1_SEQUENCE(X9_62_FIELDID) = { |
192 | | ASN1_SIMPLE(X9_62_FIELDID, fieldType, ASN1_OBJECT), |
193 | | ASN1_ADB_OBJECT(X9_62_FIELDID) |
194 | | } static_ASN1_SEQUENCE_END(X9_62_FIELDID) |
195 | | |
196 | | ASN1_SEQUENCE(X9_62_CURVE) = { |
197 | | ASN1_SIMPLE(X9_62_CURVE, a, ASN1_OCTET_STRING), |
198 | | ASN1_SIMPLE(X9_62_CURVE, b, ASN1_OCTET_STRING), |
199 | | ASN1_OPT(X9_62_CURVE, seed, ASN1_BIT_STRING) |
200 | | } static_ASN1_SEQUENCE_END(X9_62_CURVE) |
201 | | |
202 | | ASN1_SEQUENCE(ECPARAMETERS) = { |
203 | | ASN1_EMBED(ECPARAMETERS, version, INT32), |
204 | | ASN1_SIMPLE(ECPARAMETERS, fieldID, X9_62_FIELDID), |
205 | | ASN1_SIMPLE(ECPARAMETERS, curve, X9_62_CURVE), |
206 | | ASN1_SIMPLE(ECPARAMETERS, base, ASN1_OCTET_STRING), |
207 | | ASN1_SIMPLE(ECPARAMETERS, order, ASN1_INTEGER), |
208 | | ASN1_OPT(ECPARAMETERS, cofactor, ASN1_INTEGER) |
209 | | } ASN1_SEQUENCE_END(ECPARAMETERS) |
210 | | |
211 | | DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS) |
212 | | IMPLEMENT_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS) |
213 | | |
214 | | ASN1_CHOICE(ECPKPARAMETERS) = { |
215 | | ASN1_SIMPLE(ECPKPARAMETERS, value.named_curve, ASN1_OBJECT), |
216 | | ASN1_SIMPLE(ECPKPARAMETERS, value.parameters, ECPARAMETERS), |
217 | | ASN1_SIMPLE(ECPKPARAMETERS, value.implicitlyCA, ASN1_NULL) |
218 | | } ASN1_CHOICE_END(ECPKPARAMETERS) |
219 | | |
220 | | DECLARE_ASN1_FUNCTIONS_const(ECPKPARAMETERS) |
221 | | DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECPKPARAMETERS, ECPKPARAMETERS) |
222 | | IMPLEMENT_ASN1_FUNCTIONS_const(ECPKPARAMETERS) |
223 | | |
224 | | ASN1_SEQUENCE(EC_PRIVATEKEY) = { |
225 | | ASN1_EMBED(EC_PRIVATEKEY, version, INT32), |
226 | | ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING), |
227 | | ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0), |
228 | | ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1) |
229 | | } static_ASN1_SEQUENCE_END(EC_PRIVATEKEY) |
230 | | |
231 | | DECLARE_ASN1_FUNCTIONS_const(EC_PRIVATEKEY) |
232 | | DECLARE_ASN1_ENCODE_FUNCTIONS_const(EC_PRIVATEKEY, EC_PRIVATEKEY) |
233 | | IMPLEMENT_ASN1_FUNCTIONS_const(EC_PRIVATEKEY) |
234 | | |
235 | | /* some declarations of internal function */ |
236 | | |
237 | | /* ec_asn1_group2field() sets the values in a X9_62_FIELDID object */ |
238 | | static int ec_asn1_group2fieldid(const EC_GROUP *, X9_62_FIELDID *); |
239 | | /* ec_asn1_group2curve() sets the values in a X9_62_CURVE object */ |
240 | | static int ec_asn1_group2curve(const EC_GROUP *, X9_62_CURVE *); |
241 | | |
242 | | /* the function definitions */ |
243 | | |
244 | | static int ec_asn1_group2fieldid(const EC_GROUP *group, X9_62_FIELDID *field) |
245 | 0 | { |
246 | 0 | int ok = 0, nid; |
247 | 0 | BIGNUM *tmp = NULL; |
248 | 0 |
|
249 | 0 | if (group == NULL || field == NULL) |
250 | 0 | return 0; |
251 | 0 | |
252 | 0 | /* clear the old values (if necessary) */ |
253 | 0 | ASN1_OBJECT_free(field->fieldType); |
254 | 0 | ASN1_TYPE_free(field->p.other); |
255 | 0 |
|
256 | 0 | nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group)); |
257 | 0 | /* set OID for the field */ |
258 | 0 | if ((field->fieldType = OBJ_nid2obj(nid)) == NULL) { |
259 | 0 | ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB); |
260 | 0 | goto err; |
261 | 0 | } |
262 | 0 |
|
263 | 0 | if (nid == NID_X9_62_prime_field) { |
264 | 0 | if ((tmp = BN_new()) == NULL) { |
265 | 0 | ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE); |
266 | 0 | goto err; |
267 | 0 | } |
268 | 0 | /* the parameters are specified by the prime number p */ |
269 | 0 | if (!EC_GROUP_get_curve(group, tmp, NULL, NULL, NULL)) { |
270 | 0 | ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB); |
271 | 0 | goto err; |
272 | 0 | } |
273 | 0 | /* set the prime number */ |
274 | 0 | field->p.prime = BN_to_ASN1_INTEGER(tmp, NULL); |
275 | 0 | if (field->p.prime == NULL) { |
276 | 0 | ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB); |
277 | 0 | goto err; |
278 | 0 | } |
279 | 0 | } else if (nid == NID_X9_62_characteristic_two_field) |
280 | | #ifdef OPENSSL_NO_EC2M |
281 | | { |
282 | | ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_GF2M_NOT_SUPPORTED); |
283 | | goto err; |
284 | | } |
285 | | #else |
286 | 0 | { |
287 | 0 | int field_type; |
288 | 0 | X9_62_CHARACTERISTIC_TWO *char_two; |
289 | 0 |
|
290 | 0 | field->p.char_two = X9_62_CHARACTERISTIC_TWO_new(); |
291 | 0 | char_two = field->p.char_two; |
292 | 0 |
|
293 | 0 | if (char_two == NULL) { |
294 | 0 | ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE); |
295 | 0 | goto err; |
296 | 0 | } |
297 | 0 |
|
298 | 0 | char_two->m = (long)EC_GROUP_get_degree(group); |
299 | 0 |
|
300 | 0 | field_type = EC_GROUP_get_basis_type(group); |
301 | 0 |
|
302 | 0 | if (field_type == 0) { |
303 | 0 | ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB); |
304 | 0 | goto err; |
305 | 0 | } |
306 | 0 | /* set base type OID */ |
307 | 0 | if ((char_two->type = OBJ_nid2obj(field_type)) == NULL) { |
308 | 0 | ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB); |
309 | 0 | goto err; |
310 | 0 | } |
311 | 0 |
|
312 | 0 | if (field_type == NID_X9_62_tpBasis) { |
313 | 0 | unsigned int k; |
314 | 0 |
|
315 | 0 | if (!EC_GROUP_get_trinomial_basis(group, &k)) |
316 | 0 | goto err; |
317 | 0 | |
318 | 0 | char_two->p.tpBasis = ASN1_INTEGER_new(); |
319 | 0 | if (char_two->p.tpBasis == NULL) { |
320 | 0 | ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE); |
321 | 0 | goto err; |
322 | 0 | } |
323 | 0 | if (!ASN1_INTEGER_set(char_two->p.tpBasis, (long)k)) { |
324 | 0 | ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB); |
325 | 0 | goto err; |
326 | 0 | } |
327 | 0 | } else if (field_type == NID_X9_62_ppBasis) { |
328 | 0 | unsigned int k1, k2, k3; |
329 | 0 |
|
330 | 0 | if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3)) |
331 | 0 | goto err; |
332 | 0 | |
333 | 0 | char_two->p.ppBasis = X9_62_PENTANOMIAL_new(); |
334 | 0 | if (char_two->p.ppBasis == NULL) { |
335 | 0 | ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE); |
336 | 0 | goto err; |
337 | 0 | } |
338 | 0 |
|
339 | 0 | /* set k? values */ |
340 | 0 | char_two->p.ppBasis->k1 = (long)k1; |
341 | 0 | char_two->p.ppBasis->k2 = (long)k2; |
342 | 0 | char_two->p.ppBasis->k3 = (long)k3; |
343 | 0 | } else { /* field_type == NID_X9_62_onBasis */ |
344 | 0 |
|
345 | 0 | /* for ONB the parameters are (asn1) NULL */ |
346 | 0 | char_two->p.onBasis = ASN1_NULL_new(); |
347 | 0 | if (char_two->p.onBasis == NULL) { |
348 | 0 | ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE); |
349 | 0 | goto err; |
350 | 0 | } |
351 | 0 | } |
352 | 0 | } |
353 | 0 | #endif |
354 | 0 | else { |
355 | 0 | ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_UNSUPPORTED_FIELD); |
356 | 0 | goto err; |
357 | 0 | } |
358 | 0 |
|
359 | 0 | ok = 1; |
360 | 0 |
|
361 | 0 | err: |
362 | 0 | BN_free(tmp); |
363 | 0 | return ok; |
364 | 0 | } |
365 | | |
366 | | static int ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve) |
367 | 0 | { |
368 | 0 | int ok = 0; |
369 | 0 | BIGNUM *tmp_1 = NULL, *tmp_2 = NULL; |
370 | 0 | unsigned char *a_buf = NULL, *b_buf = NULL; |
371 | 0 | size_t len; |
372 | 0 |
|
373 | 0 | if (!group || !curve || !curve->a || !curve->b) |
374 | 0 | return 0; |
375 | 0 | |
376 | 0 | if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL) { |
377 | 0 | ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE); |
378 | 0 | goto err; |
379 | 0 | } |
380 | 0 |
|
381 | 0 | /* get a and b */ |
382 | 0 | if (!EC_GROUP_get_curve(group, NULL, tmp_1, tmp_2, NULL)) { |
383 | 0 | ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB); |
384 | 0 | goto err; |
385 | 0 | } |
386 | 0 |
|
387 | 0 | /* |
388 | 0 | * Per SEC 1, the curve coefficients must be padded up to size. See C.2's |
389 | 0 | * definition of Curve, C.1's definition of FieldElement, and 2.3.5's |
390 | 0 | * definition of how to encode the field elements. |
391 | 0 | */ |
392 | 0 | len = ((size_t)EC_GROUP_get_degree(group) + 7) / 8; |
393 | 0 | if ((a_buf = OPENSSL_malloc(len)) == NULL |
394 | 0 | || (b_buf = OPENSSL_malloc(len)) == NULL) { |
395 | 0 | ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE); |
396 | 0 | goto err; |
397 | 0 | } |
398 | 0 | if (BN_bn2binpad(tmp_1, a_buf, len) < 0 |
399 | 0 | || BN_bn2binpad(tmp_2, b_buf, len) < 0) { |
400 | 0 | ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB); |
401 | 0 | goto err; |
402 | 0 | } |
403 | 0 |
|
404 | 0 | /* set a and b */ |
405 | 0 | if (!ASN1_OCTET_STRING_set(curve->a, a_buf, len) |
406 | 0 | || !ASN1_OCTET_STRING_set(curve->b, b_buf, len)) { |
407 | 0 | ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB); |
408 | 0 | goto err; |
409 | 0 | } |
410 | 0 |
|
411 | 0 | /* set the seed (optional) */ |
412 | 0 | if (group->seed) { |
413 | 0 | if (!curve->seed) |
414 | 0 | if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) { |
415 | 0 | ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE); |
416 | 0 | goto err; |
417 | 0 | } |
418 | 0 | curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); |
419 | 0 | curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT; |
420 | 0 | if (!ASN1_BIT_STRING_set(curve->seed, group->seed, |
421 | 0 | (int)group->seed_len)) { |
422 | 0 | ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB); |
423 | 0 | goto err; |
424 | 0 | } |
425 | 0 | } else { |
426 | 0 | ASN1_BIT_STRING_free(curve->seed); |
427 | 0 | curve->seed = NULL; |
428 | 0 | } |
429 | 0 |
|
430 | 0 | ok = 1; |
431 | 0 |
|
432 | 0 | err: |
433 | 0 | OPENSSL_free(a_buf); |
434 | 0 | OPENSSL_free(b_buf); |
435 | 0 | BN_free(tmp_1); |
436 | 0 | BN_free(tmp_2); |
437 | 0 | return ok; |
438 | 0 | } |
439 | | |
440 | | ECPARAMETERS *EC_GROUP_get_ecparameters(const EC_GROUP *group, |
441 | | ECPARAMETERS *params) |
442 | 0 | { |
443 | 0 | size_t len = 0; |
444 | 0 | ECPARAMETERS *ret = NULL; |
445 | 0 | const BIGNUM *tmp; |
446 | 0 | unsigned char *buffer = NULL; |
447 | 0 | const EC_POINT *point = NULL; |
448 | 0 | point_conversion_form_t form; |
449 | 0 |
|
450 | 0 | if (params == NULL) { |
451 | 0 | if ((ret = ECPARAMETERS_new()) == NULL) { |
452 | 0 | ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_MALLOC_FAILURE); |
453 | 0 | goto err; |
454 | 0 | } |
455 | 0 | } else |
456 | 0 | ret = params; |
457 | 0 |
|
458 | 0 | /* set the version (always one) */ |
459 | 0 | ret->version = (long)0x1; |
460 | 0 |
|
461 | 0 | /* set the fieldID */ |
462 | 0 | if (!ec_asn1_group2fieldid(group, ret->fieldID)) { |
463 | 0 | ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB); |
464 | 0 | goto err; |
465 | 0 | } |
466 | 0 |
|
467 | 0 | /* set the curve */ |
468 | 0 | if (!ec_asn1_group2curve(group, ret->curve)) { |
469 | 0 | ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB); |
470 | 0 | goto err; |
471 | 0 | } |
472 | 0 |
|
473 | 0 | /* set the base point */ |
474 | 0 | if ((point = EC_GROUP_get0_generator(group)) == NULL) { |
475 | 0 | ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, EC_R_UNDEFINED_GENERATOR); |
476 | 0 | goto err; |
477 | 0 | } |
478 | 0 |
|
479 | 0 | form = EC_GROUP_get_point_conversion_form(group); |
480 | 0 |
|
481 | 0 | len = EC_POINT_point2buf(group, point, form, &buffer, NULL); |
482 | 0 | if (len == 0) { |
483 | 0 | ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB); |
484 | 0 | goto err; |
485 | 0 | } |
486 | 0 | if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) { |
487 | 0 | OPENSSL_free(buffer); |
488 | 0 | ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_MALLOC_FAILURE); |
489 | 0 | goto err; |
490 | 0 | } |
491 | 0 | ASN1_STRING_set0(ret->base, buffer, len); |
492 | 0 |
|
493 | 0 | /* set the order */ |
494 | 0 | tmp = EC_GROUP_get0_order(group); |
495 | 0 | if (tmp == NULL) { |
496 | 0 | ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB); |
497 | 0 | goto err; |
498 | 0 | } |
499 | 0 | ret->order = BN_to_ASN1_INTEGER(tmp, ret->order); |
500 | 0 | if (ret->order == NULL) { |
501 | 0 | ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_ASN1_LIB); |
502 | 0 | goto err; |
503 | 0 | } |
504 | 0 |
|
505 | 0 | /* set the cofactor (optional) */ |
506 | 0 | tmp = EC_GROUP_get0_cofactor(group); |
507 | 0 | if (tmp != NULL) { |
508 | 0 | ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor); |
509 | 0 | if (ret->cofactor == NULL) { |
510 | 0 | ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_ASN1_LIB); |
511 | 0 | goto err; |
512 | 0 | } |
513 | 0 | } |
514 | 0 |
|
515 | 0 | return ret; |
516 | 0 | |
517 | 0 | err: |
518 | 0 | if (params == NULL) |
519 | 0 | ECPARAMETERS_free(ret); |
520 | 0 | return NULL; |
521 | 0 | } |
522 | | |
523 | | ECPKPARAMETERS *EC_GROUP_get_ecpkparameters(const EC_GROUP *group, |
524 | | ECPKPARAMETERS *params) |
525 | 0 | { |
526 | 0 | int ok = 1, tmp; |
527 | 0 | ECPKPARAMETERS *ret = params; |
528 | 0 |
|
529 | 0 | if (ret == NULL) { |
530 | 0 | if ((ret = ECPKPARAMETERS_new()) == NULL) { |
531 | 0 | ECerr(EC_F_EC_GROUP_GET_ECPKPARAMETERS, ERR_R_MALLOC_FAILURE); |
532 | 0 | return NULL; |
533 | 0 | } |
534 | 0 | } else { |
535 | 0 | if (ret->type == 0) |
536 | 0 | ASN1_OBJECT_free(ret->value.named_curve); |
537 | 0 | else if (ret->type == 1 && ret->value.parameters) |
538 | 0 | ECPARAMETERS_free(ret->value.parameters); |
539 | 0 | } |
540 | 0 |
|
541 | 0 | if (EC_GROUP_get_asn1_flag(group)) { |
542 | 0 | /* |
543 | 0 | * use the asn1 OID to describe the elliptic curve parameters |
544 | 0 | */ |
545 | 0 | tmp = EC_GROUP_get_curve_name(group); |
546 | 0 | if (tmp) { |
547 | 0 | ret->type = 0; |
548 | 0 | if ((ret->value.named_curve = OBJ_nid2obj(tmp)) == NULL) |
549 | 0 | ok = 0; |
550 | 0 | } else |
551 | 0 | /* we don't know the nid => ERROR */ |
552 | 0 | ok = 0; |
553 | 0 | } else { |
554 | 0 | /* use the ECPARAMETERS structure */ |
555 | 0 | ret->type = 1; |
556 | 0 | if ((ret->value.parameters = |
557 | 0 | EC_GROUP_get_ecparameters(group, NULL)) == NULL) |
558 | 0 | ok = 0; |
559 | 0 | } |
560 | 0 |
|
561 | 0 | if (!ok) { |
562 | 0 | ECPKPARAMETERS_free(ret); |
563 | 0 | return NULL; |
564 | 0 | } |
565 | 0 | return ret; |
566 | 0 | } |
567 | | |
568 | | EC_GROUP *EC_GROUP_new_from_ecparameters(const ECPARAMETERS *params) |
569 | 0 | { |
570 | 0 | int ok = 0, tmp; |
571 | 0 | EC_GROUP *ret = NULL; |
572 | 0 | BIGNUM *p = NULL, *a = NULL, *b = NULL; |
573 | 0 | EC_POINT *point = NULL; |
574 | 0 | long field_bits; |
575 | 0 |
|
576 | 0 | if (!params->fieldID || !params->fieldID->fieldType || |
577 | 0 | !params->fieldID->p.ptr) { |
578 | 0 | ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR); |
579 | 0 | goto err; |
580 | 0 | } |
581 | 0 |
|
582 | 0 | /* |
583 | 0 | * Now extract the curve parameters a and b. Note that, although SEC 1 |
584 | 0 | * specifies the length of their encodings, historical versions of OpenSSL |
585 | 0 | * encoded them incorrectly, so we must accept any length for backwards |
586 | 0 | * compatibility. |
587 | 0 | */ |
588 | 0 | if (!params->curve || !params->curve->a || |
589 | 0 | !params->curve->a->data || !params->curve->b || |
590 | 0 | !params->curve->b->data) { |
591 | 0 | ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR); |
592 | 0 | goto err; |
593 | 0 | } |
594 | 0 | a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL); |
595 | 0 | if (a == NULL) { |
596 | 0 | ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB); |
597 | 0 | goto err; |
598 | 0 | } |
599 | 0 | b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL); |
600 | 0 | if (b == NULL) { |
601 | 0 | ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB); |
602 | 0 | goto err; |
603 | 0 | } |
604 | 0 |
|
605 | 0 | /* get the field parameters */ |
606 | 0 | tmp = OBJ_obj2nid(params->fieldID->fieldType); |
607 | 0 | if (tmp == NID_X9_62_characteristic_two_field) |
608 | | #ifdef OPENSSL_NO_EC2M |
609 | | { |
610 | | ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_GF2M_NOT_SUPPORTED); |
611 | | goto err; |
612 | | } |
613 | | #else |
614 | 0 | { |
615 | 0 | X9_62_CHARACTERISTIC_TWO *char_two; |
616 | 0 |
|
617 | 0 | char_two = params->fieldID->p.char_two; |
618 | 0 |
|
619 | 0 | field_bits = char_two->m; |
620 | 0 | if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) { |
621 | 0 | ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_FIELD_TOO_LARGE); |
622 | 0 | goto err; |
623 | 0 | } |
624 | 0 |
|
625 | 0 | if ((p = BN_new()) == NULL) { |
626 | 0 | ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_MALLOC_FAILURE); |
627 | 0 | goto err; |
628 | 0 | } |
629 | 0 |
|
630 | 0 | /* get the base type */ |
631 | 0 | tmp = OBJ_obj2nid(char_two->type); |
632 | 0 |
|
633 | 0 | if (tmp == NID_X9_62_tpBasis) { |
634 | 0 | long tmp_long; |
635 | 0 |
|
636 | 0 | if (!char_two->p.tpBasis) { |
637 | 0 | ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR); |
638 | 0 | goto err; |
639 | 0 | } |
640 | 0 |
|
641 | 0 | tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis); |
642 | 0 |
|
643 | 0 | if (!(char_two->m > tmp_long && tmp_long > 0)) { |
644 | 0 | ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, |
645 | 0 | EC_R_INVALID_TRINOMIAL_BASIS); |
646 | 0 | goto err; |
647 | 0 | } |
648 | 0 |
|
649 | 0 | /* create the polynomial */ |
650 | 0 | if (!BN_set_bit(p, (int)char_two->m)) |
651 | 0 | goto err; |
652 | 0 | if (!BN_set_bit(p, (int)tmp_long)) |
653 | 0 | goto err; |
654 | 0 | if (!BN_set_bit(p, 0)) |
655 | 0 | goto err; |
656 | 0 | } else if (tmp == NID_X9_62_ppBasis) { |
657 | 0 | X9_62_PENTANOMIAL *penta; |
658 | 0 |
|
659 | 0 | penta = char_two->p.ppBasis; |
660 | 0 | if (!penta) { |
661 | 0 | ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR); |
662 | 0 | goto err; |
663 | 0 | } |
664 | 0 |
|
665 | 0 | if (! |
666 | 0 | (char_two->m > penta->k3 && penta->k3 > penta->k2 |
667 | 0 | && penta->k2 > penta->k1 && penta->k1 > 0)) { |
668 | 0 | ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, |
669 | 0 | EC_R_INVALID_PENTANOMIAL_BASIS); |
670 | 0 | goto err; |
671 | 0 | } |
672 | 0 |
|
673 | 0 | /* create the polynomial */ |
674 | 0 | if (!BN_set_bit(p, (int)char_two->m)) |
675 | 0 | goto err; |
676 | 0 | if (!BN_set_bit(p, (int)penta->k1)) |
677 | 0 | goto err; |
678 | 0 | if (!BN_set_bit(p, (int)penta->k2)) |
679 | 0 | goto err; |
680 | 0 | if (!BN_set_bit(p, (int)penta->k3)) |
681 | 0 | goto err; |
682 | 0 | if (!BN_set_bit(p, 0)) |
683 | 0 | goto err; |
684 | 0 | } else if (tmp == NID_X9_62_onBasis) { |
685 | 0 | ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_NOT_IMPLEMENTED); |
686 | 0 | goto err; |
687 | 0 | } else { /* error */ |
688 | 0 |
|
689 | 0 | ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR); |
690 | 0 | goto err; |
691 | 0 | } |
692 | 0 |
|
693 | 0 | /* create the EC_GROUP structure */ |
694 | 0 | ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL); |
695 | 0 | } |
696 | 0 | #endif |
697 | 0 | else if (tmp == NID_X9_62_prime_field) { |
698 | 0 | /* we have a curve over a prime field */ |
699 | 0 | /* extract the prime number */ |
700 | 0 | if (!params->fieldID->p.prime) { |
701 | 0 | ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR); |
702 | 0 | goto err; |
703 | 0 | } |
704 | 0 | p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL); |
705 | 0 | if (p == NULL) { |
706 | 0 | ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB); |
707 | 0 | goto err; |
708 | 0 | } |
709 | 0 |
|
710 | 0 | if (BN_is_negative(p) || BN_is_zero(p)) { |
711 | 0 | ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_FIELD); |
712 | 0 | goto err; |
713 | 0 | } |
714 | 0 |
|
715 | 0 | field_bits = BN_num_bits(p); |
716 | 0 | if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) { |
717 | 0 | ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_FIELD_TOO_LARGE); |
718 | 0 | goto err; |
719 | 0 | } |
720 | 0 |
|
721 | 0 | /* create the EC_GROUP structure */ |
722 | 0 | ret = EC_GROUP_new_curve_GFp(p, a, b, NULL); |
723 | 0 | } else { |
724 | 0 | ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_FIELD); |
725 | 0 | goto err; |
726 | 0 | } |
727 | 0 |
|
728 | 0 | if (ret == NULL) { |
729 | 0 | ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB); |
730 | 0 | goto err; |
731 | 0 | } |
732 | 0 |
|
733 | 0 | /* extract seed (optional) */ |
734 | 0 | if (params->curve->seed != NULL) { |
735 | 0 | OPENSSL_free(ret->seed); |
736 | 0 | if ((ret->seed = OPENSSL_malloc(params->curve->seed->length)) == NULL) { |
737 | 0 | ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_MALLOC_FAILURE); |
738 | 0 | goto err; |
739 | 0 | } |
740 | 0 | memcpy(ret->seed, params->curve->seed->data, |
741 | 0 | params->curve->seed->length); |
742 | 0 | ret->seed_len = params->curve->seed->length; |
743 | 0 | } |
744 | 0 |
|
745 | 0 | if (!params->order || !params->base || !params->base->data) { |
746 | 0 | ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR); |
747 | 0 | goto err; |
748 | 0 | } |
749 | 0 |
|
750 | 0 | if ((point = EC_POINT_new(ret)) == NULL) |
751 | 0 | goto err; |
752 | 0 | |
753 | 0 | /* set the point conversion form */ |
754 | 0 | EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t) |
755 | 0 | (params->base->data[0] & ~0x01)); |
756 | 0 |
|
757 | 0 | /* extract the ec point */ |
758 | 0 | if (!EC_POINT_oct2point(ret, point, params->base->data, |
759 | 0 | params->base->length, NULL)) { |
760 | 0 | ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB); |
761 | 0 | goto err; |
762 | 0 | } |
763 | 0 |
|
764 | 0 | /* extract the order */ |
765 | 0 | if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) { |
766 | 0 | ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB); |
767 | 0 | goto err; |
768 | 0 | } |
769 | 0 | if (BN_is_negative(a) || BN_is_zero(a)) { |
770 | 0 | ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_GROUP_ORDER); |
771 | 0 | goto err; |
772 | 0 | } |
773 | 0 | if (BN_num_bits(a) > (int)field_bits + 1) { /* Hasse bound */ |
774 | 0 | ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_GROUP_ORDER); |
775 | 0 | goto err; |
776 | 0 | } |
777 | 0 |
|
778 | 0 | /* extract the cofactor (optional) */ |
779 | 0 | if (params->cofactor == NULL) { |
780 | 0 | BN_free(b); |
781 | 0 | b = NULL; |
782 | 0 | } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) { |
783 | 0 | ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB); |
784 | 0 | goto err; |
785 | 0 | } |
786 | 0 | /* set the generator, order and cofactor (if present) */ |
787 | 0 | if (!EC_GROUP_set_generator(ret, point, a, b)) { |
788 | 0 | ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB); |
789 | 0 | goto err; |
790 | 0 | } |
791 | 0 |
|
792 | 0 | ok = 1; |
793 | 0 |
|
794 | 0 | err: |
795 | 0 | if (!ok) { |
796 | 0 | EC_GROUP_clear_free(ret); |
797 | 0 | ret = NULL; |
798 | 0 | } |
799 | 0 |
|
800 | 0 | BN_free(p); |
801 | 0 | BN_free(a); |
802 | 0 | BN_free(b); |
803 | 0 | EC_POINT_free(point); |
804 | 0 | return ret; |
805 | 0 | } |
806 | | |
807 | | EC_GROUP *EC_GROUP_new_from_ecpkparameters(const ECPKPARAMETERS *params) |
808 | 0 | { |
809 | 0 | EC_GROUP *ret = NULL; |
810 | 0 | int tmp = 0; |
811 | 0 |
|
812 | 0 | if (params == NULL) { |
813 | 0 | ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, EC_R_MISSING_PARAMETERS); |
814 | 0 | return NULL; |
815 | 0 | } |
816 | 0 |
|
817 | 0 | if (params->type == 0) { /* the curve is given by an OID */ |
818 | 0 | tmp = OBJ_obj2nid(params->value.named_curve); |
819 | 0 | if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) { |
820 | 0 | ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, |
821 | 0 | EC_R_EC_GROUP_NEW_BY_NAME_FAILURE); |
822 | 0 | return NULL; |
823 | 0 | } |
824 | 0 | EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE); |
825 | 0 | } else if (params->type == 1) { /* the parameters are given by a |
826 | 0 | * ECPARAMETERS structure */ |
827 | 0 | ret = EC_GROUP_new_from_ecparameters(params->value.parameters); |
828 | 0 | if (!ret) { |
829 | 0 | ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, ERR_R_EC_LIB); |
830 | 0 | return NULL; |
831 | 0 | } |
832 | 0 | EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_EXPLICIT_CURVE); |
833 | 0 | } else if (params->type == 2) { /* implicitlyCA */ |
834 | 0 | return NULL; |
835 | 0 | } else { |
836 | 0 | ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, EC_R_ASN1_ERROR); |
837 | 0 | return NULL; |
838 | 0 | } |
839 | 0 |
|
840 | 0 | return ret; |
841 | 0 | } |
842 | | |
843 | | /* EC_GROUP <-> DER encoding of ECPKPARAMETERS */ |
844 | | |
845 | | EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len) |
846 | 0 | { |
847 | 0 | EC_GROUP *group = NULL; |
848 | 0 | ECPKPARAMETERS *params = NULL; |
849 | 0 | const unsigned char *p = *in; |
850 | 0 |
|
851 | 0 | if ((params = d2i_ECPKPARAMETERS(NULL, &p, len)) == NULL) { |
852 | 0 | ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE); |
853 | 0 | ECPKPARAMETERS_free(params); |
854 | 0 | return NULL; |
855 | 0 | } |
856 | 0 |
|
857 | 0 | if ((group = EC_GROUP_new_from_ecpkparameters(params)) == NULL) { |
858 | 0 | ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE); |
859 | 0 | ECPKPARAMETERS_free(params); |
860 | 0 | return NULL; |
861 | 0 | } |
862 | 0 |
|
863 | 0 | if (a) { |
864 | 0 | EC_GROUP_clear_free(*a); |
865 | 0 | *a = group; |
866 | 0 | } |
867 | 0 |
|
868 | 0 | ECPKPARAMETERS_free(params); |
869 | 0 | *in = p; |
870 | 0 | return group; |
871 | 0 | } |
872 | | |
873 | | int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out) |
874 | 0 | { |
875 | 0 | int ret = 0; |
876 | 0 | ECPKPARAMETERS *tmp = EC_GROUP_get_ecpkparameters(a, NULL); |
877 | 0 | if (tmp == NULL) { |
878 | 0 | ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE); |
879 | 0 | return 0; |
880 | 0 | } |
881 | 0 | if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) { |
882 | 0 | ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE); |
883 | 0 | ECPKPARAMETERS_free(tmp); |
884 | 0 | return 0; |
885 | 0 | } |
886 | 0 | ECPKPARAMETERS_free(tmp); |
887 | 0 | return ret; |
888 | 0 | } |
889 | | |
890 | | /* some EC_KEY functions */ |
891 | | |
892 | | EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len) |
893 | 0 | { |
894 | 0 | EC_KEY *ret = NULL; |
895 | 0 | EC_PRIVATEKEY *priv_key = NULL; |
896 | 0 | const unsigned char *p = *in; |
897 | 0 |
|
898 | 0 | if ((priv_key = d2i_EC_PRIVATEKEY(NULL, &p, len)) == NULL) { |
899 | 0 | ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB); |
900 | 0 | return NULL; |
901 | 0 | } |
902 | 0 |
|
903 | 0 | if (a == NULL || *a == NULL) { |
904 | 0 | if ((ret = EC_KEY_new()) == NULL) { |
905 | 0 | ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE); |
906 | 0 | goto err; |
907 | 0 | } |
908 | 0 | } else |
909 | 0 | ret = *a; |
910 | 0 |
|
911 | 0 | if (priv_key->parameters) { |
912 | 0 | EC_GROUP_clear_free(ret->group); |
913 | 0 | ret->group = EC_GROUP_new_from_ecpkparameters(priv_key->parameters); |
914 | 0 | } |
915 | 0 |
|
916 | 0 | if (ret->group == NULL) { |
917 | 0 | ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB); |
918 | 0 | goto err; |
919 | 0 | } |
920 | 0 |
|
921 | 0 | ret->version = priv_key->version; |
922 | 0 |
|
923 | 0 | if (priv_key->privateKey) { |
924 | 0 | ASN1_OCTET_STRING *pkey = priv_key->privateKey; |
925 | 0 | if (EC_KEY_oct2priv(ret, ASN1_STRING_get0_data(pkey), |
926 | 0 | ASN1_STRING_length(pkey)) == 0) |
927 | 0 | goto err; |
928 | 0 | } else { |
929 | 0 | ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_MISSING_PRIVATE_KEY); |
930 | 0 | goto err; |
931 | 0 | } |
932 | 0 |
|
933 | 0 | EC_POINT_clear_free(ret->pub_key); |
934 | 0 | ret->pub_key = EC_POINT_new(ret->group); |
935 | 0 | if (ret->pub_key == NULL) { |
936 | 0 | ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB); |
937 | 0 | goto err; |
938 | 0 | } |
939 | 0 |
|
940 | 0 | if (priv_key->publicKey) { |
941 | 0 | const unsigned char *pub_oct; |
942 | 0 | int pub_oct_len; |
943 | 0 |
|
944 | 0 | pub_oct = ASN1_STRING_get0_data(priv_key->publicKey); |
945 | 0 | pub_oct_len = ASN1_STRING_length(priv_key->publicKey); |
946 | 0 | if (!EC_KEY_oct2key(ret, pub_oct, pub_oct_len, NULL)) { |
947 | 0 | ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB); |
948 | 0 | goto err; |
949 | 0 | } |
950 | 0 | } else { |
951 | 0 | if (ret->group->meth->keygenpub == NULL |
952 | 0 | || ret->group->meth->keygenpub(ret) == 0) |
953 | 0 | goto err; |
954 | 0 | /* Remember the original private-key-only encoding. */ |
955 | 0 | ret->enc_flag |= EC_PKEY_NO_PUBKEY; |
956 | 0 | } |
957 | 0 |
|
958 | 0 | if (a) |
959 | 0 | *a = ret; |
960 | 0 | EC_PRIVATEKEY_free(priv_key); |
961 | 0 | *in = p; |
962 | 0 | return ret; |
963 | 0 |
|
964 | 0 | err: |
965 | 0 | if (a == NULL || *a != ret) |
966 | 0 | EC_KEY_free(ret); |
967 | 0 | EC_PRIVATEKEY_free(priv_key); |
968 | 0 | return NULL; |
969 | 0 | } |
970 | | |
971 | | int i2d_ECPrivateKey(EC_KEY *a, unsigned char **out) |
972 | 0 | { |
973 | 0 | int ret = 0, ok = 0; |
974 | 0 | unsigned char *priv= NULL, *pub= NULL; |
975 | 0 | size_t privlen = 0, publen = 0; |
976 | 0 |
|
977 | 0 | EC_PRIVATEKEY *priv_key = NULL; |
978 | 0 |
|
979 | 0 | if (a == NULL || a->group == NULL || |
980 | 0 | (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key == NULL)) { |
981 | 0 | ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER); |
982 | 0 | goto err; |
983 | 0 | } |
984 | 0 |
|
985 | 0 | if ((priv_key = EC_PRIVATEKEY_new()) == NULL) { |
986 | 0 | ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE); |
987 | 0 | goto err; |
988 | 0 | } |
989 | 0 |
|
990 | 0 | priv_key->version = a->version; |
991 | 0 |
|
992 | 0 | privlen = EC_KEY_priv2buf(a, &priv); |
993 | 0 |
|
994 | 0 | if (privlen == 0) { |
995 | 0 | ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB); |
996 | 0 | goto err; |
997 | 0 | } |
998 | 0 |
|
999 | 0 | ASN1_STRING_set0(priv_key->privateKey, priv, privlen); |
1000 | 0 | priv = NULL; |
1001 | 0 |
|
1002 | 0 | if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) { |
1003 | 0 | if ((priv_key->parameters = |
1004 | 0 | EC_GROUP_get_ecpkparameters(a->group, |
1005 | 0 | priv_key->parameters)) == NULL) { |
1006 | 0 | ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB); |
1007 | 0 | goto err; |
1008 | 0 | } |
1009 | 0 | } |
1010 | 0 |
|
1011 | 0 | if (!(a->enc_flag & EC_PKEY_NO_PUBKEY)) { |
1012 | 0 | priv_key->publicKey = ASN1_BIT_STRING_new(); |
1013 | 0 | if (priv_key->publicKey == NULL) { |
1014 | 0 | ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE); |
1015 | 0 | goto err; |
1016 | 0 | } |
1017 | 0 |
|
1018 | 0 | publen = EC_KEY_key2buf(a, a->conv_form, &pub, NULL); |
1019 | 0 |
|
1020 | 0 | if (publen == 0) { |
1021 | 0 | ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB); |
1022 | 0 | goto err; |
1023 | 0 | } |
1024 | 0 |
|
1025 | 0 | priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); |
1026 | 0 | priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT; |
1027 | 0 | ASN1_STRING_set0(priv_key->publicKey, pub, publen); |
1028 | 0 | pub = NULL; |
1029 | 0 | } |
1030 | 0 |
|
1031 | 0 | if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) { |
1032 | 0 | ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB); |
1033 | 0 | goto err; |
1034 | 0 | } |
1035 | 0 | ok = 1; |
1036 | 0 | err: |
1037 | 0 | OPENSSL_clear_free(priv, privlen); |
1038 | 0 | OPENSSL_free(pub); |
1039 | 0 | EC_PRIVATEKEY_free(priv_key); |
1040 | 0 | return (ok ? ret : 0); |
1041 | 0 | } |
1042 | | |
1043 | | int i2d_ECParameters(EC_KEY *a, unsigned char **out) |
1044 | 0 | { |
1045 | 0 | if (a == NULL) { |
1046 | 0 | ECerr(EC_F_I2D_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER); |
1047 | 0 | return 0; |
1048 | 0 | } |
1049 | 0 | return i2d_ECPKParameters(a->group, out); |
1050 | 0 | } |
1051 | | |
1052 | | EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len) |
1053 | 0 | { |
1054 | 0 | EC_KEY *ret; |
1055 | 0 |
|
1056 | 0 | if (in == NULL || *in == NULL) { |
1057 | 0 | ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER); |
1058 | 0 | return NULL; |
1059 | 0 | } |
1060 | 0 |
|
1061 | 0 | if (a == NULL || *a == NULL) { |
1062 | 0 | if ((ret = EC_KEY_new()) == NULL) { |
1063 | 0 | ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE); |
1064 | 0 | return NULL; |
1065 | 0 | } |
1066 | 0 | } else |
1067 | 0 | ret = *a; |
1068 | 0 |
|
1069 | 0 | if (!d2i_ECPKParameters(&ret->group, in, len)) { |
1070 | 0 | ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB); |
1071 | 0 | if (a == NULL || *a != ret) |
1072 | 0 | EC_KEY_free(ret); |
1073 | 0 | return NULL; |
1074 | 0 | } |
1075 | 0 |
|
1076 | 0 | if (a) |
1077 | 0 | *a = ret; |
1078 | 0 |
|
1079 | 0 | return ret; |
1080 | 0 | } |
1081 | | |
1082 | | EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len) |
1083 | 0 | { |
1084 | 0 | EC_KEY *ret = NULL; |
1085 | 0 |
|
1086 | 0 | if (a == NULL || (*a) == NULL || (*a)->group == NULL) { |
1087 | 0 | /* |
1088 | 0 | * sorry, but a EC_GROUP-structure is necessary to set the public key |
1089 | 0 | */ |
1090 | 0 | ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER); |
1091 | 0 | return 0; |
1092 | 0 | } |
1093 | 0 | ret = *a; |
1094 | 0 | if (!EC_KEY_oct2key(ret, *in, len, NULL)) { |
1095 | 0 | ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_EC_LIB); |
1096 | 0 | return 0; |
1097 | 0 | } |
1098 | 0 | *in += len; |
1099 | 0 | return ret; |
1100 | 0 | } |
1101 | | |
1102 | | int i2o_ECPublicKey(const EC_KEY *a, unsigned char **out) |
1103 | 0 | { |
1104 | 0 | size_t buf_len = 0; |
1105 | 0 | int new_buffer = 0; |
1106 | 0 |
|
1107 | 0 | if (a == NULL) { |
1108 | 0 | ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER); |
1109 | 0 | return 0; |
1110 | 0 | } |
1111 | 0 |
|
1112 | 0 | buf_len = EC_POINT_point2oct(a->group, a->pub_key, |
1113 | 0 | a->conv_form, NULL, 0, NULL); |
1114 | 0 |
|
1115 | 0 | if (out == NULL || buf_len == 0) |
1116 | 0 | /* out == NULL => just return the length of the octet string */ |
1117 | 0 | return buf_len; |
1118 | 0 | |
1119 | 0 | if (*out == NULL) { |
1120 | 0 | if ((*out = OPENSSL_malloc(buf_len)) == NULL) { |
1121 | 0 | ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_MALLOC_FAILURE); |
1122 | 0 | return 0; |
1123 | 0 | } |
1124 | 0 | new_buffer = 1; |
1125 | 0 | } |
1126 | 0 | if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form, |
1127 | 0 | *out, buf_len, NULL)) { |
1128 | 0 | ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_EC_LIB); |
1129 | 0 | if (new_buffer) { |
1130 | 0 | OPENSSL_free(*out); |
1131 | 0 | *out = NULL; |
1132 | 0 | } |
1133 | 0 | return 0; |
1134 | 0 | } |
1135 | 0 | if (!new_buffer) |
1136 | 0 | *out += buf_len; |
1137 | 0 | return buf_len; |
1138 | 0 | } |
1139 | | |
1140 | | ASN1_SEQUENCE(ECDSA_SIG) = { |
1141 | | ASN1_SIMPLE(ECDSA_SIG, r, CBIGNUM), |
1142 | | ASN1_SIMPLE(ECDSA_SIG, s, CBIGNUM) |
1143 | | } static_ASN1_SEQUENCE_END(ECDSA_SIG) |
1144 | | |
1145 | | DECLARE_ASN1_FUNCTIONS_const(ECDSA_SIG) |
1146 | | DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECDSA_SIG, ECDSA_SIG) |
1147 | | IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(ECDSA_SIG, ECDSA_SIG, ECDSA_SIG) |
1148 | | |
1149 | | ECDSA_SIG *ECDSA_SIG_new(void) |
1150 | 0 | { |
1151 | 0 | ECDSA_SIG *sig = OPENSSL_zalloc(sizeof(*sig)); |
1152 | 0 | if (sig == NULL) |
1153 | 0 | ECerr(EC_F_ECDSA_SIG_NEW, ERR_R_MALLOC_FAILURE); |
1154 | 0 | return sig; |
1155 | 0 | } |
1156 | | |
1157 | | void ECDSA_SIG_free(ECDSA_SIG *sig) |
1158 | 0 | { |
1159 | 0 | if (sig == NULL) |
1160 | 0 | return; |
1161 | 0 | BN_clear_free(sig->r); |
1162 | 0 | BN_clear_free(sig->s); |
1163 | 0 | OPENSSL_free(sig); |
1164 | 0 | } |
1165 | | |
1166 | | void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps) |
1167 | 0 | { |
1168 | 0 | if (pr != NULL) |
1169 | 0 | *pr = sig->r; |
1170 | 0 | if (ps != NULL) |
1171 | 0 | *ps = sig->s; |
1172 | 0 | } |
1173 | | |
1174 | | const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig) |
1175 | 0 | { |
1176 | 0 | return sig->r; |
1177 | 0 | } |
1178 | | |
1179 | | const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig) |
1180 | 0 | { |
1181 | 0 | return sig->s; |
1182 | 0 | } |
1183 | | |
1184 | | int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s) |
1185 | 0 | { |
1186 | 0 | if (r == NULL || s == NULL) |
1187 | 0 | return 0; |
1188 | 0 | BN_clear_free(sig->r); |
1189 | 0 | BN_clear_free(sig->s); |
1190 | 0 | sig->r = r; |
1191 | 0 | sig->s = s; |
1192 | 0 | return 1; |
1193 | 0 | } |
1194 | | |
1195 | | int ECDSA_size(const EC_KEY *r) |
1196 | 0 | { |
1197 | 0 | int ret, i; |
1198 | 0 | ASN1_INTEGER bs; |
1199 | 0 | unsigned char buf[4]; |
1200 | 0 | const EC_GROUP *group; |
1201 | 0 |
|
1202 | 0 | if (r == NULL) |
1203 | 0 | return 0; |
1204 | 0 | group = EC_KEY_get0_group(r); |
1205 | 0 | if (group == NULL) |
1206 | 0 | return 0; |
1207 | 0 | |
1208 | 0 | i = EC_GROUP_order_bits(group); |
1209 | 0 | if (i == 0) |
1210 | 0 | return 0; |
1211 | 0 | bs.length = (i + 7) / 8; |
1212 | 0 | bs.data = buf; |
1213 | 0 | bs.type = V_ASN1_INTEGER; |
1214 | 0 | /* If the top bit is set the asn1 encoding is 1 larger. */ |
1215 | 0 | buf[0] = 0xff; |
1216 | 0 |
|
1217 | 0 | i = i2d_ASN1_INTEGER(&bs, NULL); |
1218 | 0 | i += i; /* r and s */ |
1219 | 0 | ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE); |
1220 | 0 | return ret; |
1221 | 0 | } |