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 ClientCertAuthRuleFactory;
39 SAML_DLLLOCAL PluginManager<SecurityPolicyRule,const DOMElement*>::Factory MessageFlowRuleFactory;
40 SAML_DLLLOCAL PluginManager<SecurityPolicyRule,const DOMElement*>::Factory SimpleSigningRuleFactory;
41 SAML_DLLLOCAL PluginManager<SecurityPolicyRule,const DOMElement*>::Factory XMLSigningRuleFactory;
44 void SAML_API opensaml::registerSecurityPolicyRules()
46 SAMLConfig& conf=SAMLConfig::getConfig();
47 conf.SecurityPolicyRuleManager.registerFactory(CLIENTCERTAUTH_POLICY_RULE, ClientCertAuthRuleFactory);
48 conf.SecurityPolicyRuleManager.registerFactory(MESSAGEFLOW_POLICY_RULE, MessageFlowRuleFactory);
49 conf.SecurityPolicyRuleManager.registerFactory(SIMPLESIGNING_POLICY_RULE, SimpleSigningRuleFactory);
50 conf.SecurityPolicyRuleManager.registerFactory(XMLSIGNING_POLICY_RULE, XMLSigningRuleFactory);
53 SecurityPolicy::IssuerMatchingPolicy SecurityPolicy::m_defaultMatching;
55 SecurityPolicyRule::MessageExtractor SecurityPolicy::m_defaultExtractor;
57 SecurityPolicy::~SecurityPolicy()
60 delete m_matchingPolicy;
64 void SecurityPolicy::evaluate(const GenericRequest& request, const XMLObject& message)
66 for (vector<const SecurityPolicyRule*>::const_iterator i=m_rules.begin(); i!=m_rules.end(); ++i) {
69 pair<Issuer*,const RoleDescriptor*> ident =
70 (*i)->evaluate(request,message,m_metadata,&m_role,m_trust,getMessageExtractor());
72 // Make sure returned issuer doesn't conflict.
75 if (!getIssuerMatchingPolicy().issuerMatches(ident.first, m_issuer)) {
77 throw BindingException("Policy rules returned differing Issuers.");
84 if (m_issuerRole && ident.second!=m_issuerRole)
85 throw BindingException("Policy rules returned differing issuer RoleDescriptors.");
86 m_issuerRole=ident.second;
91 void SecurityPolicy::setIssuer(saml2::Issuer* issuer)
93 if (!getIssuerMatchingPolicy().issuerMatches(issuer, m_issuer)) {
95 throw BindingException("Externally provided Issuer conflicts with policy results.");
102 void SecurityPolicy::setIssuerMetadata(const RoleDescriptor* issuerRole)
104 if (issuerRole && m_issuerRole && issuerRole!=m_issuerRole)
105 throw BindingException("Externally provided RoleDescriptor conflicts with policy results.");
106 m_issuerRole=issuerRole;
109 bool SecurityPolicy::IssuerMatchingPolicy::issuerMatches(const Issuer* issuer1, const Issuer* issuer2) const
111 // NULL matches anything for the purposes of this interface.
112 if (!issuer1 || !issuer2)
115 const XMLCh* op1=issuer1->getName();
116 const XMLCh* op2=issuer2->getName();
117 if (!op1 || !op2 || !XMLString::equals(op1,op2))
120 op1=issuer1->getFormat();
121 op2=issuer2->getFormat();
122 if (!XMLString::equals(op1 ? op1 : NameIDType::ENTITY, op2 ? op2 : NameIDType::ENTITY))
125 op1=issuer1->getNameQualifier();
126 op2=issuer2->getNameQualifier();
127 if (!XMLString::equals(op1 ? op1 : &chNull, op2 ? op2 : &chNull))
130 op1=issuer1->getSPNameQualifier();
131 op2=issuer2->getSPNameQualifier();
132 if (!XMLString::equals(op1 ? op1 : &chNull, op2 ? op2 : &chNull))
139 pair<saml2::Issuer*,const XMLCh*> SecurityPolicyRule::MessageExtractor::getIssuerAndProtocol(const XMLObject& message) const
141 // We just let any bad casts throw here.
143 saml2::Issuer* issuer;
145 // Shortcuts some of the casting.
146 const XMLCh* ns = message.getElementQName().getNamespaceURI();
148 if (XMLString::equals(ns, samlconstants::SAML20P_NS) || XMLString::equals(ns, samlconstants::SAML20_NS)) {
149 // 2.0 namespace should be castable to a specialized 2.0 root.
150 const saml2::RootObject& root = dynamic_cast<const saml2::RootObject&>(message);
151 issuer = root.getIssuer();
152 if (issuer && issuer->getName()) {
153 return make_pair(issuer->cloneIssuer(), samlconstants::SAML20P_NS);
156 // No issuer in the message, so we have to try the Response approach.
157 const vector<saml2::Assertion*>& assertions = dynamic_cast<const saml2p::Response&>(message).getAssertions();
158 if (!assertions.empty()) {
159 issuer = assertions.front()->getIssuer();
160 if (issuer && issuer->getName())
161 return make_pair(issuer->cloneIssuer(), samlconstants::SAML20P_NS);
164 else if (XMLString::equals(ns, samlconstants::SAML1P_NS)) {
165 // Should be a samlp:Response, at least in OpenSAML.
166 const vector<saml1::Assertion*>& assertions = dynamic_cast<const saml1p::Response&>(message).getAssertions();
167 if (!assertions.empty()) {
168 const saml1::Assertion* a = assertions.front();
169 if (a->getIssuer()) {
170 issuer = saml2::IssuerBuilder::buildIssuer();
171 issuer->setName(a->getIssuer());
172 pair<bool,int> minor = a->getMinorVersion();
175 (minor.first && minor.second==0) ? samlconstants::SAML10_PROTOCOL_ENUM : samlconstants::SAML11_PROTOCOL_ENUM
180 else if (XMLString::equals(ns, samlconstants::SAML1_NS)) {
181 // Should be a saml:Assertion.
182 const saml1::Assertion& a = dynamic_cast<const saml1::Assertion&>(message);
184 issuer = saml2::IssuerBuilder::buildIssuer();
185 issuer->setName(a.getIssuer());
186 pair<bool,int> minor = a.getMinorVersion();
189 (minor.first && minor.second==0) ? samlconstants::SAML10_PROTOCOL_ENUM : samlconstants::SAML11_PROTOCOL_ENUM
194 return pair<saml2::Issuer*,const XMLCh*>(NULL,NULL);