Coverage Report

Created: 2021-10-21 13:35

/libfido2/src/es256.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2018-2021 Yubico AB. All rights reserved.
3
 * Use of this source code is governed by a BSD-style
4
 * license that can be found in the LICENSE file.
5
 */
6
7
#include <openssl/bn.h>
8
#include <openssl/ecdsa.h>
9
#include <openssl/obj_mac.h>
10
11
#include "fido.h"
12
#include "fido/es256.h"
13
14
static int
15
decode_coord(const cbor_item_t *item, void *xy, size_t xy_len)
16
10.4k
{
17
10.4k
        if (cbor_isa_bytestring(item) == false ||
18
10.4k
            cbor_bytestring_is_definite(item) == false ||
19
10.4k
            cbor_bytestring_length(item) != xy_len) {
20
67
                fido_log_debug("%s: cbor type", __func__);
21
67
                return (-1);
22
67
        }
23
24
10.3k
        memcpy(xy, cbor_bytestring_handle(item), xy_len);
25
26
10.3k
        return (0);
27
10.3k
}
28
29
static int
30
decode_pubkey_point(const cbor_item_t *key, const cbor_item_t *val, void *arg)
31
26.6k
{
32
26.6k
        es256_pk_t *k = arg;
33
34
26.6k
        if (cbor_isa_negint(key) == false ||
35
26.6k
            cbor_int_get_width(key) != CBOR_INT_8)
36
12.8k
                return (0); /* ignore */
37
38
13.7k
        switch (cbor_get_uint8(key)) {
39
5.30k
        case 1: /* x coordinate */
40
5.30k
                return (decode_coord(val, &k->x, sizeof(k->x)));
41
5.15k
        case 2: /* y coordinate */
42
5.15k
                return (decode_coord(val, &k->y, sizeof(k->y)));
43
3.25k
        }
44
45
3.25k
        return (0); /* ignore */
46
3.25k
}
47
48
int
49
es256_pk_decode(const cbor_item_t *item, es256_pk_t *k)
50
5.38k
{
51
5.38k
        if (cbor_isa_map(item) == false ||
52
5.38k
            cbor_map_is_definite(item) == false ||
53
5.38k
            cbor_map_iter(item, k, decode_pubkey_point) < 0) {
54
133
                fido_log_debug("%s: cbor type", __func__);
55
133
                return (-1);
56
133
        }
57
58
5.24k
        return (0);
59
5.24k
}
60
61
cbor_item_t *
62
es256_pk_encode(const es256_pk_t *pk, int ecdh)
63
3.31k
{
64
3.31k
        cbor_item_t             *item = NULL;
65
3.31k
        struct cbor_pair         argv[5];
66
3.31k
        int                      alg;
67
3.31k
        int                      ok = -1;
68
69
3.31k
        memset(argv, 0, sizeof(argv));
70
71
3.31k
        if ((item = cbor_new_definite_map(5)) == NULL)
72
3.31k
                goto fail;
73
74
        /* kty */
75
3.30k
        if ((argv[0].key = cbor_build_uint8(1)) == NULL ||
76
3.30k
            (argv[0].value = cbor_build_uint8(2)) == NULL ||
77
3.30k
            !cbor_map_add(item, argv[0]))
78
38
                goto fail;
79
80
        /*
81
         * "The COSEAlgorithmIdentifier used is -25 (ECDH-ES +
82
         * HKDF-256) although this is NOT the algorithm actually
83
         * used. Setting this to a different value may result in
84
         * compatibility issues."
85
         */
86
3.26k
        if (ecdh)
87
3.19k
                alg = COSE_ECDH_ES256;
88
3.26k
        else
89
3.26k
                alg = COSE_ES256;
90
91
        /* alg */
92
3.26k
        if ((argv[1].key = cbor_build_uint8(3)) == NULL ||
93
3.26k
            (argv[1].value = cbor_build_negint8((uint8_t)(-alg - 1))) == NULL ||
94
3.26k
            !cbor_map_add(item, argv[1]))
95
41
                goto fail;
96
97
        /* crv */
98
3.22k
        if ((argv[2].key = cbor_build_negint8(0)) == NULL ||
99
3.22k
            (argv[2].value = cbor_build_uint8(1)) == NULL ||
100
3.22k
            !cbor_map_add(item, argv[2]))
101
33
                goto fail;
102
103
        /* x */
104
3.18k
        if ((argv[3].key = cbor_build_negint8(1)) == NULL ||
105
3.18k
            (argv[3].value = cbor_build_bytestring(pk->x,
106
3.17k
            sizeof(pk->x))) == NULL || !cbor_map_add(item, argv[3]))
107
29
                goto fail;
108
109
        /* y */
110
3.15k
        if ((argv[4].key = cbor_build_negint8(2)) == NULL ||
111
3.15k
            (argv[4].value = cbor_build_bytestring(pk->y,
112
3.14k
            sizeof(pk->y))) == NULL || !cbor_map_add(item, argv[4]))
113
32
                goto fail;
114
115
3.12k
        ok = 0;
116
3.31k
fail:
117
3.31k
        if (ok < 0) {
118
184
                if (item != NULL) {
119
173
                        cbor_decref(&item);
120
173
                        item = NULL;
121
173
                }
122
184
        }
123
124
19.8k
        for (size_t i = 0; i < 5; i++) {
125
16.5k
                if (argv[i].key)
126
16.0k
                        cbor_decref(&argv[i].key);
127
16.5k
                if (argv[i].value)
128
16.0k
                        cbor_decref(&argv[i].value);
129
16.5k
        }
130
131
3.31k
        return (item);
132
3.12k
}
133
134
es256_sk_t *
135
es256_sk_new(void)
136
8.20k
{
137
8.20k
        return (calloc(1, sizeof(es256_sk_t)));
138
8.20k
}
139
140
void
141
es256_sk_free(es256_sk_t **skp)
142
8.20k
{
143
8.20k
        es256_sk_t *sk;
144
145
8.20k
        if (skp == NULL || (sk = *skp) == NULL)
146
8.20k
                return;
147
148
8.18k
        freezero(sk, sizeof(*sk));
149
8.18k
        *skp = NULL;
150
8.18k
}
151
152
es256_pk_t *
153
es256_pk_new(void)
154
16.2k
{
155
16.2k
        return (calloc(1, sizeof(es256_pk_t)));
156
16.2k
}
157
158
void
159
es256_pk_free(es256_pk_t **pkp)
160
31.2k
{
161
31.2k
        es256_pk_t *pk;
162
163
31.2k
        if (pkp == NULL || (pk = *pkp) == NULL)
164
31.2k
                return;
165
166
16.2k
        freezero(pk, sizeof(*pk));
167
16.2k
        *pkp = NULL;
168
16.2k
}
169
170
int
171
es256_pk_from_ptr(es256_pk_t *pk, const void *ptr, size_t len)
172
508
{
173
508
        const uint8_t *p = ptr;
174
175
508
        if (len < sizeof(*pk))
176
462
                return (FIDO_ERR_INVALID_ARGUMENT);
177
178
46
        if (len == sizeof(*pk) + 1 && *p == 0x04)
179
1
                memcpy(pk, ++p, sizeof(*pk)); /* uncompressed format */
180
45
        else
181
45
                memcpy(pk, ptr, sizeof(*pk)); /* libfido2 x||y format */
182
183
46
        return (FIDO_OK);
184
46
}
185
186
int
187
es256_pk_set_x(es256_pk_t *pk, const unsigned char *x)
188
72
{
189
72
        memcpy(pk->x, x, sizeof(pk->x));
190
191
72
        return (0);
192
72
}
193
194
int
195
es256_pk_set_y(es256_pk_t *pk, const unsigned char *y)
196
72
{
197
72
        memcpy(pk->y, y, sizeof(pk->y));
198
199
72
        return (0);
200
72
}
201
202
int
203
es256_sk_create(es256_sk_t *key)
204
8.15k
{
205
8.15k
        EVP_PKEY_CTX    *pctx = NULL;
206
8.15k
        EVP_PKEY_CTX    *kctx = NULL;
207
8.15k
        EVP_PKEY        *p = NULL;
208
8.15k
        EVP_PKEY        *k = NULL;
209
8.15k
        const EC_KEY    *ec;
210
8.15k
        const BIGNUM    *d;
211
8.15k
        const int        nid = NID_X9_62_prime256v1;
212
8.15k
        int              n;
213
8.15k
        int              ok = -1;
214
215
8.15k
        if ((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) == NULL ||
216
8.15k
            EVP_PKEY_paramgen_init(pctx) <= 0 ||
217
8.15k
            EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, nid) <= 0 ||
218
8.15k
            EVP_PKEY_paramgen(pctx, &p) <= 0) {
219
122
                fido_log_debug("%s: EVP_PKEY_paramgen", __func__);
220
122
                goto fail;
221
122
        }
222
223
8.02k
        if ((kctx = EVP_PKEY_CTX_new(p, NULL)) == NULL ||
224
8.02k
            EVP_PKEY_keygen_init(kctx) <= 0 || EVP_PKEY_keygen(kctx, &k) <= 0) {
225
88
                fido_log_debug("%s: EVP_PKEY_keygen", __func__);
226
88
                goto fail;
227
88
        }
228
229
7.94k
        if ((ec = EVP_PKEY_get0_EC_KEY(k)) == NULL ||
230
7.94k
            (d = EC_KEY_get0_private_key(ec)) == NULL ||
231
7.94k
            (n = BN_num_bytes(d)) < 0 || (size_t)n > sizeof(key->d) ||
232
7.94k
            (n = BN_bn2bin(d, key->d)) < 0 || (size_t)n > sizeof(key->d)) {
233
85
                fido_log_debug("%s: EC_KEY_get0_private_key", __func__);
234
85
                goto fail;
235
85
        }
236
237
7.85k
        ok = 0;
238
8.15k
fail:
239
8.15k
        if (p != NULL)
240
8.15k
                EVP_PKEY_free(p);
241
8.15k
        if (k != NULL)
242
8.15k
                EVP_PKEY_free(k);
243
8.15k
        if (pctx != NULL)
244
8.15k
                EVP_PKEY_CTX_free(pctx);
245
8.15k
        if (kctx != NULL)
246
8.15k
                EVP_PKEY_CTX_free(kctx);
247
248
8.15k
        return (ok);
249
7.85k
}
250
251
EVP_PKEY *
252
es256_pk_to_EVP_PKEY(const es256_pk_t *k)
253
4.53k
{
254
4.53k
        BN_CTX          *bnctx = NULL;
255
4.53k
        EC_KEY          *ec = NULL;
256
4.53k
        EC_POINT        *q = NULL;
257
4.53k
        EVP_PKEY        *pkey = NULL;
258
4.53k
        BIGNUM          *x = NULL;
259
4.53k
        BIGNUM          *y = NULL;
260
4.53k
        const EC_GROUP  *g = NULL;
261
4.53k
        const int        nid = NID_X9_62_prime256v1;
262
4.53k
        int              ok = -1;
263
264
4.53k
        if ((bnctx = BN_CTX_new()) == NULL)
265
4.53k
                goto fail;
266
267
4.51k
        BN_CTX_start(bnctx);
268
269
4.51k
        if ((x = BN_CTX_get(bnctx)) == NULL ||
270
4.51k
            (y = BN_CTX_get(bnctx)) == NULL)
271
4.51k
                goto fail;
272
273
4.48k
        if (BN_bin2bn(k->x, sizeof(k->x), x) == NULL ||
274
4.48k
            BN_bin2bn(k->y, sizeof(k->y), y) == NULL) {
275
33
                fido_log_debug("%s: BN_bin2bn", __func__);
276
33
                goto fail;
277
33
        }
278
279
4.45k
        if ((ec = EC_KEY_new_by_curve_name(nid)) == NULL ||
280
4.45k
            (g = EC_KEY_get0_group(ec)) == NULL) {
281
34
                fido_log_debug("%s: EC_KEY init", __func__);
282
34
                goto fail;
283
34
        }
284
285
4.41k
        if ((q = EC_POINT_new(g)) == NULL ||
286
4.41k
            EC_POINT_set_affine_coordinates_GFp(g, q, x, y, bnctx) == 0 ||
287
4.41k
            EC_KEY_set_public_key(ec, q) == 0) {
288
615
                fido_log_debug("%s: EC_KEY_set_public_key", __func__);
289
615
                goto fail;
290
615
        }
291
292
3.80k
        if ((pkey = EVP_PKEY_new()) == NULL ||
293
3.80k
            EVP_PKEY_assign_EC_KEY(pkey, ec) == 0) {
294
19
                fido_log_debug("%s: EVP_PKEY_assign_EC_KEY", __func__);
295
19
                goto fail;
296
19
        }
297
298
3.78k
        ec = NULL; /* at this point, ec belongs to evp */
299
300
3.78k
        ok = 0;
301
4.53k
fail:
302
4.53k
        if (bnctx != NULL) {
303
4.51k
                BN_CTX_end(bnctx);
304
4.51k
                BN_CTX_free(bnctx);
305
4.51k
        }
306
307
4.53k
        if (ec != NULL)
308
4.53k
                EC_KEY_free(ec);
309
4.53k
        if (q != NULL)
310
4.53k
                EC_POINT_free(q);
311
312
4.53k
        if (ok < 0 && pkey != NULL) {
313
9
                EVP_PKEY_free(pkey);
314
9
                pkey = NULL;
315
9
        }
316
317
4.53k
        return (pkey);
318
3.78k
}
319
320
int
321
es256_pk_from_EC_KEY(es256_pk_t *pk, const EC_KEY *ec)
322
7.74k
{
323
7.74k
        BN_CTX          *bnctx = NULL;
324
7.74k
        BIGNUM          *x = NULL;
325
7.74k
        BIGNUM          *y = NULL;
326
7.74k
        const EC_POINT  *q = NULL;
327
7.74k
        const EC_GROUP  *g = NULL;
328
7.74k
        int              ok = FIDO_ERR_INTERNAL;
329
7.74k
        int              n;
330
331
7.74k
        if ((q = EC_KEY_get0_public_key(ec)) == NULL ||
332
7.74k
            (g = EC_KEY_get0_group(ec)) == NULL ||
333
7.74k
            (bnctx = BN_CTX_new()) == NULL)
334
7.74k
                goto fail;
335
336
7.67k
        BN_CTX_start(bnctx);
337
338
7.67k
        if ((x = BN_CTX_get(bnctx)) == NULL ||
339
7.67k
            (y = BN_CTX_get(bnctx)) == NULL)
340
7.67k
                goto fail;
341
342
7.60k
        if (EC_POINT_get_affine_coordinates_GFp(g, q, x, y, bnctx) == 0 ||
343
7.60k
            (n = BN_num_bytes(x)) < 0 || (size_t)n > sizeof(pk->x) ||
344
7.60k
            (n = BN_num_bytes(y)) < 0 || (size_t)n > sizeof(pk->y)) {
345
37
                fido_log_debug("%s: EC_POINT_get_affine_coordinates_GFp",
346
37
                    __func__);
347
37
                goto fail;
348
37
        }
349
350
7.56k
        if ((n = BN_bn2bin(x, pk->x)) < 0 || (size_t)n > sizeof(pk->x) ||
351
7.56k
            (n = BN_bn2bin(y, pk->y)) < 0 || (size_t)n > sizeof(pk->y)) {
352
72
                fido_log_debug("%s: BN_bn2bin", __func__);
353
72
                goto fail;
354
72
        }
355
356
7.49k
        ok = FIDO_OK;
357
7.74k
fail:
358
7.74k
        if (bnctx != NULL) {
359
7.67k
                BN_CTX_end(bnctx);
360
7.67k
                BN_CTX_free(bnctx);
361
7.67k
        }
362
363
7.74k
        return (ok);
364
7.49k
}
365
366
int
367
es256_pk_from_EVP_PKEY(es256_pk_t *pk, const EVP_PKEY *pkey)
368
20
{
369
20
        EC_KEY *ec;
370
371
20
        if (EVP_PKEY_base_id(pkey) != EVP_PKEY_EC ||
372
20
            (ec = EVP_PKEY_get0(pkey)) == NULL)
373
20
                return (FIDO_ERR_INVALID_ARGUMENT);
374
375
20
        return (es256_pk_from_EC_KEY(pk, ec));
376
20
}
377
378
EVP_PKEY *
379
es256_sk_to_EVP_PKEY(const es256_sk_t *k)
380
3.75k
{
381
3.75k
        BN_CTX          *bnctx = NULL;
382
3.75k
        EC_KEY          *ec = NULL;
383
3.75k
        EVP_PKEY        *pkey = NULL;
384
3.75k
        BIGNUM          *d = NULL;
385
3.75k
        const int        nid = NID_X9_62_prime256v1;
386
3.75k
        int              ok = -1;
387
388
3.75k
        if ((bnctx = BN_CTX_new()) == NULL)
389
3.75k
                goto fail;
390
391
3.73k
        BN_CTX_start(bnctx);
392
393
3.73k
        if ((d = BN_CTX_get(bnctx)) == NULL ||
394
3.73k
            BN_bin2bn(k->d, sizeof(k->d), d) == NULL) {
395
20
                fido_log_debug("%s: BN_bin2bn", __func__);
396
20
                goto fail;
397
20
        }
398
399
3.71k
        if ((ec = EC_KEY_new_by_curve_name(nid)) == NULL ||
400
3.71k
            EC_KEY_set_private_key(ec, d) == 0) {
401
10
                fido_log_debug("%s: EC_KEY_set_private_key", __func__);
402
10
                goto fail;
403
10
        }
404
405
3.70k
        if ((pkey = EVP_PKEY_new()) == NULL ||
406
3.70k
            EVP_PKEY_assign_EC_KEY(pkey, ec) == 0) {
407
24
                fido_log_debug("%s: EVP_PKEY_assign_EC_KEY", __func__);
408
24
                goto fail;
409
24
        }
410
411
3.68k
        ec = NULL; /* at this point, ec belongs to evp */
412
413
3.68k
        ok = 0;
414
3.75k
fail:
415
3.75k
        if (bnctx != NULL) {
416
3.73k
                BN_CTX_end(bnctx);
417
3.73k
                BN_CTX_free(bnctx);
418
3.73k
        }
419
420
3.75k
        if (ec != NULL)
421
3.75k
                EC_KEY_free(ec);
422
423
3.75k
        if (ok < 0 && pkey != NULL) {
424
13
                EVP_PKEY_free(pkey);
425
13
                pkey = NULL;
426
13
        }
427
428
3.75k
        return (pkey);
429
3.68k
}
430
431
int
432
es256_derive_pk(const es256_sk_t *sk, es256_pk_t *pk)
433
7.85k
{
434
7.85k
        BIGNUM          *d = NULL;
435
7.85k
        EC_KEY          *ec = NULL;
436
7.85k
        EC_POINT        *q = NULL;
437
7.85k
        const EC_GROUP  *g = NULL;
438
7.85k
        const int        nid = NID_X9_62_prime256v1;
439
7.85k
        int              ok = -1;
440
441
7.85k
        if ((d = BN_bin2bn(sk->d, (int)sizeof(sk->d), NULL)) == NULL ||
442
7.85k
            (ec = EC_KEY_new_by_curve_name(nid)) == NULL ||
443
7.85k
            (g = EC_KEY_get0_group(ec)) == NULL ||
444
7.85k
            (q = EC_POINT_new(g)) == NULL) {
445
133
                fido_log_debug("%s: get", __func__);
446
133
                goto fail;
447
133
        }
448
449
7.72k
        if (EC_POINT_mul(g, q, d, NULL, NULL, NULL) == 0 ||
450
7.72k
            EC_KEY_set_public_key(ec, q) == 0 ||
451
7.72k
            es256_pk_from_EC_KEY(pk, ec) != FIDO_OK) {
452
244
                fido_log_debug("%s: set", __func__);
453
244
                goto fail;
454
244
        }
455
456
7.47k
        ok = 0;
457
7.85k
fail:
458
7.85k
        if (d != NULL)
459
7.85k
                BN_clear_free(d);
460
7.85k
        if (q != NULL)
461
7.85k
                EC_POINT_free(q);
462
7.85k
        if (ec != NULL)
463
7.85k
                EC_KEY_free(ec);
464
465
7.85k
        return (ok);
466
7.47k
}
467
468
int
469
es256_verify_sig(const fido_blob_t *dgst, EVP_PKEY *pkey,
470
    const fido_blob_t *sig)
471
48
{
472
48
        EVP_PKEY_CTX    *pctx = NULL;
473
48
        int              ok = -1;
474
475
48
        if (EVP_PKEY_base_id(pkey) != EVP_PKEY_EC) {
476
0
                fido_log_debug("%s: EVP_PKEY_base_id", __func__);
477
0
                goto fail;
478
0
        }
479
480
48
        if ((pctx = EVP_PKEY_CTX_new(pkey, NULL)) == NULL ||
481
48
            EVP_PKEY_verify_init(pctx) != 1 ||
482
48
            EVP_PKEY_verify(pctx, sig->ptr, sig->len, dgst->ptr,
483
48
            dgst->len) != 1) {
484
48
                fido_log_debug("%s: EVP_PKEY_verify", __func__);
485
48
                goto fail;
486
48
        }
487
488
0
        ok = 0;
489
48
fail:
490
48
        EVP_PKEY_CTX_free(pctx);
491
492
48
        return (ok);
493
0
}
494
495
int
496
es256_pk_verify_sig(const fido_blob_t *dgst, const es256_pk_t *pk,
497
    const fido_blob_t *sig)
498
43
{
499
43
        EVP_PKEY        *pkey;
500
43
        int              ok = -1;
501
502
43
        if ((pkey = es256_pk_to_EVP_PKEY(pk)) == NULL ||
503
43
            es256_verify_sig(dgst, pkey, sig) < 0) {
504
43
                fido_log_debug("%s: es256_verify_sig", __func__);
505
43
                goto fail;
506
43
        }
507
508
0
        ok = 0;
509
43
fail:
510
43
        EVP_PKEY_free(pkey);
511
512
43
        return (ok);
513
0
}