]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ec/ec_asn1.c
Reorganize local header files
[thirdparty/openssl.git] / crypto / ec / ec_asn1.c
CommitLineData
7e31164a 1/*
48e5119a 2 * Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
012c86ab 3 *
a7f182b7 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
aa6bb135
RS
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
012c86ab
BM
8 */
9
5454829a 10#include <string.h>
706457b7 11#include "ec_local.h"
0bee0e62 12#include <openssl/err.h>
012c86ab
BM
13#include <openssl/asn1t.h>
14#include <openssl/objects.h>
677963e5 15#include "internal/nelem.h"
25f2138b 16#include "crypto/asn1_dsa.h"
012c86ab 17
a9612d6c
MC
18#ifndef FIPS_MODE
19
34f1f2a8 20int EC_GROUP_get_basis_type(const EC_GROUP *group)
0f113f3e 21{
57f48f93 22 int i;
0f113f3e
MC
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
57f48f93
RS
29 /* Find the last non-zero element of group->poly[] */
30 for (i = 0;
50799f35 31 i < (int)OSSL_NELEM(group->poly) && group->poly[i] != 0;
57f48f93
RS
32 i++)
33 continue;
0f113f3e
MC
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
b3310161 44#ifndef OPENSSL_NO_EC2M
34f1f2a8 45int EC_GROUP_get_trinomial_basis(const EC_GROUP *group, unsigned int *k)
0f113f3e
MC
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
34f1f2a8 65int EC_GROUP_get_pentanomial_basis(const EC_GROUP *group, unsigned int *k1,
0f113f3e
MC
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}
b3310161 90#endif
8aefe253 91
012c86ab 92/* some structures needed for the asn1 encoding */
012c86ab 93typedef struct x9_62_pentanomial_st {
6a32a3c0
RL
94 int32_t k1;
95 int32_t k2;
96 int32_t k3;
0f113f3e 97} X9_62_PENTANOMIAL;
012c86ab 98
37af03d3 99typedef struct x9_62_characteristic_two_st {
6a32a3c0 100 int32_t m;
0f113f3e
MC
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;
37af03d3
GT
114
115typedef struct x9_62_fieldid_st {
0f113f3e
MC
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;
37af03d3 127
012c86ab 128typedef struct x9_62_curve_st {
0f113f3e
MC
129 ASN1_OCTET_STRING *a;
130 ASN1_OCTET_STRING *b;
131 ASN1_BIT_STRING *seed;
132} X9_62_CURVE;
012c86ab 133
03f880e4 134struct ec_parameters_st {
6a32a3c0 135 int32_t version;
0f113f3e
MC
136 X9_62_FIELDID *fieldID;
137 X9_62_CURVE *curve;
138 ASN1_OCTET_STRING *base;
139 ASN1_INTEGER *order;
140 ASN1_INTEGER *cofactor;
03f880e4 141} /* ECPARAMETERS */ ;
012c86ab
BM
142
143struct ecpk_parameters_st {
0f113f3e
MC
144 int type;
145 union {
146 ASN1_OBJECT *named_curve;
147 ECPARAMETERS *parameters;
148 ASN1_NULL *implicitlyCA;
149 } value;
150} /* ECPKPARAMETERS */ ;
012c86ab 151
14a7cfb3
BM
152/* SEC1 ECPrivateKey */
153typedef struct ec_privatekey_st {
6a32a3c0 154 int32_t version;
0f113f3e
MC
155 ASN1_OCTET_STRING *privateKey;
156 ECPKPARAMETERS *parameters;
157 ASN1_BIT_STRING *publicKey;
158} EC_PRIVATEKEY;
14a7cfb3 159
37af03d3
GT
160/* the OpenSSL ASN.1 definitions */
161ASN1_SEQUENCE(X9_62_PENTANOMIAL) = {
9612e157
RL
162 ASN1_EMBED(X9_62_PENTANOMIAL, k1, INT32),
163 ASN1_EMBED(X9_62_PENTANOMIAL, k2, INT32),
164 ASN1_EMBED(X9_62_PENTANOMIAL, k3, INT32)
df2ee0e2 165} static_ASN1_SEQUENCE_END(X9_62_PENTANOMIAL)
012c86ab 166
37af03d3
GT
167DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
168IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
169
170ASN1_ADB_TEMPLATE(char_two_def) = ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.other, ASN1_ANY);
012c86ab 171
37af03d3 172ASN1_ADB(X9_62_CHARACTERISTIC_TWO) = {
0f113f3e
MC
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))
37af03d3 176} ASN1_ADB_END(X9_62_CHARACTERISTIC_TWO, 0, type, 0, &char_two_def_tt, NULL);
012c86ab
BM
177
178ASN1_SEQUENCE(X9_62_CHARACTERISTIC_TWO) = {
9612e157 179 ASN1_EMBED(X9_62_CHARACTERISTIC_TWO, m, INT32),
0f113f3e
MC
180 ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, type, ASN1_OBJECT),
181 ASN1_ADB_OBJECT(X9_62_CHARACTERISTIC_TWO)
df2ee0e2 182} static_ASN1_SEQUENCE_END(X9_62_CHARACTERISTIC_TWO)
012c86ab 183
37af03d3
GT
184DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
185IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
012c86ab 186
37af03d3
GT
187ASN1_ADB_TEMPLATE(fieldID_def) = ASN1_SIMPLE(X9_62_FIELDID, p.other, ASN1_ANY);
188
189ASN1_ADB(X9_62_FIELDID) = {
0f113f3e
MC
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))
37af03d3 192} ASN1_ADB_END(X9_62_FIELDID, 0, fieldType, 0, &fieldID_def_tt, NULL);
012c86ab 193
37af03d3 194ASN1_SEQUENCE(X9_62_FIELDID) = {
0f113f3e
MC
195 ASN1_SIMPLE(X9_62_FIELDID, fieldType, ASN1_OBJECT),
196 ASN1_ADB_OBJECT(X9_62_FIELDID)
df2ee0e2 197} static_ASN1_SEQUENCE_END(X9_62_FIELDID)
012c86ab
BM
198
199ASN1_SEQUENCE(X9_62_CURVE) = {
0f113f3e
MC
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)
df2ee0e2 203} static_ASN1_SEQUENCE_END(X9_62_CURVE)
012c86ab 204
012c86ab 205ASN1_SEQUENCE(ECPARAMETERS) = {
9612e157 206 ASN1_EMBED(ECPARAMETERS, version, INT32),
0f113f3e
MC
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)
60b350a3 212} ASN1_SEQUENCE_END(ECPARAMETERS)
012c86ab 213
37af03d3
GT
214DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
215IMPLEMENT_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
012c86ab
BM
216
217ASN1_CHOICE(ECPKPARAMETERS) = {
0f113f3e
MC
218 ASN1_SIMPLE(ECPKPARAMETERS, value.named_curve, ASN1_OBJECT),
219 ASN1_SIMPLE(ECPKPARAMETERS, value.parameters, ECPARAMETERS),
220 ASN1_SIMPLE(ECPKPARAMETERS, value.implicitlyCA, ASN1_NULL)
60b350a3 221} ASN1_CHOICE_END(ECPKPARAMETERS)
012c86ab 222
9fdcc21f
DO
223DECLARE_ASN1_FUNCTIONS(ECPKPARAMETERS)
224DECLARE_ASN1_ENCODE_FUNCTIONS_name(ECPKPARAMETERS, ECPKPARAMETERS)
225IMPLEMENT_ASN1_FUNCTIONS(ECPKPARAMETERS)
012c86ab 226
0bee0e62 227ASN1_SEQUENCE(EC_PRIVATEKEY) = {
9612e157 228 ASN1_EMBED(EC_PRIVATEKEY, version, INT32),
0f113f3e
MC
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)
df2ee0e2 232} static_ASN1_SEQUENCE_END(EC_PRIVATEKEY)
0bee0e62 233
9fdcc21f
DO
234DECLARE_ASN1_FUNCTIONS(EC_PRIVATEKEY)
235DECLARE_ASN1_ENCODE_FUNCTIONS_name(EC_PRIVATEKEY, EC_PRIVATEKEY)
236IMPLEMENT_ASN1_FUNCTIONS(EC_PRIVATEKEY)
0bee0e62 237
7e31164a 238/* some declarations of internal function */
012c86ab 239
0f113f3e 240/* ec_asn1_group2field() sets the values in a X9_62_FIELDID object */
37af03d3 241static int ec_asn1_group2fieldid(const EC_GROUP *, X9_62_FIELDID *);
0f113f3e 242/* ec_asn1_group2curve() sets the values in a X9_62_CURVE object */
37af03d3 243static int ec_asn1_group2curve(const EC_GROUP *, X9_62_CURVE *);
7e31164a
BM
244
245/* the function definitions */
012c86ab 246
37af03d3 247static int ec_asn1_group2fieldid(const EC_GROUP *group, X9_62_FIELDID *field)
0f113f3e
MC
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) */
0dfb9398 256 ASN1_OBJECT_free(field->fieldType);
2ace7450 257 ASN1_TYPE_free(field->p.other);
0f113f3e
MC
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 */
9cc570d4 272 if (!EC_GROUP_get_curve(group, tmp, NULL, NULL, NULL)) {
0f113f3e
MC
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 }
6903e2e7 282 } else if (nid == NID_X9_62_characteristic_two_field)
b3310161 283#ifdef OPENSSL_NO_EC2M
0f113f3e
MC
284 {
285 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_GF2M_NOT_SUPPORTED);
286 goto err;
287 }
b3310161 288#else
0f113f3e
MC
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();
90945fa3 322 if (char_two->p.tpBasis == NULL) {
0f113f3e
MC
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();
90945fa3 337 if (char_two->p.ppBasis == NULL) {
0f113f3e
MC
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();
90945fa3 350 if (char_two->p.onBasis == NULL) {
0f113f3e
MC
351 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
352 goto err;
353 }
354 }
355 }
b3310161 356#endif
6903e2e7
DSH
357 else {
358 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_UNSUPPORTED_FIELD);
359 goto err;
360 }
012c86ab 361
0f113f3e 362 ok = 1;
012c86ab 363
23a1d5e9
RS
364 err:
365 BN_free(tmp);
26a7d938 366 return ok;
012c86ab
BM
367}
368
37af03d3 369static int ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve)
0f113f3e 370{
9cc570d4 371 int ok = 0;
0f113f3e 372 BIGNUM *tmp_1 = NULL, *tmp_2 = NULL;
fc6f579a
DB
373 unsigned char *a_buf = NULL, *b_buf = NULL;
374 size_t len;
0f113f3e
MC
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
0f113f3e 384 /* get a and b */
9cc570d4
MC
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;
0f113f3e 388 }
0f113f3e 389
fc6f579a
DB
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;
0f113f3e 400 }
fc6f579a
DB
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;
0f113f3e
MC
405 }
406
407 /* set a and b */
fc6f579a
DB
408 if (!ASN1_OCTET_STRING_set(curve->a, a_buf, len)
409 || !ASN1_OCTET_STRING_set(curve->b, b_buf, len)) {
0f113f3e
MC
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 {
2ace7450
RS
429 ASN1_BIT_STRING_free(curve->seed);
430 curve->seed = NULL;
0f113f3e
MC
431 }
432
433 ok = 1;
434
23a1d5e9 435 err:
fc6f579a
DB
436 OPENSSL_free(a_buf);
437 OPENSSL_free(b_buf);
23a1d5e9
RS
438 BN_free(tmp_1);
439 BN_free(tmp_2);
26a7d938 440 return ok;
0f113f3e 441}
012c86ab 442
60b350a3
RS
443ECPARAMETERS *EC_GROUP_get_ecparameters(const EC_GROUP *group,
444 ECPARAMETERS *params)
0f113f3e 445{
0f113f3e
MC
446 size_t len = 0;
447 ECPARAMETERS *ret = NULL;
be2e334f 448 const BIGNUM *tmp;
0f113f3e
MC
449 unsigned char *buffer = NULL;
450 const EC_POINT *point = NULL;
451 point_conversion_form_t form;
f28bc7d3 452 ASN1_INTEGER *orig;
0f113f3e 453
60b350a3 454 if (params == NULL) {
0f113f3e 455 if ((ret = ECPARAMETERS_new()) == NULL) {
60b350a3 456 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
457 goto err;
458 }
459 } else
60b350a3 460 ret = params;
0f113f3e
MC
461
462 /* set the version (always one) */
463 ret->version = (long)0x1;
464
465 /* set the fieldID */
466 if (!ec_asn1_group2fieldid(group, ret->fieldID)) {
60b350a3 467 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
0f113f3e
MC
468 goto err;
469 }
470
471 /* set the curve */
472 if (!ec_asn1_group2curve(group, ret->curve)) {
60b350a3 473 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
0f113f3e
MC
474 goto err;
475 }
476
477 /* set the base point */
478 if ((point = EC_GROUP_get0_generator(group)) == NULL) {
60b350a3 479 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, EC_R_UNDEFINED_GENERATOR);
0f113f3e
MC
480 goto err;
481 }
482
483 form = EC_GROUP_get_point_conversion_form(group);
484
981bd8a2 485 len = EC_POINT_point2buf(group, point, form, &buffer, NULL);
0f113f3e 486 if (len == 0) {
60b350a3 487 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
0f113f3e
MC
488 goto err;
489 }
0f113f3e 490 if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) {
0110a470 491 OPENSSL_free(buffer);
60b350a3 492 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
493 goto err;
494 }
0110a470 495 ASN1_STRING_set0(ret->base, buffer, len);
0f113f3e
MC
496
497 /* set the order */
be2e334f
DSH
498 tmp = EC_GROUP_get0_order(group);
499 if (tmp == NULL) {
60b350a3 500 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
0f113f3e
MC
501 goto err;
502 }
f28bc7d3 503 ret->order = BN_to_ASN1_INTEGER(tmp, orig = ret->order);
0f113f3e 504 if (ret->order == NULL) {
f28bc7d3 505 ret->order = orig;
60b350a3 506 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_ASN1_LIB);
0f113f3e
MC
507 goto err;
508 }
509
510 /* set the cofactor (optional) */
be2e334f
DSH
511 tmp = EC_GROUP_get0_cofactor(group);
512 if (tmp != NULL) {
f28bc7d3 513 ret->cofactor = BN_to_ASN1_INTEGER(tmp, orig = ret->cofactor);
0f113f3e 514 if (ret->cofactor == NULL) {
f28bc7d3 515 ret->cofactor = orig;
60b350a3 516 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_ASN1_LIB);
0f113f3e
MC
517 goto err;
518 }
519 }
520
23a1d5e9 521 return ret;
0f113f3e 522
23a1d5e9 523 err:
60b350a3 524 if (params == NULL)
23a1d5e9 525 ECPARAMETERS_free(ret);
23a1d5e9 526 return NULL;
0f113f3e
MC
527}
528
60b350a3
RS
529ECPKPARAMETERS *EC_GROUP_get_ecpkparameters(const EC_GROUP *group,
530 ECPKPARAMETERS *params)
0f113f3e
MC
531{
532 int ok = 1, tmp;
533 ECPKPARAMETERS *ret = params;
534
535 if (ret == NULL) {
536 if ((ret = ECPKPARAMETERS_new()) == NULL) {
60b350a3 537 ECerr(EC_F_EC_GROUP_GET_ECPKPARAMETERS, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
538 return NULL;
539 }
540 } else {
0dfb9398 541 if (ret->type == 0)
0f113f3e
MC
542 ASN1_OBJECT_free(ret->value.named_curve);
543 else if (ret->type == 1 && ret->value.parameters)
544 ECPARAMETERS_free(ret->value.parameters);
545 }
546
547 if (EC_GROUP_get_asn1_flag(group)) {
548 /*
436ad81f 549 * use the asn1 OID to describe the elliptic curve parameters
0f113f3e
MC
550 */
551 tmp = EC_GROUP_get_curve_name(group);
552 if (tmp) {
553 ret->type = 0;
554 if ((ret->value.named_curve = OBJ_nid2obj(tmp)) == NULL)
555 ok = 0;
556 } else
0d4fb843 557 /* we don't know the nid => ERROR */
0f113f3e
MC
558 ok = 0;
559 } else {
560 /* use the ECPARAMETERS structure */
561 ret->type = 1;
562 if ((ret->value.parameters =
60b350a3 563 EC_GROUP_get_ecparameters(group, NULL)) == NULL)
0f113f3e
MC
564 ok = 0;
565 }
566
567 if (!ok) {
568 ECPKPARAMETERS_free(ret);
569 return NULL;
570 }
571 return ret;
572}
012c86ab 573
60b350a3 574EC_GROUP *EC_GROUP_new_from_ecparameters(const ECPARAMETERS *params)
0f113f3e
MC
575{
576 int ok = 0, tmp;
bacaa618 577 EC_GROUP *ret = NULL, *dup = NULL;
0f113f3e
MC
578 BIGNUM *p = NULL, *a = NULL, *b = NULL;
579 EC_POINT *point = NULL;
580 long field_bits;
bacaa618
NT
581 int curve_name = NID_undef;
582 BN_CTX *ctx = NULL;
0f113f3e
MC
583
584 if (!params->fieldID || !params->fieldID->fieldType ||
585 !params->fieldID->p.ptr) {
60b350a3 586 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
0f113f3e
MC
587 goto err;
588 }
589
fc6f579a
DB
590 /*
591 * Now extract the curve parameters a and b. Note that, although SEC 1
592 * specifies the length of their encodings, historical versions of OpenSSL
593 * encoded them incorrectly, so we must accept any length for backwards
594 * compatibility.
595 */
0f113f3e
MC
596 if (!params->curve || !params->curve->a ||
597 !params->curve->a->data || !params->curve->b ||
598 !params->curve->b->data) {
60b350a3 599 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
0f113f3e
MC
600 goto err;
601 }
602 a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);
603 if (a == NULL) {
60b350a3 604 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);
0f113f3e
MC
605 goto err;
606 }
607 b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
608 if (b == NULL) {
60b350a3 609 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);
0f113f3e
MC
610 goto err;
611 }
612
613 /* get the field parameters */
614 tmp = OBJ_obj2nid(params->fieldID->fieldType);
615 if (tmp == NID_X9_62_characteristic_two_field)
b3310161 616#ifdef OPENSSL_NO_EC2M
0f113f3e 617 {
60b350a3 618 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_GF2M_NOT_SUPPORTED);
0f113f3e
MC
619 goto err;
620 }
b3310161 621#else
0f113f3e
MC
622 {
623 X9_62_CHARACTERISTIC_TWO *char_two;
624
625 char_two = params->fieldID->p.char_two;
626
627 field_bits = char_two->m;
628 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
60b350a3 629 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_FIELD_TOO_LARGE);
0f113f3e
MC
630 goto err;
631 }
632
633 if ((p = BN_new()) == NULL) {
60b350a3 634 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
635 goto err;
636 }
637
638 /* get the base type */
639 tmp = OBJ_obj2nid(char_two->type);
640
641 if (tmp == NID_X9_62_tpBasis) {
642 long tmp_long;
643
644 if (!char_two->p.tpBasis) {
60b350a3 645 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
0f113f3e
MC
646 goto err;
647 }
648
649 tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis);
650
651 if (!(char_two->m > tmp_long && tmp_long > 0)) {
60b350a3 652 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS,
0f113f3e
MC
653 EC_R_INVALID_TRINOMIAL_BASIS);
654 goto err;
655 }
656
657 /* create the polynomial */
658 if (!BN_set_bit(p, (int)char_two->m))
659 goto err;
660 if (!BN_set_bit(p, (int)tmp_long))
661 goto err;
662 if (!BN_set_bit(p, 0))
663 goto err;
664 } else if (tmp == NID_X9_62_ppBasis) {
665 X9_62_PENTANOMIAL *penta;
666
667 penta = char_two->p.ppBasis;
668 if (!penta) {
60b350a3 669 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
0f113f3e
MC
670 goto err;
671 }
672
673 if (!
674 (char_two->m > penta->k3 && penta->k3 > penta->k2
675 && penta->k2 > penta->k1 && penta->k1 > 0)) {
60b350a3 676 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS,
0f113f3e
MC
677 EC_R_INVALID_PENTANOMIAL_BASIS);
678 goto err;
679 }
680
681 /* create the polynomial */
682 if (!BN_set_bit(p, (int)char_two->m))
683 goto err;
684 if (!BN_set_bit(p, (int)penta->k1))
685 goto err;
686 if (!BN_set_bit(p, (int)penta->k2))
687 goto err;
688 if (!BN_set_bit(p, (int)penta->k3))
689 goto err;
690 if (!BN_set_bit(p, 0))
691 goto err;
692 } else if (tmp == NID_X9_62_onBasis) {
60b350a3 693 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_NOT_IMPLEMENTED);
0f113f3e
MC
694 goto err;
695 } else { /* error */
696
60b350a3 697 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
0f113f3e
MC
698 goto err;
699 }
700
701 /* create the EC_GROUP structure */
702 ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
703 }
b3310161 704#endif
0f113f3e
MC
705 else if (tmp == NID_X9_62_prime_field) {
706 /* we have a curve over a prime field */
707 /* extract the prime number */
708 if (!params->fieldID->p.prime) {
60b350a3 709 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
0f113f3e
MC
710 goto err;
711 }
712 p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);
713 if (p == NULL) {
60b350a3 714 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
0f113f3e
MC
715 goto err;
716 }
717
718 if (BN_is_negative(p) || BN_is_zero(p)) {
60b350a3 719 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_FIELD);
0f113f3e
MC
720 goto err;
721 }
722
723 field_bits = BN_num_bits(p);
724 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
60b350a3 725 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_FIELD_TOO_LARGE);
0f113f3e
MC
726 goto err;
727 }
728
729 /* create the EC_GROUP structure */
730 ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);
731 } else {
60b350a3 732 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_FIELD);
0f113f3e
MC
733 goto err;
734 }
735
736 if (ret == NULL) {
60b350a3 737 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
0f113f3e
MC
738 goto err;
739 }
740
741 /* extract seed (optional) */
742 if (params->curve->seed != NULL) {
b548a1f1 743 OPENSSL_free(ret->seed);
75ebbd9a 744 if ((ret->seed = OPENSSL_malloc(params->curve->seed->length)) == NULL) {
60b350a3 745 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
746 goto err;
747 }
748 memcpy(ret->seed, params->curve->seed->data,
749 params->curve->seed->length);
750 ret->seed_len = params->curve->seed->length;
751 }
752
753 if (!params->order || !params->base || !params->base->data) {
60b350a3 754 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
0f113f3e
MC
755 goto err;
756 }
757
758 if ((point = EC_POINT_new(ret)) == NULL)
759 goto err;
760
761 /* set the point conversion form */
762 EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)
763 (params->base->data[0] & ~0x01));
764
765 /* extract the ec point */
766 if (!EC_POINT_oct2point(ret, point, params->base->data,
767 params->base->length, NULL)) {
60b350a3 768 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
0f113f3e
MC
769 goto err;
770 }
771
772 /* extract the order */
773 if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {
60b350a3 774 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
0f113f3e
MC
775 goto err;
776 }
777 if (BN_is_negative(a) || BN_is_zero(a)) {
60b350a3 778 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_GROUP_ORDER);
0f113f3e
MC
779 goto err;
780 }
781 if (BN_num_bits(a) > (int)field_bits + 1) { /* Hasse bound */
60b350a3 782 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_GROUP_ORDER);
0f113f3e
MC
783 goto err;
784 }
785
786 /* extract the cofactor (optional) */
787 if (params->cofactor == NULL) {
23a1d5e9
RS
788 BN_free(b);
789 b = NULL;
0f113f3e 790 } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) {
60b350a3 791 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
0f113f3e
MC
792 goto err;
793 }
794 /* set the generator, order and cofactor (if present) */
795 if (!EC_GROUP_set_generator(ret, point, a, b)) {
60b350a3 796 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
0f113f3e
MC
797 goto err;
798 }
799
bacaa618
NT
800 /*
801 * Check if the explicit parameters group just created matches one of the
802 * built-in curves.
803 *
804 * We create a copy of the group just built, so that we can remove optional
805 * fields for the lookup: we do this to avoid the possibility that one of
806 * the optional parameters is used to force the library into using a less
807 * performant and less secure EC_METHOD instead of the specialized one.
808 * In any case, `seed` is not really used in any computation, while a
809 * cofactor different from the one in the built-in table is just
810 * mathematically wrong anyway and should not be used.
811 */
812 if ((ctx = BN_CTX_new()) == NULL) {
813 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);
814 goto err;
815 }
816 if ((dup = EC_GROUP_dup(ret)) == NULL
817 || EC_GROUP_set_seed(dup, NULL, 0) != 1
818 || !EC_GROUP_set_generator(dup, point, a, NULL)) {
819 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
820 goto err;
821 }
822 if ((curve_name = ec_curve_nid_from_params(dup, ctx)) != NID_undef) {
823 /*
824 * The input explicit parameters successfully matched one of the
825 * built-in curves: often for built-in curves we have specialized
826 * methods with better performance and hardening.
827 *
828 * In this case we replace the `EC_GROUP` created through explicit
829 * parameters with one created from a named group.
830 */
831 EC_GROUP *named_group = NULL;
832
833#ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
834 /*
835 * NID_wap_wsg_idm_ecid_wtls12 and NID_secp224r1 are both aliases for
836 * the same curve, we prefer the SECP nid when matching explicit
837 * parameters as that is associated with a specialized EC_METHOD.
838 */
839 if (curve_name == NID_wap_wsg_idm_ecid_wtls12)
840 curve_name = NID_secp224r1;
841#endif /* !def(OPENSSL_NO_EC_NISTP_64_GCC_128) */
842
843 if ((named_group = EC_GROUP_new_by_curve_name(curve_name)) == NULL) {
844 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
845 goto err;
846 }
847 EC_GROUP_free(ret);
848 ret = named_group;
849
850 /*
851 * Set the flag so that EC_GROUPs created from explicit parameters are
852 * serialized using explicit parameters by default.
853 */
854 EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_EXPLICIT_CURVE);
855 }
856
0f113f3e
MC
857 ok = 1;
858
8fdc3734
RS
859 err:
860 if (!ok) {
bacaa618 861 EC_GROUP_free(ret);
0f113f3e
MC
862 ret = NULL;
863 }
bacaa618 864 EC_GROUP_free(dup);
0f113f3e 865
23a1d5e9
RS
866 BN_free(p);
867 BN_free(a);
868 BN_free(b);
8fdc3734 869 EC_POINT_free(point);
bacaa618
NT
870
871 BN_CTX_free(ctx);
872
26a7d938 873 return ret;
012c86ab
BM
874}
875
60b350a3 876EC_GROUP *EC_GROUP_new_from_ecpkparameters(const ECPKPARAMETERS *params)
0f113f3e
MC
877{
878 EC_GROUP *ret = NULL;
879 int tmp = 0;
880
881 if (params == NULL) {
60b350a3 882 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, EC_R_MISSING_PARAMETERS);
0f113f3e
MC
883 return NULL;
884 }
885
886 if (params->type == 0) { /* the curve is given by an OID */
887 tmp = OBJ_obj2nid(params->value.named_curve);
888 if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) {
60b350a3 889 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS,
0f113f3e
MC
890 EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
891 return NULL;
892 }
893 EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);
894 } else if (params->type == 1) { /* the parameters are given by a
895 * ECPARAMETERS structure */
60b350a3 896 ret = EC_GROUP_new_from_ecparameters(params->value.parameters);
0f113f3e 897 if (!ret) {
60b350a3 898 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, ERR_R_EC_LIB);
0f113f3e
MC
899 return NULL;
900 }
e363534c 901 EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_EXPLICIT_CURVE);
0f113f3e
MC
902 } else if (params->type == 2) { /* implicitlyCA */
903 return NULL;
904 } else {
60b350a3 905 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, EC_R_ASN1_ERROR);
0f113f3e
MC
906 return NULL;
907 }
908
909 return ret;
910}
012c86ab 911
14a7cfb3 912/* EC_GROUP <-> DER encoding of ECPKPARAMETERS */
012c86ab 913
6343829a 914EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len)
0f113f3e
MC
915{
916 EC_GROUP *group = NULL;
917 ECPKPARAMETERS *params = NULL;
a46c9789 918 const unsigned char *p = *in;
0f113f3e 919
a46c9789 920 if ((params = d2i_ECPKPARAMETERS(NULL, &p, len)) == NULL) {
0f113f3e
MC
921 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE);
922 ECPKPARAMETERS_free(params);
923 return NULL;
924 }
925
60b350a3 926 if ((group = EC_GROUP_new_from_ecpkparameters(params)) == NULL) {
0f113f3e
MC
927 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE);
928 ECPKPARAMETERS_free(params);
929 return NULL;
930 }
931
8fdc3734 932 if (a) {
bacaa618 933 EC_GROUP_free(*a);
0f113f3e 934 *a = group;
8fdc3734 935 }
0f113f3e
MC
936
937 ECPKPARAMETERS_free(params);
a46c9789 938 *in = p;
26a7d938 939 return group;
0f113f3e 940}
012c86ab 941
14a7cfb3 942int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out)
0f113f3e
MC
943{
944 int ret = 0;
60b350a3 945 ECPKPARAMETERS *tmp = EC_GROUP_get_ecpkparameters(a, NULL);
0f113f3e
MC
946 if (tmp == NULL) {
947 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE);
948 return 0;
949 }
950 if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) {
951 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE);
952 ECPKPARAMETERS_free(tmp);
953 return 0;
954 }
955 ECPKPARAMETERS_free(tmp);
26a7d938 956 return ret;
0f113f3e 957}
012c86ab 958
14a7cfb3
BM
959/* some EC_KEY functions */
960
6343829a 961EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len)
0f113f3e 962{
0f113f3e
MC
963 EC_KEY *ret = NULL;
964 EC_PRIVATEKEY *priv_key = NULL;
a46c9789 965 const unsigned char *p = *in;
0f113f3e 966
a46c9789 967 if ((priv_key = d2i_EC_PRIVATEKEY(NULL, &p, len)) == NULL) {
0f113f3e 968 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
0f113f3e
MC
969 return NULL;
970 }
971
972 if (a == NULL || *a == NULL) {
973 if ((ret = EC_KEY_new()) == NULL) {
974 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
975 goto err;
976 }
0f113f3e
MC
977 } else
978 ret = *a;
979
980 if (priv_key->parameters) {
bacaa618 981 EC_GROUP_free(ret->group);
60b350a3 982 ret->group = EC_GROUP_new_from_ecpkparameters(priv_key->parameters);
0f113f3e
MC
983 }
984
985 if (ret->group == NULL) {
986 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
987 goto err;
988 }
989
990 ret->version = priv_key->version;
991
992 if (priv_key->privateKey) {
d810700b 993 ASN1_OCTET_STRING *pkey = priv_key->privateKey;
17ebf85a 994 if (EC_KEY_oct2priv(ret, ASN1_STRING_get0_data(pkey),
d810700b 995 ASN1_STRING_length(pkey)) == 0)
0f113f3e 996 goto err;
0f113f3e
MC
997 } else {
998 ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_MISSING_PRIVATE_KEY);
999 goto err;
1000 }
1001
8fdc3734 1002 EC_POINT_clear_free(ret->pub_key);
0f113f3e
MC
1003 ret->pub_key = EC_POINT_new(ret->group);
1004 if (ret->pub_key == NULL) {
1005 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1006 goto err;
1007 }
1008
1009 if (priv_key->publicKey) {
1010 const unsigned char *pub_oct;
1011 int pub_oct_len;
1012
17ebf85a 1013 pub_oct = ASN1_STRING_get0_data(priv_key->publicKey);
f422a514 1014 pub_oct_len = ASN1_STRING_length(priv_key->publicKey);
6ea04154 1015 if (!EC_KEY_oct2key(ret, pub_oct, pub_oct_len, NULL)) {
0f113f3e
MC
1016 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1017 goto err;
1018 }
1019 } else {
77470e98
DSH
1020 if (ret->group->meth->keygenpub == NULL
1021 || ret->group->meth->keygenpub(ret) == 0)
6903e2e7 1022 goto err;
0f113f3e
MC
1023 /* Remember the original private-key-only encoding. */
1024 ret->enc_flag |= EC_PKEY_NO_PUBKEY;
1025 }
1026
9e442d48
MC
1027 if (a)
1028 *a = ret;
25aaa98a 1029 EC_PRIVATEKEY_free(priv_key);
a46c9789 1030 *in = p;
26a7d938 1031 return ret;
25aaa98a
RS
1032
1033 err:
1034 if (a == NULL || *a != ret)
1035 EC_KEY_free(ret);
1036 EC_PRIVATEKEY_free(priv_key);
1037 return NULL;
0f113f3e
MC
1038}
1039
9fdcc21f 1040int i2d_ECPrivateKey(const EC_KEY *a, unsigned char **out)
0f113f3e
MC
1041{
1042 int ret = 0, ok = 0;
7fc7d1a7 1043 unsigned char *priv= NULL, *pub= NULL;
8a41fa6f 1044 size_t privlen = 0, publen = 0;
7fc7d1a7 1045
0f113f3e
MC
1046 EC_PRIVATEKEY *priv_key = NULL;
1047
d810700b 1048 if (a == NULL || a->group == NULL ||
0f113f3e
MC
1049 (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key == NULL)) {
1050 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
1051 goto err;
1052 }
1053
1054 if ((priv_key = EC_PRIVATEKEY_new()) == NULL) {
1055 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1056 goto err;
1057 }
1058
1059 priv_key->version = a->version;
1060
7fc7d1a7 1061 privlen = EC_KEY_priv2buf(a, &priv);
30cd4ff2 1062
7fc7d1a7 1063 if (privlen == 0) {
d810700b 1064 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
30cd4ff2
DE
1065 goto err;
1066 }
1067
7fc7d1a7
DSH
1068 ASN1_STRING_set0(priv_key->privateKey, priv, privlen);
1069 priv = NULL;
0f113f3e
MC
1070
1071 if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) {
1072 if ((priv_key->parameters =
60b350a3 1073 EC_GROUP_get_ecpkparameters(a->group,
0f113f3e
MC
1074 priv_key->parameters)) == NULL) {
1075 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1076 goto err;
1077 }
1078 }
1079
1080 if (!(a->enc_flag & EC_PKEY_NO_PUBKEY)) {
f422a514 1081 priv_key->publicKey = ASN1_BIT_STRING_new();
0f113f3e
MC
1082 if (priv_key->publicKey == NULL) {
1083 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1084 goto err;
1085 }
1086
7fc7d1a7 1087 publen = EC_KEY_key2buf(a, a->conv_form, &pub, NULL);
0f113f3e 1088
7fc7d1a7 1089 if (publen == 0) {
0f113f3e
MC
1090 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1091 goto err;
1092 }
1093
1094 priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
1095 priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
7fc7d1a7
DSH
1096 ASN1_STRING_set0(priv_key->publicKey, pub, publen);
1097 pub = NULL;
0f113f3e
MC
1098 }
1099
1100 if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) {
1101 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1102 goto err;
1103 }
1104 ok = 1;
1105 err:
7fc7d1a7
DSH
1106 OPENSSL_clear_free(priv, privlen);
1107 OPENSSL_free(pub);
25aaa98a 1108 EC_PRIVATEKEY_free(priv_key);
0f113f3e
MC
1109 return (ok ? ret : 0);
1110}
14a7cfb3 1111
9fdcc21f 1112int i2d_ECParameters(const EC_KEY *a, unsigned char **out)
0f113f3e
MC
1113{
1114 if (a == NULL) {
1115 ECerr(EC_F_I2D_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1116 return 0;
1117 }
1118 return i2d_ECPKParameters(a->group, out);
1119}
14a7cfb3
BM
1120
1121EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len)
0f113f3e
MC
1122{
1123 EC_KEY *ret;
1124
1125 if (in == NULL || *in == NULL) {
1126 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1127 return NULL;
1128 }
1129
1130 if (a == NULL || *a == NULL) {
1131 if ((ret = EC_KEY_new()) == NULL) {
1132 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
1133 return NULL;
1134 }
0f113f3e
MC
1135 } else
1136 ret = *a;
1137
1138 if (!d2i_ECPKParameters(&ret->group, in, len)) {
1139 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB);
5e5d53d3
MC
1140 if (a == NULL || *a != ret)
1141 EC_KEY_free(ret);
0f113f3e
MC
1142 return NULL;
1143 }
1144
5e5d53d3
MC
1145 if (a)
1146 *a = ret;
1147
0f113f3e
MC
1148 return ret;
1149}
14a7cfb3 1150
62e3163b 1151EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len)
0f113f3e
MC
1152{
1153 EC_KEY *ret = NULL;
1154
1155 if (a == NULL || (*a) == NULL || (*a)->group == NULL) {
1156 /*
0d4fb843 1157 * sorry, but a EC_GROUP-structure is necessary to set the public key
0f113f3e
MC
1158 */
1159 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1160 return 0;
1161 }
1162 ret = *a;
6ea04154 1163 if (!EC_KEY_oct2key(ret, *in, len, NULL)) {
0f113f3e
MC
1164 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_EC_LIB);
1165 return 0;
1166 }
0f113f3e
MC
1167 *in += len;
1168 return ret;
1169}
14a7cfb3 1170
60c25873 1171int i2o_ECPublicKey(const EC_KEY *a, unsigned char **out)
0f113f3e
MC
1172{
1173 size_t buf_len = 0;
1174 int new_buffer = 0;
1175
1176 if (a == NULL) {
1177 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1178 return 0;
1179 }
1180
1181 buf_len = EC_POINT_point2oct(a->group, a->pub_key,
1182 a->conv_form, NULL, 0, NULL);
1183
1184 if (out == NULL || buf_len == 0)
1185 /* out == NULL => just return the length of the octet string */
1186 return buf_len;
1187
1188 if (*out == NULL) {
1189 if ((*out = OPENSSL_malloc(buf_len)) == NULL) {
1190 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
1191 return 0;
1192 }
1193 new_buffer = 1;
1194 }
1195 if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form,
1196 *out, buf_len, NULL)) {
1197 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_EC_LIB);
1198 if (new_buffer) {
1199 OPENSSL_free(*out);
1200 *out = NULL;
1201 }
1202 return 0;
1203 }
1204 if (!new_buffer)
1205 *out += buf_len;
1206 return buf_len;
1207}
2d3d00dc 1208
9fdcc21f
DO
1209DECLARE_ASN1_FUNCTIONS(ECDSA_SIG)
1210DECLARE_ASN1_ENCODE_FUNCTIONS_name(ECDSA_SIG, ECDSA_SIG)
8cc44d97 1211
a9612d6c
MC
1212#endif /* FIPS_MODE */
1213
8cc44d97
DSH
1214ECDSA_SIG *ECDSA_SIG_new(void)
1215{
1216 ECDSA_SIG *sig = OPENSSL_zalloc(sizeof(*sig));
1217 if (sig == NULL)
1218 ECerr(EC_F_ECDSA_SIG_NEW, ERR_R_MALLOC_FAILURE);
1219 return sig;
1220}
1221
1222void ECDSA_SIG_free(ECDSA_SIG *sig)
1223{
1224 if (sig == NULL)
1225 return;
1226 BN_clear_free(sig->r);
1227 BN_clear_free(sig->s);
1228 OPENSSL_free(sig);
1229}
7236e3c8 1230
54846b7c
DM
1231ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **psig, const unsigned char **ppin, long len)
1232{
1233 ECDSA_SIG *sig;
1234
1235 if (len < 0)
1236 return NULL;
1237 if (psig != NULL && *psig != NULL) {
1238 sig = *psig;
1239 } else {
1240 sig = ECDSA_SIG_new();
1241 if (sig == NULL)
1242 return NULL;
1243 }
1244 if (sig->r == NULL)
1245 sig->r = BN_new();
1246 if (sig->s == NULL)
1247 sig->s = BN_new();
1248 if (decode_der_dsa_sig(sig->r, sig->s, ppin, (size_t)len) == 0) {
1249 if (psig == NULL || *psig == NULL)
1250 ECDSA_SIG_free(sig);
1251 return NULL;
1252 }
1253 if (psig != NULL && *psig == NULL)
1254 *psig = sig;
1255 return sig;
1256}
1257
1258int i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **ppout)
1259{
b8805834 1260 BUF_MEM *buf = NULL;
54846b7c 1261 size_t encoded_len;
b8805834 1262 WPACKET pkt;
54846b7c 1263
b8805834
MC
1264 if (ppout == NULL) {
1265 if (!WPACKET_init_null(&pkt, 0))
54846b7c 1266 return -1;
b8805834
MC
1267 } else if (*ppout == NULL) {
1268 if ((buf = BUF_MEM_new()) == NULL
1269 || !WPACKET_init_len(&pkt, buf, 0)) {
1270 BUF_MEM_free(buf);
54846b7c 1271 return -1;
b8805834 1272 }
54846b7c 1273 } else {
b8805834
MC
1274 if (!WPACKET_init_static_len(&pkt, *ppout, SIZE_MAX, 0))
1275 return -1;
54846b7c 1276 }
b8805834
MC
1277
1278 if (!encode_der_dsa_sig(&pkt, sig->r, sig->s)
1279 || !WPACKET_get_total_written(&pkt, &encoded_len)
1280 || !WPACKET_finish(&pkt)) {
1281 BUF_MEM_free(buf);
1282 WPACKET_cleanup(&pkt);
54846b7c
DM
1283 return -1;
1284 }
b8805834
MC
1285
1286 if (ppout != NULL) {
1287 if (*ppout == NULL) {
1288 *ppout = (unsigned char *)buf->data;
1289 buf->data = NULL;
1290 BUF_MEM_free(buf);
1291 } else {
1292 *ppout += encoded_len;
1293 }
1294 }
1295
54846b7c
DM
1296 return (int)encoded_len;
1297}
1298
9267c11b 1299void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
7236e3c8 1300{
91e7bcc2 1301 if (pr != NULL)
7236e3c8 1302 *pr = sig->r;
91e7bcc2 1303 if (ps != NULL)
7236e3c8
DSH
1304 *ps = sig->s;
1305}
cf517a6d 1306
0396401d
DMSP
1307const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig)
1308{
1309 return sig->r;
1310}
1311
1312const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig)
1313{
1314 return sig->s;
1315}
1316
7ca3ea22 1317int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
6a571a18 1318{
bbaa9dd8
TS
1319 if (r == NULL || s == NULL)
1320 return 0;
6a571a18
TS
1321 BN_clear_free(sig->r);
1322 BN_clear_free(sig->s);
1323 sig->r = r;
1324 sig->s = s;
1325 return 1;
1326}
1327
a9612d6c 1328#ifndef FIPS_MODE
cf517a6d
DSH
1329int ECDSA_size(const EC_KEY *r)
1330{
1331 int ret, i;
1332 ASN1_INTEGER bs;
cf517a6d
DSH
1333 unsigned char buf[4];
1334 const EC_GROUP *group;
1335
1336 if (r == NULL)
1337 return 0;
1338 group = EC_KEY_get0_group(r);
1339 if (group == NULL)
1340 return 0;
1341
be2e334f
DSH
1342 i = EC_GROUP_order_bits(group);
1343 if (i == 0)
cf517a6d 1344 return 0;
cf517a6d
DSH
1345 bs.length = (i + 7) / 8;
1346 bs.data = buf;
1347 bs.type = V_ASN1_INTEGER;
1348 /* If the top bit is set the asn1 encoding is 1 larger. */
1349 buf[0] = 0xff;
1350
1351 i = i2d_ASN1_INTEGER(&bs, NULL);
1352 i += i; /* r and s */
1353 ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE);
26a7d938 1354 return ret;
cf517a6d 1355}
a9612d6c 1356#endif