Harald Welte | 753c8aa | 2020-10-20 23:04:59 +0200 | [diff] [blame] | 1 | /* (C) 2018-2020 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 3dcdd20 | 2019-03-09 13:06:46 +0100 | [diff] [blame] | 2 | * |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 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 | * |
Harald Welte | 3dcdd20 | 2019-03-09 13:06:46 +0100 | [diff] [blame] | 17 | */ |
| 18 | |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 19 | |
| 20 | #include <osmocom/core/linuxlist.h> |
| 21 | #include <osmocom/core/talloc.h> |
| 22 | #include <osmocom/core/utils.h> |
| 23 | |
| 24 | #include <csv.h> |
Harald Welte | 753c8aa | 2020-10-20 23:04:59 +0200 | [diff] [blame] | 25 | #include <regex.h> |
| 26 | #include <errno.h> |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 27 | |
| 28 | #include "bankd.h" |
| 29 | |
| 30 | struct pcsc_slot_name { |
| 31 | struct llist_head list; |
| 32 | /* RSPRO bank slot number */ |
| 33 | struct bank_slot slot; |
| 34 | /* String name of the reader in PC/SC world */ |
Harald Welte | 753c8aa | 2020-10-20 23:04:59 +0200 | [diff] [blame] | 35 | const char *name_regex; |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 36 | }; |
| 37 | |
Harald Welte | 753c8aa | 2020-10-20 23:04:59 +0200 | [diff] [blame] | 38 | /* return a talloc-allocated string containing human-readable POSIX regex error */ |
| 39 | static char *get_regerror(void *ctx, int errcode, regex_t *compiled) |
| 40 | { |
| 41 | size_t len = regerror(errcode, compiled, NULL, 0); |
| 42 | char *buffer = talloc_size(ctx, len); |
| 43 | OSMO_ASSERT(buffer); |
| 44 | regerror(errcode, compiled, buffer, len); |
| 45 | return buffer; |
| 46 | } |
| 47 | |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 48 | enum parser_state_name { |
| 49 | ST_NONE, |
| 50 | ST_BANK_NR, |
| 51 | ST_SLOT_NR, |
| 52 | ST_PCSC_NAME, |
| 53 | }; |
| 54 | struct parser_state { |
| 55 | struct bankd *bankd; |
| 56 | enum parser_state_name state; |
| 57 | struct pcsc_slot_name *cur; |
| 58 | }; |
| 59 | |
| 60 | |
| 61 | static void parser_state_init(struct parser_state *ps) |
| 62 | { |
| 63 | ps->state = ST_BANK_NR; |
| 64 | ps->cur = NULL; |
| 65 | } |
| 66 | |
| 67 | static void cb1(void *s, size_t len, void *data) |
| 68 | { |
| 69 | char *field = (char *) s; |
| 70 | struct parser_state *ps = data; |
| 71 | |
| 72 | switch (ps->state) { |
| 73 | case ST_BANK_NR: |
| 74 | OSMO_ASSERT(!ps->cur); |
| 75 | ps->cur = talloc_zero(ps->bankd, struct pcsc_slot_name); |
| 76 | OSMO_ASSERT(ps->cur); |
| 77 | ps->cur->slot.bank_id = atoi(field); |
| 78 | ps->state = ST_SLOT_NR; |
| 79 | break; |
| 80 | case ST_SLOT_NR: |
| 81 | OSMO_ASSERT(ps->cur); |
| 82 | ps->cur->slot.slot_nr = atoi(field); |
| 83 | ps->state = ST_PCSC_NAME; |
| 84 | break; |
| 85 | case ST_PCSC_NAME: |
| 86 | OSMO_ASSERT(ps->cur); |
Harald Welte | 753c8aa | 2020-10-20 23:04:59 +0200 | [diff] [blame] | 87 | ps->cur->name_regex = talloc_strdup(ps->cur, field); |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 88 | break; |
| 89 | default: |
| 90 | OSMO_ASSERT(0); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | static void cb2(int c, void *data) |
| 95 | { |
| 96 | struct parser_state *ps = data; |
| 97 | struct pcsc_slot_name *sn = ps->cur; |
Harald Welte | 753c8aa | 2020-10-20 23:04:59 +0200 | [diff] [blame] | 98 | regex_t compiled_name; |
| 99 | int rc; |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 100 | |
Harald Welte | 7293e7b | 2021-12-08 21:29:11 +0100 | [diff] [blame] | 101 | LOGP(DMAIN, LOGL_INFO, "PC/SC slot name: %u/%u -> regex '%s'\n", |
| 102 | sn->slot.bank_id, sn->slot.slot_nr, sn->name_regex); |
Harald Welte | 753c8aa | 2020-10-20 23:04:59 +0200 | [diff] [blame] | 103 | |
| 104 | memset(&compiled_name, 0, sizeof(compiled_name)); |
| 105 | |
| 106 | rc = regcomp(&compiled_name, sn->name_regex, REG_EXTENDED); |
| 107 | if (rc != 0) { |
| 108 | char *errmsg = get_regerror(sn, rc, &compiled_name); |
Harald Welte | 7293e7b | 2021-12-08 21:29:11 +0100 | [diff] [blame] | 109 | LOGP(DMAIN, LOGL_ERROR, "Error compiling regex '%s': %s - Ignoring\n", |
| 110 | sn->name_regex, errmsg); |
Harald Welte | 753c8aa | 2020-10-20 23:04:59 +0200 | [diff] [blame] | 111 | talloc_free(errmsg); |
| 112 | talloc_free(sn); |
| 113 | } else { |
| 114 | llist_add_tail(&sn->list, &ps->bankd->pcsc_slot_names); |
| 115 | } |
| 116 | regfree(&compiled_name); |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 117 | |
| 118 | ps->state = ST_BANK_NR; |
| 119 | ps->cur = NULL; |
| 120 | } |
| 121 | |
| 122 | int bankd_pcsc_read_slotnames(struct bankd *bankd, const char *csv_file) |
| 123 | { |
| 124 | FILE *fp; |
| 125 | struct csv_parser p; |
| 126 | char buf[1024]; |
| 127 | size_t bytes_read; |
| 128 | struct parser_state ps; |
| 129 | |
Harald Welte | e9e505c | 2022-05-03 11:58:01 +0200 | [diff] [blame] | 130 | if (csv_init(&p, CSV_APPEND_NULL) != 0) { |
| 131 | LOGP(DMAIN, LOGL_FATAL, "Error during csv_init\n"); |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 132 | return -1; |
Harald Welte | e9e505c | 2022-05-03 11:58:01 +0200 | [diff] [blame] | 133 | } |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 134 | |
| 135 | fp = fopen(csv_file, "rb"); |
Harald Welte | e9e505c | 2022-05-03 11:58:01 +0200 | [diff] [blame] | 136 | if (!fp) { |
| 137 | LOGP(DMAIN, LOGL_FATAL, "Error opening %s: %s\n", csv_file, strerror(errno)); |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 138 | return -1; |
Harald Welte | e9e505c | 2022-05-03 11:58:01 +0200 | [diff] [blame] | 139 | } |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 140 | |
| 141 | parser_state_init(&ps); |
| 142 | ps.bankd = bankd; |
| 143 | |
| 144 | while ((bytes_read = fread(buf, 1, sizeof(buf), fp)) > 0) { |
| 145 | if (csv_parse(&p, buf, bytes_read, cb1, cb2, &ps) != bytes_read) { |
Harald Welte | 7293e7b | 2021-12-08 21:29:11 +0100 | [diff] [blame] | 146 | LOGP(DMAIN, LOGL_FATAL, "Error parsing bankd PC/SC CSV: %s\n", |
| 147 | csv_strerror(csv_error(&p))); |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 148 | fclose(fp); |
| 149 | return -1; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | csv_fini(&p, cb1, cb2, &ps); |
| 154 | fclose(fp); |
| 155 | csv_free(&p); |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | const char *bankd_pcsc_get_slot_name(struct bankd *bankd, const struct bank_slot *slot) |
| 161 | { |
| 162 | struct pcsc_slot_name *cur; |
| 163 | |
| 164 | llist_for_each_entry(cur, &bankd->pcsc_slot_names, list) { |
| 165 | if (bank_slot_equals(&cur->slot, slot)) |
Harald Welte | 753c8aa | 2020-10-20 23:04:59 +0200 | [diff] [blame] | 166 | return cur->name_regex; |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 167 | } |
| 168 | return NULL; |
| 169 | } |
Harald Welte | 297d72e | 2019-03-28 18:42:35 +0100 | [diff] [blame] | 170 | |
| 171 | |
| 172 | #include <wintypes.h> |
| 173 | #include <winscard.h> |
| 174 | #include <pcsclite.h> |
| 175 | |
Harald Welte | 753c8aa | 2020-10-20 23:04:59 +0200 | [diff] [blame] | 176 | #define LOGW_PCSC_ERROR(w, rv, text) \ |
| 177 | LOGW((w), text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv) |
| 178 | |
Harald Welte | 297d72e | 2019-03-28 18:42:35 +0100 | [diff] [blame] | 179 | #define PCSC_ERROR(w, rv, text) \ |
| 180 | if (rv != SCARD_S_SUCCESS) { \ |
Harald Welte | 753c8aa | 2020-10-20 23:04:59 +0200 | [diff] [blame] | 181 | LOGW_PCSC_ERROR(w, rv, text); \ |
Harald Welte | 297d72e | 2019-03-28 18:42:35 +0100 | [diff] [blame] | 182 | goto end; \ |
| 183 | } else { \ |
Harald Welte | 168d724 | 2021-12-08 15:25:42 +0100 | [diff] [blame] | 184 | LOGW((w), text ": OK\n"); \ |
Harald Welte | 297d72e | 2019-03-28 18:42:35 +0100 | [diff] [blame] | 185 | } |
| 186 | |
Harald Welte | 628672b | 2022-07-13 15:20:13 +0200 | [diff] [blame] | 187 | static DWORD bankd_share_mode(struct bankd *bankd) |
| 188 | { |
| 189 | if (bankd->cfg.permit_shared_pcsc) |
| 190 | return SCARD_SHARE_SHARED; |
| 191 | else |
| 192 | return SCARD_SHARE_EXCLUSIVE; |
| 193 | } |
| 194 | |
Harald Welte | bbd18bd | 2019-12-16 13:11:50 +0100 | [diff] [blame] | 195 | static int pcsc_get_atr(struct bankd_worker *worker) |
| 196 | { |
| 197 | long rc; |
| 198 | char pbReader[MAX_READERNAME]; |
| 199 | /* use DWORD type as this is what the PC/SC API expects */ |
| 200 | DWORD dwReaderLen = sizeof(pbReader); |
| 201 | DWORD dwAtrLen = worker->card.atr_len = sizeof(worker->card.atr); |
| 202 | DWORD dwState, dwProt; |
| 203 | |
| 204 | rc = SCardStatus(worker->reader.pcsc.hCard, pbReader, &dwReaderLen, &dwState, &dwProt, |
| 205 | worker->card.atr, &dwAtrLen); |
| 206 | PCSC_ERROR(worker, rc, "SCardStatus") |
| 207 | worker->card.atr_len = dwAtrLen; |
| 208 | LOGW(worker, "Card ATR: %s\n", osmo_hexdump_nospc(worker->card.atr, worker->card.atr_len)); |
| 209 | end: |
| 210 | return rc; |
| 211 | } |
| 212 | |
Harald Welte | 753c8aa | 2020-10-20 23:04:59 +0200 | [diff] [blame] | 213 | |
| 214 | static int pcsc_connect_slot_regex(struct bankd_worker *worker) |
| 215 | { |
| 216 | DWORD dwReaders = SCARD_AUTOALLOCATE; |
| 217 | LPSTR mszReaders = NULL; |
| 218 | regex_t compiled_name; |
| 219 | int result = -1; |
| 220 | LONG rc; |
| 221 | char *p; |
| 222 | |
| 223 | LOGW(worker, "Attempting to find card/slot using regex '%s'\n", worker->reader.name); |
| 224 | |
| 225 | rc = regcomp(&compiled_name, worker->reader.name, REG_EXTENDED); |
| 226 | if (rc != 0) { |
| 227 | LOGW(worker, "Error compiling RegEx over name '%s'\n", worker->reader.name); |
| 228 | return -EINVAL; |
| 229 | } |
| 230 | |
| 231 | rc = SCardListReaders(worker->reader.pcsc.hContext, NULL, (LPSTR)&mszReaders, &dwReaders); |
| 232 | if (rc != SCARD_S_SUCCESS) { |
| 233 | LOGW_PCSC_ERROR(worker, rc, "SCardListReaders"); |
| 234 | goto out_regfree; |
| 235 | } |
| 236 | |
| 237 | p = mszReaders; |
| 238 | while (*p) { |
| 239 | DWORD dwActiveProtocol; |
| 240 | int r = regexec(&compiled_name, p, 0, NULL, 0); |
| 241 | if (r == 0) { |
| 242 | LOGW(worker, "Attempting to open card/slot '%s'\n", p); |
Harald Welte | 628672b | 2022-07-13 15:20:13 +0200 | [diff] [blame] | 243 | rc = SCardConnect(worker->reader.pcsc.hContext, p, bankd_share_mode(worker->bankd), |
Harald Welte | 753c8aa | 2020-10-20 23:04:59 +0200 | [diff] [blame] | 244 | SCARD_PROTOCOL_T0, &worker->reader.pcsc.hCard, |
| 245 | &dwActiveProtocol); |
| 246 | if (rc == SCARD_S_SUCCESS) |
| 247 | result = 0; |
Harald Welte | 7f8bce4 | 2022-05-05 19:08:13 +0200 | [diff] [blame] | 248 | else { |
Harald Welte | 753c8aa | 2020-10-20 23:04:59 +0200 | [diff] [blame] | 249 | LOGW_PCSC_ERROR(worker, rc, "SCardConnect"); |
Harald Welte | 7f8bce4 | 2022-05-05 19:08:13 +0200 | [diff] [blame] | 250 | goto out_readerfree; |
| 251 | } |
Harald Welte | 753c8aa | 2020-10-20 23:04:59 +0200 | [diff] [blame] | 252 | break; |
| 253 | } |
| 254 | p += strlen(p) + 1; |
| 255 | } |
| 256 | |
Harald Welte | 7f8bce4 | 2022-05-05 19:08:13 +0200 | [diff] [blame] | 257 | if (result < 0) |
| 258 | LOGW(worker, "Error: Cannot find PC/SC reader/slot matching using regex '%s'\n", worker->reader.name); |
| 259 | |
| 260 | out_readerfree: |
Harald Welte | 753c8aa | 2020-10-20 23:04:59 +0200 | [diff] [blame] | 261 | SCardFreeMemory(worker->reader.pcsc.hContext, mszReaders); |
| 262 | |
| 263 | out_regfree: |
| 264 | regfree(&compiled_name); |
| 265 | |
| 266 | return result; |
| 267 | } |
| 268 | |
| 269 | |
Harald Welte | 297d72e | 2019-03-28 18:42:35 +0100 | [diff] [blame] | 270 | static int pcsc_open_card(struct bankd_worker *worker) |
| 271 | { |
| 272 | long rc; |
| 273 | |
| 274 | if (!worker->reader.pcsc.hContext) { |
| 275 | LOGW(worker, "Attempting to open PC/SC context\n"); |
| 276 | /* The PC/SC context must be created inside the thread where we'll later use it */ |
| 277 | rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &worker->reader.pcsc.hContext); |
| 278 | PCSC_ERROR(worker, rc, "SCardEstablishContext") |
| 279 | } |
| 280 | |
| 281 | if (!worker->reader.pcsc.hCard) { |
Harald Welte | 753c8aa | 2020-10-20 23:04:59 +0200 | [diff] [blame] | 282 | rc = pcsc_connect_slot_regex(worker); |
| 283 | if (rc != 0) |
| 284 | goto end; |
Harald Welte | 297d72e | 2019-03-28 18:42:35 +0100 | [diff] [blame] | 285 | } |
| 286 | |
Harald Welte | bbd18bd | 2019-12-16 13:11:50 +0100 | [diff] [blame] | 287 | rc = pcsc_get_atr(worker); |
Harald Welte | 753c8aa | 2020-10-20 23:04:59 +0200 | [diff] [blame] | 288 | |
Harald Welte | bbd18bd | 2019-12-16 13:11:50 +0100 | [diff] [blame] | 289 | end: |
| 290 | return rc; |
| 291 | } |
| 292 | |
| 293 | static int pcsc_reset_card(struct bankd_worker *worker, bool cold_reset) |
| 294 | { |
| 295 | long rc; |
| 296 | DWORD dwActiveProtocol; |
| 297 | |
| 298 | LOGW(worker, "Resetting card in '%s' (%s)\n", worker->reader.name, |
| 299 | cold_reset ? "cold reset" : "warm reset"); |
Harald Welte | 628672b | 2022-07-13 15:20:13 +0200 | [diff] [blame] | 300 | rc = SCardReconnect(worker->reader.pcsc.hCard, bankd_share_mode(worker->bankd), SCARD_PROTOCOL_T0, |
Harald Welte | bbd18bd | 2019-12-16 13:11:50 +0100 | [diff] [blame] | 301 | cold_reset ? SCARD_UNPOWER_CARD : SCARD_RESET_CARD, &dwActiveProtocol); |
| 302 | PCSC_ERROR(worker, rc, "SCardReconnect"); |
| 303 | |
| 304 | rc = pcsc_get_atr(worker); |
Harald Welte | 297d72e | 2019-03-28 18:42:35 +0100 | [diff] [blame] | 305 | end: |
| 306 | return rc; |
| 307 | } |
| 308 | |
| 309 | static int pcsc_transceive(struct bankd_worker *worker, const uint8_t *out, size_t out_len, |
| 310 | uint8_t *in, size_t *in_len) |
| 311 | { |
| 312 | const SCARD_IO_REQUEST *pioSendPci = SCARD_PCI_T0; |
| 313 | SCARD_IO_REQUEST pioRecvPci; |
| 314 | long rc; |
| 315 | |
| 316 | rc = SCardTransmit(worker->reader.pcsc.hCard, pioSendPci, out, out_len, &pioRecvPci, in, in_len); |
Harald Welte | 50a0972 | 2021-12-08 15:33:12 +0100 | [diff] [blame] | 317 | /* don't use PCSC_ERROR here as we don't want to log every successful SCardTransmit */ |
| 318 | if (rc != SCARD_S_SUCCESS) |
| 319 | LOGW_PCSC_ERROR(worker, rc, "SCardTransmit"); |
Harald Welte | 297d72e | 2019-03-28 18:42:35 +0100 | [diff] [blame] | 320 | |
Harald Welte | 297d72e | 2019-03-28 18:42:35 +0100 | [diff] [blame] | 321 | return rc; |
| 322 | } |
| 323 | |
| 324 | static void pcsc_cleanup(struct bankd_worker *worker) |
| 325 | { |
| 326 | if (worker->reader.pcsc.hCard) { |
| 327 | SCardDisconnect(worker->reader.pcsc.hCard, SCARD_UNPOWER_CARD); |
| 328 | worker->reader.pcsc.hCard = 0; |
| 329 | } |
| 330 | if (worker->reader.pcsc.hContext) { |
| 331 | SCardReleaseContext(worker->reader.pcsc.hContext); |
| 332 | worker->reader.pcsc.hContext = 0; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | const struct bankd_driver_ops pcsc_driver_ops = { |
| 337 | .open_card = pcsc_open_card, |
Harald Welte | bbd18bd | 2019-12-16 13:11:50 +0100 | [diff] [blame] | 338 | .reset_card = pcsc_reset_card, |
Harald Welte | 297d72e | 2019-03-28 18:42:35 +0100 | [diff] [blame] | 339 | .transceive = pcsc_transceive, |
| 340 | .cleanup = pcsc_cleanup, |
| 341 | }; |