gnuk/tool/gnuk_put_binary_libusb.py

113 lines
3.5 KiB
Python
Raw Normal View History

2011-01-28 08:38:52 +00:00
#! /usr/bin/python
"""
gnuk_put_binary.py - a tool to put binary to Gnuk Token
2012-06-19 01:19:26 +00:00
This tool is for importing certificate, writing serial number, etc.
2011-01-28 08:38:52 +00:00
2012-06-01 04:20:47 +00:00
Copyright (C) 2011, 2012 Free Software Initiative of Japan
2011-01-28 08:38:52 +00:00
Author: NIIBE Yutaka <gniibe@fsij.org>
This file is a part of Gnuk, a GnuPG USB Token implementation.
Gnuk is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gnuk is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from struct import *
2011-02-09 05:06:57 +00:00
import sys, time, os, binascii, string
2012-12-19 01:44:50 +00:00
from gnuk_token import *
2011-01-28 08:38:52 +00:00
# INPUT: binary file
# Assume only single CCID device is attached to computer, and it's Gnuk Token
2012-06-01 04:20:47 +00:00
DEFAULT_PW3 = "12345678"
BY_ADMIN = 3
def main(fileid, is_update, data, passwd):
2012-12-19 01:44:50 +00:00
gnuk = None
2012-06-01 04:20:47 +00:00
for (dev, config, intf) in gnuk_devices():
try:
2012-12-19 01:44:50 +00:00
gnuk = gnuk_token(dev, config, intf)
2012-06-01 04:20:47 +00:00
print "Device: ", dev.filename
print "Configuration: ", config.value
print "Interface: ", intf.interfaceNumber
break
except:
pass
2012-12-19 01:44:50 +00:00
if gnuk.icc_get_status() == 2:
2011-01-28 08:38:52 +00:00
raise ValueError, "No ICC present"
2012-12-19 01:44:50 +00:00
elif gnuk.icc_get_status() == 1:
gnuk.icc_power_on()
gnuk.cmd_verify(BY_ADMIN, passwd)
gnuk.cmd_write_binary(fileid, data, is_update)
gnuk.cmd_select_openpgp()
2012-06-01 04:20:47 +00:00
if fileid == 0:
2012-12-19 01:44:50 +00:00
data_in_device = gnuk.cmd_get_data(0x00, 0x4f)
2012-06-01 04:20:47 +00:00
for d in data_in_device:
2012-12-19 01:44:50 +00:00
print "%02x" % ord(d),
2012-06-01 04:20:47 +00:00
print
2012-12-19 02:19:17 +00:00
compare(data + '\x00\x00', data_in_device[8:])
2012-06-01 04:20:47 +00:00
elif fileid >= 1 and fileid <= 4:
2012-12-19 01:44:50 +00:00
data_in_device = gnuk.cmd_read_binary(fileid)
2012-06-01 04:20:47 +00:00
compare(data, data_in_device)
else:
2012-12-19 01:44:50 +00:00
data_in_device = gnuk.cmd_get_data(0x7f, 0x21)
2011-02-09 05:06:57 +00:00
compare(data, data_in_device)
2012-12-19 01:44:50 +00:00
gnuk.icc_power_off()
2011-01-28 08:38:52 +00:00
return 0
if __name__ == '__main__':
2012-06-01 04:20:47 +00:00
passwd = DEFAULT_PW3
if sys.argv[1] == '-p':
from getpass import getpass
passwd = getpass("Admin password: ")
sys.argv.pop(1)
if sys.argv[1] == '-u':
is_update = True
sys.argv.pop(1)
else:
is_update = False
if sys.argv[1] == '-s':
2012-05-17 03:15:24 +00:00
fileid = 0 # serial number
filename = sys.argv[2]
f = open(filename)
2011-02-09 05:06:57 +00:00
email = os.environ['EMAIL']
serial_data_hex = None
for line in f.readlines():
field = string.split(line)
2011-02-09 05:06:57 +00:00
if field[0] == email:
serial_data_hex = field[1].replace(':','')
f.close()
if not serial_data_hex:
print "No serial number"
2011-02-09 05:06:57 +00:00
exit(1)
print "Writing serial number"
data = binascii.unhexlify(serial_data_hex)
2012-06-01 04:20:47 +00:00
elif sys.argv[1] == '-k': # firmware update key
keyno = sys.argv[2]
fileid = 1 + int(keyno)
filename = sys.argv[3]
f = open(filename)
data = f.read()
f.close()
2011-02-07 02:57:27 +00:00
else:
2012-05-31 03:10:08 +00:00
fileid = 5 # Card holder certificate
2011-02-09 05:06:57 +00:00
filename = sys.argv[1]
f = open(filename)
data = f.read()
f.close()
print "%s: %d" % (filename, len(data))
2011-02-07 02:57:27 +00:00
print "Updating card holder certificate"
2012-06-01 04:20:47 +00:00
main(fileid, is_update, data, passwd)