public PolicySpiFile(Policy.Parameters params) { if (params == null) { pf = new PolicyFile(); } else { if (!(params instanceof URIParameter)) { throw new IllegalArgumentException ("Unrecognized policy parameter: " + params); } URIParameter uriParam = (URIParameter)params; try { pf = new PolicyFile(uriParam.getURI().toURL()); } catch (MalformedURLException mue) { throw new IllegalArgumentException("Invalid URIParameter", mue); } } }
public static void main(String[] args) throws Exception { // Check policy with no java.security.policy property set Policy p = Policy.getPolicy(); checkPolicy(p); // Check policy with java.security.policy '=' option System.setProperty("java.security.policy", "Extra.policy"); p.refresh(); checkPolicy(p); // Check policy with java.security.policy override '==' option System.setProperty("java.security.policy", "=Extra.policy"); p.refresh(); checkPolicy(p); // Check Policy.getInstance URI policyURI = Paths.get(System.getProperty("test.src"), "Extra.policy").toUri(); p = Policy.getInstance("JavaPolicy", new URIParameter(policyURI)); checkPolicy(p); }
/** * Construct a JAAS configuration object per storm configuration file * @param storm_conf Storm configuration * @return JAAS configuration object */ public static Configuration GetConfiguration(Map storm_conf) { Configuration login_conf = null; //find login file configuration from Storm configuration String loginConfigurationFile = (String)storm_conf.get("java.security.auth.login.config"); if ((loginConfigurationFile != null) && (loginConfigurationFile.length()>0)) { try { URI config_uri = new File(loginConfigurationFile).toURI(); login_conf = Configuration.getInstance("JavaLoginConfig", new URIParameter(config_uri)); } catch (NoSuchAlgorithmException ex1) { if (ex1.getCause() instanceof FileNotFoundException) throw new RuntimeException("configuration file "+loginConfigurationFile+" could not be found"); else throw new RuntimeException(ex1); } catch (Exception ex2) { throw new RuntimeException(ex2); } } return login_conf; }
/** * Returns a Policy object that represents a custom policy to use instead * of granting {@link AllPermission} to a {@link CodeSource} * * @return a {@link Policy} object to delegate to. May be null, which * indicates that no policy exists and AllPermissions should be granted * instead. */ private Policy getCustomTrustedPolicy() { String key = DeploymentConfiguration.KEY_SECURITY_TRUSTED_POLICY; String policyLocation = JNLPRuntime.getConfiguration().getProperty(key); Policy policy = null; if (policyLocation != null) { try { URI policyUri = new URI("file://" + policyLocation); policy = Policy.getInstance("JavaPolicy", new URIParameter(policyUri)); } catch (Exception e) { OutputController.getLogger().log(OutputController.Level.ERROR_ALL, e); } } // return the appropriate policy, or null return policy; }
/** * Construct a JAAS configuration object per storm configuration file * * @param storm_conf * Storm configuration * @return JAAS configuration object */ public static Configuration GetConfiguration(Map storm_conf) { Configuration login_conf = null; // find login file configuration from Storm configuration String loginConfigurationFile = (String) storm_conf .get("java.security.auth.login.config"); if ((loginConfigurationFile != null) && (loginConfigurationFile.length() > 0)) { try { URI config_uri = new File(loginConfigurationFile).toURI(); login_conf = Configuration.getInstance("JavaLoginConfig", new URIParameter(config_uri)); } catch (NoSuchAlgorithmException ex1) { if (ex1.getCause() instanceof FileNotFoundException) throw new RuntimeException("configuration file " + loginConfigurationFile + " could not be found"); else throw new RuntimeException(ex1); } catch (Exception ex2) { throw new RuntimeException(ex2); } } return login_conf; }
/** * Construct a JAAS configuration object per storm configuration file * * @param storm_conf Storm configuration * @return JAAS configuration object */ public static Configuration GetConfiguration(Map storm_conf) { Configuration login_conf = null; // find login file configuration from Storm configuration String loginConfigurationFile = (String) storm_conf.get("java.security.auth.login.config"); if ((loginConfigurationFile != null) && (loginConfigurationFile.length() > 0)) { File config_file = new File(loginConfigurationFile); if (!config_file.canRead()) { throw new RuntimeException("File " + loginConfigurationFile + " cannot be read."); } try { URI config_uri = config_file.toURI(); login_conf = Configuration.getInstance("JavaLoginConfig", new URIParameter(config_uri)); } catch (Exception ex) { throw new RuntimeException(ex); } } return login_conf; }
public Spi(final Configuration.Parameters params) throws IOException { // call in a doPrivileged // // we have already passed the Configuration.getInstance // security check. also this class is not freely accessible // (it is in the "sun" package). try { AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() { public Void run() throws IOException { if (params == null) { init(); } else { if (!(params instanceof URIParameter)) { throw new IllegalArgumentException ("Unrecognized parameter: " + params); } URIParameter uriParam = (URIParameter)params; url = uriParam.getURI().toURL(); init(); } return null; } }); } catch (PrivilegedActionException pae) { throw (IOException)pae.getException(); } // if init() throws some other RuntimeException, // let it percolate up naturally. }
public static void main(String[] args) throws Exception { File f = new File("a b c"); FileOutputStream fos = new FileOutputStream(f); fos.write("".getBytes()); fos.close(); System.err.println(f.toURI()); try { Configuration.getInstance("JavaLoginConfig", new URIParameter(f.toURI())); } finally { f.delete(); } }
public static void main(String[] args) throws Exception { Security.addProvider(new TestProvider()); MySecureClassLoader scl = new MySecureClassLoader(); File policyFile = new File(System.getProperty("test.src", "."), "DefineClass.policy"); Policy p = Policy.getInstance("JavaPolicy", new URIParameter(policyFile.toURI())); Policy.setPolicy(p); System.setSecurityManager(new SecurityManager()); ArrayList<Permission> perms1 = getPermissions(scl, p, "http://localhost/", "foo.Foo", FOO_CLASS, null); checkPerms(perms1, GRANTED_PERMS); ArrayList<Permission> perms2 = getPermissions(scl, p, "http://127.0.0.1/", "bar.Bar", BAR_CLASS, null); checkPerms(perms2, GRANTED_PERMS); assert(perms1.equals(perms2)); // check that class signed by baz is granted an additional permission Certificate[] chain = new Certificate[] {getCert(BAZ_CERT)}; ArrayList<Permission> perms3 = getPermissions(scl, p, "http://localhost/", "baz.Baz", BAZ_CLASS, chain); List<Permission> perms = new ArrayList<>(Arrays.asList(GRANTED_PERMS)); perms.add(new PropertyPermission("user.dir", "read")); checkPerms(perms3, perms.toArray(new Permission[0])); }
public static void main(String[] args) throws Exception { URI uri = new File(System.getProperty("test.src", "."), "BadPolicyFile.policy").toURI(); Policy.setPolicy(Policy.getInstance("JavaPolicy", new URIParameter(uri))); System.setSecurityManager(new SecurityManager()); try { String javahome = System.getProperty("java.home"); throw new Exception("Expected AccessControlException"); } catch (AccessControlException ace) { System.out.println("Test PASSED"); } }
public static void main(String[] args) throws Exception { if (initSecmod() == false) { return; } if ("sparc".equals(System.getProperty("os.arch")) == false) { // we have not updated other platforms with the proper NSS libraries yet System.out.println("Test currently works only on solaris-sparc, skipping"); return; } String configName = BASE + SEP + "fips.cfg"; Provider p = getSunPKCS11(configName); System.out.println(p); Security.addProvider(p); Security.removeProvider("SunJSSE"); Provider jsse = new com.sun.net.ssl.internal.ssl.Provider(p); Security.addProvider(jsse); System.out.println(jsse.getInfo()); KeyStore ks = KeyStore.getInstance("PKCS11", p); ks.load(null, "test12".toCharArray()); X509Certificate server = loadCertificate("certs/server.cer"); X509Certificate ca = loadCertificate("certs/ca.cer"); X509Certificate anchor = loadCertificate("certs/anchor.cer"); if (args.length > 1 && "sm".equals(args[0])) { Policy.setPolicy(Policy.getInstance("JavaPolicy", new URIParameter(new File(BASE, args[1]).toURI()))); System.setSecurityManager(new SecurityManager()); } KeyStore trustStore = KeyStore.getInstance("JKS"); trustStore.load(null, null); trustStore.setCertificateEntry("anchor", anchor); TrustManagerFactory tmf = TrustManagerFactory.getInstance("PKIX"); tmf.init(trustStore); X509TrustManager tm = (X509TrustManager)tmf.getTrustManagers()[0]; X509Certificate[] chain = {server, ca, anchor}; tm.checkServerTrusted(chain, "RSA"); System.out.println("OK"); }
private int testURIParam(int testnum) throws Exception { // get an instance of JavaLoginConfig // from SUN and have it read from the URI File file = new File(System.getProperty("test.src", "."), "GetInstance.configURI"); URI uri = file.toURI(); URIParameter uriParam = new URIParameter(uri); Configuration c = Configuration.getInstance(JAVA_CONFIG, uriParam); doTestURI(c, uriParam, testnum++); return testnum; }