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 SAML1MessageRuleFactory;
38 SAML_DLLLOCAL PluginManager<SecurityPolicyRule,string,const DOMElement*>::Factory SAML2MessageRuleFactory;
39 SAML_DLLLOCAL PluginManager<SecurityPolicyRule,string,const DOMElement*>::Factory SimpleSigningRuleFactory;
40 SAML_DLLLOCAL PluginManager<SecurityPolicyRule,string,const DOMElement*>::Factory XMLSigningRuleFactory;
43 void SAML_API opensaml::registerSecurityPolicyRules()
45 SAMLConfig& conf=SAMLConfig::getConfig();
46 conf.SecurityPolicyRuleManager.registerFactory(CLIENTCERTAUTH_POLICY_RULE, ClientCertAuthRuleFactory);
47 conf.SecurityPolicyRuleManager.registerFactory(MESSAGEFLOW_POLICY_RULE, MessageFlowRuleFactory);
48 conf.SecurityPolicyRuleManager.registerFactory(SAML1MESSAGE_POLICY_RULE, SAML1MessageRuleFactory);
49 conf.SecurityPolicyRuleManager.registerFactory(SAML2MESSAGE_POLICY_RULE, SAML2MessageRuleFactory);
50 conf.SecurityPolicyRuleManager.registerFactory(SIMPLESIGNING_POLICY_RULE, SimpleSigningRuleFactory);
51 conf.SecurityPolicyRuleManager.registerFactory(XMLSIGNING_POLICY_RULE, XMLSigningRuleFactory);
54 SecurityPolicy::IssuerMatchingPolicy SecurityPolicy::m_defaultMatching;
56 SecurityPolicy::~SecurityPolicy()
61 void SecurityPolicy::reset()
63 delete m_messageQName;
64 XMLString::release(&m_messageID);
74 void SecurityPolicy::evaluate(const XMLObject& message, const GenericRequest* request)
76 for (vector<const SecurityPolicyRule*>::const_iterator i=m_rules.begin(); i!=m_rules.end(); ++i)
77 (*i)->evaluate(message,request,*this);
80 void SecurityPolicy::setIssuer(const Issuer* issuer)
82 if (!getIssuerMatchingPolicy().issuerMatches(m_issuer, issuer))
83 throw SecurityPolicyException("A rule supplied an Issuer that conflicts with previous results.");
87 m_issuer=issuer->cloneIssuer();
91 void SecurityPolicy::setIssuer(const XMLCh* issuer)
93 if (!getIssuerMatchingPolicy().issuerMatches(m_issuer, issuer))
94 throw SecurityPolicyException("A rule supplied an Issuer that conflicts with previous results.");
96 if (!m_issuer && issuer && *issuer) {
98 m_issuer = IssuerBuilder::buildIssuer();
99 m_issuer->setName(issuer);
103 void SecurityPolicy::setIssuerMetadata(const RoleDescriptor* issuerRole)
105 if (issuerRole && m_issuerRole && issuerRole!=m_issuerRole)
106 throw SecurityPolicyException("A rule supplied a RoleDescriptor that conflicts with previous results.");
107 m_issuerRole=issuerRole;
110 bool SecurityPolicy::IssuerMatchingPolicy::issuerMatches(const Issuer* issuer1, const Issuer* issuer2) const
112 // NULL matches anything for the purposes of this interface.
113 if (!issuer1 || !issuer2)
116 const XMLCh* op1=issuer1->getName();
117 const XMLCh* op2=issuer2->getName();
118 if (!op1 || !op2 || !XMLString::equals(op1,op2))
121 op1=issuer1->getFormat();
122 op2=issuer2->getFormat();
123 if (!XMLString::equals(op1 ? op1 : NameIDType::ENTITY, op2 ? op2 : NameIDType::ENTITY))
126 op1=issuer1->getNameQualifier();
127 op2=issuer2->getNameQualifier();
128 if (!XMLString::equals(op1 ? op1 : &chNull, op2 ? op2 : &chNull))
131 op1=issuer1->getSPNameQualifier();
132 op2=issuer2->getSPNameQualifier();
133 if (!XMLString::equals(op1 ? op1 : &chNull, op2 ? op2 : &chNull))
139 bool SecurityPolicy::IssuerMatchingPolicy::issuerMatches(const Issuer* issuer1, const XMLCh* issuer2) const
141 // NULL matches anything for the purposes of this interface.
142 if (!issuer1 || !issuer2 || !*issuer2)
145 const XMLCh* op1=issuer1->getName();
146 if (!op1 || !XMLString::equals(op1,issuer2))
149 op1=issuer1->getFormat();
150 if (op1 && *op1 && !XMLString::equals(op1, NameIDType::ENTITY))
153 op1=issuer1->getNameQualifier();
157 op1=issuer1->getSPNameQualifier();