Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file reader_pcsc.c |
| 2 | * PC/SC Card reader backend for libosmosim. */ |
Harald Welte | ad41863 | 2012-09-10 10:49:59 +0200 | [diff] [blame] | 3 | /* |
| 4 | * (C) 2012 by Harald Welte <laforge@gnumonks.org> |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
| 9 | * |
Harald Welte | ad41863 | 2012-09-10 10:49:59 +0200 | [diff] [blame] | 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 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 27 | #include <string.h> |
| 28 | #include <stdint.h> |
| 29 | #include <stdio.h> |
| 30 | #include <errno.h> |
| 31 | |
| 32 | #include <osmocom/core/talloc.h> |
| 33 | #include <osmocom/sim/sim.h> |
| 34 | |
Holger Hans Peter Freyther | b0310dd | 2014-10-27 12:00:37 +0100 | [diff] [blame] | 35 | #include <wintypes.h> |
| 36 | #include <winscard.h> |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 37 | |
| 38 | #include "sim_int.h" |
| 39 | |
| 40 | #define PCSC_ERROR(rv, text) \ |
| 41 | if (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 | |
| 48 | |
| 49 | |
| 50 | struct pcsc_reader_state { |
| 51 | SCARDCONTEXT hContext; |
| 52 | SCARDHANDLE hCard; |
| 53 | DWORD dwActiveProtocol; |
| 54 | const SCARD_IO_REQUEST *pioSendPci; |
| 55 | SCARD_IO_REQUEST pioRecvPci; |
| 56 | char *name; |
| 57 | }; |
| 58 | |
| 59 | static struct osim_reader_hdl *pcsc_reader_open(int num, const char *id, void *ctx) |
| 60 | { |
| 61 | struct osim_reader_hdl *rh; |
| 62 | struct pcsc_reader_state *st; |
Harald Welte | 0d24644 | 2014-05-04 13:58:54 +0200 | [diff] [blame] | 63 | LONG rc; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 64 | LPSTR mszReaders = NULL; |
| 65 | DWORD dwReaders; |
| 66 | unsigned int num_readers; |
| 67 | char *ptr; |
| 68 | |
| 69 | /* FIXME: implement matching on id or num */ |
| 70 | |
| 71 | rh = talloc_zero(ctx, struct osim_reader_hdl); |
| 72 | st = rh->priv = talloc_zero(rh, struct pcsc_reader_state); |
| 73 | |
| 74 | rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, |
| 75 | &st->hContext); |
Harald Welte | d83d296 | 2013-03-04 17:52:33 +0000 | [diff] [blame] | 76 | PCSC_ERROR(rc, "SCardEstablishContext"); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 77 | |
| 78 | dwReaders = SCARD_AUTOALLOCATE; |
| 79 | rc = SCardListReaders(st->hContext, NULL, (LPSTR)&mszReaders, &dwReaders); |
| 80 | PCSC_ERROR(rc, "SCardListReaders"); |
| 81 | |
| 82 | num_readers = 0; |
| 83 | ptr = mszReaders; |
| 84 | while (*ptr != '\0') { |
| 85 | ptr += strlen(ptr)+1; |
| 86 | num_readers++; |
| 87 | } |
| 88 | |
| 89 | if (num_readers == 0) |
| 90 | goto end; |
| 91 | |
| 92 | st->name = talloc_strdup(rh, mszReaders); |
| 93 | st->dwActiveProtocol = -1; |
| 94 | |
| 95 | return rh; |
| 96 | end: |
| 97 | talloc_free(rh); |
| 98 | return NULL; |
| 99 | } |
| 100 | |
Harald Welte | 55790aa | 2014-10-26 18:46:50 +0100 | [diff] [blame] | 101 | static struct osim_card_hdl *pcsc_card_open(struct osim_reader_hdl *rh, |
| 102 | enum osim_proto proto) |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 103 | { |
| 104 | struct pcsc_reader_state *st = rh->priv; |
| 105 | struct osim_card_hdl *card; |
| 106 | struct osim_chan_hdl *chan; |
Harald Welte | 0d24644 | 2014-05-04 13:58:54 +0200 | [diff] [blame] | 107 | LONG rc; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 108 | |
Harald Welte | 55790aa | 2014-10-26 18:46:50 +0100 | [diff] [blame] | 109 | if (proto != OSIM_PROTO_T0) |
| 110 | return NULL; |
| 111 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 112 | rc = SCardConnect(st->hContext, st->name, SCARD_SHARE_SHARED, |
| 113 | SCARD_PROTOCOL_T0, &st->hCard, &st->dwActiveProtocol); |
| 114 | PCSC_ERROR(rc, "SCardConnect"); |
| 115 | |
| 116 | st->pioSendPci = SCARD_PCI_T0; |
| 117 | |
| 118 | card = talloc_zero(rh, struct osim_card_hdl); |
| 119 | INIT_LLIST_HEAD(&card->channels); |
| 120 | card->reader = rh; |
| 121 | rh->card = card; |
| 122 | |
| 123 | /* create a default channel */ |
| 124 | chan = talloc_zero(card, struct osim_chan_hdl); |
| 125 | chan->card = card; |
| 126 | llist_add(&chan->list, &card->channels); |
| 127 | |
| 128 | return card; |
| 129 | |
| 130 | end: |
| 131 | return NULL; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | static int pcsc_transceive(struct osim_reader_hdl *rh, struct msgb *msg) |
| 136 | { |
| 137 | struct pcsc_reader_state *st = rh->priv; |
| 138 | DWORD rlen = msgb_tailroom(msg); |
Harald Welte | 0d24644 | 2014-05-04 13:58:54 +0200 | [diff] [blame] | 139 | LONG rc; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 140 | |
| 141 | printf("TX: %s\n", osmo_hexdump(msg->data, msg->len)); |
| 142 | |
| 143 | rc = SCardTransmit(st->hCard, st->pioSendPci, msg->data, msgb_length(msg), |
| 144 | &st->pioRecvPci, msg->tail, &rlen); |
| 145 | PCSC_ERROR(rc, "SCardEndTransaction"); |
| 146 | |
| 147 | printf("RX: %s\n", osmo_hexdump(msg->tail, rlen)); |
| 148 | msgb_put(msg, rlen); |
| 149 | msgb_apdu_le(msg) = rlen; |
| 150 | |
| 151 | return 0; |
| 152 | end: |
| 153 | return -EIO; |
| 154 | } |
| 155 | |
| 156 | const struct osim_reader_ops pcsc_reader_ops = { |
| 157 | .name = "PC/SC", |
| 158 | .reader_open = pcsc_reader_open, |
| 159 | .card_open = pcsc_card_open, |
| 160 | .transceive = pcsc_transceive, |
| 161 | }; |
| 162 | |