While generating ecc keys works with pkcs15-init on OpenPGP Card based cards, pkcs11-tool does not, e.g.
pkcs11-tool -l --login-type so --so-pin 12345678 --keypairgen --key-type EC:nistp256 --id 03
in comparison to
pkcs15-init --verify --auth-id 3 --pin 12345678 --delete-objects privkey,pubkey --id 1 --generate-key ec/nistp256
Fix whatever stops pkcs11-tool to work here. Honestly, I did not had the time to look into it yet, but wanted to have the problem noted here, to not forget to look into it. If somebody already spots the problem, fine :)
Try above pkcs11-tool command with a OpenPGP Card v3.3 device (e.g. Nitrokey Pro 2 or Nitrokey Storage 2). OpenSC 0.20 on ArchLinux
Potentially similar: https://github.com/OpenSC/OpenSC/pull/1960 https://github.com/OpenSC/OpenSC/issues/1982
Perhaps one of the unmerged solutions would work?
Nitrokey forum ticket: #2355
"nistp256" is not listed in pkcs11-tool.c ec_curve_info but other names for the curve are:
94 {"prime256v1", "1.2.840.10045.3.1.7", "06082A8648CE3D030107", 256},
95 {"secp256r1", "1.2.840.10045.3.1.7", "06082A8648CE3D030107", 256},
96 {"ansiX9p256r1", "1.2.840.10045.3.1.7", "06082A8648CE3D030107", 256},
You could also use --key-type EC:1.2.840.10045.3.1.7
This is not working for me (opensc master and opensc 0.20.0). It keeps stating that it is not supported:
$ pkcs11-tool -l --login-type so --so-pin 12345678 --keypairgen --key-type EC:1.2.840.10045.3.1.7 --id 03
Using slot 0 with a present token (0x0)
error: Generate EC key mechanism not supported
Aborting.
$ pkcs11-tool -l --login-type so --so-pin 12345678 --keypairgen --key-type EC:prime256v1 --id 03
Using slot 0 with a present token (0x0)
error: Generate EC key mechanism not supported
Aborting.
I have only Nitrokey start (now loaded with eddsa keys). This sounds like there are missing mechanisms provided by the driver. What is the output of pkcs11-tool -M?
Indeed, it looks like mechanisms are not listed. What's more, Nitrokey Pro and Storage devices use the same OpenPGP card version, but have different output from pkcs11-tool -M.
Logs attached.
From your logs I see the Pro2 uses version OpenPGP 3.3, while the others use 2.1. Or do I miss something?
Certainly it looks like the missing mechanisms is a bug. The logs and outputs are from 0.20.0? Can you try with master or #1960 ?
Hi! Sorry for the delay.
Retest done with: 0.20.0-332-ge6e70d67 (current tip of #1960). Supported mechanisms show up, but the CKR_FUNCTION_NOT_SUPPORTED error is still present on the actual key generation call unfortunately. Full log: opensc1960-full.txt
Additional log with level 9 debug output set: opensc-2020-with1960-opensc_debug9-full.txt. Here I believe tool is complaining about already used slots.
After a factory reset and with a detailed log I have another error: error: Generate EC key mechanism 1040 not supported. Log after factory reset: opensc2020-with1960-debug9-after-factory-reset.txt
The first error looks like coming from here:
P:6253; T:0x140172670891840 14:55:45.895 [opensc-pkcs11] pkcs15-lib.c:1481:sc_pkcs15init_generate_key: Non unique ID of the private key
object: -1505 (Non unique object ID)
The other log does no list the mechanisms again. It could be that the current version derives mechanisms from existing objects and after factory reset, no keys are present and therefore no (keygen) mechanisms are enabled. That is indeed wrong as keygen mechanisms should not depend on key objects. Let me check.
@szszszsz it is this condition that makes it working inside of pkcs15-init, but not from pkcs11 module:
It was introduced by @alex-nitrokey in b572b383 so maybe he will be able to comment on some logic behind this.
Oh dear, now I remember -- more or less. I think I was asked by @frankmorgner to add this to limit the function to pkcs15-init indeed, as other tools would not work otherwise (regarding the past state of OpenSC and ECC). I couldn't find the old discussion and that is all I remember for now...
Good catch! Well then, @szszszsz will test without this line of code, let's see how it is working.
Ah, okay, I found it:
https://github.com/OpenSC/OpenSC/pull/1442#issuecomment-415530380
The problem here is that OpenPGP Cards are not able to actually list all supported algorithms, so what we are doing is to deduce the most likely algorithms supported by the card's specification. If one is not actually working the card will just fail gracefully. @frankmorgner asked to add this make sure that this is not breaking anything. Maybe he can let us know if he still thinks we should keep this in.
The problem here is that OpenPGP Cards are not able to actually list all supported algorithms
BTW, this should not be the case anymore with OpenPGP Card 3.4, which supports such listing. However, it is not implemented in OpenSC yet.
Most helpful comment
@szszszsz it is this condition that makes it working inside of pkcs15-init, but not from pkcs11 module:
https://github.com/OpenSC/OpenSC/blob/1bb2547abca12f3ce22d48c3c171ea5e44ab4c4a/src/libopensc/card-openpgp.c#L487
It was introduced by @alex-nitrokey in b572b383 so maybe he will be able to comment on some logic behind this.