Scripts for signing using PKCS11 library on Jenkins
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 as a package.
Download client tools
In the KeyLocker menu, go to Resources > Client tool repository.
Select your operating system, and then select the corresponding download (
) icon next to the desired client.
Create PKCS11 configuration file
To create a configuration file with the path to the shared library:
Open an integrated development environment (IDE) or plain text editor.
Copy and paste the following text into the editor:
Save the file as pkcs11properties.cfg.
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.
In DigiCert ONE, select the profile (
) icon, and then select Admin Profile.Under API keys, select Create API key.
For Name, enter a descriptive name for the key.
For End date (optional), enter the date when the key should expire.
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.
In DigiCert ONE, select the profile (
) icon, and then select Admin Profile.Under Client authentication certificates, select Create client authentication certificate.
For Nickname, enter a descriptive name for the key.
For End date, enter the date when the certificate should expire.
Select the desired Encryption and Signature hash algorithm.
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. |
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"
}
}
}
}