0N/A/*
2362N/A * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A
0N/A// common infrastructure for SunPKCS11 tests
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/Aimport java.lang.reflect.*;
0N/A
0N/Aimport java.security.*;
0N/A
0N/Apublic abstract class PKCS11Test {
0N/A
0N/A // directory of the test source
0N/A static final String BASE = System.getProperty("test.src", ".");
0N/A
0N/A static final char SEP = File.separatorChar;
0N/A
0N/A private final static String REL_CLOSED = "../../../../closed/sun/security/pkcs11".replace('/', SEP);
0N/A
0N/A // directory corresponding to BASE in the /closed hierarchy
0N/A static final String CLOSED_BASE;
0N/A
0N/A static {
0N/A // hack
0N/A String absBase = new File(BASE).getAbsolutePath();
0N/A int k = absBase.indexOf(SEP + "test" + SEP + "sun" + SEP);
0N/A if (k < 0) k = 0;
0N/A String p1 = absBase.substring(0, k + 6);
0N/A String p2 = absBase.substring(k + 5);
0N/A CLOSED_BASE = p1 + "closed" + p2;
0N/A }
0N/A
0N/A static String NSPR_PREFIX = "";
0N/A
0N/A static Provider getSunPKCS11(String config) throws Exception {
0N/A Class clazz = Class.forName("sun.security.pkcs11.SunPKCS11");
0N/A Constructor cons = clazz.getConstructor(new Class[] {String.class});
0N/A Object obj = cons.newInstance(new Object[] {config});
0N/A return (Provider)obj;
0N/A }
0N/A
0N/A public abstract void main(Provider p) throws Exception;
0N/A
0N/A private void premain(Provider p) throws Exception {
0N/A long start = System.currentTimeMillis();
0N/A System.out.println("Running test with provider " + p.getName() + "...");
0N/A main(p);
0N/A long stop = System.currentTimeMillis();
0N/A System.out.println("Completed test with provider " + p.getName() + " (" + (stop - start) + " ms).");
0N/A }
0N/A
0N/A public static void main(PKCS11Test test) throws Exception {
5361N/A Provider[] oldProviders = Security.getProviders();
5361N/A try {
5361N/A System.out.println("Beginning test run " + test.getClass().getName() + "...");
5361N/A testDefault(test);
5361N/A testNSS(test);
5361N/A testDeimos(test);
5361N/A } finally {
5361N/A Provider[] newProviders = Security.getProviders();
5361N/A // Do not restore providers if nothing changed. This is especailly
5361N/A // useful for ./Provider/Login.sh, where a SecurityManager exists.
5361N/A if (oldProviders.length == newProviders.length) {
5361N/A boolean found = false;
5361N/A for (int i = 0; i<oldProviders.length; i++) {
5361N/A if (oldProviders[i] != newProviders[i]) {
5361N/A found = true;
5361N/A break;
5361N/A }
5361N/A }
5361N/A if (!found) return;
5361N/A }
5361N/A for (Provider p: newProviders) {
5361N/A Security.removeProvider(p.getName());
5361N/A }
5361N/A for (Provider p: oldProviders) {
5361N/A Security.addProvider(p);
5361N/A }
5361N/A }
0N/A }
0N/A
0N/A public static void testDeimos(PKCS11Test test) throws Exception {
0N/A if (new File("/opt/SUNWconn/lib/libpkcs11.so").isFile() == false ||
0N/A "true".equals(System.getProperty("NO_DEIMOS"))) {
0N/A return;
0N/A }
0N/A String base = getBase();
0N/A String p11config = base + SEP + "nss" + SEP + "p11-deimos.txt";
0N/A Provider p = getSunPKCS11(p11config);
0N/A test.premain(p);
0N/A }
0N/A
0N/A public static void testDefault(PKCS11Test test) throws Exception {
0N/A // run test for default configured PKCS11 providers (if any)
0N/A
0N/A if ("true".equals(System.getProperty("NO_DEFAULT"))) {
0N/A return;
0N/A }
0N/A
0N/A Provider[] providers = Security.getProviders();
0N/A for (int i = 0; i < providers.length; i++) {
0N/A Provider p = providers[i];
0N/A if (p.getName().startsWith("SunPKCS11-")) {
0N/A test.premain(p);
0N/A }
0N/A }
0N/A }
0N/A
0N/A private static String PKCS11_BASE;
0N/A
0N/A private final static String PKCS11_REL_PATH = "sun/security/pkcs11";
0N/A
0N/A public static String getBase() throws Exception {
0N/A if (PKCS11_BASE != null) {
0N/A return PKCS11_BASE;
0N/A }
0N/A File cwd = new File(System.getProperty("test.src", ".")).getCanonicalFile();
0N/A while (true) {
0N/A File file = new File(cwd, "TEST.ROOT");
0N/A if (file.isFile()) {
0N/A break;
0N/A }
0N/A cwd = cwd.getParentFile();
0N/A if (cwd == null) {
0N/A throw new Exception("Test root directory not found");
0N/A }
0N/A }
0N/A PKCS11_BASE = new File(cwd, PKCS11_REL_PATH.replace('/', SEP)).getAbsolutePath();
0N/A return PKCS11_BASE;
0N/A }
0N/A
0N/A public static String getNSSLibDir() throws Exception {
0N/A Properties props = System.getProperties();
0N/A String osName = props.getProperty("os.name");
0N/A if (osName.startsWith("Win")) {
0N/A osName = "Windows";
0N/A NSPR_PREFIX = "lib";
0N/A }
0N/A String osid = osName + "-"
0N/A + props.getProperty("os.arch") + "-" + props.getProperty("sun.arch.data.model");
0N/A String ostype = osMap.get(osid);
0N/A if (ostype == null) {
0N/A System.out.println("Unsupported OS, skipping: " + osid);
0N/A return null;
0N/A// throw new Exception("Unsupported OS " + osid);
0N/A }
0N/A if (ostype.length() == 0) {
0N/A System.out.println("NSS not supported on this platform, skipping test");
0N/A return null;
0N/A }
0N/A String base = getBase();
0N/A String libdir = base + SEP + "nss" + SEP + "lib" + SEP + ostype + SEP;
0N/A System.setProperty("pkcs11test.nss.libdir", libdir);
0N/A return libdir;
0N/A }
0N/A
5361N/A protected static void safeReload(String lib) throws Exception {
5361N/A try {
5361N/A System.load(lib);
5361N/A } catch (UnsatisfiedLinkError e) {
5361N/A if (e.getMessage().contains("already loaded")) {
5361N/A return;
5361N/A }
5361N/A }
5361N/A }
5361N/A
0N/A static boolean loadNSPR(String libdir) throws Exception {
0N/A // load NSS softoken dependencies in advance to avoid resolver issues
5361N/A safeReload(libdir + System.mapLibraryName(NSPR_PREFIX + "nspr4"));
5361N/A safeReload(libdir + System.mapLibraryName(NSPR_PREFIX + "plc4"));
5361N/A safeReload(libdir + System.mapLibraryName(NSPR_PREFIX + "plds4"));
0N/A return true;
0N/A }
0N/A
0N/A public static void testNSS(PKCS11Test test) throws Exception {
0N/A String libdir = getNSSLibDir();
0N/A if (libdir == null) {
0N/A return;
0N/A }
0N/A String base = getBase();
0N/A
0N/A if (loadNSPR(libdir) == false) {
0N/A return;
0N/A }
0N/A
0N/A String libfile = libdir + System.mapLibraryName("softokn3");
0N/A
0N/A String customDBdir = System.getProperty("CUSTOM_DB_DIR");
0N/A String dbdir = (customDBdir != null) ?
0N/A customDBdir :
0N/A base + SEP + "nss" + SEP + "db";
0N/A // NSS always wants forward slashes for the config path
0N/A dbdir = dbdir.replace('\\', '/');
0N/A
0N/A String customConfig = System.getProperty("CUSTOM_P11_CONFIG");
0N/A String customConfigName = System.getProperty("CUSTOM_P11_CONFIG_NAME", "p11-nss.txt");
0N/A String p11config = (customConfig != null) ?
0N/A customConfig :
0N/A base + SEP + "nss" + SEP + customConfigName;
0N/A
0N/A System.setProperty("pkcs11test.nss.lib", libfile);
0N/A System.setProperty("pkcs11test.nss.db", dbdir);
0N/A Provider p = getSunPKCS11(p11config);
0N/A test.premain(p);
0N/A }
0N/A
0N/A
0N/A private static final Map<String,String> osMap;
0N/A
0N/A static {
0N/A osMap = new HashMap<String,String>();
0N/A osMap.put("SunOS-sparc-32", "solaris-sparc");
0N/A osMap.put("SunOS-sparcv9-64", "solaris-sparcv9");
0N/A osMap.put("SunOS-x86-32", "solaris-i586");
0N/A osMap.put("SunOS-amd64-64", "solaris-amd64");
0N/A osMap.put("Linux-i386-32", "linux-i586");
0N/A osMap.put("Linux-amd64-64", "linux-amd64");
0N/A osMap.put("Windows-x86-32", "windows-i586");
0N/A }
0N/A
0N/A private final static char[] hexDigits = "0123456789abcdef".toCharArray();
0N/A
0N/A public static String toString(byte[] b) {
0N/A if (b == null) {
0N/A return "(null)";
0N/A }
0N/A StringBuffer sb = new StringBuffer(b.length * 3);
0N/A for (int i = 0; i < b.length; i++) {
0N/A int k = b[i] & 0xff;
0N/A if (i != 0) {
0N/A sb.append(':');
0N/A }
0N/A sb.append(hexDigits[k >>> 4]);
0N/A sb.append(hexDigits[k & 0xf]);
0N/A }
0N/A return sb.toString();
0N/A }
0N/A
0N/A public static byte[] parse(String s) {
0N/A if (s.equals("(null)")) {
0N/A return null;
0N/A }
0N/A try {
0N/A int n = s.length();
0N/A ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3);
0N/A StringReader r = new StringReader(s);
0N/A while (true) {
0N/A int b1 = nextNibble(r);
0N/A if (b1 < 0) {
0N/A break;
0N/A }
0N/A int b2 = nextNibble(r);
0N/A if (b2 < 0) {
0N/A throw new RuntimeException("Invalid string " + s);
0N/A }
0N/A int b = (b1 << 4) | b2;
0N/A out.write(b);
0N/A }
0N/A return out.toByteArray();
0N/A } catch (IOException e) {
0N/A throw new RuntimeException(e);
0N/A }
0N/A }
0N/A
0N/A private static int nextNibble(StringReader r) throws IOException {
0N/A while (true) {
0N/A int ch = r.read();
0N/A if (ch == -1) {
0N/A return -1;
0N/A } else if ((ch >= '0') && (ch <= '9')) {
0N/A return ch - '0';
0N/A } else if ((ch >= 'a') && (ch <= 'f')) {
0N/A return ch - 'a' + 10;
0N/A } else if ((ch >= 'A') && (ch <= 'F')) {
0N/A return ch - 'A' + 10;
0N/A }
0N/A }
0N/A }
0N/A
0N/A <T> T[] concat(T[] a, T[] b) {
0N/A if ((b == null) || (b.length == 0)) {
0N/A return a;
0N/A }
0N/A T[] r = (T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), a.length + b.length);
0N/A System.arraycopy(a, 0, r, 0, a.length);
0N/A System.arraycopy(b, 0, r, a.length, b.length);
0N/A return r;
0N/A }
0N/A
0N/A}