0N/A/*
2362N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/A
0N/Aimport java.security.*;
0N/Aimport java.security.KeyStore.*;
0N/Aimport java.security.cert.*;
0N/A
0N/A/**
0N/A
0N/AThis is an approximation of the process used to create the *.db files
0N/Ain this directory.
0N/A
0N/Asetenv LD_LIBRARY_PATH $WS/test/sun/security/pkcs11/nss/lib/solaris-sparc
0N/Amodutil -create -dbdir .
0N/Amodutil -changepw "NSS Internal PKCS #11 Module" -dbdir .
0N/A
0N/A$JHOME/bin/keytool -list -storetype PKCS11 -providerclass sun.security.pkcs11.SunPKCS11 -providerarg "--name=NSS\nnssSecmodDirectory=." -v -storepass test12
0N/A
0N/Amodutil -fips true -dbdir .
0N/A
0N/A*/
0N/A
0N/Apublic class ImportKeyStore {
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A String nssCfg = "--name=NSS\nnssSecmodDirectory=.\n ";
0N/A// "attributes(*,CKO_PRIVATE_KEY,CKK_DSA) = { CKA_NETSCAPE_DB = 0h00 }";
0N/A Provider p = new sun.security.pkcs11.SunPKCS11(nssCfg);
0N/A
0N/A KeyStore ks = KeyStore.getInstance("PKCS11", p);
0N/A ks.load(null, "test12".toCharArray());
0N/A System.out.println("Aliases: " + Collections.list(ks.aliases()));
0N/A System.out.println();
0N/A
0N/A char[] srcpw = "passphrase".toCharArray();
0N/A// importKeyStore("truststore", srcpw, ks);
0N/A importKeyStore("keystore", srcpw, ks);
0N/A
0N/A System.out.println("OK.");
0N/A }
0N/A
0N/A private static void importKeyStore(String filename, char[] passwd, KeyStore dstks) throws Exception {
0N/A System.out.println("Importing JKS KeyStore " + filename);
0N/A InputStream in = new FileInputStream(filename);
0N/A KeyStore srcks = KeyStore.getInstance("JKS");
0N/A srcks.load(in, passwd);
0N/A in.close();
0N/A List<String> aliases = Collections.list(srcks.aliases());
0N/A for (String alias : aliases) {
0N/A System.out.println("Alias: " + alias);
0N/A if (srcks.isCertificateEntry(alias)) {
0N/A X509Certificate cert = (X509Certificate)srcks.getCertificate(alias);
0N/A System.out.println(" Certificate: " + cert.getSubjectX500Principal());
0N/A dstks.setCertificateEntry(alias + "-cert", cert);
0N/A } else if (srcks.isKeyEntry(alias)) {
0N/A PrivateKeyEntry entry = (PrivateKeyEntry)srcks.getEntry(alias, new PasswordProtection(passwd));
0N/A System.out.println(" Key: " + entry.getPrivateKey().toString().split("\n")[0]);
0N/A dstks.setEntry(alias, entry, null);
0N/A } else {
0N/A System.out.println(" Unknown entry: " + alias);
0N/A }
0N/A }
0N/A System.out.println();
0N/A }
0N/A
0N/A}