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 importing TPM2 keys with PKCS11 vendor attributes #866

Merged
merged 1 commit into from
Jun 20, 2024

Conversation

wxleong
Copy link
Member

@wxleong wxleong commented Jun 19, 2024

  • Add support for importing TPM2 keys (as persistent handle or key objects) using PKCS11 vendor-specific attributes
  • Add a new CLI tool: key_import
  • Add integration test
  • Add docs/KEY_IMPORT_TOOL.md

Resolves #861

Copy link

codecov bot commented Jun 19, 2024

Codecov Report

Attention: Patch coverage is 56.66667% with 325 lines in your changes missing coverage. Please review.

Project coverage is 71.45%. Comparing base (3f8d6e4) to head (3088b43).
Report is 4 commits behind head on master.

Current head 3088b43 differs from pull request most recent head ca60b14

Please upload reports for the commit ca60b14 to get more accurate results.

Files Patch % Lines
tools/key_import/import.c 49.52% 266 Missing ⚠️
src/lib/tpm.c 70.65% 27 Missing ⚠️
src/lib/key.c 75.96% 25 Missing ⚠️
src/lib/token.c 58.33% 5 Missing ⚠️
src/lib/object.c 81.81% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #866      +/-   ##
==========================================
- Coverage   72.43%   71.45%   -0.99%     
==========================================
  Files          34       35       +1     
  Lines        9773    10484     +711     
==========================================
+ Hits         7079     7491     +412     
- Misses       2694     2993     +299     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@wxleong wxleong force-pushed the impl-issue-861 branch 7 times, most recently from 32f27ff to 9a72b32 Compare June 20, 2024 01:28
- Add support for importing TPM2 keys (as persistent handle or key objects) using PKCS11 vendor-specific attributes
- Add a new CLI tool: key_import
- Add integration test
- Add docs/KEY_IMPORT_TOOL.md

Signed-off-by: wenxin.leong <wenxin.leong@infineon.com>
@AndreasFuchsTPM AndreasFuchsTPM merged commit 50a636b into tpm2-software:master Jun 20, 2024
13 checks passed
Copy link
Member

@williamcroberts williamcroberts left a comment

Choose a reason for hiding this comment

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

@AndreasFuchsTPM and @wxleong. Am I missing something here or did you essentially reimplement tpm2_ptool link commandlet?

Also, shouldn't this be done with C_CreateObject and not C_GenerateKeyPair?

I'm looking at this as a complete revert.

{ CKR_CRYPTOKI_ALREADY_INITIALIZED, "CKR_CRYPTOKI_ALREADY_INITIALIZED" },
{ CKR_MUTEX_BAD, "CKR_MUTEX_BAD" },
{ CKR_MUTEX_NOT_LOCKED, "CKR_MUTEX_NOT_LOCKED" },
{ CKR_VENDOR_DEFINED, "CKR_VENDOR_DEFINED" },
Copy link
Member

Choose a reason for hiding this comment

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

You could have just stringified these with xstr macro

CK_OBJECT_HANDLE pubkey;
CK_OBJECT_HANDLE privkey;

rv = C_GenerateKeyPair(session, mech, pub, pub_len, priv, priv_len, &pubkey, &privkey);
Copy link
Member

Choose a reason for hiding this comment

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

I do believe this should have been C_CreateObject called twice, once for the pub key and once for the private key as this doesn't generate a key pair.

@wxleong
Copy link
Member Author

wxleong commented Sep 8, 2024

@AndreasFuchsTPM and @wxleong. Am I missing something here or did you essentially reimplement tpm2_ptool link commandlet?

Also, shouldn't this be done with C_CreateObject and not C_GenerateKeyPair?

I'm looking at this as a complete revert.

tpm2_ptool imports keys by making direct changes to the database and does not support the FAPI backend. The new key_import tool, however, takes a different approach to importing pre-generated TPM keys (whether persisted or as key objects). It uses newly introduced PKCS#11 vendor-specific attributes, making it compatible with any backend. Additionally, it eliminates the need to rely on Python for the operation.

As for why it was implemented under C_GenerateKeyPair instead of C_CreateObject, the decision was based on ease of use and code reuse, as the logical flow closely mirrors C_GenerateKeyPair. While it could have been implemented under C_CreateObject, this feedback unfortunately came a bit too late. Do you see this as a critical concern?

You can find the initial proposal here: #861

@williamcroberts
Copy link
Member

@AndreasFuchsTPM and @wxleong. Am I missing something here or did you essentially reimplement tpm2_ptool link commandlet?
Also, shouldn't this be done with C_CreateObject and not C_GenerateKeyPair?
I'm looking at this as a complete revert.

tpm2_ptool imports keys by making direct changes to the database and does not support the FAPI backend. The new key_import tool, however, takes a different approach to importing pre-generated TPM keys (whether persisted or as key objects). It uses newly introduced PKCS#11 vendor-specific attributes, making it compatible with any backend. Additionally, it eliminates the need to rely on Python for the operation.

Yes I see that, the overall premise is fine, but the implementation is wrong, more below.

As for why it was implemented under C_GenerateKeyPair instead of C_CreateObject, the decision was based on ease of use and code reuse, as the logical flow closely mirrors C_GenerateKeyPair.

You don't use an API for generating key pairs as a mechanism for import as it violates the PKCS11 API. If you need to share code flows and code between two areas of a piece of software, just refactor it and place common code in functions.

While it could have been implemented under C_CreateObject, this feedback unfortunately came a bit too late. Do you see this as a critical concern?

Critical concern as it breaks the API standard for PKCS11 and should be done under C_CreateObject. This software cannot be released. However, many of the fixes patches can stay. As of right now, I think only 50a636b is being reverted. However, feel free and re-submit it under C_CreateObject.

@wxleong
Copy link
Member Author

wxleong commented Sep 8, 2024

Noted. i'll get this reimplemented.

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.

Proposal for importing persistent keys and key objects into a token without relying on third-party software
3 participants