Coverage Report

Created: 2025-06-13 06:57

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