Coverage Report

Created: 2025-10-04 18:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/libfido2/src/pcsc.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2022 Micro Focus or one of its affiliates.
3
 * Copyright (c) 2022 Yubico AB. All rights reserved.
4
 * Use of this source code is governed by a BSD-style
5
 * license that can be found in the LICENSE file.
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 */
8
9
#if __APPLE__
10
#include <PCSC/wintypes.h>
11
#include <PCSC/winscard.h>
12
#else
13
#include <winscard.h>
14
#endif /* __APPLE__ */
15
16
#include <errno.h>
17
18
#include "fido.h"
19
#include "fido/param.h"
20
#include "iso7816.h"
21
22
#if defined(_WIN32) && !defined(__MINGW32__)
23
#define SCardConnect SCardConnectA
24
#define SCardListReaders SCardListReadersA
25
#endif
26
27
#ifndef SCARD_PROTOCOL_Tx
28
29.0k
#define SCARD_PROTOCOL_Tx SCARD_PROTOCOL_ANY
29
#endif
30
31
77.9k
#define BUFSIZE 1024        /* in bytes; passed to SCardListReaders() */
32
#define APDULEN 264     /* 261 rounded up to the nearest multiple of 8 */
33
31.7k
#define READERS 8        /* maximum number of readers */
34
35
struct pcsc {
36
        SCARDCONTEXT     ctx;
37
        SCARDHANDLE      h;
38
        SCARD_IO_REQUEST req;
39
        uint8_t          rx_buf[APDULEN];
40
        size_t           rx_len;
41
};
42
43
static LONG
44
list_readers(SCARDCONTEXT ctx, char **buf)
45
30.8k
{
46
30.8k
        LONG s;
47
30.8k
        DWORD len;
48
49
30.8k
        len = BUFSIZE;
50
30.8k
        if ((*buf = calloc(1, len)) == NULL)
51
105
                goto fail;
52
30.7k
        if ((s = SCardListReaders(ctx, NULL, *buf, &len)) != SCARD_S_SUCCESS) {
53
7.17k
                fido_log_debug("%s: SCardListReaders 0x%lx", __func__, (long)s);
54
7.17k
                goto fail;
55
7.17k
        }
56
        /* sanity check "multi-string" */
57
23.5k
        if (len > BUFSIZE || len < 2) {
58
1.75k
                fido_log_debug("%s: bogus len=%u", __func__, (unsigned)len);
59
1.75k
                goto fail;
60
1.75k
        }
61
21.8k
        if ((*buf)[len - 1] != 0 || (*buf)[len - 2] != '\0') {
62
2.08k
                fido_log_debug("%s: bogus buf", __func__);
63
2.08k
                goto fail;
64
2.08k
        }
65
19.7k
        return (LONG)SCARD_S_SUCCESS;
66
11.1k
fail:
67
11.1k
        free(*buf);
68
11.1k
        *buf = NULL;
69
70
11.1k
        return (LONG)SCARD_E_NO_READERS_AVAILABLE;
71
21.8k
}
72
73
static char *
74
get_reader(SCARDCONTEXT ctx, const char *path)
75
49.6k
{
76
49.6k
        char *reader = NULL, *buf = NULL;
77
49.6k
        const char prefix[] = FIDO_PCSC_PREFIX "//slot";
78
49.6k
        uint64_t n;
79
80
49.6k
        if (path == NULL)
81
7.36k
                goto out;
82
42.2k
        if (strncmp(path, prefix, strlen(prefix)) != 0 ||
83
42.2k
            fido_to_uint64(path + strlen(prefix), 10, &n) < 0 ||
84
42.2k
            n > READERS - 1) {
85
25.4k
                fido_log_debug("%s: invalid path %s", __func__, path);
86
25.4k
                goto out;
87
25.4k
        }
88
16.8k
        if (list_readers(ctx, &buf) != SCARD_S_SUCCESS) {
89
782
                fido_log_debug("%s: list_readers", __func__);
90
782
                goto out;
91
782
        }
92
49.8k
        for (const char *name = buf; *name != 0; name += strlen(name) + 1) {
93
49.7k
                if (n == 0) {
94
15.9k
                        reader = strdup(name);
95
15.9k
                        goto out;
96
15.9k
                }
97
33.8k
                n--;
98
33.8k
        }
99
84
        fido_log_debug("%s: failed to find reader %s", __func__, path);
100
49.6k
out:
101
49.6k
        free(buf);
102
103
49.6k
        return reader;
104
84
}
105
106
static int
107
prepare_io_request(DWORD prot, SCARD_IO_REQUEST *req)
108
28.8k
{
109
28.8k
        switch (prot) {
110
14.7k
        case SCARD_PROTOCOL_T0:
111
14.7k
                req->dwProtocol = SCARD_PCI_T0->dwProtocol;
112
14.7k
                req->cbPciLength = SCARD_PCI_T0->cbPciLength;
113
14.7k
                break;
114
13.6k
        case SCARD_PROTOCOL_T1:
115
13.6k
                req->dwProtocol = SCARD_PCI_T1->dwProtocol;
116
13.6k
                req->cbPciLength = SCARD_PCI_T1->cbPciLength;
117
13.6k
                break;
118
464
        default:
119
464
                fido_log_debug("%s: unknown protocol %lu", __func__,
120
464
                    (u_long)prot);
121
464
                return -1;
122
28.8k
        }
123
124
28.4k
        return 0;
125
28.8k
}
126
127
static int
128
copy_info(fido_dev_info_t *di, SCARDCONTEXT ctx, const char *reader, size_t idx)
129
13.2k
{
130
13.2k
        SCARDHANDLE h = 0;
131
13.2k
        SCARD_IO_REQUEST req;
132
13.2k
        DWORD prot = 0;
133
13.2k
        LONG s;
134
13.2k
        int ok = -1;
135
136
13.2k
        memset(di, 0, sizeof(*di));
137
13.2k
        memset(&req, 0, sizeof(req));
138
139
13.2k
        if ((s = SCardConnect(ctx, reader, SCARD_SHARE_SHARED,
140
13.2k
            SCARD_PROTOCOL_Tx, &h, &prot)) != SCARD_S_SUCCESS) {
141
74
                fido_log_debug("%s: SCardConnect 0x%lx", __func__, (long)s);
142
74
                goto fail;
143
74
        }
144
13.1k
        if (prepare_io_request(prot, &req) < 0) {
145
65
                fido_log_debug("%s: prepare_io_request", __func__);
146
65
                goto fail;
147
65
        }
148
13.1k
        if (asprintf(&di->path, "%s//slot%zu", FIDO_PCSC_PREFIX, idx) == -1) {
149
160
                di->path = NULL;
150
160
                fido_log_debug("%s: asprintf", __func__);
151
160
                goto fail;
152
160
        }
153
12.9k
        if (nfc_is_fido(di->path) == false) {
154
12.1k
                fido_log_debug("%s: nfc_is_fido: %s", __func__, di->path);
155
12.1k
                goto fail;
156
12.1k
        }
157
759
        if ((di->manufacturer = strdup("PC/SC")) == NULL ||
158
759
            (di->product = strdup(reader)) == NULL)
159
6
                goto fail;
160
161
753
        ok = 0;
162
13.2k
fail:
163
13.2k
        if (h != 0)
164
13.1k
                SCardDisconnect(h, SCARD_LEAVE_CARD);
165
13.2k
        if (ok < 0) {
166
12.5k
                free(di->path);
167
12.5k
                free(di->manufacturer);
168
12.5k
                free(di->product);
169
12.5k
                explicit_bzero(di, sizeof(*di));
170
12.5k
        }
171
172
13.2k
        return ok;
173
753
}
174
175
int
176
fido_pcsc_manifest(fido_dev_info_t *devlist, size_t ilen, size_t *olen)
177
29.4k
{
178
29.4k
        SCARDCONTEXT ctx = 0;
179
29.4k
        char *buf = NULL;
180
29.4k
        LONG s;
181
29.4k
        size_t idx = 0;
182
29.4k
        int r = FIDO_ERR_INTERNAL;
183
184
29.4k
        *olen = 0;
185
186
29.4k
        if (ilen == 0)
187
7.64k
                return FIDO_OK;
188
21.7k
        if (devlist == NULL)
189
7.59k
                return FIDO_ERR_INVALID_ARGUMENT;
190
191
14.1k
        if ((s = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL,
192
14.1k
            &ctx)) != SCARD_S_SUCCESS || ctx == 0) {
193
160
                fido_log_debug("%s: SCardEstablishContext 0x%lx", __func__,
194
160
                    (long)s);
195
160
                if (s == (LONG)SCARD_E_NO_SERVICE ||
196
160
                    s == (LONG)SCARD_E_NO_SMARTCARD)
197
124
                        r = FIDO_OK; /* suppress error */
198
160
                goto out;
199
160
        }
200
14.0k
        if ((s = list_readers(ctx, &buf)) != SCARD_S_SUCCESS) {
201
10.3k
                fido_log_debug("%s: list_readers 0x%lx", __func__, (long)s);
202
10.3k
                if (s == (LONG)SCARD_E_NO_READERS_AVAILABLE)
203
10.3k
                        r = FIDO_OK; /* suppress error */
204
10.3k
                goto out;
205
10.3k
        }
206
207
16.9k
        for (const char *name = buf; *name != 0; name += strlen(name) + 1) {
208
13.4k
                if (idx == READERS) {
209
158
                        fido_log_debug("%s: stopping at %zu readers", __func__,
210
158
                            idx);
211
158
                        r = FIDO_OK;
212
158
                        goto out;
213
158
                }
214
13.2k
                if (copy_info(&devlist[*olen], ctx, name, idx++) == 0) {
215
753
                        devlist[*olen].io = (fido_dev_io_t) {
216
753
                                fido_pcsc_open,
217
753
                                fido_pcsc_close,
218
753
                                fido_pcsc_read,
219
753
                                fido_pcsc_write,
220
753
                        };
221
753
                        devlist[*olen].transport = (fido_dev_transport_t) {
222
753
                                fido_pcsc_rx,
223
753
                                fido_pcsc_tx,
224
753
                        };
225
753
                        if (++(*olen) == ilen)
226
5
                                break;
227
753
                }
228
13.2k
        }
229
230
3.51k
        r = FIDO_OK;
231
14.1k
out:
232
14.1k
        free(buf);
233
14.1k
        if (ctx != 0)
234
14.1k
                SCardReleaseContext(ctx);
235
236
14.1k
        return r;
237
3.51k
}
238
239
void *
240
fido_pcsc_open(const char *path)
241
50.7k
{
242
50.7k
        char *reader = NULL;
243
50.7k
        struct pcsc *dev = NULL;
244
50.7k
        SCARDCONTEXT ctx = 0;
245
50.7k
        SCARDHANDLE h = 0;
246
50.7k
        SCARD_IO_REQUEST req;
247
50.7k
        DWORD prot = 0;
248
50.7k
        LONG s;
249
250
50.7k
        memset(&req, 0, sizeof(req));
251
252
50.7k
        if ((s = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL,
253
50.7k
            &ctx)) != SCARD_S_SUCCESS || ctx == 0) {
254
1.15k
                fido_log_debug("%s: SCardEstablishContext 0x%lx", __func__,
255
1.15k
                    (long)s);
256
1.15k
                goto fail;
257
258
1.15k
        }
259
49.6k
        if ((reader = get_reader(ctx, path)) == NULL) {
260
33.8k
                fido_log_debug("%s: get_reader(%s)", __func__, path);
261
33.8k
                goto fail;
262
33.8k
        }
263
15.7k
        if ((s = SCardConnect(ctx, reader, SCARD_SHARE_SHARED,
264
15.7k
            SCARD_PROTOCOL_Tx, &h, &prot)) != SCARD_S_SUCCESS) {
265
103
                fido_log_debug("%s: SCardConnect 0x%lx", __func__, (long)s);
266
103
                goto fail;
267
103
        }
268
15.6k
        if (prepare_io_request(prot, &req) < 0) {
269
399
                fido_log_debug("%s: prepare_io_request", __func__);
270
399
                goto fail;
271
399
        }
272
15.2k
        if ((dev = calloc(1, sizeof(*dev))) == NULL)
273
344
                goto fail;
274
275
14.9k
        dev->ctx = ctx;
276
14.9k
        dev->h = h;
277
14.9k
        dev->req = req;
278
14.9k
        ctx = 0;
279
14.9k
        h = 0;
280
50.7k
fail:
281
50.7k
        if (h != 0)
282
743
                SCardDisconnect(h, SCARD_LEAVE_CARD);
283
50.7k
        if (ctx != 0)
284
35.6k
                SCardReleaseContext(ctx);
285
50.7k
        free(reader);
286
287
50.7k
        return dev;
288
14.9k
}
289
290
void
291
fido_pcsc_close(void *handle)
292
14.9k
{
293
14.9k
        struct pcsc *dev = handle;
294
295
14.9k
        if (dev->h != 0)
296
14.9k
                SCardDisconnect(dev->h, SCARD_LEAVE_CARD);
297
14.9k
        if (dev->ctx != 0)
298
14.9k
                SCardReleaseContext(dev->ctx);
299
300
14.9k
        explicit_bzero(dev->rx_buf, sizeof(dev->rx_buf));
301
14.9k
        free(dev);
302
14.9k
}
303
304
int
305
fido_pcsc_read(void *handle, unsigned char *buf, size_t len, int ms)
306
13.4k
{
307
13.4k
        struct pcsc *dev = handle;
308
13.4k
        int r;
309
310
13.4k
        (void)ms;
311
13.4k
        if (dev->rx_len == 0 || dev->rx_len > len ||
312
13.4k
            dev->rx_len > sizeof(dev->rx_buf)) {
313
8.62k
                fido_log_debug("%s: rx_len", __func__);
314
8.62k
                return -1;
315
8.62k
        }
316
4.86k
        fido_log_xxd(dev->rx_buf, dev->rx_len, "%s: reading", __func__);
317
4.86k
        memcpy(buf, dev->rx_buf, dev->rx_len);
318
4.86k
        explicit_bzero(dev->rx_buf, sizeof(dev->rx_buf));
319
4.86k
        r = (int)dev->rx_len;
320
4.86k
        dev->rx_len = 0;
321
322
4.86k
        return r;
323
13.4k
}
324
325
int
326
fido_pcsc_write(void *handle, const unsigned char *buf, size_t len)
327
21.9k
{
328
21.9k
        struct pcsc *dev = handle;
329
21.9k
        DWORD n;
330
21.9k
        LONG s;
331
332
21.9k
        if (len > INT_MAX) {
333
7.59k
                fido_log_debug("%s: len", __func__);
334
7.59k
                return -1;
335
7.59k
        }
336
337
14.3k
        explicit_bzero(dev->rx_buf, sizeof(dev->rx_buf));
338
14.3k
        dev->rx_len = 0;
339
14.3k
        n = (DWORD)sizeof(dev->rx_buf);
340
341
14.3k
        fido_log_xxd(buf, len, "%s: writing", __func__);
342
343
14.3k
        if ((s = SCardTransmit(dev->h, &dev->req, buf, (DWORD)len, NULL,
344
14.3k
            dev->rx_buf, &n)) != SCARD_S_SUCCESS) {
345
637
                fido_log_debug("%s: SCardTransmit 0x%lx", __func__, (long)s);
346
637
                explicit_bzero(dev->rx_buf, sizeof(dev->rx_buf));
347
637
                return -1;
348
637
        }
349
13.6k
        dev->rx_len = (size_t)n;
350
351
13.6k
        fido_log_xxd(dev->rx_buf, dev->rx_len, "%s: read", __func__);
352
353
13.6k
        return (int)len;
354
14.3k
}
355
356
int
357
fido_pcsc_tx(fido_dev_t *d, uint8_t cmd, const u_char *buf, size_t count)
358
15.5k
{
359
15.5k
        return fido_nfc_tx(d, cmd, buf, count);
360
15.5k
}
361
362
int
363
fido_pcsc_rx(fido_dev_t *d, uint8_t cmd, u_char *buf, size_t count, int ms)
364
14.3k
{
365
14.3k
        return fido_nfc_rx(d, cmd, buf, count, ms);
366
14.3k
}
367
368
bool
369
fido_is_pcsc(const char *path)
370
3.15M
{
371
3.15M
        return strncmp(path, FIDO_PCSC_PREFIX, strlen(FIDO_PCSC_PREFIX)) == 0;
372
3.15M
}
373
374
int
375
fido_dev_set_pcsc(fido_dev_t *d)
376
43.3k
{
377
43.3k
        if (d->io_handle != NULL) {
378
0
                fido_log_debug("%s: device open", __func__);
379
0
                return -1;
380
0
        }
381
43.3k
        d->io_own = true;
382
43.3k
        d->io = (fido_dev_io_t) {
383
43.3k
                fido_pcsc_open,
384
43.3k
                fido_pcsc_close,
385
43.3k
                fido_pcsc_read,
386
43.3k
                fido_pcsc_write,
387
43.3k
        };
388
43.3k
        d->transport = (fido_dev_transport_t) {
389
43.3k
                fido_pcsc_rx,
390
43.3k
                fido_pcsc_tx,
391
43.3k
        };
392
393
43.3k
        return 0;
394
43.3k
}