Skip to content

Commit 1a7a5ba

Browse files
authored
Fix x509.Name.build() to properly handle all fields (#270)
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
1 parent 8609892 commit 1a7a5ba

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

asn1crypto/x509.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -1015,15 +1015,27 @@ def build(cls, name_dict, use_printable=False):
10151015

10161016
for attribute_name, attribute_value in name_dict.items():
10171017
attribute_name = NameType.map(attribute_name)
1018-
if attribute_name == 'email_address':
1019-
value = EmailAddress(attribute_value)
1020-
elif attribute_name == 'domain_component':
1021-
value = DNSName(attribute_value)
1018+
attribute_class = NameTypeAndValue._oid_specs.get(attribute_name)
1019+
if not attribute_class:
1020+
raise ValueError(unwrap(
1021+
'''
1022+
No encoding specification found for %s
1023+
''',
1024+
attribute_name
1025+
))
1026+
1027+
if isinstance(attribute_value, attribute_class):
1028+
value = attribute_value
1029+
1030+
elif attribute_class is not DirectoryString:
1031+
value = attribute_class(attribute_value)
1032+
10221033
elif attribute_name in set(['dn_qualifier', 'country_name', 'serial_number']):
10231034
value = DirectoryString(
10241035
name='printable_string',
10251036
value=PrintableString(attribute_value)
10261037
)
1038+
10271039
else:
10281040
value = DirectoryString(
10291041
name=encoding_name,

tests/test_x509.py

+19
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,25 @@ def test_build_name_printable(self):
466466
self.assertIsInstance(printable_name.chosen[2][0]['value'].chosen, core.PrintableString)
467467
self.assertEqual('common_name', printable_name.chosen[2][0]['type'].native)
468468

469+
def test_build_name_type_by_oid(self):
470+
complex_name = x509.Name.build(
471+
{
472+
'country_name': 'US',
473+
'tpm_manufacturer': 'Acme Co',
474+
'unique_identifier': b'\x04\x10\x03\x09',
475+
'email_address': '[email protected]'
476+
}
477+
)
478+
self.assertEqual("country_name", complex_name.chosen[0][0]['type'].native)
479+
self.assertIsInstance(complex_name.chosen[0][0]['value'], x509.DirectoryString)
480+
self.assertIsInstance(complex_name.chosen[0][0]['value'].chosen, core.PrintableString)
481+
self.assertEqual("email_address", complex_name.chosen[1][0]['type'].native)
482+
self.assertIsInstance(complex_name.chosen[1][0]['value'], x509.EmailAddress)
483+
self.assertEqual("tpm_manufacturer", complex_name.chosen[2][0]['type'].native)
484+
self.assertIsInstance(complex_name.chosen[2][0]['value'], core.UTF8String)
485+
self.assertEqual("unique_identifier", complex_name.chosen[3][0]['type'].native)
486+
self.assertIsInstance(complex_name.chosen[3][0]['value'], core.OctetBitString)
487+
469488
def test_v1_cert(self):
470489
cert = self._load_cert('chromium/ndn.ca.crt')
471490
tbs_cert = cert['tbs_certificate']

0 commit comments

Comments
 (0)