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​​®​​ Software Trust Manager credentials

  • DigiCert​​®​​ Software Trust Manager client tools

Client tools

DigiCert​​®​​ Software Trust Manager clients can be downloaded in a package.

Download client tools

  1. Sign in to DigiCert ONE.

  2. Navigate to DigiCert​​®​​ Software Trust Manager > Resources > Client tool repository.

  3. Select your operating system.

  4. Click the download icon next to DigiCert​​®​​ Software Trust Manager 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

Software Trust Manager 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.

    Nota

    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.

    Nota

    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​​®​​ Software Trust Manager 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 Azure DevOps pipeline

Environment variables setup for pipeline

The client tools need these environment variables to connect with DigiCert​​®​​ Software Trust Manager 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>

Certificates and keypairs

The following scripts can be used to generate keypairs and certificates.

Create test keypair and certificate

An example for an Azure pipeline step to generate an RSA test keypair with certificate:

- task: CmdLine@2
  displayName: 'Generate Test Keypair'
  inputs:
    script: 'smctl keypair generate rsa <Keypair Name> --cert-alias=<Certificate Name> --cert-profile-id=<Certificate Profile ID> --generate-cert=true --key-type=TEST'

Nota

The keypair and certificate alias are must be unique inputs, meaning that they cannot already exist in Software Trust Manager.

To retrieve the certificate profile ID:

  1. Sign in to DigiCert ONE.

  2. Navigate to: DigiCert​​®​​ Software Trust Manager > Certificates > Certificate profiles.

  3. Click on the certificate profile alias that you want to generate the certificate with.

    Nota

    The profile category should be Test.

  4. Identify the Certificate profile ID field.

Create production keypair with certificate

An example of an Azure pipeline step to generate an RSA production keypair with certificate:

- task: CmdLine@2
  displayName: 'Generate Production Keypair'
  inputs:
    script: 'smctl keypair generate rsa <Keypair Name> --cert-alias=<Certificate Name> --cert-profile-id=<Certificate Profile ID> --generate-cert=true --key-type=PRODUCTION'

Nota

Select a certificate profile that has the profile category as Production.

Create certificate from existing keypair

An example of an Azure DevOps pipeline step to generate a certificate for an existing keypair:

- task: CmdLine@2
  displayName: 'Generate Certificate for Existing Keypair'
  inputs:
    script: 'smctl keypair generate-cert <Keypair ID> --cert-alias=<Certificate Name> --cert-profile-id=<Certificate Profile ID>'

For this step, the inputs required are a Keypair ID, certificate name (which should be unique) and certificate profile ID.

You can retrieve the keypair ID in two ways:

  • Run a smctl keypair ls command on the command line with all environment variables set up.

  • Navigate to: DigiCert ONE > DigiCert​​®​​ Software Trust Manager > Keypairs and click on the keypair alias to view keypair details.

Nota

The certificate profile ID used should belong to the correct profile category.

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.

Nota

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: CmdLine@2
  displayName: 'Generate Keypair'
  inputs:
    script: '/home/siddharth/smtools/smctl keypair generate rsa $KEYPAIR_NAME --cert-alias=$KEYPAIR_NAME --cert-profile-id=16b073d1-8de8-4ea2-9f5e-88ba6e79063a --generate-cert=true --key-type=PRODUCTION'

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