2 * Copyright 2001-2006 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/SecurityPolicy.h"
26 #include "saml1/core/Assertions.h"
27 #include "saml1/core/Protocols.h"
28 #include "saml2/core/Assertions.h"
29 #include "saml2/core/Protocols.h"
31 using namespace opensaml::saml2md;
32 using namespace opensaml::saml2;
33 using namespace opensaml;
34 using namespace xmltooling;
38 SAML_DLLLOCAL PluginManager<SecurityPolicyRule,const DOMElement*>::Factory MessageFlowRuleFactory;
39 SAML_DLLLOCAL PluginManager<SecurityPolicyRule,const DOMElement*>::Factory SimpleSigningRuleFactory;
40 SAML_DLLLOCAL PluginManager<SecurityPolicyRule,const DOMElement*>::Factory XMLSigningRuleFactory;
43 void SAML_API opensaml::registerSecurityPolicyRules()
45 SAMLConfig& conf=SAMLConfig::getConfig();
46 conf.SecurityPolicyRuleManager.registerFactory(MESSAGEFLOW_POLICY_RULE, MessageFlowRuleFactory);
47 conf.SecurityPolicyRuleManager.registerFactory(SIMPLESIGNING_POLICY_RULE, SimpleSigningRuleFactory);
48 conf.SecurityPolicyRuleManager.registerFactory(XMLSIGNING_POLICY_RULE, XMLSigningRuleFactory);
51 SecurityPolicy::IssuerMatchingPolicy SecurityPolicy::m_defaultMatching;
53 SecurityPolicyRule::MessageExtractor SecurityPolicy::m_defaultExtractor;
55 SecurityPolicy::~SecurityPolicy()
58 delete m_matchingPolicy;
62 void SecurityPolicy::evaluate(const GenericRequest& request, const XMLObject& message)
64 for (vector<const SecurityPolicyRule*>::const_iterator i=m_rules.begin(); i!=m_rules.end(); ++i) {
67 pair<Issuer*,const RoleDescriptor*> ident =
68 (*i)->evaluate(request,message,m_metadata,&m_role,m_trust,getMessageExtractor());
70 // Make sure returned issuer doesn't conflict.
73 if (!getIssuerMatchingPolicy().issuerMatches(ident.first, m_issuer)) {
75 throw BindingException("Policy rules returned differing Issuers.");
82 if (m_issuerRole && ident.second!=m_issuerRole)
83 throw BindingException("Policy rules returned differing issuer RoleDescriptors.");
84 m_issuerRole=ident.second;
89 void SecurityPolicy::setIssuer(saml2::Issuer* issuer)
91 if (!getIssuerMatchingPolicy().issuerMatches(issuer, m_issuer)) {
93 throw BindingException("Externally provided Issuer conflicts with policy results.");
100 void SecurityPolicy::setIssuerMetadata(const RoleDescriptor* issuerRole)
102 if (issuerRole && m_issuerRole && issuerRole!=m_issuerRole)
103 throw BindingException("Externally provided RoleDescriptor conflicts with policy results.");
104 m_issuerRole=issuerRole;
107 bool SecurityPolicy::IssuerMatchingPolicy::issuerMatches(const Issuer* issuer1, const Issuer* issuer2) const
109 // NULL matches anything for the purposes of this interface.
110 if (!issuer1 || !issuer2)
113 const XMLCh* op1=issuer1->getName();
114 const XMLCh* op2=issuer2->getName();
115 if (!op1 || !op2 || !XMLString::equals(op1,op2))
118 op1=issuer1->getFormat();
119 op2=issuer2->getFormat();
120 if (!XMLString::equals(op1 ? op1 : NameIDType::ENTITY, op2 ? op2 : NameIDType::ENTITY))
123 op1=issuer1->getNameQualifier();
124 op2=issuer2->getNameQualifier();
125 if (!XMLString::equals(op1 ? op1 : &chNull, op2 ? op2 : &chNull))
128 op1=issuer1->getSPNameQualifier();
129 op2=issuer2->getSPNameQualifier();
130 if (!XMLString::equals(op1 ? op1 : &chNull, op2 ? op2 : &chNull))
137 pair<saml2::Issuer*,const XMLCh*> SecurityPolicyRule::MessageExtractor::getIssuerAndProtocol(const XMLObject& message) const
139 // We just let any bad casts throw here.
141 saml2::Issuer* issuer;
143 // Shortcuts some of the casting.
144 const XMLCh* ns = message.getElementQName().getNamespaceURI();
146 if (XMLString::equals(ns, samlconstants::SAML20P_NS) || XMLString::equals(ns, samlconstants::SAML20_NS)) {
147 // 2.0 namespace should be castable to a specialized 2.0 root.
148 const saml2::RootObject& root = dynamic_cast<const saml2::RootObject&>(message);
149 issuer = root.getIssuer();
150 if (issuer && issuer->getName()) {
151 return make_pair(issuer->cloneIssuer(), samlconstants::SAML20P_NS);
154 // No issuer in the message, so we have to try the Response approach.
155 const vector<saml2::Assertion*>& assertions = dynamic_cast<const saml2p::Response&>(message).getAssertions();
156 if (!assertions.empty()) {
157 issuer = assertions.front()->getIssuer();
158 if (issuer && issuer->getName())
159 return make_pair(issuer->cloneIssuer(), samlconstants::SAML20P_NS);
162 else if (XMLString::equals(ns, samlconstants::SAML1P_NS)) {
163 // Should be a samlp:Response, at least in OpenSAML.
164 const vector<saml1::Assertion*>& assertions = dynamic_cast<const saml1p::Response&>(message).getAssertions();
165 if (!assertions.empty()) {
166 const saml1::Assertion* a = assertions.front();
167 if (a->getIssuer()) {
168 issuer = saml2::IssuerBuilder::buildIssuer();
169 issuer->setName(a->getIssuer());
170 pair<bool,int> minor = a->getMinorVersion();
173 (minor.first && minor.second==0) ? samlconstants::SAML10_PROTOCOL_ENUM : samlconstants::SAML11_PROTOCOL_ENUM
178 else if (XMLString::equals(ns, samlconstants::SAML1_NS)) {
179 // Should be a saml:Assertion.
180 const saml1::Assertion& a = dynamic_cast<const saml1::Assertion&>(message);
182 issuer = saml2::IssuerBuilder::buildIssuer();
183 issuer->setName(a.getIssuer());
184 pair<bool,int> minor = a.getMinorVersion();
187 (minor.first && minor.second==0) ? samlconstants::SAML10_PROTOCOL_ENUM : samlconstants::SAML11_PROTOCOL_ENUM
192 return pair<saml2::Issuer*,const XMLCh*>(NULL,NULL);