Blame SOURCES/TestCryptoLevel.java

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