Skip to content

Instantly share code, notes, and snippets.

@rena2019
Last active January 6, 2019 15:55
Show Gist options
  • Save rena2019/322811fa05cad4b9e168170ca287c955 to your computer and use it in GitHub Desktop.
Save rena2019/322811fa05cad4b9e168170ca287c955 to your computer and use it in GitHub Desktop.
--
-- Copyright 2018 rena2019
-- v0.2
--
-- @name girocard-limits
-- @description Display girocard limits
-- @targets 0.8
require('lib.tlv')
require('lib.strict')
local sw, resp --, rec12, rec14
local mycard
--local title
function getDF (str, df, second)
local index
index = string.find(str, df)
if (index == nil) then
return ""
end
if second then
index = string.find(str, df, index + 1)
end
if (index == nil) then
return ""
end
return string.sub(str, index + 6, index + 5 + (tonumber(string.sub(str, index + 4, index + 5), 16) * 2) )
end
function displayDFs(title, rec12, rec14)
print("\r\n" .. title .. " --------------------------------------------")
print("RECORD 12: " .. rec12)
print("DF01: " .. getDF(rec12, "DF01", true))
print("DF03: " .. getDF(rec12, "DF03", true))
print("DF11: " .. getDF(rec12, "DF11", true))
print("DF13: " .. getDF(rec12, "DF13", true))
print("RECORD 14: " .. rec14)
print("DF03: " .. getDF(rec14, "DF03", false))
print("DF13: " .. getDF(rec14, "DF13", false))
print("--------------------------------------------")
end
function tst ()
return not(test == nil)
end
if not(pcall(tst)) then
print("card.connect")
if card.connect() then
title = "girocard"
mycard = card.tree_startup(title)
local rec
card.warm_reset()
-- SELECT MF?
card.send(bytes.new(8,"00 A4 00 0C"))
-- SELECT GA-Maestro DF_GA_MAESTRO / DF_GA_EMV
card.send(bytes.new(8,"00 A4 01 0C 02 AC 00"))
-- SELECT EF_DO
card.send(bytes.new(8,"00 A4 02 04 02 00 31 00"))
-- MSE Restore SEID=02 ?
card.send(bytes.new(8,"00 22 F3 02"))
-- READ RECORD 12
sw,resp = card.send(bytes.new(8,"00 B2 0C 04 00"))
if sw == 0x9000 then
rec12 = tostring(resp)
print("rec12 " .. rec12)
else
print("rec12 error" .. sw)
end
-- READ RECORD 14
sw,resp = card.send(bytes.new(8,"00 B2 0E 04 00"))
if sw == 0x9000 then
rec14 = tostring(resp)
end
card.disconnect()
else
ui.question("No card detected in reader",{"OK"})
end
else
-- test values for command line. call with, e.g.:
-- cardpeek -r "pcsc://Gemalto PC Twin Reader 00 00" -e "dofile('girocard-limits.lua'); ui.exit()"
print("use test vectors")
sw = 0x9000
end
if not(rec12 == nil) and not(rec14 == nil) then
local resp
local ctl_sum_limit, ctl_sum_curr
local ct_sum_limit, ct_sum_curr
local ctl_count, ctl_max
displayDFs(title, rec12, rec14)
ctl_sum_curr = tonumber(string.sub(getDF(rec12, "DF03", true), 6)) / 100
ctl_sum_limit = getDF(rec12, "DF13", true)
ctl_sum_limit = tonumber(string.sub(ctl_sum_limit, string.len(ctl_sum_limit) - 11)) / 100
ct_sum_curr = tonumber(getDF(rec12, "DF01", true)) / 100
ct_sum_limit = getDF(rec12, "DF11", true)
ct_sum_limit = tonumber(string.sub(ct_sum_limit, string.len(ct_sum_limit) - 11)) / 100
ctl_count = tonumber(getDF(rec14, "DF03", false))
ctl_max = getDF(rec14, "DF13", false)
ctl_max = tonumber(string.sub(ctl_max, string.len(ctl_max) - 1), 16)
if not(mycard == nil) then
rec = nodes.append(mycard, {classname="record", label="RECORD 12", val=rec12})
nodes.append(rec, {classname="item", label="contactless sum current", val=ctl_sum_curr})
nodes.append(rec, {classname="item", label="contactless sum limit", val=ctl_sum_limit})
nodes.append(rec, {classname="item", label="contact sum current", val=ct_sum_curr})
nodes.append(rec, {classname="item", label="contact sum limit", val=ct_sum_limit})
rec = nodes.append(mycard, {classname="record", label="RECORD 14", val=rec14})
nodes.append(rec, {classname="item", label="contactless counter", val=ctl_count})
nodes.append(rec, {classname="item", label="contactless counter max", val=ctl_max})
end
print("contactless sum current: " .. ctl_sum_curr)
print("contactless sum limit: " .. ctl_sum_limit)
print("contact sum current: " .. ct_sum_curr)
print("contact sum limit: " .. ct_sum_limit)
print("contactless counter: " .. ctl_count)
print("contactless counter max: " .. ctl_max)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment