adding to gnuk.py
This commit is contained in:
@@ -1,3 +1,9 @@
|
|||||||
|
2012-06-28 Niibe Yutaka <gniibe@fsij.org>
|
||||||
|
|
||||||
|
* test/gnuk.py (gnuk_token): New method: increment_seq.
|
||||||
|
(icc_send_cmd): Handle timeout.
|
||||||
|
(cmd_genkey): New.
|
||||||
|
|
||||||
2012-06-27 Niibe Yutaka <gniibe@fsij.org>
|
2012-06-27 Niibe Yutaka <gniibe@fsij.org>
|
||||||
|
|
||||||
* test/features/101_decryption.feature: New.
|
* test/features/101_decryption.feature: New.
|
||||||
|
|||||||
33
test/gnuk.py
33
test/gnuk.py
@@ -77,6 +77,9 @@ class gnuk_token(object):
|
|||||||
self.__timeout = 10000
|
self.__timeout = 10000
|
||||||
self.__seq = 0
|
self.__seq = 0
|
||||||
|
|
||||||
|
def increment_seq(self):
|
||||||
|
self.__seq = (self.__seq + 1) & 0xff
|
||||||
|
|
||||||
def reset_device(self):
|
def reset_device(self):
|
||||||
try:
|
try:
|
||||||
self.__devhandle.reset()
|
self.__devhandle.reset()
|
||||||
@@ -105,7 +108,7 @@ class gnuk_token(object):
|
|||||||
def icc_get_status(self):
|
def icc_get_status(self):
|
||||||
msg = icc_compose(0x65, 0, 0, self.__seq, 0, "")
|
msg = icc_compose(0x65, 0, 0, self.__seq, 0, "")
|
||||||
self.__devhandle.bulkWrite(self.__bulkout, msg, self.__timeout)
|
self.__devhandle.bulkWrite(self.__bulkout, msg, self.__timeout)
|
||||||
self.__seq += 1
|
self.increment_seq()
|
||||||
status, chain, data = self.icc_get_result()
|
status, chain, data = self.icc_get_result()
|
||||||
# XXX: check chain, data
|
# XXX: check chain, data
|
||||||
return status
|
return status
|
||||||
@@ -113,7 +116,7 @@ class gnuk_token(object):
|
|||||||
def icc_power_on(self):
|
def icc_power_on(self):
|
||||||
msg = icc_compose(0x62, 0, 0, self.__seq, 0, "")
|
msg = icc_compose(0x62, 0, 0, self.__seq, 0, "")
|
||||||
self.__devhandle.bulkWrite(self.__bulkout, msg, self.__timeout)
|
self.__devhandle.bulkWrite(self.__bulkout, msg, self.__timeout)
|
||||||
self.__seq += 1
|
self.increment_seq()
|
||||||
status, chain, data = self.icc_get_result()
|
status, chain, data = self.icc_get_result()
|
||||||
# XXX: check status, chain
|
# XXX: check status, chain
|
||||||
self.atr = list_to_string(data) # ATR
|
self.atr = list_to_string(data) # ATR
|
||||||
@@ -121,7 +124,7 @@ class gnuk_token(object):
|
|||||||
def icc_power_off(self):
|
def icc_power_off(self):
|
||||||
msg = icc_compose(0x63, 0, 0, self.__seq, 0, "")
|
msg = icc_compose(0x63, 0, 0, self.__seq, 0, "")
|
||||||
self.__devhandle.bulkWrite(self.__bulkout, msg, self.__timeout)
|
self.__devhandle.bulkWrite(self.__bulkout, msg, self.__timeout)
|
||||||
self.__seq += 1
|
self.increment_seq()
|
||||||
status, chain, data = self.icc_get_result()
|
status, chain, data = self.icc_get_result()
|
||||||
# XXX: check chain, data
|
# XXX: check chain, data
|
||||||
return status
|
return status
|
||||||
@@ -129,19 +132,21 @@ class gnuk_token(object):
|
|||||||
def icc_send_data_block(self, data):
|
def icc_send_data_block(self, data):
|
||||||
msg = icc_compose(0x6f, len(data), 0, self.__seq, 0, data)
|
msg = icc_compose(0x6f, len(data), 0, self.__seq, 0, data)
|
||||||
self.__devhandle.bulkWrite(self.__bulkout, msg, self.__timeout)
|
self.__devhandle.bulkWrite(self.__bulkout, msg, self.__timeout)
|
||||||
self.__seq += 1
|
self.increment_seq()
|
||||||
return self.icc_get_result()
|
return self.icc_get_result()
|
||||||
|
|
||||||
def icc_send_cmd(self, data):
|
def icc_send_cmd(self, data):
|
||||||
status, chain, data_rcv = self.icc_send_data_block(data)
|
status, chain, data_rcv = self.icc_send_data_block(data)
|
||||||
if chain == 0:
|
if chain == 0:
|
||||||
|
while status == 0x80:
|
||||||
|
status, chain, data_rcv = self.icc_get_result()
|
||||||
return data_rcv
|
return data_rcv
|
||||||
elif chain == 1:
|
elif chain == 1:
|
||||||
d = data_rcv
|
d = data_rcv
|
||||||
while True:
|
while True:
|
||||||
msg = icc_compose(0x6f, 0, 0, self.__seq, 0x10, "")
|
msg = icc_compose(0x6f, 0, 0, self.__seq, 0x10, "")
|
||||||
self.__devhandle.bulkWrite(self.__bulkout, msg, self.__timeout)
|
self.__devhandle.bulkWrite(self.__bulkout, msg, self.__timeout)
|
||||||
self.__seq += 1
|
self.increment_seq()
|
||||||
status, chain, data_rcv = self.icc_get_result()
|
status, chain, data_rcv = self.icc_get_result()
|
||||||
# XXX: check status
|
# XXX: check status
|
||||||
d += data_rcv
|
d += data_rcv
|
||||||
@@ -321,6 +326,24 @@ class gnuk_token(object):
|
|||||||
raise ValueError("%02x%02x" % (sw[0], sw[1]))
|
raise ValueError("%02x%02x" % (sw[0], sw[1]))
|
||||||
return self.cmd_get_response(sw[1])
|
return self.cmd_get_response(sw[1])
|
||||||
|
|
||||||
|
def cmd_genkey(self, keyno):
|
||||||
|
if keyno == 1:
|
||||||
|
data = '\xb6\x00'
|
||||||
|
elif keyno == 2:
|
||||||
|
data = '\xb8\x00'
|
||||||
|
else:
|
||||||
|
data = '\xa4\x00'
|
||||||
|
cmd_data = iso7816_compose(0x47, 0x80, 0, data)
|
||||||
|
sw = self.icc_send_cmd(cmd_data)
|
||||||
|
if len(sw) != 2:
|
||||||
|
raise ValueError(sw)
|
||||||
|
if sw[0] == 0x90 and sw[1] == 0x00:
|
||||||
|
return ""
|
||||||
|
elif sw[0] != 0x61:
|
||||||
|
raise ValueError("%02x%02x" % (sw[0], sw[1]))
|
||||||
|
pk = self.cmd_get_response(sw[1])
|
||||||
|
return (pk[9:9+256], pk[9+256+2:9+256+2+3])
|
||||||
|
|
||||||
|
|
||||||
def compare(data_original, data_in_device):
|
def compare(data_original, data_in_device):
|
||||||
i = 0
|
i = 0
|
||||||
|
|||||||
Reference in New Issue
Block a user