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/TestNssDbSqlite.java
38855 views
1
/*
2
* Copyright (c) 2017, Red Hat, Inc. and/or its affiliates.
3
*
4
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
*
6
* This code is free software; you can redistribute it and/or modify it
7
* under the terms of the GNU General Public License version 2 only, as
8
* published by the Free Software Foundation.
9
*
10
* This code is distributed in the hope that it will be useful, but WITHOUT
11
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13
* version 2 for more details (a copy is included in the LICENSE file that
14
* accompanied this code).
15
*
16
* You should have received a copy of the GNU General Public License version
17
* 2 along with this work; if not, write to the Free Software Foundation,
18
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
*
20
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21
* or visit www.oracle.com if you need additional information or have any
22
* questions.
23
*/
24
25
/*
26
* @test
27
* @bug 8165996
28
* @summary Test NSS DB Sqlite
29
* @comment There is no NSS on Aix.
30
* @requires os.family != "aix"
31
* @library ../
32
* @run main/othervm/timeout=120 TestNssDbSqlite
33
* @author Martin Balao ([email protected])
34
*/
35
36
import java.security.PrivateKey;
37
import java.security.cert.Certificate;
38
import java.security.KeyStore;
39
import java.security.Provider;
40
import java.security.Signature;
41
42
import sun.security.rsa.SunRsaSign;
43
import sun.security.jca.ProviderList;
44
import sun.security.jca.Providers;
45
import sun.security.tools.keytool.CertAndKeyGen;
46
import sun.security.x509.X500Name;
47
48
public final class TestNssDbSqlite extends SecmodTest {
49
50
private static final boolean enableDebug = true;
51
52
private static Provider sunPKCS11NSSProvider;
53
private static Provider sunRsaSignProvider;
54
private static Provider sunJCEProvider;
55
private static KeyStore ks;
56
private static char[] passphrase = "test12".toCharArray();
57
private static PrivateKey privateKey;
58
private static Certificate certificate;
59
60
public static void main(String[] args) throws Exception {
61
62
initialize();
63
64
if (enableDebug) {
65
System.out.println("SunPKCS11 provider: " +
66
sunPKCS11NSSProvider);
67
}
68
69
testRetrieveKeysFromKeystore();
70
71
System.out.println("Test PASS - OK");
72
}
73
74
private static void testRetrieveKeysFromKeystore() throws Exception {
75
76
String plainText = "known plain text";
77
78
ks.setKeyEntry("root_ca_1", privateKey, passphrase,
79
new Certificate[]{certificate});
80
PrivateKey k1 = (PrivateKey) ks.getKey("root_ca_1", passphrase);
81
82
Signature sS = Signature.getInstance(
83
"SHA256withRSA", sunPKCS11NSSProvider);
84
sS.initSign(k1);
85
sS.update(plainText.getBytes());
86
byte[] generatedSignature = sS.sign();
87
88
if (enableDebug) {
89
System.out.println("Generated signature: ");
90
for (byte b : generatedSignature) {
91
System.out.printf("0x%02x, ", (int)(b) & 0xFF);
92
}
93
System.out.println("");
94
}
95
96
Signature sV = Signature.getInstance("SHA256withRSA", sunRsaSignProvider);
97
sV.initVerify(certificate);
98
sV.update(plainText.getBytes());
99
if(!sV.verify(generatedSignature)){
100
throw new Exception("Couldn't verify signature");
101
}
102
}
103
104
private static void initialize() throws Exception {
105
initializeProvider();
106
}
107
108
private static void initializeProvider () throws Exception {
109
useSqlite(true);
110
if (!initSecmod()) {
111
return;
112
}
113
114
sunPKCS11NSSProvider = getSunPKCS11(BASE + SEP + "nss-sqlite.cfg");
115
sunJCEProvider = new com.sun.crypto.provider.SunJCE();
116
sunRsaSignProvider = new SunRsaSign();
117
Providers.setProviderList(ProviderList.newList(
118
sunJCEProvider, sunPKCS11NSSProvider,
119
new sun.security.provider.Sun(), sunRsaSignProvider));
120
121
ks = KeyStore.getInstance("PKCS11-NSS-Sqlite", sunPKCS11NSSProvider);
122
ks.load(null, passphrase);
123
124
CertAndKeyGen gen = new CertAndKeyGen("RSA", "SHA256withRSA");
125
gen.generate(2048);
126
privateKey = gen.getPrivateKey();
127
certificate = gen.getSelfCertificate(new X500Name("CN=Me"), 365);
128
}
129
}
130
131