]> Cypherpunks.ru repositories - pygost.git/blobdiff - pygost/test_x509.py
Simplify GOST3410Curve initialization
[pygost.git] / pygost / test_x509.py
index cddee1c37ae166af20eca6ab2daf1c40faf480f5..a2112ad44c8d5f4b19ebfab62f4560c8cc9be01f 100644 (file)
@@ -1,6 +1,6 @@
 # coding: utf-8
 # PyGOST -- Pure Python GOST cryptographic functions library
-# Copyright (C) 2015-2018 Sergey Matveev <stargrave@stargrave.org>
+# Copyright (C) 2015-2019 Sergey Matveev <stargrave@stargrave.org>
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -19,8 +19,7 @@ from base64 import b64decode
 from unittest import skipIf
 from unittest import TestCase
 
-from pygost.gost3410 import CURVE_PARAMS
-from pygost.gost3410 import GOST3410Curve
+from pygost.gost3410 import CURVES
 from pygost.gost3410 import prv_unmarshal
 from pygost.gost3410 import pub_unmarshal
 from pygost.gost3410 import public_key
@@ -32,6 +31,8 @@ from pygost.utils import hexdec
 try:
     from pyderasn import OctetString
 
+    from pygost.asn1schemas.oids import id_tc26_gost3410_2012_256
+    from pygost.asn1schemas.oids import id_tc26_gost3410_2012_512
     from pygost.asn1schemas.x509 import Certificate
 except ImportError:
     pyderasn_exists = False
@@ -48,15 +49,34 @@ class TestCertificate(TestCase):
     """
 
     def process_cert(self, curve_name, mode, hasher, prv_key_raw, cert_raw):
-        cert, tail = Certificate().decode(cert_raw)
+        cert, tail = Certificate().decode(cert_raw, ctx={
+            "defines_by_path": (
+                (
+                    (
+                        "tbsCertificate",
+                        "subjectPublicKeyInfo",
+                        "algorithm",
+                        "algorithm",
+                    ),
+                    (
+                        (
+                            ("..", "subjectPublicKey"),
+                            {
+                                id_tc26_gost3410_2012_256: OctetString(),
+                                id_tc26_gost3410_2012_512: OctetString(),
+                            },
+                        ),
+                    ),
+                ),
+            ),
+        })
         self.assertSequenceEqual(tail, b"")
-        curve = GOST3410Curve(*CURVE_PARAMS[curve_name])
+        curve = CURVES[curve_name]
         prv_key = prv_unmarshal(prv_key_raw)
-        pub_key_raw, tail = OctetString().decode(
-            bytes(cert["tbsCertificate"]["subjectPublicKeyInfo"]["subjectPublicKey"])
-        )
+        spk = cert["tbsCertificate"]["subjectPublicKeyInfo"]["subjectPublicKey"]
+        self.assertIsNotNone(spk.defined)
+        _, pub_key_raw = spk.defined
         pub_key = pub_unmarshal(bytes(pub_key_raw), mode=mode)
-        self.assertSequenceEqual(tail, b"")
         self.assertSequenceEqual(pub_key, public_key(curve, prv_key))
         self.assertTrue(verify(
             curve,
@@ -84,7 +104,7 @@ dqQfmHrz6TI6Xojdh/t8ckODv/587NS5/6KsM77vc6Wh90NAT2s=
         """)
         prv_key_raw = hexdec("BFCF1D623E5CDD3032A7C6EABB4A923C46E43D640FFEAAF2C3ED39A8FA399924")[::-1]
         self.process_cert(
-            "GostR3410_2001_CryptoPro_XchA_ParamSet",
+            "id-GostR3410-2001-CryptoPro-XchA-ParamSet",
             2001,
             GOST34112012256,
             prv_key_raw,
@@ -112,7 +132,7 @@ o3eUNFkNyHJwQCk2WoOlO16zwGk2tdKH4KmD5w==
         """)
         prv_key_raw = hexdec("3FC01CDCD4EC5F972EB482774C41E66DB7F380528DFE9E67992BA05AEE462435757530E641077CE587B976C8EEB48C48FD33FD175F0C7DE6A44E014E6BCB074B")[::-1]
         self.process_cert(
-            "GostR3410_2012_TC26_ParamSetB",
+            "id-tc26-gost-3410-12-512-paramSetB",
             2012,
             GOST34112012512,
             prv_key_raw,