Path: blob/master/test/jdk/javax/crypto/CryptoPermission/RC2PermCheck.java
41149 views
/*1* Copyright (c) 2003, 2007, 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 489236526* @summary Ensure the crypto permission check on cipher algorithms27* with restricted parameter values are correctly enforced.28* @author Valerie Peng29* @key randomness30*/3132import java.io.*;33import java.util.*;3435import java.security.*;36import java.security.spec.*;3738import javax.crypto.*;39import javax.crypto.spec.*;4041public class RC2PermCheck {4243public static void main(String[] args) throws Exception {44Provider p = Security.getProvider("SunJCE");45System.out.println("Testing provider " + p.getName() + "...");46if (Cipher.getMaxAllowedKeyLength("DES") == Integer.MAX_VALUE) {47// skip this test for unlimited jurisdiction policy files48System.out.println("Skip this test due to unlimited version");49return;50}51// Currently, RC2 is the only algorithm whose parameter values52// are restricted53String algo = "RC2";54Cipher c = Cipher.getInstance(algo + "/CBC/PKCS5Padding", p);55SecretKeySpec key = new SecretKeySpec(new byte[16], "RC2");56SecureRandom srand = new SecureRandom();57int numOfTests = 6;58boolean result = true;59// test set#1: init with no parameter supplied60for (int i = 0; i < numOfTests; i++) {61try {62switch (i) {63case 0:64c.init(Cipher.ENCRYPT_MODE, key);65break;66case 1:67c.init(Cipher.ENCRYPT_MODE, key, srand);68break;69case 2:70c.init(Cipher.ENCRYPT_MODE, key,71(AlgorithmParameters) null);72break;73case 3:74c.init(Cipher.ENCRYPT_MODE, key,75(AlgorithmParameters) null, srand);76break;77case 4:78c.init(Cipher.ENCRYPT_MODE, key,79(AlgorithmParameterSpec) null);80break;81case 5:82c.init(Cipher.ENCRYPT_MODE, key,83(AlgorithmParameterSpec) null, srand);84break;85}86} catch (Exception ex) {87result = false;88System.out.println("Test#1." + i + " failed!");89ex.printStackTrace();90continue;91}92}93// test set#2: init with parameter within limit94RC2ParameterSpec paramSpec = new RC2ParameterSpec(128, new byte[8]);95AlgorithmParameters param = AlgorithmParameters.getInstance(algo, p);96param.init(paramSpec);97numOfTests = 4;98for (int i = 0; i < numOfTests; i++) {99try {100switch (i) {101case 0:102c.init(Cipher.ENCRYPT_MODE, key, paramSpec);103break;104case 1:105c.init(Cipher.ENCRYPT_MODE, key, paramSpec, srand);106break;107case 2:108c.init(Cipher.ENCRYPT_MODE, key, param);109break;110case 3:111c.init(Cipher.ENCRYPT_MODE, key, param, srand);112break;113}114} catch (Exception ex) {115result = false;116System.out.println("Test#2." + i + " failed!");117ex.printStackTrace();118}119}120// test set#3: init with parameter over limit121paramSpec = new RC2ParameterSpec(256, new byte[8]);122param = AlgorithmParameters.getInstance(algo);123param.init(paramSpec);124125for (int i = 0; i < numOfTests; i++) {126try {127switch (i) {128case 0:129c.init(Cipher.ENCRYPT_MODE, key, paramSpec);130result = false;131System.out.println("Test#3." + i + " failed!");132break;133case 1:134c.init(Cipher.ENCRYPT_MODE, key, paramSpec, srand);135result = false;136System.out.println("Test#3." + i + " failed!");137break;138case 2:139c.init(Cipher.ENCRYPT_MODE, key, param);140result = false;141System.out.println("Test#3." + i + " failed!");142break;143case 3:144c.init(Cipher.ENCRYPT_MODE, key, param, srand);145result = false;146System.out.println("Test#3." + i + " failed!");147break;148}149} catch (InvalidAlgorithmParameterException iape) {150// expected exception thrown; proceed to next test151continue;152}153}154if (result) {155System.out.println("All tests passed!");156} else {157throw new Exception("One or more test failed!");158}159}160}161162163