blob: 6bcee08561b41ae0477beeeda04b85b736ff6399 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/**
25 * @test
26 * @bug 6236533
27 * @summary verify that JKS throws the correct exception if an incorrect password is specified
28 * @author Andreas Sterbenz
29 */
30
31import java.io.*;
32import java.util.*;
33
34import java.security.*;
35import java.security.KeyStore.*;
36
37public class WrongPassword {
38
39 private final static String BASE = System.getProperty("test.src", ".");
40
41 public static void main(String[] args) throws Exception {
42 File ksFile = new File(BASE, "pw.jks");
43 char[] pw = "test12".toCharArray();
44 char[] wrongPW = "foobar".toCharArray();
45 String alias = "mykey";
46 String otherAlias = "foo";
47
48 KeyStore ks;
49 InputStream in;
50 Entry entry;
51
52 ks = KeyStore.getInstance("JKS");
53 in = new FileInputStream(ksFile);
54 ks.load(in, null);
55 in.close();
56 System.out.println(Collections.list(ks.aliases()));
57
58 ks = KeyStore.getInstance("JKS");
59 in = new FileInputStream(ksFile);
60 try {
61 ks.load(in, wrongPW);
62 } catch (IOException e) {
63 e.printStackTrace();
64 Throwable cause = e.getCause();
65 if (cause instanceof UnrecoverableKeyException == false) {
66 throw new Exception("not an UnrecoverableKeyException: " + cause);
67 }
68 }
69 in.close();
70
71 ks = KeyStore.getInstance("JKS");
72 in = new FileInputStream(ksFile);
73 ks.load(in, pw);
74 in.close();
75 System.out.println(Collections.list(ks.aliases()));
76
77 try {
78 entry = ks.getEntry(alias, null);
79 throw new Exception("no exception");
80 } catch (UnrecoverableKeyException e) {
81 System.out.println(e);
82 }
83
84 try {
85 entry = ks.getEntry(alias, new PasswordProtection(wrongPW));
86 throw new Exception("no exception");
87 } catch (UnrecoverableKeyException e) {
88 System.out.println(e);
89 }
90
91 try {
92 entry = ks.getEntry(alias, new PasswordProtection(new char[0]));
93 throw new Exception("no exception");
94 } catch (UnrecoverableKeyException e) {
95 System.out.println(e);
96 }
97
98 entry = ks.getEntry(alias, new PasswordProtection(pw));
99 System.out.println(entry.toString().split("\n")[0]);
100
101 try {
102 ks.getKey(alias, null);
103 throw new Exception("no exception");
104 } catch (UnrecoverableKeyException e) {
105 System.out.println(e);
106 }
107
108 try {
109 ks.getKey(alias, wrongPW);
110 throw new Exception("no exception");
111 } catch (UnrecoverableKeyException e) {
112 System.out.println(e);
113 }
114
115 try {
116 ks.getKey(alias, new char[0]);
117 throw new Exception("no exception");
118 } catch (UnrecoverableKeyException e) {
119 System.out.println(e);
120 }
121
122 Key k = ks.getKey(alias, pw);
123 System.out.println(k.toString().split("\n")[0]);
124
125 System.out.println(ks.getEntry(otherAlias, null));
126 System.out.println(ks.getEntry(otherAlias, new PasswordProtection(wrongPW)));
127 System.out.println(ks.getKey(otherAlias, null));
128 System.out.println(ks.getKey(otherAlias, wrongPW));
129
130 System.out.println("OK");
131 }
132}