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