Skip to main content

CircleCI script integration with PKCS11

Set up DigiCert​​®​​ KeyLocker client tools and integrate them with CircleCI for automation into a CI/CD pipeline.

Prerequisites

  • CircleCI build system

  • Any operating system that supports Java on CircleCI

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

    Opmerking

    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.

    Opmerking

    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 CircleCI

Environment variables setup for CircleCI

The client tools need these environment variables to connect with DigiCert​​®​​ KeyLocker to provide its service. They can be integrated as CircleCI Environment Variables in CircleCI project.

To set environment variables in CircleCI, go to Projects > Project Settings > Environment Variables.

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. Go to CircleCI, go to Projects > Project Settings > Environment Variables.

  4. Select Add Environment Variable and 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:

    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
       
    environment:
      SM_CLIENT_CERT_FILE: 'C:\Certificate_pkcs12.p12'

Sign

To sign with Jarsigner:

- run:
    name: "Signing using Jarsigner"
    command: |
        jarsigner -keystore NONE -storepass NONE -storetype PKCS11 -sigalg SHA256withRSA -providerClass sun.security.pkcs11.SunPKCS11 -providerArg %PKCS11_CONFIG% -signedjar <path to output the signed .jar> <path to the Jar to be signed> <certificate alias> -tsa http://timestamp.digicert.com

Verify signature

To verify a signature with Jarsigner:

- run:
    name: "Jarsigner verify"
    command: |
        jarsigner -verify <Path to Signed Jar>

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

Sample pipeline

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
    
      - run:
          name: "Setting-up-the-client-tools"
          command: |
            cd C:\
            curl.exe -X GET  https://one.digicert.com/signingmanager/api-ui/v1/releases/smtools-windows-x64.msi/download -H "x-api-key:$env:SM_API_KEY" -o smtools-windows-x64.msi
            msiexec.exe /i smtools-windows-x64.msi /quiet /qn | Wait-Process

      - run:
          name: "Signing using Jarsigner"
          command: |
            Copy-Item -Path C:\Users\circleci.PACKER-625580E2\project\UNSIGNED.jar -Destination C:\
            jarsigner -keystore NONE -storepass NONE -storetype PKCS11 -sigalg SHA256withRSA -providerClass sun.security.pkcs11.SunPKCS11 -providerArg C:\Users\circleci.PACKER-625580E2\project\pkcs11properties.cfg -signedjar C:\SIGNED.jar C:\UNSIGNED.jar $env:Keypair -tsa http://timestamp.digicert.com

      - run:
          name: "Jarsigner verify"
          command: |
            jarsigner -verify C:\SIGNED.jar

    environment:
      SM_CLIENT_CERT_FILE: 'C:\Certificate_pkcs12.p12'
      Keypair: 'keypair_circle_test'
      Smctl: 'C:\Program Files\DigiCert\DigiCert Keylocker Tools\smctl.exe'

workflows:
  my-workflow:
    jobs: