简体   繁体   English

如何以编程方式验证使用jarsigner签名的jar

[英]How to verify a jar signed with jarsigner programmatically

I'm wanting to sign a jar using jarsigner, then verify it using a Java application which does not have the signed jar as part of it's classpath (ie just using a filesystem location of the jar) 我想使用jarsigner签署一个jar,然后使用一个Java应用程序验证它,该应用程序没有签名jar作为其类路径的一部分(即只使用jar的文件系统位置)

Now my problem is getting the signature file out of the jar, is there a simple way to do this? 现在我的问题是从jar中获取签名文件,是否有一种简单的方法可以做到这一点?

I've had a play with the Inflater and Jar InputStreams with no luck. 我玩过Inflater和Jar InputStreams没有运气。

Or is this something that can be accomplished in a better way? 或者这是可以更好地完成的事情吗?

Thanks 谢谢

You can simply open the JAR with java.util.jar.JarFile and tell it to verify the JAR file. 您只需使用java.util.jar.JarFile打开JAR并告诉它验证JAR文件。 If the JAR is signed, then JarFile has the option to verify it (which is on by default). 如果JAR已签名,则JarFile可以选择对其进行验证(默认情况下处于启用状态)。 However, JarFile will also open unsigned JARs happily, therefore you must also check, whether or not the file is signed. 但是,JarFile也会愉快地打开未签名的JAR,因此您还必须检查文件是否已签名。 You can do so by checking the JAR's manifest for *-Digest attributes: Elements with such an attribute attribute are signed. 您可以通过检查* -Digest属性的JAR清单来执行此操作:具有此类属性属性的元素已签名。

Example: 例:

JarFile jar = new JarFile(new File("path/to/your/jar-file"));

// This call will throw a java.lang.SecurityException if someone has tampered
// with the signature of _any_ element of the JAR file.
// Alas, it will proceed without a problem if the JAR file is not signed at all
InputStream is = jar.getInputStream(jar.getEntry("META-INF/MANIFEST.MF"));
Manifest man = new Manifest(is);
is.close();

Set<String> signed = new HashSet();
for(Map.Entry<String, Attributes> entry: man.getEntries().entrySet()) {
    for(Object attrkey: entry.getValue().keySet()) {
        if (attrkey instanceof Attributes.Name && 
           ((Attributes.Name)attrkey).toString().indexOf("-Digest") != -1)
            signed.add(entry.getKey());
    }
}

Set<String> entries = new HashSet<String>();
for(Enumeration<JarEntry> entry = jar.entries(); entry.hasMoreElements(); ) {
    JarEntry je = entry.nextElement();
    if (!je.isDirectory())
        entries.add(je.getName());
}

// contains all entries in the Manifest that are not signed.
// Ususally, this contains:
//  * MANIFEST.MF itself
//  * *.SF files containing the signature of MANIFEST.MF
//  * *.DSA files containing public keys of the signer

Set<String> unsigned = new HashSet<String>(entries);
unsigned.removeAll(signed);

// contains all the entries with a signature that are not present in the JAR
Set<String> missing = new HashSet<String>(signed);
missing.removeAll(entries);

The security Provider implementation guide outlines the process of verifying JARs. 安全提供程序实施指南概述了验证JAR的过程。 Although these instructions are for a JCA cryptographic service provider to verify itself, they should be applicable to your problem. 虽然这些说明适用于JCA加密服务提供商进行自我验证,但它们应适用于您的问题。

Specifically, check out the verify(X509Certificate targetCert) method in the sample code, "MyJCE.java" . 具体来说,请查看示例代码“MyJCE.java”中verify(X509Certificate targetCert)方法。

You can use entry.getCodeSigners() to get the signers for a particular entry in the JAR. 您可以使用entry.getCodeSigners()来获取JAR中特定条目的签名者。

Make sure to open the JarFile with verify=true and to fully read the JAR entry before calling entry.getCodeSigners(). 确保使用verify = true打开JarFile并在调用entry.getCodeSigners()之前完全读取JAR条目。

Something like this could be used to verify each entry that is not a signature file: 这样的东西可以用来验证每个不是签名文件的条目:

boolean verify = true;
JarFile jar = new JarFile(signedFile, verify);

// Need each entry so that future calls to entry.getCodeSigners will return anything
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
   JarEntry entry = entries.nextElement();
   IOUtils.copy(jar.getInputStream(entry), new NullOutputStream());
}

// Now check each entry that is not a signature file
entries = jar.entries();
while (entries.hasMoreElements()) {
    JarEntry entry = entries.nextElement();
    String fileName = entry.getName().toUpperCase(Locale.ENGLISH);
    if (!fileName.endsWith(".SF")
       && !fileName.endsWith(".DSA")
       && !fileName.endsWith(".EC")
       && !fileName.endsWith(".RSA")) {

       // Now get code signers, inspect certificates etc here...
       // entry.getCodeSigners();
    }
 }

You can use the jarsigner application to do this. 您可以使用jarsigner应用程序执行此操作。 In processbuilder (or Runtime.exec) you can run the command with these arguments 在processbuilder(或Runtime.exec)中,您可以使用这些参数运行命令

 ProcessBulider pb = new ProcessBuilder("/usr/bin/jarsigner", "-verify", "-certs", f.getAbsolutePath());

and if the output contians verified then the jar is signed 如果输出contians已验证,则jar将被签名

Process p = pb.start();
p.waitFor();
InputStream is = p.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null)
{
if(line.contains("verified");
...

THere are more complicated things you can do when you have the output of the jarsigner code. 当你有jarsigner代码的输出时,你可以做更复杂的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM