Skip to content

Comments

Add PKCS#11 3.2 attribute to NSSDB storage#310

Merged
simo5 merged 4 commits intolatchset:mainfrom
simo5:nss_p11_3.2
Sep 29, 2025
Merged

Add PKCS#11 3.2 attribute to NSSDB storage#310
simo5 merged 4 commits intolatchset:mainfrom
simo5:nss_p11_3.2

Conversation

@simo5
Copy link
Member

@simo5 simo5 commented Jul 31, 2025

Description

This catches up nssdb to the changes happenning in NSS upstream

Fixes #307

Checklist

  • Test suite updated with functionality tests
  • Test suite updated with negative tests
  • Rustdoc string were added or updated
  • CHANGELOG and/or other documentation added or updated
  • This is not a code change

Reviewer's checklist:

  • Any issues marked for closing are fully addressed
  • There is a test suite reasonably covering new functionality or modifications
  • This feature/change has adequate documentation added
  • A changelog entry is added if the change is significant
  • Code conform to coding style that today cannot yet be enforced via the check style test
  • Commits have short titles and sensible text
  • Doc string are properly updated

@simo5 simo5 requested a review from Jakuje July 31, 2025 13:16
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.

I am wondering how the PQC algorithm tests worked before this with the NSS DB when the new attributes like Encapsulate, ParamSet and others could not have been stored in the database ....

@simo5
Copy link
Member Author

simo5 commented Jul 31, 2025

I am wondering how the PQC algorithm tests worked before this with the NSS DB when the new attributes like Encapsulate, ParamSet and others could not have been stored in the database ....

Good q, I think they simply always use the sqlite database, we'll have to figure out how to make sure most test are also tried on nssdb, perhaps a build target that has only the nssdb ?

@Jakuje
Copy link
Contributor

Jakuje commented Jul 31, 2025

I am wondering how the PQC algorithm tests worked before this with the NSS DB when the new attributes like Encapsulate, ParamSet and others could not have been stored in the database ....

Good q, I think they simply always use the sqlite database, we'll have to figure out how to make sure most test are also tried on nssdb, perhaps a build target that has only the nssdb ?

Thats not how I wrote that. When nssdb is enabled, it should default to NSSDB, unless something changed since I did this:

fn get_default_db(name: &str) -> (&'static str, String) {

@simo5
Copy link
Member Author

simo5 commented Jul 31, 2025

I think I changed that because at some point all CI jobs included nssdb, and that means sqlite was never tested, so I think I change something and then forgot about it.

Maybe we should open a separate issue to follow up on this?

@simo5 simo5 marked this pull request as draft August 1, 2025 07:12
@simo5
Copy link
Member Author

simo5 commented Aug 1, 2025

Marking this as draft because I think we need to add at least a test that will upgrade a database with the new attributes and work correctly after doing that

This catches up nssdb to the changes happenning in NSS upstream

Signed-off-by: Simo Sorce <simo@redhat.com>
@simo5 simo5 force-pushed the nss_p11_3.2 branch 2 times, most recently from c2660a7 to 9c17377 Compare September 26, 2025 17:40
@simo5
Copy link
Member Author

simo5 commented Sep 26, 2025

The test_nssdb_key_cache test does upgrade the database we hold in testdata/nssdbdir and I can see that it does in fact get properly upgraded with new columns for all the PKCS#11 3.2 new attributes.

@simo5
Copy link
Member Author

simo5 commented Sep 26, 2025

Still keeping this in draft until I can get my hands on another test database generate by a new enough version of NSS that has the new attributes so I can ensure not only the upgrade path but also full compatibility with dbs generated by new NSS versions.

Comment on lines -295 to -297
CKA_UNIQUE_ID,
CKA_COPYABLE,
CKA_DESTROYABLE,
Copy link
Contributor

Choose a reason for hiding this comment

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

please, make sure we have tests with these attributes executed with the new nss db

Copy link
Member Author

Choose a reason for hiding this comment

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

why?
they are just regular attributes like any other

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we might have some tests for them that are either excluded with the nssdb backend and if not, we should test they make it through at all 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.

I wonder if we should change the tests infra to look at an enve var in tests which will set what is the db to use for tests, and then have one of the builds test with forcing NSSDB on, thi way we have more control over what DB is used for testing regardless of how many DBs we've built into the binary ?

Copy link
Contributor

Choose a reason for hiding this comment

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

We already have the tests forcing one type of DBs based on enabled features (currently just NSS and SQLite):
https://github.com/latchset/kryoptic/blob/main/src/tests/mod.rs#L191
all of the code initialization goes through this function

Copy link
Member Author

@simo5 simo5 Sep 29, 2025

Choose a reason for hiding this comment

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

Yes, but I think we should always pick sqlitedb by default, and have an environment variable to forcibly change the DB type, such that if the db type is not compiled and the variable is set it will error out as well.

This way we can be sure of what we are testing in CI, currently it is almost a guess which DB is tested and we can't test "the other" DB when multiple are compiled in which is not great because applications can chose which one to use and we are not testing if the behavior of "the other" dbs is not broken by config options enabled by whichever one is chosen at test time.

Implement a mechanism to check and upgrade the NSS SQLite database schema upon
initialization.

This change introduces a check that runs when the database is opened. It
compares the columns in the `nssPublic` and `nssPrivate` tables against a list
of known PKCS#11 attributes. If any columns for known attributes are missing,
they are automatically added using `ALTER TABLE`. This ensures that existing
databases can be seamlessly upgraded to support new attributes (e.g., from
PKCS#11 3.2) without manual intervention.

The logic for converting database rows to objects is also refactored to use a
new column cache, improving efficiency and robustness.

Signed-off-by: Simo Sorce <simo@redhat.com>
@simo5 simo5 marked this pull request as ready for review September 29, 2025 18:45
@simo5
Copy link
Member Author

simo5 commented Sep 29, 2025

@Jakuje marking as ready to review because now I have a DB generated by NSSDB with the attributes we need (and a fix to handle a NSS bug ...)

This was generated by NSS code and we use it to test compatibility
woth NSS written DBs.

Signed-off-by: Simo Sorce <simo@redhat.com>
Some versions of NSS have a bug where the CKA_PARAMETER_SET attribute is
written to the database as a raw, platform-endian CK_ULONG (4 or 8 bytes)
instead of the expected 4-byte big-endian database integer.

This change adds a workaround to correctly read this attribute from malformed
databases. It attempts to parse the stored blob as both a 32-bit and 64-bit
integer, checking both little-endian and big-endian formats. Since the valid
values for this attribute are small numbers, we can reliably recover the
intended value, ensuring compatibility.

Signed-off-by: Simo Sorce <simo@redhat.com>
@simo5 simo5 merged commit 8f50a16 into latchset:main Sep 29, 2025
49 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.

Update NSSDB code to include new pkcs#11 3.2 attributes

2 participants