]>
Commit | Line | Data |
---|---|---|
452aab1e | 1 | local cmds = require('commands') |
2 | local getopt = require('getopt') | |
3 | local lib14b = require('read14b') | |
4 | local utils = require('utils') | |
5 | ||
6 | example = "script runs 14b raw commands to query a CAPLYPSO tag" | |
7 | author = "Iceman, 2016" | |
8 | desc = | |
9 | [[ | |
10 | This is a script to communicate with a CALYSPO / 14443b tag using the '14b raw' commands | |
11 | ||
12 | Arguments: | |
13 | -b 123 | |
14 | Examples : | |
15 | script run f -b 11223344 | |
16 | script run f | |
17 | ||
18 | Examples : | |
19 | ||
20 | # 1. Connect and don't disconnect | |
21 | script run f | |
22 | # 2. Send mf auth, read response | |
23 | script run f | |
24 | # 3. disconnect | |
25 | script run f | |
26 | ||
27 | ]] | |
28 | ||
29 | --[[ | |
30 | This script communicates with /armsrc/iso14443b.c, | |
31 | Check there for details about data format and how commands are interpreted on the | |
32 | device-side. | |
33 | ]] | |
34 | ||
35 | --- | |
36 | -- | |
37 | local function calypso_switch_on_field() | |
38 | local flags = lib14b.ISO14B_COMMAND.ISO14B_CONNECT | |
39 | local c = Command:new{cmd = cmds.CMD_ISO_14443B_COMMAND, arg1 = flags} | |
40 | return lib14b.sendToDevice(c, true) | |
41 | end | |
42 | --- | |
43 | -- Disconnect (poweroff) the antenna forcing a disconnect of a 14b tag. | |
44 | local function calypso_switch_off_field() | |
45 | local flags = lib14b.ISO14B_COMMAND.ISO14B_DISCONNECT | |
46 | local c = Command:new{cmd = cmds.CMD_ISO_14443B_COMMAND, arg1 = flags} | |
47 | return lib14b.sendToDevice(c, true) | |
48 | end | |
49 | ||
50 | local function calypso_parse(result) | |
51 | local r = Command.parse(result) | |
52 | local len = r.arg2 * 2 | |
53 | r.data = string.sub(r.data, 0, len); | |
54 | if r.arg1 == 0 then | |
55 | return r, nil | |
56 | end | |
57 | return nil,nil | |
58 | end | |
59 | --- | |
60 | -- A debug printout-function | |
61 | local function dbg(args) | |
62 | if DEBUG then | |
63 | print("###", args) | |
64 | end | |
65 | end | |
66 | --- | |
67 | -- This is only meant to be used when errors occur | |
68 | local function oops(err) | |
69 | print("ERROR: ",err) | |
70 | calypso_switch_off_field() | |
71 | return nil,err | |
72 | end | |
73 | --- | |
74 | -- Usage help | |
75 | local function help() | |
76 | print(desc) | |
77 | print("Example usage") | |
78 | print(example) | |
79 | end | |
80 | -- | |
81 | -- helper function, give current count of items in lua-table. | |
82 | local function tablelen(T) | |
83 | local count = 0 | |
84 | for _ in pairs(T) do count = count + 1 end | |
85 | return count | |
86 | end | |
87 | --- | |
88 | -- helper function, gives a sorted table from table t, | |
89 | -- order can be a seperate sorting-order function. | |
90 | local function spairs(t, order) | |
91 | -- collect the keys | |
92 | local keys = {} | |
93 | for k in pairs(t) do keys[#keys+1] = k end | |
94 | ||
95 | -- if order function given, sort by it by passing the table and keys a, b, | |
96 | -- otherwise just sort the keys | |
97 | if order then | |
98 | table.sort(keys, function(a,b) return order(t, a, b) end) | |
99 | else | |
100 | table.sort(keys) | |
101 | end | |
102 | ||
103 | -- return the iterator function | |
104 | local i = 0 | |
105 | return function() | |
106 | i = i + 1 | |
107 | if keys[i] then | |
108 | return keys[i], t[keys[i]] | |
109 | end | |
110 | end | |
111 | end | |
112 | --- | |
113 | -- Sends a usbpackage , "hf 14b raw" | |
114 | -- if it reads the response, it converts it to a lua object "Command" first and the Data is cut to correct length. | |
115 | local function calypso_send_cmd_raw(data, ignoreresponse ) | |
116 | ||
117 | local command, flags, result, err | |
118 | flags = lib14b.ISO14B_COMMAND.ISO14B_RAW + | |
119 | lib14b.ISO14B_COMMAND.ISO14B_APPEND_CRC | |
120 | ||
121 | data = data or "00" | |
122 | ||
123 | command = Command:new{cmd = cmds.CMD_ISO_14443B_COMMAND, | |
124 | arg1 = flags, | |
125 | arg2 = #data/2, -- LEN of data, half the length of the ASCII-string hex string | |
126 | arg3 = 0, | |
127 | data = data} -- data bytes (commands etc) | |
128 | result, err = lib14b.sendToDevice(command, false) | |
129 | ||
130 | if ignoreresponse then return response, err end | |
131 | ||
132 | if result then | |
133 | local r = calypso_parse(result) | |
134 | return r, nil | |
135 | end | |
136 | return respone, err | |
137 | end | |
138 | --- | |
139 | -- calypso_card_num : Reads card number from ATR and | |
140 | -- writes it in the tree in decimal format. | |
141 | local function calypso_card_num(card) | |
142 | if not card then return end | |
143 | local card_num = tonumber( card.uid:sub(1,8),16 ) | |
144 | print('Card UID', card.uid) | |
145 | print('Card Number', card_num) | |
146 | end | |
147 | --- | |
148 | -- analyse CALYPSO apdu status bytes. | |
149 | local function calypso_apdu_status(apdu) | |
150 | -- last two is CRC | |
151 | -- next two is APDU status bytes. | |
4bf0f73d | 152 | local status = false |
153 | local mess = 'FAIL' | |
452aab1e | 154 | local sw = apdu:sub( #apdu-7, #apdu-4) |
4bf0f73d | 155 | if sw == '9000' then |
156 | mess = 'OK' | |
157 | status = true | |
158 | end | |
159 | print ('SW', sw, mess ) | |
160 | return status | |
452aab1e | 161 | end |
162 | ||
163 | local _calypso_cmds = { | |
4bf0f73d | 164 | ["01.Select ICC file"] = '02 94 a4 08 00 04 3f 00 00 02', |
ab6d8a4c | 165 | ["02.ICC"] = '02 94 b2 01 041d', |
166 | ["03.Select EnvHol file"] = '02 94 a4 08 00 04 20 00 20 01', | |
167 | ["04.EnvHol1"] = '02 94 b2 01 041d', | |
168 | ["05.Select EvLog file"] = '02 94 a4 08 00 04 20 00 20 10', | |
169 | ["06.EvLog1"] = '02 94 b2 01 041d', | |
170 | ["07.EvLog2"] = '02 94 b2 02 041d', | |
171 | ["08.EvLog3"] = '02 94 b2 03 041d', | |
172 | ["09.Select ConList file"] ='02 94 a4 0800 04 2000 2050', | |
173 | ["10.ConList"] = '02 94 b2 01 041d', | |
174 | ["11.Select Contra file"] = '02 94 a4 0800 04 2000 2020', | |
175 | ["12.Contra1"] = '02 94 b2 01 041d', | |
176 | ["13.Contra2"] = '02 94 b2 02 041d', | |
177 | ["14.Contra3"] = '02 94 b2 03 041d', | |
178 | ["15.Contra4"] = '02 94 b2 04 041d', | |
179 | ["16.Select Counter file"]= '02 94 a4 0800 04 2000 2069', | |
180 | ["17.Counter"] = '02 94 b2 01 041d', | |
181 | ["18.Select SpecEv file"]= '02 94 a4 08 0004 2000 2040', | |
182 | ["19.SpecEv1"] = '02 94 b2 01 041d', | |
452aab1e | 183 | } |
184 | ||
185 | --- | |
186 | -- The main entry point | |
187 | function main(args) | |
188 | ||
189 | local data, apdu, flags, uid, cid, result, err, card | |
190 | -- Read the parameters | |
191 | for o, a in getopt.getopt(args, 'h') do | |
192 | if o == "h" then return help() end | |
193 | end | |
194 | ||
195 | calypso_switch_on_field() | |
196 | ||
197 | -- Select 14b tag. | |
198 | card, err = lib14b.waitFor14443b() | |
199 | if not card then return oops(err) end | |
200 | ||
201 | calypso_card_num(card) | |
202 | cid = card.cid | |
203 | ||
204 | --[[ | |
205 | NAME VALUE APDU_POS | |
206 | PCB 0x0A 0 | |
207 | CID 0x00 1 | |
208 | CLA 0x94 2 | |
209 | SELECT FILE 0xA4 3 | |
210 | READ FILE 0xB2 3 | |
211 | P1 4 | |
212 | P2 5 | |
213 | LEN_ | |
214 | 0 1 2 3 4 5 6 7 | |
215 | apdu = '0a 00 94 a4 08 00 04 3f 00 00 02' --select ICC file | |
216 | DF_NAME = "1TIC.ICA" | |
217 | --]] | |
218 | --for i = 1,10 do | |
219 | --result, err = calypso_send_cmd_raw('0294a40800043f000002',false) --select ICC file | |
220 | for i, apdu in spairs(_calypso_cmds) do | |
4bf0f73d | 221 | print('>>', i ) |
452aab1e | 222 | apdu = apdu:gsub("%s+","") |
223 | result, err = calypso_send_cmd_raw(apdu , false) | |
224 | if result then | |
225 | calypso_apdu_status(result.data) | |
4bf0f73d | 226 | print('<<', result.data ) |
227 | else | |
228 | print('<< no answer') | |
452aab1e | 229 | end |
230 | end | |
231 | calypso_switch_off_field() | |
232 | end | |
233 | --- | |
234 | -- a simple selftest function, tries to convert | |
235 | function selftest() | |
236 | DEBUG = true | |
237 | dbg("Performing test") | |
238 | dbg("Tests done") | |
239 | end | |
240 | -- Flip the switch here to perform a sanity check. | |
241 | -- It read a nonce in two different ways, as specified in the usage-section | |
242 | if "--test"==args then | |
243 | selftest() | |
244 | else | |
245 | -- Call the main | |
246 | main(args) | |
247 | end |