Skip to main content

Scripts for signing using PKCS11 library on Gradle

Prerequisites

  • Gradle installed on the agent

  • JDK installed on the agent

  • DigiCert​​®​​ Software Trust Manager access setup

  • DigiCert​​®​​ Software Trust Manager client for the specific OS (client tools)

  • DigiCert​​®​​ Software Trust Manager PKSC11 library (client tools)

Note

Client tools can be downloaded from DigiCert One portal at DigiCert​​®​​ Software Trust Manager > Resources >Client tool repository.

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 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​​®​​ 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.

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 Software Trust Manager client tools. Use one of the methods provided below to securely store your credentials based on your operating system.

Create keypair and certificate

Create test keypair and certificate

An example for a Gradle setup to generate an RSA test keypair with certificate:

task createKeypair(type: Exec, description: 'create test keypair using DigiCert Signing Manager pkcs11', group: 'Build') {
  def keyType = "TEST"
  def certAlias = "<CertAlias>"
  def operation = "generate"
  def keyPair = "keypair"
  def keyAlg = "<keyAlg>"
  def generateCert = "true"
  def certProfileId="<CertProfileId>"
  def alias = "<KeyPairAlias>"
  commandLine "C:/smtools/smctl.exe", keyPair, operation, keyAlg, "--cert-alias", certAlias, "--cert-profile-id", certProfileId, "--generate-cert", generateCert, "--key-type", keyType
}

The keypair name and certificate name are must be unique inputs, meaning that they cannot exist on the portal already.

The certificate profile ID to be used can be retrieved from the DigiCert One portal at DigiCert​​®​​ Software Trust Manager > Certificates > Certificate profiles. Select a profile (Profile Category should be Test) that you want to generate the certificate with, and you should see the profile ID.

Create production keypair with certificate

An example of a Gradle setup to generate an RSA production keypair with certificate:

task createKeypair(type: Exec, description: 'create production keypair using DigiCert Signing Manager pkcs11', group: 'Build') {
  def keyType = "PRODUCTION"
  def certAlias = "<certAlias>"
  def operation = "generate"
  def keyPair = "keypair"
  def keyAlg = "<keyAlg>"
  def generateCert = "true"
  def certProfileId="<certProfileId>"
  def alias = "<leyPairAlias>"
  commandLine "C:/smtools/smctl.exe", keyPair, operation, keyAlg, "--cert-alias", certAlias, "--cert-profile-id", certProfileId, "--generate-cert", generateCert, "--key-type", keyType
}

The parameters are the same as the previous case, but care should be taken to select a certificate profile that has the profile category as Production.

Create a certificate from an existing keypair

An example of a Gradle setup to generate a certificate for an existing keypair:

task createCertForKeypair(type: Exec, description: 'create certificate for existing keypair using DigiCert Signing Manager pkcs11', group: 'Build') {
  def keyType = "PRODUCTION"
  def certAlias = "<certAlias>"
  def operation = "generate-cert"
  def keyPair = "keypair"
  def keyPairID = "<keyPairId>"
  def certProfileId="<certProfileId>"
  def setAsDefault = "true"
  commandLine "C:/smtools/smctl.exe", keyPair, operation, keyPairID, "--cert-alias", certAlias, "--cert-profile-id", certProfileId, "--set-as-default-cert", setAsDefault, "--key-type", keyType
}

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.

  • Refer to DigiCert​​®​​ Software Trust Manager on the DigiCert One portal and go to DigiCert​​®​​ Software Trust Manager > Keypairs and select a keypair.

Also, as in the previous use cases, the certificate profile ID used should belong to the correct profile category.

Sign

To integrate DigiCert​​®​​ Software Trust Manager 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.