author | weijun |
Thu, 18 Jul 2019 08:53:06 +0800 | |
changeset 57488 | 94691d8e746f |
permissions | -rw-r--r-- |
57488
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
1 |
/* |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
4 |
* |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
8 |
* |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
13 |
* accompanied this code). |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
14 |
* |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
18 |
* |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
21 |
* questions. |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
22 |
*/ |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
23 |
|
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
24 |
import java.io.ByteArrayInputStream; |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
25 |
import java.lang.reflect.Method; |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
26 |
import java.nio.file.Path; |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
27 |
import java.util.jar.JarFile; |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
28 |
import java.util.jar.Manifest; |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
29 |
import java.util.jar.Attributes; |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
30 |
import java.util.jar.Attributes.Name; |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
31 |
|
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
32 |
import jdk.test.lib.util.JarUtils; |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
33 |
import jdk.test.lib.SecurityTools; |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
34 |
import org.testng.annotations.Test; |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
35 |
import org.testng.annotations.BeforeClass; |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
36 |
|
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
37 |
import static org.testng.Assert.*; |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
38 |
|
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
39 |
/** |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
40 |
* @test |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
41 |
* @bug 8217375 |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
42 |
* @library /test/lib |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
43 |
* @modules java.base/java.util.jar:+open |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
44 |
* @run testng/othervm EmptyIndividualSectionName |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
45 |
* @summary Check that an individual section with an empty name is digested |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
46 |
* and signed. |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
47 |
* <p> |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
48 |
* See also |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
49 |
* jdk/test/jdk/sun/security/util/ManifestDigester/FindSections.java |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
50 |
* for much more detailed api level tests |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
51 |
*/ |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
52 |
public class EmptyIndividualSectionName { |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
53 |
|
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
54 |
static final String KEYSTORE_FILENAME = "test.jks"; |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
55 |
|
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
56 |
@BeforeClass |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
57 |
public void prepareCertificate() throws Exception { |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
58 |
SecurityTools.keytool("-genkeypair -keyalg EC -keystore " |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
59 |
+ KEYSTORE_FILENAME + " -storepass changeit -keypass changeit " |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
60 |
+ "-alias a -dname CN=X").shouldHaveExitValue(0); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
61 |
} |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
62 |
|
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
63 |
/** |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
64 |
* Adds an additional section with name {@code sectionName} to the manifest |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
65 |
* of a JAR before signing it with {@code signOpts}. |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
66 |
* @return signature file {@code META-INF/A.SF} for further assertions |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
67 |
*/ |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
68 |
Manifest test(String sectionName, String signOpts) throws Exception { |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
69 |
Manifest mf = new Manifest(); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
70 |
mf.getMainAttributes().put(Name.MANIFEST_VERSION, "1.0"); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
71 |
mf.getEntries().put(sectionName, new Attributes()); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
72 |
String jarFilename = "test" + sectionName + |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
73 |
(signOpts != null ? signOpts : "") + ".jar"; |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
74 |
JarUtils.createJarFile(Path.of(jarFilename), mf, Path.of(".")); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
75 |
SecurityTools.jarsigner("-keystore " + KEYSTORE_FILENAME + |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
76 |
" -storepass changeit -verbose -debug " + |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
77 |
(signOpts != null ? signOpts + " " : "") + jarFilename + " a") |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
78 |
.shouldHaveExitValue(0); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
79 |
SecurityTools.jarsigner("-verify -keystore " + KEYSTORE_FILENAME + |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
80 |
" -storepass changeit -debug -verbose " + jarFilename + " a") |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
81 |
.shouldHaveExitValue(0); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
82 |
|
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
83 |
byte[] mfBytes = Utils.readJarManifestBytes(jarFilename); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
84 |
Utils.echoManifest(mfBytes, "manifest"); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
85 |
mf = new Manifest(new ByteArrayInputStream(mfBytes)); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
86 |
assertNotNull(mf.getAttributes(sectionName)); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
87 |
byte[] sfBytes = Utils.readJarEntryBytes(jarFilename, "META-INF/A.SF"); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
88 |
Utils.echoManifest(sfBytes, "signature file META-INF/A.SF"); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
89 |
return new Manifest(new ByteArrayInputStream(sfBytes)); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
90 |
} |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
91 |
|
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
92 |
/** |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
93 |
* Verifies that it makes a difference if the name is empty or not |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
94 |
* by running the same test as {@link #testNameEmpty} with only a different |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
95 |
* section name. |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
96 |
*/ |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
97 |
@Test |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
98 |
public void testNameNotEmpty() throws Exception { |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
99 |
String sectionName = "X"; |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
100 |
assertNotNull(test(sectionName, null).getAttributes(sectionName)); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
101 |
} |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
102 |
|
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
103 |
/** |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
104 |
* Verifies that individual sections are digested and signed also if the |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
105 |
* name of such a section is empty. |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
106 |
* An empty name of an individual section cannot be tested by adding a file |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
107 |
* with an empty name to a JAR because such a file name is invalid and |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
108 |
* cannot be used to add a file because it cannot be created or added to |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
109 |
* the JAR file in the first place. However, an individual section with an |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
110 |
* empty name can be added to the manifest. |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
111 |
* Expected is a corresponding digest in the signature file which was not |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
112 |
* present or produced before resolution of bug 8217375. |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
113 |
*/ |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
114 |
@Test |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
115 |
public void testNameEmpty() throws Exception { |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
116 |
String sectionName = ""; |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
117 |
assertNotNull(test(sectionName, null).getAttributes(sectionName)); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
118 |
} |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
119 |
|
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
120 |
/** |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
121 |
* Similar to {@link #testNameEmpty} but tries to show a real difference |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
122 |
* rather than just some internals in a {@code .SF} file, but TODO |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
123 |
*/ |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
124 |
@Test(enabled = false, description = "TODO") |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
125 |
public void testNameEmptyTrusted() throws Exception { |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
126 |
String sectionName = ""; |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
127 |
test(sectionName, "-sectionsonly"); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
128 |
String jarFilename = "test" + sectionName + "-sectionsonly.jar"; |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
129 |
try (JarFile jar = new JarFile(jarFilename, true)) { |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
130 |
Manifest m = jar.getManifest(); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
131 |
Method getTrustedAttributes = m.getClass() |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
132 |
.getDeclaredMethod("getTrustedAttributes", String.class); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
133 |
getTrustedAttributes.setAccessible(true); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
134 |
assertThrows(SecurityException.class, () -> |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
135 |
getTrustedAttributes.invoke(m, sectionName)); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
136 |
} |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
137 |
} |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
138 |
|
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff
changeset
|
139 |
} |