forked from L1L1/cardpeek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lua_card.c
292 lines (247 loc) · 7.22 KB
/
lua_card.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**********************************************************************
*
* This file is part of Cardpeek, the smart card reader utility.
*
* Copyright 2009-2014 by Alain Pannetrat <[email protected]>
*
* Cardpeek 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.
*
* Cardpeek 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 Cardpeek. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <lauxlib.h>
#include "smartcard.h"
#include "lua_bytes.h"
#include "iso7816.h"
#include "misc.h"
#include "lua_ext.h"
#include "ui.h"
#include <string.h>
cardreader_t *READER=NULL;
void luax_set_card_reader(cardreader_t *r)
{
READER = r;
}
/**/
static int subr_card_connect(lua_State *L)
{
if(!cardreader_connect(READER,PROTOCOL_T0 | PROTOCOL_T1))
lua_pushboolean(L,0);
else
lua_pushboolean(L,1);
return 1;
}
static int subr_card_disconnect(lua_State *L)
{
cardreader_disconnect(READER);
lua_pushboolean(L,1);
return 1;
}
static int subr_card_warm_reset(lua_State *L)
{
if (cardreader_warm_reset(READER))
lua_pushboolean(L,1);
else
lua_pushboolean(L,0);
return 1;
}
static int subr_card_last_atr(lua_State *L)
{
const bytestring_t *atr = cardreader_last_atr(READER);
lua_push_bytestring(L,bytestring_duplicate(atr));
return 1;
}
static int subr_card_info(lua_State *L)
{
char **info=cardreader_get_info(READER);
int index;
lua_newtable(L);
for (index=0; info[index]!=NULL; index+=2)
{
lua_pushstring(L,info[index]);
lua_pushstring(L,info[index+1]);
lua_settable(L,-3);
g_free(info[index]);
g_free(info[index+1]);
}
g_free(info);
/* this update is needed since cardreader_get_info is silent upon success */
ui_update();
return 1;
}
static unsigned short luax_card_send(lua_State *L, const bytestring_t *command, bytestring_t *result)
{
bytestring_t* command_dup;
bytestring_t* get_response;
bytestring_t* tmp_response;
unsigned short SW;
unsigned char SW1, SW2;
apdu_descriptor_t ad;
char *tmp;
char *sw_string = NULL;
if (iso7816_describe_apdu(&ad,command)!=ISO7816_OK)
{
tmp = bytestring_to_format("%D",command);
log_printf(LOG_ERROR,"Could not parse APDU format: %s",tmp);
g_free(tmp);
return CARDPEEK_ERROR_SW;
}
tmp = bytestring_to_format("%D",command);
if (strlen(tmp)>37)
strcpy(tmp+32,"(...)");
log_printf(LOG_INFO,"send: %s [%s]", tmp, iso7816_stringify_apdu_class(ad.apdu_class));
g_free(tmp);
SW = cardreader_transmit(READER,command,result);
SW1 = (SW>>8)&0xFF;
SW2 = SW&0xFF;
luax_variable_call("card.stringify_sw","u>s",SW,&sw_string);
tmp = bytestring_to_format("%D",result);
if (strlen(tmp)>37)
strcpy(tmp+32,"(...)");
log_printf(LOG_INFO,"Recv: %04X %s [%s]", SW, tmp, sw_string);
g_free(tmp);
if (sw_string) g_free(sw_string);
if (SW1==0x6C) /* Re-issue with right length */
{
command_dup = bytestring_duplicate(command);
if (ad.le_len==3) /* in case of extended le */
bytestring_resize(command_dup,bytestring_get_size(command_dup)-2);
bytestring_set_element(command_dup,-1,SW2);
SW = luax_card_send(L,command_dup,result);
bytestring_free(command_dup);
}
else
{
while (SW1==0x61) /* use Get Response */
{
get_response = bytestring_new_from_string("8:00C0000000");
tmp_response = bytestring_new(8);
bytestring_set_element(get_response,4,SW2);
SW = luax_card_send(L,get_response,tmp_response);
bytestring_append_data(result,
bytestring_get_size(tmp_response),
bytestring_get_data(tmp_response));
bytestring_free(get_response);
bytestring_free(tmp_response);
SW1 = (SW>>8)&0xFF;
SW2 = SW&0xFF;
}
}
return SW;
}
static int subr_card_send(lua_State *L)
{
bytestring_t *command = luaL_check_bytestring(L,1);
bytestring_t *result = bytestring_new(8);
unsigned short SW = luax_card_send(L,command,result);
lua_pushinteger(L,SW);
lua_push_bytestring(L,result);
return 2;
}
static int subr_card_set_command_interval(lua_State *L)
{
unsigned interval=(unsigned)lua_tointeger(L,1);
cardreader_set_command_interval(READER,interval);
return 0;
}
static int subr_card_make_file_path(lua_State *L)
{
int path_type;
bytestring_t *file_path = bytestring_new(8);
const char *path;
if (lua_isnoneornil(L,1))
path = "";
else
path = lua_tostring(L,1);
if (iso7816_make_file_path(file_path,&path_type,path)!=ISO7816_OK)
{
bytestring_free(file_path);
lua_pushnil(L);
lua_pushnil(L);
log_printf(LOG_ERROR,"Could not parse card file path '%s'",path);
}
else
{
lua_push_bytestring(L,file_path);
lua_pushinteger(L,path_type);
}
return 2;
}
static int subr_card_log_save(lua_State *L)
{
const char *filename;
if (lua_isnoneornil(L,1))
return luaL_error(L,"Expecting one parameter: a filename (string)");
filename= lua_tostring(L,1);
if (cardreader_log_save(READER,filename)!=SMARTCARD_OK)
{
log_printf(LOG_ERROR,"Could not write data to '%s'",filename);
lua_pushboolean(L,0);
}
else
{
log_printf(LOG_INFO,"Wrote card data to '%s'",filename);
lua_pushboolean(L,1);
}
return 1;
}
static int subr_card_log_clear(lua_State *L)
{
UNUSED(L);
cardreader_log_clear(READER);
return 0;
}
static int subr_card_stringify_sw(lua_State *L)
{
unsigned short sw = (unsigned short)luaL_checkinteger (L,1);
const char *strval = iso7816_stringify_sw(sw);
lua_pushstring(L,strval);
return 1;
}
int luax_card_reset_values(lua_State *L)
{
lua_getglobal(L,"card");
if (lua_istable(L,-1)==0)
{
lua_pop(L,1);
return 0;
}
lua_pushstring(L, "CLA");
lua_pushinteger(L, 0);
lua_settable(L,-3);
lua_pushstring(L,"stringify_sw");
lua_pushcfunction(L,subr_card_stringify_sw);
lua_settable(L,-3);
lua_pop(L,1);
return 1;
}
static const struct luaL_Reg cardlib [] =
{
{"connect", subr_card_connect },
{"disconnect", subr_card_disconnect },
{"warm_reset", subr_card_warm_reset },
{"last_atr", subr_card_last_atr },
{"info", subr_card_info },
{"send", subr_card_send },
{"set_command_interval", subr_card_set_command_interval },
{"make_file_path", subr_card_make_file_path },
{"log_clear", subr_card_log_clear },
{"log_save", subr_card_log_save },
{"stringify_sw", subr_card_stringify_sw },
{NULL, NULL} /* sentinel */
};
int luaopen_card(lua_State *L)
{
luaL_newlib(L, cardlib);
lua_setglobal(L,"card");
return 1;
}