How to query HSM slot for value "key handle" using (known) value "key label"

552 Views Asked by At

Need to query HSM (in Python) using known values HSM "slot #", "key label" to obtain (unknown value) "key handle".

Any suggestions?

2

There are 2 best solutions below

1
On

You can use pkcs11.Session.get_key() to get unique object in HSM by label.

An example to get an AES key with label "Label":

with token.open(user_pin='1234', rw=True) as session:
    key = session.get_key(object_class=ObjectClass.SECRET_KEY, key_type=KeyType.AES, label="Label")

Use pkcs11.Session.get_objects() for more complex searches.

Note: You do not need the "key handle" value at all as you can perform operations using returned key object.

Good luck!

0
On

You put me on the right track. I ended up using c_find_objects_ex to grab the key handle and used slice to chop off the trailing "L" in the output. Thanks!