Coverage Report

Created: 2022-01-17 10:46

/libfido2/src/nfc_linux.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2020 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 <sys/types.h>
8
#include <sys/uio.h>
9
#include <sys/socket.h>
10
11
#include <linux/nfc.h>
12
13
#include <errno.h>
14
#include <libudev.h>
15
#include <signal.h>
16
#include <stdio.h>
17
#include <string.h>
18
#include <unistd.h>
19
20
#include "fido.h"
21
#include "fido/param.h"
22
#include "netlink.h"
23
#include "iso7816.h"
24
25
1.60k
#define TX_CHUNK_SIZE   240
26
27
static const uint8_t aid[] = { 0xa0, 0x00, 0x00, 0x06, 0x47, 0x2f, 0x00, 0x01 };
28
static const uint8_t v_u2f[] = { 'U', '2', 'F', '_', 'V', '2' };
29
static const uint8_t v_fido[] = { 'F', 'I', 'D', 'O', '_', '2', '_', '0' };
30
31
struct nfc_linux {
32
        int             fd;
33
        uint32_t        dev;
34
        uint32_t        target;
35
        sigset_t        sigmask;
36
        const sigset_t *sigmaskp;
37
        struct fido_nl *nl;
38
};
39
40
static int
41
tx_short_apdu(fido_dev_t *d, const iso7816_header_t *h, const uint8_t *payload,
42
    uint8_t payload_len, uint8_t cla_flags)
43
1.52k
{
44
1.52k
        uint8_t apdu[5 + UINT8_MAX + 1];
45
1.52k
        uint8_t sw[2];
46
1.52k
        size_t apdu_len;
47
1.52k
        int ok = -1;
48
49
1.52k
        memset(&apdu, 0, sizeof(apdu));
50
1.52k
        apdu[0] = h->cla | cla_flags;
51
1.52k
        apdu[1] = h->ins;
52
1.52k
        apdu[2] = h->p1;
53
1.52k
        apdu[3] = h->p2;
54
1.52k
        apdu[4] = payload_len;
55
1.52k
        memcpy(&apdu[5], payload, payload_len);
56
1.52k
        apdu_len = (size_t)(5 + payload_len + 1);
57
58
1.52k
        if (d->io.write(d->io_handle, apdu, apdu_len) < 0) {
59
11
                fido_log_debug("%s: write", __func__);
60
11
                goto fail;
61
11
        }
62
63
1.51k
        if (cla_flags & 0x10) {
64
65
                if (d->io.read(d->io_handle, sw, sizeof(sw), -1) != 2) {
65
3
                        fido_log_debug("%s: read", __func__);
66
3
                        goto fail;
67
3
                }
68
62
                if ((sw[0] << 8 | sw[1]) != SW_NO_ERROR) {
69
57
                        fido_log_debug("%s: unexpected sw", __func__);
70
57
                        goto fail;
71
57
                }
72
1.45k
        }
73
74
1.45k
        ok = 0;
75
1.52k
fail:
76
1.52k
        explicit_bzero(apdu, sizeof(apdu));
77
78
1.52k
        return (ok);
79
1.45k
}
80
81
static int
82
nfc_do_tx(fido_dev_t *d, const uint8_t *apdu_ptr, size_t apdu_len)
83
1.52k
{
84
1.52k
        iso7816_header_t h;
85
86
1.52k
        if (fido_buf_read(&apdu_ptr, &apdu_len, &h, sizeof(h)) < 0) {
87
0
                fido_log_debug("%s: header", __func__);
88
0
                return (-1);
89
0
        }
90
1.52k
        if (apdu_len < 2) {
91
0
                fido_log_debug("%s: apdu_len %zu", __func__, apdu_len);
92
0
                return (-1);
93
0
        }
94
95
1.52k
        apdu_len -= 2; /* trim le1 le2 */
96
97
1.52k
        while (apdu_len > TX_CHUNK_SIZE) {
98
65
                if (tx_short_apdu(d, &h, apdu_ptr, TX_CHUNK_SIZE, 0x10) < 0) {
99
60
                        fido_log_debug("%s: chain", __func__);
100
60
                        return (-1);
101
60
                }
102
5
                apdu_ptr += TX_CHUNK_SIZE;
103
5
                apdu_len -= TX_CHUNK_SIZE;
104
5
        }
105
106
1.52k
        if (tx_short_apdu(d, &h, apdu_ptr, (uint8_t)apdu_len, 0) < 0) {
107
11
                fido_log_debug("%s: tx_short_apdu", __func__);
108
11
                return (-1);
109
11
        }
110
 
111
1.45k
        return (0);
112
1.45k
}
113
114
int
115
fido_nfc_tx(fido_dev_t *d, uint8_t cmd, const unsigned char *buf, size_t count)
116
1.59k
{
117
1.59k
        iso7816_apdu_t *apdu = NULL;
118
1.59k
        const uint8_t *ptr;
119
1.59k
        size_t len;
120
1.59k
        int ok = -1;
121
122
1.59k
        switch (cmd) {
123
745
        case CTAP_CMD_INIT: /* select */
124
745
                if ((apdu = iso7816_new(0, 0xa4, 0x04, sizeof(aid))) == NULL ||
125
745
                    iso7816_add(apdu, aid, sizeof(aid)) < 0) {
126
10
                        fido_log_debug("%s: iso7816", __func__);
127
10
                        goto fail;
128
10
                }
129
735
                break;
130
735
        case CTAP_CMD_CBOR: /* wrap cbor */
131
516
                if (count > UINT16_MAX || (apdu = iso7816_new(0x80, 0x10, 0x80,
132
513
                    (uint16_t)count)) == NULL ||
133
516
                    iso7816_add(apdu, buf, count) < 0) {
134
13
                        fido_log_debug("%s: iso7816", __func__);
135
13
                        goto fail;
136
13
                }
137
503
                break;
138
503
        case CTAP_CMD_MSG: /* already an apdu */
139
283
                break;
140
503
        default:
141
46
                fido_log_debug("%s: cmd=%02x", __func__, cmd);
142
46
                goto fail;
143
1.52k
        }
144
145
1.52k
        if (apdu != NULL) {
146
1.23k
                ptr = iso7816_ptr(apdu);
147
1.23k
                len = iso7816_len(apdu);
148
1.23k
        } else {
149
283
                ptr = buf;
150
283
                len = count;
151
283
        }
152
153
1.52k
        if (nfc_do_tx(d, ptr, len) < 0) {
154
71
                fido_log_debug("%s: nfc_do_tx", __func__);
155
71
                goto fail;
156
71
        }
157
158
1.45k
        ok = 0;
159
1.59k
fail:
160
1.59k
        iso7816_free(&apdu);
161
162
1.59k
        return (ok);
163
1.45k
}
164
165
static int
166
rx_init(fido_dev_t *d, unsigned char *buf, size_t count, int ms)
167
716
{
168
716
        fido_ctap_info_t *attr = (fido_ctap_info_t *)buf;
169
716
        uint8_t f[64];
170
716
        int n;
171
172
716
        if (count != sizeof(*attr)) {
173
0
                fido_log_debug("%s: count=%zu", __func__, count);
174
0
                return (-1);
175
0
        }
176
177
716
        memset(attr, 0, sizeof(*attr));
178
179
716
        if ((n = d->io.read(d->io_handle, f, sizeof(f), ms)) < 2 ||
180
716
            (f[n - 2] << 8 | f[n - 1]) != SW_NO_ERROR) {
181
227
                fido_log_debug("%s: read", __func__);
182
227
                return (-1);
183
227
        }
184
185
489
        n -= 2;
186
187
489
        if (n == sizeof(v_u2f) && memcmp(f, v_u2f, sizeof(v_u2f)) == 0)
188
0
                attr->flags = FIDO_CAP_CBOR;
189
489
        else if (n == sizeof(v_fido) && memcmp(f, v_fido, sizeof(v_fido)) == 0)
190
0
                attr->flags = FIDO_CAP_CBOR | FIDO_CAP_NMSG;
191
489
        else {
192
489
                fido_log_debug("%s: unknown version string", __func__);
193
489
#ifdef FIDO_FUZZ
194
489
                attr->flags = FIDO_CAP_CBOR | FIDO_CAP_NMSG;
195
#else
196
                return (-1);
197
#endif
198
489
        }
199
200
489
        memcpy(&attr->nonce, &d->nonce, sizeof(attr->nonce)); /* XXX */
201
202
489
        return ((int)count);
203
489
}
204
205
static int
206
tx_get_response(fido_dev_t *d, uint8_t count)
207
41
{
208
41
        uint8_t apdu[5];
209
210
41
        memset(apdu, 0, sizeof(apdu));
211
41
        apdu[1] = 0xc0; /* GET_RESPONSE */
212
41
        apdu[4] = count;
213
214
41
        if (d->io.write(d->io_handle, apdu, sizeof(apdu)) < 0) {
215
6
                fido_log_debug("%s: write", __func__);
216
6
                return (-1);
217
6
        }
218
219
35
        return (0);
220
35
}
221
222
static int
223
rx_apdu(fido_dev_t *d, uint8_t sw[2], unsigned char **buf, size_t *count, int *ms)
224
724
{
225
724
        uint8_t f[256 + 2];
226
724
        struct timespec ts;
227
724
        int n, ok = -1;
228
229
724
        if (fido_time_now(&ts) != 0)
230
16
                goto fail;
231
232
708
        if ((n = d->io.read(d->io_handle, f, sizeof(f), *ms)) < 2) {
233
417
                fido_log_debug("%s: read", __func__);
234
417
                goto fail;
235
417
        }
236
237
291
        if (fido_time_delta(&ts, ms) != 0)
238
9
                goto fail;
239
240
282
        if (fido_buf_write(buf, count, f, (size_t)(n - 2)) < 0) {
241
0
                fido_log_debug("%s: fido_buf_write", __func__);
242
0
                goto fail;
243
0
        }
244
245
282
        memcpy(sw, f + n - 2, 2);
246
247
282
        ok = 0;
248
724
fail:
249
724
        explicit_bzero(f, sizeof(f));
250
251
724
        return (ok);
252
282
}
253
254
static int
255
rx_msg(fido_dev_t *d, unsigned char *buf, size_t count, int ms)
256
689
{
257
689
        uint8_t sw[2];
258
689
        const size_t bufsiz = count;
259
260
689
        if (rx_apdu(d, sw, &buf, &count, &ms) < 0) {
261
425
                fido_log_debug("%s: preamble", __func__);
262
425
                return (-1);
263
425
        }
264
265
282
        while (sw[0] == SW1_MORE_DATA)
266
264
                if (tx_get_response(d, sw[1]) < 0 ||
267
41
                    rx_apdu(d, sw, &buf, &count, &ms) < 0) {
268
23
                        fido_log_debug("%s: chain", __func__);
269
23
                        return (-1);
270
23
                }
271
272
264
        if (fido_buf_write(&buf, &count, sw, sizeof(sw)) < 0) {
273
0
                fido_log_debug("%s: sw", __func__);
274
0
                return (-1);
275
0
        }
276
277
241
        if (bufsiz - count > INT_MAX) {
278
0
                fido_log_debug("%s: bufsiz", __func__);
279
0
                return (-1);
280
0
        }
281
282
241
        return ((int)(bufsiz - count));
283
241
}
284
285
static int
286
rx_cbor(fido_dev_t *d, unsigned char *buf, size_t count, int ms)
287
434
{
288
434
        int r;
289
290
434
        if ((r = rx_msg(d, buf, count, ms)) < 2)
291
250
                return (-1);
292
293
184
        return (r - 2);
294
184
}
295
296
int
297
fido_nfc_rx(fido_dev_t *d, uint8_t cmd, unsigned char *buf, size_t count, int ms)
298
1.40k
{
299
1.40k
        switch (cmd) {
300
716
        case CTAP_CMD_INIT:
301
716
                return (rx_init(d, buf, count, ms));
302
434
        case CTAP_CMD_CBOR:
303
434
                return (rx_cbor(d, buf, count, ms));
304
255
        case CTAP_CMD_MSG:
305
255
                return (rx_msg(d, buf, count, ms));
306
0
        default:
307
0
                fido_log_debug("%s: cmd=%02x", __func__, cmd);
308
0
                return (-1);
309
1.40k
        }
310
1.40k
}
311
312
static char *
313
get_parent_attr(struct udev_device *dev, const char *subsystem,
314
    const char *devtype, const char *attr)
315
2.29M
{
316
2.29M
        struct udev_device *parent;
317
2.29M
        const char *value;
318
319
2.29M
        if ((parent = udev_device_get_parent_with_subsystem_devtype(dev,
320
2.29M
            subsystem, devtype)) == NULL || (value =
321
2.28M
            udev_device_get_sysattr_value(parent, attr)) == NULL)
322
2.29M
                return (NULL);
323
324
1.14M
        return (strdup(value));
325
1.14M
}
326
327
static char *
328
get_usb_attr(struct udev_device *dev, const char *attr)
329
2.29M
{
330
2.29M
        return (get_parent_attr(dev, "usb", "usb_device", attr));
331
2.29M
}
332
333
static int
334
to_int(const char *str, int base)
335
568k
{
336
568k
        char *ep;
337
568k
        long long ll;
338
339
568k
        ll = strtoll(str, &ep, base);
340
568k
        if (str == ep || *ep != '\0')
341
0
                return (-1);
342
568k
        else if (ll == LLONG_MIN && errno == ERANGE)
343
568k
                return (-1);
344
568k
        else if (ll == LLONG_MAX && errno == ERANGE)
345
568k
                return (-1);
346
568k
        else if (ll < 0 || ll > INT_MAX)
347
568k
                return (-1);
348
349
568k
        return ((int)ll);
350
568k
}
351
352
static int
353
copy_info(fido_dev_info_t *di, struct udev *udev,
354
    struct udev_list_entry *udev_entry)
355
575k
{
356
575k
        const char *name;
357
575k
        char *str;
358
575k
        struct udev_device *dev = NULL;
359
575k
        void *ctx = NULL;
360
575k
        int id, ok = -1;
361
362
575k
        memset(di, 0, sizeof(*di));
363
364
575k
        if ((name = udev_list_entry_get_name(udev_entry)) == NULL ||
365
575k
            (dev = udev_device_new_from_syspath(udev, name)) == NULL)
366
575k
                goto fail;
367
572k
        if (asprintf(&di->path, "%s/%s", FIDO_NFC_PREFIX, name) == -1)
368
0
                goto fail;
369
572k
        if ((di->manufacturer = get_usb_attr(dev, "manufacturer")) == NULL)
370
572k
                di->manufacturer = strdup("");
371
572k
        if ((di->product = get_usb_attr(dev, "product")) == NULL)
372
572k
                di->product = strdup("");
373
572k
        if (di->manufacturer == NULL || di->product == NULL)
374
572k
                goto fail;
375
        /* XXX assumes USB for vendor/product info */
376
572k
        if ((str = get_usb_attr(dev, "idVendor")) != NULL &&
377
572k
            (id = to_int(str, 16)) > 0 && id <= UINT16_MAX)
378
572k
                di->vendor_id = (int16_t)id;
379
572k
        free(str);
380
572k
        if ((str = get_usb_attr(dev, "idProduct")) != NULL &&
381
572k
            (id = to_int(str, 16)) > 0 && id <= UINT16_MAX)
382
572k
                di->product_id = (int16_t)id;
383
572k
        free(str);
384
385
572k
        if ((ctx = fido_nfc_open(di->path)) == NULL) {
386
572k
                fido_log_debug("%s: fido_nfc_open", __func__);
387
572k
                goto fail;
388
572k
        }
389
390
0
        ok = 0;
391
575k
fail:
392
575k
        if (dev != NULL)
393
575k
                udev_device_unref(dev);
394
575k
        if (ctx != NULL)
395
575k
                fido_nfc_close(ctx);
396
397
575k
        if (ok < 0) {
398
575k
                free(di->path);
399
575k
                free(di->manufacturer);
400
575k
                free(di->product);
401
575k
                explicit_bzero(di, sizeof(*di));
402
575k
        }
403
404
575k
        return (ok);
405
0
}
406
407
static int
408
sysnum_from_syspath(const char *path)
409
572k
{
410
572k
        struct udev *udev = NULL;
411
572k
        struct udev_device *dev = NULL;
412
572k
        const char *str;
413
572k
        int idx;
414
415
572k
        if ((udev = udev_new()) == NULL ||
416
572k
            (dev = udev_device_new_from_syspath(udev, path)) == NULL ||
417
572k
            (str = udev_device_get_sysnum(dev)) == NULL)
418
572k
                idx = -1;
419
568k
        else
420
568k
                idx = to_int(str, 10);
421
422
572k
        if (dev != NULL)
423
572k
                udev_device_unref(dev);
424
572k
        if (udev != NULL)
425
572k
                udev_unref(udev);
426
427
572k
        return (idx);
428
572k
}
429
430
int
431
fido_nfc_manifest(fido_dev_info_t *devlist, size_t ilen, size_t *olen)
432
1.20k
{
433
1.20k
        struct udev *udev = NULL;
434
1.20k
        struct udev_enumerate *udev_enum = NULL;
435
1.20k
        struct udev_list_entry *udev_list;
436
1.20k
        struct udev_list_entry *udev_entry;
437
1.20k
        int r = FIDO_ERR_INTERNAL;
438
439
1.20k
        *olen = 0;
440
441
1.20k
        if (ilen == 0)
442
3
                return (FIDO_OK);
443
444
1.20k
        if (devlist == NULL)
445
1.20k
                return (FIDO_ERR_INVALID_ARGUMENT);
446
447
1.20k
        if ((udev = udev_new()) == NULL ||
448
1.20k
            (udev_enum = udev_enumerate_new(udev)) == NULL)
449
1.20k
                goto fail;
450
451
1.19k
        if (udev_enumerate_add_match_subsystem(udev_enum, "nfc") < 0 ||
452
1.19k
            udev_enumerate_scan_devices(udev_enum) < 0)
453
5
                goto fail;
454
455
1.19k
        if ((udev_list = udev_enumerate_get_list_entry(udev_enum)) == NULL) {
456
4
                r = FIDO_OK; /* zero nfc devices */
457
4
                goto fail;
458
4
        }
459
460
575k
        udev_list_entry_foreach(udev_entry, udev_list) {
461
575k
                if (copy_info(&devlist[*olen], udev, udev_entry) == 0) {
462
0
                        devlist[*olen].io = (fido_dev_io_t) {
463
0
                                fido_nfc_open,
464
0
                                fido_nfc_close,
465
0
                                fido_nfc_read,
466
0
                                fido_nfc_write,
467
0
                        };
468
0
                        devlist[*olen].transport = (fido_dev_transport_t) {
469
0
                                fido_nfc_rx,
470
0
                                fido_nfc_tx,
471
0
                        };
472
0
                        if (++(*olen) == ilen)
473
0
                                break;
474
0
                }
475
575k
        }
476
477
1.19k
        r = FIDO_OK;
478
1.20k
fail:
479
1.20k
        if (udev_enum != NULL)
480
1.20k
                udev_enumerate_unref(udev_enum);
481
1.20k
        if (udev != NULL)
482
1.20k
                udev_unref(udev);
483
484
1.20k
        return (r);
485
1.19k
}
486
487
static int
488
nfc_target_connect(struct nfc_linux *ctx)
489
0
{
490
0
        struct sockaddr_nfc sa;
491
492
0
        memset(&sa, 0, sizeof(sa));
493
0
        sa.sa_family = AF_NFC;
494
0
        sa.dev_idx = ctx->dev;
495
0
        sa.target_idx = ctx->target;
496
0
        sa.nfc_protocol = NFC_PROTO_ISO14443;
497
498
0
        if ((ctx->fd = socket(AF_NFC, SOCK_SEQPACKET | SOCK_CLOEXEC,
499
0
            NFC_SOCKPROTO_RAW)) == -1) {
500
0
                fido_log_error(errno, "%s: socket", __func__);
501
0
                return (-1);
502
0
        }
503
0
        if (connect(ctx->fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
504
0
                fido_log_error(errno, "%s: connect", __func__);
505
0
                if (close(ctx->fd) == -1)
506
0
                        fido_log_error(errno, "%s: close", __func__);
507
0
                ctx->fd = -1;
508
0
                return (-1);
509
0
        }
510
511
0
        return (0);
512
0
}
513
514
static void
515
nfc_free(struct nfc_linux **ctx_p)
516
1.14M
{
517
1.14M
        struct nfc_linux *ctx;
518
519
1.14M
        if (ctx_p == NULL || (ctx = *ctx_p) == NULL)
520
1.14M
                return;
521
566k
        if (ctx->fd != -1 && close(ctx->fd) == -1)
522
566k
                fido_log_error(errno, "%s: close", __func__);
523
566k
        if (ctx->nl != NULL)
524
566k
                fido_nl_free(&ctx->nl);
525
526
566k
        free(ctx);
527
566k
        *ctx_p = NULL;
528
566k
}
529
530
static struct nfc_linux *
531
nfc_new(uint32_t dev)
532
568k
{
533
568k
        struct nfc_linux *ctx;
534
535
568k
        if ((ctx = calloc(1, sizeof(*ctx))) == NULL ||
536
568k
            (ctx->nl = fido_nl_new()) == NULL) {
537
567k
                nfc_free(&ctx);
538
567k
                return (NULL);
539
567k
        }
540
541
288
        ctx->fd = -1;
542
288
        ctx->dev = dev;
543
544
288
        return (ctx);
545
288
}
546
547
void *
548
fido_nfc_open(const char *path)
549
572k
{
550
572k
        struct nfc_linux *ctx = NULL;
551
572k
        int idx;
552
553
572k
        if (strncmp(path, FIDO_NFC_PREFIX, strlen(FIDO_NFC_PREFIX)) != 0) {
554
0
                fido_log_debug("%s: bad prefix", __func__);
555
0
                goto fail;
556
0
        }
557
572k
        if ((idx = sysnum_from_syspath(path + strlen(FIDO_NFC_PREFIX))) < 0 ||
558
572k
            (ctx = nfc_new((uint32_t)idx)) == NULL) {
559
572k
                fido_log_debug("%s: nfc_new", __func__);
560
572k
                goto fail;
561
572k
        }
562
288
        if (fido_nl_power_nfc(ctx->nl, ctx->dev) < 0 ||
563
288
            fido_nl_get_nfc_target(ctx->nl, ctx->dev, &ctx->target) < 0 ||
564
288
            nfc_target_connect(ctx) < 0) {
565
288
                fido_log_debug("%s: netlink", __func__);
566
288
                goto fail;
567
288
        }
568
569
0
        return (ctx);
570
572k
fail:
571
572k
        nfc_free(&ctx);
572
572k
        return (NULL);
573
0
}
574
575
void
576
fido_nfc_close(void *handle)
577
0
{
578
0
        struct nfc_linux *ctx = handle;
579
580
0
        nfc_free(&ctx);
581
0
}
582
583
int
584
fido_nfc_set_sigmask(void *handle, const fido_sigset_t *sigmask)
585
0
{
586
0
        struct nfc_linux *ctx = handle;
587
588
0
        ctx->sigmask = *sigmask;
589
0
        ctx->sigmaskp = &ctx->sigmask;
590
591
0
        return (FIDO_OK);
592
0
}
593
594
int
595
fido_nfc_read(void *handle, unsigned char *buf, size_t len, int ms)
596
0
{
597
0
        struct nfc_linux *ctx = handle;
598
0
        struct iovec iov[2];
599
0
        uint8_t preamble;
600
0
        ssize_t r;
601
602
0
        memset(&iov, 0, sizeof(iov));
603
0
        iov[0].iov_base = &preamble;
604
0
        iov[0].iov_len = sizeof(preamble);
605
0
        iov[1].iov_base = buf;
606
0
        iov[1].iov_len = len;
607
608
0
        if (fido_hid_unix_wait(ctx->fd, ms, ctx->sigmaskp) < 0) {
609
0
                fido_log_debug("%s: fido_hid_unix_wait", __func__);
610
0
                return (-1);
611
0
        }
612
0
        if ((r = readv(ctx->fd, iov, nitems(iov))) == -1) {
613
0
                fido_log_error(errno, "%s: read", __func__);
614
0
                return (-1);
615
0
        }
616
0
        if (r < 1) {
617
0
                fido_log_debug("%s: %zd < 1", __func__, r);
618
0
                return (-1);
619
0
        }
620
0
        if (preamble != 0x00) {
621
0
                fido_log_debug("%s: preamble", __func__);
622
0
                return (-1);
623
0
        }
624
625
0
        r--;
626
0
        fido_log_xxd(buf, (size_t)r, "%s", __func__);
627
628
0
        return ((int)r);
629
0
}
630
631
int
632
fido_nfc_write(void *handle, const unsigned char *buf, size_t len)
633
0
{
634
0
        struct nfc_linux *ctx = handle;
635
0
        ssize_t r;
636
637
0
        fido_log_xxd(buf, len, "%s", __func__);
638
639
0
        if (len > INT_MAX) {
640
0
                fido_log_debug("%s: len", __func__);
641
0
                return (-1);
642
0
        }
643
0
        if ((r = write(ctx->fd, buf, len)) == -1) {
644
0
                fido_log_error(errno, "%s: write", __func__);
645
0
                return (-1);
646
0
        }
647
0
        if (r < 0 || (size_t)r != len) {
648
0
                fido_log_debug("%s: %zd != %zu", __func__, r, len);
649
0
                return (-1);
650
0
        }
651
652
0
        return ((int)r);
653
0
}