Add policy rules for SAML 1 SSO and SAML 2 Bearer confirmation, with unit tests.
[shibboleth/cpp-opensaml.git] / saml / binding / SecurityPolicy.h
index 1870595..6a5aac7 100644 (file)
@@ -1,6 +1,6 @@
 /*
- *  Copyright 2001-2007 Internet2
- * 
+ *  Copyright 2001-2009 Internet2
+ *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
 
 /**
  * @file saml/binding/SecurityPolicy.h
- * 
+ *
  * Overall policy used to verify the security of an incoming message.
  */
 
 #ifndef __saml_secpol_h__
 #define __saml_secpol_h__
 
-#include <saml/binding/GenericRequest.h>
+#include <saml/saml2/metadata/MetadataProvider.h>
 
 #include <ctime>
 #include <vector>
-#include <xmltooling/XMLObject.h>
+#include <xmltooling/io/GenericRequest.h>
 #include <xmltooling/security/TrustEngine.h>
 
 #if defined (_MSC_VER)
@@ -40,22 +40,18 @@ namespace opensaml {
     namespace saml2 {
         class SAML_API Issuer;
     };
-    namespace saml2md {
-        class SAML_API MetadataProvider;
-        class SAML_API RoleDescriptor;
-    };
-    
+
     class SAML_API SecurityPolicyRule;
-    
+
     /**
      * A policy used to verify the security of an incoming message.
-     * 
+     *
      * <p>Its security mechanisms may be used to examine the transport layer
      * (e.g client certificates and HTTP basic auth passwords) or to check the
      * payload of a request to ensure it meets certain criteria (e.g. valid
      * digital signature, freshness, replay).
-     * 
-     * <p>Policy objects can be reused, but are not thread-safe. 
+     *
+     * <p>Policy objects can be reused, but are not thread-safe.
      */
     class SAML_API SecurityPolicy
     {
@@ -63,46 +59,24 @@ namespace opensaml {
     public:
         /**
          * Constructor for policy.
-         * 
-         * @param metadataProvider  locked MetadataProvider instance
-         * @param role              identifies the role (generally IdP or SP) of the policy peer 
-         * @param trustEngine       TrustEngine to authenticate policy peer
-         */
-        SecurityPolicy(
-            const saml2md::MetadataProvider* metadataProvider=NULL,
-            const xmltooling::QName* role=NULL,
-            const xmltooling::TrustEngine* trustEngine=NULL
-            ) : m_messageQName(NULL), m_messageID(NULL), m_issueInstant(0), m_issuer(NULL), m_issuerRole(NULL), m_secure(false),
-                m_matchingPolicy(NULL), m_metadata(metadataProvider), m_role(NULL), m_trust(trustEngine) {
-            if (role)
-                m_role = new xmltooling::QName(*role);
-        }
-
-        /**
-         * Constructor for policy using existing rules. The lifetime of the policy rules
-         * must be at least as long as the policy object.
          *
-         * @param rules             reference to array of policy rules to use 
          * @param metadataProvider  locked MetadataProvider instance
-         * @param role              identifies the role (generally IdP or SP) of the policy peer 
+         * @param role              identifies the role (generally IdP or SP) of the policy peer
          * @param trustEngine       TrustEngine to authenticate policy peer
+         * @param validate          true iff XML parsing should be done with validation
          */
         SecurityPolicy(
-            const std::vector<const SecurityPolicyRule*>& rules,
             const saml2md::MetadataProvider* metadataProvider=NULL,
             const xmltooling::QName* role=NULL,
-            const xmltooling::TrustEngine* trustEngine=NULL
-            ) : m_messageQName(NULL), m_messageID(NULL), m_issueInstant(0), m_issuer(NULL), m_issuerRole(NULL), m_secure(false),
-                m_matchingPolicy(NULL), m_rules(rules), m_metadata(metadataProvider), m_role(NULL), m_trust(trustEngine) {
-            if (role)
-                m_role = new xmltooling::QName(*role);
-        }
+            const xmltooling::TrustEngine* trustEngine=NULL,
+            bool validate=true
+            );
 
         virtual ~SecurityPolicy();
 
         /**
          * Returns the locked MetadataProvider supplied to the policy.
-         * 
+         *
          * @return the supplied MetadataProvider or NULL
          */
         const saml2md::MetadataProvider* getMetadataProvider() const {
@@ -110,8 +84,19 @@ namespace opensaml {
         }
 
         /**
+         * Returns a reference to a MetadataProvider::Criteria instance suitable for use with the
+         * installed MetadataProvider.
+         *
+         * <p>The object will be cleared/reset when returned, so do not mutate it and then
+         * call the method again before using it.
+         *
+         * @return reference to a MetadataProvider::Criteria instance
+         */
+        virtual saml2md::MetadataProvider::Criteria& getMetadataProviderCriteria() const;
+
+        /**
          * Returns the peer role element/type supplied to the policy.
-         * 
+         *
          * @return the peer role element/type, or an empty QName
          */
         const xmltooling::QName* getRole() const {
@@ -120,7 +105,7 @@ namespace opensaml {
 
         /**
          * Returns the TrustEngine supplied to the policy.
-         * 
+         *
          * @return the supplied TrustEngine or NULL
          */
         const xmltooling::TrustEngine* getTrustEngine() const {
@@ -128,18 +113,76 @@ namespace opensaml {
         }
 
         /**
-         * Adds a SecurityPolicyRule to the policy. The lifetime of the policy rule
-         * must be at least as long as the policy object.
-         * 
-         * @param rule  SecurityPolicyRule to add
+         * Returns XML message validation setting.
+         *
+         * @return validation flag
+         */
+        bool getValidating() const {
+            return m_validate;
+        }
+
+        /**
+         * Returns flag controlling non-entity issuer support.
+         *
+         * @return flag controlling non-entity issuer support
          */
-        void addRule(const SecurityPolicyRule* rule) {
-            m_rules.push_back(rule);
+        bool requireEntityIssuer() const {
+            return m_entityOnly;
+        }
+
+        /**
+         * Returns the SAML audiences that represent the receiving peer.
+         *
+         * @return audience values of the peer processing the message
+         */
+        const std::vector<const XMLCh*>& getAudiences() const {
+            return m_audiences;
+        }
+
+        /**
+         * Returns the SAML audiences that represent the receiving peer.
+         *
+         * @return audience values of the peer processing the message
+         */
+        std::vector<const XMLCh*>& getAudiences() {
+            return m_audiences;
+        }
+
+        /**
+         * Gets the effective time of message processing.
+         *
+         * @return  the time at which the message is being processed
+         */
+        time_t getTime() const {
+            if (m_ts == 0)
+                return m_ts = time(NULL);
+            return m_ts;
+        }
+
+        /**
+         * Returns the message identifier to which the message being evaluated
+         * is a response.
+         *
+         * @return correlated message identifier
+         */
+        const XMLCh* getCorrelationID() const {
+            return m_correlationID;
+        }
+
+        /**
+         * Gets a mutable array of installed policy rules.
+         *
+         * <p>If adding rules, their lifetime must be at least as long as the policy object.
+         *
+         * @return  mutable array of rules
+         */
+        std::vector<const SecurityPolicyRule*>& getRules() {
+            return m_rules;
         }
 
         /**
          * Sets a locked MetadataProvider for the policy.
-         * 
+         *
          * @param metadata a locked MetadataProvider or NULL
          */
         void setMetadataProvider(const saml2md::MetadataProvider* metadata) {
@@ -147,8 +190,19 @@ namespace opensaml {
         }
 
         /**
+         * Sets a MetadataProvider::Criteria instance suitable for use with the
+         * installed MetadataProvider.
+         *
+         * <p>The policy will take ownership of the criteria object when this
+         * method completes.
+         *
+         * @param criteria a MetadataProvider::Criteria instance, or NULL
+         */
+        void setMetadataProviderCriteria(saml2md::MetadataProvider::Criteria* criteria);
+
+        /**
          * Sets a peer role element/type for to the policy.
-         * 
+         *
          * @param role the peer role element/type or NULL
          */
         void setRole(const xmltooling::QName* role) {
@@ -158,7 +212,7 @@ namespace opensaml {
 
         /**
          * Sets a TrustEngine for the policy.
-         * 
+         *
          * @param trust a TrustEngine or NULL
          */
         void setTrustEngine(const xmltooling::TrustEngine* trust) {
@@ -166,33 +220,83 @@ namespace opensaml {
         }
 
         /**
+         * Controls schema validation of incoming XML messages.
+         * This is separate from other forms of programmatic validation of objects,
+         * but can detect a much wider range of syntax errors.
+         *
+         * @param validate  validation setting
+         */
+        void setValidating(bool validate=true) {
+            m_validate = validate;
+        }
+
+        /**
+         * Sets flag controlling non-entity issuer support.
+         *
+         * @param entityOnly require that Issuer be in entity format
+         */
+        void requireEntityIssuer(bool entityOnly=true) {
+            m_entityOnly = entityOnly;
+        }
+
+        /**
+         * Sets effective time of message processing.
+         *
+         * <p>Assumed to be the time of policy instantiation, can be adjusted to pre- or post-date
+         * message processing.
+         *
+         * @param ts    the time at which the message is being processed
+         */
+        void setTime(time_t ts) {
+            m_ts = ts;
+        }
+
+        /**
+         * Sets the message identifier to which the message being evaluated
+         * is a response.
+         *
+         * @param correlationID correlated message identifier
+         */
+        void setCorrelationID(const XMLCh* correlationID) {
+            m_correlationID = correlationID;
+        }
+
+        /**
          * Evaluates the policy against the given request and message,
          * possibly populating message information in the policy object.
-         * 
+         *
          * @param message           the incoming message
          * @param request           the protocol request
          *
          * @throws BindingException raised if the message/request is invalid according to the supplied rules
          */
-        void evaluate(const xmltooling::XMLObject& message, const GenericRequest* request=NULL);
+        void evaluate(
+            const xmltooling::XMLObject& message, const xmltooling::GenericRequest* request=NULL
+            );
 
         /**
-         * Resets the policy object and clears any per-message state.
+         * Resets the policy object and/or clears any per-message state.
+         *
+         * <p>Resets can be complete (the default) or merely clear the previous message ID and timestamp
+         * when evaluating multiple layers of a message.
+         *
+         * @param messageOnly   true iff security and issuer state should be left in place
          */
-        void reset();
-        
+        virtual void reset(bool messageOnly=false);
+
         /**
-         * Returns the message element/type as determined by the registered policies.
-         * 
-         * @return message element/type as determined by the registered policies
+         * Resets the policy object and/or clears any per-message state for only this specific class.
+         *
+         * <p>Resets can be complete (the default) or merely clear the previous message ID and timestamp
+         * when evaluating multiple layers of a message.
+         *
+         * @param messageOnly   true iff security and issuer state should be left in place
          */
-        const xmltooling::QName* getMessageQName() const {
-            return m_messageQName;
-        }
+        void _reset(bool messageOnly=false);
 
         /**
          * Returns the message identifier as determined by the registered policies.
-         * 
+         *
          * @return message identifier as determined by the registered policies
          */
         const XMLCh* getMessageID() const {
@@ -201,7 +305,7 @@ namespace opensaml {
 
         /**
          * Returns the message timestamp as determined by the registered policies.
-         * 
+         *
          * @return message timestamp as determined by the registered policies
          */
         time_t getIssueInstant() const {
@@ -210,7 +314,7 @@ namespace opensaml {
 
         /**
          * Gets the issuer of the message as determined by the registered policies.
-         * 
+         *
          * @return issuer of the message as determined by the registered policies
          */
         const saml2::Issuer* getIssuer() const {
@@ -219,7 +323,7 @@ namespace opensaml {
 
         /**
          * Gets the metadata for the role the issuer is operating in.
-         * 
+         *
          * @return metadata for the role the issuer is operating in
          */
         const saml2md::RoleDescriptor* getIssuerMetadata() const {
@@ -227,37 +331,27 @@ namespace opensaml {
         }
 
         /**
-         * Returns the security status as determined by the registered policies.
-         * 
-         * @return true iff a SecurityPolicyRule has indicated the issuer/message has been authenticated 
-         */
-        bool isSecure() const {
-            return m_secure;
-        }
-
-        /**
-         * Sets the message element/type as determined by the registered policies.
-         * 
-         * @param messageQName message element/type
+         * Returns the authentication status of the message as determined by the registered policies.
+         *
+         * @return true iff a SecurityPolicyRule has indicated the issuer/message has been authenticated
          */
-        void setMessageQName(const xmltooling::QName* messageQName) {
-            delete m_messageQName;
-            m_messageQName = messageQName ? new xmltooling::QName(*messageQName) : NULL;
+        bool isAuthenticated() const {
+            return m_authenticated;
         }
 
         /**
          * Sets the message identifier as determined by the registered policies.
-         * 
+         *
          * @param id message identifier
          */
         void setMessageID(const XMLCh* id) {
-            XMLString::release(&m_messageID);
-            m_messageID = XMLString::replicate(id);
+            xercesc::XMLString::release(&m_messageID);
+            m_messageID = xercesc::XMLString::replicate(id);
         }
 
         /**
          * Sets the message timestamp as determined by the registered policies.
-         * 
+         *
          * @param issueInstant message timestamp
          */
         void setIssueInstant(time_t issueInstant) {
@@ -266,52 +360,71 @@ namespace opensaml {
 
         /**
          * Sets the issuer of the message as determined by the registered policies.
-         * The policy object takes ownership of the Issuer object.
-         * 
+         *
+         * @param issuer issuer of the message
+         */
+        void setIssuer(const saml2::Issuer* issuer);
+
+        /**
+         * Sets the issuer of the message as determined by the registered policies.
+         *
          * @param issuer issuer of the message
          */
-        void setIssuer(saml2::Issuer* issuer);
-        
+        void setIssuer(const XMLCh* issuer);
+
         /**
          * Sets the metadata for the role the issuer is operating in.
-         * 
+         *
          * @param issuerRole metadata for the role the issuer is operating in
          */
         void setIssuerMetadata(const saml2md::RoleDescriptor* issuerRole);
 
         /**
-         * Sets the security status as determined by the registered policies.
-         * 
-         * @param secure indicates whether the issuer/message has been authenticated
+         * Sets the authentication status of the message as determined by the registered policies.
+         *
+         * @param auth indicates whether the issuer/message has been authenticated
          */
-        void setSecure(bool secure) {
-            m_secure = secure;
+        void setAuthenticated(bool auth) {
+            m_authenticated = auth;
         }
-        
+
         /** Allows override of rules for comparing saml2:Issuer information. */
         class SAML_API IssuerMatchingPolicy {
             MAKE_NONCOPYABLE(IssuerMatchingPolicy);
         public:
             IssuerMatchingPolicy() {}
             virtual ~IssuerMatchingPolicy() {}
-            
+
             /**
              * Returns true iff the two operands "match". Applications can override this method to
-             * support non-standard issuer matching for complex policies. 
-             * 
+             * support non-standard issuer matching for complex policies.
+             *
              * <p>The default implementation does a basic comparison of the XML content, treating
              * an unsupplied Format as an "entityID".
-             * 
+             *
              * @param issuer1   the first Issuer to match
              * @param issuer2   the second Issuer to match
              * @return  true iff the operands match
              */
             virtual bool issuerMatches(const saml2::Issuer* issuer1, const saml2::Issuer* issuer2) const;
+
+            /**
+             * Returns true iff the two operands "match". Applications can override this method to
+             * support non-standard issuer matching for complex policies.
+             *
+             * <p>The default implementation does a basic comparison of the XML content, treating
+             * an unsupplied Format as an "entityID".
+             *
+             * @param issuer1   the first Issuer to match
+             * @param issuer2   the second Issuer to match
+             * @return  true iff the operands match
+             */
+            virtual bool issuerMatches(const saml2::Issuer* issuer1, const XMLCh* issuer2) const;
         };
 
         /**
          * Returns the IssuerMatchingPolicy in effect.
-         * 
+         *
          * @return the effective IssuerMatchingPolicy
          */
         const IssuerMatchingPolicy& getIssuerMatchingPolicy() const {
@@ -321,9 +434,9 @@ namespace opensaml {
         /**
          * Sets the IssuerMatchingPolicy in effect. Setting no policy will
          * cause the simple, default approach to be used.
-         * 
+         *
          * <p>The matching object will be freed by the SecurityPolicy.
-         * 
+         *
          * @param matchingPolicy the IssuerMatchingPolicy to use
          */
         void setIssuerMatchingPolicy(IssuerMatchingPolicy* matchingPolicy) {
@@ -335,21 +448,30 @@ namespace opensaml {
         /** A shared matching object that just supports the default matching rules. */
         static IssuerMatchingPolicy m_defaultMatching;
 
+        /** Manufactured MetadataProvider::Criteria instance. */
+        mutable saml2md::MetadataProvider::Criteria* m_metadataCriteria;
+
     private:
-        // information extracted from message 
-        xmltooling::QName* m_messageQName;
+        // information extracted from message
         XMLCh* m_messageID;
         time_t m_issueInstant;
         saml2::Issuer* m_issuer;
         const saml2md::RoleDescriptor* m_issuerRole;
-        bool m_secure;
-        
+        bool m_authenticated;
+
         // components governing policy rules
         IssuerMatchingPolicy* m_matchingPolicy;
         std::vector<const SecurityPolicyRule*> m_rules;
         const saml2md::MetadataProvider* m_metadata;
         xmltooling::QName* m_role;
         const xmltooling::TrustEngine* m_trust;
+        bool m_validate;
+        bool m_entityOnly;
+
+        // contextual information
+        mutable time_t m_ts;
+        const XMLCh* m_correlationID;
+        std::vector<const XMLCh*> m_audiences;
     };
 
 };