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