The problem seems to only be pkcs11-tool -l --derive
in the newest OpenSC-Version. Listing with pkcs11-tool -l --list-objects
seems to work just fine. But I might have just realized, why it did say Private Key not found
… I forgot the -l
. Without a login it`s no wonder it can’t find the key… So needless to say, even this works now.
We also confirmed now that our application can use the Keys just fine and that derive now works. So I would like to thank you for leading us on the right path! Saved us a lot of headache!
The backups where imported with sc-hsm-tool --unwrap-key <filename> --key-reference <new key-ref>
I wouldn’t say it is perfect (but it works just fine), but I will share my script that I use to modify the Backups here, so that anyone stumbling across this can use it (at their own risk). The script relies on the library asn1crypto
(available in the ubuntu repositories as python3-asn1crypto
btw.) and is written in python.
from asn1crypto.core import *
from pathlib import PosixPath
import argparse
class KeyUsage(BitString):
_map = {
0: 'encrypt',
1: 'decrypt',
2: 'sign',
3: 'signRecover',
4: 'wrap',
5: 'unwrap',
6: 'verify',
7: 'verifyRecover',
8: 'derive',
9: 'nonRepudiation',
}
class ImportantSequence(Sequence):
_fields = [
('octStr', OctetString),
('keyUsage', KeyUsage),
('bitStr2', BitString),
('keyID', Integer),
]
class Context1(Sequence):
_fields = [
('sequence1', Sequence),
('prkdseq', ImportantSequence),
('sequence2', Sequence, {'implicit': 1})
]
class KeyWrap(Sequence):
explicit_class = 1
_fields = [
('oct1', OctetString),
('cont1', Context1, {'implicit': 0}),
('appls', Sequence, {'explicit': 1}),
]
parser = argparse.ArgumentParser(description='Dieses Skript bearbeitet ASN1-Strukturen von NitroKey-Key-Backups um die KeyUsage zu ändern')
parser.add_argument('-f','--file', help='Name/Pfad der Backup-Datei', required=True, type=PosixPath)
args = parser.parse_args()
new_usage = {'signRecover', 'sign', 'derive'}
with open(args.file, 'rb') as file:
keyWrap = KeyWrap().load(file.read())
keyWrap['cont1']['prkdseq']['keyUsage'] = KeyUsage(new_usage)
with open(str(args.file) + '.fixed', 'wb') as file:
file.write(keyWrap.dump())
I didn’t know how to model the APPLICATION-Parts of the Sequence in the library, but as we don’t need to modify anything in those parts, it works without touching it just fine.