|
1 | 1 | /* |
2 | | - * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
23 | 23 | * questions. |
24 | 24 | */ |
25 | 25 |
|
| 26 | + /* |
| 27 | + * @test |
| 28 | + * @bug 8161527 8180568 |
| 29 | + * @summary NPE is thrown if exempt application is bundled with specific |
| 30 | + * cryptoPerms |
| 31 | + * @requires java.runtime.name ~= "OpenJDK.*" |
| 32 | + * @library /test/lib |
| 33 | + * @run main TestExemption |
| 34 | + */ |
26 | 35 | import javax.crypto.*; |
| 36 | +import java.nio.file.Path; |
27 | 37 | import java.security.*; |
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.List; |
| 40 | +import jdk.test.lib.process.OutputAnalyzer; |
| 41 | +import jdk.test.lib.process.ProcessTools; |
| 42 | +import jdk.test.lib.util.JarUtils; |
28 | 43 |
|
29 | 44 | public class TestExemption { |
30 | 45 |
|
31 | | - public static void main(String[] args) throws Exception { |
| 46 | + private static final String SRC = System.getProperty("test.src"); |
| 47 | + private static final String CLASSES = System.getProperty("test.classes"); |
| 48 | + private static final String NAME = TestExemption.class.getName(); |
| 49 | + private static final String SRC_CLS = NAME + ".class"; |
| 50 | + private static final String JAR_FILE = NAME + ".jar"; |
| 51 | + private static final String CRYPT_PERM = "cryptoPerms"; |
32 | 52 |
|
33 | | - KeyGenerator kg = KeyGenerator.getInstance("AES"); |
34 | | - kg.init(128); |
35 | | - SecretKey key128 = kg.generateKey(); |
| 53 | + public static void main(String[] args) throws Exception { |
36 | 54 |
|
37 | | - kg.init(192); |
38 | | - SecretKey key192 = kg.generateKey(); |
| 55 | + // With no argument passed, compile the same class, jar it and run the |
| 56 | + // test section of the jar file which is nothing but else section here. |
| 57 | + if (args.length == 0) { |
| 58 | + JarUtils.createJarFile( |
| 59 | + Path.of(JAR_FILE), Path.of(CLASSES), Path.of(SRC_CLS)); |
| 60 | + JarUtils.updateJarFile( |
| 61 | + Path.of(JAR_FILE), Path.of(SRC), Path.of(CRYPT_PERM)); |
| 62 | + OutputAnalyzer oa = ProcessTools.executeTestJava( |
| 63 | + getParameters().toArray(String[]::new)); |
| 64 | + System.out.println(oa.getOutput()); |
| 65 | + oa.shouldHaveExitValue(0); |
| 66 | + } else { |
| 67 | + // Set the crypto policy to limited so that additional policy can be |
| 68 | + // supplemented through cryptoPerms when bundled inside a jar file. |
| 69 | + Security.setProperty("crypto.policy", "limited"); |
| 70 | + KeyGenerator kg = KeyGenerator.getInstance("AES"); |
| 71 | + kg.init(128); |
| 72 | + SecretKey key128 = kg.generateKey(); |
39 | 73 |
|
40 | | - kg.init(256); |
41 | | - SecretKey key256 = kg.generateKey(); |
| 74 | + kg.init(192); |
| 75 | + SecretKey key192 = kg.generateKey(); |
42 | 76 |
|
43 | | - Cipher c = Cipher.getInstance("AES/CBC/NoPadding"); |
| 77 | + kg.init(256); |
| 78 | + SecretKey key256 = kg.generateKey(); |
44 | 79 |
|
45 | | - System.out.println("Testing 128-bit"); |
46 | | - c.init(Cipher.ENCRYPT_MODE, key128); |
| 80 | + int maxAllowed = Cipher.getMaxAllowedKeyLength("AES"); |
| 81 | + System.out.println("Max allowed: " + maxAllowed); |
| 82 | + // With limited crypto and bundled cryptoPerms maximum allowed |
| 83 | + // length of AES is upto 192. |
| 84 | + if (maxAllowed > 192) { |
| 85 | + throw new RuntimeException(">192 not supported"); |
| 86 | + } |
47 | 87 |
|
48 | | - System.out.println("Testing 192-bit"); |
49 | | - c.init(Cipher.ENCRYPT_MODE, key192); |
| 88 | + Cipher c = Cipher.getInstance("AES/CBC/NoPadding"); |
| 89 | + System.out.println("Testing 128-bit"); |
| 90 | + c.init(Cipher.ENCRYPT_MODE, key128); |
50 | 91 |
|
51 | | - try { |
52 | | - System.out.println("Testing 256-bit"); |
53 | | - c.init(Cipher.ENCRYPT_MODE, key256); |
54 | | - } catch (InvalidKeyException e) { |
55 | | - System.out.println("Caught the right exception"); |
| 92 | + System.out.println("Testing 192-bit"); |
| 93 | + c.init(Cipher.ENCRYPT_MODE, key192); |
| 94 | + try { |
| 95 | + System.out.println("Testing 256-bit"); |
| 96 | + c.init(Cipher.ENCRYPT_MODE, key256); |
| 97 | + throw new RuntimeException("Shouldn't reach here"); |
| 98 | + } catch (InvalidKeyException e) { |
| 99 | + System.out.println("Caught the right exception"); |
| 100 | + } |
| 101 | + System.out.println("DONE!"); |
56 | 102 | } |
| 103 | + } |
| 104 | + |
| 105 | + private static List<String> getParameters() { |
57 | 106 |
|
58 | | - System.out.println("DONE!"); |
| 107 | + List<String> cmds = new ArrayList<>(); |
| 108 | + cmds.add("-cp"); |
| 109 | + cmds.add(JAR_FILE); |
| 110 | + cmds.add(NAME); |
| 111 | + // Argument to run the Test section of class inside the jar file. |
| 112 | + cmds.add("run"); |
| 113 | + return cmds; |
59 | 114 | } |
| 115 | + |
60 | 116 | } |
0 commit comments