/src/libressl/crypto/ec/ec_asn1.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* $OpenBSD: ec_asn1.c,v 1.37 2022/05/24 20:06:32 tb Exp $ */ |
2 | | /* |
3 | | * Written by Nils Larsch for the OpenSSL project. |
4 | | */ |
5 | | /* ==================================================================== |
6 | | * Copyright (c) 2000-2003 The OpenSSL Project. All rights reserved. |
7 | | * |
8 | | * Redistribution and use in source and binary forms, with or without |
9 | | * modification, are permitted provided that the following conditions |
10 | | * are met: |
11 | | * |
12 | | * 1. Redistributions of source code must retain the above copyright |
13 | | * notice, this list of conditions and the following disclaimer. |
14 | | * |
15 | | * 2. Redistributions in binary form must reproduce the above copyright |
16 | | * notice, this list of conditions and the following disclaimer in |
17 | | * the documentation and/or other materials provided with the |
18 | | * distribution. |
19 | | * |
20 | | * 3. All advertising materials mentioning features or use of this |
21 | | * software must display the following acknowledgment: |
22 | | * "This product includes software developed by the OpenSSL Project |
23 | | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" |
24 | | * |
25 | | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
26 | | * endorse or promote products derived from this software without |
27 | | * prior written permission. For written permission, please contact |
28 | | * licensing@OpenSSL.org. |
29 | | * |
30 | | * 5. Products derived from this software may not be called "OpenSSL" |
31 | | * nor may "OpenSSL" appear in their names without prior written |
32 | | * permission of the OpenSSL Project. |
33 | | * |
34 | | * 6. Redistributions of any form whatsoever must retain the following |
35 | | * acknowledgment: |
36 | | * "This product includes software developed by the OpenSSL Project |
37 | | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" |
38 | | * |
39 | | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
40 | | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
41 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
42 | | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
43 | | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
44 | | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
45 | | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
46 | | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
47 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
48 | | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
49 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
50 | | * OF THE POSSIBILITY OF SUCH DAMAGE. |
51 | | * ==================================================================== |
52 | | * |
53 | | * This product includes cryptographic software written by Eric Young |
54 | | * (eay@cryptsoft.com). This product includes software written by Tim |
55 | | * Hudson (tjh@cryptsoft.com). |
56 | | * |
57 | | */ |
58 | | |
59 | | #include <string.h> |
60 | | |
61 | | #include <openssl/opensslconf.h> |
62 | | |
63 | | #include <openssl/err.h> |
64 | | #include <openssl/asn1t.h> |
65 | | #include <openssl/objects.h> |
66 | | |
67 | | #include "asn1_locl.h" |
68 | | #include "ec_lcl.h" |
69 | | |
70 | | int |
71 | | EC_GROUP_get_basis_type(const EC_GROUP * group) |
72 | 0 | { |
73 | 0 | int i = 0; |
74 | |
|
75 | 0 | if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) != |
76 | 0 | NID_X9_62_characteristic_two_field) |
77 | | /* everything else is currently not supported */ |
78 | 0 | return 0; |
79 | | |
80 | 0 | while (group->poly[i] != 0) |
81 | 0 | i++; |
82 | |
|
83 | 0 | if (i == 4) |
84 | 0 | return NID_X9_62_ppBasis; |
85 | 0 | else if (i == 2) |
86 | 0 | return NID_X9_62_tpBasis; |
87 | 0 | else |
88 | | /* everything else is currently not supported */ |
89 | 0 | return 0; |
90 | 0 | } |
91 | | |
92 | | #ifndef OPENSSL_NO_EC2M |
93 | | int |
94 | | EC_GROUP_get_trinomial_basis(const EC_GROUP * group, unsigned int *k) |
95 | 0 | { |
96 | 0 | if (group == NULL) |
97 | 0 | return 0; |
98 | | |
99 | 0 | if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) != |
100 | 0 | NID_X9_62_characteristic_two_field |
101 | 0 | || !((group->poly[0] != 0) && (group->poly[1] != 0) && (group->poly[2] == 0))) { |
102 | 0 | ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
103 | 0 | return 0; |
104 | 0 | } |
105 | 0 | if (k) |
106 | 0 | *k = group->poly[1]; |
107 | |
|
108 | 0 | return 1; |
109 | 0 | } |
110 | | |
111 | | int |
112 | | EC_GROUP_get_pentanomial_basis(const EC_GROUP * group, unsigned int *k1, |
113 | | unsigned int *k2, unsigned int *k3) |
114 | 0 | { |
115 | 0 | if (group == NULL) |
116 | 0 | return 0; |
117 | | |
118 | 0 | if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) != |
119 | 0 | NID_X9_62_characteristic_two_field |
120 | 0 | || !((group->poly[0] != 0) && (group->poly[1] != 0) && (group->poly[2] != 0) && (group->poly[3] != 0) && (group->poly[4] == 0))) { |
121 | 0 | ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
122 | 0 | return 0; |
123 | 0 | } |
124 | 0 | if (k1) |
125 | 0 | *k1 = group->poly[3]; |
126 | 0 | if (k2) |
127 | 0 | *k2 = group->poly[2]; |
128 | 0 | if (k3) |
129 | 0 | *k3 = group->poly[1]; |
130 | |
|
131 | 0 | return 1; |
132 | 0 | } |
133 | | #endif |
134 | | |
135 | | /* some structures needed for the asn1 encoding */ |
136 | | typedef struct x9_62_pentanomial_st { |
137 | | long k1; |
138 | | long k2; |
139 | | long k3; |
140 | | } X9_62_PENTANOMIAL; |
141 | | |
142 | | typedef struct x9_62_characteristic_two_st { |
143 | | long m; |
144 | | ASN1_OBJECT *type; |
145 | | union { |
146 | | char *ptr; |
147 | | /* NID_X9_62_onBasis */ |
148 | | ASN1_NULL *onBasis; |
149 | | /* NID_X9_62_tpBasis */ |
150 | | ASN1_INTEGER *tpBasis; |
151 | | /* NID_X9_62_ppBasis */ |
152 | | X9_62_PENTANOMIAL *ppBasis; |
153 | | /* anything else */ |
154 | | ASN1_TYPE *other; |
155 | | } p; |
156 | | } X9_62_CHARACTERISTIC_TWO; |
157 | | |
158 | | typedef struct x9_62_fieldid_st { |
159 | | ASN1_OBJECT *fieldType; |
160 | | union { |
161 | | char *ptr; |
162 | | /* NID_X9_62_prime_field */ |
163 | | ASN1_INTEGER *prime; |
164 | | /* NID_X9_62_characteristic_two_field */ |
165 | | X9_62_CHARACTERISTIC_TWO *char_two; |
166 | | /* anything else */ |
167 | | ASN1_TYPE *other; |
168 | | } p; |
169 | | } X9_62_FIELDID; |
170 | | |
171 | | typedef struct x9_62_curve_st { |
172 | | ASN1_OCTET_STRING *a; |
173 | | ASN1_OCTET_STRING *b; |
174 | | ASN1_BIT_STRING *seed; |
175 | | } X9_62_CURVE; |
176 | | |
177 | | typedef struct ec_parameters_st { |
178 | | long version; |
179 | | X9_62_FIELDID *fieldID; |
180 | | X9_62_CURVE *curve; |
181 | | ASN1_OCTET_STRING *base; |
182 | | ASN1_INTEGER *order; |
183 | | ASN1_INTEGER *cofactor; |
184 | | } ECPARAMETERS; |
185 | | |
186 | | struct ecpk_parameters_st { |
187 | | int type; |
188 | | union { |
189 | | ASN1_OBJECT *named_curve; |
190 | | ECPARAMETERS *parameters; |
191 | | ASN1_NULL *implicitlyCA; |
192 | | } value; |
193 | | } /* ECPKPARAMETERS */ ; |
194 | | |
195 | | /* SEC1 ECPrivateKey */ |
196 | | typedef struct ec_privatekey_st { |
197 | | long version; |
198 | | ASN1_OCTET_STRING *privateKey; |
199 | | ECPKPARAMETERS *parameters; |
200 | | ASN1_BIT_STRING *publicKey; |
201 | | } EC_PRIVATEKEY; |
202 | | |
203 | | /* the OpenSSL ASN.1 definitions */ |
204 | | static const ASN1_TEMPLATE X9_62_PENTANOMIAL_seq_tt[] = { |
205 | | { |
206 | | .flags = 0, |
207 | | .tag = 0, |
208 | | .offset = offsetof(X9_62_PENTANOMIAL, k1), |
209 | | .field_name = "k1", |
210 | | .item = &LONG_it, |
211 | | }, |
212 | | { |
213 | | .flags = 0, |
214 | | .tag = 0, |
215 | | .offset = offsetof(X9_62_PENTANOMIAL, k2), |
216 | | .field_name = "k2", |
217 | | .item = &LONG_it, |
218 | | }, |
219 | | { |
220 | | .flags = 0, |
221 | | .tag = 0, |
222 | | .offset = offsetof(X9_62_PENTANOMIAL, k3), |
223 | | .field_name = "k3", |
224 | | .item = &LONG_it, |
225 | | }, |
226 | | }; |
227 | | |
228 | | const ASN1_ITEM X9_62_PENTANOMIAL_it = { |
229 | | .itype = ASN1_ITYPE_SEQUENCE, |
230 | | .utype = V_ASN1_SEQUENCE, |
231 | | .templates = X9_62_PENTANOMIAL_seq_tt, |
232 | | .tcount = sizeof(X9_62_PENTANOMIAL_seq_tt) / sizeof(ASN1_TEMPLATE), |
233 | | .funcs = NULL, |
234 | | .size = sizeof(X9_62_PENTANOMIAL), |
235 | | .sname = "X9_62_PENTANOMIAL", |
236 | | }; |
237 | | |
238 | | X9_62_PENTANOMIAL *X9_62_PENTANOMIAL_new(void); |
239 | | void X9_62_PENTANOMIAL_free(X9_62_PENTANOMIAL *a); |
240 | | |
241 | | X9_62_PENTANOMIAL * |
242 | | X9_62_PENTANOMIAL_new(void) |
243 | 0 | { |
244 | 0 | return (X9_62_PENTANOMIAL*)ASN1_item_new(&X9_62_PENTANOMIAL_it); |
245 | 0 | } |
246 | | |
247 | | void |
248 | | X9_62_PENTANOMIAL_free(X9_62_PENTANOMIAL *a) |
249 | 0 | { |
250 | 0 | ASN1_item_free((ASN1_VALUE *)a, &X9_62_PENTANOMIAL_it); |
251 | 0 | } |
252 | | |
253 | | static const ASN1_TEMPLATE char_two_def_tt = { |
254 | | .flags = 0, |
255 | | .tag = 0, |
256 | | .offset = offsetof(X9_62_CHARACTERISTIC_TWO, p.other), |
257 | | .field_name = "p.other", |
258 | | .item = &ASN1_ANY_it, |
259 | | }; |
260 | | |
261 | | static const ASN1_ADB_TABLE X9_62_CHARACTERISTIC_TWO_adbtbl[] = { |
262 | | { |
263 | | .value = NID_X9_62_onBasis, |
264 | | .tt = { |
265 | | .flags = 0, |
266 | | .tag = 0, |
267 | | .offset = offsetof(X9_62_CHARACTERISTIC_TWO, p.onBasis), |
268 | | .field_name = "p.onBasis", |
269 | | .item = &ASN1_NULL_it, |
270 | | }, |
271 | | |
272 | | }, |
273 | | { |
274 | | .value = NID_X9_62_tpBasis, |
275 | | .tt = { |
276 | | .flags = 0, |
277 | | .tag = 0, |
278 | | .offset = offsetof(X9_62_CHARACTERISTIC_TWO, p.tpBasis), |
279 | | .field_name = "p.tpBasis", |
280 | | .item = &ASN1_INTEGER_it, |
281 | | }, |
282 | | |
283 | | }, |
284 | | { |
285 | | .value = NID_X9_62_ppBasis, |
286 | | .tt = { |
287 | | .flags = 0, |
288 | | .tag = 0, |
289 | | .offset = offsetof(X9_62_CHARACTERISTIC_TWO, p.ppBasis), |
290 | | .field_name = "p.ppBasis", |
291 | | .item = &X9_62_PENTANOMIAL_it, |
292 | | }, |
293 | | |
294 | | }, |
295 | | }; |
296 | | |
297 | | static const ASN1_ADB X9_62_CHARACTERISTIC_TWO_adb = { |
298 | | .flags = 0, |
299 | | .offset = offsetof(X9_62_CHARACTERISTIC_TWO, type), |
300 | | .tbl = X9_62_CHARACTERISTIC_TWO_adbtbl, |
301 | | .tblcount = sizeof(X9_62_CHARACTERISTIC_TWO_adbtbl) / sizeof(ASN1_ADB_TABLE), |
302 | | .default_tt = &char_two_def_tt, |
303 | | .null_tt = NULL, |
304 | | }; |
305 | | |
306 | | static const ASN1_TEMPLATE X9_62_CHARACTERISTIC_TWO_seq_tt[] = { |
307 | | { |
308 | | .flags = 0, |
309 | | .tag = 0, |
310 | | .offset = offsetof(X9_62_CHARACTERISTIC_TWO, m), |
311 | | .field_name = "m", |
312 | | .item = &LONG_it, |
313 | | }, |
314 | | { |
315 | | .flags = 0, |
316 | | .tag = 0, |
317 | | .offset = offsetof(X9_62_CHARACTERISTIC_TWO, type), |
318 | | .field_name = "type", |
319 | | .item = &ASN1_OBJECT_it, |
320 | | }, |
321 | | { |
322 | | .flags = ASN1_TFLG_ADB_OID, |
323 | | .tag = -1, |
324 | | .offset = 0, |
325 | | .field_name = "X9_62_CHARACTERISTIC_TWO", |
326 | | .item = (const ASN1_ITEM *)&X9_62_CHARACTERISTIC_TWO_adb, |
327 | | }, |
328 | | }; |
329 | | |
330 | | const ASN1_ITEM X9_62_CHARACTERISTIC_TWO_it = { |
331 | | .itype = ASN1_ITYPE_SEQUENCE, |
332 | | .utype = V_ASN1_SEQUENCE, |
333 | | .templates = X9_62_CHARACTERISTIC_TWO_seq_tt, |
334 | | .tcount = sizeof(X9_62_CHARACTERISTIC_TWO_seq_tt) / sizeof(ASN1_TEMPLATE), |
335 | | .funcs = NULL, |
336 | | .size = sizeof(X9_62_CHARACTERISTIC_TWO), |
337 | | .sname = "X9_62_CHARACTERISTIC_TWO", |
338 | | }; |
339 | | |
340 | | X9_62_CHARACTERISTIC_TWO *X9_62_CHARACTERISTIC_TWO_new(void); |
341 | | void X9_62_CHARACTERISTIC_TWO_free(X9_62_CHARACTERISTIC_TWO *a); |
342 | | |
343 | | X9_62_CHARACTERISTIC_TWO * |
344 | | X9_62_CHARACTERISTIC_TWO_new(void) |
345 | 0 | { |
346 | 0 | return (X9_62_CHARACTERISTIC_TWO*)ASN1_item_new(&X9_62_CHARACTERISTIC_TWO_it); |
347 | 0 | } |
348 | | |
349 | | void |
350 | | X9_62_CHARACTERISTIC_TWO_free(X9_62_CHARACTERISTIC_TWO *a) |
351 | 0 | { |
352 | 0 | ASN1_item_free((ASN1_VALUE *)a, &X9_62_CHARACTERISTIC_TWO_it); |
353 | 0 | } |
354 | | |
355 | | static const ASN1_TEMPLATE fieldID_def_tt = { |
356 | | .flags = 0, |
357 | | .tag = 0, |
358 | | .offset = offsetof(X9_62_FIELDID, p.other), |
359 | | .field_name = "p.other", |
360 | | .item = &ASN1_ANY_it, |
361 | | }; |
362 | | |
363 | | static const ASN1_ADB_TABLE X9_62_FIELDID_adbtbl[] = { |
364 | | { |
365 | | .value = NID_X9_62_prime_field, |
366 | | .tt = { |
367 | | .flags = 0, |
368 | | .tag = 0, |
369 | | .offset = offsetof(X9_62_FIELDID, p.prime), |
370 | | .field_name = "p.prime", |
371 | | .item = &ASN1_INTEGER_it, |
372 | | }, |
373 | | |
374 | | }, |
375 | | { |
376 | | .value = NID_X9_62_characteristic_two_field, |
377 | | .tt = { |
378 | | .flags = 0, |
379 | | .tag = 0, |
380 | | .offset = offsetof(X9_62_FIELDID, p.char_two), |
381 | | .field_name = "p.char_two", |
382 | | .item = &X9_62_CHARACTERISTIC_TWO_it, |
383 | | }, |
384 | | |
385 | | }, |
386 | | }; |
387 | | |
388 | | static const ASN1_ADB X9_62_FIELDID_adb = { |
389 | | .flags = 0, |
390 | | .offset = offsetof(X9_62_FIELDID, fieldType), |
391 | | .tbl = X9_62_FIELDID_adbtbl, |
392 | | .tblcount = sizeof(X9_62_FIELDID_adbtbl) / sizeof(ASN1_ADB_TABLE), |
393 | | .default_tt = &fieldID_def_tt, |
394 | | .null_tt = NULL, |
395 | | }; |
396 | | |
397 | | static const ASN1_TEMPLATE X9_62_FIELDID_seq_tt[] = { |
398 | | { |
399 | | .flags = 0, |
400 | | .tag = 0, |
401 | | .offset = offsetof(X9_62_FIELDID, fieldType), |
402 | | .field_name = "fieldType", |
403 | | .item = &ASN1_OBJECT_it, |
404 | | }, |
405 | | { |
406 | | .flags = ASN1_TFLG_ADB_OID, |
407 | | .tag = -1, |
408 | | .offset = 0, |
409 | | .field_name = "X9_62_FIELDID", |
410 | | .item = (const ASN1_ITEM *)&X9_62_FIELDID_adb, |
411 | | }, |
412 | | }; |
413 | | |
414 | | const ASN1_ITEM X9_62_FIELDID_it = { |
415 | | .itype = ASN1_ITYPE_SEQUENCE, |
416 | | .utype = V_ASN1_SEQUENCE, |
417 | | .templates = X9_62_FIELDID_seq_tt, |
418 | | .tcount = sizeof(X9_62_FIELDID_seq_tt) / sizeof(ASN1_TEMPLATE), |
419 | | .funcs = NULL, |
420 | | .size = sizeof(X9_62_FIELDID), |
421 | | .sname = "X9_62_FIELDID", |
422 | | }; |
423 | | |
424 | | static const ASN1_TEMPLATE X9_62_CURVE_seq_tt[] = { |
425 | | { |
426 | | .flags = 0, |
427 | | .tag = 0, |
428 | | .offset = offsetof(X9_62_CURVE, a), |
429 | | .field_name = "a", |
430 | | .item = &ASN1_OCTET_STRING_it, |
431 | | }, |
432 | | { |
433 | | .flags = 0, |
434 | | .tag = 0, |
435 | | .offset = offsetof(X9_62_CURVE, b), |
436 | | .field_name = "b", |
437 | | .item = &ASN1_OCTET_STRING_it, |
438 | | }, |
439 | | { |
440 | | .flags = ASN1_TFLG_OPTIONAL, |
441 | | .tag = 0, |
442 | | .offset = offsetof(X9_62_CURVE, seed), |
443 | | .field_name = "seed", |
444 | | .item = &ASN1_BIT_STRING_it, |
445 | | }, |
446 | | }; |
447 | | |
448 | | const ASN1_ITEM X9_62_CURVE_it = { |
449 | | .itype = ASN1_ITYPE_SEQUENCE, |
450 | | .utype = V_ASN1_SEQUENCE, |
451 | | .templates = X9_62_CURVE_seq_tt, |
452 | | .tcount = sizeof(X9_62_CURVE_seq_tt) / sizeof(ASN1_TEMPLATE), |
453 | | .funcs = NULL, |
454 | | .size = sizeof(X9_62_CURVE), |
455 | | .sname = "X9_62_CURVE", |
456 | | }; |
457 | | |
458 | | static const ASN1_TEMPLATE ECPARAMETERS_seq_tt[] = { |
459 | | { |
460 | | .flags = 0, |
461 | | .tag = 0, |
462 | | .offset = offsetof(ECPARAMETERS, version), |
463 | | .field_name = "version", |
464 | | .item = &LONG_it, |
465 | | }, |
466 | | { |
467 | | .flags = 0, |
468 | | .tag = 0, |
469 | | .offset = offsetof(ECPARAMETERS, fieldID), |
470 | | .field_name = "fieldID", |
471 | | .item = &X9_62_FIELDID_it, |
472 | | }, |
473 | | { |
474 | | .flags = 0, |
475 | | .tag = 0, |
476 | | .offset = offsetof(ECPARAMETERS, curve), |
477 | | .field_name = "curve", |
478 | | .item = &X9_62_CURVE_it, |
479 | | }, |
480 | | { |
481 | | .flags = 0, |
482 | | .tag = 0, |
483 | | .offset = offsetof(ECPARAMETERS, base), |
484 | | .field_name = "base", |
485 | | .item = &ASN1_OCTET_STRING_it, |
486 | | }, |
487 | | { |
488 | | .flags = 0, |
489 | | .tag = 0, |
490 | | .offset = offsetof(ECPARAMETERS, order), |
491 | | .field_name = "order", |
492 | | .item = &ASN1_INTEGER_it, |
493 | | }, |
494 | | { |
495 | | .flags = ASN1_TFLG_OPTIONAL, |
496 | | .tag = 0, |
497 | | .offset = offsetof(ECPARAMETERS, cofactor), |
498 | | .field_name = "cofactor", |
499 | | .item = &ASN1_INTEGER_it, |
500 | | }, |
501 | | }; |
502 | | |
503 | | const ASN1_ITEM ECPARAMETERS_it = { |
504 | | .itype = ASN1_ITYPE_SEQUENCE, |
505 | | .utype = V_ASN1_SEQUENCE, |
506 | | .templates = ECPARAMETERS_seq_tt, |
507 | | .tcount = sizeof(ECPARAMETERS_seq_tt) / sizeof(ASN1_TEMPLATE), |
508 | | .funcs = NULL, |
509 | | .size = sizeof(ECPARAMETERS), |
510 | | .sname = "ECPARAMETERS", |
511 | | }; |
512 | | |
513 | | ECPARAMETERS *ECPARAMETERS_new(void); |
514 | | void ECPARAMETERS_free(ECPARAMETERS *a); |
515 | | |
516 | | ECPARAMETERS * |
517 | | ECPARAMETERS_new(void) |
518 | 0 | { |
519 | 0 | return (ECPARAMETERS*)ASN1_item_new(&ECPARAMETERS_it); |
520 | 0 | } |
521 | | |
522 | | void |
523 | | ECPARAMETERS_free(ECPARAMETERS *a) |
524 | 0 | { |
525 | 0 | ASN1_item_free((ASN1_VALUE *)a, &ECPARAMETERS_it); |
526 | 0 | } |
527 | | |
528 | | static const ASN1_TEMPLATE ECPKPARAMETERS_ch_tt[] = { |
529 | | { |
530 | | .flags = 0, |
531 | | .tag = 0, |
532 | | .offset = offsetof(ECPKPARAMETERS, value.named_curve), |
533 | | .field_name = "value.named_curve", |
534 | | .item = &ASN1_OBJECT_it, |
535 | | }, |
536 | | { |
537 | | .flags = 0, |
538 | | .tag = 0, |
539 | | .offset = offsetof(ECPKPARAMETERS, value.parameters), |
540 | | .field_name = "value.parameters", |
541 | | .item = &ECPARAMETERS_it, |
542 | | }, |
543 | | { |
544 | | .flags = 0, |
545 | | .tag = 0, |
546 | | .offset = offsetof(ECPKPARAMETERS, value.implicitlyCA), |
547 | | .field_name = "value.implicitlyCA", |
548 | | .item = &ASN1_NULL_it, |
549 | | }, |
550 | | }; |
551 | | |
552 | | const ASN1_ITEM ECPKPARAMETERS_it = { |
553 | | .itype = ASN1_ITYPE_CHOICE, |
554 | | .utype = offsetof(ECPKPARAMETERS, type), |
555 | | .templates = ECPKPARAMETERS_ch_tt, |
556 | | .tcount = sizeof(ECPKPARAMETERS_ch_tt) / sizeof(ASN1_TEMPLATE), |
557 | | .funcs = NULL, |
558 | | .size = sizeof(ECPKPARAMETERS), |
559 | | .sname = "ECPKPARAMETERS", |
560 | | }; |
561 | | |
562 | | ECPKPARAMETERS *ECPKPARAMETERS_new(void); |
563 | | void ECPKPARAMETERS_free(ECPKPARAMETERS *a); |
564 | | ECPKPARAMETERS *d2i_ECPKPARAMETERS(ECPKPARAMETERS **a, const unsigned char **in, long len); |
565 | | int i2d_ECPKPARAMETERS(const ECPKPARAMETERS *a, unsigned char **out); |
566 | | |
567 | | ECPKPARAMETERS * |
568 | | d2i_ECPKPARAMETERS(ECPKPARAMETERS **a, const unsigned char **in, long len) |
569 | 0 | { |
570 | 0 | return (ECPKPARAMETERS *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, |
571 | 0 | &ECPKPARAMETERS_it); |
572 | 0 | } |
573 | | |
574 | | int |
575 | | i2d_ECPKPARAMETERS(const ECPKPARAMETERS *a, unsigned char **out) |
576 | 0 | { |
577 | 0 | return ASN1_item_i2d((ASN1_VALUE *)a, out, &ECPKPARAMETERS_it); |
578 | 0 | } |
579 | | |
580 | | ECPKPARAMETERS * |
581 | | ECPKPARAMETERS_new(void) |
582 | 0 | { |
583 | 0 | return (ECPKPARAMETERS *)ASN1_item_new(&ECPKPARAMETERS_it); |
584 | 0 | } |
585 | | |
586 | | void |
587 | | ECPKPARAMETERS_free(ECPKPARAMETERS *a) |
588 | 0 | { |
589 | 0 | ASN1_item_free((ASN1_VALUE *)a, &ECPKPARAMETERS_it); |
590 | 0 | } |
591 | | |
592 | | static const ASN1_TEMPLATE EC_PRIVATEKEY_seq_tt[] = { |
593 | | { |
594 | | .flags = 0, |
595 | | .tag = 0, |
596 | | .offset = offsetof(EC_PRIVATEKEY, version), |
597 | | .field_name = "version", |
598 | | .item = &LONG_it, |
599 | | }, |
600 | | { |
601 | | .flags = 0, |
602 | | .tag = 0, |
603 | | .offset = offsetof(EC_PRIVATEKEY, privateKey), |
604 | | .field_name = "privateKey", |
605 | | .item = &ASN1_OCTET_STRING_it, |
606 | | }, |
607 | | { |
608 | | .flags = ASN1_TFLG_EXPLICIT | ASN1_TFLG_OPTIONAL, |
609 | | .tag = 0, |
610 | | .offset = offsetof(EC_PRIVATEKEY, parameters), |
611 | | .field_name = "parameters", |
612 | | .item = &ECPKPARAMETERS_it, |
613 | | }, |
614 | | { |
615 | | .flags = ASN1_TFLG_EXPLICIT | ASN1_TFLG_OPTIONAL, |
616 | | .tag = 1, |
617 | | .offset = offsetof(EC_PRIVATEKEY, publicKey), |
618 | | .field_name = "publicKey", |
619 | | .item = &ASN1_BIT_STRING_it, |
620 | | }, |
621 | | }; |
622 | | |
623 | | const ASN1_ITEM EC_PRIVATEKEY_it = { |
624 | | .itype = ASN1_ITYPE_SEQUENCE, |
625 | | .utype = V_ASN1_SEQUENCE, |
626 | | .templates = EC_PRIVATEKEY_seq_tt, |
627 | | .tcount = sizeof(EC_PRIVATEKEY_seq_tt) / sizeof(ASN1_TEMPLATE), |
628 | | .funcs = NULL, |
629 | | .size = sizeof(EC_PRIVATEKEY), |
630 | | .sname = "EC_PRIVATEKEY", |
631 | | }; |
632 | | |
633 | | EC_PRIVATEKEY *EC_PRIVATEKEY_new(void); |
634 | | void EC_PRIVATEKEY_free(EC_PRIVATEKEY *a); |
635 | | EC_PRIVATEKEY *d2i_EC_PRIVATEKEY(EC_PRIVATEKEY **a, const unsigned char **in, long len); |
636 | | int i2d_EC_PRIVATEKEY(const EC_PRIVATEKEY *a, unsigned char **out); |
637 | | |
638 | | EC_PRIVATEKEY * |
639 | | d2i_EC_PRIVATEKEY(EC_PRIVATEKEY **a, const unsigned char **in, long len) |
640 | 6.07k | { |
641 | 6.07k | return (EC_PRIVATEKEY *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, |
642 | 6.07k | &EC_PRIVATEKEY_it); |
643 | 6.07k | } |
644 | | |
645 | | int |
646 | | i2d_EC_PRIVATEKEY(const EC_PRIVATEKEY *a, unsigned char **out) |
647 | 0 | { |
648 | 0 | return ASN1_item_i2d((ASN1_VALUE *)a, out, &EC_PRIVATEKEY_it); |
649 | 0 | } |
650 | | |
651 | | EC_PRIVATEKEY * |
652 | | EC_PRIVATEKEY_new(void) |
653 | 0 | { |
654 | 0 | return (EC_PRIVATEKEY *)ASN1_item_new(&EC_PRIVATEKEY_it); |
655 | 0 | } |
656 | | |
657 | | void |
658 | | EC_PRIVATEKEY_free(EC_PRIVATEKEY *a) |
659 | 6.07k | { |
660 | 6.07k | ASN1_item_free((ASN1_VALUE *)a, &EC_PRIVATEKEY_it); |
661 | 6.07k | } |
662 | | |
663 | | /* some declarations of internal function */ |
664 | | |
665 | | /* ec_asn1_group2field() sets the values in a X9_62_FIELDID object */ |
666 | | static int ec_asn1_group2fieldid(const EC_GROUP *, X9_62_FIELDID *); |
667 | | /* ec_asn1_group2curve() sets the values in a X9_62_CURVE object */ |
668 | | static int ec_asn1_group2curve(const EC_GROUP *, X9_62_CURVE *); |
669 | | /* ec_asn1_parameters2group() creates a EC_GROUP object from a |
670 | | * ECPARAMETERS object */ |
671 | | static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *); |
672 | | /* ec_asn1_group2parameters() creates a ECPARAMETERS object from a |
673 | | * EC_GROUP object */ |
674 | | static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *, ECPARAMETERS *); |
675 | | /* ec_asn1_pkparameters2group() creates a EC_GROUP object from a |
676 | | * ECPKPARAMETERS object */ |
677 | | static EC_GROUP *ec_asn1_pkparameters2group(const ECPKPARAMETERS *); |
678 | | /* ec_asn1_group2pkparameters() creates a ECPKPARAMETERS object from a |
679 | | * EC_GROUP object */ |
680 | | static ECPKPARAMETERS *ec_asn1_group2pkparameters(const EC_GROUP *, |
681 | | ECPKPARAMETERS *); |
682 | | |
683 | | /* the function definitions */ |
684 | | |
685 | | static int |
686 | | ec_asn1_group2fieldid(const EC_GROUP * group, X9_62_FIELDID * field) |
687 | 0 | { |
688 | 0 | int ok = 0, nid; |
689 | 0 | BIGNUM *tmp = NULL; |
690 | |
|
691 | 0 | if (group == NULL || field == NULL) |
692 | 0 | return 0; |
693 | | |
694 | | /* clear the old values (if necessary) */ |
695 | 0 | if (field->fieldType != NULL) |
696 | 0 | ASN1_OBJECT_free(field->fieldType); |
697 | 0 | if (field->p.other != NULL) |
698 | 0 | ASN1_TYPE_free(field->p.other); |
699 | |
|
700 | 0 | nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group)); |
701 | | /* set OID for the field */ |
702 | 0 | if ((field->fieldType = OBJ_nid2obj(nid)) == NULL) { |
703 | 0 | ECerror(ERR_R_OBJ_LIB); |
704 | 0 | goto err; |
705 | 0 | } |
706 | 0 | if (nid == NID_X9_62_prime_field) { |
707 | 0 | if ((tmp = BN_new()) == NULL) { |
708 | 0 | ECerror(ERR_R_MALLOC_FAILURE); |
709 | 0 | goto err; |
710 | 0 | } |
711 | | /* the parameters are specified by the prime number p */ |
712 | 0 | if (!EC_GROUP_get_curve(group, tmp, NULL, NULL, NULL)) { |
713 | 0 | ECerror(ERR_R_EC_LIB); |
714 | 0 | goto err; |
715 | 0 | } |
716 | | /* set the prime number */ |
717 | 0 | field->p.prime = BN_to_ASN1_INTEGER(tmp, NULL); |
718 | 0 | if (field->p.prime == NULL) { |
719 | 0 | ECerror(ERR_R_ASN1_LIB); |
720 | 0 | goto err; |
721 | 0 | } |
722 | 0 | } else /* nid == NID_X9_62_characteristic_two_field */ |
723 | | #ifdef OPENSSL_NO_EC2M |
724 | | { |
725 | | ECerror(EC_R_GF2M_NOT_SUPPORTED); |
726 | | goto err; |
727 | | } |
728 | | #else |
729 | 0 | { |
730 | 0 | int field_type; |
731 | 0 | X9_62_CHARACTERISTIC_TWO *char_two; |
732 | |
|
733 | 0 | field->p.char_two = X9_62_CHARACTERISTIC_TWO_new(); |
734 | 0 | char_two = field->p.char_two; |
735 | |
|
736 | 0 | if (char_two == NULL) { |
737 | 0 | ECerror(ERR_R_MALLOC_FAILURE); |
738 | 0 | goto err; |
739 | 0 | } |
740 | 0 | char_two->m = (long) EC_GROUP_get_degree(group); |
741 | |
|
742 | 0 | field_type = EC_GROUP_get_basis_type(group); |
743 | |
|
744 | 0 | if (field_type == 0) { |
745 | 0 | ECerror(ERR_R_EC_LIB); |
746 | 0 | goto err; |
747 | 0 | } |
748 | | /* set base type OID */ |
749 | 0 | if ((char_two->type = OBJ_nid2obj(field_type)) == NULL) { |
750 | 0 | ECerror(ERR_R_OBJ_LIB); |
751 | 0 | goto err; |
752 | 0 | } |
753 | 0 | if (field_type == NID_X9_62_tpBasis) { |
754 | 0 | unsigned int k; |
755 | |
|
756 | 0 | if (!EC_GROUP_get_trinomial_basis(group, &k)) |
757 | 0 | goto err; |
758 | | |
759 | 0 | char_two->p.tpBasis = ASN1_INTEGER_new(); |
760 | 0 | if (!char_two->p.tpBasis) { |
761 | 0 | ECerror(ERR_R_MALLOC_FAILURE); |
762 | 0 | goto err; |
763 | 0 | } |
764 | 0 | if (!ASN1_INTEGER_set(char_two->p.tpBasis, (long) k)) { |
765 | 0 | ECerror(ERR_R_ASN1_LIB); |
766 | 0 | goto err; |
767 | 0 | } |
768 | 0 | } else if (field_type == NID_X9_62_ppBasis) { |
769 | 0 | unsigned int k1, k2, k3; |
770 | |
|
771 | 0 | if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3)) |
772 | 0 | goto err; |
773 | | |
774 | 0 | char_two->p.ppBasis = X9_62_PENTANOMIAL_new(); |
775 | 0 | if (!char_two->p.ppBasis) { |
776 | 0 | ECerror(ERR_R_MALLOC_FAILURE); |
777 | 0 | goto err; |
778 | 0 | } |
779 | | /* set k? values */ |
780 | 0 | char_two->p.ppBasis->k1 = (long) k1; |
781 | 0 | char_two->p.ppBasis->k2 = (long) k2; |
782 | 0 | char_two->p.ppBasis->k3 = (long) k3; |
783 | 0 | } else { /* field_type == NID_X9_62_onBasis */ |
784 | | /* for ONB the parameters are (asn1) NULL */ |
785 | 0 | char_two->p.onBasis = ASN1_NULL_new(); |
786 | 0 | if (!char_two->p.onBasis) { |
787 | 0 | ECerror(ERR_R_MALLOC_FAILURE); |
788 | 0 | goto err; |
789 | 0 | } |
790 | 0 | } |
791 | 0 | } |
792 | 0 | #endif |
793 | | |
794 | 0 | ok = 1; |
795 | |
|
796 | 0 | err: |
797 | 0 | BN_free(tmp); |
798 | 0 | return (ok); |
799 | 0 | } |
800 | | |
801 | | static int |
802 | | ec_asn1_group2curve(const EC_GROUP * group, X9_62_CURVE * curve) |
803 | 0 | { |
804 | 0 | BIGNUM *tmp_1 = NULL, *tmp_2 = NULL; |
805 | 0 | unsigned char *buffer_1 = NULL, *buffer_2 = NULL, *a_buf = NULL, |
806 | 0 | *b_buf = NULL; |
807 | 0 | size_t len_1, len_2; |
808 | 0 | unsigned char char_zero = 0; |
809 | 0 | int ok = 0; |
810 | |
|
811 | 0 | if (!group || !curve || !curve->a || !curve->b) |
812 | 0 | return 0; |
813 | | |
814 | 0 | if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL) { |
815 | 0 | ECerror(ERR_R_MALLOC_FAILURE); |
816 | 0 | goto err; |
817 | 0 | } |
818 | | |
819 | | /* get a and b */ |
820 | 0 | if (!EC_GROUP_get_curve(group, NULL, tmp_1, tmp_2, NULL)) { |
821 | 0 | ECerror(ERR_R_EC_LIB); |
822 | 0 | goto err; |
823 | 0 | } |
824 | 0 | len_1 = (size_t) BN_num_bytes(tmp_1); |
825 | 0 | len_2 = (size_t) BN_num_bytes(tmp_2); |
826 | |
|
827 | 0 | if (len_1 == 0) { |
828 | | /* len_1 == 0 => a == 0 */ |
829 | 0 | a_buf = &char_zero; |
830 | 0 | len_1 = 1; |
831 | 0 | } else { |
832 | 0 | if ((buffer_1 = malloc(len_1)) == NULL) { |
833 | 0 | ECerror(ERR_R_MALLOC_FAILURE); |
834 | 0 | goto err; |
835 | 0 | } |
836 | 0 | if ((len_1 = BN_bn2bin(tmp_1, buffer_1)) == 0) { |
837 | 0 | ECerror(ERR_R_BN_LIB); |
838 | 0 | goto err; |
839 | 0 | } |
840 | 0 | a_buf = buffer_1; |
841 | 0 | } |
842 | | |
843 | 0 | if (len_2 == 0) { |
844 | | /* len_2 == 0 => b == 0 */ |
845 | 0 | b_buf = &char_zero; |
846 | 0 | len_2 = 1; |
847 | 0 | } else { |
848 | 0 | if ((buffer_2 = malloc(len_2)) == NULL) { |
849 | 0 | ECerror(ERR_R_MALLOC_FAILURE); |
850 | 0 | goto err; |
851 | 0 | } |
852 | 0 | if ((len_2 = BN_bn2bin(tmp_2, buffer_2)) == 0) { |
853 | 0 | ECerror(ERR_R_BN_LIB); |
854 | 0 | goto err; |
855 | 0 | } |
856 | 0 | b_buf = buffer_2; |
857 | 0 | } |
858 | | |
859 | | /* set a and b */ |
860 | 0 | if (!ASN1_STRING_set(curve->a, a_buf, len_1) || |
861 | 0 | !ASN1_STRING_set(curve->b, b_buf, len_2)) { |
862 | 0 | ECerror(ERR_R_ASN1_LIB); |
863 | 0 | goto err; |
864 | 0 | } |
865 | | |
866 | 0 | ASN1_BIT_STRING_free(curve->seed); |
867 | 0 | curve->seed = NULL; |
868 | | |
869 | | /* set the seed (optional) */ |
870 | 0 | if (group->seed != NULL) { |
871 | 0 | if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) { |
872 | 0 | ECerror(ERR_R_MALLOC_FAILURE); |
873 | 0 | goto err; |
874 | 0 | } |
875 | 0 | if (!ASN1_BIT_STRING_set(curve->seed, group->seed, |
876 | 0 | (int) group->seed_len)) { |
877 | 0 | ECerror(ERR_R_ASN1_LIB); |
878 | 0 | goto err; |
879 | 0 | } |
880 | 0 | if (!asn1_abs_set_unused_bits(curve->seed, 0)) { |
881 | 0 | ECerror(ERR_R_ASN1_LIB); |
882 | 0 | goto err; |
883 | 0 | } |
884 | 0 | } |
885 | | |
886 | 0 | ok = 1; |
887 | |
|
888 | 0 | err: |
889 | 0 | free(buffer_1); |
890 | 0 | free(buffer_2); |
891 | 0 | BN_free(tmp_1); |
892 | 0 | BN_free(tmp_2); |
893 | 0 | return (ok); |
894 | 0 | } |
895 | | |
896 | | static ECPARAMETERS * |
897 | | ec_asn1_group2parameters(const EC_GROUP * group, ECPARAMETERS * param) |
898 | 0 | { |
899 | 0 | int ok = 0; |
900 | 0 | size_t len = 0; |
901 | 0 | ECPARAMETERS *ret = NULL; |
902 | 0 | BIGNUM *tmp = NULL; |
903 | 0 | unsigned char *buffer = NULL; |
904 | 0 | const EC_POINT *point = NULL; |
905 | 0 | point_conversion_form_t form; |
906 | |
|
907 | 0 | if ((tmp = BN_new()) == NULL) { |
908 | 0 | ECerror(ERR_R_MALLOC_FAILURE); |
909 | 0 | goto err; |
910 | 0 | } |
911 | 0 | if (param == NULL) { |
912 | 0 | if ((ret = ECPARAMETERS_new()) == NULL) { |
913 | 0 | ECerror(ERR_R_MALLOC_FAILURE); |
914 | 0 | goto err; |
915 | 0 | } |
916 | 0 | } else |
917 | 0 | ret = param; |
918 | | |
919 | | /* set the version (always one) */ |
920 | 0 | ret->version = (long) 0x1; |
921 | | |
922 | | /* set the fieldID */ |
923 | 0 | if (!ec_asn1_group2fieldid(group, ret->fieldID)) { |
924 | 0 | ECerror(ERR_R_EC_LIB); |
925 | 0 | goto err; |
926 | 0 | } |
927 | | /* set the curve */ |
928 | 0 | if (!ec_asn1_group2curve(group, ret->curve)) { |
929 | 0 | ECerror(ERR_R_EC_LIB); |
930 | 0 | goto err; |
931 | 0 | } |
932 | | /* set the base point */ |
933 | 0 | if ((point = EC_GROUP_get0_generator(group)) == NULL) { |
934 | 0 | ECerror(EC_R_UNDEFINED_GENERATOR); |
935 | 0 | goto err; |
936 | 0 | } |
937 | 0 | form = EC_GROUP_get_point_conversion_form(group); |
938 | |
|
939 | 0 | len = EC_POINT_point2oct(group, point, form, NULL, len, NULL); |
940 | 0 | if (len == 0) { |
941 | 0 | ECerror(ERR_R_EC_LIB); |
942 | 0 | goto err; |
943 | 0 | } |
944 | 0 | if ((buffer = malloc(len)) == NULL) { |
945 | 0 | ECerror(ERR_R_MALLOC_FAILURE); |
946 | 0 | goto err; |
947 | 0 | } |
948 | 0 | if (!EC_POINT_point2oct(group, point, form, buffer, len, NULL)) { |
949 | 0 | ECerror(ERR_R_EC_LIB); |
950 | 0 | goto err; |
951 | 0 | } |
952 | 0 | if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) { |
953 | 0 | ECerror(ERR_R_MALLOC_FAILURE); |
954 | 0 | goto err; |
955 | 0 | } |
956 | 0 | if (!ASN1_OCTET_STRING_set(ret->base, buffer, len)) { |
957 | 0 | ECerror(ERR_R_ASN1_LIB); |
958 | 0 | goto err; |
959 | 0 | } |
960 | | /* set the order */ |
961 | 0 | if (!EC_GROUP_get_order(group, tmp, NULL)) { |
962 | 0 | ECerror(ERR_R_EC_LIB); |
963 | 0 | goto err; |
964 | 0 | } |
965 | 0 | ret->order = BN_to_ASN1_INTEGER(tmp, ret->order); |
966 | 0 | if (ret->order == NULL) { |
967 | 0 | ECerror(ERR_R_ASN1_LIB); |
968 | 0 | goto err; |
969 | 0 | } |
970 | | /* set the cofactor (optional) */ |
971 | 0 | if (EC_GROUP_get_cofactor(group, tmp, NULL)) { |
972 | 0 | ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor); |
973 | 0 | if (ret->cofactor == NULL) { |
974 | 0 | ECerror(ERR_R_ASN1_LIB); |
975 | 0 | goto err; |
976 | 0 | } |
977 | 0 | } |
978 | 0 | ok = 1; |
979 | |
|
980 | 0 | err: |
981 | 0 | if (!ok) { |
982 | 0 | if (ret && !param) |
983 | 0 | ECPARAMETERS_free(ret); |
984 | 0 | ret = NULL; |
985 | 0 | } |
986 | 0 | BN_free(tmp); |
987 | 0 | free(buffer); |
988 | 0 | return (ret); |
989 | 0 | } |
990 | | |
991 | | ECPKPARAMETERS * |
992 | | ec_asn1_group2pkparameters(const EC_GROUP * group, ECPKPARAMETERS * params) |
993 | 0 | { |
994 | 0 | int ok = 1, tmp; |
995 | 0 | ECPKPARAMETERS *ret = params; |
996 | |
|
997 | 0 | if (ret == NULL) { |
998 | 0 | if ((ret = ECPKPARAMETERS_new()) == NULL) { |
999 | 0 | ECerror(ERR_R_MALLOC_FAILURE); |
1000 | 0 | return NULL; |
1001 | 0 | } |
1002 | 0 | } else { |
1003 | 0 | if (ret->type == 0 && ret->value.named_curve) |
1004 | 0 | ASN1_OBJECT_free(ret->value.named_curve); |
1005 | 0 | else if (ret->type == 1 && ret->value.parameters) |
1006 | 0 | ECPARAMETERS_free(ret->value.parameters); |
1007 | 0 | } |
1008 | | |
1009 | 0 | if (EC_GROUP_get_asn1_flag(group)) { |
1010 | | /* |
1011 | | * use the asn1 OID to describe the elliptic curve |
1012 | | * parameters |
1013 | | */ |
1014 | 0 | tmp = EC_GROUP_get_curve_name(group); |
1015 | 0 | if (tmp) { |
1016 | 0 | ret->type = 0; |
1017 | 0 | if ((ret->value.named_curve = OBJ_nid2obj(tmp)) == NULL) |
1018 | 0 | ok = 0; |
1019 | 0 | } else |
1020 | | /* we don't know the group => ERROR */ |
1021 | 0 | ok = 0; |
1022 | 0 | } else { |
1023 | | /* use the ECPARAMETERS structure */ |
1024 | 0 | ret->type = 1; |
1025 | 0 | if ((ret->value.parameters = ec_asn1_group2parameters( |
1026 | 0 | group, NULL)) == NULL) |
1027 | 0 | ok = 0; |
1028 | 0 | } |
1029 | |
|
1030 | 0 | if (!ok) { |
1031 | 0 | ECPKPARAMETERS_free(ret); |
1032 | 0 | return NULL; |
1033 | 0 | } |
1034 | 0 | return ret; |
1035 | 0 | } |
1036 | | |
1037 | | static EC_GROUP * |
1038 | | ec_asn1_parameters2group(const ECPARAMETERS * params) |
1039 | 0 | { |
1040 | 0 | int ok = 0, tmp; |
1041 | 0 | EC_GROUP *ret = NULL; |
1042 | 0 | BIGNUM *p = NULL, *a = NULL, *b = NULL; |
1043 | 0 | EC_POINT *point = NULL; |
1044 | 0 | long field_bits; |
1045 | |
|
1046 | 0 | if (!params->fieldID || !params->fieldID->fieldType || |
1047 | 0 | !params->fieldID->p.ptr) { |
1048 | 0 | ECerror(EC_R_ASN1_ERROR); |
1049 | 0 | goto err; |
1050 | 0 | } |
1051 | | /* now extract the curve parameters a and b */ |
1052 | 0 | if (!params->curve || !params->curve->a || |
1053 | 0 | !params->curve->a->data || !params->curve->b || |
1054 | 0 | !params->curve->b->data) { |
1055 | 0 | ECerror(EC_R_ASN1_ERROR); |
1056 | 0 | goto err; |
1057 | 0 | } |
1058 | 0 | a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL); |
1059 | 0 | if (a == NULL) { |
1060 | 0 | ECerror(ERR_R_BN_LIB); |
1061 | 0 | goto err; |
1062 | 0 | } |
1063 | 0 | b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL); |
1064 | 0 | if (b == NULL) { |
1065 | 0 | ECerror(ERR_R_BN_LIB); |
1066 | 0 | goto err; |
1067 | 0 | } |
1068 | | /* get the field parameters */ |
1069 | 0 | tmp = OBJ_obj2nid(params->fieldID->fieldType); |
1070 | 0 | if (tmp == NID_X9_62_characteristic_two_field) |
1071 | | #ifdef OPENSSL_NO_EC2M |
1072 | | { |
1073 | | ECerror(EC_R_GF2M_NOT_SUPPORTED); |
1074 | | goto err; |
1075 | | } |
1076 | | #else |
1077 | 0 | { |
1078 | 0 | X9_62_CHARACTERISTIC_TWO *char_two; |
1079 | |
|
1080 | 0 | char_two = params->fieldID->p.char_two; |
1081 | |
|
1082 | 0 | field_bits = char_two->m; |
1083 | 0 | if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) { |
1084 | 0 | ECerror(EC_R_FIELD_TOO_LARGE); |
1085 | 0 | goto err; |
1086 | 0 | } |
1087 | 0 | if ((p = BN_new()) == NULL) { |
1088 | 0 | ECerror(ERR_R_MALLOC_FAILURE); |
1089 | 0 | goto err; |
1090 | 0 | } |
1091 | | /* get the base type */ |
1092 | 0 | tmp = OBJ_obj2nid(char_two->type); |
1093 | |
|
1094 | 0 | if (tmp == NID_X9_62_tpBasis) { |
1095 | 0 | long tmp_long; |
1096 | |
|
1097 | 0 | if (!char_two->p.tpBasis) { |
1098 | 0 | ECerror(EC_R_ASN1_ERROR); |
1099 | 0 | goto err; |
1100 | 0 | } |
1101 | 0 | tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis); |
1102 | |
|
1103 | 0 | if (!(char_two->m > tmp_long && tmp_long > 0)) { |
1104 | 0 | ECerror(EC_R_INVALID_TRINOMIAL_BASIS); |
1105 | 0 | goto err; |
1106 | 0 | } |
1107 | | /* create the polynomial */ |
1108 | 0 | if (!BN_set_bit(p, (int) char_two->m)) |
1109 | 0 | goto err; |
1110 | 0 | if (!BN_set_bit(p, (int) tmp_long)) |
1111 | 0 | goto err; |
1112 | 0 | if (!BN_set_bit(p, 0)) |
1113 | 0 | goto err; |
1114 | 0 | } else if (tmp == NID_X9_62_ppBasis) { |
1115 | 0 | X9_62_PENTANOMIAL *penta; |
1116 | |
|
1117 | 0 | penta = char_two->p.ppBasis; |
1118 | 0 | if (!penta) { |
1119 | 0 | ECerror(EC_R_ASN1_ERROR); |
1120 | 0 | goto err; |
1121 | 0 | } |
1122 | 0 | if (!(char_two->m > penta->k3 && penta->k3 > penta->k2 && penta->k2 > penta->k1 && penta->k1 > 0)) { |
1123 | 0 | ECerror(EC_R_INVALID_PENTANOMIAL_BASIS); |
1124 | 0 | goto err; |
1125 | 0 | } |
1126 | | /* create the polynomial */ |
1127 | 0 | if (!BN_set_bit(p, (int) char_two->m)) |
1128 | 0 | goto err; |
1129 | 0 | if (!BN_set_bit(p, (int) penta->k1)) |
1130 | 0 | goto err; |
1131 | 0 | if (!BN_set_bit(p, (int) penta->k2)) |
1132 | 0 | goto err; |
1133 | 0 | if (!BN_set_bit(p, (int) penta->k3)) |
1134 | 0 | goto err; |
1135 | 0 | if (!BN_set_bit(p, 0)) |
1136 | 0 | goto err; |
1137 | 0 | } else if (tmp == NID_X9_62_onBasis) { |
1138 | 0 | ECerror(EC_R_NOT_IMPLEMENTED); |
1139 | 0 | goto err; |
1140 | 0 | } else { /* error */ |
1141 | 0 | ECerror(EC_R_ASN1_ERROR); |
1142 | 0 | goto err; |
1143 | 0 | } |
1144 | | |
1145 | | /* create the EC_GROUP structure */ |
1146 | 0 | ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL); |
1147 | 0 | } |
1148 | 0 | #endif |
1149 | 0 | else if (tmp == NID_X9_62_prime_field) { |
1150 | | /* we have a curve over a prime field */ |
1151 | | /* extract the prime number */ |
1152 | 0 | if (!params->fieldID->p.prime) { |
1153 | 0 | ECerror(EC_R_ASN1_ERROR); |
1154 | 0 | goto err; |
1155 | 0 | } |
1156 | 0 | p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL); |
1157 | 0 | if (p == NULL) { |
1158 | 0 | ECerror(ERR_R_ASN1_LIB); |
1159 | 0 | goto err; |
1160 | 0 | } |
1161 | 0 | if (BN_is_negative(p) || BN_is_zero(p)) { |
1162 | 0 | ECerror(EC_R_INVALID_FIELD); |
1163 | 0 | goto err; |
1164 | 0 | } |
1165 | 0 | field_bits = BN_num_bits(p); |
1166 | 0 | if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) { |
1167 | 0 | ECerror(EC_R_FIELD_TOO_LARGE); |
1168 | 0 | goto err; |
1169 | 0 | } |
1170 | | /* create the EC_GROUP structure */ |
1171 | 0 | ret = EC_GROUP_new_curve_GFp(p, a, b, NULL); |
1172 | 0 | } else { |
1173 | 0 | ECerror(EC_R_INVALID_FIELD); |
1174 | 0 | goto err; |
1175 | 0 | } |
1176 | | |
1177 | 0 | if (ret == NULL) { |
1178 | 0 | ECerror(ERR_R_EC_LIB); |
1179 | 0 | goto err; |
1180 | 0 | } |
1181 | | /* extract seed (optional) */ |
1182 | 0 | if (params->curve->seed != NULL) { |
1183 | 0 | free(ret->seed); |
1184 | 0 | if (!(ret->seed = malloc(params->curve->seed->length))) { |
1185 | 0 | ECerror(ERR_R_MALLOC_FAILURE); |
1186 | 0 | goto err; |
1187 | 0 | } |
1188 | 0 | memcpy(ret->seed, params->curve->seed->data, |
1189 | 0 | params->curve->seed->length); |
1190 | 0 | ret->seed_len = params->curve->seed->length; |
1191 | 0 | } |
1192 | 0 | if (!params->order || !params->base || !params->base->data) { |
1193 | 0 | ECerror(EC_R_ASN1_ERROR); |
1194 | 0 | goto err; |
1195 | 0 | } |
1196 | 0 | if ((point = EC_POINT_new(ret)) == NULL) |
1197 | 0 | goto err; |
1198 | | |
1199 | | /* set the point conversion form */ |
1200 | 0 | EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t) |
1201 | 0 | (params->base->data[0] & ~0x01)); |
1202 | | |
1203 | | /* extract the ec point */ |
1204 | 0 | if (!EC_POINT_oct2point(ret, point, params->base->data, |
1205 | 0 | params->base->length, NULL)) { |
1206 | 0 | ECerror(ERR_R_EC_LIB); |
1207 | 0 | goto err; |
1208 | 0 | } |
1209 | | /* extract the order */ |
1210 | 0 | if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) { |
1211 | 0 | ECerror(ERR_R_ASN1_LIB); |
1212 | 0 | goto err; |
1213 | 0 | } |
1214 | 0 | if (BN_is_negative(a) || BN_is_zero(a)) { |
1215 | 0 | ECerror(EC_R_INVALID_GROUP_ORDER); |
1216 | 0 | goto err; |
1217 | 0 | } |
1218 | 0 | if (BN_num_bits(a) > (int) field_bits + 1) { /* Hasse bound */ |
1219 | 0 | ECerror(EC_R_INVALID_GROUP_ORDER); |
1220 | 0 | goto err; |
1221 | 0 | } |
1222 | | /* extract the cofactor (optional) */ |
1223 | 0 | if (params->cofactor == NULL) { |
1224 | 0 | BN_free(b); |
1225 | 0 | b = NULL; |
1226 | 0 | } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) { |
1227 | 0 | ECerror(ERR_R_ASN1_LIB); |
1228 | 0 | goto err; |
1229 | 0 | } |
1230 | | /* set the generator, order and cofactor (if present) */ |
1231 | 0 | if (!EC_GROUP_set_generator(ret, point, a, b)) { |
1232 | 0 | ECerror(ERR_R_EC_LIB); |
1233 | 0 | goto err; |
1234 | 0 | } |
1235 | 0 | ok = 1; |
1236 | |
|
1237 | 0 | err: |
1238 | 0 | if (!ok) { |
1239 | 0 | EC_GROUP_clear_free(ret); |
1240 | 0 | ret = NULL; |
1241 | 0 | } |
1242 | 0 | BN_free(p); |
1243 | 0 | BN_free(a); |
1244 | 0 | BN_free(b); |
1245 | 0 | EC_POINT_free(point); |
1246 | 0 | return (ret); |
1247 | 0 | } |
1248 | | |
1249 | | EC_GROUP * |
1250 | | ec_asn1_pkparameters2group(const ECPKPARAMETERS * params) |
1251 | 6.07k | { |
1252 | 6.07k | EC_GROUP *ret = NULL; |
1253 | 6.07k | int tmp = 0; |
1254 | | |
1255 | 6.07k | if (params == NULL) { |
1256 | 0 | ECerror(EC_R_MISSING_PARAMETERS); |
1257 | 0 | return NULL; |
1258 | 0 | } |
1259 | 6.07k | if (params->type == 0) {/* the curve is given by an OID */ |
1260 | 6.07k | tmp = OBJ_obj2nid(params->value.named_curve); |
1261 | 6.07k | if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) { |
1262 | 0 | ECerror(EC_R_EC_GROUP_NEW_BY_NAME_FAILURE); |
1263 | 0 | return NULL; |
1264 | 0 | } |
1265 | 6.07k | EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE); |
1266 | 6.07k | } else if (params->type == 1) { /* the parameters are given by a |
1267 | | * ECPARAMETERS structure */ |
1268 | 0 | ret = ec_asn1_parameters2group(params->value.parameters); |
1269 | 0 | if (!ret) { |
1270 | 0 | ECerror(ERR_R_EC_LIB); |
1271 | 0 | return NULL; |
1272 | 0 | } |
1273 | 0 | EC_GROUP_set_asn1_flag(ret, 0x0); |
1274 | 0 | } else if (params->type == 2) { /* implicitlyCA */ |
1275 | 0 | return NULL; |
1276 | 0 | } else { |
1277 | 0 | ECerror(EC_R_ASN1_ERROR); |
1278 | 0 | return NULL; |
1279 | 0 | } |
1280 | | |
1281 | 6.07k | return ret; |
1282 | 6.07k | } |
1283 | | |
1284 | | /* EC_GROUP <-> DER encoding of ECPKPARAMETERS */ |
1285 | | |
1286 | | EC_GROUP * |
1287 | | d2i_ECPKParameters(EC_GROUP ** a, const unsigned char **in, long len) |
1288 | 0 | { |
1289 | 0 | EC_GROUP *group = NULL; |
1290 | 0 | ECPKPARAMETERS *params; |
1291 | |
|
1292 | 0 | if ((params = d2i_ECPKPARAMETERS(NULL, in, len)) == NULL) { |
1293 | 0 | ECerror(EC_R_D2I_ECPKPARAMETERS_FAILURE); |
1294 | 0 | goto err; |
1295 | 0 | } |
1296 | 0 | if ((group = ec_asn1_pkparameters2group(params)) == NULL) { |
1297 | 0 | ECerror(EC_R_PKPARAMETERS2GROUP_FAILURE); |
1298 | 0 | goto err; |
1299 | 0 | } |
1300 | | |
1301 | 0 | if (a != NULL) { |
1302 | 0 | EC_GROUP_clear_free(*a); |
1303 | 0 | *a = group; |
1304 | 0 | } |
1305 | |
|
1306 | 0 | err: |
1307 | 0 | ECPKPARAMETERS_free(params); |
1308 | 0 | return (group); |
1309 | 0 | } |
1310 | | |
1311 | | int |
1312 | | i2d_ECPKParameters(const EC_GROUP * a, unsigned char **out) |
1313 | 0 | { |
1314 | 0 | int ret = 0; |
1315 | 0 | ECPKPARAMETERS *tmp = ec_asn1_group2pkparameters(a, NULL); |
1316 | 0 | if (tmp == NULL) { |
1317 | 0 | ECerror(EC_R_GROUP2PKPARAMETERS_FAILURE); |
1318 | 0 | return 0; |
1319 | 0 | } |
1320 | 0 | if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) { |
1321 | 0 | ECerror(EC_R_I2D_ECPKPARAMETERS_FAILURE); |
1322 | 0 | ECPKPARAMETERS_free(tmp); |
1323 | 0 | return 0; |
1324 | 0 | } |
1325 | 0 | ECPKPARAMETERS_free(tmp); |
1326 | 0 | return (ret); |
1327 | 0 | } |
1328 | | |
1329 | | /* some EC_KEY functions */ |
1330 | | |
1331 | | EC_KEY * |
1332 | | d2i_ECPrivateKey(EC_KEY ** a, const unsigned char **in, long len) |
1333 | 6.07k | { |
1334 | 6.07k | EC_KEY *ret = NULL; |
1335 | 6.07k | EC_PRIVATEKEY *priv_key = NULL; |
1336 | | |
1337 | 6.07k | if ((priv_key = d2i_EC_PRIVATEKEY(NULL, in, len)) == NULL) { |
1338 | 0 | ECerror(ERR_R_EC_LIB); |
1339 | 0 | return NULL; |
1340 | 0 | } |
1341 | 6.07k | if (a == NULL || *a == NULL) { |
1342 | 6.07k | if ((ret = EC_KEY_new()) == NULL) { |
1343 | 0 | ECerror(ERR_R_MALLOC_FAILURE); |
1344 | 0 | goto err; |
1345 | 0 | } |
1346 | 6.07k | } else |
1347 | 0 | ret = *a; |
1348 | | |
1349 | 6.07k | if (priv_key->parameters) { |
1350 | 6.07k | EC_GROUP_clear_free(ret->group); |
1351 | 6.07k | ret->group = ec_asn1_pkparameters2group(priv_key->parameters); |
1352 | 6.07k | } |
1353 | 6.07k | if (ret->group == NULL) { |
1354 | 0 | ECerror(ERR_R_EC_LIB); |
1355 | 0 | goto err; |
1356 | 0 | } |
1357 | 6.07k | ret->version = priv_key->version; |
1358 | | |
1359 | 6.07k | if (priv_key->privateKey) { |
1360 | 6.07k | ret->priv_key = BN_bin2bn( |
1361 | 6.07k | ASN1_STRING_data(priv_key->privateKey), |
1362 | 6.07k | ASN1_STRING_length(priv_key->privateKey), |
1363 | 6.07k | ret->priv_key); |
1364 | 6.07k | if (ret->priv_key == NULL) { |
1365 | 0 | ECerror(ERR_R_BN_LIB); |
1366 | 0 | goto err; |
1367 | 0 | } |
1368 | 6.07k | } else { |
1369 | 0 | ECerror(EC_R_MISSING_PRIVATE_KEY); |
1370 | 0 | goto err; |
1371 | 0 | } |
1372 | | |
1373 | 6.07k | if (ret->pub_key) |
1374 | 0 | EC_POINT_clear_free(ret->pub_key); |
1375 | 6.07k | ret->pub_key = EC_POINT_new(ret->group); |
1376 | 6.07k | if (ret->pub_key == NULL) { |
1377 | 0 | ECerror(ERR_R_EC_LIB); |
1378 | 0 | goto err; |
1379 | 0 | } |
1380 | | |
1381 | 6.07k | if (priv_key->publicKey) { |
1382 | 6.07k | const unsigned char *pub_oct; |
1383 | 6.07k | size_t pub_oct_len; |
1384 | | |
1385 | 6.07k | pub_oct = ASN1_STRING_data(priv_key->publicKey); |
1386 | 6.07k | pub_oct_len = ASN1_STRING_length(priv_key->publicKey); |
1387 | 6.07k | if (pub_oct == NULL || pub_oct_len <= 0) { |
1388 | 0 | ECerror(EC_R_BUFFER_TOO_SMALL); |
1389 | 0 | goto err; |
1390 | 0 | } |
1391 | | |
1392 | | /* save the point conversion form */ |
1393 | 6.07k | ret->conv_form = (point_conversion_form_t) (pub_oct[0] & ~0x01); |
1394 | 6.07k | if (!EC_POINT_oct2point(ret->group, ret->pub_key, |
1395 | 6.07k | pub_oct, pub_oct_len, NULL)) { |
1396 | 0 | ECerror(ERR_R_EC_LIB); |
1397 | 0 | goto err; |
1398 | 0 | } |
1399 | 6.07k | } else { |
1400 | 0 | if (!EC_POINT_mul(ret->group, ret->pub_key, ret->priv_key, |
1401 | 0 | NULL, NULL, NULL)) { |
1402 | 0 | ECerror(ERR_R_EC_LIB); |
1403 | 0 | goto err; |
1404 | 0 | } |
1405 | | /* Remember the original private-key-only encoding. */ |
1406 | 0 | ret->enc_flag |= EC_PKEY_NO_PUBKEY; |
1407 | 0 | } |
1408 | | |
1409 | 6.07k | EC_PRIVATEKEY_free(priv_key); |
1410 | 6.07k | if (a != NULL) |
1411 | 0 | *a = ret; |
1412 | 6.07k | return (ret); |
1413 | | |
1414 | 0 | err: |
1415 | 0 | if (a == NULL || *a != ret) |
1416 | 0 | EC_KEY_free(ret); |
1417 | 0 | if (priv_key) |
1418 | 0 | EC_PRIVATEKEY_free(priv_key); |
1419 | |
|
1420 | 0 | return (NULL); |
1421 | 6.07k | } |
1422 | | |
1423 | | int |
1424 | | i2d_ECPrivateKey(EC_KEY * a, unsigned char **out) |
1425 | 0 | { |
1426 | 0 | int ret = 0, ok = 0; |
1427 | 0 | unsigned char *buffer = NULL; |
1428 | 0 | size_t buf_len = 0, tmp_len; |
1429 | 0 | EC_PRIVATEKEY *priv_key = NULL; |
1430 | |
|
1431 | 0 | if (a == NULL || a->group == NULL || a->priv_key == NULL || |
1432 | 0 | (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key == NULL)) { |
1433 | 0 | ECerror(ERR_R_PASSED_NULL_PARAMETER); |
1434 | 0 | goto err; |
1435 | 0 | } |
1436 | 0 | if ((priv_key = EC_PRIVATEKEY_new()) == NULL) { |
1437 | 0 | ECerror(ERR_R_MALLOC_FAILURE); |
1438 | 0 | goto err; |
1439 | 0 | } |
1440 | 0 | priv_key->version = a->version; |
1441 | |
|
1442 | 0 | buf_len = (size_t) BN_num_bytes(a->priv_key); |
1443 | 0 | buffer = malloc(buf_len); |
1444 | 0 | if (buffer == NULL) { |
1445 | 0 | ECerror(ERR_R_MALLOC_FAILURE); |
1446 | 0 | goto err; |
1447 | 0 | } |
1448 | 0 | if (!BN_bn2bin(a->priv_key, buffer)) { |
1449 | 0 | ECerror(ERR_R_BN_LIB); |
1450 | 0 | goto err; |
1451 | 0 | } |
1452 | 0 | if (!ASN1_STRING_set(priv_key->privateKey, buffer, buf_len)) { |
1453 | 0 | ECerror(ERR_R_ASN1_LIB); |
1454 | 0 | goto err; |
1455 | 0 | } |
1456 | 0 | if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) { |
1457 | 0 | if ((priv_key->parameters = ec_asn1_group2pkparameters( |
1458 | 0 | a->group, priv_key->parameters)) == NULL) { |
1459 | 0 | ECerror(ERR_R_EC_LIB); |
1460 | 0 | goto err; |
1461 | 0 | } |
1462 | 0 | } |
1463 | 0 | if (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key != NULL) { |
1464 | 0 | priv_key->publicKey = ASN1_BIT_STRING_new(); |
1465 | 0 | if (priv_key->publicKey == NULL) { |
1466 | 0 | ECerror(ERR_R_MALLOC_FAILURE); |
1467 | 0 | goto err; |
1468 | 0 | } |
1469 | 0 | tmp_len = EC_POINT_point2oct(a->group, a->pub_key, |
1470 | 0 | a->conv_form, NULL, 0, NULL); |
1471 | |
|
1472 | 0 | if (tmp_len > buf_len) { |
1473 | 0 | unsigned char *tmp_buffer = realloc(buffer, tmp_len); |
1474 | 0 | if (!tmp_buffer) { |
1475 | 0 | ECerror(ERR_R_MALLOC_FAILURE); |
1476 | 0 | goto err; |
1477 | 0 | } |
1478 | 0 | buffer = tmp_buffer; |
1479 | 0 | buf_len = tmp_len; |
1480 | 0 | } |
1481 | 0 | if (!EC_POINT_point2oct(a->group, a->pub_key, |
1482 | 0 | a->conv_form, buffer, buf_len, NULL)) { |
1483 | 0 | ECerror(ERR_R_EC_LIB); |
1484 | 0 | goto err; |
1485 | 0 | } |
1486 | 0 | if (!ASN1_STRING_set(priv_key->publicKey, buffer, buf_len)) { |
1487 | 0 | ECerror(ERR_R_ASN1_LIB); |
1488 | 0 | goto err; |
1489 | 0 | } |
1490 | 0 | if (!asn1_abs_set_unused_bits(priv_key->publicKey, 0)) { |
1491 | 0 | ECerror(ERR_R_ASN1_LIB); |
1492 | 0 | goto err; |
1493 | 0 | } |
1494 | 0 | } |
1495 | 0 | if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) { |
1496 | 0 | ECerror(ERR_R_EC_LIB); |
1497 | 0 | goto err; |
1498 | 0 | } |
1499 | 0 | ok = 1; |
1500 | 0 | err: |
1501 | 0 | free(buffer); |
1502 | 0 | if (priv_key) |
1503 | 0 | EC_PRIVATEKEY_free(priv_key); |
1504 | 0 | return (ok ? ret : 0); |
1505 | 0 | } |
1506 | | |
1507 | | int |
1508 | | i2d_ECParameters(EC_KEY * a, unsigned char **out) |
1509 | 0 | { |
1510 | 0 | if (a == NULL) { |
1511 | 0 | ECerror(ERR_R_PASSED_NULL_PARAMETER); |
1512 | 0 | return 0; |
1513 | 0 | } |
1514 | 0 | return i2d_ECPKParameters(a->group, out); |
1515 | 0 | } |
1516 | | |
1517 | | EC_KEY * |
1518 | | d2i_ECParameters(EC_KEY ** a, const unsigned char **in, long len) |
1519 | 0 | { |
1520 | 0 | EC_KEY *ret; |
1521 | |
|
1522 | 0 | if (in == NULL || *in == NULL) { |
1523 | 0 | ECerror(ERR_R_PASSED_NULL_PARAMETER); |
1524 | 0 | return NULL; |
1525 | 0 | } |
1526 | 0 | if (a == NULL || *a == NULL) { |
1527 | 0 | if ((ret = EC_KEY_new()) == NULL) { |
1528 | 0 | ECerror(ERR_R_MALLOC_FAILURE); |
1529 | 0 | return NULL; |
1530 | 0 | } |
1531 | 0 | } else |
1532 | 0 | ret = *a; |
1533 | | |
1534 | 0 | if (!d2i_ECPKParameters(&ret->group, in, len)) { |
1535 | 0 | ECerror(ERR_R_EC_LIB); |
1536 | 0 | if (a == NULL || *a != ret) |
1537 | 0 | EC_KEY_free(ret); |
1538 | 0 | return NULL; |
1539 | 0 | } |
1540 | | |
1541 | 0 | if (a != NULL) |
1542 | 0 | *a = ret; |
1543 | 0 | return ret; |
1544 | 0 | } |
1545 | | |
1546 | | EC_KEY * |
1547 | | o2i_ECPublicKey(EC_KEY ** a, const unsigned char **in, long len) |
1548 | 6.07k | { |
1549 | 6.07k | EC_KEY *ret = NULL; |
1550 | | |
1551 | 6.07k | if (a == NULL || (*a) == NULL || (*a)->group == NULL) { |
1552 | | /* An EC_GROUP structure is necessary to set the public key. */ |
1553 | 0 | ECerror(ERR_R_PASSED_NULL_PARAMETER); |
1554 | 0 | return 0; |
1555 | 0 | } |
1556 | 6.07k | ret = *a; |
1557 | 6.07k | if (ret->pub_key == NULL && |
1558 | 6.07k | (ret->pub_key = EC_POINT_new(ret->group)) == NULL) { |
1559 | 0 | ECerror(ERR_R_MALLOC_FAILURE); |
1560 | 0 | return 0; |
1561 | 0 | } |
1562 | 6.07k | if (!EC_POINT_oct2point(ret->group, ret->pub_key, *in, len, NULL)) { |
1563 | 0 | ECerror(ERR_R_EC_LIB); |
1564 | 0 | return 0; |
1565 | 0 | } |
1566 | | /* save the point conversion form */ |
1567 | 6.07k | ret->conv_form = (point_conversion_form_t) (*in[0] & ~0x01); |
1568 | 6.07k | *in += len; |
1569 | 6.07k | return ret; |
1570 | 6.07k | } |
1571 | | |
1572 | | int |
1573 | | i2o_ECPublicKey(const EC_KEY * a, unsigned char **out) |
1574 | 0 | { |
1575 | 0 | size_t buf_len = 0; |
1576 | 0 | int new_buffer = 0; |
1577 | |
|
1578 | 0 | if (a == NULL) { |
1579 | 0 | ECerror(ERR_R_PASSED_NULL_PARAMETER); |
1580 | 0 | return 0; |
1581 | 0 | } |
1582 | 0 | buf_len = EC_POINT_point2oct(a->group, a->pub_key, |
1583 | 0 | a->conv_form, NULL, 0, NULL); |
1584 | |
|
1585 | 0 | if (out == NULL || buf_len == 0) |
1586 | | /* out == NULL => just return the length of the octet string */ |
1587 | 0 | return buf_len; |
1588 | | |
1589 | 0 | if (*out == NULL) { |
1590 | 0 | if ((*out = malloc(buf_len)) == NULL) { |
1591 | 0 | ECerror(ERR_R_MALLOC_FAILURE); |
1592 | 0 | return 0; |
1593 | 0 | } |
1594 | 0 | new_buffer = 1; |
1595 | 0 | } |
1596 | 0 | if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form, |
1597 | 0 | *out, buf_len, NULL)) { |
1598 | 0 | ECerror(ERR_R_EC_LIB); |
1599 | 0 | if (new_buffer) { |
1600 | 0 | free(*out); |
1601 | 0 | *out = NULL; |
1602 | 0 | } |
1603 | 0 | return 0; |
1604 | 0 | } |
1605 | 0 | if (!new_buffer) |
1606 | 0 | *out += buf_len; |
1607 | 0 | return buf_len; |
1608 | 0 | } |