Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/Secmod/AddTrustedCert.java
38855 views
1
/*
2
* Copyright (c) 2005, 2016, 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 ..
30
* @run main/othervm AddTrustedCert
31
* @run main/othervm AddTrustedCert sm policy
32
*/
33
34
import java.io.File;
35
import java.io.FileInputStream;
36
import java.io.InputStream;
37
import java.security.KeyStore;
38
import java.security.KeyStore.TrustedCertificateEntry;
39
import java.security.Provider;
40
import java.security.Security;
41
import java.security.cert.CertificateFactory;
42
import java.security.cert.X509Certificate;
43
import java.util.Collection;
44
import java.util.Collections;
45
import java.util.TreeSet;
46
47
public class AddTrustedCert extends SecmodTest {
48
49
public static void main(String[] args) throws Exception {
50
if (initSecmod() == false) {
51
return;
52
}
53
54
X509Certificate cert;
55
try (InputStream in = new FileInputStream(BASE + SEP + "anchor.cer")) {
56
CertificateFactory factory =
57
CertificateFactory.getInstance("X.509");
58
cert = (X509Certificate)factory.generateCertificate(in);
59
}
60
61
String configName = BASE + SEP + "nss.cfg";
62
Provider p = getSunPKCS11(configName);
63
64
System.out.println(p);
65
Security.addProvider(p);
66
67
if (args.length > 1 && "sm".equals(args[0])) {
68
System.setProperty("java.security.policy",
69
BASE + File.separator + args[1]);
70
System.setSecurityManager(new SecurityManager());
71
}
72
73
KeyStore ks = KeyStore.getInstance(PKCS11, p);
74
ks.load(null, password);
75
Collection<String> aliases = new TreeSet<>(Collections.list(
76
ks.aliases()));
77
System.out.println("entries: " + aliases.size());
78
System.out.println(aliases);
79
int size1 = aliases.size();
80
81
String alias = "anchor";
82
if (ks.containsAlias(alias)) {
83
throw new Exception("Alias exists: " + alias);
84
}
85
86
ks.setCertificateEntry(alias, cert);
87
KeyStore.Entry first = ks.getEntry(alias, null);
88
System.out.println("first entry = " + first);
89
if (!ks.entryInstanceOf(alias, TrustedCertificateEntry.class)) {
90
throw new Exception("Unexpected first entry type: " + first);
91
}
92
93
ks.setCertificateEntry(alias, cert);
94
KeyStore.Entry second = ks.getEntry(alias, null);
95
System.out.println("second entry = " + second);
96
if (!ks.entryInstanceOf(alias, TrustedCertificateEntry.class)) {
97
throw new Exception("Unexpected second entry type: "
98
+ second);
99
}
100
101
aliases = new TreeSet<>(Collections.list(ks.aliases()));
102
System.out.println("entries: " + aliases.size());
103
System.out.println(aliases);
104
int size2 = aliases.size();
105
106
if ((size2 != size1 + 1) || (aliases.contains(alias) == false)) {
107
throw new Exception("Trusted cert not added");
108
}
109
X509Certificate cert2 = (X509Certificate)ks.getCertificate(alias);
110
if (cert.equals(cert2) == false) {
111
throw new Exception("KeyStore returned incorrect certificate");
112
}
113
114
ks.deleteEntry(alias);
115
if (ks.containsAlias(alias)) {
116
throw new Exception("Alias still exists: " + alias);
117
}
118
119
System.out.println("OK");
120
}
121
122
}
123
124