【发布时间】:2011-08-01 01:17:33
【问题描述】:
我正在尝试以编程方式验证 jar 文件是否未被明显篡改。我有 2 个要防止的用例。 1) 现有类的修改 2) jar 中新增类
我使用 jarsigner 对 jar 进行了签名。当我使用 jarsigner 验证上述任何一种情况时,它的工作方式与我预期的一样。
当我尝试使用示例以编程方式执行此操作时 How to verify a jar signed with jarsigner programmatically 要么 How to verify signature on self signed jar? 但是,我没有得到任何 SecurityExceptions ......或任何异常。
不确定我做错了什么,因为这些 sn-ps 似乎对其他人有用。有任何想法吗?这是 JDK 1.6 顺便说一句。
编辑:
按照下面的要求,代码示例...提供您自己修改过的 jar :)
JarFile myJar;
try
{
//Insert the full path to the jar here
String libPath = ""
stature = new JarFile(libPath,true);
//Don't really need this right now but was using it to inspect the SHA1 hashes
InputStream is = myJar.getInputStream(myJar.getEntry("META-INF/MANIFEST.MF"));
Manifest man = myJar.getManifest();
is.close();
verifyJar(myJar);
}
catch (IOException ioe)
{
throw new Exception("Cannot load jar file", ioe);
}
private void verifyJar(JarFile jar) throws Exception
{
Enumeration<java.util.jar.JarEntry> entries = jar.entries();
while (entries.hasMoreElements())
{
java.util.jar.JarEntry entry = entries.nextElement();
try
{
jar.getInputStream(entry);
//Also tried actually creating a variable from the stream in case it was discarding it before verification
//InputStream is = jar.getInputStream(entry);
//is.close();
}
catch (SecurityException se)
{
/* Incorrect signature */
throw new Exception("Signature verification failed", se);
}
catch (IOException ioe)
{
throw new Exception("Cannot load jar file entry", ioe);
}
}
}
【问题讨论】:
-
你是如何篡改被测 JAR 的?
-
我用 7zip 打开它。我添加了一个新的包目录,里面有一些类文件,并用重新编译的版本修改了一些现有的类文件。
-
如果
jarsigner拒绝更改后的 JAR,但您的代码接受它,sscce 可能有助于发现问题。
标签: java jar-signing