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 8061842
29  * @summary Package jurisdiction policy files as something other than JAR
30  * @run main/othervm TestUnlimited use_default default
31  * @run main/othervm TestUnlimited "" exception
32  * @run main/othervm TestUnlimited limited limited
33  * @run main/othervm TestUnlimited unlimited unlimited
34  * @run main/othervm TestUnlimited unlimited/ unlimited
35  * @run main/othervm TestUnlimited NosuchDir exception
36  * @run main/othervm TestUnlimited . exception
37  * @run main/othervm TestUnlimited /tmp/unlimited exception
38  * @run main/othervm TestUnlimited ../policy/unlimited exception
39  * @run main/othervm TestUnlimited ./unlimited exception
40  * @run main/othervm TestUnlimited /unlimited exception
41  */
42 import javax.crypto.*;
43 import java.security.Security;
44 import java.nio.file.*;
45 import java.util.stream.*;
46 
47 public class TestUnlimited {
48 
49     private enum Result {
50         UNLIMITED,
51         LIMITED,
52         EXCEPTION,
53         UNKNOWN
54     };
55 
56     /*
57      * Grab the default policy entry from java.security.
58      *
59      * If the input java.security file is malformed
60      * (missing crypto.policy, attribute/no value, etc), throw
61      * exception.  split() might throw AIOOB which
62      * is ok behavior.
63      */
getDefaultPolicy()64     private static String getDefaultPolicy() throws Exception {
65         String javaHome = System.getProperty("java.home");
66         Path path = Paths.get(javaHome, "conf", "security", "java.security");
67 
68         try (Stream<String> lines = Files.lines(path)) {
69             return lines.filter(x -> x.startsWith("crypto.policy="))
70                     .findFirst().orElseThrow(
71                             () -> new Exception("Missing crypto.policy"))
72                     .split("=")[1].trim();
73         }
74     }
75 
main(String[] args)76     public static void main(String[] args) throws Exception {
77         /*
78          * Override the Security property to allow for unlimited policy.
79          * Would need appropriate permissions if Security Manager were
80          * active.
81          */
82         if (args.length != 2) {
83             throw new Exception("Two args required");
84         }
85 
86         String testStr = args[0];
87         String expectedStr = args[1];
88         if (testStr.equals("use_default")) {
89             expectedStr = getDefaultPolicy();
90         }
91 
92         Result expected = Result.UNKNOWN;  // avoid NPE warnings
93         Result result;
94 
95         switch (expectedStr) {
96         case "unlimited":
97             expected = Result.UNLIMITED;
98             break;
99         case "limited":
100             expected = Result.LIMITED;
101             break;
102         case "exception":
103             expected = Result.EXCEPTION;
104             break;
105         default:
106             throw new Exception("Unexpected argument");
107         }
108 
109         System.out.println("Testing: " + testStr);
110         if (testStr.equals("\"\"")) {
111             Security.setProperty("crypto.policy", "");
112         } else {
113             // skip default case.
114             if (!testStr.equals("use_default")) {
115                 Security.setProperty("crypto.policy", testStr);
116             }
117         }
118 
119         /*
120          * Use the AES as the test Cipher
121          * If there is an error initializing, we will never get past here.
122          */
123         try {
124             int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES");
125             System.out.println("max AES key len:" + maxKeyLen);
126             if (maxKeyLen > 128) {
127                 System.out.println("Unlimited policy is active");
128                 result = Result.UNLIMITED;
129             } else {
130                 System.out.println("Unlimited policy is NOT active");
131                 result = Result.LIMITED;
132             }
133         } catch (Throwable e) {
134             //ExceptionInInitializerError's
135             result = Result.EXCEPTION;
136         }
137 
138         System.out.println(
139                 "Expected:\t" + expected + "\nResult:\t\t" + result);
140         if (!expected.equals(result)) {
141             throw new Exception("Didn't match");
142         }
143 
144         System.out.println("DONE!");
145     }
146 }
147