Skip to content

Support import export TPM private keys#162

Open
shmset wants to merge 1 commit intotpm2-software:masterfrom
shmset:add-key-handle-serialization
Open

Support import export TPM private keys#162
shmset wants to merge 1 commit intotpm2-software:masterfrom
shmset:add-key-handle-serialization

Conversation

@shmset
Copy link
Copy Markdown
Contributor

@shmset shmset commented Mar 4, 2026

This commit adds support to export a primary key from the TPM by saving the key-type and a key handle in the ASN1 structure inside the TSS2 PRIVATE KEY. The biggest change is on line 134 in file src/tpm2-provider-pkey.c where instead of simply assuming KEY_TYPE_BLOB, I check if a handle is used instead. Without it, the part that handles KEY_TYPE_HANDLE is never called.

I decided to use two additional, but optional, fields in the ASN1 structure. Another approach would be to use the existing privkey field and interpret it according to the privkeyType.


The main application for this change is being able to export the key as seen in the test:

openssl ec -provider tpm2 -in "handle:${HANDLE}" -out primary_key.pem

This output file can then be used in curl for mTLS.

curl --cert ${CLIENT_CERT_FILE} \
     --capath ${ROOT_CA_DIR} \
     --key primary_key.pem \
     --engine "tpm2" \
     <server>

@shmset shmset closed this Mar 4, 2026
@shmset shmset reopened this Mar 4, 2026
Comment thread src/tpm2-provider-pkey.c Outdated
tpk->privkeyType = ASN1_INTEGER_new();
if (tpk->privkeyType == NULL)
goto error;
if (!ASN1_INTEGER_set_uint64(tpk->privkeyType, keydata->privatetype))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@smhmeier Shouldn't the value of tpk->privkeyType be set even if the field wasn't NULL to begin with?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@throwException Yes. You're absolutely right. I'll fix it.

Comment thread src/tpm2-provider.c
{ "EC", "provider=tpm2,output=der,structure=PrivateKeyInfo", tpm2_tss_encoder_PrivateKeyInfo_der_functions },
{ "EC", "provider=tpm2,output=pem,structure=PrivateKeyInfo", tpm2_tss_encoder_PrivateKeyInfo_pem_functions },
{ "EC", "provider=tpm2,output=der,structure=type-specific", tpm2_tss_encoder_PrivateKeyInfo_der_functions },
{ "EC", "provider=tpm2,output=pem,structure=type-specific", tpm2_tss_encoder_PrivateKeyInfo_pem_functions },
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@smhmeier Maybe the type-specific encoder should also be enabled for RSA?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@throwException Good suggestion. I'll add it for RSA/RSA-PSS and add some tests.

@shmset shmset force-pushed the add-key-handle-serialization branch 4 times, most recently from d07cffa to 5062ec9 Compare March 5, 2026 11:29
@shmset shmset force-pushed the add-key-handle-serialization branch 2 times, most recently from c659361 to bf9033c Compare March 11, 2026 09:13
Signed-off-by: Silas Steinhauser <silas.steinhauser@gapfruit.com>
@shmset shmset force-pushed the add-key-handle-serialization branch from bf9033c to ba95826 Compare March 11, 2026 09:58
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.

2 participants