]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ec/ec_asn1.c
Add Explicit EC parameter support to providers.
[thirdparty/openssl.git] / crypto / ec / ec_asn1.c
1 /*
2 * Copyright 2002-2020 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 /*
11 * ECDSA low level APIs are deprecated for public use, but still ok for
12 * internal use.
13 */
14 #include "internal/deprecated.h"
15
16 #include <string.h>
17 #include "ec_local.h"
18 #include <openssl/err.h>
19 #include <openssl/asn1t.h>
20 #include <openssl/objects.h>
21 #include "internal/nelem.h"
22 #include "crypto/asn1_dsa.h"
23
24 #ifndef FIPS_MODULE
25
26 /* some structures needed for the asn1 encoding */
27 typedef struct x9_62_pentanomial_st {
28 int32_t k1;
29 int32_t k2;
30 int32_t k3;
31 } X9_62_PENTANOMIAL;
32
33 typedef struct x9_62_characteristic_two_st {
34 int32_t m;
35 ASN1_OBJECT *type;
36 union {
37 char *ptr;
38 /* NID_X9_62_onBasis */
39 ASN1_NULL *onBasis;
40 /* NID_X9_62_tpBasis */
41 ASN1_INTEGER *tpBasis;
42 /* NID_X9_62_ppBasis */
43 X9_62_PENTANOMIAL *ppBasis;
44 /* anything else */
45 ASN1_TYPE *other;
46 } p;
47 } X9_62_CHARACTERISTIC_TWO;
48
49 typedef struct x9_62_fieldid_st {
50 ASN1_OBJECT *fieldType;
51 union {
52 char *ptr;
53 /* NID_X9_62_prime_field */
54 ASN1_INTEGER *prime;
55 /* NID_X9_62_characteristic_two_field */
56 X9_62_CHARACTERISTIC_TWO *char_two;
57 /* anything else */
58 ASN1_TYPE *other;
59 } p;
60 } X9_62_FIELDID;
61
62 typedef struct x9_62_curve_st {
63 ASN1_OCTET_STRING *a;
64 ASN1_OCTET_STRING *b;
65 ASN1_BIT_STRING *seed;
66 } X9_62_CURVE;
67
68 struct ec_parameters_st {
69 int32_t version;
70 X9_62_FIELDID *fieldID;
71 X9_62_CURVE *curve;
72 ASN1_OCTET_STRING *base;
73 ASN1_INTEGER *order;
74 ASN1_INTEGER *cofactor;
75 } /* ECPARAMETERS */ ;
76
77 struct ecpk_parameters_st {
78 int type;
79 union {
80 ASN1_OBJECT *named_curve;
81 ECPARAMETERS *parameters;
82 ASN1_NULL *implicitlyCA;
83 } value;
84 } /* ECPKPARAMETERS */ ;
85
86 /* SEC1 ECPrivateKey */
87 typedef struct ec_privatekey_st {
88 int32_t version;
89 ASN1_OCTET_STRING *privateKey;
90 ECPKPARAMETERS *parameters;
91 ASN1_BIT_STRING *publicKey;
92 } EC_PRIVATEKEY;
93
94 /* the OpenSSL ASN.1 definitions */
95 ASN1_SEQUENCE(X9_62_PENTANOMIAL) = {
96 ASN1_EMBED(X9_62_PENTANOMIAL, k1, INT32),
97 ASN1_EMBED(X9_62_PENTANOMIAL, k2, INT32),
98 ASN1_EMBED(X9_62_PENTANOMIAL, k3, INT32)
99 } static_ASN1_SEQUENCE_END(X9_62_PENTANOMIAL)
100
101 DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
102 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
103
104 ASN1_ADB_TEMPLATE(char_two_def) = ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.other, ASN1_ANY);
105
106 ASN1_ADB(X9_62_CHARACTERISTIC_TWO) = {
107 ADB_ENTRY(NID_X9_62_onBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.onBasis, ASN1_NULL)),
108 ADB_ENTRY(NID_X9_62_tpBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.tpBasis, ASN1_INTEGER)),
109 ADB_ENTRY(NID_X9_62_ppBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.ppBasis, X9_62_PENTANOMIAL))
110 } ASN1_ADB_END(X9_62_CHARACTERISTIC_TWO, 0, type, 0, &char_two_def_tt, NULL);
111
112 ASN1_SEQUENCE(X9_62_CHARACTERISTIC_TWO) = {
113 ASN1_EMBED(X9_62_CHARACTERISTIC_TWO, m, INT32),
114 ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, type, ASN1_OBJECT),
115 ASN1_ADB_OBJECT(X9_62_CHARACTERISTIC_TWO)
116 } static_ASN1_SEQUENCE_END(X9_62_CHARACTERISTIC_TWO)
117
118 DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
119 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
120
121 ASN1_ADB_TEMPLATE(fieldID_def) = ASN1_SIMPLE(X9_62_FIELDID, p.other, ASN1_ANY);
122
123 ASN1_ADB(X9_62_FIELDID) = {
124 ADB_ENTRY(NID_X9_62_prime_field, ASN1_SIMPLE(X9_62_FIELDID, p.prime, ASN1_INTEGER)),
125 ADB_ENTRY(NID_X9_62_characteristic_two_field, ASN1_SIMPLE(X9_62_FIELDID, p.char_two, X9_62_CHARACTERISTIC_TWO))
126 } ASN1_ADB_END(X9_62_FIELDID, 0, fieldType, 0, &fieldID_def_tt, NULL);
127
128 ASN1_SEQUENCE(X9_62_FIELDID) = {
129 ASN1_SIMPLE(X9_62_FIELDID, fieldType, ASN1_OBJECT),
130 ASN1_ADB_OBJECT(X9_62_FIELDID)
131 } static_ASN1_SEQUENCE_END(X9_62_FIELDID)
132
133 ASN1_SEQUENCE(X9_62_CURVE) = {
134 ASN1_SIMPLE(X9_62_CURVE, a, ASN1_OCTET_STRING),
135 ASN1_SIMPLE(X9_62_CURVE, b, ASN1_OCTET_STRING),
136 ASN1_OPT(X9_62_CURVE, seed, ASN1_BIT_STRING)
137 } static_ASN1_SEQUENCE_END(X9_62_CURVE)
138
139 ASN1_SEQUENCE(ECPARAMETERS) = {
140 ASN1_EMBED(ECPARAMETERS, version, INT32),
141 ASN1_SIMPLE(ECPARAMETERS, fieldID, X9_62_FIELDID),
142 ASN1_SIMPLE(ECPARAMETERS, curve, X9_62_CURVE),
143 ASN1_SIMPLE(ECPARAMETERS, base, ASN1_OCTET_STRING),
144 ASN1_SIMPLE(ECPARAMETERS, order, ASN1_INTEGER),
145 ASN1_OPT(ECPARAMETERS, cofactor, ASN1_INTEGER)
146 } ASN1_SEQUENCE_END(ECPARAMETERS)
147
148 DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
149 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
150
151 ASN1_CHOICE(ECPKPARAMETERS) = {
152 ASN1_SIMPLE(ECPKPARAMETERS, value.named_curve, ASN1_OBJECT),
153 ASN1_SIMPLE(ECPKPARAMETERS, value.parameters, ECPARAMETERS),
154 ASN1_SIMPLE(ECPKPARAMETERS, value.implicitlyCA, ASN1_NULL)
155 } ASN1_CHOICE_END(ECPKPARAMETERS)
156
157 DECLARE_ASN1_FUNCTIONS(ECPKPARAMETERS)
158 DECLARE_ASN1_ENCODE_FUNCTIONS_name(ECPKPARAMETERS, ECPKPARAMETERS)
159 IMPLEMENT_ASN1_FUNCTIONS(ECPKPARAMETERS)
160
161 ASN1_SEQUENCE(EC_PRIVATEKEY) = {
162 ASN1_EMBED(EC_PRIVATEKEY, version, INT32),
163 ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING),
164 ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0),
165 ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1)
166 } static_ASN1_SEQUENCE_END(EC_PRIVATEKEY)
167
168 DECLARE_ASN1_FUNCTIONS(EC_PRIVATEKEY)
169 DECLARE_ASN1_ENCODE_FUNCTIONS_name(EC_PRIVATEKEY, EC_PRIVATEKEY)
170 IMPLEMENT_ASN1_FUNCTIONS(EC_PRIVATEKEY)
171
172 /* some declarations of internal function */
173
174 /* ec_asn1_group2field() sets the values in a X9_62_FIELDID object */
175 static int ec_asn1_group2fieldid(const EC_GROUP *, X9_62_FIELDID *);
176 /* ec_asn1_group2curve() sets the values in a X9_62_CURVE object */
177 static int ec_asn1_group2curve(const EC_GROUP *, X9_62_CURVE *);
178
179 /* the function definitions */
180
181 static int ec_asn1_group2fieldid(const EC_GROUP *group, X9_62_FIELDID *field)
182 {
183 int ok = 0, nid;
184 BIGNUM *tmp = NULL;
185
186 if (group == NULL || field == NULL)
187 return 0;
188
189 /* clear the old values (if necessary) */
190 ASN1_OBJECT_free(field->fieldType);
191 ASN1_TYPE_free(field->p.other);
192
193 nid = EC_GROUP_get_field_type(group);
194 /* set OID for the field */
195 if ((field->fieldType = OBJ_nid2obj(nid)) == NULL) {
196 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
197 goto err;
198 }
199
200 if (nid == NID_X9_62_prime_field) {
201 if ((tmp = BN_new()) == NULL) {
202 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
203 goto err;
204 }
205 /* the parameters are specified by the prime number p */
206 if (!EC_GROUP_get_curve(group, tmp, NULL, NULL, NULL)) {
207 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
208 goto err;
209 }
210 /* set the prime number */
211 field->p.prime = BN_to_ASN1_INTEGER(tmp, NULL);
212 if (field->p.prime == NULL) {
213 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
214 goto err;
215 }
216 } else if (nid == NID_X9_62_characteristic_two_field)
217 #ifdef OPENSSL_NO_EC2M
218 {
219 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_GF2M_NOT_SUPPORTED);
220 goto err;
221 }
222 #else
223 {
224 int field_type;
225 X9_62_CHARACTERISTIC_TWO *char_two;
226
227 field->p.char_two = X9_62_CHARACTERISTIC_TWO_new();
228 char_two = field->p.char_two;
229
230 if (char_two == NULL) {
231 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
232 goto err;
233 }
234
235 char_two->m = (long)EC_GROUP_get_degree(group);
236
237 field_type = EC_GROUP_get_basis_type(group);
238
239 if (field_type == 0) {
240 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
241 goto err;
242 }
243 /* set base type OID */
244 if ((char_two->type = OBJ_nid2obj(field_type)) == NULL) {
245 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
246 goto err;
247 }
248
249 if (field_type == NID_X9_62_tpBasis) {
250 unsigned int k;
251
252 if (!EC_GROUP_get_trinomial_basis(group, &k))
253 goto err;
254
255 char_two->p.tpBasis = ASN1_INTEGER_new();
256 if (char_two->p.tpBasis == NULL) {
257 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
258 goto err;
259 }
260 if (!ASN1_INTEGER_set(char_two->p.tpBasis, (long)k)) {
261 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
262 goto err;
263 }
264 } else if (field_type == NID_X9_62_ppBasis) {
265 unsigned int k1, k2, k3;
266
267 if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3))
268 goto err;
269
270 char_two->p.ppBasis = X9_62_PENTANOMIAL_new();
271 if (char_two->p.ppBasis == NULL) {
272 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
273 goto err;
274 }
275
276 /* set k? values */
277 char_two->p.ppBasis->k1 = (long)k1;
278 char_two->p.ppBasis->k2 = (long)k2;
279 char_two->p.ppBasis->k3 = (long)k3;
280 } else { /* field_type == NID_X9_62_onBasis */
281
282 /* for ONB the parameters are (asn1) NULL */
283 char_two->p.onBasis = ASN1_NULL_new();
284 if (char_two->p.onBasis == NULL) {
285 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
286 goto err;
287 }
288 }
289 }
290 #endif
291 else {
292 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_UNSUPPORTED_FIELD);
293 goto err;
294 }
295
296 ok = 1;
297
298 err:
299 BN_free(tmp);
300 return ok;
301 }
302
303 static int ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve)
304 {
305 int ok = 0;
306 BIGNUM *tmp_1 = NULL, *tmp_2 = NULL;
307 unsigned char *a_buf = NULL, *b_buf = NULL;
308 size_t len;
309
310 if (!group || !curve || !curve->a || !curve->b)
311 return 0;
312
313 if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL) {
314 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
315 goto err;
316 }
317
318 /* get a and b */
319 if (!EC_GROUP_get_curve(group, NULL, tmp_1, tmp_2, NULL)) {
320 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
321 goto err;
322 }
323
324 /*
325 * Per SEC 1, the curve coefficients must be padded up to size. See C.2's
326 * definition of Curve, C.1's definition of FieldElement, and 2.3.5's
327 * definition of how to encode the field elements.
328 */
329 len = ((size_t)EC_GROUP_get_degree(group) + 7) / 8;
330 if ((a_buf = OPENSSL_malloc(len)) == NULL
331 || (b_buf = OPENSSL_malloc(len)) == NULL) {
332 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
333 goto err;
334 }
335 if (BN_bn2binpad(tmp_1, a_buf, len) < 0
336 || BN_bn2binpad(tmp_2, b_buf, len) < 0) {
337 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
338 goto err;
339 }
340
341 /* set a and b */
342 if (!ASN1_OCTET_STRING_set(curve->a, a_buf, len)
343 || !ASN1_OCTET_STRING_set(curve->b, b_buf, len)) {
344 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
345 goto err;
346 }
347
348 /* set the seed (optional) */
349 if (group->seed) {
350 if (!curve->seed)
351 if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) {
352 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
353 goto err;
354 }
355 curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
356 curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT;
357 if (!ASN1_BIT_STRING_set(curve->seed, group->seed,
358 (int)group->seed_len)) {
359 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
360 goto err;
361 }
362 } else {
363 ASN1_BIT_STRING_free(curve->seed);
364 curve->seed = NULL;
365 }
366
367 ok = 1;
368
369 err:
370 OPENSSL_free(a_buf);
371 OPENSSL_free(b_buf);
372 BN_free(tmp_1);
373 BN_free(tmp_2);
374 return ok;
375 }
376
377 ECPARAMETERS *EC_GROUP_get_ecparameters(const EC_GROUP *group,
378 ECPARAMETERS *params)
379 {
380 size_t len = 0;
381 ECPARAMETERS *ret = NULL;
382 const BIGNUM *tmp;
383 unsigned char *buffer = NULL;
384 const EC_POINT *point = NULL;
385 point_conversion_form_t form;
386 ASN1_INTEGER *orig;
387
388 if (params == NULL) {
389 if ((ret = ECPARAMETERS_new()) == NULL) {
390 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
391 goto err;
392 }
393 } else
394 ret = params;
395
396 /* set the version (always one) */
397 ret->version = (long)0x1;
398
399 /* set the fieldID */
400 if (!ec_asn1_group2fieldid(group, ret->fieldID)) {
401 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
402 goto err;
403 }
404
405 /* set the curve */
406 if (!ec_asn1_group2curve(group, ret->curve)) {
407 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
408 goto err;
409 }
410
411 /* set the base point */
412 if ((point = EC_GROUP_get0_generator(group)) == NULL) {
413 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, EC_R_UNDEFINED_GENERATOR);
414 goto err;
415 }
416
417 form = EC_GROUP_get_point_conversion_form(group);
418
419 len = EC_POINT_point2buf(group, point, form, &buffer, NULL);
420 if (len == 0) {
421 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
422 goto err;
423 }
424 if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) {
425 OPENSSL_free(buffer);
426 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
427 goto err;
428 }
429 ASN1_STRING_set0(ret->base, buffer, len);
430
431 /* set the order */
432 tmp = EC_GROUP_get0_order(group);
433 if (tmp == NULL) {
434 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
435 goto err;
436 }
437 ret->order = BN_to_ASN1_INTEGER(tmp, orig = ret->order);
438 if (ret->order == NULL) {
439 ret->order = orig;
440 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_ASN1_LIB);
441 goto err;
442 }
443
444 /* set the cofactor (optional) */
445 tmp = EC_GROUP_get0_cofactor(group);
446 if (tmp != NULL) {
447 ret->cofactor = BN_to_ASN1_INTEGER(tmp, orig = ret->cofactor);
448 if (ret->cofactor == NULL) {
449 ret->cofactor = orig;
450 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_ASN1_LIB);
451 goto err;
452 }
453 }
454
455 return ret;
456
457 err:
458 if (params == NULL)
459 ECPARAMETERS_free(ret);
460 return NULL;
461 }
462
463 ECPKPARAMETERS *EC_GROUP_get_ecpkparameters(const EC_GROUP *group,
464 ECPKPARAMETERS *params)
465 {
466 int ok = 1, tmp;
467 ECPKPARAMETERS *ret = params;
468
469 if (ret == NULL) {
470 if ((ret = ECPKPARAMETERS_new()) == NULL) {
471 ECerr(EC_F_EC_GROUP_GET_ECPKPARAMETERS, ERR_R_MALLOC_FAILURE);
472 return NULL;
473 }
474 } else {
475 if (ret->type == 0)
476 ASN1_OBJECT_free(ret->value.named_curve);
477 else if (ret->type == 1 && ret->value.parameters)
478 ECPARAMETERS_free(ret->value.parameters);
479 }
480
481 if (EC_GROUP_get_asn1_flag(group)) {
482 /*
483 * use the asn1 OID to describe the elliptic curve parameters
484 */
485 tmp = EC_GROUP_get_curve_name(group);
486 if (tmp) {
487 ASN1_OBJECT *asn1obj = OBJ_nid2obj(tmp);
488
489 if (asn1obj == NULL || OBJ_length(asn1obj) == 0) {
490 ASN1_OBJECT_free(asn1obj);
491 ECerr(EC_F_EC_GROUP_GET_ECPKPARAMETERS, EC_R_MISSING_OID);
492 ok = 0;
493 } else {
494 ret->type = 0;
495 ret->value.named_curve = asn1obj;
496 }
497 } else
498 /* we don't know the nid => ERROR */
499 ok = 0;
500 } else {
501 /* use the ECPARAMETERS structure */
502 ret->type = 1;
503 if ((ret->value.parameters =
504 EC_GROUP_get_ecparameters(group, NULL)) == NULL)
505 ok = 0;
506 }
507
508 if (!ok) {
509 ECPKPARAMETERS_free(ret);
510 return NULL;
511 }
512 return ret;
513 }
514
515 EC_GROUP *EC_GROUP_new_from_ecparameters(const ECPARAMETERS *params)
516 {
517 int ok = 0, tmp;
518 EC_GROUP *ret = NULL, *dup = NULL;
519 BIGNUM *p = NULL, *a = NULL, *b = NULL;
520 EC_POINT *point = NULL;
521 long field_bits;
522 int curve_name = NID_undef;
523 BN_CTX *ctx = NULL;
524
525 if (params->fieldID == NULL
526 || params->fieldID->fieldType == NULL
527 || params->fieldID->p.ptr == NULL) {
528 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
529 goto err;
530 }
531
532 /*
533 * Now extract the curve parameters a and b. Note that, although SEC 1
534 * specifies the length of their encodings, historical versions of OpenSSL
535 * encoded them incorrectly, so we must accept any length for backwards
536 * compatibility.
537 */
538 if (params->curve == NULL
539 || params->curve->a == NULL || params->curve->a->data == NULL
540 || params->curve->b == NULL || params->curve->b->data == NULL) {
541 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
542 goto err;
543 }
544 a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);
545 if (a == NULL) {
546 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);
547 goto err;
548 }
549 b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
550 if (b == NULL) {
551 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);
552 goto err;
553 }
554
555 /* get the field parameters */
556 tmp = OBJ_obj2nid(params->fieldID->fieldType);
557 if (tmp == NID_X9_62_characteristic_two_field)
558 #ifdef OPENSSL_NO_EC2M
559 {
560 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_GF2M_NOT_SUPPORTED);
561 goto err;
562 }
563 #else
564 {
565 X9_62_CHARACTERISTIC_TWO *char_two;
566
567 char_two = params->fieldID->p.char_two;
568
569 field_bits = char_two->m;
570 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
571 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_FIELD_TOO_LARGE);
572 goto err;
573 }
574
575 if ((p = BN_new()) == NULL) {
576 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
577 goto err;
578 }
579
580 /* get the base type */
581 tmp = OBJ_obj2nid(char_two->type);
582
583 if (tmp == NID_X9_62_tpBasis) {
584 long tmp_long;
585
586 if (!char_two->p.tpBasis) {
587 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
588 goto err;
589 }
590
591 tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis);
592
593 if (!(char_two->m > tmp_long && tmp_long > 0)) {
594 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS,
595 EC_R_INVALID_TRINOMIAL_BASIS);
596 goto err;
597 }
598
599 /* create the polynomial */
600 if (!BN_set_bit(p, (int)char_two->m))
601 goto err;
602 if (!BN_set_bit(p, (int)tmp_long))
603 goto err;
604 if (!BN_set_bit(p, 0))
605 goto err;
606 } else if (tmp == NID_X9_62_ppBasis) {
607 X9_62_PENTANOMIAL *penta;
608
609 penta = char_two->p.ppBasis;
610 if (penta == NULL) {
611 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
612 goto err;
613 }
614
615 if (!
616 (char_two->m > penta->k3 && penta->k3 > penta->k2
617 && penta->k2 > penta->k1 && penta->k1 > 0)) {
618 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS,
619 EC_R_INVALID_PENTANOMIAL_BASIS);
620 goto err;
621 }
622
623 /* create the polynomial */
624 if (!BN_set_bit(p, (int)char_two->m))
625 goto err;
626 if (!BN_set_bit(p, (int)penta->k1))
627 goto err;
628 if (!BN_set_bit(p, (int)penta->k2))
629 goto err;
630 if (!BN_set_bit(p, (int)penta->k3))
631 goto err;
632 if (!BN_set_bit(p, 0))
633 goto err;
634 } else if (tmp == NID_X9_62_onBasis) {
635 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_NOT_IMPLEMENTED);
636 goto err;
637 } else { /* error */
638
639 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
640 goto err;
641 }
642
643 /* create the EC_GROUP structure */
644 ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
645 }
646 #endif
647 else if (tmp == NID_X9_62_prime_field) {
648 /* we have a curve over a prime field */
649 /* extract the prime number */
650 if (params->fieldID->p.prime == NULL) {
651 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
652 goto err;
653 }
654 p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);
655 if (p == NULL) {
656 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
657 goto err;
658 }
659
660 if (BN_is_negative(p) || BN_is_zero(p)) {
661 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_FIELD);
662 goto err;
663 }
664
665 field_bits = BN_num_bits(p);
666 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
667 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_FIELD_TOO_LARGE);
668 goto err;
669 }
670
671 /* create the EC_GROUP structure */
672 ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);
673 } else {
674 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_FIELD);
675 goto err;
676 }
677
678 if (ret == NULL) {
679 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
680 goto err;
681 }
682
683 /* extract seed (optional) */
684 if (params->curve->seed != NULL) {
685 OPENSSL_free(ret->seed);
686 if ((ret->seed = OPENSSL_malloc(params->curve->seed->length)) == NULL) {
687 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
688 goto err;
689 }
690 memcpy(ret->seed, params->curve->seed->data,
691 params->curve->seed->length);
692 ret->seed_len = params->curve->seed->length;
693 }
694
695 if (params->order == NULL
696 || params->base == NULL
697 || params->base->data == NULL) {
698 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
699 goto err;
700 }
701
702 if ((point = EC_POINT_new(ret)) == NULL)
703 goto err;
704
705 /* set the point conversion form */
706 EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)
707 (params->base->data[0] & ~0x01));
708
709 /* extract the ec point */
710 if (!EC_POINT_oct2point(ret, point, params->base->data,
711 params->base->length, NULL)) {
712 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
713 goto err;
714 }
715
716 /* extract the order */
717 if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {
718 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
719 goto err;
720 }
721 if (BN_is_negative(a) || BN_is_zero(a)) {
722 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_GROUP_ORDER);
723 goto err;
724 }
725 if (BN_num_bits(a) > (int)field_bits + 1) { /* Hasse bound */
726 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_GROUP_ORDER);
727 goto err;
728 }
729
730 /* extract the cofactor (optional) */
731 if (params->cofactor == NULL) {
732 BN_free(b);
733 b = NULL;
734 } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) {
735 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
736 goto err;
737 }
738 /* set the generator, order and cofactor (if present) */
739 if (!EC_GROUP_set_generator(ret, point, a, b)) {
740 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
741 goto err;
742 }
743
744 /*
745 * Check if the explicit parameters group just created matches one of the
746 * built-in curves.
747 *
748 * We create a copy of the group just built, so that we can remove optional
749 * fields for the lookup: we do this to avoid the possibility that one of
750 * the optional parameters is used to force the library into using a less
751 * performant and less secure EC_METHOD instead of the specialized one.
752 * In any case, `seed` is not really used in any computation, while a
753 * cofactor different from the one in the built-in table is just
754 * mathematically wrong anyway and should not be used.
755 */
756 if ((ctx = BN_CTX_new()) == NULL) {
757 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);
758 goto err;
759 }
760 if ((dup = EC_GROUP_dup(ret)) == NULL
761 || EC_GROUP_set_seed(dup, NULL, 0) != 1
762 || !EC_GROUP_set_generator(dup, point, a, NULL)) {
763 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
764 goto err;
765 }
766 if ((curve_name = ec_curve_nid_from_params(dup, ctx)) != NID_undef) {
767 /*
768 * The input explicit parameters successfully matched one of the
769 * built-in curves: often for built-in curves we have specialized
770 * methods with better performance and hardening.
771 *
772 * In this case we replace the `EC_GROUP` created through explicit
773 * parameters with one created from a named group.
774 */
775 EC_GROUP *named_group = NULL;
776
777 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
778 /*
779 * NID_wap_wsg_idm_ecid_wtls12 and NID_secp224r1 are both aliases for
780 * the same curve, we prefer the SECP nid when matching explicit
781 * parameters as that is associated with a specialized EC_METHOD.
782 */
783 if (curve_name == NID_wap_wsg_idm_ecid_wtls12)
784 curve_name = NID_secp224r1;
785 #endif /* !def(OPENSSL_NO_EC_NISTP_64_GCC_128) */
786
787 if ((named_group = EC_GROUP_new_by_curve_name(curve_name)) == NULL) {
788 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
789 goto err;
790 }
791 EC_GROUP_free(ret);
792 ret = named_group;
793
794 /*
795 * Set the flag so that EC_GROUPs created from explicit parameters are
796 * serialized using explicit parameters by default.
797 */
798 EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_EXPLICIT_CURVE);
799
800 /*
801 * If the input params do not contain the optional seed field we make
802 * sure it is not added to the returned group.
803 *
804 * The seed field is not really used inside libcrypto anyway, and
805 * adding it to parsed explicit parameter keys would alter their DER
806 * encoding output (because of the extra field) which could impact
807 * applications fingerprinting keys by their DER encoding.
808 */
809 if (params->curve->seed == NULL) {
810 if (EC_GROUP_set_seed(ret, NULL, 0) != 1)
811 goto err;
812 }
813 }
814
815 ok = 1;
816
817 err:
818 if (!ok) {
819 EC_GROUP_free(ret);
820 ret = NULL;
821 }
822 EC_GROUP_free(dup);
823
824 BN_free(p);
825 BN_free(a);
826 BN_free(b);
827 EC_POINT_free(point);
828
829 BN_CTX_free(ctx);
830
831 return ret;
832 }
833
834 EC_GROUP *EC_GROUP_new_from_ecpkparameters(const ECPKPARAMETERS *params)
835 {
836 EC_GROUP *ret = NULL;
837 int tmp = 0;
838
839 if (params == NULL) {
840 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, EC_R_MISSING_PARAMETERS);
841 return NULL;
842 }
843
844 if (params->type == 0) { /* the curve is given by an OID */
845 tmp = OBJ_obj2nid(params->value.named_curve);
846 if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) {
847 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS,
848 EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
849 return NULL;
850 }
851 EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);
852 } else if (params->type == 1) { /* the parameters are given by a
853 * ECPARAMETERS structure */
854 ret = EC_GROUP_new_from_ecparameters(params->value.parameters);
855 if (!ret) {
856 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, ERR_R_EC_LIB);
857 return NULL;
858 }
859 EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_EXPLICIT_CURVE);
860 } else if (params->type == 2) { /* implicitlyCA */
861 return NULL;
862 } else {
863 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, EC_R_ASN1_ERROR);
864 return NULL;
865 }
866
867 return ret;
868 }
869
870 /* EC_GROUP <-> DER encoding of ECPKPARAMETERS */
871
872 EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len)
873 {
874 EC_GROUP *group = NULL;
875 ECPKPARAMETERS *params = NULL;
876 const unsigned char *p = *in;
877
878 if ((params = d2i_ECPKPARAMETERS(NULL, &p, len)) == NULL) {
879 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE);
880 ECPKPARAMETERS_free(params);
881 return NULL;
882 }
883
884 if ((group = EC_GROUP_new_from_ecpkparameters(params)) == NULL) {
885 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE);
886 ECPKPARAMETERS_free(params);
887 return NULL;
888 }
889
890 if (a) {
891 EC_GROUP_free(*a);
892 *a = group;
893 }
894
895 ECPKPARAMETERS_free(params);
896 *in = p;
897 return group;
898 }
899
900 int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out)
901 {
902 int ret = 0;
903 ECPKPARAMETERS *tmp = EC_GROUP_get_ecpkparameters(a, NULL);
904 if (tmp == NULL) {
905 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE);
906 return 0;
907 }
908 if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) {
909 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE);
910 ECPKPARAMETERS_free(tmp);
911 return 0;
912 }
913 ECPKPARAMETERS_free(tmp);
914 return ret;
915 }
916
917 /* some EC_KEY functions */
918
919 EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len)
920 {
921 EC_KEY *ret = NULL;
922 EC_PRIVATEKEY *priv_key = NULL;
923 const unsigned char *p = *in;
924
925 if ((priv_key = d2i_EC_PRIVATEKEY(NULL, &p, len)) == NULL) {
926 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
927 return NULL;
928 }
929
930 if (a == NULL || *a == NULL) {
931 if ((ret = EC_KEY_new()) == NULL) {
932 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
933 goto err;
934 }
935 } else
936 ret = *a;
937
938 if (priv_key->parameters) {
939 EC_GROUP_free(ret->group);
940 ret->group = EC_GROUP_new_from_ecpkparameters(priv_key->parameters);
941 }
942
943 if (ret->group == NULL) {
944 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
945 goto err;
946 }
947
948 ret->version = priv_key->version;
949
950 if (priv_key->privateKey) {
951 ASN1_OCTET_STRING *pkey = priv_key->privateKey;
952 if (EC_KEY_oct2priv(ret, ASN1_STRING_get0_data(pkey),
953 ASN1_STRING_length(pkey)) == 0)
954 goto err;
955 } else {
956 ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_MISSING_PRIVATE_KEY);
957 goto err;
958 }
959
960 EC_POINT_clear_free(ret->pub_key);
961 ret->pub_key = EC_POINT_new(ret->group);
962 if (ret->pub_key == NULL) {
963 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
964 goto err;
965 }
966
967 if (priv_key->publicKey) {
968 const unsigned char *pub_oct;
969 int pub_oct_len;
970
971 pub_oct = ASN1_STRING_get0_data(priv_key->publicKey);
972 pub_oct_len = ASN1_STRING_length(priv_key->publicKey);
973 if (!EC_KEY_oct2key(ret, pub_oct, pub_oct_len, NULL)) {
974 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
975 goto err;
976 }
977 } else {
978 if (ret->group->meth->keygenpub == NULL
979 || ret->group->meth->keygenpub(ret) == 0)
980 goto err;
981 /* Remember the original private-key-only encoding. */
982 ret->enc_flag |= EC_PKEY_NO_PUBKEY;
983 }
984
985 if (a)
986 *a = ret;
987 EC_PRIVATEKEY_free(priv_key);
988 *in = p;
989 ret->dirty_cnt++;
990 return ret;
991
992 err:
993 if (a == NULL || *a != ret)
994 EC_KEY_free(ret);
995 EC_PRIVATEKEY_free(priv_key);
996 return NULL;
997 }
998
999 int i2d_ECPrivateKey(const EC_KEY *a, unsigned char **out)
1000 {
1001 int ret = 0, ok = 0;
1002 unsigned char *priv= NULL, *pub= NULL;
1003 size_t privlen = 0, publen = 0;
1004
1005 EC_PRIVATEKEY *priv_key = NULL;
1006
1007 if (a == NULL || a->group == NULL ||
1008 (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key == NULL)) {
1009 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
1010 goto err;
1011 }
1012
1013 if ((priv_key = EC_PRIVATEKEY_new()) == NULL) {
1014 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1015 goto err;
1016 }
1017
1018 priv_key->version = a->version;
1019
1020 privlen = EC_KEY_priv2buf(a, &priv);
1021
1022 if (privlen == 0) {
1023 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1024 goto err;
1025 }
1026
1027 ASN1_STRING_set0(priv_key->privateKey, priv, privlen);
1028 priv = NULL;
1029
1030 if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) {
1031 if ((priv_key->parameters =
1032 EC_GROUP_get_ecpkparameters(a->group,
1033 priv_key->parameters)) == NULL) {
1034 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1035 goto err;
1036 }
1037 }
1038
1039 if (!(a->enc_flag & EC_PKEY_NO_PUBKEY)) {
1040 priv_key->publicKey = ASN1_BIT_STRING_new();
1041 if (priv_key->publicKey == NULL) {
1042 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1043 goto err;
1044 }
1045
1046 publen = EC_KEY_key2buf(a, a->conv_form, &pub, NULL);
1047
1048 if (publen == 0) {
1049 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1050 goto err;
1051 }
1052
1053 priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
1054 priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
1055 ASN1_STRING_set0(priv_key->publicKey, pub, publen);
1056 pub = NULL;
1057 }
1058
1059 if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) {
1060 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1061 goto err;
1062 }
1063 ok = 1;
1064 err:
1065 OPENSSL_clear_free(priv, privlen);
1066 OPENSSL_free(pub);
1067 EC_PRIVATEKEY_free(priv_key);
1068 return (ok ? ret : 0);
1069 }
1070
1071 int i2d_ECParameters(const EC_KEY *a, unsigned char **out)
1072 {
1073 if (a == NULL) {
1074 ECerr(EC_F_I2D_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1075 return 0;
1076 }
1077 return i2d_ECPKParameters(a->group, out);
1078 }
1079
1080 EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len)
1081 {
1082 EC_KEY *ret;
1083
1084 if (in == NULL || *in == NULL) {
1085 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1086 return NULL;
1087 }
1088
1089 if (a == NULL || *a == NULL) {
1090 if ((ret = EC_KEY_new()) == NULL) {
1091 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
1092 return NULL;
1093 }
1094 } else
1095 ret = *a;
1096
1097 if (!d2i_ECPKParameters(&ret->group, in, len)) {
1098 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB);
1099 if (a == NULL || *a != ret)
1100 EC_KEY_free(ret);
1101 else
1102 ret->dirty_cnt++;
1103 return NULL;
1104 }
1105 ret->dirty_cnt++;
1106
1107 if (a)
1108 *a = ret;
1109
1110 return ret;
1111 }
1112
1113 EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len)
1114 {
1115 EC_KEY *ret = NULL;
1116
1117 if (a == NULL || (*a) == NULL || (*a)->group == NULL) {
1118 /*
1119 * sorry, but a EC_GROUP-structure is necessary to set the public key
1120 */
1121 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1122 return 0;
1123 }
1124 ret = *a;
1125 /* EC_KEY_opt2key updates dirty_cnt */
1126 if (!EC_KEY_oct2key(ret, *in, len, NULL)) {
1127 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_EC_LIB);
1128 return 0;
1129 }
1130 *in += len;
1131 return ret;
1132 }
1133
1134 int i2o_ECPublicKey(const EC_KEY *a, unsigned char **out)
1135 {
1136 size_t buf_len = 0;
1137 int new_buffer = 0;
1138
1139 if (a == NULL) {
1140 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1141 return 0;
1142 }
1143
1144 buf_len = EC_POINT_point2oct(a->group, a->pub_key,
1145 a->conv_form, NULL, 0, NULL);
1146
1147 if (out == NULL || buf_len == 0)
1148 /* out == NULL => just return the length of the octet string */
1149 return buf_len;
1150
1151 if (*out == NULL) {
1152 if ((*out = OPENSSL_malloc(buf_len)) == NULL) {
1153 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
1154 return 0;
1155 }
1156 new_buffer = 1;
1157 }
1158 if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form,
1159 *out, buf_len, NULL)) {
1160 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_EC_LIB);
1161 if (new_buffer) {
1162 OPENSSL_free(*out);
1163 *out = NULL;
1164 }
1165 return 0;
1166 }
1167 if (!new_buffer)
1168 *out += buf_len;
1169 return buf_len;
1170 }
1171
1172 DECLARE_ASN1_FUNCTIONS(ECDSA_SIG)
1173 DECLARE_ASN1_ENCODE_FUNCTIONS_name(ECDSA_SIG, ECDSA_SIG)
1174
1175 #endif /* FIPS_MODULE */
1176
1177 ECDSA_SIG *ECDSA_SIG_new(void)
1178 {
1179 ECDSA_SIG *sig = OPENSSL_zalloc(sizeof(*sig));
1180 if (sig == NULL)
1181 ECerr(EC_F_ECDSA_SIG_NEW, ERR_R_MALLOC_FAILURE);
1182 return sig;
1183 }
1184
1185 void ECDSA_SIG_free(ECDSA_SIG *sig)
1186 {
1187 if (sig == NULL)
1188 return;
1189 BN_clear_free(sig->r);
1190 BN_clear_free(sig->s);
1191 OPENSSL_free(sig);
1192 }
1193
1194 ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **psig, const unsigned char **ppin, long len)
1195 {
1196 ECDSA_SIG *sig;
1197
1198 if (len < 0)
1199 return NULL;
1200 if (psig != NULL && *psig != NULL) {
1201 sig = *psig;
1202 } else {
1203 sig = ECDSA_SIG_new();
1204 if (sig == NULL)
1205 return NULL;
1206 }
1207 if (sig->r == NULL)
1208 sig->r = BN_new();
1209 if (sig->s == NULL)
1210 sig->s = BN_new();
1211 if (decode_der_dsa_sig(sig->r, sig->s, ppin, (size_t)len) == 0) {
1212 if (psig == NULL || *psig == NULL)
1213 ECDSA_SIG_free(sig);
1214 return NULL;
1215 }
1216 if (psig != NULL && *psig == NULL)
1217 *psig = sig;
1218 return sig;
1219 }
1220
1221 int i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **ppout)
1222 {
1223 BUF_MEM *buf = NULL;
1224 size_t encoded_len;
1225 WPACKET pkt;
1226
1227 if (ppout == NULL) {
1228 if (!WPACKET_init_null(&pkt, 0))
1229 return -1;
1230 } else if (*ppout == NULL) {
1231 if ((buf = BUF_MEM_new()) == NULL
1232 || !WPACKET_init_len(&pkt, buf, 0)) {
1233 BUF_MEM_free(buf);
1234 return -1;
1235 }
1236 } else {
1237 if (!WPACKET_init_static_len(&pkt, *ppout, SIZE_MAX, 0))
1238 return -1;
1239 }
1240
1241 if (!encode_der_dsa_sig(&pkt, sig->r, sig->s)
1242 || !WPACKET_get_total_written(&pkt, &encoded_len)
1243 || !WPACKET_finish(&pkt)) {
1244 BUF_MEM_free(buf);
1245 WPACKET_cleanup(&pkt);
1246 return -1;
1247 }
1248
1249 if (ppout != NULL) {
1250 if (*ppout == NULL) {
1251 *ppout = (unsigned char *)buf->data;
1252 buf->data = NULL;
1253 BUF_MEM_free(buf);
1254 } else {
1255 *ppout += encoded_len;
1256 }
1257 }
1258
1259 return (int)encoded_len;
1260 }
1261
1262 void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
1263 {
1264 if (pr != NULL)
1265 *pr = sig->r;
1266 if (ps != NULL)
1267 *ps = sig->s;
1268 }
1269
1270 const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig)
1271 {
1272 return sig->r;
1273 }
1274
1275 const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig)
1276 {
1277 return sig->s;
1278 }
1279
1280 int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
1281 {
1282 if (r == NULL || s == NULL)
1283 return 0;
1284 BN_clear_free(sig->r);
1285 BN_clear_free(sig->s);
1286 sig->r = r;
1287 sig->s = s;
1288 return 1;
1289 }
1290
1291 int ECDSA_size(const EC_KEY *ec)
1292 {
1293 int ret;
1294 ECDSA_SIG sig;
1295 const EC_GROUP *group;
1296 const BIGNUM *bn;
1297
1298 if (ec == NULL)
1299 return 0;
1300 group = EC_KEY_get0_group(ec);
1301 if (group == NULL)
1302 return 0;
1303
1304 bn = EC_GROUP_get0_order(group);
1305 if (bn == NULL)
1306 return 0;
1307
1308 sig.r = sig.s = (BIGNUM *)bn;
1309 ret = i2d_ECDSA_SIG(&sig, NULL);
1310
1311 if (ret < 0)
1312 ret = 0;
1313 return ret;
1314 }