def selectNull(self): self._comm.rstConnectionRaw() try: toSend = apdu.CommandAPDU("00", "A4", "00", "00", "", "", "FF") return binToHexRep(self._comm.transmit(toSend, "Select File")) except Iso7816Exception, msg: return "SW1:{} SW2:{}".format(msg[1], msg[2])
def sendGetChallengeNull(self): self._comm.rstConnection() try: toSend = apdu.CommandAPDU("00", "84", "00", "00", "", "", "01") return (True, binToHexRep(self._comm.transmit(toSend, "Get Challenge"))) except Iso7816Exception, msg: (data, sw1, sw2) = msg return (False, "SW1:{} SW2:{}".format(sw1, sw2))
def _sendCmdData(self, cmd_data): """ @note: Code fragment from the pyPassport.doc9303.bac.BAC class """ data = binToHexRep(cmd_data) lc = hexToHexRep(len(data)/2) toSend = apdu.CommandAPDU("00", "82", "00", "00", lc, data, "28") return self._iso7816.transmit(toSend, "Mutual Authentication")
def getErrorsMessage(self): test = ["44", "82", "84", "88", "A4", "B0", "B1"] errors = dict() for ins in test: self._comm.rstConnection() try: toSend = apdu.CommandAPDU("00", ins, "00", "00", "", "", "00") self._comm.transmit(toSend, "Select File") errors[ins] = "SW1:{} SW2:{}".format(144, 0) except Iso7816Exception, msg: (data, sw1, sw2) = msg errors[ins] = "SW1:{} SW2:{}".format(sw1, sw2)
def sendCustom(self, cla="00", ins="00", p1="00", p2="00", lc="", data="", le="00"): """ Send custom APDU in order to trigger errors. @param cla, ins, p1, p2, lc, data, le: APDU value @type cla, ins, p1, p2, lc, data, le: String of 2hex (from 00 to FF) except lc and date that may be an empty String @return: A set composed of boolean (True=Succeed, False=Error) and a passport answer """ toSend = apdu.CommandAPDU(cla, ins, p1, p2, lc, data, le) try: self.log("Send APDU: {0}:{1}:{2}:{3}:{4}:{5}:{6}".format(cla, ins, p1, p2, lc, data, le)) ans = self._iso7816.transmit(toSend, "Custom APDU") return (True, binToHexRep(ans)) except Iso7816Exception, msg: return (False, msg)
def _sendPair(self, cmd_data="\x55" * 40): """Send a message/MAC. If the cmd_data is not set, it sends a random pair in order to make sure the MAC check fails If set, a wrong message is sent together with a valid MAC in order to pass the MAC check @param cmd_data: pair to send @type cmd_data: a string of the raw data to send @return: The response time together with error message """ self._iso7816.getChallenge() data = binToHexRep(cmd_data) self.log("Send a message with a wrong MAC") self.log("Message/MAC:" + data) lc = hexToHexRep(len(data) / 2) toSend = apdu.CommandAPDU("00", "82", "00", "00", lc, data, "28") starttime = time.time() try: response = self._iso7816.transmit(toSend, "Wrong MAC") except Iso7816Exception, msg: response = msg
def send(self): try: self.initIso7816() cla = self.customCLAForm.get() ins = self.customINSForm.get() p1 = self.customP1Form.get() p2 = self.customP2Form.get() lc = self.customLCForm.get() data = self.customDATAForm.get() le = self.customLEForm.get() if not lc: if data: lc = hexToHexRep(len(data) / 2) toSend = apdu.CommandAPDU(cla, ins, p1, p2, lc, data, le) ans = self._iso7816.transmitRaw(toSend) rep = binToHexRep(ans.res) sw1 = ans.sw1 sw2 = ans.sw2 self.writeToLog( "REQUEST:\n APDU: CLA:{0} INS:{1} P1:{2} P2:{3} LC:{4} DATA:{5} LE:{6}" .format(cla, ins, p1, p2, lc, data, le)) self.writeToLog( "RESPONSE:\n APDU:\n Data:{0}\n SW1:{1}\n SW2:{2}". format(rep, hex(sw1), hex(sw2))) self.customRespDataForm.delete(0, END) self.customRespDataForm.insert(0, rep) self.customSW1Form.delete(0, END) self.customSW1Form.insert(0, sw1) self.customSW2Form.delete(0, END) self.customSW2Form.insert(0, sw2) except Exception, msg: tkMessageBox.showerror("Error: Send", str(msg))
def _mutualAuthentication(self, cmd_data): data = binToHexRep(cmd_data) lc = hexToHexRep(len(data) / 2) toSend = apdu.CommandAPDU("00", "82", "00", "00", lc, data, "28") return self._iso7816.transmit(toSend, "Mutual Authentication")
def internalAuthentication(self, rnd_ifd): data = binToHexRep(rnd_ifd) lc = hexToHexRep(int(len(data) / 2)) toSend = apdu.CommandAPDU("00", "88", "00", "00", lc, data, "00") res = self.transmit(toSend, "Internal Authentication") return res
def getChallenge(self): toSend = apdu.CommandAPDU("00", "84", "00", "00", "", "", "08") return self.transmit(toSend, "Get Challenge")
def updateBinary(self, offset, data, cla="00", ins="D6"): os = "%04x" % int(offset) data = binToHexRep(data) lc = hexToHexRep(len(data) / 2) toSend = apdu.CommandAPDU(cla, ins, os[0:2], os[2:4], lc, data, "") return self.transmit(toSend, "Update Binary")
def readBinary(self, offset, nbOfByte): os = "%04x" % int(offset) toSend = apdu.CommandAPDU("00", "B0", os[0:2], os[2:4], "", "", hexToHexRep(nbOfByte)) return self.transmit(toSend, "Read Binary")
def selectFile(self, p1, p2, file="", cla="00", ins="A4"): lc = hexToHexRep(int(len(file) / 2)) toSend = apdu.CommandAPDU(cla, ins, p1, p2, lc, file, "") return self.transmit(toSend, "Select File")
def getUID(self): toSend = apdu.CommandAPDU("FF", "CA", "00", "00", "", "", "00") return self.transmit(toSend, "Get UID")