Coverage Report

Created: 2026-04-08 06:32

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
162k
#define SCARD_PROTOCOL_Tx SCARD_PROTOCOL_ANY
29
#endif
30
31
355k
#define BUFSIZE 1024        /* in bytes; passed to SCardListReaders() */
32
#define APDULEN 264     /* 261 rounded up to the nearest multiple of 8 */
33
169k
#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
122k
{
46
122k
        LONG s;
47
122k
        DWORD len;
48
49
122k
        len = BUFSIZE;
50
122k
        if ((*buf = calloc(1, len)) == NULL)
51
230
                goto fail;
52
122k
        if ((s = SCardListReaders(ctx, NULL, *buf, &len)) != SCARD_S_SUCCESS) {
53
6.07k
                fido_log_debug("%s: SCardListReaders 0x%lx", __func__, (long)s);
54
6.07k
                goto fail;
55
6.07k
        }
56
        /* sanity check "multi-string" */
57
116k
        if (len > BUFSIZE || len < 2) {
58
2.08k
                fido_log_debug("%s: bogus len=%u", __func__, (unsigned)len);
59
2.08k
                goto fail;
60
2.08k
        }
61
114k
        if ((*buf)[len - 1] != 0 || (*buf)[len - 2] != '\0') {
62
3.67k
                fido_log_debug("%s: bogus buf", __func__);
63
3.67k
                goto fail;
64
3.67k
        }
65
110k
        return (LONG)SCARD_S_SUCCESS;
66
12.0k
fail:
67
12.0k
        free(*buf);
68
12.0k
        *buf = NULL;
69
70
12.0k
        return (LONG)SCARD_E_NO_READERS_AVAILABLE;
71
114k
}
72
73
static char *
74
get_reader(SCARDCONTEXT ctx, const char *path)
75
202k
{
76
202k
        char *reader = NULL, *buf = NULL;
77
202k
        const char prefix[] = FIDO_PCSC_PREFIX "//slot";
78
202k
        uint64_t n;
79
80
202k
        if (path == NULL)
81
26.0k
                goto out;
82
176k
        if (strncmp(path, prefix, strlen(prefix)) != 0 ||
83
176k
            fido_to_uint64(path + strlen(prefix), 10, &n) < 0 ||
84
176k
            n > READERS - 1) {
85
84.9k
                fido_log_debug("%s: invalid path %s", __func__, path);
86
84.9k
                goto out;
87
84.9k
        }
88
91.9k
        if (list_readers(ctx, &buf) != SCARD_S_SUCCESS) {
89
1.59k
                fido_log_debug("%s: list_readers", __func__);
90
1.59k
                goto out;
91
1.59k
        }
92
276k
        for (const char *name = buf; *name != 0; name += strlen(name) + 1) {
93
275k
                if (n == 0) {
94
89.3k
                        reader = strdup(name);
95
89.3k
                        goto out;
96
89.3k
                }
97
186k
                n--;
98
186k
        }
99
980
        fido_log_debug("%s: failed to find reader %s", __func__, path);
100
202k
out:
101
202k
        free(buf);
102
103
202k
        return reader;
104
980
}
105
106
static int
107
prepare_io_request(DWORD prot, SCARD_IO_REQUEST *req)
108
162k
{
109
162k
        switch (prot) {
110
82.1k
        case SCARD_PROTOCOL_T0:
111
82.1k
                req->dwProtocol = SCARD_PCI_T0->dwProtocol;
112
82.1k
                req->cbPciLength = SCARD_PCI_T0->cbPciLength;
113
82.1k
                break;
114
79.2k
        case SCARD_PROTOCOL_T1:
115
79.2k
                req->dwProtocol = SCARD_PCI_T1->dwProtocol;
116
79.2k
                req->cbPciLength = SCARD_PCI_T1->cbPciLength;
117
79.2k
                break;
118
879
        default:
119
879
                fido_log_debug("%s: unknown protocol %lu", __func__,
120
879
                    (u_long)prot);
121
879
                return -1;
122
162k
        }
123
124
161k
        return 0;
125
162k
}
126
127
static int
128
copy_info(fido_dev_info_t *di, SCARDCONTEXT ctx, const char *reader, size_t idx)
129
73.9k
{
130
73.9k
        SCARDHANDLE h = 0;
131
73.9k
        SCARD_IO_REQUEST req;
132
73.9k
        DWORD prot = 0;
133
73.9k
        LONG s;
134
73.9k
        int ok = -1;
135
136
73.9k
        memset(di, 0, sizeof(*di));
137
73.9k
        memset(&req, 0, sizeof(req));
138
139
73.9k
        if ((s = SCardConnect(ctx, reader, SCARD_SHARE_SHARED,
140
73.9k
            SCARD_PROTOCOL_Tx, &h, &prot)) != SCARD_S_SUCCESS) {
141
112
                fido_log_debug("%s: SCardConnect 0x%lx", __func__, (long)s);
142
112
                goto fail;
143
112
        }
144
73.8k
        if (prepare_io_request(prot, &req) < 0) {
145
123
                fido_log_debug("%s: prepare_io_request", __func__);
146
123
                goto fail;
147
123
        }
148
73.6k
        if (asprintf(&di->path, "%s//slot%zu", FIDO_PCSC_PREFIX, idx) == -1) {
149
180
                di->path = NULL;
150
180
                fido_log_debug("%s: asprintf", __func__);
151
180
                goto fail;
152
180
        }
153
73.5k
        if (nfc_is_fido(di->path) == false) {
154
67.8k
                fido_log_debug("%s: nfc_is_fido: %s", __func__, di->path);
155
67.8k
                goto fail;
156
67.8k
        }
157
5.61k
        if ((di->manufacturer = strdup("PC/SC")) == NULL ||
158
5.61k
            (di->product = strdup(reader)) == NULL)
159
14
                goto fail;
160
161
5.59k
        ok = 0;
162
73.9k
fail:
163
73.9k
        if (h != 0)
164
73.8k
                SCardDisconnect(h, SCARD_LEAVE_CARD);
165
73.9k
        if (ok < 0) {
166
68.3k
                free(di->path);
167
68.3k
                free(di->manufacturer);
168
68.3k
                free(di->product);
169
68.3k
                explicit_bzero(di, sizeof(*di));
170
68.3k
        }
171
172
73.9k
        return ok;
173
5.59k
}
174
175
int
176
fido_pcsc_manifest(fido_dev_info_t *devlist, size_t ilen, size_t *olen)
177
84.1k
{
178
84.1k
        SCARDCONTEXT ctx = 0;
179
84.1k
        char *buf = NULL;
180
84.1k
        LONG s;
181
84.1k
        size_t idx = 0;
182
84.1k
        int r = FIDO_ERR_INTERNAL;
183
184
84.1k
        *olen = 0;
185
186
84.1k
        if (ilen == 0)
187
26.5k
                return FIDO_OK;
188
57.5k
        if (devlist == NULL)
189
26.4k
                return FIDO_ERR_INVALID_ARGUMENT;
190
191
31.1k
        if ((s = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL,
192
31.1k
            &ctx)) != SCARD_S_SUCCESS || ctx == 0) {
193
191
                fido_log_debug("%s: SCardEstablishContext 0x%lx", __func__,
194
191
                    (long)s);
195
191
                if (s == (LONG)SCARD_E_NO_SERVICE ||
196
191
                    s == (LONG)SCARD_E_NO_SMARTCARD)
197
124
                        r = FIDO_OK; /* suppress error */
198
191
                goto out;
199
191
        }
200
30.9k
        if ((s = list_readers(ctx, &buf)) != SCARD_S_SUCCESS) {
201
10.4k
                fido_log_debug("%s: list_readers 0x%lx", __func__, (long)s);
202
10.4k
                if (s == (LONG)SCARD_E_NO_READERS_AVAILABLE)
203
10.4k
                        r = FIDO_OK; /* suppress error */
204
10.4k
                goto out;
205
10.4k
        }
206
207
94.2k
        for (const char *name = buf; *name != 0; name += strlen(name) + 1) {
208
75.4k
                if (idx == READERS) {
209
1.48k
                        fido_log_debug("%s: stopping at %zu readers", __func__,
210
1.48k
                            idx);
211
1.48k
                        r = FIDO_OK;
212
1.48k
                        goto out;
213
1.48k
                }
214
73.9k
                if (copy_info(&devlist[*olen], ctx, name, idx++) == 0) {
215
5.59k
                        devlist[*olen].io = (fido_dev_io_t) {
216
5.59k
                                fido_pcsc_open,
217
5.59k
                                fido_pcsc_close,
218
5.59k
                                fido_pcsc_read,
219
5.59k
                                fido_pcsc_write,
220
5.59k
                        };
221
5.59k
                        devlist[*olen].transport = (fido_dev_transport_t) {
222
5.59k
                                fido_pcsc_rx,
223
5.59k
                                fido_pcsc_tx,
224
5.59k
                        };
225
5.59k
                        if (++(*olen) == ilen)
226
177
                                break;
227
5.59k
                }
228
73.9k
        }
229
230
18.9k
        r = FIDO_OK;
231
31.1k
out:
232
31.1k
        free(buf);
233
31.1k
        if (ctx != 0)
234
31.0k
                SCardReleaseContext(ctx);
235
236
31.1k
        return r;
237
18.9k
}
238
239
void *
240
fido_pcsc_open(const char *path)
241
205k
{
242
205k
        char *reader = NULL;
243
205k
        struct pcsc *dev = NULL;
244
205k
        SCARDCONTEXT ctx = 0;
245
205k
        SCARDHANDLE h = 0;
246
205k
        SCARD_IO_REQUEST req;
247
205k
        DWORD prot = 0;
248
205k
        LONG s;
249
250
205k
        memset(&req, 0, sizeof(req));
251
252
205k
        if ((s = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL,
253
205k
            &ctx)) != SCARD_S_SUCCESS || ctx == 0) {
254
2.34k
                fido_log_debug("%s: SCardEstablishContext 0x%lx", __func__,
255
2.34k
                    (long)s);
256
2.34k
                goto fail;
257
258
2.34k
        }
259
202k
        if ((reader = get_reader(ctx, path)) == NULL) {
260
113k
                fido_log_debug("%s: get_reader(%s)", __func__, path);
261
113k
                goto fail;
262
113k
        }
263
88.9k
        if ((s = SCardConnect(ctx, reader, SCARD_SHARE_SHARED,
264
88.9k
            SCARD_PROTOCOL_Tx, &h, &prot)) != SCARD_S_SUCCESS) {
265
523
                fido_log_debug("%s: SCardConnect 0x%lx", __func__, (long)s);
266
523
                goto fail;
267
523
        }
268
88.4k
        if (prepare_io_request(prot, &req) < 0) {
269
756
                fido_log_debug("%s: prepare_io_request", __func__);
270
756
                goto fail;
271
756
        }
272
87.7k
        if ((dev = calloc(1, sizeof(*dev))) == NULL)
273
479
                goto fail;
274
275
87.2k
        dev->ctx = ctx;
276
87.2k
        dev->h = h;
277
87.2k
        dev->req = req;
278
87.2k
        ctx = 0;
279
87.2k
        h = 0;
280
205k
fail:
281
205k
        if (h != 0)
282
1.23k
                SCardDisconnect(h, SCARD_LEAVE_CARD);
283
205k
        if (ctx != 0)
284
117k
                SCardReleaseContext(ctx);
285
205k
        free(reader);
286
287
205k
        return dev;
288
87.2k
}
289
290
void
291
fido_pcsc_close(void *handle)
292
87.2k
{
293
87.2k
        struct pcsc *dev = handle;
294
295
87.2k
        if (dev->h != 0)
296
87.2k
                SCardDisconnect(dev->h, SCARD_LEAVE_CARD);
297
87.2k
        if (dev->ctx != 0)
298
87.2k
                SCardReleaseContext(dev->ctx);
299
300
87.2k
        explicit_bzero(dev->rx_buf, sizeof(dev->rx_buf));
301
87.2k
        free(dev);
302
87.2k
}
303
304
int
305
fido_pcsc_read(void *handle, unsigned char *buf, size_t len, int ms)
306
86.3k
{
307
86.3k
        struct pcsc *dev = handle;
308
86.3k
        int r;
309
310
86.3k
        (void)ms;
311
86.3k
        if (dev->rx_len == 0 || dev->rx_len > len ||
312
86.3k
            dev->rx_len > sizeof(dev->rx_buf)) {
313
58.6k
                fido_log_debug("%s: rx_len", __func__);
314
58.6k
                return -1;
315
58.6k
        }
316
27.6k
        fido_log_xxd(dev->rx_buf, dev->rx_len, "%s: reading", __func__);
317
27.6k
        memcpy(buf, dev->rx_buf, dev->rx_len);
318
27.6k
        explicit_bzero(dev->rx_buf, sizeof(dev->rx_buf));
319
27.6k
        r = (int)dev->rx_len;
320
27.6k
        dev->rx_len = 0;
321
322
27.6k
        return r;
323
86.3k
}
324
325
int
326
fido_pcsc_write(void *handle, const unsigned char *buf, size_t len)
327
116k
{
328
116k
        struct pcsc *dev = handle;
329
116k
        DWORD n;
330
116k
        LONG s;
331
332
116k
        if (len > INT_MAX) {
333
26.4k
                fido_log_debug("%s: len", __func__);
334
26.4k
                return -1;
335
26.4k
        }
336
337
89.6k
        explicit_bzero(dev->rx_buf, sizeof(dev->rx_buf));
338
89.6k
        dev->rx_len = 0;
339
89.6k
        n = (DWORD)sizeof(dev->rx_buf);
340
341
89.6k
        fido_log_xxd(buf, len, "%s: writing", __func__);
342
343
89.6k
        if ((s = SCardTransmit(dev->h, &dev->req, buf, (DWORD)len, NULL,
344
89.6k
            dev->rx_buf, &n)) != SCARD_S_SUCCESS) {
345
1.59k
                fido_log_debug("%s: SCardTransmit 0x%lx", __func__, (long)s);
346
1.59k
                explicit_bzero(dev->rx_buf, sizeof(dev->rx_buf));
347
1.59k
                return -1;
348
1.59k
        }
349
88.0k
        dev->rx_len = (size_t)n;
350
351
88.0k
        fido_log_xxd(dev->rx_buf, dev->rx_len, "%s: read", __func__);
352
353
88.0k
        return (int)len;
354
89.6k
}
355
356
int
357
fido_pcsc_tx(fido_dev_t *d, uint8_t cmd, const u_char *buf, size_t count)
358
92.3k
{
359
92.3k
        return fido_nfc_tx(d, cmd, buf, count);
360
92.3k
}
361
362
int
363
fido_pcsc_rx(fido_dev_t *d, uint8_t cmd, u_char *buf, size_t count, int ms)
364
89.8k
{
365
89.8k
        return fido_nfc_rx(d, cmd, buf, count, ms);
366
89.8k
}
367
368
bool
369
fido_is_pcsc(const char *path)
370
2.97M
{
371
2.97M
        return strncmp(path, FIDO_PCSC_PREFIX, strlen(FIDO_PCSC_PREFIX)) == 0;
372
2.97M
}
373
374
int
375
fido_dev_set_pcsc(fido_dev_t *d)
376
179k
{
377
179k
        if (d->io_handle != NULL) {
378
0
                fido_log_debug("%s: device open", __func__);
379
0
                return -1;
380
0
        }
381
179k
        d->io_own = true;
382
179k
        d->io = (fido_dev_io_t) {
383
179k
                fido_pcsc_open,
384
179k
                fido_pcsc_close,
385
179k
                fido_pcsc_read,
386
179k
                fido_pcsc_write,
387
179k
        };
388
179k
        d->transport = (fido_dev_transport_t) {
389
179k
                fido_pcsc_rx,
390
179k
                fido_pcsc_tx,
391
179k
        };
392
393
179k
        return 0;
394
179k
}