From 5a2bb54bbb7b68a7407ab5d62c828c329166bd81 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Mon, 19 Oct 2015 23:45:59 -0500 Subject: encode countryName with PrintableString This commit adds a dependency on asn1crypto for testing purposes to parse the certificate and confirm that countryName is encoded with PrintableString while other fields are UTF8String. This is a test only dep. --- tests/test_x509.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'tests') diff --git a/tests/test_x509.py b/tests/test_x509.py index 8035886c..1fa4d82a 100644 --- a/tests/test_x509.py +++ b/tests/test_x509.py @@ -9,6 +9,8 @@ import datetime import ipaddress import os +from asn1crypto import core, x509 as asn1cryptox509 + import pytest import six @@ -834,6 +836,43 @@ class TestRSACertificateRequest(object): x509.DNSName(u"cryptography.io"), ] + def test_build_cert_printable_string_country_name(self, backend): + issuer_private_key = RSA_KEY_2048.private_key(backend) + subject_private_key = RSA_KEY_2048.private_key(backend) + + not_valid_before = datetime.datetime(2002, 1, 1, 12, 1) + not_valid_after = datetime.datetime(2030, 12, 31, 8, 30) + + builder = x509.CertificateBuilder().serial_number( + 777 + ).issuer_name(x509.Name([ + x509.NameAttribute(NameOID.COUNTRY_NAME, u'US'), + x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, u'Texas'), + ])).subject_name(x509.Name([ + x509.NameAttribute(NameOID.COUNTRY_NAME, u'US'), + x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, u'Texas'), + ])).public_key( + subject_private_key.public_key() + ).not_valid_before( + not_valid_before + ).not_valid_after( + not_valid_after + ) + + cert = builder.sign(issuer_private_key, hashes.SHA256(), backend) + + parsedasn1 = asn1cryptox509.Certificate.load( + cert.public_bytes(serialization.Encoding.DER) + ) + assert isinstance( + parsedasn1.subject.chosen[0][0]['value'].chosen, + core.PrintableString + ) + assert isinstance( + parsedasn1.subject.chosen[1][0]['value'].chosen, + core.UTF8String + ) + class TestCertificateBuilder(object): @pytest.mark.requires_backend_interface(interface=RSABackend) -- cgit v1.2.3 From 8b5d094ca3bb5cafa61001b83c1798e40af37223 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Tue, 27 Oct 2015 09:35:17 +0900 Subject: switch to using pyasn1_modules for the test --- tests/test_x509.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'tests') diff --git a/tests/test_x509.py b/tests/test_x509.py index 1fa4d82a..79424752 100644 --- a/tests/test_x509.py +++ b/tests/test_x509.py @@ -9,7 +9,9 @@ import datetime import ipaddress import os -from asn1crypto import core, x509 as asn1cryptox509 +from pyasn1.codec.der import decoder + +from pyasn1_modules import rfc2459 import pytest @@ -861,17 +863,17 @@ class TestRSACertificateRequest(object): cert = builder.sign(issuer_private_key, hashes.SHA256(), backend) - parsedasn1 = asn1cryptox509.Certificate.load( - cert.public_bytes(serialization.Encoding.DER) - ) - assert isinstance( - parsedasn1.subject.chosen[0][0]['value'].chosen, - core.PrintableString - ) - assert isinstance( - parsedasn1.subject.chosen[1][0]['value'].chosen, - core.UTF8String - ) + parsed, _ = decoder.decode( + cert.public_bytes(serialization.Encoding.DER), + asn1Spec=rfc2459.Certificate() + ) + tbs_cert = parsed.getComponentByName('tbsCertificate') + subject = tbs_cert.getComponentByName('subject') + issuer = tbs_cert.getComponentByName('issuer') + # \x13 is printable string. The first byte of the value of the + # node corresponds to the ASN.1 string type. + assert str(subject[0][0][0][1])[0] == "\x13" + assert str(issuer[0][0][0][1])[0] == "\x13" class TestCertificateBuilder(object): -- cgit v1.2.3 From 39c4445347ec3778a3cf0c68a0116dbbc4c53fa6 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Tue, 27 Oct 2015 10:43:37 +0900 Subject: remove unneeded str --- tests/test_x509.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/test_x509.py b/tests/test_x509.py index 79424752..19b525d3 100644 --- a/tests/test_x509.py +++ b/tests/test_x509.py @@ -872,8 +872,8 @@ class TestRSACertificateRequest(object): issuer = tbs_cert.getComponentByName('issuer') # \x13 is printable string. The first byte of the value of the # node corresponds to the ASN.1 string type. - assert str(subject[0][0][0][1])[0] == "\x13" - assert str(issuer[0][0][0][1])[0] == "\x13" + assert subject[0][0][0][1][0] == "\x13" + assert issuer[0][0][0][1][0] == "\x13" class TestCertificateBuilder(object): -- cgit v1.2.3 From 225a08f9e4b507493744f566b8f0a8bf2a06e82e Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Tue, 27 Oct 2015 10:51:00 +0900 Subject: work on py3 --- tests/test_x509.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/test_x509.py b/tests/test_x509.py index 19b525d3..43eca472 100644 --- a/tests/test_x509.py +++ b/tests/test_x509.py @@ -872,8 +872,8 @@ class TestRSACertificateRequest(object): issuer = tbs_cert.getComponentByName('issuer') # \x13 is printable string. The first byte of the value of the # node corresponds to the ASN.1 string type. - assert subject[0][0][0][1][0] == "\x13" - assert issuer[0][0][0][1][0] == "\x13" + assert subject[0][0][0][1][0] == b"\x13"[0] + assert issuer[0][0][0][1][0] == b"\x13"[0] class TestCertificateBuilder(object): -- cgit v1.2.3