Hopefully this question makes some sense... We've created a public certificate in PEM format, using a...
Hopefully this question makes some sense...
We've created a public certificate in PEM format, using a private key stored in a TPM2 device on a computer running Ubuntu 22.04 and OpenSSL 3, using the TPM2 providers available in tpm2-openssl and the various command line tools. We want to use the certificate for SSL/TLS authentication to a connected device. This appears to work, using the "openssl s_server" command and a pile of parameters.
But we want to use that authentication in a program we've written in C#, running on .NET 8 on the Ubuntu system. So we need to create an X509Certificate2 object from our external public key PEM, and the private key stored at a known handle in the TPM2. Again, we're expecting to use TPM2 provider functions.
Sadly, the provider function(s) we want are in .NET 9. We have tried the recommended functions, per Microsoft's instructions, using a self-contained .NET 9 test program, and we've found these functions don't work as expected. An issue has been raised with Microsoft, they believe it to be a bug in their code, and they've indicated a solution may be available in .NET 10 later this year (2025).
So ... tried creating a C DLL that can get the private key and call that from a C# wrapper, or tried to get a C DLL that could get the private key and create the certificate we need and write a C# wrapper to consume that. Tricky bit is - my C is very rusty, and the various OpenSSL/TPM2 calls are fairly convoluted to just hack together. Tried various AIs to generate the code (desperate times and all...), and while I have a .so file that gets consumed, it fails.
HENCE ... can anyone point me to a library/sample code that will load a private key from a known TPM2 handle and either return that in a way that consumable by the .NET X509Certificate2 creator routines, or will load a key and PEM and create the certificate object? Ideally I need to consume this from a C# program so that I have a certificate for our SSL/TLS authentication. We have a working server using two PEM files. It's the TPM2 stuff that's having us bang our heads on the desk.
And I know someone may suggest the TPM2 engines. They require OpenSSL 1.1 to work, and the support built into .NET 8 was buggy. Filed that bug, Microsoft has a fix that is supposedly getting backported to a .NET 8 release this year. But the OpenSSL 1.1 is a deal-breaker for us.
Thanks. Sorry about the length of this, but there seem to be a lot of pieces associated with explaining this stuff.
❤️
👍
💡
👏
😂
Explore More Posts
The latest tpm2-tools 5.8 release addresses three GitHub security advisories. One is a heap overflow in tpm2_getekcertificate, and the other is an out-of-bounds write that happens when too many sessions are in use. I don’t have information on the third advisory. Beyond the fixes, 5.8 extends EK certificate handling. It adds the option to read EK certificates from NV RAM, introduces new EK templates and supports a marshaled quote format. https://github.com/tpm2-software/tpm2-tools/releases/tag/5.8
Random Number Generator (TPM2)
1. IntroductionAmong other features, Trusted Platform Modules (TPM) comes with a True Random Number Generator (TRNG) that can be used for various applications including cryptographic purposes. This RNG module serves as the source for randomness for the TPM 2.0 Chip. A RNG module in TPM2 will typically contain the following -
An entropy source and collector
State Register
A mixing func.
There can be two types of calls to the TPM’s Random Number Generator, Internal call and External Call. RNG is capable to provide enough randomness for all the internal calls made. As well as, RNG is capable of providing 32 octets of randomness as per the specification to an external call. Another thing to note here is, each call made to the RNG produces a new value irrespective of the type of call. 1.1 The source of RandomnessThe source of entropy in the TPM is the entropy collector. As the name suggests, entropy collector collects entropy from entropy sources and also removes bias. A TPM2.0 has at least one internal source of entropy. These sources can include noise, clock variations, air movement amongst other events. As depicted in Fig 1.1 the entropy collector provides input to the State Register. 1.2 Stages involved Various algorithms and process supported by TPM calls RNG for unpredictable numbers, which is actually obtained from “RNG Protected Capability”. The “Deterministic Random Bit Generator (DRBG) Protected Capability“ obtains entropy from “Entropy Protected Capability”. Along with Entropy Protected Capability, TPM2_StirRandom() can also gather entropy from other entropy sources within the same TPM. 2. Random Numbers via TPM2 ToolsIf one needs to use TPM’s RNG for other external applications, then TPM2 Tools Command Line tools can be a great option. TPM2 Tools Command : tpm2_getrandom - - hex 16 - - hex : Converts output to hexadecimal 16 : Size in bytes Now, if one wants to have additional randomness, TPM2 Tools provides a convenient utility for this purpose called tpm2_stirrandom. To inject entropy via TPM2_StirRandom() to “Entropy Protected Capability” , we can use some file or use stdin. TPM2 Tools Command : echo -n “some_random_data” | tpm2_stirrandom Note : 128 bytes can only be injected using stdin at once through stirrandom() 3. ConclusionThe TPM2 provides in a sense True Random Numbers for use. These unpredictable numbers are coming from a dedicated hardware, hence can be trusted to be given as input to Crypto Algorithms amongst other applications. TPM2 also uses this for its internal use, and TCG claims it to be as secure as the strongest algorithm that TPM supports. Also, using the RNG is pretty straight forward via TPM2 Tools. #Tutorials



Comments
Start the Conversation