Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicate definition of RecipientKeyIdentifier #266

Merged
merged 3 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions asn1crypto/cms.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,12 @@ class RecipientKeyIdentifier(Sequence):
('other', OtherKeyAttribute, {'optional': True}),
]

def _setup(self):
super(RecipientKeyIdentifier, self)._setup()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super()._setup() should be fine

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This project targets Python 2.6+ currently, hence the parameters passed to super.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unexpected as 2.6 has been EOL for 9, 2.7 for 3 years, but you'll have your reasons.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I’m more interested in tests for the code you added

# 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']


class KeyAgreementRecipientIdentifier(Choice):
_alternatives = [
Expand Down Expand Up @@ -929,21 +935,20 @@ def decompressed(self):
return self._decompressed


class RecipientKeyIdentifier(Sequence):
_fields = [
('subjectKeyIdentifier', OctetString),
('date', GeneralizedTime, {'optional': True}),
('other', OtherKeyAttribute, {'optional': True}),
]


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
38 changes: 38 additions & 0 deletions tests/test_cms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1001,3 +1001,41 @@ 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,
)
self.assertEqual(
new_sekp.chosen.untag().dump(True),
rki.dump(True),
)
self.assertEqual(
'recipient_key_id',
new_sekp.name,
)
self.assertEqual(
'recipient_key_id',
old_sekp.name,
)
Loading