Skip to main content

Scripts for signing using PKCS11 library on Azure pipeline

Prerequisites

  • Azure DevOps

  • Any custom agent (self-hosted) with an operating system that supports Java on Azure DevOps

  • 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 Azure DevOps pipeline

Environment variables setup for pipeline

The client tools need these environment variables to connect with DigiCert​​®​​ KeyLocker to provide its service. They can be integrated as variables in the pipeline which Azure will automatically inject as environment variables in the agent or they can be configured at an operating system environment level.

variables:
- name: SM_CLIENT_CERT_PASSWORD
 value: <client authentication certificate password>
- name: SM_CLIENT_CERT_FILE
  value: <Path to client authentication certificate file>
- name: SM_HOST
  value: <host url>
- name: SM_API_KEY
  value: <API Token>
- name: PKCS11_CONFIG
  value: <Path to pkcs11properties.cfg>

Sign

An example for an Azure DevOps pipeline step for signing a jar is:

- task: CmdLine@2
  displayName: 'Sign Jar'
  inputs:
    script: 'jarsigner -keystore NONE -storepass NONE -storetype PKCS11 -sigalg SHA256withRSA -providerClass sun.security.pkcs11.SunPKCS11 -providerArg $PKCS11_CONFIG -signedJar <Output path for the signed jar> <Input path for jar to be signed> <certificate alias> -tsa http://timestamp.digicert.com'

The input parameters for this example are the path where the signed jar needs to be output, the path to the jar that needs to be signed and the name/alias of the certificate that needs to be used for signing.

Note

The PKCS11 configuration file was defined in the environment of the pipeline. If it was not defined, the path to the configuration file can be used instead.

Verification

An example of an Azure DevOps step that verifies a signed jar:

- task: CmdLine@2
  displayName: 'Verify Signed Jar'
  inputs:
    script: 'jarsigner -verify <path to the signed jar>'

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

Sample pipeline

trigger:
- main

pool:
  name: 'default'
  demands: agent.os -equals Linux

variables:
  - name: SM_CLIENT_CERT_PASSWORD
    value: gxmiK9Oe72Pn
  - name: SM_CLIENT_CERT_FILE
    value: "/home/john.doe/smtools/local_pkcs12.p12"
  - name: SM_HOST
    value: https://clientauth.one.digicert.com
  - name: SM_API_KEY
    value: 01ff018928e385329550b6e7a1_9b38fc5fbb4ef99ceeb7d61e6984107efa4283583cb7a64f5e292582cd3ed848
  - name: PKCS11_CONFIG
    value: "/home/john.doe/smtools/pkcs11properties.cfg"
  - name: KEYPAIR_NAME
    value: "azure-keypair-$(Build.BuildId)"

steps:

- task: Gradle@2
  displayName: 'Build'
  inputs:
    workingDirectory: ''
    gradleWrapperFile: 'gradlew'
    gradleOptions: '-Xmx3072m'
    javaHomeOption: 'path'
    jdkDirectory: '/usr/lib/jvm/java-11-openjdk-amd64'
    jdkArchitectureOption: 'x64'
    publishJUnitResults: true
    testResultsFiles: '**/TEST-*.xml'
    tasks: 'build'

- task: CmdLine@2
  displayName: 'Sign Jar'
  inputs:
    script: 'jarsigner -keystore NONE -storepass NONE -storetype PKCS11 -sigalg SHA256withRSA -providerClass sun.security.pkcs11.SunPKCS11 -providerArg $PKCS11_CONFIG -signedJar $(System.DefaultWorkingDirectory)/app/build/libs/signed.jar $(System.DefaultWorkingDirectory)/app/build/libs/app.jar $KEYPAIR_NAME -tsa http://timestamp.digicert.com'

- task: PublishBuildArtifacts@1
  displayName: 'Publish Unsigned Jar'
  inputs:
    PathtoPublish: '$(System.DefaultWorkingDirectory)/app/build/libs/app.jar'
    ArtifactName: 'Unsigned Jar'
    publishLocation: 'Container'

- task: PublishBuildArtifacts@1
  displayName: 'Publish Signed Jar'
  inputs:
    PathtoPublish: '$(System.DefaultWorkingDirectory)/app/build/libs/signed.jar'
    ArtifactName: 'Signed Jar'
    publishLocation: 'Container'

- task: CmdLine@2
  displayName: 'Verify Signed Jar'
  inputs:
    script: 'jarsigner -verify $(System.DefaultWorkingDirectory)/app/build/libs/signed.jar'