📄 perso_example.py
字号:
### <Description># An example of a card personalisation:# - install 3 files:# . photo file is public# . info file is public# . secret is protected by authentication## - go through the all the personalisation process:# . install psk key# . lock the init phase# . install administrative key# . create some files# . lock the perso phase# ### </Description>### <Init>import sys, osimport structsys.path.append( os.path.join( "..", "..", "..", "inkit", "jayapy" ) )sys.path.append( os.path.join( "..", "..", "..", "inkit", "spy" ) )from script_player import *### </Init>### <Script>DF_DEMO_FID = "3000"DF_DEMO_SFI = 00EF_PHOTO_FID = "3001" EF_PHOTO_SIZE = 4000EF_PHOTO_SFI = 01EF_PHOTO_ACS = 0xFFFFEF_INFO_FID = "3002"EF_INFO_SFI = 02#EF_INFO_REC_SIZE = 30#EF_INFO_REC_NB = 10EF_INFO_SIZE = 700EF_INFO_ACS = 0xFFFFEF_SECRET_FID = "3003" EF_SECRET_SIZE = 700EF_SECRET_SFI = 03EF_SECRET_ACS = 0xFFFFuser_info = [ "1", "22", "333", "4444", "55555" ]secret_info = " ".join(map( str, range(100)))file_photo_name = "photo.bin"file_photo_size = 512def s( *params, **kwparams ): apply( sp.send, params, kwparams )def ascii_lst2str( ascii_lst ): """Converts a list of bytes into a string. Each character of the string has an ascii value corresponding to the one of the byte of the string""" return "".join( map( lambda el : struct.pack( "B", el ), ascii_lst ) )def str2ascii_lst( s ): """Convert a string into a list of bytes (ascii values of the characters)""" return map( lambda c: struct.unpack("B", c )[0], s )def readFileContentAsBinary( fname ): """This reads a file on the computer disk and return its content as a list of bytes. We need this function because python only allow to read file content as strings""" return str2ascii_lst( open( fname ).read() )def createPhotoFileOnDisk( fname=file_photo_name, size=file_photo_size ): """This create a file on the disk with the given size. We use it to simulate a photo""" f = open( fname, "w" ) n = size / 10 while n: f.write( "0123456789" ) n -= 1 f.write( "0123456789"[ : size % 10 ] ) f.close()def reportListDifference( name1, list1, name2, list2 ): if list1 == list2: return print "%s : len=%d" % (name1, len(list1) ) print "%s : len=%d" % (name2, len(list2) ) offset = 0 offset_inc = 20 while( offset < max( len( list1), len( list2) ) ): list1_chunk = list1[offset:offset+offset_inc] list2_chunk = list2[offset:offset+offset_inc] if list1_chunk != list2_chunk: print "%10s %2d-%2d: %s " % (name1, offset, offset+offset_inc, hex2str( list1_chunk) ) print "%10s %2d-%2d: %s " % (name2, offset, offset+offset_inc, hex2str( list2_chunk) ) else: print "chunk %d-%d matches" % (offset, offset+offset_inc) offset += offset_inc raise AssertionError( "%s and %s are not the same" % (name1, name2) )def create_files( sp, auth ): """Create the photo file, the secret info file and the info file on the card and update their content""" sp.ExternalAuthenticate( auth ) sp.CreateDF( fid=DF_DEMO_FID, sfi=DF_DEMO_SFI, acs=0xFFFF) sp.CreateTransparentFile( fid=EF_PHOTO_FID, sfi=EF_PHOTO_SFI, acs=EF_PHOTO_ACS, size=EF_PHOTO_SIZE) sp.CreateTransparentFile( fid=EF_INFO_FID, sfi=EF_INFO_SFI, acs=EF_INFO_ACS, size=EF_INFO_SIZE) sp.CreateTransparentFile( fid=EF_SECRET_FID, sfi=EF_SECRET_SFI, acs=EF_SECRET_ACS, size=EF_SECRET_SIZE)def update_files( sp ): sp.Select( abs_path = "3F00"+DF_DEMO_FID ) sp.UpdateWholeTransparentFile( fid=EF_PHOTO_FID, content = readFileContentAsBinary(file_photo_name) ) # update user_info file sp.UpdateWholeTransparentFile( fid=EF_SECRET_FID, content = secret_info )def init_card( sp ): print "============= Init Card (bootstrap fs) ================" sp.InitCard()def perso_card( sp ): print "============= Perso Card ================" create_files( sp, "PSK" ) sp.PersoCard() # check photo file sp.Select( abs_path="3F00" + DF_DEMO_FID + EF_PHOTO_FID ) photo_content = sp.ReadWholeTransparentFile( size=file_photo_size ) expected_content = readFileContentAsBinary( file_photo_name ) reportListDifference( "Photo on card", photo_content, "Photo", expected_content ) # check secret information file sp.Select( abs_path="3F00" + DF_DEMO_FID + EF_SECRET_FID ) secret_content = sp.ReadWholeTransparentFile( size=len(secret_info) ) reportListDifference( "Secret on card", secret_content, "Secret info", secret_info )def full_perso( sp ): sp.powerOn() init_card( sp ) perso_card( sp ) # XXX check that card is in phase appli # try to create a file without authentication # check that one is rejected # verify pin # verify wrong pin # try to change pin # verify pin # verify wrong pin # change admin key # verify new admin key # erase files # create them again### </Script>### <Main>if __name__ == "__main__": jlog_disable_component( "ContactlessBinder" ) jlog_disable_component( "SimuReader" ) jlog_disable_component( "Rd T=CL" ) jlog_disable_component( "Apdu" ) sp = ScriptPlayer(None, 1) full_perso( sp ) stop_simulator()### </Main>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -