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/fips/ImportKeyStore.java
38855 views
1
/*
2
* Copyright (c) 2005, 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
import java.io.*;
25
import java.util.*;
26
27
import java.security.*;
28
import java.security.KeyStore.*;
29
import java.security.cert.*;
30
31
/**
32
33
This is an approximation of the process used to create the *.db files
34
in this directory.
35
36
setenv LD_LIBRARY_PATH $WS/test/sun/security/pkcs11/nss/lib/solaris-sparc
37
modutil -create -dbdir .
38
modutil -changepw "NSS Internal PKCS #11 Module" -dbdir .
39
40
$JHOME/bin/keytool -list -storetype PKCS11 -providerclass sun.security.pkcs11.SunPKCS11 -providerarg "--name=NSS\nnssSecmodDirectory=." -v -storepass test12
41
42
modutil -fips true -dbdir .
43
44
*/
45
46
public class ImportKeyStore {
47
48
public static void main(String[] args) throws Exception {
49
String nssCfg = "--name=NSS\nnssSecmodDirectory=.\n ";
50
// "attributes(*,CKO_PRIVATE_KEY,CKK_DSA) = { CKA_NETSCAPE_DB = 0h00 }";
51
Provider p = new sun.security.pkcs11.SunPKCS11(nssCfg);
52
53
KeyStore ks = KeyStore.getInstance("PKCS11", p);
54
ks.load(null, "test12".toCharArray());
55
System.out.println("Aliases: " + Collections.list(ks.aliases()));
56
System.out.println();
57
58
char[] srcpw = "passphrase".toCharArray();
59
// importKeyStore("truststore", srcpw, ks);
60
importKeyStore("keystore", srcpw, ks);
61
62
System.out.println("OK.");
63
}
64
65
private static void importKeyStore(String filename, char[] passwd, KeyStore dstks) throws Exception {
66
System.out.println("Importing JKS KeyStore " + filename);
67
InputStream in = new FileInputStream(filename);
68
KeyStore srcks = KeyStore.getInstance("JKS");
69
srcks.load(in, passwd);
70
in.close();
71
List<String> aliases = Collections.list(srcks.aliases());
72
for (String alias : aliases) {
73
System.out.println("Alias: " + alias);
74
if (srcks.isCertificateEntry(alias)) {
75
X509Certificate cert = (X509Certificate)srcks.getCertificate(alias);
76
System.out.println(" Certificate: " + cert.getSubjectX500Principal());
77
dstks.setCertificateEntry(alias + "-cert", cert);
78
} else if (srcks.isKeyEntry(alias)) {
79
PrivateKeyEntry entry = (PrivateKeyEntry)srcks.getEntry(alias, new PasswordProtection(passwd));
80
System.out.println(" Key: " + entry.getPrivateKey().toString().split("\n")[0]);
81
dstks.setEntry(alias, entry, null);
82
} else {
83
System.out.println(" Unknown entry: " + alias);
84
}
85
}
86
System.out.println();
87
}
88
89
}
90
91