8313206: PKCS11 tests silently skip execution

Reviewed-by: ssahoo, mullan
This commit is contained in:
Rajan Halade 2023-08-08 20:21:16 +00:00
parent 6864441163
commit 515add88ed
10 changed files with 268 additions and 184 deletions

View File

@ -58,6 +58,8 @@ import javax.security.auth.Subject;
import com.sun.security.auth.module.*; import com.sun.security.auth.module.*;
import com.sun.security.auth.callback.*; import com.sun.security.auth.callback.*;
import jtreg.SkippedException;
import org.testng.SkipException;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -113,7 +115,11 @@ public class Basic extends PKCS11Test {
@Test @Test
public void testBasic() throws Exception { public void testBasic() throws Exception {
String[] args = {"sm", "Basic.policy"}; String[] args = {"sm", "Basic.policy"};
main(new Basic(), args); try {
main(new Basic(), args);
} catch (SkippedException se) {
throw new SkipException("One or more tests are skipped");
}
} }
private static class FooEntry implements KeyStore.Entry { } private static class FooEntry implements KeyStore.Entry { }

View File

@ -38,6 +38,8 @@ import java.security.KeyStoreException;
import java.security.Provider; import java.security.Provider;
import java.security.cert.Certificate; import java.security.cert.Certificate;
import jtreg.SkippedException;
import org.testng.SkipException;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -74,7 +76,11 @@ public class CertChainRemoval extends PKCS11Test {
@Test @Test
public void test() throws Exception { public void test() throws Exception {
main(new CertChainRemoval()); try {
main(new CertChainRemoval());
} catch (SkippedException se) {
throw new SkipException("One or more tests are skipped");
}
} }
private static void printKeyStore(String header, KeyStore ks) private static void printKeyStore(String header, KeyStore ks)

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -29,6 +29,8 @@
* @run testng/othervm ClientAuth * @run testng/othervm ClientAuth
*/ */
import jtreg.SkippedException;
import org.testng.SkipException;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -119,7 +121,11 @@ public class ClientAuth extends PKCS11Test {
private void runTest(String[] args) throws Exception { private void runTest(String[] args) throws Exception {
System.out.println("Running with args: " + Arrays.toString(args)); System.out.println("Running with args: " + Arrays.toString(args));
parseArguments(args); parseArguments(args);
main(new ClientAuth()); try {
main(new ClientAuth());
} catch (SkippedException se) {
throw new SkipException("One or more tests are skipped");
}
} }
/* /*

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -26,6 +26,8 @@
* @library /test/lib .. * @library /test/lib ..
* @run testng/othervm SecretKeysBasic * @run testng/othervm SecretKeysBasic
*/ */
import jtreg.SkippedException;
import org.testng.SkipException;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -62,7 +64,11 @@ public class SecretKeysBasic extends PKCS11Test {
@Test @Test
public void testBasic() throws Exception { public void testBasic() throws Exception {
main(new SecretKeysBasic()); try {
main(new SecretKeysBasic());
} catch (SkippedException se) {
throw new SkipException("One or more tests are skipped");
}
} }
public void main(Provider p) throws Exception { public void main(Provider p) throws Exception {

View File

@ -62,27 +62,38 @@ import javax.crypto.spec.SecretKeySpec;
import jdk.test.lib.artifacts.Artifact; import jdk.test.lib.artifacts.Artifact;
import jdk.test.lib.artifacts.ArtifactResolver; import jdk.test.lib.artifacts.ArtifactResolver;
import jdk.test.lib.artifacts.ArtifactResolverException; import jdk.test.lib.artifacts.ArtifactResolverException;
import jtreg.SkippedException;
public abstract class PKCS11Test { public abstract class PKCS11Test {
private boolean enableSM = false;
static final Properties props = System.getProperties(); static final Properties props = System.getProperties();
static final String PKCS11 = "PKCS11"; static final String PKCS11 = "PKCS11";
// directory of the test source // directory of the test source
static final String BASE = System.getProperty("test.src", "."); static final String BASE = System.getProperty("test.src", ".");
static final String TEST_CLASSES = System.getProperty("test.classes", "."); static final String TEST_CLASSES = System.getProperty("test.classes", ".");
static final char SEP = File.separatorChar; static final char SEP = File.separatorChar;
private static final String DEFAULT_POLICY =
BASE + SEP + ".." + SEP + "policy";
// directory corresponding to BASE in the /closed hierarchy // directory corresponding to BASE in the /closed hierarchy
static final String CLOSED_BASE; static final String CLOSED_BASE;
private static final String DEFAULT_POLICY = BASE + SEP + ".." + SEP + "policy";
private static final String PKCS11_REL_PATH = "sun/security/pkcs11";
private static final char[] HEX_DIGITS = "0123456789abcdef".toCharArray();
private static final SecureRandom srdm = new SecureRandom();
static double nss_version = -1;
static ECCState nss_ecc_status = ECCState.Basic;
// 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";
// NSS versions of each library. It is simpler 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;
static Provider pkcs11 = newPKCS11Provider();
private static String PKCS11_BASE;
private static Map<String, String[]> osMap;
static { static {
// hack // hack
@ -97,24 +108,18 @@ public abstract class PKCS11Test {
System.setProperty("closed.base", CLOSED_BASE); System.setProperty("closed.base", CLOSED_BASE);
} }
// NSS version info static {
public static enum ECCState { None, Basic, Extended }; try {
static double nss_version = -1; PKCS11_BASE = getBase();
static ECCState nss_ecc_status = ECCState.Basic; } catch (Exception e) {
// ignore
}
}
// The NSS library we need to search for in getNSSLibDir() private boolean enableSM = false;
// Default is "libsoftokn3.so", listed as "softokn3"
// The other is "libnss3.so", listed as "nss3".
static String nss_library = "softokn3";
// 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;
static Provider pkcs11 = newPKCS11Provider();
public static Provider newPKCS11Provider() { public static Provider newPKCS11Provider() {
ServiceLoader sl = ServiceLoader.load(java.security.Provider.class); ServiceLoader<Provider> sl = ServiceLoader.load(java.security.Provider.class);
Iterator<Provider> iter = sl.iterator(); Iterator<Provider> iter = sl.iterator();
Provider p = null; Provider p = null;
boolean found = false; boolean found = false;
@ -132,8 +137,8 @@ public abstract class PKCS11Test {
// Nothing found through ServiceLoader; fall back to reflection // Nothing found through ServiceLoader; fall back to reflection
if (!found) { if (!found) {
try { try {
Class clazz = Class.forName("sun.security.pkcs11.SunPKCS11"); Class<?> clazz = Class.forName("sun.security.pkcs11.SunPKCS11");
p = (Provider) clazz.newInstance(); p = (Provider) clazz.getDeclaredConstructor().newInstance();
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
@ -154,38 +159,6 @@ public abstract class PKCS11Test {
return p.configure(config); return p.configure(config);
} }
public abstract void main(Provider p) throws Exception;
protected boolean skipTest(Provider p) {
return false;
}
private void premain(Provider p) throws Exception {
if (skipTest(p)) {
return;
}
// 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);
}
}
}
public static void main(PKCS11Test test) throws Exception { public static void main(PKCS11Test test) throws Exception {
main(test, null); main(test, null);
} }
@ -210,9 +183,39 @@ public abstract class PKCS11Test {
Provider[] oldProviders = Security.getProviders(); Provider[] oldProviders = Security.getProviders();
try { try {
System.out.println("Beginning test run " + test.getClass().getName() + "..."); System.out.println("Beginning test run " + test.getClass().getName() + "...");
testDefault(test); boolean skippedDefault = false;
testNSS(test); boolean skippedNSS = false;
testDeimos(test); boolean skippedDeimos = false;
// Use separate try-catch for each test to allow all test run
try {
testDefault(test);
} catch (SkippedException se) {
System.out.println("testDefault: Skipped");
skippedDefault = true;
se.printStackTrace(System.out);
}
try {
testNSS(test);
} catch (SkippedException se) {
System.out.println("testNSS: Skipped");
skippedNSS = true;
se.printStackTrace(System.out);
}
try {
testDeimos(test);
} catch (SkippedException se) {
System.out.println("testDeimos: Skipped");
skippedDeimos = true;
se.printStackTrace(System.out);
}
if (skippedDefault && skippedNSS && skippedDeimos) {
throw new SkippedException("All tests are skipped, check logs");
}
} finally { } finally {
// NOTE: Do not place a 'return' in any finally block // NOTE: Do not place a 'return' in any finally block
// as it will suppress exceptions and hide test failures. // as it will suppress exceptions and hide test failures.
@ -222,7 +225,7 @@ public abstract class PKCS11Test {
// useful for ./Provider/Login.sh, where a SecurityManager exists. // useful for ./Provider/Login.sh, where a SecurityManager exists.
if (oldProviders.length == newProviders.length) { if (oldProviders.length == newProviders.length) {
found = false; found = false;
for (int i = 0; i<oldProviders.length; i++) { for (int i = 0; i < oldProviders.length; i++) {
if (oldProviders[i] != newProviders[i]) { if (oldProviders[i] != newProviders[i]) {
found = true; found = true;
break; break;
@ -230,10 +233,10 @@ public abstract class PKCS11Test {
} }
} }
if (found) { if (found) {
for (Provider p: newProviders) { for (Provider p : newProviders) {
Security.removeProvider(p.getName()); Security.removeProvider(p.getName());
} }
for (Provider p: oldProviders) { for (Provider p : oldProviders) {
Security.addProvider(p); Security.addProvider(p);
} }
} }
@ -241,42 +244,49 @@ public abstract class PKCS11Test {
} }
public static void testDeimos(PKCS11Test test) throws Exception { public static void testDeimos(PKCS11Test test) throws Exception {
if (new File("/opt/SUNWconn/lib/libpkcs11.so").isFile() == false || System.out.println("===> testDeimos: Starting test run");
"true".equals(System.getProperty("NO_DEIMOS"))) { if ("true".equals(System.getProperty("NO_DEIMOS"))) {
System.out.println("Skip Deimos software as test configured with NO_DEIMOS");
return; return;
} }
if (!new File("/opt/SUNWconn/lib/libpkcs11.so").isFile()) {
throw new SkippedException("testDeimos: \"/opt/SUNWconn/lib/libpkcs11.so\" " +
"file required for Deimos not found");
}
String base = getBase(); String base = getBase();
String p11config = base + SEP + "nss" + SEP + "p11-deimos.txt"; String p11config = base + SEP + "nss" + SEP + "p11-deimos.txt";
Provider p = getSunPKCS11(p11config); Provider p = getSunPKCS11(p11config);
test.premain(p); test.premain(p);
System.out.println("testDeimos: Completed");
} }
// Run test for default configured PKCS11 providers (if any)
public static void testDefault(PKCS11Test test) throws Exception { public static void testDefault(PKCS11Test test) throws Exception {
// run test for default configured PKCS11 providers (if any) System.out.println("===> testDefault: Starting test run");
boolean foundPKCS11 = false;
if ("true".equals(System.getProperty("NO_DEFAULT"))) { if ("true".equals(System.getProperty("NO_DEFAULT"))) {
System.out.println("Skip default provider as test configured with NO_DEFAULT");
return; return;
} }
Provider[] providers = Security.getProviders(); Provider[] providers = Security.getProviders();
for (int i = 0; i < providers.length; i++) { for (Provider p : providers) {
Provider p = providers[i];
if (p.getName().startsWith("SunPKCS11-")) { if (p.getName().startsWith("SunPKCS11-")) {
foundPKCS11 = true;
test.premain(p); test.premain(p);
} }
} }
}
private static String PKCS11_BASE; if (!foundPKCS11) {
static { throw new SkippedException("testDefault: Skip default test as SunPKCS11 " +
try { "provider is not configured");
PKCS11_BASE = getBase();
} catch (Exception e) {
// ignore
} }
}
private final static String PKCS11_REL_PATH = "sun/security/pkcs11"; System.out.println("testDefault: Completed");
}
public static String getBase() throws Exception { public static String getBase() throws Exception {
if (PKCS11_BASE != null) { if (PKCS11_BASE != null) {
@ -290,7 +300,7 @@ public abstract class PKCS11Test {
} }
cwd = cwd.getParentFile(); cwd = cwd.getParentFile();
if (cwd == null) { if (cwd == null) {
throw new Exception("Test root directory not found"); throw new RuntimeException("Test root directory not found");
} }
} }
PKCS11_BASE = new File(cwd, PKCS11_REL_PATH.replace('/', SEP)).getAbsolutePath(); PKCS11_BASE = new File(cwd, PKCS11_REL_PATH.replace('/', SEP)).getAbsolutePath();
@ -322,7 +332,7 @@ public abstract class PKCS11Test {
String[] nssLibDirs = getNssLibPaths(osid); String[] nssLibDirs = getNssLibPaths(osid);
if (nssLibDirs == null) { if (nssLibDirs == null) {
System.out.println("Warning: unsupported OS: " + osid System.out.println("Warning: unsupported OS: " + osid
+ ", please initialize NSS librarys location firstly, skipping test"); + ", please initialize NSS library location, skipping test");
return null; return null;
} }
if (nssLibDirs.length == 0) { if (nssLibDirs.length == 0) {
@ -339,7 +349,7 @@ public abstract class PKCS11Test {
} }
} }
if (nssLibPath == null) { if (nssLibPath == null) {
System.out.println("Warning: can't find NSS librarys on this machine, skipping test"); System.out.println("Warning: can't find NSS library on this machine, skipping test");
return null; return null;
} }
return nssLibPath; return nssLibPath;
@ -352,9 +362,8 @@ public abstract class PKCS11Test {
} else if (osName.equals("Mac OS X")) { } else if (osName.equals("Mac OS X")) {
osName = "MacOSX"; osName = "MacOSX";
} }
String osid = osName + "-" + props.getProperty("os.arch") + "-" return osName + "-" + props.getProperty("os.arch") + "-"
+ props.getProperty("sun.arch.data.model"); + props.getProperty("sun.arch.data.model");
return osid;
} }
static boolean isBadNSSVersion(Provider p) { static boolean isBadNSSVersion(Provider p) {
@ -367,7 +376,7 @@ public abstract class PKCS11Test {
return false; return false;
} }
protected static void safeReload(String lib) throws Exception { protected static void safeReload(String lib) {
try { try {
System.load(lib); System.load(lib);
} catch (UnsatisfiedLinkError e) { } catch (UnsatisfiedLinkError e) {
@ -377,11 +386,9 @@ public abstract class PKCS11Test {
} }
} }
static boolean loadNSPR(String libdir) throws Exception { static boolean loadNSPR(String libdir) {
// load NSS softoken dependencies in advance to avoid resolver issues // load NSS softoken dependencies in advance to avoid resolver issues
String dir = libdir.endsWith(File.separator) String dir = libdir.endsWith(File.separator) ? libdir : libdir + File.separator;
? libdir
: libdir + File.separator;
safeReload(dir + System.mapLibraryName("nspr4")); safeReload(dir + System.mapLibraryName("nspr4"));
safeReload(dir + System.mapLibraryName("plc4")); safeReload(dir + System.mapLibraryName("plc4"));
safeReload(dir + System.mapLibraryName("plds4")); safeReload(dir + System.mapLibraryName("plds4"));
@ -392,7 +399,7 @@ public abstract class PKCS11Test {
// Check the provider being used is NSS // Check the provider being used is NSS
public static boolean isNSS(Provider p) { public static boolean isNSS(Provider p) {
return p.getName().toUpperCase().equals("SUNPKCS11-NSS"); return p.getName().equalsIgnoreCase("SUNPKCS11-NSS");
} }
static double getNSSVersion() { static double getNSSVersion() {
@ -488,25 +495,25 @@ public abstract class PKCS11Test {
// the index after whitespace after nssHeader // the index after whitespace after nssHeader
int afterheader = s.indexOf("NSS", i) + 4; int afterheader = s.indexOf("NSS", i) + 4;
String version = String.valueOf(s.charAt(afterheader)); StringBuilder version = new StringBuilder(String.valueOf(s.charAt(afterheader)));
for (char c = s.charAt(++afterheader); for (char c = s.charAt(++afterheader);
c == '.' || (c >= '0' && c <= '9'); c == '.' || (c >= '0' && c <= '9');
c = s.charAt(++afterheader)) { c = s.charAt(++afterheader)) {
version += c; version.append(c);
} }
// If a "dot dot" release, strip the extra dots for double parsing // If a "dot dot" release, strip the extra dots for double parsing
String[] dot = version.split("\\."); String[] dot = version.toString().split("\\.");
if (dot.length > 2) { if (dot.length > 2) {
version = dot[0]+"."+dot[1]; version = new StringBuilder(dot[0] + "." + dot[1]);
for (int j = 2; dot.length > j; j++) { for (int j = 2; dot.length > j; j++) {
version += dot[j]; version.append(dot[j]);
} }
} }
// Convert to double for easier version value checking // Convert to double for easier version value checking
try { try {
nss_version = Double.parseDouble(version); nss_version = Double.parseDouble(version.toString());
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
System.out.println("===== Content start ====="); System.out.println("===== Content start =====");
System.out.println(s); System.out.println(s);
@ -516,7 +523,7 @@ public abstract class PKCS11Test {
e.printStackTrace(); e.printStackTrace();
} }
System.out.print("lib" + library + " version = "+version+". "); System.out.print("library: " + library + ", version: " + version + ". ");
// Check for ECC // Check for ECC
if (s.indexOf("Basic") > 0) { if (s.indexOf("Basic") > 0) {
@ -545,13 +552,15 @@ public abstract class PKCS11Test {
// Run NSS testing on a Provider p configured with test nss config // Run NSS testing on a Provider p configured with test nss config
public static void testNSS(PKCS11Test test) throws Exception { public static void testNSS(PKCS11Test test) throws Exception {
System.out.println("===> testNSS: Starting test run");
String nssConfig = getNssConfig(); String nssConfig = getNssConfig();
if (nssConfig == null) { if (nssConfig == null) {
// issue loading libraries throw new SkippedException("testNSS: Problem loading NSS libraries");
return;
} }
Provider p = getSunPKCS11(nssConfig); Provider p = getSunPKCS11(nssConfig);
test.premain(p); test.premain(p);
System.out.println("testNSS: Completed");
} }
public static String getNssConfig() throws Exception { public static String getNssConfig() throws Exception {
@ -560,7 +569,7 @@ public abstract class PKCS11Test {
return null; return null;
} }
if (loadNSPR(libdir) == false) { if (!loadNSPR(libdir)) {
return null; return null;
} }
@ -598,12 +607,12 @@ public abstract class PKCS11Test {
if (kcProp == null) { if (kcProp == null) {
throw new RuntimeException( throw new RuntimeException(
"\"AlgorithmParameters.EC SupportedCurves property\" not found"); "\"AlgorithmParameters.EC SupportedCurves property\" not found");
} }
System.out.println("Finding supported curves using list from SunEC\n"); System.out.println("Finding supported curves using list from SunEC\n");
index = 0; index = 0;
for (;;) { for (; ; ) {
// Each set of curve names is enclosed with brackets. // Each set of curve names is enclosed with brackets.
begin = kcProp.indexOf('[', index); begin = kcProp.indexOf('[', index);
end = kcProp.indexOf(']', index); end = kcProp.indexOf(']', index);
@ -620,12 +629,12 @@ public abstract class PKCS11Test {
end = kcProp.indexOf(',', begin); end = kcProp.indexOf(',', begin);
if (end == -1) { if (end == -1) {
// Only one name in the set. // Only one name in the set.
end = index -1; end = index - 1;
} }
curve = kcProp.substring(begin, end); curve = kcProp.substring(begin, end);
getSupportedECParameterSpec(curve, p) getSupportedECParameterSpec(curve, p)
.ifPresent(spec -> results.add(spec)); .ifPresent(spec -> results.add(spec));
} }
if (results.size() == 0) { if (results.size() == 0) {
@ -636,9 +645,9 @@ public abstract class PKCS11Test {
} }
static Optional<ECParameterSpec> getSupportedECParameterSpec(String curve, static Optional<ECParameterSpec> getSupportedECParameterSpec(String curve,
Provider p) throws Exception { Provider p) throws Exception {
ECParameterSpec e = getECParameterSpec(p, curve); ECParameterSpec e = getECParameterSpec(p, curve);
System.out.print("\t "+ curve + ": "); System.out.print("\t " + curve + ": ");
try { try {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", p); KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", p);
kpg.initialize(e); kpg.initialize(e);
@ -660,26 +669,13 @@ public abstract class PKCS11Test {
throws Exception { throws Exception {
AlgorithmParameters parameters = AlgorithmParameters parameters =
AlgorithmParameters.getInstance("EC", p); AlgorithmParameters.getInstance("EC", p);
parameters.init(new ECGenParameterSpec(name)); parameters.init(new ECGenParameterSpec(name));
return parameters.getParameterSpec(ECParameterSpec.class); return parameters.getParameterSpec(ECParameterSpec.class);
} }
// Check support for a curve with a provided Vector of EC support
boolean checkSupport(List<ECParameterSpec> supportedEC,
ECParameterSpec curve) {
for (ECParameterSpec ec: supportedEC) {
if (ec.equals(curve)) {
return true;
}
}
return false;
}
private static Map<String,String[]> osMap;
// Location of the NSS libraries on each supported platform // Location of the NSS libraries on each supported platform
private static Map<String, String[]> getOsMap() { private static Map<String, String[]> getOsMap() {
if (osMap != null) { if (osMap != null) {
@ -687,23 +683,23 @@ public abstract class PKCS11Test {
} }
osMap = new HashMap<>(); osMap = new HashMap<>();
osMap.put("Linux-i386-32", new String[] { osMap.put("Linux-i386-32", new String[]{
"/usr/lib/i386-linux-gnu/", "/usr/lib/i386-linux-gnu/",
"/usr/lib32/", "/usr/lib32/",
"/usr/lib/" }); "/usr/lib/"});
osMap.put("Linux-amd64-64", new String[] { osMap.put("Linux-amd64-64", new String[]{
"/usr/lib/x86_64-linux-gnu/", "/usr/lib/x86_64-linux-gnu/",
"/usr/lib/x86_64-linux-gnu/nss/", "/usr/lib/x86_64-linux-gnu/nss/",
"/usr/lib64/" }); "/usr/lib64/"});
osMap.put("Linux-ppc64-64", new String[] { "/usr/lib64/" }); osMap.put("Linux-ppc64-64", new String[]{"/usr/lib64/"});
osMap.put("Linux-ppc64le-64", new String[] { "/usr/lib64/" }); osMap.put("Linux-ppc64le-64", new String[]{"/usr/lib64/"});
osMap.put("Linux-s390x-64", new String[] { "/usr/lib64/" }); osMap.put("Linux-s390x-64", new String[]{"/usr/lib64/"});
osMap.put("Windows-x86-32", new String[] {}); osMap.put("Windows-x86-32", new String[]{});
osMap.put("Windows-amd64-64", new String[] {}); osMap.put("Windows-amd64-64", new String[]{});
osMap.put("MacOSX-x86_64-64", new String[] {}); osMap.put("MacOSX-x86_64-64", new String[]{});
osMap.put("Linux-arm-32", new String[] { osMap.put("Linux-arm-32", new String[]{
"/usr/lib/arm-linux-gnueabi/nss/", "/usr/lib/arm-linux-gnueabi/nss/",
"/usr/lib/arm-linux-gnueabihf/nss/" }); "/usr/lib/arm-linux-gnueabihf/nss/"});
// Exclude linux-aarch64 at the moment until the following bug is fixed: // Exclude linux-aarch64 at the moment until the following bug is fixed:
// 8296631: NSS tests failing on OL9 linux-aarch64 hosts // 8296631: NSS tests failing on OL9 linux-aarch64 hosts
// osMap.put("Linux-aarch64-64", new String[] { // osMap.put("Linux-aarch64-64", new String[] {
@ -744,11 +740,9 @@ public abstract class PKCS11Test {
} }
} }
return nssLibPaths.toArray(new String[nssLibPaths.size()]); return nssLibPaths.toArray(new String[0]);
} }
private final static char[] hexDigits = "0123456789abcdef".toCharArray();
public static String toString(byte[] b) { public static String toString(byte[] b) {
if (b == null) { if (b == null) {
return "(null)"; return "(null)";
@ -759,8 +753,8 @@ public abstract class PKCS11Test {
if (i != 0) { if (i != 0) {
sb.append(':'); sb.append(':');
} }
sb.append(hexDigits[k >>> 4]); sb.append(HEX_DIGITS[k >>> 4]);
sb.append(hexDigits[k & 0xf]); sb.append(HEX_DIGITS[k & 0xf]);
} }
return sb.toString(); return sb.toString();
} }
@ -806,20 +800,11 @@ public abstract class PKCS11Test {
} }
} }
<T> T[] concat(T[] a, T[] b) {
if ((b == null) || (b.length == 0)) {
return a;
}
T[] r = Arrays.copyOf(a, a.length + b.length);
System.arraycopy(b, 0, r, a.length, b.length);
return r;
}
/** /**
* Returns supported algorithms of specified type. * Returns supported algorithms of specified type.
*/ */
static List<String> getSupportedAlgorithms(String type, String alg, static List<String> getSupportedAlgorithms(String type, String alg,
Provider p) { Provider p) {
// prepare a list of supported algorithms // prepare a list of supported algorithms
List<String> algorithms = new ArrayList<>(); List<String> algorithms = new ArrayList<>();
Set<Provider.Service> services = p.getServices(); Set<Provider.Service> services = p.getServices();
@ -832,8 +817,6 @@ public abstract class PKCS11Test {
return algorithms; return algorithms;
} }
private static final SecureRandom srdm = new SecureRandom();
static SecretKey generateKey(String alg, int keySize) { static SecretKey generateKey(String alg, int keySize) {
if (alg.contains("PBE")) { if (alg.contains("PBE")) {
return generateKeyPBE(alg, keySize); return generateKeyPBE(alg, keySize);
@ -861,22 +844,27 @@ public abstract class PKCS11Test {
public String getAlgorithm() { public String getAlgorithm() {
return "PBE"; return "PBE";
} }
@Override @Override
public String getFormat() { public String getFormat() {
return null; return null;
} }
@Override @Override
public byte[] getEncoded() { public byte[] getEncoded() {
throw new RuntimeException("Should not be called"); throw new RuntimeException("Should not be called");
} }
@Override @Override
public char[] getPassword() { public char[] getPassword() {
return pass; return pass;
} }
@Override @Override
public byte[] getSalt() { public byte[] getSalt() {
return salt; return salt;
} }
@Override @Override
public int getIterationCount() { public int getIterationCount() {
return iterations; return iterations;
@ -886,7 +874,7 @@ public abstract class PKCS11Test {
static byte[] generateData(int length) { static byte[] generateData(int length) {
byte data[] = new byte[length]; byte data[] = new byte[length];
for (int i=0; i<data.length; i++) { for (int i = 0; i < data.length; i++) {
data[i] = (byte) (i % 256); data[i] = (byte) (i % 256);
} }
return data; return data;
@ -894,20 +882,20 @@ public abstract class PKCS11Test {
private static String fetchNssLib(String osId) { private static String fetchNssLib(String osId) {
switch (osId) { switch (osId) {
case "Windows-x86-32": case "Windows-x86-32":
return fetchNssLib(WINDOWS_X86.class); return fetchNssLib(WINDOWS_X86.class);
case "Windows-amd64-64": case "Windows-amd64-64":
return fetchNssLib(WINDOWS_X64.class); return fetchNssLib(WINDOWS_X64.class);
case "MacOSX-x86_64-64": case "MacOSX-x86_64-64":
return fetchNssLib(MACOSX_X64.class); return fetchNssLib(MACOSX_X64.class);
case "Linux-amd64-64": case "Linux-amd64-64":
return fetchNssLib(LINUX_X64.class); return fetchNssLib(LINUX_X64.class);
default: default:
return null; return null;
} }
} }
@ -931,6 +919,58 @@ public abstract class PKCS11Test {
return path; return path;
} }
public abstract void main(Provider p) throws Exception;
protected boolean skipTest(Provider p) {
return false;
}
private void premain(Provider p) throws Exception {
if (skipTest(p)) {
return;
}
// 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);
}
}
}
// Check support for a curve with a provided Vector of EC support
boolean checkSupport(List<ECParameterSpec> supportedEC,
ECParameterSpec curve) {
for (ECParameterSpec ec : supportedEC) {
if (ec.equals(curve)) {
return true;
}
}
return false;
}
<T> T[] concat(T[] a, T[] b) {
if ((b == null) || (b.length == 0)) {
return a;
}
T[] r = Arrays.copyOf(a, a.length + b.length);
System.arraycopy(b, 0, r, a.length, b.length);
return r;
}
protected void setCommonSystemProps() { protected void setCommonSystemProps() {
System.setProperty("java.security.debug", "true"); System.setProperty("java.security.debug", "true");
System.setProperty("NO_DEIMOS", "true"); System.setProperty("NO_DEIMOS", "true");
@ -956,31 +996,38 @@ public abstract class PKCS11Test {
StandardCopyOption.REPLACE_EXISTING); StandardCopyOption.REPLACE_EXISTING);
} }
// NSS version info
public static enum ECCState {None, Basic, Extended}
@Artifact( @Artifact(
organization = "jpg.tests.jdk.nsslib", organization = "jpg.tests.jdk.nsslib",
name = "nsslib-windows_x64", name = "nsslib-windows_x64",
revision = "3.46-VS2017", revision = "3.46-VS2017",
extension = "zip") extension = "zip")
private static class WINDOWS_X64 { } private static class WINDOWS_X64 {
}
@Artifact( @Artifact(
organization = "jpg.tests.jdk.nsslib", organization = "jpg.tests.jdk.nsslib",
name = "nsslib-windows_x86", name = "nsslib-windows_x86",
revision = "3.46-VS2017", revision = "3.46-VS2017",
extension = "zip") extension = "zip")
private static class WINDOWS_X86 { } private static class WINDOWS_X86 {
}
@Artifact( @Artifact(
organization = "jpg.tests.jdk.nsslib", organization = "jpg.tests.jdk.nsslib",
name = "nsslib-macosx_x64", name = "nsslib-macosx_x64",
revision = "3.46", revision = "3.46",
extension = "zip") extension = "zip")
private static class MACOSX_X64 { } private static class MACOSX_X64 {
}
@Artifact( @Artifact(
organization = "jpg.tests.jdk.nsslib", organization = "jpg.tests.jdk.nsslib",
name = "nsslib-linux_x64", name = "nsslib-linux_x64",
revision = "3.46", revision = "3.46",
extension = "zip") extension = "zip")
private static class LINUX_X64 { } private static class LINUX_X64 {
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2004, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2004, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -28,6 +28,8 @@
* @run testng/othervm ConfigQuotedString * @run testng/othervm ConfigQuotedString
*/ */
import jtreg.SkippedException;
import org.testng.SkipException;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -44,7 +46,11 @@ public class ConfigQuotedString extends PKCS11Test {
@Test @Test
public void testQuotedString() throws Exception { public void testQuotedString() throws Exception {
main(new ConfigQuotedString()); try {
main(new ConfigQuotedString());
} catch (SkippedException se) {
throw new SkipException("One or more tests are skipped");
}
} }
public void main(Provider p) throws Exception { public void main(Provider p) throws Exception {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -28,6 +28,8 @@
* @run testng/othervm -Djava.security.manager=allow Login * @run testng/othervm -Djava.security.manager=allow Login
*/ */
import jtreg.SkippedException;
import org.testng.SkipException;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -55,7 +57,11 @@ public class Login extends PKCS11Test {
@Test @Test
public void testLogin() throws Exception { public void testLogin() throws Exception {
String[] args = new String[]{ "sm", "Login.policy"}; String[] args = new String[]{ "sm", "Login.policy"};
main(new Login(), args); try {
main(new Login(), args);
} catch (SkippedException se) {
throw new SkipException("One or more tests are skipped");
}
} }
public void main(Provider p) throws Exception { public void main(Provider p) throws Exception {

View File

@ -1,5 +1,5 @@
# #
# Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. # Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
# #
# This code is free software; you can redistribute it and/or modify it # This code is free software; you can redistribute it and/or modify it
@ -111,6 +111,7 @@ ${COMPILEJAVA}${FS}bin${FS}javac ${TESTJAVACOPTS} ${TESTTOOLVMOPTS} \
--add-modules jdk.crypto.cryptoki \ --add-modules jdk.crypto.cryptoki \
--add-exports jdk.crypto.cryptoki/sun.security.pkcs11=ALL-UNNAMED \ --add-exports jdk.crypto.cryptoki/sun.security.pkcs11=ALL-UNNAMED \
${TESTSRC}${FS}..${FS}..${FS}..${FS}..${FS}..${FS}lib${FS}jdk${FS}test${FS}lib${FS}artifacts${FS}*.java \ ${TESTSRC}${FS}..${FS}..${FS}..${FS}..${FS}..${FS}lib${FS}jdk${FS}test${FS}lib${FS}artifacts${FS}*.java \
${TESTSRC}${FS}..${FS}..${FS}..${FS}..${FS}..${FS}lib${FS}jtreg${FS}*.java \
${TESTSRC}${FS}MultipleLogins.java \ ${TESTSRC}${FS}MultipleLogins.java \
${TESTSRC}${FS}..${FS}PKCS11Test.java ${TESTSRC}${FS}..${FS}PKCS11Test.java

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -24,6 +24,8 @@
// common infrastructure for Secmod tests // common infrastructure for Secmod tests
import jtreg.SkippedException;
import java.io.*; import java.io.*;
import java.security.Provider; import java.security.Provider;
@ -43,13 +45,11 @@ public class SecmodTest extends PKCS11Test {
static boolean initSecmod() throws Exception { static boolean initSecmod() throws Exception {
useNSS(); useNSS();
LIBPATH = getNSSLibDir(); LIBPATH = getNSSLibDir();
if (LIBPATH == null) {
return false;
}
// load all the libraries except libnss3 into memory // load all the libraries except libnss3 into memory
if (loadNSPR(LIBPATH) == false) { if ((LIBPATH == null) || (!loadNSPR(LIBPATH))) {
return false; throw new SkippedException("Failed to load NSS libraries");
} }
safeReload(LIBPATH + System.mapLibraryName("softokn3")); safeReload(LIBPATH + System.mapLibraryName("softokn3"));
safeReload(LIBPATH + System.mapLibraryName("nssckbi")); safeReload(LIBPATH + System.mapLibraryName("nssckbi"));

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it