2007-12-01 00:00:00 +00:00
|
|
|
/*
|
2016-01-26 21:32:07 +00:00
|
|
|
* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
|
2007-12-01 00:00:00 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2010-05-25 22:58:33 +00:00
|
|
|
* 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.
|
2007-12-01 00:00:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
// common infrastructure for SunPKCS11 tests
|
|
|
|
|
2016-01-26 21:32:07 +00:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.StringReader;
|
|
|
|
import java.security.AlgorithmParameters;
|
|
|
|
import java.security.InvalidAlgorithmParameterException;
|
|
|
|
import java.security.KeyPairGenerator;
|
|
|
|
import java.security.NoSuchProviderException;
|
|
|
|
import java.security.Provider;
|
|
|
|
import java.security.ProviderException;
|
|
|
|
import java.security.Security;
|
2013-12-04 18:59:17 +00:00
|
|
|
import java.security.spec.ECGenParameterSpec;
|
|
|
|
import java.security.spec.ECParameterSpec;
|
2016-01-26 21:32:07 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Properties;
|
|
|
|
import java.util.ServiceLoader;
|
|
|
|
import java.util.Set;
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
public abstract class PKCS11Test {
|
|
|
|
|
2016-01-26 21:32:07 +00:00
|
|
|
private boolean enableSM = false;
|
|
|
|
|
|
|
|
static final Properties props = System.getProperties();
|
|
|
|
|
2015-09-14 16:54:58 +00:00
|
|
|
static final String PKCS11 = "PKCS11";
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
// directory of the test source
|
|
|
|
static final String BASE = System.getProperty("test.src", ".");
|
|
|
|
|
|
|
|
static final char SEP = File.separatorChar;
|
|
|
|
|
2016-01-26 21:32:07 +00:00
|
|
|
private static final String DEFAULT_POLICY =
|
|
|
|
BASE + SEP + ".." + SEP + "policy";
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
// 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;
|
2016-01-26 21:32:07 +00:00
|
|
|
|
|
|
|
// set it as a system property to make it available in policy file
|
|
|
|
System.setProperty("closed.base", CLOSED_BASE);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static String NSPR_PREFIX = "";
|
|
|
|
|
2013-07-29 20:43:24 +00:00
|
|
|
// NSS version info
|
|
|
|
public static enum ECCState { None, Basic, Extended };
|
|
|
|
static double nss_version = -1;
|
|
|
|
static ECCState nss_ecc_status = ECCState.Extended;
|
|
|
|
|
2013-07-19 18:34:33 +00:00
|
|
|
// 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";
|
|
|
|
|
2014-06-10 19:04:42 +00:00
|
|
|
// NSS versions of each library. It is simplier to keep nss_version
|
|
|
|
// for quick checking for generic testing than many if-else statements.
|
|
|
|
static double softoken3_version = -1;
|
|
|
|
static double nss3_version = -1;
|
2015-06-26 21:34:34 +00:00
|
|
|
static Provider pkcs11;
|
2014-06-10 19:04:42 +00:00
|
|
|
|
2015-06-26 21:34:34 +00:00
|
|
|
// Goes through ServiceLoader instead of Provider.getInstance() since it
|
|
|
|
// works on all platforms
|
|
|
|
static {
|
|
|
|
ServiceLoader sl = ServiceLoader.load(java.security.Provider.class);
|
|
|
|
Iterator<Provider> iter = sl.iterator();
|
|
|
|
Provider p = null;
|
|
|
|
boolean found = false;
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
try {
|
|
|
|
p = iter.next();
|
|
|
|
if (p.getName().equals("SunPKCS11")) {
|
|
|
|
found = true;
|
|
|
|
break;
|
2016-01-26 21:32:07 +00:00
|
|
|
}
|
2015-06-26 21:34:34 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
// ignore and move on to the next one
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Nothing found through ServiceLoader; fall back to reflection
|
|
|
|
if (!found) {
|
|
|
|
try {
|
|
|
|
Class clazz = Class.forName("sun.security.pkcs11.SunPKCS11");
|
|
|
|
p = (Provider) clazz.newInstance();
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pkcs11 = p;
|
|
|
|
}
|
|
|
|
|
2016-01-26 21:32:07 +00:00
|
|
|
/*
|
|
|
|
* Use Solaris SPARC 11.2 or later to avoid an intermittent failure
|
|
|
|
* when running SunPKCS11-Solaris (8044554)
|
|
|
|
*/
|
|
|
|
static boolean isBadSolarisSparc(Provider p) {
|
|
|
|
if ("SunPKCS11-Solaris".equals(p.getName()) && badSolarisSparc) {
|
|
|
|
System.out.println("SunPKCS11-Solaris provider requires " +
|
|
|
|
"Solaris SPARC 11.2 or later, skipping");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-26 21:34:34 +00:00
|
|
|
// Return a SunPKCS11 provider configured with the specified config file
|
2007-12-01 00:00:00 +00:00
|
|
|
static Provider getSunPKCS11(String config) throws Exception {
|
2015-06-26 21:34:34 +00:00
|
|
|
if (pkcs11 == null) {
|
|
|
|
throw new NoSuchProviderException("No PKCS11 provider available");
|
|
|
|
}
|
|
|
|
return pkcs11.configure(config);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public abstract void main(Provider p) throws Exception;
|
|
|
|
|
|
|
|
private void premain(Provider p) throws Exception {
|
2016-01-26 21:32:07 +00:00
|
|
|
// set a security manager and policy before a test case runs,
|
|
|
|
// and disable them after the test case finished
|
|
|
|
try {
|
|
|
|
if (enableSM) {
|
|
|
|
System.setSecurityManager(new SecurityManager());
|
|
|
|
}
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
System.out.printf(
|
|
|
|
"Running test with provider %s (security manager %s) ...%n",
|
|
|
|
p.getName(), enableSM ? "enabled" : "disabled");
|
|
|
|
main(p);
|
|
|
|
long stop = System.currentTimeMillis();
|
|
|
|
System.out.println("Completed test with provider " + p.getName() +
|
|
|
|
" (" + (stop - start) + " ms).");
|
|
|
|
} finally {
|
|
|
|
if (enableSM) {
|
|
|
|
System.setSecurityManager(null);
|
|
|
|
}
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void main(PKCS11Test test) throws Exception {
|
2016-01-26 21:32:07 +00:00
|
|
|
main(test, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void main(PKCS11Test test, String[] args) throws Exception {
|
|
|
|
if (args != null) {
|
|
|
|
if (args.length > 0 && "sm".equals(args[0])) {
|
|
|
|
test.enableSM = true;
|
|
|
|
}
|
|
|
|
if (test.enableSM) {
|
|
|
|
System.setProperty("java.security.policy",
|
|
|
|
(args.length > 1) ? BASE + SEP + args[1]
|
|
|
|
: DEFAULT_POLICY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-12 04:26:31 +00:00
|
|
|
Provider[] oldProviders = Security.getProviders();
|
|
|
|
try {
|
|
|
|
System.out.println("Beginning test run " + test.getClass().getName() + "...");
|
|
|
|
testDefault(test);
|
|
|
|
testNSS(test);
|
|
|
|
testDeimos(test);
|
|
|
|
} finally {
|
2013-07-19 18:34:33 +00:00
|
|
|
// NOTE: Do not place a 'return' in any finally block
|
|
|
|
// as it will suppress exceptions and hide test failures.
|
2011-08-12 04:26:31 +00:00
|
|
|
Provider[] newProviders = Security.getProviders();
|
2013-07-19 18:34:33 +00:00
|
|
|
boolean found = true;
|
2011-08-12 04:26:31 +00:00
|
|
|
// Do not restore providers if nothing changed. This is especailly
|
|
|
|
// useful for ./Provider/Login.sh, where a SecurityManager exists.
|
|
|
|
if (oldProviders.length == newProviders.length) {
|
2013-07-19 18:34:33 +00:00
|
|
|
found = false;
|
2011-08-12 04:26:31 +00:00
|
|
|
for (int i = 0; i<oldProviders.length; i++) {
|
|
|
|
if (oldProviders[i] != newProviders[i]) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-19 18:34:33 +00:00
|
|
|
if (found) {
|
|
|
|
for (Provider p: newProviders) {
|
|
|
|
Security.removeProvider(p.getName());
|
|
|
|
}
|
|
|
|
for (Provider p: oldProviders) {
|
|
|
|
Security.addProvider(p);
|
|
|
|
}
|
2011-08-12 04:26:31 +00:00
|
|
|
}
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2012-03-26 16:14:20 +00:00
|
|
|
static {
|
|
|
|
try {
|
|
|
|
PKCS11_BASE = getBase();
|
|
|
|
} catch (Exception e) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
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 {
|
2014-06-10 19:04:42 +00:00
|
|
|
return getNSSLibDir(nss_library);
|
|
|
|
}
|
|
|
|
|
|
|
|
static String getNSSLibDir(String library) throws Exception {
|
2007-12-01 00:00:00 +00:00
|
|
|
String osName = props.getProperty("os.name");
|
|
|
|
if (osName.startsWith("Win")) {
|
|
|
|
osName = "Windows";
|
|
|
|
NSPR_PREFIX = "lib";
|
2014-10-06 15:44:57 +00:00
|
|
|
} else if (osName.equals("Mac OS X")) {
|
|
|
|
osName = "MacOSX";
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
String osid = osName + "-"
|
|
|
|
+ props.getProperty("os.arch") + "-" + props.getProperty("sun.arch.data.model");
|
2012-08-13 13:06:44 +00:00
|
|
|
String[] nssLibDirs = osMap.get(osid);
|
|
|
|
if (nssLibDirs == null) {
|
2007-12-01 00:00:00 +00:00
|
|
|
System.out.println("Unsupported OS, skipping: " + osid);
|
|
|
|
return null;
|
|
|
|
}
|
2012-08-13 13:06:44 +00:00
|
|
|
if (nssLibDirs.length == 0) {
|
2007-12-01 00:00:00 +00:00
|
|
|
System.out.println("NSS not supported on this platform, skipping test");
|
|
|
|
return null;
|
|
|
|
}
|
2012-08-13 13:06:44 +00:00
|
|
|
String nssLibDir = null;
|
|
|
|
for (String dir : nssLibDirs) {
|
2013-07-19 18:34:33 +00:00
|
|
|
if (new File(dir).exists() &&
|
2014-06-10 19:04:42 +00:00
|
|
|
new File(dir + System.mapLibraryName(library)).exists()) {
|
2012-08-13 13:06:44 +00:00
|
|
|
nssLibDir = dir;
|
|
|
|
System.setProperty("pkcs11test.nss.libdir", nssLibDir);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-03-26 16:14:20 +00:00
|
|
|
return nssLibDir;
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 21:32:07 +00:00
|
|
|
static boolean isBadNSSVersion(Provider p) {
|
|
|
|
if (isNSS(p) && badNSSVersion) {
|
|
|
|
System.out.println("NSS 3.11 has a DER issue that recent " +
|
|
|
|
"version do not.");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-08-12 04:26:31 +00:00
|
|
|
protected static void safeReload(String lib) throws Exception {
|
2007-12-01 00:00:00 +00:00
|
|
|
try {
|
2011-08-12 04:26:31 +00:00
|
|
|
System.load(lib);
|
2007-12-01 00:00:00 +00:00
|
|
|
} catch (UnsatisfiedLinkError e) {
|
2011-08-12 04:26:31 +00:00
|
|
|
if (e.getMessage().contains("already loaded")) {
|
|
|
|
return;
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
2011-08-12 04:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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"));
|
2012-03-26 16:14:20 +00:00
|
|
|
safeReload(libdir + System.mapLibraryName("sqlite3"));
|
|
|
|
safeReload(libdir + System.mapLibraryName("nssutil3"));
|
2007-12-01 00:00:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-29 20:43:24 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2014-06-10 19:04:42 +00:00
|
|
|
public static double getLibsoftokn3Version() {
|
|
|
|
if (softoken3_version == -1)
|
|
|
|
return getNSSInfo("softokn3");
|
|
|
|
return softoken3_version;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static double getLibnss3Version() {
|
|
|
|
if (nss3_version == -1)
|
|
|
|
return getNSSInfo("nss3");
|
|
|
|
return nss3_version;
|
|
|
|
}
|
|
|
|
|
2013-07-29 20:43:24 +00:00
|
|
|
/* Read the library to find out the verison */
|
|
|
|
static void getNSSInfo() {
|
2014-06-10 19:04:42 +00:00
|
|
|
getNSSInfo(nss_library);
|
|
|
|
}
|
|
|
|
|
|
|
|
static double getNSSInfo(String library) {
|
2013-07-29 20:43:24 +00:00
|
|
|
String nssHeader = "$Header: NSS";
|
|
|
|
boolean found = false;
|
|
|
|
String s = null;
|
|
|
|
int i = 0;
|
|
|
|
String libfile = "";
|
|
|
|
|
2014-06-10 19:04:42 +00:00
|
|
|
if (library.compareTo("softokn3") == 0 && softoken3_version > -1)
|
|
|
|
return softoken3_version;
|
|
|
|
if (library.compareTo("nss3") == 0 && nss3_version > -1)
|
|
|
|
return nss3_version;
|
|
|
|
|
2013-07-29 20:43:24 +00:00
|
|
|
try {
|
2014-06-10 19:04:42 +00:00
|
|
|
libfile = getNSSLibDir() + System.mapLibraryName(library);
|
2016-01-26 21:32:07 +00:00
|
|
|
try (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);
|
|
|
|
}
|
2013-07-29 20:43:24 +00:00
|
|
|
|
2016-01-26 21:32:07 +00:00
|
|
|
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;
|
|
|
|
}
|
2013-07-29 20:43:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
2014-06-10 19:04:42 +00:00
|
|
|
System.out.println("lib" + library +
|
|
|
|
" version not found, set to 0.0: " + libfile);
|
2013-07-29 20:43:24 +00:00
|
|
|
nss_version = 0.0;
|
2014-06-10 19:04:42 +00:00
|
|
|
return nss_version;
|
2013-07-29 20:43:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2014-06-10 19:04:42 +00:00
|
|
|
System.out.println("Failed to parse lib" + library +
|
|
|
|
" version. Set to 0.0");
|
2013-07-29 20:43:24 +00:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2014-06-10 19:04:42 +00:00
|
|
|
System.out.print("lib" + library + " version = "+version+". ");
|
2013-07-29 20:43:24 +00:00
|
|
|
|
|
|
|
// 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.");
|
2014-06-10 19:04:42 +00:00
|
|
|
} else {
|
|
|
|
System.out.println("ECC None.");
|
2013-07-29 20:43:24 +00:00
|
|
|
}
|
2014-06-10 19:04:42 +00:00
|
|
|
|
|
|
|
if (library.compareTo("softokn3") == 0) {
|
|
|
|
softoken3_version = nss_version;
|
|
|
|
} else if (library.compareTo("nss3") == 0) {
|
|
|
|
nss3_version = nss_version;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nss_version;
|
2013-07-29 20:43:24 +00:00
|
|
|
}
|
|
|
|
|
2013-07-19 18:34:33 +00:00
|
|
|
// Used to set the nss_library file to search for libsoftokn3.so
|
|
|
|
public static void useNSS() {
|
|
|
|
nss_library = "nss3";
|
|
|
|
}
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
public static void testNSS(PKCS11Test test) throws Exception {
|
|
|
|
String libdir = getNSSLibDir();
|
|
|
|
if (libdir == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
String base = getBase();
|
|
|
|
|
|
|
|
if (loadNSPR(libdir) == false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-19 18:34:33 +00:00
|
|
|
String libfile = libdir + System.mapLibraryName(nss_library);
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-12-04 18:59:17 +00:00
|
|
|
// Generate a vector of supported elliptic curves of a given provider
|
2016-01-26 21:32:07 +00:00
|
|
|
static List<ECParameterSpec> getKnownCurves(Provider p) throws Exception {
|
2013-12-04 18:59:17 +00:00
|
|
|
int index;
|
|
|
|
int begin;
|
|
|
|
int end;
|
|
|
|
String curve;
|
|
|
|
|
2016-01-26 21:32:07 +00:00
|
|
|
List<ECParameterSpec> results = new ArrayList<>();
|
2013-12-04 18:59:17 +00:00
|
|
|
// Get Curves to test from SunEC.
|
|
|
|
String kcProp = Security.getProvider("SunEC").
|
|
|
|
getProperty("AlgorithmParameters.EC SupportedCurves");
|
|
|
|
|
|
|
|
if (kcProp == null) {
|
|
|
|
throw new RuntimeException(
|
|
|
|
"\"AlgorithmParameters.EC SupportedCurves property\" not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
System.out.println("Finding supported curves using list from SunEC\n");
|
|
|
|
index = 0;
|
|
|
|
for (;;) {
|
|
|
|
// Each set of curve names is enclosed with brackets.
|
|
|
|
begin = kcProp.indexOf('[', index);
|
|
|
|
end = kcProp.indexOf(']', index);
|
|
|
|
if (begin == -1 || end == -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Each name is separated by a comma.
|
|
|
|
* Just get the first name in the set.
|
|
|
|
*/
|
|
|
|
index = end + 1;
|
|
|
|
begin++;
|
|
|
|
end = kcProp.indexOf(',', begin);
|
|
|
|
if (end == -1) {
|
|
|
|
// Only one name in the set.
|
|
|
|
end = index -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
curve = kcProp.substring(begin, end);
|
|
|
|
ECParameterSpec e = getECParameterSpec(p, curve);
|
|
|
|
System.out.print("\t "+ curve + ": ");
|
|
|
|
try {
|
|
|
|
KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", p);
|
|
|
|
kpg.initialize(e);
|
2016-01-26 21:32:07 +00:00
|
|
|
kpg.generateKeyPair();
|
2013-12-04 18:59:17 +00:00
|
|
|
results.add(e);
|
|
|
|
System.out.println("Supported");
|
|
|
|
} catch (ProviderException ex) {
|
|
|
|
System.out.println("Unsupported: PKCS11: " +
|
|
|
|
ex.getCause().getMessage());
|
|
|
|
} catch (InvalidAlgorithmParameterException ex) {
|
|
|
|
System.out.println("Unsupported: Key Length: " +
|
|
|
|
ex.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (results.size() == 0) {
|
|
|
|
throw new RuntimeException("No supported EC curves found");
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static ECParameterSpec getECParameterSpec(Provider p, String name)
|
|
|
|
throws Exception {
|
|
|
|
|
|
|
|
AlgorithmParameters parameters =
|
|
|
|
AlgorithmParameters.getInstance("EC", p);
|
|
|
|
|
|
|
|
parameters.init(new ECGenParameterSpec(name));
|
|
|
|
|
|
|
|
return parameters.getParameterSpec(ECParameterSpec.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check support for a curve with a provided Vector of EC support
|
2016-01-26 21:32:07 +00:00
|
|
|
boolean checkSupport(List<ECParameterSpec> supportedEC,
|
2013-12-04 18:59:17 +00:00
|
|
|
ECParameterSpec curve) {
|
|
|
|
for (ECParameterSpec ec: supportedEC) {
|
|
|
|
if (ec.equals(curve)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2012-08-13 13:06:44 +00:00
|
|
|
private static final Map<String,String[]> osMap;
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2012-03-26 16:14:20 +00:00
|
|
|
// Location of the NSS libraries on each supported platform
|
2007-12-01 00:00:00 +00:00
|
|
|
static {
|
2016-01-26 21:32:07 +00:00
|
|
|
osMap = new HashMap<>();
|
2012-08-13 13:06:44 +00:00
|
|
|
osMap.put("SunOS-sparc-32", new String[]{"/usr/lib/mps/"});
|
|
|
|
osMap.put("SunOS-sparcv9-64", new String[]{"/usr/lib/mps/64/"});
|
|
|
|
osMap.put("SunOS-x86-32", new String[]{"/usr/lib/mps/"});
|
|
|
|
osMap.put("SunOS-amd64-64", new String[]{"/usr/lib/mps/64/"});
|
|
|
|
osMap.put("Linux-i386-32", new String[]{
|
2014-05-29 14:50:36 +00:00
|
|
|
"/usr/lib/i386-linux-gnu/", "/usr/lib32/", "/usr/lib/"});
|
2012-08-13 13:06:44 +00:00
|
|
|
osMap.put("Linux-amd64-64", new String[]{
|
2013-07-19 18:34:33 +00:00
|
|
|
"/usr/lib/x86_64-linux-gnu/", "/usr/lib/x86_64-linux-gnu/nss/",
|
|
|
|
"/usr/lib64/"});
|
2015-11-02 13:57:04 +00:00
|
|
|
osMap.put("Linux-ppc64-64", new String[]{"/usr/lib64/"});
|
2015-12-11 18:45:58 +00:00
|
|
|
osMap.put("Linux-ppc64le-64", new String[]{"/usr/lib64/"});
|
2012-08-13 13:06:44 +00:00
|
|
|
osMap.put("Windows-x86-32", new String[]{
|
|
|
|
PKCS11_BASE + "/nss/lib/windows-i586/".replace('/', SEP)});
|
|
|
|
osMap.put("Windows-amd64-64", new String[]{
|
|
|
|
PKCS11_BASE + "/nss/lib/windows-amd64/".replace('/', SEP)});
|
2014-10-06 15:44:57 +00:00
|
|
|
osMap.put("MacOSX-x86_64-64", new String[]{
|
|
|
|
PKCS11_BASE + "/nss/lib/macosx-x86_64/"});
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private final static char[] hexDigits = "0123456789abcdef".toCharArray();
|
|
|
|
|
2016-01-26 21:32:07 +00:00
|
|
|
static final boolean badNSSVersion =
|
|
|
|
getNSSVersion() >= 3.11 && getNSSVersion() < 3.12;
|
|
|
|
|
|
|
|
static final boolean badSolarisSparc =
|
|
|
|
System.getProperty("os.name").equals("SunOS") &&
|
|
|
|
System.getProperty("os.arch").equals("sparcv9") &&
|
|
|
|
System.getProperty("os.version").compareTo("5.11") <= 0 &&
|
|
|
|
getDistro().compareTo("11.2") < 0;
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
public static String toString(byte[] b) {
|
|
|
|
if (b == null) {
|
|
|
|
return "(null)";
|
|
|
|
}
|
2016-01-26 21:32:07 +00:00
|
|
|
StringBuilder sb = new StringBuilder(b.length * 3);
|
2007-12-01 00:00:00 +00:00
|
|
|
for (int i = 0; i < b.length; i++) {
|
|
|
|
int k = b[i] & 0xff;
|
|
|
|
if (i != 0) {
|
|
|
|
sb.append(':');
|
|
|
|
}
|
|
|
|
sb.append(hexDigits[k >>> 4]);
|
|
|
|
sb.append(hexDigits[k & 0xf]);
|
|
|
|
}
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static byte[] parse(String s) {
|
|
|
|
if (s.equals("(null)")) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
int n = s.length();
|
|
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3);
|
|
|
|
StringReader r = new StringReader(s);
|
|
|
|
while (true) {
|
|
|
|
int b1 = nextNibble(r);
|
|
|
|
if (b1 < 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
int b2 = nextNibble(r);
|
|
|
|
if (b2 < 0) {
|
|
|
|
throw new RuntimeException("Invalid string " + s);
|
|
|
|
}
|
|
|
|
int b = (b1 << 4) | b2;
|
|
|
|
out.write(b);
|
|
|
|
}
|
|
|
|
return out.toByteArray();
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int nextNibble(StringReader r) throws IOException {
|
|
|
|
while (true) {
|
|
|
|
int ch = r.read();
|
|
|
|
if (ch == -1) {
|
|
|
|
return -1;
|
|
|
|
} else if ((ch >= '0') && (ch <= '9')) {
|
|
|
|
return ch - '0';
|
|
|
|
} else if ((ch >= 'a') && (ch <= 'f')) {
|
|
|
|
return ch - 'a' + 10;
|
|
|
|
} else if ((ch >= 'A') && (ch <= 'F')) {
|
|
|
|
return ch - 'A' + 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
<T> T[] concat(T[] a, T[] b) {
|
|
|
|
if ((b == null) || (b.length == 0)) {
|
|
|
|
return a;
|
|
|
|
}
|
2015-06-26 21:34:34 +00:00
|
|
|
T[] r = Arrays.copyOf(a, a.length + b.length);
|
2007-12-01 00:00:00 +00:00
|
|
|
System.arraycopy(b, 0, r, a.length, b.length);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2015-01-13 11:26:34 +00:00
|
|
|
/**
|
|
|
|
* Returns supported algorithms of specified type.
|
|
|
|
*/
|
|
|
|
static List<String> getSupportedAlgorithms(String type, String alg,
|
|
|
|
Provider p) {
|
|
|
|
// prepare a list of supported algorithms
|
|
|
|
List<String> algorithms = new ArrayList<>();
|
|
|
|
Set<Provider.Service> services = p.getServices();
|
|
|
|
for (Provider.Service service : services) {
|
|
|
|
if (service.getType().equals(type)
|
|
|
|
&& service.getAlgorithm().startsWith(alg)) {
|
|
|
|
algorithms.add(service.getAlgorithm());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return algorithms;
|
|
|
|
}
|
|
|
|
|
2015-08-12 13:38:09 +00:00
|
|
|
/**
|
|
|
|
* Get the identifier for the operating system distribution
|
|
|
|
*/
|
2016-01-26 21:32:07 +00:00
|
|
|
static String getDistro() {
|
2015-08-12 13:38:09 +00:00
|
|
|
try (BufferedReader in =
|
|
|
|
new BufferedReader(new InputStreamReader(
|
|
|
|
Runtime.getRuntime().exec("uname -v").getInputStream()))) {
|
|
|
|
|
|
|
|
return in.readLine();
|
|
|
|
} catch (Exception e) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
2015-09-14 16:54:58 +00:00
|
|
|
|
|
|
|
static byte[] generateData(int length) {
|
|
|
|
byte data[] = new byte[length];
|
|
|
|
for (int i=0; i<data.length; i++) {
|
|
|
|
data[i] = (byte) (i % 256);
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|