Skip to content

Commit

Permalink
Add tests & fix SMIMEEncryptionKeyPreference to use correct name format
Browse files Browse the repository at this point in the history
  • Loading branch information
wbond committed Oct 12, 2023
1 parent d7fe9f2 commit 4693b14
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
13 changes: 11 additions & 2 deletions asn1crypto/cms.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,8 @@ class RecipientKeyIdentifier(Sequence):

def _setup(self):
super(RecipientKeyIdentifier, self)._setup()
# This creates a backwards compatible shim for an
# incorrect format field name that was in old versions
self._field_map['subjectKeyIdentifier'] = self._field_map['subject_key_identifier']


Expand Down Expand Up @@ -936,10 +938,17 @@ def decompressed(self):
class SMIMEEncryptionKeyPreference(Choice):
_alternatives = [
('issuer_and_serial_number', IssuerAndSerialNumber, {'implicit': 0}),
('recipientKeyId', RecipientKeyIdentifier, {'implicit': 1}),
('subjectAltKeyIdentifier', PublicKeyInfo, {'implicit': 2}),
('recipient_key_id', RecipientKeyIdentifier, {'implicit': 1}),
('subject_alt_key_identifier', PublicKeyInfo, {'implicit': 2}),
]

def _setup(self):
super(SMIMEEncryptionKeyPreference, self)._setup()
# This creates backwards compatible shims for two
# incorrect format alternative names that were in old versions
self._name_map['recipientKeyId'] = self._name_map['recipient_key_id']
self._name_map['subjectAltKeyIdentifier'] = self._name_map['subject_alt_key_identifier']


class SMIMEEncryptionKeyPreferences(SetOf):
_child_spec = SMIMEEncryptionKeyPreference
Expand Down
11 changes: 11 additions & 0 deletions asn1crypto/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3429,6 +3429,17 @@ def __init__(self, value=None, default=None, **kwargs):
self.__setitem__(key, value[key])
unused_keys.remove(key)

# This handles the situation where there is field name
# mapping going on due to a field be renamed. Normally
# the keys are checked against the primary field list.
# If there are still keys left over, check to see if they
# are mapped via checking the _field_map.
if len(unused_keys):
for key in list(unused_keys):
if key in self._field_map:
self.__setitem__(key, value[key])
unused_keys.remove(key)

if len(unused_keys):
raise ValueError(unwrap(
'''
Expand Down
26 changes: 26 additions & 0 deletions tests/test_cms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1001,3 +1001,29 @@ def test_create_role_syntax(self):
]),
rs.native
)

def test_backwards_compat_field_name(self):
new_version = cms.RecipientKeyIdentifier({'subject_key_identifier': b'\x08\x09\x10'})
old_version = cms.RecipientKeyIdentifier({'subjectKeyIdentifier': b'\x08\x09\x10'})
self.assertEqual(
new_version.dump(True),
old_version.dump(True),
)
self.assertEqual(
new_version.native,
old_version.native,
)

def test_backwards_compat_choice_names(self):
rki = cms.RecipientKeyIdentifier({'subject_key_identifier': b'\x08\x09\x10'})
new_sekp = cms.SMIMEEncryptionKeyPreference({'recipient_key_id': rki})
old_sekp = cms.SMIMEEncryptionKeyPreference({'recipientKeyId': rki})

self.assertEqual(
new_sekp.dump(True),
old_sekp.dump(True),
)
self.assertEqual(
new_sekp.native,
old_sekp.native,
)

0 comments on commit 4693b14

Please sign in to comment.