Skip to content

Commit

Permalink
Fix x509.Name.build() to properly handle all fields
Browse files Browse the repository at this point in the history
Previously it did not properly handle the following fields:

 - unique_identifier
 - tpm_manufacturer
 - tpm_model
 - tpm_version
 - platform_manufacturer
 - platform_model
 - platform_version

Fixes #260
  • Loading branch information
wbond committed Nov 3, 2023
1 parent 8609892 commit 2de5853
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
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

0 comments on commit 2de5853

Please sign in to comment.