Skip to content

Commit

Permalink
Allows an x509.Name to be hashable if it contains unique_identifier
Browse files Browse the repository at this point in the history
Fixes #268
  • Loading branch information
wbond committed Nov 3, 2023
1 parent 1a7a5ba commit 7bc07e7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
10 changes: 8 additions & 2 deletions asn1crypto/x509.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down
17 changes: 17 additions & 0 deletions tests/test_x509.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,23 @@ def test_build_name_type_by_oid(self):
self.assertEqual("unique_identifier", complex_name.chosen[3][0]['type'].native)
self.assertIsInstance(complex_name.chosen[3][0]['value'], core.OctetBitString)

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': '[email protected]'
}
)
self.assertEqual(
"country_name: us \x1e"
"email_address: [email protected] \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']
Expand Down

0 comments on commit 7bc07e7

Please sign in to comment.