Skip to main content

Scripts for signing using PKCS11 library on Jenkins

Get DigiCert​​®​​ Software Trust Manager client tools set up and integrated with Jenkins so that it can be automated into a CI/CD pipeline.

Prerequisites

  • Jenkins build system

  • Any agent with OS that supports Java on Jenkins

  • JDK installed on the agent

  • DigiCert​​®​​ Software Trust Manager access setup (given below)

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

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

注意

You can download client tools from the DigiCert ONE® portal in the 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 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.

    注意

    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.

    注意

    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 Jenkins

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 environment variables that are part of the pipeline as shown in the example below or they can be configured at an OS environment level.

pipeline {
    environment{
       SM_CLIENT_CERT_PASSWORD='**********'
       SM_CLIENT_CERT_FILE='<Path to client authentication certificate file>'
       SM_HOST='<host url>'
       SM_API_KEY='<API token>'
       PKCS11_CONFIG='<Path to pkcs11properties.cfg>'
    }
}

Create keypair and certificate

Create test keypair and certificate

An example for a Jenkins stage to generate an RSA test keypair with certificate:

stage('keypair setup') {
    steps {
        //For Windows
        bat 'smctl keypair generate rsa <Keypair Name> --cert-alias=<Certificate Name> --cert-profile-id=<Certificate Profile ID> --generate-cert=true --key-type=TEST'
        //For Linux
        sh 'smctl keypair generate rsa <Keypair Name> --cert-alias=<Certificate Name> --cert-profile-id=<Certificate Profile ID> --generate-cert=true --key-type=TEST'
    }
 }

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

You can retrieve the certificate profile ID 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 use to generate the certificate.

Create a production keypair with a certificate

An example of a Jenkins stage to generate an RSA production keypair with certificate:

stage('keypair setup') {
   steps {
        //For Windows
        bat 'smctl keypair generate rsa <Keypair Name> --cert-alias=<Certificate Name> --cert-profile-id=<Certificate Profile ID> --generate-cert=true --key-type=PRODUCTION'
        //For Linux
        sh 'smctl keypair generate rsa <Keypair Name> --cert-alias=<Certificate Name> --cert-profile-id=<Certificate Profile ID> --generate-cert=true --key-type=PRODUCTION'
    } 
}

The parameters are the same as the previous case, but ensure you select a certificate profile that has the profile category as "Production."

Create a certificate from an existing keypair

An example of a Jenkins stage to generate a certificate for an existing keypair:

stage('keypair setup') {
    steps {
        //For Windows
        bat 'smctl keypair generate-cert <Keypair ID> --cert-alias=<Certificate Name> --cert-profile-id=<Certificate Profile ID>'
        //For Linux
        sh '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. The Keypair ID can be retrieved in two ways:

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

  • Referring to DigiCert​​®​​ Software Trust Manager on the DigiCert One portal and going 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 right profile category.

Sign

Example for a Jenkins stage for signing a jar:

stage('sign') {
  steps {
      //For Windows
      bat 'jarsigner -keystore NONE -storepass NONE -storetype PKCS11 -sigalg SHA256withRSA -providerClass sun.security.pkcs11.SunPKCS11 -providerArg %PKCS11_CONFIG% -signedjar <Path to Output Signed Jar> <Path to the Jar to be Signed> <Keypair Certificate Name/Alias > -tsa http://timestamp.digicert.com'

      //For Linux
      sh 'jarsigner -keystore NONE -storepass NONE -storetype PKCS11 -sigalg SHA256withRSA -providerClass sun.security.pkcs11.SunPKCS11 -providerArg $PKCS11_CONFIG -signedjar <Path to Output Signed Jar> <Path to the Jar to be Signed> <Keypair Certificate Name/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.

注意

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.

Verify signature

Example of a Jenkins stage that verifies a signed jar:

stage('verify') {
  steps {
      //For Windows
      bat 'jarsigner -verify <Path to Signed Jar>'

      //For Linux
      sh 'jarsigner -verify <Path to Signed Jar>'
  } 
}

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

Sample pipeline

pipeline {
    agent{ label 'windows' }

    tools {
        gradle 'Gradle'
    }

    environment {
        SM_CLIENT_CERT_PASSWORD="gxmiK9Oe72Pn"
        SM_CLIENT_CERT_FILE="C:\\Workspace\\smtools-windows\\local_pkcs12.p12"
        SM_HOST="https://clientauth.one.digicert.com"
        SM_API_KEY="01ff018928e385329550b6e7a7_9b38fc5fbb4ef99ceeb7d61e6984107efa4283583cb7a64f5e292582cd3ed848"
        PKCS11_CONFIG="C:\\Workspace\\smtools-windows\\pkcs11properties.cfg"
        KEYPAIR_CERT_NAME="jenkins_test_rsa_2048_${env.BUILD_NUMBER}"
    }

    stages {
        stage('checkout') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/main']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'Siddharth-Srinivas', url: 'https://github.com/Siddharth-Srinivas/jenkins-test-repo.git']]])
            }
        }
        stage('keypair setup') {
            steps {
                bat "smctl keypair generate rsa %KEYPAIR_CERT_NAME% --cert-alias=%KEYPAIR_CERT_NAME% --cert-profile-id=e3a85eab-8621-473f-99b8-3154ee803e40 --generate-cert=true --key-type=TEST"
            }
        }
        stage('clean') {
            steps {
                bat "./gradlew clean"
            }
        }
        stage('test') {
            steps {
                withGradle {
                    bat "./gradlew test"
                }
            }
        
            post {
                success {
                    junit "**/build/test-results/test/TEST-*.xml"
                }
            }
        }
        stage('build') {
            steps {
                withGradle {
                    bat "./gradlew build"
                }
            }
        }
        stage('sign') {
            steps {
                bat "jarsigner -keystore NONE -storepass NONE -storetype PKCS11 -sigalg SHA256withRSA -providerClass sun.security.pkcs11.SunPKCS11 -providerArg %PKCS11_CONFIG% -signedjar app/build/libs/signed_app.jar app/build/libs/app.jar %KEYPAIR_CERT_NAME% -tsa http://timestamp.digicert.com"
            }
        
            post {
                success {
                    archiveArtifacts "app/build/libs/signed_app.jar"
                }
            }
        }
        stage('verify') {
            steps {
                bat "jarsigner -verify app/build/libs/signed_app.jar"
            }
        }
    }
}