]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ec/ec_asn1.c
Add set methods.
[thirdparty/openssl.git] / crypto / ec / ec_asn1.c
CommitLineData
012c86ab 1/* crypto/ec/ec_asn1.c */
7e31164a
BM
2/*
3 * Written by Nils Larsch for the OpenSSL project.
4 */
012c86ab 5/* ====================================================================
82516e3b 6 * Copyright (c) 2000-2003 The OpenSSL Project. All rights reserved.
012c86ab
BM
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
0f113f3e 13 * notice, this list of conditions and the following disclaimer.
012c86ab
BM
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
5454829a 59#include <string.h>
012c86ab 60#include "ec_lcl.h"
0bee0e62 61#include <openssl/err.h>
012c86ab
BM
62#include <openssl/asn1t.h>
63#include <openssl/objects.h>
64
34f1f2a8 65int EC_GROUP_get_basis_type(const EC_GROUP *group)
0f113f3e
MC
66{
67 int i = 0;
68
69 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
70 NID_X9_62_characteristic_two_field)
71 /* everything else is currently not supported */
72 return 0;
73
74 while (group->poly[i] != 0)
75 i++;
76
77 if (i == 4)
78 return NID_X9_62_ppBasis;
79 else if (i == 2)
80 return NID_X9_62_tpBasis;
81 else
82 /* everything else is currently not supported */
83 return 0;
84}
85
b3310161 86#ifndef OPENSSL_NO_EC2M
34f1f2a8 87int EC_GROUP_get_trinomial_basis(const EC_GROUP *group, unsigned int *k)
0f113f3e
MC
88{
89 if (group == NULL)
90 return 0;
91
92 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
93 NID_X9_62_characteristic_two_field
94 || !((group->poly[0] != 0) && (group->poly[1] != 0)
95 && (group->poly[2] == 0))) {
96 ECerr(EC_F_EC_GROUP_GET_TRINOMIAL_BASIS,
97 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
98 return 0;
99 }
100
101 if (k)
102 *k = group->poly[1];
103
104 return 1;
105}
106
34f1f2a8 107int EC_GROUP_get_pentanomial_basis(const EC_GROUP *group, unsigned int *k1,
0f113f3e
MC
108 unsigned int *k2, unsigned int *k3)
109{
110 if (group == NULL)
111 return 0;
112
113 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
114 NID_X9_62_characteristic_two_field
115 || !((group->poly[0] != 0) && (group->poly[1] != 0)
116 && (group->poly[2] != 0) && (group->poly[3] != 0)
117 && (group->poly[4] == 0))) {
118 ECerr(EC_F_EC_GROUP_GET_PENTANOMIAL_BASIS,
119 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
120 return 0;
121 }
122
123 if (k1)
124 *k1 = group->poly[3];
125 if (k2)
126 *k2 = group->poly[2];
127 if (k3)
128 *k3 = group->poly[1];
129
130 return 1;
131}
b3310161 132#endif
8aefe253 133
012c86ab 134/* some structures needed for the asn1 encoding */
012c86ab 135typedef struct x9_62_pentanomial_st {
0f113f3e
MC
136 long k1;
137 long k2;
138 long k3;
139} X9_62_PENTANOMIAL;
012c86ab 140
37af03d3 141typedef struct x9_62_characteristic_two_st {
0f113f3e
MC
142 long m;
143 ASN1_OBJECT *type;
144 union {
145 char *ptr;
146 /* NID_X9_62_onBasis */
147 ASN1_NULL *onBasis;
148 /* NID_X9_62_tpBasis */
149 ASN1_INTEGER *tpBasis;
150 /* NID_X9_62_ppBasis */
151 X9_62_PENTANOMIAL *ppBasis;
152 /* anything else */
153 ASN1_TYPE *other;
154 } p;
155} X9_62_CHARACTERISTIC_TWO;
37af03d3
GT
156
157typedef struct x9_62_fieldid_st {
0f113f3e
MC
158 ASN1_OBJECT *fieldType;
159 union {
160 char *ptr;
161 /* NID_X9_62_prime_field */
162 ASN1_INTEGER *prime;
163 /* NID_X9_62_characteristic_two_field */
164 X9_62_CHARACTERISTIC_TWO *char_two;
165 /* anything else */
166 ASN1_TYPE *other;
167 } p;
168} X9_62_FIELDID;
37af03d3 169
012c86ab 170typedef struct x9_62_curve_st {
0f113f3e
MC
171 ASN1_OCTET_STRING *a;
172 ASN1_OCTET_STRING *b;
173 ASN1_BIT_STRING *seed;
174} X9_62_CURVE;
012c86ab 175
14a7cfb3 176typedef struct ec_parameters_st {
0f113f3e
MC
177 long version;
178 X9_62_FIELDID *fieldID;
179 X9_62_CURVE *curve;
180 ASN1_OCTET_STRING *base;
181 ASN1_INTEGER *order;
182 ASN1_INTEGER *cofactor;
183} ECPARAMETERS;
012c86ab
BM
184
185struct ecpk_parameters_st {
0f113f3e
MC
186 int type;
187 union {
188 ASN1_OBJECT *named_curve;
189 ECPARAMETERS *parameters;
190 ASN1_NULL *implicitlyCA;
191 } value;
192} /* ECPKPARAMETERS */ ;
012c86ab 193
14a7cfb3
BM
194/* SEC1 ECPrivateKey */
195typedef struct ec_privatekey_st {
0f113f3e
MC
196 long version;
197 ASN1_OCTET_STRING *privateKey;
198 ECPKPARAMETERS *parameters;
199 ASN1_BIT_STRING *publicKey;
200} EC_PRIVATEKEY;
14a7cfb3 201
37af03d3
GT
202/* the OpenSSL ASN.1 definitions */
203ASN1_SEQUENCE(X9_62_PENTANOMIAL) = {
0f113f3e
MC
204 ASN1_SIMPLE(X9_62_PENTANOMIAL, k1, LONG),
205 ASN1_SIMPLE(X9_62_PENTANOMIAL, k2, LONG),
206 ASN1_SIMPLE(X9_62_PENTANOMIAL, k3, LONG)
df2ee0e2 207} static_ASN1_SEQUENCE_END(X9_62_PENTANOMIAL)
012c86ab 208
37af03d3
GT
209DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
210IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
211
212ASN1_ADB_TEMPLATE(char_two_def) = ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.other, ASN1_ANY);
012c86ab 213
37af03d3 214ASN1_ADB(X9_62_CHARACTERISTIC_TWO) = {
0f113f3e
MC
215 ADB_ENTRY(NID_X9_62_onBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.onBasis, ASN1_NULL)),
216 ADB_ENTRY(NID_X9_62_tpBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.tpBasis, ASN1_INTEGER)),
217 ADB_ENTRY(NID_X9_62_ppBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.ppBasis, X9_62_PENTANOMIAL))
37af03d3 218} ASN1_ADB_END(X9_62_CHARACTERISTIC_TWO, 0, type, 0, &char_two_def_tt, NULL);
012c86ab
BM
219
220ASN1_SEQUENCE(X9_62_CHARACTERISTIC_TWO) = {
0f113f3e
MC
221 ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, m, LONG),
222 ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, type, ASN1_OBJECT),
223 ASN1_ADB_OBJECT(X9_62_CHARACTERISTIC_TWO)
df2ee0e2 224} static_ASN1_SEQUENCE_END(X9_62_CHARACTERISTIC_TWO)
012c86ab 225
37af03d3
GT
226DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
227IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
012c86ab 228
37af03d3
GT
229ASN1_ADB_TEMPLATE(fieldID_def) = ASN1_SIMPLE(X9_62_FIELDID, p.other, ASN1_ANY);
230
231ASN1_ADB(X9_62_FIELDID) = {
0f113f3e
MC
232 ADB_ENTRY(NID_X9_62_prime_field, ASN1_SIMPLE(X9_62_FIELDID, p.prime, ASN1_INTEGER)),
233 ADB_ENTRY(NID_X9_62_characteristic_two_field, ASN1_SIMPLE(X9_62_FIELDID, p.char_two, X9_62_CHARACTERISTIC_TWO))
37af03d3 234} ASN1_ADB_END(X9_62_FIELDID, 0, fieldType, 0, &fieldID_def_tt, NULL);
012c86ab 235
37af03d3 236ASN1_SEQUENCE(X9_62_FIELDID) = {
0f113f3e
MC
237 ASN1_SIMPLE(X9_62_FIELDID, fieldType, ASN1_OBJECT),
238 ASN1_ADB_OBJECT(X9_62_FIELDID)
df2ee0e2 239} static_ASN1_SEQUENCE_END(X9_62_FIELDID)
012c86ab
BM
240
241ASN1_SEQUENCE(X9_62_CURVE) = {
0f113f3e
MC
242 ASN1_SIMPLE(X9_62_CURVE, a, ASN1_OCTET_STRING),
243 ASN1_SIMPLE(X9_62_CURVE, b, ASN1_OCTET_STRING),
244 ASN1_OPT(X9_62_CURVE, seed, ASN1_BIT_STRING)
df2ee0e2 245} static_ASN1_SEQUENCE_END(X9_62_CURVE)
012c86ab 246
012c86ab 247ASN1_SEQUENCE(ECPARAMETERS) = {
0f113f3e
MC
248 ASN1_SIMPLE(ECPARAMETERS, version, LONG),
249 ASN1_SIMPLE(ECPARAMETERS, fieldID, X9_62_FIELDID),
250 ASN1_SIMPLE(ECPARAMETERS, curve, X9_62_CURVE),
251 ASN1_SIMPLE(ECPARAMETERS, base, ASN1_OCTET_STRING),
252 ASN1_SIMPLE(ECPARAMETERS, order, ASN1_INTEGER),
253 ASN1_OPT(ECPARAMETERS, cofactor, ASN1_INTEGER)
df2ee0e2 254} static_ASN1_SEQUENCE_END(ECPARAMETERS)
012c86ab 255
37af03d3
GT
256DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
257IMPLEMENT_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
012c86ab
BM
258
259ASN1_CHOICE(ECPKPARAMETERS) = {
0f113f3e
MC
260 ASN1_SIMPLE(ECPKPARAMETERS, value.named_curve, ASN1_OBJECT),
261 ASN1_SIMPLE(ECPKPARAMETERS, value.parameters, ECPARAMETERS),
262 ASN1_SIMPLE(ECPKPARAMETERS, value.implicitlyCA, ASN1_NULL)
df2ee0e2 263} static_ASN1_CHOICE_END(ECPKPARAMETERS)
012c86ab
BM
264
265DECLARE_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
14a7cfb3 266DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECPKPARAMETERS, ECPKPARAMETERS)
012c86ab
BM
267IMPLEMENT_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
268
0bee0e62 269ASN1_SEQUENCE(EC_PRIVATEKEY) = {
0f113f3e
MC
270 ASN1_SIMPLE(EC_PRIVATEKEY, version, LONG),
271 ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING),
272 ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0),
273 ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1)
df2ee0e2 274} static_ASN1_SEQUENCE_END(EC_PRIVATEKEY)
0bee0e62 275
14a7cfb3
BM
276DECLARE_ASN1_FUNCTIONS_const(EC_PRIVATEKEY)
277DECLARE_ASN1_ENCODE_FUNCTIONS_const(EC_PRIVATEKEY, EC_PRIVATEKEY)
0bee0e62
BM
278IMPLEMENT_ASN1_FUNCTIONS_const(EC_PRIVATEKEY)
279
7e31164a 280/* some declarations of internal function */
012c86ab 281
0f113f3e 282/* ec_asn1_group2field() sets the values in a X9_62_FIELDID object */
37af03d3 283static int ec_asn1_group2fieldid(const EC_GROUP *, X9_62_FIELDID *);
0f113f3e 284/* ec_asn1_group2curve() sets the values in a X9_62_CURVE object */
37af03d3 285static int ec_asn1_group2curve(const EC_GROUP *, X9_62_CURVE *);
0f113f3e
MC
286/*
287 * ec_asn1_parameters2group() creates a EC_GROUP object from a ECPARAMETERS
288 * object
289 */
290static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *);
291/*
292 * ec_asn1_group2parameters() creates a ECPARAMETERS object from a EC_GROUP
293 * object
294 */
295static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *,
296 ECPARAMETERS *);
297/*
298 * ec_asn1_pkparameters2group() creates a EC_GROUP object from a
299 * ECPKPARAMETERS object
300 */
301static EC_GROUP *ec_asn1_pkparameters2group(const ECPKPARAMETERS *);
302/*
303 * ec_asn1_group2pkparameters() creates a ECPKPARAMETERS object from a
304 * EC_GROUP object
305 */
306static ECPKPARAMETERS *ec_asn1_group2pkparameters(const EC_GROUP *,
307 ECPKPARAMETERS *);
7e31164a
BM
308
309/* the function definitions */
012c86ab 310
37af03d3 311static int ec_asn1_group2fieldid(const EC_GROUP *group, X9_62_FIELDID *field)
0f113f3e
MC
312{
313 int ok = 0, nid;
314 BIGNUM *tmp = NULL;
315
316 if (group == NULL || field == NULL)
317 return 0;
318
319 /* clear the old values (if necessary) */
0dfb9398 320 ASN1_OBJECT_free(field->fieldType);
2ace7450 321 ASN1_TYPE_free(field->p.other);
0f113f3e
MC
322
323 nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
324 /* set OID for the field */
325 if ((field->fieldType = OBJ_nid2obj(nid)) == NULL) {
326 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
327 goto err;
328 }
329
330 if (nid == NID_X9_62_prime_field) {
331 if ((tmp = BN_new()) == NULL) {
332 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
333 goto err;
334 }
335 /* the parameters are specified by the prime number p */
336 if (!EC_GROUP_get_curve_GFp(group, tmp, NULL, NULL, NULL)) {
337 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
338 goto err;
339 }
340 /* set the prime number */
341 field->p.prime = BN_to_ASN1_INTEGER(tmp, NULL);
342 if (field->p.prime == NULL) {
343 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
344 goto err;
345 }
346 } else /* nid == NID_X9_62_characteristic_two_field */
b3310161 347#ifdef OPENSSL_NO_EC2M
0f113f3e
MC
348 {
349 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_GF2M_NOT_SUPPORTED);
350 goto err;
351 }
b3310161 352#else
0f113f3e
MC
353 {
354 int field_type;
355 X9_62_CHARACTERISTIC_TWO *char_two;
356
357 field->p.char_two = X9_62_CHARACTERISTIC_TWO_new();
358 char_two = field->p.char_two;
359
360 if (char_two == NULL) {
361 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
362 goto err;
363 }
364
365 char_two->m = (long)EC_GROUP_get_degree(group);
366
367 field_type = EC_GROUP_get_basis_type(group);
368
369 if (field_type == 0) {
370 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
371 goto err;
372 }
373 /* set base type OID */
374 if ((char_two->type = OBJ_nid2obj(field_type)) == NULL) {
375 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
376 goto err;
377 }
378
379 if (field_type == NID_X9_62_tpBasis) {
380 unsigned int k;
381
382 if (!EC_GROUP_get_trinomial_basis(group, &k))
383 goto err;
384
385 char_two->p.tpBasis = ASN1_INTEGER_new();
90945fa3 386 if (char_two->p.tpBasis == NULL) {
0f113f3e
MC
387 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
388 goto err;
389 }
390 if (!ASN1_INTEGER_set(char_two->p.tpBasis, (long)k)) {
391 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
392 goto err;
393 }
394 } else if (field_type == NID_X9_62_ppBasis) {
395 unsigned int k1, k2, k3;
396
397 if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3))
398 goto err;
399
400 char_two->p.ppBasis = X9_62_PENTANOMIAL_new();
90945fa3 401 if (char_two->p.ppBasis == NULL) {
0f113f3e
MC
402 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
403 goto err;
404 }
405
406 /* set k? values */
407 char_two->p.ppBasis->k1 = (long)k1;
408 char_two->p.ppBasis->k2 = (long)k2;
409 char_two->p.ppBasis->k3 = (long)k3;
410 } else { /* field_type == NID_X9_62_onBasis */
411
412 /* for ONB the parameters are (asn1) NULL */
413 char_two->p.onBasis = ASN1_NULL_new();
90945fa3 414 if (char_two->p.onBasis == NULL) {
0f113f3e
MC
415 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
416 goto err;
417 }
418 }
419 }
b3310161 420#endif
012c86ab 421
0f113f3e 422 ok = 1;
012c86ab 423
23a1d5e9
RS
424 err:
425 BN_free(tmp);
0f113f3e 426 return (ok);
012c86ab
BM
427}
428
37af03d3 429static int ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve)
0f113f3e
MC
430{
431 int ok = 0, nid;
432 BIGNUM *tmp_1 = NULL, *tmp_2 = NULL;
433 unsigned char *buffer_1 = NULL, *buffer_2 = NULL,
434 *a_buf = NULL, *b_buf = NULL;
435 size_t len_1, len_2;
436 unsigned char char_zero = 0;
437
438 if (!group || !curve || !curve->a || !curve->b)
439 return 0;
440
441 if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL) {
442 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
443 goto err;
444 }
445
446 nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
447
448 /* get a and b */
449 if (nid == NID_X9_62_prime_field) {
450 if (!EC_GROUP_get_curve_GFp(group, NULL, tmp_1, tmp_2, NULL)) {
451 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
452 goto err;
453 }
454 }
b3310161 455#ifndef OPENSSL_NO_EC2M
0f113f3e
MC
456 else { /* nid == NID_X9_62_characteristic_two_field */
457
458 if (!EC_GROUP_get_curve_GF2m(group, NULL, tmp_1, tmp_2, NULL)) {
459 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
460 goto err;
461 }
462 }
b3310161 463#endif
0f113f3e
MC
464 len_1 = (size_t)BN_num_bytes(tmp_1);
465 len_2 = (size_t)BN_num_bytes(tmp_2);
466
467 if (len_1 == 0) {
468 /* len_1 == 0 => a == 0 */
469 a_buf = &char_zero;
470 len_1 = 1;
471 } else {
472 if ((buffer_1 = OPENSSL_malloc(len_1)) == NULL) {
473 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
474 goto err;
475 }
476 if ((len_1 = BN_bn2bin(tmp_1, buffer_1)) == 0) {
477 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
478 goto err;
479 }
480 a_buf = buffer_1;
481 }
482
483 if (len_2 == 0) {
484 /* len_2 == 0 => b == 0 */
485 b_buf = &char_zero;
486 len_2 = 1;
487 } else {
488 if ((buffer_2 = OPENSSL_malloc(len_2)) == NULL) {
489 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
490 goto err;
491 }
492 if ((len_2 = BN_bn2bin(tmp_2, buffer_2)) == 0) {
493 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
494 goto err;
495 }
496 b_buf = buffer_2;
497 }
498
499 /* set a and b */
f422a514
DSH
500 if (!ASN1_OCTET_STRING_set(curve->a, a_buf, len_1) ||
501 !ASN1_OCTET_STRING_set(curve->b, b_buf, len_2)) {
0f113f3e
MC
502 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
503 goto err;
504 }
505
506 /* set the seed (optional) */
507 if (group->seed) {
508 if (!curve->seed)
509 if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) {
510 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
511 goto err;
512 }
513 curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
514 curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT;
515 if (!ASN1_BIT_STRING_set(curve->seed, group->seed,
516 (int)group->seed_len)) {
517 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
518 goto err;
519 }
520 } else {
2ace7450
RS
521 ASN1_BIT_STRING_free(curve->seed);
522 curve->seed = NULL;
0f113f3e
MC
523 }
524
525 ok = 1;
526
23a1d5e9 527 err:
b548a1f1
RS
528 OPENSSL_free(buffer_1);
529 OPENSSL_free(buffer_2);
23a1d5e9
RS
530 BN_free(tmp_1);
531 BN_free(tmp_2);
0f113f3e
MC
532 return (ok);
533}
012c86ab
BM
534
535static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *group,
536 ECPARAMETERS *param)
0f113f3e 537{
0f113f3e
MC
538 size_t len = 0;
539 ECPARAMETERS *ret = NULL;
540 BIGNUM *tmp = NULL;
541 unsigned char *buffer = NULL;
542 const EC_POINT *point = NULL;
543 point_conversion_form_t form;
544
545 if ((tmp = BN_new()) == NULL) {
546 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
547 goto err;
548 }
549
550 if (param == NULL) {
551 if ((ret = ECPARAMETERS_new()) == NULL) {
552 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
553 goto err;
554 }
555 } else
556 ret = param;
557
558 /* set the version (always one) */
559 ret->version = (long)0x1;
560
561 /* set the fieldID */
562 if (!ec_asn1_group2fieldid(group, ret->fieldID)) {
563 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
564 goto err;
565 }
566
567 /* set the curve */
568 if (!ec_asn1_group2curve(group, ret->curve)) {
569 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
570 goto err;
571 }
572
573 /* set the base point */
574 if ((point = EC_GROUP_get0_generator(group)) == NULL) {
575 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, EC_R_UNDEFINED_GENERATOR);
576 goto err;
577 }
578
579 form = EC_GROUP_get_point_conversion_form(group);
580
581 len = EC_POINT_point2oct(group, point, form, NULL, len, NULL);
582 if (len == 0) {
583 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
584 goto err;
585 }
586 if ((buffer = OPENSSL_malloc(len)) == NULL) {
587 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
588 goto err;
589 }
590 if (!EC_POINT_point2oct(group, point, form, buffer, len, NULL)) {
591 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
592 goto err;
593 }
594 if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) {
595 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
596 goto err;
597 }
598 if (!ASN1_OCTET_STRING_set(ret->base, buffer, len)) {
599 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
600 goto err;
601 }
602
603 /* set the order */
604 if (!EC_GROUP_get_order(group, tmp, NULL)) {
605 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
606 goto err;
607 }
608 ret->order = BN_to_ASN1_INTEGER(tmp, ret->order);
609 if (ret->order == NULL) {
610 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
611 goto err;
612 }
613
614 /* set the cofactor (optional) */
615 if (EC_GROUP_get_cofactor(group, tmp, NULL)) {
616 ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor);
617 if (ret->cofactor == NULL) {
618 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
619 goto err;
620 }
621 }
622
23a1d5e9 623 return ret;
0f113f3e 624
23a1d5e9
RS
625 err:
626 if (!param)
627 ECPARAMETERS_free(ret);
628 BN_free(tmp);
b548a1f1 629 OPENSSL_free(buffer);
23a1d5e9 630 return NULL;
0f113f3e
MC
631}
632
633ECPKPARAMETERS *ec_asn1_group2pkparameters(const EC_GROUP *group,
012c86ab 634 ECPKPARAMETERS *params)
0f113f3e
MC
635{
636 int ok = 1, tmp;
637 ECPKPARAMETERS *ret = params;
638
639 if (ret == NULL) {
640 if ((ret = ECPKPARAMETERS_new()) == NULL) {
641 ECerr(EC_F_EC_ASN1_GROUP2PKPARAMETERS, ERR_R_MALLOC_FAILURE);
642 return NULL;
643 }
644 } else {
0dfb9398 645 if (ret->type == 0)
0f113f3e
MC
646 ASN1_OBJECT_free(ret->value.named_curve);
647 else if (ret->type == 1 && ret->value.parameters)
648 ECPARAMETERS_free(ret->value.parameters);
649 }
650
651 if (EC_GROUP_get_asn1_flag(group)) {
652 /*
653 * use the asn1 OID to describe the the elliptic curve parameters
654 */
655 tmp = EC_GROUP_get_curve_name(group);
656 if (tmp) {
657 ret->type = 0;
658 if ((ret->value.named_curve = OBJ_nid2obj(tmp)) == NULL)
659 ok = 0;
660 } else
661 /* we don't kmow the nid => ERROR */
662 ok = 0;
663 } else {
664 /* use the ECPARAMETERS structure */
665 ret->type = 1;
666 if ((ret->value.parameters =
667 ec_asn1_group2parameters(group, NULL)) == NULL)
668 ok = 0;
669 }
670
671 if (!ok) {
672 ECPKPARAMETERS_free(ret);
673 return NULL;
674 }
675 return ret;
676}
012c86ab
BM
677
678static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *params)
0f113f3e
MC
679{
680 int ok = 0, tmp;
681 EC_GROUP *ret = NULL;
682 BIGNUM *p = NULL, *a = NULL, *b = NULL;
683 EC_POINT *point = NULL;
684 long field_bits;
685
686 if (!params->fieldID || !params->fieldID->fieldType ||
687 !params->fieldID->p.ptr) {
688 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
689 goto err;
690 }
691
692 /* now extract the curve parameters a and b */
693 if (!params->curve || !params->curve->a ||
694 !params->curve->a->data || !params->curve->b ||
695 !params->curve->b->data) {
696 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
697 goto err;
698 }
699 a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);
700 if (a == NULL) {
701 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
702 goto err;
703 }
704 b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
705 if (b == NULL) {
706 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
707 goto err;
708 }
709
710 /* get the field parameters */
711 tmp = OBJ_obj2nid(params->fieldID->fieldType);
712 if (tmp == NID_X9_62_characteristic_two_field)
b3310161 713#ifdef OPENSSL_NO_EC2M
0f113f3e
MC
714 {
715 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_GF2M_NOT_SUPPORTED);
716 goto err;
717 }
b3310161 718#else
0f113f3e
MC
719 {
720 X9_62_CHARACTERISTIC_TWO *char_two;
721
722 char_two = params->fieldID->p.char_two;
723
724 field_bits = char_two->m;
725 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
726 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE);
727 goto err;
728 }
729
730 if ((p = BN_new()) == NULL) {
731 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE);
732 goto err;
733 }
734
735 /* get the base type */
736 tmp = OBJ_obj2nid(char_two->type);
737
738 if (tmp == NID_X9_62_tpBasis) {
739 long tmp_long;
740
741 if (!char_two->p.tpBasis) {
742 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
743 goto err;
744 }
745
746 tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis);
747
748 if (!(char_two->m > tmp_long && tmp_long > 0)) {
749 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP,
750 EC_R_INVALID_TRINOMIAL_BASIS);
751 goto err;
752 }
753
754 /* create the polynomial */
755 if (!BN_set_bit(p, (int)char_two->m))
756 goto err;
757 if (!BN_set_bit(p, (int)tmp_long))
758 goto err;
759 if (!BN_set_bit(p, 0))
760 goto err;
761 } else if (tmp == NID_X9_62_ppBasis) {
762 X9_62_PENTANOMIAL *penta;
763
764 penta = char_two->p.ppBasis;
765 if (!penta) {
766 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
767 goto err;
768 }
769
770 if (!
771 (char_two->m > penta->k3 && penta->k3 > penta->k2
772 && penta->k2 > penta->k1 && penta->k1 > 0)) {
773 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP,
774 EC_R_INVALID_PENTANOMIAL_BASIS);
775 goto err;
776 }
777
778 /* create the polynomial */
779 if (!BN_set_bit(p, (int)char_two->m))
780 goto err;
781 if (!BN_set_bit(p, (int)penta->k1))
782 goto err;
783 if (!BN_set_bit(p, (int)penta->k2))
784 goto err;
785 if (!BN_set_bit(p, (int)penta->k3))
786 goto err;
787 if (!BN_set_bit(p, 0))
788 goto err;
789 } else if (tmp == NID_X9_62_onBasis) {
790 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_NOT_IMPLEMENTED);
791 goto err;
792 } else { /* error */
793
794 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
795 goto err;
796 }
797
798 /* create the EC_GROUP structure */
799 ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
800 }
b3310161 801#endif
0f113f3e
MC
802 else if (tmp == NID_X9_62_prime_field) {
803 /* we have a curve over a prime field */
804 /* extract the prime number */
805 if (!params->fieldID->p.prime) {
806 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
807 goto err;
808 }
809 p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);
810 if (p == NULL) {
811 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
812 goto err;
813 }
814
815 if (BN_is_negative(p) || BN_is_zero(p)) {
816 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD);
817 goto err;
818 }
819
820 field_bits = BN_num_bits(p);
821 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
822 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE);
823 goto err;
824 }
825
826 /* create the EC_GROUP structure */
827 ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);
828 } else {
829 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD);
830 goto err;
831 }
832
833 if (ret == NULL) {
834 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
835 goto err;
836 }
837
838 /* extract seed (optional) */
839 if (params->curve->seed != NULL) {
b548a1f1 840 OPENSSL_free(ret->seed);
75ebbd9a 841 if ((ret->seed = OPENSSL_malloc(params->curve->seed->length)) == NULL) {
0f113f3e
MC
842 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE);
843 goto err;
844 }
845 memcpy(ret->seed, params->curve->seed->data,
846 params->curve->seed->length);
847 ret->seed_len = params->curve->seed->length;
848 }
849
850 if (!params->order || !params->base || !params->base->data) {
851 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
852 goto err;
853 }
854
855 if ((point = EC_POINT_new(ret)) == NULL)
856 goto err;
857
858 /* set the point conversion form */
859 EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)
860 (params->base->data[0] & ~0x01));
861
862 /* extract the ec point */
863 if (!EC_POINT_oct2point(ret, point, params->base->data,
864 params->base->length, NULL)) {
865 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
866 goto err;
867 }
868
869 /* extract the order */
870 if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {
871 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
872 goto err;
873 }
874 if (BN_is_negative(a) || BN_is_zero(a)) {
875 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER);
876 goto err;
877 }
878 if (BN_num_bits(a) > (int)field_bits + 1) { /* Hasse bound */
879 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER);
880 goto err;
881 }
882
883 /* extract the cofactor (optional) */
884 if (params->cofactor == NULL) {
23a1d5e9
RS
885 BN_free(b);
886 b = NULL;
0f113f3e
MC
887 } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) {
888 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
889 goto err;
890 }
891 /* set the generator, order and cofactor (if present) */
892 if (!EC_GROUP_set_generator(ret, point, a, b)) {
893 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
894 goto err;
895 }
896
897 ok = 1;
898
8fdc3734
RS
899 err:
900 if (!ok) {
901 EC_GROUP_clear_free(ret);
0f113f3e
MC
902 ret = NULL;
903 }
904
23a1d5e9
RS
905 BN_free(p);
906 BN_free(a);
907 BN_free(b);
8fdc3734 908 EC_POINT_free(point);
0f113f3e 909 return (ret);
012c86ab
BM
910}
911
7e31164a 912EC_GROUP *ec_asn1_pkparameters2group(const ECPKPARAMETERS *params)
0f113f3e
MC
913{
914 EC_GROUP *ret = NULL;
915 int tmp = 0;
916
917 if (params == NULL) {
918 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_MISSING_PARAMETERS);
919 return NULL;
920 }
921
922 if (params->type == 0) { /* the curve is given by an OID */
923 tmp = OBJ_obj2nid(params->value.named_curve);
924 if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) {
925 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP,
926 EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
927 return NULL;
928 }
929 EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);
930 } else if (params->type == 1) { /* the parameters are given by a
931 * ECPARAMETERS structure */
932 ret = ec_asn1_parameters2group(params->value.parameters);
933 if (!ret) {
934 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, ERR_R_EC_LIB);
935 return NULL;
936 }
937 EC_GROUP_set_asn1_flag(ret, 0x0);
938 } else if (params->type == 2) { /* implicitlyCA */
939 return NULL;
940 } else {
941 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_ASN1_ERROR);
942 return NULL;
943 }
944
945 return ret;
946}
012c86ab 947
14a7cfb3 948/* EC_GROUP <-> DER encoding of ECPKPARAMETERS */
012c86ab 949
6343829a 950EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len)
0f113f3e
MC
951{
952 EC_GROUP *group = NULL;
953 ECPKPARAMETERS *params = NULL;
a46c9789 954 const unsigned char *p = *in;
0f113f3e 955
a46c9789 956 if ((params = d2i_ECPKPARAMETERS(NULL, &p, len)) == NULL) {
0f113f3e
MC
957 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE);
958 ECPKPARAMETERS_free(params);
959 return NULL;
960 }
961
962 if ((group = ec_asn1_pkparameters2group(params)) == NULL) {
963 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE);
964 ECPKPARAMETERS_free(params);
965 return NULL;
966 }
967
8fdc3734 968 if (a) {
0f113f3e 969 EC_GROUP_clear_free(*a);
0f113f3e 970 *a = group;
8fdc3734 971 }
0f113f3e
MC
972
973 ECPKPARAMETERS_free(params);
a46c9789 974 *in = p;
0f113f3e
MC
975 return (group);
976}
012c86ab 977
14a7cfb3 978int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out)
0f113f3e
MC
979{
980 int ret = 0;
981 ECPKPARAMETERS *tmp = ec_asn1_group2pkparameters(a, NULL);
982 if (tmp == NULL) {
983 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE);
984 return 0;
985 }
986 if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) {
987 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE);
988 ECPKPARAMETERS_free(tmp);
989 return 0;
990 }
991 ECPKPARAMETERS_free(tmp);
992 return (ret);
993}
012c86ab 994
14a7cfb3
BM
995/* some EC_KEY functions */
996
6343829a 997EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len)
0f113f3e 998{
0f113f3e
MC
999 EC_KEY *ret = NULL;
1000 EC_PRIVATEKEY *priv_key = NULL;
a46c9789 1001 const unsigned char *p = *in;
0f113f3e 1002
a46c9789 1003 if ((priv_key = d2i_EC_PRIVATEKEY(NULL, &p, len)) == NULL) {
0f113f3e 1004 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
0f113f3e
MC
1005 return NULL;
1006 }
1007
1008 if (a == NULL || *a == NULL) {
1009 if ((ret = EC_KEY_new()) == NULL) {
1010 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1011 goto err;
1012 }
0f113f3e
MC
1013 } else
1014 ret = *a;
1015
1016 if (priv_key->parameters) {
8fdc3734 1017 EC_GROUP_clear_free(ret->group);
0f113f3e
MC
1018 ret->group = ec_asn1_pkparameters2group(priv_key->parameters);
1019 }
1020
1021 if (ret->group == NULL) {
1022 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1023 goto err;
1024 }
1025
1026 ret->version = priv_key->version;
1027
1028 if (priv_key->privateKey) {
74924dcb
RS
1029 if (ret->priv_key == NULL)
1030 ret->priv_key = BN_secure_new();
90945fa3
MC
1031 if (ret->priv_key == NULL) {
1032 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1033 goto err;
1034 }
f422a514
DSH
1035 ret->priv_key = BN_bin2bn(ASN1_STRING_data(priv_key->privateKey),
1036 ASN1_STRING_length(priv_key->privateKey),
0f113f3e
MC
1037 ret->priv_key);
1038 if (ret->priv_key == NULL) {
1039 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_BN_LIB);
1040 goto err;
1041 }
1042 } else {
1043 ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_MISSING_PRIVATE_KEY);
1044 goto err;
1045 }
1046
8fdc3734 1047 EC_POINT_clear_free(ret->pub_key);
0f113f3e
MC
1048 ret->pub_key = EC_POINT_new(ret->group);
1049 if (ret->pub_key == NULL) {
1050 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1051 goto err;
1052 }
1053
1054 if (priv_key->publicKey) {
1055 const unsigned char *pub_oct;
1056 int pub_oct_len;
1057
f422a514
DSH
1058 pub_oct = ASN1_STRING_data(priv_key->publicKey);
1059 pub_oct_len = ASN1_STRING_length(priv_key->publicKey);
0f113f3e
MC
1060 /*
1061 * The first byte - point conversion form - must be present.
1062 */
1063 if (pub_oct_len <= 0) {
1064 ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_BUFFER_TOO_SMALL);
1065 goto err;
1066 }
1067 /* Save the point conversion form. */
1068 ret->conv_form = (point_conversion_form_t) (pub_oct[0] & ~0x01);
1069 if (!EC_POINT_oct2point(ret->group, ret->pub_key,
1070 pub_oct, (size_t)(pub_oct_len), NULL)) {
1071 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1072 goto err;
1073 }
1074 } else {
1075 if (!EC_POINT_mul
1076 (ret->group, ret->pub_key, ret->priv_key, NULL, NULL, NULL)) {
1077 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1078 goto err;
1079 }
1080 /* Remember the original private-key-only encoding. */
1081 ret->enc_flag |= EC_PKEY_NO_PUBKEY;
1082 }
1083
9e442d48
MC
1084 if (a)
1085 *a = ret;
25aaa98a 1086 EC_PRIVATEKEY_free(priv_key);
a46c9789 1087 *in = p;
0f113f3e 1088 return (ret);
25aaa98a
RS
1089
1090 err:
1091 if (a == NULL || *a != ret)
1092 EC_KEY_free(ret);
1093 EC_PRIVATEKEY_free(priv_key);
1094 return NULL;
0f113f3e
MC
1095}
1096
1097int i2d_ECPrivateKey(EC_KEY *a, unsigned char **out)
1098{
1099 int ret = 0, ok = 0;
1100 unsigned char *buffer = NULL;
30cd4ff2 1101 size_t buf_len = 0, tmp_len, bn_len;
0f113f3e
MC
1102 EC_PRIVATEKEY *priv_key = NULL;
1103
1104 if (a == NULL || a->group == NULL || a->priv_key == NULL ||
1105 (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key == NULL)) {
1106 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
1107 goto err;
1108 }
1109
1110 if ((priv_key = EC_PRIVATEKEY_new()) == NULL) {
1111 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1112 goto err;
1113 }
1114
1115 priv_key->version = a->version;
1116
30cd4ff2
DE
1117 bn_len = (size_t)BN_num_bytes(a->priv_key);
1118
1119 /* Octetstring may need leading zeros if BN is to short */
1120
1121 buf_len = (EC_GROUP_get_degree(a->group) + 7) / 8;
1122
1123 if (bn_len > buf_len) {
1124 ECerr(EC_F_I2D_ECPRIVATEKEY, EC_R_BUFFER_TOO_SMALL);
1125 goto err;
1126 }
1127
0f113f3e
MC
1128 buffer = OPENSSL_malloc(buf_len);
1129 if (buffer == NULL) {
1130 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1131 goto err;
1132 }
1133
30cd4ff2 1134 if (!BN_bn2bin(a->priv_key, buffer + buf_len - bn_len)) {
0f113f3e
MC
1135 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_BN_LIB);
1136 goto err;
1137 }
1138
30cd4ff2
DE
1139 if (buf_len - bn_len > 0) {
1140 memset(buffer, 0, buf_len - bn_len);
1141 }
1142
f422a514 1143 if (!ASN1_OCTET_STRING_set(priv_key->privateKey, buffer, buf_len)) {
0f113f3e
MC
1144 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB);
1145 goto err;
1146 }
1147
1148 if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) {
1149 if ((priv_key->parameters =
1150 ec_asn1_group2pkparameters(a->group,
1151 priv_key->parameters)) == NULL) {
1152 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1153 goto err;
1154 }
1155 }
1156
1157 if (!(a->enc_flag & EC_PKEY_NO_PUBKEY)) {
f422a514 1158 priv_key->publicKey = ASN1_BIT_STRING_new();
0f113f3e
MC
1159 if (priv_key->publicKey == NULL) {
1160 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1161 goto err;
1162 }
1163
1164 tmp_len = EC_POINT_point2oct(a->group, a->pub_key,
1165 a->conv_form, NULL, 0, NULL);
1166
1167 if (tmp_len > buf_len) {
1168 unsigned char *tmp_buffer = OPENSSL_realloc(buffer, tmp_len);
1169 if (!tmp_buffer) {
1170 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1171 goto err;
1172 }
1173 buffer = tmp_buffer;
1174 buf_len = tmp_len;
1175 }
1176
1177 if (!EC_POINT_point2oct(a->group, a->pub_key,
1178 a->conv_form, buffer, buf_len, NULL)) {
1179 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1180 goto err;
1181 }
1182
1183 priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
1184 priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
f422a514 1185 if (!ASN1_BIT_STRING_set(priv_key->publicKey, buffer, buf_len)) {
0f113f3e
MC
1186 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB);
1187 goto err;
1188 }
1189 }
1190
1191 if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) {
1192 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1193 goto err;
1194 }
1195 ok = 1;
1196 err:
b548a1f1 1197 OPENSSL_free(buffer);
25aaa98a 1198 EC_PRIVATEKEY_free(priv_key);
0f113f3e
MC
1199 return (ok ? ret : 0);
1200}
14a7cfb3
BM
1201
1202int i2d_ECParameters(EC_KEY *a, unsigned char **out)
0f113f3e
MC
1203{
1204 if (a == NULL) {
1205 ECerr(EC_F_I2D_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1206 return 0;
1207 }
1208 return i2d_ECPKParameters(a->group, out);
1209}
14a7cfb3
BM
1210
1211EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len)
0f113f3e
MC
1212{
1213 EC_KEY *ret;
1214
1215 if (in == NULL || *in == NULL) {
1216 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1217 return NULL;
1218 }
1219
1220 if (a == NULL || *a == NULL) {
1221 if ((ret = EC_KEY_new()) == NULL) {
1222 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
1223 return NULL;
1224 }
0f113f3e
MC
1225 } else
1226 ret = *a;
1227
1228 if (!d2i_ECPKParameters(&ret->group, in, len)) {
1229 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB);
5e5d53d3
MC
1230 if (a == NULL || *a != ret)
1231 EC_KEY_free(ret);
0f113f3e
MC
1232 return NULL;
1233 }
1234
5e5d53d3
MC
1235 if (a)
1236 *a = ret;
1237
0f113f3e
MC
1238 return ret;
1239}
14a7cfb3 1240
62e3163b 1241EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len)
0f113f3e
MC
1242{
1243 EC_KEY *ret = NULL;
1244
1245 if (a == NULL || (*a) == NULL || (*a)->group == NULL) {
1246 /*
1247 * sorry, but a EC_GROUP-structur is necessary to set the public key
1248 */
1249 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1250 return 0;
1251 }
1252 ret = *a;
1253 if (ret->pub_key == NULL &&
1254 (ret->pub_key = EC_POINT_new(ret->group)) == NULL) {
1255 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
1256 return 0;
1257 }
1258 if (!EC_POINT_oct2point(ret->group, ret->pub_key, *in, len, NULL)) {
1259 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_EC_LIB);
1260 return 0;
1261 }
1262 /* save the point conversion form */
1263 ret->conv_form = (point_conversion_form_t) (*in[0] & ~0x01);
1264 *in += len;
1265 return ret;
1266}
14a7cfb3 1267
62e3163b 1268int i2o_ECPublicKey(EC_KEY *a, unsigned char **out)
0f113f3e
MC
1269{
1270 size_t buf_len = 0;
1271 int new_buffer = 0;
1272
1273 if (a == NULL) {
1274 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1275 return 0;
1276 }
1277
1278 buf_len = EC_POINT_point2oct(a->group, a->pub_key,
1279 a->conv_form, NULL, 0, NULL);
1280
1281 if (out == NULL || buf_len == 0)
1282 /* out == NULL => just return the length of the octet string */
1283 return buf_len;
1284
1285 if (*out == NULL) {
1286 if ((*out = OPENSSL_malloc(buf_len)) == NULL) {
1287 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
1288 return 0;
1289 }
1290 new_buffer = 1;
1291 }
1292 if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form,
1293 *out, buf_len, NULL)) {
1294 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_EC_LIB);
1295 if (new_buffer) {
1296 OPENSSL_free(*out);
1297 *out = NULL;
1298 }
1299 return 0;
1300 }
1301 if (!new_buffer)
1302 *out += buf_len;
1303 return buf_len;
1304}