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