Skip to main content

Scripts for signing using PKCS11 library on GitLab

Set up 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​​®​​ 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.

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

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.

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

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​​®​​ KeyLocker. They can be integrated as GitLab variables in GitLab Repository.

The values for these environment variables:

Tabella 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 Keylocker Tools'

Nota

The path for Keylockertools is: "C:\Program Files\DigiCert\DigiCert Keylocker 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'

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 Keylocker 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/Keylockertools-windows-x64.msi/download -H "x-api-key:$SM_API_KEY" -o Keylockertools-windows-x64.msi
      msiexec.exe /i Keylockertools-windows-x64.msi /quiet /qn | Wait-Process

      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