Blame SOURCES/TestCryptoLevel.java

8f4f46
/* TestCryptoLevel -- Ensure unlimited crypto policy is in use.
8f4f46
   Copyright (C) 2012 Red Hat, Inc.
8f4f46
8f4f46
This program is free software: you can redistribute it and/or modify
8f4f46
it under the terms of the GNU Affero General Public License as
8f4f46
published by the Free Software Foundation, either version 3 of the
8f4f46
License, or (at your option) any later version.
8f4f46
8f4f46
This program is distributed in the hope that it will be useful,
8f4f46
but WITHOUT ANY WARRANTY; without even the implied warranty of
8f4f46
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8f4f46
GNU Affero General Public License for more details.
8f4f46
8f4f46
You should have received a copy of the GNU Affero General Public License
8f4f46
along with this program.  If not, see <http://www.gnu.org/licenses/>.
8f4f46
*/
8f4f46
8f4f46
import java.lang.reflect.Field;
8f4f46
import java.lang.reflect.Method;
8f4f46
import java.lang.reflect.InvocationTargetException;
8f4f46
8f4f46
import java.security.Permission;
8f4f46
import java.security.PermissionCollection;
8f4f46
8f4f46
public class TestCryptoLevel
8f4f46
{
8f4f46
  public static void main(String[] args)
8f4f46
    throws NoSuchFieldException, ClassNotFoundException,
8f4f46
           IllegalAccessException, InvocationTargetException
8f4f46
  {
8f4f46
    Class cls = null;
8f4f46
    Method def = null, exempt = null;
8f4f46
8f4f46
    try
8f4f46
      {
8f4f46
        cls = Class.forName("javax.crypto.JceSecurity");
8f4f46
      }
8f4f46
    catch (ClassNotFoundException ex)
8f4f46
      {
8f4f46
        System.err.println("Running a non-Sun JDK.");
8f4f46
        System.exit(0);
8f4f46
      }
8f4f46
    try
8f4f46
      {
8f4f46
        def = cls.getDeclaredMethod("getDefaultPolicy");
8f4f46
        exempt = cls.getDeclaredMethod("getExemptPolicy");
8f4f46
      }
8f4f46
    catch (NoSuchMethodException ex)
8f4f46
      {
8f4f46
        System.err.println("Running IcedTea with the original crypto patch.");
8f4f46
        System.exit(0);
8f4f46
      }
8f4f46
    def.setAccessible(true);
8f4f46
    exempt.setAccessible(true);
8f4f46
    PermissionCollection defPerms = (PermissionCollection) def.invoke(null);
8f4f46
    PermissionCollection exemptPerms = (PermissionCollection) exempt.invoke(null);
8f4f46
    Class apCls = Class.forName("javax.crypto.CryptoAllPermission");
8f4f46
    Field apField = apCls.getDeclaredField("INSTANCE");
8f4f46
    apField.setAccessible(true);
8f4f46
    Permission allPerms = (Permission) apField.get(null);
8f4f46
    if (defPerms.implies(allPerms) && (exemptPerms == null || exemptPerms.implies(allPerms)))
8f4f46
      {
8f4f46
        System.err.println("Running with the unlimited policy.");
8f4f46
        System.exit(0);
8f4f46
      }
8f4f46
    else
8f4f46
      {
8f4f46
        System.err.println("WARNING: Running with a restricted crypto policy.");
8f4f46
        System.exit(-1);
8f4f46
      }
8f4f46
  }
8f4f46
}