blob: fc0eae01272a1455c0061f509abadfd21dd30ad9 [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
vinniec7e5e8a2012-03-05 14:13:29 +00002 * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
duke6e45e102007-12-01 00:00:00 +00003 * 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 *
ohair2283b9d2010-05-25 15:58:33 -070019 * 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.
duke6e45e102007-12-01 00:00:00 +000022 */
23
24
25// common infrastructure for SunPKCS11 tests
26
27import java.io.*;
28import java.util.*;
29import java.lang.reflect.*;
30
31import java.security.*;
32
33public 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 {
weijundff52e62011-08-12 12:26:31 +080075 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 }
duke6e45e102007-12-01 00:00:00 +0000102 }
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;
132
133 private final static String PKCS11_REL_PATH = "sun/security/pkcs11";
134
135 public static String getBase() throws Exception {
136 if (PKCS11_BASE != null) {
137 return PKCS11_BASE;
138 }
139 File cwd = new File(System.getProperty("test.src", ".")).getCanonicalFile();
140 while (true) {
141 File file = new File(cwd, "TEST.ROOT");
142 if (file.isFile()) {
143 break;
144 }
145 cwd = cwd.getParentFile();
146 if (cwd == null) {
147 throw new Exception("Test root directory not found");
148 }
149 }
150 PKCS11_BASE = new File(cwd, PKCS11_REL_PATH.replace('/', SEP)).getAbsolutePath();
151 return PKCS11_BASE;
152 }
153
154 public static String getNSSLibDir() throws Exception {
155 Properties props = System.getProperties();
156 String osName = props.getProperty("os.name");
157 if (osName.startsWith("Win")) {
158 osName = "Windows";
159 NSPR_PREFIX = "lib";
160 }
161 String osid = osName + "-"
162 + props.getProperty("os.arch") + "-" + props.getProperty("sun.arch.data.model");
vinniec7e5e8a2012-03-05 14:13:29 +0000163 String ostype = osMap.get(osid);
164 if (ostype == null) {
duke6e45e102007-12-01 00:00:00 +0000165 System.out.println("Unsupported OS, skipping: " + osid);
166 return null;
vinniec7e5e8a2012-03-05 14:13:29 +0000167// throw new Exception("Unsupported OS " + osid);
duke6e45e102007-12-01 00:00:00 +0000168 }
vinniec7e5e8a2012-03-05 14:13:29 +0000169 if (ostype.length() == 0) {
duke6e45e102007-12-01 00:00:00 +0000170 System.out.println("NSS not supported on this platform, skipping test");
171 return null;
172 }
vinniec7e5e8a2012-03-05 14:13:29 +0000173 String base = getBase();
174 String libdir = base + SEP + "nss" + SEP + "lib" + SEP + ostype + SEP;
175 System.setProperty("pkcs11test.nss.libdir", libdir);
176 return libdir;
duke6e45e102007-12-01 00:00:00 +0000177 }
178
weijundff52e62011-08-12 12:26:31 +0800179 protected static void safeReload(String lib) throws Exception {
180 try {
181 System.load(lib);
182 } catch (UnsatisfiedLinkError e) {
183 if (e.getMessage().contains("already loaded")) {
184 return;
185 }
186 }
187 }
188
duke6e45e102007-12-01 00:00:00 +0000189 static boolean loadNSPR(String libdir) throws Exception {
190 // load NSS softoken dependencies in advance to avoid resolver issues
weijundff52e62011-08-12 12:26:31 +0800191 safeReload(libdir + System.mapLibraryName(NSPR_PREFIX + "nspr4"));
192 safeReload(libdir + System.mapLibraryName(NSPR_PREFIX + "plc4"));
193 safeReload(libdir + System.mapLibraryName(NSPR_PREFIX + "plds4"));
duke6e45e102007-12-01 00:00:00 +0000194 return true;
195 }
196
197 public static void testNSS(PKCS11Test test) throws Exception {
198 String libdir = getNSSLibDir();
199 if (libdir == null) {
200 return;
201 }
202 String base = getBase();
203
204 if (loadNSPR(libdir) == false) {
205 return;
206 }
207
208 String libfile = libdir + System.mapLibraryName("softokn3");
209
210 String customDBdir = System.getProperty("CUSTOM_DB_DIR");
211 String dbdir = (customDBdir != null) ?
212 customDBdir :
213 base + SEP + "nss" + SEP + "db";
214 // NSS always wants forward slashes for the config path
215 dbdir = dbdir.replace('\\', '/');
216
217 String customConfig = System.getProperty("CUSTOM_P11_CONFIG");
218 String customConfigName = System.getProperty("CUSTOM_P11_CONFIG_NAME", "p11-nss.txt");
219 String p11config = (customConfig != null) ?
220 customConfig :
221 base + SEP + "nss" + SEP + customConfigName;
222
223 System.setProperty("pkcs11test.nss.lib", libfile);
224 System.setProperty("pkcs11test.nss.db", dbdir);
225 Provider p = getSunPKCS11(p11config);
226 test.premain(p);
227 }
228
229
230 private static final Map<String,String> osMap;
231
232 static {
233 osMap = new HashMap<String,String>();
vinniec7e5e8a2012-03-05 14:13:29 +0000234 osMap.put("SunOS-sparc-32", "solaris-sparc");
235 osMap.put("SunOS-sparcv9-64", "solaris-sparcv9");
236 osMap.put("SunOS-x86-32", "solaris-i586");
237 osMap.put("SunOS-amd64-64", "solaris-amd64");
238 osMap.put("Linux-i386-32", "linux-i586");
239 osMap.put("Linux-amd64-64", "linux-amd64");
240 osMap.put("Windows-x86-32", "windows-i586");
duke6e45e102007-12-01 00:00:00 +0000241 }
242
243 private final static char[] hexDigits = "0123456789abcdef".toCharArray();
244
245 public static String toString(byte[] b) {
246 if (b == null) {
247 return "(null)";
248 }
249 StringBuffer sb = new StringBuffer(b.length * 3);
250 for (int i = 0; i < b.length; i++) {
251 int k = b[i] & 0xff;
252 if (i != 0) {
253 sb.append(':');
254 }
255 sb.append(hexDigits[k >>> 4]);
256 sb.append(hexDigits[k & 0xf]);
257 }
258 return sb.toString();
259 }
260
261 public static byte[] parse(String s) {
262 if (s.equals("(null)")) {
263 return null;
264 }
265 try {
266 int n = s.length();
267 ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3);
268 StringReader r = new StringReader(s);
269 while (true) {
270 int b1 = nextNibble(r);
271 if (b1 < 0) {
272 break;
273 }
274 int b2 = nextNibble(r);
275 if (b2 < 0) {
276 throw new RuntimeException("Invalid string " + s);
277 }
278 int b = (b1 << 4) | b2;
279 out.write(b);
280 }
281 return out.toByteArray();
282 } catch (IOException e) {
283 throw new RuntimeException(e);
284 }
285 }
286
287 private static int nextNibble(StringReader r) throws IOException {
288 while (true) {
289 int ch = r.read();
290 if (ch == -1) {
291 return -1;
292 } else if ((ch >= '0') && (ch <= '9')) {
293 return ch - '0';
294 } else if ((ch >= 'a') && (ch <= 'f')) {
295 return ch - 'a' + 10;
296 } else if ((ch >= 'A') && (ch <= 'F')) {
297 return ch - 'A' + 10;
298 }
299 }
300 }
301
302 <T> T[] concat(T[] a, T[] b) {
303 if ((b == null) || (b.length == 0)) {
304 return a;
305 }
306 T[] r = (T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), a.length + b.length);
307 System.arraycopy(a, 0, r, 0, a.length);
308 System.arraycopy(b, 0, r, a.length, b.length);
309 return r;
310 }
311
312}