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