rewrite and cleanup complete
git-svn-id: https://vsmartcard.svn.sourceforge.net/svnroot/vsmartcard@301 96b47cad-a561-4643-ad3b-153ac7d7599c
This commit is contained in:
@@ -15,148 +15,166 @@
|
|||||||
#include "picc_to_pcsc.h"
|
#include "picc_to_pcsc.h"
|
||||||
#include "pcscutil.h"
|
#include "pcscutil.h"
|
||||||
|
|
||||||
static FILE *fd; /*filehandle used for PICCDEV*/
|
static FILE *picc_fd = 0; /*filehandle used for PICCDEV*/
|
||||||
|
|
||||||
|
static LPSTR readers = NULL;
|
||||||
|
static SCARDCONTEXT hContext = 0;
|
||||||
|
static SCARDHANDLE hCard = 0;
|
||||||
|
|
||||||
|
|
||||||
static sighandler_t register_sig(int signo, sighandler_t sighandler) {
|
static sighandler_t register_sig(int signo, sighandler_t sighandler) {
|
||||||
struct sigaction new_sig, old_sig;
|
|
||||||
|
|
||||||
new_sig.sa_handler = sighandler;
|
|
||||||
sigemptyset (&new_sig.sa_mask);
|
|
||||||
new_sig.sa_flags = SA_RESTART;
|
|
||||||
if (sigaction (signo, &new_sig, &old_sig) < 0)
|
|
||||||
return SIG_ERR;
|
|
||||||
return old_sig.sa_handler;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void checkSig(int signo){
|
void cleanup_exit(int signo){
|
||||||
if (signo == SIGINT)
|
|
||||||
printf("Received SIGINT, exiting.\n");
|
|
||||||
cleanup();
|
cleanup();
|
||||||
}
|
|
||||||
|
|
||||||
void cleanup(void) {
|
|
||||||
/*Close the device*/
|
|
||||||
fclose(fd);
|
|
||||||
/*Die*/
|
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int picc_decode(char *inbuf, size_t inlen,
|
void cleanup(void) {
|
||||||
unsigned char **outbuf, size_t *outlen)
|
fclose(picc_fd);
|
||||||
|
pcsc_disconnect(hContext, hCard, readers);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t picc_decode_apdu(char *inbuf, size_t inlen, unsigned char **outbuf)
|
||||||
{
|
{
|
||||||
size_t pos;
|
size_t pos, length;
|
||||||
unsigned char buf[0xffff];
|
unsigned char buf[0xffff];
|
||||||
char *end, *p;
|
char *end, *p;
|
||||||
unsigned long int b, length;
|
unsigned long int b;
|
||||||
|
|
||||||
if (inbuf == NULL || inlen == 0 || inbuf[0] == '\0') {
|
if (inbuf == NULL || inlen == 0 || inbuf[0] == '\0') {
|
||||||
//Ignore empty lines and 'RESET' lines
|
//Ignore empty lines and 'RESET' lines
|
||||||
*outlen = 0;
|
goto noapdu;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
errno = 0;
|
|
||||||
length = strtoul(inbuf, &end, 16);
|
length = strtoul(inbuf, &end, 16);
|
||||||
if (errno)
|
|
||||||
goto err;
|
|
||||||
|
|
||||||
/* check for ':' right behind the length */
|
/* check for ':' right behind the length */
|
||||||
if (inbuf+inlen < end+1 || end[0] != ':')
|
if (inbuf+inlen < end+1 || end[0] != ':')
|
||||||
goto err;
|
goto noapdu;
|
||||||
end++;
|
end++;
|
||||||
|
|
||||||
p = realloc(*outbuf, length);
|
p = realloc(*outbuf, length);
|
||||||
if (!p)
|
if (!p) {
|
||||||
goto err;
|
fprintf(stderr, "Error allocating memory for decoded C-APDU\n");
|
||||||
|
goto noapdu;
|
||||||
|
}
|
||||||
*outbuf = p;
|
*outbuf = p;
|
||||||
*outlen = length;
|
|
||||||
|
|
||||||
pos = 0;
|
pos = 0;
|
||||||
while(inbuf+inlen > end && length > pos) {
|
while(inbuf+inlen > end && length > pos) {
|
||||||
b = strtoul(end, &end, 16);
|
b = strtoul(end, &end, 16);
|
||||||
if (errno || b > 0xff)
|
if (b > 0xff) {
|
||||||
goto err;
|
fprintf(stderr, "%s:%u Error decoding C-APDU\n", __FILE__, __LINE__);
|
||||||
|
goto noapdu;
|
||||||
|
}
|
||||||
|
|
||||||
(*outbuf)[pos++] = b;
|
(*outbuf)[pos++] = b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return length;
|
||||||
|
|
||||||
|
noapdu:
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t picc_encode_rapdu(unsigned char *inbuf, size_t inlen, char **outbuf)
|
||||||
|
{
|
||||||
|
char *p;
|
||||||
|
unsigned char *next;
|
||||||
|
size_t length;
|
||||||
|
|
||||||
|
if (!inbuf || inlen > 0xffff)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
length = inlen*3+5;
|
||||||
|
p = realloc(*outbuf, length);
|
||||||
|
if (!p) {
|
||||||
|
fprintf(stderr, "Error allocating memory for encoded R-APDU\n");
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
*outbuf = p;
|
||||||
|
|
||||||
|
sprintf(*outbuf, "%04X:", inlen);
|
||||||
|
|
||||||
|
next = inbuf;
|
||||||
|
/* let p point behind ':' */
|
||||||
|
p += 5;
|
||||||
|
while (inbuf+inlen > next) {
|
||||||
|
sprintf(p," %02X",*next);
|
||||||
|
next++;
|
||||||
|
p += 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
return length;
|
||||||
|
|
||||||
err:
|
err:
|
||||||
*outlen = 0;
|
return 0;
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void write_to_device(unsigned char *buffer, unsigned int msgLength) {
|
|
||||||
char* foo;
|
|
||||||
foo = (char*) malloc(msgLength*3+5);
|
|
||||||
//fprintf(fd, "%04X:", msgLength);
|
|
||||||
sprintf(foo,"%04X:",msgLength);
|
|
||||||
int i;
|
|
||||||
for(i=0; i<msgLength; i++) {
|
|
||||||
sprintf(&foo[3*i+5]," %02X",buffer[i]);
|
|
||||||
//fprintf(fd, " %02X", buffer[i]);
|
|
||||||
}
|
|
||||||
printf("%s",foo);
|
|
||||||
fprintf(fd,"%s", foo);
|
|
||||||
fprintf(fd, "\r\n");
|
|
||||||
fflush(fd);
|
|
||||||
free(foo);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main (int argc, char **argv)
|
int main (int argc, char **argv)
|
||||||
{
|
{
|
||||||
/*printf("%s:%d\n", __FILE__, __LINE__);*/
|
/*printf("%s:%d\n", __FILE__, __LINE__);*/
|
||||||
unsigned char *inputBuffer = NULL;
|
void *buf = NULL;
|
||||||
size_t inputLength;
|
size_t buflen;
|
||||||
|
|
||||||
BYTE outputBuffer[MAX_BUFFER_SIZE];
|
BYTE outputBuffer[MAX_BUFFER_SIZE];
|
||||||
DWORD outputLength;
|
DWORD outputLength;
|
||||||
|
|
||||||
LPSTR readers = NULL;
|
LONG r = SCARD_S_SUCCESS;
|
||||||
LONG r;
|
|
||||||
DWORD ctl, protocol;
|
DWORD ctl, protocol;
|
||||||
SCARDCONTEXT hContext;
|
|
||||||
SCARDHANDLE hCard;
|
|
||||||
|
|
||||||
char *read = NULL;
|
char *read = NULL;
|
||||||
size_t readlen = 0;
|
size_t readlen = 0;
|
||||||
ssize_t linelen;
|
ssize_t linelen;
|
||||||
|
|
||||||
unsigned int readernum = 0;
|
unsigned int readernum = 0;
|
||||||
|
int verbose = 0;
|
||||||
|
|
||||||
|
struct sigaction new_sig, old_sig;
|
||||||
|
|
||||||
|
|
||||||
if (argc > 1) {
|
if (argc > 1) {
|
||||||
|
readernum = strtoul(argv[1], NULL, 10);
|
||||||
if (argc > 2) {
|
if (argc > 2) {
|
||||||
|
if (0 != strcmp(argv[2], "verbose") || argc > 3) {
|
||||||
fprintf(stderr, "Usage: "
|
fprintf(stderr, "Usage: "
|
||||||
"%s [reader number] [PIN]\n", argv[0]);
|
"%s [reader number] [verbose]\n", argv[0]);
|
||||||
}
|
|
||||||
if (sscanf(argv[1], "%u", &readernum) != 1) {
|
|
||||||
fprintf(stderr, "Could not get number of reader\n");
|
|
||||||
exit(2);
|
exit(2);
|
||||||
}
|
}
|
||||||
|
verbose++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Register signal handlers */
|
/* Register signal handlers */
|
||||||
register_sig(SIGINT,checkSig);
|
new_sig.sa_handler = cleanup_exit;
|
||||||
|
sigemptyset(&new_sig.sa_mask);
|
||||||
|
new_sig.sa_flags = SA_RESTART;
|
||||||
|
if (sigaction(SIGINT, &new_sig, &old_sig) < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
|
||||||
/* Open the device */
|
/* Open the device */
|
||||||
fd = fopen(PICCDEV, "a+"); /*O_NOCTTY ?*/
|
picc_fd = fopen(PICCDEV, "a+"); /*O_NOCTTY ?*/
|
||||||
if (fd == NULL) {
|
if (!picc_fd) {
|
||||||
fprintf(stderr,"Failed to open %s. Exiting.\n", PICCDEV);
|
fprintf(stderr,"Error opening %s\n", PICCDEV);
|
||||||
exit(-1);
|
goto err;
|
||||||
} else
|
}
|
||||||
printf("Initialisation completed\n");
|
printf("Connected to %s\n", PICCDEV);
|
||||||
|
|
||||||
|
|
||||||
|
/* connect to reader and card */
|
||||||
r = pcsc_connect(readernum, SCARD_SHARE_EXCLUSIVE, SCARD_PROTOCOL_ANY,
|
r = pcsc_connect(readernum, SCARD_SHARE_EXCLUSIVE, SCARD_PROTOCOL_ANY,
|
||||||
&hContext, &readers, &hCard, &protocol);
|
&hContext, &readers, &hCard, &protocol);
|
||||||
if (r != SCARD_S_SUCCESS)
|
if (r != SCARD_S_SUCCESS)
|
||||||
goto err;
|
goto err;
|
||||||
|
|
||||||
|
|
||||||
while(1) {
|
while(1) {
|
||||||
linelen = getline(&read, &readlen, fd);
|
/* read C-APDU */
|
||||||
|
linelen = getline(&read, &readlen, picc_fd);
|
||||||
if (linelen < 0) {
|
if (linelen < 0) {
|
||||||
if (linelen < 0) {
|
if (linelen < 0) {
|
||||||
fprintf(stderr,"Error reading from %s\n", PICCDEV);
|
fprintf(stderr,"Error reading from %s\n", PICCDEV);
|
||||||
@@ -165,28 +183,45 @@ int main (int argc, char **argv)
|
|||||||
}
|
}
|
||||||
if (linelen == 0)
|
if (linelen == 0)
|
||||||
continue;
|
continue;
|
||||||
fflush(fd);
|
fflush(picc_fd);
|
||||||
|
|
||||||
|
if (verbose)
|
||||||
printf("%s\n", read);
|
printf("%s\n", read);
|
||||||
|
|
||||||
if (picc_decode(read, linelen, &inputBuffer, &inputLength) < 0)
|
|
||||||
|
/* decode C-APDU */
|
||||||
|
buflen = picc_decode_apdu(read, linelen, (unsigned char **) &buf);
|
||||||
|
if (!buflen)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
printb("C-APDU:\n", inputBuffer, inputLength);
|
if (!verbose)
|
||||||
|
printb("C-APDU: ===================================================\n", buf, buflen);
|
||||||
|
|
||||||
|
|
||||||
|
/* transmit APDU to card */
|
||||||
outputLength = MAX_BUFFER_SIZE;
|
outputLength = MAX_BUFFER_SIZE;
|
||||||
r = pcsc_transmit(protocol, hCard, inputBuffer, inputLength, outputBuffer, &outputLength);
|
r = pcsc_transmit(protocol, hCard, buf, buflen, outputBuffer, &outputLength);
|
||||||
if (r != SCARD_S_SUCCESS)
|
if (r != SCARD_S_SUCCESS)
|
||||||
goto err;
|
goto err;
|
||||||
|
|
||||||
|
if (!verbose)
|
||||||
printb("R-APDU:\n", outputBuffer, outputLength);
|
printb("R-APDU:\n", outputBuffer, outputLength);
|
||||||
|
|
||||||
write_to_device(outputBuffer, outputLength);
|
|
||||||
//fprintf(fd,"0002: 90 00\0");
|
/* encode R-APDU */
|
||||||
|
buflen = picc_encode_rapdu(outputBuffer, outputLength, (char **) &buf);
|
||||||
|
|
||||||
|
|
||||||
|
/* write R-APDU */
|
||||||
|
if (verbose)
|
||||||
|
printf("INF: Writing R-APDU\n\n%s\n\n", buf);
|
||||||
|
|
||||||
|
fprintf(picc_fd,"%s\r\n", buf);
|
||||||
|
fflush(picc_fd);
|
||||||
}
|
}
|
||||||
cleanup();
|
|
||||||
|
|
||||||
err:
|
err:
|
||||||
|
cleanup();
|
||||||
stringify_error(r);
|
stringify_error(r);
|
||||||
pcsc_disconnect(hContext, hCard, readers);
|
pcsc_disconnect(hContext, hCard, readers);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user