8c383d6ae3002969a359e6a69468e9c727964550
[shibboleth/cpp-sp.git] / shibsp / impl / XMLSecurityPolicyProvider.cpp
1 /**
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.
6  *
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
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20
21 /**
22  * XMLSecurityPolicyProvider.cpp
23  *
24  * XML-based security policy provider.
25  */
26
27 #include "internal.h"
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"
34
35 #include <map>
36 #include <boost/shared_ptr.hpp>
37 #include <saml/SAMLConfig.h>
38 #include <saml/binding/SecurityPolicyRule.h>
39 #include <xmltooling/io/HTTPResponse.h>
40 #include <xmltooling/util/NDC.h>
41 #include <xmltooling/util/ReloadableXMLFile.h>
42 #include <xmltooling/util/Threads.h>
43 #include <xmltooling/util/XMLHelper.h>
44 #include <xercesc/util/XMLStringTokenizer.hpp>
45 #include <xercesc/util/XMLUniDefs.hpp>
46
47 using shibspconstants::SHIB2SPCONFIG_NS;
48 using opensaml::SAMLConfig;
49 using opensaml::SecurityPolicyRule;
50 using namespace shibsp;
51 using namespace xmltooling;
52 using namespace boost;
53 using namespace std;
54
55 namespace shibsp {
56
57 #if defined (_MSC_VER)
58     #pragma warning( push )
59     #pragma warning( disable : 4250 )
60 #endif
61
62     class SHIBSP_DLLLOCAL XMLSecurityPolicyProviderImpl
63     {
64     public:
65         XMLSecurityPolicyProviderImpl(const DOMElement*, Category&);
66         ~XMLSecurityPolicyProviderImpl() {
67             if (m_document)
68                 m_document->release();
69         }
70
71         void setDocument(DOMDocument* doc) {
72             m_document = doc;
73         }
74
75     private:
76         DOMDocument* m_document;
77         bool m_includeDefaultBlacklist;
78         vector<xstring> m_whitelist,m_blacklist;
79         vector< boost::shared_ptr<SecurityPolicyRule> > m_ruleJanitor;   // need this to maintain vector type in API
80         typedef map< string,pair< boost::shared_ptr<PropertySet>,vector<const SecurityPolicyRule*> > > policymap_t;
81         policymap_t m_policyMap;
82         policymap_t::const_iterator m_defaultPolicy;
83
84         friend class SHIBSP_DLLLOCAL XMLSecurityPolicyProvider;
85     };
86
87     class XMLSecurityPolicyProvider : public SecurityPolicyProvider, public ReloadableXMLFile
88     {
89     public:
90         XMLSecurityPolicyProvider(const DOMElement* e)
91                 : ReloadableXMLFile(e, Category::getInstance(SHIBSP_LOGCAT".SecurityPolicyProvider.XML")) {
92             background_load(); // guarantees an exception or the policy is loaded
93         }
94
95         ~XMLSecurityPolicyProvider() {
96             shutdown();
97         }
98
99         const PropertySet* getPolicySettings(const char* id=nullptr) const {
100             if (!id || !*id)
101                 return m_impl->m_defaultPolicy->second.first.get();
102             XMLSecurityPolicyProviderImpl::policymap_t::const_iterator i = m_impl->m_policyMap.find(id);
103             if (i != m_impl->m_policyMap.end())
104                 return i->second.first.get();
105             throw ConfigurationException("Security Policy ($1) not found, check <SecurityPolicies> element.", params(1,id));
106         }
107
108         const vector<const SecurityPolicyRule*>& getPolicyRules(const char* id=nullptr) const {
109             if (!id || !*id)
110                 return m_impl->m_defaultPolicy->second.second;
111             XMLSecurityPolicyProviderImpl::policymap_t::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));
115         }
116         const vector<xstring>& getDefaultAlgorithmBlacklist() const {
117             return m_impl->m_includeDefaultBlacklist ? m_defaultBlacklist : m_empty;
118         }
119         const vector<xstring>& getAlgorithmBlacklist() const {
120             return m_impl->m_blacklist;
121         }
122         const vector<xstring>& getAlgorithmWhitelist() const {
123             return m_impl->m_whitelist;
124         }
125         
126     protected:
127         pair<bool,DOMElement*> load(bool backup);
128         pair<bool,DOMElement*> background_load();
129
130     private:
131         scoped_ptr<XMLSecurityPolicyProviderImpl> m_impl;
132         static vector<xstring> m_empty;
133     };
134
135 #if defined (_MSC_VER)
136     #pragma warning( pop )
137 #endif
138
139     SecurityPolicyProvider* SHIBSP_DLLLOCAL XMLSecurityPolicyProviderFactory(const DOMElement* const & e)
140     {
141         return new XMLSecurityPolicyProvider(e);
142     }
143
144     class SHIBSP_DLLLOCAL PolicyNodeFilter : public DOMNodeFilter
145     {
146     public:
147 #ifdef SHIBSP_XERCESC_SHORT_ACCEPTNODE
148         short
149 #else
150         FilterAction
151 #endif
152         acceptNode(const DOMNode* node) const {
153             return FILTER_REJECT;
154         }
155     };
156
157     static const XMLCh _id[] =                  UNICODE_LITERAL_2(i,d);
158     static const XMLCh _type[] =                UNICODE_LITERAL_4(t,y,p,e);
159     static const XMLCh includeDefaultBlacklist[] = UNICODE_LITERAL_23(i,n,c,l,u,d,e,D,e,f,a,u,l,t,B,l,a,c,k,l,i,s,t);
160     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);
161     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);
162     static const XMLCh Policy[] =               UNICODE_LITERAL_6(P,o,l,i,c,y);
163     static const XMLCh PolicyRule[] =           UNICODE_LITERAL_10(P,o,l,i,c,y,R,u,l,e);
164     static const XMLCh Rule[] =                 UNICODE_LITERAL_4(R,u,l,e);
165     static const XMLCh SecurityPolicies[] =     UNICODE_LITERAL_16(S,e,c,u,r,i,t,y,P,o,l,i,c,i,e,s);
166 }
167
168 void SHIBSP_API shibsp::registerSecurityPolicyProviders()
169 {
170     SPConfig::getConfig().SecurityPolicyProviderManager.registerFactory(XML_SECURITYPOLICY_PROVIDER, XMLSecurityPolicyProviderFactory);
171 }
172
173 SecurityPolicyProvider::SecurityPolicyProvider()
174 {
175     m_defaultBlacklist.push_back(DSIGConstants::s_unicodeStrURIRSA_MD5);
176     m_defaultBlacklist.push_back(DSIGConstants::s_unicodeStrURIMD5);
177     m_defaultBlacklist.push_back(DSIGConstants::s_unicodeStrURIRSA_1_5);
178 }
179
180 SecurityPolicyProvider::~SecurityPolicyProvider()
181 {
182 }
183
184 const vector<xstring>& SecurityPolicyProvider::getDefaultAlgorithmBlacklist() const
185 {
186     return m_defaultBlacklist;
187 }
188
189 SecurityPolicy* SecurityPolicyProvider::createSecurityPolicy(
190     const Application& application, const xmltooling::QName* role, const char* policyId
191     ) const
192 {
193     pair<bool,bool> validate = getPolicySettings(policyId ? policyId : application.getString("policyId").second)->getBool("validate");
194     return new SecurityPolicy(application, role, (validate.first && validate.second), policyId);
195 }
196
197 XMLSecurityPolicyProviderImpl::XMLSecurityPolicyProviderImpl(const DOMElement* e, Category& log)
198     : m_document(nullptr), m_includeDefaultBlacklist(true), m_defaultPolicy(m_policyMap.end())
199 {
200 #ifdef _DEBUG
201     xmltooling::NDC ndc("XMLSecurityPolicyProviderImpl");
202 #endif
203
204     if (!XMLHelper::isNodeNamed(e, SHIB2SPCONFIG_NS, SecurityPolicies))
205         throw ConfigurationException("XML SecurityPolicyProvider requires conf:SecurityPolicies at root of configuration.");
206
207     const XMLCh* algs = nullptr;
208     const DOMElement* alglist = XMLHelper::getLastChildElement(e, AlgorithmBlacklist);
209     if (alglist) {
210         m_includeDefaultBlacklist = XMLHelper::getAttrBool(alglist, true, includeDefaultBlacklist);
211         if (alglist->hasChildNodes()) {
212             algs = alglist->getFirstChild()->getNodeValue();
213         }
214     }
215     else if ((alglist = XMLHelper::getLastChildElement(e, AlgorithmWhitelist)) && alglist->hasChildNodes()) {
216         algs = alglist->getFirstChild()->getNodeValue();
217         m_includeDefaultBlacklist = false;
218     }
219     if (algs) {
220         const XMLCh* token;
221         XMLStringTokenizer tokenizer(algs);
222         while (tokenizer.hasMoreTokens()) {
223             token = tokenizer.nextToken();
224             if (token) {
225                 if (XMLString::equals(alglist->getLocalName(), AlgorithmBlacklist))
226                     m_blacklist.push_back(token);
227                 else
228                     m_whitelist.push_back(token);
229             }
230         }
231     }
232
233     PolicyNodeFilter filter;
234     SAMLConfig& samlConf = SAMLConfig::getConfig();
235     e = XMLHelper::getFirstChildElement(e, Policy);
236     while (e) {
237         string id(XMLHelper::getAttrString(e, nullptr, _id));
238         policymap_t::mapped_type& rules = m_policyMap[id];
239         boost::shared_ptr<DOMPropertySet> settings(new DOMPropertySet());
240         settings->load(e, nullptr, &filter);
241         rules.first = settings;
242
243         // Set default policy if not set, or id is "default".
244         if (m_defaultPolicy == m_policyMap.end() || id == "default")
245             m_defaultPolicy = m_policyMap.find(id);
246
247         // Process PolicyRule elements.
248         const DOMElement* rule = XMLHelper::getFirstChildElement(e, PolicyRule);
249         while (rule) {
250             string t(XMLHelper::getAttrString(rule, nullptr, _type));
251             if (!t.empty()) {
252                 try {
253                     boost::shared_ptr<SecurityPolicyRule> ptr(samlConf.SecurityPolicyRuleManager.newPlugin(t.c_str(), rule));
254                     m_ruleJanitor.push_back(ptr);
255                     rules.second.push_back(ptr.get());
256                 }
257                 catch (std::exception& ex) {
258                     log.crit("error instantiating policy rule (%s) in policy (%s): %s", t.c_str(), id.c_str(), ex.what());
259                 }
260             }
261             rule = XMLHelper::getNextSiblingElement(rule, PolicyRule);
262         }
263
264         if (rules.second.size() == 0) {
265             // Process Rule elements.
266             log.warn("detected deprecated Policy configuration, consider converting to new PolicyRule syntax");
267             rule = XMLHelper::getFirstChildElement(e, Rule);
268             while (rule) {
269                 string t(XMLHelper::getAttrString(rule, nullptr, _type));
270                 if (!t.empty()) {
271                     try {
272                         boost::shared_ptr<SecurityPolicyRule> ptr(samlConf.SecurityPolicyRuleManager.newPlugin(t.c_str(), rule));
273                         m_ruleJanitor.push_back(ptr);
274                         rules.second.push_back(ptr.get());
275                     }
276                     catch (std::exception& ex) {
277                         log.crit("error instantiating policy rule (%s) in policy (%s): %s", t.c_str(), id.c_str(), ex.what());
278                     }
279                 }
280                 rule = XMLHelper::getNextSiblingElement(rule, Rule);
281             }
282
283             // Manually add a basic Conditions rule.
284             log.info("installing a default Conditions rule in policy (%s) for compatibility with legacy configuration", id.c_str());
285             boost::shared_ptr<SecurityPolicyRule> cptr(samlConf.SecurityPolicyRuleManager.newPlugin(CONDITIONS_POLICY_RULE, nullptr));
286             m_ruleJanitor.push_back(cptr);
287             rules.second.push_back(cptr.get());
288         }
289
290         e = XMLHelper::getNextSiblingElement(e, Policy);
291     }
292
293     if (m_defaultPolicy == m_policyMap.end())
294         throw ConfigurationException("XML SecurityPolicyProvider requires at least one Policy.");
295 }
296
297 vector<xstring> XMLSecurityPolicyProvider::m_empty;
298
299 pair<bool,DOMElement*> XMLSecurityPolicyProvider::load(bool backup)
300 {
301     // Load from source using base class.
302     pair<bool,DOMElement*> raw = ReloadableXMLFile::load(backup);
303
304     // If we own it, wrap it.
305     XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : nullptr);
306
307     scoped_ptr<XMLSecurityPolicyProviderImpl> impl(new XMLSecurityPolicyProviderImpl(raw.second, m_log));
308
309     // If we held the document, transfer it to the impl. If we didn't, it's a no-op.
310     impl->setDocument(docjanitor.release());
311
312     // Perform the swap inside a lock.
313     if (m_lock)
314         m_lock->wrlock();
315     SharedLock locker(m_lock, false);
316     m_impl.swap(impl);
317
318     return make_pair(false,(DOMElement*)nullptr);
319 }
320
321 pair<bool,DOMElement*> XMLSecurityPolicyProvider::background_load()
322 {
323     try {
324         return load(false);
325     }
326     catch (long& ex) {
327         if (ex == HTTPResponse::XMLTOOLING_HTTP_STATUS_NOTMODIFIED)
328             m_log.info("remote resource (%s) unchanged", m_source.c_str());
329         if (!m_loaded && !m_backing.empty())
330             return load(true);
331         throw;
332     }
333     catch (std::exception&) {
334         if (!m_loaded && !m_backing.empty())
335             return load(true);
336         throw;
337     }
338 }