|
1 /* |
|
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 /** |
|
27 * @test |
|
28 * @bug 8169335 |
|
29 * @summary Add a crypto policy fallback in case Security Property |
|
30 * 'crypto.policy' does not exist. |
|
31 * @run main/othervm CryptoPolicyFallback |
|
32 */ |
|
33 import java.io.*; |
|
34 import java.nio.file.*; |
|
35 import java.util.stream.*; |
|
36 import javax.crypto.*; |
|
37 |
|
38 /* |
|
39 * Take the current java.security file, strip out the 'crypto.policy' entry, |
|
40 * write to a new file in the current directory, then use that file as the |
|
41 * replacement java.security file. This test will fail if the crypto.policy |
|
42 * entry doesn't match the compiled in value. |
|
43 */ |
|
44 public class CryptoPolicyFallback { |
|
45 |
|
46 private static final String FILENAME = "java.security"; |
|
47 |
|
48 public static void main(String[] args) throws Exception { |
|
49 |
|
50 String javaHome = System.getProperty("java.home"); |
|
51 |
|
52 Path path = Paths.get(javaHome, "conf", "security", FILENAME); |
|
53 |
|
54 /* |
|
55 * Get the default value. |
|
56 */ |
|
57 String defaultPolicy; |
|
58 try (Stream<String> lines = Files.lines(path)) { |
|
59 /* |
|
60 * If the input java.security file is malformed |
|
61 * (missing crypto.policy, attribute/no value, etc), throw |
|
62 * exception. split() might throw AIOOB which |
|
63 * is ok behavior. |
|
64 */ |
|
65 defaultPolicy = lines.filter(x -> x.startsWith("crypto.policy=")) |
|
66 .findFirst().orElseThrow( |
|
67 () -> new Exception("Missing crypto.policy")) |
|
68 .split("=")[1].trim(); |
|
69 } |
|
70 |
|
71 /* |
|
72 * We know there is at least one crypto.policy entry, strip |
|
73 * all of them out of the java.security file. |
|
74 */ |
|
75 try (PrintWriter out = new PrintWriter(FILENAME); |
|
76 Stream<String> lines = Files.lines(path)) { |
|
77 lines.filter(x -> !x.trim().startsWith("crypto.policy=")) |
|
78 .forEach(out::println); |
|
79 } |
|
80 |
|
81 /* |
|
82 * "-Djava.security.properties==file" does a complete replacement |
|
83 * of the system java.security file. i.e. value must be "=file" |
|
84 */ |
|
85 System.setProperty("java.security.properties", "=" + FILENAME); |
|
86 |
|
87 /* |
|
88 * Find out expected value. |
|
89 */ |
|
90 int expected; |
|
91 switch (defaultPolicy) { |
|
92 case "limited": |
|
93 expected = 128; |
|
94 break; |
|
95 case "unlimited": |
|
96 expected = Integer.MAX_VALUE; |
|
97 break; |
|
98 default: |
|
99 throw new Exception( |
|
100 "Unexpected Default Policy Value: " + defaultPolicy); |
|
101 } |
|
102 |
|
103 /* |
|
104 * Do the actual check. If the JCE Framework can't initialize |
|
105 * an Exception is normally thrown here. |
|
106 */ |
|
107 int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES"); |
|
108 |
|
109 System.out.println("Default Policy: " + defaultPolicy |
|
110 + "\nExpected max AES key length: " + expected |
|
111 + ", received : " + maxKeyLen); |
|
112 |
|
113 if (expected != maxKeyLen) { |
|
114 throw new Exception("Wrong Key Length size!"); |
|
115 } |
|
116 |
|
117 System.out.println("PASSED!"); |
|
118 } |
|
119 } |