本文整理汇总了Python中TLV_utils类的典型用法代码示例。如果您正苦于以下问题:Python TLV_utils类的具体用法?Python TLV_utils怎么用?Python TLV_utils使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TLV_utils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _parse
def _parse(self, contents):
self._rawdata = contents
self._tlvdata = TLV_utils.unpack(contents)
tmp = TLV_utils.tlv_find_tag(self._tlvdata, 0xEA, num_results = 1)
if len(tmp) == 0:
raise ValueError, "Can't parse information file, tag 0xEA not found"
tmp = TLV_utils.tlv_find_tag(tmp, 0x85, num_results = 1)
if len(tmp) == 0:
raise ValueError, "Can't parse information file, tag 0x85 not found"
self._mainblob = tmp[0][2]
tmp = self._mainblob
some_id, tmp = tmp[:4], tmp[4:]
ascii_field_len = ord(tmp[0])
tmp = tmp[1:]
ascii_field, tmp = tmp[:ascii_field_len], tmp[ascii_field_len:]
self._maindata = ascii_field.split(" ")
if len(tmp) > 0:
if tmp[0] == "\x01":
tmp = tmp[1:]
birthdate_bin, tmp = tmp[:4], tmp[4:]
birthdate = binascii.b2a_hex(birthdate_bin)
self._birthdate = datetime.date( int(birthdate[0:4]), int(birthdate[4:6]), int(birthdate[6:8]) )
if len(tmp) > 0:
print "Warning: unparsed data trailing: %r" % tmp
示例2: deformat_response
def deformat_response(self, tlv_data, sw):
WHITELIST = (0x84, 0x86, 0x98)
result = []
is_ok = True
for data in tlv_data:
t = data[0] & ~0x01
if t not in WHITELIST and t in range(0x80, 0xBF+1):
is_ok = False # Unrecognized SM field present
if is_ok:
for data in tlv_data:
t = data[0] & ~0x01
value = data[2]
if t in WHITELIST:
if t == 0x86:
result.append( value[1:] )
elif t == 0x98:
if sw != value:
print "Warning: SW from SM not equal SW from RAPDU"
sw = value
else:
result.append( value )
else:
result.append( TLV_utils.pack( (data,), recalculate_length = True) )
else:
result.append( TLV_utils.pack( tlv_data, recalculate_length = True) )
return "".join(result), sw
示例3: cmd_open
def cmd_open(self, file):
"Open a file"
fid = binascii.a2b_hex("".join(file.split()))
result = self.open_file(fid)
if len(result.data) > 0:
print utils.hexdump(result.data)
print TLV_utils.decode(result.data,tags=self.TLV_OBJECTS)
示例4: parse_DG1
def parse_DG1(self, contents):
structure = TLV_utils.unpack(contents)
try:
mrz = TLV_utils.tlv_find_tag(structure, 0x5F1F, 1)[0][2]
except IndexError:
raise PassportParseError, "Could not find MRZ information in DG1"
# Length of an MRZ line is either 30+5 or 31+5 or 39+5, depending on document type. (LDS technical report 2004, section 16.1)
mrz_data = (mrz[:len(mrz)/2], mrz[len(mrz)/2:])
self.dg1_mrz = mrz_data
self._parse_mrz(mrz_data)
示例5: cmd_selectapplication
def cmd_selectapplication(self, application):
"""Select an application on the card.
application can be given either as hexadecimal aid or by symbolic name (if known)."""
aid = self.resolve_symbolic_aid(application)
result = self.select_application(aid)
if len(result.data) > 0:
print utils.hexdump(result.data)
print TLV_utils.decode(result.data,tags=self.TLV_OBJECTS)
示例6: cmd_selectfile
def cmd_selectfile(self, p1, p2, fid):
"""Select a file on the card."""
p1 = binascii.a2b_hex("".join(p1.split()))
p2 = binascii.a2b_hex("".join(p2.split()))
fid = binascii.a2b_hex("".join(fid.split()))
result = self.select_file(p1, p2, fid)
if len(result.data) > 0:
print utils.hexdump(result.data)
print TLV_utils.decode(result.data,tags=self.TLV_OBJECTS)
示例7: cmd_cd
def cmd_cd(self, dir = None):
"Change into a DF, or into the MF if no dir is given"
if dir is None:
result = self.change_dir()
else:
fid = binascii.a2b_hex("".join(dir.split()))
result = self.change_dir(fid)
if len(result.data) > 0:
print utils.hexdump(result.data)
print TLV_utils.decode(result.data,tags=self.TLV_OBJECTS)
示例8: cmd_parsetlv
def cmd_parsetlv(self, start = None, end = None):
"Decode the TLV data in the last response, start and end are optional"
lastlen = len(self.last_result.data)
if start is not None:
start = (lastlen + (int(start,0) % lastlen) ) % lastlen
else:
start = 0
if end is not None:
end = (lastlen + (int(end,0) % lastlen) ) % lastlen
else:
end = lastlen
print TLV_utils.decode(self.last_result.data[start:end], tags=self.TLV_OBJECTS, context = self.DEFAULT_CONTEXT)
示例9: dump
def dump(data):
print "Dump following (%i bytes)" % (len(data))
print utils.hexdump(data)
try:
print "Trying TLV parse:"
print TLV_utils.decode(data, tags=card.TLV_OBJECTS, context = card.DEFAULT_CONTEXT)
print "TLV parsed successfully"
except (SystemExit, KeyboardInterrupt):
raise
except:
print "TLV error"
pass
示例10: process_apdu
def process_apdu(self, apdu):
if apdu.cla & 0x0c in (0x0c, 0x08):
tlv_data = TLV_utils.unpack(apdu.data, with_marks = apdu.marks, include_filler=True)
tlv_data = self.encrypt_command(tlv_data)
tlv_data = self.authenticate_command(apdu, tlv_data)
data = TLV_utils.pack(tlv_data, recalculate_length = True)
new_apdu = C_APDU(apdu, data = data)
return new_apdu
else:
return apdu
示例11: cmd_passive_auth
def cmd_passive_auth(self, verbose=1):
"Perform passive authentication"
hashes = {}
result = ""
i = 0
for name in ("DG1", "DG2", "SOD"):
fid = None
for n, f in self.INTERESTING_FILES:
if n == name:
fid = f
break
if fid is None:
return
i += 1
result = self.open_file(fid, 0x0c)
if self.check_sw(result.sw):
contents, sw = self.read_binary_file()
#self.last_result = R_APDU(contents + self.last_sw)
if name != "SOD":
hashes[i] = crypto_utils.hash("SHA", contents)
else:
result = self.verify_cms(contents[4:])
#print hexdump(result)
#print "DG1: %s" % hexdump(hashes[i])
#print "DG2: %s" % hexdump(hashes[2])
res = TLV_utils.tlv_find_tag(TLV_utils.unpack(result), 0x04)
if len(res) == 0:
print "failed to verify EF.SOD"
return
else:
print "verified EF.SOD"
i = 0
for tag, length, hash in res:
i += 1
if hexdump(hashes[i]) == hexdump(hash):
print "DG%d hash verified: %s" % (i, binascii.b2a_hex(hash))
else:
print "DG%d hash failed:" % i
print "was: %s" % binascii.b2a_hex(hashes[i])
print "expected: %s" % binascii.b2a_hex(hash)
return
示例12: _dump_internal
def _dump_internal(self, data, indent, do_tlv=True):
c = utils.hexdump(data)
r = map(lambda a: self.get_indent(indent)+a, c.splitlines(False))
if do_tlv:
try:
if self._card_object is not None:
c = TLV_utils.decode(data, tags=self._card_object.TLV_OBJECTS, context = self._card_object.DEFAULT_CONTEXT)
else:
c = TLV_utils.decode(data)
r.append( self.get_indent(indent) + "Trying TLV parse:" )
r.extend( map(lambda a: self.get_indent(indent)+a, c.splitlines(False)) )
except (SystemExit, KeyboardInterrupt):
raise
except:
pass
return r
示例13: set_passport
def set_passport(self, passport):
self.passport = passport
for sources, transform, destinations in self.PROPERTY_TRANSFORMATIONS:
values = [getattr(passport, src) for src in sources]
transformed = transform(passport, values)
for index, dst in enumerate(destinations):
widget = self.main_window_xml.get_widget(dst)
if not self.format_strings.has_key(dst):
self.format_strings[dst] = widget.get_label()
widget.set_label( self.format_strings[dst] % transformed[index] )
data = []
if hasattr(passport, "dg2_cbeff") and passport.dg2_cbeff is not None:
for biometric in passport.dg2_cbeff.biometrics:
data = data + [(a,b,"Encoded Face") for (a,b) in biometric.get_images()]
for dg, tag, type in ( ("dg5", 0x5F40, "Displayed Portrait"), ("dg7", 0x5F43, "Displayed Signature or Usual Mark") ):
if hasattr(passport, "%s_tlv" % dg):
structure = getattr(passport, "%s_tlv" % dg)
if structure is not None:
hits = TLV_utils.tlv_find_tag(structure, tag)
for t,l,v in hits:
data.append( ("jpg",v,type) )
self._set_images(data)
示例14: _format_management_information
def _format_management_information(self, indent):
result = []
if self._management_information is None: return result
try:
if self._card_object is not None:
c = TLV_utils.decode(self._management_information, tags=self._card_object.TLV_OBJECTS, context = self._card_object.DEFAULT_CONTEXT)
else:
c = TLV_utils.decode(self._management_information)
result.append(self.get_indent(indent+1) + "Management information:")
result.extend( map(lambda a: self.get_indent(indent+2)+a, c.splitlines(False)) )
except (SystemExit, KeyboardInterrupt):
raise
except:
result.append(self.get_indent(indent+1) + "Raw dump of unparseable management information following:")
result.extend(self._dump_internal(self._management_information, indent=indent+2, do_tlv=False))
return result
示例15: __init__
def __init__(self, structure, top_tag = 0x7F60):
"Create a new CBEFF instance from a nested TLV structure (as returned by TLV_utils.unpack)."
self.biometrics = []
self.unknown_biometrics = []
blocks = TLV_utils.tlv_find_tag(structure, top_tag)
for block in blocks:
self.addbiometric(block[2])