Skip to content

Commit 0af82a3

Browse files
authored
Allow generation of keys of different types in the same session (#227)
1 parent ecf10f7 commit 0af82a3

2 files changed

Lines changed: 67 additions & 2 deletions

File tree

pkcs11/attributes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def public_key_template(
209209
label: str | None,
210210
store: bool,
211211
) -> dict[Attribute, Any]:
212-
template = self.default_public_key_template
212+
template = dict(self.default_public_key_template)
213213
_apply_capabilities(
214214
template, (Attribute.ENCRYPT, Attribute.WRAP, Attribute.VERIFY), capabilities
215215
)
@@ -224,7 +224,7 @@ def private_key_template(
224224
label: str | None,
225225
store: bool,
226226
) -> dict[Attribute, Any]:
227-
template = self.default_private_key_template
227+
template = dict(self.default_private_key_template)
228228
_apply_capabilities(
229229
template,
230230
(Attribute.DECRYPT, Attribute.UNWRAP, Attribute.SIGN, Attribute.DERIVE),

tests/test_attributes.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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

Comments
 (0)