Skip to main content

Scripts for signing using PKCS11 library on Gradle

Prerequisites

  • Gradle installed on the agent

  • JDK installed on the agent

  • access setup

  • client for the specific OS (client tools)

  • PKSC11 library (client tools)

Note

Client tools can be downloaded from DigiCert One portal at  > Resources >Client tool repository.

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.

Secure your credentials

Your DigiCert ONE host environment, API key, client authentication certificate, and password make up your environment variables and are required to access KeyLocker client tools. Use one of the following methods to securely store your credentials based on your operating system.

Sign

To integrate PKCS11 with Gradle implementation, add the code snippet to the build.gradle file:

plugins {
    // Apply the java-library plugin for API and implementation separation.
    id 'java'
}

task sign(type: Exec, dependsOn: 'jar', description: 'jar signing using DigiCert Signing Manager pkcs11', group: 'Build') {
  def storePassword = "NONE"
  def keyStore = "NONE"
  def storeType = "PKCS11"
  def providerClass = "sun.security.pkcs11.SunPKCS11"
  def providerArg = "<Path to smpkcs11.config>"
  def alias = "<keypair alias>"
  def signedjarfile = "<Path of signed file>"
  def unsignedjarfile=""<Path of unsigned file>""
  commandLine "${System.env.JAVA_HOME}/bin/jarsigner.exe", "-keystore", keyStore, "-storepass", storePassword, "-storetype", storeType,  "-providerClass", providerClass, "-providerArg", providerArg, "-signedjar", signedjarfile, "-unsignedjarfile", unsignedjarfile, alias
}

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

Verify signature

An example of a Gradle setup that verifies a signed jar:

plugins {
    // Apply the java-library plugin for API and implementation separation.
    id 'java'
}

task verify(type: Exec, dependsOn: 'jar', description: 'jar verification using DigiCert Signing Manager pkcs11', group: 'Build') {
  def signedjarfile = ""<Path of signed file>""
  commandLine "${System.env.JAVA_HOME}/bin/jarsigner.exe", "-verify", signedjarfile
}

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