From 6c17891d1a55d3febe8abaf17e364db0b274d3b9 Mon Sep 17 00:00:00 2001 From: wbond Date: Fri, 3 Nov 2023 07:42:28 -0400 Subject: [PATCH] Allows an x509.Name to be hashable if it container unique_identifier Fixes #268 --- asn1crypto/x509.py | 10 ++++++++-- tests/test_x509.py | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/asn1crypto/x509.py b/asn1crypto/x509.py index 8cfb2c78..bcd18e4a 100644 --- a/asn1crypto/x509.py +++ b/asn1crypto/x509.py @@ -27,7 +27,7 @@ from ._errors import unwrap from ._iri import iri_to_uri, uri_to_iri from ._ordereddict import OrderedDict -from ._types import type_name, str_cls, bytes_to_list +from ._types import type_name, str_cls, byte_cls, bytes_to_list from .algos import AlgorithmIdentifier, AnyAlgorithmIdentifier, DigestAlgorithm, SignedDigestAlgorithm from .core import ( Any, @@ -708,7 +708,13 @@ def prepped_value(self): """ if self._prepped is None: - self._prepped = self._ldap_string_prep(self['value'].native) + native = self['value'].native + if isinstance(native, str_cls): + self._prepped = self._ldap_string_prep(native) + else: + if isinstance(native, byte_cls): + native = ' ' + native.decode('cp1252') + ' ' + self._prepped = native return self._prepped def __ne__(self, other): diff --git a/tests/test_x509.py b/tests/test_x509.py index ece92520..a9d10dea 100644 --- a/tests/test_x509.py +++ b/tests/test_x509.py @@ -466,6 +466,23 @@ 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_name_hashable(self): + complex_name = x509.Name.build( + { + 'country_name': 'US', + 'tpm_manufacturer': 'Acme Co', + 'unique_identifier': b'\x04\x10\x03\x09', + 'email_address': 'will@codexns.io' + } + ) + self.assertEqual( + "country_name: us \x1e" + "email_address: will@codexns.io \x1e" + "tpm_manufacturer: acme co \x1e" + "unique_identifier: \x04\x10\x03\x09 ", + complex_name.hashable + ) + def test_v1_cert(self): cert = self._load_cert('chromium/ndn.ca.crt') tbs_cert = cert['tbs_certificate']