Change license header, remove stale pkg files.
[shibboleth/cpp-opensaml.git] / saml / binding / impl / SecurityPolicy.cpp
index 02812a4..ba31f94 100644 (file)
@@ -1,17 +1,21 @@
-/*
- *  Copyright 2001-2007 Internet2
+/**
+ * Licensed to the University Corporation for Advanced Internet
+ * Development, Inc. (UCAID) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for
+ * additional information regarding copyright ownership.
  *
- * 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
+ * UCAID licenses this file to you 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
  *
- *     http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
  *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific
+ * language governing permissions and limitations under the License.
  */
 
 /**
 
 #include "internal.h"
 #include "exceptions.h"
+#include "binding/SecurityPolicy.h"
 #include "binding/SecurityPolicyRule.h"
 #include "saml2/core/Assertions.h"
 
+#include <xercesc/util/XMLUniDefs.hpp>
+
 using namespace opensaml::saml2md;
 using namespace opensaml::saml2;
 using namespace opensaml;
@@ -32,58 +39,243 @@ using namespace xmltooling;
 using namespace std;
 
 namespace opensaml {
+    SAML_DLLLOCAL PluginManager<SecurityPolicyRule,string,const DOMElement*>::Factory AudienceRestrictionRuleFactory;
     SAML_DLLLOCAL PluginManager<SecurityPolicyRule,string,const DOMElement*>::Factory ClientCertAuthRuleFactory;
+    SAML_DLLLOCAL PluginManager<SecurityPolicyRule,string,const DOMElement*>::Factory ConditionsRuleFactory;
+    SAML_DLLLOCAL PluginManager<SecurityPolicyRule,string,const DOMElement*>::Factory IgnoreRuleFactory;
     SAML_DLLLOCAL PluginManager<SecurityPolicyRule,string,const DOMElement*>::Factory MessageFlowRuleFactory;
     SAML_DLLLOCAL PluginManager<SecurityPolicyRule,string,const DOMElement*>::Factory NullSecurityRuleFactory;
     SAML_DLLLOCAL PluginManager<SecurityPolicyRule,string,const DOMElement*>::Factory SimpleSigningRuleFactory;
     SAML_DLLLOCAL PluginManager<SecurityPolicyRule,string,const DOMElement*>::Factory XMLSigningRuleFactory;
+
+    namespace saml1 {
+        SAML_DLLLOCAL PluginManager<SecurityPolicyRule,string,const DOMElement*>::Factory BrowserSSORuleFactory;
+    }
+
+    namespace saml2 {
+        SAML_DLLLOCAL PluginManager<SecurityPolicyRule,string,const DOMElement*>::Factory BearerConfirmationRuleFactory;
+        SAML_DLLLOCAL PluginManager<SecurityPolicyRule,string,const DOMElement*>::Factory DelegationRestrictionRuleFactory;
+    }
 };
 
 void SAML_API opensaml::registerSecurityPolicyRules()
 {
     SAMLConfig& conf=SAMLConfig::getConfig();
+    conf.SecurityPolicyRuleManager.registerFactory(AUDIENCE_POLICY_RULE, AudienceRestrictionRuleFactory);
     conf.SecurityPolicyRuleManager.registerFactory(CLIENTCERTAUTH_POLICY_RULE, ClientCertAuthRuleFactory);
+    conf.SecurityPolicyRuleManager.registerFactory(CONDITIONS_POLICY_RULE, ConditionsRuleFactory);
+    conf.SecurityPolicyRuleManager.registerFactory(IGNORE_POLICY_RULE, IgnoreRuleFactory);
     conf.SecurityPolicyRuleManager.registerFactory(MESSAGEFLOW_POLICY_RULE, MessageFlowRuleFactory);
     conf.SecurityPolicyRuleManager.registerFactory(NULLSECURITY_POLICY_RULE, NullSecurityRuleFactory);
     conf.SecurityPolicyRuleManager.registerFactory(SIMPLESIGNING_POLICY_RULE, SimpleSigningRuleFactory);
     conf.SecurityPolicyRuleManager.registerFactory(XMLSIGNING_POLICY_RULE, XMLSigningRuleFactory);
+    conf.SecurityPolicyRuleManager.registerFactory(SAML1BROWSERSSO_POLICY_RULE, saml1::BrowserSSORuleFactory);
+    conf.SecurityPolicyRuleManager.registerFactory(BEARER_POLICY_RULE, saml2::BearerConfirmationRuleFactory);
+    conf.SecurityPolicyRuleManager.registerFactory(DELEGATION_POLICY_RULE, saml2::DelegationRestrictionRuleFactory);
 }
 
-SecurityPolicy::IssuerMatchingPolicy SecurityPolicy::m_defaultMatching;
+SecurityPolicyRule::SecurityPolicyRule()
+{
+}
 
-SecurityPolicy::~SecurityPolicy()
+SecurityPolicyRule::~SecurityPolicyRule()
 {
-    reset(false);
 }
 
-void SecurityPolicy::reset(bool messageOnly)
+SecurityPolicy::SecurityPolicy(
+    const saml2md::MetadataProvider* metadataProvider,
+    const xmltooling::QName* role,
+    const xmltooling::TrustEngine* trustEngine,
+    bool validate
+    ) : m_metadataCriteria(nullptr),
+        m_issueInstant(0),
+        m_issuer(nullptr),
+        m_issuerRole(nullptr),
+        m_authenticated(false),
+        m_matchingPolicy(nullptr),
+        m_metadata(metadataProvider),
+        m_role(nullptr),
+        m_trust(trustEngine),
+        m_validate(validate),
+        m_entityOnly(true),
+        m_ts(0)
+{
+    if (role)
+        m_role = new xmltooling::QName(*role);
+}
+
+SecurityPolicy::~SecurityPolicy()
 {
-    XMLString::release(&m_messageID);
-    m_messageID=NULL;
-    m_issueInstant=0;
     delete m_metadataCriteria;
-    m_metadataCriteria=NULL;
-    if (!messageOnly) {
-        delete m_issuer;
-        m_issuer=NULL;
-        m_issuerRole=NULL;
-        m_authenticated=false;
-    }
+    delete m_issuer;
+}
+
+const MetadataProvider* SecurityPolicy::getMetadataProvider() const
+{
+    return m_metadata;
 }
 
 MetadataProvider::Criteria& SecurityPolicy::getMetadataProviderCriteria() const
 {
     if (!m_metadataCriteria)
         m_metadataCriteria=new MetadataProvider::Criteria();
+    else
+        m_metadataCriteria->reset();
     return *m_metadataCriteria;
 }
 
+const xmltooling::QName* SecurityPolicy::getRole() const
+{
+    return m_role;
+}
+
+const TrustEngine* SecurityPolicy::getTrustEngine() const
+{
+    return m_trust;
+}
+
+bool SecurityPolicy::getValidating() const
+{
+    return m_validate;
+}
+
+bool SecurityPolicy::requireEntityIssuer() const
+{
+    return m_entityOnly;
+}
+
+const vector<xstring>& SecurityPolicy::getAudiences() const
+{
+    return m_audiences;
+}
+
+vector<xstring>& SecurityPolicy::getAudiences()
+{
+    return m_audiences;
+}
+
+time_t SecurityPolicy::getTime() const
+{
+    if (m_ts == 0)
+        return m_ts = time(nullptr);
+    return m_ts;
+}
+
+const XMLCh* SecurityPolicy::getCorrelationID() const
+{
+    return m_correlationID.c_str();
+}
+
+vector<const SecurityPolicyRule*>& SecurityPolicy::getRules()
+{
+    return m_rules;
+}
+
+void SecurityPolicy::setMetadataProvider(const MetadataProvider* metadata)
+{
+    m_metadata = metadata;
+}
+
+void SecurityPolicy::setMetadataProviderCriteria(MetadataProvider::Criteria* criteria)
+{
+    if (m_metadataCriteria)
+        delete m_metadataCriteria;
+    m_metadataCriteria=criteria;
+}
+
+void SecurityPolicy::setRole(const xmltooling::QName* role)
+{
+    delete m_role;
+    m_role = role ? new xmltooling::QName(*role) : nullptr;
+}
+
+void SecurityPolicy::setTrustEngine(const TrustEngine* trust)
+{
+    m_trust = trust;
+}
+
+void SecurityPolicy::setValidating(bool validate)
+{
+    m_validate = validate;
+}
+
+void SecurityPolicy::requireEntityIssuer(bool entityOnly)
+{
+    m_entityOnly = entityOnly;
+}
+
+void SecurityPolicy::setTime(time_t ts)
+{
+    m_ts = ts;
+}
+
+void SecurityPolicy::setCorrelationID(const XMLCh* correlationID)
+{
+    m_correlationID.erase();
+    if (correlationID)
+        m_correlationID = correlationID;
+}
+
 void SecurityPolicy::evaluate(const XMLObject& message, const GenericRequest* request)
 {
     for (vector<const SecurityPolicyRule*>::const_iterator i=m_rules.begin(); i!=m_rules.end(); ++i)
         (*i)->evaluate(message,request,*this);
 }
 
+void SecurityPolicy::reset(bool messageOnly)
+{
+    _reset(messageOnly);
+}
+
+void SecurityPolicy::_reset(bool messageOnly)
+{
+    m_messageID.erase();
+    m_issueInstant=0;
+    if (!messageOnly) {
+        delete m_issuer;
+        m_issuer=nullptr;
+        m_issuerRole=nullptr;
+        m_authenticated=false;
+    }
+}
+
+const XMLCh* SecurityPolicy::getMessageID() const
+{
+    return m_messageID.c_str();
+}
+
+time_t SecurityPolicy::getIssueInstant() const
+{
+    return m_issueInstant;
+}
+
+const Issuer* SecurityPolicy::getIssuer() const
+{
+    return m_issuer;
+}
+
+const RoleDescriptor* SecurityPolicy::getIssuerMetadata() const
+{
+    return m_issuerRole;
+}
+
+bool SecurityPolicy::isAuthenticated() const
+{
+    return m_authenticated;
+}
+
+void SecurityPolicy::setMessageID(const XMLCh* id)
+{
+    m_messageID.erase();
+    if (id)
+        m_messageID = id;
+}
+
+void SecurityPolicy::setIssueInstant(time_t issueInstant)
+{
+    m_issueInstant = issueInstant;
+}
+
 void SecurityPolicy::setIssuer(const Issuer* issuer)
 {
     if (!getIssuerMatchingPolicy().issuerMatches(m_issuer, issuer))
@@ -92,7 +284,7 @@ void SecurityPolicy::setIssuer(const Issuer* issuer)
     if (!m_issuer) {
         if (m_entityOnly && issuer->getFormat() && !XMLString::equals(issuer->getFormat(), NameIDType::ENTITY))
             throw SecurityPolicyException("A non-entity Issuer was supplied, violating policy.");
-        m_issuerRole = NULL;
+        m_issuerRole = nullptr;
         m_issuer=issuer->cloneIssuer();
     }
 }
@@ -103,7 +295,7 @@ void SecurityPolicy::setIssuer(const XMLCh* issuer)
         throw SecurityPolicyException("An Issuer was supplied that conflicts with previous results.");
 
     if (!m_issuer && issuer && *issuer) {
-        m_issuerRole = NULL;
+        m_issuerRole = nullptr;
         m_issuer = IssuerBuilder::buildIssuer();
         m_issuer->setName(issuer);
     }
@@ -116,9 +308,22 @@ void SecurityPolicy::setIssuerMetadata(const RoleDescriptor* issuerRole)
     m_issuerRole=issuerRole;
 }
 
+void SecurityPolicy::setAuthenticated(bool auth)
+{
+    m_authenticated = auth;
+}
+
+SecurityPolicy::IssuerMatchingPolicy::IssuerMatchingPolicy()
+{
+}
+
+SecurityPolicy::IssuerMatchingPolicy::~IssuerMatchingPolicy()
+{
+}
+
 bool SecurityPolicy::IssuerMatchingPolicy::issuerMatches(const Issuer* issuer1, const Issuer* issuer2) const
 {
-    // NULL matches anything for the purposes of this interface.
+    // nullptr matches anything for the purposes of this interface.
     if (!issuer1 || !issuer2)
         return true;
 
@@ -147,7 +352,7 @@ bool SecurityPolicy::IssuerMatchingPolicy::issuerMatches(const Issuer* issuer1,
 
 bool SecurityPolicy::IssuerMatchingPolicy::issuerMatches(const Issuer* issuer1, const XMLCh* issuer2) const
 {
-    // NULL matches anything for the purposes of this interface.
+    // nullptr matches anything for the purposes of this interface.
     if (!issuer1 || !issuer2 || !*issuer2)
         return true;
 
@@ -169,3 +374,16 @@ bool SecurityPolicy::IssuerMatchingPolicy::issuerMatches(const Issuer* issuer1,
 
     return true;
 }
+
+SecurityPolicy::IssuerMatchingPolicy SecurityPolicy::m_defaultMatching;
+
+const SecurityPolicy::IssuerMatchingPolicy& SecurityPolicy::getIssuerMatchingPolicy() const
+{
+    return m_matchingPolicy ? *m_matchingPolicy : m_defaultMatching;
+}
+
+void SecurityPolicy::setIssuerMatchingPolicy(IssuerMatchingPolicy* matchingPolicy)
+{
+    delete m_matchingPolicy;
+    m_matchingPolicy = matchingPolicy;
+}