Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/tools/jarsigner/SectionNameContinuedVsLineBreak.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.io.ByteArrayOutputStream;
26
import java.io.IOException;
27
import java.nio.file.Files;
28
import java.nio.file.Path;
29
import java.util.Map;
30
import java.util.function.Function;
31
import java.util.jar.Manifest;
32
import java.util.jar.JarFile;
33
import jdk.test.lib.util.JarUtils;
34
import jdk.test.lib.SecurityTools;
35
import org.testng.annotations.BeforeTest;
36
import org.testng.annotations.Test;
37
38
import static java.nio.charset.StandardCharsets.UTF_8;
39
40
/**
41
* @test
42
* @bug 8217375
43
* @library /test/lib
44
* @run testng SectionNameContinuedVsLineBreak
45
* @summary Checks some specific line break character sequences in section name
46
* continuation line breaks.
47
*/
48
public class SectionNameContinuedVsLineBreak {
49
50
static final String KEYSTORE_FILENAME = "test.jks";
51
52
@BeforeTest
53
public void prepareCertificate() throws Exception {
54
SecurityTools.keytool("-genkeypair -keyalg EC -keystore "
55
+ KEYSTORE_FILENAME + " -storepass changeit -keypass changeit"
56
+ " -alias a -dname CN=A").shouldHaveExitValue(0);
57
}
58
59
void manipulateManifestSignAgainA(
60
String srcJarFilename, String dstJarFilename,
61
Function<Manifest, byte[]> manifestManipulation) throws Exception {
62
byte[] manipulatedManifest = manifestManipulation.apply(
63
new Manifest(new ByteArrayInputStream(
64
Utils.readJarManifestBytes(srcJarFilename))));
65
Utils.echoManifest(manipulatedManifest, "manipulated manifest");
66
JarUtils.updateJar(srcJarFilename, dstJarFilename, Map.of(
67
JarFile.MANIFEST_NAME, manipulatedManifest));
68
SecurityTools.jarsigner("-keystore " + KEYSTORE_FILENAME +
69
" -storepass changeit -verbose -debug " + dstJarFilename + " a")
70
.shouldHaveExitValue(0);
71
Utils.echoManifest(Utils.readJarManifestBytes(
72
dstJarFilename), "manipulated jar signed again with a");
73
// check assumption that jar is valid at this point
74
SecurityTools.jarsigner("-verify -keystore " + KEYSTORE_FILENAME +
75
" -storepass changeit -verbose -debug " + dstJarFilename + " a")
76
.shouldHaveExitValue(0);
77
}
78
79
void test(String name, Function<Manifest, byte[]> manifestManipulation,
80
String jarContentFilename) throws Exception {
81
String jarFilename1 = "test-" + name + "-step1.jar";
82
Files.write(Path.of(jarContentFilename),
83
jarContentFilename.getBytes(UTF_8));
84
JarUtils.createJarFile(Path.of(jarFilename1), (Manifest)
85
/* no manifest will let jarsigner create a default one */ null,
86
Path.of("."), Path.of(jarContentFilename));
87
SecurityTools.jarsigner("-keystore " + KEYSTORE_FILENAME +
88
" -storepass changeit -verbose -debug " + jarFilename1 +
89
" a").shouldHaveExitValue(0);
90
Utils.echoManifest(Utils.readJarManifestBytes(
91
jarFilename1), "signed jar");
92
String jarFilename2 = "test-" + name + "-step2.jar";
93
manipulateManifestSignAgainA(jarFilename1, jarFilename2,
94
manifestManipulation);
95
96
SecurityTools.jarsigner("-verify -strict -keystore " +
97
KEYSTORE_FILENAME + " -storepass changeit -debug -verbose " +
98
jarFilename2 + " a").shouldHaveExitValue(0);
99
}
100
101
/**
102
* Test signing a jar with a manifest that has an entry the name of
103
* which continued on a continuation line with '\r' as line break before
104
* the continuation line space ' ' on the line the name starts.
105
*/
106
@Test
107
public void testContinueNameAfterCr() throws Exception {
108
String filename = "abc";
109
test("testContinueNameAfterCr", m -> {
110
String digest = m.getAttributes("abc").getValue("SHA-256-Digest");
111
m.getEntries().remove("abc");
112
return (manifestToString(m)
113
+ "Name: a\r"
114
+ " bc\r\n"
115
+ "SHA-256-Digest: " + digest + "\r\n"
116
+ "\r\n").getBytes(UTF_8);
117
}, filename);
118
}
119
120
/**
121
* Test signing a jar with a manifest that has an entry the name of
122
* which continued on a continuation line with '\r' as line break before
123
* the continuation line space ' ' after a first continuation.
124
*/
125
@Test
126
public void testContinueNameAfterCrOnContinuationLine() throws Exception {
127
String filename = "abc";
128
test("testContinueNameAfterCr", m -> {
129
String digest = m.getAttributes("abc").getValue("SHA-256-Digest");
130
m.getEntries().remove("abc");
131
return (manifestToString(m)
132
+ "Name: a\r\n"
133
+ " b\r"
134
+ " c\r\n"
135
+ "SHA-256-Digest: " + digest + "\r\n"
136
+ "\r\n").getBytes(UTF_8);
137
}, filename);
138
}
139
140
/**
141
* Test signing a jar with a manifest that has an entry the name of
142
* which continued on a continuation line and terminated with '\r' as line
143
* break after the name.
144
*/
145
@Test
146
public void testEndNameWithCrOnContinuationLine() throws Exception {
147
String filename = "abc";
148
test("testContinueNameAfterCr", m -> {
149
String digest = m.getAttributes("abc").getValue("SHA-256-Digest");
150
m.getEntries().remove("abc");
151
return (manifestToString(m)
152
+ "Name: a\r\n"
153
+ " bc\r"
154
+ "SHA-256-Digest: " + digest + "\r\n"
155
+ "\r\n").getBytes(UTF_8);
156
}, filename);
157
}
158
159
String manifestToString(Manifest mf) {
160
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
161
mf.write(out);
162
return out.toString(UTF_8);
163
} catch (IOException e) {
164
throw new RuntimeException(e);
165
}
166
}
167
168
}
169
170