CkJava Requires Chilkat v11.7.0+
CkJava
Verify the Authenticode Signature of an MSI, PowerShell, or VBScript File
See more Code Signing Examples
Demonstrates how to verify the Authenticode signature of a Windows Installer package (.msi), a PowerShell file (.ps1, .psm1, .psd1), or a VBScript file (.vbs), and how to get the certificate of the signer. Verifying these file types requires Chilkat v11.7.0 or later.Chilkat CkJava Downloads
import com.chilkatsoft.*;
public class ChilkatExample {
static {
try {
System.loadLibrary("chilkat");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
public static void main(String argv[])
{
boolean success = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Starting in Chilkat v11.7.0, VerifySignature can verify the Authenticode signature of
// - Windows Installer packages (.msi)
// - PowerShell scripts, modules, and data files (.ps1, .psm1, .psd1)
// - VBScript files (.vbs)
// in addition to EXE and DLL files.
// This example verifies a signed PowerShell script.
// To verify an .msi or a .vbs, pass the path of that file instead.
String path = "C:/someDir/Deploy.ps1";
CkCodeSign codeSign = new CkCodeSign();
CkJsonObject sigInfo = new CkJsonObject();
// VerifySignature returns true if the file has a valid Authenticode signature.
// It returns false if the file is not signed, if the file was changed after it was signed,
// or if the signing certificate is not trusted.
boolean valid = codeSign.VerifySignature(path,sigInfo);
if (valid == false) {
// The LastErrorText explains why the verification failed.
System.out.println(codeSign.lastErrorText());
return;
}
System.out.println("The Authenticode signature is valid.");
// The details of the signature are returned in the JSON, in the same layout as for an EXE or DLL.
// See Verify Authenticode Signature of EXE or DLL for a sample of the JSON and code that parses it.
System.out.println("signing algorithm: " + sigInfo.stringOf("pkcs7.verify.signerInfo[0].signingAlgName"));
System.out.println("hash algorithm: " + sigInfo.stringOf("pkcs7.verify.signerInfo[0].cert.digestAlgName"));
// Get the certificate of the signer.
// The signer certificate is available only after a call to VerifySignature that returned true.
// It is the public certificate only. It does not include the signer's private key.
CkCert signerCert = new CkCert();
success = codeSign.GetSignerCert(signerCert);
if (success == false) {
System.out.println(codeSign.lastErrorText());
return;
}
System.out.println("signed by: " + signerCert.subjectCN());
System.out.println("certificate issued by: " + signerCert.issuerCN());
System.out.println("certificate serial number: " + signerCert.serialNumber());
System.out.println("certificate valid until: " + signerCert.validToStr());
}
}