Add pubkey tests
authorNIIBE Yutaka <gniibe@fsij.org>
Sat, 15 Oct 2016 07:55:22 +0000 (16:55 +0900)
committerNIIBE Yutaka <gniibe@fsij.org>
Sat, 15 Oct 2016 07:55:22 +0000 (16:55 +0900)
tests/openpgp_card.py
tests/test_empty_card.py
tests/test_personalize_card.py
tests/test_personalize_reset_card.py

index d92fd83..1b63b29 100644 (file)
@@ -278,14 +278,23 @@ class OpenPGP_Card(object):
             data = b'\xb8\x00'
         else:
             data = b'\xa4\x00'
-        cmd_data = iso7816_compose(0x47, 0x81, 0, data)
-        sw = self.__reader.send_cmd(cmd_data)
-        if len(sw) != 2:
-            raise ValueError(sw)
-        elif sw[0] != 0x61:
+        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)
+            r = self.__reader.send_cmd(cmd_data)
+        if len(r) < 2:
+            raise ValueError(r)
+        sw = r[-2:]
+        r = r[0:-2]
+        if sw[0] == 0x61:
+            pk = self.cmd_get_response(sw[1])
+        elif sw[0] == 0x90 and sw[1] == 0x00:
+            pk = r
+        else:
             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])
+        return pk
 
     def cmd_put_data_remove(self, tagh, tagl):
         cmd_data = iso7816_compose(0xda, tagh, tagl, b"")
index 8199f1a..db22521 100644 (file)
@@ -24,6 +24,7 @@ from binascii import hexlify
 from re import match, DOTALL
 from struct import pack
 from util import *
+import pytest
 
 EMPTY_60=bytes(60)
 
@@ -152,6 +153,21 @@ def test_algorithm_attributes_3(card):
     a = get_data_object(card, 0xc3)
     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):
     a = get_data_object(card, 0x4f)
     print()
index 6f472cb..5914f7c 100644 (file)
@@ -136,6 +136,19 @@ def test_timestamp_3_put(card):
     r = card.cmd_put_data(0x00, 0xd0, timestamp3)
     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):
     r = card.cmd_change_reference_data(1, FACTORY_PASSPHRASE_PW1 + PW1_TEST0)
     assert r
index fda9b0c..ab6828a 100644 (file)
@@ -54,6 +54,7 @@ def test_sex_put(card):
         # Gnuk
         r = card.cmd_put_data(0x5f, 0x35, b"")
     except ValueError:
+        # OpenPGP card which doesn't allow b""
         r = card.cmd_put_data(0x5f, 0x35, b"9")
     assert r