1 local cmds = require('commands')
2 local getopt = require('getopt')
3 local lib14b = require('read14b')
4 local utils = require('utils')
6 example = "script runs 14b raw commands to query a CAPLYPSO tag"
7 author = "Iceman, 2016"
10 This is a script to communicate with a CALYSPO / 14443b tag using the '14b raw' commands
15 script run f -b 11223344
20 # 1. Connect and don't disconnect
22 # 2. Send mf auth, read response
30 This script communicates with /armsrc/iso14443b.c,
31 Check there for details about data format and how commands are interpreted on the
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)
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)
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);
60 -- A debug printout-function
61 local function dbg(args)
67 -- This is only meant to be used when errors occur
68 local function oops(err)
70 calypso_switch_off_field()
77 print("Example usage")
81 -- helper function, give current count of items in lua-table.
82 local function tablelen(T)
84 for _ in pairs(T) do count = count + 1 end
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)
93 for k in pairs(t) do keys[#keys+1] = k end
95 -- if order function given, sort by it by passing the table and keys a, b,
96 -- otherwise just sort the keys
98 table.sort(keys, function(a,b) return order(t, a, b) end)
103 -- return the iterator function
108 return keys[i], t[keys[i]]
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 )
117 local command, flags, result, err
118 flags = lib14b.ISO14B_COMMAND.ISO14B_RAW +
119 lib14b.ISO14B_COMMAND.ISO14B_APPEND_CRC
123 command = Command:new{cmd = cmds.CMD_ISO_14443B_COMMAND,
125 arg2 = #data/2, -- LEN of data, half the length of the ASCII-string hex string
127 data = data} -- data bytes (commands etc)
128 result, err = lib14b.sendToDevice(command, false)
130 if ignoreresponse then return response, err end
133 local r = calypso_parse(result)
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)
148 -- analyse CALYPSO apdu status bytes.
149 local function calypso_apdu_status(apdu)
151 -- next two is APDU status bytes.
152 local sw = apdu:sub( #apdu-7, #apdu-4)
155 if sw == '9000' then return 1 end
159 local _calypso_cmds = {
160 ["1.Select ICC file"] = "02 94 a4 08 00 04 3f 00 00 02",
161 ["2.ICC"] = "02 94 b2 01 04 1d",
162 ["3.Select EnvHol file"]= '02 94 a4 08 00 04 20 00 20 01',
163 ["4.EnvHol1"] = '02 94 b2 01 04 1d',
164 ["5.Select EvLog file"] = '02 94 a4 08 00 04 20 00 20 10',
165 ["6.EvLog1"] = '06 00b2 0104 1d',
166 ["7.EvLog2"] = '06 00b2 0204 1d',
167 ["8.EvLog3"] = '06 00b2 0304 1d',
168 -- ["Select ConList file"]= '42 01 04 0a 00a4 0800 04 2000 2050',
169 -- ["ConList"] = '42 01 06 06 00b2 0104 1d',
170 -- ["Select Contra file"]= '42 01 08 0a 00a4 0800 04 2000 2020',
171 -- ["Contra1"] = '42 01 0a 06 00b2 0104 1d',
172 -- ["Contra2"] = '42 01 0c 06 00b2 0204 1d',
173 -- ["Contra3"] = '42 01 0e 06 00b2 0304 1d',
174 -- ["Contra4"] = '42 01 00 06 00b2 0404 1d',
175 -- ["Select Counter file"]= '42 01 02 0a 00a4 0800 04 2000 2069',
176 -- ["Counter"] = '42 01 04 06 00b2 0104 1d',
177 -- ["Select SpecEv file"]= '42 01 06 0a 00a4 08 0004 2000 2040',
178 -- ["SpecEv1"] = '42 01 08 06 00b2 0104 1d',
182 -- The main entry point
185 local data, apdu, flags, uid, cid, result, err, card
186 -- Read the parameters
187 for o, a in getopt.getopt(args, 'h') do
188 if o == "h" then return help() end
191 calypso_switch_on_field()
194 card, err = lib14b.waitFor14443b()
195 if not card then return oops(err) end
197 calypso_card_num(card)
211 apdu = '0a 00 94 a4 08 00 04 3f 00 00 02' --select ICC file
215 --result, err = calypso_send_cmd_raw('0294a40800043f000002',false) --select ICC file
216 for i, apdu in spairs(_calypso_cmds) do
217 apdu = apdu:gsub("%s+","")
218 result, err = calypso_send_cmd_raw(apdu , false)
220 calypso_apdu_status(result.data)
224 calypso_switch_off_field()
227 -- a simple selftest function, tries to convert
230 dbg("Performing test")
233 -- Flip the switch here to perform a sanity check.
234 -- It read a nonce in two different ways, as specified in the usage-section
235 if "--test"==args then