From 7ef417ae36f5d476a95ea740d0b4baa31731f04d Mon Sep 17 00:00:00 2001 From: NIIBE Yutaka Date: Wed, 1 Feb 2017 12:34:35 +0900 Subject: [PATCH] tool: Improve tool/*.py. -- Szczepan Zalega's idea of using the file GNUK_USB_DEVICE_ID would good, but not merged yet. Because it makes difficult to distribute the scripts. We need to consider installing tools and the file like GNUK_USB_DEVICE_ID altogether. Signed-off-by: NIIBE Yutaka --- ChangeLog | 11 +++++++++++ THANKS | 1 + tool/gnuk_token.py | 23 ++++++++++++++--------- tool/upgrade_by_passwd.py | 7 ++++++- tool/usb_strings.py | 18 ++---------------- 5 files changed, 34 insertions(+), 26 deletions(-) diff --git a/ChangeLog b/ChangeLog index 74e9e29..e777c33 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2017-02-01 NIIBE Yutaka + + * tool/upgrade_by_passwd.py (main): More verbose messages + suggested by Szczepan Zalega . + + * tool/gnuk_token.py (USB_PRODUCT_LIST): New. + (gnuk_devices_by_vidpid): Support searching by USB_PRODUCT_LIST. + Thanks to Szczepan Zalega . + + * tool/usb_strings.py: Use gnuk_token.py. + 2016-10-21 Niibe Yutaka * src/ecc.c (check_secret): Fix condition. diff --git a/THANKS b/THANKS index 5d912c1..56a18b7 100644 --- a/THANKS +++ b/THANKS @@ -32,6 +32,7 @@ NOKUBI Takatsugu knok@daionet.gr.jp Paul Bakker polarssl_maintainer@polarssl.org Santiago Ruano Rincón santiago@debian.org Shane Coughlan scoughlan@openinventionnetwork.com +Szczepan Zalega szczepan@nitrokey.com Vasily Evseenko Werner Koch wk@gnupg.org Yuji Imai ug@xcast.jp diff --git a/tool/gnuk_token.py b/tool/gnuk_token.py index c4ffac7..6d46b9c 100644 --- a/tool/gnuk_token.py +++ b/tool/gnuk_token.py @@ -1,7 +1,7 @@ """ gnuk_token.py - a library for Gnuk Token -Copyright (C) 2011, 2012, 2013, 2015 +Copyright (C) 2011, 2012, 2013, 2015, 2017 Free Software Initiative of Japan Author: NIIBE Yutaka @@ -26,6 +26,12 @@ import binascii import usb, time from array import array +# Possible Gnuk Token products +USB_PRODUCT_LIST=[ + { 'vendor' : 0x234b, 'product' : 0x0000 }, # FSIJ Gnuk Token + { 'vendor' : 0x20a0, 'product' : 0x4211 }, # Nitrokey Start +] + # USB class, subclass, protocol CCID_CLASS = 0x0B CCID_SUBCLASS = 0x00 @@ -586,19 +592,18 @@ def gnuk_devices(): alt.interfaceProtocol == CCID_PROTOCOL_0: yield dev, config, alt -USB_VENDOR_FSIJ=0x234b -USB_PRODUCT_GNUK=0x0000 - def gnuk_devices_by_vidpid(): busses = usb.busses() for bus in busses: devices = bus.devices for dev in devices: - if dev.idVendor != USB_VENDOR_FSIJ: - continue - if dev.idProduct != USB_PRODUCT_GNUK: - continue - yield dev + for cand in USB_PRODUCT_LIST: + if dev.idVendor != cand['vendor']: + continue + if dev.idProduct != cand['product']: + continue + yield dev + break def get_gnuk_device(): icc = None diff --git a/tool/upgrade_by_passwd.py b/tool/upgrade_by_passwd.py index c8876bd..18d9b46 100755 --- a/tool/upgrade_by_passwd.py +++ b/tool/upgrade_by_passwd.py @@ -69,8 +69,9 @@ def main(wait_e, keyno, passwd, data_regnual, data_upgrade): gnuk = None # reg = None + print("Waiting for device to appear:") while reg == None: - print("Wait %d seconds..." % wait_e) + print(" Wait %d seconds..." % wait_e) time.sleep(wait_e) for dev in gnuk_devices_by_vidpid(): try: @@ -84,9 +85,13 @@ def main(wait_e, keyno, passwd, data_regnual, data_upgrade): print("%08x:%08x" % mem_info) print("Downloading the program") reg.download(mem_info[0], data_upgrade) + print("Protecting device") reg.protect() + print("Finish flashing") reg.finish() + print("Resetting device") reg.reset_device() + print("Update procedure finished") return 0 from getpass import getpass diff --git a/tool/usb_strings.py b/tool/usb_strings.py index 41defed..a8c3748 100755 --- a/tool/usb_strings.py +++ b/tool/usb_strings.py @@ -3,7 +3,7 @@ """ usb_strings.py - a tool to dump USB string -Copyright (C) 2012, 2015 Free Software Initiative of Japan +Copyright (C) 2012, 2015, 2017 Free Software Initiative of Japan Author: NIIBE Yutaka This file is a part of Gnuk, a GnuPG USB Token implementation. @@ -22,28 +22,14 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . """ +from gnuk_token import * import usb, sys -USB_VENDOR_FSIJ=0x234b -USB_PRODUCT_GNUK=0x0000 - -def gnuk_devices_by_vidpid(): - busses = usb.busses() - for bus in busses: - devices = bus.devices - for dev in devices: - if dev.idVendor != USB_VENDOR_FSIJ: - continue - if dev.idProduct != USB_PRODUCT_GNUK: - continue - yield dev - field = ['', 'Vendor', 'Product', 'Serial', 'Revision', 'Config', 'Sys', 'Board'] def main(n): for dev in gnuk_devices_by_vidpid(): handle = dev.open() - print("Device: %s" % dev.filename) try: for i in range(1,n): s = handle.getString(i, 512)