PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/infinitekind/openjdk7u-jdk
Java | 312 lines | 240 code | 40 blank | 32 comment | 57 complexity | 8ff4fa4c985cc9d12270323014751750 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, LGPL-3.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. Provider[] oldProviders = Security.getProviders();
  61. try {
  62. System.out.println("Beginning test run " + test.getClass().getName() + "...");
  63. testDefault(test);
  64. testNSS(test);
  65. testDeimos(test);
  66. } finally {
  67. Provider[] newProviders = Security.getProviders();
  68. // Do not restore providers if nothing changed. This is especailly
  69. // useful for ./Provider/Login.sh, where a SecurityManager exists.
  70. if (oldProviders.length == newProviders.length) {
  71. boolean found = false;
  72. for (int i = 0; i<oldProviders.length; i++) {
  73. if (oldProviders[i] != newProviders[i]) {
  74. found = true;
  75. break;
  76. }
  77. }
  78. if (!found) return;
  79. }
  80. for (Provider p: newProviders) {
  81. Security.removeProvider(p.getName());
  82. }
  83. for (Provider p: oldProviders) {
  84. Security.addProvider(p);
  85. }
  86. }
  87. }
  88. public static void testDeimos(PKCS11Test test) throws Exception {
  89. if (new File("/opt/SUNWconn/lib/libpkcs11.so").isFile() == false ||
  90. "true".equals(System.getProperty("NO_DEIMOS"))) {
  91. return;
  92. }
  93. String base = getBase();
  94. String p11config = base + SEP + "nss" + SEP + "p11-deimos.txt";
  95. Provider p = getSunPKCS11(p11config);
  96. test.premain(p);
  97. }
  98. public static void testDefault(PKCS11Test test) throws Exception {
  99. // run test for default configured PKCS11 providers (if any)
  100. if ("true".equals(System.getProperty("NO_DEFAULT"))) {
  101. return;
  102. }
  103. Provider[] providers = Security.getProviders();
  104. for (int i = 0; i < providers.length; i++) {
  105. Provider p = providers[i];
  106. if (p.getName().startsWith("SunPKCS11-")) {
  107. test.premain(p);
  108. }
  109. }
  110. }
  111. private static String PKCS11_BASE;
  112. private final static String PKCS11_REL_PATH = "sun/security/pkcs11";
  113. public static String getBase() throws Exception {
  114. if (PKCS11_BASE != null) {
  115. return PKCS11_BASE;
  116. }
  117. File cwd = new File(System.getProperty("test.src", ".")).getCanonicalFile();
  118. while (true) {
  119. File file = new File(cwd, "TEST.ROOT");
  120. if (file.isFile()) {
  121. break;
  122. }
  123. cwd = cwd.getParentFile();
  124. if (cwd == null) {
  125. throw new Exception("Test root directory not found");
  126. }
  127. }
  128. PKCS11_BASE = new File(cwd, PKCS11_REL_PATH.replace('/', SEP)).getAbsolutePath();
  129. return PKCS11_BASE;
  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. protected static void safeReload(String lib) throws Exception {
  156. try {
  157. System.load(lib);
  158. } catch (UnsatisfiedLinkError e) {
  159. if (e.getMessage().contains("already loaded")) {
  160. return;
  161. }
  162. }
  163. }
  164. static boolean loadNSPR(String libdir) throws Exception {
  165. // load NSS softoken dependencies in advance to avoid resolver issues
  166. safeReload(libdir + System.mapLibraryName(NSPR_PREFIX + "nspr4"));
  167. safeReload(libdir + System.mapLibraryName(NSPR_PREFIX + "plc4"));
  168. safeReload(libdir + System.mapLibraryName(NSPR_PREFIX + "plds4"));
  169. return true;
  170. }
  171. public static void testNSS(PKCS11Test test) throws Exception {
  172. String libdir = getNSSLibDir();
  173. if (libdir == null) {
  174. return;
  175. }
  176. String base = getBase();
  177. if (loadNSPR(libdir) == false) {
  178. return;
  179. }
  180. String libfile = libdir + System.mapLibraryName("softokn3");
  181. String customDBdir = System.getProperty("CUSTOM_DB_DIR");
  182. String dbdir = (customDBdir != null) ?
  183. customDBdir :
  184. base + SEP + "nss" + SEP + "db";
  185. // NSS always wants forward slashes for the config path
  186. dbdir = dbdir.replace('\\', '/');
  187. String customConfig = System.getProperty("CUSTOM_P11_CONFIG");
  188. String customConfigName = System.getProperty("CUSTOM_P11_CONFIG_NAME", "p11-nss.txt");
  189. String p11config = (customConfig != null) ?
  190. customConfig :
  191. base + SEP + "nss" + SEP + customConfigName;
  192. System.setProperty("pkcs11test.nss.lib", libfile);
  193. System.setProperty("pkcs11test.nss.db", dbdir);
  194. Provider p = getSunPKCS11(p11config);
  195. test.premain(p);
  196. }
  197. private static final Map<String,String> osMap;
  198. static {
  199. osMap = new HashMap<String,String>();
  200. osMap.put("SunOS-sparc-32", "solaris-sparc");
  201. osMap.put("SunOS-sparcv9-64", "solaris-sparcv9");
  202. osMap.put("SunOS-x86-32", "solaris-i586");
  203. osMap.put("SunOS-amd64-64", "solaris-amd64");
  204. osMap.put("Linux-i386-32", "linux-i586");
  205. osMap.put("Linux-amd64-64", "linux-amd64");
  206. osMap.put("Windows-x86-32", "windows-i586");
  207. }
  208. private final static char[] hexDigits = "0123456789abcdef".toCharArray();
  209. public static String toString(byte[] b) {
  210. if (b == null) {
  211. return "(null)";
  212. }
  213. StringBuffer sb = new StringBuffer(b.length * 3);
  214. for (int i = 0; i < b.length; i++) {
  215. int k = b[i] & 0xff;
  216. if (i != 0) {
  217. sb.append(':');
  218. }
  219. sb.append(hexDigits[k >>> 4]);
  220. sb.append(hexDigits[k & 0xf]);
  221. }
  222. return sb.toString();
  223. }
  224. public static byte[] parse(String s) {
  225. if (s.equals("(null)")) {
  226. return null;
  227. }
  228. try {
  229. int n = s.length();
  230. ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3);
  231. StringReader r = new StringReader(s);
  232. while (true) {
  233. int b1 = nextNibble(r);
  234. if (b1 < 0) {
  235. break;
  236. }
  237. int b2 = nextNibble(r);
  238. if (b2 < 0) {
  239. throw new RuntimeException("Invalid string " + s);
  240. }
  241. int b = (b1 << 4) | b2;
  242. out.write(b);
  243. }
  244. return out.toByteArray();
  245. } catch (IOException e) {
  246. throw new RuntimeException(e);
  247. }
  248. }
  249. private static int nextNibble(StringReader r) throws IOException {
  250. while (true) {
  251. int ch = r.read();
  252. if (ch == -1) {
  253. return -1;
  254. } else if ((ch >= '0') && (ch <= '9')) {
  255. return ch - '0';
  256. } else if ((ch >= 'a') && (ch <= 'f')) {
  257. return ch - 'a' + 10;
  258. } else if ((ch >= 'A') && (ch <= 'F')) {
  259. return ch - 'A' + 10;
  260. }
  261. }
  262. }
  263. <T> T[] concat(T[] a, T[] b) {
  264. if ((b == null) || (b.length == 0)) {
  265. return a;
  266. }
  267. T[] r = (T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), a.length + b.length);
  268. System.arraycopy(a, 0, r, 0, a.length);
  269. System.arraycopy(b, 0, r, a.length, b.length);
  270. return r;
  271. }
  272. }