Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/fips/ImportKeyStore.java
38855 views
/*1* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.*;24import java.util.*;2526import java.security.*;27import java.security.KeyStore.*;28import java.security.cert.*;2930/**3132This is an approximation of the process used to create the *.db files33in this directory.3435setenv LD_LIBRARY_PATH $WS/test/sun/security/pkcs11/nss/lib/solaris-sparc36modutil -create -dbdir .37modutil -changepw "NSS Internal PKCS #11 Module" -dbdir .3839$JHOME/bin/keytool -list -storetype PKCS11 -providerclass sun.security.pkcs11.SunPKCS11 -providerarg "--name=NSS\nnssSecmodDirectory=." -v -storepass test124041modutil -fips true -dbdir .4243*/4445public class ImportKeyStore {4647public static void main(String[] args) throws Exception {48String nssCfg = "--name=NSS\nnssSecmodDirectory=.\n ";49// "attributes(*,CKO_PRIVATE_KEY,CKK_DSA) = { CKA_NETSCAPE_DB = 0h00 }";50Provider p = new sun.security.pkcs11.SunPKCS11(nssCfg);5152KeyStore ks = KeyStore.getInstance("PKCS11", p);53ks.load(null, "test12".toCharArray());54System.out.println("Aliases: " + Collections.list(ks.aliases()));55System.out.println();5657char[] srcpw = "passphrase".toCharArray();58// importKeyStore("truststore", srcpw, ks);59importKeyStore("keystore", srcpw, ks);6061System.out.println("OK.");62}6364private static void importKeyStore(String filename, char[] passwd, KeyStore dstks) throws Exception {65System.out.println("Importing JKS KeyStore " + filename);66InputStream in = new FileInputStream(filename);67KeyStore srcks = KeyStore.getInstance("JKS");68srcks.load(in, passwd);69in.close();70List<String> aliases = Collections.list(srcks.aliases());71for (String alias : aliases) {72System.out.println("Alias: " + alias);73if (srcks.isCertificateEntry(alias)) {74X509Certificate cert = (X509Certificate)srcks.getCertificate(alias);75System.out.println(" Certificate: " + cert.getSubjectX500Principal());76dstks.setCertificateEntry(alias + "-cert", cert);77} else if (srcks.isKeyEntry(alias)) {78PrivateKeyEntry entry = (PrivateKeyEntry)srcks.getEntry(alias, new PasswordProtection(passwd));79System.out.println(" Key: " + entry.getPrivateKey().toString().split("\n")[0]);80dstks.setEntry(alias, entry, null);81} else {82System.out.println(" Unknown entry: " + alias);83}84}85System.out.println();86}8788}899091