Using reader with a card: Nitrokey Nitrokey HSM 0
Wrapped key contains:
Key blob
Private Key Description (PRKD)
Enter User PIN :
Key successfully imported
After pkcs15-tool -D there is only UserPIN and SOPIN visible.
Even pkcs15-tool -k shows no key Using reader with a card: Nitrokey Nitrokey HSM 0
When I try to import again, I get: Found existing private key description in EF with fid c401. Please remove key first, select unused key reference or use --force.
So seems the key is already present.
I also tried to import the related certificate and intermediate certificate, but still there is no private key shown.
What am I doing wrong?
I remember creating a backup token with 3.3 with this way successfully. But no more after the update to 3.5.
One thing which is I think is wrong is that you are doing --write-object without giving pkcs11-tools any details about the IDs of the object to import.
Your keys were obviously generated somewhere else. Has the desired X.509 certificate been already imported before you did the backup of a wrapped key? Or just the key got generated?
Strange, C401 should be so-called “private key information object” and should not contain the
certificate. Is CA01 containing the same information?
The problem seems to be with the original key blob. Apparently OpenSC things that the blob contains the key and the PRKD, but no certificate:
Using reader with a card: Nitrokey Nitrokey HSM 0
Wrapped key contains:
Key blob
Private Key Description (PRKD)
Enter User PIN :
The PRKD goes into C4xx, but in your case the PRKD contains the certificate. I’ve no idea how the certificate could end up in PRKD. How did you create the key blob ? With sc-hsm-tool --wrap-key ? Which version of OpenSC ?
On the SmartCard-HSM, the certificate is always hard-linked to the key and written to CExx. The key is referenced by CCxx, the PRKD by C4xx and the linked certificate by CExx.
Certificates without a private key on the device are written to CAxx, where xx is dynamically allocated in the range 00 to FF. The certificate meta data is written to C8xx.
As long as the wrapped key is fine, it should be possible to reconstruct the PRKD. But we first need to find out how this happened.
I’m using on my local windows 10 computer 0.22 and as I can see it is exactly: OpenSC-0.22.0-rc1
I wonder why I got a RC. Filedate is 10.08.2021. Maybe file version (remove rc1) was not changed after release, as on github the 0.22 was released exactly on that date.
I currently have one device with old firmware and works fine (in production) where I also created a backup (wrap key) again, but this didn’t help on the new firmware on the current device, too.
My main intention was:
Update firmware to fix vulnerabilities, install key + certificate again.
Can you run the following script in the Smart Card Shell ?
var File = require("scsh/file/File").File;
var f = new File("Joe Doe (RSA2048)(1).wky");
var bin = f.readAllAsBinary();
f.close();
print(new ASN1(bin));
That should output the content of the key blob, which should contain three elements like the example below:
Das System kann die angegebene Datei nicht finden) (C:\Users\Markus.Stein\Downloads\scsh3.17.548-noinstall\scsh3.17.548\scsh\file\File.js#124
at C:\Users\Markus.Stein\Downloads\scsh3.17.548-noinstall\scsh3.17.548\scsh\file\File.js#124
at C:\Users\Markus.Stein\Downloads\scsh3.17.548-noinstall\scsh3.17.548\scsh\file\File.js#151
>>GPError: ASN1 (INVALID_TYPE/0) - "Argument must be of type ByteString" in shell#0
Did you save the lines to a script file and fixed the name in File("…") to match your file ? The file must be located in the workspace (i.e. the directory selected when starting the shell).
But I think with 0.20 or 0.19 it worked … (at least with the version where I initially created the token and backup token)
So should I downgrade OpenSC?
Edit: Tried to unwrap the key with OpenSC 0.19 but even no success. Seems the certificate was again not imported automatically from the unwrap and the key will nowhere be shown.
Any suggestions how to restore a backup on the token with the firmware update?
As @sc-hsm said, it should be possible to restore PRKD and make your key work again as before. I don’t know how to approach it since I am not working for neither Nitrokey nor CardContact and doing that kind of support is above my “pay grade”.
I’ve created a script that aims at fixing the issue with the missing PRKD section in the backup file.
The script named repair-prkd.js is located in sc-hsm-workspace/sc-hsm-sdk-scripts/examples. The workspace is contained in the StarterKit in the sc-hsm-workspace-20220201.zip archive.
Unzip the Starterkit and then unzip the contained workspace. Start the Smart Card Shell and select the sc-hsm-workspace folder as workspace. Then run the repair-prkd.js script using either CTRL-R or by entering
The script will prompt for a backup file (*.wky) and then output public information from that file. If the PRKD section is missing, then you are prompted to provide a key label, the key size in bits and the key id (CKA_ID at the PKCS#11) interface. Use ‘01’ or similar if unsure what id should be used. The fixed backup files is stored under a filename composed of the original filename, appended with a “-fixed” suffix.
You will also find the script in the sc-hsm-sdk-scripts git repository in the CDN.