Blame SOURCES/TestCryptoLevel.java

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