]> Cypherpunks.ru repositories - pyderasn.git/commitdiff
Ability to set values during Sequence initialization 3.6
authorSergey Matveev <stargrave@stargrave.org>
Sun, 29 Apr 2018 18:51:02 +0000 (21:51 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Sun, 29 Apr 2018 18:56:37 +0000 (21:56 +0300)
VERSION
doc/examples.rst
doc/news.rst
pyderasn.py
tests/test_crts.py
tests/test_pyderasn.py

diff --git a/VERSION b/VERSION
index 5a958026daa3208cb329c403433beb56abd5c036..d70c8f8d89f2a10a58af47d5da32162321804e6a 100644 (file)
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-3.5
+3.6
index 512c8f8f1a9041daf61c4eee9a3ae0dcddb2e487..bdc8faf8229ddd620cb485413d6fab8045bf43ec 100644 (file)
@@ -364,9 +364,10 @@ Let's create some simple self-signed X.509 certificate from the ground::
     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()
@@ -378,20 +379,26 @@ Let's create some simple self-signed X.509 certificate from the ground::
             ("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 = Name()
-    issuer["rdnSequence"] = rdnSeq
+        rdnSeq.append(
+            RelativeDistinguishedName((
+                AttributeTypeAndValue((
+                    ("type", AttributeType(oid)),
+                    ("value", AttributeValue(klass(text))),
+                )),
+            ))
+        )
+    issuer = Name(("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()
index fd7551b0a8c3e0f673af5a368d49731225ee96e7..ea774542889be194300fc3c01ad730fd50ba7115 100644 (file)
@@ -1,6 +1,12 @@
 News
 ====
 
+.. _release3.6:
+
+3.6
+---
+* Ability to set values during Sequence initialization.
+
 .. _release3.5:
 
 3.5
index 04f8ed5a21cbbaa69123786ca9d595564836f7a9..68d67d07e91a746886d22bb629d19a8ae6000f43 100755 (executable)
@@ -3740,7 +3740,7 @@ class Sequence(Obj):
     pyderasn.InvalidValueType: invalid value type, expected: <class 'pyderasn.ObjectIdentifier'>
     >>> ext["extnID"] = ObjectIdentifier("1.2.3")
 
-    You can know if sequence is ready to be encoded:
+    You can determine if sequence is ready to be encoded:
 
     >>> ext.ready
     False
@@ -3766,7 +3766,15 @@ class Sequence(Obj):
 
     Assign ``None`` to remove value from sequence.
 
-    You can know if value exists/set in the sequence and take its value:
+    You can set values in Sequence during its initialization:
+
+    >>> AlgorithmIdentifier((
+        ("algorithm", ObjectIdentifier("1.2.3")),
+        ("parameters", Any(Null()))
+    ))
+    AlgorithmIdentifier SEQUENCE[OBJECT IDENTIFIER 1.2.3, ANY 0500 OPTIONAL]
+
+    You can determine if value exists/set in the sequence and take its value:
 
     >>> "extnID" in ext, "extnValue" in ext, "critical" in ext
     (True, True, False)
@@ -3823,9 +3831,17 @@ class Sequence(Obj):
         )
         self._value = {}
         if value is not None:
-            self._value = self._value_sanitize(value)
+            if issubclass(value.__class__, Sequence):
+                self._value = value._value
+            elif hasattr(value, "__iter__"):
+                for seq_key, seq_value in value:
+                    self[seq_key] = seq_value
+            else:
+                raise InvalidValueType((Sequence,))
         if default is not None:
-            default_value = self._value_sanitize(default)
+            if not issubclass(default.__class__, Sequence):
+                raise InvalidValueType((Sequence,))
+            default_value = default._value
             default_obj = self.__class__(impl=self.tag, expl=self._expl)
             default_obj.specs = self.specs
             default_obj._value = default_value
@@ -3833,11 +3849,6 @@ class Sequence(Obj):
             if value is None:
                 self._value = default_obj.copy()._value
 
-    def _value_sanitize(self, value):
-        if not issubclass(value.__class__, Sequence):
-            raise InvalidValueType((Sequence,))
-        return value._value
-
     @property
     def ready(self):
         for name, spec in self.specs.items():
index 04e54e4747194edf72ecd84113eb3eb36dfb2c23..ae9bfd2e6e71c4f33e866185841d71a3afb10803 100644 (file)
@@ -221,12 +221,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"),
@@ -236,22 +236,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)
@@ -282,9 +285,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()
@@ -296,20 +300,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()
index 723020c4820802e2970d87712bbb879a36146ece..dfea4300e78a6e4ccc8c17cd05457e6a3bc07fb6 100644 (file)
@@ -3967,7 +3967,7 @@ def sequences_strategy(draw, seq_klass):
 class SeqMixing(object):
     def test_invalid_value_type(self):
         with self.assertRaises(InvalidValueType) as err:
-            self.base_klass((1, 2, 3))
+            self.base_klass(123)
         repr(err.exception)
 
     def test_invalid_value_type_set(self):