Coverage Report

Created: 2022-11-30 06:20

/src/openssl/crypto/ec/ec_asn1.c
Line
Count
Source (jump to first uncovered line)
1
/* crypto/ec/ec_asn1.c */
2
/*
3
 * Written by Nils Larsch for the OpenSSL project.
4
 */
5
/* ====================================================================
6
 * Copyright (c) 2000-2003 The OpenSSL Project.  All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 *
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 *
15
 * 2. Redistributions in binary form must reproduce the above copyright
16
 *    notice, this list of conditions and the following disclaimer in
17
 *    the documentation and/or other materials provided with the
18
 *    distribution.
19
 *
20
 * 3. All advertising materials mentioning features or use of this
21
 *    software must display the following acknowledgment:
22
 *    "This product includes software developed by the OpenSSL Project
23
 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24
 *
25
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26
 *    endorse or promote products derived from this software without
27
 *    prior written permission. For written permission, please contact
28
 *    licensing@OpenSSL.org.
29
 *
30
 * 5. Products derived from this software may not be called "OpenSSL"
31
 *    nor may "OpenSSL" appear in their names without prior written
32
 *    permission of the OpenSSL Project.
33
 *
34
 * 6. Redistributions of any form whatsoever must retain the following
35
 *    acknowledgment:
36
 *    "This product includes software developed by the OpenSSL Project
37
 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38
 *
39
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50
 * OF THE POSSIBILITY OF SUCH DAMAGE.
51
 * ====================================================================
52
 *
53
 * This product includes cryptographic software written by Eric Young
54
 * (eay@cryptsoft.com).  This product includes software written by Tim
55
 * Hudson (tjh@cryptsoft.com).
56
 *
57
 */
58
59
#include <string.h>
60
#include "ec_lcl.h"
61
#include <openssl/err.h>
62
#include <openssl/asn1t.h>
63
#include <openssl/objects.h>
64
65
0
#define OSSL_NELEM(x)    (sizeof(x)/sizeof(x[0]))
66
67
int EC_GROUP_get_basis_type(const EC_GROUP *group)
68
0
{
69
0
    int i;
70
71
0
    if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
72
0
        NID_X9_62_characteristic_two_field)
73
        /* everything else is currently not supported */
74
0
        return 0;
75
76
    /* Find the last non-zero element of group->poly[] */
77
0
    for (i = 0;
78
0
         i < (int)OSSL_NELEM(group->poly) && group->poly[i] != 0;
79
0
         i++)
80
0
        continue;
81
82
0
    if (i == 4)
83
0
        return NID_X9_62_ppBasis;
84
0
    else if (i == 2)
85
0
        return NID_X9_62_tpBasis;
86
0
    else
87
        /* everything else is currently not supported */
88
0
        return 0;
89
0
}
90
91
#ifndef OPENSSL_NO_EC2M
92
int EC_GROUP_get_trinomial_basis(const EC_GROUP *group, unsigned int *k)
93
0
{
94
0
    if (group == NULL)
95
0
        return 0;
96
97
0
    if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
98
0
        NID_X9_62_characteristic_two_field
99
0
        || !((group->poly[0] != 0) && (group->poly[1] != 0)
100
0
             && (group->poly[2] == 0))) {
101
0
        ECerr(EC_F_EC_GROUP_GET_TRINOMIAL_BASIS,
102
0
              ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
103
0
        return 0;
104
0
    }
105
106
0
    if (k)
107
0
        *k = group->poly[1];
108
109
0
    return 1;
110
0
}
111
112
int EC_GROUP_get_pentanomial_basis(const EC_GROUP *group, unsigned int *k1,
113
                                   unsigned int *k2, unsigned int *k3)
114
0
{
115
0
    if (group == NULL)
116
0
        return 0;
117
118
0
    if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
119
0
        NID_X9_62_characteristic_two_field
120
0
        || !((group->poly[0] != 0) && (group->poly[1] != 0)
121
0
             && (group->poly[2] != 0) && (group->poly[3] != 0)
122
0
             && (group->poly[4] == 0))) {
123
0
        ECerr(EC_F_EC_GROUP_GET_PENTANOMIAL_BASIS,
124
0
              ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
125
0
        return 0;
126
0
    }
127
128
0
    if (k1)
129
0
        *k1 = group->poly[3];
130
0
    if (k2)
131
0
        *k2 = group->poly[2];
132
0
    if (k3)
133
0
        *k3 = group->poly[1];
134
135
0
    return 1;
136
0
}
137
#endif
138
139
/* some structures needed for the asn1 encoding */
140
typedef struct x9_62_pentanomial_st {
141
    long k1;
142
    long k2;
143
    long k3;
144
} X9_62_PENTANOMIAL;
145
146
typedef struct x9_62_characteristic_two_st {
147
    long m;
148
    ASN1_OBJECT *type;
149
    union {
150
        char *ptr;
151
        /* NID_X9_62_onBasis */
152
        ASN1_NULL *onBasis;
153
        /* NID_X9_62_tpBasis */
154
        ASN1_INTEGER *tpBasis;
155
        /* NID_X9_62_ppBasis */
156
        X9_62_PENTANOMIAL *ppBasis;
157
        /* anything else */
158
        ASN1_TYPE *other;
159
    } p;
160
} X9_62_CHARACTERISTIC_TWO;
161
162
typedef struct x9_62_fieldid_st {
163
    ASN1_OBJECT *fieldType;
164
    union {
165
        char *ptr;
166
        /* NID_X9_62_prime_field */
167
        ASN1_INTEGER *prime;
168
        /* NID_X9_62_characteristic_two_field */
169
        X9_62_CHARACTERISTIC_TWO *char_two;
170
        /* anything else */
171
        ASN1_TYPE *other;
172
    } p;
173
} X9_62_FIELDID;
174
175
typedef struct x9_62_curve_st {
176
    ASN1_OCTET_STRING *a;
177
    ASN1_OCTET_STRING *b;
178
    ASN1_BIT_STRING *seed;
179
} X9_62_CURVE;
180
181
typedef struct ec_parameters_st {
182
    long version;
183
    X9_62_FIELDID *fieldID;
184
    X9_62_CURVE *curve;
185
    ASN1_OCTET_STRING *base;
186
    ASN1_INTEGER *order;
187
    ASN1_INTEGER *cofactor;
188
} ECPARAMETERS;
189
190
struct ecpk_parameters_st {
191
    int type;
192
    union {
193
        ASN1_OBJECT *named_curve;
194
        ECPARAMETERS *parameters;
195
        ASN1_NULL *implicitlyCA;
196
    } value;
197
} /* ECPKPARAMETERS */ ;
198
199
/* SEC1 ECPrivateKey */
200
typedef struct ec_privatekey_st {
201
    long version;
202
    ASN1_OCTET_STRING *privateKey;
203
    ECPKPARAMETERS *parameters;
204
    ASN1_BIT_STRING *publicKey;
205
} EC_PRIVATEKEY;
206
207
/* the OpenSSL ASN.1 definitions */
208
ASN1_SEQUENCE(X9_62_PENTANOMIAL) = {
209
        ASN1_SIMPLE(X9_62_PENTANOMIAL, k1, LONG),
210
        ASN1_SIMPLE(X9_62_PENTANOMIAL, k2, LONG),
211
        ASN1_SIMPLE(X9_62_PENTANOMIAL, k3, LONG)
212
} ASN1_SEQUENCE_END(X9_62_PENTANOMIAL)
213
214
DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
215
IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
216
217
ASN1_ADB_TEMPLATE(char_two_def) = ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.other, ASN1_ANY);
218
219
ASN1_ADB(X9_62_CHARACTERISTIC_TWO) = {
220
        ADB_ENTRY(NID_X9_62_onBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.onBasis, ASN1_NULL)),
221
        ADB_ENTRY(NID_X9_62_tpBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.tpBasis, ASN1_INTEGER)),
222
        ADB_ENTRY(NID_X9_62_ppBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.ppBasis, X9_62_PENTANOMIAL))
223
} ASN1_ADB_END(X9_62_CHARACTERISTIC_TWO, 0, type, 0, &char_two_def_tt, NULL);
224
225
ASN1_SEQUENCE(X9_62_CHARACTERISTIC_TWO) = {
226
        ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, m, LONG),
227
        ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, type, ASN1_OBJECT),
228
        ASN1_ADB_OBJECT(X9_62_CHARACTERISTIC_TWO)
229
} ASN1_SEQUENCE_END(X9_62_CHARACTERISTIC_TWO)
230
231
DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
232
IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
233
234
ASN1_ADB_TEMPLATE(fieldID_def) = ASN1_SIMPLE(X9_62_FIELDID, p.other, ASN1_ANY);
235
236
ASN1_ADB(X9_62_FIELDID) = {
237
        ADB_ENTRY(NID_X9_62_prime_field, ASN1_SIMPLE(X9_62_FIELDID, p.prime, ASN1_INTEGER)),
238
        ADB_ENTRY(NID_X9_62_characteristic_two_field, ASN1_SIMPLE(X9_62_FIELDID, p.char_two, X9_62_CHARACTERISTIC_TWO))
239
} ASN1_ADB_END(X9_62_FIELDID, 0, fieldType, 0, &fieldID_def_tt, NULL);
240
241
ASN1_SEQUENCE(X9_62_FIELDID) = {
242
        ASN1_SIMPLE(X9_62_FIELDID, fieldType, ASN1_OBJECT),
243
        ASN1_ADB_OBJECT(X9_62_FIELDID)
244
} ASN1_SEQUENCE_END(X9_62_FIELDID)
245
246
ASN1_SEQUENCE(X9_62_CURVE) = {
247
        ASN1_SIMPLE(X9_62_CURVE, a, ASN1_OCTET_STRING),
248
        ASN1_SIMPLE(X9_62_CURVE, b, ASN1_OCTET_STRING),
249
        ASN1_OPT(X9_62_CURVE, seed, ASN1_BIT_STRING)
250
} ASN1_SEQUENCE_END(X9_62_CURVE)
251
252
ASN1_SEQUENCE(ECPARAMETERS) = {
253
        ASN1_SIMPLE(ECPARAMETERS, version, LONG),
254
        ASN1_SIMPLE(ECPARAMETERS, fieldID, X9_62_FIELDID),
255
        ASN1_SIMPLE(ECPARAMETERS, curve, X9_62_CURVE),
256
        ASN1_SIMPLE(ECPARAMETERS, base, ASN1_OCTET_STRING),
257
        ASN1_SIMPLE(ECPARAMETERS, order, ASN1_INTEGER),
258
        ASN1_OPT(ECPARAMETERS, cofactor, ASN1_INTEGER)
259
} ASN1_SEQUENCE_END(ECPARAMETERS)
260
261
DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
262
IMPLEMENT_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
263
264
ASN1_CHOICE(ECPKPARAMETERS) = {
265
        ASN1_SIMPLE(ECPKPARAMETERS, value.named_curve, ASN1_OBJECT),
266
        ASN1_SIMPLE(ECPKPARAMETERS, value.parameters, ECPARAMETERS),
267
        ASN1_SIMPLE(ECPKPARAMETERS, value.implicitlyCA, ASN1_NULL)
268
} ASN1_CHOICE_END(ECPKPARAMETERS)
269
270
DECLARE_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
271
DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECPKPARAMETERS, ECPKPARAMETERS)
272
IMPLEMENT_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
273
274
ASN1_SEQUENCE(EC_PRIVATEKEY) = {
275
        ASN1_SIMPLE(EC_PRIVATEKEY, version, LONG),
276
        ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING),
277
        ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0),
278
        ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1)
279
} ASN1_SEQUENCE_END(EC_PRIVATEKEY)
280
281
DECLARE_ASN1_FUNCTIONS_const(EC_PRIVATEKEY)
282
DECLARE_ASN1_ENCODE_FUNCTIONS_const(EC_PRIVATEKEY, EC_PRIVATEKEY)
283
IMPLEMENT_ASN1_FUNCTIONS_const(EC_PRIVATEKEY)
284
285
/* some declarations of internal function */
286
287
/* ec_asn1_group2field() sets the values in a X9_62_FIELDID object */
288
static int ec_asn1_group2fieldid(const EC_GROUP *, X9_62_FIELDID *);
289
/* ec_asn1_group2curve() sets the values in a X9_62_CURVE object */
290
static int ec_asn1_group2curve(const EC_GROUP *, X9_62_CURVE *);
291
/*
292
 * ec_asn1_parameters2group() creates a EC_GROUP object from a ECPARAMETERS
293
 * object
294
 */
295
static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *);
296
/*
297
 * ec_asn1_group2parameters() creates a ECPARAMETERS object from a EC_GROUP
298
 * object
299
 */
300
static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *,
301
                                              ECPARAMETERS *);
302
/*
303
 * ec_asn1_pkparameters2group() creates a EC_GROUP object from a
304
 * ECPKPARAMETERS object
305
 */
306
static EC_GROUP *ec_asn1_pkparameters2group(const ECPKPARAMETERS *);
307
/*
308
 * ec_asn1_group2pkparameters() creates a ECPKPARAMETERS object from a
309
 * EC_GROUP object
310
 */
311
static ECPKPARAMETERS *ec_asn1_group2pkparameters(const EC_GROUP *,
312
                                                  ECPKPARAMETERS *);
313
314
/* the function definitions */
315
316
static int ec_asn1_group2fieldid(const EC_GROUP *group, X9_62_FIELDID *field)
317
0
{
318
0
    int ok = 0, nid;
319
0
    BIGNUM *tmp = NULL;
320
321
0
    if (group == NULL || field == NULL)
322
0
        return 0;
323
324
    /* clear the old values (if necessary) */
325
0
    if (field->fieldType != NULL)
326
0
        ASN1_OBJECT_free(field->fieldType);
327
0
    if (field->p.other != NULL)
328
0
        ASN1_TYPE_free(field->p.other);
329
330
0
    nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
331
    /* set OID for the field */
332
0
    if ((field->fieldType = OBJ_nid2obj(nid)) == NULL) {
333
0
        ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
334
0
        goto err;
335
0
    }
336
337
0
    if (nid == NID_X9_62_prime_field) {
338
0
        if ((tmp = BN_new()) == NULL) {
339
0
            ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
340
0
            goto err;
341
0
        }
342
        /* the parameters are specified by the prime number p */
343
0
        if (!EC_GROUP_get_curve_GFp(group, tmp, NULL, NULL, NULL)) {
344
0
            ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
345
0
            goto err;
346
0
        }
347
        /* set the prime number */
348
0
        field->p.prime = BN_to_ASN1_INTEGER(tmp, NULL);
349
0
        if (field->p.prime == NULL) {
350
0
            ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
351
0
            goto err;
352
0
        }
353
0
    } else                      /* nid == NID_X9_62_characteristic_two_field */
354
#ifdef OPENSSL_NO_EC2M
355
    {
356
        ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_GF2M_NOT_SUPPORTED);
357
        goto err;
358
    }
359
#else
360
0
    {
361
0
        int field_type;
362
0
        X9_62_CHARACTERISTIC_TWO *char_two;
363
364
0
        field->p.char_two = X9_62_CHARACTERISTIC_TWO_new();
365
0
        char_two = field->p.char_two;
366
367
0
        if (char_two == NULL) {
368
0
            ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
369
0
            goto err;
370
0
        }
371
372
0
        char_two->m = (long)EC_GROUP_get_degree(group);
373
374
0
        field_type = EC_GROUP_get_basis_type(group);
375
376
0
        if (field_type == 0) {
377
0
            ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
378
0
            goto err;
379
0
        }
380
        /* set base type OID */
381
0
        if ((char_two->type = OBJ_nid2obj(field_type)) == NULL) {
382
0
            ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
383
0
            goto err;
384
0
        }
385
386
0
        if (field_type == NID_X9_62_tpBasis) {
387
0
            unsigned int k;
388
389
0
            if (!EC_GROUP_get_trinomial_basis(group, &k))
390
0
                goto err;
391
392
0
            char_two->p.tpBasis = ASN1_INTEGER_new();
393
0
            if (!char_two->p.tpBasis) {
394
0
                ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
395
0
                goto err;
396
0
            }
397
0
            if (!ASN1_INTEGER_set(char_two->p.tpBasis, (long)k)) {
398
0
                ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
399
0
                goto err;
400
0
            }
401
0
        } else if (field_type == NID_X9_62_ppBasis) {
402
0
            unsigned int k1, k2, k3;
403
404
0
            if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3))
405
0
                goto err;
406
407
0
            char_two->p.ppBasis = X9_62_PENTANOMIAL_new();
408
0
            if (!char_two->p.ppBasis) {
409
0
                ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
410
0
                goto err;
411
0
            }
412
413
            /* set k? values */
414
0
            char_two->p.ppBasis->k1 = (long)k1;
415
0
            char_two->p.ppBasis->k2 = (long)k2;
416
0
            char_two->p.ppBasis->k3 = (long)k3;
417
0
        } else {                /* field_type == NID_X9_62_onBasis */
418
419
            /* for ONB the parameters are (asn1) NULL */
420
0
            char_two->p.onBasis = ASN1_NULL_new();
421
0
            if (!char_two->p.onBasis) {
422
0
                ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
423
0
                goto err;
424
0
            }
425
0
        }
426
0
    }
427
0
#endif
428
429
0
    ok = 1;
430
431
0
 err:if (tmp)
432
0
        BN_free(tmp);
433
0
    return (ok);
434
0
}
435
436
static int ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve)
437
0
{
438
0
    int ok = 0, nid;
439
0
    BIGNUM *tmp_1 = NULL, *tmp_2 = NULL;
440
0
    unsigned char *buffer_1 = NULL, *buffer_2 = NULL,
441
0
        *a_buf = NULL, *b_buf = NULL;
442
0
    size_t len_1, len_2;
443
0
    unsigned char char_zero = 0;
444
445
0
    if (!group || !curve || !curve->a || !curve->b)
446
0
        return 0;
447
448
0
    if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL) {
449
0
        ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
450
0
        goto err;
451
0
    }
452
453
0
    nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
454
455
    /* get a and b */
456
0
    if (nid == NID_X9_62_prime_field) {
457
0
        if (!EC_GROUP_get_curve_GFp(group, NULL, tmp_1, tmp_2, NULL)) {
458
0
            ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
459
0
            goto err;
460
0
        }
461
0
    }
462
0
#ifndef OPENSSL_NO_EC2M
463
0
    else {                      /* nid == NID_X9_62_characteristic_two_field */
464
465
0
        if (!EC_GROUP_get_curve_GF2m(group, NULL, tmp_1, tmp_2, NULL)) {
466
0
            ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
467
0
            goto err;
468
0
        }
469
0
    }
470
0
#endif
471
0
    len_1 = (size_t)BN_num_bytes(tmp_1);
472
0
    len_2 = (size_t)BN_num_bytes(tmp_2);
473
474
0
    if (len_1 == 0) {
475
        /* len_1 == 0 => a == 0 */
476
0
        a_buf = &char_zero;
477
0
        len_1 = 1;
478
0
    } else {
479
0
        if ((buffer_1 = OPENSSL_malloc(len_1)) == NULL) {
480
0
            ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
481
0
            goto err;
482
0
        }
483
0
        if ((len_1 = BN_bn2bin(tmp_1, buffer_1)) == 0) {
484
0
            ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
485
0
            goto err;
486
0
        }
487
0
        a_buf = buffer_1;
488
0
    }
489
490
0
    if (len_2 == 0) {
491
        /* len_2 == 0 => b == 0 */
492
0
        b_buf = &char_zero;
493
0
        len_2 = 1;
494
0
    } else {
495
0
        if ((buffer_2 = OPENSSL_malloc(len_2)) == NULL) {
496
0
            ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
497
0
            goto err;
498
0
        }
499
0
        if ((len_2 = BN_bn2bin(tmp_2, buffer_2)) == 0) {
500
0
            ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
501
0
            goto err;
502
0
        }
503
0
        b_buf = buffer_2;
504
0
    }
505
506
    /* set a and b */
507
0
    if (!M_ASN1_OCTET_STRING_set(curve->a, a_buf, len_1) ||
508
0
        !M_ASN1_OCTET_STRING_set(curve->b, b_buf, len_2)) {
509
0
        ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
510
0
        goto err;
511
0
    }
512
513
    /* set the seed (optional) */
514
0
    if (group->seed) {
515
0
        if (!curve->seed)
516
0
            if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) {
517
0
                ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
518
0
                goto err;
519
0
            }
520
0
        curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
521
0
        curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT;
522
0
        if (!ASN1_BIT_STRING_set(curve->seed, group->seed,
523
0
                                 (int)group->seed_len)) {
524
0
            ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
525
0
            goto err;
526
0
        }
527
0
    } else {
528
0
        if (curve->seed) {
529
0
            ASN1_BIT_STRING_free(curve->seed);
530
0
            curve->seed = NULL;
531
0
        }
532
0
    }
533
534
0
    ok = 1;
535
536
0
 err:if (buffer_1)
537
0
        OPENSSL_free(buffer_1);
538
0
    if (buffer_2)
539
0
        OPENSSL_free(buffer_2);
540
0
    if (tmp_1)
541
0
        BN_free(tmp_1);
542
0
    if (tmp_2)
543
0
        BN_free(tmp_2);
544
0
    return (ok);
545
0
}
546
547
static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *group,
548
                                              ECPARAMETERS *param)
549
0
{
550
0
    int ok = 0;
551
0
    size_t len = 0;
552
0
    ECPARAMETERS *ret = NULL;
553
0
    BIGNUM *tmp = NULL;
554
0
    unsigned char *buffer = NULL;
555
0
    const EC_POINT *point = NULL;
556
0
    point_conversion_form_t form;
557
558
0
    if ((tmp = BN_new()) == NULL) {
559
0
        ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
560
0
        goto err;
561
0
    }
562
563
0
    if (param == NULL) {
564
0
        if ((ret = ECPARAMETERS_new()) == NULL) {
565
0
            ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
566
0
            goto err;
567
0
        }
568
0
    } else
569
0
        ret = param;
570
571
    /* set the version (always one) */
572
0
    ret->version = (long)0x1;
573
574
    /* set the fieldID */
575
0
    if (!ec_asn1_group2fieldid(group, ret->fieldID)) {
576
0
        ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
577
0
        goto err;
578
0
    }
579
580
    /* set the curve */
581
0
    if (!ec_asn1_group2curve(group, ret->curve)) {
582
0
        ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
583
0
        goto err;
584
0
    }
585
586
    /* set the base point */
587
0
    if ((point = EC_GROUP_get0_generator(group)) == NULL) {
588
0
        ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, EC_R_UNDEFINED_GENERATOR);
589
0
        goto err;
590
0
    }
591
592
0
    form = EC_GROUP_get_point_conversion_form(group);
593
594
0
    len = EC_POINT_point2oct(group, point, form, NULL, len, NULL);
595
0
    if (len == 0) {
596
0
        ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
597
0
        goto err;
598
0
    }
599
0
    if ((buffer = OPENSSL_malloc(len)) == NULL) {
600
0
        ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
601
0
        goto err;
602
0
    }
603
0
    if (!EC_POINT_point2oct(group, point, form, buffer, len, NULL)) {
604
0
        ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
605
0
        goto err;
606
0
    }
607
0
    if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) {
608
0
        ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
609
0
        goto err;
610
0
    }
611
0
    if (!ASN1_OCTET_STRING_set(ret->base, buffer, len)) {
612
0
        ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
613
0
        goto err;
614
0
    }
615
616
    /* set the order */
617
0
    if (!EC_GROUP_get_order(group, tmp, NULL)) {
618
0
        ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
619
0
        goto err;
620
0
    }
621
0
    ret->order = BN_to_ASN1_INTEGER(tmp, ret->order);
622
0
    if (ret->order == NULL) {
623
0
        ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
624
0
        goto err;
625
0
    }
626
627
    /* set the cofactor (optional) */
628
0
    if (EC_GROUP_get_cofactor(group, tmp, NULL)) {
629
0
        ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor);
630
0
        if (ret->cofactor == NULL) {
631
0
            ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
632
0
            goto err;
633
0
        }
634
0
    }
635
636
0
    ok = 1;
637
638
0
 err:if (!ok) {
639
0
        if (ret && !param)
640
0
            ECPARAMETERS_free(ret);
641
0
        ret = NULL;
642
0
    }
643
0
    if (tmp)
644
0
        BN_free(tmp);
645
0
    if (buffer)
646
0
        OPENSSL_free(buffer);
647
0
    return (ret);
648
0
}
649
650
ECPKPARAMETERS *ec_asn1_group2pkparameters(const EC_GROUP *group,
651
                                           ECPKPARAMETERS *params)
652
0
{
653
0
    int ok = 1, tmp;
654
0
    ECPKPARAMETERS *ret = params;
655
656
0
    if (ret == NULL) {
657
0
        if ((ret = ECPKPARAMETERS_new()) == NULL) {
658
0
            ECerr(EC_F_EC_ASN1_GROUP2PKPARAMETERS, ERR_R_MALLOC_FAILURE);
659
0
            return NULL;
660
0
        }
661
0
    } else {
662
0
        if (ret->type == 0 && ret->value.named_curve)
663
0
            ASN1_OBJECT_free(ret->value.named_curve);
664
0
        else if (ret->type == 1 && ret->value.parameters)
665
0
            ECPARAMETERS_free(ret->value.parameters);
666
0
    }
667
668
0
    if (EC_GROUP_get_asn1_flag(group)) {
669
        /*
670
         * use the asn1 OID to describe the the elliptic curve parameters
671
         */
672
0
        tmp = EC_GROUP_get_curve_name(group);
673
0
        if (tmp) {
674
0
            ret->type = 0;
675
0
            if ((ret->value.named_curve = OBJ_nid2obj(tmp)) == NULL)
676
0
                ok = 0;
677
0
        } else
678
            /* we don't kmow the nid => ERROR */
679
0
            ok = 0;
680
0
    } else {
681
        /* use the ECPARAMETERS structure */
682
0
        ret->type = 1;
683
0
        if ((ret->value.parameters =
684
0
             ec_asn1_group2parameters(group, NULL)) == NULL)
685
0
            ok = 0;
686
0
    }
687
688
0
    if (!ok) {
689
0
        ECPKPARAMETERS_free(ret);
690
0
        return NULL;
691
0
    }
692
0
    return ret;
693
0
}
694
695
static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *params)
696
0
{
697
0
    int ok = 0, tmp;
698
0
    EC_GROUP *ret = NULL;
699
0
    BIGNUM *p = NULL, *a = NULL, *b = NULL;
700
0
    EC_POINT *point = NULL;
701
0
    long field_bits;
702
703
0
    if (!params->fieldID || !params->fieldID->fieldType ||
704
0
        !params->fieldID->p.ptr) {
705
0
        ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
706
0
        goto err;
707
0
    }
708
709
    /* now extract the curve parameters a and b */
710
0
    if (!params->curve || !params->curve->a ||
711
0
        !params->curve->a->data || !params->curve->b ||
712
0
        !params->curve->b->data) {
713
0
        ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
714
0
        goto err;
715
0
    }
716
0
    a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);
717
0
    if (a == NULL) {
718
0
        ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
719
0
        goto err;
720
0
    }
721
0
    b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
722
0
    if (b == NULL) {
723
0
        ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
724
0
        goto err;
725
0
    }
726
727
    /* get the field parameters */
728
0
    tmp = OBJ_obj2nid(params->fieldID->fieldType);
729
0
    if (tmp == NID_X9_62_characteristic_two_field)
730
#ifdef OPENSSL_NO_EC2M
731
    {
732
        ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_GF2M_NOT_SUPPORTED);
733
        goto err;
734
    }
735
#else
736
0
    {
737
0
        X9_62_CHARACTERISTIC_TWO *char_two;
738
739
0
        char_two = params->fieldID->p.char_two;
740
741
0
        field_bits = char_two->m;
742
0
        if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
743
0
            ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE);
744
0
            goto err;
745
0
        }
746
747
0
        if ((p = BN_new()) == NULL) {
748
0
            ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE);
749
0
            goto err;
750
0
        }
751
752
        /* get the base type */
753
0
        tmp = OBJ_obj2nid(char_two->type);
754
755
0
        if (tmp == NID_X9_62_tpBasis) {
756
0
            long tmp_long;
757
758
0
            if (!char_two->p.tpBasis) {
759
0
                ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
760
0
                goto err;
761
0
            }
762
763
0
            tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis);
764
765
0
            if (!(char_two->m > tmp_long && tmp_long > 0)) {
766
0
                ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP,
767
0
                      EC_R_INVALID_TRINOMIAL_BASIS);
768
0
                goto err;
769
0
            }
770
771
            /* create the polynomial */
772
0
            if (!BN_set_bit(p, (int)char_two->m))
773
0
                goto err;
774
0
            if (!BN_set_bit(p, (int)tmp_long))
775
0
                goto err;
776
0
            if (!BN_set_bit(p, 0))
777
0
                goto err;
778
0
        } else if (tmp == NID_X9_62_ppBasis) {
779
0
            X9_62_PENTANOMIAL *penta;
780
781
0
            penta = char_two->p.ppBasis;
782
0
            if (!penta) {
783
0
                ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
784
0
                goto err;
785
0
            }
786
787
0
            if (!
788
0
                (char_two->m > penta->k3 && penta->k3 > penta->k2
789
0
                 && penta->k2 > penta->k1 && penta->k1 > 0)) {
790
0
                ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP,
791
0
                      EC_R_INVALID_PENTANOMIAL_BASIS);
792
0
                goto err;
793
0
            }
794
795
            /* create the polynomial */
796
0
            if (!BN_set_bit(p, (int)char_two->m))
797
0
                goto err;
798
0
            if (!BN_set_bit(p, (int)penta->k1))
799
0
                goto err;
800
0
            if (!BN_set_bit(p, (int)penta->k2))
801
0
                goto err;
802
0
            if (!BN_set_bit(p, (int)penta->k3))
803
0
                goto err;
804
0
            if (!BN_set_bit(p, 0))
805
0
                goto err;
806
0
        } else if (tmp == NID_X9_62_onBasis) {
807
0
            ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_NOT_IMPLEMENTED);
808
0
            goto err;
809
0
        } else {                /* error */
810
811
0
            ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
812
0
            goto err;
813
0
        }
814
815
        /* create the EC_GROUP structure */
816
0
        ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
817
0
    }
818
0
#endif
819
0
    else if (tmp == NID_X9_62_prime_field) {
820
        /* we have a curve over a prime field */
821
        /* extract the prime number */
822
0
        if (!params->fieldID->p.prime) {
823
0
            ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
824
0
            goto err;
825
0
        }
826
0
        p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);
827
0
        if (p == NULL) {
828
0
            ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
829
0
            goto err;
830
0
        }
831
832
0
        if (BN_is_negative(p) || BN_is_zero(p)) {
833
0
            ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD);
834
0
            goto err;
835
0
        }
836
837
0
        field_bits = BN_num_bits(p);
838
0
        if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
839
0
            ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE);
840
0
            goto err;
841
0
        }
842
843
        /* create the EC_GROUP structure */
844
0
        ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);
845
0
    } else {
846
0
        ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD);
847
0
        goto err;
848
0
    }
849
850
0
    if (ret == NULL) {
851
0
        ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
852
0
        goto err;
853
0
    }
854
855
    /* extract seed (optional) */
856
0
    if (params->curve->seed != NULL) {
857
0
        if (ret->seed != NULL)
858
0
            OPENSSL_free(ret->seed);
859
0
        if (!(ret->seed = OPENSSL_malloc(params->curve->seed->length))) {
860
0
            ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE);
861
0
            goto err;
862
0
        }
863
0
        memcpy(ret->seed, params->curve->seed->data,
864
0
               params->curve->seed->length);
865
0
        ret->seed_len = params->curve->seed->length;
866
0
    }
867
868
0
    if (!params->order || !params->base || !params->base->data) {
869
0
        ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
870
0
        goto err;
871
0
    }
872
873
0
    if ((point = EC_POINT_new(ret)) == NULL)
874
0
        goto err;
875
876
    /* set the point conversion form */
877
0
    EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)
878
0
                                       (params->base->data[0] & ~0x01));
879
880
    /* extract the ec point */
881
0
    if (!EC_POINT_oct2point(ret, point, params->base->data,
882
0
                            params->base->length, NULL)) {
883
0
        ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
884
0
        goto err;
885
0
    }
886
887
    /* extract the order */
888
0
    if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {
889
0
        ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
890
0
        goto err;
891
0
    }
892
0
    if (BN_is_negative(a) || BN_is_zero(a)) {
893
0
        ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER);
894
0
        goto err;
895
0
    }
896
0
    if (BN_num_bits(a) > (int)field_bits + 1) { /* Hasse bound */
897
0
        ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER);
898
0
        goto err;
899
0
    }
900
901
    /* extract the cofactor (optional) */
902
0
    if (params->cofactor == NULL) {
903
0
        if (b) {
904
0
            BN_free(b);
905
0
            b = NULL;
906
0
        }
907
0
    } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) {
908
0
        ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
909
0
        goto err;
910
0
    }
911
    /* set the generator, order and cofactor (if present) */
912
0
    if (!EC_GROUP_set_generator(ret, point, a, b)) {
913
0
        ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
914
0
        goto err;
915
0
    }
916
917
0
    ok = 1;
918
919
0
 err:if (!ok) {
920
0
        if (ret)
921
0
            EC_GROUP_clear_free(ret);
922
0
        ret = NULL;
923
0
    }
924
925
0
    if (p)
926
0
        BN_free(p);
927
0
    if (a)
928
0
        BN_free(a);
929
0
    if (b)
930
0
        BN_free(b);
931
0
    if (point)
932
0
        EC_POINT_free(point);
933
0
    return (ret);
934
0
}
935
936
EC_GROUP *ec_asn1_pkparameters2group(const ECPKPARAMETERS *params)
937
0
{
938
0
    EC_GROUP *ret = NULL;
939
0
    int tmp = 0;
940
941
0
    if (params == NULL) {
942
0
        ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_MISSING_PARAMETERS);
943
0
        return NULL;
944
0
    }
945
946
0
    if (params->type == 0) {    /* the curve is given by an OID */
947
0
        tmp = OBJ_obj2nid(params->value.named_curve);
948
0
        if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) {
949
0
            ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP,
950
0
                  EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
951
0
            return NULL;
952
0
        }
953
0
        EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);
954
0
    } else if (params->type == 1) { /* the parameters are given by a
955
                                     * ECPARAMETERS structure */
956
0
        ret = ec_asn1_parameters2group(params->value.parameters);
957
0
        if (!ret) {
958
0
            ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, ERR_R_EC_LIB);
959
0
            return NULL;
960
0
        }
961
0
        EC_GROUP_set_asn1_flag(ret, 0x0);
962
0
    } else if (params->type == 2) { /* implicitlyCA */
963
0
        return NULL;
964
0
    } else {
965
0
        ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_ASN1_ERROR);
966
0
        return NULL;
967
0
    }
968
969
0
    return ret;
970
0
}
971
972
/* EC_GROUP <-> DER encoding of ECPKPARAMETERS */
973
974
EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len)
975
0
{
976
0
    EC_GROUP *group = NULL;
977
0
    ECPKPARAMETERS *params = NULL;
978
0
    const unsigned char *p = *in;
979
980
0
    if ((params = d2i_ECPKPARAMETERS(NULL, &p, len)) == NULL) {
981
0
        ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE);
982
0
        ECPKPARAMETERS_free(params);
983
0
        return NULL;
984
0
    }
985
986
0
    if ((group = ec_asn1_pkparameters2group(params)) == NULL) {
987
0
        ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE);
988
0
        ECPKPARAMETERS_free(params);
989
0
        return NULL;
990
0
    }
991
992
0
    if (a && *a)
993
0
        EC_GROUP_clear_free(*a);
994
0
    if (a)
995
0
        *a = group;
996
997
0
    ECPKPARAMETERS_free(params);
998
0
    *in = p;
999
0
    return (group);
1000
0
}
1001
1002
int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out)
1003
0
{
1004
0
    int ret = 0;
1005
0
    ECPKPARAMETERS *tmp = ec_asn1_group2pkparameters(a, NULL);
1006
0
    if (tmp == NULL) {
1007
0
        ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE);
1008
0
        return 0;
1009
0
    }
1010
0
    if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) {
1011
0
        ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE);
1012
0
        ECPKPARAMETERS_free(tmp);
1013
0
        return 0;
1014
0
    }
1015
0
    ECPKPARAMETERS_free(tmp);
1016
0
    return (ret);
1017
0
}
1018
1019
/* some EC_KEY functions */
1020
1021
EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len)
1022
0
{
1023
0
    int ok = 0;
1024
0
    EC_KEY *ret = NULL;
1025
0
    EC_PRIVATEKEY *priv_key = NULL;
1026
0
    const unsigned char *p = *in;
1027
1028
0
    if ((priv_key = d2i_EC_PRIVATEKEY(NULL, &p, len)) == NULL) {
1029
0
        ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1030
0
        return NULL;
1031
0
    }
1032
1033
0
    if (a == NULL || *a == NULL) {
1034
0
        if ((ret = EC_KEY_new()) == NULL) {
1035
0
            ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1036
0
            goto err;
1037
0
        }
1038
0
    } else
1039
0
        ret = *a;
1040
1041
0
    if (priv_key->parameters) {
1042
0
        if (ret->group)
1043
0
            EC_GROUP_clear_free(ret->group);
1044
0
        ret->group = ec_asn1_pkparameters2group(priv_key->parameters);
1045
0
    }
1046
1047
0
    if (ret->group == NULL) {
1048
0
        ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1049
0
        goto err;
1050
0
    }
1051
1052
0
    ret->version = priv_key->version;
1053
1054
0
    if (priv_key->privateKey) {
1055
0
        ret->priv_key = BN_bin2bn(M_ASN1_STRING_data(priv_key->privateKey),
1056
0
                                  M_ASN1_STRING_length(priv_key->privateKey),
1057
0
                                  ret->priv_key);
1058
0
        if (ret->priv_key == NULL) {
1059
0
            ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_BN_LIB);
1060
0
            goto err;
1061
0
        }
1062
0
    } else {
1063
0
        ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_MISSING_PRIVATE_KEY);
1064
0
        goto err;
1065
0
    }
1066
1067
0
    if (ret->pub_key)
1068
0
        EC_POINT_clear_free(ret->pub_key);
1069
0
    ret->pub_key = EC_POINT_new(ret->group);
1070
0
    if (ret->pub_key == NULL) {
1071
0
        ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1072
0
        goto err;
1073
0
    }
1074
1075
0
    if (priv_key->publicKey) {
1076
0
        const unsigned char *pub_oct;
1077
0
        int pub_oct_len;
1078
1079
0
        pub_oct = M_ASN1_STRING_data(priv_key->publicKey);
1080
0
        pub_oct_len = M_ASN1_STRING_length(priv_key->publicKey);
1081
        /*
1082
         * The first byte - point conversion form - must be present.
1083
         */
1084
0
        if (pub_oct_len <= 0) {
1085
0
            ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_BUFFER_TOO_SMALL);
1086
0
            goto err;
1087
0
        }
1088
        /* Save the point conversion form. */
1089
0
        ret->conv_form = (point_conversion_form_t) (pub_oct[0] & ~0x01);
1090
0
        if (!EC_POINT_oct2point(ret->group, ret->pub_key,
1091
0
                                pub_oct, (size_t)(pub_oct_len), NULL)) {
1092
0
            ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1093
0
            goto err;
1094
0
        }
1095
0
    } else {
1096
0
        if (!EC_POINT_mul
1097
0
            (ret->group, ret->pub_key, ret->priv_key, NULL, NULL, NULL)) {
1098
0
            ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1099
0
            goto err;
1100
0
        }
1101
        /* Remember the original private-key-only encoding. */
1102
0
        ret->enc_flag |= EC_PKEY_NO_PUBKEY;
1103
0
    }
1104
1105
0
    if (a)
1106
0
        *a = ret;
1107
0
    *in = p;
1108
0
    ok = 1;
1109
0
 err:
1110
0
    if (!ok) {
1111
0
        if (ret && (a == NULL || *a != ret))
1112
0
            EC_KEY_free(ret);
1113
0
        ret = NULL;
1114
0
    }
1115
1116
0
    if (priv_key)
1117
0
        EC_PRIVATEKEY_free(priv_key);
1118
1119
0
    return (ret);
1120
0
}
1121
1122
int i2d_ECPrivateKey(EC_KEY *a, unsigned char **out)
1123
0
{
1124
0
    int ret = 0, ok = 0;
1125
0
    unsigned char *buffer = NULL;
1126
0
    size_t buf_len = 0, tmp_len, bn_len;
1127
0
    EC_PRIVATEKEY *priv_key = NULL;
1128
1129
0
    if (a == NULL || a->group == NULL || a->priv_key == NULL ||
1130
0
        (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key == NULL)) {
1131
0
        ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
1132
0
        goto err;
1133
0
    }
1134
1135
0
    if ((priv_key = EC_PRIVATEKEY_new()) == NULL) {
1136
0
        ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1137
0
        goto err;
1138
0
    }
1139
1140
0
    priv_key->version = a->version;
1141
1142
0
    bn_len = (size_t)BN_num_bytes(a->priv_key);
1143
1144
    /* Octetstring may need leading zeros if BN is to short */
1145
1146
0
    buf_len = (EC_GROUP_get_degree(a->group) + 7) / 8;
1147
1148
0
    if (bn_len > buf_len) {
1149
0
        ECerr(EC_F_I2D_ECPRIVATEKEY, EC_R_BUFFER_TOO_SMALL);
1150
0
        goto err;
1151
0
    }
1152
1153
0
    buffer = OPENSSL_malloc(buf_len);
1154
0
    if (buffer == NULL) {
1155
0
        ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1156
0
        goto err;
1157
0
    }
1158
1159
0
    if (!BN_bn2bin(a->priv_key, buffer + buf_len - bn_len)) {
1160
0
        ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_BN_LIB);
1161
0
        goto err;
1162
0
    }
1163
1164
0
    if (buf_len - bn_len > 0) {
1165
0
        memset(buffer, 0, buf_len - bn_len);
1166
0
    }
1167
1168
0
    if (!M_ASN1_OCTET_STRING_set(priv_key->privateKey, buffer, buf_len)) {
1169
0
        ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB);
1170
0
        goto err;
1171
0
    }
1172
1173
0
    if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) {
1174
0
        if ((priv_key->parameters =
1175
0
             ec_asn1_group2pkparameters(a->group,
1176
0
                                        priv_key->parameters)) == NULL) {
1177
0
            ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1178
0
            goto err;
1179
0
        }
1180
0
    }
1181
1182
0
    if (!(a->enc_flag & EC_PKEY_NO_PUBKEY)) {
1183
0
        priv_key->publicKey = M_ASN1_BIT_STRING_new();
1184
0
        if (priv_key->publicKey == NULL) {
1185
0
            ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1186
0
            goto err;
1187
0
        }
1188
1189
0
        tmp_len = EC_POINT_point2oct(a->group, a->pub_key,
1190
0
                                     a->conv_form, NULL, 0, NULL);
1191
1192
0
        if (tmp_len > buf_len) {
1193
0
            unsigned char *tmp_buffer = OPENSSL_realloc(buffer, tmp_len);
1194
0
            if (!tmp_buffer) {
1195
0
                ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1196
0
                goto err;
1197
0
            }
1198
0
            buffer = tmp_buffer;
1199
0
            buf_len = tmp_len;
1200
0
        }
1201
1202
0
        if (!EC_POINT_point2oct(a->group, a->pub_key,
1203
0
                                a->conv_form, buffer, buf_len, NULL)) {
1204
0
            ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1205
0
            goto err;
1206
0
        }
1207
1208
0
        priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
1209
0
        priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
1210
0
        if (!M_ASN1_BIT_STRING_set(priv_key->publicKey, buffer, buf_len)) {
1211
0
            ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB);
1212
0
            goto err;
1213
0
        }
1214
0
    }
1215
1216
0
    if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) {
1217
0
        ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1218
0
        goto err;
1219
0
    }
1220
0
    ok = 1;
1221
0
 err:
1222
0
    if (buffer)
1223
0
        OPENSSL_free(buffer);
1224
0
    if (priv_key)
1225
0
        EC_PRIVATEKEY_free(priv_key);
1226
0
    return (ok ? ret : 0);
1227
0
}
1228
1229
int i2d_ECParameters(EC_KEY *a, unsigned char **out)
1230
0
{
1231
0
    if (a == NULL) {
1232
0
        ECerr(EC_F_I2D_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1233
0
        return 0;
1234
0
    }
1235
0
    return i2d_ECPKParameters(a->group, out);
1236
0
}
1237
1238
EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len)
1239
0
{
1240
0
    EC_KEY *ret;
1241
1242
0
    if (in == NULL || *in == NULL) {
1243
0
        ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1244
0
        return NULL;
1245
0
    }
1246
1247
0
    if (a == NULL || *a == NULL) {
1248
0
        if ((ret = EC_KEY_new()) == NULL) {
1249
0
            ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
1250
0
            return NULL;
1251
0
        }
1252
0
    } else
1253
0
        ret = *a;
1254
1255
0
    if (!d2i_ECPKParameters(&ret->group, in, len)) {
1256
0
        ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB);
1257
0
        if (a == NULL || *a != ret)
1258
0
             EC_KEY_free(ret);
1259
0
        return NULL;
1260
0
    }
1261
1262
0
    if (a)
1263
0
        *a = ret;
1264
1265
0
    return ret;
1266
0
}
1267
1268
EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len)
1269
0
{
1270
0
    EC_KEY *ret = NULL;
1271
1272
0
    if (a == NULL || (*a) == NULL || (*a)->group == NULL) {
1273
        /*
1274
         * sorry, but a EC_GROUP-structur is necessary to set the public key
1275
         */
1276
0
        ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1277
0
        return 0;
1278
0
    }
1279
0
    ret = *a;
1280
0
    if (ret->pub_key == NULL &&
1281
0
        (ret->pub_key = EC_POINT_new(ret->group)) == NULL) {
1282
0
        ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
1283
0
        return 0;
1284
0
    }
1285
0
    if (!EC_POINT_oct2point(ret->group, ret->pub_key, *in, len, NULL)) {
1286
0
        ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_EC_LIB);
1287
0
        return 0;
1288
0
    }
1289
    /* save the point conversion form */
1290
0
    ret->conv_form = (point_conversion_form_t) (*in[0] & ~0x01);
1291
0
    *in += len;
1292
0
    return ret;
1293
0
}
1294
1295
int i2o_ECPublicKey(EC_KEY *a, unsigned char **out)
1296
0
{
1297
0
    size_t buf_len = 0;
1298
0
    int new_buffer = 0;
1299
1300
0
    if (a == NULL) {
1301
0
        ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1302
0
        return 0;
1303
0
    }
1304
1305
0
    buf_len = EC_POINT_point2oct(a->group, a->pub_key,
1306
0
                                 a->conv_form, NULL, 0, NULL);
1307
1308
0
    if (out == NULL || buf_len == 0)
1309
        /* out == NULL => just return the length of the octet string */
1310
0
        return buf_len;
1311
1312
0
    if (*out == NULL) {
1313
0
        if ((*out = OPENSSL_malloc(buf_len)) == NULL) {
1314
0
            ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
1315
0
            return 0;
1316
0
        }
1317
0
        new_buffer = 1;
1318
0
    }
1319
0
    if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form,
1320
0
                            *out, buf_len, NULL)) {
1321
0
        ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_EC_LIB);
1322
0
        if (new_buffer) {
1323
0
            OPENSSL_free(*out);
1324
0
            *out = NULL;
1325
0
        }
1326
0
        return 0;
1327
0
    }
1328
0
    if (!new_buffer)
1329
0
        *out += buf_len;
1330
0
    return buf_len;
1331
0
}