org/objectweb/proactive/ext/security/ProActiveSecurityDescriptorHandler.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.ext.security;
00032 
00033 import java.io.IOException;
00034 import java.security.Provider;
00035 import java.security.Security;
00036 import java.security.cert.X509Certificate;
00037 import java.util.ArrayList;
00038 
00039 import org.apache.log4j.Logger;
00040 import org.objectweb.proactive.core.util.log.Loggers;
00041 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00042 import org.objectweb.proactive.core.xml.handler.AbstractUnmarshallerDecorator;
00043 import org.objectweb.proactive.core.xml.handler.BasicUnmarshaller;
00044 import org.objectweb.proactive.core.xml.handler.SingleValueUnmarshaller;
00045 import org.objectweb.proactive.core.xml.handler.UnmarshallerHandler;
00046 import org.objectweb.proactive.core.xml.io.Attributes;
00047 import org.objectweb.proactive.ext.security.exceptions.InvalidPolicyFile;
00048 import org.objectweb.proactive.ext.security.securityentity.DefaultEntity;
00049 import org.objectweb.proactive.ext.security.securityentity.Entity;
00050 import org.objectweb.proactive.ext.security.securityentity.EntityVirtualNode;
00051 import org.xml.sax.SAXException;
00052 
00053 
00060 public class ProActiveSecurityDescriptorHandler
00061     extends AbstractUnmarshallerDecorator {
00062     protected PolicyServer policyServer;
00063     protected X509Certificate applicationCertificate;
00064     protected String pkcs12Keystore = null;
00065     protected String applicationName = null;
00066     protected String applicationPrivateKeyPath = null;
00067     protected String applicationCertificatePath = null;
00068     protected ArrayList<PolicyRule> policyRules = null;
00069     static Logger logger = ProActiveLogger.getLogger(Loggers.SECURITY);
00070     protected static String PROACTIVE_SECURITY_TAG = "Policy";
00071     protected String RULE_TAG = "Rule";
00072     protected String ENTITY_TAG = "Entity";
00073     protected String RULES_TAG = "Rules";
00074     protected String PRIVATE_KEY_TAG = "PrivateKey";
00075     protected String CERTIFICATE_TAG = "Certificate";
00076     protected String TRUSTED_CERTIFICATION_AUTHORITY_TAG = "TrustedCertificationAuthority";
00077     protected String ENTITY_FROM_TAG = "From";
00078     protected String ENTITY_TO_TAG = "To";
00079     protected String RULE_COMMUNICATION_TAG = "Communication";
00080     protected String RULE_COMMUNICATION_TO_TAG = "Request";
00081     protected String RULE_COMMUNICATION_FROM_TAG = "Reply";
00082     protected String RULE_COMMUNICATION_MIGRATION_TAG = "Migration";
00083     protected String RULE_COMMUNICATION_AOCREATION_TAG = "OACreation";
00084     protected String RULE_COMMUNICATION_ATTRIBUTES_TAG = "Attributes";
00085     protected String RULE_MIGRATION_AUTHORIZED = "authorized";
00086     protected String RULE_MIGRATION_DENIED = "denied";
00087     protected String RULE_AOCREATION_AUTHORIZED = "authorized";
00088     protected String APPLICATION_NAME_TAG = "ApplicationName";
00089     protected String PKCS12_CERTIFICATE = "PKCS12KeyStore";
00090 
00091     static {
00092         ProActiveSecurity.loadProvider();
00093     }
00094 
00098     public ProActiveSecurityDescriptorHandler() {
00099         super();
00100         Provider myProvider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
00101         Security.addProvider(myProvider);
00102         policyServer = new PolicyServer();
00103         addHandler(APPLICATION_NAME_TAG, new SingleValueUnmarshaller());
00104         addHandler(PRIVATE_KEY_TAG, new SingleValueUnmarshaller());
00105         addHandler(CERTIFICATE_TAG, new SingleValueUnmarshaller());
00106         addHandler(PKCS12_CERTIFICATE, new SingleValueUnmarshaller());
00107 
00108         addHandler(RULES_TAG, new RulesHandler());
00109     }
00110 
00111     /* (non-Javadoc)
00112      * @see org.objectweb.proactive.core.xml.handler.AbstractUnmarshallerDecorator#notifyEndActiveHandler(java.lang.String, org.objectweb.proactive.core.xml.handler.UnmarshallerHandler)
00113      */
00114     protected void notifyEndActiveHandler(String name,
00115         UnmarshallerHandler activeHandler) throws SAXException {
00116         //        if (name.equals(PRIVATE_KEY_TAG)) {
00117         //           applicationPrivateKeyPath = (String) activeHandler.getResultObject();
00118         //           policyServer.setApplicationPrivateKey(applicationPrivateKeyPath);
00119         //        } else if (name.equals(CERTIFICATE_TAG)) {
00120         //            applicationCertificatePath = (String) activeHandler.getResultObject();
00121         //            policyServer.setApplicationCertificate(applicationCertificatePath);
00122         //        } else 
00123         if (name.equals(RULES_TAG)) {
00124             
00125             policyRules =   (ArrayList) activeHandler.getResultObject();
00126             policyServer.setPolicies(policyRules);
00127         } else if (name.equals(APPLICATION_NAME_TAG)) {
00128             applicationName = (String) activeHandler.getResultObject();
00129             policyServer.setApplicationName(applicationName);
00130         } else if (name.equals(PKCS12_CERTIFICATE)) {
00131             pkcs12Keystore = (String) activeHandler.getResultObject();
00132             policyServer.setPKCS12Keystore(pkcs12Keystore);
00133         }
00134     }
00135 
00136     /* (non-Javadoc)
00137      * @see org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#getResultObject()
00138      */
00139     public Object getResultObject() throws SAXException {
00140         //        if (pkcs12Keystore != null) {
00141         //            KeyStore keyStore = null;
00142         //        
00143         //        try {
00144         //             keyStore = KeyStore.getInstance("PKCS12", "BC");
00145         //        keyStore.load(new FileInputStream(pkcs12Keystore), "ha".toCharArray());
00146         //        } catch (Exception e ) {
00147         //            e.printStackTrace();
00148         //        }
00149         //        return new PolicyServer(keyStore, policyRules);
00150         //    } else {
00151         //        policyServer = new PolicyServer();
00152         //        policyServer.setApplicationPrivateKey(applicationPrivateKeyPath);
00153         //        policyServer.setApplicationCertificate(applicationCertificatePath);
00154         //        policyServer.setPolicies(policyRules);
00155         //        policyServer.setApplicationName(applicationName);
00156         return policyServer;
00157         //    }
00158     }
00159 
00160     /* (non-Javadoc)
00161      * @see org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#startContextElement(java.lang.String, org.objectweb.proactive.core.xml.io.Attributes)
00162      */
00163     public void startContextElement(String name, Attributes attributes)
00164         throws SAXException {
00165     }
00166 
00170     private class RulesHandler extends AbstractUnmarshallerDecorator {
00171         RuleHandler ruleHandler = null;
00172         private ArrayList<PolicyRule> policies;
00173 
00174         public RulesHandler() {
00175             super();
00176             policies = new ArrayList<PolicyRule>();
00177             ruleHandler = new RuleHandler();
00178             addHandler(RULE_TAG, ruleHandler);
00179         }
00180 
00181         public void startContextElement(String name, Attributes attributes)
00182             throws org.xml.sax.SAXException {
00183             if (name.equals(RULE_TAG)) {
00184                 //policies.add(activeHandler.getResultObject());
00185                 // ruleHandler = new RuleHandler();
00186             }
00187         }
00188 
00189         protected void notifyEndActiveHandler(String name,
00190             UnmarshallerHandler activeHandler) throws org.xml.sax.SAXException {
00191             // new handler otherwise all policies reference the same object, maybe there is another thing to do
00192             // addHandler(RULE_TAG, new RuleHandler());
00193             // ruleHandler = new RuleHandler();
00194             if (name.equals(RULE_TAG)) {
00195                 policies.add((PolicyRule) activeHandler.getResultObject());
00196                 //        ruleHandler = new RuleHandler();
00197             }
00198             addHandler(RULE_TAG, new RuleHandler());
00199         }
00200 
00201         /* (non-Javadoc)
00202          * @see org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#getResultObject()
00203          */
00204         public Object getResultObject() throws SAXException {
00205             return policies;
00206         }
00207     }
00208 
00209     // end inner class RulesHandler
00210 
00214     private static class InitialHandler extends AbstractUnmarshallerDecorator {
00215         // line added to return a ProactiveDescriptorHandler object
00216         private ProActiveSecurityDescriptorHandler proActiveSecurityDescriptorHandler;
00217         protected PolicyServer ps;
00218 
00219         private InitialHandler() {
00220             super();
00221             proActiveSecurityDescriptorHandler = new ProActiveSecurityDescriptorHandler();
00222             this.addHandler(PROACTIVE_SECURITY_TAG,
00223                 proActiveSecurityDescriptorHandler);
00224         }
00225 
00226         public Object getResultObject() throws org.xml.sax.SAXException {
00227             return ps; //(PolicyServer) proActiveSecurityDescriptorHandler.getResultObject();
00228         }
00229 
00230         public void startContextElement(String name, Attributes attributes)
00231             throws org.xml.sax.SAXException {
00232         }
00233 
00234         protected void notifyEndActiveHandler(String name,
00235             UnmarshallerHandler activeHandler) throws org.xml.sax.SAXException {
00236             if (name.equals(PROACTIVE_SECURITY_TAG)) {
00237                 ps = (PolicyServer) activeHandler.getResultObject();
00238             }
00239         }
00240     }
00241 
00245     private class RuleHandler extends AbstractUnmarshallerDecorator {
00246         private PolicyRule policy;
00247 
00248         public RuleHandler() {
00249             super();
00250             policy = new PolicyRule();
00251             addHandler(ENTITY_FROM_TAG, new EntityCollector());
00252             addHandler(ENTITY_TO_TAG, new EntityCollector());
00253             addHandler(RULE_COMMUNICATION_TAG,
00254                 new CommunicationCollectionHandler());
00255             addHandler(RULE_COMMUNICATION_AOCREATION_TAG,
00256                 new SingleValueUnmarshaller());
00257             addHandler(RULE_COMMUNICATION_MIGRATION_TAG,
00258                 new SingleValueUnmarshaller());
00259         }
00260 
00261         public void startContextElement(String name, Attributes attributes)
00262             throws org.xml.sax.SAXException {
00263             policy = new PolicyRule();
00264         }
00265 
00266         /* (non-Javadoc)
00267          * @see org.objectweb.proactive.core.xml.handler.AbstractUnmarshallerDecorator#notifyEndActiveHandler(java.lang.String, org.objectweb.proactive.core.xml.handler.UnmarshallerHandler)
00268          */
00269         protected void notifyEndActiveHandler(String name,
00270             UnmarshallerHandler activeHandler) throws SAXException {
00271             if (name.equals(ENTITY_FROM_TAG)) {
00272                 policy.setEntitiesFrom((ArrayList) activeHandler.getResultObject());
00273             } else if (name.equals(ENTITY_TO_TAG)) {
00274                 policy.setEntitiesTo((ArrayList) activeHandler.getResultObject());
00275             } else if (name.equals(RULE_COMMUNICATION_TAG)) {
00276                 policy.setCommunicationRules((Communication[]) activeHandler.getResultObject());
00277             } else if (name.equals(RULE_COMMUNICATION_AOCREATION_TAG)) {
00278                 String value = (String) activeHandler.getResultObject();
00279                 boolean b;
00280                 if (value.equals(RULE_AOCREATION_AUTHORIZED)) {
00281                     b = true;
00282                 } else {
00283                     b = false;
00284                 }
00285                 policy.setAocreation(b);
00286             } else if (name.equals(RULE_COMMUNICATION_MIGRATION_TAG)) {
00287                 String value = (String) activeHandler.getResultObject();
00288                 boolean b;
00289                 if (value.equals(RULE_MIGRATION_AUTHORIZED)) {
00290                     b = true;
00291                 } else {
00292                     b = false;
00293                 }
00294                 policy.setMigration(b);
00295             }
00296         }
00297 
00298         /* (non-Javadoc)
00299          * @see org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#getResultObject()
00300          */
00301         public Object getResultObject() throws SAXException {
00302             return policy;
00303         }
00304     }
00305 
00306     // end inner class RulesHandler
00307     private class EntityCollector extends AbstractUnmarshallerDecorator {
00308         private ArrayList entities;
00309 
00310         public EntityCollector() {
00311             entities = new ArrayList();
00312             addHandler(ENTITY_TAG, new EntityHandler());
00313         }
00314 
00315         /* (non-Javadoc)
00316          * @see org.objectweb.proactive.core.xml.handler.AbstractUnmarshallerDecorator#notifyEndActiveHandler(java.lang.String, org.objectweb.proactive.core.xml.handler.UnmarshallerHandler)
00317          */
00318         protected void notifyEndActiveHandler(String name,
00319             UnmarshallerHandler activeHandler) throws SAXException {
00320             entities.add(activeHandler.getResultObject());
00321         }
00322 
00323         /* (non-Javadoc)
00324          * @see org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#getResultObject()
00325          */
00326         public Object getResultObject() throws SAXException {
00327             return entities;
00328         }
00329 
00330         /* (non-Javadoc)
00331          * @see org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#startContextElement(java.lang.String, org.objectweb.proactive.core.xml.io.Attributes)
00332          */
00333         public void startContextElement(String name, Attributes attributes)
00334             throws SAXException {
00335         }
00336     }
00337 
00341     private class EntityHandler extends BasicUnmarshaller {
00342         private Entity entity;
00343 
00344         public EntityHandler() {
00345             super();
00346         }
00347 
00348         public void startContextElement(String name, Attributes attributes)
00349             throws org.xml.sax.SAXException {
00350             if (attributes.getValue("type").equals("VN")) {
00351                 entity = new EntityVirtualNode(attributes.getValue("name"),
00352                         policyServer.getApplicationCertificate(), null);
00353             } else if (attributes.getValue("type").equals("DefaultVirtualNode")) {
00354                 entity = new DefaultEntity();
00355             }
00356         }
00357 
00358         /* (non-Javadoc)
00359          * @see org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#getResultObject()
00360          */
00361         public Object getResultObject() throws SAXException {
00362             return entity;
00363         }
00364     }
00365 
00366     // end inner class EntityHandler
00367     private class CommunicationCollectionHandler
00368         extends AbstractUnmarshallerDecorator {
00369         private Communication[] communication;
00370 
00371         public CommunicationCollectionHandler() {
00372             super();
00373             communication = new Communication[2];
00374             addHandler(RULE_COMMUNICATION_FROM_TAG, new CommunicationHandler());
00375             addHandler(RULE_COMMUNICATION_TO_TAG, new CommunicationHandler());
00376         }
00377 
00378         /* (non-Javadoc)
00379          * @see org.objectweb.proactive.core.xml.handler.AbstractUnmarshallerDecorator#notifyEndActiveHandler(java.lang.String, org.objectweb.proactive.core.xml.handler.UnmarshallerHandler)
00380          */
00381         protected void notifyEndActiveHandler(String name,
00382             UnmarshallerHandler activeHandler) throws SAXException {
00383             if (name.equals(RULE_COMMUNICATION_FROM_TAG)) {
00384                 communication[0] = (Communication) activeHandler.getResultObject();
00385                 //                System.out.println("TAG FROM !!!!" + communication[0]);
00386             } else if (name.equals(RULE_COMMUNICATION_TO_TAG)) {
00387                 communication[1] = (Communication) activeHandler.getResultObject();
00388             }
00389         }
00390 
00391         /* (non-Javadoc)
00392          * @see org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#getResultObject()
00393          */
00394         public Object getResultObject() throws SAXException {
00395             return communication;
00396         }
00397 
00398         /* (non-Javadoc)
00399          * @see org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#startContextElement(java.lang.String, org.objectweb.proactive.core.xml.io.Attributes)
00400          */
00401         public void startContextElement(String name, Attributes attributes)
00402             throws SAXException {
00403         }
00404     }
00405 
00409     private class CommunicationHandler extends AbstractUnmarshallerDecorator {
00410         private Communication communication;
00411 
00412         public CommunicationHandler() {
00413             super();
00414 
00415             addHandler(RULE_COMMUNICATION_ATTRIBUTES_TAG,
00416                 new CommunicationAttributesHandler());
00417         }
00418 
00419         public void startContextElement(String name, Attributes attributes)
00420             throws org.xml.sax.SAXException {
00421         }
00422 
00423         /* (non-Javadoc)
00424          * @see org.objectweb.proactive.core.xml.handler.AbstractUnmarshallerDecorator#notifyEndActiveHandler(java.lang.String, org.objectweb.proactive.core.xml.handler.UnmarshallerHandler)
00425          */
00426         protected void notifyEndActiveHandler(String name,
00427             UnmarshallerHandler activeHandler) throws SAXException {
00428             if (name.equals(RULE_COMMUNICATION_ATTRIBUTES_TAG)) {
00429                 communication = (Communication) activeHandler.getResultObject();
00430             }
00431         }
00432 
00433         /* (non-Javadoc)
00434          * @see org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#getResultObject()
00435          */
00436         public Object getResultObject() throws SAXException {
00437             //logger.info(" communication : "+ communication);
00438             return communication;
00439         }
00440     }
00441 
00445     private class CommunicationAttributesHandler extends BasicUnmarshaller {
00446         private Communication communication;
00447 
00448         public CommunicationAttributesHandler() {
00449             super();
00450         }
00451 
00452         public void startContextElement(String name, Attributes attributes)
00453             throws org.xml.sax.SAXException {
00454             communication = new Communication(convert(attributes.getValue(
00455                             "authentication")),
00456                     convert(attributes.getValue("integrity")),
00457                     convert(attributes.getValue("confidentiality")));
00458         }
00459 
00460         /* (non-Javadoc)
00461          * @see org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#getResultObject()
00462          */
00463         public Object getResultObject() throws SAXException {
00464             return communication;
00465         }
00466     }
00467 
00468     // end inner class CommunicationHandler
00469 
00474     public static PolicyServer createPolicyServer(String xmlDescriptorUrl)
00475         throws InvalidPolicyFile {
00476         //static method added to replace main method
00477         String uri = null;
00478         try {
00479             InitialHandler h = new InitialHandler();
00480 
00481             // ProActiveSecurityDescriptorHandler h = new ProActiveSecurityDescriptorHandler();
00482             uri = xmlDescriptorUrl;
00483             org.objectweb.proactive.core.xml.io.StreamReader sr = new org.objectweb.proactive.core.xml.io.StreamReader(new org.xml.sax.InputSource(
00484                         uri), h);
00485             sr.read();
00486             ((PolicyServer) h.getResultObject()).setPolicyRulesFileLocation(uri);
00487 
00488             return (PolicyServer) h.getResultObject();
00489         } catch (Exception e) {
00490             e.printStackTrace();
00491             ProActiveLogger.getLogger(Loggers.SECURITY)
00492                            .warn("a problem occurs when getting the security part of the ProActiveDescriptorHandler at location \""+uri+"\".");
00493             throw new InvalidPolicyFile(e);
00494         }
00495     }
00496 
00497     private int convert(String name) {
00498         if (name == null) {
00499             return Communication.OPTIONAL;
00500         }
00501         if (name.equals("required") || name.equals("allowed") ||
00502                 name.equals("authorized")) {
00503             return Communication.REQUIRED;
00504         } else if (name.equals("denied")) {
00505             return Communication.DENIED;
00506         } else {
00507             return Communication.OPTIONAL;
00508         }
00509     }
00510 
00511     public static void main(String[] args) throws IOException, org.xml.sax.SAXException {
00512         InitialHandler h = new InitialHandler();
00513 
00514         // ProActiveSecurityDescriptorHandler h = new ProActiveSecurityDescriptorHandler();
00515         String uri = "/net/home/acontes/dev/ProActive/descriptors/scurrav2.xml";
00516         org.objectweb.proactive.core.xml.io.StreamReader sr = new org.objectweb.proactive.core.xml.io.StreamReader(new org.xml.sax.InputSource(
00517                     args[0]), h);
00518         sr.read();
00519     }
00520     
00521     
00522 
00523 }

Generated on Mon Jan 22 15:16:11 2007 for ProActive by  doxygen 1.5.1