blob: 5e670912b5dd2d9fecefe8e97744230020ddb550 [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;
Harald Welte0d246442014-05-04 13:58:54 +020060 LONG rc;
Harald Welted54c2ee2012-01-17 18:25:50 +010061 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
Harald Welte55790aa2014-10-26 18:46:50 +0100100static struct osim_card_hdl *pcsc_card_open(struct osim_reader_hdl *rh,
101 enum osim_proto proto)
Harald Welted54c2ee2012-01-17 18:25:50 +0100102{
103 struct pcsc_reader_state *st = rh->priv;
104 struct osim_card_hdl *card;
105 struct osim_chan_hdl *chan;
Harald Welte0d246442014-05-04 13:58:54 +0200106 LONG rc;
Harald Welted54c2ee2012-01-17 18:25:50 +0100107
Harald Welte55790aa2014-10-26 18:46:50 +0100108 if (proto != OSIM_PROTO_T0)
109 return NULL;
110
Harald Welted54c2ee2012-01-17 18:25:50 +0100111 rc = SCardConnect(st->hContext, st->name, SCARD_SHARE_SHARED,
112 SCARD_PROTOCOL_T0, &st->hCard, &st->dwActiveProtocol);
113 PCSC_ERROR(rc, "SCardConnect");
114
115 st->pioSendPci = SCARD_PCI_T0;
116
117 card = talloc_zero(rh, struct osim_card_hdl);
118 INIT_LLIST_HEAD(&card->channels);
119 card->reader = rh;
120 rh->card = card;
121
122 /* create a default channel */
123 chan = talloc_zero(card, struct osim_chan_hdl);
124 chan->card = card;
125 llist_add(&chan->list, &card->channels);
126
127 return card;
128
129end:
130 return NULL;
131}
132
133
134static int pcsc_transceive(struct osim_reader_hdl *rh, struct msgb *msg)
135{
136 struct pcsc_reader_state *st = rh->priv;
137 DWORD rlen = msgb_tailroom(msg);
Harald Welte0d246442014-05-04 13:58:54 +0200138 LONG rc;
Harald Welted54c2ee2012-01-17 18:25:50 +0100139
140 printf("TX: %s\n", osmo_hexdump(msg->data, msg->len));
141
142 rc = SCardTransmit(st->hCard, st->pioSendPci, msg->data, msgb_length(msg),
143 &st->pioRecvPci, msg->tail, &rlen);
144 PCSC_ERROR(rc, "SCardEndTransaction");
145
146 printf("RX: %s\n", osmo_hexdump(msg->tail, rlen));
147 msgb_put(msg, rlen);
148 msgb_apdu_le(msg) = rlen;
149
150 return 0;
151end:
152 return -EIO;
153}
154
155const struct osim_reader_ops pcsc_reader_ops = {
156 .name = "PC/SC",
157 .reader_open = pcsc_reader_open,
158 .card_open = pcsc_card_open,
159 .transceive = pcsc_transceive,
160};
161