Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/tools/jarsigner/JavaKeyStoreAliasCaseInsensitive.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
/*
25
* @test
26
* @bug 8221719
27
* @library /test/lib
28
* @run testng JavaKeyStoreAliasCaseInsensitive
29
* @summary Checks that jarsigner verifies a signed jar with the same alias as
30
* was specified for signing, particularly regarding upper and lower case and
31
* its conversion to lower case by JKS
32
* ({@link sun.security.provider.JavaKeyStore.JKS#convertAlias(String)}).
33
*/
34
35
import java.nio.file.Files;
36
import java.nio.file.Paths;
37
import jdk.test.lib.util.JarUtils;
38
import jdk.test.lib.SecurityTools;
39
import org.testng.annotations.Test;
40
41
public class JavaKeyStoreAliasCaseInsensitive {
42
43
/**
44
* Alias for certificates in the keystore with letters in different
45
* (upper and lower) cases.
46
*/
47
static final String ALIAS = "AlIaS";
48
49
@Test
50
public void testAliasCase() throws Exception {
51
final String KEYSTORE_OPTIONS = "-storetype JKS -keystore "
52
+ "test-alias-case.jks -storepass changeit";
53
SecurityTools.keytool(KEYSTORE_OPTIONS + " -genkeypair -keyalg DSA"
54
+ " -keypass changeit -alias " + ALIAS + " -dname CN=" + ALIAS)
55
.shouldHaveExitValue(0);
56
String jarFilename = "test-alias-case.jar";
57
JarUtils.createJarFile(Paths.get(jarFilename), Paths.get("."),
58
Files.write(Paths.get("aFile"), new byte[1]));
59
60
SecurityTools.jarsigner(KEYSTORE_OPTIONS + " -verbose -debug " +
61
jarFilename + " " + ALIAS).shouldHaveExitValue(0);
62
63
SecurityTools.jarsigner("-verify -strict " + KEYSTORE_OPTIONS +
64
" -debug -verbose " + jarFilename + " " + ALIAS)
65
.shouldHaveExitValue(0)
66
.shouldNotContain(
67
"This jar contains signed entries which are not "
68
+ "signed by the specified alias(es).");
69
}
70
71
/**
72
* This test essentially covers compatibility with the previous version of
73
* {@link sun.security.tools.jarsigner.Main#inKeyStoreForOneSigner} in case
74
* a certificate and alias entry is already in
75
* {@link sun.security.tools.jarsigner.Main#storeHash} when
76
* {@link sun.security.tools.jarsigner.Main#inKeyStoreForOneSigner} is
77
* invoked from a previous invocation of it.
78
* It passed with the previous {@code jarsigner} version with a lowercase
79
* alias {@link #ALIAS} and basically covers the duplicated portions of
80
* code in {@link sun.security.tools.jarsigner.Main#inKeyStoreForOneSigner}
81
* near {@code IN_KEYSTORE} and {@code SIGNED_BY_ALIAS} before having
82
* refactored and re-unified them in order to demonstrate identical
83
* behavior.
84
*/
85
@Test
86
public void testAliasCaseStoreHash() throws Exception {
87
// Create a keystore with a certificate associated with ALIAS + "2"
88
// signed by another certificate associated with ALIAS + "1".
89
final String KEYSTORE_OPTIONS = "-storetype JKS -keystore"
90
+ " test-alias-storeHash-case.jks -storepass changeit";
91
SecurityTools.keytool(KEYSTORE_OPTIONS + " -genkeypair -keyalg DSA"
92
+ " -keypass changeit -alias " + ALIAS + "1 -dname CN=" +
93
ALIAS + "1" + " -ext bc:c").shouldHaveExitValue(0);
94
SecurityTools.keytool(KEYSTORE_OPTIONS + " -genkeypair -keyalg DSA"
95
+ " -keypass changeit -alias " + ALIAS + "2 -dname CN="
96
+ ALIAS + "2").shouldHaveExitValue(0);
97
String certReq = SecurityTools.keytool(KEYSTORE_OPTIONS +
98
" -certreq -keypass changeit -alias " + ALIAS + "2")
99
.shouldHaveExitValue(0).getStdout();
100
SecurityTools.setResponse(certReq);
101
String cert = SecurityTools.keytool(KEYSTORE_OPTIONS +
102
" -gencert -rfc -keypass changeit -alias " + ALIAS + "1")
103
.shouldHaveExitValue(0).getOutput();
104
SecurityTools.setResponse(cert);
105
SecurityTools.keytool(KEYSTORE_OPTIONS +
106
" -importcert -keypass changeit -alias " + ALIAS + "2")
107
.shouldHaveExitValue(0);
108
109
// Create a jar file signed by ALIAS + "2" and then add another file to
110
// that same jar and sign it by ALIAS + "1", ALIAS + "1" being an alias
111
// for a certificate which is part of the certificate chain of ALIAS +
112
// "2" but not the certificate ALIAS + "2" points to directly.
113
String jarFilename = "test-alias-storeHash-case.jar";
114
JarUtils.createJarFile(Paths.get(jarFilename), Paths.get("."));
115
SecurityTools.jarsigner(KEYSTORE_OPTIONS + " -verbose -debug " +
116
jarFilename + " " + ALIAS + "2").shouldHaveExitValue(0);
117
JarUtils.updateJarFile(Paths.get(jarFilename), Paths.get("."),
118
Files.write(Paths.get("added-file"), new byte[1]));
119
SecurityTools.jarsigner(KEYSTORE_OPTIONS + " -verbose -debug " +
120
jarFilename + " " + ALIAS + "1").shouldHaveExitValue(0);
121
122
// The later added file "added-file" is signed by the certificate
123
// associated with alias ALIAS + "1" directly while the other jarfile
124
// contents is signed by a certificate associated with alias ALIAS + "2"
125
// which includes the certificate associated with alias ALIAS + "1" in
126
// its certification path.
127
SecurityTools.jarsigner("-verify -strict " + KEYSTORE_OPTIONS +
128
" -debug -verbose " + jarFilename + " " + ALIAS + "1")
129
.shouldHaveExitValue(0)
130
.shouldNotContain(
131
"This jar contains signed entries which are not "
132
+ "signed by the specified alias(es).");
133
}
134
135
}
136
137