1 local cmds
= require('commands')
2 local getopt
= require('getopt')
3 local lib14b
= require('read14b')
4 local utils
= require('utils')
5 local iso7816
= require('7816_error')
6 local ansicolors
= require('ansicolors')
12 This is a script to communicate with a CALYSPO / 14443b tag using the '14b raw' commands
15 script run hf_14b_calypso -b 11223344
19 script run hf_14b_calypso -h -b
27 This script communicates with /armsrc/iso14443b.c,
28 Check there for details about data format and how commands are interpreted on the
32 local function calypso_parse(result
)
33 if result
.Oldarg0
>= 0 then
34 local len
= result
.Oldarg0
* 2
36 local d
= string.sub(result
.Data
, 0, len
);
40 return nil, "calypso_parse failed"
43 -- A debug printout-function
44 local function dbg(args
)
45 if not DEBUG
then return end
46 if type(args
) == 'table' then
57 -- This is only meant to be used when errors occur
58 local function oops(err
)
70 print(ansicolors
.cyan
..'Usage'..ansicolors
.reset
)
72 print(ansicolors
.cyan
..'Arguments'..ansicolors
.reset
)
74 print(ansicolors
.cyan
..'Example usage'..ansicolors
.reset
)
78 -- helper function, give current count of items in lua-table.
79 local function tablelen(T
)
81 for _
in pairs(T
) do count
= count
+ 1 end
85 -- helper function, gives a sorted table from table t,
86 -- order can be a seperate sorting-order function.
87 local function spairs(t
, order
)
90 for k
in pairs(t
) do keys
[#keys
+1] = k
end
92 -- if order function given, sort by it by passing the table and keys a, b,
93 -- otherwise just sort the keys
95 table.sort(keys
, function(a
,b
) return order(t
, a
, b
) end)
100 -- return the iterator function
105 return keys
[i
], t
[keys
[i]]
110 -- Sends a usbpackage , "hf 14b raw"
111 -- if it reads the response, it converts it to a lua object "Command" first and the Data is cut to correct length.
112 local function calypso_send_cmd_raw(data
, ignoreresponse
)
114 local flags
= lib14b
.ISO14B_COMMAND
.ISO14B_APDU
115 -- flags = lib14b.ISO14B_COMMAND.ISO14B_RAW +
116 -- lib14b.ISO14B_COMMAND.ISO14B_APPEND_CRC
117 local flags
= lib14b
.ISO14B_COMMAND
.ISO14B_APDU
119 -- LEN of data, half the length of the ASCII-string hex string
125 local flags_str
= ('%04x'):format(utils
.SwapEndianness(('%04x'):format(flags
), 16))
126 local time_str
= ('%08x'):format(0)
127 local rawlen_str
= ('%04x'):format(utils
.SwapEndianness(('%04x'):format(( 8 + #data
/2)), 16))
128 local senddata
= ('%s%s%s%s'):format(flags_str
, time_str
, rawlen_str
,data
)
129 local c
= Command
:newNG
{cmd
= cmds
.CMD_HF_ISO14443B_COMMAND
, data
= senddata
}
130 local result
, err
= c
:sendNG(ignoreresponse
, 2000)
132 if result
.Oldarg0
>= 0 then
133 return calypso_parse(result
)
135 err
= 'card response failed'
138 err
= 'No response from card'
143 -- calypso_card_num : Reads card number from ATR and
144 -- writes it in the tree in decimal format.
145 local function calypso_card_num(card
)
146 if not card
then return end
147 local card_num
= tonumber( card
.uid
:sub(1,8),16 )
149 print('Card UID ' ..ansicolors
.green
..card
.uid
:format('%x')..ansicolors
.reset
)
150 print('Card Number ' ..ansicolors
.green
..string.format('%u', card_num
)..ansicolors
.reset
)
151 print('-----------------------')
154 -- analyse CALYPSO apdu status bytes.
155 local function calypso_apdu_status(apdu
)
157 -- next two is APDU status bytes.
159 local sw
= apdu
:sub( #apdu
-7, #apdu
-4)
160 desc
, err
= iso7816
.tostring(sw
)
161 --print ('SW', sw, desc, err )
162 local status
= ( sw
== '9000' )
163 return status
, desc
, err
167 local _calypso_cmds
= {
169 -- Break down of command bytes:
172 -- 0x3F = master file
173 -- 0x00 = master file id, is constant to 0x00.
175 -- DF Dedicated File 38nn
176 -- can be seen as directories
179 -- ["01.Select ICC file"] = '0294 a4 080004 3f00 0002',
181 -- EF Elementary File
185 -- Electronic deposit file
186 -- Electronic Purse file
187 -- Electronic Transaction log file
189 ['01.Select ICC file'] = CLA
..'a4 080004 3f00 0002',
190 ['02.ICC'] = CLA
..'b2 01 041d',
191 ['03.Select EnvHol file'] = CLA
..'a4 080004 2000 2001',
192 ['04.EnvHol1'] = CLA
..'b2 01 041d',
193 ['05.Select EvLog file'] = CLA
..'a4 080004 2000 2010',
194 ['06.EvLog1'] = CLA
..'b2 01 041d',
195 ['07.EvLog2'] = CLA
..'b2 02 041d',
196 ['08.EvLog3'] = CLA
..'b2 03 041d',
197 ['09.Select ConList file']= CLA
..'a4 080004 2000 2050',
198 ['10.ConList'] = CLA
..'b2 01 041d',
199 ['11.Select Contra file'] = CLA
..'a4 080004 2000 2020',
200 ['12.Contra1'] = CLA
..'b2 01 041d',
201 ['13.Contra2'] = CLA
..'b2 02 041d',
202 ['14.Contra3'] = CLA
..'b2 03 041d',
203 ['15.Contra4'] = CLA
..'b2 04 041d',
204 ['16.Select Counter file']= CLA
..'a4 080004 2000 2069',
205 ['17.Counter'] = CLA
..'b2 01 041d',
206 ['18.Select SpecEv file'] = CLA
..'a4 080004 2000 2040',
207 ['19.SpecEv1'] = CLA
..'b2 01 041d',
211 -- The main entry point
214 print( string.rep('--',20) )
215 print( string.rep('--',20) )
218 local data
, apdu
, flags
, uid
, cid
, result
, err
, card
219 -- Read the parameters
220 for o
, a
in getopt
.getopt(args
, 'h') do
221 if o
== 'h' then return help() end
222 if o
== 'b' then bytes
= a
end
228 card
, err
= lib14b
.waitFor14443b()
229 if not card
then return oops(err
) end
231 calypso_card_num(card
)
245 apdu = '02 94 a4 08 00 04 3f 00 00 02' --select ICC file
249 --result, err = calypso_send_cmd_raw('0294a40800043f000002',false) --select ICC file
250 for i
, apdu
in spairs(_calypso_cmds
) do
251 print('>> '..ansicolors
.yellow
..i
..ansicolors
.reset
)
252 apdu
= apdu
:gsub('%s+', '')
253 data
, err
= calypso_send_cmd_raw(apdu
, false)
258 local status
, desc
, err
= calypso_apdu_status(data
)
259 local d
= data
:sub(3, (#data
- 8))
261 print('<< '..d
..' ('..ansicolors
.green
..'ok'..ansicolors
.reset
..')')
263 print('<< '..d
..' '..ansicolors
.red
..err
..ansicolors
.reset
)
266 print('<< no answer')
273 -- a simple selftest function, tries to convert
276 dbg('Performing test')
279 -- Flip the switch here to perform a sanity check.
280 -- It read a nonce in two different ways, as specified in the usage-section
281 if '--test'==args
then