Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/ec/ec_key.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4
 *
5
 * Licensed under the OpenSSL license (the "License").  You may not use
6
 * this file except in compliance with the License.  You can obtain a copy
7
 * in the file LICENSE in the source distribution or at
8
 * https://www.openssl.org/source/license.html
9
 */
10
11
#include "internal/cryptlib.h"
12
#include <string.h>
13
#include "ec_lcl.h"
14
#include "internal/refcount.h"
15
#include <openssl/err.h>
16
#include <openssl/engine.h>
17
18
EC_KEY *EC_KEY_new(void)
19
0
{
20
0
    return EC_KEY_new_method(NULL);
21
0
}
22
23
EC_KEY *EC_KEY_new_by_curve_name(int nid)
24
0
{
25
0
    EC_KEY *ret = EC_KEY_new();
26
0
    if (ret == NULL)
27
0
        return NULL;
28
0
    ret->group = EC_GROUP_new_by_curve_name(nid);
29
0
    if (ret->group == NULL) {
30
0
        EC_KEY_free(ret);
31
0
        return NULL;
32
0
    }
33
0
    if (ret->meth->set_group != NULL
34
0
        && ret->meth->set_group(ret, ret->group) == 0) {
35
0
        EC_KEY_free(ret);
36
0
        return NULL;
37
0
    }
38
0
    return ret;
39
0
}
40
41
void EC_KEY_free(EC_KEY *r)
42
0
{
43
0
    int i;
44
0
45
0
    if (r == NULL)
46
0
        return;
47
0
48
0
    CRYPTO_DOWN_REF(&r->references, &i, r->lock);
49
0
    REF_PRINT_COUNT("EC_KEY", r);
50
0
    if (i > 0)
51
0
        return;
52
0
    REF_ASSERT_ISNT(i < 0);
53
0
54
0
    if (r->meth->finish != NULL)
55
0
        r->meth->finish(r);
56
0
57
0
#ifndef OPENSSL_NO_ENGINE
58
0
    ENGINE_finish(r->engine);
59
0
#endif
60
0
61
0
    if (r->group && r->group->meth->keyfinish)
62
0
        r->group->meth->keyfinish(r);
63
0
64
0
    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, r, &r->ex_data);
65
0
    CRYPTO_THREAD_lock_free(r->lock);
66
0
    EC_GROUP_free(r->group);
67
0
    EC_POINT_free(r->pub_key);
68
0
    BN_clear_free(r->priv_key);
69
0
70
0
    OPENSSL_clear_free((void *)r, sizeof(EC_KEY));
71
0
}
72
73
EC_KEY *EC_KEY_copy(EC_KEY *dest, const EC_KEY *src)
74
0
{
75
0
    if (dest == NULL || src == NULL) {
76
0
        ECerr(EC_F_EC_KEY_COPY, ERR_R_PASSED_NULL_PARAMETER);
77
0
        return NULL;
78
0
    }
79
0
    if (src->meth != dest->meth) {
80
0
        if (dest->meth->finish != NULL)
81
0
            dest->meth->finish(dest);
82
0
        if (dest->group && dest->group->meth->keyfinish)
83
0
            dest->group->meth->keyfinish(dest);
84
0
#ifndef OPENSSL_NO_ENGINE
85
0
        if (ENGINE_finish(dest->engine) == 0)
86
0
            return 0;
87
0
        dest->engine = NULL;
88
0
#endif
89
0
    }
90
0
    /* copy the parameters */
91
0
    if (src->group != NULL) {
92
0
        const EC_METHOD *meth = EC_GROUP_method_of(src->group);
93
0
        /* clear the old group */
94
0
        EC_GROUP_free(dest->group);
95
0
        dest->group = EC_GROUP_new(meth);
96
0
        if (dest->group == NULL)
97
0
            return NULL;
98
0
        if (!EC_GROUP_copy(dest->group, src->group))
99
0
            return NULL;
100
0
101
0
        /*  copy the public key */
102
0
        if (src->pub_key != NULL) {
103
0
            EC_POINT_free(dest->pub_key);
104
0
            dest->pub_key = EC_POINT_new(src->group);
105
0
            if (dest->pub_key == NULL)
106
0
                return NULL;
107
0
            if (!EC_POINT_copy(dest->pub_key, src->pub_key))
108
0
                return NULL;
109
0
        }
110
0
        /* copy the private key */
111
0
        if (src->priv_key != NULL) {
112
0
            if (dest->priv_key == NULL) {
113
0
                dest->priv_key = BN_new();
114
0
                if (dest->priv_key == NULL)
115
0
                    return NULL;
116
0
            }
117
0
            if (!BN_copy(dest->priv_key, src->priv_key))
118
0
                return NULL;
119
0
            if (src->group->meth->keycopy
120
0
                && src->group->meth->keycopy(dest, src) == 0)
121
0
                return NULL;
122
0
        }
123
0
    }
124
0
125
0
126
0
    /* copy the rest */
127
0
    dest->enc_flag = src->enc_flag;
128
0
    dest->conv_form = src->conv_form;
129
0
    dest->version = src->version;
130
0
    dest->flags = src->flags;
131
0
    if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY,
132
0
                            &dest->ex_data, &src->ex_data))
133
0
        return NULL;
134
0
135
0
    if (src->meth != dest->meth) {
136
0
#ifndef OPENSSL_NO_ENGINE
137
0
        if (src->engine != NULL && ENGINE_init(src->engine) == 0)
138
0
            return NULL;
139
0
        dest->engine = src->engine;
140
0
#endif
141
0
        dest->meth = src->meth;
142
0
    }
143
0
144
0
    if (src->meth->copy != NULL && src->meth->copy(dest, src) == 0)
145
0
        return NULL;
146
0
147
0
    return dest;
148
0
}
149
150
EC_KEY *EC_KEY_dup(const EC_KEY *ec_key)
151
0
{
152
0
    EC_KEY *ret = EC_KEY_new_method(ec_key->engine);
153
0
154
0
    if (ret == NULL)
155
0
        return NULL;
156
0
157
0
    if (EC_KEY_copy(ret, ec_key) == NULL) {
158
0
        EC_KEY_free(ret);
159
0
        return NULL;
160
0
    }
161
0
    return ret;
162
0
}
163
164
int EC_KEY_up_ref(EC_KEY *r)
165
0
{
166
0
    int i;
167
0
168
0
    if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
169
0
        return 0;
170
0
171
0
    REF_PRINT_COUNT("EC_KEY", r);
172
0
    REF_ASSERT_ISNT(i < 2);
173
0
    return ((i > 1) ? 1 : 0);
174
0
}
175
176
ENGINE *EC_KEY_get0_engine(const EC_KEY *eckey)
177
0
{
178
0
    return eckey->engine;
179
0
}
180
181
int EC_KEY_generate_key(EC_KEY *eckey)
182
0
{
183
0
    if (eckey == NULL || eckey->group == NULL) {
184
0
        ECerr(EC_F_EC_KEY_GENERATE_KEY, ERR_R_PASSED_NULL_PARAMETER);
185
0
        return 0;
186
0
    }
187
0
    if (eckey->meth->keygen != NULL)
188
0
        return eckey->meth->keygen(eckey);
189
0
    ECerr(EC_F_EC_KEY_GENERATE_KEY, EC_R_OPERATION_NOT_SUPPORTED);
190
0
    return 0;
191
0
}
192
193
int ossl_ec_key_gen(EC_KEY *eckey)
194
0
{
195
0
    return eckey->group->meth->keygen(eckey);
196
0
}
197
198
int ec_key_simple_generate_key(EC_KEY *eckey)
199
0
{
200
0
    int ok = 0;
201
0
    BN_CTX *ctx = NULL;
202
0
    BIGNUM *priv_key = NULL;
203
0
    const BIGNUM *order = NULL;
204
0
    EC_POINT *pub_key = NULL;
205
0
206
0
    if ((ctx = BN_CTX_new()) == NULL)
207
0
        goto err;
208
0
209
0
    if (eckey->priv_key == NULL) {
210
0
        priv_key = BN_new();
211
0
        if (priv_key == NULL)
212
0
            goto err;
213
0
    } else
214
0
        priv_key = eckey->priv_key;
215
0
216
0
    order = EC_GROUP_get0_order(eckey->group);
217
0
    if (order == NULL)
218
0
        goto err;
219
0
220
0
    do
221
0
        if (!BN_priv_rand_range(priv_key, order))
222
0
            goto err;
223
0
    while (BN_is_zero(priv_key)) ;
224
0
225
0
    if (eckey->pub_key == NULL) {
226
0
        pub_key = EC_POINT_new(eckey->group);
227
0
        if (pub_key == NULL)
228
0
            goto err;
229
0
    } else
230
0
        pub_key = eckey->pub_key;
231
0
232
0
    if (!EC_POINT_mul(eckey->group, pub_key, priv_key, NULL, NULL, ctx))
233
0
        goto err;
234
0
235
0
    eckey->priv_key = priv_key;
236
0
    eckey->pub_key = pub_key;
237
0
238
0
    ok = 1;
239
0
240
0
 err:
241
0
    if (eckey->pub_key == NULL)
242
0
        EC_POINT_free(pub_key);
243
0
    if (eckey->priv_key != priv_key)
244
0
        BN_free(priv_key);
245
0
    BN_CTX_free(ctx);
246
0
    return ok;
247
0
}
248
249
int ec_key_simple_generate_public_key(EC_KEY *eckey)
250
0
{
251
0
    return EC_POINT_mul(eckey->group, eckey->pub_key, eckey->priv_key, NULL,
252
0
                        NULL, NULL);
253
0
}
254
255
int EC_KEY_check_key(const EC_KEY *eckey)
256
0
{
257
0
    if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
258
0
        ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
259
0
        return 0;
260
0
    }
261
0
262
0
    if (eckey->group->meth->keycheck == NULL) {
263
0
        ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
264
0
        return 0;
265
0
    }
266
0
267
0
    return eckey->group->meth->keycheck(eckey);
268
0
}
269
270
int ec_key_simple_check_key(const EC_KEY *eckey)
271
0
{
272
0
    int ok = 0;
273
0
    BN_CTX *ctx = NULL;
274
0
    const BIGNUM *order = NULL;
275
0
    EC_POINT *point = NULL;
276
0
277
0
    if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
278
0
        ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
279
0
        return 0;
280
0
    }
281
0
282
0
    if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key)) {
283
0
        ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_POINT_AT_INFINITY);
284
0
        goto err;
285
0
    }
286
0
287
0
    if ((ctx = BN_CTX_new()) == NULL)
288
0
        goto err;
289
0
    if ((point = EC_POINT_new(eckey->group)) == NULL)
290
0
        goto err;
291
0
292
0
    /* testing whether the pub_key is on the elliptic curve */
293
0
    if (EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx) <= 0) {
294
0
        ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_POINT_IS_NOT_ON_CURVE);
295
0
        goto err;
296
0
    }
297
0
    /* testing whether pub_key * order is the point at infinity */
298
0
    order = eckey->group->order;
299
0
    if (BN_is_zero(order)) {
300
0
        ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_INVALID_GROUP_ORDER);
301
0
        goto err;
302
0
    }
303
0
    if (!EC_POINT_mul(eckey->group, point, NULL, eckey->pub_key, order, ctx)) {
304
0
        ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_EC_LIB);
305
0
        goto err;
306
0
    }
307
0
    if (!EC_POINT_is_at_infinity(eckey->group, point)) {
308
0
        ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_WRONG_ORDER);
309
0
        goto err;
310
0
    }
311
0
    /*
312
0
     * in case the priv_key is present : check if generator * priv_key ==
313
0
     * pub_key
314
0
     */
315
0
    if (eckey->priv_key != NULL) {
316
0
        if (BN_cmp(eckey->priv_key, order) >= 0) {
317
0
            ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_WRONG_ORDER);
318
0
            goto err;
319
0
        }
320
0
        if (!EC_POINT_mul(eckey->group, point, eckey->priv_key,
321
0
                          NULL, NULL, ctx)) {
322
0
            ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_EC_LIB);
323
0
            goto err;
324
0
        }
325
0
        if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, ctx) != 0) {
326
0
            ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_INVALID_PRIVATE_KEY);
327
0
            goto err;
328
0
        }
329
0
    }
330
0
    ok = 1;
331
0
 err:
332
0
    BN_CTX_free(ctx);
333
0
    EC_POINT_free(point);
334
0
    return ok;
335
0
}
336
337
int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
338
                                             BIGNUM *y)
339
0
{
340
0
    BN_CTX *ctx = NULL;
341
0
    BIGNUM *tx, *ty;
342
0
    EC_POINT *point = NULL;
343
0
    int ok = 0;
344
0
345
0
    if (key == NULL || key->group == NULL || x == NULL || y == NULL) {
346
0
        ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
347
0
              ERR_R_PASSED_NULL_PARAMETER);
348
0
        return 0;
349
0
    }
350
0
    ctx = BN_CTX_new();
351
0
    if (ctx == NULL)
352
0
        return 0;
353
0
354
0
    BN_CTX_start(ctx);
355
0
    point = EC_POINT_new(key->group);
356
0
357
0
    if (point == NULL)
358
0
        goto err;
359
0
360
0
    tx = BN_CTX_get(ctx);
361
0
    ty = BN_CTX_get(ctx);
362
0
    if (ty == NULL)
363
0
        goto err;
364
0
365
0
    if (!EC_POINT_set_affine_coordinates(key->group, point, x, y, ctx))
366
0
        goto err;
367
0
    if (!EC_POINT_get_affine_coordinates(key->group, point, tx, ty, ctx))
368
0
        goto err;
369
0
370
0
    /*
371
0
     * Check if retrieved coordinates match originals and are less than field
372
0
     * order: if not values are out of range.
373
0
     */
374
0
    if (BN_cmp(x, tx) || BN_cmp(y, ty)
375
0
        || (BN_cmp(x, key->group->field) >= 0)
376
0
        || (BN_cmp(y, key->group->field) >= 0)) {
377
0
        ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
378
0
              EC_R_COORDINATES_OUT_OF_RANGE);
379
0
        goto err;
380
0
    }
381
0
382
0
    if (!EC_KEY_set_public_key(key, point))
383
0
        goto err;
384
0
385
0
    if (EC_KEY_check_key(key) == 0)
386
0
        goto err;
387
0
388
0
    ok = 1;
389
0
390
0
 err:
391
0
    BN_CTX_end(ctx);
392
0
    BN_CTX_free(ctx);
393
0
    EC_POINT_free(point);
394
0
    return ok;
395
0
396
0
}
397
398
const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key)
399
0
{
400
0
    return key->group;
401
0
}
402
403
int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group)
404
0
{
405
0
    if (key->meth->set_group != NULL && key->meth->set_group(key, group) == 0)
406
0
        return 0;
407
0
    EC_GROUP_free(key->group);
408
0
    key->group = EC_GROUP_dup(group);
409
0
    return (key->group == NULL) ? 0 : 1;
410
0
}
411
412
const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key)
413
0
{
414
0
    return key->priv_key;
415
0
}
416
417
int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key)
418
0
{
419
0
    if (key->group == NULL || key->group->meth == NULL)
420
0
        return 0;
421
0
    if (key->group->meth->set_private != NULL
422
0
        && key->group->meth->set_private(key, priv_key) == 0)
423
0
        return 0;
424
0
    if (key->meth->set_private != NULL
425
0
        && key->meth->set_private(key, priv_key) == 0)
426
0
        return 0;
427
0
    BN_clear_free(key->priv_key);
428
0
    key->priv_key = BN_dup(priv_key);
429
0
    return (key->priv_key == NULL) ? 0 : 1;
430
0
}
431
432
const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key)
433
0
{
434
0
    return key->pub_key;
435
0
}
436
437
int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub_key)
438
0
{
439
0
    if (key->meth->set_public != NULL
440
0
        && key->meth->set_public(key, pub_key) == 0)
441
0
        return 0;
442
0
    EC_POINT_free(key->pub_key);
443
0
    key->pub_key = EC_POINT_dup(pub_key, key->group);
444
0
    return (key->pub_key == NULL) ? 0 : 1;
445
0
}
446
447
unsigned int EC_KEY_get_enc_flags(const EC_KEY *key)
448
0
{
449
0
    return key->enc_flag;
450
0
}
451
452
void EC_KEY_set_enc_flags(EC_KEY *key, unsigned int flags)
453
0
{
454
0
    key->enc_flag = flags;
455
0
}
456
457
point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key)
458
0
{
459
0
    return key->conv_form;
460
0
}
461
462
void EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform)
463
0
{
464
0
    key->conv_form = cform;
465
0
    if (key->group != NULL)
466
0
        EC_GROUP_set_point_conversion_form(key->group, cform);
467
0
}
468
469
void EC_KEY_set_asn1_flag(EC_KEY *key, int flag)
470
0
{
471
0
    if (key->group != NULL)
472
0
        EC_GROUP_set_asn1_flag(key->group, flag);
473
0
}
474
475
int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx)
476
0
{
477
0
    if (key->group == NULL)
478
0
        return 0;
479
0
    return EC_GROUP_precompute_mult(key->group, ctx);
480
0
}
481
482
int EC_KEY_get_flags(const EC_KEY *key)
483
0
{
484
0
    return key->flags;
485
0
}
486
487
void EC_KEY_set_flags(EC_KEY *key, int flags)
488
0
{
489
0
    key->flags |= flags;
490
0
}
491
492
void EC_KEY_clear_flags(EC_KEY *key, int flags)
493
0
{
494
0
    key->flags &= ~flags;
495
0
}
496
497
size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form,
498
                        unsigned char **pbuf, BN_CTX *ctx)
499
0
{
500
0
    if (key == NULL || key->pub_key == NULL || key->group == NULL)
501
0
        return 0;
502
0
    return EC_POINT_point2buf(key->group, key->pub_key, form, pbuf, ctx);
503
0
}
504
505
int EC_KEY_oct2key(EC_KEY *key, const unsigned char *buf, size_t len,
506
                   BN_CTX *ctx)
507
0
{
508
0
    if (key == NULL || key->group == NULL)
509
0
        return 0;
510
0
    if (key->pub_key == NULL)
511
0
        key->pub_key = EC_POINT_new(key->group);
512
0
    if (key->pub_key == NULL)
513
0
        return 0;
514
0
    if (EC_POINT_oct2point(key->group, key->pub_key, buf, len, ctx) == 0)
515
0
        return 0;
516
0
    /*
517
0
     * Save the point conversion form.
518
0
     * For non-custom curves the first octet of the buffer (excluding
519
0
     * the last significant bit) contains the point conversion form.
520
0
     * EC_POINT_oct2point() has already performed sanity checking of
521
0
     * the buffer so we know it is valid.
522
0
     */
523
0
    if ((key->group->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0)
524
0
        key->conv_form = (point_conversion_form_t)(buf[0] & ~0x01);
525
0
    return 1;
526
0
}
527
528
size_t EC_KEY_priv2oct(const EC_KEY *eckey,
529
                       unsigned char *buf, size_t len)
530
0
{
531
0
    if (eckey->group == NULL || eckey->group->meth == NULL)
532
0
        return 0;
533
0
    if (eckey->group->meth->priv2oct == NULL) {
534
0
        ECerr(EC_F_EC_KEY_PRIV2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
535
0
        return 0;
536
0
    }
537
0
538
0
    return eckey->group->meth->priv2oct(eckey, buf, len);
539
0
}
540
541
size_t ec_key_simple_priv2oct(const EC_KEY *eckey,
542
                              unsigned char *buf, size_t len)
543
0
{
544
0
    size_t buf_len;
545
0
546
0
    buf_len = (EC_GROUP_order_bits(eckey->group) + 7) / 8;
547
0
    if (eckey->priv_key == NULL)
548
0
        return 0;
549
0
    if (buf == NULL)
550
0
        return buf_len;
551
0
    else if (len < buf_len)
552
0
        return 0;
553
0
554
0
    /* Octetstring may need leading zeros if BN is to short */
555
0
556
0
    if (BN_bn2binpad(eckey->priv_key, buf, buf_len) == -1) {
557
0
        ECerr(EC_F_EC_KEY_SIMPLE_PRIV2OCT, EC_R_BUFFER_TOO_SMALL);
558
0
        return 0;
559
0
    }
560
0
561
0
    return buf_len;
562
0
}
563
564
int EC_KEY_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
565
0
{
566
0
    if (eckey->group == NULL || eckey->group->meth == NULL)
567
0
        return 0;
568
0
    if (eckey->group->meth->oct2priv == NULL) {
569
0
        ECerr(EC_F_EC_KEY_OCT2PRIV, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
570
0
        return 0;
571
0
    }
572
0
    return eckey->group->meth->oct2priv(eckey, buf, len);
573
0
}
574
575
int ec_key_simple_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
576
0
{
577
0
    if (eckey->priv_key == NULL)
578
0
        eckey->priv_key = BN_secure_new();
579
0
    if (eckey->priv_key == NULL) {
580
0
        ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_MALLOC_FAILURE);
581
0
        return 0;
582
0
    }
583
0
    eckey->priv_key = BN_bin2bn(buf, len, eckey->priv_key);
584
0
    if (eckey->priv_key == NULL) {
585
0
        ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_BN_LIB);
586
0
        return 0;
587
0
    }
588
0
    return 1;
589
0
}
590
591
size_t EC_KEY_priv2buf(const EC_KEY *eckey, unsigned char **pbuf)
592
0
{
593
0
    size_t len;
594
0
    unsigned char *buf;
595
0
596
0
    len = EC_KEY_priv2oct(eckey, NULL, 0);
597
0
    if (len == 0)
598
0
        return 0;
599
0
    if ((buf = OPENSSL_malloc(len)) == NULL) {
600
0
        ECerr(EC_F_EC_KEY_PRIV2BUF, ERR_R_MALLOC_FAILURE);
601
0
        return 0;
602
0
    }
603
0
    len = EC_KEY_priv2oct(eckey, buf, len);
604
0
    if (len == 0) {
605
0
        OPENSSL_free(buf);
606
0
        return 0;
607
0
    }
608
0
    *pbuf = buf;
609
0
    return len;
610
0
}
611
612
int EC_KEY_can_sign(const EC_KEY *eckey)
613
0
{
614
0
    if (eckey->group == NULL || eckey->group->meth == NULL
615
0
        || (eckey->group->meth->flags & EC_FLAGS_NO_SIGN))
616
0
        return 0;
617
0
    return 1;
618
0
}