blob: 5e7fb7a6c040cc416f4fd4ca975560a94b54e926 [file] [log] [blame]
Harald Welte9fac4962020-02-14 21:01:23 +01001/* (C) 2020 by Harald Welte <laforge@gnumonks.org>
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 Welte9fac4962020-02-14 21:01:23 +010017 */
18
19/* This is a remsim-client that provides an IFD_Handler (reader driver)
20 * towards the PC/SC services. This effectively allows any local PC/SC client
21 * application to use a remote smartcard via osmo-remsim.
22 *
23 * In order to use this, you will need an /etc/reader.conf.d/osmo-remsim-client
24 * file with the following content:
25 *
26 * FRIENDLYNAME "osmo-remsim-client"
27 * DEVICENAME 0:0:192.168.11.10:9998
28 * LIBPATH /usr/lib/pcsc/drivers/serial/libifd_remsim_client.so
29 *
30 * Where DEVICENAME has the following format:
31 * [ClientID:[SlotNr:[ServerIp:[ServerPort]]]]
32 *
33 */
34
35#include <errno.h>
36#include <unistd.h>
37#include <pthread.h>
38
39#include <osmocom/core/select.h>
40#include <osmocom/core/application.h>
41extern int osmo_ctx_init(const char *id);
42
43#include "client.h"
44
45/* ensure this current thread has an osmo_ctx and hence can use OTC_GLOBAL and friends */
46static void ensure_osmo_ctx(void)
47{
48 if (!osmo_ctx)
49 osmo_ctx_init("");
50}
51
52/* inter-thread messages between IFD thread and remsim-client thread */
53enum itmsg_type {
54 ITMSG_TYPE_NONE,
55
56 /* card present? */
57 ITMSG_TYPE_CARD_PRES_REQ,
58 ITMSG_TYPE_CARD_PRES_RESP,
59
60 /* obtain ATR */
61 ITMSG_TYPE_ATR_REQ,
62 ITMSG_TYPE_ATR_RESP,
63
64 /* transceive APDU: Send C-APDU, receive R-APDU */
65 ITMSG_TYPE_C_APDU_REQ,
66 ITMSG_TYPE_R_APDU_IND,
67
68 /* power off the card */
69 ITMSG_TYPE_POWER_OFF_REQ,
70 ITMSG_TYPE_POWER_OFF_RESP,
71
72 /* power on the card */
73 ITMSG_TYPE_POWER_ON_REQ,
74 ITMSG_TYPE_POWER_ON_RESP,
75
76 /* reset the card */
77 ITMSG_TYPE_RESET_REQ,
78 ITMSG_TYPE_RESET_RESP,
79};
80
81struct itmsg {
82 enum itmsg_type type;
83 uint16_t status; /* 0 == success */
84 uint16_t len; /* length of 'data' */
85 uint8_t data[0];
86};
87
88/* allocate + initialize msgb-wrapped inter-thread message (struct itmsg) */
89struct msgb *itmsg_alloc(enum itmsg_type type, uint16_t status, const uint8_t *data, uint16_t len)
90{
91 struct msgb *msg = msgb_alloc_c(OTC_GLOBAL, sizeof(struct itmsg)+len, "Tx itmsg");
92 struct itmsg *im;
93
94 if (!msg)
95 return NULL;
96
97 im = (struct itmsg *) msgb_put(msg, sizeof(struct itmsg) + len);
98 im->type = type;
99 im->status = status;
100 im->len = len;
101 if (len)
102 memcpy(im->data, data, len);
103
104 return msg;
105}
106
107/***********************************************************************
108 * remsim_client thread
109 ***********************************************************************/
110
111void __thread *talloc_asn1_ctx;
112
113struct client_thread {
Harald Weltea9bb9a92022-05-03 15:28:02 +0200114 /* bankd client running inside this thread */
Harald Welte9fac4962020-02-14 21:01:23 +0100115 struct bankd_client *bc;
116
117 /* inter-thread osmo-fd; communication with IFD/PCSC thread */
118 struct osmo_fd it_ofd;
119 struct llist_head it_msgq;
120
121 /* ATR as received from remsim-bankd */
122 uint8_t atr[ATR_SIZE_MAX];
123 uint8_t atr_len;
124};
125
126/* configuration of client thread; passed in from IFD thread */
127struct client_thread_cfg {
Harald Welte9fac4962020-02-14 21:01:23 +0100128 const char *server_host;
129 int server_port;
130 int client_id;
131 int client_slot;
132 int it_sock_fd;
133};
134
Harald Weltea9bb9a92022-05-03 15:28:02 +0200135/* enqueue a msgb (containing 'struct itmsg') towards the IFD-handler thread */
Harald Welte9fac4962020-02-14 21:01:23 +0100136static void enqueue_to_ifd(struct client_thread *ct, struct msgb *msg)
137{
138 if (!msg)
139 return;
140
141 msgb_enqueue(&ct->it_msgq, msg);
142 ct->it_ofd.when |= OSMO_FD_WRITE;
143}
144
145/***********************************************************************
Harald Welte0e968cc2020-02-22 18:16:16 +0100146 * frontend to remsim-client main FSM code
Harald Welte9fac4962020-02-14 21:01:23 +0100147 ***********************************************************************/
148
Harald Welte0e968cc2020-02-22 18:16:16 +0100149int frontend_request_card_insert(struct bankd_client *bc)
Harald Welte9fac4962020-02-14 21:01:23 +0100150{
Harald Welte0e968cc2020-02-22 18:16:16 +0100151 return 0;
152}
153
Harald Welte7b87ba12021-09-08 21:38:35 +0200154int frontend_request_card_remove(struct bankd_client *bc)
155{
156 return 0;
157}
158
Harald Welte0e968cc2020-02-22 18:16:16 +0100159int frontend_request_sim_remote(struct bankd_client *bc)
160{
161 return 0;
162}
163
Harald Welte7b87ba12021-09-08 21:38:35 +0200164int frontend_request_sim_local(struct bankd_client *bc)
165{
166 return 0;
167}
168
Harald Welte0e968cc2020-02-22 18:16:16 +0100169int frontend_request_modem_reset(struct bankd_client *bc)
170{
171 return 0;
172}
173
174int frontend_handle_card2modem(struct bankd_client *bc, const uint8_t *data, size_t len)
175{
Harald Welte9fac4962020-02-14 21:01:23 +0100176 struct client_thread *ct = bc->data;
177 struct msgb *msg;
178
Harald Welte0e968cc2020-02-22 18:16:16 +0100179 OSMO_ASSERT(data);
Harald Welte9fac4962020-02-14 21:01:23 +0100180
Harald Welte0e968cc2020-02-22 18:16:16 +0100181 DEBUGP(DMAIN, "R-APDU: %s\n", osmo_hexdump(data, len));
Harald Welte9fac4962020-02-14 21:01:23 +0100182 /* enqueue towards IFD thread */
Harald Welte0e968cc2020-02-22 18:16:16 +0100183 msg = itmsg_alloc(ITMSG_TYPE_R_APDU_IND, 0, data, len);
Harald Welte9fac4962020-02-14 21:01:23 +0100184 OSMO_ASSERT(msg);
185 enqueue_to_ifd(ct, msg);
186
187 return 0;
188}
189
Harald Welte0e968cc2020-02-22 18:16:16 +0100190int frontend_handle_set_atr(struct bankd_client *bc, const uint8_t *data, size_t len)
Harald Welte9fac4962020-02-14 21:01:23 +0100191{
192 struct client_thread *ct = bc->data;
Harald Welte9fac4962020-02-14 21:01:23 +0100193 unsigned int atr_len;
194
Harald Welte0e968cc2020-02-22 18:16:16 +0100195 OSMO_ASSERT(data);
Harald Welte9fac4962020-02-14 21:01:23 +0100196
Harald Welte0e968cc2020-02-22 18:16:16 +0100197 DEBUGP(DMAIN, "SET_ATR: %s\n", osmo_hexdump(data, len));
Harald Welte9fac4962020-02-14 21:01:23 +0100198
199 /* store ATR in local data structure until somebody needs it */
Harald Welte0e968cc2020-02-22 18:16:16 +0100200 atr_len = len;
Harald Welte9fac4962020-02-14 21:01:23 +0100201 if (atr_len > sizeof(ct->atr))
202 atr_len = sizeof(ct->atr);
Harald Welte0e968cc2020-02-22 18:16:16 +0100203 memcpy(ct->atr, data, atr_len);
Harald Welte9fac4962020-02-14 21:01:23 +0100204 ct->atr_len = atr_len;
205
Harald Welte9fac4962020-02-14 21:01:23 +0100206 return 0;
207}
208
Harald Welte0e968cc2020-02-22 18:16:16 +0100209int frontend_handle_slot_status(struct bankd_client *bc, const SlotPhysStatus_t *sts)
Harald Welte9fac4962020-02-14 21:01:23 +0100210{
Harald Welte0e968cc2020-02-22 18:16:16 +0100211 return 0;
212}
Harald Welte9fac4962020-02-14 21:01:23 +0100213
Harald Weltee580c932020-05-24 16:03:56 +0200214int frontend_append_script_env(struct bankd_client *bc, char **env, int idx, size_t max_env)
Harald Welte0e968cc2020-02-22 18:16:16 +0100215{
Harald Weltee580c932020-05-24 16:03:56 +0200216 return idx;
Harald Welte9fac4962020-02-14 21:01:23 +0100217}
218
219/***********************************************************************
220 * Incoming command from the user application
221 ***********************************************************************/
222
223/* handle a single msgb-wrapped 'struct itmsg' from the IFD-handler thread */
224static void handle_it_msg(struct client_thread *ct, struct itmsg *itmsg)
225{
226 struct bankd_client *bc = ct->bc;
227 struct msgb *tx = NULL;
228 RsproPDU_t *pdu;
229 BankSlot_t bslot;
230
231 bank_slot2rspro(&bslot, &ct->bc->bankd_slot);
232
233 switch (itmsg->type) {
234 case ITMSG_TYPE_CARD_PRES_REQ:
235 if (bc->bankd_conn.fi->state == 2 /*SRVC_ST_CONNECTED*/)
236 tx = itmsg_alloc(ITMSG_TYPE_CARD_PRES_RESP, 0, NULL, 0);
237 else
238 tx = itmsg_alloc(ITMSG_TYPE_CARD_PRES_RESP, 0xffff, NULL, 0);
239 OSMO_ASSERT(tx);
240 break;
241
242 case ITMSG_TYPE_ATR_REQ:
243 /* respond to IFD */
244 tx = itmsg_alloc(ITMSG_TYPE_ATR_RESP, 0, ct->atr, ct->atr_len);
245 OSMO_ASSERT(tx);
246 break;
247
248 case ITMSG_TYPE_POWER_OFF_REQ:
249 pdu = rspro_gen_ClientSlotStatusInd(bc->srv_conn.clslot, &bslot,
250 true, false, false, true);
251 server_conn_send_rspro(&bc->bankd_conn, pdu);
252 /* respond to IFD */
253 tx = itmsg_alloc(ITMSG_TYPE_POWER_OFF_RESP, 0, NULL, 0);
254 OSMO_ASSERT(tx);
255 break;
256
257 case ITMSG_TYPE_POWER_ON_REQ:
258 pdu = rspro_gen_ClientSlotStatusInd(bc->srv_conn.clslot, &bslot,
259 false, true, true, true);
260 server_conn_send_rspro(&bc->bankd_conn, pdu);
261 /* respond to IFD */
262 tx = itmsg_alloc(ITMSG_TYPE_POWER_ON_RESP, 0, NULL, 0);
263 OSMO_ASSERT(tx);
264 break;
265
266 case ITMSG_TYPE_RESET_REQ:
267 /* reset the [remote] card */
268 pdu = rspro_gen_ClientSlotStatusInd(bc->srv_conn.clslot, &bslot,
269 true, true, true, true);
270 server_conn_send_rspro(&bc->bankd_conn, pdu);
271 /* and take it out of reset again */
272 pdu = rspro_gen_ClientSlotStatusInd(bc->srv_conn.clslot, &bslot,
273 false, true, true, true);
274 server_conn_send_rspro(&bc->bankd_conn, pdu);
275 /* respond to IFD */
276 tx = itmsg_alloc(ITMSG_TYPE_RESET_RESP, 0, NULL, 0);
277 OSMO_ASSERT(tx);
278 break;
279 case ITMSG_TYPE_C_APDU_REQ:
280 if (!bc->srv_conn.clslot) {
281 LOGP(DMAIN, LOGL_ERROR, "Cannot send command; no client slot\n");
282 /* FIXME: Response? */
283 return;
284 }
285
286 /* Send CMD APDU to [remote] card */
287 pdu = rspro_gen_TpduModem2Card(bc->srv_conn.clslot, &bslot, itmsg->data, itmsg->len);
288 server_conn_send_rspro(&bc->bankd_conn, pdu);
289 /* response will come in asynchronously */
290 break;
291 default:
292 LOGP(DMAIN, LOGL_ERROR, "Unknown inter-thread msg type %u\n", itmsg->type);
293 break;
294 }
295
296 if (tx)
297 enqueue_to_ifd(ct, tx);
298
299}
300
301/* call-back function for inter-thread socket */
302static int it_sock_fd_cb(struct osmo_fd *ofd, unsigned int what)
303{
304 struct client_thread *ct = ofd->data;
305 int rc;
306
307 if (what & OSMO_FD_READ) {
308 struct msgb *msg = msgb_alloc_c(OTC_GLOBAL, 1024, "Rx it_fd");
309 struct itmsg *itmsg;
310
311 OSMO_ASSERT(msg);
312 rc = read(ofd->fd, msg->tail, msgb_tailroom(msg));
313 if (rc <= 0) {
314 LOGP(DMAIN, LOGL_ERROR, "Error reading from inter-thread fd: %d\n", rc);
315 pthread_exit(NULL);
316 }
317 msgb_put(msg, rc);
318 itmsg = (struct itmsg *) msgb_data(msg);
319 if (msgb_length(msg) < sizeof(*itmsg) ||
320 msgb_length(msg) < sizeof(*itmsg) + itmsg->len) {
321 LOGP(DMAIN, LOGL_ERROR, "Dropping short inter-thread message\n");
322 } else {
323 handle_it_msg(ct, itmsg);
324 }
325 msgb_free(msg);
326 }
327
328 if (what & OSMO_FD_WRITE) {
329 struct msgb *msg = msgb_dequeue(&ct->it_msgq);
330 if (!msg) {
331 /* last message: disable write events */
332 ofd->when &= ~OSMO_FD_WRITE;
333 } else {
334 unsigned int len = msgb_length(msg);
335 rc = write(ofd->fd, msgb_data(msg), len);
336 msgb_free(msg);
337 if (rc < len) {
338 LOGP(DMAIN, LOGL_ERROR, "Short write on inter-thread fd: %d < %d\n",
339 rc, len);
340 }
341 }
342 }
343
344
345 return 0;
346}
347
348/* release all resources allocated by thread */
349static void client_pthread_cleanup(void *arg)
350{
351 struct client_thread *ct = arg;
352
353 LOGP(DMAIN, LOGL_INFO, "Cleaning up remsim-client thread\n");
354 //FIXME remsim_client_destroy(ct->bc);
355 ct->bc = NULL;
356 msgb_queue_free(&ct->it_msgq);
357 osmo_fd_unregister(&ct->it_ofd);
358 close(ct->it_ofd.fd);
359 ct->it_ofd.fd = -1;
360 talloc_free(ct);
361}
362
363/* main function of remsim-client pthread */
364static void *client_pthread_main(void *arg)
365{
366 struct client_thread_cfg *cfg = arg;
Harald Welte0e968cc2020-02-22 18:16:16 +0100367 struct client_config *ccfg;
Harald Welte9fac4962020-02-14 21:01:23 +0100368 struct client_thread *ct;
Harald Welteeea631b2022-05-03 15:14:19 +0200369 char hostname[256];
Harald Welte9fac4962020-02-14 21:01:23 +0100370 int rc;
371
Harald Welteeea631b2022-05-03 15:14:19 +0200372 if (gethostname(hostname, sizeof(hostname)) < 0)
373 OSMO_STRLCPY_ARRAY(hostname, "unknown");
374
Harald Welte9fac4962020-02-14 21:01:23 +0100375 osmo_select_init();
376 rc = osmo_ctx_init("client");
377 OSMO_ASSERT(rc == 0);
378
379 ct = talloc_zero(OTC_GLOBAL, struct client_thread);
380 OSMO_ASSERT(ct);
381
Harald Welte0e968cc2020-02-22 18:16:16 +0100382 ccfg = client_config_init(ct);
383 OSMO_ASSERT(ccfg);
384 osmo_talloc_replace_string(ccfg, &ccfg->server_host, cfg->server_host);
385 if (cfg->server_port >= 0)
386 ccfg->server_port = cfg->server_port;
387 ccfg->client_id = cfg->client_id;
388 ccfg->client_slot = cfg->client_slot;
389
Harald Welte9fac4962020-02-14 21:01:23 +0100390 if (!talloc_asn1_ctx)
391 talloc_asn1_ctx= talloc_named_const(ct, 0, "asn1");
392
Harald Welteeea631b2022-05-03 15:14:19 +0200393 ct->bc = remsim_client_create(ct, hostname, "remsim_ifdhandler", ccfg);
Harald Welte9fac4962020-02-14 21:01:23 +0100394 OSMO_ASSERT(ct->bc);
395 ct->bc->data = ct;
Harald Welte9fac4962020-02-14 21:01:23 +0100396
397 INIT_LLIST_HEAD(&ct->it_msgq);
398 osmo_fd_setup(&ct->it_ofd, cfg->it_sock_fd, OSMO_FD_READ, &it_sock_fd_cb, ct, 0);
399 osmo_fd_register(&ct->it_ofd);
400
401 /* ensure we get properly cleaned up if cancelled */
402 pthread_cleanup_push(client_pthread_cleanup, ct);
403
404 osmo_fsm_inst_dispatch(ct->bc->srv_conn.fi, SRVC_E_ESTABLISH, NULL);
405
406 while (1) {
407 osmo_select_main(0);
408 }
409
410 pthread_cleanup_pop(1);
411 return NULL;
412}
413
414/***********************************************************************
415 * PC/SC ifd_handler API functions
416 ***********************************************************************/
417
418#include <ifdhandler.h>
419#include <debuglog.h>
420
421#include <sys/types.h>
422#include <sys/socket.h>
423
424static const struct value_string ifd_status_names[] = {
425 OSMO_VALUE_STRING(IFD_SUCCESS),
426 OSMO_VALUE_STRING(IFD_ERROR_TAG),
427 OSMO_VALUE_STRING(IFD_ERROR_SET_FAILURE),
428 OSMO_VALUE_STRING(IFD_ERROR_VALUE_READ_ONLY),
429 OSMO_VALUE_STRING(IFD_ERROR_PTS_FAILURE),
430 OSMO_VALUE_STRING(IFD_ERROR_NOT_SUPPORTED),
431 OSMO_VALUE_STRING(IFD_PROTOCOL_NOT_SUPPORTED),
432 OSMO_VALUE_STRING(IFD_ERROR_POWER_ACTION),
433 OSMO_VALUE_STRING(IFD_ERROR_SWALLOW),
434 OSMO_VALUE_STRING(IFD_ERROR_EJECT),
435 OSMO_VALUE_STRING(IFD_ERROR_CONFISCATE),
436 OSMO_VALUE_STRING(IFD_COMMUNICATION_ERROR),
437 OSMO_VALUE_STRING(IFD_RESPONSE_TIMEOUT),
438 OSMO_VALUE_STRING(IFD_NOT_SUPPORTED),
439 OSMO_VALUE_STRING(IFD_ICC_PRESENT),
440 OSMO_VALUE_STRING(IFD_ICC_NOT_PRESENT),
441 OSMO_VALUE_STRING(IFD_NO_SUCH_DEVICE),
442 OSMO_VALUE_STRING(IFD_ERROR_INSUFFICIENT_BUFFER),
443 { 0, NULL }
444};
445
446static const struct value_string ifd_tag_names[] = {
447 OSMO_VALUE_STRING(TAG_IFD_ATR),
448 OSMO_VALUE_STRING(TAG_IFD_SLOTNUM),
449 OSMO_VALUE_STRING(TAG_IFD_SLOT_THREAD_SAFE),
450 OSMO_VALUE_STRING(TAG_IFD_THREAD_SAFE),
451 OSMO_VALUE_STRING(TAG_IFD_SLOTS_NUMBER),
452 OSMO_VALUE_STRING(TAG_IFD_SIMULTANEOUS_ACCESS),
453 OSMO_VALUE_STRING(TAG_IFD_POLLING_THREAD),
454 OSMO_VALUE_STRING(TAG_IFD_POLLING_THREAD_KILLABLE),
455 OSMO_VALUE_STRING(TAG_IFD_STOP_POLLING_THREAD),
456 OSMO_VALUE_STRING(TAG_IFD_POLLING_THREAD_WITH_TIMEOUT),
457 { 0, NULL }
458};
459
460#define LOG_EXIT(Lun, r) \
461 Log4(r == IFD_SUCCESS || r == IFD_ICC_NOT_PRESENT ? PCSC_LOG_DEBUG : PCSC_LOG_ERROR, \
462 "%s(0x%08lx) => %s\n", __func__, Lun, get_value_string(ifd_status_names, r))
463
464#define LOG_EXITF(Lun, r, fmt, args...) \
465 Log5(r == IFD_SUCCESS ? PCSC_LOG_DEBUG : PCSC_LOG_ERROR, \
466 "%s(0x%08lx) "fmt" => %s\n", __func__, Lun, ## args, get_value_string(ifd_status_names, r))
467
468/* IFD side handle for a remsim-client [thread] */
469struct ifd_client {
470 /* the client pthread itself */
471 pthread_t pthread;
472 /* socket to talk to thread */
473 int it_fd;
474 /* configuration passed into the thread */
475 struct client_thread_cfg cfg;
476};
477
478static struct msgb *ifd_xceive_client(struct ifd_client *ic, struct msgb *tx)
479{
480 struct msgb *rx = msgb_alloc_c(OTC_GLOBAL, 1024, "ifd_rx itmsg");
481 struct itmsg *rx_it;
482 int rc;
483
484 rc = write(ic->it_fd, msgb_data(tx), msgb_length(tx));
485 msgb_free(tx);
486 if (rc < msgb_length(tx)) {
487 Log2(PCSC_LOG_ERROR, "Short write IFD->client thread: %d\n", rc);
488 msgb_free(rx);
489 return NULL;
490 }
491 rc = read(ic->it_fd, rx->tail, msgb_tailroom(rx));
492 if (rc <= 0) {
493 Log2(PCSC_LOG_ERROR, "Short read IFD<-client thread: %d\n", rc);
494 msgb_free(rx);
495 return NULL;
496 }
497 msgb_put(rx, rc);
498 rx_it = (struct itmsg *) msgb_data(rx);
499 if (msgb_length(rx) < sizeof(*rx_it) + rx_it->len) {
500 Log2(PCSC_LOG_ERROR, "Short itmsg IFD<-client thread: %d\n", msgb_length(rx));
501 msgb_free(rx);
502 return NULL;
503 }
504 return rx;
505}
506
507/* function called on IFD side to create socketpair + start remsim-client thread */
508static struct ifd_client *create_ifd_client(const struct client_thread_cfg *cfg)
509{
510 struct ifd_client *ic = talloc_zero(OTC_GLOBAL, struct ifd_client);
511 int sp[2];
512 int rc;
513
514 /* copy over configuration */
515 ic->cfg = *cfg;
516
517 /* create socket pair for communication between threads */
518 rc = socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sp);
519 if (rc != 0) {
520 talloc_free(ic);
521 return NULL;
522 }
523
524 ic->it_fd = sp[0];
525 ic->cfg.it_sock_fd = sp[1];
526
527 /* start the thread */
528 rc = pthread_create(&ic->pthread, NULL, client_pthread_main, &ic->cfg);
529 if (rc != 0) {
530 Log1(PCSC_LOG_ERROR, "Error creating remsim-client pthread\n");
531 close(sp[0]);
532 close(sp[1]);
533 talloc_free(ic);
534 return NULL;
535 }
536
537 return ic;
538}
539
540/* function called on IFD side to destroy (terminate) remsim-client thread */
541static void destroy_ifd_client(struct ifd_client *ic)
542{
543 if (!ic)
544 return;
545
546 pthread_cancel(ic->pthread);
547 pthread_join(ic->pthread, NULL);
548}
549
550#define MAX_SLOTS 256
551static struct ifd_client *ifd_client[MAX_SLOTS];
552
553#define LUN2SLOT(lun) ((lun) & 0xffff)
554#define LUN2RDR(lun) ((lun) >> 16)
555
556
557RESPONSECODE IFDHCreateChannel(DWORD Lun, DWORD Channel)
558{
559 return IFD_COMMUNICATION_ERROR;
560}
561
562RESPONSECODE IFDHCreateChannelByName(DWORD Lun, LPSTR DeviceName)
563{
564 struct ifd_client *ic;
565 struct client_thread_cfg cfg = {
Harald Welte9fac4962020-02-14 21:01:23 +0100566 .server_host = "127.0.0.1",
567 .server_port = -1,
568 .client_id = 0,
569 .client_slot = 0,
570 };
571 char *r, *client_id, *slot_nr, *host, *port;
572
573 if (LUN2RDR(Lun) != 0)
574 return IFD_NO_SUCH_DEVICE;
575
576 if (LUN2SLOT(Lun) >= ARRAY_SIZE(ifd_client))
577 return IFD_NO_SUCH_DEVICE;
578
579 ensure_osmo_ctx();
580
581 client_id = strtok_r(DeviceName, ":", &r);
582 if (!client_id)
583 goto end_parse;
584 cfg.client_id = atoi(client_id);
585
586 slot_nr = strtok_r(NULL, ":", &r);
587 if (!slot_nr)
588 goto end_parse;
589 cfg.client_slot = atoi(slot_nr);
590
591 host = strtok_r(NULL, ":", &r);
592 if (!host)
593 goto end_parse;
594 cfg.server_host = strdup(host);
595
596 port = strtok_r(NULL, ":", &r);
597 cfg.server_port = atoi(port);
598
599
600end_parse:
601 LOGP(DMAIN, LOGL_NOTICE, "remsim-client C%d:%d bankd=%s:%d\n",
602 cfg.client_id, cfg.client_slot, cfg.server_host, cfg.server_port);
603
604 ic = create_ifd_client(&cfg);
605 if (ic) {
606 ifd_client[LUN2SLOT(Lun)] = ic;
607 return IFD_SUCCESS;
608 } else
609 return IFD_COMMUNICATION_ERROR;
610}
611
612RESPONSECODE IFDHControl(DWORD Lun, DWORD dwControlCode, PUCHAR TxBuffer, DWORD TxLength,
613 PUCHAR RxBuffer, DWORD RxLength, LPDWORD pdwBytesReturned)
614{
615 RESPONSECODE r = IFD_COMMUNICATION_ERROR;
616
617 ensure_osmo_ctx();
618
619 if (LUN2RDR(Lun) != 0) {
620 r = IFD_NO_SUCH_DEVICE;
621 goto err;
622 }
623
624 if (LUN2SLOT(Lun) >= ARRAY_SIZE(ifd_client)) {
625 r = IFD_NO_SUCH_DEVICE;
626 goto err;
627 }
628
629 if (pdwBytesReturned)
630 *pdwBytesReturned = 0;
631
632 r = IFD_ERROR_NOT_SUPPORTED;
633err:
634 LOG_EXIT(Lun, r);
635 return r;
636}
637
638RESPONSECODE IFDHCloseChannel(DWORD Lun)
639{
640 RESPONSECODE r = IFD_COMMUNICATION_ERROR;
641
642 ensure_osmo_ctx();
643
644 if (LUN2RDR(Lun) != 0) {
645 r = IFD_NO_SUCH_DEVICE;
646 goto err;
647 }
648
649 if (LUN2SLOT(Lun) >= ARRAY_SIZE(ifd_client)) {
650 r = IFD_NO_SUCH_DEVICE;
651 goto err;
652 }
653
654 destroy_ifd_client(ifd_client[LUN2SLOT(Lun)]);
655 ifd_client[LUN2SLOT(Lun)] = NULL;
656
657 r = IFD_SUCCESS;
658err:
659 LOG_EXIT(Lun, r);
660 return r;
661}
662
663RESPONSECODE IFDHGetCapabilities(DWORD Lun, DWORD Tag, PDWORD Length, PUCHAR Value)
664{
665 RESPONSECODE r = IFD_COMMUNICATION_ERROR;
666 struct ifd_client *ic;
667 struct msgb *rx, *tx;
668 struct itmsg *rx_it;
669
670 ensure_osmo_ctx();
671
672 if (LUN2RDR(Lun) != 0) {
673 r = IFD_NO_SUCH_DEVICE;
674 goto err;
675 }
676
677 if (LUN2SLOT(Lun) >= ARRAY_SIZE(ifd_client)) {
678 r = IFD_NO_SUCH_DEVICE;
679 goto err;
680 }
681
682 ic = ifd_client[LUN2SLOT(Lun)];
683 if (!ic) {
684 r = IFD_NO_SUCH_DEVICE;
685 goto err;
686 }
687
688 if (!Length || !Value)
689 goto err;
690
691 switch (Tag) {
692 case TAG_IFD_ATR:
693 /* Return the ATR and its size */
694 tx = itmsg_alloc(ITMSG_TYPE_ATR_REQ, 0, NULL, 0);
695 OSMO_ASSERT(tx);
696 rx = ifd_xceive_client(ic, tx);
697 if (!rx) {
698 r = IFD_NO_SUCH_DEVICE;
699 goto err;
700 }
701 rx_it = (struct itmsg *)msgb_data(rx);
702 if (*Length > rx_it->len)
703 *Length = rx_it->len;
704 memcpy(Value, rx_it->data, *Length);
705 msgb_free(rx);
706 break;
707 case TAG_IFD_SIMULTANEOUS_ACCESS:
708 /* Return the number of sessions (readers) the driver
709 * can handle in Value[0]. This is used for multiple
710 * readers sharing the same driver. */
711 if (*Length < 1)
712 goto err;
713 *Value = 1;
714 *Length = 1;
715 break;
716 case TAG_IFD_SLOTS_NUMBER:
717 /* Return the number of slots in this reader in Value[0] */
718 if (*Length < 1)
719 goto err;
720 *Value = 1;
721 *Length = 1;
722 break;
723 case TAG_IFD_THREAD_SAFE:
724 /* If the driver supports more than one reader (see
725 * TAG_IFD_SIMULTANEOUS_ACCESS above) this tag indicates
726 * if the driver supports access to multiple readers at
727 * the same time. */
728 if (*Length < 1)
729 goto err;
730 *Value = 0;
731 *Length = 1;
732 break;
733 case TAG_IFD_SLOT_THREAD_SAFE:
734 /* If the reader has more than one slot (see
735 * TAG_IFD_SLOTS_NUMBER above) this tag indicates if the
736 * driver supports access to multiple slots of the same
737 * reader at the same time. */
738 if (*Length < 1)
739 goto err;
740 *Value = 0;
741 *Length = 1;
742 break;
743 default:
744 r = IFD_ERROR_TAG;
745 goto err;
746 }
747
748 r = IFD_SUCCESS;
749
750err:
751 if (r != IFD_SUCCESS && Length)
752 *Length = 0;
753
754 LOG_EXITF(Lun, r, "%s", get_value_string(ifd_tag_names, Tag));
755 return r;
756}
757
758RESPONSECODE IFDHSetCapabilities(DWORD Lun, DWORD Tag, DWORD Length, PUCHAR Value)
759{
760 ensure_osmo_ctx();
761
762 if (LUN2RDR(Lun) != 0)
763 return IFD_NO_SUCH_DEVICE;
764
765 if (LUN2SLOT(Lun) >= ARRAY_SIZE(ifd_client))
766 return IFD_NO_SUCH_DEVICE;
767
768
769 LOG_EXIT(Lun, IFD_NOT_SUPPORTED);
770 return IFD_NOT_SUPPORTED;
771}
772
773RESPONSECODE IFDHSetProtocolParameters(DWORD Lun, DWORD Protocol, UCHAR Flags, UCHAR PTS1,
774 UCHAR PTS2, UCHAR PTS3)
775{
776 ensure_osmo_ctx();
777
778 if (LUN2RDR(Lun) != 0)
779 return IFD_NO_SUCH_DEVICE;
780
781 if (LUN2SLOT(Lun) >= ARRAY_SIZE(ifd_client))
782 return IFD_NO_SUCH_DEVICE;
783
784 LOG_EXIT(Lun, IFD_SUCCESS);
785 return IFD_SUCCESS;
786}
787
788RESPONSECODE IFDHPowerICC(DWORD Lun, DWORD Action, PUCHAR Atr, PDWORD AtrLength)
789{
790 RESPONSECODE r = IFD_COMMUNICATION_ERROR;
791 struct ifd_client *ic;
792 struct msgb *rx, *tx;
793
794 ensure_osmo_ctx();
795
796 if (LUN2RDR(Lun) != 0) {
797 r = IFD_NO_SUCH_DEVICE;
798 goto err;
799 }
800
801 if (LUN2SLOT(Lun) >= ARRAY_SIZE(ifd_client)) {
802 r = IFD_NO_SUCH_DEVICE;
803 goto err;
804 }
805
806 ic = ifd_client[LUN2SLOT(Lun)];
807 if (!ic) {
808 r = IFD_NO_SUCH_DEVICE;
809 goto err;
810 }
811
812 switch (Action) {
813 case IFD_POWER_DOWN:
814 tx = itmsg_alloc(ITMSG_TYPE_POWER_OFF_REQ, 0, NULL, 0);
815 break;
816 case IFD_POWER_UP:
817 tx = itmsg_alloc(ITMSG_TYPE_POWER_ON_REQ, 0, NULL, 0);
818 break;
819 case IFD_RESET:
820 tx = itmsg_alloc(ITMSG_TYPE_RESET_REQ, 0, NULL, 0);
821 break;
822 default:
823 r = IFD_NOT_SUPPORTED;
824 goto err;
825 }
826
827 rx = ifd_xceive_client(ic, tx);
828 if (!rx) {
829 r = IFD_NO_SUCH_DEVICE;
830 goto err;
831 }
832
833 r = IFD_SUCCESS;
834 msgb_free(rx);
835
836err:
837 if (r != IFD_SUCCESS && AtrLength)
838 *AtrLength = 0;
839 else
840 r = IFDHGetCapabilities(Lun, TAG_IFD_ATR, AtrLength, Atr);
841
842 LOG_EXIT(Lun, r);
843 return r;
844}
845
846RESPONSECODE IFDHTransmitToICC(DWORD Lun, SCARD_IO_HEADER SendPci, PUCHAR TxBuffer,
847 DWORD TxLength, PUCHAR RxBuffer, PDWORD RxLength,
848 PSCARD_IO_HEADER RecvPci)
849{
850 RESPONSECODE r = IFD_COMMUNICATION_ERROR;
851 struct ifd_client *ic;
852 struct msgb *rx, *tx;
853 struct itmsg *rx_it;
854
855 ensure_osmo_ctx();
856
857 if (LUN2RDR(Lun) != 0) {
858 r = IFD_NO_SUCH_DEVICE;
859 goto err;
860 }
861
862 if (LUN2SLOT(Lun) >= ARRAY_SIZE(ifd_client)) {
863 r = IFD_NO_SUCH_DEVICE;
864 goto err;
865 }
866
867 ic = ifd_client[LUN2SLOT(Lun)];
868 if (!ic) {
869 r = IFD_NO_SUCH_DEVICE;
870 goto err;
871 }
872
873 tx = itmsg_alloc(ITMSG_TYPE_C_APDU_REQ, 0, TxBuffer, TxLength);
874 OSMO_ASSERT(tx);
875 /* transmit C-APDU to remote reader + blocking wait for response from peer */
876 rx = ifd_xceive_client(ic, tx);
877 if (!rx) {
878 r = IFD_NO_SUCH_DEVICE;
879 goto err;
880 }
881 rx_it = (struct itmsg *) msgb_data(rx);
882 if (*RxLength > rx_it->len)
883 *RxLength = rx_it->len;
884 memcpy(RxBuffer, rx_it->data, *RxLength);
885 msgb_free(rx);
886
887 r = IFD_SUCCESS;
888err:
889 if (r != IFD_SUCCESS && RxLength)
890 *RxLength = 0;
891
892 LOG_EXIT(Lun, r);
893 return r;
894}
895
896RESPONSECODE IFDHICCPresence(DWORD Lun)
897{
898 RESPONSECODE r = IFD_COMMUNICATION_ERROR;
899 struct ifd_client *ic;
900 struct msgb *rx, *tx;
901 struct itmsg *rx_it;
902
903 ensure_osmo_ctx();
904
905 if (LUN2RDR(Lun) != 0) {
906 r = IFD_NO_SUCH_DEVICE;
907 goto err;
908 }
909
910 if (LUN2SLOT(Lun) >= ARRAY_SIZE(ifd_client)) {
911 r = IFD_NO_SUCH_DEVICE;
912 goto err;
913 }
914
915 ic = ifd_client[LUN2SLOT(Lun)];
916 if (!ic) {
917 r = IFD_NO_SUCH_DEVICE;
918 goto err;
919 }
920
921 tx = itmsg_alloc(ITMSG_TYPE_CARD_PRES_REQ, 0, NULL, 0);
922 OSMO_ASSERT(tx);
923 rx = ifd_xceive_client(ic, tx);
924 if (!rx) {
925 r = IFD_NO_SUCH_DEVICE;
926 goto err;
927 }
928 rx_it = (struct itmsg *) msgb_data(rx);
929 if (rx_it->status == 0)
930 r = IFD_SUCCESS;
931 else
932 r = IFD_ICC_NOT_PRESENT;
933
934err:
935 LOG_EXIT(Lun, r);
936 return r;
937}
938
939static __attribute__((constructor)) void on_dso_load_ifd(void)
940{
941 void *g_tall_ctx = NULL;
942 ensure_osmo_ctx();
943 osmo_init_logging2(g_tall_ctx, &log_info);
944}