author | vinnie |
Mon, 13 Aug 2012 14:06:44 +0100 | |
changeset 13565 | 6298d600104f |
parent 13250 | c342d30b70da |
child 19066 | daf668acb10e |
permissions | -rw-r--r-- |
2 | 1 |
/* |
12294
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
2 |
* Copyright (c) 2003, 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 |
|
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 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
24 |
||
25 |
// common infrastructure for SunPKCS11 tests |
|
26 |
||
27 |
import java.io.*; |
|
28 |
import java.util.*; |
|
29 |
import java.lang.reflect.*; |
|
30 |
||
31 |
import java.security.*; |
|
32 |
||
33 |
public abstract class PKCS11Test { |
|
34 |
||
35 |
// directory of the test source |
|
36 |
static final String BASE = System.getProperty("test.src", "."); |
|
37 |
||
38 |
static final char SEP = File.separatorChar; |
|
39 |
||
40 |
private final static String REL_CLOSED = "../../../../closed/sun/security/pkcs11".replace('/', SEP); |
|
41 |
||
42 |
// directory corresponding to BASE in the /closed hierarchy |
|
43 |
static final String CLOSED_BASE; |
|
44 |
||
45 |
static { |
|
46 |
// hack |
|
47 |
String absBase = new File(BASE).getAbsolutePath(); |
|
48 |
int k = absBase.indexOf(SEP + "test" + SEP + "sun" + SEP); |
|
49 |
if (k < 0) k = 0; |
|
50 |
String p1 = absBase.substring(0, k + 6); |
|
51 |
String p2 = absBase.substring(k + 5); |
|
52 |
CLOSED_BASE = p1 + "closed" + p2; |
|
53 |
} |
|
54 |
||
55 |
static String NSPR_PREFIX = ""; |
|
56 |
||
57 |
static Provider getSunPKCS11(String config) throws Exception { |
|
58 |
Class clazz = Class.forName("sun.security.pkcs11.SunPKCS11"); |
|
59 |
Constructor cons = clazz.getConstructor(new Class[] {String.class}); |
|
60 |
Object obj = cons.newInstance(new Object[] {config}); |
|
61 |
return (Provider)obj; |
|
62 |
} |
|
63 |
||
64 |
public abstract void main(Provider p) throws Exception; |
|
65 |
||
66 |
private void premain(Provider p) throws Exception { |
|
67 |
long start = System.currentTimeMillis(); |
|
68 |
System.out.println("Running test with provider " + p.getName() + "..."); |
|
69 |
main(p); |
|
70 |
long stop = System.currentTimeMillis(); |
|
71 |
System.out.println("Completed test with provider " + p.getName() + " (" + (stop - start) + " ms)."); |
|
72 |
} |
|
73 |
||
74 |
public static void main(PKCS11Test test) throws Exception { |
|
10328 | 75 |
Provider[] oldProviders = Security.getProviders(); |
76 |
try { |
|
77 |
System.out.println("Beginning test run " + test.getClass().getName() + "..."); |
|
78 |
testDefault(test); |
|
79 |
testNSS(test); |
|
80 |
testDeimos(test); |
|
81 |
} finally { |
|
82 |
Provider[] newProviders = Security.getProviders(); |
|
83 |
// Do not restore providers if nothing changed. This is especailly |
|
84 |
// useful for ./Provider/Login.sh, where a SecurityManager exists. |
|
85 |
if (oldProviders.length == newProviders.length) { |
|
86 |
boolean found = false; |
|
87 |
for (int i = 0; i<oldProviders.length; i++) { |
|
88 |
if (oldProviders[i] != newProviders[i]) { |
|
89 |
found = true; |
|
90 |
break; |
|
91 |
} |
|
92 |
} |
|
93 |
if (!found) return; |
|
94 |
} |
|
95 |
for (Provider p: newProviders) { |
|
96 |
Security.removeProvider(p.getName()); |
|
97 |
} |
|
98 |
for (Provider p: oldProviders) { |
|
99 |
Security.addProvider(p); |
|
100 |
} |
|
101 |
} |
|
2 | 102 |
} |
103 |
||
104 |
public static void testDeimos(PKCS11Test test) throws Exception { |
|
105 |
if (new File("/opt/SUNWconn/lib/libpkcs11.so").isFile() == false || |
|
106 |
"true".equals(System.getProperty("NO_DEIMOS"))) { |
|
107 |
return; |
|
108 |
} |
|
109 |
String base = getBase(); |
|
110 |
String p11config = base + SEP + "nss" + SEP + "p11-deimos.txt"; |
|
111 |
Provider p = getSunPKCS11(p11config); |
|
112 |
test.premain(p); |
|
113 |
} |
|
114 |
||
115 |
public static void testDefault(PKCS11Test test) throws Exception { |
|
116 |
// run test for default configured PKCS11 providers (if any) |
|
117 |
||
118 |
if ("true".equals(System.getProperty("NO_DEFAULT"))) { |
|
119 |
return; |
|
120 |
} |
|
121 |
||
122 |
Provider[] providers = Security.getProviders(); |
|
123 |
for (int i = 0; i < providers.length; i++) { |
|
124 |
Provider p = providers[i]; |
|
125 |
if (p.getName().startsWith("SunPKCS11-")) { |
|
126 |
test.premain(p); |
|
127 |
} |
|
128 |
} |
|
129 |
} |
|
130 |
||
131 |
private static String PKCS11_BASE; |
|
12294
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
132 |
static { |
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
133 |
try { |
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
134 |
PKCS11_BASE = getBase(); |
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
135 |
} catch (Exception e) { |
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
136 |
// ignore |
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
137 |
} |
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
138 |
} |
2 | 139 |
|
140 |
private final static String PKCS11_REL_PATH = "sun/security/pkcs11"; |
|
141 |
||
142 |
public static String getBase() throws Exception { |
|
143 |
if (PKCS11_BASE != null) { |
|
144 |
return PKCS11_BASE; |
|
145 |
} |
|
146 |
File cwd = new File(System.getProperty("test.src", ".")).getCanonicalFile(); |
|
147 |
while (true) { |
|
148 |
File file = new File(cwd, "TEST.ROOT"); |
|
149 |
if (file.isFile()) { |
|
150 |
break; |
|
151 |
} |
|
152 |
cwd = cwd.getParentFile(); |
|
153 |
if (cwd == null) { |
|
154 |
throw new Exception("Test root directory not found"); |
|
155 |
} |
|
156 |
} |
|
157 |
PKCS11_BASE = new File(cwd, PKCS11_REL_PATH.replace('/', SEP)).getAbsolutePath(); |
|
158 |
return PKCS11_BASE; |
|
159 |
} |
|
160 |
||
161 |
public static String getNSSLibDir() throws Exception { |
|
162 |
Properties props = System.getProperties(); |
|
163 |
String osName = props.getProperty("os.name"); |
|
164 |
if (osName.startsWith("Win")) { |
|
165 |
osName = "Windows"; |
|
166 |
NSPR_PREFIX = "lib"; |
|
167 |
} |
|
168 |
String osid = osName + "-" |
|
169 |
+ props.getProperty("os.arch") + "-" + props.getProperty("sun.arch.data.model"); |
|
13565
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
170 |
String[] nssLibDirs = osMap.get(osid); |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
171 |
if (nssLibDirs == null) { |
2 | 172 |
System.out.println("Unsupported OS, skipping: " + osid); |
173 |
return null; |
|
174 |
} |
|
13565
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
175 |
if (nssLibDirs.length == 0) { |
2 | 176 |
System.out.println("NSS not supported on this platform, skipping test"); |
177 |
return null; |
|
178 |
} |
|
13565
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
179 |
String nssLibDir = null; |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
180 |
for (String dir : nssLibDirs) { |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
181 |
if (new File(dir).exists()) { |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
182 |
nssLibDir = dir; |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
183 |
System.setProperty("pkcs11test.nss.libdir", nssLibDir); |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
184 |
break; |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
185 |
} |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
186 |
} |
12294
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
187 |
return nssLibDir; |
2 | 188 |
} |
189 |
||
10328 | 190 |
protected static void safeReload(String lib) throws Exception { |
191 |
try { |
|
192 |
System.load(lib); |
|
193 |
} catch (UnsatisfiedLinkError e) { |
|
194 |
if (e.getMessage().contains("already loaded")) { |
|
195 |
return; |
|
196 |
} |
|
197 |
} |
|
198 |
} |
|
199 |
||
2 | 200 |
static boolean loadNSPR(String libdir) throws Exception { |
201 |
// load NSS softoken dependencies in advance to avoid resolver issues |
|
10328 | 202 |
safeReload(libdir + System.mapLibraryName(NSPR_PREFIX + "nspr4")); |
203 |
safeReload(libdir + System.mapLibraryName(NSPR_PREFIX + "plc4")); |
|
204 |
safeReload(libdir + System.mapLibraryName(NSPR_PREFIX + "plds4")); |
|
12294
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
205 |
safeReload(libdir + System.mapLibraryName("sqlite3")); |
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
206 |
safeReload(libdir + System.mapLibraryName("nssutil3")); |
2 | 207 |
return true; |
208 |
} |
|
209 |
||
210 |
public static void testNSS(PKCS11Test test) throws Exception { |
|
211 |
String libdir = getNSSLibDir(); |
|
212 |
if (libdir == null) { |
|
213 |
return; |
|
214 |
} |
|
215 |
String base = getBase(); |
|
216 |
||
217 |
if (loadNSPR(libdir) == false) { |
|
218 |
return; |
|
219 |
} |
|
220 |
||
221 |
String libfile = libdir + System.mapLibraryName("softokn3"); |
|
222 |
||
223 |
String customDBdir = System.getProperty("CUSTOM_DB_DIR"); |
|
224 |
String dbdir = (customDBdir != null) ? |
|
225 |
customDBdir : |
|
226 |
base + SEP + "nss" + SEP + "db"; |
|
227 |
// NSS always wants forward slashes for the config path |
|
228 |
dbdir = dbdir.replace('\\', '/'); |
|
229 |
||
230 |
String customConfig = System.getProperty("CUSTOM_P11_CONFIG"); |
|
231 |
String customConfigName = System.getProperty("CUSTOM_P11_CONFIG_NAME", "p11-nss.txt"); |
|
232 |
String p11config = (customConfig != null) ? |
|
233 |
customConfig : |
|
234 |
base + SEP + "nss" + SEP + customConfigName; |
|
235 |
||
236 |
System.setProperty("pkcs11test.nss.lib", libfile); |
|
237 |
System.setProperty("pkcs11test.nss.db", dbdir); |
|
238 |
Provider p = getSunPKCS11(p11config); |
|
239 |
test.premain(p); |
|
240 |
} |
|
241 |
||
242 |
||
13565
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
243 |
private static final Map<String,String[]> osMap; |
2 | 244 |
|
12294
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
245 |
// Location of the NSS libraries on each supported platform |
2 | 246 |
static { |
13565
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
247 |
osMap = new HashMap<String,String[]>(); |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
248 |
osMap.put("SunOS-sparc-32", new String[]{"/usr/lib/mps/"}); |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
249 |
osMap.put("SunOS-sparcv9-64", new String[]{"/usr/lib/mps/64/"}); |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
250 |
osMap.put("SunOS-x86-32", new String[]{"/usr/lib/mps/"}); |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
251 |
osMap.put("SunOS-amd64-64", new String[]{"/usr/lib/mps/64/"}); |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
252 |
osMap.put("Linux-i386-32", new String[]{ |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
253 |
"/usr/lib/i386-linux-gnu/", "/usr/lib/"}); |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
254 |
osMap.put("Linux-amd64-64", new String[]{ |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
255 |
"/usr/lib/x86_64-linux-gnu/", "/usr/lib64/"}); |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
256 |
osMap.put("Windows-x86-32", new String[]{ |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
257 |
PKCS11_BASE + "/nss/lib/windows-i586/".replace('/', SEP)}); |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
258 |
osMap.put("Windows-amd64-64", new String[]{ |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
259 |
PKCS11_BASE + "/nss/lib/windows-amd64/".replace('/', SEP)}); |
2 | 260 |
} |
261 |
||
262 |
private final static char[] hexDigits = "0123456789abcdef".toCharArray(); |
|
263 |
||
264 |
public static String toString(byte[] b) { |
|
265 |
if (b == null) { |
|
266 |
return "(null)"; |
|
267 |
} |
|
268 |
StringBuffer sb = new StringBuffer(b.length * 3); |
|
269 |
for (int i = 0; i < b.length; i++) { |
|
270 |
int k = b[i] & 0xff; |
|
271 |
if (i != 0) { |
|
272 |
sb.append(':'); |
|
273 |
} |
|
274 |
sb.append(hexDigits[k >>> 4]); |
|
275 |
sb.append(hexDigits[k & 0xf]); |
|
276 |
} |
|
277 |
return sb.toString(); |
|
278 |
} |
|
279 |
||
280 |
public static byte[] parse(String s) { |
|
281 |
if (s.equals("(null)")) { |
|
282 |
return null; |
|
283 |
} |
|
284 |
try { |
|
285 |
int n = s.length(); |
|
286 |
ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3); |
|
287 |
StringReader r = new StringReader(s); |
|
288 |
while (true) { |
|
289 |
int b1 = nextNibble(r); |
|
290 |
if (b1 < 0) { |
|
291 |
break; |
|
292 |
} |
|
293 |
int b2 = nextNibble(r); |
|
294 |
if (b2 < 0) { |
|
295 |
throw new RuntimeException("Invalid string " + s); |
|
296 |
} |
|
297 |
int b = (b1 << 4) | b2; |
|
298 |
out.write(b); |
|
299 |
} |
|
300 |
return out.toByteArray(); |
|
301 |
} catch (IOException e) { |
|
302 |
throw new RuntimeException(e); |
|
303 |
} |
|
304 |
} |
|
305 |
||
306 |
private static int nextNibble(StringReader r) throws IOException { |
|
307 |
while (true) { |
|
308 |
int ch = r.read(); |
|
309 |
if (ch == -1) { |
|
310 |
return -1; |
|
311 |
} else if ((ch >= '0') && (ch <= '9')) { |
|
312 |
return ch - '0'; |
|
313 |
} else if ((ch >= 'a') && (ch <= 'f')) { |
|
314 |
return ch - 'a' + 10; |
|
315 |
} else if ((ch >= 'A') && (ch <= 'F')) { |
|
316 |
return ch - 'A' + 10; |
|
317 |
} |
|
318 |
} |
|
319 |
} |
|
320 |
||
321 |
<T> T[] concat(T[] a, T[] b) { |
|
322 |
if ((b == null) || (b.length == 0)) { |
|
323 |
return a; |
|
324 |
} |
|
325 |
T[] r = (T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), a.length + b.length); |
|
326 |
System.arraycopy(a, 0, r, 0, a.length); |
|
327 |
System.arraycopy(b, 0, r, a.length, b.length); |
|
328 |
return r; |
|
329 |
} |
|
330 |
||
331 |
} |