Import a private EC key using PKCS#11 into Nitrokey Start

I’m trying to import an EC key into my new Nitrokey Start using a C# application with the pkcs#11Interop library and OpenSC but I get a CKR_GENERAL_ERROR as a result. What am I missing or doing wrong?

The pkcs11-spy.log looks like this: (I replaced the key value in CKA_VALUE from the log entry for obvious reasons :smile:)

12: C_OpenSession
P:20088; T:26324 2024-11-14 13:25:13.477
[in] slotID = 0x4
[in] flags = 0x6
[in] pApplication = 0000000000000000
[in] Notify = 0000000000000000
[out] *phSession = 0x5fb130
Returned:  0 CKR_OK

13: C_Login
P:20088; T:26324 2024-11-14 13:25:13.478
[in] hSession = 0x5fb130
[in] userType = CKU_SO
[in] pPin[ulPinLen] 00000268365e6358 / 8
    00000000  31 32 33 34 35 36 37 38                          12345678        
Returned:  0 CKR_OK

14: C_CreateObject
P:20088; T:26324 2024-11-14 13:25:13.497
[in] hSession = 0x5fb130
[in] pTemplate[9]: 
    CKA_CLASS             CKO_PRIVATE_KEY      
    CKA_KEY_TYPE          CKK_EC             
    CKA_LABEL             00000268005dbc90 / 0
    CKA_ID                00000268005dbca0 / 2
    00000000  01 02                                                          
    CKA_TOKEN             True
    CKA_SENSITIVE         True
    CKA_DERIVE            True
    CKA_EC_PARAMS         00000268025b2290 / 10
    00000000  06 08 2A 86 48 CE 3D 03 01 07                          
    CKA_VALUE             00000268025700d0 / 32
    00000000  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  
    00000010  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  
Returned:  5 CKR_GENERAL_ERROR

The key has been generated with BouncyCastle. It is a SecP256R1Curve key:
Key algorithm: EC
Curve name: Org.BouncyCastle.Math.EC.Custom.Sec.SecP256R1Curve
Key type identifier: 1.2.840.10045.3.1.7

I tried with user login as well as with admin (SO) login.

I can generate and import an EC key using GPG as described in the Nitrokey manual. So the Nitrokey Start is working fine.

Do I miss anything in my pkcs#11 key object?
Do I need a special preparation/initialisation of my Nitrokey Start?

The error message CKR_GENERAL_ERROR is not really helpful.

I tested with OpenSC debug mode and get the following log:

P:35608; T:35644 2024-11-14 17:39:05.281 [opensc-pkcs11] Found known curve 'secp256r1'
P:35608; T:35644 2024-11-14 17:39:05.297 [opensc-pkcs11] Curve length 256
P:35608; T:35644 2024-11-14 17:39:05.298 [opensc-pkcs11] pkcs15-pubkey.c:1589:sc_pkcs15_fix_ec_parameters: returning with: 0 (Success)
P:35608; T:35644 2024-11-14 17:39:05.298 [opensc-pkcs11] Private EC key length 256
P:35608; T:35644 2024-11-14 17:39:05.298 [opensc-pkcs11] pkcs15-lib.c:2563:check_key_compatibility: called
P:35608; T:35644 2024-11-14 17:39:05.298 [opensc-pkcs11] pkcs15-lib.c:2600:check_key_compatibility: returning with: -1406 (Object not valid)
P:35608; T:35644 2024-11-14 17:39:05.298 [opensc-pkcs11] pkcs15-lib.c:1786:sc_pkcs15init_store_private_key: Card does not support this key for crypto. Cannot store it as non extractable.: -1503 (Key length/algorithm not supported by card)
P:35608; T:35644 2024-11-14 17:39:05.315 [opensc-pkcs11] libopensc return value: -1503 (Key length/algorithm not supported by card)
P:35608; T:35644 2024-11-14 17:39:05.315 [opensc-pkcs11] pkcs15-lib.c:435:sc_pkcs15init_unbind: called
P:35608; T:35644 2024-11-14 17:39:05.315 [opensc-pkcs11] Pksc15init Unbind: 0:000001C6022CA910:1
P:35608; T:35644 2024-11-14 17:39:05.315 [opensc-pkcs11] card.c:523:sc_unlock: called

Why is the key not accepted? According to the spec, the Nitrokey Start supports NIST P-256.

Did not dig deeper into this and what upstream version is used, but this Gnuk v2.1 note states removal of NIST P-256.

Gnuk 1.2.19 is being used. So the curve support should still be there.

@szszszsz Is the curve only supported for the generated GPG keys and not for imported keys?