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