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; \ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 47 | struct 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 | |
Harald Welte | 22117a7 | 2021-04-25 20:50:13 +0200 | [diff] [blame] | 56 | static int pcsc_get_atr(struct osim_card_hdl *card) |
| 57 | { |
| 58 | struct osim_reader_hdl *rh = card->reader; |
| 59 | struct pcsc_reader_state *st = rh->priv; |
| 60 | char pbReader[MAX_READERNAME]; |
| 61 | DWORD dwReaderLen = sizeof(pbReader); |
| 62 | DWORD dwAtrLen = sizeof(card->atr); |
| 63 | DWORD dwState, dwProt; |
| 64 | long rc; |
| 65 | |
| 66 | rc = SCardStatus(st->hCard, pbReader, &dwReaderLen, &dwState, &dwProt, |
| 67 | card->atr, &dwAtrLen); |
| 68 | PCSC_ERROR(rc, "SCardStatus"); |
| 69 | card->atr_len = dwAtrLen; |
| 70 | |
| 71 | return 0; |
| 72 | |
| 73 | end: |
| 74 | return -EIO; |
| 75 | } |
| 76 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 77 | static struct osim_reader_hdl *pcsc_reader_open(int num, const char *id, void *ctx) |
| 78 | { |
| 79 | struct osim_reader_hdl *rh; |
| 80 | struct pcsc_reader_state *st; |
Harald Welte | 0d24644 | 2014-05-04 13:58:54 +0200 | [diff] [blame] | 81 | LONG rc; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 82 | LPSTR mszReaders = NULL; |
| 83 | DWORD dwReaders; |
| 84 | unsigned int num_readers; |
| 85 | char *ptr; |
| 86 | |
| 87 | /* FIXME: implement matching on id or num */ |
| 88 | |
| 89 | rh = talloc_zero(ctx, struct osim_reader_hdl); |
| 90 | st = rh->priv = talloc_zero(rh, struct pcsc_reader_state); |
| 91 | |
| 92 | rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, |
| 93 | &st->hContext); |
Harald Welte | d83d296 | 2013-03-04 17:52:33 +0000 | [diff] [blame] | 94 | PCSC_ERROR(rc, "SCardEstablishContext"); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 95 | |
| 96 | dwReaders = SCARD_AUTOALLOCATE; |
| 97 | rc = SCardListReaders(st->hContext, NULL, (LPSTR)&mszReaders, &dwReaders); |
| 98 | PCSC_ERROR(rc, "SCardListReaders"); |
| 99 | |
Eric Wild | 94cd4ac | 2019-10-31 19:18:45 +0100 | [diff] [blame] | 100 | /* SCARD_S_SUCCESS means there is at least one reader in the group */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 101 | num_readers = 0; |
| 102 | ptr = mszReaders; |
Eric Wild | 94cd4ac | 2019-10-31 19:18:45 +0100 | [diff] [blame] | 103 | while (*ptr != '\0' && num_readers != num) { |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 104 | ptr += strlen(ptr)+1; |
| 105 | num_readers++; |
| 106 | } |
| 107 | |
Eric Wild | 18caa87 | 2020-01-29 14:41:18 +0100 | [diff] [blame] | 108 | if (num != num_readers) { |
| 109 | SCardFreeMemory(st->hContext, mszReaders); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 110 | goto end; |
Eric Wild | 18caa87 | 2020-01-29 14:41:18 +0100 | [diff] [blame] | 111 | } |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 112 | |
Eric Wild | 94cd4ac | 2019-10-31 19:18:45 +0100 | [diff] [blame] | 113 | st->name = talloc_strdup(rh, ptr); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 114 | st->dwActiveProtocol = -1; |
Eric Wild | 18caa87 | 2020-01-29 14:41:18 +0100 | [diff] [blame] | 115 | SCardFreeMemory(st->hContext, mszReaders); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 116 | |
| 117 | return rh; |
| 118 | end: |
| 119 | talloc_free(rh); |
| 120 | return NULL; |
| 121 | } |
| 122 | |
Harald Welte | 55790aa | 2014-10-26 18:46:50 +0100 | [diff] [blame] | 123 | static struct osim_card_hdl *pcsc_card_open(struct osim_reader_hdl *rh, |
| 124 | enum osim_proto proto) |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 125 | { |
| 126 | struct pcsc_reader_state *st = rh->priv; |
| 127 | struct osim_card_hdl *card; |
| 128 | struct osim_chan_hdl *chan; |
Harald Welte | 0d24644 | 2014-05-04 13:58:54 +0200 | [diff] [blame] | 129 | LONG rc; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 130 | |
Harald Welte | 55790aa | 2014-10-26 18:46:50 +0100 | [diff] [blame] | 131 | if (proto != OSIM_PROTO_T0) |
| 132 | return NULL; |
| 133 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 134 | rc = SCardConnect(st->hContext, st->name, SCARD_SHARE_SHARED, |
| 135 | SCARD_PROTOCOL_T0, &st->hCard, &st->dwActiveProtocol); |
| 136 | PCSC_ERROR(rc, "SCardConnect"); |
| 137 | |
| 138 | st->pioSendPci = SCARD_PCI_T0; |
| 139 | |
| 140 | card = talloc_zero(rh, struct osim_card_hdl); |
| 141 | INIT_LLIST_HEAD(&card->channels); |
Harald Welte | 429adec | 2020-03-20 13:05:40 +0100 | [diff] [blame] | 142 | INIT_LLIST_HEAD(&card->apps); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 143 | card->reader = rh; |
| 144 | rh->card = card; |
| 145 | |
| 146 | /* create a default channel */ |
| 147 | chan = talloc_zero(card, struct osim_chan_hdl); |
| 148 | chan->card = card; |
| 149 | llist_add(&chan->list, &card->channels); |
| 150 | |
Harald Welte | 22117a7 | 2021-04-25 20:50:13 +0200 | [diff] [blame] | 151 | pcsc_get_atr(card); |
| 152 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 153 | return card; |
| 154 | |
| 155 | end: |
| 156 | return NULL; |
| 157 | } |
| 158 | |
Harald Welte | 20199da | 2021-06-01 20:11:19 +0200 | [diff] [blame] | 159 | static int pcsc_card_reset(struct osim_card_hdl *card, bool cold_reset) |
| 160 | { |
| 161 | struct pcsc_reader_state *st = card->reader->priv; |
| 162 | LONG rc; |
| 163 | |
| 164 | rc = SCardReconnect(st->hCard, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0, |
| 165 | cold_reset ? SCARD_UNPOWER_CARD : SCARD_RESET_CARD, |
| 166 | &st->dwActiveProtocol); |
| 167 | PCSC_ERROR(rc, "SCardReconnect"); |
| 168 | |
| 169 | return 0; |
| 170 | end: |
| 171 | return -EIO; |
| 172 | } |
| 173 | |
| 174 | static int pcsc_card_close(struct osim_card_hdl *card) |
| 175 | { |
| 176 | struct pcsc_reader_state *st = card->reader->priv; |
| 177 | LONG rc; |
| 178 | |
| 179 | rc = SCardDisconnect(st->hCard, SCARD_UNPOWER_CARD); |
| 180 | PCSC_ERROR(rc, "SCardDisconnect"); |
| 181 | |
| 182 | return 0; |
| 183 | end: |
| 184 | return -EIO; |
| 185 | } |
| 186 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 187 | |
| 188 | static int pcsc_transceive(struct osim_reader_hdl *rh, struct msgb *msg) |
| 189 | { |
| 190 | struct pcsc_reader_state *st = rh->priv; |
| 191 | DWORD rlen = msgb_tailroom(msg); |
Harald Welte | 0d24644 | 2014-05-04 13:58:54 +0200 | [diff] [blame] | 192 | LONG rc; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 193 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 194 | rc = SCardTransmit(st->hCard, st->pioSendPci, msg->data, msgb_length(msg), |
| 195 | &st->pioRecvPci, msg->tail, &rlen); |
| 196 | PCSC_ERROR(rc, "SCardEndTransaction"); |
| 197 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 198 | msgb_put(msg, rlen); |
| 199 | msgb_apdu_le(msg) = rlen; |
| 200 | |
| 201 | return 0; |
| 202 | end: |
| 203 | return -EIO; |
| 204 | } |
| 205 | |
| 206 | const struct osim_reader_ops pcsc_reader_ops = { |
| 207 | .name = "PC/SC", |
| 208 | .reader_open = pcsc_reader_open, |
| 209 | .card_open = pcsc_card_open, |
Harald Welte | 20199da | 2021-06-01 20:11:19 +0200 | [diff] [blame] | 210 | .card_reset = pcsc_card_reset, |
| 211 | .card_close = pcsc_card_close, |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 212 | .transceive = pcsc_transceive, |
| 213 | }; |
| 214 | |