forked from frankmorgner/vsmartcard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcsc-relay.c
255 lines (210 loc) · 6.37 KB
/
pcsc-relay.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
* Copyright (C) 2009 by Dominik Oepen <[email protected]>.
* Copyright (C) 2010-2012 by Frank Morgner <[email protected]>.
*
* 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 <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "cmdline.h"
#include "pcsc-relay.h"
#ifndef MAX_BUFFER_SIZE
/** Maximum Tx/Rx Buffer for short APDU */
#define MAX_BUFFER_SIZE 261
#endif
#ifndef MAX_EXT_BUFFER_SIZE
/** Maximum Tx/Rx Buffer for extended APDU */
#define MAX_EXT_BUFFER_SIZE 65538
#endif
int verbose = 0;
static struct rf_driver *rfdriver = &driver_openpicc;
static driver_data_t *rfdriver_data = NULL;
static struct sc_driver *scdriver = &driver_pcsc;
static driver_data_t *scdriver_data = NULL;
static unsigned char *buf = NULL;
static size_t buflen = 0;
/* Forward declaration */
static void daemonize(void);
static void cleanup_exit(int signo);
static void cleanup(void);
#if HAVE_WORKING_FORK
void daemonize(void) {
pid_t pid, sid;
/* Fork and continue as child process */
pid = fork();
if (pid < 0) {
RELAY_ERROR("fork: %s\n", strerror(errno));
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) {
RELAY_ERROR("setsid: %s\n", strerror(errno));
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) {
RELAY_ERROR("chdir: %s\n", strerror(errno));
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);
}
#else
/* If fork() is not available, daemon mode will not be supported. I don't want
* to hassle with any workarounds. */
void daemonize(void)
{ RELAY_ERROR("Daemon mode currently not supported on your system.\n"); exit(1); }
#endif
void cleanup_exit(int signo){
cleanup();
exit(0);
}
void cleanup(void) {
rfdriver->disconnect(rfdriver_data);
rfdriver_data = NULL;
scdriver->disconnect(scdriver_data);
scdriver_data = NULL;
free(buf);
}
void
hexdump(const char *label, unsigned char *buf, size_t len)
{
size_t i = 0;
if (verbose >= LEVEL_NORMAL) {
printf("%s", label);
while (i < len) {
printf("%02X", buf[i]);
i++;
if (i%20)
printf(" ");
else if (i != len)
printf("\n");
}
printf("\n");
}
}
int main (int argc, char **argv)
{
/*printf("%s:%d\n", __FILE__, __LINE__);*/
unsigned char outputBuffer[MAX_EXT_BUFFER_SIZE];
size_t outputLength;
struct gengetopt_args_info args_info;
/* Parse command line */
if (cmdline_parser (argc, argv, &args_info) != 0)
exit(1) ;
switch (args_info.emulator_arg) {
case emulator_arg_openpicc:
rfdriver = &driver_openpicc;
break;
case emulator_arg_libnfc:
rfdriver = &driver_libnfc;
break;
case emulator_arg_vpcd:
rfdriver = &driver_vicc;
break;
default:
exit(2);
}
readernum = args_info.reader_arg;
switch (args_info.connector_arg) {
case connector_arg_vicc:
scdriver = &driver_vpcd;
break;
case connector_arg_pcsc:
scdriver = &driver_pcsc;
break;
default:
exit(2);
}
vpcdport = args_info.vpcd_port_arg;
if (args_info.vpcd_hostname_given)
vpcdhostname = args_info.vpcd_hostname_arg;
viccport = args_info.vicc_port_arg;
if (args_info.vicc_hostname_given)
vicchostname = args_info.vicc_hostname_arg;
verbose = args_info.verbose_given;
#if HAVE_SIGACTION
struct sigaction new_sig, old_sig;
/* 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)) {
RELAY_ERROR("sigaction: %s\n", strerror(errno));
goto err;
}
#endif
/* connect to reader and card */
if (!scdriver->connect(&scdriver_data))
goto err;
/* Open the device */
if (!rfdriver->connect(&rfdriver_data))
goto err;
if (!args_info.foreground_flag) {
INFO("Forking to background...\n");
verbose = -1;
daemonize();
}
cmdline_parser_free (&args_info);
while(1) {
/* get C-APDU */
if (!rfdriver->receive_capdu(rfdriver_data, &buf, &buflen)) {
do {
INFO("Trying to recover by reconnecting to emulator\n");
sleep(10);
} while (!rfdriver->connect(&rfdriver_data));
}
if (!buflen || !buf)
continue;
hexdump("C-APDU:\n", buf, buflen);
/* transmit APDU to card */
outputLength = sizeof outputBuffer;
if (!scdriver->transmit(scdriver_data, buf, buflen, outputBuffer,
&outputLength))
goto err;
/* send R-APDU */
hexdump("R-APDU:\n", outputBuffer, outputLength);
if (!rfdriver->send_rapdu(rfdriver_data, outputBuffer, outputLength)) {
do {
INFO("Trying to recover by reconnecting to emulator\n");
sleep(10);
} while (!rfdriver->connect(&rfdriver_data));
}
}
err:
cleanup();
exit(0);
}