author | michaelm |
Thu, 24 Oct 2013 20:39:21 +0100 | |
changeset 22339 | e91bfaf4360d |
parent 14182 | 3041082abb40 |
child 24868 | 89d9bd9eba96 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14182
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
2 |
* Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.tools; |
|
27 |
||
14182
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
28 |
import java.io.BufferedReader; |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
29 |
import java.io.File; |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
30 |
import java.io.FileInputStream; |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
31 |
import java.io.IOException; |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
32 |
import java.io.InputStreamReader; |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
33 |
|
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
34 |
import java.net.URL; |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
35 |
|
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
36 |
import java.security.KeyStore; |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
37 |
|
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
38 |
import java.text.Collator; |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
39 |
|
10369
e9d2e59e53f0
7059542: JNDI name operations should be locale independent
xuelei
parents:
5506
diff
changeset
|
40 |
import java.util.Locale; |
e9d2e59e53f0
7059542: JNDI name operations should be locale independent
xuelei
parents:
5506
diff
changeset
|
41 |
|
2 | 42 |
/** |
43 |
* <p> This class provides several utilities to <code>KeyStore</code>. |
|
44 |
* |
|
45 |
* @since 1.6.0 |
|
46 |
*/ |
|
47 |
public class KeyStoreUtil { |
|
48 |
||
49 |
private KeyStoreUtil() { |
|
50 |
// this class is not meant to be instantiated |
|
51 |
} |
|
52 |
||
14182
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
53 |
private static final String JKS = "jks"; |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
54 |
|
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
55 |
private static final Collator collator = Collator.getInstance(); |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
56 |
static { |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
57 |
// this is for case insensitive string comparisons |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
58 |
collator.setStrength(Collator.PRIMARY); |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
59 |
}; |
2 | 60 |
|
61 |
/** |
|
62 |
* Returns true if KeyStore has a password. This is true except for |
|
63 |
* MSCAPI KeyStores |
|
64 |
*/ |
|
65 |
public static boolean isWindowsKeyStore(String storetype) { |
|
66 |
return storetype.equalsIgnoreCase("Windows-MY") |
|
67 |
|| storetype.equalsIgnoreCase("Windows-ROOT"); |
|
68 |
} |
|
69 |
||
70 |
/** |
|
71 |
* Returns standard-looking names for storetype |
|
72 |
*/ |
|
73 |
public static String niceStoreTypeName(String storetype) { |
|
74 |
if (storetype.equalsIgnoreCase("Windows-MY")) { |
|
75 |
return "Windows-MY"; |
|
76 |
} else if(storetype.equalsIgnoreCase("Windows-ROOT")) { |
|
77 |
return "Windows-ROOT"; |
|
78 |
} else { |
|
10369
e9d2e59e53f0
7059542: JNDI name operations should be locale independent
xuelei
parents:
5506
diff
changeset
|
79 |
return storetype.toUpperCase(Locale.ENGLISH); |
2 | 80 |
} |
81 |
} |
|
14182
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
82 |
|
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
83 |
/** |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
84 |
* Returns the keystore with the configured CA certificates. |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
85 |
*/ |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
86 |
public static KeyStore getCacertsKeyStore() |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
87 |
throws Exception |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
88 |
{ |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
89 |
String sep = File.separator; |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
90 |
File file = new File(System.getProperty("java.home") + sep |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
91 |
+ "lib" + sep + "security" + sep |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
92 |
+ "cacerts"); |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
93 |
if (!file.exists()) { |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
94 |
return null; |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
95 |
} |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
96 |
KeyStore caks = null; |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
97 |
try (FileInputStream fis = new FileInputStream(file)) { |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
98 |
caks = KeyStore.getInstance(JKS); |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
99 |
caks.load(fis, null); |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
100 |
} |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
101 |
return caks; |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
102 |
} |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
103 |
|
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
104 |
public static char[] getPassWithModifier(String modifier, String arg, |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
105 |
java.util.ResourceBundle rb) { |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
106 |
if (modifier == null) { |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
107 |
return arg.toCharArray(); |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
108 |
} else if (collator.compare(modifier, "env") == 0) { |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
109 |
String value = System.getenv(arg); |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
110 |
if (value == null) { |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
111 |
System.err.println(rb.getString( |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
112 |
"Cannot.find.environment.variable.") + arg); |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
113 |
return null; |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
114 |
} else { |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
115 |
return value.toCharArray(); |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
116 |
} |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
117 |
} else if (collator.compare(modifier, "file") == 0) { |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
118 |
try { |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
119 |
URL url = null; |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
120 |
try { |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
121 |
url = new URL(arg); |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
122 |
} catch (java.net.MalformedURLException mue) { |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
123 |
File f = new File(arg); |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
124 |
if (f.exists()) { |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
125 |
url = f.toURI().toURL(); |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
126 |
} else { |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
127 |
System.err.println(rb.getString( |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
128 |
"Cannot.find.file.") + arg); |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
129 |
return null; |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
130 |
} |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
131 |
} |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
132 |
|
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
133 |
try (BufferedReader br = |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
134 |
new BufferedReader(new InputStreamReader( |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
135 |
url.openStream()))) { |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
136 |
String value = br.readLine(); |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
137 |
|
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
138 |
if (value == null) { |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
139 |
return new char[0]; |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
140 |
} |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
141 |
|
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
142 |
return value.toCharArray(); |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
143 |
} |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
144 |
} catch (IOException ioe) { |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
145 |
System.err.println(ioe); |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
146 |
return null; |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
147 |
} |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
148 |
} else { |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
149 |
System.err.println(rb.getString("Unknown.password.type.") + |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
150 |
modifier); |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
151 |
return null; |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
152 |
} |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10369
diff
changeset
|
153 |
} |
2 | 154 |
} |