From 4693b14410cd8677209b00af7e016837d0c08272 Mon Sep 17 00:00:00 2001 From: wbond Date: Wed, 11 Oct 2023 19:00:55 -0400 Subject: [PATCH] Add tests & fix SMIMEEncryptionKeyPreference to use correct name format --- asn1crypto/cms.py | 13 +++++++++++-- asn1crypto/core.py | 11 +++++++++++ tests/test_cms.py | 26 ++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/asn1crypto/cms.py b/asn1crypto/cms.py index 2800051f..b104c995 100644 --- a/asn1crypto/cms.py +++ b/asn1crypto/cms.py @@ -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'] @@ -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 diff --git a/asn1crypto/core.py b/asn1crypto/core.py index 2edd4f35..428ef0e5 100644 --- a/asn1crypto/core.py +++ b/asn1crypto/core.py @@ -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( ''' diff --git a/tests/test_cms.py b/tests/test_cms.py index 8f9b1e64..c1e7acc1 100644 --- a/tests/test_cms.py +++ b/tests/test_cms.py @@ -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, + )