1
0
Fork 0
forked from fun/fun

Added basic PC/SC (WinSCard) smartcard support. (0.17.0)

This commit is contained in:
Johannes Findeisen 2025-10-02 20:08:21 +02:00
commit 945d668ccb
18 changed files with 693 additions and 4 deletions

View file

@ -131,7 +131,15 @@ typedef enum {
OP_SHL, // pops s, a; pushes (uint32_t)(a << (s&31))
OP_SHR, // pops s, a; pushes (uint32_t)(a >> (s&31)) logical
OP_ROTL, // pops s, a; pushes rotl32(a, s)
OP_ROTR // pops s, a; pushes rotr32(a, s)
OP_ROTR, // pops s, a; pushes rotr32(a, s)
// PCSC (smart card) opcodes
OP_PCSC_ESTABLISH, // returns context id (>0) or 0
OP_PCSC_RELEASE, // pops ctx id; returns 1/0
OP_PCSC_LIST_READERS, // pops ctx id; returns array of reader names (possibly empty)
OP_PCSC_CONNECT, // pops reader, ctx id; returns handle id (>0) or 0
OP_PCSC_DISCONNECT, // pops handle id; returns 1/0
OP_PCSC_TRANSMIT // pops apdu array, handle id; returns map {"data":[],"sw1":n,"sw2":n,"code":n}
} OpCode;
typedef struct {

View file

@ -678,6 +678,61 @@ static int emit_primary(Bytecode *bc, const char *src, size_t len, size_t *pos)
free(name);
return 1;
}
/* PCSC builtins */
if (strcmp(name, "pcsc_establish") == 0) {
(*pos)++; /* '(' */
/* no args */
if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "pcsc_establish expects ()"); free(name); return 0; }
bytecode_add_instruction(bc, OP_PCSC_ESTABLISH, 0);
free(name);
return 1;
}
if (strcmp(name, "pcsc_release") == 0) {
(*pos)++; /* '(' */
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "pcsc_release expects 1 argument (ctx)"); free(name); return 0; }
if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after pcsc_release arg"); free(name); return 0; }
bytecode_add_instruction(bc, OP_PCSC_RELEASE, 0);
free(name);
return 1;
}
if (strcmp(name, "pcsc_list_readers") == 0) {
(*pos)++; /* '(' */
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "pcsc_list_readers expects 1 argument (ctx)"); free(name); return 0; }
if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after pcsc_list_readers arg"); free(name); return 0; }
bytecode_add_instruction(bc, OP_PCSC_LIST_READERS, 0);
free(name);
return 1;
}
if (strcmp(name, "pcsc_connect") == 0) {
(*pos)++; /* '(' */
/* ctx, reader */
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "pcsc_connect expects (ctx, reader)"); free(name); return 0; }
if (!consume_char(src, len, pos, ',')) { parser_fail(*pos, "pcsc_connect expects (ctx, reader)"); free(name); return 0; }
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "pcsc_connect expects (ctx, reader)"); free(name); return 0; }
if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after pcsc_connect args"); free(name); return 0; }
bytecode_add_instruction(bc, OP_PCSC_CONNECT, 0);
free(name);
return 1;
}
if (strcmp(name, "pcsc_disconnect") == 0) {
(*pos)++; /* '(' */
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "pcsc_disconnect expects 1 argument (handle)"); free(name); return 0; }
if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after pcsc_disconnect arg"); free(name); return 0; }
bytecode_add_instruction(bc, OP_PCSC_DISCONNECT, 0);
free(name);
return 1;
}
if (strcmp(name, "pcsc_transmit") == 0) {
(*pos)++; /* '(' */
/* handle, bytes */
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "pcsc_transmit expects (handle, bytes)"); free(name); return 0; }
if (!consume_char(src, len, pos, ',')) { parser_fail(*pos, "pcsc_transmit expects (handle, bytes)"); free(name); return 0; }
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "pcsc_transmit expects (handle, bytes)"); free(name); return 0; }
if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after pcsc_transmit args"); free(name); return 0; }
bytecode_add_instruction(bc, OP_PCSC_TRANSMIT, 0);
free(name);
return 1;
}
/* string ops */
if (strcmp(name, "split") == 0) {
(*pos)++; /* '(' */

74
src/pcsc.c Normal file
View file

@ -0,0 +1,74 @@
/**
* 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 helpers: registries and helper functions.
* Included at file scope from vm.c.
*/
#ifdef FUN_WITH_PCSC
#if defined(__has_include)
#if __has_include(<PCSC/winscard.h>)
#include <PCSC/winscard.h>
#include <PCSC/wintypes.h>
#elif __has_include(<winscard.h>)
#include <winscard.h>
#else
#error "FUN_WITH_PCSC is enabled but PCSC headers were not found"
#endif
#else
#include <PCSC/winscard.h>
#include <PCSC/wintypes.h>
#endif
#include <string.h>
typedef struct {
SCARDCONTEXT ctx;
int in_use;
} pcsc_ctx_entry;
typedef struct {
SCARDHANDLE h;
DWORD proto;
int in_use;
} pcsc_card_entry;
static pcsc_ctx_entry g_pcsc_ctx[8];
static pcsc_card_entry g_pcsc_card[32];
static int pcsc_alloc_ctx_slot(void) {
for (int i = 0; i < (int)(sizeof(g_pcsc_ctx)/sizeof(g_pcsc_ctx[0])); ++i) {
if (!g_pcsc_ctx[i].in_use) { g_pcsc_ctx[i].in_use = 1; g_pcsc_ctx[i].ctx = 0; return i + 1; }
}
return 0;
}
static int pcsc_alloc_card_slot(void) {
for (int i = 0; i < (int)(sizeof(g_pcsc_card)/sizeof(g_pcsc_card[0])); ++i) {
if (!g_pcsc_card[i].in_use) { g_pcsc_card[i].in_use = 1; g_pcsc_card[i].h = 0; g_pcsc_card[i].proto = 0; return i + 1; }
}
return 0;
}
static pcsc_ctx_entry* pcsc_get_ctx(int id) {
if (id <= 0) return NULL;
int idx = id - 1;
if (idx < 0 || idx >= (int)(sizeof(g_pcsc_ctx)/sizeof(g_pcsc_ctx[0]))) return NULL;
if (!g_pcsc_ctx[idx].in_use) return NULL;
return &g_pcsc_ctx[idx];
}
static pcsc_card_entry* pcsc_get_card(int id) {
if (id <= 0) return NULL;
int idx = id - 1;
if (idx < 0 || idx >= (int)(sizeof(g_pcsc_card)/sizeof(g_pcsc_card[0]))) return NULL;
if (!g_pcsc_card[idx].in_use) return NULL;
return &g_pcsc_card[idx];
}
#endif /* FUN_WITH_PCSC */

View file

@ -11,6 +11,7 @@
#include "iter.c"
#include "map.c"
#include "string.c"
#include "pcsc.c"
#include "vm.h"
#include "value.h"
#include <stdio.h>
@ -321,6 +322,12 @@ void vm_run(VM *vm, Bytecode *entry) {
#include "vm/os/thread_spawn.c"
#include "vm/os/proc_run.c"
#include "vm/os/proc_system.c"
#include "vm/pcsc/establish.c"
#include "vm/pcsc/release.c"
#include "vm/pcsc/list_readers.c"
#include "vm/pcsc/connect.c"
#include "vm/pcsc/disconnect.c"
#include "vm/pcsc/transmit.c"
#include "vm/strings/find.c"
#include "vm/strings/split.c"

View file

@ -35,7 +35,8 @@ static const char *opcode_names[] = {
"MAKE_MAP","KEYS","VALUES","HAS_KEY",
"READ_FILE","WRITE_FILE","ENV","INPUT_LINE","PROC_RUN","PROC_SYSTEM",
"THREAD_SPAWN","THREAD_JOIN","SLEEP_MS",
"BAND","BOR","BXOR","BNOT","SHL","SHR","ROTL","ROTR"
"BAND","BOR","BXOR","BNOT","SHL","SHR","ROTL","ROTR",
"PCSC_ESTABLISH","PCSC_RELEASE","PCSC_LIST_READERS","PCSC_CONNECT","PCSC_DISCONNECT","PCSC_TRANSMIT"
};
typedef struct {
@ -78,7 +79,7 @@ void vm_dump_globals(VM *vm);
void vm_run(VM *vm, Bytecode *entry);
static inline int opcode_is_valid(int op) {
return op >= OP_NOP && op <= OP_ROTR; // all current opcodes
return op >= OP_NOP && op <= OP_PCSC_TRANSMIT; // all current opcodes
}
#endif

48
src/vm/pcsc/connect.c Normal file
View 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
View 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
View 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;
}

View 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
View 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
View 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;
}