Skip to content
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

Fix C_Login() crashing when using uninitialized card #2045

Closed
wants to merge 2 commits into from

Conversation

llogar
Copy link
Contributor

@llogar llogar commented May 29, 2020

pkcs11-tool --init-pin ... crashes if the card hasn't been initialized beforehand with pkcs11-tool --init-token ... (or by any other means) as p15card == NULL

Tested on IsoApplet javacard.

Checklist
  • Documentation is added or updated
  • New files have a LGPL 2.1 license statement
  • PKCS#11 module is tested
  • Windows minidriver is tested
  • macOS tokend is tested

@Jakuje
Copy link
Member

Jakuje commented May 29, 2020

The code change looks good.

At this moment, the tests for iso-applet are not very extensive, but would it make sense to try to add a regression test with a reproducer?

The least work would be dumping few commands into the .travis.yml, but much better would be creating some bash script containing these tests with comments that would be invoked from the .travis.yml. You are certainly more familiar with ISO applet so I believe you would be able to come up with few more use cases that would be nice to test.

@frankmorgner
Copy link
Member

Did you check the other PKCS#11 entry points for this possible problem?

@llogar
Copy link
Contributor Author

llogar commented Jun 1, 2020

@Jakuje:

At this moment, the tests for iso-applet are not very extensive, but would it make sense to try to add a regression test with a reproducer?

I don't know. I've only noticed this as I am using a modified version of IsoApplet where among other improvements I can use PKCS#11 functions to initialise the token (C_InitToken & C_InitPin) instead of the IsoApplet "official" pkcs15-init --create-pkcs15 .... And also due to the issue below you actually couldn't invoke a C_InitToken() on an applet running in a simulator (and catch this bug):

ATR used in jcardsim config (3B:80:80:01:01) clashes with SmartCard-HSM with fingerprint sensor and PIN pad from opensc.conf which can have some undesired side effects - for example pkcs11_enable_InitToken setting is not honored in card_detect(). It would probably be wise to change it to something else.

The least work would be dumping few commands into the .travis.yml, but much better would be creating some bash script containing these tests with comments that would be invoked from the .travis.yml. You are certainly more familiar with ISO applet so I believe you would be able to come up with few more use cases that would be nice to test.

Perhaps adding pin change and pin unblock tests?

pkcs15-tool --change-pin --pin 123456 --new-pin 654321
pkcs15-tool --unblock-pin --puk 0123456789abcdef --new-pin 123456

Apart from these, I think the basics are pretty much covered...

@frankmorgner: No, but now that you have mentioned it, I have tried a couple more possible problematic scenarios (very unsystematic, I know) and it didn't crash. So I hope this was the only one...

@Jakuje
Copy link
Member

Jakuje commented Jun 1, 2020

Can you add these two cheks to the test and rebase on current master to get the CI green?

@Jakuje
Copy link
Member

Jakuje commented Jun 2, 2020

Good to see the results green and check added. Can you rebase the change on top of current master (instead of the merge commit you did?). Just git rebase upstream/master in your branch should do that.

Luka Logar added 2 commits June 2, 2020 12:03
pkcs11-tool --init-pin ... crashes if the card hasn't been initialized beforehand with pkcs11-tool --init-token
as p15card == NULL
...so it does not clash with "SmartCard-HSM with fingerprint sensor and PIN pad"
@@ -153,7 +153,7 @@ before_script:
javac -classpath jcardsim/target/jcardsim-3.0.5-SNAPSHOT.jar IsoApplet/src/net/pwendland/javacard/pki/isoapplet/*.java;
echo "com.licel.jcardsim.card.applet.0.AID=F276A288BCFBA69D34F31001" > isoapplet_jcardsim.cfg;
echo "com.licel.jcardsim.card.applet.0.Class=net.pwendland.javacard.pki.isoapplet.IsoApplet" >> isoapplet_jcardsim.cfg;
echo "com.licel.jcardsim.card.ATR=3B80800101" >> isoapplet_jcardsim.cfg;
echo "com.licel.jcardsim.card.ATR=3B8B8001805949736F4170706C6574BA" >> isoapplet_jcardsim.cfg;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you choose this ATR? I think, IsoApplet doesn't require any specific one...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose this specific ATR for no reason, any unique one would do.
As I explained in my answer to @Jakuje, If you leave the stock ATR, then you can not initialize the token using PKCS#11 functions (C_InitToken & C_InitPin) as pkcs11_enable_InitToken is taken from the card_atr 3B:80:80:01:01 section which "belongs" to one of the SmartCard-HSMs, unless of course you add pkcs11_enable_InitToken=yes to that section...

@frankmorgner
Copy link
Member

Sigh, this seems to be a generic problem. Almost nowhere there's a check for p15_card != NULL 🙄

Also, I think we should to change the return code to CKR_TOKEN_NOT_RECOGNIZED

frankmorgner added a commit to frankmorgner/OpenSC that referenced this pull request Jun 2, 2020
@frankmorgner
Copy link
Member

I think I've found all the missing checks. Could you check if #2049 works as well? I've also changed back the ATR; having the same ATR for different cards with different applets should not be a problem.

frankmorgner added a commit to frankmorgner/OpenSC that referenced this pull request Jun 2, 2020
@llogar
Copy link
Contributor Author

llogar commented Jun 2, 2020

Also, I think we should to change the return code to CKR_TOKEN_NOT_RECOGNIZED

per specification CKR_TOKEN_NOT_RECOGNIZED is not among the possible return codes of C_Login(). I don't know how strict are we about this...

@frankmorgner
Copy link
Member

ATR used in jcardsim config (3B:80:80:01:01) clashes with SmartCard-HSM with fingerprint sensor and PIN pad from opensc.conf which can have some undesired side effects - for example pkcs11_enable_InitToken setting is not honored in card_detect(). It would probably be wise to change it to something else.

3B:80:80:01:01 is an unspecific ATR, which is why we're catching this ATR in card-sc-hsm.c only for specific stuff, but not for locking this into a specific card type. We have removed this from the default configuration. I think currently there's no way to avoid modifying opensc.conf for initializing a token via PKCS#11. This would mean that you can use any (specific or unspecific ATR to do this), right?

@frankmorgner
Copy link
Member

per specification CKR_TOKEN_NOT_RECOGNIZED is not among the possible return codes of C_Login(). I don't know how strict are we about this...

Thanks for the pointer, I've now checked the return of CKR_TOKEN_NOT_RECOGNIZED to match the specified return values.

@llogar
Copy link
Contributor Author

llogar commented Jun 2, 2020

3B:80:80:01:01 is an unspecific ATR, which is why we're catching this ATR in card-sc-hsm.c only for specific stuff, but not for locking this into a specific card type. We have removed this from the default configuration. I think currently there's no way to avoid modifying opensc.conf for initializing a token via PKCS#11. This would mean that you can use any (specific or unspecific ATR to do this), right?

Right. And this brings me back to #1543.

mouse07410 pushed a commit to mouse07410/OpenSC that referenced this pull request Jun 8, 2020
@llogar llogar mentioned this pull request Oct 19, 2021
5 tasks
@llogar llogar deleted the fix1 branch November 14, 2023 21:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants