Skip to content

Commit f63a59a

Browse files
Use central templates for unwrapping & deriving
1 parent a0a0423 commit f63a59a

File tree

2 files changed

+46
-50
lines changed

2 files changed

+46
-50
lines changed

pkcs11/_pkcs11.pyx

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,24 +1770,19 @@ class UnwrapMixin(types.UnwrapMixin):
17701770
self.key_type, DEFAULT_WRAP_MECHANISMS,
17711771
mechanism, mechanism_param)
17721772

1773-
# Build attributes
1774-
template_ = {
1775-
Attribute.CLASS: object_class,
1776-
Attribute.KEY_TYPE: key_type,
1777-
Attribute.ID: id or b'',
1778-
Attribute.LABEL: label or '',
1779-
Attribute.TOKEN: store,
1780-
# Capabilities
1781-
Attribute.ENCRYPT: MechanismFlag.ENCRYPT & capabilities,
1782-
Attribute.DECRYPT: MechanismFlag.DECRYPT & capabilities,
1783-
Attribute.WRAP: MechanismFlag.WRAP & capabilities,
1784-
Attribute.UNWRAP: MechanismFlag.UNWRAP & capabilities,
1785-
Attribute.SIGN: MechanismFlag.SIGN & capabilities,
1786-
Attribute.VERIFY: MechanismFlag.VERIFY & capabilities,
1787-
Attribute.DERIVE: MechanismFlag.DERIVE & capabilities,
1788-
}
1789-
17901773
cdef Session session = self.session
1774+
1775+
# Build attributes
1776+
template_ = session.attribute_mapper.generic_key_template(
1777+
{
1778+
Attribute.CLASS: object_class,
1779+
Attribute.KEY_TYPE: key_type,
1780+
},
1781+
id_=id,
1782+
label=label,
1783+
store=store,
1784+
capabilities=capabilities,
1785+
)
17911786
cdef AttributeList attrs = session.make_attribute_list(merge_templates(template_, template))
17921787
cdef CK_MECHANISM *mech_data = mech.data
17931788
cdef CK_OBJECT_HANDLE unwrapping_key = self.handle
@@ -1831,27 +1826,13 @@ class DeriveMixin(types.DeriveMixin):
18311826
self.key_type, DEFAULT_DERIVE_MECHANISMS,
18321827
mechanism, mechanism_param)
18331828

1834-
# Build attributes
1835-
template_ = {
1836-
Attribute.CLASS: ObjectClass.SECRET_KEY,
1837-
Attribute.KEY_TYPE: key_type,
1838-
Attribute.ID: id or b'',
1839-
Attribute.LABEL: label or '',
1840-
Attribute.TOKEN: store,
1841-
Attribute.VALUE_LEN: key_length // 8, # In bytes
1842-
Attribute.PRIVATE: True,
1843-
Attribute.SENSITIVE: True,
1844-
# Capabilities
1845-
Attribute.ENCRYPT: MechanismFlag.ENCRYPT & capabilities,
1846-
Attribute.DECRYPT: MechanismFlag.DECRYPT & capabilities,
1847-
Attribute.WRAP: MechanismFlag.WRAP & capabilities,
1848-
Attribute.UNWRAP: MechanismFlag.UNWRAP & capabilities,
1849-
Attribute.SIGN: MechanismFlag.SIGN & capabilities,
1850-
Attribute.VERIFY: MechanismFlag.VERIFY & capabilities,
1851-
Attribute.DERIVE: MechanismFlag.DERIVE & capabilities,
1852-
}
1853-
18541829
cdef Session session = self.session
1830+
1831+
template_ = session.attribute_mapper.secret_key_template(
1832+
capabilities=capabilities, id_=id, label=label, store=store,
1833+
)
1834+
template_[Attribute.KEY_TYPE] = key_type
1835+
template_[Attribute.VALUE_LEN] = key_length // 8 # In bytes
18551836
cdef AttributeList attrs = session.make_attribute_list(merge_templates(template_, template))
18561837
cdef CK_MECHANISM *mech_data = mech.data
18571838
cdef CK_OBJECT_HANDLE src_key = self.handle

pkcs11/attributes.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ def _enum(type_):
9494
Map of attributes to (serialize, deserialize) functions.
9595
"""
9696

97+
ALL_CAPABILITIES = (
98+
Attribute.ENCRYPT,
99+
Attribute.DECRYPT,
100+
Attribute.WRAP,
101+
Attribute.UNWRAP,
102+
Attribute.SIGN,
103+
Attribute.VERIFY,
104+
Attribute.DERIVE,
105+
)
106+
97107

98108
def _apply_common(template, id_, label, store):
99109
if id_:
@@ -206,19 +216,24 @@ def secret_key_template(
206216
label,
207217
store,
208218
):
209-
template = self.default_secret_key_template
210-
_apply_capabilities(
211-
template,
212-
(
213-
Attribute.ENCRYPT,
214-
Attribute.DECRYPT,
215-
Attribute.WRAP,
216-
Attribute.UNWRAP,
217-
Attribute.SIGN,
218-
Attribute.VERIFY,
219-
Attribute.DERIVE,
220-
),
221-
capabilities,
219+
return self.generic_key_template(
220+
self.default_secret_key_template,
221+
capabilities=capabilities,
222+
id_=id_,
223+
label=label,
224+
store=store,
222225
)
226+
227+
def generic_key_template(
228+
self,
229+
base_template,
230+
*,
231+
capabilities,
232+
id_,
233+
label,
234+
store,
235+
):
236+
template = dict(base_template)
237+
_apply_capabilities(template, ALL_CAPABILITIES, capabilities)
223238
_apply_common(template, id_, label, store)
224239
return template

0 commit comments

Comments
 (0)