PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/psandoz/lambda-jdk-pipeline-patches
Java | 323 lines | 249 code | 40 blank | 34 comment | 57 complexity | 4a35e1f1d823a3978d890deb9fb1c7c8 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, LGPL-3.0
  1. /*
  2. * Copyright (c) 2003, 2012, 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. static {
  113. try {
  114. PKCS11_BASE = getBase();
  115. } catch (Exception e) {
  116. // ignore
  117. }
  118. }
  119. private final static String PKCS11_REL_PATH = "sun/security/pkcs11";
  120. public static String getBase() throws Exception {
  121. if (PKCS11_BASE != null) {
  122. return PKCS11_BASE;
  123. }
  124. File cwd = new File(System.getProperty("test.src", ".")).getCanonicalFile();
  125. while (true) {
  126. File file = new File(cwd, "TEST.ROOT");
  127. if (file.isFile()) {
  128. break;
  129. }
  130. cwd = cwd.getParentFile();
  131. if (cwd == null) {
  132. throw new Exception("Test root directory not found");
  133. }
  134. }
  135. PKCS11_BASE = new File(cwd, PKCS11_REL_PATH.replace('/', SEP)).getAbsolutePath();
  136. return PKCS11_BASE;
  137. }
  138. public static String getNSSLibDir() throws Exception {
  139. Properties props = System.getProperties();
  140. String osName = props.getProperty("os.name");
  141. if (osName.startsWith("Win")) {
  142. osName = "Windows";
  143. NSPR_PREFIX = "lib";
  144. }
  145. String osid = osName + "-"
  146. + props.getProperty("os.arch") + "-" + props.getProperty("sun.arch.data.model");
  147. String nssLibDir = osMap.get(osid);
  148. if (nssLibDir == null) {
  149. System.out.println("Unsupported OS, skipping: " + osid);
  150. return null;
  151. // throw new Exception("Unsupported OS " + osName);
  152. }
  153. if (nssLibDir.length() == 0) {
  154. System.out.println("NSS not supported on this platform, skipping test");
  155. return null;
  156. }
  157. System.setProperty("pkcs11test.nss.libdir", nssLibDir);
  158. return nssLibDir;
  159. }
  160. protected static void safeReload(String lib) throws Exception {
  161. try {
  162. System.load(lib);
  163. } catch (UnsatisfiedLinkError e) {
  164. if (e.getMessage().contains("already loaded")) {
  165. return;
  166. }
  167. }
  168. }
  169. static boolean loadNSPR(String libdir) throws Exception {
  170. // load NSS softoken dependencies in advance to avoid resolver issues
  171. safeReload(libdir + System.mapLibraryName(NSPR_PREFIX + "nspr4"));
  172. safeReload(libdir + System.mapLibraryName(NSPR_PREFIX + "plc4"));
  173. safeReload(libdir + System.mapLibraryName(NSPR_PREFIX + "plds4"));
  174. safeReload(libdir + System.mapLibraryName("sqlite3"));
  175. safeReload(libdir + System.mapLibraryName("nssutil3"));
  176. return true;
  177. }
  178. public static void testNSS(PKCS11Test test) throws Exception {
  179. String libdir = getNSSLibDir();
  180. if (libdir == null) {
  181. return;
  182. }
  183. String base = getBase();
  184. if (loadNSPR(libdir) == false) {
  185. return;
  186. }
  187. String libfile = libdir + System.mapLibraryName("softokn3");
  188. String customDBdir = System.getProperty("CUSTOM_DB_DIR");
  189. String dbdir = (customDBdir != null) ?
  190. customDBdir :
  191. base + SEP + "nss" + SEP + "db";
  192. // NSS always wants forward slashes for the config path
  193. dbdir = dbdir.replace('\\', '/');
  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. System.setProperty("pkcs11test.nss.lib", libfile);
  200. System.setProperty("pkcs11test.nss.db", dbdir);
  201. Provider p = getSunPKCS11(p11config);
  202. test.premain(p);
  203. }
  204. private static final Map<String,String> osMap;
  205. // Location of the NSS libraries on each supported platform
  206. static {
  207. osMap = new HashMap<String,String>();
  208. osMap.put("SunOS-sparc-32", "/usr/lib/mps/");
  209. osMap.put("SunOS-sparcv9-64", "/usr/lib/mps/64/");
  210. osMap.put("SunOS-x86-32", "/usr/lib/mps/");
  211. osMap.put("SunOS-amd64-64", "/usr/lib/mps/64/");
  212. osMap.put("Linux-i386-32", "/usr/lib/");
  213. osMap.put("Linux-amd64-64", "/usr/lib64/");
  214. osMap.put("Windows-x86-32",
  215. PKCS11_BASE + "/nss/lib/windows-i586/".replace('/', SEP));
  216. osMap.put("Windows-amd64-64",
  217. PKCS11_BASE + "/nss/lib/windows-amd64/".replace('/', SEP));
  218. }
  219. private final static char[] hexDigits = "0123456789abcdef".toCharArray();
  220. public static String toString(byte[] b) {
  221. if (b == null) {
  222. return "(null)";
  223. }
  224. StringBuffer sb = new StringBuffer(b.length * 3);
  225. for (int i = 0; i < b.length; i++) {
  226. int k = b[i] & 0xff;
  227. if (i != 0) {
  228. sb.append(':');
  229. }
  230. sb.append(hexDigits[k >>> 4]);
  231. sb.append(hexDigits[k & 0xf]);
  232. }
  233. return sb.toString();
  234. }
  235. public static byte[] parse(String s) {
  236. if (s.equals("(null)")) {
  237. return null;
  238. }
  239. try {
  240. int n = s.length();
  241. ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3);
  242. StringReader r = new StringReader(s);
  243. while (true) {
  244. int b1 = nextNibble(r);
  245. if (b1 < 0) {
  246. break;
  247. }
  248. int b2 = nextNibble(r);
  249. if (b2 < 0) {
  250. throw new RuntimeException("Invalid string " + s);
  251. }
  252. int b = (b1 << 4) | b2;
  253. out.write(b);
  254. }
  255. return out.toByteArray();
  256. } catch (IOException e) {
  257. throw new RuntimeException(e);
  258. }
  259. }
  260. private static int nextNibble(StringReader r) throws IOException {
  261. while (true) {
  262. int ch = r.read();
  263. if (ch == -1) {
  264. return -1;
  265. } else if ((ch >= '0') && (ch <= '9')) {
  266. return ch - '0';
  267. } else if ((ch >= 'a') && (ch <= 'f')) {
  268. return ch - 'a' + 10;
  269. } else if ((ch >= 'A') && (ch <= 'F')) {
  270. return ch - 'A' + 10;
  271. }
  272. }
  273. }
  274. <T> T[] concat(T[] a, T[] b) {
  275. if ((b == null) || (b.length == 0)) {
  276. return a;
  277. }
  278. T[] r = (T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), a.length + b.length);
  279. System.arraycopy(a, 0, r, 0, a.length);
  280. System.arraycopy(b, 0, r, a.length, b.length);
  281. return r;
  282. }
  283. }