Path: blob/master/test/jdk/java/security/misc/TestDefaultRandom.java
41149 views
/*1* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 826027426* @run main/othervm TestDefaultRandom APG 127* @run main/othervm TestDefaultRandom APG 228* @run main/othervm TestDefaultRandom KPG 129* @run main/othervm TestDefaultRandom KPG 230* @run main/othervm TestDefaultRandom CIP 131* @run main/othervm TestDefaultRandom CIP 232* @run main/othervm TestDefaultRandom CIP 333* @run main/othervm TestDefaultRandom CIP 434* @run main/othervm TestDefaultRandom KA 135* @run main/othervm TestDefaultRandom KA 236* @run main/othervm TestDefaultRandom KG 137* @run main/othervm TestDefaultRandom KG 238* @summary Ensure the default SecureRandom impl is used as the javadoc39* spec stated when none supplied.40*/41import java.lang.reflect.InvocationTargetException;42import java.util.List;43import java.util.Map;44import java.security.*;45import java.security.cert.Certificate;46import java.security.spec.AlgorithmParameterSpec;47import java.security.spec.DSAGenParameterSpec;48import java.security.spec.RSAKeyGenParameterSpec;49import javax.crypto.Cipher;50import javax.crypto.KeyAgreement;51import javax.crypto.KeyGenerator;52import javax.crypto.SecretKey;53import javax.crypto.spec.SecretKeySpec;54import javax.crypto.spec.IvParameterSpec;5556public class TestDefaultRandom {5758public static void main(String[] argv) throws Exception {59if (argv.length != 2) {60throw new RuntimeException("Error: missing test parameters");61}6263switch (argv[0]) {64case "APG" ->65check(AlgorithmParameterGenerator.getInstance("DSA"), argv[1]);66case "KPG" ->67check(KeyPairGenerator.getInstance("RSA"), argv[1]);68case "CIP" ->69check(Cipher.getInstance("AES/CBC/NoPadding"), argv[1]);70case "KA" -> check(KeyAgreement.getInstance("DH"), argv[1]);71case "KG" -> check(KeyGenerator.getInstance("AES"), argv[1]);72default -> throw new RuntimeException73("Error: unsupported test type");74}75}7677private static void check(Object obj, String testNum) {78if (obj == null) throw new NullPointerException();7980SampleProvider prov = new SampleProvider();81Security.insertProviderAt(prov, 1);8283int b4Cnt = SampleProvider.count;8485System.out.println("before, count = " + b4Cnt);86// Note that the arguments may not be valid, they just need to be87// non-null to trigger the call for checking if the default88// SecureRandom impl is used89try {90if (obj instanceof AlgorithmParameterGenerator) {91AlgorithmParameterGenerator apg =92(AlgorithmParameterGenerator) obj;93switch (testNum) {94case "1" -> apg.init(123);95case "2" -> apg.init((AlgorithmParameterSpec) null);96default -> throw new RuntimeException97("Error: invalid test#");98}99} else if (obj instanceof KeyPairGenerator) {100KeyPairGenerator kpg = (KeyPairGenerator) obj;101switch (testNum) {102case "1" -> kpg.initialize(123);103case "2" -> kpg.initialize((AlgorithmParameterSpec) null);104default -> throw new RuntimeException105("Error: invalid test#");106}107} else if (obj instanceof Cipher) {108Cipher c = (Cipher) obj;109switch (testNum) {110case "1" -> c.init(Cipher.ENCRYPT_MODE, (Key) null);111case "2" -> c.init(Cipher.ENCRYPT_MODE, (Key) null,112(AlgorithmParameterSpec) null);113case "3" -> c.init(Cipher.ENCRYPT_MODE, (Key) null,114(AlgorithmParameters) null);115case "4" -> c.init(Cipher.ENCRYPT_MODE, (Certificate)null);116default -> throw new RuntimeException117("Error: invalid test#");118}119} else if (obj instanceof KeyAgreement) {120KeyAgreement ka = (KeyAgreement) obj;121switch (testNum) {122case "1" -> ka.init((Key) null);123case "2" -> ka.init((Key) null, (AlgorithmParameterSpec)124null);125default -> throw new RuntimeException126("Error: invalid test#");127}128} else if (obj instanceof KeyGenerator) {129KeyGenerator kg = (KeyGenerator) obj;130switch (testNum) {131case "1" -> kg.init(123);132case "2" -> kg.init((AlgorithmParameterSpec) null);133default -> throw new RuntimeException134("Error: invalid test#");135}136} else {137throw new RuntimeException("Error: Unsupported type");138}139} catch (GeneralSecurityException | InvalidParameterException e) {140// expected; ignore141}142System.out.println("after, count = " + SampleProvider.count);143if (SampleProvider.count == b4Cnt) {144throw new RuntimeException("Test Failed");145}146}147148private static class SampleProvider extends Provider {149150static int count = 0;151static String SR_ALGO = "Custom";152153SampleProvider() {154super("Sample", "1.0", "test provider with custom SR impl");155putService(new SampleService(this, "SecureRandom", SR_ALGO,156"SampleSecureRandom.class" /* stub class name */,157null, null));158}159160private static class SampleService extends Service {161162SampleService(Provider p, String type, String alg, String cn,163List<String> aliases, Map<String,String> attrs) {164super(p, type, alg, cn, aliases, attrs);165}166167@Override168public Object newInstance(Object param)169throws NoSuchAlgorithmException {170String alg = getAlgorithm();171String type = getType();172173if (type.equals("SecureRandom") && alg.equals(SR_ALGO)) {174SampleProvider.count++;175return new CustomSR();176} else {177// should never happen178throw new NoSuchAlgorithmException("No support for " + alg);179}180}181}182183private static class CustomSR extends SecureRandomSpi {184@Override185protected void engineSetSeed(byte[] seed) {186}187188@Override189protected void engineNextBytes(byte[] bytes) {190}191192@Override193protected byte[] engineGenerateSeed(int numBytes) {194return new byte[numBytes];195}196}197}198}199200201