-
Notifications
You must be signed in to change notification settings - Fork 736
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IDPrime 840 failure to login: PKCS11 function C_Login failed: rv = CKR_DATA_LEN_RANGE (0x21) #2202
Comments
How long is your pin? The IDPrime driver should accept pins from 4 to 16 characters: https://github.com/OpenSC/OpenSC/blob/master/src/libopensc/pkcs15-idprime.c#L98 I used a bit different version of IDPrime (probably Gemalto IDPrime 3810) with slightly different ATR: https://smartcard-atr.apdu.fr/parse?ATR=3B+7F+96+00+00+80+31+80+65+B0+84+41+3D+F6+12+0F+FE+82+90+00 Unfortunately, the debug log now excludes the PIN APDUs so it is not clear whether the error is returned by the driver or by the card. I see also
which comes from here: https://github.com/OpenSC/OpenSC/blob/master/src/libopensc/card-idprime.c#L256 I tested with OS version 1 and 2, but I do not know what differences in PIN handling are in the OS version 3. You can get complete debug log including Verify APDUs with OPENSC_DEBUG=9 (note, that it will include actual PIN!). If you have official driver, you can try to log APDUs it sends and compare it with the Verify APDU sent by OpenSC. If there will be some difference, it should be easy to update OpenSC to work also with your card. |
I was suspecting the pin length might be the issue. When I submitted the bug report it was 4 characters long. Afterwards I changed the pin on windows with Gemalto's MiniDriver to 6 characters long and retested but I still got the same error.
Here's a with debug = 9. I changed the PIN just in case my redacting capabilities were insufficient. About the official driver - I did get an installer package, but I'll need some guidance how to log the actual APDUs. I think I can also do that on linux and windows, if you can give me a guide. |
If you debug on linux, simplest way is to start pcscd in a separate window. (You may have to kill it first.) The run a test using their driver. Then running a simple test to query the login state which returns the number of retries before the card is locked:
From your debug log the command would be: There are a number of different way a cards a card handles a PIN. In you debug log it shows the driver is sending just the pin. I assume you redacted the pin to be 00 00 00 00 00 00. i.e. 00 20 00 11 06 00 00 00 00 00 00 Assuming your pin is 123456, the above should look like Another way is sending "FF" padded version of the pin like: If the max is really 16, then You may want to try this, but be careful not to send one of these with the wrong pin, as you could lock the card.
|
Thanks so much for your help! Here's my findings: The correct APDU to query for remaining pin attempts is: i.e. it is zero padded. I'll start working on a PR. I'd create a new model - V3, but if you can give me some direction where I can adjust the APDU sequences I'd be very grateful! |
…with zeroes) Fixes OpenSC#2202
…with zeroes) Fixes OpenSC#2202
…with zeroes) Fixes OpenSC#2202
Thanks for checking. I tried that padding with zeroes to 16 bytes does not work with my (v1) card. Can you try the following commit if it works for you: d6bc421 (from this branch: https://github.com/Jakuje/OpenSC/tree/idprime-v3) |
On Linux both your branch and the 0.21.0 release give me The last APDUs I see is:
I'm currently downloading XCode and I'll try to compile it and report back. On macOS I do get to the PIN prompt. |
Are you trying the |
I compiled your branch on OS X there is some progress: Listing objects I tried using the pkc11 module from firefox but that fails with the general test fails too:
I've uploaded the (I think) relevant section of the debug=9 log here. Let me know if I missed something On linux
|
Good to hear that.
Yeah, that is the actual sign operation, which goes wrong for some reason. It looks like this is another place where the card will behave a bit differently. From your log:
The SW 0x6A88 means DATA NOT FOUND so it sounds like the key reference is wrong in this case. But this key reference is set in the previous APDU (set security environment -- The key reference is already magic number in both versions I had a chance to interact with so it is quite expected that it will need some tweak also for a new version. I think the official driver provides also a pkcs11 module, so you should be able to run the opensc's pkcs11-tool tests on the proprietary module, logging APDUs also for the signature operations to get what key references are used. The APDU for setting security environment is here (0x22): https://github.com/OpenSC/OpenSC/blob/master/src/libopensc/iso7816.c#L835 It will look like:
The "magic" code for getting the right key reference for v1 and v2 cards is here: https://github.com/OpenSC/OpenSC/blob/master/src/libopensc/card-idprime.c#L198 It might already help to try the same method that is used with V2 cards (at this moment, the V1 is used for unknown versions).
This sounds suspicious. |
Gist updated. I removed only the certificates.
Indeed I have a propriatery module as well. By tests do you mean, to run
I'll get you a debug=9 on linux as well. I just want to clarify that I'm running linux under virtualbox and passing through the USB card reader device. Although I doubt this should have any impact. If you suspect it might be an issue, I have a raspberry Pi which I can run it "natively". |
I updated my branch with the changed key reference for V3: https://github.com/Jakuje/OpenSC/commits/idprime-v3
Yes. Sorry, I wanted to add a command-line arguments, but forgot.
Thanks. Using Virtualbox with pass-through USB should not cause any issues I would know about. I used that (the other way round) in qemu for some work without any issues. |
I cleaned, pulled and recompiled but (without looking at the debug log) I get the same error message.
|
It still fails with the same error (even though there is different key reference) so we will need a log from proprietary driver. From there:
There are two keys, one with key reference 0xF7 (247) and the other with 0xF8 (248) (the last byte) In OpenSC the keys have reference 0x31 (49) and 0x32 (50)
(the first key is used in the opensc log) I updated my branch with an attempt to guess correct key reference for your card. Unfortunately, I did not figure out any better way than using another magic numbers. For the Linux log, it looks like the error comes from here:
The functions around registering mechanisms do not provide much logging, but I assume it is because you do not have openssl devel package installed and therefore it fails to add the RSA-PSS mechanisms in Additionally, there is
which prevents the idprime driver the decompress the certificate. They are stored in compressed format on the card and zlib devel package is needed for building opensc with support for compressed certificates. |
Sorry about the missing libraries - the Windows application showed "uncompressed" in the UI, but perhaps that's wrong. Linux: OpenSC Debug One thing I noticed is that when using the proprietary driver pkcs11-tool says "Logging in to "CARD ..." while the OpenSC driver says "Logging in to "Name on certificate". I don't know whether this has any relevance. P.S. for some reason pkcs11-tool does not generate a debug file when the proprietary module is used. To me it makes sense - likely the proprietary module doesn't have that functionality at all. |
MacOS Debug - this time properly compiled with OpenSSL and ZLIB |
Do I read it right that the current version with changes in the branch works for you now?
This is just the name of the token presented by the pkcs11 module -- opensc tries to make it user friendly by providing the name from certificate, while the propriatery driver probably does not bother.
the OPENSC_DEBUG is generated by the opensc pkcs11 library, not by the pkcs11-tool. The last log from #2202 (comment) is missing couple of first columns, especially the APDUs. |
I think the only thing that does not work correctly at present is the pkcs11 module that I use in Firefox for identification purposes. Firefox gives me I tried signing documents with an external software that works with OpenSC - signing works. I'm a bit puzzled why Firefox fails to work. |
On Linux I tried the PKCS11 authentication with OpenSC vs Proprietary, here's both logs: What I see is a difference in the APDUs sent - Gist with both logs OpenSC sends: The 8th number is 45, and not 00. I have no idea how relevant this is. |
The 8th byte is algorithm reference (I wrote this wrongly in the comment yesterday #2202 (comment)). The value 0x45 means RSA-PSS with SHA256: https://github.com/OpenSC/OpenSC/blob/master/src/libopensc/card-idprime.c#L637 But the value 0x00 is suspicious as we do not have this value in our the above code. It might be that the card attempts to use raw-RSA or something. In any case, the response to the following APDU is 0x6985, which means CONDITION_NOT_SATISFIED (whatever it means). I do not see this APDU in the previous debug logs. You can use Output from |
Output of
|
The mechanisms look good. From the log:
mechanism 0xd is
The flags here mean
What we are interested in are the last sec flag, which is only I have tests for RSA_PSS mechanisms in p11test ( |
Well thanks a lot for the work you've done so far! I do have to admit that this issue is out of my league, but I'll be standing by to test and provide logs if you come up with a solution! |
The use of CKM_RSA_PKCS_PSS is another reason to change the "flags" from 32 to 64 bits, so a card driver can register the PSS hash it supports. (alg_info->flags:0x00e0e032 pad:0x00000000 sec:0x00000010) |
AFAIK the flags are already there https://github.com/OpenSC/OpenSC/blob/master/src/libopensc/card-idprime.c#L305 They are already needed for the |
Missed that. This might work. (not tested or compiled.)
Split the iflags up between sflags done by card, and pflags done by software. |
Thanks. I used the above change and it looks like working fine for me (after updating the driver to use the MGF1 values rather than the HASH ones, which are not present for this mechanism. |
I can confirm that with the latest commit it works under both Linux and MacOS! P.S. It doesn't seem to be usable in Safari/Mail or found by the
|
Thanks for verifying the fix. I used the p11test to check all works fine. It has the whole matrix of HASH/MGF1/salt_length combinations, but just couple of them work in OpenSC generally. I think the error from |
@Jakuje Thank you! I also managed to get the certificate to show up in Safari and Mail and in |
Some more feedback regarding macOS: I installed the latest nightly which includes the v3 code. Safari does display the certificate selection screen but never asks for a pin. On the other hand Other than that the Firefox security module is working fine and so do the signing applications. |
Thanks for verification. Probably yes. I do not know what Safari and macOS do with smart cards. |
Problem Description
I have a Gemalto IDPrime 840 card used via Gemalto IDBridge K30 sim-sized card reader. OpenSC is able to identify the card, show me the data that is on the card, but whenever I try to log in (i.e. enter PIN) I'm greated with the above mentioned error.
I get the same error on a Intel MacBook Pro running macOS 10.15, as well as on a M1 MacBook Pro running macOS 11.
I tried with OpenSC 0.21.0 as well as the latest nightly, as requested in https://github.com/OpenSC/OpenSC/wiki/How-to-write-a-good-bug-report, same result.
What I also did was check the ATTR of the card at https://smartcard-atr.apdu.fr - it is indeed recognized as Gemalto IDPrime MD 840 (PKI).
I have to mention that all other commands which I tried and do not require entering a PIN do work, e.g.:
opensc-tool -l
opensc-tool -n
pkcs15-tool -D
opensc-tool -a
Proposed Resolution
N/A
Steps to reproduce
pkcs11-tool --login --test
Logs
Gist with debug = 3
The command-line error from
pkcs11-tool
The text was updated successfully, but these errors were encountered: