- cats-test and picc_to_pcsc are using the same pcsc abstraction. this reduces the code to maintain

- partial rewrite of picc_to_pcsc to reduce memory leaks and add fault tolerancy


git-svn-id: https://vsmartcard.svn.sourceforge.net/svnroot/vsmartcard@300 96b47cad-a561-4643-ad3b-153ac7d7599c
This commit is contained in:
frankmorgner
2010-10-07 10:41:45 +00:00
parent 320cca0731
commit 480cf4d722
10 changed files with 223 additions and 931 deletions

View File

@@ -3,8 +3,8 @@ EXTRA_DIST = picc.py
bin_PROGRAMS = picc_to_pcsc
bin_SCRIPTS = picc.py
picc_to_pcsc_SOURCES = picc_to_pcsc.c
picc_to_pcsc_SOURCES = picc_to_pcsc.c pcscutil.c
picc_to_pcsc_LDADD = $(PCSC_LIBS)
picc_to_pcsc_CFLAGS = $(PCSC_CFLAGS)
noinst_HEADERS = picc_to_pcsc.h
noinst_HEADERS = picc_to_pcsc.h pcscutil.h

1
picc_to_pcsc/src/pcscutil.c Symbolic link
View File

@@ -0,0 +1 @@
../../ccid/src/pcscutil.c

1
picc_to_pcsc/src/pcscutil.h Symbolic link
View File

@@ -0,0 +1 @@
../../ccid/src/pcscutil.h

View File

@@ -10,8 +10,13 @@
#include <unistd.h>
#include <winscard.h>
#include <string.h>
#include <errno.h>
#include "picc_to_pcsc.h"
#include "pcscutil.h"
static FILE *fd; /*filehandle used for PICCDEV*/
static sighandler_t register_sig(int signo, sighandler_t sighandler) {
struct sigaction new_sig, old_sig;
@@ -25,179 +30,63 @@ static sighandler_t register_sig(int signo, sighandler_t sighandler) {
}
char* perform_initialization(int num)
{
char *readers, *str;
DWORD size=0;
LONG r;
r = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hcontext);
if (r != SCARD_S_SUCCESS) {
fprintf(stderr, "pc/sc error: %s\n", pcsc_stringify_error(r));
SCardReleaseContext(hcontext);
return NULL;
}
r = SCardListReaders(hcontext, NULL, NULL, &size);
if (size == 0)
r = SCARD_E_UNKNOWN_READER;
if (r != SCARD_S_SUCCESS) {
fprintf(stderr, "pc/sc error: %s\n", pcsc_stringify_error(r));
SCardReleaseContext(hcontext);
return NULL;
}
/* get all readers */
readers = (char*)malloc(size);
if (readers == NULL) {
fprintf(stderr, "pc/sc error: %s\n",
pcsc_stringify_error(SCARD_E_NO_MEMORY));
SCardReleaseContext(hcontext);
return NULL;
}
r = SCardListReaders(hcontext, NULL, readers, &size);
if (r != SCARD_S_SUCCESS) {
free(readers);
fprintf(stderr, "pc/sc error: %s\n", pcsc_stringify_error(r));
SCardReleaseContext(hcontext);
return NULL;
}
/* name of reader number num*/
str = readers;
for (size = 0; size < num; size++) {
/* go to the next name */
str += strlen(str) + 1;
/* no more readers available? */
if (strlen(str) == 0) {
free(readers);
fprintf(stderr, "pc/sc error: %s\n",
pcsc_stringify_error(SCARD_E_UNKNOWN_READER));
SCardReleaseContext(hcontext);
return NULL;
}
}
strcpy(reader_name, str);
free(readers);
rstate.dwCurrentState = SCARD_STATE_UNAWARE;
rstate.szReader = reader_name;
return reader_name;
}
void setup(void) {
/*setup environment, open device, set global filehandle*/
DWORD dwActiveProtocol;
LONG pcsc_result;
perform_initialization(2); /*Right num for PICC?*/
printf("Initialised reader: %s\n",reader_name);
pcsc_result = SCardConnect(hcontext, reader_name,
SCARD_SHARE_EXCLUSIVE, (SCARD_PROTOCOL_T0|SCARD_PROTOCOL_T1),
&hcard, &dwActiveProtocol);
if (pcsc_result == SCARD_S_SUCCESS) {
pcsc_result = SCardGetStatusChange(hcontext, 1, &rstate, 1);
if (pcsc_result != SCARD_S_SUCCESS) fprintf(stderr,"%s\n",pcsc_stringify_error(pcsc_result));
} else {
fprintf(stderr,"%s\n",pcsc_stringify_error(pcsc_result));
}
/*Register our signal handlers*/
register_sig(SIGINT,checkSig);
/*Open the device*/
fd = fopen(PICCDEV, "a+"); /*O_NOCTTY ?*/
if (fd == NULL) {
fprintf(stderr,"Failed to open %s. Exiting.\n", PICCDEV);
exit(-1);
} else printf("Initialisation completed\n");
}
void checkSig(int signo){
if (signo == SIGINT)
printf("Received SIGINT, exiting.\n");
cleanup();
}
void printHex(char *title, unsigned char *msg, unsigned int length) {
int n = 0;
printf("%s: \t",title);
while (n<length) {
printf("%x ",msg[n]);
n++;
}
printf("\n");
}
void cleanup(void) {
int releaseContext;
/*Power down card*/
SCardDisconnect(hcard, SCARD_UNPOWER_CARD);
releaseContext = SCardReleaseContext(hcontext);
if (releaseContext != SCARD_S_SUCCESS) {
printf("%s\n",pcsc_stringify_error(releaseContext));
}
printf("Smartcard disconnected\n");
/*Close the device*/
fclose(fd);
/*Die*/
exit(0);
}
unsigned int read_from_device(unsigned char *inputBuffer) {
char *line = NULL;
size_t linelen=0;
if(getline(&line, &linelen, fd) != -1) {
printf("%s\n",line);
fflush(stdout);
} else {line = NULL;}// cleanup();} //Should we quit when we encounter an EOL?
unsigned int length=0;
unsigned char *buf=NULL;
if (line != NULL && line[0] == '0') { //Ignore empty lines and 'RESET' lines
sscanf(line, "%x : ", &length );
buf = malloc(length);
if(buf == NULL) { fprintf(stderr, "WAHH!\n"); return 0; }
int pos=0;
while(sscanf(line+(pos*3+5), " %x ", (unsigned int *) &(buf[pos])) == 1) {
printf("[%02X]", buf[pos]);
if(++pos >= length) break;
}
printf("Have %i bytes, should have %i bytes\n", pos, length);
memcpy(inputBuffer,buf,length);
}
int picc_decode(char *inbuf, size_t inlen,
unsigned char **outbuf, size_t *outlen)
{
size_t pos;
unsigned char buf[0xffff];
char *end, *p;
unsigned long int b, length;
if(line != NULL) {
free(line);
}
if(buf != NULL) {
free(buf);
}
return length;
}
if (inbuf == NULL || inlen == 0 || inbuf[0] == '\0') {
//Ignore empty lines and 'RESET' lines
*outlen = 0;
return 0;
}
int transmit_to_pcsc(unsigned char *inputBuffer, unsigned int inputLength, unsigned char *outputBuffer, unsigned int *outputLength) {
errno = 0;
length = strtoul(inbuf, &end, 16);
if (errno)
goto err;
printHex("APDU", inputBuffer, inputLength);
unsigned int dwRecvLength = MAX_BUFFER_SIZE;
int result;
result = SCardTransmit(hcard, SCARD_PCI_T1, inputBuffer,
inputLength, NULL, outputBuffer, (DWORD*) &dwRecvLength);
/* check for ':' right behind the length */
if (inbuf+inlen < end+1 || end[0] != ':')
goto err;
end++;
p = realloc(*outbuf, length);
if (!p)
goto err;
*outbuf = p;
*outlen = length;
pos = 0;
while(inbuf+inlen > end && length > pos) {
b = strtoul(end, &end, 16);
if (errno || b > 0xff)
goto err;
(*outbuf)[pos++] = b;
}
/*Check response code */
if (result != SCARD_S_SUCCESS) {
fprintf(stderr,"SCardTransmit failed.\n");
fprintf(stderr,"dwRecvLength = %d maximum is:%d\n",dwRecvLength,MAX_BUFFER_SIZE);
fprintf(stderr,"%s\n",pcsc_stringify_error(result));
return -1;
} else {
printf("Received %d Byte\n",dwRecvLength);
printHex("RAPDU", outputBuffer, dwRecvLength);
*outputLength = dwRecvLength;
}
return 0;
err:
*outlen = 0;
return -1;
}
void write_to_device(unsigned char *buffer, unsigned int msgLength) {
@@ -217,25 +106,89 @@ void write_to_device(unsigned char *buffer, unsigned int msgLength) {
free(foo);
}
int main (int argv, char **args) {
unsigned char inputBuffer[MAX_BUFFER_SIZE];
unsigned char outputBuffer[MAX_BUFFER_SIZE];
unsigned int inputLength, outputLength = MAX_BUFFER_SIZE;
int status;
setup();
int main (int argc, char **argv)
{
/*printf("%s:%d\n", __FILE__, __LINE__);*/
unsigned char *inputBuffer = NULL;
size_t inputLength;
BYTE outputBuffer[MAX_BUFFER_SIZE];
DWORD outputLength;
while(1) {
inputLength = read_from_device(inputBuffer);
if (inputLength == 0) continue;
fflush(fd);
status = transmit_to_pcsc(inputBuffer,inputLength,outputBuffer,&outputLength);
if (!status) {
write_to_device(outputBuffer, outputLength);
//fprintf(fd,"0002: 90 00\0");
} else {
printf("Failed\n");
}
}
cleanup();
return 0;
LPSTR readers = NULL;
LONG r;
DWORD ctl, protocol;
SCARDCONTEXT hContext;
SCARDHANDLE hCard;
char *read = NULL;
size_t readlen = 0;
ssize_t linelen;
unsigned int readernum = 0;
if (argc > 1) {
if (argc > 2) {
fprintf(stderr, "Usage: "
"%s [reader number] [PIN]\n", argv[0]);
}
if (sscanf(argv[1], "%u", &readernum) != 1) {
fprintf(stderr, "Could not get number of reader\n");
exit(2);
}
}
/*Register signal handlers*/
register_sig(SIGINT,checkSig);
/*Open the device*/
fd = fopen(PICCDEV, "a+"); /*O_NOCTTY ?*/
if (fd == NULL) {
fprintf(stderr,"Failed to open %s. Exiting.\n", PICCDEV);
exit(-1);
} else
printf("Initialisation completed\n");
r = pcsc_connect(readernum, SCARD_SHARE_EXCLUSIVE, SCARD_PROTOCOL_ANY,
&hContext, &readers, &hCard, &protocol);
if (r != SCARD_S_SUCCESS)
goto err;
while(1) {
linelen = getline(&read, &readlen, fd);
if (linelen < 0) {
if (linelen < 0) {
fprintf(stderr,"Error reading from %s\n", PICCDEV);
goto err;
}
}
if (linelen == 0)
continue;
fflush(fd);
printf("%s\n", read);
if (picc_decode(read, linelen, &inputBuffer, &inputLength) < 0)
continue;
printb("C-APDU:\n", inputBuffer, inputLength);
outputLength = MAX_BUFFER_SIZE;
r = pcsc_transmit(protocol, hCard, inputBuffer, inputLength, outputBuffer, &outputLength);
if (r != SCARD_S_SUCCESS)
goto err;
printb("R-APDU:\n", outputBuffer, outputLength);
write_to_device(outputBuffer, outputLength);
//fprintf(fd,"0002: 90 00\0");
}
cleanup();
err:
stringify_error(r);
pcsc_disconnect(hContext, hCard, readers);
exit(r == SCARD_S_SUCCESS ? 0 : 1);
}

View File

@@ -4,18 +4,10 @@
#define PICCDEV "/dev/ttyACM0"
typedef void (*sighandler_t)(int);
/*Global variables*/
FILE *fd; /*filehandle used for PICCDEV*/
SCARDCONTEXT hcontext;
SCARDHANDLE hcard;
SCARD_READERSTATE rstate;
char reader_name[MAX_READERNAME];
static sighandler_t register_sig(int signo, sighandler_t sighandler);
void checkSig(int signo);
unsigned int read_from_device(unsigned char *inputBuffer);
int transmit_to_pcsc(unsigned char *inputBuffer, unsigned int inputLength, unsigned char *outputBuffer, unsigned int *outputLength);
void write_to_device(unsigned char *buffer, unsigned int msglength);
void setup(void);
void cleanup(void);