/* * Copyright (c) 2003, 2022, 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.
*/
// directory corresponding to BASE in the /closed hierarchy staticfinal String CLOSED_BASE;
static { // hack
String absBase = new File(BASE).getAbsolutePath(); int k = absBase.indexOf(SEP + "test" + SEP + "jdk" + SEP); if (k < 0) k = 0;
String p1 = absBase.substring(0, k);
String p2 = absBase.substring(k);
CLOSED_BASE = p1 + "/../closed" + p2;
// set it as a system property to make it available in policy file
System.setProperty("closed.base", CLOSED_BASE);
}
// NSS version info publicstaticenum ECCState { None, Basic, Extended }; staticdouble 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 simplier to keep nss_version // for quick checking for generic testing than many if-else statements. staticdouble softoken3_version = -1; staticdouble nss3_version = -1; static Provider pkcs11 = newPKCS11Provider();
publicstatic Provider newPKCS11Provider() {
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;
}
} catch (Exception | ServiceConfigurationError 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();
}
} return p;
}
// Return the static test SunPKCS11 provider configured with the specified config file static Provider getSunPKCS11(String config) throws Exception { return getSunPKCS11(config, pkcs11);
}
// Return the Provider p configured with the specified config file static Provider getSunPKCS11(String config, Provider p) throws Exception { if (p == null) { thrownew NoSuchProviderException("No PKCS11 provider available");
} return p.configure(config);
}
// 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);
}
}
}
publicstaticvoid main(PKCS11Test test, String[] args) throws Exception { if (args != null) { if (args.length > 0) { if ("sm".equals(args[0])) {
test.enableSM = true;
} else { thrownew RuntimeException("Unknown Command, use 'sm' as "
+ "first argument to enable security manager");
}
} if (test.enableSM) {
System.setProperty("java.security.policy",
(args.length > 1) ? BASE + SEP + args[1]
: DEFAULT_POLICY);
}
}
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);
}
}
}
}
publicstaticvoid 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);
}
publicstaticvoid 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);
}
}
}
/* Read the library to find out the verison */ staticvoid getNSSInfo() {
getNSSInfo(nss_library);
}
// Try to parse the version for the specified library. // Assuming the library contains either of the following patterns: // $Header: NSS <version> // Version: NSS <version> // Here, <version> stands for NSS version. staticdouble getNSSInfo(String library) { // look for two types of headers in NSS libraries
String nssHeader1 = "$Header: NSS";
String nssHeader2 = "Version: NSS"; boolean found = false;
String s = null; int i = 0;
Path libfile = null;
if (library.compareTo("softokn3") == 0 && softoken3_version > -1) return softoken3_version; if (library.compareTo("nss3") == 0 && nss3_version > -1) return nss3_version;
try {
libfile = getNSSLibPath(); if (libfile == null) { return 0.0;
} try (InputStream is = Files.newInputStream(libfile)) { byte[] data = newbyte[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, StandardCharsets.US_ASCII);
i = s.indexOf(nssHeader1); if (i > 0 || (i = s.indexOf(nssHeader2)) > 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;
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
if (!found) {
System.out.println("lib" + library + " version not found, set to 0.0: " + libfile);
nss_version = 0.0; return nss_version;
}
// the index after whitespace after nssHeader int afterheader = s.indexOf("NSS", i) + 4;
String version = String.valueOf(s.charAt(afterheader)); for (char c = s.charAt(++afterheader);
c == '.' || (c >= '0' && c <= '9');
c = s.charAt(++afterheader)) {
version += c;
}
// 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("===== Content start =====");
System.out.println(s);
System.out.println("===== Content end =====");
System.out.println("Failed to parse lib" + library + " version. Set to 0.0");
e.printStackTrace();
}
System.out.print("lib" + library + " version = "+version+". ");
// Used to set the nss_library file to search for libsoftokn3.so publicstaticvoid useNSS() {
nss_library = "nss3";
}
// Run NSS testing on a Provider p configured with test nss config publicstaticvoid testNSS(PKCS11Test test) throws Exception {
String nssConfig = getNssConfig(); if (nssConfig == null) { // issue loading libraries return;
}
Provider p = getSunPKCS11(nssConfig);
test.premain(p);
}
// Generate a vector of supported elliptic curves of a given provider static List<ECParameterSpec> getKnownCurves(Provider p) throws Exception { int index; int begin; int end;
String curve;
List<ECParameterSpec> results = new ArrayList<>(); // Get Curves to test from SunEC.
String kcProp = Security.getProvider("SunEC").
getProperty("AlgorithmParameters.EC SupportedCurves");
if (kcProp == null) { thrownew 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;
}
// 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)) { returntrue;
}
} returnfalse;
}
privatestatic Map<String,String[]> osMap;
// Location of the NSS libraries on each supported platform privatestatic Map<String, String[]> getOsMap() { if (osMap != null) { return osMap;
}
osMap = new HashMap<>();
osMap.put("Linux-i386-32", new String[] { "/usr/lib/i386-linux-gnu/", "/usr/lib32/", "/usr/lib/" });
osMap.put("Linux-amd64-64", new String[] { "/usr/lib/x86_64-linux-gnu/", "/usr/lib/x86_64-linux-gnu/nss/", "/usr/lib64/" });
osMap.put("Linux-ppc64-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("Windows-x86-32", new String[] {});
osMap.put("Windows-amd64-64", new String[] {});
osMap.put("MacOSX-x86_64-64", new String[] {});
osMap.put("Linux-arm-32", new String[] { "/usr/lib/arm-linux-gnueabi/nss/", "/usr/lib/arm-linux-gnueabihf/nss/" }); // Exclude linux-aarch64 at the moment until the following bug is fixed: // 8296631: NSS tests failing on OL9 linux-aarch64 hosts // osMap.put("Linux-aarch64-64", new String[] { // "/usr/lib/aarch64-linux-gnu/", // "/usr/lib/aarch64-linux-gnu/nss/", // "/usr/lib64/" }); return osMap;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.