blob: 0874f7c0a3a06308735867378a6f634c8e9d1b48 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24import java.io.*;
25import java.util.*;
26
27import java.security.*;
28import java.security.KeyStore.*;
29import java.security.cert.*;
30
31/**
32
33This is an approximation of the process used to create the *.db files
34in this directory.
35
36setenv LD_LIBRARY_PATH $WS/test/sun/security/pkcs11/nss/lib/solaris-sparc
37modutil -create -dbdir .
38modutil -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
42modutil -fips true -dbdir .
43
44*/
45
46public 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}