Automatically calculate certificate thumbprint
sign requires a certificate thumbprint to be explicitly specified in the command line:
Usage:
sign code certificate-store <file(s)>... [options]
Arguments:
<file(s)> File(s) to sign.
Options:
-cfp, --certificate-fingerprint <certificate-fingerprint> (REQUIRED) SHA fingerprint used to identify a certificate.
-cf, --certificate-file <certificate-file> PFX, P7B, or CER file containing a certificate and potentially a private key.
-p, --password <password> Password for certificate file.
-csp, --crypto-service-provider <crypto-service-provider> Cryptographic Service Provider containing the private key container. Requires /k and optionally /km.
-k, --key-container <key-container> Private key container name.
However, when a certificate file is specified with -cf ( --certificate-file) option, it already has all the information to calculate its thumbprint. In this case, -cfp (--certificate-fingerprint) option should not be obligatory.
The current behavior requires a user to always specify -cfp (--certificate-fingerprint) option, even when the certificate file is specified explicitly. This adds certain frictions to sign usage as the user now has to manually extract/calculate the certificate fingerprint, even when it isn't strictly needed and can be extracted from the specified certificate file automatically.
sign version: 0.9.1-beta.25379.1+ba6e717abf74a693f0f9c5e891c0e3ef624956b3
I absolutely agree. Personally, I've got the issue that the fingerprint is not recognized as valid. There is no clear documentation but I ended up using this command to sign a simple hello world executable:
sign.exe code certificate-store --base-directory %WORKDIR% --certificate-file %SIGNING_CERT_FILE_PATH% --password %SIGNING_CERT_PASSWORD% --certificate-fingerprint 7ff9ec366aa510597ea6067c8939a80eb8d53c61 HelloWorld.Net10.exe
Result with return code 1:
Invalid value for --certificate-fingerprint. The value must be a SHA-256, SHA-384, or SHA-512 certificate fingerprint (in hexadecimal).
Description:
Use Windows Certificate Store or a local certificate file.
Usage:
sign code certificate-store <file(s)>... [options]
Arguments:
<file(s)> File(s) to sign.
....
This is the powershell script I use before, which also works with Windows' Powershell, which is older:
$securePassword = ConvertTo-SecureString -String $env:SIGNING_CERT_PASSWORD -AsPlainText -Force
$pfxData = Get-PfxData -FilePath $env:SIGNING_CERT_FILE_PATH -Password $securePassword
$pfxData.EndEntityCertificates[0].Thumbprint.ToLower()
Is there anything I miss while using dotnet-sign?
Hi @thopiekar, and maintainers! I’d like to work on this. My understanding of the approach: when --certificate-file is given and --certificate-fingerprint is omitted, compute the SHA-256 fingerprint from the certificate; an explicit --certificate-fingerprint would still take precedence.
Two questions:
If the file contains more than one certificate (e.g. a P7B), should we require --certificate-fingerprint (clear error), or pick the end-entity certificate with the private key? For a CER/P7B without a private key (key from --crypto-service-provider / --key-container), is deriving the fingerprint also in scope, or only PFX files?
I recently contributed #1076 and will add tests with the change.
@minahilatiqdev - thank you. We think that the fix should be as follows:
- If a CER is provided then certificate fingerprint is not required.
- If a P7B or PFX file is provided and it contains only one certificate then certificate fingerprint is not required.
- If a P7B or PFX file is provided and it contains multiple certificates then we should fail fast and display a clear message.
Thanks @kartheekp-ms, that’s clear. I’ll implement it that way: no fingerprint needed for CER, or for PFX/P7B containing a single certificate; fail fast with a clear message when a PFX/P7B contains multiple certificates and no fingerprint is given. An explicit --certificate-fingerprint will still be honored. I’ll start on a PR with tests.
Hi @kartheekp-ms, I opened a PR for this: #1126.
It follows the rules you described: the fingerprint is not required for a CER file or a PFX/P7B with a single certificate; a PFX/P7B with multiple certificates fails with a clear message; and an explicit -cfp is still honored. When no certificate file is given, the fingerprint is still required.
One question: a PFX that contains a certificate chain (leaf plus intermediates) is counted as multiple certificates, so it requires -cfp. Would you prefer that I automatically pick the certificate that has a private key in that case? Happy to change it if so.