Scripts for signing using KSP library on GitHub
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.
Nota
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.
Nota
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.
Nota
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.
Setup
To use the client tools and connect to DigiCert® Software Trust Manager for operations, you must have access to DigiCert® Software Trust Manager on DigiCert ONE®. If you do not have access to DigiCert® Software Trust Manager, contact an administrator for DigiCert ONE and request access.
Set certificate file in environment variables
Identify your client authentication certificate.
Encode the file to base64. For encoding run command:
base64 file_name
In GitHub Secrets add a new repository secret with:
Name = SM_CLIENT_CERT_FILE_B64 Value = encoded value from base64 file_name
Set dup certificate file for signing:
- name: Setup Certificate run: | echo "${{ secrets.SM_CLIENT_CERT_FILE_B64 }}" | base64 --decode > /d/Certificate_pkcs12.p12 shell: bash
Environment variables setup for GitHub Actions
The client tools need these environment variables to connect with DigiCert® Software Trust Manager,.
To integrate environment variables as GitHub secrets:
Access GitHub repository
Select Settings
Select Secrets > Actions
Select New repository secret
The values for these environment variables:
Variable | Description |
---|---|
SM_API_KEY | Provide your API token. |
SM_CLIENT_CERT_FILE_B64 | The base64 encoded text of certificate downloaded from client authentication certificate setup. |
SM_CLIENT_CERT_PASSWORD | Provide your client certificate password. |
SM_HOST | Provide your host environment. |
SM_CODE_SIGNING_CERT_SHA1_HASH | The certificate fingerprint. |
Command to set environment variables:
- name: Set variables
id: variables
run: |
echo "::set-output name=version::${GITHUB_REF#refs/tags/v}"
echo "SM_HOST=${{ secrets.SM_HOST }}" >> "$GITHUB_ENV"
echo "SM_API_KEY=${{ secrets.SM_API_KEY }}" >> "$GITHUB_ENV"
echo "SM_CLIENT_CERT_FILE=D:\\Certificate_pkcs12.p12" >> "$GITHUB_ENV"
echo "SM_CLIENT_CERT_PASSWORD=${{ secrets.SM_CLIENT_CERT_PASSWORD }}" >> "$GITHUB_ENV"
echo "C:\Program Files (x86)\Windows Kits\10\App Certification Kit" >> $GITHUB_PATH
echo "C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools" >> $GITHUB_PATH
echo "C:\Program Files\DigiCert\DigiCert One Signing Manager Tools" >> $GITHUB_PATH
shell: bash
Suggerimento
The following tool paths are provided above:
Software Trust Manager client tools
C:\Program Files\DigiCert\DigiCert One Signing Manager Tools
Signtool.exe
C:\Program Files (x86)\Windows Kits\10\App Certification Kit
Mage.exe
C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools
Client tool setup
Connect the KSP to GitHub actions and verify that it works.
Client control setup
To invoke the client control from GitHub actions
Download and install the smtools-windows-x64.msi using curl command given below:
- name: Setup SSM KSP on windows latest run: | curl -X GET https://one.digicert.com/signingmanager/api-ui/v1/releases/smtools-windows-x64.msi/download -H "x-api-key:%SM_API_KEY%" -o smtools-windows-x64.msi msiexec /i smtools-windows-x64.msi /quiet /qn shell: cmd
Nota
“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:
- name: Register KSP run: | smksp_registrar.exe list shell: cmd
Integration with GitHub actions
Find Signtool and Mage
Command to find signtool:
- name: Find Signtool run: | dir "C:\Program Files (x86)\Microsoft SDKs\Windows\ " /s /b | findstr /i signtool.exe shell: cmd
Command to find Mage:
- name: Find Signtool run: | dir "C:\Program Files (x86)\Microsoft SDKs\Windows\ " /s /b | findstr /i mage.exe shell: cmd
Create keypair and certificate
Create test keypair and certificate
An example for a GitHub actions step to generate a RSA test keypair with certificate:
- name: Generate Test Keypair run: | smctl keypair generate rsa <Keypair Name> --cert-alias=<Certificate Name> --cert-profile-id=<Certificate Profile ID> --generate-cert=true --key-type=TEST shell: cmd
The keypair name and certificate name must be unique, meaning that they cannot exist on the portal already.
To retrieve the certificate profile ID:
Go to DigiCert ONE > DigiCert® Software Trust Manager > Certificates > Certificate profiles.
Select a certificate profile to generate the certificate (Profile category must be test).
Identify the Profile ID.
Create production keypair with certificate
An example of a GitHub actions pipeline step to generate an RSA production keypair with certificate:
- name: Generate Production Keypair run: | smctl keypair generate rsa <Keypair Name> --cert-alias=<Certificate Name> --cert-profile-id=<Certificate Profile ID> --generate-cert=true --key-type=PRODUCTION shell: cmd
The parameters are the same as the previous case, but ensure you select a certificate profile that has the profile category as "Production".
Create certificate from an existing keypair
An example of a GitHub actions step to generate a certificate for an existing keypair:
- name: Generate Certificate for Existing Keypair run: | smctl keypair generate-cert <Keypair ID> --cert-alias=<Certificate Name> --cert-profile-id=<Certificate Profile ID> shell: cmd
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:
Set the environment variables in SMCTL.
Run
smctl keypair ls
Access DigiCert ONE > DigiCert® Software Trust Manager > Keypairs.
Select a keypair.
Identify the keypair ID.
As in the previous use cases, the certificate profile ID used must belong to the correct profile category.
Sign
You can sign with Mage, NuGet, or SignTool.
Sign with Mage
To sign with Mage:
- name: Signing using mage run: | mage.exe -sign <file_name> -CertHash <hash_or_cert_fingerprint> -a sha256RSA shell: cmd
Sign with NuGet
To sign with NuGet:
- name: Signing using nuget run: | nuget sign <package folder> -Timestamper http://timestamp.digicert.com -outputdirectory <output folder> -Certificate Fingerprint <SHA1 Thumbprint> -HashAlgorithm SHA256 -Verbosity detailed -Overwrite shell: cmd
Sign with SignTool
To sign with SignTool:
- name: Signing using signtool run: | signtool.exe sign /sha1 <certificate thumbprint> /tr http://timestamp.digicert.com /td SHA256 /fd SHA256 <file to be signed> shell: cmd
Verify signature
You can verify a signature using Mage, NuGet, or SignTool.
Verify signature with Mage
To verify a signature with Mage:
- name: Mage verify run: | mage -Verify <Path to signed .application file> shell: cmd- name: Mage verify run: | mage -Verify <path to signed .application file> shell: cmd
The only input required here is the path to the signed .application file.
Verify signature with NuGet
To verify a signature with NuGet:
- name: Nuget verify run: | nuget verify -All <output folder>\* shell: cmd- name: Nuget verify run: | nuget verify -All <output folder>\* shell: cmd
The only input required here is the path to the signed folder.
Verify signature with SignTool
To verify a signature with SignTool:
- name: Signtool verify run: | signtool verify /v /pa <Path to the signed exe/DLL> shell: cmd- name: Signtool verify run: | signtool verify /v /pa <path to the signed .exe or .dll> shell: cmd
The only input for this step is the path to the signed .exe or .dll file that needs to be verified.
Sample GitHub actions
KSP Signing using GitHub action
name: 'build and release electron installer' on: push: tags: - 'v*' jobs: release: runs-on: ${{ matrix.os }} strategy: matrix: os: [windows-latest] steps: - name: Check out Git repository uses: actions/checkout@v2 - name: Install Node.js, NPM and Yarn uses: actions/setup-node@v2 with: node-version: 14 - name: Build/release Electron app uses: samuelmeuli/action-electron-builder@v1 with: github_token: ${{ secrets.github_token }} release: ${{ startsWith(github.ref, 'refs/tags/v') }} build_script_name: dist - name: NuGet Install uses: NuGet/setup-nuget@v1.0.5 with: nuget-version: latest - name: Setup Certificate run: | echo "${{ secrets.SM_CLIENT_CERT_FILE_B64 }}" | base64 --decode > /d/Certificate_pkcs12.p12 cat /d/Certificate_pkcs12.p12 shell: bash - name: Set variables id: variables run: | echo "::set-output name=version::${GITHUB_REF#refs/tags/v}" echo "SM_HOST=${{ secrets.SM_HOST }}" >> "$GITHUB_ENV" echo "SM_API_KEY=${{ secrets.SM_API_KEY }}" >> "$GITHUB_ENV" echo "SM_CLIENT_CERT_FILE=D:\\Certificate_pkcs12.p12" >> "$GITHUB_ENV" echo "SM_CLIENT_CERT_PASSWORD=${{ secrets.SM_CLIENT_CERT_PASSWORD }}" >> "$GITHUB_ENV" echo "C:\Program Files (x86)\Windows Kits\10\App Certification Kit" >> $GITHUB_PATH echo "C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools" >> $GITHUB_PATH echo "C:\Program Files\DigiCert\ DigiCert One Signing Manager Tools" >> $GITHUB_PATH shell: bash - name: Setup SSM KSP on windows latest run: | curl -X GET https://one.digicert.com/signingmanager/api-ui/v1/releases/smtools-windows-x64.msi/download -H "x-api-key:%SM_API_KEY%" -o smtools-windows-x64.msi msiexec /i smtools-windows-x64.msi /quiet /qn smksp_registrar.exe list smctl.exe keypair ls C:\Windows\System32\certutil.exe -csp "DigiCert Signing Manager KSP" -key -user smksp_cert_sync.exe shell: cmd - name: Signing using Signtool run: | signtool.exe sign /sha1 ${{ secrets.SM_CODE_SIGNING_CERT_SHA1_HASH }} /tr http://timestamp.digicert.com /td SHA256 /fd SHA256 "D:\a\github-action-electron\github-action-electron\dist\my-electron-app Setup ${{ steps.variables.outputs.version }}.exe" signtool.exe verify /v /pa "D:\a\github-action-electron\github-action-electron\dist\my-electron-app Setup ${{ steps.variables.outputs.version }}.exe" - name: Signing using Nuget run: | copy D:\a\github-action-electron\github-action-electron\UNSIGNED.nupkg D:\a\github-action-electron\github-action-electron\dist\UNSIGNED.nupkg nuget sign "D:\a\github-action-electron\github-action-electron\dist\UNSIGNED.nupkg" -Timestamper http://timestamp.digicert.com -outputdirectory "D:\a\github-action-electron\github-action-electron\dist\Signed" -CertificateFingerprint ${{ secrets.SM_CODE_SIGNING_CERT_SHA1_HASH }} -HashAlgorithm SHA256 -Verbosity detailed -Overwrite nuget verify -All "D:\a\github-action-electron\github-action-electron\dist\Signed\*" - name: Signing using Mage run: | copy D:\a\github-action-electron\github-action-electron\UNSIGNED2.manifest D:\a\github-action-electron\github-action-electron\dist\UNSIGNED2.manifest mage.exe -sign "D:\a\github-action-electron\github-action-electron\dist\UNSIGNED2.manifest" -CertHash ${{ secrets.SM_CODE_SIGNING_CERT_SHA1_HASH }} -a sha256RSA mage -Verify "D:\a\github-action-electron\github-action-electron\dist\UNSIGNED2.manifest" - name: Upload artifacts uses: actions/upload-artifact@v2 with: name: ${{ matrix.os }} path: | dist
How to run and build the code
Update the version in your project's package.json file ( e.g. 1.2.3)
Commit that change (git commit -am v1.2.3)
Tag your commit (git tag v1.2.3). Make sure your tag name's format is v*.*.*.
Nota
Your workflow will use this tag to detect when to create a release.
Push your changes to GitHub (git push && git push --tags)