Path: blob/master/test/jdk/javax/crypto/CryptoPermission/AllPermCheck.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 495355426* @summary Ensure either IllegalAlgorithmParameterException or27* InvalidKeyException is thrown instead of SecurityException when28* crypto permssion checks failed.29* @author Valerie Peng30* @key randomness31*/3233import java.io.*;34import java.util.*;3536import java.security.*;37import java.security.spec.*;3839import javax.crypto.*;40import javax.crypto.spec.*;4142public class AllPermCheck {4344private static String SYM_ALGOS[] = {45"AES", "Blowfish", "RC2", "ARCFOUR"46};4748public static void runTest(Cipher c, Key key) throws Exception {49SecureRandom sr = new SecureRandom();5051for (int i = 0; i < 6; i++) {52try {53switch (i) {54case 0:55c.init(Cipher.ENCRYPT_MODE, key);56break;57case 1:58c.init(Cipher.ENCRYPT_MODE, key, sr);59break;60case 2:61c.init(Cipher.ENCRYPT_MODE, key,62(AlgorithmParameters)null);63break;64case 3:65c.init(Cipher.ENCRYPT_MODE, key,66(AlgorithmParameters)null, sr);67break;68case 4:69c.init(Cipher.ENCRYPT_MODE, key,70(AlgorithmParameterSpec)null);71break;72case 5:73c.init(Cipher.ENCRYPT_MODE, key,74(AlgorithmParameterSpec)null, sr);75break;76}77throw new Exception("...#" + i + " should throw IKE for " +78key.getEncoded().length + "-byte keys");79} catch (InvalidKeyException ike) {80System.out.println("...#" + i + " expected IKE thrown");81}82}83}8485public static void main(String[] args) throws Exception {86Provider p = Security.getProvider("SunJCE");87System.out.println("Testing provider " + p.getName() + "...");88if (Cipher.getMaxAllowedKeyLength("DES") == Integer.MAX_VALUE) {89// skip this test for unlimited jurisdiction policy files90System.out.println("Skip this test due to unlimited version");91return;92}93for (int i = 0; i < SYM_ALGOS.length; i++) {94String algo = SYM_ALGOS[i];95Cipher c = Cipher.getInstance(algo, p);96int keyLength = Cipher.getMaxAllowedKeyLength(algo);97SecretKey key = new SecretKeySpec(new byte[keyLength/8 + 8], algo);98System.out.println("Testing " + algo + " Cipher");99runTest(c, key);100}101System.out.println("All tests passed!");102}103}104105106