]> Cypherpunks.ru repositories - pyderasn.git/blobdiff - tests/test_crts.py
More decod() usage examples
[pyderasn.git] / tests / test_crts.py
index 93162e0b8ad5a6eac35f2551ae115802ae5f87b8..4b58fac97f596119f0aa6619922ecf0b5c2a892c 100644 (file)
@@ -1,11 +1,10 @@
 # coding: utf-8
 # PyDERASN -- Python ASN.1 DER codec with abstract structures
-# Copyright (C) 2017 Sergey Matveev <stargrave@stargrave.org>
+# Copyright (C) 2017-2020 Sergey Matveev <stargrave@stargrave.org>
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Lesser General Public License as
-# published by the Free Software Foundation, either version 3 of the
-# License, or (at your option) any later version.
+# published by the Free Software Foundation, version 3 of the License.
 #
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -37,6 +36,7 @@ from pyderasn import SequenceOf
 from pyderasn import SetOf
 from pyderasn import tag_ctxc
 from pyderasn import tag_ctxp
+from pyderasn import TeletexString
 from pyderasn import UTCTime
 
 
@@ -62,7 +62,6 @@ some_oids = {
 
 
 class Version(Integer):
-    __slots__ = ()
     schema = (
         ("v1", 0),
         ("v2", 1),
@@ -71,12 +70,10 @@ class Version(Integer):
 
 
 class CertificateSerialNumber(Integer):
-    __slots__ = ()
     pass
 
 
 class AlgorithmIdentifier(Sequence):
-    __slots__ = ()
     schema = (
         ("algorithm", ObjectIdentifier()),
         ("parameters", Any(optional=True)),
@@ -84,43 +81,49 @@ class AlgorithmIdentifier(Sequence):
 
 
 class AttributeType(ObjectIdentifier):
-    __slots__ = ()
     pass
 
 
 class AttributeValue(Any):
-    __slots__ = ()
     pass
 
 
+class OrganizationName(Choice):
+    schema = (
+        ("printableString", PrintableString()),
+        ("teletexString", TeletexString()),
+    )
+
+
 class AttributeTypeAndValue(Sequence):
-    __slots__ = ()
     schema = (
-        ("type", AttributeType()),
+        ("type", AttributeType(defines=(((".", "value"), {
+            ObjectIdentifier("2.5.4.6"): PrintableString(),
+            ObjectIdentifier("2.5.4.8"): PrintableString(),
+            ObjectIdentifier("2.5.4.7"): PrintableString(),
+            ObjectIdentifier("2.5.4.10"): OrganizationName(),
+            ObjectIdentifier("2.5.4.3"): PrintableString(),
+        }),))),
         ("value", AttributeValue()),
     )
 
 
 class RelativeDistinguishedName(SetOf):
-    __slots__ = ()
     schema = AttributeTypeAndValue()
     bounds = (1, float("+inf"))
 
 
 class RDNSequence(SequenceOf):
-    __slots__ = ()
     schema = RelativeDistinguishedName()
 
 
 class Name(Choice):
-    __slots__ = ()
     schema = (
         ("rdnSequence", RDNSequence()),
     )
 
 
 class Time(Choice):
-    __slots__ = ()
     schema = (
         ("utcTime", UTCTime()),
         ("generalTime", GeneralizedTime()),
@@ -128,7 +131,6 @@ class Time(Choice):
 
 
 class Validity(Sequence):
-    __slots__ = ()
     schema = (
         ("notBefore", Time()),
         ("notAfter", Time()),
@@ -136,7 +138,6 @@ class Validity(Sequence):
 
 
 class SubjectPublicKeyInfo(Sequence):
-    __slots__ = ()
     schema = (
         ("algorithm", AlgorithmIdentifier()),
         ("subjectPublicKey", BitString()),
@@ -144,12 +145,10 @@ class SubjectPublicKeyInfo(Sequence):
 
 
 class UniqueIdentifier(BitString):
-    __slots__ = ()
     pass
 
 
 class Extension(Sequence):
-    __slots__ = ()
     schema = (
         ("extnID", ObjectIdentifier()),
         ("critical", Boolean(default=False)),
@@ -158,13 +157,11 @@ class Extension(Sequence):
 
 
 class Extensions(SequenceOf):
-    __slots__ = ()
     schema = Extension()
     bounds = (1, float("+inf"))
 
 
 class TBSCertificate(Sequence):
-    __slots__ = ()
     schema = (
         ("version", Version(expl=tag_ctxc(0), default="v1")),
         ("serialNumber", CertificateSerialNumber()),
@@ -180,7 +177,6 @@ class TBSCertificate(Sequence):
 
 
 class Certificate(Sequence):
-    __slots__ = ()
     schema = (
         ("tbsCertificate", TBSCertificate()),
         ("signatureAlgorithm", AlgorithmIdentifier()),
@@ -209,8 +205,7 @@ class TestGoSelfSignedVector(TestCase):
             "ba3ca12568fdc6c7b4511cd40a7f659980402df2b998bb9a4a8cbeb34c0f0a78c",
             "f8d91ede14a5ed76bf116fe360aafa8821490435",
         )))
-        crt, tail = Certificate().decode(raw)
-        self.assertSequenceEqual(tail, b"")
+        crt = Certificate().decod(raw)
         tbs = crt["tbsCertificate"]
         self.assertEqual(tbs["version"], 0)
         self.assertFalse(tbs["version"].decoded)
@@ -224,12 +219,12 @@ class TestGoSelfSignedVector(TestCase):
                 expect.encode(),
             )
         assert_raw_equals(tbs["serialNumber"], Integer(10143011886257155224))
-        algo_id = AlgorithmIdentifier()
-        algo_id["algorithm"] = ObjectIdentifier("1.2.840.113549.1.1.5")
-        algo_id["parameters"] = Any(Null())
+        algo_id = AlgorithmIdentifier((
+            ("algorithm", ObjectIdentifier("1.2.840.113549.1.1.5")),
+            ("parameters", Any(Null())),
+        ))
         self.assertEqual(tbs["signature"], algo_id)
         assert_raw_equals(tbs["signature"], algo_id)
-        issuer = Name()
         rdnSeq = RDNSequence()
         for oid, klass, text in (
                 ("2.5.4.6", PrintableString, "XX"),
@@ -239,22 +234,25 @@ class TestGoSelfSignedVector(TestCase):
                 ("2.5.4.3", PrintableString, "false.example.com"),
                 ("1.2.840.113549.1.9.1", IA5String, "false@example.com"),
         ):
-            attr = AttributeTypeAndValue()
-            attr["type"] = AttributeType(oid)
-            attr["value"] = AttributeValue(klass(text))
-            rdn = RelativeDistinguishedName()
-            rdn.append(attr)
-            rdnSeq.append(rdn)
-        issuer["rdnSequence"] = rdnSeq
+            rdnSeq.append(
+                RelativeDistinguishedName((
+                    AttributeTypeAndValue((
+                        ("type", AttributeType(oid)),
+                        ("value", AttributeValue(klass(text))),
+                    )),
+                ))
+            )
+        issuer = Name(("rdnSequence", rdnSeq))
         self.assertEqual(tbs["issuer"], issuer)
         assert_raw_equals(tbs["issuer"], issuer)
-        validity = Validity()
-        validity["notBefore"] = Time(
-            ("utcTime", UTCTime(datetime(2009, 10, 8, 0, 25, 53)))
-        )
-        validity["notAfter"] = Time(
-            ("utcTime", UTCTime(datetime(2010, 10, 8, 0, 25, 53)))
-        )
+        validity = Validity((
+            ("notBefore", Time(
+                ("utcTime", UTCTime(datetime(2009, 10, 8, 0, 25, 53)))
+            )),
+            ("notAfter", Time(
+                ("utcTime", UTCTime(datetime(2010, 10, 8, 0, 25, 53)))
+            )),
+        ))
         self.assertEqual(tbs["validity"], validity)
         assert_raw_equals(tbs["validity"], validity)
         self.assertEqual(tbs["subject"], issuer)
@@ -285,9 +283,10 @@ class TestGoSelfSignedVector(TestCase):
         tbs = TBSCertificate()
         tbs["serialNumber"] = CertificateSerialNumber(10143011886257155224)
 
-        sign_algo_id = AlgorithmIdentifier()
-        sign_algo_id["algorithm"] = ObjectIdentifier("1.2.840.113549.1.1.5")
-        sign_algo_id["parameters"] = Any(Null())
+        sign_algo_id = AlgorithmIdentifier((
+            ("algorithm", ObjectIdentifier("1.2.840.113549.1.1.5")),
+            ("parameters", Any(Null())),
+        ))
         tbs["signature"] = sign_algo_id
 
         rdnSeq = RDNSequence()
@@ -299,20 +298,27 @@ class TestGoSelfSignedVector(TestCase):
                 ("2.5.4.3", PrintableString, "false.example.com"),
                 ("1.2.840.113549.1.9.1", IA5String, "false@example.com"),
         ):
-            attr = AttributeTypeAndValue()
-            attr["type"] = AttributeType(oid)
-            attr["value"] = AttributeValue(klass(text))
-            rdn = RelativeDistinguishedName()
-            rdn.append(attr)
-            rdnSeq.append(rdn)
+            rdnSeq.append(
+                RelativeDistinguishedName((
+                    AttributeTypeAndValue((
+                        ("type", AttributeType(oid)),
+                        ("value", AttributeValue(klass(text))),
+                    )),
+                ))
+            )
         issuer = Name()
         issuer["rdnSequence"] = rdnSeq
         tbs["issuer"] = issuer
         tbs["subject"] = issuer
 
-        validity = Validity()
-        validity["notBefore"] = Time(("utcTime", UTCTime(datetime(2009, 10, 8, 0, 25, 53))))
-        validity["notAfter"] = Time(("utcTime", UTCTime(datetime(2010, 10, 8, 0, 25, 53))))
+        validity = Validity((
+            ("notBefore", Time(
+                ("utcTime", UTCTime(datetime(2009, 10, 8, 0, 25, 53)),),
+            )),
+            ("notAfter", Time(
+                ("utcTime", UTCTime(datetime(2010, 10, 8, 0, 25, 53)),),
+            )),
+        ))
         tbs["validity"] = validity
 
         spki = SubjectPublicKeyInfo()
@@ -390,8 +396,7 @@ class TestGoPayPalVector(TestCase):
             "07ba44cce54a2d723f9847f626dc054605076321ab469b9c78d5545b3d0c1ec86",
             "48cb55023826fdbb8221c439607a8bb",
         )))
-        crt, tail = Certificate().decode(raw)
-        self.assertSequenceEqual(tail, b"")
+        crt = Certificate().decod(raw)
         self.assertSequenceEqual(crt.encode(), raw)
         pprint(crt)
         repr(crt)