Coverage Report

Created: 2025-12-10 06:24

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-2025 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
        ossl_asn1_string_set_bits_left(curve->seed, 0);
352
0
        if (!ASN1_BIT_STRING_set(curve->seed, group->seed,
353
0
                (int)group->seed_len)) {
354
0
            ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
355
0
            goto err;
356
0
        }
357
0
    } else {
358
0
        ASN1_BIT_STRING_free(curve->seed);
359
0
        curve->seed = NULL;
360
0
    }
361
362
0
    ok = 1;
363
364
0
err:
365
0
    OPENSSL_free(a_buf);
366
0
    OPENSSL_free(b_buf);
367
0
    BN_free(tmp_1);
368
0
    BN_free(tmp_2);
369
0
    return ok;
370
0
}
371
372
ECPARAMETERS *EC_GROUP_get_ecparameters(const EC_GROUP *group,
373
    ECPARAMETERS *params)
374
0
{
375
0
    size_t len = 0;
376
0
    ECPARAMETERS *ret = NULL;
377
0
    const BIGNUM *tmp;
378
0
    unsigned char *buffer = NULL;
379
0
    const EC_POINT *point = NULL;
380
0
    point_conversion_form_t form;
381
0
    ASN1_INTEGER *orig;
382
383
0
    if (params == NULL) {
384
0
        if ((ret = ECPARAMETERS_new()) == NULL) {
385
0
            ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
386
0
            goto err;
387
0
        }
388
0
    } else
389
0
        ret = params;
390
391
    /* set the version (always one) */
392
0
    ret->version = (long)0x1;
393
394
    /* set the fieldID */
395
0
    if (!ec_asn1_group2fieldid(group, ret->fieldID)) {
396
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
397
0
        goto err;
398
0
    }
399
400
    /* set the curve */
401
0
    if (!ec_asn1_group2curve(group, ret->curve)) {
402
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
403
0
        goto err;
404
0
    }
405
406
    /* set the base point */
407
0
    if ((point = EC_GROUP_get0_generator(group)) == NULL) {
408
0
        ERR_raise(ERR_LIB_EC, EC_R_UNDEFINED_GENERATOR);
409
0
        goto err;
410
0
    }
411
412
0
    form = EC_GROUP_get_point_conversion_form(group);
413
414
0
    len = EC_POINT_point2buf(group, point, form, &buffer, NULL);
415
0
    if (len == 0 || len > INT_MAX) {
416
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
417
0
        goto err;
418
0
    }
419
0
    if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) {
420
0
        OPENSSL_free(buffer);
421
0
        ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
422
0
        goto err;
423
0
    }
424
0
    ASN1_STRING_set0(ret->base, buffer, (int)len);
425
426
    /* set the order */
427
0
    tmp = EC_GROUP_get0_order(group);
428
0
    if (tmp == NULL) {
429
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
430
0
        goto err;
431
0
    }
432
0
    ret->order = BN_to_ASN1_INTEGER(tmp, orig = ret->order);
433
0
    if (ret->order == NULL) {
434
0
        ret->order = orig;
435
0
        ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
436
0
        goto err;
437
0
    }
438
439
    /* set the cofactor (optional) */
440
0
    tmp = EC_GROUP_get0_cofactor(group);
441
0
    if (tmp != NULL) {
442
0
        ret->cofactor = BN_to_ASN1_INTEGER(tmp, orig = ret->cofactor);
443
0
        if (ret->cofactor == NULL) {
444
0
            ret->cofactor = orig;
445
0
            ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
446
0
            goto err;
447
0
        }
448
0
    }
449
450
0
    return ret;
451
452
0
err:
453
0
    if (params == NULL)
454
0
        ECPARAMETERS_free(ret);
455
0
    return NULL;
456
0
}
457
458
ECPKPARAMETERS *EC_GROUP_get_ecpkparameters(const EC_GROUP *group,
459
    ECPKPARAMETERS *params)
460
0
{
461
0
    int ok = 1, tmp;
462
0
    ECPKPARAMETERS *ret = params;
463
464
0
    if (ret == NULL) {
465
0
        if ((ret = ECPKPARAMETERS_new()) == NULL) {
466
0
            ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
467
0
            return NULL;
468
0
        }
469
0
    } else {
470
0
        if (ret->type == ECPKPARAMETERS_TYPE_NAMED)
471
0
            ASN1_OBJECT_free(ret->value.named_curve);
472
0
        else if (ret->type == ECPKPARAMETERS_TYPE_EXPLICIT
473
0
            && ret->value.parameters != NULL)
474
0
            ECPARAMETERS_free(ret->value.parameters);
475
0
    }
476
477
0
    if (EC_GROUP_get_asn1_flag(group) == OPENSSL_EC_NAMED_CURVE) {
478
        /*
479
         * use the asn1 OID to describe the elliptic curve parameters
480
         */
481
0
        tmp = EC_GROUP_get_curve_name(group);
482
0
        if (tmp) {
483
0
            ASN1_OBJECT *asn1obj = OBJ_nid2obj(tmp);
484
485
0
            if (asn1obj == NULL || OBJ_length(asn1obj) == 0) {
486
0
                ASN1_OBJECT_free(asn1obj);
487
0
                ERR_raise(ERR_LIB_EC, EC_R_MISSING_OID);
488
0
                ok = 0;
489
0
            } else {
490
0
                ret->type = ECPKPARAMETERS_TYPE_NAMED;
491
0
                ret->value.named_curve = asn1obj;
492
0
            }
493
0
        } else
494
            /* we don't know the nid => ERROR */
495
0
            ok = 0;
496
0
    } else {
497
        /* use the ECPARAMETERS structure */
498
0
        ret->type = ECPKPARAMETERS_TYPE_EXPLICIT;
499
0
        if ((ret->value.parameters = EC_GROUP_get_ecparameters(group, NULL)) == NULL)
500
0
            ok = 0;
501
0
    }
502
503
0
    if (!ok) {
504
0
        ECPKPARAMETERS_free(ret);
505
0
        return NULL;
506
0
    }
507
0
    return ret;
508
0
}
509
510
EC_GROUP *EC_GROUP_new_from_ecparameters(const ECPARAMETERS *params)
511
0
{
512
0
    int ok = 0, tmp;
513
0
    EC_GROUP *ret = NULL, *dup = NULL;
514
0
    BIGNUM *p = NULL, *a = NULL, *b = NULL;
515
0
    EC_POINT *point = NULL;
516
0
    long field_bits;
517
0
    int curve_name = NID_undef;
518
0
    BN_CTX *ctx = NULL;
519
520
0
    if (params->fieldID == NULL
521
0
        || params->fieldID->fieldType == NULL
522
0
        || params->fieldID->p.ptr == NULL) {
523
0
        ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR);
524
0
        goto err;
525
0
    }
526
527
    /*
528
     * Now extract the curve parameters a and b. Note that, although SEC 1
529
     * specifies the length of their encodings, historical versions of OpenSSL
530
     * encoded them incorrectly, so we must accept any length for backwards
531
     * compatibility.
532
     */
533
0
    if (params->curve == NULL
534
0
        || params->curve->a == NULL || params->curve->a->data == NULL
535
0
        || params->curve->b == NULL || params->curve->b->data == NULL) {
536
0
        ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR);
537
0
        goto err;
538
0
    }
539
0
    a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);
540
0
    if (a == NULL) {
541
0
        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
542
0
        goto err;
543
0
    }
544
0
    b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
545
0
    if (b == NULL) {
546
0
        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
547
0
        goto err;
548
0
    }
549
550
    /* get the field parameters */
551
0
    tmp = OBJ_obj2nid(params->fieldID->fieldType);
552
0
    if (tmp == NID_X9_62_characteristic_two_field)
553
#ifdef OPENSSL_NO_EC2M
554
    {
555
        ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);
556
        goto err;
557
    }
558
#else
559
0
    {
560
0
        X9_62_CHARACTERISTIC_TWO *char_two;
561
562
0
        char_two = params->fieldID->p.char_two;
563
564
0
        field_bits = char_two->m;
565
0
        if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
566
0
            ERR_raise(ERR_LIB_EC, EC_R_FIELD_TOO_LARGE);
567
0
            goto err;
568
0
        }
569
570
0
        if ((p = BN_new()) == NULL) {
571
0
            ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
572
0
            goto err;
573
0
        }
574
575
        /* get the base type */
576
0
        tmp = OBJ_obj2nid(char_two->type);
577
578
0
        if (tmp == NID_X9_62_tpBasis) {
579
0
            long tmp_long;
580
581
0
            if (!char_two->p.tpBasis) {
582
0
                ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR);
583
0
                goto err;
584
0
            }
585
586
0
            tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis);
587
588
0
            if (!(char_two->m > tmp_long && tmp_long > 0)) {
589
0
                ERR_raise(ERR_LIB_EC, EC_R_INVALID_TRINOMIAL_BASIS);
590
0
                goto err;
591
0
            }
592
593
            /* create the polynomial */
594
0
            if (!BN_set_bit(p, (int)char_two->m))
595
0
                goto err;
596
0
            if (!BN_set_bit(p, (int)tmp_long))
597
0
                goto err;
598
0
            if (!BN_set_bit(p, 0))
599
0
                goto err;
600
0
        } else if (tmp == NID_X9_62_ppBasis) {
601
0
            X9_62_PENTANOMIAL *penta;
602
603
0
            penta = char_two->p.ppBasis;
604
0
            if (penta == NULL) {
605
0
                ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR);
606
0
                goto err;
607
0
            }
608
609
0
            if (!(char_two->m > penta->k3 && penta->k3 > penta->k2
610
0
                    && penta->k2 > penta->k1 && penta->k1 > 0)) {
611
0
                ERR_raise(ERR_LIB_EC, EC_R_INVALID_PENTANOMIAL_BASIS);
612
0
                goto err;
613
0
            }
614
615
            /* create the polynomial */
616
0
            if (!BN_set_bit(p, (int)char_two->m))
617
0
                goto err;
618
0
            if (!BN_set_bit(p, (int)penta->k1))
619
0
                goto err;
620
0
            if (!BN_set_bit(p, (int)penta->k2))
621
0
                goto err;
622
0
            if (!BN_set_bit(p, (int)penta->k3))
623
0
                goto err;
624
0
            if (!BN_set_bit(p, 0))
625
0
                goto err;
626
0
        } else if (tmp == NID_X9_62_onBasis) {
627
0
            ERR_raise(ERR_LIB_EC, EC_R_NOT_IMPLEMENTED);
628
0
            goto err;
629
0
        } else { /* error */
630
631
0
            ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR);
632
0
            goto err;
633
0
        }
634
635
        /* create the EC_GROUP structure */
636
0
        ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
637
0
    }
638
0
#endif
639
0
    else if (tmp == NID_X9_62_prime_field) {
640
        /* we have a curve over a prime field */
641
        /* extract the prime number */
642
0
        if (params->fieldID->p.prime == NULL) {
643
0
            ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR);
644
0
            goto err;
645
0
        }
646
0
        p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);
647
0
        if (p == NULL) {
648
0
            ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
649
0
            goto err;
650
0
        }
651
652
0
        if (BN_is_negative(p) || BN_is_zero(p)) {
653
0
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);
654
0
            goto err;
655
0
        }
656
657
0
        field_bits = BN_num_bits(p);
658
0
        if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
659
0
            ERR_raise(ERR_LIB_EC, EC_R_FIELD_TOO_LARGE);
660
0
            goto err;
661
0
        }
662
663
        /* create the EC_GROUP structure */
664
0
        ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);
665
0
    } else {
666
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);
667
0
        goto err;
668
0
    }
669
670
0
    if (ret == NULL) {
671
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
672
0
        goto err;
673
0
    }
674
675
    /* extract seed (optional) */
676
0
    if (params->curve->seed != NULL) {
677
        /*
678
         * This happens for instance with
679
         * fuzz/corpora/asn1/65cf44e85614c62f10cf3b7a7184c26293a19e4a
680
         * and causes the OPENSSL_malloc below to choke on the
681
         * zero length allocation request.
682
         */
683
0
        if (params->curve->seed->length == 0) {
684
0
            ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR);
685
0
            goto err;
686
0
        }
687
0
        OPENSSL_free(ret->seed);
688
0
        if ((ret->seed = OPENSSL_malloc(params->curve->seed->length)) == NULL)
689
0
            goto err;
690
0
        memcpy(ret->seed, params->curve->seed->data,
691
0
            params->curve->seed->length);
692
0
        ret->seed_len = params->curve->seed->length;
693
0
    }
694
695
0
    if (params->order == NULL
696
0
        || params->base == NULL
697
0
        || params->base->data == NULL
698
0
        || params->base->length == 0) {
699
0
        ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR);
700
0
        goto err;
701
0
    }
702
703
0
    if ((point = EC_POINT_new(ret)) == NULL)
704
0
        goto err;
705
706
    /* set the point conversion form */
707
0
    EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)(params->base->data[0] & ~0x01));
708
709
    /* extract the ec point */
710
0
    if (!EC_POINT_oct2point(ret, point, params->base->data,
711
0
            params->base->length, NULL)) {
712
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
713
0
        goto err;
714
0
    }
715
716
    /* extract the order */
717
0
    if (ASN1_INTEGER_to_BN(params->order, a) == NULL) {
718
0
        ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
719
0
        goto err;
720
0
    }
721
0
    if (BN_is_negative(a) || BN_is_zero(a)) {
722
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);
723
0
        goto err;
724
0
    }
725
0
    if (BN_num_bits(a) > (int)field_bits + 1) { /* Hasse bound */
726
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);
727
0
        goto err;
728
0
    }
729
730
    /* extract the cofactor (optional) */
731
0
    if (params->cofactor == NULL) {
732
0
        BN_free(b);
733
0
        b = NULL;
734
0
    } else if (ASN1_INTEGER_to_BN(params->cofactor, b) == NULL) {
735
0
        ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
736
0
        goto err;
737
0
    }
738
    /* set the generator, order and cofactor (if present) */
739
0
    if (!EC_GROUP_set_generator(ret, point, a, b)) {
740
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
741
0
        goto err;
742
0
    }
743
744
    /*
745
     * Check if the explicit parameters group just created matches one of the
746
     * built-in curves.
747
     *
748
     * We create a copy of the group just built, so that we can remove optional
749
     * fields for the lookup: we do this to avoid the possibility that one of
750
     * the optional parameters is used to force the library into using a less
751
     * performant and less secure EC_METHOD instead of the specialized one.
752
     * In any case, `seed` is not really used in any computation, while a
753
     * cofactor different from the one in the built-in table is just
754
     * mathematically wrong anyway and should not be used.
755
     */
756
0
    if ((ctx = BN_CTX_new()) == NULL) {
757
0
        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
758
0
        goto err;
759
0
    }
760
0
    if ((dup = EC_GROUP_dup(ret)) == NULL
761
0
        || EC_GROUP_set_seed(dup, NULL, 0) != 1
762
0
        || !EC_GROUP_set_generator(dup, point, a, NULL)) {
763
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
764
0
        goto err;
765
0
    }
766
0
    if ((curve_name = ossl_ec_curve_nid_from_params(dup, ctx)) != NID_undef) {
767
        /*
768
         * The input explicit parameters successfully matched one of the
769
         * built-in curves: often for built-in curves we have specialized
770
         * methods with better performance and hardening.
771
         *
772
         * In this case we replace the `EC_GROUP` created through explicit
773
         * parameters with one created from a named group.
774
         */
775
0
        EC_GROUP *named_group = NULL;
776
777
#ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
778
        /*
779
         * NID_wap_wsg_idm_ecid_wtls12 and NID_secp224r1 are both aliases for
780
         * the same curve, we prefer the SECP nid when matching explicit
781
         * parameters as that is associated with a specialized EC_METHOD.
782
         */
783
        if (curve_name == NID_wap_wsg_idm_ecid_wtls12)
784
            curve_name = NID_secp224r1;
785
#endif /* !def(OPENSSL_NO_EC_NISTP_64_GCC_128) */
786
787
0
        if ((named_group = EC_GROUP_new_by_curve_name(curve_name)) == NULL) {
788
0
            ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
789
0
            goto err;
790
0
        }
791
0
        EC_GROUP_free(ret);
792
0
        ret = named_group;
793
794
        /*
795
         * Set the flag so that EC_GROUPs created from explicit parameters are
796
         * serialized using explicit parameters by default.
797
         */
798
0
        EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_EXPLICIT_CURVE);
799
800
        /*
801
         * If the input params do not contain the optional seed field we make
802
         * sure it is not added to the returned group.
803
         *
804
         * The seed field is not really used inside libcrypto anyway, and
805
         * adding it to parsed explicit parameter keys would alter their DER
806
         * encoding output (because of the extra field) which could impact
807
         * applications fingerprinting keys by their DER encoding.
808
         */
809
0
        if (params->curve->seed == NULL) {
810
0
            if (EC_GROUP_set_seed(ret, NULL, 0) != 1)
811
0
                goto err;
812
0
        }
813
0
    }
814
815
0
    ok = 1;
816
817
0
err:
818
0
    if (!ok) {
819
0
        EC_GROUP_free(ret);
820
0
        ret = NULL;
821
0
    }
822
0
    EC_GROUP_free(dup);
823
824
0
    BN_free(p);
825
0
    BN_free(a);
826
0
    BN_free(b);
827
0
    EC_POINT_free(point);
828
829
0
    BN_CTX_free(ctx);
830
831
0
    return ret;
832
0
}
833
834
EC_GROUP *EC_GROUP_new_from_ecpkparameters(const ECPKPARAMETERS *params)
835
0
{
836
0
    EC_GROUP *ret = NULL;
837
0
    int tmp = 0;
838
839
0
    if (params == NULL) {
840
0
        ERR_raise(ERR_LIB_EC, EC_R_MISSING_PARAMETERS);
841
0
        return NULL;
842
0
    }
843
844
0
    if (params->type == ECPKPARAMETERS_TYPE_NAMED) {
845
        /* the curve is given by an OID */
846
0
        tmp = OBJ_obj2nid(params->value.named_curve);
847
0
        if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) {
848
0
            ERR_raise(ERR_LIB_EC, EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
849
0
            return NULL;
850
0
        }
851
0
        EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);
852
0
    } else if (params->type == ECPKPARAMETERS_TYPE_EXPLICIT) {
853
        /* the parameters are given by an ECPARAMETERS structure */
854
0
        ret = EC_GROUP_new_from_ecparameters(params->value.parameters);
855
0
        if (!ret) {
856
0
            ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
857
0
            return NULL;
858
0
        }
859
0
        EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_EXPLICIT_CURVE);
860
0
    } else if (params->type == ECPKPARAMETERS_TYPE_IMPLICIT) {
861
        /* implicit parameters inherited from CA - unsupported */
862
0
        return NULL;
863
0
    } else {
864
0
        ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR);
865
0
        return NULL;
866
0
    }
867
868
0
    return ret;
869
0
}
870
871
/* EC_GROUP <-> DER encoding of ECPKPARAMETERS */
872
873
EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len)
874
0
{
875
0
    EC_GROUP *group = NULL;
876
0
    ECPKPARAMETERS *params = NULL;
877
0
    const unsigned char *p = *in;
878
879
0
    if ((params = d2i_ECPKPARAMETERS(NULL, &p, len)) == NULL) {
880
0
        ECPKPARAMETERS_free(params);
881
0
        return NULL;
882
0
    }
883
884
0
    if ((group = EC_GROUP_new_from_ecpkparameters(params)) == NULL) {
885
0
        ECPKPARAMETERS_free(params);
886
0
        return NULL;
887
0
    }
888
889
0
    if (params->type == ECPKPARAMETERS_TYPE_EXPLICIT)
890
0
        group->decoded_from_explicit_params = 1;
891
892
0
    if (a) {
893
0
        EC_GROUP_free(*a);
894
0
        *a = group;
895
0
    }
896
897
0
    ECPKPARAMETERS_free(params);
898
0
    *in = p;
899
0
    return group;
900
0
}
901
902
int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out)
903
0
{
904
0
    int ret = 0;
905
0
    ECPKPARAMETERS *tmp = EC_GROUP_get_ecpkparameters(a, NULL);
906
0
    if (tmp == NULL) {
907
0
        ERR_raise(ERR_LIB_EC, EC_R_GROUP2PKPARAMETERS_FAILURE);
908
0
        return 0;
909
0
    }
910
0
    if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) {
911
0
        ERR_raise(ERR_LIB_EC, EC_R_I2D_ECPKPARAMETERS_FAILURE);
912
0
        ECPKPARAMETERS_free(tmp);
913
0
        return 0;
914
0
    }
915
0
    ECPKPARAMETERS_free(tmp);
916
0
    return ret;
917
0
}
918
919
/* some EC_KEY functions */
920
921
EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len)
922
0
{
923
0
    EC_KEY *ret = NULL;
924
0
    EC_PRIVATEKEY *priv_key = NULL;
925
0
    const unsigned char *p = *in;
926
927
0
    if ((priv_key = d2i_EC_PRIVATEKEY(NULL, &p, len)) == NULL)
928
0
        return NULL;
929
930
0
    if (a == NULL || *a == NULL) {
931
0
        if ((ret = EC_KEY_new()) == NULL) {
932
0
            ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
933
0
            goto err;
934
0
        }
935
0
    } else
936
0
        ret = *a;
937
938
0
    if (priv_key->parameters) {
939
0
        EC_GROUP_free(ret->group);
940
0
        ret->group = EC_GROUP_new_from_ecpkparameters(priv_key->parameters);
941
0
        if (ret->group != NULL
942
0
            && priv_key->parameters->type == ECPKPARAMETERS_TYPE_EXPLICIT)
943
0
            ret->group->decoded_from_explicit_params = 1;
944
0
    }
945
946
0
    if (ret->group == NULL) {
947
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
948
0
        goto err;
949
0
    }
950
951
0
    ret->version = priv_key->version;
952
953
0
    if (priv_key->privateKey) {
954
0
        ASN1_OCTET_STRING *pkey = priv_key->privateKey;
955
0
        if (EC_KEY_oct2priv(ret, ASN1_STRING_get0_data(pkey),
956
0
                ASN1_STRING_length(pkey))
957
0
            == 0)
958
0
            goto err;
959
0
    } else {
960
0
        ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY);
961
0
        goto err;
962
0
    }
963
964
0
    if (EC_GROUP_get_curve_name(ret->group) == NID_sm2)
965
0
        EC_KEY_set_flags(ret, EC_FLAG_SM2_RANGE);
966
967
0
    EC_POINT_clear_free(ret->pub_key);
968
0
    ret->pub_key = EC_POINT_new(ret->group);
969
0
    if (ret->pub_key == NULL) {
970
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
971
0
        goto err;
972
0
    }
973
974
0
    if (priv_key->publicKey) {
975
0
        const unsigned char *pub_oct;
976
0
        int pub_oct_len;
977
978
0
        pub_oct = ASN1_STRING_get0_data(priv_key->publicKey);
979
0
        pub_oct_len = ASN1_STRING_length(priv_key->publicKey);
980
0
        if (!EC_KEY_oct2key(ret, pub_oct, pub_oct_len, NULL)) {
981
0
            ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
982
0
            goto err;
983
0
        }
984
0
    } else {
985
0
        if (ret->group->meth->keygenpub == NULL
986
0
            || ret->group->meth->keygenpub(ret) == 0)
987
0
            goto err;
988
        /* Remember the original private-key-only encoding. */
989
0
        ret->enc_flag |= EC_PKEY_NO_PUBKEY;
990
0
    }
991
992
0
    if (a)
993
0
        *a = ret;
994
0
    EC_PRIVATEKEY_free(priv_key);
995
0
    *in = p;
996
0
    ret->dirty_cnt++;
997
0
    return ret;
998
999
0
err:
1000
0
    if (a == NULL || *a != ret)
1001
0
        EC_KEY_free(ret);
1002
0
    EC_PRIVATEKEY_free(priv_key);
1003
0
    return NULL;
1004
0
}
1005
1006
int i2d_ECPrivateKey(const EC_KEY *a, unsigned char **out)
1007
0
{
1008
0
    int ret = 0, ok = 0;
1009
0
    unsigned char *priv = NULL, *pub = NULL;
1010
0
    size_t privlen = 0, publen = 0;
1011
1012
0
    EC_PRIVATEKEY *priv_key = NULL;
1013
1014
0
    if (a == NULL || a->group == NULL || (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key == NULL)) {
1015
0
        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
1016
0
        goto err;
1017
0
    }
1018
1019
0
    if ((priv_key = EC_PRIVATEKEY_new()) == NULL) {
1020
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
1021
0
        goto err;
1022
0
    }
1023
1024
0
    priv_key->version = a->version;
1025
1026
0
    privlen = EC_KEY_priv2buf(a, &priv);
1027
1028
0
    if (privlen == 0 || privlen > INT_MAX) {
1029
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
1030
0
        goto err;
1031
0
    }
1032
1033
0
    ASN1_STRING_set0(priv_key->privateKey, priv, (int)privlen);
1034
0
    priv = NULL;
1035
1036
0
    if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) {
1037
0
        if ((priv_key->parameters = EC_GROUP_get_ecpkparameters(a->group,
1038
0
                 priv_key->parameters))
1039
0
            == NULL) {
1040
0
            ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
1041
0
            goto err;
1042
0
        }
1043
0
    }
1044
1045
0
    if (!(a->enc_flag & EC_PKEY_NO_PUBKEY)) {
1046
0
        priv_key->publicKey = ASN1_BIT_STRING_new();
1047
0
        if (priv_key->publicKey == NULL) {
1048
0
            ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
1049
0
            goto err;
1050
0
        }
1051
1052
0
        publen = EC_KEY_key2buf(a, a->conv_form, &pub, NULL);
1053
1054
0
        if (publen == 0 || publen > INT_MAX) {
1055
0
            ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
1056
0
            goto err;
1057
0
        }
1058
1059
0
        ossl_asn1_string_set_bits_left(priv_key->publicKey, 0);
1060
0
        ASN1_STRING_set0(priv_key->publicKey, pub, (int)publen);
1061
0
        pub = NULL;
1062
0
    }
1063
1064
0
    if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) {
1065
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
1066
0
        goto err;
1067
0
    }
1068
0
    ok = 1;
1069
0
err:
1070
0
    OPENSSL_clear_free(priv, privlen);
1071
0
    OPENSSL_free(pub);
1072
0
    EC_PRIVATEKEY_free(priv_key);
1073
0
    return (ok ? ret : 0);
1074
0
}
1075
1076
int i2d_ECParameters(const EC_KEY *a, unsigned char **out)
1077
0
{
1078
0
    if (a == NULL) {
1079
0
        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
1080
0
        return 0;
1081
0
    }
1082
0
    return i2d_ECPKParameters(a->group, out);
1083
0
}
1084
1085
EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len)
1086
0
{
1087
0
    EC_KEY *ret;
1088
1089
0
    if (in == NULL || *in == NULL) {
1090
0
        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
1091
0
        return NULL;
1092
0
    }
1093
1094
0
    if (a == NULL || *a == NULL) {
1095
0
        if ((ret = EC_KEY_new()) == NULL) {
1096
0
            ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
1097
0
            return NULL;
1098
0
        }
1099
0
    } else
1100
0
        ret = *a;
1101
1102
0
    if (!d2i_ECPKParameters(&ret->group, in, len)) {
1103
0
        if (a == NULL || *a != ret)
1104
0
            EC_KEY_free(ret);
1105
0
        else
1106
0
            ret->dirty_cnt++;
1107
0
        return NULL;
1108
0
    }
1109
1110
0
    if (EC_GROUP_get_curve_name(ret->group) == NID_sm2)
1111
0
        EC_KEY_set_flags(ret, EC_FLAG_SM2_RANGE);
1112
1113
0
    ret->dirty_cnt++;
1114
1115
0
    if (a)
1116
0
        *a = ret;
1117
1118
0
    return ret;
1119
0
}
1120
1121
EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len)
1122
0
{
1123
0
    EC_KEY *ret = NULL;
1124
1125
0
    if (a == NULL || (*a) == NULL || (*a)->group == NULL) {
1126
        /*
1127
         * sorry, but a EC_GROUP-structure is necessary to set the public key
1128
         */
1129
0
        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
1130
0
        return 0;
1131
0
    }
1132
0
    ret = *a;
1133
    /* EC_KEY_opt2key updates dirty_cnt */
1134
0
    if (!EC_KEY_oct2key(ret, *in, len, NULL)) {
1135
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
1136
0
        return 0;
1137
0
    }
1138
0
    *in += len;
1139
0
    return ret;
1140
0
}
1141
1142
int i2o_ECPublicKey(const EC_KEY *a, unsigned char **out)
1143
0
{
1144
0
    size_t buf_len = 0;
1145
0
    int new_buffer = 0;
1146
1147
0
    if (a == NULL || a->pub_key == NULL) {
1148
0
        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
1149
0
        return 0;
1150
0
    }
1151
1152
0
    buf_len = EC_POINT_point2oct(a->group, a->pub_key,
1153
0
        a->conv_form, NULL, 0, NULL);
1154
1155
0
    if (buf_len > INT_MAX) {
1156
0
        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_INVALID_ARGUMENT);
1157
0
        return 0;
1158
0
    }
1159
0
    if (out == NULL || buf_len == 0)
1160
        /* out == NULL => just return the length of the octet string */
1161
0
        return (int)buf_len;
1162
1163
0
    if (*out == NULL) {
1164
0
        if ((*out = OPENSSL_malloc(buf_len)) == NULL)
1165
0
            return 0;
1166
0
        new_buffer = 1;
1167
0
    }
1168
0
    if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form,
1169
0
            *out, buf_len, NULL)) {
1170
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
1171
0
        if (new_buffer) {
1172
0
            OPENSSL_free(*out);
1173
0
            *out = NULL;
1174
0
        }
1175
0
        return 0;
1176
0
    }
1177
0
    if (!new_buffer)
1178
0
        *out += buf_len;
1179
0
    return (int)buf_len;
1180
0
}
1181
1182
DECLARE_ASN1_FUNCTIONS(ECDSA_SIG)
1183
DECLARE_ASN1_ENCODE_FUNCTIONS_name(ECDSA_SIG, ECDSA_SIG)
1184
1185
#endif /* FIPS_MODULE */
1186
1187
ECDSA_SIG *ECDSA_SIG_new(void)
1188
0
{
1189
0
    ECDSA_SIG *sig = OPENSSL_zalloc(sizeof(*sig));
1190
1191
0
    return sig;
1192
0
}
1193
1194
void ECDSA_SIG_free(ECDSA_SIG *sig)
1195
0
{
1196
0
    if (sig == NULL)
1197
0
        return;
1198
0
    BN_clear_free(sig->r);
1199
0
    BN_clear_free(sig->s);
1200
0
    OPENSSL_free(sig);
1201
0
}
1202
1203
ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **psig, const unsigned char **ppin, long len)
1204
0
{
1205
0
    ECDSA_SIG *sig;
1206
1207
0
    if (len < 0)
1208
0
        return NULL;
1209
0
    if (psig != NULL && *psig != NULL) {
1210
0
        sig = *psig;
1211
0
    } else {
1212
0
        sig = ECDSA_SIG_new();
1213
0
        if (sig == NULL)
1214
0
            return NULL;
1215
0
    }
1216
0
    if (sig->r == NULL)
1217
0
        sig->r = BN_new();
1218
0
    if (sig->s == NULL)
1219
0
        sig->s = BN_new();
1220
0
    if (sig->r == NULL || sig->s == NULL
1221
0
        || ossl_decode_der_dsa_sig(sig->r, sig->s, ppin, (size_t)len) == 0) {
1222
0
        if (psig == NULL || *psig == NULL)
1223
0
            ECDSA_SIG_free(sig);
1224
0
        return NULL;
1225
0
    }
1226
0
    if (psig != NULL && *psig == NULL)
1227
0
        *psig = sig;
1228
0
    return sig;
1229
0
}
1230
1231
int i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **ppout)
1232
0
{
1233
0
    BUF_MEM *buf = NULL;
1234
0
    size_t encoded_len;
1235
0
    WPACKET pkt;
1236
1237
0
    if (ppout == NULL) {
1238
0
        if (!WPACKET_init_null(&pkt, 0))
1239
0
            return -1;
1240
0
    } else if (*ppout == NULL) {
1241
0
        if ((buf = BUF_MEM_new()) == NULL
1242
0
            || !WPACKET_init_len(&pkt, buf, 0)) {
1243
0
            BUF_MEM_free(buf);
1244
0
            return -1;
1245
0
        }
1246
0
    } else {
1247
0
        if (!WPACKET_init_static_len(&pkt, *ppout, SIZE_MAX, 0))
1248
0
            return -1;
1249
0
    }
1250
1251
0
    if (!ossl_encode_der_dsa_sig(&pkt, sig->r, sig->s)
1252
0
        || !WPACKET_get_total_written(&pkt, &encoded_len)
1253
0
        || !WPACKET_finish(&pkt)) {
1254
0
        BUF_MEM_free(buf);
1255
0
        WPACKET_cleanup(&pkt);
1256
0
        return -1;
1257
0
    }
1258
1259
0
    if (ppout != NULL) {
1260
0
        if (*ppout == NULL) {
1261
0
            *ppout = (unsigned char *)buf->data;
1262
0
            buf->data = NULL;
1263
0
            BUF_MEM_free(buf);
1264
0
        } else {
1265
0
            *ppout += encoded_len;
1266
0
        }
1267
0
    }
1268
1269
0
    return (int)encoded_len;
1270
0
}
1271
1272
void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
1273
0
{
1274
0
    if (pr != NULL)
1275
0
        *pr = sig->r;
1276
0
    if (ps != NULL)
1277
0
        *ps = sig->s;
1278
0
}
1279
1280
const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig)
1281
0
{
1282
0
    return sig->r;
1283
0
}
1284
1285
const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig)
1286
0
{
1287
0
    return sig->s;
1288
0
}
1289
1290
int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
1291
0
{
1292
0
    if (r == NULL || s == NULL)
1293
0
        return 0;
1294
0
    BN_clear_free(sig->r);
1295
0
    BN_clear_free(sig->s);
1296
0
    sig->r = r;
1297
0
    sig->s = s;
1298
0
    return 1;
1299
0
}
1300
1301
int ECDSA_size(const EC_KEY *ec)
1302
0
{
1303
0
    int ret;
1304
0
    ECDSA_SIG sig;
1305
0
    const EC_GROUP *group;
1306
0
    const BIGNUM *bn;
1307
1308
0
    if (ec == NULL)
1309
0
        return 0;
1310
0
    group = EC_KEY_get0_group(ec);
1311
0
    if (group == NULL)
1312
0
        return 0;
1313
1314
0
    bn = EC_GROUP_get0_order(group);
1315
0
    if (bn == NULL)
1316
0
        return 0;
1317
1318
0
    sig.r = sig.s = (BIGNUM *)bn;
1319
0
    ret = i2d_ECDSA_SIG(&sig, NULL);
1320
1321
0
    if (ret < 0)
1322
0
        ret = 0;
1323
0
    return ret;
1324
0
}