Skip to content

Conversation

@bukka
Copy link
Contributor

@bukka bukka commented Sep 11, 2025

This should fix #721 . The type is differnt from RSA PSS so it needs to be added there.

I tested the change manual and the import went through after this. I don't see any tests for util so it comes wihtout a test.

Summary by CodeRabbit

  • New Features
    • Added support for importing RSA-PSS key pairs; RSA-PSS keys are now recognized and processed the same as standard RSA during import.
    • Existing import behavior for RSA, DSA, EC, and EdDSA remains unchanged.
    • No changes to public interfaces, CLI options, or configuration are required.

✏️ Tip: You can customize this high-level summary in your review settings.

@bukka bukka requested a review from a team as a code owner September 11, 2025 17:10
@coderabbitai
Copy link

coderabbitai bot commented Sep 11, 2025

Walkthrough

Adds recognition of OpenSSL EVP_PKEY_RSA_PSS in crypto_import_key_pair, routing RSA‑PSS keys through the existing RSA import path (EVP_PKEY_get1_RSA). No other logic, control flow, or public interfaces were changed.

Changes

Cohort / File(s) Summary
RSA‑PSS import handling
src/bin/util/softhsm2-util-ossl.cpp
Extend key import switch to handle EVP_PKEY_RSA_PSS by treating it as RSA and using EVP_PKEY_get1_RSA; DSA/ECC/EDDSA handling unchanged; no public signature changes.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor User
  participant CLI as softhsm2-util
  participant OSSL as OpenSSL EVP
  participant Token as Token Store

  User->>CLI: softhsm2-util --import <key.pem>
  CLI->>OSSL: parse PEM -> EVP_PKEY*
  OSSL-->>CLI: EVP_PKEY*
  CLI->>CLI: switch(EVP_PKEY_base_id)
  alt RSA or RSA‑PSS
    note right of CLI `#lightblue`: RSA‑PSS now mapped to RSA path
    CLI->>OSSL: EVP_PKEY_get1_RSA()
    OSSL-->>CLI: RSA*
    CLI->>CLI: build PKCS#11 key objects
  else DSA/ECC/EDDSA/...
    CLI->>CLI: existing handlers (unchanged)
  end
  CLI->>Token: store key pair and attributes
  Token-->>CLI: OK / Error
  CLI-->>User: result
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Pay attention to: correctness of the EVP_PKEY_base_id comparison for EVP_PKEY_RSA_PSS and proper reference counting/cleanup after EVP_PKEY_get1_RSA() to avoid leaks or double frees.

Poem

I nibbled through code with a careful press,
Found keys in a cape labeled “RSA‑PSS.”
Now they hop in like regular RSA,
Into the burrow without delay.
Carrots up! The import’s spry—🥕🐇

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and accurately describes the main change: adding RSA-PSS import support to softhsm2-util, which is concise and specific.
Linked Issues check ✅ Passed The code change directly addresses issue #721 by adding RSA-PSS key handling in crypto_import_key_pair, enabling softhsm2-util to import RSA-PSS keys without the 'Cannot handle this algorithm' error.
Out of Scope Changes check ✅ Passed All changes are scoped to the RSA-PSS import support requirement; only the necessary switch case for EVP_PKEY_RSA_PSS was added with no unrelated modifications.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 15a4860 and 7944e7d.

📒 Files selected for processing (1)
  • src/bin/util/softhsm2-util-ossl.cpp (1 hunks)
🧰 Additional context used
🧠 Learnings (8)
📓 Common learnings
Learnt from: bukka
Repo: softhsm/SoftHSMv2 PR: 815
File: src/lib/SoftHSM.cpp:6543-6546
Timestamp: 2025-09-11T16:54:00.370Z
Learning: SoftHSM PR #815 (pkcs11.h 3.1.0 update, CK_RSA_AES_KEY_WRAP_PARAMS fix): Maintainer bukka prefers to keep scope minimal (header sync and param rename only); broader OAEP parameter enforcement or backend plumbing is out of scope for this PR and should be handled in a follow-up issue.
📚 Learning: 2025-09-11T16:54:00.370Z
Learnt from: bukka
Repo: softhsm/SoftHSMv2 PR: 815
File: src/lib/SoftHSM.cpp:6543-6546
Timestamp: 2025-09-11T16:54:00.370Z
Learning: SoftHSM PR #815 (pkcs11.h 3.1.0 update, CK_RSA_AES_KEY_WRAP_PARAMS fix): Maintainer bukka prefers to keep scope minimal (header sync and param rename only); broader OAEP parameter enforcement or backend plumbing is out of scope for this PR and should be handled in a follow-up issue.

Applied to files:

  • src/bin/util/softhsm2-util-ossl.cpp
📚 Learning: 2025-10-13T09:42:31.270Z
Learnt from: bukka
Repo: softhsm/SoftHSMv2 PR: 818
File: src/lib/SoftHSM.cpp:3238-3266
Timestamp: 2025-10-13T09:42:31.270Z
Learning: In src/lib/SoftHSM.cpp, SoftHSM::AsymDecryptInit should use MechParamCheckRSAPKCSOAEP for CKM_RSA_PKCS_OAEP to keep OAEP parameter validation identical to AsymEncryptInit, and avoid redundant manual checks for pMechanism->pParameter and MGF allowlists.

Applied to files:

  • src/bin/util/softhsm2-util-ossl.cpp
📚 Learning: 2025-10-13T09:43:45.885Z
Learnt from: bukka
Repo: softhsm/SoftHSMv2 PR: 818
File: src/lib/SoftHSM.cpp:13265-13282
Timestamp: 2025-10-13T09:43:45.885Z
Learning: SoftHSM OAEP policy: In SoftHSM::MechParamCheckRSAPKCSOAEP (src/lib/SoftHSM.cpp), enforce that the MGF1 variant must correspond to the selected hashAlg (e.g., SHA-256 -> CKG_MGF1_SHA256). Mismatches should return CKR_ARGUMENTS_BAD with "Hash and MGF don't match". This mirrors the existing PSS enforcement.

Applied to files:

  • src/bin/util/softhsm2-util-ossl.cpp
📚 Learning: 2025-11-11T19:42:46.886Z
Learnt from: JoaoHenrique12
Repo: softhsm/SoftHSMv2 PR: 823
File: src/lib/SoftHSM.cpp:58-58
Timestamp: 2025-11-11T19:42:46.886Z
Learning: SoftHSM include policy: Algorithm key/parameter headers (e.g., SLHParameters, SLH*Key) are included unconditionally in SoftHSM.cpp/SoftHSM.h; feature gating is handled in mechanism tables and CryptoFactory (WITH_*). Avoid wrapping such includes in WITH_* guards.

Applied to files:

  • src/bin/util/softhsm2-util-ossl.cpp
📚 Learning: 2025-10-27T16:44:57.865Z
Learnt from: antoinelochet
Repo: softhsm/SoftHSMv2 PR: 809
File: src/lib/SoftHSM.cpp:5550-5596
Timestamp: 2025-10-27T16:44:57.865Z
Learning: SoftHSM ML‑DSA additional context: avoid transient malloc in SoftHSM::{AsymSignInit, AsymVerifyInit}; let Session::setParameters deep‑copy SIGN_ADDITIONAL_CONTEXT by allocating one buffer for struct + context bytes and fixing the stored pointer; Session::resetOp frees m_pParameters so both struct and context are released.

Applied to files:

  • src/bin/util/softhsm2-util-ossl.cpp
📚 Learning: 2025-08-11T16:42:23.012Z
Learnt from: bukka
Repo: softhsm/SoftHSMv2 PR: 806
File: src/lib/test/p11test.cpp:81-84
Timestamp: 2025-08-11T16:42:23.012Z
Learning: In SoftHSM test files (like p11test.cpp and cryptotest.cpp), error handling for OSSL_PROVIDER_load() calls for "legacy" and "default" providers is intentionally omitted because these providers won't fail in practice and the test code is kept deliberately simple.

Applied to files:

  • src/bin/util/softhsm2-util-ossl.cpp
📚 Learning: 2025-08-18T19:46:46.659Z
Learnt from: antoinelochet
Repo: softhsm/SoftHSMv2 PR: 809
File: src/lib/SoftHSM.cpp:10044-10055
Timestamp: 2025-08-18T19:46:46.659Z
Learning: In SoftHSM::generateMLDSA (src/lib/SoftHSM.cpp), the project prefers using CKP_ML_DSA_* constants for parameter set checks and logs "Unsupported parameter set: %lu". The code currently returns CKR_PARAMETER_SET_NOT_SUPPORTED for unsupported sets.

Applied to files:

  • src/bin/util/softhsm2-util-ossl.cpp
🪛 Cppcheck (2.18.0)
src/bin/util/softhsm2-util-ossl.cpp

[information] Limiting analysis of branches. Use --check-level=exhaustive to analyze all branches.

(normalCheckLevelMaxBranches)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (8)
  • GitHub Check: Windows (x64, openssl)
  • GitHub Check: Windows (x86, openssl)
  • GitHub Check: macOS (openssl)
  • GitHub Check: Windows (x64, botan)
  • GitHub Check: macOS (botan)
  • GitHub Check: Linux with OpenSSL 1.1.1
  • GitHub Check: Linux with Botan
  • GitHub Check: Linux with OpenSSL 3.0

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (4)
src/bin/util/softhsm2-util-ossl.cpp (4)

156-164: Add compile-time guard for EVP_PKEY_RSA_PSS to preserve older OpenSSL builds.

Some downstreams still build against older OpenSSL where EVP_PKEY_RSA_PSS may be undefined. Guard it to avoid compile errors.

Apply:

-        case EVP_PKEY_RSA:
-        case EVP_PKEY_RSA_PSS:
+        case EVP_PKEY_RSA:
+#ifdef EVP_PKEY_RSA_PSS
+        case EVP_PKEY_RSA_PSS:
+#endif
           rsa = EVP_PKEY_get1_RSA(pkey);
           break;

156-156: Prefer EVP_PKEY_base_id(pkey) over EVP_PKEY_type(EVP_PKEY_id(pkey)).

This simplifies the logic and is robust across OpenSSL variants/providers; it directly returns the base type (e.g., RSA for RSA-PSS).

Apply:

-    switch (EVP_PKEY_type(EVP_PKEY_id(pkey)))
+    switch (EVP_PKEY_base_id(pkey))

(Keeping the RSA_PSS case is harmless but becomes redundant.)


189-193: RSA-PSS semantics: consider restricting key usage on import.

Mapping RSA-PSS to generic RSA sets CKA_DECRYPT/UNWRAP/ENCRYPT/WRAP to TRUE in crypto_save_rsa(), which may allow non-PSS operations (e.g., RSAES-PKCS1-v1_5). For PSS-only keys, consider:

  • setting CKA_ENCRYPT/WRAP/DECRYPT/UNWRAP to FALSE, and
  • optionally setting CKA_ALLOWED_MECHANISMS to PSS-only mechanisms.

Given the PR’s minimal scope, this can be a follow-up issue.

I can draft a follow-up PR that toggles these attributes when the source key was RSA_PSS and, if supported, sets CKA_ALLOWED_MECHANISMS.


179-183: Improve diagnostics for unsupported algorithms.

Include the algorithm name to aid debugging (e.g., OBJ_nid2sn(EVP_PKEY_id(pkey)) or EVP_PKEY_get0_type_name in OpenSSL 3).

Example:

-            fprintf(stderr, "ERROR: Cannot handle this algorithm.\n");
+            const char* alg =
+#if OPENSSL_VERSION_NUMBER >= 0x30000000L
+                EVP_PKEY_get0_type_name(pkey);
+#else
+                OBJ_nid2sn(EVP_PKEY_id(pkey));
+#endif
+            fprintf(stderr, "ERROR: Cannot handle this algorithm: %s.\n", alg ? alg : "unknown");
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4f296fa and 15a4860.

📒 Files selected for processing (1)
  • src/bin/util/softhsm2-util-ossl.cpp (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: bukka
PR: softhsm/SoftHSMv2#815
File: src/lib/SoftHSM.cpp:6543-6546
Timestamp: 2025-09-11T16:54:00.347Z
Learning: SoftHSM PR #815 (pkcs11.h 3.1.0 update, CK_RSA_AES_KEY_WRAP_PARAMS fix): Maintainer bukka prefers to keep scope minimal (header sync and param rename only); broader OAEP parameter enforcement or backend plumbing is out of scope for this PR and should be handled in a follow-up issue.
📚 Learning: 2025-09-11T16:54:00.347Z
Learnt from: bukka
PR: softhsm/SoftHSMv2#815
File: src/lib/SoftHSM.cpp:6543-6546
Timestamp: 2025-09-11T16:54:00.347Z
Learning: SoftHSM PR #815 (pkcs11.h 3.1.0 update, CK_RSA_AES_KEY_WRAP_PARAMS fix): Maintainer bukka prefers to keep scope minimal (header sync and param rename only); broader OAEP parameter enforcement or backend plumbing is out of scope for this PR and should be handled in a follow-up issue.

Applied to files:

  • src/bin/util/softhsm2-util-ossl.cpp
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (8)
  • GitHub Check: Windows (x86, openssl)
  • GitHub Check: Linux with OpenSSL 1.1.1
  • GitHub Check: Linux with OpenSSL 3.0
  • GitHub Check: Windows (x64, openssl)
  • GitHub Check: Windows (x64, botan)
  • GitHub Check: macOS (openssl)
  • GitHub Check: macOS (botan)
  • GitHub Check: Linux with Botan
🔇 Additional comments (2)
src/bin/util/softhsm2-util-ossl.cpp (2)

158-161: LGTM: RSA-PSS now recognized and routed through the RSA import path.

This unblocks importing RSA-PSS PKCS#8 keys and matches the minimal scope of the PR.


156-164: Run RSA‑PSS import smoke test locally — softhsm2-util not available here.

Couldn't run the provided script: softhsm2-util: command not found. Please run the original 3-step check on a machine with softhsm2 installed and confirm it exits 0 and prints "The key pair has been imported."

@bukka bukka force-pushed the util-import-rsa-pss branch from 15a4860 to 7944e7d Compare December 9, 2025 09:36
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.

Import fails with RSA-PSS keys

1 participant