|
Java example source code file (PKCS11Test.java)
The PKCS11Test.java Java example source code/* * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ // common infrastructure for SunPKCS11 tests import java.io.*; import java.util.*; import java.lang.reflect.*; import java.security.*; import java.security.spec.ECGenParameterSpec; import java.security.spec.ECParameterSpec; public abstract class PKCS11Test { // directory of the test source static final String BASE = System.getProperty("test.src", "."); static final char SEP = File.separatorChar; private final static String REL_CLOSED = "../../../../closed/sun/security/pkcs11".replace('/', SEP); // directory corresponding to BASE in the /closed hierarchy static final String CLOSED_BASE; static { // hack String absBase = new File(BASE).getAbsolutePath(); int k = absBase.indexOf(SEP + "test" + SEP + "sun" + SEP); if (k < 0) k = 0; String p1 = absBase.substring(0, k + 6); String p2 = absBase.substring(k + 5); CLOSED_BASE = p1 + "closed" + p2; } static String NSPR_PREFIX = ""; // NSS version info public static enum ECCState { None, Basic, Extended }; static double nss_version = -1; static ECCState nss_ecc_status = ECCState.Extended; // The NSS library we need to search for in getNSSLibDir() // Default is "libsoftokn3.so", listed as "softokn3" // The other is "libnss3.so", listed as "nss3". static String nss_library = "softokn3"; static Provider getSunPKCS11(String config) throws Exception { Class clazz = Class.forName("sun.security.pkcs11.SunPKCS11"); Constructor cons = clazz.getConstructor(new Class[] {String.class}); Object obj = cons.newInstance(new Object[] {config}); return (Provider)obj; } public abstract void main(Provider p) throws Exception; private void premain(Provider p) throws Exception { long start = System.currentTimeMillis(); System.out.println("Running test with provider " + p.getName() + "..."); main(p); long stop = System.currentTimeMillis(); System.out.println("Completed test with provider " + p.getName() + " (" + (stop - start) + " ms)."); } public static void main(PKCS11Test test) throws Exception { Provider[] oldProviders = Security.getProviders(); try { System.out.println("Beginning test run " + test.getClass().getName() + "..."); testDefault(test); testNSS(test); testDeimos(test); } finally { // NOTE: Do not place a 'return' in any finally block // as it will suppress exceptions and hide test failures. Provider[] newProviders = Security.getProviders(); boolean found = true; // Do not restore providers if nothing changed. This is especailly // useful for ./Provider/Login.sh, where a SecurityManager exists. if (oldProviders.length == newProviders.length) { found = false; for (int i = 0; i<oldProviders.length; i++) { if (oldProviders[i] != newProviders[i]) { found = true; break; } } } if (found) { for (Provider p: newProviders) { Security.removeProvider(p.getName()); } for (Provider p: oldProviders) { Security.addProvider(p); } } } } public static void testDeimos(PKCS11Test test) throws Exception { if (new File("/opt/SUNWconn/lib/libpkcs11.so").isFile() == false || "true".equals(System.getProperty("NO_DEIMOS"))) { return; } String base = getBase(); String p11config = base + SEP + "nss" + SEP + "p11-deimos.txt"; Provider p = getSunPKCS11(p11config); test.premain(p); } public static void testDefault(PKCS11Test test) throws Exception { // run test for default configured PKCS11 providers (if any) if ("true".equals(System.getProperty("NO_DEFAULT"))) { return; } Provider[] providers = Security.getProviders(); for (int i = 0; i < providers.length; i++) { Provider p = providers[i]; if (p.getName().startsWith("SunPKCS11-")) { test.premain(p); } } } private static String PKCS11_BASE; static { try { PKCS11_BASE = getBase(); } catch (Exception e) { // ignore } } private final static String PKCS11_REL_PATH = "sun/security/pkcs11"; public static String getBase() throws Exception { if (PKCS11_BASE != null) { return PKCS11_BASE; } File cwd = new File(System.getProperty("test.src", ".")).getCanonicalFile(); while (true) { File file = new File(cwd, "TEST.ROOT"); if (file.isFile()) { break; } cwd = cwd.getParentFile(); if (cwd == null) { throw new Exception("Test root directory not found"); } } PKCS11_BASE = new File(cwd, PKCS11_REL_PATH.replace('/', SEP)).getAbsolutePath(); return PKCS11_BASE; } public static String getNSSLibDir() throws Exception { Properties props = System.getProperties(); String osName = props.getProperty("os.name"); if (osName.startsWith("Win")) { osName = "Windows"; NSPR_PREFIX = "lib"; } String osid = osName + "-" + props.getProperty("os.arch") + "-" + props.getProperty("sun.arch.data.model"); String[] nssLibDirs = osMap.get(osid); if (nssLibDirs == null) { System.out.println("Unsupported OS, skipping: " + osid); return null; } if (nssLibDirs.length == 0) { System.out.println("NSS not supported on this platform, skipping test"); return null; } String nssLibDir = null; for (String dir : nssLibDirs) { if (new File(dir).exists() && new File(dir + System.mapLibraryName(nss_library)).exists()) { nssLibDir = dir; System.setProperty("pkcs11test.nss.libdir", nssLibDir); break; } } return nssLibDir; } protected static void safeReload(String lib) throws Exception { try { System.load(lib); } catch (UnsatisfiedLinkError e) { if (e.getMessage().contains("already loaded")) { return; } } } static boolean loadNSPR(String libdir) throws Exception { // load NSS softoken dependencies in advance to avoid resolver issues safeReload(libdir + System.mapLibraryName(NSPR_PREFIX + "nspr4")); safeReload(libdir + System.mapLibraryName(NSPR_PREFIX + "plc4")); safeReload(libdir + System.mapLibraryName(NSPR_PREFIX + "plds4")); safeReload(libdir + System.mapLibraryName("sqlite3")); safeReload(libdir + System.mapLibraryName("nssutil3")); return true; } // Check the provider being used is NSS public static boolean isNSS(Provider p) { return p.getName().toUpperCase().equals("SUNPKCS11-NSS"); } static double getNSSVersion() { if (nss_version == -1) getNSSInfo(); return nss_version; } static ECCState getNSSECC() { if (nss_version == -1) getNSSInfo(); return nss_ecc_status; } /* Read the library to find out the verison */ static void getNSSInfo() { String nssHeader = "$Header: NSS"; boolean found = false; String s = null; int i = 0; String libfile = ""; try { libfile = getNSSLibDir() + System.mapLibraryName(nss_library); FileInputStream is = new FileInputStream(libfile); byte[] data = new byte[1000]; int read = 0; while (is.available() > 0) { if (read == 0) { read = is.read(data, 0, 1000); } else { // Prepend last 100 bytes in case the header was split // between the reads. System.arraycopy(data, 900, data, 0, 100); read = 100 + is.read(data, 100, 900); } s = new String(data, 0, read); if ((i = s.indexOf(nssHeader)) > 0) { found = true; // If the nssHeader is before 920 we can break, otherwise // we may not have the whole header so do another read. If // no bytes are in the stream, that is ok, found is true. if (i < 920) { break; } } } is.close(); } catch (Exception e) { e.printStackTrace(); } if (!found) { System.out.println("NSS version not found, set to 0.0: "+libfile); nss_version = 0.0; return; } // the index after whitespace after nssHeader int afterheader = s.indexOf("NSS", i) + 4; String version = s.substring(afterheader, s.indexOf(' ', afterheader)); // If a "dot dot" release, strip the extra dots for double parsing String[] dot = version.split("\\."); if (dot.length > 2) { version = dot[0]+"."+dot[1]; for (int j = 2; dot.length > j; j++) { version += dot[j]; } } // Convert to double for easier version value checking try { nss_version = Double.parseDouble(version); } catch (NumberFormatException e) { System.out.println("Failed to parse NSS version. Set to 0.0"); e.printStackTrace(); } System.out.print("NSS version = "+version+". "); // Check for ECC if (s.indexOf("Basic") > 0) { nss_ecc_status = ECCState.Basic; System.out.println("ECC Basic."); } else if (s.indexOf("Extended") > 0) { nss_ecc_status = ECCState.Extended; System.out.println("ECC Extended."); } } // Used to set the nss_library file to search for libsoftokn3.so public static void useNSS() { nss_library = "nss3"; } public static void testNSS(PKCS11Test test) throws Exception { String libdir = getNSSLibDir(); if (libdir == null) { return; } String base = getBase(); if (loadNSPR(libdir) == false) { return; } String libfile = libdir + System.mapLibraryName(nss_library); String customDBdir = System.getProperty("CUSTOM_DB_DIR"); String dbdir = (customDBdir != null) ? customDBdir : base + SEP + "nss" + SEP + "db"; // NSS always wants forward slashes for the config path dbdir = dbdir.replace('\\', '/'); String customConfig = System.getProperty("CUSTOM_P11_CONFIG"); String customConfigName = System.getProperty("CUSTOM_P11_CONFIG_NAME", "p11-nss.txt"); String p11config = (customConfig != null) ? customConfig : base + SEP + "nss" + SEP + customConfigName; System.setProperty("pkcs11test.nss.lib", libfile); System.setProperty("pkcs11test.nss.db", dbdir); Provider p = getSunPKCS11(p11config); test.premain(p); } // Generate a vector of supported elliptic curves of a given provider static Vector<ECParameterSpec> getKnownCurves(Provider p) throws Exception { int index; int begin; int end; String curve; KeyPair kp = null; Vector<ECParameterSpec> results = new Vector Other Java examples (source code examples)Here is a short list of links related to this Java PKCS11Test.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.