Skip to main content

Scripts for signing using PKCS11 library on GitLab

Set up DigiCert​​®​​ Software Trust Manager client tools and integrate them with GitLab for automation into a CI/CD pipeline.

Prerequisites

  • GitLab build system

  • Any operating system that supports Java on GitLab

  • JDK installed on the agent

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

Create PKCS11 configuration file

To create a configuration file with the path to this shared library:

  1. Open an integrated development environment (IDE) or plain text editor.

  2. Copy and paste the following text into the editor:

  3. Save the file as pkcs11properties.cfg.

  4. Move the pkcs11properties.cfg file to the same location as the PKCS11 library.

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:

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

    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.

PKCS11_CONFIG

Provide the path to the PKCS11 configuration file.

SM_TLS_SKIP_VERIFY

Enter true to disable or false to enable TLS verification on client side.

Integration with GitLab

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:

Tabela 1. Environment variables

Environment variable

Description

SM_CLIENT_CERT_PASSWORD

Password from client authentication certificate setup.

SM_CLIENT_CERT_FILE_B64

Base64-encoded text of the 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.


Command to set environment variables:

  - set-variables

variables:
  SM_API_KEY: $SM_API_KEY
  SM_CLIENT_CERT_PASSWORD: $SM_CLIENT_CERT_PASSWORD
  SM_HOST: $SM_HOST
  Keypair: 'keypair-GitLab-test'
  SSM: 'C:\Program Files\DigiCert\DigiCert One Signing Manager Tools'

Nota

The path for smtools is: "C:\Program Files\DigiCert\DigiCert One Signing Manager Tools".

Set certificate file in environment variables

  1. Identify your downloaded client authentication certificate.

  2. Encode the file to base64, using the command:

    base64 file_name
  3. For GitLab variables, go to GitLab > Settings > CI/CD > Variables.

  4. Add the new variable name as "SM_CLIENT_CERT_FILE_B64" and the encoded value from base64 file_name in the "Value" field.

  5. Run this code to setup the 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'

Create keypair and certificate

Create test keypair and certificate

To generate an RSA test keypair with a certificate using GitLab:

stages:
  - Create-Test-Keypair

Create_Test_Keypair:
  stage: Create-Test-Keypair
  tags:  
  - shared-windows
  - windows
  - windows-1809
variables:
  SSM: 'C:\Program Files\DigiCert\DigiCert One Signing Manager Tools'
  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, meaning these names cannot already exist on the portal.

To retrieve the certificate profile ID:

  1. Access DigiCert ONE > DigiCert​​®​​ Software Trust Manager > Certificates > Certificate profiles.

  2. Select a certificate profile to generate the certificate (profile category must be test).

  3. Identify the Profile ID

Create production keypair with certificate

To generate an RSA production keypair with a given certificate using the GitLab pipeline:

stages:
  - Create-Production-Keypair
variables:
  SSM: 'C:\Program Files\DigiCert\DigiCert One Signing Manager Tools'
Create_Production_Keypair:
  stage: Create-Production-Keypair
  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 that you select a certificate profile that has the profile category "Production"

Create certificate from an existing keypair

To generate a certificate for an existing keypair using GitLab:

stages:
  - Create-Certificate
variables:
  SSM: 'C:\Program Files\DigiCert\DigiCert One Signing Manager Tools'
Create_Certificate:
  stage: Create-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>

For this step, the inputs required are:

  • Keypair ID

  • Certificate name (which must be unique)

  • Certificate profile ID

You can retrieve the keypair ID in two ways:

  1. Set the environment variables in SMCTL.

  2. Run

    smctl keypair ls

or

  1. Access DigiCert ONE > DigiCert​​®​​ Software Trust Manager > Keypairs.

  2. Select a keypair.

  3. Identify the keypair ID.

Nota

As in the previous use cases, the certificate profile ID used must belong to the correct profile category.

Sign

To sign using jarsigner:

stages:
  - Jarsigner-Signing
Jarsigner_signing:
  stage: Jarsigner-Signing
  tags:  
  - shared-windows
  - windows
  - windows-1809

  script:
    - |
      jarsigner -keystore NONE -storepass NONE -storetype PKCS11 -sigalg SHA256withRSA -providerClass sun.security.pkcs11.SunPKCS11 -providerArg %PKCS11_CONFIG% -signedjar <Path to Output Signed Jar> <Path to the Jar to be Signed> <Keypair Certificate Name/Alias > -tsa http://timestamp.digicert.com

The input parameters for this example are:

  • The output path for the signed .jar file.

  • The path to the .jar file to be signed.

  • The name or alias of the certificate to be used for signing.

Verify signature

To verify a signature using jarsigner:

stages:
  - Jarsigner-Verification
Jarsigner_verification:
  stage: Jarsigner-Verification
  tags:  
  - shared-windows
  - windows
  - windows-1809

  script:
    - |
      jarsigner -verify <path to signed .jar file>

The only input for this command is the path to the signed.jar file to be verified.

Sample pipeline

stages:
  - set-variables
  - pkcs11-signing

variables:
  SM_API_KEY: $SM_API_KEY
  SM_CLIENT_CERT_PASSWORD: $SM_CLIENT_CERT_PASSWORD
  SM_HOST: $SM_HOST
  Keypair: 'keypair-GitLab-test2'
  SSM: 'C:\Program Files\DigiCert\DigiCert One Signing Manager Tools'

pkcs11_signing:
  stage: pkcs11-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:$SM_API_KEY" -o smtools-windows-x64.msi
      msiexec.exe /i s-windows-x64.msi /quiet /qn | Wait-Process

      & $SSM\smctl.exe keypair generate rsa $Keypair --cert-alias=$Keypair --cert-profile-id=93a61900-e33f-4d24-a91e-f1e405db4452 --generate-cert=true --key-type=PRODUCTION

      Copy-Item -Path C:\GitLab-Runner\builds\digicert1\SSM-Signing\UNSIGNED.jar -Destination C:\

      jarsigner -keystore NONE -storepass NONE -storetype PKCS11 -sigalg SHA256withRSA -providerClass sun.security.pkcs11.SunPKCS11 -providerArg C:\GitLab-Runner\builds\digicert1\SSM-Signing\pkcs11properties.cfg -signedjar C:\SIGNED.jar C:\UNSIGNED.jar $Keypair -tsa http://timestamp.digicert.com

      jarsigner -verify C:\SIGNED.jar

PKCS11 file content

name=signingmanager
library= C:\GitLab-Runner\builds\digicert1\SSM-Signing\smpkcs11.dll
slotListIndex=0