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

Add support for RSA PSS with SmartCard-HSM #1404

Merged
merged 3 commits into from
Jul 11, 2018
Merged

Conversation

CardContact
Copy link
Member

@CardContact CardContact commented Jun 22, 2018

Tested with SmartCard-HSM

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

Copy link
Member

@Jakuje Jakuje left a comment

Choose a reason for hiding this comment

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

Thank you for the good work. I did not test it, but the changes look reasonable with the comments in the code.

case CKM_SHA256:
if (hlen != 32)
return CKR_MECHANISM_PARAM_INVALID;
break;
Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense to cover also the other SHA2 variants for future, even if they might not be supported now by your cards?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'd rather add the code when it can be tested properly. Should be easy to adapt when cards with more algorithms arrive.

}

// SmartCards typically only support MGFs based on the same hash as the
// message digest
Copy link
Member

Choose a reason for hiding this comment

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

This limitation is not on PKCS#11 level so the errors might be confusing. Even though, this might be a good idea to implement now opensc-wise (since the sc-hsm is the only supporting PSS), future drivers might struggle with it so I would propose to do this limitation in the sc-hsm code, if you know your cards do not support these cases.

Copy link
Member Author

Choose a reason for hiding this comment

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

Unfortunately this would require substantial changes in the lower layer to pass PSS parameter down into the card driver. It is something we could do in the long run, however I don't expect cards to support arbitrary salt length and MGF anytime soon. PKCS#11 allows the flexibility, but most implementation use saltlen = hashlen and MGF hash = message hash as all other cases would need to be explicitly agreed between sender and receiver.

Copy link
Member

Choose a reason for hiding this comment

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

Thank you for clarification. Yes, it makes sense to support at least something for now.

return CKR_MECHANISM_PARAM_INVALID;
}

// SmartCards typically support only a salt length equal to the hash length
Copy link
Member

Choose a reason for hiding this comment

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

The same for this condition -- PKCS#11 happily allows wider range of combinations.

Copy link
Contributor

Choose a reason for hiding this comment

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

And some cards/tokens (like YubiHSM2) do support other SHA2 family members.

Copy link
Member

Choose a reason for hiding this comment

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

But the yubihsm driver is not part of opensc. Anyway completeness of PKCS#11 mechanisms is implemented in #1435.

@@ -420,12 +420,16 @@ int sc_pkcs15_compute_signature(struct sc_pkcs15_card *p15card,

/* add the padding bytes (if necessary) */
if (pad_flags != 0) {
size_t tmplen = sizeof(buf);
if (flags & SC_ALGORITHM_RSA_PAD_PSS) {
// TODO PSS padding
Copy link
Member

Choose a reason for hiding this comment

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

This will be needed to implement the PSS off the card. Do you plan to have a look into that, or should I have a look into the possibilities here?

Copy link
Member

Choose a reason for hiding this comment

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

We could maybe use this: NWilson@42f3199

frankmorgner referenced this pull request in NWilson/OpenSC Jun 28, 2018
A card driver may declare support for computing the padding on the card,
or else the padding will be applied locally in padding.c.  All five
PKCS11 PSS mechanisms are supported, for signature and verification.

There are a few limits on what we choose to support, in particular I
don't see a need for arbitrary combinations of MGF hash, data hash, and
salt length, so I've restricted it (for the user's benefit) to the only
cases that really matter, where salt_len = hash_len and the same hash is
used for the MGF and data hashing.
@frankmorgner
Copy link
Member

@CardContact the code looks good, thanks! I think, I'll have time to test this next week.

Could you please also check which the necessary bits are to get this functionality into the minidriver?

@frankmorgner
Copy link
Member

currently PSS is also announced for decryption. You need to apply this patch:

diff --git a/src/pkcs11/framework-pkcs15.c b/src/pkcs11/framework-pkcs15.c
index 3040df63..e8611591 100644
--- a/src/pkcs11/framework-pkcs15.c
+++ b/src/pkcs11/framework-pkcs15.c
@@ -5072,6 +5072,9 @@ register_mechanisms(struct sc_pkcs11_card *p11card)
        /* TODO support other padding mechanisms */
 
        if (rsa_flags & SC_ALGORITHM_RSA_PAD_PSS) {
+               CK_FLAGS old_flags = mech_info.flags;
+               mech_info.flags &= ~CKF_DECRYPT;
+
                mt = sc_pkcs11_new_fw_mechanism(CKM_RSA_PKCS_PSS, &mech_info, CKK_RSA, NULL, NULL);
                rc = sc_pkcs11_register_mechanism(p11card, mt);
                if (rc != CKR_OK)
@@ -5087,6 +5090,8 @@ register_mechanisms(struct sc_pkcs11_card *p11card)
                        if (rc != CKR_OK)
                                return rc;
                }
+
+               mech_info.flags = old_flags;
        }
 
        if (rsa_flags & SC_ALGORITHM_ONBOARD_KEY_GEN) {

Copy link
Member

@frankmorgner frankmorgner left a comment

Choose a reason for hiding this comment

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

@frankmorgner frankmorgner merged commit 6f8bfc3 into OpenSC:master Jul 11, 2018
@frankmorgner
Copy link
Member

thanks!

@CardContact please also have a look into the minidriver

@Jakuje looking forward for your port of NWilson@42f3199

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

4 participants