Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/tools/jarsigner/Options.java
41152 views
1
/*
2
* Copyright (c) 2015, 2020, 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
/**
25
* @test
26
* @bug 8056174 8242260
27
* @summary Make sure the jarsigner tool still works after it's modified to
28
* be based on JarSigner API
29
* @library /test/lib
30
* @modules java.base/sun.security.pkcs
31
* java.base/sun.security.x509
32
*/
33
34
import com.sun.jarsigner.ContentSigner;
35
import com.sun.jarsigner.ContentSignerParameters;
36
import jdk.test.lib.Asserts;
37
import jdk.test.lib.SecurityTools;
38
import jdk.test.lib.util.JarUtils;
39
import sun.security.pkcs.PKCS7;
40
41
import java.io.ByteArrayInputStream;
42
import java.io.InputStream;
43
import java.nio.file.Files;
44
import java.nio.file.Path;
45
import java.util.*;
46
import java.util.jar.Attributes;
47
import java.util.jar.JarEntry;
48
import java.util.jar.JarFile;
49
import java.util.jar.Manifest;
50
51
public class Options {
52
53
public static void main(String[] args) throws Exception {
54
55
// Help
56
boolean lastLineHasAltSigner = false;
57
for (String line : SecurityTools.jarsigner("--help").asLines()) {
58
if (line.contains("-altsigner")) {
59
lastLineHasAltSigner = true;
60
} else {
61
if (lastLineHasAltSigner) {
62
Asserts.assertTrue(line.contains("deprecated and will be removed"));
63
}
64
lastLineHasAltSigner = false;
65
}
66
}
67
68
// Prepares raw file
69
Files.write(Path.of("a"), List.of("a"));
70
71
// Pack
72
JarUtils.createJarFile(Path.of("a.jar"), Path.of("."), Path.of("a"));
73
74
// Prepare a keystore
75
SecurityTools.keytool(
76
"-keystore jks -storepass changeit -keypass changeit -dname" +
77
" CN=A -alias a -genkeypair -keyalg rsa")
78
.shouldHaveExitValue(0);
79
80
// -altsign
81
SecurityTools.jarsigner(
82
"-debug -signedjar altsign.jar -keystore jks -storepass changeit" +
83
" -altsigner Options$X" +
84
" -altsignerpath " + System.getProperty("test.classes") +
85
" a.jar a")
86
.shouldContain("removed in a future release: -altsigner")
87
.shouldContain("removed in a future release: -altsignerpath")
88
.shouldContain("PKCS7.parse"); // signature not parseable
89
// but signing succeeds
90
91
try (JarFile jf = new JarFile("altsign.jar")) {
92
JarEntry je = jf.getJarEntry("META-INF/A.RSA");
93
try (InputStream is = jf.getInputStream(je)) {
94
if (!Arrays.equals(is.readAllBytes(), "1234".getBytes())) {
95
throw new Exception("altsign go wrong");
96
}
97
}
98
}
99
100
// -altsign with no -altsignerpath
101
Files.copy(Path.of(System.getProperty("test.classes"), "Options$X.class"),
102
Path.of("Options$X.class"));
103
SecurityTools.jarsigner(
104
"-debug -signedjar altsign.jar -keystore jks -storepass changeit" +
105
" -altsigner Options$X" +
106
" a.jar a")
107
.shouldContain("removed in a future release: -altsigner")
108
.shouldNotContain("removed in a future release: -altsignerpath")
109
.shouldContain("PKCS7.parse"); // signature not parseable
110
// but signing succeeds
111
112
// -sigfile, -digestalg, -sigalg, -internalsf, -sectionsonly
113
SecurityTools.jarsigner(
114
"-debug -signedjar new.jar -keystore jks -storepass changeit" +
115
" -sigfile olala -digestalg SHA1 -sigalg SHA224withRSA" +
116
" -internalsf -sectionsonly a.jar a")
117
.shouldHaveExitValue(0)
118
.shouldNotContain("Exception"); // a real success
119
120
try (JarFile jf = new JarFile("new.jar")) {
121
JarEntry je = jf.getJarEntry("META-INF/OLALA.SF");
122
Objects.requireNonNull(je); // check -sigfile
123
byte[] sf = null; // content of .SF
124
try (InputStream is = jf.getInputStream(je)) {
125
sf = is.readAllBytes(); // save for later comparison
126
Attributes attrs = new Manifest(new ByteArrayInputStream(sf))
127
.getMainAttributes();
128
// check -digestalg
129
if (!attrs.containsKey(new Attributes.Name(
130
"SHA1-Digest-Manifest-Main-Attributes"))) {
131
throw new Exception("digestalg incorrect");
132
}
133
// check -sectionsonly
134
if (attrs.containsKey(new Attributes.Name(
135
"SHA1-Digest-Manifest"))) {
136
throw new Exception("SF should not have file digest");
137
}
138
}
139
140
je = jf.getJarEntry("META-INF/OLALA.RSA");
141
try (InputStream is = jf.getInputStream(je)) {
142
PKCS7 p7 = new PKCS7(is.readAllBytes());
143
String alg = p7.getSignerInfos()[0]
144
.getDigestAlgorithmId().getName();
145
if (!alg.equals("SHA-224")) { // check -sigalg
146
throw new Exception("PKCS7 signing is using " + alg);
147
}
148
// check -internalsf
149
if (!Arrays.equals(sf, p7.getContentInfo().getData())) {
150
throw new Exception("SF not in RSA");
151
}
152
}
153
154
}
155
156
// TSA-related ones are checked in ts.sh
157
}
158
159
public static class X extends ContentSigner {
160
@Override
161
public byte[] generateSignedData(ContentSignerParameters parameters,
162
boolean omitContent, boolean applyTimestamp) {
163
return "1234".getBytes();
164
}
165
}
166
}
167
168