blob: 60a9dee1bb8454a81c82e929d34bd3508da632ac [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);
73 if (rc != SCARD_S_SUCCESS)
74 goto end;
75
76 dwReaders = SCARD_AUTOALLOCATE;
77 rc = SCardListReaders(st->hContext, NULL, (LPSTR)&mszReaders, &dwReaders);
78 PCSC_ERROR(rc, "SCardListReaders");
79
80 num_readers = 0;
81 ptr = mszReaders;
82 while (*ptr != '\0') {
83 ptr += strlen(ptr)+1;
84 num_readers++;
85 }
86
87 if (num_readers == 0)
88 goto end;
89
90 st->name = talloc_strdup(rh, mszReaders);
91 st->dwActiveProtocol = -1;
92
93 return rh;
94end:
95 talloc_free(rh);
96 return NULL;
97}
98
99static struct osim_card_hdl *pcsc_card_open(struct osim_reader_hdl *rh)
100{
101 struct pcsc_reader_state *st = rh->priv;
102 struct osim_card_hdl *card;
103 struct osim_chan_hdl *chan;
104 int rc;
105
106 rc = SCardConnect(st->hContext, st->name, SCARD_SHARE_SHARED,
107 SCARD_PROTOCOL_T0, &st->hCard, &st->dwActiveProtocol);
108 PCSC_ERROR(rc, "SCardConnect");
109
110 st->pioSendPci = SCARD_PCI_T0;
111
112 card = talloc_zero(rh, struct osim_card_hdl);
113 INIT_LLIST_HEAD(&card->channels);
114 card->reader = rh;
115 rh->card = card;
116
117 /* create a default channel */
118 chan = talloc_zero(card, struct osim_chan_hdl);
119 chan->card = card;
120 llist_add(&chan->list, &card->channels);
121
122 return card;
123
124end:
125 return NULL;
126}
127
128
129static int pcsc_transceive(struct osim_reader_hdl *rh, struct msgb *msg)
130{
131 struct pcsc_reader_state *st = rh->priv;
132 DWORD rlen = msgb_tailroom(msg);
133 int rc;
134
135 printf("TX: %s\n", osmo_hexdump(msg->data, msg->len));
136
137 rc = SCardTransmit(st->hCard, st->pioSendPci, msg->data, msgb_length(msg),
138 &st->pioRecvPci, msg->tail, &rlen);
139 PCSC_ERROR(rc, "SCardEndTransaction");
140
141 printf("RX: %s\n", osmo_hexdump(msg->tail, rlen));
142 msgb_put(msg, rlen);
143 msgb_apdu_le(msg) = rlen;
144
145 return 0;
146end:
147 return -EIO;
148}
149
150const struct osim_reader_ops pcsc_reader_ops = {
151 .name = "PC/SC",
152 .reader_open = pcsc_reader_open,
153 .card_open = pcsc_card_open,
154 .transceive = pcsc_transceive,
155};
156