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