Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/Secmod/LoadKeystore.java
38855 views
/*1* Copyright (c) 2015, 2016, 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.File;24import java.io.IOException;25import java.security.KeyStore;26import java.security.KeyStoreException;27import java.security.Provider;28import java.security.Security;29import java.security.UnrecoverableKeyException;30import java.util.Collections;3132/*33* @test34* @bug 8048622 813423235* @summary Checks that PKCS#11 keystore can't be loaded with wrong password36* @library ../37* @run main/othervm LoadKeystore38* @run main/othervm LoadKeystore sm policy39*/40public class LoadKeystore extends SecmodTest {4142public static void main(String[] args) throws Exception {43if (!initSecmod()) {44return;45}4647String configName = BASE + SEP + "nss.cfg";48Provider p = getSunPKCS11(configName);4950System.out.println("Add provider " + p);51System.out.println();52Security.addProvider(p);5354if (args.length > 1 && "sm".equals(args[0])) {55System.setProperty("java.security.policy",56BASE + File.separator + args[1]);57System.setSecurityManager(new SecurityManager());58}5960try {61System.out.println("Load keystore with wrong type");62KeyStore.getInstance("unknown", p);63throw new RuntimeException("Expected exception not thrown");64} catch(KeyStoreException e) {65System.out.println("Expected exception: " + e);66}6768KeyStore ks = KeyStore.getInstance("PKCS11", p);69if (!"PKCS11".equals(ks.getType())) {70throw new RuntimeException("Unexpected keystore type: "71+ ks.getType());72}73if (!p.equals(ks.getProvider())) {74throw new RuntimeException("Unexpected keystore provider: "75+ ks.getProvider());76}7778try {79System.out.println("Load keystore with wrong password");80ks.load(null, "wrong".toCharArray());81throw new RuntimeException("Expected exception not thrown");82} catch(IOException e) {83System.out.println("Expected exception: " + e);84Throwable cause = e.getCause();85if (!(cause instanceof UnrecoverableKeyException)) {86e.printStackTrace(System.out);87throw new RuntimeException("Unexpected cause: " + cause);88}89System.out.println("Expected cause: " + cause);90}9192System.out.println("Load keystore with correct password");93ks.load(null, password);94for (String alias : Collections.list(ks.aliases())) {95System.out.println("Alias: " + alias);96}9798System.out.println("Test passed");99}100101}102103104