From 8604ed7e5f423239c839e4bfacd3a585daf0b320 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Sun, 29 Apr 2018 21:51:02 +0300 Subject: [PATCH] Ability to set values during Sequence initialization --- VERSION | 2 +- doc/examples.rst | 35 ++++++++++++--------- doc/news.rst | 6 ++++ pyderasn.py | 29 +++++++++++------ tests/test_crts.py | 71 ++++++++++++++++++++++++------------------ tests/test_pyderasn.py | 2 +- 6 files changed, 90 insertions(+), 55 deletions(-) diff --git a/VERSION b/VERSION index 5a95802..d70c8f8 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.5 +3.6 diff --git a/doc/examples.rst b/doc/examples.rst index 512c8f8..bdc8faf 100644 --- a/doc/examples.rst +++ b/doc/examples.rst @@ -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() diff --git a/doc/news.rst b/doc/news.rst index fd7551b..ea77454 100644 --- a/doc/news.rst +++ b/doc/news.rst @@ -1,6 +1,12 @@ News ==== +.. _release3.6: + +3.6 +--- +* Ability to set values during Sequence initialization. + .. _release3.5: 3.5 diff --git a/pyderasn.py b/pyderasn.py index 04f8ed5..68d67d0 100755 --- a/pyderasn.py +++ b/pyderasn.py @@ -3740,7 +3740,7 @@ class Sequence(Obj): pyderasn.InvalidValueType: invalid value type, expected: >>> 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(): diff --git a/tests/test_crts.py b/tests/test_crts.py index 04e54e4..ae9bfd2 100644 --- a/tests/test_crts.py +++ b/tests/test_crts.py @@ -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() diff --git a/tests/test_pyderasn.py b/tests/test_pyderasn.py index 723020c..dfea430 100644 --- a/tests/test_pyderasn.py +++ b/tests/test_pyderasn.py @@ -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): -- 2.44.0