Add pubkey tests

This commit is contained in:
NIIBE Yutaka
2016-10-15 16:55:22 +09:00
parent ffa9bf1f94
commit 4de605ed63
4 changed files with 46 additions and 7 deletions

View File

@@ -278,14 +278,23 @@ class OpenPGP_Card(object):
data = b'\xb8\x00' data = b'\xb8\x00'
else: else:
data = b'\xa4\x00' data = b'\xa4\x00'
if self.__reader.is_tpdu_reader():
cmd_data = iso7816_compose(0x47, 0x81, 0, data, le=512)
r = self.__reader.send_cmd(cmd_data)
else:
cmd_data = iso7816_compose(0x47, 0x81, 0, data) cmd_data = iso7816_compose(0x47, 0x81, 0, data)
sw = self.__reader.send_cmd(cmd_data) r = self.__reader.send_cmd(cmd_data)
if len(sw) != 2: if len(r) < 2:
raise ValueError(sw) raise ValueError(r)
elif sw[0] != 0x61: sw = r[-2:]
raise ValueError("%02x%02x" % (sw[0], sw[1])) r = r[0:-2]
if sw[0] == 0x61:
pk = self.cmd_get_response(sw[1]) pk = self.cmd_get_response(sw[1])
return (pk[9:9+256], pk[9+256+2:9+256+2+3]) elif sw[0] == 0x90 and sw[1] == 0x00:
pk = r
else:
raise ValueError("%02x%02x" % (sw[0], sw[1]))
return pk
def cmd_put_data_remove(self, tagh, tagl): def cmd_put_data_remove(self, tagh, tagl):
cmd_data = iso7816_compose(0xda, tagh, tagl, b"") cmd_data = iso7816_compose(0xda, tagh, tagl, b"")

View File

@@ -24,6 +24,7 @@ from binascii import hexlify
from re import match, DOTALL from re import match, DOTALL
from struct import pack from struct import pack
from util import * from util import *
import pytest
EMPTY_60=bytes(60) EMPTY_60=bytes(60)
@@ -152,6 +153,21 @@ def test_algorithm_attributes_3(card):
a = get_data_object(card, 0xc3) a = get_data_object(card, 0xc3)
assert a == None or a == b'\x01\x08\x00\x00\x20\x00' assert a == None or a == b'\x01\x08\x00\x00\x20\x00'
def test_public_key_1(card):
with pytest.raises(Exception) as excinfo:
pk = card.cmd_get_public_key(1)
assert excinfo.value.args[0] == "6a88"
def test_public_key_2(card):
with pytest.raises(Exception) as excinfo:
pk = card.cmd_get_public_key(2)
assert excinfo.value.args[0] == "6a88"
def test_public_key_3(card):
with pytest.raises(Exception) as excinfo:
pk = card.cmd_get_public_key(3)
assert excinfo.value.args[0] == "6a88"
def test_AID(card): def test_AID(card):
a = get_data_object(card, 0x4f) a = get_data_object(card, 0x4f)
print() print()

View File

@@ -136,6 +136,19 @@ def test_timestamp_3_put(card):
r = card.cmd_put_data(0x00, 0xd0, timestamp3) r = card.cmd_put_data(0x00, 0xd0, timestamp3)
assert r assert r
def test_public_key_1(card):
pk = card.cmd_get_public_key(1)
assert rsa_keys.key[0][0] == pk[9:9+256]
def test_public_key_2(card):
pk = card.cmd_get_public_key(2)
assert rsa_keys.key[1][0] == pk[9:9+256]
def test_public_key_3(card):
pk = card.cmd_get_public_key(3)
assert rsa_keys.key[2][0] == pk[9:9+256]
def test_setup_pw1_0(card): def test_setup_pw1_0(card):
r = card.cmd_change_reference_data(1, FACTORY_PASSPHRASE_PW1 + PW1_TEST0) r = card.cmd_change_reference_data(1, FACTORY_PASSPHRASE_PW1 + PW1_TEST0)
assert r assert r

View File

@@ -54,6 +54,7 @@ def test_sex_put(card):
# Gnuk # Gnuk
r = card.cmd_put_data(0x5f, 0x35, b"") r = card.cmd_put_data(0x5f, 0x35, b"")
except ValueError: except ValueError:
# OpenPGP card which doesn't allow b""
r = card.cmd_put_data(0x5f, 0x35, b"9") r = card.cmd_put_data(0x5f, 0x35, b"9")
assert r assert r