blob: c2d5a2fd243907c3d792ca15af4a3bc1348d3342 [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
vinniebc475592012-03-02 17:24:08 +00002 * Copyright (c) 2003, 2012, 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
vinniebc475592012-03-02 17:24:08 +000045 // Minimum supported NSS library version
46 private static final String MINIMUM_SUPPORTED_NSS_VERSION = "3.12";
47
duke6e45e102007-12-01 00:00:00 +000048 static {
49 // hack
50 String absBase = new File(BASE).getAbsolutePath();
51 int k = absBase.indexOf(SEP + "test" + SEP + "sun" + SEP);
52 if (k < 0) k = 0;
53 String p1 = absBase.substring(0, k + 6);
54 String p2 = absBase.substring(k + 5);
55 CLOSED_BASE = p1 + "closed" + p2;
56 }
57
58 static String NSPR_PREFIX = "";
59
60 static Provider getSunPKCS11(String config) throws Exception {
61 Class clazz = Class.forName("sun.security.pkcs11.SunPKCS11");
62 Constructor cons = clazz.getConstructor(new Class[] {String.class});
63 Object obj = cons.newInstance(new Object[] {config});
64 return (Provider)obj;
65 }
66
67 public abstract void main(Provider p) throws Exception;
68
69 private void premain(Provider p) throws Exception {
70 long start = System.currentTimeMillis();
71 System.out.println("Running test with provider " + p.getName() + "...");
72 main(p);
73 long stop = System.currentTimeMillis();
74 System.out.println("Completed test with provider " + p.getName() + " (" + (stop - start) + " ms).");
75 }
76
77 public static void main(PKCS11Test test) throws Exception {
weijundff52e62011-08-12 12:26:31 +080078 Provider[] oldProviders = Security.getProviders();
79 try {
80 System.out.println("Beginning test run " + test.getClass().getName() + "...");
81 testDefault(test);
82 testNSS(test);
83 testDeimos(test);
84 } finally {
85 Provider[] newProviders = Security.getProviders();
86 // Do not restore providers if nothing changed. This is especailly
87 // useful for ./Provider/Login.sh, where a SecurityManager exists.
88 if (oldProviders.length == newProviders.length) {
89 boolean found = false;
90 for (int i = 0; i<oldProviders.length; i++) {
91 if (oldProviders[i] != newProviders[i]) {
92 found = true;
93 break;
94 }
95 }
96 if (!found) return;
97 }
98 for (Provider p: newProviders) {
99 Security.removeProvider(p.getName());
100 }
101 for (Provider p: oldProviders) {
102 Security.addProvider(p);
103 }
104 }
duke6e45e102007-12-01 00:00:00 +0000105 }
106
107 public static void testDeimos(PKCS11Test test) throws Exception {
108 if (new File("/opt/SUNWconn/lib/libpkcs11.so").isFile() == false ||
109 "true".equals(System.getProperty("NO_DEIMOS"))) {
110 return;
111 }
112 String base = getBase();
113 String p11config = base + SEP + "nss" + SEP + "p11-deimos.txt";
114 Provider p = getSunPKCS11(p11config);
115 test.premain(p);
116 }
117
118 public static void testDefault(PKCS11Test test) throws Exception {
119 // run test for default configured PKCS11 providers (if any)
120
121 if ("true".equals(System.getProperty("NO_DEFAULT"))) {
122 return;
123 }
124
125 Provider[] providers = Security.getProviders();
126 for (int i = 0; i < providers.length; i++) {
127 Provider p = providers[i];
128 if (p.getName().startsWith("SunPKCS11-")) {
129 test.premain(p);
130 }
131 }
132 }
133
134 private static String PKCS11_BASE;
vinniebc475592012-03-02 17:24:08 +0000135 static {
136 try {
137 PKCS11_BASE = getBase();
138 } catch (Exception e) {
139 // ignore
140 }
141 }
duke6e45e102007-12-01 00:00:00 +0000142
143 private final static String PKCS11_REL_PATH = "sun/security/pkcs11";
144
145 public static String getBase() throws Exception {
146 if (PKCS11_BASE != null) {
147 return PKCS11_BASE;
148 }
149 File cwd = new File(System.getProperty("test.src", ".")).getCanonicalFile();
150 while (true) {
151 File file = new File(cwd, "TEST.ROOT");
152 if (file.isFile()) {
153 break;
154 }
155 cwd = cwd.getParentFile();
156 if (cwd == null) {
157 throw new Exception("Test root directory not found");
158 }
159 }
160 PKCS11_BASE = new File(cwd, PKCS11_REL_PATH.replace('/', SEP)).getAbsolutePath();
161 return PKCS11_BASE;
162 }
163
164 public static String getNSSLibDir() throws Exception {
165 Properties props = System.getProperties();
166 String osName = props.getProperty("os.name");
167 if (osName.startsWith("Win")) {
168 osName = "Windows";
169 NSPR_PREFIX = "lib";
170 }
171 String osid = osName + "-"
172 + props.getProperty("os.arch") + "-" + props.getProperty("sun.arch.data.model");
vinniebc475592012-03-02 17:24:08 +0000173 String nssLibDir = osMap.get(osid);
174 if (nssLibDir == null) {
duke6e45e102007-12-01 00:00:00 +0000175 System.out.println("Unsupported OS, skipping: " + osid);
176 return null;
vinniebc475592012-03-02 17:24:08 +0000177// throw new Exception("Unsupported OS " + osName);
duke6e45e102007-12-01 00:00:00 +0000178 }
vinniebc475592012-03-02 17:24:08 +0000179 if (nssLibDir.length() == 0) {
duke6e45e102007-12-01 00:00:00 +0000180 System.out.println("NSS not supported on this platform, skipping test");
181 return null;
182 }
vinniebc475592012-03-02 17:24:08 +0000183 System.setProperty("pkcs11test.nss.libdir", nssLibDir);
184 return nssLibDir;
duke6e45e102007-12-01 00:00:00 +0000185 }
186
weijundff52e62011-08-12 12:26:31 +0800187 protected static void safeReload(String lib) throws Exception {
188 try {
189 System.load(lib);
190 } catch (UnsatisfiedLinkError e) {
191 if (e.getMessage().contains("already loaded")) {
192 return;
193 }
194 }
195 }
196
duke6e45e102007-12-01 00:00:00 +0000197 static boolean loadNSPR(String libdir) throws Exception {
198 // load NSS softoken dependencies in advance to avoid resolver issues
weijundff52e62011-08-12 12:26:31 +0800199 safeReload(libdir + System.mapLibraryName(NSPR_PREFIX + "nspr4"));
200 safeReload(libdir + System.mapLibraryName(NSPR_PREFIX + "plc4"));
201 safeReload(libdir + System.mapLibraryName(NSPR_PREFIX + "plds4"));
vinniebc475592012-03-02 17:24:08 +0000202 safeReload(libdir + System.mapLibraryName("sqlite3"));
203 safeReload(libdir + System.mapLibraryName("nssutil3"));
duke6e45e102007-12-01 00:00:00 +0000204 return true;
205 }
206
207 public static void testNSS(PKCS11Test test) throws Exception {
208 String libdir = getNSSLibDir();
209 if (libdir == null) {
210 return;
211 }
212 String base = getBase();
213
214 if (loadNSPR(libdir) == false) {
215 return;
216 }
217
218 String libfile = libdir + System.mapLibraryName("softokn3");
219
220 String customDBdir = System.getProperty("CUSTOM_DB_DIR");
221 String dbdir = (customDBdir != null) ?
222 customDBdir :
223 base + SEP + "nss" + SEP + "db";
224 // NSS always wants forward slashes for the config path
225 dbdir = dbdir.replace('\\', '/');
226
227 String customConfig = System.getProperty("CUSTOM_P11_CONFIG");
228 String customConfigName = System.getProperty("CUSTOM_P11_CONFIG_NAME", "p11-nss.txt");
229 String p11config = (customConfig != null) ?
230 customConfig :
231 base + SEP + "nss" + SEP + customConfigName;
232
vinniebc475592012-03-02 17:24:08 +0000233 System.out.println("[WARNING: NSS libraries loaded from " + libdir +
234 " must be at least version " +
235 MINIMUM_SUPPORTED_NSS_VERSION + "]");
duke6e45e102007-12-01 00:00:00 +0000236 System.setProperty("pkcs11test.nss.lib", libfile);
vinniebc475592012-03-02 17:24:08 +0000237 System.setProperty("pkcs11test.nss.libVersionCheck",
238 MINIMUM_SUPPORTED_NSS_VERSION);
duke6e45e102007-12-01 00:00:00 +0000239 System.setProperty("pkcs11test.nss.db", dbdir);
240 Provider p = getSunPKCS11(p11config);
241 test.premain(p);
242 }
243
244
245 private static final Map<String,String> osMap;
246
vinniebc475592012-03-02 17:24:08 +0000247 // Location of the NSS libraries on each supported platform
duke6e45e102007-12-01 00:00:00 +0000248 static {
249 osMap = new HashMap<String,String>();
vinniebc475592012-03-02 17:24:08 +0000250 osMap.put("SunOS-sparc-32", "/usr/lib/mps/");
251 osMap.put("SunOS-sparcv9-64", "/usr/lib/mps/64/");
252 osMap.put("SunOS-x86-32", "/usr/lib/mps/");
253 osMap.put("SunOS-amd64-64", "/usr/lib/mps/64/");
254 osMap.put("Linux-i386-32", "/usr/lib/");
255 osMap.put("Linux-amd64-64", "/usr/lib64/");
256 osMap.put("Windows-x86-32",
257 PKCS11_BASE + "/nss/lib/windows-i586/".replace('/', SEP));
258 osMap.put("Windows-amd64-64",
259 PKCS11_BASE + "/nss/lib/windows-amd64/".replace('/', SEP));
duke6e45e102007-12-01 00:00:00 +0000260 }
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}