From: Eugene Crosser Date: Sun, 20 Oct 2013 20:22:07 +0000 (+0400) Subject: parse options, specify ykneo slot X-Git-Url: http://www.average.org/gitweb/?p=pam_pcsc_cr.git;a=commitdiff_plain;h=0894042d90da9c8ad0d03e875c727442e778a7d6 parse options, specify ykneo slot --- diff --git a/Makefile b/Makefile index db68800..80c9948 100644 --- a/Makefile +++ b/Makefile @@ -4,3 +4,7 @@ CFLAGS += -g $(shell pkg-config --cflags libpcsclite) LDLIBS += $(shell pkg-config --libs libpcsclite) test_cr: test_cr.o pcsc_cr.o ykneo.o + +test_cr.o: pcsc_cr.h +pcsc_cr.o: pcsc_cr.h token.h +ykneo.o: token.h diff --git a/pcsc_cr.c b/pcsc_cr.c index 0354c32..0559212 100644 --- a/pcsc_cr.c +++ b/pcsc_cr.c @@ -1,4 +1,6 @@ +#include #include +#include #include "token.h" #include @@ -97,3 +99,26 @@ free_out: char *pcsc_errstr(long err) { return pcsc_stringify_error(err); } + +int pcsc_option(char *option) +{ + char *name, *key, *val; + int i, rc = -1; + struct token_interface *type; + + name=(char *)alloca(strlen(option)+1); + strcpy(name, option); + if ((key = strchr(name, ':'))) *(key++) = '\0'; + else return -1; + if ((val = strchr(key, '='))) *(val++) = '\0'; + else return -1; + if (*val == '\0') return -1; + for (i = 0; types[i]; i++) { + type = types[i]; + if (!strcmp(type->name,name)) { + rc = type->parse_option(key, val); + break; + } + } + return rc; +} diff --git a/pcsc_cr.h b/pcsc_cr.h index da97333..35be3a4 100644 --- a/pcsc_cr.h +++ b/pcsc_cr.h @@ -1,6 +1,7 @@ #ifndef _PCSC_CR_H #define _PCSC_CR_H +int pcsc_option(char *option); long pcsc_cr(unsigned char *chal, int csize, unsigned char *resp, int *rsize); char *pcsc_errstr(long err); diff --git a/test_cr.c b/test_cr.c index 2d66d5f..987df3f 100644 --- a/test_cr.c +++ b/test_cr.c @@ -1,15 +1,39 @@ #include +#include +#include +#include #include "pcsc_cr.h" char chal[] = { 0x0f,0x65,0xd1,0x3a,0xfe,0xcb,0xc4,0xb9,0x52,0xb1,0x60,0xcf,0xe8,0x55,0x6a,0xdd,0xfb,0xef,0xf6,0x55,0x83,0x4c,0x8d,0xea,0x38,0xea,0x3b,0x26,0xf7,0x0a,0xe8,0x0d,0x31,0x38,0xee,0x16,0x5d,0xab,0x8b,0x7f,0xf0,0x1b,0xe3,0xbe,0xd8,0x4b,0x6e,0x44,0x42,0x8d,0x0f,0xc1,0x3b,0x23,0xea,0xfe,0xc0,0x68,0xc1,0x0f,0x60,0x6c,0xf4}; +static void usage(const char const *cmd) +{ + fprintf(stderr, "usage: %s [-o backend:name=value] ...\n", cmd); +} + int main(int argc, char *argv[]) { unsigned char rbuf[20]; int rsize = sizeof(rbuf); int i; long rc; + int c; + + while ((c = getopt(argc, argv, "ho:")) != -1) switch (c) { + case 'h': + usage(argv[0]); + exit(0); + case 'o': + if (pcsc_option(optarg)) { + fprintf(stderr, "Option \"%s\" bad\n", optarg); + exit(1); + } + break; + default: + usage(argv[0]); + exit(1); + } memset(rbuf, 0xFE, sizeof(rbuf)); rc = pcsc_cr(chal, sizeof(chal), rbuf, &rsize); diff --git a/token.h b/token.h index 743aafb..3af9ddb 100644 --- a/token.h +++ b/token.h @@ -6,6 +6,8 @@ extern SCARD_IO_REQUEST pioSendPci; struct token_interface { + char *name; + int (*parse_option)(char *key, char *val); DWORD (*check_atr_hb)(LPTSTR str, DWORD size); DWORD (*prologue)(SCARDHANDLE hCard,LPTSTR envp[]); DWORD (*trancieve)(SCARDHANDLE hCard,LPTSTR envp[], diff --git a/ykneo.c b/ykneo.c index 3399f71..79c86e5 100644 --- a/ykneo.c +++ b/ykneo.c @@ -1,3 +1,4 @@ +#include #include #include @@ -7,7 +8,27 @@ static const BYTE selcmd[] = {0x00, 0xA4, 0x04, 0x00, 0x07, 0xA0, 0x00, 0x00, 0x05, 0x27, 0x20, 0x01, 0x00}; -static const BYTE cr_cmd[] = {0x00, 0x01, 0x38, 0x00}; +static const BYTE cr_cmd[] = {0x00, 0x01, 0xff, 0x00}; + +static BYTE cr_for_slot[3] = {0xff, 0x30, 0x38}; + +static int slot; + +static int ykn_parse_option(char *key, char *val) +{ + if (!strcmp(key, "slot")) { + if (!strcmp(val, "1")) { + slot = 1; + } else if (!strcmp(val, "2")) { + slot = 2; + } else { + return -1; + } + } else { + return -1; + } + return 0; +} static DWORD ykn_check_atr_hb(LPTSTR str, DWORD size) { @@ -39,6 +60,7 @@ static DWORD ykn_trancieve(SCARDHANDLE hCard,LPTSTR envp[], BYTE *rbuf = alloca(rsize); BYTE *sbuf = alloca(sendsize + 6); memcpy(sbuf, cr_cmd, sizeof(cr_cmd)); + sbuf[2] = cr_for_slot[slot]; sbuf[sizeof(cr_cmd)] = sendsize; memcpy(sbuf + sizeof(cr_cmd) + 1, send, sendsize); sbuf[sendsize + 5] = rsize; @@ -58,6 +80,8 @@ static DWORD ykn_epilogue(SCARDHANDLE hCard,LPTSTR envp[]) } struct token_interface ykneo_interface = { + .name = "ykneo", + .parse_option = ykn_parse_option, .check_atr_hb = ykn_check_atr_hb, .prologue = ykn_prologue, .trancieve = ykn_trancieve,