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

Ensure ECC key attributes are aligned with the PKCS#11 Spec #119

Merged
merged 9 commits into from
Nov 27, 2024

Conversation

simo5
Copy link
Member

@simo5 simo5 commented Nov 22, 2024

The different ECC key types accept values encoded in different ways.

Add code to ensure values are correctly formed on import and generation according to what the spec prescribes.

Fixes #115

@simo5
Copy link
Member Author

simo5 commented Nov 22, 2024

Addressing one key at a time, first patch addressed CKK_EC Public key, other to come as I implement and add test.

@simo5 simo5 marked this pull request as draft November 22, 2024 22:39
@simo5 simo5 force-pushed the ecvals branch 2 times, most recently from 167b206 to acce4ca Compare November 25, 2024 16:38
Copy link
Contributor

@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.

the formatter is not happy though.

src/ec/mod.rs Outdated Show resolved Hide resolved
@simo5 simo5 force-pushed the ecvals branch 3 times, most recently from cc831cf to e45d489 Compare November 26, 2024 02:17
@simo5 simo5 marked this pull request as ready for review November 26, 2024 02:17
@simo5
Copy link
Member Author

simo5 commented Nov 26, 2024

This PR is ready for review, however, being spec compliant means a lot of tools will just fail to deal with Edwaurds and Montgomery public curves as they expect a DER encoded OCTET STRING for the public EC_POINT for these curves.

However a DER OCTET STRING is incorrect even if we look at the 3.0 spec that specified a DER BIT STRING vs the 3.1 spec which specifies just a raw byte buffer.

Fixing all tools to expect both encodings will take a while ...

src/ec/mod.rs Show resolved Hide resolved
@simo5
Copy link
Member Author

simo5 commented Nov 26, 2024

So the last commit adds a feature to enable 3.1 compatibility and handles 3.0 compatibility as default.

I am not entirely sure I like it this way, most applications and most tokens will probably fail to deal with this appropriately anyway, so perhaps we should have a runtime configuration switch rather than a build time feature. A runtime switch will make it easier to deal with.

The issue though is where to put this switch. One option is the config file, or we could have both a config file and an environment variable ... what a mess...

@simo5
Copy link
Member Author

simo5 commented Nov 26, 2024

Ok I pushed a fixup that completely changes the last commit, to use an env var (or a new config option) to change the behavior at runtime rather than dabbling with build time options.

Tested that pkcs11-provider test fail if the env var is not set, but pass fine if the env var set.

I will now propose a PR in pkcs11-provider to properly set config so that we do not break its CI when we merge this PR.

src/ec/eddsa.rs Outdated Show resolved Hide resolved
src/lib.rs Show resolved Hide resolved
Signed-off-by: Simo Sorce <[email protected]>
Specifically both CKA_EC_PARAMS is checd to contain a valid curve
and CKA_EC_POINT is correctly formatted with DER(qx,qy)

Signed-off-by: Simo Sorce <[email protected]>
The initial implementation of Edwards keys used a definition partially
consisent with the language in the 3.0 spec that has since changed in
the 3.1 spec.

Namely the 3.0 spec indicated that the public CKA_EC_POINT for
CKK_EC_EDWARDS curves needed to be encoded as a DER BIT STRING, however
the implementation was taking a DER OCTET STRING instead.

The 3.1 spec clarifes that the CKA_EC_POINT for Edwards curves is just a
buffer of bytes, with no ASN.1 encoding, just like the private
CKA_VALUE.

This patch adds all the necessary checks on import and aligns the
implementation to the 3.1 Spec.

Signed-off-by: Simo Sorce <[email protected]>
The PKCS#11 3.0 Spec was released with a bug in the description of the
public point encoding for Edwards and Montgomery curves that foooled
many implementations to assume these curves would return a DER encoded
octet string as the value for CKA_EC_POINT.

However an errata was issued and the 3.1 spec also confirms that the
CKA_EC_POINT for these curves should be just a byte array (like the
private value is).

It is easy to accept either form and conver it for storage, the real
tricky part is returning the point to applications. Applications that
only handle one form and can't deal with different encodings may get
an encoding they do not like and fail.

Signed-off-by: Simo Sorce <[email protected]>
Copy link
Contributor

@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.

Looks good!

@simo5 simo5 merged commit 7f93a14 into latchset:main Nov 27, 2024
6 checks passed
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.

Review and fix once and for all CKA_EC_POINT and CKA_VALUE for EC curves
2 participants