2022-01-17 01:14:07 +03:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
|
|
|
# Copyright 2022 David Fort <contact@hardening-consulting.com>
|
|
|
|
#
|
|
|
|
# This script is meant to parse some FreeRDP logs in DEBUG mode (WLOG_LEVEL=DEBUG) and interpret the
|
|
|
|
# smartcard traffic, dissecting the PIV or GIDS commands
|
|
|
|
#
|
|
|
|
# usage:
|
|
|
|
# * live: WLOG_LEVEL=DEBUG xfreerdp <args with smartcard> | python3 smartcard-interpreter.py
|
|
|
|
# * on an existing log file: python3 smartcard-interpreter.py <log file>
|
|
|
|
#
|
|
|
|
import sys
|
|
|
|
import codecs
|
|
|
|
|
|
|
|
|
|
|
|
CMD_NAMES = {
|
|
|
|
0x04: "DESACTIVATE FILE",
|
|
|
|
0x0C: "ERASE RECORD",
|
|
|
|
0x0E: "ERASE BINARY",
|
|
|
|
0x0F: "ERASE BINARY",
|
|
|
|
0x20: "VERIFY",
|
|
|
|
0x21: "VERIFY",
|
|
|
|
0x22: "MSE",
|
|
|
|
0x24: "CHANGE REFERENCE DATA",
|
|
|
|
0x25: "MSE",
|
|
|
|
0x26: "DISABLE VERIFICATION REQUIREMENT",
|
|
|
|
0x28: "ENABLE VERIFICATION REQUIREMENT",
|
|
|
|
0x2A: "PSO",
|
|
|
|
0x2C: "RESET RETRY COUNTER",
|
|
|
|
0x2D: "RESET RETRY COUNTER",
|
|
|
|
0x44: "ACTIVATE FILE",
|
|
|
|
0x46: "GENERATE ASYMMETRIC KEY PAIR",
|
|
|
|
0x47: "GENERATE ASYMMETRIC KEY PAIR",
|
|
|
|
0x84: "GET CHALLENGE",
|
|
|
|
0x86: "GENERAL AUTHENTICATE",
|
|
|
|
0x87: "GENERAL AUTHENTICATE",
|
|
|
|
0x88: "INTERNAL AUTHENTICATE",
|
|
|
|
0xA0: "SEARCH BINARY",
|
|
|
|
0xA1: "SEARCH BINARY",
|
|
|
|
0xA2: "SEARCH RECORD",
|
|
|
|
0xA4: "SELECT",
|
|
|
|
0xB0: "READ BINARY",
|
|
|
|
0xB1: "READ BINARY",
|
|
|
|
0xB2: "READ RECORD",
|
|
|
|
0xB3: "READ RECORD",
|
|
|
|
0xC0: "GET RESPONSE",
|
|
|
|
0xC2: "ENVELOPPE",
|
|
|
|
0xC3: "ENVELOPPE",
|
|
|
|
0xCA: "GET DATA",
|
|
|
|
0xCB: "GET DATA",
|
|
|
|
0xD0: "WRITE BINARY",
|
|
|
|
0xD1: "WRITE BINARY",
|
|
|
|
0xD2: "WRITE RECORD",
|
|
|
|
0xD6: "UPDATE BINARY",
|
|
|
|
0xD7: "UPDATE BINARY",
|
|
|
|
0xDA: "PUT DATA",
|
|
|
|
0xDB: "PUT DATA",
|
|
|
|
0xDC: "UPDATE RECORD",
|
|
|
|
0xDD: "UPDATE RECORD",
|
|
|
|
0xE0: "CREATE FILE",
|
|
|
|
0xE2: "APPEND RECORD",
|
|
|
|
0xE4: "DELETE FILE",
|
|
|
|
0xE6: "TERMINATE DF",
|
|
|
|
0xE8: "TERMINATE EF",
|
|
|
|
0xFE: "TERMINATE CARD USAGE",
|
|
|
|
}
|
|
|
|
|
|
|
|
AIDs = {
|
|
|
|
"a00000039742544659": "MsGidsAID",
|
|
|
|
"a000000308": "PIV",
|
|
|
|
"a0000003974349445f0100": "SC PNP",
|
2024-06-18 16:38:17 +03:00
|
|
|
"a0000001510000": "GPC",
|
2022-01-17 01:14:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
FIDs = {
|
|
|
|
0x0000: "Current EF",
|
|
|
|
0x2F00: "EF.DIR",
|
|
|
|
0x2F01: "EF.ATR",
|
|
|
|
0x3FFF: "Current application(ADF)",
|
|
|
|
}
|
|
|
|
|
|
|
|
DOs = {
|
|
|
|
"df1f": "DO_FILESYSTEMTABLE",
|
|
|
|
"df20": "DO_CARDID",
|
|
|
|
"df21": "DO_CARDAPPS",
|
|
|
|
"df22": "DO_CARDCF",
|
|
|
|
"df23": "DO_CMAPFILE",
|
|
|
|
"df24": "DO_KXC00",
|
|
|
|
}
|
|
|
|
|
|
|
|
ERROR_CODES = {
|
|
|
|
0x9000: "success",
|
2024-06-18 16:38:17 +03:00
|
|
|
0x6100: "more bytes",
|
2022-01-17 01:14:07 +03:00
|
|
|
0x6282: "end of file or record",
|
2024-06-18 16:38:17 +03:00
|
|
|
0x6283: "card locked",
|
2022-01-17 01:14:07 +03:00
|
|
|
0x63C0: "warning counter 0",
|
|
|
|
0x63C1: "warning counter 1",
|
|
|
|
0x63C2: "warning counter 2",
|
|
|
|
0x63C3: "warning counter 3",
|
|
|
|
0x63C4: "warning counter 4",
|
|
|
|
0x63C5: "warning counter 5",
|
|
|
|
0x63C6: "warning counter 6",
|
|
|
|
0x63C7: "warning counter 7",
|
|
|
|
0x63C8: "warning counter 8",
|
|
|
|
0x63C9: "warning counter 9",
|
|
|
|
0x6982: "security status not satisfied",
|
2024-06-18 16:38:17 +03:00
|
|
|
0x6882: "Secure messaging not supported",
|
2022-01-17 01:14:07 +03:00
|
|
|
0x6985: "condition of use not satisfied",
|
|
|
|
0x6A80: "incorrect parameter cmd data field",
|
|
|
|
0x6A81: "function not suppported",
|
|
|
|
0x6A82: "file or application not found",
|
|
|
|
0x6A83: "record not found",
|
|
|
|
0x6A88: "REFERENCE DATA NOT FOUND",
|
|
|
|
0x6D00: "unsupported",
|
|
|
|
}
|
|
|
|
|
|
|
|
PIV_OIDs = {
|
|
|
|
"5fc101": "X.509 Certificate for Card Authentication",
|
|
|
|
"5fc102": "Card Holder Unique Identifier",
|
|
|
|
"5fc103": "Cardholder Fingerprints",
|
|
|
|
"5fc105": "X.509 Certificate for PIV Authentication",
|
|
|
|
"5fc106": "Security Object",
|
|
|
|
"5fc107": "Card Capability Container",
|
|
|
|
"5fc108": "Cardholder Facial Image",
|
|
|
|
"5fc10a": "X.509 Certificate for Digital Signature",
|
|
|
|
"5fc10b": "X.509 Certificate for Key Management",
|
|
|
|
"5fc10d": "Retired X.509 Certificate for Key Management 1",
|
|
|
|
"5fc10e": "Retired X.509 Certificate for Key Management 2",
|
|
|
|
"5fc10f": "Retired X.509 Certificate for Key Management 3",
|
|
|
|
}
|
|
|
|
|
|
|
|
class ApplicationDummy(object):
|
2024-06-18 16:38:17 +03:00
|
|
|
"""Base application"""
|
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
def __init__(self, aid):
|
|
|
|
self.aid = aid
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
def getAID(self):
|
|
|
|
return self.aid
|
|
|
|
|
|
|
|
def selectResult(self, fci, status, body):
|
2024-06-18 16:38:17 +03:00
|
|
|
return 'selectResult(fci=%s, status=0x%x) = %s\n' %(fci, status, body.hex())
|
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
def getData(self, fileId, bytes):
|
2024-06-18 16:38:17 +03:00
|
|
|
return 'getData(status=0x%x) = %s\n' %(fileId, bytes.hex())
|
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
def getDataResult(self, status, body):
|
2024-06-18 16:38:17 +03:00
|
|
|
return 'getDataResult(status=0x%x) = %s\n' %(status, body.hex())
|
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
def mse(self, body):
|
|
|
|
return body.hex()
|
|
|
|
|
|
|
|
def mseResult(self, status, body):
|
|
|
|
return body.hex()
|
2024-06-18 16:38:17 +03:00
|
|
|
|
|
|
|
def pso(self, body):
|
|
|
|
return body.hex()
|
|
|
|
|
|
|
|
def psoResult(self, status, body):
|
|
|
|
return body.hex()
|
|
|
|
|
|
|
|
def getResponse(self, status, body):
|
|
|
|
return body.hex()
|
|
|
|
|
|
|
|
def getResponseResult(self, status, body):
|
|
|
|
return body.hex()
|
|
|
|
|
|
|
|
def verify(self, status, body):
|
|
|
|
return "verify(%s)" % body.hex()
|
|
|
|
|
|
|
|
def verifyResult(self, status, body):
|
|
|
|
return "verify(%s)" % body.hex()
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
class ApplicationGpc(object):
|
|
|
|
"""GlobalPlatform application"""
|
|
|
|
|
|
|
|
def __init__(self, aid):
|
|
|
|
self.aid = aid
|
|
|
|
self.lastGetData = None
|
|
|
|
|
|
|
|
def getAID(self):
|
|
|
|
return self.aid
|
|
|
|
|
|
|
|
def selectResult(self, fci, status, body):
|
|
|
|
return 'selectResult(fci=%s, status=0x%x) = %s\n' %(fci, status, body.hex())
|
|
|
|
|
|
|
|
def getData(self, fileId, bytes):
|
|
|
|
tags = {
|
|
|
|
0x42: 'Issuer Identification Number',
|
|
|
|
0x45: 'Card Image Number',
|
|
|
|
0x66: 'Card Data',
|
|
|
|
0x67: 'Card Capability Information' # ???
|
|
|
|
}
|
|
|
|
self.lastGetData = fileId
|
|
|
|
return 'getData(%s)\n' % tags.get(fileId, '<unknown 0x%x>' % fileId)
|
|
|
|
|
|
|
|
def getDataResult(self, status, body):
|
|
|
|
if self.lastGetData == 0x66:
|
|
|
|
# Card Data
|
|
|
|
pass
|
|
|
|
return 'getDataResult(0x%x) = %s\n' %(status, body.hex())
|
|
|
|
|
|
|
|
def mse(self, body):
|
|
|
|
return body.hex()
|
|
|
|
|
|
|
|
def mseResult(self, status, body):
|
|
|
|
return body.hex()
|
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
def pso(self, body):
|
|
|
|
return body.hex()
|
|
|
|
|
|
|
|
def psoResult(self, status, body):
|
|
|
|
return body.hex()
|
|
|
|
|
2024-06-18 16:38:17 +03:00
|
|
|
def getResponse(self, status, body):
|
|
|
|
return body.hex()
|
|
|
|
|
|
|
|
def getResponseResult(self, status, body):
|
|
|
|
return body.hex()
|
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
|
|
|
|
class ApplicationPIV(object):
|
2024-06-18 16:38:17 +03:00
|
|
|
"""PIV application"""
|
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
def __init__(self, aid):
|
|
|
|
self.lastGet = None
|
|
|
|
self.aid = aid
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
def getAID(self):
|
|
|
|
return self.aid
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
def selectResult(self, selectT, status, body):
|
|
|
|
ret = ''
|
|
|
|
appTag = body[0]
|
|
|
|
appLen = body[1]
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
body = body[2:2+appLen]
|
|
|
|
while len(body) > 2:
|
|
|
|
tag = body[0]
|
|
|
|
tagLen = body[1]
|
2024-06-18 16:38:17 +03:00
|
|
|
if tagLen != 1:
|
|
|
|
pass
|
2022-01-17 01:14:07 +03:00
|
|
|
if selectT == "FCI":
|
|
|
|
if tag == 0x4f:
|
|
|
|
ret += "\tpiv version: %s\n" % body[2:2 + tagLen].hex()
|
|
|
|
elif tag == 0x79:
|
2024-06-18 16:38:17 +03:00
|
|
|
subBody = body[2:2 + tagLen]
|
2022-01-17 01:14:07 +03:00
|
|
|
|
|
|
|
subTag = subBody[0]
|
|
|
|
subLen = subBody[1]
|
|
|
|
|
|
|
|
content = subBody.hex()
|
|
|
|
if subTag == 0x4f:
|
|
|
|
v = content[4:]
|
|
|
|
if v.startswith('a000000308'):
|
2024-06-18 16:38:17 +03:00
|
|
|
content = 'NIST RID'
|
|
|
|
ret += '\tCoexistent tag allocation authority: %s\n' % content
|
2022-01-17 01:14:07 +03:00
|
|
|
|
|
|
|
elif tag == 0x50:
|
2024-06-18 16:38:17 +03:00
|
|
|
ret += '\tapplication label: %s\n' % body[2:2+tagLen].decode('utf8')
|
2022-01-17 01:14:07 +03:00
|
|
|
elif tag == 0xac:
|
2024-06-18 16:38:17 +03:00
|
|
|
ret += '\tCryptographic algorithms supported: %s\n' % body[2:2+tagLen].hex()
|
2022-01-17 01:14:07 +03:00
|
|
|
else:
|
|
|
|
rety += '\tunknown tag 0x%x\n' % tag
|
|
|
|
|
|
|
|
else:
|
|
|
|
ret += "\tTODO: selectType %s\n" % selectT
|
|
|
|
|
|
|
|
body = body[2+tagLen:]
|
|
|
|
|
|
|
|
return ret
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
def getData(self, fileId, bytes):
|
2024-06-18 16:38:17 +03:00
|
|
|
ret = "\tfileId=%s(%0.4x)\n" % (FIDs.get(fileId, ""), fileId)
|
|
|
|
|
|
|
|
if len(bytes) < 7:
|
|
|
|
return ret + "\t/!\\ too short !!!!"
|
2022-01-17 01:14:07 +03:00
|
|
|
|
|
|
|
lc = bytes[4]
|
|
|
|
tag = bytes[5]
|
|
|
|
tagLen = bytes[6]
|
|
|
|
|
|
|
|
if lc == 4:
|
|
|
|
ret += "\tdoId=%0.4x\n"% (bytes[7] * 256 + bytes[8])
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
elif lc == 0xa:
|
|
|
|
keyStr = ''
|
|
|
|
# TLV
|
|
|
|
i = 7
|
|
|
|
tag = bytes[i]
|
|
|
|
tagLen = bytes[i+1]
|
|
|
|
keyRef = bytes[i+3]
|
|
|
|
keyStr = "key(tag=0x%x len=%d ref=0x%x)=" % (tag, tagLen, keyRef)
|
|
|
|
i = i + 2 + tagLen
|
|
|
|
|
|
|
|
tag = bytes[i]
|
|
|
|
tagLen = bytes[i+1]
|
|
|
|
keyStr += "value(tag=0x%x len=%d)"
|
2024-06-18 16:38:17 +03:00
|
|
|
elif lc == 3:
|
|
|
|
ret += "\tDiscovery Object\n"
|
|
|
|
elif lc == 4:
|
|
|
|
ret += "\tBiometric Information Templates (BIT) Group Template\n"
|
2022-01-17 01:14:07 +03:00
|
|
|
elif lc == 5:
|
|
|
|
if tag == 0x5C:
|
2024-06-18 16:38:17 +03:00
|
|
|
tagStr = bytes[7:10].hex()
|
2022-01-17 01:14:07 +03:00
|
|
|
ret += '\ttag: %s(%s)\n' % (tagStr, PIV_OIDs.get(tagStr, '<unknown>'))
|
|
|
|
self.lastGet = tagStr
|
|
|
|
else:
|
2024-06-18 16:38:17 +03:00
|
|
|
ret += "\tunknown key access(lc=0x%x)\n" % lc
|
2022-01-17 01:14:07 +03:00
|
|
|
|
|
|
|
return ret
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
def getDataResult(self, status, body):
|
|
|
|
ret = ''
|
|
|
|
if not len(body):
|
|
|
|
return ''
|
|
|
|
appTag = body[0]
|
|
|
|
appLen = body[1]
|
|
|
|
|
|
|
|
body = body[2:2+appLen]
|
|
|
|
while len(body) > 2:
|
|
|
|
tag = body[0]
|
|
|
|
tagLen = body[1]
|
|
|
|
tagBody = body[2:2+tagLen]
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
if self.lastGet in ('5fc102',):
|
|
|
|
# Card holder Unique Identifier
|
|
|
|
if tag == 0x30:
|
|
|
|
ret += '\tFASC-N: %s\n' % tagBody.hex()
|
|
|
|
elif tag == 0x34:
|
|
|
|
ret += '\tGUID: %s\n' % tagBody.hex()
|
|
|
|
elif tag == 0x35:
|
|
|
|
ret += '\texpirationDate: %s\n' % tagBody.decode('utf8')
|
|
|
|
elif tag == 0x3e:
|
|
|
|
ret += '\tIssuer Asymmetric Signature: %s\n' % tagBody.hex()
|
|
|
|
else:
|
|
|
|
ret += "\tunknown tag=0x%x len=%d content=%s\n" % (tag, tagLen, tagBody.hex())
|
2024-06-18 16:38:17 +03:00
|
|
|
|
|
|
|
elif self.lastGet in ('5fc107',):
|
|
|
|
# Card Capability Container
|
|
|
|
capas = {
|
|
|
|
0xf0: "Card Identifier",
|
|
|
|
0xf1: "Capability Container version number",
|
|
|
|
0xf2: "Capability Grammar version number",
|
|
|
|
0xf3: "Applications CardURL",
|
|
|
|
0xf4: "PKCS#15",
|
|
|
|
0xf5: "Registered Data Model number",
|
|
|
|
0xf6: "Access Control Rule Table",
|
|
|
|
0xf7: "Card APDUs",
|
|
|
|
0xfa: "Redirection Tag",
|
|
|
|
0xfb: "Capability Tuples (CTs)",
|
|
|
|
0xfc: "Status Tuples (STs)",
|
|
|
|
0xfd: "Next CCC",
|
|
|
|
0xe3: "Extended Application CardURL",
|
|
|
|
0xb4: "Security Object Buffer",
|
|
|
|
0xfe: "Error Detection Code"
|
|
|
|
}
|
|
|
|
|
|
|
|
if tag in capas.keys():
|
|
|
|
if tagLen:
|
|
|
|
ret += "\t%s: len=%d %s\n" % (capas[tag], tagLen, tagBody.hex())
|
|
|
|
else:
|
|
|
|
ret += "\tunknown capa tag 0x%x: %s\n" % (tag, tagBody.hex())
|
|
|
|
|
|
|
|
elif self.lastGet == '5fc105':
|
|
|
|
# X.509 Certificate for PIV Authentication
|
|
|
|
pass
|
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
else:
|
2024-06-18 16:38:17 +03:00
|
|
|
ret += "\t%s: unimplemented tag=0x%x len=%d content=%s\n" % (self.lastGet, tag, tagLen, tagBody.hex())
|
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
body = body[2+tagLen:]
|
|
|
|
|
|
|
|
return ret
|
2024-06-18 16:38:17 +03:00
|
|
|
|
|
|
|
def getResponse(self, status, body):
|
|
|
|
return body.hex()
|
|
|
|
|
|
|
|
def getResponseResult(self, status, body):
|
|
|
|
return body.hex()
|
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
def mse(self, body):
|
|
|
|
return body.hex()
|
|
|
|
|
|
|
|
def mseResult(self, status, body):
|
|
|
|
return body.hex()
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
def pso(self, body):
|
|
|
|
return body.hex()
|
|
|
|
|
|
|
|
def psoResult(self, status, body):
|
|
|
|
return body.hex()
|
|
|
|
|
2024-06-18 16:38:17 +03:00
|
|
|
def verify(self, status, body):
|
|
|
|
return "verify(%s)" % body.hex()
|
|
|
|
|
|
|
|
def verifyResult(self, status, body):
|
|
|
|
return "verify(%s)" % body.hex()
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
|
|
|
|
class ApplicationGids(object):
|
2024-06-18 16:38:17 +03:00
|
|
|
"""GIDS application"""
|
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
def __init__(self, aid):
|
|
|
|
self.aid = aid
|
|
|
|
self.lastDo = None
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
def getAID(self):
|
|
|
|
return self.aid
|
|
|
|
|
|
|
|
def parseFcp(self, bytes):
|
|
|
|
ret = ''
|
|
|
|
tag = bytes[0]
|
|
|
|
tagLen = bytes[1]
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
body = bytes[2:2+tagLen]
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
if tag == 0x62:
|
|
|
|
ret += '\tFCP\n'
|
|
|
|
|
|
|
|
while len(body) > 2:
|
|
|
|
tag2 = body[0]
|
|
|
|
tag2Len = body[1]
|
|
|
|
tag2Body = body[2:2+tag2Len]
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
if tag2 == 0x82:
|
|
|
|
ret += '\t\tFileDescriptor: %s\n' % tag2Body.hex()
|
|
|
|
elif tag2 == 0x8a:
|
|
|
|
ret += '\t\tLifeCycleByte: %s\n' % tag2Body.hex()
|
|
|
|
elif tag2 == 0x84:
|
|
|
|
ret += '\t\tDF name: %s\n' % tag2Body.encode('utf8')
|
|
|
|
elif tag2 == 0x8C:
|
|
|
|
ret += '\t\tSecurityAttributes: %s\n' % tag2Body.hex()
|
|
|
|
else:
|
|
|
|
ret += '\t\tunhandled tag=0x%x body=%s\n' % (tag2, tag2Body.hex())
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
body = body[2+tag2Len:]
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
return ret
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
def parseFci(self, bytes):
|
|
|
|
ret = ''
|
|
|
|
tag = bytes[0]
|
|
|
|
tagLen = bytes[1]
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
body = bytes[2:2+tagLen]
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
if tag == 0x61:
|
|
|
|
ret += '\tFCI\n'
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
while len(body) > 2:
|
|
|
|
tag2 = body[0]
|
|
|
|
tag2Len = body[1]
|
|
|
|
tag2Body = body[2:2+tag2Len]
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
if tag2 == 0x4F:
|
|
|
|
ret += '\t\tApplication AID: %s\n' % tag2Body.hex()
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
elif tag2 == 0x50:
|
|
|
|
ret += '\t\tApplication label: %s\n' % tag2Body.encode('utf8')
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
elif tag2 == 0x73:
|
|
|
|
body2 = tag2Body
|
|
|
|
tokens = []
|
|
|
|
while len(body2) > 2:
|
|
|
|
tag3 = body2[0]
|
|
|
|
tag3Len = body2[1]
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
if tag3 == 0x40:
|
|
|
|
v = body2[2]
|
|
|
|
if v & 0x80:
|
|
|
|
tokens.append('mutualAuthSymAlgo')
|
|
|
|
if v & 0x40:
|
|
|
|
tokens.append('extAuthSymAlgo')
|
|
|
|
if v & 0x20:
|
|
|
|
tokens.append('keyEstabIntAuthECC')
|
|
|
|
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
body2 = body2[2+tag3Len:]
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
ret += '\t\tDiscretionary data objects: %s\n' % ",".join(tokens)
|
|
|
|
else:
|
|
|
|
ret += '\t\tunhandled tag=0x%x body=%s\n' % (tag2, tag2Body.hex())
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
body = body[2+tag2Len:]
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
def selectResult(self, selectT, status, body):
|
|
|
|
if not len(body):
|
|
|
|
return ''
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
if selectT == 'FCP':
|
|
|
|
return self.parseFcp(body)
|
|
|
|
elif selectT == 'FCI':
|
|
|
|
return self.parseFci(body)
|
|
|
|
else:
|
2024-06-18 16:38:17 +03:00
|
|
|
return '\tselectResult(fci=%s, status=0x%x) = %s\n' % (selectT, status, body.hex())
|
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
def getData(self, fileId, bytes):
|
|
|
|
lc = bytes[4]
|
|
|
|
tag = bytes[5]
|
|
|
|
tagLen = bytes[6]
|
|
|
|
|
|
|
|
if tag == 0x5c:
|
|
|
|
doStr = bytes[7:7+tagLen].hex()
|
|
|
|
ret = '\tDO=%s\n' % DOs.get(doStr, "<%s>" % doStr)
|
|
|
|
self.lastDo = doStr
|
|
|
|
else:
|
|
|
|
ret = '\tunknown tag=0%x len=%d v=%s' % (tag, tagLen, bytes[7:7+tagLen].hex())
|
|
|
|
|
|
|
|
return ret
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
def getDataResult(self, status, body):
|
|
|
|
ret = ''
|
|
|
|
'''
|
|
|
|
while len(body) > 2:
|
|
|
|
tag = body[0]
|
|
|
|
tagLen = body[1]
|
|
|
|
|
|
|
|
ret += '\ttag=0x%x len=%d content=%s\n' % (tag, tagLen, body[2:2+tagLen].hex())
|
|
|
|
|
|
|
|
body = body[2+tagLen:]
|
|
|
|
'''
|
|
|
|
return ret
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
def mse(self, body):
|
|
|
|
return body.hex()
|
|
|
|
|
|
|
|
def mseResult(self, status, body):
|
|
|
|
return body.hex()
|
2024-06-18 16:38:17 +03:00
|
|
|
|
|
|
|
def getResponse(self, status, body):
|
|
|
|
return body.hex()
|
|
|
|
|
|
|
|
def getResponseResult(self, status, body):
|
|
|
|
return body.hex()
|
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
def pso(self, body):
|
|
|
|
return body.hex()
|
|
|
|
|
|
|
|
def psoResult(self, status, body):
|
|
|
|
return body.hex()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def createAppByAid(aid):
|
2024-06-18 16:38:17 +03:00
|
|
|
if aid in ("a000000308", 'a00000030800001000',):
|
2022-01-17 01:14:07 +03:00
|
|
|
return ApplicationPIV(aid)
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
elif aid in ('a00000039742544659',):
|
|
|
|
return ApplicationGids(aid)
|
|
|
|
|
2024-06-18 16:38:17 +03:00
|
|
|
elif aid in ('a0000001510000',):
|
|
|
|
return ApplicationGpc(aid)
|
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
return ApplicationDummy(aid)
|
|
|
|
|
|
|
|
|
2024-06-18 16:38:17 +03:00
|
|
|
def getErrorCode(status):
|
|
|
|
if status & 0x6100:
|
|
|
|
return "%d more bytes" % (status & 0xff)
|
|
|
|
|
|
|
|
return ERROR_CODES.get(status, "<unknown>")
|
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
fin = open(sys.argv[1], "r")
|
|
|
|
else:
|
|
|
|
fin = sys.stdin
|
|
|
|
|
|
|
|
lineno = 0
|
|
|
|
lastCmd = 0
|
|
|
|
lastSelect = None
|
|
|
|
lastSelectFCI = False
|
|
|
|
lastGetItem = None
|
|
|
|
currentApp = None
|
|
|
|
|
|
|
|
for l in fin.readlines():
|
|
|
|
lineno += 1
|
|
|
|
|
|
|
|
if not len(l):
|
|
|
|
continue
|
|
|
|
|
|
|
|
# smartcard loggers have changed
|
|
|
|
#if l.find("[DEBUG][com.freerdp.channels.smartcard.client]") == -1:
|
|
|
|
# continue
|
|
|
|
|
|
|
|
body = ''
|
|
|
|
recvKey = 'pbRecvBuffer: { '
|
|
|
|
|
|
|
|
pos = l.find(recvKey)
|
|
|
|
if pos != -1:
|
|
|
|
toCard = False
|
|
|
|
|
|
|
|
pos += len(recvKey)
|
|
|
|
pos2 = l.find(' }', pos)
|
|
|
|
if pos2 == -1:
|
|
|
|
print("line %d: invalid recvBuffer")
|
|
|
|
continue
|
|
|
|
|
|
|
|
else:
|
|
|
|
toCard = True
|
|
|
|
sendKey = 'pbSendBuffer: { '
|
|
|
|
pos = l.find(sendKey)
|
|
|
|
if pos == -1:
|
|
|
|
continue
|
|
|
|
pos += len(sendKey)
|
|
|
|
|
|
|
|
pos2 = l.find(' }', pos)
|
|
|
|
if pos2 == -1:
|
|
|
|
print("line %d: invalid sendBuffer")
|
|
|
|
continue
|
|
|
|
|
|
|
|
body = l[pos:pos2]
|
|
|
|
|
|
|
|
print(l[0:-1])
|
|
|
|
bytes = codecs.decode(body, 'hex')
|
|
|
|
if toCard:
|
|
|
|
(cla, ins, p1, p2) = bytes[0:4]
|
|
|
|
cmdName = CMD_NAMES.get(ins, "<COMMAND 0x%x>" % ins)
|
|
|
|
print(cmdName + ":")
|
|
|
|
|
|
|
|
if cmdName == "SELECT":
|
|
|
|
lc = bytes[4]
|
|
|
|
i = 5
|
|
|
|
|
|
|
|
if p1 == 0x00:
|
|
|
|
print("\tselectByFID: %0.2x%0.2x" % (bytes[i], bytes[i+1]))
|
|
|
|
i = i + lc
|
|
|
|
|
|
|
|
elif p1 == 0x4:
|
|
|
|
aid = bytes[i:i+lc].hex()
|
2024-06-18 16:38:17 +03:00
|
|
|
lastSelect = AIDs.get(aid, '<unknown %s>' % aid)
|
2022-01-17 01:14:07 +03:00
|
|
|
print("\tselectByAID: %s(%s)" % (aid, lastSelect))
|
|
|
|
|
|
|
|
if p2 == 0x00:
|
|
|
|
lastSelectT = "FCI"
|
|
|
|
print('\tFCI')
|
|
|
|
elif p2 == 0x04:
|
|
|
|
print('\tFCP')
|
|
|
|
lastSelectT = "FCP"
|
|
|
|
elif p2 == 0x08:
|
|
|
|
print('\tFMD')
|
|
|
|
lastSelectT = "FMD"
|
|
|
|
|
|
|
|
if not currentApp or currentApp.getAID() != aid:
|
|
|
|
currentApp = createAppByAid(aid)
|
|
|
|
|
|
|
|
|
|
|
|
elif cmdName == "VERIFY":
|
|
|
|
lc = bytes[4]
|
|
|
|
P2_DATA_QUALIFIER = {
|
|
|
|
0x00: "Card global password",
|
|
|
|
0x01: "RFU",
|
|
|
|
0x80: "Application password",
|
|
|
|
0x81: "Application resetting password",
|
|
|
|
0x82: "Application security status resetting code",
|
|
|
|
}
|
|
|
|
|
|
|
|
pin=''
|
|
|
|
if lc:
|
|
|
|
pin = ", pin='" + bytes[5:5+lc-2].decode('utf8)') + "'"
|
|
|
|
|
|
|
|
print("\t%s%s" % (P2_DATA_QUALIFIER.get(p2, "<unknown>"), pin))
|
|
|
|
|
|
|
|
elif cmdName == "GET DATA":
|
|
|
|
lc = bytes[4]
|
|
|
|
fileId = p1 * 256 + p2
|
|
|
|
|
|
|
|
ret = currentApp.getData(fileId, bytes)
|
|
|
|
print("%s" % ret)
|
2024-06-18 16:38:17 +03:00
|
|
|
|
|
|
|
elif cmdName == "GET RESPONSE":
|
|
|
|
#lc = bytes[4]
|
|
|
|
#fileId = p1 * 256 + p2
|
2022-01-17 01:14:07 +03:00
|
|
|
|
2024-06-18 16:38:17 +03:00
|
|
|
#ret = currentApp.getResponse(fileId, bytes)
|
|
|
|
#print("%s" % ret)
|
|
|
|
pass
|
2022-01-17 01:14:07 +03:00
|
|
|
elif cmdName == "MSE":
|
|
|
|
ret = currentApp.mse(bytes[5:5+lc])
|
|
|
|
print("%s" % ret)
|
|
|
|
|
|
|
|
elif cmdName == "PSO":
|
|
|
|
ret = currentApp.pso(bytes[5:5+lc])
|
|
|
|
print("%s" % ret)
|
|
|
|
else:
|
|
|
|
print('handle %s' % cmdName)
|
|
|
|
|
|
|
|
lastCmd = cmdName
|
|
|
|
|
|
|
|
else:
|
|
|
|
# Responses
|
2024-06-18 16:38:17 +03:00
|
|
|
if not len(bytes):
|
|
|
|
continue
|
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
status = bytes[-1] + bytes[-2] * 256
|
|
|
|
body = bytes[0:-2]
|
2024-06-18 16:38:17 +03:00
|
|
|
print("status=0x%0.4x(%s)" % (status, getErrorCode(status)))
|
2022-01-17 01:14:07 +03:00
|
|
|
|
|
|
|
if not len(body):
|
|
|
|
continue
|
2024-06-18 16:38:17 +03:00
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
ret = ''
|
|
|
|
if lastCmd == "SELECT":
|
|
|
|
ret = currentApp.selectResult(lastSelectT, status, body)
|
|
|
|
elif lastCmd == "GET DATA":
|
|
|
|
ret = currentApp.getDataResult(status, body)
|
|
|
|
elif lastCmd == "MSE":
|
|
|
|
ret = currentApp.mseResult(status, body)
|
|
|
|
elif lastCmd == "PSO":
|
|
|
|
ret = currentApp.psoResult(status, body)
|
2024-06-18 16:38:17 +03:00
|
|
|
elif lastCmd == "GET RESPONSE":
|
|
|
|
ret = currentApp.getResponseResult(status, body)
|
|
|
|
elif lastCmd == "VERIFY":
|
|
|
|
ret = currentApp.verifyResult(status, body)
|
|
|
|
|
2022-01-17 01:14:07 +03:00
|
|
|
if ret:
|
2024-06-18 16:38:17 +03:00
|
|
|
print("%s" % ret)
|