Blame SOURCES/TestCryptoLevel.java

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