Added basic PC/SC (WinSCard) smartcard support. (0.17.0)
This commit is contained in:
parent
1ac163a01e
commit
945d668ccb
18 changed files with 693 additions and 4 deletions
48
src/vm/pcsc/connect.c
Normal file
48
src/vm/pcsc/connect.c
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-10-02
|
||||
*/
|
||||
|
||||
/* PCSC connect */
|
||||
case OP_PCSC_CONNECT: {
|
||||
#ifdef FUN_WITH_PCSC
|
||||
/* Stack: [..., ctx_id, reader_name] -> pops reader_name first, then ctx_id */
|
||||
Value vreader = pop_value(vm);
|
||||
Value vctx = pop_value(vm);
|
||||
|
||||
int ctx_id = (int)vctx.i;
|
||||
free_value(vctx);
|
||||
char *rname = value_to_string_alloc(&vreader);
|
||||
free_value(vreader);
|
||||
|
||||
pcsc_ctx_entry *e = pcsc_get_ctx(ctx_id);
|
||||
if (!e || !rname) { if (rname) free(rname); push_value(vm, make_int(0)); break; }
|
||||
|
||||
int hslot = pcsc_alloc_card_slot();
|
||||
if (!hslot) { free(rname); push_value(vm, make_int(0)); break; }
|
||||
pcsc_card_entry *ce = pcsc_get_card(hslot);
|
||||
DWORD dwActive = 0;
|
||||
LONG rv = SCardConnect(e->ctx, rname, SCARD_SHARE_SHARED,
|
||||
SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1,
|
||||
&ce->h, &dwActive);
|
||||
free(rname);
|
||||
if (rv != SCARD_S_SUCCESS) {
|
||||
ce->in_use = 0;
|
||||
push_value(vm, make_int(0));
|
||||
break;
|
||||
}
|
||||
ce->proto = dwActive;
|
||||
push_value(vm, make_int(hslot));
|
||||
#else
|
||||
Value vreader = pop_value(vm); free_value(vreader);
|
||||
Value vctx = pop_value(vm); free_value(vctx);
|
||||
push_value(vm, make_int(0));
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
30
src/vm/pcsc/disconnect.c
Normal file
30
src/vm/pcsc/disconnect.c
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-10-02
|
||||
*/
|
||||
|
||||
/* PCSC disconnect */
|
||||
case OP_PCSC_DISCONNECT: {
|
||||
#ifdef FUN_WITH_PCSC
|
||||
Value vh = pop_value(vm);
|
||||
int hid = (int)vh.i;
|
||||
free_value(vh);
|
||||
pcsc_card_entry *ce = pcsc_get_card(hid);
|
||||
if (!ce) { push_value(vm, make_int(0)); break; }
|
||||
SCardDisconnect(ce->h, SCARD_LEAVE_CARD);
|
||||
ce->in_use = 0;
|
||||
ce->h = 0;
|
||||
ce->proto = 0;
|
||||
push_value(vm, make_int(1));
|
||||
#else
|
||||
Value vh = pop_value(vm); free_value(vh);
|
||||
push_value(vm, make_int(0));
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
26
src/vm/pcsc/establish.c
Normal file
26
src/vm/pcsc/establish.c
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-10-02
|
||||
*/
|
||||
|
||||
/* PCSC establish */
|
||||
case OP_PCSC_ESTABLISH: {
|
||||
#ifdef FUN_WITH_PCSC
|
||||
int slot = pcsc_alloc_ctx_slot();
|
||||
if (!slot) { push_value(vm, make_int(0)); break; }
|
||||
pcsc_ctx_entry *e = pcsc_get_ctx(slot);
|
||||
if (!e) { push_value(vm, make_int(0)); break; }
|
||||
LONG rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &e->ctx);
|
||||
if (rv != SCARD_S_SUCCESS) { e->in_use = 0; push_value(vm, make_int(0)); break; }
|
||||
push_value(vm, make_int(slot));
|
||||
#else
|
||||
push_value(vm, make_int(0));
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
64
src/vm/pcsc/list_readers.c
Normal file
64
src/vm/pcsc/list_readers.c
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-10-02
|
||||
*/
|
||||
|
||||
/* PCSC list_readers */
|
||||
case OP_PCSC_LIST_READERS: {
|
||||
#ifdef FUN_WITH_PCSC
|
||||
/* Pop context id; return [] if anything goes wrong */
|
||||
Value vid = pop_value(vm);
|
||||
int id = (int)vid.i;
|
||||
free_value(vid);
|
||||
|
||||
pcsc_ctx_entry *e = pcsc_get_ctx(id);
|
||||
if (!e) { push_value(vm, make_array_from_values(NULL, 0)); break; }
|
||||
|
||||
DWORD sz = 0;
|
||||
LONG rv = SCardListReaders(e->ctx, NULL, NULL, &sz);
|
||||
if (rv != SCARD_S_SUCCESS || sz == 0) {
|
||||
push_value(vm, make_array_from_values(NULL, 0));
|
||||
break;
|
||||
}
|
||||
|
||||
char *msz = (char*)malloc(sz);
|
||||
if (!msz) { push_value(vm, make_array_from_values(NULL, 0)); break; }
|
||||
|
||||
rv = SCardListReaders(e->ctx, NULL, msz, &sz);
|
||||
if (rv != SCARD_S_SUCCESS) {
|
||||
free(msz);
|
||||
push_value(vm, make_array_from_values(NULL, 0));
|
||||
break;
|
||||
}
|
||||
|
||||
Value vals[64];
|
||||
int count = 0;
|
||||
char *p = msz;
|
||||
while (*p && count < (int)(sizeof(vals)/sizeof(vals[0]))) {
|
||||
size_t n = strlen(p);
|
||||
vals[count++] = make_string(p);
|
||||
p += n + 1;
|
||||
}
|
||||
Value arr = make_array_from_values(vals, count);
|
||||
for (int i = 0; i < count; ++i) free_value(vals[i]);
|
||||
free(msz);
|
||||
|
||||
if (arr.type != VAL_ARRAY) {
|
||||
push_value(vm, make_array_from_values(NULL, 0));
|
||||
} else {
|
||||
push_value(vm, arr);
|
||||
}
|
||||
#else
|
||||
/* Fallback (no PCSC): consume ctx id and return [] to keep the stack consistent */
|
||||
Value vid = pop_value(vm);
|
||||
free_value(vid);
|
||||
push_value(vm, make_array_from_values(NULL, 0));
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
29
src/vm/pcsc/release.c
Normal file
29
src/vm/pcsc/release.c
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-10-02
|
||||
*/
|
||||
|
||||
/* PCSC release */
|
||||
case OP_PCSC_RELEASE: {
|
||||
#ifdef FUN_WITH_PCSC
|
||||
Value vid = pop_value(vm);
|
||||
int id = (int)vid.i;
|
||||
free_value(vid);
|
||||
pcsc_ctx_entry *e = pcsc_get_ctx(id);
|
||||
if (!e) { push_value(vm, make_int(0)); break; }
|
||||
SCardReleaseContext(e->ctx);
|
||||
e->in_use = 0;
|
||||
e->ctx = 0;
|
||||
push_value(vm, make_int(1));
|
||||
#else
|
||||
Value vid = pop_value(vm); free_value(vid);
|
||||
push_value(vm, make_int(0));
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
95
src/vm/pcsc/transmit.c
Normal file
95
src/vm/pcsc/transmit.c
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-10-02
|
||||
*/
|
||||
|
||||
/* PCSC transmit */
|
||||
case OP_PCSC_TRANSMIT: {
|
||||
#ifdef FUN_WITH_PCSC
|
||||
/* pops apdu array, handle_id */
|
||||
Value vapdu = pop_value(vm);
|
||||
Value vh = pop_value(vm);
|
||||
int hid = (int)vh.i;
|
||||
free_value(vh);
|
||||
pcsc_card_entry *ce = pcsc_get_card(hid);
|
||||
|
||||
Value m = make_map_empty();
|
||||
map_set(&m, "data", make_array_from_values(NULL, 0));
|
||||
map_set(&m, "sw1", make_int(-1));
|
||||
map_set(&m, "sw2", make_int(-1));
|
||||
map_set(&m, "code", make_int(-1));
|
||||
|
||||
if (!ce || vapdu.type != VAL_ARRAY) {
|
||||
free_value(vapdu);
|
||||
push_value(vm, m);
|
||||
break;
|
||||
}
|
||||
int n = array_length(&vapdu);
|
||||
if (n < 0) n = 0;
|
||||
unsigned char sbuf[4096];
|
||||
if (n > (int)sizeof(sbuf)) n = (int)sizeof(sbuf);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
Value tmp;
|
||||
if (array_get_copy(&vapdu, i, &tmp)) {
|
||||
int64_t v = tmp.type == VAL_INT ? tmp.i : 0;
|
||||
sbuf[i] = (unsigned char)(v & 0xFF);
|
||||
free_value(tmp);
|
||||
} else {
|
||||
sbuf[i] = 0;
|
||||
}
|
||||
}
|
||||
free_value(vapdu);
|
||||
|
||||
SCARD_IO_REQUEST pio;
|
||||
if (ce->proto == SCARD_PROTOCOL_T0) pio = *SCARD_PCI_T0;
|
||||
else if (ce->proto == SCARD_PROTOCOL_T1) pio = *SCARD_PCI_T1;
|
||||
else { pio = *SCARD_PCI_T1; }
|
||||
|
||||
unsigned char rbuf[4096];
|
||||
DWORD rlen = sizeof(rbuf);
|
||||
LONG rv = SCardTransmit(ce->h, &pio, sbuf, (DWORD)n, NULL, rbuf, &rlen);
|
||||
|
||||
int sw1 = -1, sw2 = -1;
|
||||
int datalen = 0;
|
||||
if (rv == SCARD_S_SUCCESS && rlen >= 2) {
|
||||
sw1 = rbuf[rlen - 2];
|
||||
sw2 = rbuf[rlen - 1];
|
||||
datalen = (int)rlen - 2;
|
||||
}
|
||||
|
||||
map_set(&m, "sw1", make_int(sw1));
|
||||
map_set(&m, "sw2", make_int(sw2));
|
||||
map_set(&m, "code", make_int((int)rv));
|
||||
|
||||
if (datalen > 0) {
|
||||
Value *vals = (Value*)malloc(sizeof(Value) * (size_t)datalen);
|
||||
if (!vals) {
|
||||
map_set(&m, "data", make_array_from_values(NULL, 0));
|
||||
} else {
|
||||
for (int i = 0; i < datalen; ++i) vals[i] = make_int((int64_t)rbuf[i]);
|
||||
Value arr = make_array_from_values(vals, datalen);
|
||||
for (int i = 0; i < datalen; ++i) free_value(vals[i]);
|
||||
free(vals);
|
||||
map_set(&m, "data", arr);
|
||||
}
|
||||
}
|
||||
|
||||
push_value(vm, m);
|
||||
#else
|
||||
Value vapdu = pop_value(vm); free_value(vapdu);
|
||||
Value vh = pop_value(vm); free_value(vh);
|
||||
Value m = make_map_empty();
|
||||
map_set(&m, "data", make_array_from_values(NULL, 0));
|
||||
map_set(&m, "sw1", make_int(-1));
|
||||
map_set(&m, "sw2", make_int(-1));
|
||||
map_set(&m, "code", make_int(-2));
|
||||
push_value(vm, m);
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue