1 |
/* TestCryptoLevel -- Ensure unlimited crypto policy is in use. |
2 |
Copyright (C) 2012 Red Hat, Inc. |
3 |
|
4 |
This program is free software: you can redistribute it and/or modify |
5 |
it under the terms of the GNU Affero General Public License as |
6 |
published by the Free Software Foundation, either version 3 of the |
7 |
License, or (at your option) any later version. |
8 |
|
9 |
This program is distributed in the hope that it will be useful, |
10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 |
GNU Affero General Public License for more details. |
13 |
|
14 |
You should have received a copy of the GNU Affero General Public License |
15 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 |
*/ |
17 |
|
18 |
import java.lang.reflect.Field; |
19 |
import java.lang.reflect.Method; |
20 |
import java.lang.reflect.InvocationTargetException; |
21 |
|
22 |
import java.security.Permission; |
23 |
import java.security.PermissionCollection; |
24 |
|
25 |
public class TestCryptoLevel |
26 |
{ |
27 |
public static void main(String[] args) |
28 |
throws NoSuchFieldException, ClassNotFoundException, |
29 |
IllegalAccessException, InvocationTargetException |
30 |
{ |
31 |
Class<?> cls = null; |
32 |
Method def = null, exempt = null; |
33 |
|
34 |
try |
35 |
{ |
36 |
cls = Class.forName("javax.crypto.JceSecurity"); |
37 |
} |
38 |
catch (ClassNotFoundException ex) |
39 |
{ |
40 |
System.err.println("Running a non-Sun JDK."); |
41 |
System.exit(0); |
42 |
} |
43 |
try |
44 |
{ |
45 |
def = cls.getDeclaredMethod("getDefaultPolicy"); |
46 |
exempt = cls.getDeclaredMethod("getExemptPolicy"); |
47 |
} |
48 |
catch (NoSuchMethodException ex) |
49 |
{ |
50 |
System.err.println("Running IcedTea with the original crypto patch."); |
51 |
System.exit(0); |
52 |
} |
53 |
def.setAccessible(true); |
54 |
exempt.setAccessible(true); |
55 |
PermissionCollection defPerms = (PermissionCollection) def.invoke(null); |
56 |
PermissionCollection exemptPerms = (PermissionCollection) exempt.invoke(null); |
57 |
Class<?> apCls = Class.forName("javax.crypto.CryptoAllPermission"); |
58 |
Field apField = apCls.getDeclaredField("INSTANCE"); |
59 |
apField.setAccessible(true); |
60 |
Permission allPerms = (Permission) apField.get(null); |
61 |
if (defPerms.implies(allPerms) && (exemptPerms == null || exemptPerms.implies(allPerms))) |
62 |
{ |
63 |
System.err.println("Running with the unlimited policy."); |
64 |
System.exit(0); |
65 |
} |
66 |
else |
67 |
{ |
68 |
System.err.println("WARNING: Running with a restricted crypto policy."); |
69 |
System.exit(-1); |
70 |
} |
71 |
} |
72 |
} |