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 Keys #514

Merged
merged 5 commits into from
Jan 27, 2025
Merged

Add support for RSA-PSS Keys #514

merged 5 commits into from
Jan 27, 2025

Conversation

Jakuje
Copy link
Contributor

@Jakuje Jakuje commented Jan 23, 2025

Description

This is not as straightforward as it looked like initially and consists of several changes that surface on different occasions:

  • The private RSA keys can have CKA_ALLOWED_MECHANISMS which can be used to determine if a key is generic key (can be used for any operation) or RSA-PSS key, so that it could be used only for any or specific RSA-PSS operations.

  • These keys get different identifier on OpenSSL level. They also get different OIDs on the ASN.1 in various places of the X.509 certificates:

    • The signatures in X.509 has a OID + parameters describing the hashes, mgf and salt length used. This is mandatory.

    • The public key encoding can contain the RSA-PSS restrictions. These are not mandatory so it allows us to indicate the key is restricted to PSS operations without the need to stick to specific combination of parameters.

  • When we force all operation in pkcs11 provider, the rsapss table was missing the match() callback, making key comparison fail when using RSA-PSS keys.

Given that the certtool we use for signing certificates during setup can not distinguish RSA-PSS restricted keys and therefore generates unrestricted certificates, we need to generate the certificate later using openssl, making the tests a bit more ugly.

Fixes: #489

Checklist

  • Code modified for feature
  • Test suite updated with functionality tests
  • Test suite updated with negative tests
  • Documentation updated

Reviewer's checklist:

  • Any issues marked for closing are addressed
  • There is a test suite reasonably covering new functionality or modifications
  • This feature/change has adequate documentation added
  • Code conform to coding style that today cannot yet be enforced via the check style test
  • Commits have short titles and sensible commit messages
  • Coverity Scan has run if needed (code PR) and no new defects were found

@Jakuje
Copy link
Contributor Author

Jakuje commented Jan 23, 2025

(sorry for huge one-commit, but it was pile of different interleaving changes I did not manage to separate to sensible smaller chunks yet)

The failed test on macos times out in uri test as it is likely much slower with the new keys -- I can probably increase the timeout if we will agree that this is the right direction.

@Jakuje Jakuje marked this pull request as ready for review January 23, 2025 15:51
simo5
simo5 previously approved these changes Jan 23, 2025
Copy link
Member

@simo5 simo5 left a comment

Choose a reason for hiding this comment

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

I wish the ASN.1 code was more straightforward and weren't obfuscated by all these OpenSSL Opaque structures.
For example all of the MD and MFG1 EVP_md stuff is ultimately only need to translate from the Hash name to the corresponding OID used in X509 ... I wonder if we shouldn't just store ASN1_OBJECT * pointers instead of the EVP_md ones in our structures to avoid all the churn. Even better if there was a way to just store the serialized ASN.1 once instead of reconstructing it from scratch at every invocation ...
After all there is nothing really dynamic in this information from our POV, the same fixed algorithm will always generate a very defined ASN.1 output.
Maybe we should just generate it once via some ASN.1 utility and store the pre-generated ASN.1 in the rsa-pss_map ?
Is there any need for runtime generation ?

src/objects.c Outdated Show resolved Hide resolved
src/signature.c Outdated Show resolved Hide resolved
src/signature.c Outdated Show resolved Hide resolved
src/store.c Show resolved Hide resolved
@Jakuje Jakuje force-pushed the rsa-pss branch 2 times, most recently from 1eb982a to 55cbfdf Compare January 24, 2025 15:37
When we force all operation in pkcs11 provider,  key comparison and matching
of RSA-PSS did not work.

Signed-off-by: Jakub Jelen <[email protected]>
... instead of the removed autotools

Signed-off-by: Jakub Jelen <[email protected]>
@Jakuje
Copy link
Contributor Author

Jakuje commented Jan 27, 2025

I have the signature parameters rewritten to use static structures, but X509_PUBKEY_set0_param() requires again the ASN1_STRING structure as an input so we will have to convert it from the static structures again (with d2i_ASN1_STRING function likely), which makes it a bit defeating the point of having static structures. But I think we can live with it.

Edit: The ASN1_STRING is just a wrapper for any string so I can just feed it with the static buffer using ASN1_STRING_set.

@simo5
Copy link
Member

simo5 commented Jan 27, 2025

You already amended your comment, yeah an ASN1_STRING is a lightweight wrapper for a "bunch of bytes", so is not a big deal.

@Jakuje Jakuje force-pushed the rsa-pss branch 2 times, most recently from b24c863 to 542a6ea Compare January 27, 2025 16:37
This is not as straightforward as it looked like initially and consists of
several changes that surface on different occasions:

 * The private RSA keys in PKCS#11 can have `CKA_ALLOWED_MECHANISMS` which can
   be used to determine if a key is generic key (can be used for any operation)
   or RSA-PSS key, so that it could be used only for any or specified
   operations.

 * These keys get different identifier on OpenSSL level. They also get different
   OIDs on the ASN.1 in various places of the X.509 certificates:

   * The signatures in X.509 has a OID + parameters describing the hashes, mgf
     and salt length used to create the signature. This is mandatory.

   * The public key encoding can contain the RSA-PSS restrictions. These are not
     mandatory so it allows us to indicate the key is restricted to any PSS
     operations without the need to stick to specific combination of parameters.
     But it allows to specify only one of the parameters (hash, MGF1-hash,
     salt size). But there is no way to express this in PKCS#11. We can limit
     ourselves to specific hash. Note, from this only the diffentiation between
     RSA and RSA-PSS SPKI is done

Given that the certtool we use for signing certificates during setup can not
distinguish RSA-PSS restricted keys and therefore generates unrestricted
certificates, we need to generate the certificate later using openssl, making
the tests a bit more ugly.

Fixes: latchset#489

Signed-off-by: Jakub Jelen <[email protected]>
At this moment, this was already copied into two places.

This also generates second restricted RSA-PSS key during setup to avoid the
need to generate key as part of rsapssam test, which makes it reentrant and
should make the uri test a bit faster as we will have less keys.

Signed-off-by: Jakub Jelen <[email protected]>
simo5
simo5 previously approved these changes Jan 27, 2025
Copy link
Member

@simo5 simo5 left a comment

Choose a reason for hiding this comment

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

LGTM

@simo5 simo5 added the covscan Triggers Coverity Scanner label Jan 27, 2025
@github-actions github-actions bot removed the covscan Triggers Coverity Scanner label Jan 27, 2025
@simo5 simo5 added the covscan-ok Coverity scan passed label Jan 27, 2025
@simo5
Copy link
Member

simo5 commented Jan 27, 2025

URI test still very slow on Mac + softhsm, but URI+softhsm is v. slow on other platforms too, pushing for now, and we'll investigate later

@simo5 simo5 merged commit bdebdc7 into latchset:main Jan 27, 2025
45 of 46 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
covscan-ok Coverity scan passed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for RSA-PSS-only keys
2 participants