00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
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     
00112 
00113 
00114     protected void notifyEndActiveHandler(String name,
00115         UnmarshallerHandler activeHandler) throws SAXException {
00116         
00117         
00118         
00119         
00120         
00121         
00122         
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     
00137 
00138 
00139     public Object getResultObject() throws SAXException {
00140         
00141         
00142         
00143         
00144         
00145         
00146         
00147         
00148         
00149         
00150         
00151         
00152         
00153         
00154         
00155         
00156         return policyServer;
00157         
00158     }
00159 
00160     
00161 
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                 
00185                 
00186             }
00187         }
00188 
00189         protected void notifyEndActiveHandler(String name,
00190             UnmarshallerHandler activeHandler) throws org.xml.sax.SAXException {
00191             
00192             
00193             
00194             if (name.equals(RULE_TAG)) {
00195                 policies.add((PolicyRule) activeHandler.getResultObject());
00196                 
00197             }
00198             addHandler(RULE_TAG, new RuleHandler());
00199         }
00200 
00201         
00202 
00203 
00204         public Object getResultObject() throws SAXException {
00205             return policies;
00206         }
00207     }
00208 
00209     
00210 
00214     private static class InitialHandler extends AbstractUnmarshallerDecorator {
00215         
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; 
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         
00267 
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         
00299 
00300 
00301         public Object getResultObject() throws SAXException {
00302             return policy;
00303         }
00304     }
00305 
00306     
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         
00316 
00317 
00318         protected void notifyEndActiveHandler(String name,
00319             UnmarshallerHandler activeHandler) throws SAXException {
00320             entities.add(activeHandler.getResultObject());
00321         }
00322 
00323         
00324 
00325 
00326         public Object getResultObject() throws SAXException {
00327             return entities;
00328         }
00329 
00330         
00331 
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         
00359 
00360 
00361         public Object getResultObject() throws SAXException {
00362             return entity;
00363         }
00364     }
00365 
00366     
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         
00379 
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                 
00386             } else if (name.equals(RULE_COMMUNICATION_TO_TAG)) {
00387                 communication[1] = (Communication) activeHandler.getResultObject();
00388             }
00389         }
00390 
00391         
00392 
00393 
00394         public Object getResultObject() throws SAXException {
00395             return communication;
00396         }
00397 
00398         
00399 
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         
00424 
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         
00434 
00435 
00436         public Object getResultObject() throws SAXException {
00437             
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         
00461 
00462 
00463         public Object getResultObject() throws SAXException {
00464             return communication;
00465         }
00466     }
00467 
00468     
00469 
00474     public static PolicyServer createPolicyServer(String xmlDescriptorUrl)
00475         throws InvalidPolicyFile {
00476         
00477         String uri = null;
00478         try {
00479             InitialHandler h = new InitialHandler();
00480 
00481             
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         
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 }