|
| 1 | +""" |
| 2 | +PKCS#11 attribute mapper tests. |
| 3 | +""" |
| 4 | + |
| 5 | +import unittest |
| 6 | + |
| 7 | +from pkcs11 import Attribute, MechanismFlag |
| 8 | +from pkcs11.attributes import AttributeMapper |
| 9 | + |
| 10 | + |
| 11 | +class AttributeMapperTests(unittest.TestCase): |
| 12 | + def test_public_key_template_is_not_shared_between_calls(self): |
| 13 | + mapper = AttributeMapper() |
| 14 | + |
| 15 | + rsa_template = mapper.public_key_template( |
| 16 | + capabilities=MechanismFlag.ENCRYPT | MechanismFlag.VERIFY, |
| 17 | + id_=b"rsa", |
| 18 | + label="rsa", |
| 19 | + store=True, |
| 20 | + ) |
| 21 | + rsa_template.update( |
| 22 | + { |
| 23 | + Attribute.PUBLIC_EXPONENT: b"\x01\x00\x01", |
| 24 | + Attribute.MODULUS_BITS: 4096, |
| 25 | + } |
| 26 | + ) |
| 27 | + |
| 28 | + ec_template = mapper.public_key_template( |
| 29 | + capabilities=MechanismFlag.VERIFY, |
| 30 | + id_=b"ec", |
| 31 | + label="ec", |
| 32 | + store=False, |
| 33 | + ) |
| 34 | + |
| 35 | + self.assertNotIn(Attribute.PUBLIC_EXPONENT, mapper.default_public_key_template) |
| 36 | + self.assertNotIn(Attribute.MODULUS_BITS, mapper.default_public_key_template) |
| 37 | + self.assertNotIn(Attribute.PUBLIC_EXPONENT, ec_template) |
| 38 | + self.assertNotIn(Attribute.MODULUS_BITS, ec_template) |
| 39 | + self.assertEqual(ec_template[Attribute.ID], b"ec") |
| 40 | + self.assertEqual(ec_template[Attribute.LABEL], "ec") |
| 41 | + self.assertFalse(ec_template[Attribute.TOKEN]) |
| 42 | + |
| 43 | + def test_private_key_template_is_not_shared_between_calls(self): |
| 44 | + mapper = AttributeMapper() |
| 45 | + |
| 46 | + rsa_template = mapper.private_key_template( |
| 47 | + capabilities=MechanismFlag.DECRYPT | MechanismFlag.SIGN, |
| 48 | + id_=b"rsa", |
| 49 | + label="rsa", |
| 50 | + store=True, |
| 51 | + ) |
| 52 | + rsa_template[Attribute.EXTRACTABLE] = True |
| 53 | + |
| 54 | + ec_template = mapper.private_key_template( |
| 55 | + capabilities=MechanismFlag.SIGN | MechanismFlag.DERIVE, |
| 56 | + id_=b"ec", |
| 57 | + label="ec", |
| 58 | + store=False, |
| 59 | + ) |
| 60 | + |
| 61 | + self.assertNotIn(Attribute.EXTRACTABLE, mapper.default_private_key_template) |
| 62 | + self.assertNotIn(Attribute.EXTRACTABLE, ec_template) |
| 63 | + self.assertEqual(ec_template[Attribute.ID], b"ec") |
| 64 | + self.assertEqual(ec_template[Attribute.LABEL], "ec") |
| 65 | + self.assertFalse(ec_template[Attribute.TOKEN]) |
0 commit comments