Scripts for signing using KSP library on CircleCI
Prerequisites
A GitHub repository
DigiCert® Software Trust Manager credentials
DigiCert® Software Trust Manager client tools
Client tools
DigiCert® Software Trust Manager clients can be downloaded in a package.
Download Client tools
Sign in to DigiCert ONE.
Navigate to DigiCert® Software Trust Manager > Resources > Client tool repository.
Select your operating system.
Click the download icon next to DigiCert® Software Trust Manager clients.
Register the KSP
To register the KSP, open a command prompt and run:
smksp_registrar.exe register
Verify the KSP
To verify that your KSP is configured properly, and that your client can properly authenticate to the DigiCert® Software Trust Manager service, run:
certutil.exe -csp "DigiCert Software Trust Manager KSP" -key -user
Synchronize certificates
For the client tools to access the private keys in the service through the Key Storage Provider (KSP), your certificates must be synchronized to the local certificate store. Only if the certificate is synchronized, the private key remains stored securely in DigiCert® Software Trust Manager.
To synchronize your certificates to the local certificate store, open a command prompt and run:
smksp_cert_sync.exe
To view the certificates, open Certificate Manager for the user account used to run the certificate sync utility:
certmgr.msc
If you do not see your certificates in the Certificate Manager, verify that you have opened the correct certificate store. There is a different certificate store for each Windows user account.
Note
All certificates are synched to the user store only. The certificates are not synchronized to the machine store (yet).
Set PATH environment variables
Operating systems use the environment variable called PATH to determine where executable files are stored on your system. Use the PATH environment variable to store the file path to your signing tools to ensure that the CLI can reference these signing tools.
User authentication
Software Trust Manager enforces multifactor authentication for security. To access keypairs, certificates, and sign code, you need to set up two types of credentials: an API token and an authentication certificate.
Create an API token
The API token is an authentication method used to verify you as a user and your permissions assigned in DigiCert ONE. The API token provides the first factor authentication.
Follow these steps to generate an API token:
Sign in to DigiCert ONE.
Select the profile icon (top-right).
Select Admin Profile.
Scroll down to API Tokens.
Select Create API token.
Note
The API token is only shown once, securely store the API key to use it later.
Create an authentication certificate
The client authentication certificate is an authentication method used to verify you as a user and your permissions assigned in DigiCert ONE. The client authentication certificate provides the second factor authentication.
Follow these steps to create a client authentication certificate:
Sign in to DigiCert ONE.
Select the profile icon (top-right).
Select Admin Profile.
Scroll down to Authentication certificates.
Select Create authentication certificate.
Note
The client authentication certificate password shown after creating an client authentication certificate cannot be accessed again, download the certificate and securely store the password to use it later.
DigiCert® Software Trust Manager environment variables
Set the following environment variables:
Variable | Description |
---|---|
SM_API_KEY | Provide your API token. |
SM_CLIENT_CERT_FILE | Provide your client authentication certificate. |
SM_CLIENT_CERT_PASSWORD | Provide your client certificate password. |
SM_HOST | Provide your host environment. |
Client tools setup
Client control setup
Based on the OS, download the DigiCert® Software Trust Manager client in a package of your choice from DigiCert ONE > DigiCert® Software Trust Manager > Resources > Client tool repository. Download and install the smtools-windows-x64.msi using curl command given below, to invoke the client control from CircleCI:
steps: - checkout - run: name: "Client-Tool-Setup" command: | cd C:\ curl.exe -X GET https://one.digicert.com/signingmanager/api-ui/v1/releases/smtools-windows-x64.msi/download -H "x-api-key:$env:SM_API_KEY" -o smtools-windows-x64.msi
msiexec.exe /i smtools-windows-x64.msi /quiet /qn | Wait-Process
steps: - checkout - run: name: "Client-Tool-Setup" command: | cd C:\ curl.exe -X GET https://one.digicert.com/signingmanager/api-ui/v1/releases/Keylockertools-windows-x64.msi/download -H "x-api-key:$env:SM_API_KEY" -o Keylockertools-windows-x64.msi msiexec.exe /i Keylockertools-windows-x64.msi /quiet /qn | Wait-Process
Note
“curl” command is used to install the smtools-windows-x64.msi installer and “msiexec” is used to silently install the smtools-windows-x64.msi.
Verify the KSP
To verify the KSP is registered, run:
steps:
- checkout
- run:
name: "Verify-KSP"
command: |
& $env:SSM\smksp_registrar.exe list
environment:
SSM: 'C:\Program Files\DigiCert\DigiCert One Signing Manager Tools'
Integration with CircleCI
Find SignTool and Mage
Command to find SignTool:
steps: - checkout - run: name: "Find Signtool" command: | Get-ChildItem -Path 'C:\Program Files (x86)\Windows Kits\10\App Certification Kit' -Filter signtool.exe -Recurse
Command to find Mage:
steps: - checkout - run: name: "Find Mage" command: | Get-ChildItem -Path 'C:\Program Files (x86)\Microsoft SDKs\Windows' -Filter mage.exe -Recurse
Create keypair and certificate
Create test keypair and certificate
An example for a CircleCI step to generate an RSA test keypair with certificate:
steps:
- checkout
- run:
name: "Create-Test-Keypair"
command: |
& $env:SSM\smctl.exe keypair generate rsa <Keypair Name> --cert-alias=<Certificate Name> --cert-profile-id=<Certificate Profile ID> --generate-cert=true --key-type=TEST
environment:
SSM: 'C:\Program Files\DigiCert\DigiCert One Signing Manager Tools'
The keypair name and certificate name must be unique inputs, meaning that they cannot exist on the portal already.
To retrieve the certificate profile ID:
Sign in to DigiCert ONE.
Navigate to:DigiCert® Software Trust Manager > Certificates > Certificate profiles.
Select a certificate profile that you want to use to generate the certificate.
Note
Certificate profile category should be Test.
Identify the Certificate profile ID field.
Create production keypair with certificate
An example of a CircleCI pipeline step to generate a RSA production keypair with certificate:
steps:
- checkout
- run:
name: "Create-Production-Keypair"
command: |
& $env:SSM\smctl.exe keypair generate rsa <Keypair Name> --cert-alias=<Certificate Name> --cert-profile-id=<Certificate Profile ID> --generate-cert=true --key-type=PRODUCTION
environment:
SSM: 'C:\Program Files\DigiCert\DigiCert One Signing Manager Tools'
The parameters are the same as the previous case, but be sure to select a certificate profile with the profile category "Production".
Create certificate from an existing keypair
An example of a CircleCI step to generate a certificate for an existing keypair:
steps:
- checkout
- run:
name: "Create-Certificate"
command: |
& $env:SSM\smctl.exe keypair generate-cert <Keypair ID> --cert-alias=<Certificate Name> --cert-profile-id=<Certificate Profile ID>
environment:
SSM: 'C:\Program Files\DigiCert\DigiCert One Signing Manager Tools'
For this step, the inputs required are a keypair ID, certificate name (which must be unique), and certificate profile ID.
You can retrieve the keypair ID in two ways:
Run a
smctl keypair ls
command on the command line with all environment variables set up.Refer to DigiCert ONE > DigiCert® Software Trust Manager > Keypairs and select a keypair.
As in the previous use cases, the certificate profile ID used must belong to the correct profile category.
Sign
You can sign using Mage, NuGet, or SignTool. These commands require the certificate thumbprint.
Sign with SignTool
To sign with SignTool:
steps: - checkout - run: name: "Signtool-Signing" command: | & $env:Signtool sign /sha1 <certificate thumbprint> /tr http://timestamp.digicert.com /td SHA256 /fd SHA256 <file to be signed> environment: Signtool: 'C:\Program Files (x86)\Windows Kits\10\App Certification Kit\signtool.exe'
The input parameters are the thumbprint of the certificate to be used for signing and the path to the .exe or .dll to be signed.
Sign with Mage
To sign with Mage:
steps: - checkout - run: name: "Mage-Signing" command: | & $env:Mage -sign <file_name> -CertHash <hash_or_cert_fingerprint> -a sha256RSA environment: Mage: 'C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\mage.exe'
The input parameters are the path to the .application file to be signed and the thumbprint of the certificate. You can find the thumbprint on the Certificate details page in DigiCert ONE.
Sign with NuGet
To sign with NuGet:
steps: - checkout - run: name: "Nuget-Signing" command: | nuget sign <package folder> -Timestamper http://timestamp.digicert.com -outputdirectory <output folder> -Certificate Fingerprint <SHA1 Thumbprint> -HashAlgorithm SHA256 -Verbosity detailed -Overwrite
The inputs to this task are the path to the package and the thumbprint of the certificate. You can find the certificate thumbprint on Certificate details page in DigiCert ONE.
Verify signature
You can verify a signature using Mage, NuGet, or SignTool.
Verify signature with Mage
To verify a signature with Mage:
steps: - checkout - run: name: "Mage-Verification" command: | & $env:Mage -Verify <Path to signed .application file> environment: Mage: 'C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\mage.exe'
The only input required here is the path to the signed .application file.
Verify signature with NuGet
To verify a signature with NuGet:
steps: - checkout - run: name: "Nuget-Verification" command: | nuget verify -All <output folder>\*
Verify signature with SignTool
To verify a signature with SignTool:
steps: - checkout - run: name: "Signtool-Verification" command: | & $env:Signtool verify /v /pa <Path to the signed exe/DLL> environment: Signtool: 'C:\Program Files (x86)\Windows Kits\10\App Certification Kit\signtool.exe'
The only input for this step is the path to the signed .exe or .dll that needs to be verified.
Sample CircleCI
KSP signing using CircleCI
version: 2.1 orbs: win: circleci/windows@4.1 jobs: SSM_Signing: executor: win/server-2022 steps: - checkout - run: name: "Certificate-Setup" command: | cd C:\ New-Item C:\CERT_FILE.p12.b64 Set-Content -Path C:\CERT_FILE.p12.b64 -Value $env:SM_CLIENT_CERT_FILE_B64 certutil -decode CERT_FILE.p12.b64 Certificate_pkcs12.p12 cat Certificate_pkcs12.p12 - run: name: "Client-Tool-Setup" command: | cd C:\ curl.exe -X GET https://one.digicert.com/signingmanager/api-ui/v1/releases/smtools-windows-x64.msi/download -H "x-api-key:$env:SM_API_KEY" -o smtools-windows-x64.msi msiexec.exe /i smtools-windows-x64.msi /quiet /qn | Wait-Process & 'C:\Program Files\DigiCert\DigiCert One Signing Manager Tools\smksp_cert_sync.exe' - run: name: "Signtool-Signing" command: | Get-ChildItem -Path 'C:\Program Files (x86)\Windows Kits\10\App Certification Kit' -Filter signtool.exe -Recurse Copy-Item -Path C:\Users\circleci.PACKER-624EE792\project\UNSIGNED.EXE -Destination C:\ & $env:Signtool sign /sha1 $env:SM_CODE_SIGNING_CERT_SHA1_HASH /tr http://timestamp.digicert.com /td SHA256 /fd SHA256 C:\UNSIGNED.EXE & $env:Signtool verify /v /pa C:\UNSIGNED.EXE - run: name: "Nuget-Signing" command: | Copy-Item -Path C:\Users\circleci.PACKER-624EE792\project\UNSIGNED.nupkg -Destination C:\ nuget sign C:\UNSIGNED.nupkg -Timestamper http://timestamp.digicert.com -outputdirectory C:\Signed -CertificateFingerprint $env:SM_CODE_SIGNING_CERT_SHA1_HASH -HashAlgorithm SHA256 -Verbosity detailed -Overwrite nuget verify -All C:\Signed\* - run: name: "Mage-Signing" command: | Copy-Item -Path C:\Users\circleci.PACKER-624EE792\project\UNSIGNED1.manifest -Destination C:\ & $env:Mage -sign C:\UNSIGNED1.manifest -CertHash $env:SM_CODE_SIGNING_CERT_SHA1_HASH -a sha256RSA & $env:Mage -Verify C:\UNSIGNED1.manifest environment: SM_CLIENT_CERT_FILE: 'C:\Certificate_pkcs12.p12' Signtool: 'C:\Program Files (x86)\Windows Kits\10\App Certification Kit\signtool.exe' Mage: 'C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\mage.exe' workflows: my-workflow: jobs: - SSM_Signing
version: 2.1
orbs:
win: circleci/windows@4.1
jobs:
SSM_Signing:
executor: win/server-2022
steps:
- checkout
- run:
name: "Certificate-Setup"
command: |
cd C:\
New-Item C:\CERT_FILE.p12.b64
Set-Content -Path C:\CERT_FILE.p12.b64 -Value $env:SM_CLIENT_CERT_FILE_B64
certutil -decode CERT_FILE.p12.b64 Certificate_pkcs12.p12
cat Certificate_pkcs12.p12
- run:
name: "Client-Tool-Setup"
command: |
cd C:\
curl.exe -X GET https://one.digicert.com/signingmanager/api-ui/v1/releases/Keylockertools-windows-x64.msi/download -H "x-api-key:$env:SM_API_KEY" -o Keylockertools-windows-x64.msi
msiexec.exe /i Keylockertools-windows-x64.msi /quiet /qn | Wait-Process
& 'C:\Program Files\DigiCert\DigiCert One Signing Manager Tools\smksp_cert_sync.exe'
- run:
name: "Signtool-Signing"
command: |
Get-ChildItem -Path 'C:\Program Files (x86)\Windows Kits\10\App Certification Kit' -Filter signtool.exe -Recurse
Copy-Item -Path C:\Users\circleci.PACKER-624EE792\project\UNSIGNED.EXE -Destination C:\
& $env:Signtool sign /sha1 $env:SM_CODE_SIGNING_CERT_SHA1_HASH /tr http://timestamp.digicert.com /td SHA256 /fd SHA256 C:\UNSIGNED.EXE
& $env:Signtool verify /v /pa C:\UNSIGNED.EXE
- run:
name: "Nuget-Signing"
command: |
Copy-Item -Path C:\Users\circleci.PACKER-624EE792\project\UNSIGNED.nupkg -Destination C:\
nuget sign C:\UNSIGNED.nupkg -Timestamper http://timestamp.digicert.com -outputdirectory C:\Signed -CertificateFingerprint $env:SM_CODE_SIGNING_CERT_SHA1_HASH -HashAlgorithm SHA256 -Verbosity detailed -Overwrite
nuget verify -All C:\Signed\*
- run:
name: "Mage-Signing"
command: |
Copy-Item -Path C:\Users\circleci.PACKER-624EE792\project\UNSIGNED1.manifest -Destination C:\
& $env:Mage -sign C:\UNSIGNED1.manifest -CertHash $env:SM_CODE_SIGNING_CERT_SHA1_HASH -a sha256RSA
& $env:Mage -Verify C:\UNSIGNED1.manifest
environment:
SM_CLIENT_CERT_FILE: 'C:\Certificate_pkcs12.p12'
Signtool: 'C:\Program Files (x86)\Windows Kits\10\App Certification Kit\signtool.exe'
Mage: 'C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\mage.exe'
workflows:
my-workflow:
jobs:
- SSM_Signing