Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/pkcs11/Secmod/GetPrivateKey.java
41153 views
1
/*
2
* Copyright (c) 2005, 2018, 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 6273877 6322208 6275523
27
* @summary make sure we can access the NSS softtoken KeyStore
28
* and use a private key
29
* @author Andreas Sterbenz
30
* @library /test/lib ..
31
* @modules jdk.crypto.cryptoki
32
* @run main/othervm GetPrivateKey
33
* @run main/othervm -Djava.security.manager=allow GetPrivateKey sm policy
34
*/
35
36
import java.io.File;
37
import java.security.KeyStore;
38
import java.security.PrivateKey;
39
import java.security.Provider;
40
import java.security.Security;
41
import java.security.Signature;
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 GetPrivateKey extends SecmodTest {
48
49
public static void main(String[] args) throws Exception {
50
if (args.length > 1 && "sm".equals(args[0])) {
51
System.setProperty("java.security.policy",
52
BASE + File.separator + args[1]);
53
}
54
55
if (initSecmod() == false) {
56
return;
57
}
58
59
String configName = BASE + SEP + "nss.cfg";
60
Provider p = getSunPKCS11(configName);
61
62
System.out.println(p);
63
Security.addProvider(p);
64
65
if (args.length > 1 && "sm".equals(args[0])) {
66
System.setSecurityManager(new SecurityManager());
67
}
68
69
KeyStore ks = KeyStore.getInstance(PKCS11, p);
70
ks.load(null, password);
71
Collection<String> aliases = new TreeSet<>(
72
Collections.list(ks.aliases()));
73
System.out.println("entries: " + aliases.size());
74
System.out.println(aliases);
75
76
PrivateKey privateKey = (PrivateKey)ks.getKey(keyAlias, password);
77
System.out.println(privateKey);
78
79
byte[] data = generateData(1024);
80
81
System.out.println("Signing...");
82
Signature signature = Signature.getInstance("MD5withRSA");
83
signature.initSign(privateKey);
84
signature.update(data);
85
byte[] sig = signature.sign();
86
87
X509Certificate[] chain =
88
(X509Certificate[]) ks.getCertificateChain(keyAlias);
89
signature.initVerify(chain[0].getPublicKey());
90
signature.update(data);
91
boolean ok = signature.verify(sig);
92
if (ok == false) {
93
throw new Exception("Signature verification error");
94
}
95
96
System.out.println("OK");
97
98
}
99
100
}
101
102