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