blob: ca232c7a8412fd019ef3cf8dc9c51175f052a0df [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved.
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 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
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 {
75 System.out.println("Beginning test run " + test.getClass().getName() + "...");
76 testDefault(test);
77 testNSS(test);
78 testDeimos(test);
79 }
80
81 public static void testDeimos(PKCS11Test test) throws Exception {
82 if (new File("/opt/SUNWconn/lib/libpkcs11.so").isFile() == false ||
83 "true".equals(System.getProperty("NO_DEIMOS"))) {
84 return;
85 }
86 String base = getBase();
87 String p11config = base + SEP + "nss" + SEP + "p11-deimos.txt";
88 Provider p = getSunPKCS11(p11config);
89 test.premain(p);
90 }
91
92 public static void testDefault(PKCS11Test test) throws Exception {
93 // run test for default configured PKCS11 providers (if any)
94
95 if ("true".equals(System.getProperty("NO_DEFAULT"))) {
96 return;
97 }
98
99 Provider[] providers = Security.getProviders();
100 for (int i = 0; i < providers.length; i++) {
101 Provider p = providers[i];
102 if (p.getName().startsWith("SunPKCS11-")) {
103 test.premain(p);
104 }
105 }
106 }
107
108 private static String PKCS11_BASE;
109
110 private final static String PKCS11_REL_PATH = "sun/security/pkcs11";
111
112 public static String getBase() throws Exception {
113 if (PKCS11_BASE != null) {
114 return PKCS11_BASE;
115 }
116 File cwd = new File(System.getProperty("test.src", ".")).getCanonicalFile();
117 while (true) {
118 File file = new File(cwd, "TEST.ROOT");
119 if (file.isFile()) {
120 break;
121 }
122 cwd = cwd.getParentFile();
123 if (cwd == null) {
124 throw new Exception("Test root directory not found");
125 }
126 }
127 PKCS11_BASE = new File(cwd, PKCS11_REL_PATH.replace('/', SEP)).getAbsolutePath();
128 return PKCS11_BASE;
129 }
130
131 public static String getNSSLibDir() throws Exception {
132 Properties props = System.getProperties();
133 String osName = props.getProperty("os.name");
134 if (osName.startsWith("Win")) {
135 osName = "Windows";
136 NSPR_PREFIX = "lib";
137 }
138 String osid = osName + "-"
139 + props.getProperty("os.arch") + "-" + props.getProperty("sun.arch.data.model");
140 String ostype = osMap.get(osid);
141 if (ostype == null) {
142 System.out.println("Unsupported OS, skipping: " + osid);
143 return null;
144// throw new Exception("Unsupported OS " + osid);
145 }
146 if (ostype.length() == 0) {
147 System.out.println("NSS not supported on this platform, skipping test");
148 return null;
149 }
150 String base = getBase();
151 String libdir = base + SEP + "nss" + SEP + "lib" + SEP + ostype + SEP;
152 System.setProperty("pkcs11test.nss.libdir", libdir);
153 return libdir;
154 }
155
156 static boolean loadNSPR(String libdir) throws Exception {
157 // load NSS softoken dependencies in advance to avoid resolver issues
158 try {
159 System.load(libdir + System.mapLibraryName(NSPR_PREFIX + "nspr4"));
160 } catch (UnsatisfiedLinkError e) {
161 // GLIBC problem on older linux-amd64 machines
162 if (libdir.contains("linux-amd64")) {
163 System.out.println(e);
164 System.out.println("NSS does not work on this platform, skipping.");
165 return false;
166 }
167 throw e;
168 }
169 System.load(libdir + System.mapLibraryName(NSPR_PREFIX + "plc4"));
170 System.load(libdir + System.mapLibraryName(NSPR_PREFIX + "plds4"));
171 return true;
172 }
173
174 public static void testNSS(PKCS11Test test) throws Exception {
175 String libdir = getNSSLibDir();
176 if (libdir == null) {
177 return;
178 }
179 String base = getBase();
180
181 if (loadNSPR(libdir) == false) {
182 return;
183 }
184
185 String libfile = libdir + System.mapLibraryName("softokn3");
186
187 String customDBdir = System.getProperty("CUSTOM_DB_DIR");
188 String dbdir = (customDBdir != null) ?
189 customDBdir :
190 base + SEP + "nss" + SEP + "db";
191 // NSS always wants forward slashes for the config path
192 dbdir = dbdir.replace('\\', '/');
193
194 String customConfig = System.getProperty("CUSTOM_P11_CONFIG");
195 String customConfigName = System.getProperty("CUSTOM_P11_CONFIG_NAME", "p11-nss.txt");
196 String p11config = (customConfig != null) ?
197 customConfig :
198 base + SEP + "nss" + SEP + customConfigName;
199
200 System.setProperty("pkcs11test.nss.lib", libfile);
201 System.setProperty("pkcs11test.nss.db", dbdir);
202 Provider p = getSunPKCS11(p11config);
203 test.premain(p);
204 }
205
206
207 private static final Map<String,String> osMap;
208
209 static {
210 osMap = new HashMap<String,String>();
211 osMap.put("SunOS-sparc-32", "solaris-sparc");
212 osMap.put("SunOS-sparcv9-64", "solaris-sparcv9");
213 osMap.put("SunOS-x86-32", "solaris-i586");
214 osMap.put("SunOS-amd64-64", "solaris-amd64");
215 osMap.put("Linux-i386-32", "linux-i586");
216 osMap.put("Linux-amd64-64", "linux-amd64");
217 osMap.put("Windows-x86-32", "windows-i586");
218 }
219
220 private final static char[] hexDigits = "0123456789abcdef".toCharArray();
221
222 public static String toString(byte[] b) {
223 if (b == null) {
224 return "(null)";
225 }
226 StringBuffer sb = new StringBuffer(b.length * 3);
227 for (int i = 0; i < b.length; i++) {
228 int k = b[i] & 0xff;
229 if (i != 0) {
230 sb.append(':');
231 }
232 sb.append(hexDigits[k >>> 4]);
233 sb.append(hexDigits[k & 0xf]);
234 }
235 return sb.toString();
236 }
237
238 public static byte[] parse(String s) {
239 if (s.equals("(null)")) {
240 return null;
241 }
242 try {
243 int n = s.length();
244 ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3);
245 StringReader r = new StringReader(s);
246 while (true) {
247 int b1 = nextNibble(r);
248 if (b1 < 0) {
249 break;
250 }
251 int b2 = nextNibble(r);
252 if (b2 < 0) {
253 throw new RuntimeException("Invalid string " + s);
254 }
255 int b = (b1 << 4) | b2;
256 out.write(b);
257 }
258 return out.toByteArray();
259 } catch (IOException e) {
260 throw new RuntimeException(e);
261 }
262 }
263
264 private static int nextNibble(StringReader r) throws IOException {
265 while (true) {
266 int ch = r.read();
267 if (ch == -1) {
268 return -1;
269 } else if ((ch >= '0') && (ch <= '9')) {
270 return ch - '0';
271 } else if ((ch >= 'a') && (ch <= 'f')) {
272 return ch - 'a' + 10;
273 } else if ((ch >= 'A') && (ch <= 'F')) {
274 return ch - 'A' + 10;
275 }
276 }
277 }
278
279 <T> T[] concat(T[] a, T[] b) {
280 if ((b == null) || (b.length == 0)) {
281 return a;
282 }
283 T[] r = (T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), a.length + b.length);
284 System.arraycopy(a, 0, r, 0, a.length);
285 System.arraycopy(b, 0, r, a.length, b.length);
286 return r;
287 }
288
289}