switched to autotools
git-svn-id: https://vsmartcard.svn.sourceforge.net/svnroot/vsmartcard@103 96b47cad-a561-4643-ad3b-153ac7d7599c
This commit is contained in:
27
virtualsmartcard/src/vpcd/Makefile.am
Normal file
27
virtualsmartcard/src/vpcd/Makefile.am
Normal file
@@ -0,0 +1,27 @@
|
||||
VPCD_LIB = libvpcd.$(DYN_LIB_EXT)
|
||||
|
||||
lib_LTLIBRARIES = libvpcd.la
|
||||
libvpcd_la_SOURCES = ifd.c vpcd.c
|
||||
libvpcd_la_LIBADD = $(PCSC_LIBS)
|
||||
libvpcd_la_CFLAGS = $(PCSC_CFLAGS)
|
||||
|
||||
EXTRA_DIST = reader.conf.in
|
||||
|
||||
install: install_libvpcd
|
||||
|
||||
install_libvpcd: libvpcd.la
|
||||
$(mkinstalldirs) $(DESTDIR)$(prefix)$(serialdropdir)
|
||||
cp .libs/$(VPCD_LIB) $(DESTDIR)$(prefix)$(serialdropdir)/$(VPCD_LIB).$(VERSION)
|
||||
ln -fs $(VPCD_LIB).$(VERSION) $(DESTDIR)$(prefix)$(serialdropdir)/$(VPCD_LIB)
|
||||
if [ -e $(DESTDIR)$(sysconfdir)/reader.conf ] ; \
|
||||
then \
|
||||
echo "Edit existing /etc/reader.conf" ; \
|
||||
else \
|
||||
$(mkinstalldirs) $(DESTDIR)$(sysconfdir) ; \
|
||||
perl -ne "s|TARGET|$(serialdropdir)/$(VPCD_LIB)| ; print" $(srcdir)/reader.conf.in > $(DESTDIR)$(sysconfdir)/reader.conf ; \
|
||||
fi
|
||||
|
||||
uninstall: uninstall_libvpcd
|
||||
|
||||
uninstall_libvpcd:
|
||||
rm -f $(DESTDIR)$(prefix)$(serialdropdir)/$(VPCD_LIB).$(VERSION)
|
||||
170
virtualsmartcard/src/vpcd/ifd.c
Normal file
170
virtualsmartcard/src/vpcd/ifd.c
Normal file
@@ -0,0 +1,170 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <ifdhandler.h>
|
||||
#include <debuglog.h>
|
||||
|
||||
#include "vpcd.h"
|
||||
|
||||
/*
|
||||
* List of Defined Functions Available to IFD_Handler 1.0
|
||||
*/
|
||||
RESPONSECODE
|
||||
IO_Create_Channel(DWORD channelid)
|
||||
{
|
||||
Log1(PCSC_LOG_DEBUG, "");
|
||||
if (vicc_init() < 0) return IFD_COMMUNICATION_ERROR;
|
||||
|
||||
return IFD_SUCCESS;
|
||||
}
|
||||
|
||||
RESPONSECODE
|
||||
IO_Close_Channel()
|
||||
{
|
||||
Log1(PCSC_LOG_DEBUG, "");
|
||||
RESPONSECODE r = IFD_Eject_ICC();
|
||||
if (vicc_exit() < 0) return IFD_COMMUNICATION_ERROR;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
RESPONSECODE
|
||||
IFD_Get_Capabilities(DWORD tag, PUCHAR value)
|
||||
{
|
||||
char *atr;
|
||||
int size;
|
||||
switch (tag) {
|
||||
case TAG_IFD_ATR:
|
||||
|
||||
size = vicc_getatr(&atr);
|
||||
if (size < 0) {
|
||||
Log1(PCSC_LOG_ERROR, "could not get ATR");
|
||||
return IFD_COMMUNICATION_ERROR;
|
||||
}
|
||||
Log2(PCSC_LOG_DEBUG, "Got ATR (%d bytes)", size);
|
||||
|
||||
memcpy(value, atr, size);
|
||||
free(atr);
|
||||
return IFD_SUCCESS;
|
||||
|
||||
case TAG_IFD_SLOTS_NUMBER:
|
||||
(*value) = 0;
|
||||
return IFD_SUCCESS;
|
||||
|
||||
//case TAG_IFD_POLLING_THREAD_KILLABLE:
|
||||
//return IFD_SUCCESS;
|
||||
|
||||
default:
|
||||
Log2(PCSC_LOG_DEBUG, "unknown tag %d", (int)tag);
|
||||
}
|
||||
|
||||
return IFD_ERROR_TAG;
|
||||
}
|
||||
|
||||
RESPONSECODE
|
||||
IFD_Set_Capabilities(DWORD tag, PUCHAR value)
|
||||
{
|
||||
return IFD_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
RESPONSECODE
|
||||
IFD_Set_Protocol_Parameters(DWORD protocoltype, UCHAR selectionflags, UCHAR
|
||||
pts1, UCHAR pts2, UCHAR pts3)
|
||||
{
|
||||
Log1(PCSC_LOG_DEBUG, "");
|
||||
return IFD_SUCCESS;
|
||||
}
|
||||
|
||||
RESPONSECODE
|
||||
IFD_Power_ICC(DWORD a)
|
||||
{
|
||||
switch (a) {
|
||||
case IFD_POWER_DOWN:
|
||||
if (vicc_poweroff() < 0) {
|
||||
Log1(PCSC_LOG_ERROR, "could not powerdown");
|
||||
return IFD_COMMUNICATION_ERROR;
|
||||
}
|
||||
break;
|
||||
case IFD_POWER_UP:
|
||||
if (vicc_poweron() < 0) {
|
||||
Log1(PCSC_LOG_ERROR, "could not powerup");
|
||||
return IFD_COMMUNICATION_ERROR;
|
||||
}
|
||||
break;
|
||||
case IFD_RESET:
|
||||
if (vicc_reset() < 0) {
|
||||
Log1(PCSC_LOG_ERROR, "could not reset");
|
||||
return IFD_COMMUNICATION_ERROR;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
Log2(PCSC_LOG_ERROR, "%d not supported", a);
|
||||
return IFD_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
return IFD_SUCCESS;
|
||||
}
|
||||
|
||||
RESPONSECODE
|
||||
IFD_Swallow_ICC()
|
||||
{
|
||||
Log1(PCSC_LOG_DEBUG, "");
|
||||
return IFD_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
RESPONSECODE
|
||||
IFD_Eject_ICC()
|
||||
{
|
||||
if (vicc_eject() < 0) {
|
||||
return IFD_COMMUNICATION_ERROR;
|
||||
}
|
||||
return IFD_SUCCESS;
|
||||
}
|
||||
|
||||
RESPONSECODE
|
||||
IFD_Confiscate_ICC()
|
||||
{
|
||||
Log1(PCSC_LOG_DEBUG, "");
|
||||
return IFD_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
RESPONSECODE
|
||||
IFD_Transmit_to_ICC(SCARD_IO_HEADER SendPci, PUCHAR TxBuffer, DWORD TxLength,
|
||||
PUCHAR RxBuffer, PDWORD RxLength, PSCARD_IO_HEADER RecvPci)
|
||||
{
|
||||
Log1(PCSC_LOG_DEBUG, "");
|
||||
(*RxLength) = 0;
|
||||
char *rapdu;
|
||||
|
||||
int size = vicc_transmit(TxLength, (char *) TxBuffer, &rapdu);
|
||||
if (size < 0) {
|
||||
Log1(PCSC_LOG_ERROR, "could not send apdu or receive rapdu");
|
||||
return IFD_COMMUNICATION_ERROR;
|
||||
}
|
||||
|
||||
(*RxLength) = size;
|
||||
RxBuffer = memcpy(RxBuffer, rapdu, size);
|
||||
free(rapdu);
|
||||
|
||||
return IFD_SUCCESS;
|
||||
}
|
||||
|
||||
RESPONSECODE
|
||||
IFD_Is_ICC_Present()
|
||||
{
|
||||
switch (vicc_present()) {
|
||||
case 0: return IFD_ICC_NOT_PRESENT;
|
||||
case 1: return IFD_ICC_PRESENT;
|
||||
default:
|
||||
Log1(PCSC_LOG_DEBUG, "Could not get ICC state");
|
||||
return IFD_COMMUNICATION_ERROR;
|
||||
}
|
||||
return IFD_COMMUNICATION_ERROR;
|
||||
}
|
||||
|
||||
RESPONSECODE
|
||||
IFD_Is_ICC_Absent()
|
||||
{
|
||||
return IFD_Is_ICC_Present();
|
||||
}
|
||||
4
virtualsmartcard/src/vpcd/reader.conf.in
Normal file
4
virtualsmartcard/src/vpcd/reader.conf.in
Normal file
@@ -0,0 +1,4 @@
|
||||
FRIENDLYNAME "Virtual PCD"
|
||||
DEVICENAME /dev/null
|
||||
LIBPATH TARGET
|
||||
CHANNELID 0
|
||||
221
virtualsmartcard/src/vpcd/vpcd.c
Normal file
221
virtualsmartcard/src/vpcd/vpcd.c
Normal file
@@ -0,0 +1,221 @@
|
||||
/*
|
||||
* Copyright (C) 2009 Frank Morgner
|
||||
*
|
||||
* This file is part of virtualsmartcard.
|
||||
*
|
||||
* virtualsmartcard 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.
|
||||
*
|
||||
* virtualsmartcard 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
|
||||
* virtualsmartcard. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <arpa/inet.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "vpcd.h"
|
||||
|
||||
static int server_sock = -1;
|
||||
static int client_sock = -1;
|
||||
|
||||
/*
|
||||
* Send all size bytes from buffer to sock
|
||||
*/
|
||||
static int sendall(int sock, size_t size, const char* buffer);
|
||||
/*
|
||||
* Receive size bytes from sock
|
||||
*/
|
||||
static char* recvall(int sock, size_t size);
|
||||
/*
|
||||
* Open a TCP socket and listen.
|
||||
*/
|
||||
static int opensock(unsigned short port);
|
||||
|
||||
|
||||
int sendall(int sock, size_t size, const char* buffer) {
|
||||
size_t sent = 0;
|
||||
int i;
|
||||
while (sent < size) {
|
||||
i = send(sock, buffer, size-sent, 0);
|
||||
if (i < 0) return i;
|
||||
sent += i;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
char* recvall(int sock, size_t size) {
|
||||
char* buffer = (char*) malloc(size);
|
||||
if (buffer == NULL) return NULL;
|
||||
|
||||
if (recv(sock, buffer, size, MSG_WAITALL) < size) {
|
||||
free(buffer);
|
||||
return NULL;
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
int opensock(unsigned short port)
|
||||
{
|
||||
int sock;
|
||||
|
||||
sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sock < 0) return -1;
|
||||
|
||||
struct sockaddr_in server_sockaddr;
|
||||
memset(&server_sockaddr, 0, sizeof(server_sockaddr));
|
||||
server_sockaddr.sin_family = PF_INET;
|
||||
server_sockaddr.sin_port = htons(VPCDPORT);
|
||||
server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
|
||||
if (bind(sock, (struct sockaddr*)&server_sockaddr,
|
||||
sizeof(server_sockaddr)) < 0) return -1;
|
||||
|
||||
if (listen(sock, 0) < 0) return -1;
|
||||
|
||||
return sock;
|
||||
}
|
||||
|
||||
int waitforclient(int server, long int secs, long int usecs) {
|
||||
int sock;
|
||||
|
||||
fd_set rfds;
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(server, &rfds);
|
||||
|
||||
/* Wait up to one microsecond. */
|
||||
struct timeval tv;
|
||||
tv.tv_sec = secs;
|
||||
tv.tv_usec = usecs;
|
||||
|
||||
if (select(server+1, &rfds, NULL, NULL, &tv) < 0) return -1;
|
||||
|
||||
if (FD_ISSET(server, &rfds)) {
|
||||
struct sockaddr_in client_sockaddr;
|
||||
socklen_t client_socklen = sizeof(client_sockaddr);
|
||||
sock = accept(server,
|
||||
(struct sockaddr*)&client_sockaddr,
|
||||
&client_socklen);
|
||||
}
|
||||
|
||||
return sock;
|
||||
}
|
||||
|
||||
int sendToVICC(uint16_t size, const char* buffer) {
|
||||
/* send size of message */
|
||||
uint16_t i = htons(size);
|
||||
i = sendall(client_sock, sizeof i, (char *) &i);
|
||||
if (i<0) {
|
||||
vicc_eject();
|
||||
return i;
|
||||
}
|
||||
/* send message */
|
||||
i = sendall(client_sock, size, buffer);
|
||||
if (i<0) {
|
||||
vicc_eject();
|
||||
return i;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Receive a message from icc
|
||||
*/
|
||||
int recvFromVICC(char** buffer) {
|
||||
/* receive size of message on LENLEN bytes */
|
||||
uint16_t *p = (uint16_t *) recvall(client_sock, sizeof *p);
|
||||
if (p == NULL) {
|
||||
vicc_eject();
|
||||
return -1;
|
||||
}
|
||||
uint16_t size = ntohs(*p);
|
||||
free(p);
|
||||
|
||||
/* receive message */
|
||||
*buffer = recvall(client_sock, size);
|
||||
if (*buffer == NULL) {
|
||||
vicc_eject();
|
||||
return -1;
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
int vicc_eject() {
|
||||
if (client_sock > 0) {
|
||||
client_sock = close(client_sock);
|
||||
if (client_sock < 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int vicc_init() {
|
||||
server_sock = opensock(VPCDPORT);
|
||||
if (server_sock < 0)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int vicc_exit() {
|
||||
if (server_sock > 0) {
|
||||
server_sock = close(server_sock);
|
||||
if (server_sock < 0) return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int vicc_transmit(int apdu_len, const char *apdu, char **rapdu) {
|
||||
if (sendToVICC(apdu_len, apdu) < 0) return -1;
|
||||
|
||||
return recvFromVICC(rapdu);
|
||||
}
|
||||
|
||||
int vicc_present() {
|
||||
if (client_sock > 0) return 1;
|
||||
else {
|
||||
/* Wait up to one microsecond. */
|
||||
client_sock = waitforclient(server_sock, 0, 1);
|
||||
if (client_sock < 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int vicc_getatr(char** atr) {
|
||||
char i = VPCD_CTRL_ATR;
|
||||
return vicc_transmit(VPCD_CTRL_LEN, &i, atr);
|
||||
}
|
||||
|
||||
int vicc_poweron() {
|
||||
char i = VPCD_CTRL_ON;
|
||||
return sendToVICC(VPCD_CTRL_LEN, &i);
|
||||
}
|
||||
|
||||
int vicc_poweroff() {
|
||||
char i = VPCD_CTRL_OFF;
|
||||
return sendToVICC(VPCD_CTRL_LEN, &i);
|
||||
}
|
||||
|
||||
int vicc_reset() {
|
||||
char i = VPCD_CTRL_RESET;
|
||||
return sendToVICC(VPCD_CTRL_LEN, &i);
|
||||
}
|
||||
61
virtualsmartcard/src/vpcd/vpcd.h
Normal file
61
virtualsmartcard/src/vpcd/vpcd.h
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright (C) 2009 Frank Morgner
|
||||
*
|
||||
* This file is part of virtualsmartcard.
|
||||
*
|
||||
* virtualsmartcard 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.
|
||||
*
|
||||
* virtualsmartcard 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
|
||||
* virtualsmartcard. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef _VPCD_H_
|
||||
#define _VPCD_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
#define VPCDPORT 35963
|
||||
|
||||
#define VPCD_CTRL_LEN 1
|
||||
|
||||
#define VPCD_CTRL_OFF 0
|
||||
#define VPCD_CTRL_ON 1
|
||||
#define VPCD_CTRL_RESET 2
|
||||
#define VPCD_CTRL_ATR 4
|
||||
|
||||
/*
|
||||
* First send length (unsigned integer in network byte order), then send the
|
||||
* message itself to the socket.
|
||||
*/
|
||||
int sendToVICC(uint16_t size, const char* buffer);
|
||||
/*
|
||||
* Receive a message from icc
|
||||
*/
|
||||
int recvFromVICC(char** buffer);
|
||||
|
||||
int vicc_eject();
|
||||
int vicc_init();
|
||||
int vicc_exit();
|
||||
int vicc_transmit(int apdu_len, const char *apdu, char **rapdu);
|
||||
int vicc_getatr(char** atr);
|
||||
int vicc_present();
|
||||
int vicc_poweron();
|
||||
int vicc_poweroff();
|
||||
int vicc_reset();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
Reference in New Issue
Block a user