Skip to content

Commit acdbf4d

Browse files
authored
pkcs11: Only get needed attributes to avoid driver issue (#496)
Only get the needed attributes to avoid a call to CK_OBJECT_HANDLE's to_dict method, which may throw an error if attributes cannot be retrieved and return more attribute and values than needed. The getAttributesValue method returns a list of values, one value for each requested attribute. Resolves: #492 Signed-off-by: Stefan Berger <[email protected]>
1 parent a3e0292 commit acdbf4d

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/model_signing/_signing/sign_pkcs11.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,16 @@ def _check_supported_ec_key(public_key: ec.EllipticCurvePublicKey):
6363

6464

6565
def encode_ec_public_key(public_key: PyKCS11.CK_OBJECT_HANDLE) -> PublicKeyInfo:
66-
obj_d = public_key.to_dict()
66+
ec_params, ec_point = public_key.session.getAttributeValue(
67+
public_key, [PyKCS11.CKA_EC_PARAMS, PyKCS11.CKA_EC_POINT]
68+
)
6769
return PublicKeyInfo(
6870
{
6971
"algorithm": {
7072
"algorithm": "ec",
71-
"parameters": ECDomainParameters.load(
72-
bytes(obj_d["CKA_EC_PARAMS"])
73-
),
73+
"parameters": ECDomainParameters.load(bytes(ec_params)),
7474
},
75-
"public_key": bytes(OctetString.load(bytes(obj_d["CKA_EC_POINT"]))),
75+
"public_key": bytes(OctetString.load(bytes(ec_point))),
7676
}
7777
).dump()
7878

@@ -145,10 +145,12 @@ def find_object(
145145
cka_id = tuple([x for x in id])
146146

147147
for obj in self.session.findObjects([(PyKCS11.CKA_CLASS, clas)]):
148-
obj_d = obj.to_dict()
149-
if label is not None and label != obj_d.get("CKA_LABEL"):
148+
obj_label, obj_id = obj.session.getAttributeValue(
149+
obj, [PyKCS11.CKA_LABEL, PyKCS11.CKA_ID]
150+
)
151+
if label is not None and label != obj_label:
150152
continue
151-
if cka_id is not None and cka_id != obj_d.get("CKA_ID"):
153+
if cka_id is not None and cka_id != obj_id:
152154
continue
153155
return obj
154156
raise ValueError(f"Could not find any object with {msg}")

0 commit comments

Comments
 (0)