def setCiphering(self): try: self.initIso7816() KSenc = hexRepToBin(self.customKencForm.get()) KSmac = hexRepToBin(self.customKmacForm.get()) ssc = hexRepToBin(self.customSscForm.get()) sm = securemessaging.SecureMessaging(KSenc, KSmac, ssc) self._iso7816.setCiphering(sm) self.writeToLog("CIPHERING SET:\n{0}".format(sm)) except Exception, msg: tkMessageBox.showerror("Error: Set ciphering", str(msg))
def sscGenerator(self): rnd_icc = hexRepToBin(self.field1Form.get()) rnd_ifd = hexRepToBin(self.field2Form.get()) ssc = rnd_icc[-4:] + rnd_ifd[-4:] self.writeToLog("SSC GENERATOR:\n RND ICC: {0}\n RND IFD: {1}\n SSC: {2}".format(self.field1Form.get(), self.field2Form.get(), binToHexRep(ssc))) self.field1Form.delete(0, END) self.field2Form.delete(0, END) self.field1Form.insert(0, binToHexRep(ssc))
def sscGenerator(self): rnd_icc = hexRepToBin(self.field1Form.get()) rnd_ifd = hexRepToBin(self.field2Form.get()) ssc = rnd_icc[-4:] + rnd_ifd[-4:] self.writeToLog( "SSC GENERATOR:\n RND ICC: {0}\n RND IFD: {1}\n SSC: {2}". format(self.field1Form.get(), self.field2Form.get(), binToHexRep(ssc))) self.field1Form.delete(0, END) self.field2Form.delete(0, END) self.field1Form.insert(0, binToHexRep(ssc))
def createMAC(self): try: m = iso9797.mac(hexRepToBin(self.field2Form.get()), iso9797.pad(hexRepToBin(self.field1Form.get()))) self.writeToLog("MAC:\n message: {0}\n Key: {1}\n MAC: {2}".format(self.field1Form.get(), self.field2Form.get(), binToHexRep(m))) self.field1Form.delete(0, END) self.field2Form.delete(0, END) self.field1Form.insert(0, binToHexRep(m)) except Exception, msg: tkMessageBox.showerror("Error: BAC", str(msg))
def createMAC(self): try: m = iso9797.mac(hexRepToBin(self.field2Form.get()), iso9797.pad(hexRepToBin(self.field1Form.get()))) self.writeToLog( "MAC:\n message: {0}\n Key: {1}\n MAC: {2}".format( self.field1Form.get(), self.field2Form.get(), binToHexRep(m))) self.field1Form.delete(0, END) self.field2Form.delete(0, END) self.field1Form.insert(0, binToHexRep(m)) except Exception, msg: tkMessageBox.showerror("Error: BAC", str(msg))
def tdesDecrypt(self): try: tdes= DES3.new(hexRepToBin(self.field2Form.get()), DES.MODE_CBC, b'\x00\x00\x00\x00\x00\x00\x00\x00') m = tdes.decrypt(hexRepToBin(self.field1Form.get())) self.writeToLog("TDES DECRYPTION:\n cipher: {0}\n key: {1}\n message: {2}".format(self.field1Form.get(), self.field2Form.get(), binToHexRep(m))) self.field1Form.delete(0, END) self.field2Form.delete(0, END) self.field1Form.insert(0, binToHexRep(m)) except Exception, msg: tkMessageBox.showerror("Error: BAC", str(msg))
def tdesDecrypt(self): try: tdes = DES3.new(hexRepToBin(self.field2Form.get()), DES.MODE_CBC, b'\x00\x00\x00\x00\x00\x00\x00\x00') m = tdes.decrypt(hexRepToBin(self.field1Form.get())) self.writeToLog( "TDES DECRYPTION:\n cipher: {0}\n key: {1}\n message: {2}". format(self.field1Form.get(), self.field2Form.get(), binToHexRep(m))) self.field1Form.delete(0, END) self.field2Form.delete(0, END) self.field1Form.insert(0, binToHexRep(m)) except Exception, msg: tkMessageBox.showerror("Error: BAC", str(msg))
def readHeader(self): try: header = hexRepToBin(self.field1Form.get()) (bodySize, offset) = asn1.asn1Length(header[1:]) bodySize = hexToHexRep(bodySize) offset = hexToHexRep(offset + 1) self.writeToLog("HEADER:\n Body size: {0}\n Offset: {1}".format( bodySize, offset)) self.field1Form.delete(0, END) self.field2Form.delete(0, END) self.field1Form.insert(0, bodySize) self.field2Form.insert(0, offset) except Exception, msg: tkMessageBox.showerror("Error: Read header", str(msg))
def readHeader(self): try: header = hexRepToBin(self.field1Form.get()) (bodySize, offset) = asn1.asn1Length(header[1:]) bodySize = hexToHexRep(bodySize) offset = hexToHexRep(offset+1) self.writeToLog("HEADER:\n Body size: {0}\n Offset: {1}".format(bodySize, offset)) self.field1Form.delete(0, END) self.field2Form.delete(0, END) self.field1Form.insert(0, bodySize) self.field2Form.insert(0, offset) except Exception, msg: tkMessageBox.showerror("Error: Read header", str(msg))
def keyDerivation(self): keyBin = hexRepToBin(self.field1Form.get()) h = sha1(str(keyBin)).digest() Ka = h[:8] Kb = h[8:16] Ka = self.DESParity(Ka) Kb = self.DESParity(Kb) key = binToHexRep(Ka+Kb) self.writeToLog("KEY DERIVATION:\n key: {0}\n derived key: {1}".format(self.field1Form.get(), key)) self.field1Form.delete(0, END) self.field2Form.delete(0, END) self.field1Form.insert(0, key)
def keyDerivation(self): keyBin = hexRepToBin(self.field1Form.get()) h = sha1(str(keyBin)).digest() Ka = h[:8] Kb = h[8:16] Ka = self.DESParity(Ka) Kb = self.DESParity(Kb) key = binToHexRep(Ka + Kb) self.writeToLog( "KEY DERIVATION:\n key: {0}\n derived key: {1}".format( self.field1Form.get(), key)) self.field1Form.delete(0, END) self.field2Form.delete(0, END) self.field1Form.insert(0, key)
def generate( firstname, surname, sex, dob, nationality, id_doc, doe, issuer, face_path, country, organisation, pob, middle_name, issuing_auth, doi, height, eyes, address, update = True, cap_path = None): if firstname == "": firstname = "John" if surname == "": surname = "Doe" if sex == "": sex = "M" if dob == "YYYY/MM/DD": dob = "1970/01/01" if nationality == "": nationality = "BEL" if id_doc == "": id_doc = "EH123456" if doe == "YYYY/MM/DD": doe = datetime.date.today().strftime("%Y/%m/%d") if issuer == "": issuer = "BEL" if face_path == "": face_path = "epassportviewer/ressources/face.jpg" if country == "": country = "BEL" if organisation == "": organisation = "UCL" if doi == "YYYY/MM/DD": doi = None date_cmp = doe.split("/") doe = datetime.date(int(date_cmp[0]), int(date_cmp[1]), int(date_cmp[2])) date_cmp = dob.split("/") dob = datetime.date(int(date_cmp[0]), int(date_cmp[1]), int(date_cmp[2])) ################### # CA # ################### # Create a fake Country Signer Certification Authority CSCA = pki.DistinguishedName(C=country[:2], O=organisation, CN="CSCA") # Create a fake Document Signer bond to CSCA DS = pki.DistinguishedName(C=country[:2], O=organisation, CN="Document-Signer") ca = pki.CA() (csca, cscaKey) = ca.createCSCA(1024, 720, CSCA) (ds, dsKey) = ca.createDS(1024, 365, DS) ################### # DATAGROUP # ################### # Create DataGroup 1 (MRZ) [mandatory] dg1 = datagroupcreation.DataGroup1Creation().create("P", issuer, surname, firstname, nationality, sex, id_doc, dob.strftime("%d%m%y"), doe.strftime("%d%m%y")) # Create DataGroup 2 (Face) [Mandatory] dg2 = datagroupcreation.DataGroup2Creation().create(face_path) dgs = [dg1, dg2] # Create DataGroup 11 (Additional document holder information) [Optional] if pob or middle_name: dgc = datagroupcreation.DataGroupFileCreation(converter.toTAG("DG11")) content = "" # Full name if middle_name: middle_name = middle_name.replace(' ', '<') full_name = surname.upper() + "<<" + firstname.upper() + middle_name.upper() dgc.addDataObject("5F02", full_name) # Place of birth if pob: pob = pob.replace(' ', '<') dgc.addDataObject("5F11", pob) content += "5F11" dgc.addDataObject("5C", hexfunctions.hexRepToBin("5F11")) dg11 = datagroup.DataGroupFactory().create(dgc) dgs.append(dg11) # Create DataGroup 12 (Additional document information) [Optional] if issuing_auth or doi: dgc = datagroupcreation.DataGroupFileCreation(converter.toTAG("DG12")) content = "" # Issuing authority if issuing_auth: dgc.addDataObject("5F19", issuing_auth) content += "5F19" # Date of issue if doi: date_cmp = doi.split("/") doi = datetime.date(int(date_cmp[0]), int(date_cmp[1]), int(date_cmp[2])) dgc.addDataObject("5F26", doi.strftime("%Y%m%d")) content += "5F26" dgc.addDataObject("5C", hexfunctions.hexRepToBin(content)) dg12 = datagroup.DataGroupFactory().create(dgc) dgs.append(dg12) # Create DataGroup 13 (Reserved for national specific data) [Optional] if height or eyes or address: print "DG13" dgc = datagroupcreation.DataGroupFileCreation(converter.toTAG("DG13")) content = "" # Height if height: dgc.addDataObject("9F01", height) content += "9F01" # Eyes if eyes: dgc.addDataObject("9F02", eyes) content += "9F02" # Address if address: dgc.addDataObject("9F03", address) content += "9F03" dgc.addDataObject("5C", hexfunctions.hexRepToBin(content)) dg13 = datagroup.DataGroupFactory().create(dgc) dgs.append(dg13) # Create presence map [mandatory] com = datagroupcreation.ComCreation().create(dgs) # Create a signer data strcture [mandatory] sod = datagroupcreation.SODCreation().create(ds, dsKey, dgs) dgs.append(com) dgs.append(sod) ################### # JCOP # ################### # Initializing the GPlatform object with the reader #1 if not update: jc = jcop.GPlatform(reader_nb) jc.install(cap_path) r = readerAbstract.waitForCard() jcw = jcop.JavaCardWritter(r) # Write the forged ePassport in the JCOP for dg in dgs: jcw.writeDG(dg) jcw.setKseed(dg1)