blob: b490b97f6b465fdad5ae01cd7cc8b226d9e9b416 [file] [log] [blame]
Harald Weltead418632012-09-10 10:49:59 +02001/* PC/SC Card reader backend for libosmosim */
2/*
3 * (C) 2012 by Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23
Harald Welted54c2ee2012-01-17 18:25:50 +010024#include <string.h>
25#include <stdint.h>
26#include <stdio.h>
27#include <errno.h>
28
29#include <osmocom/core/talloc.h>
30#include <osmocom/sim/sim.h>
31
32#include <PCSC/wintypes.h>
33#include <PCSC/winscard.h>
34
35#include "sim_int.h"
36
37#define PCSC_ERROR(rv, text) \
38if (rv != SCARD_S_SUCCESS) { \
39 fprintf(stderr, text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \
40 goto end; \
41} else { \
42 printf(text ": OK\n\n"); \
43}
44
45
46
47struct pcsc_reader_state {
48 SCARDCONTEXT hContext;
49 SCARDHANDLE hCard;
50 DWORD dwActiveProtocol;
51 const SCARD_IO_REQUEST *pioSendPci;
52 SCARD_IO_REQUEST pioRecvPci;
53 char *name;
54};
55
56static struct osim_reader_hdl *pcsc_reader_open(int num, const char *id, void *ctx)
57{
58 struct osim_reader_hdl *rh;
59 struct pcsc_reader_state *st;
60 long rc;
61 LPSTR mszReaders = NULL;
62 DWORD dwReaders;
63 unsigned int num_readers;
64 char *ptr;
65
66 /* FIXME: implement matching on id or num */
67
68 rh = talloc_zero(ctx, struct osim_reader_hdl);
69 st = rh->priv = talloc_zero(rh, struct pcsc_reader_state);
70
71 rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL,
72 &st->hContext);
Harald Welted83d2962013-03-04 17:52:33 +000073 PCSC_ERROR(rc, "SCardEstablishContext");
Harald Welted54c2ee2012-01-17 18:25:50 +010074 if (rc != SCARD_S_SUCCESS)
75 goto end;
76
77 dwReaders = SCARD_AUTOALLOCATE;
78 rc = SCardListReaders(st->hContext, NULL, (LPSTR)&mszReaders, &dwReaders);
79 PCSC_ERROR(rc, "SCardListReaders");
80
81 num_readers = 0;
82 ptr = mszReaders;
83 while (*ptr != '\0') {
84 ptr += strlen(ptr)+1;
85 num_readers++;
86 }
87
88 if (num_readers == 0)
89 goto end;
90
91 st->name = talloc_strdup(rh, mszReaders);
92 st->dwActiveProtocol = -1;
93
94 return rh;
95end:
96 talloc_free(rh);
97 return NULL;
98}
99
100static struct osim_card_hdl *pcsc_card_open(struct osim_reader_hdl *rh)
101{
102 struct pcsc_reader_state *st = rh->priv;
103 struct osim_card_hdl *card;
104 struct osim_chan_hdl *chan;
105 int rc;
106
107 rc = SCardConnect(st->hContext, st->name, SCARD_SHARE_SHARED,
108 SCARD_PROTOCOL_T0, &st->hCard, &st->dwActiveProtocol);
109 PCSC_ERROR(rc, "SCardConnect");
110
111 st->pioSendPci = SCARD_PCI_T0;
112
113 card = talloc_zero(rh, struct osim_card_hdl);
114 INIT_LLIST_HEAD(&card->channels);
115 card->reader = rh;
116 rh->card = card;
117
118 /* create a default channel */
119 chan = talloc_zero(card, struct osim_chan_hdl);
120 chan->card = card;
121 llist_add(&chan->list, &card->channels);
122
123 return card;
124
125end:
126 return NULL;
127}
128
129
130static int pcsc_transceive(struct osim_reader_hdl *rh, struct msgb *msg)
131{
132 struct pcsc_reader_state *st = rh->priv;
133 DWORD rlen = msgb_tailroom(msg);
134 int rc;
135
136 printf("TX: %s\n", osmo_hexdump(msg->data, msg->len));
137
138 rc = SCardTransmit(st->hCard, st->pioSendPci, msg->data, msgb_length(msg),
139 &st->pioRecvPci, msg->tail, &rlen);
140 PCSC_ERROR(rc, "SCardEndTransaction");
141
142 printf("RX: %s\n", osmo_hexdump(msg->tail, rlen));
143 msgb_put(msg, rlen);
144 msgb_apdu_le(msg) = rlen;
145
146 return 0;
147end:
148 return -EIO;
149}
150
151const struct osim_reader_ops pcsc_reader_ops = {
152 .name = "PC/SC",
153 .reader_open = pcsc_reader_open,
154 .card_open = pcsc_card_open,
155 .transceive = pcsc_transceive,
156};
157