Path: blob/master/test/jdk/javax/crypto/Cipher/GetMaxAllowed.java
41149 views
/*1* Copyright (c) 2003, 2012, 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 4807942 703317026* @summary Test the Cipher.getMaxAllowedKeyLength(String) and27* getMaxAllowedParameterSpec(String) methods28* @author Valerie Peng29*/3031import java.util.*;32import java.nio.*;3334import java.security.*;35import java.security.spec.*;3637import javax.crypto.*;38import javax.crypto.spec.*;3940public class GetMaxAllowed {4142private static void runTest1(boolean isUnlimited) throws Exception {43System.out.println("Testing " + (isUnlimited? "un":"") +44"limited policy...");4546String algo = "Blowfish";47int keyLength = Cipher.getMaxAllowedKeyLength(algo);48AlgorithmParameterSpec spec = Cipher.getMaxAllowedParameterSpec(algo);49if (isUnlimited) {50if ((keyLength != Integer.MAX_VALUE) || (spec != null)) {51throw new Exception("Check for " + algo +52" failed under unlimited policy");53}54} else {55if ((keyLength != 128) || (spec != null)) {56throw new Exception("Check for " + algo +57" failed under default policy");58}59}60algo = "RC5";61keyLength = Cipher.getMaxAllowedKeyLength(algo);62RC5ParameterSpec rc5param = (RC5ParameterSpec)63Cipher.getMaxAllowedParameterSpec(algo);64if (isUnlimited) {65if ((keyLength != Integer.MAX_VALUE) || (rc5param != null)) {66throw new Exception("Check for " + algo +67" failed under unlimited policy");68}69} else {70if ((keyLength != 128) || (rc5param.getRounds() != 12) ||71(rc5param.getVersion() != Integer.MAX_VALUE) ||72(rc5param.getWordSize() != Integer.MAX_VALUE)) {73throw new Exception("Check for " + algo +74" failed under default policy");75}76}77System.out.println("All tests passed");78}7980private static void runTest2() throws Exception {81System.out.println("Testing against Security.getAlgorithms()");8283Set<String> algorithms = Security.getAlgorithms("Cipher");8485for (String algorithm: algorithms) {86int keylength = -1;8788// if 7033170 is not fixed, NoSuchAlgorithmException is thrown89keylength = Cipher.getMaxAllowedKeyLength(algorithm);9091}92}9394public static void main(String[] args) throws Exception {95// decide if the installed jurisdiction policy file is the96// unlimited version97boolean isUnlimited = true;98Cipher c = Cipher.getInstance("AES", "SunJCE");99try {100c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(new byte[24], "AES"));101} catch (InvalidKeyException ike) {102isUnlimited = false;103}104runTest1(isUnlimited);105106// test using the set of algorithms returned by Security.getAlgorithms()107runTest2();108}109}110111112