more python3 fix

This commit is contained in:
NIIBE Yutaka 2015-08-03 19:22:02 +09:00
parent 94d6208542
commit 0fb6853fc7
8 changed files with 62 additions and 62 deletions

View File

@ -39,25 +39,25 @@ def main(fileid, is_update, data, passwd):
for (dev, config, intf) in gnuk_devices():
try:
gnuk = gnuk_token(dev, config, intf)
print "Device: ", dev.filename
print "Configuration: ", config.value
print "Interface: ", intf.interfaceNumber
print("Device: %s" % dev.filename)
print("Configuration: %d" % config.value)
print("Interface: %d" % intf.interfaceNumber)
break
except:
pass
if gnuk.icc_get_status() == 2:
raise ValueError, "No ICC present"
raise ValueError("No ICC present")
elif gnuk.icc_get_status() == 1:
gnuk.icc_power_on()
gnuk.cmd_verify(BY_ADMIN, passwd)
gnuk.cmd_verify(BY_ADMIN, passwd.encode('UTF-8'))
gnuk.cmd_write_binary(fileid, data, is_update)
gnuk.cmd_select_openpgp()
if fileid == 0:
data_in_device = gnuk.cmd_get_data(0x00, 0x4f)
for d in data_in_device:
print "%02x" % ord(d),
print
compare(data + '\x00\x00', data_in_device[8:])
print("%02x" % d, end=' ')
print()
compare(data + b'\x00\x00', data_in_device[8:])
elif fileid >= 1 and fileid <= 4:
data_in_device = gnuk.cmd_read_binary(fileid)
compare(data, data_in_device)
@ -90,23 +90,23 @@ if __name__ == '__main__':
serial_data_hex = field[1].replace(':','')
f.close()
if not serial_data_hex:
print "No serial number"
print("No serial number")
exit(1)
print "Writing serial number"
print("Writing serial number")
data = binascii.unhexlify(serial_data_hex)
elif sys.argv[1] == '-k': # firmware update key
keyno = sys.argv[2]
fileid = 1 + int(keyno)
filename = sys.argv[3]
f = open(filename)
f = open(filename, "rb")
data = f.read()
f.close()
else:
fileid = 5 # Card holder certificate
filename = sys.argv[1]
f = open(filename)
f = open(filename, "rb")
data = f.read()
f.close()
print "%s: %d" % (filename, len(data))
print "Updating card holder certificate"
print("%s: %d" % (filename, len(data)))
print("Updating card holder certificate")
main(fileid, is_update, data, passwd)

View File

@ -36,17 +36,17 @@ def main(passwd):
for (dev, config, intf) in gnuk_devices():
try:
gnuk = gnuk_token(dev, config, intf)
print "Device: ", dev.filename
print "Configuration: ", config.value
print "Interface: ", intf.interfaceNumber
print("Device: %s" % dev.filename)
print("Configuration: %d" % config.value)
print("Interface: %d" % intf.interfaceNumber)
break
except:
pass
if gnuk.icc_get_status() == 2:
raise ValueError, "No ICC present"
raise ValueError("No ICC present")
elif gnuk.icc_get_status() == 1:
gnuk.icc_power_on()
gnuk.cmd_verify(BY_ADMIN, passwd)
gnuk.cmd_verify(BY_ADMIN, passwd.encode('UTF-8'))
gnuk.cmd_select_openpgp()
gnuk.cmd_put_data_remove(0x00, 0xc7) # FP_SIG
gnuk.cmd_put_data_remove(0x00, 0xce) # KGTIME_SIG

View File

@ -1,7 +1,8 @@
"""
gnuk_token.py - a library for Gnuk Token
Copyright (C) 2011, 2012, 2013 Free Software Initiative of Japan
Copyright (C) 2011, 2012, 2013, 2015
Free Software Initiative of Japan
Author: NIIBE Yutaka <gniibe@fsij.org>
This file is a part of Gnuk, a GnuPG USB Token implementation.
@ -23,6 +24,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
from struct import *
import string, binascii
import usb, time
from array import array
# USB class, subclass, protocol
CCID_CLASS = 0x0B
@ -50,9 +52,6 @@ def iso7816_compose(ins, p1, p2, data, cls=0x00, le=None):
return pack('>BBBBB', cls, ins, p1, p2, data_len) \
+ data + pack('>B', le)
def list_to_string(l):
return string.join([chr(c) for c in l], '')
# This class only supports Gnuk (for now)
class gnuk_token(object):
def __init__(self, device, configuration, interface):
@ -152,10 +151,11 @@ class gnuk_token(object):
timeout = 10)
def icc_get_result(self):
msg = self.__devhandle.bulkRead(self.__bulkin, 1024, self.__timeout)
if len(msg) < 10:
print(msg)
usbmsg = self.__devhandle.bulkRead(self.__bulkin, 1024, self.__timeout)
if len(usbmsg) < 10:
print(usbmsg)
raise ValueError("icc_get_result")
msg = array('B', usbmsg)
msg_type = msg[0]
data_len = msg[1] + (msg[2]<<8) + (msg[3]<<16) + (msg[4]<<24)
slot = msg[5]
@ -168,7 +168,7 @@ class gnuk_token(object):
return (status, chain, data)
def icc_get_status(self):
msg = icc_compose(0x65, 0, 0, self.__seq, 0, "")
msg = icc_compose(0x65, 0, 0, self.__seq, 0, b"")
self.__devhandle.bulkWrite(self.__bulkout, msg, self.__timeout)
self.increment_seq()
status, chain, data = self.icc_get_result()
@ -176,16 +176,16 @@ class gnuk_token(object):
return status
def icc_power_on(self):
msg = icc_compose(0x62, 0, 0, self.__seq, 0, "")
msg = icc_compose(0x62, 0, 0, self.__seq, 0, b"")
self.__devhandle.bulkWrite(self.__bulkout, msg, self.__timeout)
self.increment_seq()
status, chain, data = self.icc_get_result()
# XXX: check status, chain
self.atr = list_to_string(data) # ATR
self.atr = data
return self.atr
def icc_power_off(self):
msg = icc_compose(0x63, 0, 0, self.__seq, 0, "")
msg = icc_compose(0x63, 0, 0, self.__seq, 0, b"")
self.__devhandle.bulkWrite(self.__bulkout, msg, self.__timeout)
self.increment_seq()
status, chain, data = self.icc_get_result()
@ -207,7 +207,7 @@ class gnuk_token(object):
elif chain == 1:
d = data_rcv
while True:
msg = icc_compose(0x6f, 0, 0, self.__seq, 0x10, "")
msg = icc_compose(0x6f, 0, 0, self.__seq, 0x10, b"")
self.__devhandle.bulkWrite(self.__bulkout, msg, self.__timeout)
self.increment_seq()
status, chain, data_rcv = self.icc_get_result()
@ -224,14 +224,14 @@ class gnuk_token(object):
raise ValueError("icc_send_cmd")
def cmd_get_response(self, expected_len):
result = []
result = array('B')
while True:
cmd_data = iso7816_compose(0xc0, 0x00, 0x00, '') + pack('>B', expected_len)
cmd_data = iso7816_compose(0xc0, 0x00, 0x00, b'') + pack('>B', expected_len)
response = self.icc_send_cmd(cmd_data)
result += response[:-2]
sw = response[-2:]
if sw[0] == 0x90 and sw[1] == 0x00:
return list_to_string(result)
return result
elif sw[0] != 0x61:
raise ValueError("%02x%02x" % (sw[0], sw[1]))
else:
@ -247,7 +247,7 @@ class gnuk_token(object):
return True
def cmd_read_binary(self, fileid):
cmd_data = iso7816_compose(0xb0, 0x80+fileid, 0x00, '')
cmd_data = iso7816_compose(0xb0, 0x80+fileid, 0x00, b'')
sw = self.icc_send_cmd(cmd_data)
if len(sw) != 2:
raise ValueError(sw)
@ -291,7 +291,7 @@ class gnuk_token(object):
count += 1
def cmd_select_openpgp(self):
cmd_data = iso7816_compose(0xa4, 0x04, 0x0c, "\xD2\x76\x00\x01\x24\x01")
cmd_data = iso7816_compose(0xa4, 0x04, 0x0c, b"\xD2\x76\x00\x01\x24\x01")
sw = self.icc_send_cmd(cmd_data)
if len(sw) != 2:
raise ValueError, sw
@ -300,7 +300,7 @@ class gnuk_token(object):
return True
def cmd_get_data(self, tagh, tagl):
cmd_data = iso7816_compose(0xca, tagh, tagl, "")
cmd_data = iso7816_compose(0xca, tagh, tagl, b"")
sw = self.icc_send_cmd(cmd_data)
if len(sw) != 2:
raise ValueError, sw
@ -391,11 +391,11 @@ class gnuk_token(object):
def cmd_genkey(self, keyno):
if keyno == 1:
data = '\xb6\x00'
data = b'\xb6\x00'
elif keyno == 2:
data = '\xb8\x00'
data = b'\xb8\x00'
else:
data = '\xa4\x00'
data = b'\xa4\x00'
cmd_data = iso7816_compose(0x47, 0x80, 0, data)
sw = self.icc_send_cmd(cmd_data)
if len(sw) != 2:
@ -409,11 +409,11 @@ class gnuk_token(object):
def cmd_get_public_key(self, keyno):
if keyno == 1:
data = '\xb6\x00'
data = b'\xb6\x00'
elif keyno == 2:
data = '\xb8\x00'
data = b'\xb8\x00'
else:
data = '\xa4\x00'
data = b'\xa4\x00'
cmd_data = iso7816_compose(0x47, 0x81, 0, data)
sw = self.icc_send_cmd(cmd_data)
if len(sw) != 2:
@ -424,19 +424,19 @@ class gnuk_token(object):
return (pk[9:9+256], pk[9+256+2:9+256+2+3])
def cmd_put_data_remove(self, tagh, tagl):
cmd_data = iso7816_compose(0xda, tagh, tagl, "")
cmd_data = iso7816_compose(0xda, tagh, tagl, b"")
sw = self.icc_send_cmd(cmd_data)
if sw[0] != 0x90 and sw[1] != 0x00:
raise ValueError, ("%02x%02x" % (sw[0], sw[1]))
def cmd_put_data_key_import_remove(self, keyno):
if keyno == 1:
keyspec = "\xb6\x00" # SIG
keyspec = b"\xb6\x00" # SIG
elif keyno == 2:
keyspec = "\xb8\x00" # DEC
keyspec = b"\xb8\x00" # DEC
else:
keyspec = "\xa4\x00" # AUT
cmd_data = iso7816_compose(0xdb, 0x3f, 0xff, "\x4d\x02" + keyspec)
keyspec = b"\xa4\x00" # AUT
cmd_data = iso7816_compose(0xdb, 0x3f, 0xff, b"\x4d\x02" + keyspec)
sw = self.icc_send_cmd(cmd_data)
if sw[0] != 0x90 and sw[1] != 0x00:
raise ValueError, ("%02x%02x" % (sw[0], sw[1]))

View File

@ -3,7 +3,7 @@
"""
gnuk_upgrade.py - a tool to upgrade firmware of Gnuk Token
Copyright (C) 2012, 2015 Free Software Initiative of Japan
Copyright (C) 2012, 2015 Free Software Initiative of Japan
Author: NIIBE Yutaka <gniibe@fsij.org>
This file is a part of Gnuk, a GnuPG USB Token implementation.

View File

@ -1,7 +1,7 @@
"""
gpg_agent.py - a library to connect gpg-agent
Copyright (C) 2013, 2015 Free Software Initiative of Japan
Copyright (C) 2013, 2015 Free Software Initiative of Japan
Author: NIIBE Yutaka <gniibe@fsij.org>
This file is a part of Gnuk, a GnuPG USB Token implementation.

View File

@ -36,7 +36,7 @@ class intel_hex(object):
memory[addr] = data
prev_addr = addr
prev_data_len = len(data)
self.memory = memory
self.memory = memory
def calc_checksum(self, byte_count, offset, type_code, data):
s = byte_count
@ -57,19 +57,19 @@ class intel_hex(object):
except:
pass
else:
raise ValueError, "data overwritten (%d)" % self.lineno
raise ValueError("data overwritten (%d)" % self.lineno)
self.memory[address] = data
def parse_line(self, line):
if line[0] != ':':
raise ValueError, "invalid line (%d)" % self.lineno
raise ValueError("invalid line (%d)" % self.lineno)
count = int(line[1:3], 16)
offset = int(line[3:7], 16)
type_code = int(line[7:9], 16)
data = binascii.unhexlify(line[9:(9+count*2)])
check_sum = int(line[(9+count*2):], 16)
if check_sum != self.calc_checksum(count, offset, type_code, data):
raise ValueError, "invalid checksum (%d)" % self.lineno
raise ValueError("invalid checksum (%d)" % self.lineno)
if type_code == 0x00:
self.add_data(count, offset, data)
return 0
@ -77,18 +77,18 @@ class intel_hex(object):
return 1
elif type_code == 0x04:
if count != 2:
raise ValueError, "invalid count (%d): (%d) Expected 2" \
% (self.lineno, count)
raise ValueError("invalid count (%d): (%d) Expected 2" \
% (self.lineno, count))
self.address = ((ord(data[0])&0xff)<<24) + ((ord(data[1])&0xff)<<16)
return 0
elif type_code == 0x05:
if count != 4:
raise ValueError, "invalid count (%d): (%d) Expected 4" \
% (self.lineno, count)
raise ValueError("invalid count (%d): (%d) Expected 4" \
% (self.lineno, count))
self.start_address \
= ((ord(data[0])&0xff)<<24) + ((ord(data[1])&0xff)<<16) \
+ ((ord(data[2])&0xff)<<8) + ((ord(data[3])&0xff))
return 0
else:
raise ValueError, "invalid type code (%d): (%d)" \
% (self.lineno, type_code)
raise ValueError("invalid type code (%d): (%d)" \
% (self.lineno, type_code))

View File

@ -3,7 +3,7 @@
"""
stlinkv2.py - a tool to control ST-Link/V2
Copyright (C) 2012, 2013, 2015 Free Software Initiative of Japan
Copyright (C) 2012, 2013, 2015 Free Software Initiative of Japan
Author: NIIBE Yutaka <gniibe@fsij.org>
This file is a part of Gnuk, a GnuPG USB Token implementation.

View File

@ -44,7 +44,7 @@ def main(keyno, passwd, data_regnual, data_upgrade):
rsa_raw_pubkey = rsa.get_raw_pubkey(rsa_key)
gnuk = get_gnuk_device()
gnuk.cmd_verify(BY_ADMIN, passwd)
gnuk.cmd_verify(BY_ADMIN, passwd.encode('UTF-8'))
gnuk.cmd_write_binary(1+keyno, rsa_raw_pubkey, False)
gnuk.cmd_select_openpgp()