Skip to main content

Scripts for signing using KSP library on CircleCI

Prerequisites

  • A GitHub repository

  • DigiCert​​®​​ KeyLocker credentials

  • DigiCert​​®​​ KeyLocker client tools

Client tools

DigiCert​​®​​ KeyLocker clients can be downloaded in a package.

Download Client tools

  1. Sign in to DigiCert ONE.

  2. Navigate to DigiCert​​®​​ KeyLocker > Resources > Client tool repository.

  3. Select your operating system.

  4. Click the download icon next to DigiCert​​®​​ KeyLocker 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​​®​​ KeyLocker 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​​®​​ KeyLocker.

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.

注意

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

DigiCert​​®​​ KeyLocker 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:

  1. Sign in to DigiCert ONE.

  2. Select the profile icon (top-right).

  3. Select Admin Profile.

  4. Scroll down to API Tokens.

  5. Select  Create API token.

    注意

    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:

  1. Sign in to DigiCert ONE.

  2. Select the profile icon (top-right).

  3. Select Admin Profile.

  4. Scroll down to Authentication certificates.

  5. Select Create authentication certificate.

    注意

    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​​®​​ KeyLocker 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​​®​​ KeyLocker client in a package of your choice from DigiCert ONE > DigiCert​​®​​ KeyLocker > Resources > Client tool repository. Download and install the Keylockertools-windows-x64.msi using curl command given below, to invoke the client control from CircleCI:

注意

“curl” command is used to install the Keylockertools-windows-x64.msi installer and “msiexec” is used to silently install the Keylockertools-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 Keylocker 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 

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/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 Keylocker 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