blob: 03a7f320d5d380c24f567b0c7e773183d775634a [file] [log] [blame]
Harald Welte3aa901d2018-08-13 18:32:36 +02001/*! \file reader_pcsc.c
2 * PC/SC Card reader backend for libosmosim. */
3/*
4 * (C) 2012 by Harald Welte <laforge@gnumonks.org>
5 *
6 * All Rights Reserved
7 *
8 * SPDX-License-Identifier: GPL-2.0+
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 *
24 */
25
26
27#include <string.h>
28#include <stdint.h>
29#include <stdio.h>
30#include <errno.h>
31
32#include <talloc.h>
33#include <osmocom/core/linuxlist.h>
Harald Welte3aa901d2018-08-13 18:32:36 +020034
35#include <wintypes.h>
36#include <winscard.h>
37
38#include "internal.h"
39
40#define PCSC_ERROR(rv, text) \
41if (rv != SCARD_S_SUCCESS) { \
42 fprintf(stderr, text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \
43 goto end; \
44} else { \
45 printf(text ": OK\n\n"); \
46}
47
48static void pcsc_readers_probe(void *ctx)
49{
50 LONG rc;
51 LPSTR mszReaders = NULL;
52 DWORD dwReaders;
53 SCARDCONTEXT hContext;
54 unsigned int num_readers;
55 char *ptr;
56
57 rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
58 PCSC_ERROR(rc, "SCardEstablishContext");
59
60 dwReaders = SCARD_AUTOALLOCATE;
61 rc = SCardListReaders(hContext, NULL, (LPSTR)&mszReaders, &dwReaders);
62 PCSC_ERROR(rc, "SCardListReaders");
63
64 num_readers = 0;
65 ptr = mszReaders;
66 while (*ptr != '\0') {
67 struct card_reader *cr;
68 /* while CCID has the nice feature to distinguish between readers and slots, PC/SC
69 * doesn't have this distinction, so we end up having one "reader" per slot */
70 cr = card_reader_alloc(ctx, ptr, NULL, NULL);
71 card_reader_slot_alloc(cr, 0);
72 ptr += strlen(ptr)+1;
73 num_readers++;
74 }
75
76 printf("num_readers=%d\n", num_readers);
77
78end:
79 if (mszReaders)
80 SCardFreeMemory(hContext, mszReaders);
81}
82
83static int pcsc_reader_open_slot(struct card_reader_slot *slot)
84{
Harald Welte77911b02018-08-14 23:47:30 +020085#if 0
Harald Welte3aa901d2018-08-13 18:32:36 +020086 struct osim_card_hdl *card;
87 LONG rc;
88
89 if (proto != OSIM_PROTO_T0)
90 return NULL;
91
92 rc = SCardConnect(st->hContext, st->name, SCARD_SHARE_SHARED,
93 SCARD_PROTOCOL_T0, &st->hCard, &st->dwActiveProtocol);
94 PCSC_ERROR(rc, "SCardConnect");
95
96 st->pioSendPci = SCARD_PCI_T0;
97
98 card = talloc_zero(rh, struct osim_card_hdl);
99 INIT_LLIST_HEAD(&card->channels);
100 card->reader = rh;
101 rh->card = card;
102
103end:
Harald Welte77911b02018-08-14 23:47:30 +0200104#endif
105 return -1;
Harald Welte3aa901d2018-08-13 18:32:36 +0200106}
107
108
109static const struct card_reader_driver_ops pcsc_driver_ops = {
110 .probe = pcsc_readers_probe,
111 .open_slot = pcsc_reader_open_slot,
112 .close_slot = NULL,
113 .transceive_apdu = NULL,
114};
115
116static struct card_reader_driver pcsc_driver = {
117 .name = "PCSC",
118 .ops = &pcsc_driver_ops,
119};
120
121__attribute__ ((constructor)) void pcsc_reader_init(void)
122{
123 card_reader_driver_register(&pcsc_driver);
124}