Skip to main content

Scripts for signing using KSP library on GitLab

Prerequisites

  • A GitLab 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

  1. Sign in to DigiCert ONE.

  2. Navigate to DigiCert​​®​​ Software Trust Manager > Resources > Client tool repository.

  3. Select your operating system.

  4. 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 permissionsUsers 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.

    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 permissionsUsers 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.

    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.

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.

Set certificate file in environment variables

  1. Identify your client authentication certificate.

  2. Encode the file to base64. For encoding run command:

    base64 file_name
  3. In GitLab > CI/CD Settings > Variables add the new variables:

    Key = SM_CLIENT_CERT_FILE_B64
    Value = encoded value from base64 file_name
  4. Setup certificate file for signing:

    stages:
      - set-SM-certificate
    
    variables:
      SM_API_KEY: $SM_API_KEY
      SM_CLIENT_CERT_PASSWORD: $SM_CLIENT_CERT_PASSWORD
      SM_HOST: $SM_HOST
    
    set_SM_certificate:
      stage: set-SM-certificate
      tags:  
      - shared-windows
      - windows
      - windows-1809
    
      script:
        - |
          cd C:\
          New-Item C:\CERT_FILE.p12.b64
          Set-Content -Path C:\CERT_FILE.p12.b64 -Value $SM_CLIENT_CERT_FILE_B64
          certutil -decode CERT_FILE.p12.b64 Certificate_pkcs12.p12
    
      variables:
        SM_CLIENT_CERT_FILE: 'C:\Certificate_pkcs12.p12'

Environment variables setup for GitLab CI/CD

The client tools need these environment variables to connect with DigiCert​​®​​ Software Trust Manager.

They can be integrated as GitLab variables in GitLab Repository.

The values for these environment variables:

tabla 1. Environment variables

Environment variables

Description

SM_CLIENT_CERT_PASSWORD

The password from client authentication certificate setup.

SM_CLIENT_CERT_FILE_B64

The base64 encoded text of certificate downloaded from client authentication certificate setup.

SM_HOST

The path to the DigiCert ONE portal with client authorization.

Nota

In most cases, this path stays as it is unless you are connecting to a self-hosted instance of the DigiCert ONE product.

SM_API_KEY

The API token generated during API token setup.

SM_CODE_SIGNING_CERT_SHA1_HASH

The certificate fingerprint.


Command to set environment variables:

stages:
  - set-variables

variables:
  SM_API_KEY: $SM_API_KEY
  SM_CLIENT_CERT_PASSWORD: $SM_CLIENT_CERT_PASSWORD
  SM_HOST: $SM_HOST
  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'
  SSM: 'C:\Program Files\DigiCert\DigiCert ONE Signing Manager Tools'

Nota

"C:\Program Files (x86)\Windows Kits\10\App Certification Kit" is the path of signtool.exe and "C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools" is the path of mage.exe

Client tools setup

Client control setup

Download and install the smtools-windows-x64.msi using curl command given below, to invoke the client control from GitLab CI/CD:

stages:
  - set-SM-certificate

set_SM_certificate:
  stage: set-SM-certificate
  tags:  
  - shared-windows
  - windows
  - windows-1809

  script:
    - |
      curl.exe -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.exe /i smtools-windows-x64.msi /quiet /qn | Wait-Process

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 that the KSP is registered, run:

stages:
  - set-SM-certificate

set_SM_certificate:
  stage: set-SM-certificate
  tags:  
  - shared-windows
  - windows
  - windows-1809

  script:
    - |
      & $SSM\smksp_registrar.exe list

Integration with GitLab CI/CD

Find Signtool and Mage

Command to find signtool:

stages:
  - set-SM-certificate

set_SM_certificate:
  stage: set-SM-certificate
  tags:  
  - shared-windows
  - windows
  - windows-1809

  script:
    - |
      Get-ChildItem -Path 'C:\Program Files (x86)\Windows Kits\10\App Certification Kit' -Filter signtool.exe -Recurse

Command to find Mage:

stages:
  - set-SM-certificate

set_SM_certificate:
  stage: set-SM-certificate
  tags:  
  - shared-windows
  - windows
  - windows-1809

  script:
    - |
      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 GitLab CI/CD step to generate a RSA test keypair with certificate:

stages:
  - set-SM-certificate

set_SM_certificate:
  stage: set-SM-certificate
  tags:  
  - shared-windows
  - windows
  - windows-1809

  script:
    - |
      & $SSM\smctl.exe keypair generate rsa <keypair name> --cert-alias=<certificate name> --cert-profile-id=<certificate profile id> --generate-cert=true --key-type=TEST

Nota

The keypair name and certificate name must be unique inputs, meaning that they cannot exist on the portal already.

Create production keypair with certificate

An example of a GitLab CI/CD pipeline step to generate a RSA production keypair with certificate:

stages:
  - set-SM-certificate

set_SM_certificate:
  stage: set-SM-certificate
  tags:  
  - shared-windows
  - windows
  - windows-1809

  script:
    - |
          & $SSM\smctl.exe keypair generate rsa <keypair name> --cert-alias=<certificate name> --cert-profile-id=<certificate profile id> --generate-cert=true --key-type=PRODUCTION

The parameters are the same as the previous case, but ensure you select a certificate profile that has the profile category “Production”.

Create certificate from an existing keypair

An example of a GitLab CI/CD step to generate a certificate for an existing keypair:

stages:
  - set-SM-certificate

set_SM_certificate:
  stage: set-SM-certificate
  tags:  
  - shared-windows
  - windows
  - windows-1809

  script:
    - |
       & $SSM\smctl.exe keypair generate-cert <keypair id> --cert-alias=<certificate name> --cert-profile-id=<certificate profile id>

Sign

You can sign with Mage, NuGet, or SignTool.

Sign with SignTool

To sign with SignTool:

stages:
  - set-SM-certificate

set_SM_certificate:
  stage: set-SM-certificate
  tags:  
  - shared-windows
  - windows
  - windows-1809

  script:
    - |
          & $SIGNTOOL sign /sha1 <certificate thumbprint> /tr http://timestamp.digicert.com /td SHA256 /fd SHA256 <file to be signed>

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:

Signing using nuget.exe:
stages:
  - set-SM-certificate

set_SM_certificate:
  stage: set-SM-certificate
  tags:  
  - shared-windows
  - windows
  - windows-1809

  script:
    - |
           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. The certificate thumbprint on can be identified on the certificate details page in DigiCert ONE portal.

Verify signature

You can verify a signature with Mage, NuGet, or SignTool.

Verify signature with Mage

To verify a signature with Mage:

stages:
  - set-SM-certificate

set_SM_certificate:
  stage: set-SM-certificate
  tags:  
  - shared-windows
  - windows
  - windows-1809

  script:
    - |
           & $MAGE -Verify <Path to signed .application file>

The only input required here is the path to the signed .application file.

Verify signature with NuGet

To verify a signature with NuGet:

stages:
  - set-SM-certificate

set_SM_certificate:
  stage: set-SM-certificate
  tags:  
  - shared-windows
  - windows
  - windows-1809

  script:
    - |
           nuget verify -All <output folder>\*

The only input required here is the path to the signed folder.

Verify signature with SignTool

To verify a signature with SignTool:

stages:
  - set-SM-certificate

set_SM_certificate:
  stage: set-SM-certificate
  tags:  
  - shared-windows
  - windows
  - windows-1809

  script:
    - |
           & $SIGNTOOL verify /v /pa <Path to the signed .exe or .dll>

The only input for this step is the path to the signed .exe or .dll that needs to be verified.

Sample GitLab CI/CD

KSP Signing using GitLab CI/CD

stages:
  - set-variables
  - ksp-signing

variables:
  SM_API_KEY: $SM_API_KEY
  SM_CLIENT_CERT_PASSWORD: $SM_CLIENT_CERT_PASSWORD
  SM_HOST: $SM_HOST
  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'
  SSM: 'C:\Program Files\DigiCert\DigiCert ONE Signing Manager Tools'

ksp_signing_job:
  stage: ksp-signing
  tags:  
  - shared-windows
  - windows
  - windows-1809

  variables:
    SM_CLIENT_CERT_FILE: 'C:\Certificate_pkcs12.p12'


  script:
    - |
      cd C:\
      New-Item C:\CERT_FILE.p12.b64
      Set-Content -Path C:\CERT_FILE.p12.b64 -Value $SM_CLIENT_CERT_FILE_B64
      certutil -decode CERT_FILE.p12.b64 Certificate_pkcs12.p12
      
      curl.exe -X GET  https://one.digicert.com/signingmanager/api-ui/v1/releases/smtools-windows-x64.msi/download -H "x-api-key:019d9d49dd7271dd236e84fb83_c879133c1c3f524f2ef68327674135080d34018a3d6128b84e5c982c5769b088" -o smtools-windows-x64.msi
      msiexec.exe /i smtools-windows-x64.msi /quiet /qn | Wait-Process

      & $SSM\smksp_cert_sync.exe

      Copy-Item -Path GitLab-Runner\builds\mypersonal23\signing\UNSIGNED.EXE -Destination C:\
      Copy-Item -Path GitLab-Runner\builds\mypersonal23\signing\UNSIGNED1.manifest -Destination C:\
      Copy-Item -Path GitLab-Runner\builds\mypersonal23\signing\UNSIGNED.nupkg -Destination C:\
      dir 'C:\'

      & $SIGNTOOL sign /sha1 $SM_CODE_SIGNING_CERT_SHA1_HASH /tr http://timestamp.digicert.com /td SHA256 /fd SHA256 C:\UNSIGNED.EXE
      & $SIGNTOOL verify /v /pa C:\UNSIGNED.EXE

      & $MAGE -sign C:\UNSIGNED1.manifest -CertHash $SM_CODE_SIGNING_CERT_SHA1_HASH -a sha256RSA
      & $MAGE -Verify C:\UNSIGNED1.manifest
      
      nuget sign C:\UNSIGNED.nupkg -Timestamper http://timestamp.digicert.com -outputdirectory C:\Signed -CertificateFingerprint $SM_CODE_SIGNING_CERT_SHA1_HASH -HashAlgorithm SHA256 -Verbosity detailed -Overwrite
      nuget verify -All C:\Signed\*
      dir C:\