Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix x509.Name.build() to properly handle all fields #270

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions asn1crypto/x509.py
Original file line number Diff line number Diff line change
Expand Up @@ -1015,15 +1015,27 @@ def build(cls, name_dict, use_printable=False):

for attribute_name, attribute_value in name_dict.items():
attribute_name = NameType.map(attribute_name)
if attribute_name == 'email_address':
value = EmailAddress(attribute_value)
elif attribute_name == 'domain_component':
value = DNSName(attribute_value)
attribute_class = NameTypeAndValue._oid_specs.get(attribute_name)
if not attribute_class:
raise ValueError(unwrap(
'''
No encoding specification found for %s
''',
attribute_name
))

if isinstance(attribute_value, attribute_class):
value = attribute_value

elif attribute_class is not DirectoryString:
value = attribute_class(attribute_value)

elif attribute_name in set(['dn_qualifier', 'country_name', 'serial_number']):
value = DirectoryString(
name='printable_string',
value=PrintableString(attribute_value)
)

else:
value = DirectoryString(
name=encoding_name,
Expand Down
19 changes: 19 additions & 0 deletions tests/test_x509.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,25 @@ def test_build_name_printable(self):
self.assertIsInstance(printable_name.chosen[2][0]['value'].chosen, core.PrintableString)
self.assertEqual('common_name', printable_name.chosen[2][0]['type'].native)

def test_build_name_type_by_oid(self):
complex_name = x509.Name.build(
{
'country_name': 'US',
'tpm_manufacturer': 'Acme Co',
'unique_identifier': b'\x04\x10\x03\x09',
'email_address': '[email protected]'
}
)
self.assertEqual("country_name", complex_name.chosen[0][0]['type'].native)
self.assertIsInstance(complex_name.chosen[0][0]['value'], x509.DirectoryString)
self.assertIsInstance(complex_name.chosen[0][0]['value'].chosen, core.PrintableString)
self.assertEqual("email_address", complex_name.chosen[1][0]['type'].native)
self.assertIsInstance(complex_name.chosen[1][0]['value'], x509.EmailAddress)
self.assertEqual("tpm_manufacturer", complex_name.chosen[2][0]['type'].native)
self.assertIsInstance(complex_name.chosen[2][0]['value'], core.UTF8String)
self.assertEqual("unique_identifier", complex_name.chosen[3][0]['type'].native)
self.assertIsInstance(complex_name.chosen[3][0]['value'], core.OctetBitString)

def test_v1_cert(self):
cert = self._load_cert('chromium/ndn.ca.crt')
tbs_cert = cert['tbs_certificate']
Expand Down
Loading