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 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​​®​​ Software Trust Managerのセットアップ

システム変数として設定することで、環境変数が永続的に残るようにしてください。

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