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? 에서 프로그래밍 샘플을 사용하여 수행하려고
, 나는 그 문제에 관해서는 전혀는 SecurityException ... 또는 예외를하지 않습니다.다른 사람들을 위해 작동하는 것처럼 보이기 때문에 내가 뭘 잘못하고 있는지 확실하지 않습니다. 어떤 아이디어? 이것은 JDK 1.6 BTW입니다.
편집 : 은, 아래의 코드의 샘플을 요청으로 ... 아래의 예를 사용하여 자신의 수정 항아리 :
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을 어떻게 변경 했습니까? – trashgod
7zip으로 열었습니다. 새로운 패키지 디렉토리에 클래스 파일을 추가하고 재 컴파일 된 버전으로 기존 클래스 파일을 수정했습니다. – Amasuriel
'jarsigner'가 변경된 JAR를 거부하지만 코드가이를 받아들이면 [sscce] (http://sscce.org/)가 문제를 발견하는 데 도움이 될 수 있습니다. – trashgod