tpm2-tss-engine icon indicating copy to clipboard operation
tpm2-tss-engine copied to clipboard

Import existing keys into TPM for use with openssl

Open geobra opened this issue 6 years ago • 11 comments

Hi

My usecase is to import existing keys into the TPM to be able to use it for an TLS connection using openssl.

What works so far is to generate a key inside tpm on client and server, generate the certificates and connect:

-> execute on client or server accordingly... "export TYPE=client|server"

generate key in tpm

tpm2tss-genkey -a rsa $TYPE.tss

gen a cert sign request

openssl req -new -key $TYPE.tss -engine tpm2tss -keyform engine -out $TYPE.csr

gen the certificate

openssl x509 -req -in $TYPE.csr -CA ca-root.pem -CAkey ca-key.pem -CAcreateserial -out $TYPE-cert.pem -days 365

on server:

openssl s_server -cert server-cert.pem -key server.tss -keyform engine -engine tpm2tss -CAfile ca-root.pem -accept 8443 -Verify 1

on client:

openssl s_client -keyform engine -engine tpm2tss -connect :8443 -cert client-cert.pem -key client.tss -CAfile ca-root.pem

But what I really want is to use existing private key and certificate on the client. So I tried to import my key into the TPM.

generate primary

tpm2_createprimary -G 0x0001 -g 0x000b -H o -C parent.ctx tpm2_import -C parent.ctx -G 0x0001 -k client-key.pem -u client-key-tpm.pub -r client-key-tpm.priv

But now I have the encrypted parts of the private and public keys in a binary format. I need it in tss format for openssl to be usefull.

Can this be converted? Where to go from here? Or am I on the wrong path with this approach?

geobra avatar Oct 24 '18 13:10 geobra

This is a duplicate of #12 though title and description are better. It's a planned feature...

AndreasFuchsTPM avatar Oct 25 '18 03:10 AndreasFuchsTPM

And yes... in the meantime, assuming that primary key you generated is the same as the one the engine would generate, you can actually take those binary blobs and put them into a PEM file. How are you at writing ASN.1 structures by hand...? :)

Run openssl asn1parse -in client.tss then replace the OCTET-STRINGs for the pub and priv key with the blobs you have...

dwmw2 avatar Oct 27 '18 05:10 dwmw2

Thanks for this information! I am off for one week now and will try this when I am back.

geobra avatar Oct 27 '18 11:10 geobra

Note for implementer: This is about importing keys generated openssl. Question is: Do we use Esys_Import directly or do we use the result of the tpm2_import command ? Rather not the tpm2_import result because of the parent key stuff... What about openssl rsa -engine tpm2tss -in private.pem -outform engine ? What internal engine function is called then ? Current error is: bad output format specified for outfile

AndreasFuchsTPM avatar Nov 22 '18 14:11 AndreasFuchsTPM

For the public and private portions of the object returned by tpm2_import or a create command, we just serialize the blob and write to disk. So they would be in the proper format to un- serialize and hand off to ESYS. Perhaps a tool in this project to convert those blobs into the ossl engine format or use them directly and have a different way to provide needed metadata?

williamcroberts avatar Feb 12 '19 16:02 williamcroberts

Whichever on-disk format(s) end up being supported, please ensure they are in one of two categories:

  • A format which should be added to http://david.woodhou.se/draft-woodhouse-cert-best-practice.html which we should expect all well-behaved applications to consume.
  • Something explicitly not portable or consumable except internally.

Think carefully about whether you should ever do something in the latter category.

dwmw2 avatar Feb 12 '19 16:02 dwmw2

The tpm2tss-genkey executable shall (in the future) gain 2 import parameters to support importing of OpenSSL-PEM-keys as well as TPM2-keyblobs. Maybe even auto-detectable.

So implementer note: The tpm2_import command from tpm2-tools has some code snippets for reuse here.

AndreasFuchsTPM avatar Feb 12 '19 16:02 AndreasFuchsTPM

Try this utility: https://github.com/rpofuk/tpm2-asn-packer

I used it also to connect OpenVPN to AWS Client VPN

rpofuk avatar Aug 27 '19 15:08 rpofuk

There's also some prototype bit-rotting at https://github.com/AndreasFuchsSIT/tpm2-tss-engine/tree/add_tpmkey_import

AndreasFuchsTPM avatar Aug 27 '19 15:08 AndreasFuchsTPM

Any chance of picking this up? I'm (finally) adding some TPM testing to OpenConnect and would like to be able to import the existing keys from the test suite.

dwmw2 avatar May 11 '21 12:05 dwmw2

Is anyone working on this?

hallyn avatar Oct 18 '23 14:10 hallyn