The Policy

  

It has been mentioned that a protection domain has the grant entry. The grant entry is mapped from a security policy that takes the form of Listing 18-7.

Listing 18-7: The grant entry structure
start example
 grant [SignedBy "signer_names"] [, CodeBase "URL"]       [, Principal [principal_class_name] "principal_name"]       [, Principal [principal_class_name] "principal_name"] ... {     permission permission_class_name [ "target_name" ]                 [, "action"] [, SignedBy "signer_names"];     permission ... }; 
end example
 

The format of the policy file can contain multiple grant entries, as shown in Listing 18-7, as long as they have a different CodeSource . The CodeSource differentiates one grant entry from another in the same file. The only other way to separate grant entries is to have them defined in different files. The Principals , SignedBy , and CodeBase make up the CodeSource associated with the grant entry. The CodeSource , if defined, specifies a URL to download updated class or JAR files for the application. The signedBy , if defined, contains a list of names . The CodeBase searches the keystore entries for matching names and, for each one found, it returns the matching X.509 certificate. When the code base is downloaded, the CodeSource checks the signatures of the class files and JARs to ensure that the individuals in the signedBy list signed the files. See Listing 18-8 for a signedBy example.

Listing 18-8: signedBy example
start example
 grant signedBy "Rich" {   permission java.io.FilePermission "/tmp/*", "read";   permission java.util.PropertyPermission "user.*"; }; 
end example
 

This example will grant the permission set to code only signed by the individual "Rich" after comparing the certificate from the keystore with the signed code. Another example, CodeBase from www.richware.com , is shown in Listing 18-9.

Listing 18-9: CodeBase example
start example
 grant codeBase "http://www.richware.com/*", signedBy "Rich" {     permission java.io.FilePermission "/tmp/*", "read";     permission java.io.SocketPermission "*", "connect"; }; 
end example
 

This example will grant the permission set only to code signed by "Rich" and downloaded from www.richware.com . An individual can also sign individual permission types to validate any new permission code.

The third part of the CodeBase is the principal name. The principal name can be any principal name derived from the java.security.Principal class such as the javax.security.auth.x500.X500Principal class. The purpose of the principal entries is to define the users that are allowed to access the permission when the code is executing. Listing 18-10 shows an example of the executing code.

Listing 18-10: Principal example
start example
 grant principal javax.security.auth.x500.X500Principal "cn=Rich" {   permission java.io.FilePermission "/", "write"; }; 
end example
 

Checking the permission set will only work if the security manager is defined with the -Djava.security.manager system property; however, the policy file is always set. By default, the policy file will be called by the JRE subdirectory ${JRE}/lib/security/java.policy file unless otherwise set with the -Djava.security.policy system property. Listing 18-11 demonstrates the java.policy that is set by default. The example selects the protection domain without the signedBy and CodeBase entry. Selecting the grant entry is defined by which policy file is currently active in the system properties and then selecting the specific grant entry out of the policy file by defining a CodeSource . Listing 18-12 demonstrates an example of getting the policy and the permission collection from the current policy in the system properties.

Listing 18-11: The RichPolicy class: A policy example code
start example
 package com.richware.chap18; import java.security.*; import java.io.*;     /**  * Class RichPolicy  * Description: A custom demonstration of  * printing out default policies.  *  * Copyright:    Copyright (c) 2002 Wiley Publishing, Inc.  * @author Rich Helton <rhelton@richware.com>  * @version 1.0  * DISCLAIMER: Please refer to the disclaimer at the beginning of this book.   */ public class RichPolicy {   /**    * Method main    * Description: The main driver to run the methods.    * @param args (no arguments presently).    *    */   public static void main(String args[]) {     try {       System.out.println("Starting RichPolicy.....");       /*        * Get the Policy        */        Policy localPolicy = Policy.getPolicy();       /*        * Get the CodeSource        * Shown here is an empty CodeSource        */        CodeSource codesource = new CodeSource(null,null);       /*        * Get the Permission Collection        * from the CodeSource        */        PermissionCollection permissioncollection =  localPolicy.getPermissions(codesource);        /*        * Get the current ClassLoader        */        ClassLoader loader = ClassLoader.getSystemClassLoader();       /*        * Get the ProtectionDomain        * from the CodeSource & Permission Collection        */        ProtectionDomain protectiondomain = new  ProtectionDomain(codesource, permissioncollection);       /*        * Get the current Security Manager        */        SecurityManager sm = System.getSecurityManager();        System.out.println("********Security Manager**********");        System.out.println(sm);        System.out.println("********CodeSource**********");        System.out.println(codesource);        System.out.println("********ClassLoader*********");        System.out.println(loader);        System.out.println("********Protection Domain********");        System.out.println(protectiondomain);        System.out.println("********Permissions*********");        System.out.println(permissioncollection);     } catch (Exception e) {       e.printStackTrace();     }   } } 
end example
 

By default, the outcome of running the JDK 1.4 distribution will produce Listing 18-12.

Listing 18-12: Policy example code output
start example
 >java com.richware.chap18.RichPolicy Starting RichPolicy.....  ********Security Manager********** null ********CodeSource********** (null <no certificates>) ********ClassLoader********* sun.misc.Launcher$AppClassLoader@bac748 ********Protection Domain******** ProtectionDomain  (null <no certificates>)  null  <no principals>  java.security.Permissions@3c5982 (  (java.util.PropertyPermission java.specification.vendor read)  (java.util.PropertyPermission java.vm.specification.vendor read)  (java.util.PropertyPermission path.separator read)  (java.util.PropertyPermission java.vm.name read)  (java.util.PropertyPermission java.class.version read)  (java.util.PropertyPermission os.name read)  (java.util.PropertyPermission java.vendor.url read)  (java.util.PropertyPermission java.vendor read)  (java.util.PropertyPermission java.vm.vendor read)  (java.util.PropertyPermission file.separator read)  (java.util.PropertyPermission os.version read)  (java.util.PropertyPermission java.vm.version read)  (java.util.PropertyPermission java.version read)  (java.util.PropertyPermission line.separator read)   (java.util.PropertyPermission java.vm.specification.version read)  (java.util.PropertyPermission java.specification.name read)  (java.util.PropertyPermission java.vm.specification.name read)  (java.util.PropertyPermission java.specification.version read)  (java.util.PropertyPermission os.arch read)  (java.lang.RuntimePermission stopThread)  (java.net.SocketPermission localhost:1024- listen,resolve) )                      ********Permissions********* java.security.Permissions@3c5982 (  (java.util.PropertyPermission java.specification.vendor read)  (java.util.PropertyPermission java.vm.specification.vendor read)  (java.util.PropertyPermission path.separator read)  (java.util.PropertyPermission java.vm.name read)  (java.util.PropertyPermission java.class.version read)  (java.util.PropertyPermission os.name read)  (java.util.PropertyPermission java.vendor.url read)  (java.util.PropertyPermission java.vendor read)  (java.util.PropertyPermission java.vm.vendor read)  (java.util.PropertyPermission file.separator read)  (java.util.PropertyPermission os.version read)  (java.util.PropertyPermission java.vm.version read)  (java.util.PropertyPermission java.version read)  (java.util.PropertyPermission line.separator read)  (java.util.PropertyPermission java.vm.specification.version read)  (java.util.PropertyPermission java.specification.name read)  (java.util.PropertyPermission java.vm.specification.name read)  (java.util.PropertyPermission java.specification.version read)  (java.util.PropertyPermission os.arch read)  (java.lang.RuntimePermission stopThread)  (java.net.SocketPermission localhost:1024- listen,resolve) ) 
end example
 

Notice in Listing 18-12 that, by default, there is no security manager defined and that the grant permission set is opened up for property and socket permissions from the java.policy file.

  


Java Security Solutions
Java Security Solutions
ISBN: 0764549286
EAN: 2147483647
Year: 2001
Pages: 222

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net