-
Notifications
You must be signed in to change notification settings - Fork 2
/
cmd_sec.c
290 lines (216 loc) · 6.71 KB
/
cmd_sec.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
/*
* DESFire-Shell: Modify MIFARE DESFire Cards
*
* Copyright (C) 2015-2021 Mario Haustein
*
* This program 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.
*
* This program 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
* this program. If not, see https://www.gnu.org/licenses/.
*/
#include <stdlib.h>
#include <lua.h>
#include <lauxlib.h>
#include <freefare.h>
#include "cmd.h"
#include "debug.h"
#include "desflua.h"
#include "desfsh.h"
#include "fn.h"
#include "key.h"
static int cmd_auth(lua_State *l);
static int cmd_cks(lua_State *l);
static int cmd_gks(lua_State *l);
static int cmd_ck(lua_State *l);
static int cmd_gkv(lua_State *l);
FN_ALIAS(cmd_auth) = { "auth", "Authenticate", NULL };
FN_PARAM(cmd_auth) =
{
FNPARAM("kno", "Key Number", 0),
FNPARAM("key", "Authentication Key", 0),
FNPARAMEND
};
FN_RET(cmd_auth) =
{
FNPARAM("code", "Return Code", 0),
FNPARAM("err", "Error String", 0),
FNPARAMEND
};
FN("cmd", cmd_auth, "Authenticate to PICC",
"Establish authentication to PICC via key <kno> of the currently selected\n" \
"application. <key> is the shared secret key to use for authentication.\n" \
"See help(\"key\") for further details.\n");
static int cmd_auth(lua_State *l)
{
int result;
uint8_t keyno;
MifareDESFireKey k;
char *keystr;
luaL_argcheck(l, lua_isnumber(l, 1), 1, "key number expected");
result = key_get(l, 2, &k, &keystr); if(result) { desflua_argerror(l, 2, "key"); }
keyno = lua_tointeger(l, 1);
debug_cmd("Authenticate");
debug_gen(DEBUG_IN, "KNO", "%d", keyno);
debug_gen(DEBUG_IN, "KEY", "%s", keystr);
free(keystr);
result = mifare_desfire_authenticate(tag, keyno, k);
desflua_handle_result(l, result, tag);
mifare_desfire_key_free(k);
return lua_gettop(l);
}
FN_ALIAS(cmd_cks) = { "cks", "ChangeKeySettings", NULL };
FN_PARAM(cmd_cks) =
{
FNPARAM("settings", "New Key Settings", 0),
FNPARAMEND
};
FN_RET(cmd_cks) =
{
FNPARAM("code", "Return Code", 0),
FNPARAM("err", "Error String", 0),
FNPARAMEND
};
FN("cmd", cmd_cks, "Change Master Key Configuration",
"Changes the master key configuration of the currently selected application.\n" \
"The configuration must be given as an 8 bit number. See help(\"keysettings\")\n" \
"for further information.\n");
static int cmd_cks(lua_State *l)
{
int result;
uint8_t settings;
luaL_argcheck(l, lua_isnumber(l, 1), 1, "key settings must be a number");
settings = lua_tointeger(l, 1);
debug_cmd("ChangeKeySettings");
debug_keysettings(DEBUG_IN, settings);
result = mifare_desfire_change_key_settings(tag, settings);
desflua_handle_result(l, result, tag);
return lua_gettop(l);
}
FN_ALIAS(cmd_gks) = { "gks", "GetKeySettings", NULL };
FN_PARAM(cmd_gks) =
{
FNPARAMEND
};
FN_RET(cmd_gks) =
{
FNPARAM("code", "Return Code", 0),
FNPARAM("err", "Error String", 0),
FNPARAM("settings", "Key Settings", 1),
FNPARAMEND
};
FN("cmd", cmd_gks, "Get Master Key Configuration",
"Retrieves the master key configuration of the currently selected application.\n" \
"When successful, <settings> contains the binary coded master key\n" \
"configuration. In case of an error, <settings> will be 'nil'. For further\n" \
"information about master key configurations see help(\"keysettings\").\n");
static int cmd_gks(lua_State *l)
{
int result;
uint8_t settings, maxkeys;
debug_cmd("GetKeySettings");
result = mifare_desfire_get_key_settings(tag, &settings, &maxkeys);
desflua_handle_result(l, result, tag);
if(result < 0)
goto exit;
lua_checkstack(l, 2);
lua_pushinteger(l, settings);
lua_pushinteger(l, maxkeys);
debug_keysettings(DEBUG_OUT, settings);
debug_gen(DEBUG_OUT, "MAXKEYS", "%d", maxkeys);
exit:
return lua_gettop(l);
}
FN_ALIAS(cmd_ck) = { "ck", "ChangeKey", NULL };
FN_PARAM(cmd_ck) =
{
FNPARAM("kno", "Key number", 0),
FNPARAM("knew", "New Key", 0),
FNPARAM("kold", "Old Key", 1),
FNPARAMEND
};
FN_RET(cmd_ck) =
{
FNPARAM("code", "Return code", 0),
FNPARAM("err", "Error string", 0),
FNPARAMEND
};
FN("cmd", cmd_ck, "Change Key",
"Changes key <kno> of the currently selected application to <knew>. Depending\n" \
"on the master key settings, a prior authentification has to be achieved. The\n" \
"current value of <kno> has to specified via <kold> when authentication is\n" \
"required and the key change key differs from <kno>.\n");
static int cmd_ck(lua_State *l)
{
int result;
uint8_t keyno;
int oldidx;
MifareDESFireKey kold, knew;
char *knewstr, *koldstr;
luaL_argcheck(l, lua_isnumber(l, 1), 1, "key number expected");
result = key_get(l, 2, &knew, &knewstr); if(result) { desflua_argerror(l, 2, "new key"); }
oldidx = (lua_gettop(l) < 3 || lua_isnil(l, 3)) ? 2 : 3;
result = key_get(l, oldidx, &kold, &koldstr);
if(result)
{
mifare_desfire_key_free(knew);
free(knewstr);
desflua_argerror(l, 3, "old key");
}
keyno = lua_tointeger(l, 1);
debug_cmd("ChangeKey");
debug_gen(DEBUG_IN, "KNO", "%d", keyno);
debug_gen(DEBUG_IN, "KNEW", "%s", knewstr);
if(oldidx == 3)
debug_gen(DEBUG_IN, "KOLD", "%s", koldstr);
free(knewstr);
free(koldstr);
result = mifare_desfire_change_key(tag, keyno, knew, kold);
desflua_handle_result(l, result, tag);
mifare_desfire_key_free(kold);
mifare_desfire_key_free(knew);
return lua_gettop(l);
}
FN_ALIAS(cmd_gkv) = { "gkv", "GetKeyVersion", NULL };
FN_PARAM(cmd_gkv) =
{
FNPARAM("kno", "Key Number", 0),
FNPARAMEND
};
FN_RET(cmd_gkv) =
{
FNPARAM("code", "Return Code", 0),
FNPARAM("err", "Error String", 0),
FNPARAM("ver", "Key Version", 1),
FNPARAMEND
};
FN("cmd", cmd_gkv, "Get Key Version", \
"On success, the commands returns the version number of the key <kno> of the\n"
"currently selected application via the <ver> return value. In case of an\n"
"error, <ver> will be 'nil'.\n");
static int cmd_gkv(lua_State *l)
{
int result;
uint8_t keyno;
uint8_t ver;
luaL_argcheck(l, lua_isnumber(l, 1), 1, "key number expected");
keyno = lua_tointeger(l, 1);
debug_gen(DEBUG_IN, "KNO", "%d", keyno);
debug_cmd("GetKeyVersion");
result = mifare_desfire_get_key_version(tag, keyno, &ver);
desflua_handle_result(l, result, tag);
if(result < 0)
goto exit;
lua_checkstack(l, 1);
lua_pushinteger(l, ver);
debug_gen(DEBUG_OUT, "KVER", "%d", ver);
exit:
return lua_gettop(l);
}