CircleCI script integration with PKCS11
Set up DigiCert® KeyLocker client tools and integrate them with CircleCI for automation into a CI/CD pipeline.
Prerequisites
CircleCI build system
Any operating system that supports Java on CircleCI
JDK installed on the agent
DigiCert® KeyLocker credentials
DigiCert® KeyLocker client tools
Client tools
DigiCert® KeyLocker clients can be downloaded in a package.
Download client tools
Sign in to DigiCert ONE.
Navigate to DigiCert® KeyLocker > Resources > Client tool repository.
Select your operating system.
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:
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
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:
Sign in to DigiCert ONE.
Select the profile icon (top-right).
Select Admin Profile.
Scroll down to API Tokens.
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:
Sign in to DigiCert ONE.
Select the profile icon (top-right).
Select Admin Profile.
Scroll down to Authentication certificates.
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® 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. |
SM_TLS_SKIP_VERIFY | Enter true to disable or false to enable TLS verification on client side. |
Integration with CircleCI
Environment variables setup for CircleCI
The client tools need these environment variables to connect with DigiCert® KeyLocker to provide its service. They can be integrated as CircleCI Environment Variables in CircleCI project.
To set environment variables in CircleCI, go to Projects > Project Settings > Environment Variables.
Set certificate file in environment variables:
Identify your downloaded client authentication certificate.
Encode the file to base64, using the command:
base64 file_name
Go to CircleCI, go to Projects > Project Settings > Environment Variables.
Select Add Environment Variable and add the new variable name as "SM_CLIENT_CERT_FILE_B64" and the encoded value from base64 file_name in the Value field.
Run this code to setup the certificate file for signing:
steps: - checkout - run: name: "Certificate-Setup" command: | cd C:\ New-Item C:\CERT_FILE.p12.b64 Set-Content -Path C:\CERT_FILE.p12.b64 -Value $env:SM_CLIENT_CERT_FILE_B64 certutil -decode CERT_FILE.p12.b64 Certificate_pkcs12.p12 cat Certificate_pkcs12.p12 environment: SM_CLIENT_CERT_FILE: 'C:\Certificate_pkcs12.p12'
Sign
To sign with Jarsigner:
- run: name: "Signing using Jarsigner" command: | jarsigner -keystore NONE -storepass NONE -storetype PKCS11 -sigalg SHA256withRSA -providerClass sun.security.pkcs11.SunPKCS11 -providerArg %PKCS11_CONFIG% -signedjar <path to output the signed .jar> <path to the Jar to be signed> <certificate alias> -tsa http://timestamp.digicert.com
Verify signature
To verify a signature with Jarsigner:
- run: name: "Jarsigner verify" command: | jarsigner -verify <Path to Signed Jar>
The only input for this command is the path to the signed.jar file to be verified.
Sample pipeline
version: 2.1
orbs:
win: circleci/windows@4.1
jobs:
SSM_Signing:
executor: win/server-2022
steps:
- checkout
- run:
name: "Certificate-Setup"
command: |
cd C:\
New-Item C:\CERT_FILE.p12.b64
Set-Content -Path C:\CERT_FILE.p12.b64 -Value $env:SM_CLIENT_CERT_FILE_B64
certutil -decode CERT_FILE.p12.b64 Certificate_pkcs12.p12
- run:
name: "Setting-up-the-client-tools"
command: |
cd C:\
curl.exe -X GET https://one.digicert.com/signingmanager/api-ui/v1/releases/smtools-windows-x64.msi/download -H "x-api-key:$env:SM_API_KEY" -o smtools-windows-x64.msi
msiexec.exe /i smtools-windows-x64.msi /quiet /qn | Wait-Process
- run:
name: "Signing using Jarsigner"
command: |
Copy-Item -Path C:\Users\circleci.PACKER-625580E2\project\UNSIGNED.jar -Destination C:\
jarsigner -keystore NONE -storepass NONE -storetype PKCS11 -sigalg SHA256withRSA -providerClass sun.security.pkcs11.SunPKCS11 -providerArg C:\Users\circleci.PACKER-625580E2\project\pkcs11properties.cfg -signedjar C:\SIGNED.jar C:\UNSIGNED.jar $env:Keypair -tsa http://timestamp.digicert.com
- run:
name: "Jarsigner verify"
command: |
jarsigner -verify C:\SIGNED.jar
environment:
SM_CLIENT_CERT_FILE: 'C:\Certificate_pkcs12.p12'
Keypair: 'keypair_circle_test'
Smctl: 'C:\Program Files\DigiCert\DigiCert Keylocker Tools\smctl.exe'
workflows:
my-workflow:
jobs: