2 * Copyright 2001-2007 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 * Overall policy used to verify the security of an incoming message.
24 #include "exceptions.h"
25 #include "binding/SecurityPolicyRule.h"
26 #include "saml2/core/Assertions.h"
28 using namespace opensaml::saml2md;
29 using namespace opensaml::saml2;
30 using namespace opensaml;
31 using namespace xmltooling;
35 SAML_DLLLOCAL PluginManager<SecurityPolicyRule,string,const DOMElement*>::Factory ClientCertAuthRuleFactory;
36 SAML_DLLLOCAL PluginManager<SecurityPolicyRule,string,const DOMElement*>::Factory MessageFlowRuleFactory;
37 SAML_DLLLOCAL PluginManager<SecurityPolicyRule,string,const DOMElement*>::Factory NullSecurityRuleFactory;
38 SAML_DLLLOCAL PluginManager<SecurityPolicyRule,string,const DOMElement*>::Factory SimpleSigningRuleFactory;
39 SAML_DLLLOCAL PluginManager<SecurityPolicyRule,string,const DOMElement*>::Factory XMLSigningRuleFactory;
42 void SAML_API opensaml::registerSecurityPolicyRules()
44 SAMLConfig& conf=SAMLConfig::getConfig();
45 conf.SecurityPolicyRuleManager.registerFactory(CLIENTCERTAUTH_POLICY_RULE, ClientCertAuthRuleFactory);
46 conf.SecurityPolicyRuleManager.registerFactory(MESSAGEFLOW_POLICY_RULE, MessageFlowRuleFactory);
47 conf.SecurityPolicyRuleManager.registerFactory(NULLSECURITY_POLICY_RULE, NullSecurityRuleFactory);
48 conf.SecurityPolicyRuleManager.registerFactory(SIMPLESIGNING_POLICY_RULE, SimpleSigningRuleFactory);
49 conf.SecurityPolicyRuleManager.registerFactory(XMLSIGNING_POLICY_RULE, XMLSigningRuleFactory);
52 SecurityPolicy::IssuerMatchingPolicy SecurityPolicy::m_defaultMatching;
54 SecurityPolicy::~SecurityPolicy()
59 void SecurityPolicy::reset(bool messageOnly)
61 XMLString::release(&m_messageID);
68 m_authenticated=false;
72 void SecurityPolicy::evaluate(const XMLObject& message, const GenericRequest* request)
74 for (vector<const SecurityPolicyRule*>::const_iterator i=m_rules.begin(); i!=m_rules.end(); ++i)
75 (*i)->evaluate(message,request,*this);
78 void SecurityPolicy::setIssuer(const Issuer* issuer)
80 if (!getIssuerMatchingPolicy().issuerMatches(m_issuer, issuer))
81 throw SecurityPolicyException("A rule supplied an Issuer that conflicts with previous results.");
85 m_issuer=issuer->cloneIssuer();
89 void SecurityPolicy::setIssuer(const XMLCh* issuer)
91 if (!getIssuerMatchingPolicy().issuerMatches(m_issuer, issuer))
92 throw SecurityPolicyException("A rule supplied an Issuer that conflicts with previous results.");
94 if (!m_issuer && issuer && *issuer) {
96 m_issuer = IssuerBuilder::buildIssuer();
97 m_issuer->setName(issuer);
101 void SecurityPolicy::setIssuerMetadata(const RoleDescriptor* issuerRole)
103 if (issuerRole && m_issuerRole && issuerRole!=m_issuerRole)
104 throw SecurityPolicyException("A rule supplied a RoleDescriptor that conflicts with previous results.");
105 m_issuerRole=issuerRole;
108 bool SecurityPolicy::IssuerMatchingPolicy::issuerMatches(const Issuer* issuer1, const Issuer* issuer2) const
110 // NULL matches anything for the purposes of this interface.
111 if (!issuer1 || !issuer2)
114 const XMLCh* op1=issuer1->getName();
115 const XMLCh* op2=issuer2->getName();
116 if (!op1 || !op2 || !XMLString::equals(op1,op2))
119 op1=issuer1->getFormat();
120 op2=issuer2->getFormat();
121 if (!XMLString::equals(op1 ? op1 : NameIDType::ENTITY, op2 ? op2 : NameIDType::ENTITY))
124 op1=issuer1->getNameQualifier();
125 op2=issuer2->getNameQualifier();
126 if (!XMLString::equals(op1 ? op1 : &chNull, op2 ? op2 : &chNull))
129 op1=issuer1->getSPNameQualifier();
130 op2=issuer2->getSPNameQualifier();
131 if (!XMLString::equals(op1 ? op1 : &chNull, op2 ? op2 : &chNull))
137 bool SecurityPolicy::IssuerMatchingPolicy::issuerMatches(const Issuer* issuer1, const XMLCh* issuer2) const
139 // NULL matches anything for the purposes of this interface.
140 if (!issuer1 || !issuer2 || !*issuer2)
143 const XMLCh* op1=issuer1->getName();
144 if (!op1 || !XMLString::equals(op1,issuer2))
147 op1=issuer1->getFormat();
148 if (op1 && *op1 && !XMLString::equals(op1, NameIDType::ENTITY))
151 op1=issuer1->getNameQualifier();
155 op1=issuer1->getSPNameQualifier();