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:
commit
0a580b313d
70422 changed files with 17213626 additions and 0 deletions
168
test/jdk/java/security/KeyPairGenerator/Failover.java
Normal file
168
test/jdk/java/security/KeyPairGenerator/Failover.java
Normal file
|
|
@ -0,0 +1,168 @@
|
|||
/*
|
||||
* Copyright (c) 2003, 2025, 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 4894125 7054918 8130181
|
||||
* @library /test/lib
|
||||
* @summary test that failover for KeyPairGenerator works
|
||||
* @author Andreas Sterbenz
|
||||
*/
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import java.security.*;
|
||||
import java.security.spec.*;
|
||||
import jdk.test.lib.security.ProvidersSnapshot;
|
||||
|
||||
public class Failover {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
ProvidersSnapshot snapshot = ProvidersSnapshot.create();
|
||||
try {
|
||||
main0(args);
|
||||
} finally {
|
||||
snapshot.restore();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main0(String[] args) throws Exception {
|
||||
Security.insertProviderAt(new ProviderFail(), 1);
|
||||
Security.addProvider(new ProviderPass());
|
||||
System.out.println(Arrays.asList(Security.getProviders()));
|
||||
|
||||
KeyPairGenerator kpg;
|
||||
kpg = KeyPairGenerator.getInstance("FOO");
|
||||
kpg.generateKeyPair();
|
||||
kpg.generateKeyPair();
|
||||
|
||||
kpg = KeyPairGenerator.getInstance("FOO");
|
||||
kpg.initialize(1024);
|
||||
kpg.initialize(1024);
|
||||
kpg.initialize(null, null);
|
||||
kpg.generateKeyPair();
|
||||
|
||||
kpg = KeyPairGenerator.getInstance("FOO");
|
||||
kpg.initialize(null, null);
|
||||
kpg.generateKeyPair();
|
||||
|
||||
kpg = KeyPairGenerator.getInstance("FOO");
|
||||
kpg.initialize(512);
|
||||
kpg.generateKeyPair();
|
||||
kpg.generateKeyPair();
|
||||
|
||||
// the SUN DSA KeyPairGenerator implementation extends
|
||||
// KeyPairGenerator (in order to implement
|
||||
// java.security.interfaces.DSAKeyPairGenerator)
|
||||
// failover cannot work
|
||||
kpg = KeyPairGenerator.getInstance("DSA");
|
||||
try {
|
||||
kpg.initialize(1024);
|
||||
throw new Exception("no exception");
|
||||
} catch (InvalidParameterException e) {
|
||||
System.out.println(e);
|
||||
}
|
||||
|
||||
KeyPair kp;
|
||||
kpg = KeyPairGenerator.getInstance("RSA");
|
||||
kpg.initialize(512);
|
||||
kp = kpg.generateKeyPair();
|
||||
System.out.println(kp.getPublic());
|
||||
|
||||
kpg = KeyPairGenerator.getInstance("RSA");
|
||||
kpg.initialize(768);
|
||||
kp = kpg.generateKeyPair();
|
||||
System.out.println(kp.getPublic());
|
||||
|
||||
kpg = KeyPairGenerator.getInstance("RSA");
|
||||
kp = kpg.generateKeyPair();
|
||||
System.out.println(kp.getPublic());
|
||||
|
||||
kpg = KeyPairGenerator.getInstance("RSA");
|
||||
try {
|
||||
kpg.initialize(128);
|
||||
throw new Exception("no exception");
|
||||
} catch (InvalidParameterException e) {
|
||||
System.out.println(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class ProviderPass extends Provider {
|
||||
ProviderPass() {
|
||||
super("Pass", "1.0", "Pass");
|
||||
put("KeyPairGenerator.FOO" , "Failover$KeyPairGeneratorPass");
|
||||
}
|
||||
}
|
||||
|
||||
private static class ProviderFail extends Provider {
|
||||
ProviderFail() {
|
||||
super("Fail", "1.0", "Fail");
|
||||
put("KeyPairGenerator.FOO" , "Failover$KeyPairGeneratorFail");
|
||||
put("KeyPairGenerator.DSA" , "Failover$KeyPairGeneratorFail");
|
||||
put("KeyPairGenerator.RSA" , "Failover$KeyPairGeneratorFail");
|
||||
}
|
||||
}
|
||||
|
||||
public static class KeyPairGeneratorPass extends KeyPairGeneratorSpi {
|
||||
|
||||
public void initialize(int keysize, SecureRandom random) {
|
||||
System.out.println("KeyPairGeneratorPass.initialize(" + keysize + ", " + random + ")");
|
||||
}
|
||||
|
||||
public void initialize(AlgorithmParameterSpec params,
|
||||
SecureRandom random) throws InvalidAlgorithmParameterException {
|
||||
System.out.println("KeyPairGeneratorPass.initialize()");
|
||||
}
|
||||
|
||||
public KeyPair generateKeyPair() {
|
||||
System.out.println("KeyPairGeneratorPass.generateKeyPair()");
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class KeyPairGeneratorFail extends KeyPairGeneratorSpi {
|
||||
|
||||
public void initialize(int keysize, SecureRandom random) {
|
||||
if (keysize != 512) {
|
||||
System.out.println("KeyPairGeneratorFail.initialize()");
|
||||
throw new InvalidParameterException();
|
||||
}
|
||||
System.out.println("KeyPairGeneratorFail.initialize() PASS");
|
||||
}
|
||||
|
||||
public void initialize(AlgorithmParameterSpec params,
|
||||
SecureRandom random) throws InvalidAlgorithmParameterException {
|
||||
System.out.println("KeyPairGeneratorFail.initialize()");
|
||||
throw new InvalidParameterException();
|
||||
}
|
||||
|
||||
public KeyPair generateKeyPair() {
|
||||
System.out.println("KeyPairGeneratorFail.generateKeyPair()");
|
||||
throw new InvalidParameterException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
90
test/jdk/java/security/KeyPairGenerator/FinalizeHalf.java
Normal file
90
test/jdk/java/security/KeyPairGenerator/FinalizeHalf.java
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
/*
|
||||
* Copyright (c) 2016, 2019, 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 8163896 8223003
|
||||
* @summary Finalizing one key of a KeyPair invalidates the other key
|
||||
*/
|
||||
|
||||
import java.security.Key;
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.Provider;
|
||||
import java.security.ProviderException;
|
||||
import java.security.Security;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class FinalizeHalf {
|
||||
|
||||
static int failures = 0;
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
List<Consumer<Key>> methods = new ArrayList<>();
|
||||
methods.add((Key k) -> k.getEncoded());
|
||||
methods.add((Key k) -> k.toString());
|
||||
|
||||
for (String algo : new String[] {"DiffieHellman", "DSA", "RSA"}) {
|
||||
for (Provider provider : Security.getProviders()) {
|
||||
for (boolean priv : new boolean[] {true, false}) {
|
||||
for (Consumer<Key> method : methods) {
|
||||
test(algo, provider, priv, method);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (failures > 0) {
|
||||
throw new RuntimeException(failures + " test(s) failed.");
|
||||
}
|
||||
}
|
||||
|
||||
static void test(String algo, Provider provider, boolean priv,
|
||||
Consumer<Key> method) throws Exception {
|
||||
KeyPairGenerator generator;
|
||||
try {
|
||||
generator = KeyPairGenerator.getInstance(algo, provider);
|
||||
} catch (NoSuchAlgorithmException nsae) {
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("Checking " + provider.getName() + ", " + algo);
|
||||
|
||||
KeyPair pair = generator.generateKeyPair();
|
||||
Key key = priv ? pair.getPrivate() : pair.getPublic();
|
||||
|
||||
pair = null;
|
||||
for (int i = 0; i < 32; ++i) {
|
||||
System.gc();
|
||||
}
|
||||
|
||||
try {
|
||||
method.accept(key);
|
||||
} catch (ProviderException pe) {
|
||||
failures++;
|
||||
}
|
||||
}
|
||||
}
|
||||
55
test/jdk/java/security/KeyPairGenerator/GenerateKeypair.java
Normal file
55
test/jdk/java/security/KeyPairGenerator/GenerateKeypair.java
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
* Copyright (c) 1999, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4221800
|
||||
* @library /test/lib
|
||||
* @summary Test restored generateKeyPair method
|
||||
*/
|
||||
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.security.KeyPair;
|
||||
import jdk.test.lib.security.SecurityUtils;
|
||||
|
||||
public class GenerateKeypair {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
String kpgAlgorithm = "DSA";
|
||||
KeyPairGenerator kpg = KeyPairGenerator.getInstance(kpgAlgorithm);
|
||||
kpg.initialize(SecurityUtils.getTestKeySize(kpgAlgorithm));
|
||||
|
||||
// test generateKeyPair
|
||||
KeyPair kpair = kpg.generateKeyPair();
|
||||
if (kpair == null) {
|
||||
throw new Exception("no keypair generated");
|
||||
}
|
||||
|
||||
// test genKeyPair
|
||||
kpair = kpg.genKeyPair();
|
||||
if (kpair == null) {
|
||||
throw new Exception("no keypair generated");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
* Copyright (c) 2001, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4297026
|
||||
* @library /test/lib
|
||||
* @summary Make sure that RSA Keypair generation using
|
||||
* java.security.spec.RSAKeyGenParameterSpec passes
|
||||
*/
|
||||
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.security.KeyPair;
|
||||
import java.security.spec.RSAKeyGenParameterSpec;
|
||||
import jdk.test.lib.security.SecurityUtils;
|
||||
|
||||
public class GenerateRSAKeyPair {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
String kpgAlgorithm = "RSA";
|
||||
RSAKeyGenParameterSpec rsaSpec =
|
||||
new RSAKeyGenParameterSpec (SecurityUtils.getTestKeySize(kpgAlgorithm),
|
||||
RSAKeyGenParameterSpec.F4);
|
||||
KeyPairGenerator kpg = KeyPairGenerator.getInstance(kpgAlgorithm,
|
||||
System.getProperty("test.provider.name", "SunRsaSign"));
|
||||
kpg.initialize(rsaSpec);
|
||||
|
||||
// test generateKeyPair
|
||||
KeyPair kpair = kpg.generateKeyPair();
|
||||
if (kpair == null) {
|
||||
throw new Exception("no keypair generated");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue