Path: blob/master/test/jdk/sun/security/pkcs11/Secmod/JksSetPrivateKey.java
41153 views
/*1* Copyright (c) 2006, 2018, 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*/2223/*24* @test25* @bug 626984726* @summary store a NSS PKCS11 PrivateKeyEntry to JKS KeyStore throws confusing NPE27* @author Wang Weijun28* @library /test/lib ..29* @modules jdk.crypto.cryptoki30* @run main/othervm JksSetPrivateKey31* @run main/othervm -Djava.security.manager=allow JksSetPrivateKey sm policy32*/3334import java.io.File;35import java.security.KeyStore;36import java.security.KeyStoreException;37import java.security.PrivateKey;38import java.security.Provider;39import java.security.Security;40import java.security.cert.X509Certificate;41import java.util.Collection;42import java.util.Collections;43import java.util.TreeSet;4445public class JksSetPrivateKey extends SecmodTest {4647public static void main(String[] args) throws Exception {48if (args.length > 1 && "sm".equals(args[0])) {49System.setProperty("java.security.policy",50BASE + File.separator + args[1]);51}5253if (initSecmod() == false) {54return;55}5657String configName = BASE + SEP + "nss.cfg";58Provider p = getSunPKCS11(configName);5960System.out.println(p);61Security.addProvider(p);6263if (args.length > 1 && "sm".equals(args[0])) {64System.setSecurityManager(new SecurityManager());65}6667KeyStore ks = KeyStore.getInstance("PKCS11", p);68ks.load(null, password);69Collection<String> aliases = new TreeSet<>(Collections.list(ks.aliases()));70System.out.println("entries: " + aliases.size());71System.out.println(aliases);7273PrivateKey privateKey = (PrivateKey)ks.getKey(keyAlias, password);74System.out.println(privateKey);7576X509Certificate[] chain = (X509Certificate[])ks.getCertificateChain(keyAlias);7778KeyStore jks = KeyStore.getInstance("JKS");79jks.load(null, null);8081try {82jks.setKeyEntry("k1", privateKey, "changeit".toCharArray(), chain);83throw new Exception("No, an NSS PrivateKey shouldn't be extractable and put inside a JKS keystore");84} catch (KeyStoreException e) {85System.err.println(e); // This is OK86}8788try {89jks.setKeyEntry("k2", new DummyPrivateKey(), "changeit".toCharArray(), chain);90throw new Exception("No, non-PKCS#8 key shouldn't be put inside a KeyStore");91} catch (KeyStoreException e) {92System.err.println(e); // This is OK93}94System.out.println("OK");9596try {97jks.setKeyEntry("k3", new DummyPrivateKey2(), "changeit".toCharArray(), chain);98throw new Exception("No, not-extractble key shouldn't be put inside a KeyStore");99} catch (KeyStoreException e) {100System.err.println(e); // This is OK101}102System.out.println("OK");103}104}105106class DummyPrivateKey implements PrivateKey {107@Override108public String getAlgorithm() {109return "DUMMY";110}111112@Override113public String getFormat() {114return "DUMMY";115}116117@Override118public byte[] getEncoded() {119return "DUMMY".getBytes();120}121}122123class DummyPrivateKey2 implements PrivateKey {124@Override125public String getAlgorithm() {126return "DUMMY";127}128129@Override130public String getFormat() {131return "PKCS#8";132}133134@Override135public byte[] getEncoded() {136return null;137}138}139140141