Blame SOURCES/TestCryptoLevel.java

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