4 upgrade_by_passwd.py - a tool to install another firmware for Gnuk Token
5 which is just shipped from factory
7 Copyright (C) 2012, 2013, 2015 Free Software Initiative of Japan
8 Author: NIIBE Yutaka <gniibe@fsij.org>
10 This file is a part of Gnuk, a GnuPG USB Token implementation.
12 Gnuk is free software: you can redistribute it and/or modify it
13 under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
17 Gnuk is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
20 License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 from gnuk_token import *
27 import sys, binascii, time, os
30 DEFAULT_PW3 = "12345678"
35 def main(wait_e, keyno, passwd, data_regnual, data_upgrade):
38 data_regnual = data_regnual.ljust(l + 4 - (l & 0x03), chr(0))
39 crc32code = crc32(data_regnual)
40 print("CRC32: %04x\n" % crc32code)
41 data_regnual += pack('<I', crc32code)
43 rsa_key = rsa.read_key_from_file('rsa_example.key')
44 rsa_raw_pubkey = rsa.get_raw_pubkey(rsa_key)
46 gnuk = get_gnuk_device()
47 gnuk.cmd_select_openpgp()
48 gnuk.cmd_verify(BY_ADMIN, passwd.encode('UTF-8'))
49 gnuk.cmd_write_binary(1+keyno, rsa_raw_pubkey, False)
51 gnuk.cmd_select_openpgp()
52 challenge = gnuk.cmd_get_challenge().tostring()
53 digestinfo = binascii.unhexlify(SHA256_OID_PREFIX) + challenge
54 signed = rsa.compute_signature(rsa_key, digestinfo)
55 signed_bytes = rsa.integer_to_bytes_256(signed)
56 gnuk.cmd_external_authenticate(keyno, signed_bytes)
58 mem_info = gnuk.mem_info()
59 print("%08x:%08x" % mem_info)
61 print("Downloading flash upgrade program...")
62 gnuk.download(mem_info[0], data_regnual)
63 print("Run flash upgrade program...")
64 gnuk.execute(mem_info[0] + len(data_regnual) - 4)
73 print("Wait %d seconds..." % wait_e)
75 for dev in gnuk_devices_by_vidpid():
78 print("Device: %s" % dev.filename)
82 # Then, send upgrade program...
83 mem_info = reg.mem_info()
84 print("%08x:%08x" % mem_info)
85 print("Downloading the program")
86 reg.download(mem_info[0], data_upgrade)
92 from getpass import getpass
94 # This should be event driven, not guessing some period, or polling.
95 DEFAULT_WAIT_FOR_REENUMERATION=1
97 if __name__ == '__main__':
98 if os.getcwd() != os.path.dirname(os.path.abspath(__file__)):
99 print("Please change working directory to: %s" % os.path.dirname(os.path.abspath(__file__)))
104 wait_e = DEFAULT_WAIT_FOR_REENUMERATION
105 while len(sys.argv) > 3:
108 if option == '-f': # F for Factory setting
110 elif option == '-e': # E for Enumeration
111 wait_e = int(sys.argv[1])
113 elif option == '-k': # K for Key number
114 keyno = int(sys.argv[1])
117 raise ValueError("unknown option", option)
119 passwd = getpass("Admin password: ")
120 filename_regnual = sys.argv[1]
121 filename_upgrade = sys.argv[2]
122 f = open(filename_regnual,"rb")
123 data_regnual = f.read()
125 print("%s: %d" % (filename_regnual, len(data_regnual)))
126 f = open(filename_upgrade,"rb")
127 data_upgrade = f.read()
129 print("%s: %d" % (filename_upgrade, len(data_upgrade)))
130 # First 4096-byte in data_upgrade is SYS, so, skip it.
131 main(wait_e, keyno, passwd, data_regnual, data_upgrade[4096:])