Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/pkcs11/Secmod/AddTrustedCert.java
41153 views
1
/*
2
* Copyright (c) 2005, 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 6298106
27
* @summary make sure we can add a trusted cert to the NSS KeyStore module
28
* @author Andreas Sterbenz
29
* @library /test/lib ..
30
* @modules jdk.crypto.cryptoki
31
* @run main/othervm AddTrustedCert
32
* @run main/othervm -Djava.security.manager=allow AddTrustedCert sm policy
33
*/
34
35
import java.io.File;
36
import java.io.FileInputStream;
37
import java.io.InputStream;
38
import java.security.KeyStore;
39
import java.security.KeyStore.TrustedCertificateEntry;
40
import java.security.Provider;
41
import java.security.Security;
42
import java.security.cert.CertificateFactory;
43
import java.security.cert.X509Certificate;
44
import java.util.Collection;
45
import java.util.Collections;
46
import java.util.TreeSet;
47
48
public class AddTrustedCert extends SecmodTest {
49
50
public static void main(String[] args) throws Exception {
51
if (args.length > 1 && "sm".equals(args[0])) {
52
System.setProperty("java.security.policy",
53
BASE + File.separator + args[1]);
54
}
55
56
if (initSecmod() == false) {
57
return;
58
}
59
60
X509Certificate cert;
61
try (InputStream in = new FileInputStream(BASE + SEP + "anchor.cer")) {
62
CertificateFactory factory =
63
CertificateFactory.getInstance("X.509");
64
cert = (X509Certificate)factory.generateCertificate(in);
65
}
66
67
String configName = BASE + SEP + "nss.cfg";
68
Provider p = getSunPKCS11(configName);
69
70
if (improperNSSVersion(p)) {
71
System.out.println(
72
"Skip test due to improper NSS version in [3.28, 3.35). "
73
+ "See JDK-8180837 for more detatils.");
74
return;
75
}
76
77
System.out.println(p);
78
Security.addProvider(p);
79
80
if (args.length > 1 && "sm".equals(args[0])) {
81
System.setProperty("java.security.policy",
82
BASE + File.separator + args[1]);
83
System.setSecurityManager(new SecurityManager());
84
}
85
86
KeyStore ks = KeyStore.getInstance(PKCS11, p);
87
ks.load(null, password);
88
Collection<String> aliases = new TreeSet<>(Collections.list(
89
ks.aliases()));
90
System.out.println("entries: " + aliases.size());
91
System.out.println(aliases);
92
int size1 = aliases.size();
93
94
String alias = "anchor";
95
if (ks.containsAlias(alias)) {
96
throw new Exception("Alias exists: " + alias);
97
}
98
99
ks.setCertificateEntry(alias, cert);
100
KeyStore.Entry first = ks.getEntry(alias, null);
101
System.out.println("first entry = " + first);
102
if (!ks.entryInstanceOf(alias, TrustedCertificateEntry.class)) {
103
throw new Exception("Unexpected first entry type: " + first);
104
}
105
106
ks.setCertificateEntry(alias, cert);
107
KeyStore.Entry second = ks.getEntry(alias, null);
108
System.out.println("second entry = " + second);
109
if (!ks.entryInstanceOf(alias, TrustedCertificateEntry.class)) {
110
throw new Exception("Unexpected second entry type: "
111
+ second);
112
}
113
114
aliases = new TreeSet<>(Collections.list(ks.aliases()));
115
System.out.println("entries: " + aliases.size());
116
System.out.println(aliases);
117
int size2 = aliases.size();
118
119
if ((size2 != size1 + 1) || (aliases.contains(alias) == false)) {
120
throw new Exception("Trusted cert not added");
121
}
122
X509Certificate cert2 = (X509Certificate)ks.getCertificate(alias);
123
if (cert.equals(cert2) == false) {
124
throw new Exception("KeyStore returned incorrect certificate");
125
}
126
127
ks.deleteEntry(alias);
128
if (ks.containsAlias(alias)) {
129
throw new Exception("Alias still exists: " + alias);
130
}
131
132
System.out.println("OK");
133
}
134
135
private static boolean improperNSSVersion(Provider p) {
136
double nssVersion = getNSSVersion();
137
if (p.getName().equalsIgnoreCase("SunPKCS11-NSSKeyStore")
138
&& nssVersion >= 3.28 && nssVersion < 3.35) {
139
return true;
140
}
141
142
return false;
143
}
144
}
145
146