author | ascarpino |
Mon, 29 Jul 2013 13:43:24 -0700 | |
changeset 19067 | 5271291b7121 |
parent 19066 | daf668acb10e |
child 21977 | 17f538f05b73 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
19066 | 2 |
* Copyright (c) 2003, 2013, 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 |
||
19067
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
57 |
// NSS version info |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
58 |
public static enum ECCState { None, Basic, Extended }; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
59 |
static double nss_version = -1; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
60 |
static ECCState nss_ecc_status = ECCState.Extended; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
61 |
|
19066 | 62 |
// The NSS library we need to search for in getNSSLibDir() |
63 |
// Default is "libsoftokn3.so", listed as "softokn3" |
|
64 |
// The other is "libnss3.so", listed as "nss3". |
|
65 |
static String nss_library = "softokn3"; |
|
66 |
||
2 | 67 |
static Provider getSunPKCS11(String config) throws Exception { |
68 |
Class clazz = Class.forName("sun.security.pkcs11.SunPKCS11"); |
|
69 |
Constructor cons = clazz.getConstructor(new Class[] {String.class}); |
|
70 |
Object obj = cons.newInstance(new Object[] {config}); |
|
71 |
return (Provider)obj; |
|
72 |
} |
|
73 |
||
74 |
public abstract void main(Provider p) throws Exception; |
|
75 |
||
76 |
private void premain(Provider p) throws Exception { |
|
77 |
long start = System.currentTimeMillis(); |
|
78 |
System.out.println("Running test with provider " + p.getName() + "..."); |
|
79 |
main(p); |
|
80 |
long stop = System.currentTimeMillis(); |
|
81 |
System.out.println("Completed test with provider " + p.getName() + " (" + (stop - start) + " ms)."); |
|
82 |
} |
|
83 |
||
84 |
public static void main(PKCS11Test test) throws Exception { |
|
10328 | 85 |
Provider[] oldProviders = Security.getProviders(); |
86 |
try { |
|
87 |
System.out.println("Beginning test run " + test.getClass().getName() + "..."); |
|
88 |
testDefault(test); |
|
89 |
testNSS(test); |
|
90 |
testDeimos(test); |
|
91 |
} finally { |
|
19066 | 92 |
// NOTE: Do not place a 'return' in any finally block |
93 |
// as it will suppress exceptions and hide test failures. |
|
10328 | 94 |
Provider[] newProviders = Security.getProviders(); |
19066 | 95 |
boolean found = true; |
10328 | 96 |
// Do not restore providers if nothing changed. This is especailly |
97 |
// useful for ./Provider/Login.sh, where a SecurityManager exists. |
|
98 |
if (oldProviders.length == newProviders.length) { |
|
19066 | 99 |
found = false; |
10328 | 100 |
for (int i = 0; i<oldProviders.length; i++) { |
101 |
if (oldProviders[i] != newProviders[i]) { |
|
102 |
found = true; |
|
103 |
break; |
|
104 |
} |
|
105 |
} |
|
106 |
} |
|
19066 | 107 |
if (found) { |
108 |
for (Provider p: newProviders) { |
|
109 |
Security.removeProvider(p.getName()); |
|
110 |
} |
|
111 |
for (Provider p: oldProviders) { |
|
112 |
Security.addProvider(p); |
|
113 |
} |
|
10328 | 114 |
} |
115 |
} |
|
2 | 116 |
} |
117 |
||
118 |
public static void testDeimos(PKCS11Test test) throws Exception { |
|
119 |
if (new File("/opt/SUNWconn/lib/libpkcs11.so").isFile() == false || |
|
120 |
"true".equals(System.getProperty("NO_DEIMOS"))) { |
|
121 |
return; |
|
122 |
} |
|
123 |
String base = getBase(); |
|
124 |
String p11config = base + SEP + "nss" + SEP + "p11-deimos.txt"; |
|
125 |
Provider p = getSunPKCS11(p11config); |
|
126 |
test.premain(p); |
|
127 |
} |
|
128 |
||
129 |
public static void testDefault(PKCS11Test test) throws Exception { |
|
130 |
// run test for default configured PKCS11 providers (if any) |
|
131 |
||
132 |
if ("true".equals(System.getProperty("NO_DEFAULT"))) { |
|
133 |
return; |
|
134 |
} |
|
135 |
||
136 |
Provider[] providers = Security.getProviders(); |
|
137 |
for (int i = 0; i < providers.length; i++) { |
|
138 |
Provider p = providers[i]; |
|
139 |
if (p.getName().startsWith("SunPKCS11-")) { |
|
140 |
test.premain(p); |
|
141 |
} |
|
142 |
} |
|
143 |
} |
|
144 |
||
145 |
private static String PKCS11_BASE; |
|
12294
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
146 |
static { |
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
147 |
try { |
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
148 |
PKCS11_BASE = getBase(); |
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
149 |
} catch (Exception e) { |
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
150 |
// ignore |
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
151 |
} |
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
152 |
} |
2 | 153 |
|
154 |
private final static String PKCS11_REL_PATH = "sun/security/pkcs11"; |
|
155 |
||
156 |
public static String getBase() throws Exception { |
|
157 |
if (PKCS11_BASE != null) { |
|
158 |
return PKCS11_BASE; |
|
159 |
} |
|
160 |
File cwd = new File(System.getProperty("test.src", ".")).getCanonicalFile(); |
|
161 |
while (true) { |
|
162 |
File file = new File(cwd, "TEST.ROOT"); |
|
163 |
if (file.isFile()) { |
|
164 |
break; |
|
165 |
} |
|
166 |
cwd = cwd.getParentFile(); |
|
167 |
if (cwd == null) { |
|
168 |
throw new Exception("Test root directory not found"); |
|
169 |
} |
|
170 |
} |
|
171 |
PKCS11_BASE = new File(cwd, PKCS11_REL_PATH.replace('/', SEP)).getAbsolutePath(); |
|
172 |
return PKCS11_BASE; |
|
173 |
} |
|
174 |
||
175 |
public static String getNSSLibDir() throws Exception { |
|
176 |
Properties props = System.getProperties(); |
|
177 |
String osName = props.getProperty("os.name"); |
|
178 |
if (osName.startsWith("Win")) { |
|
179 |
osName = "Windows"; |
|
180 |
NSPR_PREFIX = "lib"; |
|
181 |
} |
|
182 |
String osid = osName + "-" |
|
183 |
+ 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
|
184 |
String[] nssLibDirs = osMap.get(osid); |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
185 |
if (nssLibDirs == null) { |
2 | 186 |
System.out.println("Unsupported OS, skipping: " + osid); |
187 |
return null; |
|
188 |
} |
|
13565
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
189 |
if (nssLibDirs.length == 0) { |
2 | 190 |
System.out.println("NSS not supported on this platform, skipping test"); |
191 |
return null; |
|
192 |
} |
|
13565
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
193 |
String nssLibDir = null; |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
194 |
for (String dir : nssLibDirs) { |
19066 | 195 |
if (new File(dir).exists() && |
196 |
new File(dir + System.mapLibraryName(nss_library)).exists()) { |
|
13565
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
197 |
nssLibDir = dir; |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
198 |
System.setProperty("pkcs11test.nss.libdir", nssLibDir); |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
199 |
break; |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
200 |
} |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
201 |
} |
12294
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
202 |
return nssLibDir; |
2 | 203 |
} |
204 |
||
10328 | 205 |
protected static void safeReload(String lib) throws Exception { |
206 |
try { |
|
207 |
System.load(lib); |
|
208 |
} catch (UnsatisfiedLinkError e) { |
|
209 |
if (e.getMessage().contains("already loaded")) { |
|
210 |
return; |
|
211 |
} |
|
212 |
} |
|
213 |
} |
|
214 |
||
2 | 215 |
static boolean loadNSPR(String libdir) throws Exception { |
216 |
// load NSS softoken dependencies in advance to avoid resolver issues |
|
10328 | 217 |
safeReload(libdir + System.mapLibraryName(NSPR_PREFIX + "nspr4")); |
218 |
safeReload(libdir + System.mapLibraryName(NSPR_PREFIX + "plc4")); |
|
219 |
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
|
220 |
safeReload(libdir + System.mapLibraryName("sqlite3")); |
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
221 |
safeReload(libdir + System.mapLibraryName("nssutil3")); |
2 | 222 |
return true; |
223 |
} |
|
224 |
||
19067
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
225 |
// Check the provider being used is NSS |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
226 |
public static boolean isNSS(Provider p) { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
227 |
return p.getName().toUpperCase().equals("SUNPKCS11-NSS"); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
228 |
} |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
229 |
|
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
230 |
static double getNSSVersion() { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
231 |
if (nss_version == -1) |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
232 |
getNSSInfo(); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
233 |
return nss_version; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
234 |
} |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
235 |
|
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
236 |
static ECCState getNSSECC() { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
237 |
if (nss_version == -1) |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
238 |
getNSSInfo(); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
239 |
return nss_ecc_status; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
240 |
} |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
241 |
|
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
242 |
/* Read the library to find out the verison */ |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
243 |
static void getNSSInfo() { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
244 |
String nssHeader = "$Header: NSS"; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
245 |
boolean found = false; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
246 |
String s = null; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
247 |
int i = 0; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
248 |
String libfile = ""; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
249 |
|
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
250 |
try { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
251 |
libfile = getNSSLibDir() + System.mapLibraryName(nss_library); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
252 |
FileInputStream is = new FileInputStream(libfile); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
253 |
byte[] data = new byte[1000]; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
254 |
int read = 0; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
255 |
|
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
256 |
while (is.available() > 0) { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
257 |
if (read == 0) { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
258 |
read = is.read(data, 0, 1000); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
259 |
} else { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
260 |
// Prepend last 100 bytes in case the header was split |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
261 |
// between the reads. |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
262 |
System.arraycopy(data, 900, data, 0, 100); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
263 |
read = 100 + is.read(data, 100, 900); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
264 |
} |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
265 |
|
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
266 |
s = new String(data, 0, read); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
267 |
if ((i = s.indexOf(nssHeader)) > 0) { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
268 |
found = true; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
269 |
// If the nssHeader is before 920 we can break, otherwise |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
270 |
// we may not have the whole header so do another read. If |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
271 |
// no bytes are in the stream, that is ok, found is true. |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
272 |
if (i < 920) { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
273 |
break; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
274 |
} |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
275 |
} |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
276 |
} |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
277 |
|
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
278 |
is.close(); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
279 |
|
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
280 |
} catch (Exception e) { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
281 |
e.printStackTrace(); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
282 |
} |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
283 |
|
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
284 |
if (!found) { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
285 |
System.out.println("NSS version not found, set to 0.0: "+libfile); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
286 |
nss_version = 0.0; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
287 |
return; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
288 |
} |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
289 |
|
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
290 |
// the index after whitespace after nssHeader |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
291 |
int afterheader = s.indexOf("NSS", i) + 4; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
292 |
String version = s.substring(afterheader, s.indexOf(' ', afterheader)); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
293 |
|
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
294 |
// If a "dot dot" release, strip the extra dots for double parsing |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
295 |
String[] dot = version.split("\\."); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
296 |
if (dot.length > 2) { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
297 |
version = dot[0]+"."+dot[1]; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
298 |
for (int j = 2; dot.length > j; j++) { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
299 |
version += dot[j]; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
300 |
} |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
301 |
} |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
302 |
|
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
303 |
// Convert to double for easier version value checking |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
304 |
try { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
305 |
nss_version = Double.parseDouble(version); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
306 |
} catch (NumberFormatException e) { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
307 |
System.out.println("Failed to parse NSS version. Set to 0.0"); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
308 |
e.printStackTrace(); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
309 |
} |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
310 |
|
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
311 |
System.out.print("NSS version = "+version+". "); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
312 |
|
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
313 |
// Check for ECC |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
314 |
if (s.indexOf("Basic") > 0) { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
315 |
nss_ecc_status = ECCState.Basic; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
316 |
System.out.println("ECC Basic."); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
317 |
} else if (s.indexOf("Extended") > 0) { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
318 |
nss_ecc_status = ECCState.Extended; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
319 |
System.out.println("ECC Extended."); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
320 |
} |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
321 |
} |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
322 |
|
19066 | 323 |
// Used to set the nss_library file to search for libsoftokn3.so |
324 |
public static void useNSS() { |
|
325 |
nss_library = "nss3"; |
|
326 |
} |
|
327 |
||
2 | 328 |
public static void testNSS(PKCS11Test test) throws Exception { |
329 |
String libdir = getNSSLibDir(); |
|
330 |
if (libdir == null) { |
|
331 |
return; |
|
332 |
} |
|
333 |
String base = getBase(); |
|
334 |
||
335 |
if (loadNSPR(libdir) == false) { |
|
336 |
return; |
|
337 |
} |
|
338 |
||
19066 | 339 |
String libfile = libdir + System.mapLibraryName(nss_library); |
2 | 340 |
|
341 |
String customDBdir = System.getProperty("CUSTOM_DB_DIR"); |
|
342 |
String dbdir = (customDBdir != null) ? |
|
343 |
customDBdir : |
|
344 |
base + SEP + "nss" + SEP + "db"; |
|
345 |
// NSS always wants forward slashes for the config path |
|
346 |
dbdir = dbdir.replace('\\', '/'); |
|
347 |
||
348 |
String customConfig = System.getProperty("CUSTOM_P11_CONFIG"); |
|
349 |
String customConfigName = System.getProperty("CUSTOM_P11_CONFIG_NAME", "p11-nss.txt"); |
|
350 |
String p11config = (customConfig != null) ? |
|
351 |
customConfig : |
|
352 |
base + SEP + "nss" + SEP + customConfigName; |
|
353 |
||
354 |
System.setProperty("pkcs11test.nss.lib", libfile); |
|
355 |
System.setProperty("pkcs11test.nss.db", dbdir); |
|
356 |
Provider p = getSunPKCS11(p11config); |
|
357 |
test.premain(p); |
|
358 |
} |
|
359 |
||
360 |
||
13565
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
361 |
private static final Map<String,String[]> osMap; |
2 | 362 |
|
12294
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
363 |
// Location of the NSS libraries on each supported platform |
2 | 364 |
static { |
13565
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
365 |
osMap = new HashMap<String,String[]>(); |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
366 |
osMap.put("SunOS-sparc-32", new String[]{"/usr/lib/mps/"}); |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
367 |
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
|
368 |
osMap.put("SunOS-x86-32", new String[]{"/usr/lib/mps/"}); |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
369 |
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
|
370 |
osMap.put("Linux-i386-32", new String[]{ |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
371 |
"/usr/lib/i386-linux-gnu/", "/usr/lib/"}); |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
372 |
osMap.put("Linux-amd64-64", new String[]{ |
19066 | 373 |
"/usr/lib/x86_64-linux-gnu/", "/usr/lib/x86_64-linux-gnu/nss/", |
374 |
"/usr/lib64/"}); |
|
13565
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
375 |
osMap.put("Windows-x86-32", new String[]{ |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
376 |
PKCS11_BASE + "/nss/lib/windows-i586/".replace('/', SEP)}); |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
377 |
osMap.put("Windows-amd64-64", new String[]{ |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
378 |
PKCS11_BASE + "/nss/lib/windows-amd64/".replace('/', SEP)}); |
2 | 379 |
} |
380 |
||
381 |
private final static char[] hexDigits = "0123456789abcdef".toCharArray(); |
|
382 |
||
383 |
public static String toString(byte[] b) { |
|
384 |
if (b == null) { |
|
385 |
return "(null)"; |
|
386 |
} |
|
387 |
StringBuffer sb = new StringBuffer(b.length * 3); |
|
388 |
for (int i = 0; i < b.length; i++) { |
|
389 |
int k = b[i] & 0xff; |
|
390 |
if (i != 0) { |
|
391 |
sb.append(':'); |
|
392 |
} |
|
393 |
sb.append(hexDigits[k >>> 4]); |
|
394 |
sb.append(hexDigits[k & 0xf]); |
|
395 |
} |
|
396 |
return sb.toString(); |
|
397 |
} |
|
398 |
||
399 |
public static byte[] parse(String s) { |
|
400 |
if (s.equals("(null)")) { |
|
401 |
return null; |
|
402 |
} |
|
403 |
try { |
|
404 |
int n = s.length(); |
|
405 |
ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3); |
|
406 |
StringReader r = new StringReader(s); |
|
407 |
while (true) { |
|
408 |
int b1 = nextNibble(r); |
|
409 |
if (b1 < 0) { |
|
410 |
break; |
|
411 |
} |
|
412 |
int b2 = nextNibble(r); |
|
413 |
if (b2 < 0) { |
|
414 |
throw new RuntimeException("Invalid string " + s); |
|
415 |
} |
|
416 |
int b = (b1 << 4) | b2; |
|
417 |
out.write(b); |
|
418 |
} |
|
419 |
return out.toByteArray(); |
|
420 |
} catch (IOException e) { |
|
421 |
throw new RuntimeException(e); |
|
422 |
} |
|
423 |
} |
|
424 |
||
425 |
private static int nextNibble(StringReader r) throws IOException { |
|
426 |
while (true) { |
|
427 |
int ch = r.read(); |
|
428 |
if (ch == -1) { |
|
429 |
return -1; |
|
430 |
} else if ((ch >= '0') && (ch <= '9')) { |
|
431 |
return ch - '0'; |
|
432 |
} else if ((ch >= 'a') && (ch <= 'f')) { |
|
433 |
return ch - 'a' + 10; |
|
434 |
} else if ((ch >= 'A') && (ch <= 'F')) { |
|
435 |
return ch - 'A' + 10; |
|
436 |
} |
|
437 |
} |
|
438 |
} |
|
439 |
||
440 |
<T> T[] concat(T[] a, T[] b) { |
|
441 |
if ((b == null) || (b.length == 0)) { |
|
442 |
return a; |
|
443 |
} |
|
444 |
T[] r = (T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), a.length + b.length); |
|
445 |
System.arraycopy(a, 0, r, 0, a.length); |
|
446 |
System.arraycopy(b, 0, r, a.length, b.length); |
|
447 |
return r; |
|
448 |
} |
|
449 |
||
450 |
} |