Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 1 | /*! \file reader_pcsc.c |
| 2 | * PC/SC Card reader backend for libosmosim. */ |
| 3 | /* |
| 4 | * (C) 2012 by Harald Welte <laforge@gnumonks.org> |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * SPDX-License-Identifier: GPL-2.0+ |
| 9 | * |
| 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 | |
| 27 | #include <string.h> |
| 28 | #include <stdint.h> |
| 29 | #include <stdio.h> |
| 30 | #include <errno.h> |
| 31 | |
| 32 | #include <talloc.h> |
| 33 | #include <osmocom/core/linuxlist.h> |
| 34 | #include <osmocom/sim/sim.h> |
| 35 | |
| 36 | #include <wintypes.h> |
| 37 | #include <winscard.h> |
| 38 | |
| 39 | #include "internal.h" |
| 40 | |
| 41 | #define PCSC_ERROR(rv, text) \ |
| 42 | if (rv != SCARD_S_SUCCESS) { \ |
| 43 | fprintf(stderr, text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \ |
| 44 | goto end; \ |
| 45 | } else { \ |
| 46 | printf(text ": OK\n\n"); \ |
| 47 | } |
| 48 | |
| 49 | static void pcsc_readers_probe(void *ctx) |
| 50 | { |
| 51 | LONG rc; |
| 52 | LPSTR mszReaders = NULL; |
| 53 | DWORD dwReaders; |
| 54 | SCARDCONTEXT hContext; |
| 55 | unsigned int num_readers; |
| 56 | char *ptr; |
| 57 | |
| 58 | rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext); |
| 59 | PCSC_ERROR(rc, "SCardEstablishContext"); |
| 60 | |
| 61 | dwReaders = SCARD_AUTOALLOCATE; |
| 62 | rc = SCardListReaders(hContext, NULL, (LPSTR)&mszReaders, &dwReaders); |
| 63 | PCSC_ERROR(rc, "SCardListReaders"); |
| 64 | |
| 65 | num_readers = 0; |
| 66 | ptr = mszReaders; |
| 67 | while (*ptr != '\0') { |
| 68 | struct card_reader *cr; |
| 69 | /* while CCID has the nice feature to distinguish between readers and slots, PC/SC |
| 70 | * doesn't have this distinction, so we end up having one "reader" per slot */ |
| 71 | cr = card_reader_alloc(ctx, ptr, NULL, NULL); |
| 72 | card_reader_slot_alloc(cr, 0); |
| 73 | ptr += strlen(ptr)+1; |
| 74 | num_readers++; |
| 75 | } |
| 76 | |
| 77 | printf("num_readers=%d\n", num_readers); |
| 78 | |
| 79 | end: |
| 80 | if (mszReaders) |
| 81 | SCardFreeMemory(hContext, mszReaders); |
| 82 | } |
| 83 | |
| 84 | static int pcsc_reader_open_slot(struct card_reader_slot *slot) |
| 85 | { |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 86 | #if 0 |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 87 | struct osim_card_hdl *card; |
| 88 | LONG rc; |
| 89 | |
| 90 | if (proto != OSIM_PROTO_T0) |
| 91 | return NULL; |
| 92 | |
| 93 | rc = SCardConnect(st->hContext, st->name, SCARD_SHARE_SHARED, |
| 94 | SCARD_PROTOCOL_T0, &st->hCard, &st->dwActiveProtocol); |
| 95 | PCSC_ERROR(rc, "SCardConnect"); |
| 96 | |
| 97 | st->pioSendPci = SCARD_PCI_T0; |
| 98 | |
| 99 | card = talloc_zero(rh, struct osim_card_hdl); |
| 100 | INIT_LLIST_HEAD(&card->channels); |
| 101 | card->reader = rh; |
| 102 | rh->card = card; |
| 103 | |
| 104 | end: |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 105 | #endif |
| 106 | return -1; |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | |
| 110 | static const struct card_reader_driver_ops pcsc_driver_ops = { |
| 111 | .probe = pcsc_readers_probe, |
| 112 | .open_slot = pcsc_reader_open_slot, |
| 113 | .close_slot = NULL, |
| 114 | .transceive_apdu = NULL, |
| 115 | }; |
| 116 | |
| 117 | static struct card_reader_driver pcsc_driver = { |
| 118 | .name = "PCSC", |
| 119 | .ops = &pcsc_driver_ops, |
| 120 | }; |
| 121 | |
| 122 | __attribute__ ((constructor)) void pcsc_reader_init(void) |
| 123 | { |
| 124 | card_reader_driver_register(&pcsc_driver); |
| 125 | } |