undefect. CWE-407 — 63 sites patched across 27 ecosystems

Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com

Patches, unit tests, benchmarks, whitepaper, and outreach briefs.
Public domain — no copyright claimed. Use freely.
This commit is contained in:
russell@unturf.com 2026-03-26 17:11:57 -04:00
commit 0a580b313d
70422 changed files with 17213626 additions and 0 deletions

View file

@ -0,0 +1,697 @@
/*
* Copyright (c) 2016, 2024, 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.
*/
import java.security.*;
import java.security.cert.*;
import javax.crypto.*;
import javax.net.ssl.*;
import javax.security.auth.login.*;
import java.lang.reflect.*;
import java.util.Arrays;
/*
* @test
* @bug 4985694
* @summary Incomplete spec for most of the getInstances
*/
/**
* A simple test to see what is being thrown when null Strings are passed
* to the various getInstance() methods.
*
* These tests use various algorithm names that don't exist (e.g. "FOO"
* Ciphers). Just need something non-null for testing, as the tests will throw
* exceptions before trying to instantiate a real object.
*/
public class GetInstanceNullsEmpties {
private static final String providerName = System.getProperty("test.provider.name", "SUN");
private static final Provider provider = Security.getProvider(providerName);
/*
* See if there are more than "expected" number of getInstance() methods,
* which will indicate to developers that this test needs an update.
*/
private static void checkNewMethods(Class<?> clazz, int expected)
throws Exception {
long found = Arrays.stream(clazz.getMethods())
.filter(name -> name.getName().equals("getInstance"))
.count();
if (found != expected) {
throw new Exception("Number of getInstance() mismatch: "
+ expected + " expected, " + found + " found");
}
}
/**
* Main loop.
*/
public static void main(String[] args) throws Exception {
/*
* JCA
*/
testAlgorithmParameterGenerator();
testAlgorithmParameters();
testCertificateFactory();
testCertPathBuilder();
testCertPathValidator();
testCertStore();
testKeyFactory();
testKeyPairGenerator();
testKeyStore();
testMessageDigest();
testPolicy();
testSecureRandom();
testSignature();
/*
* JCE
*/
testCipher();
testExemptionMechanism();
testKeyAgreement();
testKeyGenerator();
testMac();
testSecretKeyFactory();
/*
* JSSE
*/
testKeyManagerFactory();
testSSLContext();
testTrustManagerFactory();
/*
* JGSS
*
* KeyTab.getInstance doesn't take algorithm names, so we'll
* ignore this one.
*/
testConfiguration();
System.out.println("\nTEST PASSED!");
}
private static Method getInstance(Class clazz, Class... args)
throws Exception {
boolean firstPrinted = false;
System.out.print("\n" + clazz.getName() + "(");
for (Class c : args) {
System.out.print(firstPrinted
? ", " + c.getName() : c.getName());
firstPrinted = true;
}
System.out.println("):");
return clazz.getMethod("getInstance", args);
}
private static void run(Method m, Class expectedException,
Object... args) throws Exception {
try {
m.invoke(null, args);
throw new Exception("Didn't throw exception");
} catch (InvocationTargetException ite) {
Throwable root = ite.getCause();
if (root instanceof Exception) {
Exception e = (Exception) root;
if (expectedException.isInstance(e)) {
System.out.print("OK ");
return;
} else {
System.out.println(
"Unexpected InvocationTargetException!");
throw e;
}
}
throw ite;
}
}
/*
* Constants so lines aren't so long.
*/
private static final Class STRING = String.class;
private static final Class PROVIDER = Provider.class;
private static void testAlgorithmParameterGenerator() throws Exception {
Class clazz = AlgorithmParameterGenerator.class;
Method m;
checkNewMethods(clazz, 3);
m = getInstance(clazz, STRING);
run(m, NullPointerException.class, (Object) null);
run(m, NoSuchAlgorithmException.class, "");
m = getInstance(clazz, STRING, STRING);
run(m, NullPointerException.class, null, providerName);
run(m, NoSuchAlgorithmException.class, "", providerName);
run(m, IllegalArgumentException.class, "FOO", null);
run(m, IllegalArgumentException.class, "FOO", "");
m = getInstance(clazz, STRING, PROVIDER);
run(m, NullPointerException.class, null, provider);
run(m, NoSuchAlgorithmException.class, "", provider);
run(m, IllegalArgumentException.class, "FOO", null);
}
private static void testAlgorithmParameters() throws Exception {
Class clazz = AlgorithmParameters.class;
Method m;
checkNewMethods(clazz, 3);
m = getInstance(clazz, STRING);
run(m, NullPointerException.class, (Object) null);
run(m, NoSuchAlgorithmException.class, "");
m = getInstance(clazz, STRING, STRING);
run(m, NullPointerException.class, null, providerName);
run(m, NoSuchAlgorithmException.class, "", providerName);
run(m, IllegalArgumentException.class, "FOO", null);
run(m, IllegalArgumentException.class, "FOO", "");
m = getInstance(clazz, STRING, PROVIDER);
run(m, NullPointerException.class, null, provider);
run(m, NoSuchAlgorithmException.class, "", provider);
run(m, IllegalArgumentException.class, "FOO", null);
}
private static void testCertPathBuilder() throws Exception {
Class clazz = CertPathBuilder.class;
Method m;
checkNewMethods(clazz, 3);
m = getInstance(clazz, STRING);
run(m, NullPointerException.class, (Object) null);
run(m, NoSuchAlgorithmException.class, "");
m = getInstance(clazz, STRING, STRING);
run(m, NullPointerException.class, null, providerName);
run(m, NoSuchAlgorithmException.class, "", providerName);
run(m, IllegalArgumentException.class, "FOO", null);
run(m, IllegalArgumentException.class, "FOO", "");
m = getInstance(clazz, STRING, PROVIDER);
run(m, NullPointerException.class, null, provider);
run(m, NoSuchAlgorithmException.class, "", provider);
run(m, IllegalArgumentException.class, "FOO", null);
}
private static void testCertPathValidator() throws Exception {
Class clazz = CertPathValidator.class;
Method m;
checkNewMethods(clazz, 3);
m = getInstance(clazz, STRING);
run(m, NullPointerException.class, (Object) null);
run(m, NoSuchAlgorithmException.class, "");
m = getInstance(clazz, STRING, STRING);
run(m, NullPointerException.class, null, providerName);
run(m, NoSuchAlgorithmException.class, "", providerName);
run(m, IllegalArgumentException.class, "FOO", null);
run(m, IllegalArgumentException.class, "FOO", "");
m = getInstance(clazz, STRING, PROVIDER);
run(m, NullPointerException.class, null, provider);
run(m, NoSuchAlgorithmException.class, "", provider);
run(m, IllegalArgumentException.class, "FOO", null);
}
private static void testCertStore() throws Exception {
Class clazz = CertStore.class;
Method m;
CertStoreParameters csp = () -> null;
checkNewMethods(clazz, 3);
m = getInstance(clazz, STRING, CertStoreParameters.class);
run(m, NullPointerException.class, (Object) null, csp);
run(m, NoSuchAlgorithmException.class, "", csp);
m = getInstance(clazz, STRING, CertStoreParameters.class, STRING);
run(m, NullPointerException.class, null, csp, providerName);
run(m, NoSuchAlgorithmException.class, "", csp, providerName);
run(m, IllegalArgumentException.class, "FOO", csp, null);
run(m, IllegalArgumentException.class, "FOO", csp, "");
m = getInstance(clazz, STRING, CertStoreParameters.class, PROVIDER);
run(m, NullPointerException.class, null, csp, provider);
run(m, NoSuchAlgorithmException.class, "", csp, provider);
run(m, IllegalArgumentException.class, "FOO", csp, null);
}
private static void testCertificateFactory() throws Exception {
Class clazz = CertificateFactory.class;
Method m;
checkNewMethods(clazz, 3);
m = getInstance(clazz, STRING);
run(m, NullPointerException.class, (Object) null);
run(m, CertificateException.class, "");
m = getInstance(clazz, STRING, STRING);
run(m, NullPointerException.class, null, providerName);
run(m, CertificateException.class, "", providerName);
run(m, IllegalArgumentException.class, "FOO", null);
run(m, IllegalArgumentException.class, "FOO", "");
m = getInstance(clazz, STRING, PROVIDER);
run(m, NullPointerException.class, null, provider);
run(m, CertificateException.class, "", provider);
run(m, IllegalArgumentException.class, "FOO", null);
}
private static void testCipher() throws Exception {
Class clazz = Cipher.class;
Method m;
checkNewMethods(clazz, 3);
/*
* Note the Cipher API is spec'd to throw a NoSuchAlgorithmException
* for a null transformation.
*/
m = getInstance(clazz, STRING);
run(m, NoSuchAlgorithmException.class, (Object) null);
run(m, NoSuchAlgorithmException.class, "");
m = getInstance(clazz, STRING, STRING);
run(m, NoSuchAlgorithmException.class, null, providerName);
run(m, NoSuchAlgorithmException.class, "", providerName);
run(m, IllegalArgumentException.class, "FOO", null);
run(m, IllegalArgumentException.class, "FOO", "");
m = getInstance(clazz, STRING, PROVIDER);
run(m, NoSuchAlgorithmException.class, null, provider);
run(m, NoSuchAlgorithmException.class, "", provider);
run(m, IllegalArgumentException.class, "FOO", null);
}
private static void testConfiguration() throws Exception {
Class clazz = Configuration.class;
Method m;
Configuration.Parameters cp = new Configuration.Parameters() {
};
checkNewMethods(clazz, 3);
m = getInstance(clazz, STRING, Configuration.Parameters.class);
run(m, NullPointerException.class, (Object) null, cp);
run(m, NoSuchAlgorithmException.class, "", cp);
m = getInstance(clazz, STRING, Configuration.Parameters.class, STRING);
run(m, NullPointerException.class, null, cp, providerName);
run(m, NoSuchAlgorithmException.class, "", cp, providerName);
run(m, IllegalArgumentException.class, "FOO", cp, null);
run(m, IllegalArgumentException.class, "FOO", cp, "");
m = getInstance(clazz, STRING, Configuration.Parameters.class,
PROVIDER);
run(m, NullPointerException.class, null, cp, provider);
run(m, NoSuchAlgorithmException.class, "", cp, provider);
run(m, IllegalArgumentException.class, "FOO", cp, null);
}
private static void testExemptionMechanism() throws Exception {
Class clazz = ExemptionMechanism.class;
Method m;
checkNewMethods(clazz, 3);
m = getInstance(clazz, STRING);
run(m, NullPointerException.class, (Object) null);
run(m, NoSuchAlgorithmException.class, "");
m = getInstance(clazz, STRING, STRING);
run(m, NullPointerException.class, null, providerName);
run(m, NoSuchAlgorithmException.class, "", providerName);
run(m, IllegalArgumentException.class, "FOO", null);
run(m, IllegalArgumentException.class, "FOO", "");
m = getInstance(clazz, STRING, PROVIDER);
run(m, NullPointerException.class, null, provider);
run(m, NoSuchAlgorithmException.class, "", provider);
run(m, IllegalArgumentException.class, "FOO", null);
}
private static void testKeyAgreement() throws Exception {
Class clazz = KeyAgreement.class;
Method m;
checkNewMethods(clazz, 3);
m = getInstance(clazz, STRING);
run(m, NullPointerException.class, (Object) null);
run(m, NoSuchAlgorithmException.class, "");
m = getInstance(clazz, STRING, STRING);
run(m, NullPointerException.class, null, providerName);
run(m, NoSuchAlgorithmException.class, "", providerName);
run(m, IllegalArgumentException.class, "FOO", null);
run(m, IllegalArgumentException.class, "FOO", "");
m = getInstance(clazz, STRING, PROVIDER);
run(m, NullPointerException.class, null, provider);
run(m, NoSuchAlgorithmException.class, "", provider);
run(m, IllegalArgumentException.class, "FOO", null);
}
private static void testKeyFactory() throws Exception {
Class clazz = KeyFactory.class;
Method m;
checkNewMethods(clazz, 3);
m = getInstance(clazz, STRING);
run(m, NullPointerException.class, (Object) null);
run(m, NoSuchAlgorithmException.class, "");
m = getInstance(clazz, STRING, STRING);
run(m, NullPointerException.class, null, providerName);
run(m, NoSuchAlgorithmException.class, "", providerName);
run(m, IllegalArgumentException.class, "FOO", null);
run(m, IllegalArgumentException.class, "FOO", "");
m = getInstance(clazz, STRING, PROVIDER);
run(m, NullPointerException.class, null, provider);
run(m, NoSuchAlgorithmException.class, "", provider);
run(m, IllegalArgumentException.class, "FOO", null);
}
private static void testKeyGenerator() throws Exception {
Class clazz = KeyGenerator.class;
Method m;
checkNewMethods(clazz, 3);
m = getInstance(clazz, STRING);
run(m, NullPointerException.class, (Object) null);
run(m, NoSuchAlgorithmException.class, "");
m = getInstance(clazz, STRING, STRING);
run(m, NullPointerException.class, null, providerName);
run(m, NoSuchAlgorithmException.class, "", providerName);
run(m, IllegalArgumentException.class, "FOO", null);
run(m, IllegalArgumentException.class, "FOO", "");
m = getInstance(clazz, STRING, PROVIDER);
run(m, NullPointerException.class, null, provider);
run(m, NoSuchAlgorithmException.class, "", provider);
run(m, IllegalArgumentException.class, "FOO", null);
}
private static void testKeyManagerFactory() throws Exception {
Class clazz = KeyManagerFactory.class;
Method m;
checkNewMethods(clazz, 3);
m = getInstance(clazz, STRING);
run(m, NullPointerException.class, (Object) null);
run(m, NoSuchAlgorithmException.class, "");
m = getInstance(clazz, STRING, STRING);
run(m, NullPointerException.class, null, providerName);
run(m, NoSuchAlgorithmException.class, "", providerName);
run(m, IllegalArgumentException.class, "FOO", null);
run(m, IllegalArgumentException.class, "FOO", "");
m = getInstance(clazz, STRING, PROVIDER);
run(m, NullPointerException.class, null, provider);
run(m, NoSuchAlgorithmException.class, "", provider);
run(m, IllegalArgumentException.class, "FOO", null);
}
private static void testKeyPairGenerator() throws Exception {
Class clazz = KeyPairGenerator.class;
Method m;
checkNewMethods(clazz, 3);
m = getInstance(clazz, STRING);
run(m, NullPointerException.class, (Object) null);
run(m, NoSuchAlgorithmException.class, "");
m = getInstance(clazz, STRING, STRING);
run(m, NullPointerException.class, null, providerName);
run(m, NoSuchAlgorithmException.class, "", providerName);
run(m, IllegalArgumentException.class, "FOO", null);
run(m, IllegalArgumentException.class, "FOO", "");
m = getInstance(clazz, STRING, PROVIDER);
run(m, NullPointerException.class, null, provider);
run(m, NoSuchAlgorithmException.class, "", provider);
run(m, IllegalArgumentException.class, "FOO", null);
}
private static void testKeyStore() throws Exception {
Class clazz = KeyStore.class;
Method m;
/*
* There are actually two additional getInstance() methods with File
* as the first parameter.
*/
checkNewMethods(clazz, 5);
m = getInstance(clazz, STRING);
run(m, NullPointerException.class, (Object) null);
run(m, KeyStoreException.class, "");
m = getInstance(clazz, STRING, STRING);
run(m, NullPointerException.class, null, providerName);
run(m, KeyStoreException.class, "", providerName);
run(m, IllegalArgumentException.class, "FOO", null);
run(m, IllegalArgumentException.class, "FOO", "");
m = getInstance(clazz, STRING, PROVIDER);
run(m, NullPointerException.class, null, provider);
run(m, KeyStoreException.class, "", provider);
run(m, IllegalArgumentException.class, "FOO", null);
}
private static void testMac() throws Exception {
Class clazz = Mac.class;
Method m;
checkNewMethods(clazz, 3);
m = getInstance(clazz, STRING);
run(m, NullPointerException.class, (Object) null);
run(m, NoSuchAlgorithmException.class, "");
m = getInstance(clazz, STRING, STRING);
run(m, NullPointerException.class, null, providerName);
run(m, NoSuchAlgorithmException.class, "", providerName);
run(m, IllegalArgumentException.class, "FOO", null);
run(m, IllegalArgumentException.class, "FOO", "");
m = getInstance(clazz, STRING, PROVIDER);
run(m, NullPointerException.class, null, provider);
run(m, NoSuchAlgorithmException.class, "", provider);
run(m, IllegalArgumentException.class, "FOO", null);
}
private static void testMessageDigest() throws Exception {
Class clazz = MessageDigest.class;
Method m;
checkNewMethods(clazz, 3);
m = getInstance(clazz, STRING);
run(m, NullPointerException.class, (Object) null);
run(m, NoSuchAlgorithmException.class, "");
m = getInstance(clazz, STRING, STRING);
run(m, NullPointerException.class, null, providerName);
run(m, NoSuchAlgorithmException.class, "", providerName);
run(m, IllegalArgumentException.class, "FOO", null);
run(m, IllegalArgumentException.class, "FOO", "");
m = getInstance(clazz, STRING, PROVIDER);
run(m, NullPointerException.class, null, provider);
run(m, NoSuchAlgorithmException.class, "", provider);
run(m, IllegalArgumentException.class, "FOO", null);
}
private static void testPolicy() throws Exception {
Class clazz = Policy.class;
Method m;
Policy.Parameters pp = new Policy.Parameters() {
};
checkNewMethods(clazz, 3);
m = getInstance(clazz, STRING, Policy.Parameters.class);
run(m, NullPointerException.class, (Object) null, pp);
run(m, NoSuchAlgorithmException.class, "", pp);
m = getInstance(clazz, STRING, Policy.Parameters.class, STRING);
run(m, NullPointerException.class, null, pp, providerName);
run(m, NoSuchAlgorithmException.class, "", pp, providerName);
run(m, IllegalArgumentException.class, "FOO", pp, null);
run(m, IllegalArgumentException.class, "FOO", pp, "");
m = getInstance(clazz, STRING, Policy.Parameters.class, PROVIDER);
run(m, NullPointerException.class, null, pp, provider);
run(m, NoSuchAlgorithmException.class, "", pp, provider);
run(m, IllegalArgumentException.class, "FOO", pp, null);
}
private static void testSSLContext() throws Exception {
Class clazz = SSLContext.class;
Method m;
checkNewMethods(clazz, 3);
m = getInstance(clazz, STRING);
run(m, NullPointerException.class, (Object) null);
run(m, NoSuchAlgorithmException.class, "");
m = getInstance(clazz, STRING, STRING);
run(m, NullPointerException.class, null, providerName);
run(m, NoSuchAlgorithmException.class, "", providerName);
run(m, IllegalArgumentException.class, "FOO", null);
run(m, IllegalArgumentException.class, "FOO", "");
m = getInstance(clazz, STRING, PROVIDER);
run(m, NullPointerException.class, null, provider);
run(m, NoSuchAlgorithmException.class, "", provider);
run(m, IllegalArgumentException.class, "FOO", null);
}
private static void testSecretKeyFactory() throws Exception {
Class clazz = SecretKeyFactory.class;
Method m;
checkNewMethods(clazz, 3);
m = getInstance(clazz, STRING);
run(m, NullPointerException.class, (Object) null);
run(m, NoSuchAlgorithmException.class, "");
m = getInstance(clazz, STRING, STRING);
run(m, NullPointerException.class, null, providerName);
run(m, NoSuchAlgorithmException.class, "", providerName);
run(m, IllegalArgumentException.class, "FOO", null);
run(m, IllegalArgumentException.class, "FOO", "");
m = getInstance(clazz, STRING, PROVIDER);
run(m, NullPointerException.class, null, provider);
run(m, NoSuchAlgorithmException.class, "", provider);
run(m, IllegalArgumentException.class, "FOO", null);
}
private static void testSecureRandom() throws Exception {
Class clazz = SecureRandom.class;
Method m;
SecureRandomParameters srp = new SecureRandomParameters() {
};
checkNewMethods(clazz, 6);
m = getInstance(clazz, STRING);
run(m, NullPointerException.class, (Object) null);
run(m, NoSuchAlgorithmException.class, "");
m = getInstance(clazz, STRING, STRING);
run(m, NullPointerException.class, null, providerName);
run(m, NoSuchAlgorithmException.class, "", providerName);
run(m, IllegalArgumentException.class, "FOO", null);
run(m, IllegalArgumentException.class, "FOO", "");
m = getInstance(clazz, STRING, PROVIDER);
run(m, NullPointerException.class, null, provider);
run(m, NoSuchAlgorithmException.class, "", provider);
run(m, IllegalArgumentException.class, "FOO", null);
m = getInstance(clazz, STRING, SecureRandomParameters.class);
run(m, NullPointerException.class, (Object) null, srp);
run(m, NoSuchAlgorithmException.class, "", srp);
m = getInstance(clazz, STRING, SecureRandomParameters.class, STRING);
run(m, NullPointerException.class, null, srp, providerName);
run(m, NoSuchAlgorithmException.class, "", srp, providerName);
run(m, IllegalArgumentException.class, "FOO", srp, null);
run(m, IllegalArgumentException.class, "FOO", srp, "");
m = getInstance(clazz, STRING, SecureRandomParameters.class, PROVIDER);
run(m, NullPointerException.class, null, srp, provider);
run(m, NoSuchAlgorithmException.class, "", srp, provider);
run(m, IllegalArgumentException.class, "FOO", srp, null);
}
private static void testSignature() throws Exception {
Class clazz = Signature.class;
Method m;
checkNewMethods(clazz, 3);
m = getInstance(clazz, STRING);
run(m, NullPointerException.class, (Object) null);
run(m, NoSuchAlgorithmException.class, "");
m = getInstance(clazz, STRING, STRING);
run(m, NullPointerException.class, null, providerName);
run(m, NoSuchAlgorithmException.class, "", providerName);
run(m, IllegalArgumentException.class, "FOO", null);
run(m, IllegalArgumentException.class, "FOO", "");
m = getInstance(clazz, STRING, PROVIDER);
run(m, NullPointerException.class, null, provider);
run(m, NoSuchAlgorithmException.class, "", provider);
run(m, IllegalArgumentException.class, "FOO", null);
}
private static void testTrustManagerFactory() throws Exception {
Class clazz = TrustManagerFactory.class;
Method m;
checkNewMethods(clazz, 3);
m = getInstance(clazz, STRING);
run(m, NullPointerException.class, (Object) null);
run(m, NoSuchAlgorithmException.class, "");
m = getInstance(clazz, STRING, STRING);
run(m, NullPointerException.class, null, providerName);
run(m, NoSuchAlgorithmException.class, "", providerName);
run(m, IllegalArgumentException.class, "FOO", null);
run(m, IllegalArgumentException.class, "FOO", "");
m = getInstance(clazz, STRING, PROVIDER);
run(m, NullPointerException.class, null, provider);
run(m, NoSuchAlgorithmException.class, "", provider);
run(m, IllegalArgumentException.class, "FOO", null);
}
}

View file

@ -0,0 +1,199 @@
/*
* Copyright (c) 2021, 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.
*/
/**
* @test
* @bug 8260274
* @run main/othervm TestDefaultRandom APG 1
* @run main/othervm TestDefaultRandom APG 2
* @run main/othervm TestDefaultRandom KPG 1
* @run main/othervm TestDefaultRandom KPG 2
* @run main/othervm TestDefaultRandom CIP 1
* @run main/othervm TestDefaultRandom CIP 2
* @run main/othervm TestDefaultRandom CIP 3
* @run main/othervm TestDefaultRandom CIP 4
* @run main/othervm TestDefaultRandom KA 1
* @run main/othervm TestDefaultRandom KA 2
* @run main/othervm TestDefaultRandom KG 1
* @run main/othervm TestDefaultRandom KG 2
* @summary Ensure the default SecureRandom impl is used as the javadoc
* spec stated when none supplied.
*/
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.security.*;
import java.security.cert.Certificate;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.DSAGenParameterSpec;
import java.security.spec.RSAKeyGenParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.KeyAgreement;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.IvParameterSpec;
public class TestDefaultRandom {
public static void main(String[] argv) throws Exception {
if (argv.length != 2) {
throw new RuntimeException("Error: missing test parameters");
}
switch (argv[0]) {
case "APG" ->
check(AlgorithmParameterGenerator.getInstance("DSA"), argv[1]);
case "KPG" ->
check(KeyPairGenerator.getInstance("RSA"), argv[1]);
case "CIP" ->
check(Cipher.getInstance("AES/CBC/NoPadding"), argv[1]);
case "KA" -> check(KeyAgreement.getInstance("DH"), argv[1]);
case "KG" -> check(KeyGenerator.getInstance("AES"), argv[1]);
default -> throw new RuntimeException
("Error: unsupported test type");
}
}
private static void check(Object obj, String testNum) {
if (obj == null) throw new NullPointerException();
SampleProvider prov = new SampleProvider();
Security.insertProviderAt(prov, 1);
int b4Cnt = SampleProvider.count;
System.out.println("before, count = " + b4Cnt);
// Note that the arguments may not be valid, they just need to be
// non-null to trigger the call for checking if the default
// SecureRandom impl is used
try {
if (obj instanceof AlgorithmParameterGenerator) {
AlgorithmParameterGenerator apg =
(AlgorithmParameterGenerator) obj;
switch (testNum) {
case "1" -> apg.init(123);
case "2" -> apg.init((AlgorithmParameterSpec) null);
default -> throw new RuntimeException
("Error: invalid test#");
}
} else if (obj instanceof KeyPairGenerator) {
KeyPairGenerator kpg = (KeyPairGenerator) obj;
switch (testNum) {
case "1" -> kpg.initialize(123);
case "2" -> kpg.initialize((AlgorithmParameterSpec) null);
default -> throw new RuntimeException
("Error: invalid test#");
}
} else if (obj instanceof Cipher) {
Cipher c = (Cipher) obj;
switch (testNum) {
case "1" -> c.init(Cipher.ENCRYPT_MODE, (Key) null);
case "2" -> c.init(Cipher.ENCRYPT_MODE, (Key) null,
(AlgorithmParameterSpec) null);
case "3" -> c.init(Cipher.ENCRYPT_MODE, (Key) null,
(AlgorithmParameters) null);
case "4" -> c.init(Cipher.ENCRYPT_MODE, (Certificate)null);
default -> throw new RuntimeException
("Error: invalid test#");
}
} else if (obj instanceof KeyAgreement) {
KeyAgreement ka = (KeyAgreement) obj;
switch (testNum) {
case "1" -> ka.init((Key) null);
case "2" -> ka.init((Key) null, (AlgorithmParameterSpec)
null);
default -> throw new RuntimeException
("Error: invalid test#");
}
} else if (obj instanceof KeyGenerator) {
KeyGenerator kg = (KeyGenerator) obj;
switch (testNum) {
case "1" -> kg.init(123);
case "2" -> kg.init((AlgorithmParameterSpec) null);
default -> throw new RuntimeException
("Error: invalid test#");
}
} else {
throw new RuntimeException("Error: Unsupported type");
}
} catch (GeneralSecurityException | InvalidParameterException e) {
// expected; ignore
}
System.out.println("after, count = " + SampleProvider.count);
if (SampleProvider.count == b4Cnt) {
throw new RuntimeException("Test Failed");
}
}
private static class SampleProvider extends Provider {
static int count = 0;
static String SR_ALGO = "Custom";
SampleProvider() {
super("Sample", "1.0", "test provider with custom SR impl");
putService(new SampleService(this, "SecureRandom", SR_ALGO,
"SampleSecureRandom.class" /* stub class name */,
null, null));
}
private static class SampleService extends Service {
SampleService(Provider p, String type, String alg, String cn,
List<String> aliases, Map<String,String> attrs) {
super(p, type, alg, cn, aliases, attrs);
}
@Override
public Object newInstance(Object param)
throws NoSuchAlgorithmException {
String alg = getAlgorithm();
String type = getType();
if (type.equals("SecureRandom") && alg.equals(SR_ALGO)) {
SampleProvider.count++;
return new CustomSR();
} else {
// should never happen
throw new NoSuchAlgorithmException("No support for " + alg);
}
}
}
private static class CustomSR extends SecureRandomSpi {
@Override
protected void engineSetSeed(byte[] seed) {
}
@Override
protected void engineNextBytes(byte[] bytes) {
}
@Override
protected byte[] engineGenerateSeed(int numBytes) {
return new byte[numBytes];
}
}
}
}

View file

@ -0,0 +1,94 @@
/*
* Copyright (c) 2019, 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.
*/
/*
* @test
* @bug 8232357 8221801 8244674
* @summary Make sure version info is consistent between code and license
* @run testng Versions
*/
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Versions {
Path rootPath = Path.of(System.getProperty("test.root"), "../..");
Path legalPath = Path.of(System.getProperty("test.jdk"), "legal");
@DataProvider(name = "data")
public Object[][] createData() {
return new Object[][]{
{"src/java.xml.crypto/share/classes/org/jcp/xml/dsig/internal/dom/XMLDSigRI.java",
Pattern.compile("// Apache Santuario XML Security for Java, version (?<n>\\S+)"),
"src/java.xml.crypto/share/legal/santuario.md",
Pattern.compile("## Apache Santuario v(?<n>\\S+)"),
"java.xml.crypto/santuario.md"},
{"src/java.base/share/data/publicsuffixlist/VERSION",
Pattern.compile("list/(?<n>[0-9a-f]+)/public_suffix_list.dat"),
"src/java.base/share/legal/public_suffix.md",
Pattern.compile("list/(?<n>[0-9a-f]+)/public_suffix_list.dat"),
"java.base/public_suffix.md"},
{"src/java.smartcardio/unix/native/libj2pcsc/MUSCLE/pcsclite.h",
Pattern.compile("#define PCSCLITE_VERSION_NUMBER +\"(?<n>[0-9\\.]+)\""),
"src/java.smartcardio/unix/legal/pcsclite.md",
Pattern.compile("## PC/SC Lite v(?<n>[0-9\\.]+)"),
"java.smartcardio/pcsclite.md"}
};
}
@Test(dataProvider = "data")
public void Test(String src, Pattern sp, String legal, Pattern lp,
String legalInBuild) throws IOException {
Path pSrc = rootPath.resolve(src);
Path pLegal = rootPath.resolve(legal);
Assert.assertEquals(fetch(pSrc, sp), fetch(pLegal, lp));
Path pLegalInBuild = legalPath.resolve(legalInBuild);
if (!Files.exists(pLegalInBuild)) {
System.out.println("Not an image build, or file platform dependent");
} else {
Assert.assertEquals(Files.mismatch(pLegal, pLegalInBuild), -1);
}
}
// Find a match in path and return the extracted named group
static String fetch(Path path, Pattern pattern)
throws IOException {
return Files.lines(path)
.map(pattern::matcher)
.filter(Matcher::find)
.findFirst()
.map(m -> m.group("n"))
.get();
}
}