Path: blob/master/test/jdk/sun/security/pkcs11/Secmod/LoadKeystore.java
41153 views
/*1* Copyright (c) 2015, 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 8048622 813423226* @summary Checks that PKCS#11 keystore can't be loaded with wrong password27* @library /test/lib ../28* @modules jdk.crypto.cryptoki29* @run main/othervm LoadKeystore30* @run main/othervm -Djava.security.manager=allow LoadKeystore sm policy31*/3233import java.io.File;34import java.io.IOException;35import java.security.KeyStore;36import java.security.KeyStoreException;37import java.security.Provider;38import java.security.Security;39import java.security.UnrecoverableKeyException;40import java.util.Collections;4142public class LoadKeystore extends SecmodTest {4344public static void main(String[] args) throws Exception {45if (args.length > 1 && "sm".equals(args[0])) {46System.setProperty("java.security.policy",47BASE + File.separator + args[1]);48}4950if (!initSecmod()) {51return;52}5354String configName = BASE + SEP + "nss.cfg";55Provider p = getSunPKCS11(configName);5657System.out.println("Add provider " + p);58System.out.println();59Security.addProvider(p);6061if (args.length > 1 && "sm".equals(args[0])) {62System.setSecurityManager(new SecurityManager());63}6465try {66System.out.println("Load keystore with wrong type");67KeyStore.getInstance("unknown", p);68throw new RuntimeException("Expected exception not thrown");69} catch(KeyStoreException e) {70System.out.println("Expected exception: " + e);71}7273KeyStore ks = KeyStore.getInstance("PKCS11", p);74if (!"PKCS11".equals(ks.getType())) {75throw new RuntimeException("Unexpected keystore type: "76+ ks.getType());77}78if (!p.equals(ks.getProvider())) {79throw new RuntimeException("Unexpected keystore provider: "80+ ks.getProvider());81}8283try {84System.out.println("Load keystore with wrong password");85ks.load(null, "wrong".toCharArray());86throw new RuntimeException("Expected exception not thrown");87} catch(IOException e) {88System.out.println("Expected exception: " + e);89Throwable cause = e.getCause();90if (!(cause instanceof UnrecoverableKeyException)) {91e.printStackTrace(System.out);92throw new RuntimeException("Unexpected cause: " + cause);93}94System.out.println("Expected cause: " + cause);95}9697System.out.println("Load keystore with correct password");98ks.load(null, password);99for (String alias : Collections.list(ks.aliases())) {100System.out.println("Alias: " + alias);101}102103System.out.println("Test passed");104}105106}107108109