Path: blob/master/test/jdk/javax/crypto/CryptoPermissions/TestExemption.java
41149 views
/*1* Copyright (c) 2016, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* @test27* @bug 8161527 818056828* @summary NPE is thrown if exempt application is bundled with specific29* cryptoPerms30* @requires java.runtime.name ~= "OpenJDK.*"31* @library /test/lib32* @run main TestExemption33*/34import javax.crypto.*;35import java.nio.file.Path;36import java.security.*;37import java.util.ArrayList;38import java.util.List;39import jdk.test.lib.process.OutputAnalyzer;40import jdk.test.lib.process.ProcessTools;41import jdk.test.lib.util.JarUtils;4243public class TestExemption {4445private static final String SRC = System.getProperty("test.src");46private static final String CLASSES = System.getProperty("test.classes");47private static final String NAME = TestExemption.class.getName();48private static final String SRC_CLS = NAME + ".class";49private static final String JAR_FILE = NAME + ".jar";50private static final String CRYPT_PERM = "cryptoPerms";5152public static void main(String[] args) throws Exception {5354// With no argument passed, compile the same class, jar it and run the55// test section of the jar file which is nothing but else section here.56if (args.length == 0) {57JarUtils.createJarFile(58Path.of(JAR_FILE), Path.of(CLASSES), Path.of(SRC_CLS));59JarUtils.updateJarFile(60Path.of(JAR_FILE), Path.of(SRC), Path.of(CRYPT_PERM));61OutputAnalyzer oa = ProcessTools.executeTestJava(62getParameters().toArray(String[]::new));63System.out.println(oa.getOutput());64oa.shouldHaveExitValue(0);65} else {66// Set the crypto policy to limited so that additional policy can be67// supplemented through cryptoPerms when bundled inside a jar file.68Security.setProperty("crypto.policy", "limited");69KeyGenerator kg = KeyGenerator.getInstance("AES");70kg.init(128);71SecretKey key128 = kg.generateKey();7273kg.init(192);74SecretKey key192 = kg.generateKey();7576kg.init(256);77SecretKey key256 = kg.generateKey();7879int maxAllowed = Cipher.getMaxAllowedKeyLength("AES");80System.out.println("Max allowed: " + maxAllowed);81// With limited crypto and bundled cryptoPerms maximum allowed82// length of AES is upto 192.83if (maxAllowed > 192) {84throw new RuntimeException(">192 not supported");85}8687Cipher c = Cipher.getInstance("AES/CBC/NoPadding");88System.out.println("Testing 128-bit");89c.init(Cipher.ENCRYPT_MODE, key128);9091System.out.println("Testing 192-bit");92c.init(Cipher.ENCRYPT_MODE, key192);93try {94System.out.println("Testing 256-bit");95c.init(Cipher.ENCRYPT_MODE, key256);96throw new RuntimeException("Shouldn't reach here");97} catch (InvalidKeyException e) {98System.out.println("Caught the right exception");99}100System.out.println("DONE!");101}102}103104private static List<String> getParameters() {105106List<String> cmds = new ArrayList<>();107cmds.add("-cp");108cmds.add(JAR_FILE);109cmds.add(NAME);110// Argument to run the Test section of class inside the jar file.111cmds.add("run");112return cmds;113}114115}116117118