Coverage Report

Created: 2024-02-05 19:20

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