Skip to main content

Scripts for signing using PKCS11 library on Ant

Prerequisites

  • Ant installed on the agent

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

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

The following commands require your certificate profile ID and keypair alias or keypair ID.

Create test keypair and certificate

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

<property name="smctl" value="<path to smctl.exe>" />
<target name="createkeyTest" description="create test keypair using smctl">
<exec executable="${smctl}">
<arg line="keypair generate rsa <keypair alias> --cert-alias=<certificate alias> --cert-profile-id=<certificate profile ID> --generate-cert=true --key-type=TEST" />
</exec>    
</target>

Create production keypair with certificate

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

<property name="smctl" value="<path to smctl.exe>" />
<target name="createkeyProd" description="create production keypair using smctl">
<exec executable="${smctl}">
<arg line=" keypair generate rsa <keypair alias> --cert-alias=<certificate alias> --cert-profile-id=<certificate profile ID> --generate-cert=true --key-type=PRODUCTION" />
</exec>
</target>

Sugerencia

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

Create certificate from existing keypair

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

<property name="smctl" value="="<path to smctl.exe>" " />
<target name="createcertforkey" description="create certificate for existing keypair using smctl">
<exec executable="${smctl}">
<arg line="keypair generate-cert <keypair ID> --cert-alias=<certificate alias> --cert-profile-id=<certificate profile ID> --set-as-default-cert-cert=true --key-type=PRODUCTION" />
</exec>
</target>

Sugerencia

Depending on whether you want to create a test or production certificate, select a certificate profile that belongs to the correct profile category.

Sign

To integrate DigiCert​​®​​ Software Trust Manager PKCS11 with Ant, add to the build.xml file:

<project name="pkcs11ant" default="dist" basedir=".">
<description>
sample application
</description>
<!-- set global properties for this build -->
<property name="name" value="pkcs11ant"/>
<property name="src" location="src/main/java" />
<property name="build" location="ant-build" />
<property name="dist" location="${build}" />
<property name="version" value="1.0" />
<property environment="env"/>
<property name="jarfilename" value="${name}-${version}.jar" />
<property name="jarsigner" value="${env.JAVA_HOME}/bin/jarsigner.exe" />
<target name="init">
<!-- Create the time stamp -->
<tstamp />
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}" />
</target>
<target name="compile" depends="init" description="compile the source">
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}" />
</target>
<target name="dist" depends="compile" description="generate the distribution">
<buildnumber />
<!-- Create the distribution directory -->
<mkdir dir="${dist}/lib" />
<!-- Put everything in ${build} into the MyApplication-${version}.${build.number}.jar -->
<jar destfile="${dist}/lib/${jarfilename}" basedir="${build}" />
</target>
<target name="sign" depends="dist" description="sign the jar using jarsigner">
<exec executable="${jarsigner}">
<arg line="-providerArg = <path of smpkcs11.config> -keystore NONE -storetype PKCS11 -storepass NONE  -providerClass sun.security.pkcs11.SunPKCS11 ${dist}/lib/${jarfilename} <keyPairAlias>,${jarfilename}" />
</exec>
</target>
<target name="clean" description="clean up">
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}" />
<delete dir="${dist}" />
</target>
</project>

Verify signature

An example of an Ant setup that verifies a signed jar:

<property name="jarsigner" value="${env.JAVA_HOME}/bin/jarsigner.exe" />    
target name="dist" depends="compile" description="generate the distribution">
<buildnumber />
<!-- Create the distribution directory -->
<mkdir dir="${dist}/lib" />
<!-- Put everything in ${build} into the MyApplication-${version}.${build.number}.jar -->
<jar destfile="${dist}/lib/${jarfilename}" basedir="${build}" />
</target>

<target name="verify" depends="dist" description="verify the jar using jarsigner">
<exec executable="${jarsigner}">
<arg line="-verify ${dist}/lib/${jarfilename}" />
</exec>
</target>

Sugerencia

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