tests: Support keygen by OpenPGP card.
authorNIIBE Yutaka <gniibe@fsij.org>
Wed, 3 Apr 2019 08:02:00 +0000 (17:02 +0900)
committerNIIBE Yutaka <gniibe@fsij.org>
Wed, 3 Apr 2019 08:02:37 +0000 (17:02 +0900)
tests/card_test_keygen.py
tests/openpgp_card.py

index ecfcf5f..6ff0c9b 100644 (file)
@@ -56,7 +56,7 @@ class Test_Card_Keygen(object):
     def test_signature_sigkey(self, card):
         msg = b"Sign me please"
         pk = card.cmd_get_public_key(1)
-        pk_info = (pk[9:9+256], pk[9+256+2:9+256+2+3])
+        pk_info = (pk[9:9+256], pk[9+256+2:])
         digest = rsa_keys.compute_digestinfo(msg)
         sig = int(hexlify(card.cmd_pso(0x9e, 0x9a, digest)),16)
         r = rsa_keys.verify_signature(pk_info, digest, sig)
@@ -69,7 +69,7 @@ class Test_Card_Keygen(object):
     def test_decryption(self, card):
         msg = b"encrypt me please"
         pk = card.cmd_get_public_key(2)
-        pk_info = (pk[9:9+256], pk[9+256+2:9+256+2+3])
+        pk_info = (pk[9:9+256], pk[9+256+2:])
         ciphertext = rsa_keys.encrypt_with_pubkey(pk_info, msg)
         r = card.cmd_pso(0x80, 0x86, ciphertext)
         assert r == msg
@@ -77,7 +77,7 @@ class Test_Card_Keygen(object):
     def test_signature_authkey(self, card):
         msg = b"Sign me please to authenticate"
         pk = card.cmd_get_public_key(3)
-        pk_info = (pk[9:9+256], pk[9+256+2:9+256+2+3])
+        pk_info = (pk[9:9+256], pk[9+256+2:])
         digest = rsa_keys.compute_digestinfo(msg)
         sig = int(hexlify(card.cmd_internal_authenticate(digest)),16)
         r = rsa_keys.verify_signature(pk_info, digest, sig)
index f3bd953..5db9244 100644 (file)
@@ -339,16 +339,20 @@ class OpenPGP_Card(object):
             data = b'\xb8\x00'
         else:
             data = b'\xa4\x00'
-        cmd_data = iso7816_compose(0x47, 0x80, 0, data)
+        if self.__reader.is_tpdu_reader():
+            cmd_data = iso7816_compose(0x47, 0x80, 0, data, le=512)
+        else:
+            cmd_data = iso7816_compose(0x47, 0x80, 0, data)
         sw = self.__reader.send_cmd(cmd_data)
-        if len(sw) != 2:
+        if len(sw) < 2:
             raise ValueError(sw)
-        if sw[0] == 0x90 and sw[1] == 0x00:
-            return b""
-        elif sw[0] != 0x61:
+        if sw[-2] == 0x61:
+            pk = self.cmd_get_response(sw[1])
+        elif sw[-2] == 0x90 and sw[-1] == 0x00:
+            pk = sw
+        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[9:9+256], pk[9+256+2:-2])
 
     def cmd_get_public_key(self, keyno):
         if keyno == 1:
@@ -359,10 +363,9 @@ class OpenPGP_Card(object):
             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)
-            r = self.__reader.send_cmd(cmd_data)
+        r = self.__reader.send_cmd(cmd_data)
         if len(r) < 2:
             raise ValueError(r)
         sw = r[-2:]