Coverage Report

Created: 2026-04-28 06:29

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