Java (jarsigner)
Use jarsigner from the Java Development Kit (JDK) to sign JAR files. For current public DigiCert organization validation (OV) or extended validation (EV) code signing, the private key stays on a token or hardware security module (HSM). Configure jarsigner with Public-Key Cryptography Standards (PKCS) #11, not a software .jks that holds the private key.
Hardware keys only. Public code signing certificates issued since June 1, 2023 cannot use an exportable software keystore. The keytool import flow below applies to legacy Sun/Java code signing products or to importing the public certificate into a store that already points at a hardware key. Do not try to import a modern code signing private key into a .jks.
PKCS #11 config (USB eToken)
Create a config file (for example etoken.cfg). Adjust the library path for your OS and SafeNet version:
name = eToken
library = C:\Windows\System32\eToken.dll
slotListIndex = 0
Linux library paths vary (often under /usr/lib or /usr/lib64 after SafeNet install). Confirm with SafeNet documentation for your build.
Sign:
jarsigner -keystore NONE -storetype PKCS11 \
-providerClass sun.security.pkcs11.SunPKCS11 \
-providerArg etoken.cfg \
-tsa http://timestamp.digicert.com \
-signedjar SignedApp.jar UnsignedApp.jar "<token-cert-alias>"
You will be prompted for the eToken Password. List aliases if you are unsure:
keytool -list -keystore NONE -storetype PKCS11 \
-providerClass sun.security.pkcs11.SunPKCS11 \
-providerArg etoken.cfg
CloudHSM uses a similar PKCS #11 config pointing at the CloudHSM library. See AWS CloudHSM.
Verify
jarsigner -verify -verbose -certs SignedApp.jar
Error: keytool error: java.lang.Exception: Input not an X.509 certificate
This error appears when importing a certificate into a Java keystore with keytool. Typical causes:
- Wrong alias — Use the same alias that was used when the certificate signing request (CSR) or hardware key was created. List entries with
keytool -list -keystore your_keystore -v. - Wrong file format — The file is not a single X.509 cert or PKCS #7 that
keytoolaccepts. Try Privacy-Enhanced Mail (PEM) or Distinguished Encoding Rules (DER) for the leaf, or a.p7bwith-trustcacertsas documented for your JDK. - Wrong keystore — You must import into the same keystore (or PKCS #11 slot) that holds the matching private key. Importing into a new empty
.jksfails.
If the keystore that generated a legacy software CSR is lost, you cannot attach the issued cert to a new key. Reissue or replace the certificate and generate a new hardware-backed key.
For current DigiCert code signing on an eToken, prefer PKCS #11 jarsigner above instead of keytool -import into a .jks.