Jam*_*arr 26 java code-signing signature jarsigner
我想使用jarsigner签署一个jar,然后使用一个Java应用程序验证它,该应用程序没有签名的jar作为其类路径的一部分(即只使用jar的文件系统位置)
现在我的问题是从jar中获取签名文件,是否有一种简单的方法可以做到这一点?
我玩过Inflater和Jar InputStreams没有运气.
或者这是可以更好地完成的事情吗?
谢谢
nd.*_*nd. 17
您只需使用java.util.jar.JarFile打开JAR并告诉它验证JAR文件.如果JAR已签名,则JarFile可以选择对其进行验证(默认情况下处于启用状态).但是,JarFile也会愉快地打开未签名的JAR,因此您还必须检查文件是否已签名.您可以通过检查*-Digest属性的JAR清单来执行此操作:具有此类属性属性的元素已签名.
例:
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);
Run Code Online (Sandbox Code Playgroud)
eri*_*son 14
在安全提供者实施指南列出了验证JAR文件的过程.虽然这些说明适用于JCA加密服务提供商进行自我验证,但它们应适用于您的问题.
具体来说,请查看verify(X509Certificate targetCert)示例代码"MyJCE.java"中的方法.
| 归档时间: |
|
| 查看次数: |
31233 次 |
| 最近记录: |