Skip to main content

Scripts for signing using PKCS11 library on GitHub

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

Prerequisites

  • GitHub Action build system

  • Any operating system that supports Java on GitHub Actions

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

    Note

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

    Note

    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 GitHub Action

Set certificate file in environment variables

  1. Identify your downloaded client authentication certificate.

  2. Encode the file to base64. For encoding run command:

    base64 file_name
  3. In GitHub Secrets add a new repository secret with: Name = SM_CLIENT_CERT_FILE_B64 Value = encoded value from base64 file_name

  4. Setup certificate file for signing using this command:

    - name: Setup Certificate
      run: |
        echo "${{ secrets.SM_CLIENT_CERT_FILE_B64 }}" | base64 --decode > /d/Certificate_pkcs12.p12
      shell: bash

Environment variables setup for GitHub Actions

The client tools need these environment variables to connect with DigiCert​​®​​ KeyLocker. To integrate environment variables as GitHub secrets:

  1. Access GitHub repository

  2. Select Settings

  3. Select Secrets > Actions

  4. Select New repository secret

The values for these environment variables:

Tableau 1. Environment variables

Environment variables

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.

Note

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:

- name: Set variables
  id: variables
  run: |
    echo "::set-output name=version::${GITHUB_REF#refs/tags/v}"
    echo "::set-output name=KEYPAIR_NAME::github-action-keypair"
    echo "SM_HOST=${{ secrets.SM_HOST }}" >> "$GITHUB_ENV"
    echo "SM_API_KEY=${{ secrets.SM_API_KEY }}" >> "$GITHUB_ENV"
    echo "SM_CLIENT_CERT_FILE=D:\\Certificate_pkcs12.p12" >> "$GITHUB_ENV"
    echo "SM_CLIENT_CERT_PASSWORD=${{ secrets.SM_CLIENT_CERT_PASSWORD }}" >> "$GITHUB_ENV"
    echo "C:\Program Files\DigiCert\DigiCert Keylocker Tools" >> $GITHUB_PATH
  shell: bash

Note

The path for smtools is: "C:\Program Files\DigiCert\DigiCert Keylocker Tools".

Sign

Sign with Jarsigner:

- name: Signing using jarsigner
  run: |
    jarsigner -keystore NONE -storepass NONE -storetype PKCS11 -sigalg SHA256withRSA -providerClass sun.security.pkcs11.SunPKCS11 -providerArg pkcs11properties.cfg -signedjar <Path to Output Signed Jar> <Path to the Jar to be Signed> <Keypair Certificate Name/Alias > -tsa http://timestamp.digicert.com
 shell: cmd

The input parameters for this example are the output path for the signed .jar file, the path to the .jar file to be signed, and the name or alias of the certificate to be used for signing.

Verify signature

Verify with Jarsigner:

- name: Jarsigner verify
  run: |
    jarsigner -verify <Path to Signed .jar>
  shell: cmd

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

Sample pipeline

name: 'build and release electron installer'
on:
  push:
    tags:
      - 'v*'
      
jobs:
  release:
    runs-on: ${{ matrix.os }}
    
    strategy:
      matrix:
        os: [windows-latest]
        
    steps:
      - name: Check out Git repository
        uses: actions/checkout@v2

      - name: Install Node.js, NPM and Yarn
        uses: actions/setup-node@v2
        with: 
          node-version: 14
  
      - name: Setup Certificate
        run: |
          echo "${{ secrets.SM_CLIENT_CERT_FILE_B64 }}" | base64 --decode > /d/Certificate_pkcs12.p12
          cat /d/Certificate_pkcs12.p12
        shell: bash

          
      - name: Set variables
        id: variables
        run: |
          echo "::set-output name=version::${GITHUB_REF#refs/tags/v}"
          echo "::set-output name=KEYPAIR_NAME::github-action-keypair-test3"
          echo "SM_HOST=${{ secrets.SM_HOST }}" >> "$GITHUB_ENV"
          echo "SM_API_KEY=${{ secrets.SM_API_KEY }}" >> "$GITHUB_ENV"
          echo "SM_CLIENT_CERT_FILE=D:\\Certificate_pkcs12.p12" >> "$GITHUB_ENV"
          echo "SM_CLIENT_CERT_PASSWORD=${{ secrets.SM_CLIENT_CERT_PASSWORD }}" >> "$GITHUB_ENV"
          echo "C:\Program Files (x86)\Windows Kits\10\App Certification Kit" >> $GITHUB_PATH
          echo "C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools" >> $GITHUB_PATH
          echo "C:\Program Files\DigiCert\DigiCert Keylocker Tools" >> $GITHUB_PATH
        shell: bash

      - name: Setting up the client tools
        run: |
          curl -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 /i Keylockertools-windows-x64.msi /quiet /qn
        shell: cmd

      - name: create folder
        run: |
          mkdir D:\a\github-action-electron\github-action-electron\dist
          copy D:\a\github-action-electron\github-action-electron\UNSIGNED.jar D:\a\github-action-electron\github-action-electron\dist\UNSIGNED.jar

      - name: Signing using Jarsigner
        run: |
          jarsigner -keystore NONE -storepass NONE -storetype PKCS11 -sigalg SHA256withRSA -providerClass sun.security.pkcs11.SunPKCS11 -providerArg D:\a\github-action-electron\github-action-electron\pkcs11properties.cfg -signedjar D:\a\github-action-electron\github-action-electron\dist\SIGNED.jar D:\a\github-action-electron\github-action-electron\dist\UNSIGNED.jar ${{ steps.variables.outputs.KEYPAIR_NAME }} -tsa http://timestamp.digicert.com

      - name: Jarsigner verify
        run: |
          jarsigner -verify D:\a\github-action-electron\github-action-electron\dist\SIGNED.jar

      - name: Upload artifacts
        uses: actions/upload-artifact@v2
        with:
          name: ${{ matrix.os }}
          path: |
            dist 

PKCS11 file content

name=signingmanager
library=D:\a\github-action-electron\github-action-electron\smpkcs11.dll
slotListIndex=0