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