From 4d15700580eb992c365f2b2c9351c0dfc71acc81 Mon Sep 17 00:00:00 2001 From: NIIBE Yutaka Date: Fri, 19 Feb 2021 13:12:48 +0900 Subject: [PATCH] Python3 update things. Signed-off-by: NIIBE Yutaka --- ChangeLog | 9 +++++++++ tool/gnuk_get_random.py | 2 +- tool/gnuk_put_binary_libusb.py | 6 +++--- tool/gnuk_remove_keys_libusb.py | 6 +++--- tool/gnuk_upgrade.py | 30 ++++++++++++++++-------------- tool/upgrade_by_passwd.py | 8 ++++---- 6 files changed, 36 insertions(+), 25 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2eb4241..aca1420 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,14 @@ 2021-02-19 NIIBE Yutaka + * tool/upgrade_by_passwd.py (main): Use tobytes. + * tool/gnuk_get_random.py: Likewise. + * tool/gnuk_remove_keys_libusb.py (main): Likewise. + Switch to Python3. + * tool/gnuk_put_binary_libusb.py (main): Use tobytes. + Switch to Python3. + * tool/gnuk_upgrade.py (main): Use tobytes. + Switch to Python3. + * chopstx: Update to 1.19. 2021-02-18 Vincent Pelletier diff --git a/tool/gnuk_get_random.py b/tool/gnuk_get_random.py index c140ee4..5229ab0 100755 --- a/tool/gnuk_get_random.py +++ b/tool/gnuk_get_random.py @@ -11,7 +11,7 @@ if __name__ == '__main__': looping = (len(sys.argv) > 1) while True: try: - challenge = gnuk.cmd_get_challenge().tostring() + challenge = gnuk.cmd_get_challenge().tobytes() except Exception as e: print(count) raise e diff --git a/tool/gnuk_put_binary_libusb.py b/tool/gnuk_put_binary_libusb.py index 5363615..f2c66ea 100755 --- a/tool/gnuk_put_binary_libusb.py +++ b/tool/gnuk_put_binary_libusb.py @@ -1,10 +1,10 @@ -#! /usr/bin/python +#! /usr/bin/python3 """ gnuk_put_binary.py - a tool to put binary to Gnuk Token This tool is for importing certificate, writing serial number, etc. -Copyright (C) 2011, 2012 Free Software Initiative of Japan +Copyright (C) 2011, 2012, 2021 Free Software Initiative of Japan Author: NIIBE Yutaka This file is a part of Gnuk, a GnuPG USB Token implementation. @@ -56,7 +56,7 @@ def main(fileid, is_update, data, passwd): if fileid == 0: data_in_device = gnuk.cmd_get_data(0x00, 0x4f) print(' '.join([ "%02x" % d for d in data_in_device ])) - compare(data + b'\x00\x00', data_in_device[8:].tostring()) + compare(data + b'\x00\x00', data_in_device[8:].tobytes()) elif fileid >= 1 and fileid <= 4: data_in_device = gnuk.cmd_read_binary(fileid) compare(data, data_in_device) diff --git a/tool/gnuk_remove_keys_libusb.py b/tool/gnuk_remove_keys_libusb.py index 185bdca..277461e 100755 --- a/tool/gnuk_remove_keys_libusb.py +++ b/tool/gnuk_remove_keys_libusb.py @@ -1,9 +1,9 @@ -#! /usr/bin/python +#! /usr/bin/python3 """ gnuk_remove_keys_libusb.py - a tool to remove keys in Gnuk Token -Copyright (C) 2012, 2018 Free Software Initiative of Japan +Copyright (C) 2012, 2018, 2021 Free Software Initiative of Japan Author: NIIBE Yutaka This file is a part of Gnuk, a GnuPG USB Token implementation. @@ -51,7 +51,7 @@ def main(passwd): gnuk.icc_power_on() gnuk.cmd_select_openpgp() # Compute passwd data - kdf_data = gnuk.cmd_get_data(0x00, 0xf9).tostring() + kdf_data = gnuk.cmd_get_data(0x00, 0xf9).tobytes() if kdf_data == b"": passwd_data = passwd.encode('UTF-8') else: diff --git a/tool/gnuk_upgrade.py b/tool/gnuk_upgrade.py index 09d9e23..02dab0e 100755 --- a/tool/gnuk_upgrade.py +++ b/tool/gnuk_upgrade.py @@ -1,9 +1,9 @@ -#! /usr/bin/python +#! /usr/bin/python3 """ gnuk_upgrade.py - a tool to upgrade firmware of Gnuk Token -Copyright (C) 2012, 2015 Free Software Initiative of Japan +Copyright (C) 2012, 2015, 2021 Free Software Initiative of Japan Author: NIIBE Yutaka This file is a part of Gnuk, a GnuPG USB Token implementation. @@ -70,7 +70,7 @@ def gpg_sign(keygrip, hash): raise ValueError(binascii.hexlify(signed)) return signed -def main(keyno,keygrip, data_regnual, data_upgrade): +def main(keyno, keygrip, data_regnual, data_upgrade): l = len(data_regnual) if (l & 0x03) != 0: data_regnual = data_regnual.ljust(l + 4 - (l & 0x03), b'\x00') @@ -91,7 +91,7 @@ def main(keyno,keygrip, data_regnual, data_upgrade): elif icc.icc_get_status() == 1: icc.icc_power_on() icc.cmd_select_openpgp() - challenge = icc.cmd_get_challenge().tostring() + challenge = icc.cmd_get_challenge().tobytes() signed = gpg_sign(keygrip, binascii.hexlify(challenge)) icc.cmd_external_authenticate(keyno, signed) icc.stop_gnuk() @@ -107,17 +107,19 @@ def main(keyno,keygrip, data_regnual, data_upgrade): del icc icc = None # - print("Wait 3 seconds...") - time.sleep(3) - # Then, send upgrade program... reg = None - for dev in gnuk_devices_by_vidpid(): - try: - reg = regnual(dev) - print("Device: %d" % dev.filename) - break - except: - pass + print("Waiting for device to appear:") + while reg == None: + print(" Wait {} second{}...".format(wait_e, 's' if wait_e > 1 else '')) + time.sleep(wait_e) + for dev in gnuk_devices_by_vidpid(): + try: + reg = regnual(dev) + print("Device: %s" % dev.filename) + break + except: + pass + # Then, send upgrade program... mem_info = reg.mem_info() print("%08x:%08x" % mem_info) print("Downloading the program") diff --git a/tool/upgrade_by_passwd.py b/tool/upgrade_by_passwd.py index fce7812..814d220 100755 --- a/tool/upgrade_by_passwd.py +++ b/tool/upgrade_by_passwd.py @@ -1,10 +1,10 @@ -#! /usr/bin/python +#! /usr/bin/python3 """ upgrade_by_passwd.py - a tool to install another firmware for Gnuk Token which is just shipped from factory -Copyright (C) 2012, 2013, 2015, 2018 +Copyright (C) 2012, 2013, 2015, 2018, 2021 Free Software Initiative of Japan Author: NIIBE Yutaka @@ -52,7 +52,7 @@ def main(wait_e, keyno, passwd, data_regnual, data_upgrade): gnuk.cmd_select_openpgp() # Compute passwd data try: - kdf_data = gnuk.cmd_get_data(0x00, 0xf9).tostring() + kdf_data = gnuk.cmd_get_data(0x00, 0xf9).tobytes() except: kdf_data = b"" if kdf_data == b"": @@ -70,7 +70,7 @@ def main(wait_e, keyno, passwd, data_regnual, data_upgrade): gnuk.cmd_write_binary(1+keyno, rsa_raw_pubkey, False) gnuk.cmd_select_openpgp() - challenge = gnuk.cmd_get_challenge().tostring() + challenge = gnuk.cmd_get_challenge().tobytes() digestinfo = binascii.unhexlify(SHA256_OID_PREFIX) + challenge signed = rsa.compute_signature(rsa_key, digestinfo) signed_bytes = rsa.integer_to_bytes_256(signed)