Commit f1245abd authored by Arturo Hernandez's avatar Arturo Hernandez

working write, inc, dec & test

parent 41c2ecdf
......@@ -37,17 +37,17 @@ def card_start():
LOGGER.info('UUIDHex: {}'.format(toHexString(uid, 1)))
return card, atr, uid
def find_atr(data, atr):
""" requires data{ATR_match:[], ATR_possible:[]}"""
def find_atr(output, atr):
""" requires output{ATR_match:[], ATR_possible:[]}"""
found = parseATR.match_atr_differentiated(atr, "src/parseATR/smartcard_list.txt")
if found:
if atr in found:
for d in found[atr]:
data["ATR_match"].append(str(d))
output["ATR_match"].append(str(d))
del found[atr]
for a in found:
for d in found[a]:
data["ATR_possible"].append(str(d))
output["ATR_possible"].append(str(d))
class BaseHandler(tornado.web.RequestHandler):
def set_default_headers(self):
......@@ -85,7 +85,7 @@ class ReadCardHandler(BaseHandler):
self.write({"error": str(e)})
return
data = {
output = {
'reader': str(card.reader),
'ATR': atr,
'ATR_match': [],
......@@ -97,32 +97,40 @@ class ReadCardHandler(BaseHandler):
if block >=0:
LOGGER.info("reading block %i", block)
if card.auth_with_keys(block,key_a, key_b, use_keyb):
block_data = card.read_block(block)
data["data"]={
data = card.read_block(block)
output["data"]={
"block" : block,
"array" : block_data,
"hex": toHexString(block_data,1),
"string" : toHexString(block_data),
"array" : data,
"hex": toHexString(data,1),
"string" : toHexString(data),
"isValue": False,
"value": 0,
"isTrailer": (block & 0x03) == 0x03 if block < 128 else (block & 0x0F) == 0x0F,
}
#check if value
value = card.parse_value(block_data)
value = card.parse_value(data)
if value is not None:
data["data"]["isValue"] = True
data["data"]["value"] = value[0] #TODO: adr?
output["data"]["isValue"] = True
output["data"]["value"] = value[0]
output["data"]["value_adr"] = value[1]
else:
data["message"] = "Sin autorización para leer bloque de datos"
find_atr(data, atr)
self.write(data)
output["message"] = "Sin autorización para leer bloque de datos"
find_atr(output, atr)
self.write(output)
class WriteCardHandler(BaseHandler):
def post(self):
print("writing card...")
LOGGER.info("writing card...")
block = int(self.get_argument("block",strip=True))
data = list(bytes.fromhex(self.get_argument("data", strip=True)))
value = self.get_argument("value", default=None, strip=True)
value_adr = self.get_argument("value_adr", default=None, strip=True)
if value is not None and value_adr is not None:
#format value
value = int(value)
value_adr = int(value_adr)
data = OMNIKEYCard.format_value(value, value_adr)
else:
data = list(bytes.fromhex(self.get_argument("data", strip=True)))
key_a = self.get_argument("key_a", default=config.DEFAULT_KEY_A, strip=True)
key_b = self.get_argument("key_b", default=config.DEFAULT_KEY_B, strip=True)
use_keyb = config.str2bool(self.get_argument("use_keyb", default="False"))
......@@ -133,7 +141,7 @@ class WriteCardHandler(BaseHandler):
self.write({"error": str(e)})
return
data = {
output = {
'reader': str(card.reader),
'ATR': atr,
'ATR_match': [],
......@@ -146,9 +154,83 @@ class WriteCardHandler(BaseHandler):
if not card.auth_with_keys(block,key_a, key_b, use_keyb):
self.write({"error": "No se pudo autenticar"})
return
card.write_block(block,data)
find_atr(data, atr)
self.write(data)
card.write_block(block, data)
find_atr(output, atr)
self.write(output)
class IncrementValueHandler(BaseHandler):
def post(self):
LOGGER.info("increment card value...")
block = int(self.get_argument("block",strip=True))
value = self.get_argument("value", strip=True)
value = int(value)
key_a = self.get_argument("key_a", default=config.DEFAULT_KEY_A, strip=True)
key_b = self.get_argument("key_b", default=config.DEFAULT_KEY_B, strip=True)
use_keyb = config.str2bool(self.get_argument("use_keyb", default="False"))
try:
card, atr, uid = card_start()
except OMNIKEYException as e:
LOGGER.info("Error: {}".format(e))
self.write({"error": str(e)})
return
output = {
'reader': str(card.reader),
'ATR': atr,
'ATR_match': [],
'ATR_possible': [],
'UUIDHex': toHexString(uid, 1),
'message': 'OK'
}
LOGGER.info("Writing block %i", block)
if not card.auth_with_keys(block,key_a, key_b, use_keyb):
self.write({"error": "No se pudo autenticar"})
return
try:
card.increment_value(block, value)
except SWException as e:
self.write({"error": "No se pudo incrementar valor"})
return
find_atr(output, atr)
self.write(output)
class DecrementValueHandler(BaseHandler):
def post(self):
LOGGER.info("decrement card value...")
block = int(self.get_argument("block",strip=True))
value = self.get_argument("value", strip=True)
value = int(value)
key_a = self.get_argument("key_a", default=config.DEFAULT_KEY_A, strip=True)
key_b = self.get_argument("key_b", default=config.DEFAULT_KEY_B, strip=True)
use_keyb = config.str2bool(self.get_argument("use_keyb", default="False"))
try:
card, atr, uid = card_start()
except OMNIKEYException as e:
LOGGER.info("Error: {}".format(e))
self.write({"error": str(e)})
return
output = {
'reader': str(card.reader),
'ATR': atr,
'ATR_match': [],
'ATR_possible': [],
'UUIDHex': toHexString(uid, 1),
'message': 'OK'
}
LOGGER.info("Writing block %i", block)
if not card.auth_with_keys(block,key_a, key_b, use_keyb):
self.write({"error": "No se pudo autenticar"})
return
try:
card.decrement_value(block, value)
except SWException as e:
self.write({"error": "No se pudo decrementar valor"})
return
find_atr(output, atr)
self.write(output)
class InitCardHandler(BaseHandler):
""" WIP mf plus"""
......@@ -170,7 +252,8 @@ class InitCardHandler(BaseHandler):
#init stuff here!
card.APDU_get_version()
init = card.MP_BO_Init()
data = {
#TODO: init keys here?
output = {
'reader': str(card.reader),
'ATR': atr,
'ATR_match': [],
......@@ -179,8 +262,8 @@ class InitCardHandler(BaseHandler):
'message': 'OK',
'init': str(init)
}
find_atr(data, atr)
self.write(data)
find_atr(output, atr)
self.write(output)
class MainHandler(tornado.web.RequestHandler):
def get(self):
......@@ -204,7 +287,9 @@ def make_app():
(r"/api/?", TestHandler),
(r"/api/read-card/?", ReadCardHandler),
(r"/api/init-card/?", InitCardHandler),
(r"/api/write-card/?", MainHandler),
(r"/api/write-card/?", WriteCardHandler),
(r"/api/increment-value/?", IncrementValueHandler),
(r"/api/decrement-value/?", DecrementValueHandler),
(r"/mst_cert.crt()",tornado.web.StaticFileHandler, {"path": config.MST_CERT}),
(r"/(.*)",tornado.web.StaticFileHandler, {"path": "src/www"},), #path fixed to top
], template_path="src/www", debug=config.DEBUG) #path fixed to top
......
......@@ -153,12 +153,18 @@ class OMNIKEYCard(object):
v1 = block[:4]
v2 = block[4:8]
v3 = block[8:12]
if v2 == [255-v for v in v1] and v1 == v3 and block[12] == block[14] and block[13] == block[15] and 255 - block[12] == block[12]:
value = (unpack("<i",v1)[0], block[12])
LOGGER.debug("testing for v1={} v2={} v3={}".format(v1, v2, v3))
if v2 == [255-v for v in v1] and v1 == v3 and block[12] == block[14] and block[13] == block[15] and 255 - block[13] == block[12]:
value = (unpack("<i",bytes(v1))[0], block[12])
LOGGER.debug("value found!")
else:
LOGGER.debug("not a value block...")
return value
@staticmethod
def format_value(value, adr) -> list:
block = list(pack("<iiibbbb", value, ~value, value, adr, ~adr, adr, ~adr))
return block
def APDU_get_version(self) -> str:
data, sw1, sw2 = self.connection.transmit([0xFF, 0x68, 0x0E, 0x08, 0x02, 0x01, 0x00]) # propietary reader command "get version"
......@@ -274,7 +280,7 @@ class OMNIKEYCard(object):
raise OMNIKEYException("bloqueado acceso a llaves")
#TODO: check data size must be always 16
if len(data) != 16:
raise OMNIKEYException("tamaño de datos incorrecto")
raise OMNIKEYException("tamaño de datos incorrecto: {} {}".format(len(data), data))
response, sw1, sw2 = self.connection.transmit([0xFF, 0xD6, 0x00 , block, len(data)] + data) # read block
LOGGER.debug ("WB#{}, data:{}, sw {}". format(block, toHexString(data), toHexString([sw1, sw2])))
self.sw1 = sw1; self.sw2 = sw2
......@@ -307,9 +313,9 @@ class OMNIKEYCard(object):
LOGGER.debug ("Intentando escribir bloque de llaves #{}". format(block))
raise OMNIKEYException("bloqueado acceso a llaves")
#TODO: check value is int32
data = [ byte2int(x) for x in pack("I", value_inc)]
data = [ byte2int(x) for x in pack("<i", value_inc)]
response, sw1, sw2 = self.connection.transmit([0xFF, 0xD4, 0x00 , block, 4] + data) # read block
LOGGER.debug ("WB#{}, data:{}, sw {}". format(block, toHexString(data), toHexString([sw1, sw2])))
LOGGER.debug ("WI#{}, data:{}, sw {}". format(block, toHexString(data), toHexString([sw1, sw2])))
self.sw1 = sw1; self.sw2 = sw2
errorchain[0](response, sw1, sw2)
......@@ -318,9 +324,9 @@ class OMNIKEYCard(object):
LOGGER.debug ("Intentando escribir bloque de llaves #{}". format(block))
raise OMNIKEYException("bloqueado acceso a llaves")
#TODO: check value is int32
data = [ byte2int(x) for x in pack("I", value_dec)]
data = [ byte2int(x) for x in pack("<i", value_dec)]
response, sw1, sw2 = self.connection.transmit([0xFF, 0xD8, 0x00 , block, 4] + data) # read block
LOGGER.debug ("WB#{}, data:{}, sw {}". format(block, toHexString(data), toHexString([sw1, sw2])))
LOGGER.debug ("WD#{}, data:{}, sw {}". format(block, toHexString(data), toHexString([sw1, sw2])))
self.sw1 = sw1; self.sw2 = sw2
errorchain[0](response, sw1, sw2)
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment