From 0cda571fed3308365e094d57dcc9c4db3184e5d8 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Thu, 6 Feb 2020 16:33:34 +0300 Subject: [PATCH] Test code human readable OIDs --- doc/examples.rst | 4 ++-- tests/test_crts.py | 57 +++++++++++++++++++++++----------------------- 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/doc/examples.rst b/doc/examples.rst index 6f16af6..953001a 100644 --- a/doc/examples.rst +++ b/doc/examples.rst @@ -235,7 +235,7 @@ ___________________ If you have got dictionaries with ObjectIdentifiers, like example one from ``tests/test_crts.py``:: - some_oids = { + stroid2name = { "1.2.840.113549.1.1.1": "id-rsaEncryption", "1.2.840.113549.1.1.5": "id-sha1WithRSAEncryption", [...] @@ -245,7 +245,7 @@ from ``tests/test_crts.py``:: then you can pass it to pretty printer to see human readable OIDs:: - $ python -m pyderasn --oids tests.test_crts:some_oids path/to/file + $ python -m pyderasn --oids tests.test_crts:stroid2name path/to/file [...] 37 [1,1, 11] . . . . . . >: SET OF 39 [1,1, 9] . . . . . . . . >: SEQUENCE OF diff --git a/tests/test_crts.py b/tests/test_crts.py index d878529..aaf359e 100644 --- a/tests/test_crts.py +++ b/tests/test_crts.py @@ -45,25 +45,26 @@ from pyderasn import TeletexString from pyderasn import UTCTime -some_oids = { - "1.2.840.113549.1.1.1": "id-rsaEncryption", - "1.2.840.113549.1.1.5": "id-sha1WithRSAEncryption", - "1.2.840.113549.1.9.1": "id-emailAddress", - "2.5.29.14": "id-ce-subjectKeyIdentifier", - "2.5.29.15": "id-ce-keyUsage", - "2.5.29.17": "id-ce-subjectAltName", - "2.5.29.18": "id-ce-issuerAltName", - "2.5.29.19": "id-ce-basicConstraints", - "2.5.29.31": "id-ce-cRLDistributionPoints", - "2.5.29.35": "id-ce-authorityKeyIdentifier", - "2.5.29.37": "id-ce-extKeyUsage", - "2.5.4.3": "id-at-commonName", - "2.5.4.6": "id-at-countryName", - "2.5.4.7": "id-at-localityName", - "2.5.4.8": "id-at-stateOrProvinceName", - "2.5.4.10": "id-at-organizationName", - "2.5.4.11": "id-at-organizationalUnitName", +name2oid = { + "id-rsaEncryption": ObjectIdentifier("1.2.840.113549.1.1.1"), + "id-sha1WithRSAEncryption": ObjectIdentifier("1.2.840.113549.1.1.5"), + "id-emailAddress": ObjectIdentifier("1.2.840.113549.1.9.1"), + "id-ce-subjectKeyIdentifier": ObjectIdentifier("2.5.29.14"), + "id-ce-keyUsage": ObjectIdentifier("2.5.29.15"), + "id-ce-subjectAltName": ObjectIdentifier("2.5.29.17"), + "id-ce-issuerAltName": ObjectIdentifier("2.5.29.18"), + "id-ce-basicConstraints": ObjectIdentifier("2.5.29.19"), + "id-ce-cRLDistributionPoints": ObjectIdentifier("2.5.29.31"), + "id-ce-authorityKeyIdentifier": ObjectIdentifier("2.5.29.35"), + "id-ce-extKeyUsage": ObjectIdentifier("2.5.29.37"), + "id-at-commonName": ObjectIdentifier("2.5.4.3"), + "id-at-countryName": ObjectIdentifier("2.5.4.6"), + "id-at-localityName": ObjectIdentifier("2.5.4.7"), + "id-at-stateOrProvinceName": ObjectIdentifier("2.5.4.8"), + "id-at-organizationName": ObjectIdentifier("2.5.4.10"), + "id-at-organizationalUnitName": ObjectIdentifier("2.5.4.11"), } +stroid2name = {str(oid): name for name, oid in name2oid.items()} class Version(Integer): @@ -103,11 +104,11 @@ class OrganizationName(Choice): class AttributeTypeAndValue(Sequence): schema = ( ("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(), + name2oid["id-at-countryName"]: PrintableString(), + name2oid["id-at-localityName"]: PrintableString(), + name2oid["id-at-stateOrProvinceName"]: PrintableString(), + name2oid["id-at-organizationName"]: OrganizationName(), + name2oid["id-at-commonName"]: PrintableString(), }),))), ("value", AttributeValue()), ) @@ -225,7 +226,7 @@ class TestGoSelfSignedVector(TestCase): ) assert_raw_equals(tbs["serialNumber"], Integer(10143011886257155224)) algo_id = AlgorithmIdentifier(( - ("algorithm", ObjectIdentifier("1.2.840.113549.1.1.5")), + ("algorithm", name2oid["id-sha1WithRSAEncryption"]), ("parameters", Any(Null())), )) self.assertEqual(tbs["signature"], algo_id) @@ -263,7 +264,7 @@ class TestGoSelfSignedVector(TestCase): self.assertEqual(tbs["subject"], issuer) assert_raw_equals(tbs["subject"], issuer) spki = SubjectPublicKeyInfo() - algo_id["algorithm"] = ObjectIdentifier("1.2.840.113549.1.1.1") + algo_id["algorithm"] = name2oid["id-rsaEncryption"] spki["algorithm"] = algo_id spki["subjectPublicKey"] = BitString(hexdec("".join(( "3048024100cdb7639c3278f006aa277f6eaf42902b592d8cbcbe38a1c92ba4695", @@ -275,7 +276,7 @@ class TestGoSelfSignedVector(TestCase): self.assertNotIn("issuerUniqueID", tbs) self.assertNotIn("subjectUniqueID", tbs) self.assertNotIn("extensions", tbs) - algo_id["algorithm"] = ObjectIdentifier("1.2.840.113549.1.1.5") + algo_id["algorithm"] = name2oid["id-sha1WithRSAEncryption"] self.assertEqual(crt["signatureAlgorithm"], algo_id) self.assertEqual(crt["signatureValue"], BitString(hexdec("".join(( "a67b06ec5ece92772ca413cba3ca12568fdc6c7b4511cd40a7f659980402df2b", @@ -290,7 +291,7 @@ class TestGoSelfSignedVector(TestCase): tbs["serialNumber"] = CertificateSerialNumber(10143011886257155224) sign_algo_id = AlgorithmIdentifier(( - ("algorithm", ObjectIdentifier("1.2.840.113549.1.1.5")), + ("algorithm", name2oid["id-sha1WithRSAEncryption"]), ("parameters", Any(Null())), )) tbs["signature"] = sign_algo_id @@ -329,7 +330,7 @@ class TestGoSelfSignedVector(TestCase): spki = SubjectPublicKeyInfo() spki_algo_id = copy(sign_algo_id) - spki_algo_id["algorithm"] = ObjectIdentifier("1.2.840.113549.1.1.1") + spki_algo_id["algorithm"] = name2oid["id-rsaEncryption"] spki["algorithm"] = spki_algo_id spki["subjectPublicKey"] = BitString(hexdec("".join(( "3048024100cdb7639c3278f006aa277f6eaf42902b592d8cbcbe38a1c92ba4695", -- 2.44.0