tpm2-software / tpm2-tss-engine

OpenSSL Engine for TPM2 devices
https://tpm2-software.github.io
BSD 3-Clause "New" or "Revised" License
151 stars 100 forks source link

Add ability to digest & sign with restricted ECC key #237

Closed rshearman closed 2 years ago

rshearman commented 2 years ago

The TPM architecture mandates that a restricted signing key may only sign a digest that has been produced by the TPM. Having a TPM key be restricted is useful for implementing attestation using the key.

The current mechanism for signing data using OpenSSL with an ECC key is through the EC_KEY sign method. This is too low-level, since it relies on the digest having already been done (in software). The EVP pkey object is the right level, since OpenSSL exposes EVP_DigestSign* functions that operate on it.

So implement methods on EVP pkey for digest and sign for the EC type, inheriting other EVP pkey methods from the built-in EC type in OpenSSL. The digest and sign operation is implemented by making use of the TPM to perform the digest and then passing the validation ticket, which is required for a restricted key, (along with digest data) into the subsequent sign operation in the TPM.

Make use of the digest_custom method, rather than signctx_init method for performing initialisation of our context used for the digest-and-sign operation so that the message digest type is already set in the OpenSSL message digest context. It isn't possible to override the builtin methods registered for OpenSSL message digest types in a general sense, but it is possible to override the update method called during a updating digest data in the context of EVP_DigestSign* functions, so this is what is done to allow the digest update to be performed in the TPM.

Whilst this is only implemented for ECC keys currently, the implementation is done in a way where the parts not specific to the key type are in a file not specific to the key type, so that they can be used for doing the same with RSA in the future.

AndreasFuchsTPM commented 2 years ago

I am very sorry, but seems like another PR created a merge-conflict with this one. Could you rebase onto latest master, so I can hit the merge button ?

Also would you be willing to add the same support for RSA keys ?

rshearman commented 2 years ago

No worries on the merge conflict.

Sure, I can add the same support for RSA.