2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
22 * XMLSecurityPolicyProvider.cpp
24 * XML-based security policy provider.
28 #include "exceptions.h"
29 #include "Application.h"
30 #include "security/SecurityPolicy.h"
31 #include "security/SecurityPolicyProvider.h"
32 #include "util/DOMPropertySet.h"
33 #include "util/SPConstants.h"
36 #include <saml/SAMLConfig.h>
37 #include <saml/binding/SecurityPolicyRule.h>
38 #include <xmltooling/io/HTTPResponse.h>
39 #include <xmltooling/util/NDC.h>
40 #include <xmltooling/util/ReloadableXMLFile.h>
41 #include <xmltooling/util/Threads.h>
42 #include <xmltooling/util/XMLHelper.h>
43 #include <xercesc/util/XMLStringTokenizer.hpp>
44 #include <xercesc/util/XMLUniDefs.hpp>
46 using shibspconstants::SHIB2SPCONFIG_NS;
47 using opensaml::SAMLConfig;
48 using opensaml::SecurityPolicyRule;
49 using namespace shibsp;
50 using namespace xmltooling;
55 #if defined (_MSC_VER)
56 #pragma warning( push )
57 #pragma warning( disable : 4250 )
60 class SHIBSP_DLLLOCAL XMLSecurityPolicyProviderImpl
63 XMLSecurityPolicyProviderImpl(const DOMElement* e, Category& log);
64 ~XMLSecurityPolicyProviderImpl() {
65 for (map< string,pair<PropertySet*,vector<const SecurityPolicyRule*> > >::iterator i = m_policyMap.begin(); i != m_policyMap.end(); ++i) {
66 delete i->second.first;
67 for_each(i->second.second.begin(), i->second.second.end(), xmltooling::cleanup<SecurityPolicyRule>());
70 m_document->release();
73 void setDocument(DOMDocument* doc) {
78 DOMDocument* m_document;
79 vector<xstring> m_whitelist,m_blacklist;
80 map< string,pair< PropertySet*,vector<const SecurityPolicyRule*> > > m_policyMap;
81 map< string,pair< PropertySet*,vector<const SecurityPolicyRule*> > >::const_iterator m_defaultPolicy;
83 friend class SHIBSP_DLLLOCAL XMLSecurityPolicyProvider;
86 class XMLSecurityPolicyProvider : public SecurityPolicyProvider, public ReloadableXMLFile
89 XMLSecurityPolicyProvider(const DOMElement* e)
90 : ReloadableXMLFile(e, Category::getInstance(SHIBSP_LOGCAT".SecurityPolicyProvider.XML")), m_impl(nullptr) {
91 background_load(); // guarantees an exception or the policy is loaded
94 ~XMLSecurityPolicyProvider() {
99 const PropertySet* getPolicySettings(const char* id=nullptr) const {
101 return m_impl->m_defaultPolicy->second.first;
102 map<string,pair<PropertySet*,vector<const SecurityPolicyRule*> > >::const_iterator i = m_impl->m_policyMap.find(id);
103 if (i != m_impl->m_policyMap.end())
104 return i->second.first;
105 throw ConfigurationException("Security Policy ($1) not found, check <SecurityPolicies> element.", params(1,id));
108 const vector<const SecurityPolicyRule*>& getPolicyRules(const char* id=nullptr) const {
110 return m_impl->m_defaultPolicy->second.second;
111 map<string,pair<PropertySet*,vector<const SecurityPolicyRule*> > >::const_iterator i = m_impl->m_policyMap.find(id);
112 if (i != m_impl->m_policyMap.end())
113 return i->second.second;
114 throw ConfigurationException("Security Policy ($1) not found, check <SecurityPolicies> element.", params(1,id));
116 const vector<xstring>& getAlgorithmBlacklist() const {
117 return m_impl->m_blacklist;
119 const vector<xstring>& getAlgorithmWhitelist() const {
120 return m_impl->m_whitelist;
124 pair<bool,DOMElement*> load(bool backup);
125 pair<bool,DOMElement*> background_load();
128 XMLSecurityPolicyProviderImpl* m_impl;
131 #if defined (_MSC_VER)
132 #pragma warning( pop )
135 SecurityPolicyProvider* SHIBSP_DLLLOCAL XMLSecurityPolicyProviderFactory(const DOMElement* const & e)
137 return new XMLSecurityPolicyProvider(e);
140 class SHIBSP_DLLLOCAL PolicyNodeFilter : public DOMNodeFilter
143 #ifdef SHIBSP_XERCESC_SHORT_ACCEPTNODE
148 acceptNode(const DOMNode* node) const {
149 return FILTER_REJECT;
153 static const XMLCh _id[] = UNICODE_LITERAL_2(i,d);
154 static const XMLCh _type[] = UNICODE_LITERAL_4(t,y,p,e);
155 static const XMLCh AlgorithmBlacklist[] = UNICODE_LITERAL_18(A,l,g,o,r,i,t,h,m,B,l,a,c,k,l,i,s,t);
156 static const XMLCh AlgorithmWhitelist[] = UNICODE_LITERAL_18(A,l,g,o,r,i,t,h,m,W,h,i,t,e,l,i,s,t);
157 static const XMLCh Policy[] = UNICODE_LITERAL_6(P,o,l,i,c,y);
158 static const XMLCh PolicyRule[] = UNICODE_LITERAL_10(P,o,l,i,c,y,R,u,l,e);
159 static const XMLCh Rule[] = UNICODE_LITERAL_4(R,u,l,e);
160 static const XMLCh SecurityPolicies[] = UNICODE_LITERAL_16(S,e,c,u,r,i,t,y,P,o,l,i,c,i,e,s);
163 void SHIBSP_API shibsp::registerSecurityPolicyProviders()
165 SPConfig::getConfig().SecurityPolicyProviderManager.registerFactory(XML_SECURITYPOLICY_PROVIDER, XMLSecurityPolicyProviderFactory);
168 SecurityPolicyProvider::SecurityPolicyProvider()
172 SecurityPolicyProvider::~SecurityPolicyProvider()
176 SecurityPolicy* SecurityPolicyProvider::createSecurityPolicy(
177 const Application& application, const xmltooling::QName* role, const char* policyId
180 pair<bool,bool> validate = getPolicySettings(policyId ? policyId : application.getString("policyId").second)->getBool("validate");
181 return new SecurityPolicy(application, role, (validate.first && validate.second), policyId);
184 XMLSecurityPolicyProviderImpl::XMLSecurityPolicyProviderImpl(const DOMElement* e, Category& log)
185 : m_document(nullptr), m_defaultPolicy(m_policyMap.end())
188 xmltooling::NDC ndc("XMLSecurityPolicyProviderImpl");
191 if (!XMLHelper::isNodeNamed(e, SHIB2SPCONFIG_NS, SecurityPolicies))
192 throw ConfigurationException("XML SecurityPolicyProvider requires conf:SecurityPolicies at root of configuration.");
194 const XMLCh* algs = nullptr;
195 const DOMElement* alglist = XMLHelper::getLastChildElement(e, AlgorithmBlacklist);
196 if (alglist && alglist->hasChildNodes()) {
197 algs = alglist->getFirstChild()->getNodeValue();
199 else if ((alglist = XMLHelper::getLastChildElement(e, AlgorithmWhitelist)) && alglist->hasChildNodes()) {
200 algs = alglist->getFirstChild()->getNodeValue();
204 XMLStringTokenizer tokenizer(algs);
205 while (tokenizer.hasMoreTokens()) {
206 token = tokenizer.nextToken();
208 if (XMLString::equals(alglist->getLocalName(), AlgorithmBlacklist))
209 m_blacklist.push_back(token);
211 m_whitelist.push_back(token);
216 PolicyNodeFilter filter;
217 SAMLConfig& samlConf = SAMLConfig::getConfig();
218 e = XMLHelper::getFirstChildElement(e, Policy);
220 string id(XMLHelper::getAttrString(e, nullptr, _id));
221 pair< PropertySet*,vector<const SecurityPolicyRule*> >& rules = m_policyMap[id];
222 rules.first = nullptr;
223 auto_ptr<DOMPropertySet> settings(new DOMPropertySet());
224 settings->load(e, nullptr, &filter);
225 rules.first = settings.release();
227 // Set default policy if not set, or id is "default".
228 if (m_defaultPolicy == m_policyMap.end() || id == "default")
229 m_defaultPolicy = m_policyMap.find(id);
231 // Process PolicyRule elements.
232 const DOMElement* rule = XMLHelper::getFirstChildElement(e, PolicyRule);
234 string t(XMLHelper::getAttrString(rule, nullptr, _type));
237 rules.second.push_back(samlConf.SecurityPolicyRuleManager.newPlugin(t.c_str(), rule));
239 catch (exception& ex) {
240 log.crit("error instantiating policy rule (%s) in policy (%s): %s", t.c_str(), id.c_str(), ex.what());
243 rule = XMLHelper::getNextSiblingElement(rule, PolicyRule);
246 if (rules.second.size() == 0) {
247 // Process Rule elements.
248 log.warn("detected legacy Policy configuration, please convert to new PolicyRule syntax");
249 rule = XMLHelper::getFirstChildElement(e, Rule);
251 string t(XMLHelper::getAttrString(rule, nullptr, _type));
254 rules.second.push_back(samlConf.SecurityPolicyRuleManager.newPlugin(t.c_str(), rule));
256 catch (exception& ex) {
257 log.crit("error instantiating policy rule (%s) in policy (%s): %s", t.c_str(), id.c_str(), ex.what());
260 rule = XMLHelper::getNextSiblingElement(rule, Rule);
263 // Manually add a basic Conditions rule.
264 log.info("installing a default Conditions rule in policy (%s) for compatibility with legacy configuration", id.c_str());
265 rules.second.push_back(samlConf.SecurityPolicyRuleManager.newPlugin(CONDITIONS_POLICY_RULE, nullptr));
268 e = XMLHelper::getNextSiblingElement(e, Policy);
271 if (m_defaultPolicy == m_policyMap.end())
272 throw ConfigurationException("XML SecurityPolicyProvider requires at least one Policy.");
275 pair<bool,DOMElement*> XMLSecurityPolicyProvider::load(bool backup)
277 // Load from source using base class.
278 pair<bool,DOMElement*> raw = ReloadableXMLFile::load(backup);
280 // If we own it, wrap it.
281 XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : nullptr);
283 XMLSecurityPolicyProviderImpl* impl = new XMLSecurityPolicyProviderImpl(raw.second, m_log);
285 // If we held the document, transfer it to the impl. If we didn't, it's a no-op.
286 impl->setDocument(docjanitor.release());
288 // Perform the swap inside a lock.
291 SharedLock locker(m_lock, false);
296 return make_pair(false,(DOMElement*)nullptr);
299 pair<bool,DOMElement*> XMLSecurityPolicyProvider::background_load()
305 if (ex == HTTPResponse::XMLTOOLING_HTTP_STATUS_NOTMODIFIED)
306 m_log.info("remote resource (%s) unchanged", m_source.c_str());
307 if (!m_loaded && !m_backing.empty())
312 if (!m_loaded && !m_backing.empty())