Scripts for signing using PKCS11 library on Azure pipeline
Prerequisites
Azure DevOps
Any custom agent (self-hosted) with an operating system that supports Java on Azure DevOps
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.
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:
Sign in to DigiCert ONE.
Select the profile icon (top-right).
Select Admin Profile.
Scroll down to Authentication certificates.
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® 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 Azure DevOps pipeline
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 variables in the pipeline which Azure will automatically inject as environment variables in the agent or they can be configured at an operating system environment level.
variables: - name: SM_CLIENT_CERT_PASSWORD value: <client authentication certificate password> - name: SM_CLIENT_CERT_FILE value: <Path to client authentication certificate file> - name: SM_HOST value: <host url> - name: SM_API_KEY value: <API Token> - name: PKCS11_CONFIG value: <Path to pkcs11properties.cfg>
Sign
An example for an Azure DevOps pipeline step for signing a jar is:
- task: CmdLine@2 displayName: 'Sign Jar' inputs: script: 'jarsigner -keystore NONE -storepass NONE -storetype PKCS11 -sigalg SHA256withRSA -providerClass sun.security.pkcs11.SunPKCS11 -providerArg $PKCS11_CONFIG -signedJar <Output path for the signed jar> <Input path for jar to be signed> <certificate 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.
Nota
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.
Verification
An example of an Azure DevOps step that verifies a signed jar:
- task: CmdLine@2 displayName: 'Verify Signed Jar' inputs: script: 'jarsigner -verify <path to the signed jar>'
The only input for this step is the path to the signed jar that needs to be verified.
Sample pipeline
trigger: - main pool: name: 'default' demands: agent.os -equals Linux variables: - name: SM_CLIENT_CERT_PASSWORD value: gxmiK9Oe72Pn - name: SM_CLIENT_CERT_FILE value: "/home/john.doe/smtools/local_pkcs12.p12" - name: SM_HOST value: https://clientauth.one.digicert.com - name: SM_API_KEY value: 01ff018928e385329550b6e7a1_9b38fc5fbb4ef99ceeb7d61e6984107efa4283583cb7a64f5e292582cd3ed848 - name: PKCS11_CONFIG value: "/home/john.doe/smtools/pkcs11properties.cfg" - name: KEYPAIR_NAME value: "azure-keypair-$(Build.BuildId)" steps: - task: Gradle@2 displayName: 'Build' inputs: workingDirectory: '' gradleWrapperFile: 'gradlew' gradleOptions: '-Xmx3072m' javaHomeOption: 'path' jdkDirectory: '/usr/lib/jvm/java-11-openjdk-amd64' jdkArchitectureOption: 'x64' publishJUnitResults: true testResultsFiles: '**/TEST-*.xml' tasks: 'build' - task: CmdLine@2 displayName: 'Sign Jar' inputs: script: 'jarsigner -keystore NONE -storepass NONE -storetype PKCS11 -sigalg SHA256withRSA -providerClass sun.security.pkcs11.SunPKCS11 -providerArg $PKCS11_CONFIG -signedJar $(System.DefaultWorkingDirectory)/app/build/libs/signed.jar $(System.DefaultWorkingDirectory)/app/build/libs/app.jar $KEYPAIR_NAME -tsa http://timestamp.digicert.com' - task: PublishBuildArtifacts@1 displayName: 'Publish Unsigned Jar' inputs: PathtoPublish: '$(System.DefaultWorkingDirectory)/app/build/libs/app.jar' ArtifactName: 'Unsigned Jar' publishLocation: 'Container' - task: PublishBuildArtifacts@1 displayName: 'Publish Signed Jar' inputs: PathtoPublish: '$(System.DefaultWorkingDirectory)/app/build/libs/signed.jar' ArtifactName: 'Signed Jar' publishLocation: 'Container' - task: CmdLine@2 displayName: 'Verify Signed Jar' inputs: script: 'jarsigner -verify $(System.DefaultWorkingDirectory)/app/build/libs/signed.jar'