42 import java.util.zip.ZipOutputStream; |
46 import java.util.zip.ZipOutputStream; |
43 |
47 |
44 public class DiffEnd { |
48 public class DiffEnd { |
45 |
49 |
46 static void check() throws Exception { |
50 static void check() throws Exception { |
47 SecurityTools.jarsigner("-keystore " |
51 String ksArgs = "-keystore " + Path.of(System.getProperty("test.src")) |
48 + Path.of(System.getProperty("test.src"), "JarSigning.keystore") |
52 .resolve("JarSigning.keystore") + " -storepass bbbbbb"; |
49 .toString() |
|
50 + " -storepass bbbbbb -digestalg SHA1" |
|
51 + " -signedjar diffend.new.jar diffend.jar c"); |
|
52 |
53 |
53 try (JarFile jf = new JarFile("diffend.new.jar")) { |
54 SecurityTools.jarsigner(ksArgs + " -digestalg SHA1 " |
|
55 + "-signedjar diffend.signed.jar diffend.jar c") |
|
56 .shouldHaveExitValue(0); |
|
57 SecurityTools.jarsigner(" -verify " + ksArgs + " -verbose " |
|
58 + "diffend.signed.jar c") |
|
59 .stdoutShouldMatch("^smk .* 1$").shouldHaveExitValue(0); |
|
60 |
|
61 try (JarFile jf = new JarFile("diffend.signed.jar")) { |
54 Asserts.assertTrue(jf.getManifest().getMainAttributes() |
62 Asserts.assertTrue(jf.getManifest().getMainAttributes() |
55 .containsKey(new Attributes.Name("Today"))); |
63 .containsKey(new Attributes.Name("Today"))); |
56 } |
64 } |
57 } |
65 } |
58 |
66 |
59 public static void main(String[] args) throws Exception { |
67 public static void main(String[] args) throws Exception { |
60 |
|
61 // A MANIFEST.MF using \n as newlines and no double newlines at the end |
68 // A MANIFEST.MF using \n as newlines and no double newlines at the end |
62 byte[] manifest = |
69 byte[] manifest = ("Manifest-Version: 1.0\n" |
63 ("Manifest-Version: 1.0\n" |
|
64 + "Created-By: 1.7.0-internal (Sun Microsystems Inc.)\n" |
70 + "Created-By: 1.7.0-internal (Sun Microsystems Inc.)\n" |
65 + "Today: Monday\n").getBytes(StandardCharsets.UTF_8); |
71 + "Today: Monday\n").getBytes(StandardCharsets.UTF_8); |
|
72 |
|
73 // Without the fake .RSA file, to trigger the if (wasSigned) else block |
|
74 try (FileOutputStream fos = new FileOutputStream("diffend.jar"); |
|
75 ZipOutputStream zos = new ZipOutputStream(fos)) { |
|
76 zos.putNextEntry(new ZipEntry(JarFile.MANIFEST_NAME)); |
|
77 zos.write(manifest); |
|
78 zos.putNextEntry(new ZipEntry("1")); |
|
79 zos.write(new byte[10]); |
|
80 } |
|
81 check(); |
66 |
82 |
67 // With the fake .RSA file, to trigger the if (wasSigned) block |
83 // With the fake .RSA file, to trigger the if (wasSigned) block |
68 try (FileOutputStream fos = new FileOutputStream("diffend.jar"); |
84 try (FileOutputStream fos = new FileOutputStream("diffend.jar"); |
69 ZipOutputStream zos = new ZipOutputStream(fos)) { |
85 ZipOutputStream zos = new ZipOutputStream(fos)) { |
70 zos.putNextEntry(new ZipEntry("META-INF/MANIFEST.MF")); |
86 zos.putNextEntry(new ZipEntry(JarFile.MANIFEST_NAME)); |
71 zos.write(manifest); |
87 zos.write(manifest); |
72 zos.putNextEntry(new ZipEntry("META-INF/x.RSA")); |
88 zos.putNextEntry(new ZipEntry("META-INF/x.RSA")); // fake .RSA |
73 zos.putNextEntry(new ZipEntry("1")); |
89 zos.putNextEntry(new ZipEntry("1")); |
74 zos.write(new byte[10]); |
90 zos.write(new byte[10]); |
75 } |
91 } |
76 |
|
77 check(); |
|
78 |
|
79 // Without the fake .RSA file, to trigger the else block |
|
80 try (FileOutputStream fos = new FileOutputStream("diffend.jar"); |
|
81 ZipOutputStream zos = new ZipOutputStream(fos)) { |
|
82 zos.putNextEntry(new ZipEntry("META-INF/MANIFEST.MF")); |
|
83 zos.write(manifest); |
|
84 zos.putNextEntry(new ZipEntry("1")); |
|
85 zos.write(new byte[10]); |
|
86 } |
|
87 |
|
88 check(); |
92 check(); |
89 } |
93 } |
90 } |
94 } |