Path: blob/master/test/jdk/sun/security/tools/jarsigner/SignedAgain.java
41152 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 821592226* @summary jar spec is not precise when describing jar file re-signing27* @library /test/lib28*/2930import jdk.test.lib.Asserts;31import jdk.test.lib.util.JarUtils;3233import java.io.InputStream;34import java.security.MessageDigest;35import java.util.Base64;36import java.util.jar.JarEntry;37import java.util.jar.JarFile;38import java.util.jar.Manifest;3940import static jdk.test.lib.SecurityTools.*;4142public class SignedAgain {43public static void main(String[] args) throws Exception {4445String opt = "-storepass changeit -keystore ks";4647keytool(opt + " -genkeypair -alias a -dname CN=A -keyalg RSA");48keytool(opt + " -genkeypair -alias b -dname CN=B -keyalg RSA");4950JarUtils.createJar("a.jar", "f1");5152// as.jar: signed by a53jarsigner(opt + " -signedjar as.jar a.jar a");5455JarUtils.updateJar("as.jar", "b.jar", "f2");5657// bs.jar: signed again by b58jarsigner(opt + " -signedjar bs.jar b.jar b");5960// verified61jarsigner(opt + " -verify -strict -verbose -certs bs.jar")62.shouldHaveExitValue(0);6364try (JarFile ja = new JarFile("as.jar");65JarFile jb = new JarFile("bs.jar");66InputStream ma = ja.getInputStream(67new JarEntry("META-INF/MANIFEST.MF"));68InputStream sa = jb.getInputStream(new JarEntry("META-INF/A.SF"));69InputStream mb = jb.getInputStream(70new JarEntry("META-INF/MANIFEST.MF"));71InputStream sb = jb.getInputStream(new JarEntry("META-INF/B.SF"))) {7273// Hash of manifest for 2 signed JAR files74String da = Base64.getEncoder().encodeToString(MessageDigest75.getInstance("SHA-256").digest(ma.readAllBytes()));76String db = Base64.getEncoder().encodeToString(MessageDigest77.getInstance("SHA-256").digest(mb.readAllBytes()));7879// They are not the same80Asserts.assertNotEquals(da, db);8182// Digest-Manifest in A.SF matches da83Asserts.assertEQ(new Manifest(sa).getMainAttributes()84.getValue("SHA-256-Digest-Manifest"), da);8586// Digest-Manifest in B.SF matches db87Asserts.assertEQ(new Manifest(sb).getMainAttributes()88.getValue("SHA-256-Digest-Manifest"), db);89}90}91}929394