vletoux / GidsApplet

Generic Identity Device Specification Applet
GNU General Public License v3.0
102 stars 38 forks source link

Ability to import an RSA/4096 key? #17

Closed rpavlik closed 10 months ago

rpavlik commented 11 months ago

Thanks for this great work! (And sorry for breaking your issue-free streak, hopefully it's just user error :wink: ) I've managed to get it going with an on-card generated 4096-bit RSA key and OpenSC as follows:

However, if I replace that last step with pkcs15-init -v -v --verify-pin --store-private-key pyprivate_ca.pem --auth-id 80 --key-usage sign --label test where pyprivate_ca.pem is a 4096-bit private key (dumped from py crypto), I eventually get this from OpenSC:

P:2735428; T:0x140230475139136 11:02:20.472 [pkcs15-init] card-gids.c:1537:gids_import_key: unable to put the private key - key greater than 2048 bits ?: -1217 (Not enough memory on card)
Failed to store private key: Not enough memory on card

I'm using a 180K J3R180 card https://www.amazon.com/dp/B0CFFCJ9W1 so I would think the actual card space is OK, though perhaps the applet doesn't allocate enough.

Strangely, after I do this, pkcs15-tool --dump seems to suggest the key is there anyway:

PKCS#15 Card [GIDS card]:
    Version        : 2
    Serial number  : f5e011e64b2b0dd153d85205f3f1fd86
    Manufacturer ID: www.mysmartlogon.com
    Flags          : 

PIN [UserPIN]
    Object Flags   : [0x03], private, modifiable
    ID             : 80
    Flags          : [0x12], local, initialized
    Length         : min_len:4, max_len:15, stored_len:0
    Pad char       : 0x00
    Reference      : 128 (0x80)
    Type           : ascii-numeric
    Tries left     : 3

Private RSA Key [test]
    Object Flags   : [0x01], private
    Usage          : [0x04], sign
    Access Flags   : [0x1D], sensitive, alwaysSensitive, neverExtract, local
    Algo_refs      : 0
    ModLength      : 4096
    Key ref        : 129 (0x81)
    Native         : yes
    Auth ID        : 80
    ID             : 00
    MD:guid        : 4bd0fb77-a08c-5848-733e-98a23e7df51c

Public RSA Key [test]
    Object Flags   : [0x00]
    Usage          : [0x40], verify
    Access Flags   : [0x02], extract
    ModLength      : 4096
    Key ref        : 129 (0x81)
    Native         : yes
    Path           : 3fffb081
    ID             : 00

I did not try actually using it yet. I did find I could not delete it with pkcs15-init without gp --uninstall GidsApplet.cap.

Updates:

rpavlik commented 11 months ago

OK, I did a little binary search until I got tired of re-flashing the card :wink: . 2372 is enough buffer, but 2364 is not, so it's somewhere in between there. I suppose this makes sense, a little bit less than double the buffer needed for 2048 (some overhead is presumably not duplicated). My change is here: https://github.com/rpavlik/GidsApplet/tree/increase-buf-size

In case you couldn't tell, this is definitely not something I am experienced at, so the buffer size is about all I will be messing around with at this time. That said, it would be nice to see some of the comment and security fixes from https://github.com/JavaCardSpot-dev/GidsApplet picked into this tree. There are also some differences from the IsoApplet that I'm not sure whose code is better - there are a few places where they've added transactions, etc. even in the "old version" branch.

rpavlik commented 10 months ago

Closing since #21 was merged. thanks!