completed renaming. fixed memory loss in openpicc driver. implemented libnfc driver.
git-svn-id: https://vsmartcard.svn.sourceforge.net/svnroot/vsmartcard@349 96b47cad-a561-4643-ad3b-153ac7d7599c
This commit is contained in:
10
pcsc-relay/src/Makefile.am
Normal file
10
pcsc-relay/src/Makefile.am
Normal file
@@ -0,0 +1,10 @@
|
||||
EXTRA_DIST = picc.py
|
||||
|
||||
bin_PROGRAMS = pcsc-relay
|
||||
bin_SCRIPTS = picc.py
|
||||
|
||||
pcsc_relay_SOURCES = pcsc-relay.c pcscutil.c opicc.c lnfc.c
|
||||
pcsc_relay_LDADD = $(PCSC_LIBS) $(LIBNFC_LIBS)
|
||||
pcsc_relay_CFLAGS = $(PCSC_CFLAGS) $(LIBNFC_CFLAGS)
|
||||
|
||||
noinst_HEADERS = pcscutil.h pcsc-relay.h
|
||||
166
pcsc-relay/src/lnfc.c
Normal file
166
pcsc-relay/src/lnfc.c
Normal file
@@ -0,0 +1,166 @@
|
||||
/*
|
||||
* Copyright (C) 2010 Frank Morgner
|
||||
*
|
||||
* This file is part of pcsc-relay.
|
||||
*
|
||||
* pcsc-relay is free software: you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, either version 3 of the License, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* pcsc-relay is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* pcsc-relay. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <nfc/nfc.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "pcsc-relay.h"
|
||||
|
||||
struct lnfc_data {
|
||||
/* PN53X only supports short APDUs */
|
||||
byte_t abtCapdu[4+1+0xff+1];
|
||||
size_t szCapduLen;
|
||||
nfc_device_t *pndTarget;
|
||||
};
|
||||
|
||||
|
||||
static int lnfc_connect(void **driver_data)
|
||||
{
|
||||
struct lnfc_data *data;
|
||||
nfc_target_t ntEmulatedTarget = {
|
||||
.nm.nmt = NMT_ISO14443A,
|
||||
.nm.nbr = NBR_106,
|
||||
};
|
||||
|
||||
if (!driver_data)
|
||||
return 0;
|
||||
|
||||
|
||||
data = realloc(*driver_data, sizeof *data);
|
||||
if (!data)
|
||||
return 0;
|
||||
memset(data, 0, sizeof *data);
|
||||
*driver_data = data;
|
||||
|
||||
/* FIXME
|
||||
* ntEmulatedTarget.nti.nai.abtUid
|
||||
* ntEmulatedTarget.nti.nai.abtAtqa
|
||||
* ntEmulatedTarget.nti.nai.btSak
|
||||
* ntEmulatedTarget.nti.nai.abtAts
|
||||
*/
|
||||
// We can only emulate a short UID, so fix length & ATQA bit:
|
||||
ntEmulatedTarget.nti.nai.szUidLen = 4;
|
||||
ntEmulatedTarget.nti.nai.abtAtqa[1] &= (0xFF-0x40);
|
||||
// First byte of UID is always automatically replaced by 0x08 in this mode anyway
|
||||
ntEmulatedTarget.nti.nai.abtUid[0] = 0x08;
|
||||
// ATS is always automatically replaced by PN532, we've no control on it:
|
||||
// ATS = (05) 75 33 92 03
|
||||
// (TL) T0 TA TB TC
|
||||
// | | | +-- CID supported, NAD supported
|
||||
// | | +----- FWI=9 SFGI=2 => FWT=154ms, SFGT=1.21ms
|
||||
// | +-------- DR=2,4 DS=2,4 => supports 106, 212 & 424bps in both directions
|
||||
// +----------- TA,TB,TC, FSCI=5 => FSC=64
|
||||
// It seems hazardous to tell we support NAD if the tag doesn't support NAD but I don't know how to disable it
|
||||
// PC/SC pseudo-ATR = 3B 80 80 01 01 if there is no historical bytes
|
||||
|
||||
// Creates ATS and copy max 48 bytes of Tk:
|
||||
/*
|
||||
byte_t * pbtTk;
|
||||
size_t szTk;
|
||||
pbtTk = iso14443a_locate_historical_bytes (ntEmulatedTarget.nti.nai.abtAts, ntEmulatedTarget.nti.nai.szAtsLen, &szTk);
|
||||
szTk = (szTk > 48) ? 48 : szTk;
|
||||
byte_t pbtTkt[48];
|
||||
memcpy(pbtTkt, pbtTk, szTk);
|
||||
ntEmulatedTarget.nti.nai.abtAts[0] = 0x75;
|
||||
ntEmulatedTarget.nti.nai.abtAts[1] = 0x33;
|
||||
ntEmulatedTarget.nti.nai.abtAts[2] = 0x92;
|
||||
ntEmulatedTarget.nti.nai.abtAts[3] = 0x03;
|
||||
ntEmulatedTarget.nti.nai.szAtsLen = 4 + szTk;
|
||||
memcpy(&(ntEmulatedTarget.nti.nai.abtAts[4]), pbtTkt, szTk);
|
||||
*/
|
||||
|
||||
// Try to open the NFC emulator device
|
||||
data->pndTarget = nfc_connect (NULL);
|
||||
if (data->pndTarget == NULL) {
|
||||
if (verbose || debug)
|
||||
fprintf (stderr, "Error connecting NFC emulator device\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (verbose)
|
||||
printf ("Connected to the NFC emulator device: %s\n", data->pndTarget->acName);
|
||||
|
||||
if (!nfc_target_init (data->pndTarget, &ntEmulatedTarget, data->abtCapdu, &data->szCapduLen)) {
|
||||
if (verbose || debug)
|
||||
fprintf (stderr, "%s", "Initialization of NFC emulator failed");
|
||||
nfc_disconnect (data->pndTarget);
|
||||
return 0;
|
||||
}
|
||||
if (debug)
|
||||
printf ("%s\n", "Done, relaying frames now!");
|
||||
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int lnfc_disconnect(void *driver_data)
|
||||
{
|
||||
struct lnfc_data *data = driver_data;
|
||||
|
||||
|
||||
nfc_disconnect (data->pndTarget);
|
||||
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int lnfc_receive_capdu(void *driver_data,
|
||||
unsigned char **capdu, size_t *len)
|
||||
{
|
||||
struct lnfc_data *data = driver_data;
|
||||
|
||||
if (!data || !capdu || !len)
|
||||
return 0;
|
||||
|
||||
|
||||
// Receive external reader command through target
|
||||
if (!nfc_target_receive_bytes(data->pndTarget, data->abtCapdu, &data->szCapduLen)) {
|
||||
if (verbose || debug)
|
||||
nfc_perror (data->pndTarget, "nfc_target_receive_bytes");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int lnfc_send_rapdu(void *driver_data,
|
||||
const unsigned char *rapdu, size_t len)
|
||||
{
|
||||
struct lnfc_data *data = driver_data;
|
||||
|
||||
if (!data || !rapdu)
|
||||
return 0;
|
||||
|
||||
|
||||
if (!nfc_target_send_bytes(data->pndTarget, rapdu, len))
|
||||
nfc_perror (data->pndTarget, "nfc_target_send_bytes");
|
||||
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
struct rf_driver driver_libnfc = {
|
||||
.data = NULL,
|
||||
.connect = lnfc_connect,
|
||||
.disconnect = lnfc_disconnect,
|
||||
.receive_capdu = lnfc_receive_capdu,
|
||||
.send_rapdu = lnfc_send_rapdu,
|
||||
};
|
||||
224
pcsc-relay/src/opicc.c
Normal file
224
pcsc-relay/src/opicc.c
Normal file
@@ -0,0 +1,224 @@
|
||||
/*
|
||||
* Copyright (C) 2010 Frank Morgner
|
||||
*
|
||||
* This file is part of pcsc-relay.
|
||||
*
|
||||
* pcsc-relay is free software: you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, either version 3 of the License, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* pcsc-relay is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* pcsc-relay. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "pcsc-relay.h"
|
||||
|
||||
struct picc_data {
|
||||
char *buf;
|
||||
size_t buflen;
|
||||
FILE *fd;
|
||||
};
|
||||
static size_t picc_encode_rapdu(const unsigned char *inbuf, size_t inlen, char **outbuf);
|
||||
static size_t picc_decode_apdu(const char *inbuf, size_t inlen, unsigned char **outbuf);
|
||||
|
||||
|
||||
size_t picc_encode_rapdu(const unsigned char *inbuf, size_t inlen, char **outbuf)
|
||||
{
|
||||
char *p;
|
||||
const unsigned char *next;
|
||||
size_t length;
|
||||
|
||||
if (!inbuf || inlen > 0xffff || !outbuf)
|
||||
goto err;
|
||||
|
||||
length = 5+inlen*3+1;
|
||||
p = realloc(*outbuf, length);
|
||||
if (!p) {
|
||||
if (debug || verbose)
|
||||
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:
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t picc_decode_apdu(const char *inbuf, size_t inlen, unsigned char **outbuf)
|
||||
{
|
||||
size_t pos, length;
|
||||
unsigned char buf[0xffff];
|
||||
char *end, *p;
|
||||
unsigned long int b;
|
||||
|
||||
if (!outbuf || inbuf == NULL || inlen == 0 || inbuf[0] == '\0') {
|
||||
/* Ignore invalid parameters, empty and 'RESET' lines */
|
||||
goto noapdu;
|
||||
}
|
||||
|
||||
length = strtoul(inbuf, &end, 16);
|
||||
|
||||
/* check for ':' right behind the length */
|
||||
if (inbuf+inlen < end+1 || end[0] != ':')
|
||||
goto noapdu;
|
||||
end++;
|
||||
|
||||
p = realloc(*outbuf, length);
|
||||
if (!p) {
|
||||
if (debug || verbose)
|
||||
fprintf(stderr, "Error allocating memory for decoded C-APDU\n");
|
||||
goto noapdu;
|
||||
}
|
||||
*outbuf = p;
|
||||
|
||||
pos = 0;
|
||||
while(inbuf+inlen > end && length > pos) {
|
||||
b = strtoul(end, &end, 16);
|
||||
if (b > 0xff) {
|
||||
if (debug || verbose)
|
||||
fprintf(stderr, "%s:%u Error decoding C-APDU\n", __FILE__, __LINE__);
|
||||
goto noapdu;
|
||||
}
|
||||
|
||||
(*outbuf)[pos++] = b;
|
||||
}
|
||||
|
||||
return length;
|
||||
|
||||
noapdu:
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int picc_connect(void **driver_data)
|
||||
{
|
||||
struct picc_data *data;
|
||||
|
||||
if (!driver_data)
|
||||
return 0;
|
||||
|
||||
data = realloc(*driver_data, sizeof *data);
|
||||
if (!data)
|
||||
return 0;
|
||||
memset(data, 0, sizeof *data);
|
||||
*driver_data = data;
|
||||
|
||||
data->fd = fopen(PICCDEV, "a+"); /*O_NOCTTY ?*/
|
||||
if (!data->fd) {
|
||||
if (debug || verbose)
|
||||
fprintf(stderr,"Error opening %s\n", PICCDEV);
|
||||
return 0;
|
||||
}
|
||||
if (debug || verbose)
|
||||
printf("Connected to %s\n", PICCDEV);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int picc_disconnect(void *driver_data)
|
||||
{
|
||||
struct picc_data *data = driver_data;
|
||||
if (data) {
|
||||
if (data->fd)
|
||||
fclose(data->fd);
|
||||
if (data->buf)
|
||||
free(data->buf);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int picc_receive_capdu(void *driver_data,
|
||||
unsigned char **capdu, size_t *len)
|
||||
{
|
||||
ssize_t linelen;
|
||||
struct picc_data *data = driver_data;
|
||||
|
||||
if (!data || !capdu || !len)
|
||||
return 0;
|
||||
|
||||
|
||||
/* read C-APDU */
|
||||
linelen = getline(&data->buf, &data->buflen, data->fd);
|
||||
if (linelen < 0) {
|
||||
if (linelen < 0) {
|
||||
if (debug || verbose)
|
||||
fprintf(stderr,"Error reading from %s\n", PICCDEV);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (linelen == 0) {
|
||||
*len = 0;
|
||||
return 1;
|
||||
}
|
||||
fflush(data->fd);
|
||||
|
||||
if (debug)
|
||||
printf("%s\n", data->buf);
|
||||
|
||||
|
||||
/* decode C-APDU */
|
||||
*len = picc_decode_apdu(data->buf, linelen, capdu);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int picc_send_rapdu(void *driver_data,
|
||||
const unsigned char *rapdu, size_t len)
|
||||
{
|
||||
size_t buflen;
|
||||
struct picc_data *data = driver_data;
|
||||
|
||||
if (!data || !rapdu)
|
||||
return 0;
|
||||
|
||||
|
||||
/* encode R-APDU */
|
||||
buflen = picc_encode_rapdu(rapdu, len, &data->buf);
|
||||
if (buflen > data->buflen)
|
||||
data->buflen = buflen;
|
||||
|
||||
|
||||
/* write R-APDU */
|
||||
if (debug)
|
||||
printf("INF: Writing R-APDU\n\n%s\n\n", data->buf);
|
||||
|
||||
if (fprintf(data->fd,"%s\r\n", (char *) data->buf) < 0
|
||||
|| fflush(data->fd) != 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
struct rf_driver driver_openpicc = {
|
||||
.data = NULL,
|
||||
.connect = picc_connect,
|
||||
.disconnect = picc_disconnect,
|
||||
.receive_capdu = picc_receive_capdu,
|
||||
.send_rapdu = picc_send_rapdu,
|
||||
};
|
||||
177
pcsc-relay/src/pcsc-relay.c
Normal file
177
pcsc-relay/src/pcsc-relay.c
Normal file
@@ -0,0 +1,177 @@
|
||||
/*
|
||||
* Copyright (C) 2010 Dominik Oepen, Frank Morgner
|
||||
*
|
||||
* This file is part of pcsc-relay.
|
||||
*
|
||||
* pcsc-relay is free software: you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, either version 3 of the License, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* pcsc-relay is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* pcsc-relay. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <winscard.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "pcsc-relay.h"
|
||||
#include "pcscutil.h"
|
||||
|
||||
struct rf_driver *driver = &driver_openpicc;
|
||||
|
||||
|
||||
static LPSTR readers = NULL;
|
||||
static SCARDCONTEXT hContext = 0;
|
||||
static SCARDHANDLE hCard = 0;
|
||||
int verbose = 0, debug = 0;
|
||||
|
||||
/* Forward declaration */
|
||||
static void daemonize();
|
||||
static void cleanup_exit(int signo);
|
||||
static void cleanup(void);
|
||||
|
||||
void daemonize() {
|
||||
pid_t pid, sid;
|
||||
|
||||
/*Fork and continue as child process*/
|
||||
pid = fork();
|
||||
if (pid < 0)
|
||||
goto err;
|
||||
if (pid > 0) /* Exit the parent */
|
||||
exit(0);
|
||||
|
||||
umask(0);
|
||||
|
||||
/* Create new session and set the process group ID */
|
||||
sid = setsid();
|
||||
if (sid < 0)
|
||||
goto err;
|
||||
|
||||
/* Change the current working directory. This prevents the current
|
||||
directory from being locked; hence not being able to remove it. */
|
||||
if (chdir("/") < 0)
|
||||
goto err;
|
||||
|
||||
/* Redirect standard files to /dev/null */
|
||||
close(STDIN_FILENO);
|
||||
close(STDOUT_FILENO);
|
||||
close(STDERR_FILENO);
|
||||
|
||||
return;
|
||||
|
||||
err:
|
||||
fprintf(stderr, "Failed to start the daemon. Exiting.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void cleanup_exit(int signo){
|
||||
cleanup();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void cleanup(void) {
|
||||
driver->disconnect(driver->data);
|
||||
pcsc_disconnect(hContext, hCard, readers);
|
||||
}
|
||||
|
||||
int main (int argc, char **argv)
|
||||
{
|
||||
/*printf("%s:%d\n", __FILE__, __LINE__);*/
|
||||
void *buf = NULL;
|
||||
size_t buflen;
|
||||
|
||||
BYTE outputBuffer[MAX_BUFFER_SIZE];
|
||||
DWORD outputLength;
|
||||
|
||||
LONG r = SCARD_S_SUCCESS;
|
||||
DWORD ctl, protocol;
|
||||
|
||||
char *read = NULL;
|
||||
size_t readlen = 0;
|
||||
|
||||
unsigned int readernum = 0;
|
||||
|
||||
struct sigaction new_sig, old_sig;
|
||||
|
||||
|
||||
if (argc > 1) {
|
||||
readernum = strtoul(argv[1], NULL, 10);
|
||||
if (argc > 2) {
|
||||
if (0 == strcmp(argv[2], "verbose"))
|
||||
verbose++;
|
||||
else if (0 == strcmp(argv[2], "debug"))
|
||||
debug++;
|
||||
else
|
||||
goto parse_err;
|
||||
if (argc > 3) {
|
||||
parse_err:
|
||||
fprintf(stderr, "Usage: "
|
||||
"%s [reader number] [verbose | debug]\n", argv[0]);
|
||||
exit(2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!verbose && !debug)
|
||||
daemonize();
|
||||
|
||||
/* Register signal handlers */
|
||||
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)
|
||||
|| (sigaction(SIGTERM, &new_sig, &old_sig) < 0))
|
||||
goto err;
|
||||
|
||||
|
||||
/* Open the device */
|
||||
driver->connect(&driver->data);
|
||||
|
||||
/* connect to reader and card */
|
||||
r = pcsc_connect(readernum, SCARD_SHARE_EXCLUSIVE, SCARD_PROTOCOL_ANY,
|
||||
&hContext, &readers, &hCard, &protocol);
|
||||
if (r != SCARD_S_SUCCESS)
|
||||
goto err;
|
||||
|
||||
|
||||
while(1) {
|
||||
driver->receive_capdu(driver->data, (unsigned char **) &buf, &buflen);
|
||||
|
||||
if (!verbose)
|
||||
printb("C-APDU: ===================================================\n", buf, buflen);
|
||||
|
||||
|
||||
/* transmit APDU to card */
|
||||
outputLength = MAX_BUFFER_SIZE;
|
||||
r = pcsc_transmit(protocol, hCard, buf, buflen, outputBuffer, &outputLength);
|
||||
if (r != SCARD_S_SUCCESS)
|
||||
goto err;
|
||||
|
||||
if (!verbose)
|
||||
printb("R-APDU:\n", outputBuffer, outputLength);
|
||||
|
||||
|
||||
driver->send_rapdu(driver->data, outputBuffer, outputLength);
|
||||
}
|
||||
|
||||
err:
|
||||
cleanup();
|
||||
|
||||
exit(r == SCARD_S_SUCCESS ? 0 : 1);
|
||||
}
|
||||
49
pcsc-relay/src/pcsc-relay.h
Normal file
49
pcsc-relay/src/pcsc-relay.h
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (C) 2010 Frank Morgner
|
||||
*
|
||||
* This file is part of pcsc-relay.
|
||||
*
|
||||
* pcsc-relay is free software: you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, either version 3 of the License, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* pcsc-relay is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* pcsc-relay. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/**
|
||||
* @file
|
||||
*/
|
||||
#ifndef _PCSC_RELAY_H
|
||||
#define _PCSC_RELAY_H
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct rf_driver {
|
||||
void *data;
|
||||
int (*connect) (void **driver_data);
|
||||
int (*disconnect) (void *driver_data);
|
||||
int (*receive_capdu) (void *driver_data,
|
||||
unsigned char **capdu, size_t *len);
|
||||
int (*send_rapdu) (void *driver_data,
|
||||
const unsigned char *rapdu, size_t len);
|
||||
};
|
||||
|
||||
extern int verbose, debug;
|
||||
|
||||
extern struct rf_driver driver_openpicc;
|
||||
extern struct rf_driver driver_libnfc;
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
1
pcsc-relay/src/pcscutil.c
Symbolic link
1
pcsc-relay/src/pcscutil.c
Symbolic link
@@ -0,0 +1 @@
|
||||
../../ccid/src/pcscutil.c
|
||||
1
pcsc-relay/src/pcscutil.h
Symbolic link
1
pcsc-relay/src/pcscutil.h
Symbolic link
@@ -0,0 +1 @@
|
||||
../../ccid/src/pcscutil.h
|
||||
153
pcsc-relay/src/picc.py
Normal file
153
pcsc-relay/src/picc.py
Normal file
@@ -0,0 +1,153 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
"""
|
||||
* Copyright (C) 2009 Dominik Oepen
|
||||
*
|
||||
* This file is part of picc.
|
||||
*
|
||||
* picc is free software: you can redistribute it and/or modify it under the
|
||||
* terms of the GNU General Public License as published by the Free Software
|
||||
* Foundation, either version 3 of the License, or (at your option) any later
|
||||
* version.
|
||||
*
|
||||
* picc is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
* details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* picc. If not, see <http://www.gnu.org/licenses/>.
|
||||
"""
|
||||
|
||||
import sys
|
||||
import smartcard.util
|
||||
from optparse import OptionParser
|
||||
from smartcard.System import readers
|
||||
from struct import pack
|
||||
|
||||
def setup(readernumber):
|
||||
"""Open up a connection to the specified reader via PCSC
|
||||
"""
|
||||
|
||||
r = readers()
|
||||
connection = r[readernumber].createConnection()
|
||||
connection.connect()
|
||||
return connection
|
||||
|
||||
def read_from_picc(fd):
|
||||
"""Read a line from the terminal, parse it and return a string
|
||||
representation
|
||||
"""
|
||||
|
||||
line = fd.readline()
|
||||
#Parse line
|
||||
if (line[0] != '0'):
|
||||
print(line)
|
||||
return None
|
||||
else:
|
||||
idx = line.find(':')
|
||||
if (idx == -1):
|
||||
print "Failed to parse string"
|
||||
return None
|
||||
length = int(line[:idx])
|
||||
apdu = line[idx+2:]
|
||||
apdu = apdu.replace(' ','')
|
||||
#if (len(apdu) != length*2+1):
|
||||
# print "Encountered corrupted APDU"
|
||||
# return None
|
||||
return apdu
|
||||
|
||||
def transmit_to_pcsc(line, connection):
|
||||
"""Write a line (including the string representation of an APDU)
|
||||
to PCSC. Return the response from PCSC or NULL in case of error
|
||||
"""
|
||||
|
||||
#Convert string to tuple of Bytes
|
||||
try:
|
||||
apdu_bytes = smartcard.util.toBytes(line)
|
||||
except TypeError, e:
|
||||
print "Failed to transmit APDU to PCSC: " + e
|
||||
return #FIXME
|
||||
|
||||
rapdu = connection.transmit( SELECT + DF_TELECOM )
|
||||
#print "%x %x" % (sw1, sw2)
|
||||
|
||||
rv = smartcard.util.toHexString(rapdu, PACK)
|
||||
|
||||
return rv
|
||||
|
||||
def write_to_picc(line,fd):
|
||||
"""Read a line and format it for the OpenPICC. Then write it
|
||||
to the terminal
|
||||
"""
|
||||
|
||||
#fd.write("0002: 90 00\r\n")
|
||||
length = len(line)
|
||||
length /= 2
|
||||
output_string = hex(length)[2:]
|
||||
output_string += ":"
|
||||
|
||||
for (i in range[0, len(line), 2])
|
||||
output_string += " " + line[i:i+2]
|
||||
|
||||
output_string += "\r\n"
|
||||
fd.write(output_string)
|
||||
#fd.flush()
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
#Parse command line arguments
|
||||
parser = OptionParser()
|
||||
parser.add_option("-t", "--tty", action="store", dest="ttyno",
|
||||
type="string", default='0',
|
||||
help="Number of ttyACM the OpenPICC is connected to [default: %default]")
|
||||
parser.add_option("-r", "--reader", action="store", dest="readerno",
|
||||
type="string", default='0',
|
||||
help="Number of ttyACM the OpenPICC is connected to [default: %default]")
|
||||
(options, args) = parser.parse_args()
|
||||
if (options.ttyno.isdigit()):
|
||||
nr = options.ttyno
|
||||
else:
|
||||
print "Invalid tty number %s. Using default tty (/dev/ttyACM0)" % options.ttyno
|
||||
nr = '0'
|
||||
if (options.readerno.isdigit()):
|
||||
readerno = int(options.readerno)
|
||||
else:
|
||||
print "Invalid reader number %s. Using default reader (0)"
|
||||
readerno = 0
|
||||
ttyPath = "/dev/ttyACM" + nr
|
||||
|
||||
#Connect to Virtual PCD via PC/SC
|
||||
try:
|
||||
connection = setup(readerno)
|
||||
except:
|
||||
print "PC/SC Error"
|
||||
sys.exit()
|
||||
|
||||
#Open terminal
|
||||
try:
|
||||
fd = open(ttyPath,'a+')
|
||||
except IOError:
|
||||
print "Failed to open %s" % ttyPath
|
||||
sys.exit()
|
||||
|
||||
#Actual processing
|
||||
while (True):
|
||||
|
||||
try:
|
||||
apdu = read_from_picc(fd)
|
||||
except IOError:
|
||||
print "Failed to read from %s" % ttyPath
|
||||
sys.exit()
|
||||
if (apdu == None):
|
||||
continue
|
||||
else:
|
||||
print("Got APDU: %s" %apdu)
|
||||
|
||||
response = transmit_to_pcsc(apdu)
|
||||
|
||||
try:
|
||||
write_to_picc(response,fd)
|
||||
except IOError:
|
||||
print "Failed to write to %s" % ttyPath
|
||||
sys.exit()
|
||||
Reference in New Issue
Block a user