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 as a package.

Download client tools

  1. In the KeyLocker menu, go to Resources > Client tool repository.

  2. Select your operating system, and then select the corresponding download (download_icon.png) icon next to the desired client.

Create PKCS11 configuration file

To create a configuration file with the path to the 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

KeyLocker enforces multi-factor authentication for security purposes. To access keypairs, certificates, and sign code, you need to set up two types of credentials: An API key and an authentication certificate.

Create an API key

The API key is an authentication method used to verify you as a user and your permissions assigned in DigiCert ONE. The API key provides the first factor authentication.

  1. In DigiCert ONE, select the profile (profile_icon.png) icon, and then select Admin Profile.

  2. Under API keys, select Create API key.

  3. For Name, enter a descriptive name for the key.

  4. For End date (optional), enter the date when the key should expire.

  5. Select Create. The API key appears this one time and can't be accessed again. Securely store the API key for future use.

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.

  1. In DigiCert ONE, select the profile (profile_icon.png) icon, and then select Admin Profile.

  2. Under Client authentication certificates, select Create client authentication certificate.

  3. For Nickname, enter a descriptive name for the key.

  4. For End date, enter the date when the certificate should expire.

  5. Select the desired Encryption and Signature hash algorithm.

  6. Select Generate certificate. The password appears this one time and can't be accessed again. Download the certificate and securely store the password for future use.

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 PKCS#11 configuration file.

SM_TLS_SKIP_VERIFY

Enter true to disable or false to enable TLS verification on the 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:

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 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:  
      - saas-windows-medium-amd64
    
      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:  
  - saas-windows-medium-amd64

  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:  
  - saas-windows-medium-amd64

  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:  
  - saas-windows-medium-amd64

  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