Blame SOURCES/TestCryptoLevel.java

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