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