SmartCard-HSM card fails when generating ECC keypair

Bug #1312462 reported by Gert van Dijk on 2014-04-24
12
This bug affects 2 people
Affects Status Importance Assigned to Milestone
opensc (Ubuntu)
Undecided
Unassigned

Bug Description

Generating an ECC keypair on-card will result in a private key on the card, but it does not list the public key (pkcs15-tool -D, pkcs11-tool --list-objects). Possibly related to #1311921.

Steps to reproduce:

1. Generate an ECC keypair will produce a warning:

$ pkcs11-tool --module opensc-pkcs11.so -l --keypairgen --key-type EC:prime256v1 --label "My first EC key" --id 12

Outputs a warning which is unexpected:

Using slot 1 with a present token (0x1)
Logging in to "SmartCard-HSM (UserPIN)".
Please enter User PIN:
Key pair generated:
Private Key Object; EC
  label: My first EC key
  ID: 12
  Usage: decrypt, sign, unwrap
Public Key Object; EC EC_POINT 264 bits
 EC_POINT: 04430441049c70fbabddb007a398c370bcc61f9182b2da7c1146921d36580ab03ec66093a1724cc7b3a04ee0b49965c21996f9fa3580a0bde3c4b708fe4cf5f023a3e47005
warning: PKCS11 function C_GetAttributeValue(EC_PARAMS) failed: rv = CKR_ATTRIBUTE_TYPE_INVALID (0x12)

  label: My first EC key
  ID: 12
  Usage: encrypt, verify, wrap

2. Verifying both the public and private key to be listed fails. Only my RSA public keys are listed.

$ pkcs15-tool -D
[...]
Private RSA Key [Private Key]
[...]
Private RSA Key [rsa1024 test]
[...]
Private EC Key [My first EC key]
[...]
Public RSA Key [Private Key]
[...]
Public RSA Key [rsa1024 test]
[...]

$ pkcs11-tool --module opensc-pkcs11.so --list-objects
Using slot 1 with a present token (0x1)
Public Key Object; RSA 2048 bits
  label: Private Key
  ID: 10
  Usage: none
Public Key Object; RSA 1024 bits
  label: rsa1024 test
  ID: 11
  Usage: none

Applying https://github.com/OpenSC/OpenSC/commit/b34d916e60a6c3fc281e051ce1447191584a85a5 on top of 0.13.0-3ubuntu4 (together with my debdiff in https://bugs.launchpad.net/ubuntu/+source/opensc/+bug/1311921/comments/1 ) does not fix it for me, unfortunately.

I'm currently researching on how to get this working with a minimal set of patches from upstream.

Launchpad Janitor (janitor) wrote :

Status changed to 'Confirmed' because the bug affects multiple users.

Changed in opensc (Ubuntu):
status: New → Confirmed
Jonathan Davies (jpds) wrote :

I did some tests with the latest Git code and something interesting is happening:

Generating a key with Ubuntu packages (0.13.0-3ubuntu4.1):

$ pkcs11-tool --module opensc-pkcs11.so -l --keypairgen --key-type EC:prime256v1 --label "My first EC key"
Using slot 1 with a present token (0x1)
Logging in to "SmartCard-HSM (UserPIN)".
Please enter User PIN:
Key pair generated:
Private Key Object; EC
  label: My first EC key
  ID: foobar
  Usage: decrypt, sign, unwrap
Public Key Object; EC EC_POINT 264 bits
 EC_POINT: 0443044104eb915ffd349158919584cf51915e047784a81a18b3b33804dc318eb0f85608c84f242cba6e88462ed95d15666e0f52659a0abe80b8a0261e30c5b1eab24f80fb
warning: PKCS11 function C_GetAttributeValue(EC_PARAMS) failed: rv = CKR_ATTRIBUTE_TYPE_INVALID (0x12)

  label: My first EC key
  ID: foobar
  Usage: encrypt, verify, wrap

$ pkcs15-tool -D
...
Private EC Key [My first EC key]
...

But no public key.

Now using pkcs15-tool from upstream Git:

$ ./src/tools/pkcs15-tool -D
...
Public EC Key [My first EC key]
        Object Flags : [0x0]
        Usage : [0x0]
        Access Flags : [0x2], extract
        FieldLength : 256
        Key ref : 0 (0x0)
        Native : no
        ID : foobar
        DirectValue : <present>

Trying to generate a key with latest upstream code:

$ ./src/tools/pkcs11-tool --module opensc-pkcs11.so -l --keypairgen --key-type EC:prime256v1 --label "My first EC key"
Using slot 1 with a present token (0x1)
Logging in to "SmartCard-HSM (UserPIN)".
Please enter User PIN:
error: PKCS11 function C_GenerateKeyPair failed: rv = CKR_FUNCTION_NOT_SUPPORTED (0x54)

Aborting.

Jonathan Davies (jpds) wrote :

The latter seems related to:

0x7ff363a22740 11:30:29.006 [opensc-pkcs11] pkcs15-lib.c:436:sc_pkcs15init_set_p15card: called
0x7ff363a22740 11:30:29.006 [opensc-pkcs11] pkcs15-lib.c:470:sc_pkcs15init_set_p15card: sc_pkcs15init_set_p15card() returns
0x7ff363a22740 11:30:29.006 [opensc-pkcs11] framework-pkcs15.c:2680:pkcs15_gen_keypair: Try on-card key pair generation
0x7ff363a22740 11:30:29.006 [opensc-pkcs11] pkcs15-lib.c:1271:sc_pkcs15init_generate_key: called
0x7ff363a22740 11:30:29.006 [opensc-pkcs11] pkcs15-lib.c:1993:check_keygen_params_consistency: returning with: -1408 (Not supported)
0x7ff363a22740 11:30:29.006 [opensc-pkcs11] pkcs15-lib.c:1274:sc_pkcs15init_generate_key: Invalid key size: -1408 (Not supported)
0x7ff363a22740 11:30:29.006 [opensc-pkcs11] framework-pkcs15.c:2693:pkcs15_gen_keypair: sc_pkcs15init_generate_key returned -1408
0x7ff363a22740 11:30:29.006 [opensc-pkcs11] misc.c:61:sc_to_cryptoki_error_common: libopensc return value: -1408 (Not supported)

Gert van Dijk (gertvdijk) wrote :

Yes, I think I've seen similar results when doing some testing around the time of this bug report with OpenSC upstream, OpenSC-CardContact upstream and Ubuntu's 0.13.0 version. I did not manage to find a set of patches to get it working properly with EC keys, even with CardContact's OpenSC. https://github.com/CardContact/OpenSC

OpenSC has changed a lot internally between 0.13.0 and master. For me this was too much to nail down the issue.

The main patch to add support for persistent ECC public keys is in [1], however a lot of the ECC code was changed in the patch series around Feb 9, 2014.

The safest ways is to change to the stable 0.14 version released a couple of weeks ago. Version 0.13 is about two years old.

Andreas

[1] https://github.com/OpenSC/OpenSC/commit/67dc60f70780e14a04a62945362fd98a3c1251f5

Download full text (8.3 KiB)

Just did a quick test on my machine running OpenSC 0.14:

asc@calzone:~/tmp/ecctest$ sc-hsm-tool
Using reader with a card: SCM SCR 3310 [CCID Interface] (21120843305113) 00 00
Version : 1.2
User PIN tries left : 3

asc@calzone:~/tmp/ecctest$ pkcs11-tool --module /usr/local/lib/opensc-pkcs11.so -l --pin 648219 --keypairgen --key-type EC:prime256v1 --id 12 --label defaultkey --read-object --type pubkey --output-file pubkey.spki
Using slot 0 with a present token (0x0)
Key pair generated:
Private Key Object; EC
  label: defaultkey
  ID: 12
  Usage: decrypt, sign, unwrap
Public Key Object; EC EC_POINT 256 bits
  EC_POINT: 0441044e68fd16a81555c2bf448d25b767572c398ac9706298c748d9bd3c88557bb161bef155491140fdc2541c5f032d73abcedd4b6540021b615c2467bbf9bf04c5b8
  EC_PARAMS: 06082a8648ce3d030107
  label: defaultkey
  ID: 12
  Usage: encrypt, verify, wrap

asc@calzone:~/tmp/ecctest$ hexdump pubkey.spki
0000000 4104 4e04 fd68 a816 5515 bfc2 8d44 b725
0000010 5767 392c c98a 6270 c798 d948 3cbd 5588
0000020 b17b be61 55f1 1149 fd40 54c2 5f1c 2d03
0000030 ab73 ddce 654b 0240 611b 245c bb67 bff9
0000040 c504 00b8
0000043

asc@calzone:~/tmp/ecctest$ pkcs15-tool -D
Using reader with a card: SCM SCR 3310 [CCID Interface] (21120843305113) 00 00
PKCS#15 Card [SmartCard-HSM]:
        Version : 0
        Serial number : UTCC0200013
        Manufacturer ID: www.CardContact.de
        Flags :

PIN [UserPIN]
        Object Flags : [0x3], private, modifiable
        ID : 01
        Flags : [0x81A], local, unblock-disabled, initialized, exchangeRefData
        Length : min_len:6, max_len:15, stored_len:0
        Pad char : 0x00
        Reference : 129 (0x81)
        Type : ascii-numeric
        Tries left : 3

PIN [SOPIN]
        Object Flags : [0x1], private
        ID : 02 ...

Read more...

To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers