PageRenderTime 60ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

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

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