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/A/**
0N/A * @test
0N/A * @bug 6236533
0N/A * @summary verify that JKS throws the correct exception if an incorrect password is specified
0N/A * @author Andreas Sterbenz
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/A
0N/Aimport java.security.*;
0N/Aimport java.security.KeyStore.*;
0N/A
0N/Apublic class WrongPassword {
0N/A
0N/A private final static String BASE = System.getProperty("test.src", ".");
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A File ksFile = new File(BASE, "pw.jks");
0N/A char[] pw = "test12".toCharArray();
0N/A char[] wrongPW = "foobar".toCharArray();
0N/A String alias = "mykey";
0N/A String otherAlias = "foo";
0N/A
0N/A KeyStore ks;
0N/A InputStream in;
0N/A Entry entry;
0N/A
0N/A ks = KeyStore.getInstance("JKS");
0N/A in = new FileInputStream(ksFile);
0N/A ks.load(in, null);
0N/A in.close();
0N/A System.out.println(Collections.list(ks.aliases()));
0N/A
0N/A ks = KeyStore.getInstance("JKS");
0N/A in = new FileInputStream(ksFile);
0N/A try {
0N/A ks.load(in, wrongPW);
0N/A } catch (IOException e) {
0N/A e.printStackTrace();
0N/A Throwable cause = e.getCause();
0N/A if (cause instanceof UnrecoverableKeyException == false) {
0N/A throw new Exception("not an UnrecoverableKeyException: " + cause);
0N/A }
0N/A }
0N/A in.close();
0N/A
0N/A ks = KeyStore.getInstance("JKS");
0N/A in = new FileInputStream(ksFile);
0N/A ks.load(in, pw);
0N/A in.close();
0N/A System.out.println(Collections.list(ks.aliases()));
0N/A
0N/A try {
0N/A entry = ks.getEntry(alias, null);
0N/A throw new Exception("no exception");
0N/A } catch (UnrecoverableKeyException e) {
0N/A System.out.println(e);
0N/A }
0N/A
0N/A try {
0N/A entry = ks.getEntry(alias, new PasswordProtection(wrongPW));
0N/A throw new Exception("no exception");
0N/A } catch (UnrecoverableKeyException e) {
0N/A System.out.println(e);
0N/A }
0N/A
0N/A try {
0N/A entry = ks.getEntry(alias, new PasswordProtection(new char[0]));
0N/A throw new Exception("no exception");
0N/A } catch (UnrecoverableKeyException e) {
0N/A System.out.println(e);
0N/A }
0N/A
0N/A entry = ks.getEntry(alias, new PasswordProtection(pw));
0N/A System.out.println(entry.toString().split("\n")[0]);
0N/A
0N/A try {
0N/A ks.getKey(alias, null);
0N/A throw new Exception("no exception");
0N/A } catch (UnrecoverableKeyException e) {
0N/A System.out.println(e);
0N/A }
0N/A
0N/A try {
0N/A ks.getKey(alias, wrongPW);
0N/A throw new Exception("no exception");
0N/A } catch (UnrecoverableKeyException e) {
0N/A System.out.println(e);
0N/A }
0N/A
0N/A try {
0N/A ks.getKey(alias, new char[0]);
0N/A throw new Exception("no exception");
0N/A } catch (UnrecoverableKeyException e) {
0N/A System.out.println(e);
0N/A }
0N/A
0N/A Key k = ks.getKey(alias, pw);
0N/A System.out.println(k.toString().split("\n")[0]);
0N/A
0N/A System.out.println(ks.getEntry(otherAlias, null));
0N/A System.out.println(ks.getEntry(otherAlias, new PasswordProtection(wrongPW)));
0N/A System.out.println(ks.getKey(otherAlias, null));
0N/A System.out.println(ks.getKey(otherAlias, wrongPW));
0N/A
0N/A System.out.println("OK");
0N/A }
0N/A}