Blame SOURCES/TestCryptoLevel.java

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