Here is how signature works:
- Get a binary somehow. Build from source for example. Can be anything, but its format matters.
- Compute a hash of the binary
- Compute a digital signature of the hash, using an asymmetric algorithm like RSA. Whatever thing does this step needs direct access to the private key (not the public key nor the certificate that holds it).
- Embed the signature in the right binary format, and usually also the hash and certificate in the binary.
Steps 1 and 2 are well known. It gets complicated at step 3 and 4.
If your private key is in an HSM, only the HSM can do step 3: sign the hash.
The fact that Hashicorp Vault (Enterprise version) is connected to the HSM does not help because Vault does not provide a secret engine that can do step 4. Vault does not know how to insert a signature in a HLK binary file. Even worse, Vault, as of July 2022, does not offer a way to sign an arbitrary hash with a private key that is stored in an HSM.
Long story short: you cannot use Vault for your use case.
Since only the HSM has access to the private key, the HSM will do the actual signature. It will never reveal the private key, only return the results to the caller. Whatever called the HSM will have to insert the signature in the package, respecting the format.
Using signtool.exe
is out of the question because it does not support the Open Packaging Conventions standard. But it will help you debug your configuration.
I would break the problem down to these steps:
Make the HSM work with Windows.
Read your HSM vendor documentation so that your HSM's DLL is installed and configured to work as a 1st class Windows Cryptographic Service Provider. It could a smart card inserted in your workstation or a networked HSM (via a proprietary protocol, beware of firewalls between you and it).
Import your signing certificate in your Certificate Store
Using the The Hardware Lab Kit to sign your .hlkx package, you must select the "Use the certificate store" option. The "certificate file" option implies that you have the private key, but you don't. The HSM has it and it will never export it.
So for that option to work, the certificate must be in your certificate store. Check your vendor documentation to make sure you import the certificate in a way that tells the certificate store that the private key is held by the HSM.
Some examples have you provide the certificate file, but I suspect it's just to extract its hash and look it up in the certificate store.
Test your configuration
At this point, you need a referee. You don't want to debug your HSM configuration when a pesky bug in your code is the real culprit. So run signtool
to sign any old binary you have lying around. You can sign notepad.exe
if you want:
copy %windir%\system32\notepad.exe .\my-notepad.exe
signtool sign /f certificate.cer /csp "Hardware Cryptography Module" my-notepad.exe
Replace Hardware Cryptography Module
with the name of your HSM's CSP name. Check your vendor documentation for this.
Run the code
There is a sample C# program that will sign using an HSM. You must provide the CSP's name, it is the same as the one you provided when testing with signtool.exe
.