PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/jdk/test/sun/security/pkcs11/PKCS11Test.java

https://bitbucket.org/nkabir/jdk-6
Java | 299 lines | 226 code | 41 blank | 32 comment | 54 complexity | be17f8c69e9923326fa2a331730f1015 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, BSD-3-Clause
  1. /*
  2. * Copyright (c) 2003, 2008, Oracle and/or its affiliates. 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 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.
  22. */
  23. // common infrastructure for SunPKCS11 tests
  24. import java.io.*;
  25. import java.util.*;
  26. import java.lang.reflect.*;
  27. import java.security.*;
  28. public abstract class PKCS11Test {
  29. // directory of the test source
  30. static final String BASE = System.getProperty("test.src", ".");
  31. static final char SEP = File.separatorChar;
  32. private final static String REL_CLOSED = "../../../../closed/sun/security/pkcs11".replace('/', SEP);
  33. // directory corresponding to BASE in the /closed hierarchy
  34. static final String CLOSED_BASE;
  35. // directory corresponding to NSS binaries for various OS.
  36. static final String NSS_BASE;
  37. static {
  38. // hack
  39. String absBase = new File(BASE).getAbsolutePath();
  40. if (absBase.indexOf("closed") == -1) {
  41. int k = absBase.indexOf(SEP + "test" + SEP + "sun" + SEP);
  42. if (k < 0) k = 0;
  43. String p1 = absBase.substring(0, k + 6);
  44. String p2 = absBase.substring(k + 5);
  45. CLOSED_BASE = p1 + "closed" + p2;
  46. } else {
  47. CLOSED_BASE = BASE;
  48. }
  49. int n = CLOSED_BASE.indexOf(SEP + "closed" + SEP);
  50. NSS_BASE = CLOSED_BASE.substring(0, n + 8) +
  51. ("sun/security/pkcs11/nss".replace('/', SEP));
  52. }
  53. static String NSPR_PREFIX = "";
  54. static Provider getSunPKCS11(String config) throws Exception {
  55. Class clazz = Class.forName("sun.security.pkcs11.SunPKCS11");
  56. Constructor cons = clazz.getConstructor(new Class[] {String.class});
  57. Object obj = cons.newInstance(new Object[] {config});
  58. return (Provider)obj;
  59. }
  60. public abstract void main(Provider p) throws Exception;
  61. private void premain(Provider p) throws Exception {
  62. long start = System.currentTimeMillis();
  63. System.out.println("Running test with provider " + p.getName() + "...");
  64. main(p);
  65. long stop = System.currentTimeMillis();
  66. System.out.println("Completed test with provider " + p.getName() + " (" + (stop - start) + " ms).");
  67. }
  68. public static void main(PKCS11Test test) throws Exception {
  69. System.out.println("Beginning test run " + test.getClass().getName() + "...");
  70. testDefault(test);
  71. testNSS(test);
  72. testDeimos(test);
  73. }
  74. public static void testDeimos(PKCS11Test test) throws Exception {
  75. if (new File("/opt/SUNWconn/lib/libpkcs11.so").isFile() == false ||
  76. "true".equals(System.getProperty("NO_DEIMOS"))) {
  77. return;
  78. }
  79. if (!(new File(NSS_BASE)).exists()) return;
  80. String p11config = NSS_BASE + SEP + "p11-deimos.txt";
  81. Provider p = getSunPKCS11(p11config);
  82. test.premain(p);
  83. }
  84. public static void testDefault(PKCS11Test test) throws Exception {
  85. // run test for default configured PKCS11 providers (if any)
  86. if ("true".equals(System.getProperty("NO_DEFAULT"))) {
  87. return;
  88. }
  89. Provider[] providers = Security.getProviders();
  90. for (int i = 0; i < providers.length; i++) {
  91. Provider p = providers[i];
  92. if (p.getName().startsWith("SunPKCS11-")) {
  93. test.premain(p);
  94. }
  95. }
  96. }
  97. private static String PKCS11_BASE;
  98. private final static String PKCS11_REL_PATH = "sun/security/pkcs11";
  99. public static String getBase() throws Exception {
  100. if (PKCS11_BASE != null) {
  101. return PKCS11_BASE;
  102. }
  103. File cwd = new File(System.getProperty("test.src", ".")).getCanonicalFile();
  104. while (true) {
  105. File file = new File(cwd, "TEST.ROOT");
  106. if (file.isFile()) {
  107. break;
  108. }
  109. cwd = cwd.getParentFile();
  110. if (cwd == null) {
  111. throw new Exception("Test root directory not found");
  112. }
  113. }
  114. PKCS11_BASE = new File(cwd, PKCS11_REL_PATH.replace('/', SEP)).getAbsolutePath();
  115. return PKCS11_BASE;
  116. }
  117. public static String getNSSLibDir() throws Exception {
  118. Properties props = System.getProperties();
  119. String osName = props.getProperty("os.name");
  120. if (osName.startsWith("Win")) {
  121. osName = "Windows";
  122. NSPR_PREFIX = "lib";
  123. }
  124. String osid = osName + "-"
  125. + props.getProperty("os.arch") + "-" + props.getProperty("sun.arch.data.model");
  126. String ostype = osMap.get(osid);
  127. if (ostype == null) {
  128. System.out.println("Unsupported OS, skipping: " + osid);
  129. return null;
  130. // throw new Exception("Unsupported OS " + osid);
  131. }
  132. if (ostype.length() == 0) {
  133. System.out.println("NSS not supported on this platform, skipping test");
  134. return null;
  135. }
  136. String libdir = NSS_BASE + SEP + "lib" + SEP + ostype + SEP;
  137. System.setProperty("pkcs11test.nss.libdir", libdir);
  138. return libdir;
  139. }
  140. static boolean loadNSPR(String libdir) throws Exception {
  141. // load NSS softoken dependencies in advance to avoid resolver issues
  142. try {
  143. System.load(libdir + System.mapLibraryName(NSPR_PREFIX + "nspr4"));
  144. } catch (UnsatisfiedLinkError e) {
  145. // GLIBC problem on older linux-amd64 machines
  146. if (libdir.contains("linux-amd64")) {
  147. System.out.println(e);
  148. System.out.println("NSS does not work on this platform, skipping.");
  149. return false;
  150. }
  151. throw e;
  152. }
  153. System.load(libdir + System.mapLibraryName(NSPR_PREFIX + "plc4"));
  154. System.load(libdir + System.mapLibraryName(NSPR_PREFIX + "plds4"));
  155. return true;
  156. }
  157. public static void testNSS(PKCS11Test test) throws Exception {
  158. if (!(new File(NSS_BASE)).exists()) return;
  159. String libdir = getNSSLibDir();
  160. if (libdir == null) {
  161. return;
  162. }
  163. if (loadNSPR(libdir) == false) {
  164. return;
  165. }
  166. String libfile = libdir + System.mapLibraryName("softokn3");
  167. String customDBdir = System.getProperty("CUSTOM_DB_DIR");
  168. String dbdir = (customDBdir != null) ?
  169. customDBdir :
  170. NSS_BASE + SEP + "db";
  171. // NSS always wants forward slashes for the config path
  172. dbdir = dbdir.replace('\\', '/');
  173. String customConfig = System.getProperty("CUSTOM_P11_CONFIG");
  174. String customConfigName = System.getProperty("CUSTOM_P11_CONFIG_NAME", "p11-nss.txt");
  175. String p11config = (customConfig != null) ?
  176. customConfig :
  177. NSS_BASE + SEP + customConfigName;
  178. System.setProperty("pkcs11test.nss.lib", libfile);
  179. System.setProperty("pkcs11test.nss.db", dbdir);
  180. Provider p = getSunPKCS11(p11config);
  181. test.premain(p);
  182. }
  183. private static final Map<String,String> osMap;
  184. static {
  185. osMap = new HashMap<String,String>();
  186. osMap.put("SunOS-sparc-32", "solaris-sparc");
  187. osMap.put("SunOS-sparcv9-64", "solaris-sparcv9");
  188. osMap.put("SunOS-x86-32", "solaris-i586");
  189. osMap.put("SunOS-amd64-64", "solaris-amd64");
  190. osMap.put("Linux-i386-32", "linux-i586");
  191. osMap.put("Linux-amd64-64", "linux-amd64");
  192. osMap.put("Windows-x86-32", "windows-i586");
  193. }
  194. private final static char[] hexDigits = "0123456789abcdef".toCharArray();
  195. public static String toString(byte[] b) {
  196. if (b == null) {
  197. return "(null)";
  198. }
  199. StringBuffer sb = new StringBuffer(b.length * 3);
  200. for (int i = 0; i < b.length; i++) {
  201. int k = b[i] & 0xff;
  202. if (i != 0) {
  203. sb.append(':');
  204. }
  205. sb.append(hexDigits[k >>> 4]);
  206. sb.append(hexDigits[k & 0xf]);
  207. }
  208. return sb.toString();
  209. }
  210. public static byte[] parse(String s) {
  211. if (s.equals("(null)")) {
  212. return null;
  213. }
  214. try {
  215. int n = s.length();
  216. ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3);
  217. StringReader r = new StringReader(s);
  218. while (true) {
  219. int b1 = nextNibble(r);
  220. if (b1 < 0) {
  221. break;
  222. }
  223. int b2 = nextNibble(r);
  224. if (b2 < 0) {
  225. throw new RuntimeException("Invalid string " + s);
  226. }
  227. int b = (b1 << 4) | b2;
  228. out.write(b);
  229. }
  230. return out.toByteArray();
  231. } catch (IOException e) {
  232. throw new RuntimeException(e);
  233. }
  234. }
  235. private static int nextNibble(StringReader r) throws IOException {
  236. while (true) {
  237. int ch = r.read();
  238. if (ch == -1) {
  239. return -1;
  240. } else if ((ch >= '0') && (ch <= '9')) {
  241. return ch - '0';
  242. } else if ((ch >= 'a') && (ch <= 'f')) {
  243. return ch - 'a' + 10;
  244. } else if ((ch >= 'A') && (ch <= 'F')) {
  245. return ch - 'A' + 10;
  246. }
  247. }
  248. }
  249. <T> T[] concat(T[] a, T[] b) {
  250. if ((b == null) || (b.length == 0)) {
  251. return a;
  252. }
  253. T[] r = (T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), a.length + b.length);
  254. System.arraycopy(a, 0, r, 0, a.length);
  255. System.arraycopy(b, 0, r, a.length, b.length);
  256. return r;
  257. }
  258. }