Dear OpenSC-Team
I've got a Nitrokey Pro (Firmware version: 0.8) and would like to generate RSA keys on it via OpenSSL and use them in turn to issue certificates. The integration of OpenSC's PKCS11 engine for OpenSSL works, but when I try to create a self-signed certificate with it, I get errors.
I expect that the RSA keys are successfully created on the Nitrokey and in turn are useable (e.g. for issuing X509 certificates.
When I try to create a self-signed certificate with it, I get the following error:
OpenSSL> engine -t dynamic -pre SO_PATH:/usr/local/Cellar/engine_pkcs11/0.1.8/lib/engines/engine_pkcs11.so -pre ID:pkcs11 -pre LIST_ADD:1 -pre LOAD -pre MODULE_PATH:/usr/local/Cellar/opensc/0.16.0/lib/pkcs11/opensc-pkcs11.so
(dynamic) Dynamic engine loading support
[Success]: SO_PATH:/usr/local/Cellar/engine_pkcs11/0.1.8/lib/engines/engine_pkcs11.so
[Success]: ID:pkcs11
[Success]: LIST_ADD:1
[Success]: LOAD
[Success]: MODULE_PATH:/usr/local/Cellar/opensc/0.16.0/lib/pkcs11/opensc-pkcs11.so
Loaded: (pkcs11) pkcs11 engine
[ available ]
OpenSSL>
OpenSSL>
OpenSSL>
OpenSSL> req -engine pkcs11 -new -keyform engine -out cert.pem -text -x509 -days 3640 -key label_test -subj '/CN=test'
engine "pkcs11" set.
failed to enumerate slots
PKCS11_get_private_key returned NULL
cannot load Private Key from engine
140735752430600:error:80002030:PKCS11 library:PKCS11_enum_slots:Device error:p11_slot.c:312:
140735752430600:error:26096080:engine routines:ENGINE_load_private_key:failed loading private key:eng_pkey.c:124:
unable to load Private Key
error in req
In addition, creating new RSA keys (2048 & 4096) via the pkcs11-tool doesn't work yet as well. In both cases, I get the following exceptions:
Here are some fragments of the logs (with OpenSC log level = 9):
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] apdu.c:386:sc_single_transmit: returning with: 0 (Success)
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] apdu.c:539:sc_transmit: returning with: 0 (Success)
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] card.c:434:sc_unlock: called
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] card-openpgp.c:2157:pgp_gen_key: Card has done key generation.
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] iso7816.c:121:iso7816_check_sw: Security status not satisfied
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] card-openpgp.c:2167:pgp_gen_key: Please verify PIN first.
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] card-openpgp.c:2177:pgp_gen_key: returning with: -1211 (Security status not satisfied)
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] card-openpgp.c:2595:pgp_card_ctl: returning with: -1211 (Security status not satisfied)
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] card.c:866:sc_card_ctl: returning with: -1211 (Security status not satisfied)
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] pkcs15-openpgp.c:217:openpgp_generate_key: returning with: -1211 (Security status not satisfied)
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] pkcs15-lib.c:1383:sc_pkcs15init_generate_key: Failed to generate key: -1211 (Security status not satisfied)
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] framework-pkcs15.c:2803:pkcs15_gen_keypair: sc_pkcs15init_generate_key returned -1211
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] misc.c:61:sc_to_cryptoki_error_common: libopensc return value: -1211 (Security status not satisfied)
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] pkcs15-lib.c:419:sc_pkcs15init_unbind: called
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] pkcs15-lib.c:420:sc_pkcs15init_unbind: Pksc15init Unbind: 0:0x7f80ac2054f0:1
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] card.c:434:sc_unlock: called
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] reader-pcsc.c:587:pcsc_unlock: called
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] pkcs11-global.c:304:C_Finalize: C_Finalize()
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] ctx.c:819:sc_cancel: called
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] reader-pcsc.c:637:pcsc_cancel: called
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] slot.c:163:card_removed: Nitrokey Nitrokey Pro: card removed
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] slot.c:425:slot_token_removed: slot_token_removed(0x0)
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] pkcs11-session.c:129:sc_pkcs11_close_all_sessions: real C_CloseAllSessions(0x0) 1
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] pkcs11-session.c:98:sc_pkcs11_close_session: real C_CloseSession(0x7f80aae00280)
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] framework-pkcs15.c:1364:pkcs15_release_token: pkcs15_release_token() not implemented
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] slot.c:425:slot_token_removed: slot_token_removed(0x1)
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] pkcs11-session.c:129:sc_pkcs11_close_all_sessions: real C_CloseAllSessions(0x1) 0
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] framework-pkcs15.c:1364:pkcs15_release_token: pkcs15_release_token() not implemented
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] slot.c:425:slot_token_removed: slot_token_removed(0x2)
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] pkcs11-session.c:129:sc_pkcs11_close_all_sessions: real C_CloseAllSessions(0x2) 0
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] slot.c:425:slot_token_removed: slot_token_removed(0x3)
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] pkcs11-session.c:129:sc_pkcs11_close_all_sessions: real C_CloseAllSessions(0x3) 0
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] pkcs15.c:1264:sc_pkcs15_unbind: called
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] pkcs15-pin.c:690:sc_pkcs15_pincache_clear: called
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] misc.c:61:sc_to_cryptoki_error_common: libopensc return value: 0 (Success)
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] card.c:336:sc_disconnect_card: called
0x7fff9887e3c0 13:39:04.309 [opensc-pkcs11] reader-pcsc.c:533:pcsc_disconnect: called
0x7fff9887e3c0 13:39:05.265 [opensc-pkcs11] card.c:357:sc_disconnect_card: returning with: 0 (Success)
0x7fff9887e3c0 13:39:05.265 [opensc-pkcs11] ctx.c:842:sc_release_context: called
0x7fff9887e3c0 13:39:05.265 [opensc-pkcs11] reader-pcsc.c:794:pcsc_finish: called
0x7fff9887e3c0 13:45:55.751 [opensc-pkcs11] framework-pkcs15.c:2791:pkcs15_gen_keypair: Try on-card key pair generation
0x7fff9887e3c0 13:45:55.751 [opensc-pkcs11] pkcs15-lib.c:1326:sc_pkcs15init_generate_key: called
0x7fff9887e3c0 13:45:55.751 [opensc-pkcs11] pkcs15-lib.c:2094:check_keygen_params_consistency: returning with: -1408 (Not supported)
0x7fff9887e3c0 13:45:55.751 [opensc-pkcs11] pkcs15-lib.c:1329:sc_pkcs15init_generate_key: Invalid key size: -1408 (Not supported)
0x7fff9887e3c0 13:45:55.751 [opensc-pkcs11] framework-pkcs15.c:2803:pkcs15_gen_keypair: sc_pkcs15init_generate_key returned -1408
0x7fff9887e3c0 13:45:55.751 [opensc-pkcs11] misc.c:61:sc_to_cryptoki_error_common: libopensc return value: -1408 (Not supported)
0x7fff9887e3c0 13:45:55.751 [opensc-pkcs11] pkcs15-lib.c:419:sc_pkcs15init_unbind: called
0x7fff9887e3c0 13:45:55.751 [opensc-pkcs11] pkcs15-lib.c:420:sc_pkcs15init_unbind: Pksc15init Unbind: 0:0x7fdd44f06860:1
0x7fff9887e3c0 13:45:55.751 [opensc-pkcs11] card.c:434:sc_unlock: called
0x7fff9887e3c0 13:45:55.751 [opensc-pkcs11] reader-pcsc.c:587:pcsc_unlock: called
0x7fff9887e3c0 13:45:55.751 [opensc-pkcs11] pkcs11-global.c:304:C_Finalize: C_Finalize()
0x7fff9887e3c0 13:45:55.751 [opensc-pkcs11] ctx.c:819:sc_cancel: called
0x7fff9887e3c0 13:45:55.751 [opensc-pkcs11] reader-pcsc.c:637:pcsc_cancel: called
0x7fff9887e3c0 13:45:55.751 [opensc-pkcs11] slot.c:163:card_removed: Nitrokey Nitrokey Pro: card removed
0x7fff9887e3c0 13:45:55.751 [opensc-pkcs11] slot.c:425:slot_token_removed: slot_token_removed(0x0)
0x7fff9887e3c0 13:45:55.751 [opensc-pkcs11] pkcs11-session.c:129:sc_pkcs11_close_all_sessions: real C_CloseAllSessions(0x0) 1
0x7fff9887e3c0 13:45:55.751 [opensc-pkcs11] pkcs11-session.c:98:sc_pkcs11_close_session: real C_CloseSession(0x7fdd44d1eae0)
0x7fff9887e3c0 13:45:55.751 [opensc-pkcs11] pkcs15-pin.c:690:sc_pkcs15_pincache_clear: called
0x7fff9887e3c0 13:45:55.751 [opensc-pkcs11] framework-pkcs15.c:1364:pkcs15_release_token: pkcs15_release_token() not implemented
0x7fff9887e3c0 13:45:55.751 [opensc-pkcs11] slot.c:425:slot_token_removed: slot_token_removed(0x1)
0x7fff9887e3c0 13:45:55.751 [opensc-pkcs11] pkcs11-session.c:129:sc_pkcs11_close_all_sessions: real C_CloseAllSessions(0x1) 0
0x7fff9887e3c0 13:45:55.751 [opensc-pkcs11] framework-pkcs15.c:1364:pkcs15_release_token: pkcs15_release_token() not implemented
0x7fff9887e3c0 13:45:55.751 [opensc-pkcs11] slot.c:425:slot_token_removed: slot_token_removed(0x2)
0x7fff9887e3c0 13:45:55.751 [opensc-pkcs11] pkcs11-session.c:129:sc_pkcs11_close_all_sessions: real C_CloseAllSessions(0x2) 0
0x7fff9887e3c0 13:45:55.751 [opensc-pkcs11] slot.c:425:slot_token_removed: slot_token_removed(0x3)
0x7fff9887e3c0 13:45:55.751 [opensc-pkcs11] pkcs11-session.c:129:sc_pkcs11_close_all_sessions: real C_CloseAllSessions(0x3) 0
0x7fff9887e3c0 13:45:55.751 [opensc-pkcs11] pkcs15.c:1264:sc_pkcs15_unbind: called
0x7fff9887e3c0 13:45:55.751 [opensc-pkcs11] pkcs15-pin.c:690:sc_pkcs15_pincache_clear: called
0x7fff9887e3c0 13:45:55.751 [opensc-pkcs11] misc.c:61:sc_to_cryptoki_error_common: libopensc return value: 0 (Success)
0x7fff9887e3c0 13:45:55.751 [opensc-pkcs11] card.c:336:sc_disconnect_card: called
0x7fff9887e3c0 13:45:55.751 [opensc-pkcs11] reader-pcsc.c:533:pcsc_disconnect: called
0x7fff9887e3c0 13:45:56.706 [opensc-pkcs11] card.c:357:sc_disconnect_card: returning with: 0 (Success)
0x7fff9887e3c0 13:45:56.706 [opensc-pkcs11] ctx.c:842:sc_release_context: called
0x7fff9887e3c0 13:45:56.706 [opensc-pkcs11] reader-pcsc.c:794:pcsc_finish: called
It seems that 4096 bit keys are not supported, but they should be supported. The Nitrokey support asked me to place my concerns here. Can you help me, please? Any hints are welcome. I've tested and experienced the same behaviour on macOS Sierra and on Raspian GNU/Linux.
Thx & best wishes
PGP in OpenSC is not widely used. Maybe @hongquan can help.
If that doesn't help, please use gpg for initialization.
Dear @prom3th3uz,
To generate key for OpenPGP card via PKCS#11 is not possible yet. The reason is that this operation needs "Admin PIN", but I found no way to make OpenSC's PKCS#11 module to ask for PIN.
Dear @hongquan
Thank you very much for your quick answer. Therefore, does it mean that it should be possible to initialise Nitrokey via GPG (i.e. generate an RSA-Key) and use that in turn via PKCS11 (e.g. to issue X509 certs)?
Thx & best wishes
@prom3th3uz
Therefore, does it mean that it should be possible to initialise Nitrokey via GPG (i.e. generate an RSA-Key) and use that in turn via PKCS11 (e.g. to issue X509 certs)?
Yes. You can also generate keys by other tools from OpenSC (pkcs5init, openpgp-tool) as in this wiki: https://github.com/OpenSC/OpenSC/wiki/OpenPGP-card
Referring to RSA key sizes>2048 bit in OpenSC:
AFAIK, current OpenSC releases don't support using those key sizes, but since PR#814 merged (or use my 2 patch files for releases), OpenSC-git-master has (some) support for up to 4096 bit keys for sign, decipher and pkcs11-tool -lt does work too (for my driver carblue/acos5_64). Maybe, card-openpgp.c requires adaptions as well.
@prom3th3uz:
For self-signing with card keys, this might be useful: https://github.com/carblue/acos5_64/blob/master/howto/HOWTO_Create_Your_own_CA_root_hierarchy_on_Linux
Finally, I had success with this command: pkcs15-init --store-certificate some.cert.pem --auth-id ?? --id ?? --format pem --pin ??
I'm interested to hear about Your final experience with Nitrokey Pro, as I consider to buy that one too.
Hi @carblue and @hongquan
Thank you very much for your hints. I've used the GPG approach to initalize the Nitrokeys and then in turn used PKCS11 to use the Private Keys for issuing certificates. My experience with the Nitrokeys are good so far, I can recommend it if you are looking for a Yubikey alternative that is open source.
When are 4096 bit keys going to be officially supported? Currently, one can only generate 3072 bit RSA Keys. In addition, when it comes to backup and recovery, what approach do you recommend?
Thx & cheers
Hi @prom3th3uz
I'm looking into this issue. Hope that I can fix it this week. But may be limited on Ubuntu only. I'm not so experience with programming on Windows.
Hi @hongquan
I am currently using Raspian. Linux would be fine :D.
What about the backup/recovery process? Do you have a recommendation?
Export the private keys to secring.gpg, convert them to PKCS12 and import them again if required (=> worst case scenario)?
Nitrokey (and OpenPGP in general) does not let you export private key. If you want to have a backup, generate the key outside, then import to Nitrokey (as p12 file).
But because Nitrokey cannot generate 4096 bit key, I doubt it can import 4096 bit key from p12 file.
Hi @hongquan
Exactly, that would be my approach as well. I currently use 3072 bit keys, that should work. I will test it.
Hi @hongquan @prom3th3uz
Recently RSA4096 key generation with Nitrokey Pro started to work on GPG2 from 2.1.20 (it was not working on versions 2.1.14-19).
https://github.com/Nitrokey/nitrokey-pro-firmware/issues/33
Hi @prom3th3uz.
I tested and openpgp-tool can generate 4096-bit key on Ubuntu 17.04, with the latest OpenSC source.
$ openpgp-tool --verify CHV3 --pin 12345678 --gen-key 1 --key-len 4096
Using reader with a card: Nitrokey Nitrokey Pro (00003A630000000000000000) 00 00
Fingerprint:
C33CBCE1AF1A25621C2BB66D0070F2AF 049E0864
$ pkcs15-tool -k
Using reader with a card: Nitrokey Nitrokey Pro (00003A630000000000000000) 00 00
Private RSA Key [Signature key]
Object Flags : [0x3], private, modifiable
Usage : [0x20C], sign, signRecover, nonRepudiation
Access Flags : [0x1D], sensitive, alwaysSensitive, neverExtract, local
ModLength : 4096
Key ref : 0 (0x0)
Native : yes
Auth ID : 01
ID : 01
MD:guid : 9cb8d513-372a-e0f8-8916-07812208e323
Hi @szszszsz @hongquan
I've updated gpg2 to 2.1.20. But when I try to generate new keys, I get the following errors:
Nitrokey 1: GPG: error clearing forced signature PIN flag: No pinentry
Nitrokey 2: GPG: error checking the PIN: No pinentry
I've also tried to add a gpg-agent.conf with a pinentry-program line without success:
pinentry-program /usr/bin/pinentry-gtk-2
Do you have any hints? What is going wrong here?
Thx & cheers
No worries, killing and restarting the gpg-agent solved the problem :). It works! GPG2 2.1.20 is able to generate 4096 bit RSA keys. Thx a lot @hongquan and @szszszsz. I will also try to import 4096 bit keys and let you know.
Importing 4096 bit RSA keys works also fine. But, as a precondition, it must be ensured that the Nitrokey was initialised with a 4096 bit RSA key beforehand, e.g. via GPG2. Otherwise, I experienced the error message that says that the requested key length is not supported.
Otherwise, I experienced the error message that says that the requested key length is not supported.
Could you please provide log?
no activity since more than 4 weeks