From d54c2ee8c51b41b7f7a5a469efd6bb391a0c2b75 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Tue, 17 Jan 2012 18:25:50 +0100 Subject: initial checkin of 'libosmosim' --- src/sim/reader_pcsc.c | 133 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 src/sim/reader_pcsc.c (limited to 'src/sim/reader_pcsc.c') diff --git a/src/sim/reader_pcsc.c b/src/sim/reader_pcsc.c new file mode 100644 index 00000000..0a36f49c --- /dev/null +++ b/src/sim/reader_pcsc.c @@ -0,0 +1,133 @@ +#include +#include +#include +#include + +#include +#include + +#include +#include + +#include "sim_int.h" + +#define PCSC_ERROR(rv, text) \ +if (rv != SCARD_S_SUCCESS) { \ + fprintf(stderr, text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \ + goto end; \ +} else { \ + printf(text ": OK\n\n"); \ +} + + + +struct pcsc_reader_state { + SCARDCONTEXT hContext; + SCARDHANDLE hCard; + DWORD dwActiveProtocol; + const SCARD_IO_REQUEST *pioSendPci; + SCARD_IO_REQUEST pioRecvPci; + char *name; +}; + +static struct osim_reader_hdl *pcsc_reader_open(int num, const char *id, void *ctx) +{ + struct osim_reader_hdl *rh; + struct pcsc_reader_state *st; + long rc; + LPSTR mszReaders = NULL; + DWORD dwReaders; + unsigned int num_readers; + char *ptr; + + /* FIXME: implement matching on id or num */ + + rh = talloc_zero(ctx, struct osim_reader_hdl); + st = rh->priv = talloc_zero(rh, struct pcsc_reader_state); + + rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, + &st->hContext); + if (rc != SCARD_S_SUCCESS) + goto end; + + dwReaders = SCARD_AUTOALLOCATE; + rc = SCardListReaders(st->hContext, NULL, (LPSTR)&mszReaders, &dwReaders); + PCSC_ERROR(rc, "SCardListReaders"); + + num_readers = 0; + ptr = mszReaders; + while (*ptr != '\0') { + ptr += strlen(ptr)+1; + num_readers++; + } + + if (num_readers == 0) + goto end; + + st->name = talloc_strdup(rh, mszReaders); + st->dwActiveProtocol = -1; + + return rh; +end: + talloc_free(rh); + return NULL; +} + +static struct osim_card_hdl *pcsc_card_open(struct osim_reader_hdl *rh) +{ + struct pcsc_reader_state *st = rh->priv; + struct osim_card_hdl *card; + struct osim_chan_hdl *chan; + int rc; + + rc = SCardConnect(st->hContext, st->name, SCARD_SHARE_SHARED, + SCARD_PROTOCOL_T0, &st->hCard, &st->dwActiveProtocol); + PCSC_ERROR(rc, "SCardConnect"); + + st->pioSendPci = SCARD_PCI_T0; + + card = talloc_zero(rh, struct osim_card_hdl); + INIT_LLIST_HEAD(&card->channels); + card->reader = rh; + rh->card = card; + + /* create a default channel */ + chan = talloc_zero(card, struct osim_chan_hdl); + chan->card = card; + llist_add(&chan->list, &card->channels); + + return card; + +end: + return NULL; +} + + +static int pcsc_transceive(struct osim_reader_hdl *rh, struct msgb *msg) +{ + struct pcsc_reader_state *st = rh->priv; + DWORD rlen = msgb_tailroom(msg); + int rc; + + printf("TX: %s\n", osmo_hexdump(msg->data, msg->len)); + + rc = SCardTransmit(st->hCard, st->pioSendPci, msg->data, msgb_length(msg), + &st->pioRecvPci, msg->tail, &rlen); + PCSC_ERROR(rc, "SCardEndTransaction"); + + printf("RX: %s\n", osmo_hexdump(msg->tail, rlen)); + msgb_put(msg, rlen); + msgb_apdu_le(msg) = rlen; + + return 0; +end: + return -EIO; +} + +const struct osim_reader_ops pcsc_reader_ops = { + .name = "PC/SC", + .reader_open = pcsc_reader_open, + .card_open = pcsc_card_open, + .transceive = pcsc_transceive, +}; + -- cgit v1.2.3 From ad41863b8d3f3ac7df0c34b2c7d342b864d944cc Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Mon, 10 Sep 2012 10:49:59 +0200 Subject: sim: add copyright notices and merge file_codec.c into core.c --- src/sim/reader_pcsc.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/sim/reader_pcsc.c') diff --git a/src/sim/reader_pcsc.c b/src/sim/reader_pcsc.c index 0a36f49c..60a9dee1 100644 --- a/src/sim/reader_pcsc.c +++ b/src/sim/reader_pcsc.c @@ -1,3 +1,26 @@ +/* PC/SC Card reader backend for libosmosim */ +/* + * (C) 2012 by Harald Welte + * + * All Rights Reserved + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + + #include #include #include -- cgit v1.2.3 From d83d29610a7c63a387fee77dbc2b18b19dc83b14 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Mon, 4 Mar 2013 17:52:33 +0000 Subject: sim: further updates/fixes --- src/sim/reader_pcsc.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src/sim/reader_pcsc.c') diff --git a/src/sim/reader_pcsc.c b/src/sim/reader_pcsc.c index 60a9dee1..b490b97f 100644 --- a/src/sim/reader_pcsc.c +++ b/src/sim/reader_pcsc.c @@ -70,6 +70,7 @@ static struct osim_reader_hdl *pcsc_reader_open(int num, const char *id, void *c rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &st->hContext); + PCSC_ERROR(rc, "SCardEstablishContext"); if (rc != SCARD_S_SUCCESS) goto end; -- cgit v1.2.3 From 0d24644f5243b3420c18e7afdee9ae641f179a56 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sun, 4 May 2014 13:58:54 +0200 Subject: sim: reader_pcsc.c: Avoid compiler warnings by using pcsc-lite LONG --- src/sim/reader_pcsc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/sim/reader_pcsc.c') diff --git a/src/sim/reader_pcsc.c b/src/sim/reader_pcsc.c index b490b97f..c5b8112b 100644 --- a/src/sim/reader_pcsc.c +++ b/src/sim/reader_pcsc.c @@ -57,7 +57,7 @@ static struct osim_reader_hdl *pcsc_reader_open(int num, const char *id, void *c { struct osim_reader_hdl *rh; struct pcsc_reader_state *st; - long rc; + LONG rc; LPSTR mszReaders = NULL; DWORD dwReaders; unsigned int num_readers; @@ -102,7 +102,7 @@ static struct osim_card_hdl *pcsc_card_open(struct osim_reader_hdl *rh) struct pcsc_reader_state *st = rh->priv; struct osim_card_hdl *card; struct osim_chan_hdl *chan; - int rc; + LONG rc; rc = SCardConnect(st->hContext, st->name, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0, &st->hCard, &st->dwActiveProtocol); @@ -131,7 +131,7 @@ static int pcsc_transceive(struct osim_reader_hdl *rh, struct msgb *msg) { struct pcsc_reader_state *st = rh->priv; DWORD rlen = msgb_tailroom(msg); - int rc; + LONG rc; printf("TX: %s\n", osmo_hexdump(msg->data, msg->len)); -- cgit v1.2.3 From 55790aa09a8f92d437ea06b3ef2c74465612fa8b Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sun, 26 Oct 2014 18:46:50 +0100 Subject: sim: Prepare infrastructure for protocols != T=0 and other drivers --- src/sim/reader_pcsc.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/sim/reader_pcsc.c') diff --git a/src/sim/reader_pcsc.c b/src/sim/reader_pcsc.c index c5b8112b..5e670912 100644 --- a/src/sim/reader_pcsc.c +++ b/src/sim/reader_pcsc.c @@ -97,13 +97,17 @@ end: return NULL; } -static struct osim_card_hdl *pcsc_card_open(struct osim_reader_hdl *rh) +static struct osim_card_hdl *pcsc_card_open(struct osim_reader_hdl *rh, + enum osim_proto proto) { struct pcsc_reader_state *st = rh->priv; struct osim_card_hdl *card; struct osim_chan_hdl *chan; LONG rc; + if (proto != OSIM_PROTO_T0) + return NULL; + rc = SCardConnect(st->hContext, st->name, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0, &st->hCard, &st->dwActiveProtocol); PCSC_ERROR(rc, "SCardConnect"); -- cgit v1.2.3