Skip to main content

Jenkins script integration with PKCS11

Get 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

  • access setup (given below)

  • client for the specific OS (client tools)

  • PKCS11 library (client tools)

Anmerkung

You can download client tools from the DigiCert ONE® portal in the  > Resources > Client tool repository

Client tools

DigiCert​​®​​ KeyLocker clients can be downloaded in a package.

Download Client tools

  1. Sign in to DigiCert ONE.

  2. Navigate to DigiCert​​®​​ KeyLocker > Resources > Client tool repository.

  3. Select your operating system.

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

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

    Anmerkung

    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.

    Anmerkung

    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​​®​​ 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 PKCS11 configuration file.

Integration with Jenkins

Environment variables setup for pipeline

The client tools need these environment variables to connect with DigiCert​​®​​ KeyLocker 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>'
    }
}

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.

Anmerkung

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('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"
            }
        }
    }
}