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

Simplify differences between CardOS 5 versions and unbreak 5.3 signatures #1080

Merged
merged 2 commits into from
Jul 6, 2017

Conversation

Jakuje
Copy link
Member

@Jakuje Jakuje commented Jun 26, 2017

This is an alternative approach to the #1079, which resolves the same problem (and I somehow forgot to fill the PR earlier), but also unbreaks the signatures too as stated in #1055.

On the other hand, it "breaks" back the mechanisms advertised by CardOS cards by adding RSA_X_509 to the list which, in fact, is not supported by the card operations.

I would appreciate ideas how to improve this situation sooner or later. Currently, this fact is making the tests in pkcs11-tool failing, because the RSA_X_509 signatures do not work.

This was referenced Jun 26, 2017
@Jakuje Jakuje changed the title Simplify differenced between CardOS 5 versions and unbreak 5.3 signatures Simplify differences between CardOS 5 versions and unbreak 5.3 signatures Jun 26, 2017
@frankmorgner
Copy link
Member

  • 👍 for making the driver code simpler and making CardOS 5.0 support improvement #1079 obsolete
  • But I suspect that the problem with RSA_X_509 could break some real world applications...

Since I'm not a user of this card, I'm somewhat undecided...

@frankmorgner
Copy link
Member

@debrouxl could you have a look?

@Jakuje
Copy link
Member Author

Jakuje commented Jul 3, 2017

@frankmorgner that is why I am asking somebody with better overview about the project architecture. I believe we will see more cards that would not allow using raw signatures.

I can try to add back SC_ALGORITHM_RSA_PAD_PKCS1 to the flags and add the PKCS#1 padding "manually" in the cardos_compute_signature(), which should result in the correct flags and hopefully correct signature. Lets give it a try.

@Jakuje
Copy link
Member Author

Jakuje commented Jul 3, 2017

To clarify what is the CardOS driver trying to do with RSA-X-509 (RSA_SIG internal mechanism):

message[256 B] ->
| OpenSC -> removes PCKCS#1.5 padding, digest info -> message [~30 B] -> card [adds a padding back] |
-> signature [256 B]

The signature can be verified using OpenSSL as a data with valid PKCS#1.5 padding which was added by the card. This works fine for short messages but clearly fails for more than ~100B (that can not be padded? Tested with pkcs11-tool by adding variable padding of length from 155 bytes).

Interesting thing is that if I use the current master, the resulting signature of RSA-PKCS (using RSA_PURE_SIG) are what we would expect from RSA-X-509 (~30 B).

message[~30 B] ->
| OpenSC -> card |
-> signature [~30 B]

But I am not able to verify that signature as a raw using OpenSSL. With this PR and short-enough message I am able to get verifiable signature with some mess around.

So I still do not have any better idea how this can be resolved. Ideas welcomed. I can provide the full logs for specific inputs if it could help. Otherwise I would consider this solution as a "good enough".

@debrouxl
Copy link

debrouxl commented Jul 3, 2017

I've tested these two commits applied on top of current master.
With my CardOS 5.0 card, the sets of commands from https://gist.github.com/Jakuje/5a993d2b2d8a9cac35203599e49e6831 show that signature and validation work, but decryption fails with CKR_GENERAL_ERROR:
"
Using slot 0 with a present token (0x0)
Using decrypt algorithm RSA-PKCS
error: PKCS11 function C_Decrypt failed: rv = CKR_GENERAL_ERROR (0x5)
Aborting.
"
"
Using slot 0 with a present token (0x0)
Using decrypt algorithm RSA-X-509
error: PKCS11 function C_Decrypt failed: rv = CKR_GENERAL_ERROR (0x5)
Aborting.
"

Likewise for pkcs11-tool -tl, as expected.

@Jakuje
Copy link
Member Author

Jakuje commented Jul 3, 2017

Thank you for testing.
That is unfortunately "expected". The decryption methods are not implemented in the CardOS driver so it is using generic ISO methods that do not work here.

I have somewhere a patch that tries to resolve this (by adding a pkcs1 padding back), but we can never know what were the random bytes in padding (which was stripped by the card) so rather than trying to return wrong decrypted message, I would rather say it is not supported (as it never was to my understanding).

@frankmorgner
Copy link
Member

What about applying #1079 as quick fix for 0.17.0 and after the release we can merge this PR (#1080)? This should give us more time for testing with real world applications (or even more time for coming up with a better solution).

@debrouxl
Copy link

debrouxl commented Jul 4, 2017 via email

@Jakuje
Copy link
Member Author

Jakuje commented Jul 4, 2017

The fix in #1079 does not resolve the problem completely. There are also bad flags set up during initialization. They were already present in the #1003, but are fixed in this PR. Applying this PR is that minimal fix that I would like to see in 0.17.0.

The pkcs11-tool --test is failing because of the advertised RSA_X_509 mechanism, which is in fact not supported (for generic data sizes). I am able to make it pass if I modify the data to have proper PKCS#1.5 padding and the message shorter than 100 bytes (see following patch). Then it fails little bit further:

diff --git a/src/tools/pkcs11-tool.c b/src/tools/pkcs11-tool.c
--- a/src/tools/pkcs11-tool.c
+++ b/src/tools/pkcs11-tool.c
@@ -4312,6 +4312,10 @@ static int test_signature(CK_SESSION_HANDLE sess)
 	if (firstMechType == CKM_RSA_X_509) {
 		/* make sure our data is smaller than the modulus */
 		data[0] = 0x00;
+		data[1] = 0x01;
+		for (i = 2; i < 154; i++)
+			data[i] = 0xff;
+		data[i] = 0x00;
 	}
 
 	ck_mech.mechanism = firstMechType;

@frankmorgner
Copy link
Member

OK, then we'll accept this PR. Thanks for giving the details.

The change in pkcs11-tool would only mask the real problem (advertising RSA_X_509 though not supporting it), so I'd rather not do this change.

@frankmorgner frankmorgner merged commit 9d813c0 into OpenSC:master Jul 6, 2017
mouse07410 pushed a commit to mouse07410/OpenSC that referenced this pull request Jul 6, 2017
…ures (OpenSC#1080)

* Simplify CardOS 5.0 support (removing explicit 5.3 marker since the behavior should be the same)

* Restore RSA_PKCS signatures functionality

Closes OpenSC#1079
@frankmorgner
Copy link
Member

On the PKCS#15 layer, the card could initialize p15card->tokeninfo->supported_algos ,
which allows to be more explicit (see comments here #508 (comment))

@Jakuje
Copy link
Member Author

Jakuje commented Jul 10, 2017

Thank you for the pointer. I read through the comments (specially the linked one), but still I am not sure what is your proposed solution and how clean clean would it be.
Modifying the initialized private key structures that carries the list of supported algorithms with all we know sounds for me very cumbersome. But if there will not be any other possibility, I will try to do that.

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