Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 129 lines (113 sloc) 3.388 kb
447c384 first commit
Adam Laurie authored
1 #!/usr/bin/python
2
3
4 # jcopsetatrhist.py - set ATR History bytes on JCOP cards
5 #
6 # The applet jcop_set_atr_hist.cap must be loaded onto the card first,
7 # and it must be installed as "default selectable" (priv mode 0x04).
8 #
9 # Adam Laurie <adam@algroup.co.uk>
10 # http://rfidiot.org/
11 #
12 # This code is copyright (c) Adam Laurie, 2008, All rights reserved.
13 # For non-commercial use only, the following terms apply - for all other
14 # uses, please contact the author:
15 #
16 # This code is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
20 #
21 # This code is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
25 #
26
271cf40 @AdamLaurie more package changes - be sure to read INSTALL for small but important d...
authored
27 import rfidiot
447c384 first commit
Adam Laurie authored
28 import sys
29 import os
30 import string
31
32 try:
271cf40 @AdamLaurie more package changes - be sure to read INSTALL for small but important d...
authored
33 card= rfidiot.card
447c384 first commit
Adam Laurie authored
34 except:
35 os._exit(True)
36
271cf40 @AdamLaurie more package changes - be sure to read INSTALL for small but important d...
authored
37 args= rfidiot.args
38 Help= rfidiot.help
447c384 first commit
Adam Laurie authored
39
40 # fixed values required by JCOP applet
41 CLA= '80'
42 P1= '00'
43 P2= '00'
44 JCOP_ATR_AID= 'DC4420060607'
45
46 if Help or len(args) < 2:
47 print '\nUsage:\n\n\t%s [OPTIONS] \'SET\' <HEX DATA>' % sys.argv[0]
48 print
49 print '\tHEX DATA is up to 15 BYTES of ASCII HEX.'
50 print
51 print '\tExample:'
52 print
53 print '\t./jcopsetatrhist.py SET 0064041101013180009000'
54 print
55 os._exit(True)
56
57 def jcop_set_atr_hist(bytes):
58 cla= CLA
59 ins= 'ATR_HIST'
60 p1= P1
61 p2= P2
62 data= '%02X' % (len(bytes) / 2) + bytes
63 lc= '%02X' % (len(data) / 2)
64 if card.send_apdu('','','','',cla,ins,p1,p2,lc,data,''):
65 return True, card.data
66 return False, card.errorcode
67
68 def select_atrhist_app():
69 "select atr_hist application (AID: DC4420060607)"
70 ins= 'SELECT_FILE'
71 p1= '04'
72 p2= '0C'
73 data= JCOP_ATR_AID
74 lc= '%02X' % (len(data) / 2)
75 card.send_apdu('','','','','',ins,p1,p2,lc,data,'')
76 if card.errorcode == card.ISO_OK:
77 return True
78 else:
79 return False
80
81 def error_exit(message,error):
82 print ' %s, error number: %s' % (message,error),
83 try:
84 print card.ISO7816ErrorCodes[error]
85 except:
86 print
87 os._exit(True)
88
89 card.info('jcopsetatrhist v0.1c')
90
91 if card.select():
92 print ' Card ID: ' + card.uid
93 if card.readertype == card.READER_PCSC:
94 print ' ATR: ' + card.pcsc_atr
95 else:
96 print ' No card present'
97 os._exit(True)
98
99 # high speed select required for ACG
100 if not card.hsselect('08'):
101 print ' Could not select card for APDU processing'
102 os._exit(True)
103
104 if not select_atrhist_app():
105 print
106 print " Can't select atrhist applet!"
107 print ' Please load jcop_set_atr_hist.cap onto JCOP card.'
108 print ' (Use command: gpshell java/jcop_set_atr_hist.gpsh)'
109 print
110 os._exit(True)
111
112 if args[0] == 'SET':
113 stat, data= jcop_set_atr_hist(args[1])
114 if not stat:
115 error_exit('Set hist bytes failed', data)
116 else:
117 print
118 print ' ATR History Bytes (ATS) set to', args[1]
119 print
120 print ' *** Remove card from reader and replace to finalise!'
121 print
122 print ' You can now delete jcop_set_atr_hist.cap from the JCOP card.'
123 print ' (Use command: gpshell java/jcop_delete_atr_hist.gpsh)'
124 print
125 os._exit(False)
126 else:
127 print "Unrecognised command:", args[0]
128 os._exit(True)
Something went wrong with that request. Please try again.