d9aee6426b72018a7f508f0696a6aa7ed078dcb3
[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  */\r
20 \r
21 /**\r
22  * XMLSecurityPolicyProvider.cpp\r
23  *\r
24  * XML-based security policy provider.\r
25  */\r
26 \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
34 \r
35 #include <map>\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
45 \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
52 \r
53 namespace shibsp {\r
54 \r
55 #if defined (_MSC_VER)\r
56     #pragma warning( push )\r
57     #pragma warning( disable : 4250 )\r
58 #endif\r
59 \r
60     class SHIBSP_DLLLOCAL XMLSecurityPolicyProviderImpl\r
61     {\r
62     public:\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
68             }\r
69             if (m_document)\r
70                 m_document->release();\r
71         }\r
72 \r
73         void setDocument(DOMDocument* doc) {\r
74             m_document = doc;\r
75         }\r
76 \r
77     private:\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
82 \r
83         friend class SHIBSP_DLLLOCAL XMLSecurityPolicyProvider;\r
84     };\r
85 \r
86     class XMLSecurityPolicyProvider : public SecurityPolicyProvider, public ReloadableXMLFile\r
87     {\r
88     public:\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
92         }\r
93 \r
94         ~XMLSecurityPolicyProvider() {\r
95             shutdown();\r
96             delete m_impl;\r
97         }\r
98 \r
99         const PropertySet* getPolicySettings(const char* id=nullptr) const {\r
100             if (!id || !*id)\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
106         }\r
107 \r
108         const vector<const SecurityPolicyRule*>& getPolicyRules(const char* id=nullptr) const {\r
109             if (!id || !*id)\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
115         }\r
116         const vector<xstring>& getAlgorithmBlacklist() const {\r
117             return m_impl->m_blacklist;\r
118         }\r
119         const vector<xstring>& getAlgorithmWhitelist() const {\r
120             return m_impl->m_whitelist;\r
121         }\r
122         \r
123     protected:\r
124         pair<bool,DOMElement*> load(bool backup);\r
125         pair<bool,DOMElement*> background_load();\r
126 \r
127     private:\r
128         XMLSecurityPolicyProviderImpl* m_impl;\r
129     };\r
130 \r
131 #if defined (_MSC_VER)\r
132     #pragma warning( pop )\r
133 #endif\r
134 \r
135     SecurityPolicyProvider* SHIBSP_DLLLOCAL XMLSecurityPolicyProviderFactory(const DOMElement* const & e)\r
136     {\r
137         return new XMLSecurityPolicyProvider(e);\r
138     }\r
139 \r
140     class SHIBSP_DLLLOCAL PolicyNodeFilter : public DOMNodeFilter\r
141     {\r
142     public:\r
143 #ifdef SHIBSP_XERCESC_SHORT_ACCEPTNODE\r
144         short\r
145 #else\r
146         FilterAction\r
147 #endif\r
148         acceptNode(const DOMNode* node) const {\r
149             return FILTER_REJECT;\r
150         }\r
151     };\r
152 \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
161 }\r
162 \r
163 void SHIBSP_API shibsp::registerSecurityPolicyProviders()\r
164 {\r
165     SPConfig::getConfig().SecurityPolicyProviderManager.registerFactory(XML_SECURITYPOLICY_PROVIDER, XMLSecurityPolicyProviderFactory);\r
166 }\r
167 \r
168 SecurityPolicyProvider::SecurityPolicyProvider()\r
169 {\r
170 }\r
171 \r
172 SecurityPolicyProvider::~SecurityPolicyProvider()\r
173 {\r
174 }\r
175 \r
176 SecurityPolicy* SecurityPolicyProvider::createSecurityPolicy(\r
177     const Application& application, const xmltooling::QName* role, const char* policyId\r
178     ) const\r
179 {\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
182 }\r
183 \r
184 XMLSecurityPolicyProviderImpl::XMLSecurityPolicyProviderImpl(const DOMElement* e, Category& log)\r
185     : m_document(nullptr), m_defaultPolicy(m_policyMap.end())\r
186 {\r
187 #ifdef _DEBUG\r
188     xmltooling::NDC ndc("XMLSecurityPolicyProviderImpl");\r
189 #endif\r
190 \r
191     if (!XMLHelper::isNodeNamed(e, SHIB2SPCONFIG_NS, SecurityPolicies))\r
192         throw ConfigurationException("XML SecurityPolicyProvider requires conf:SecurityPolicies at root of configuration.");\r
193 \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
198     }\r
199     else if ((alglist = XMLHelper::getLastChildElement(e, AlgorithmWhitelist)) && alglist->hasChildNodes()) {\r
200         algs = alglist->getFirstChild()->getNodeValue();\r
201     }\r
202     if (algs) {\r
203         const XMLCh* token;\r
204         XMLStringTokenizer tokenizer(algs);\r
205         while (tokenizer.hasMoreTokens()) {\r
206             token = tokenizer.nextToken();\r
207             if (token) {\r
208                 if (XMLString::equals(alglist->getLocalName(), AlgorithmBlacklist))\r
209                     m_blacklist.push_back(token);\r
210                 else\r
211                     m_whitelist.push_back(token);\r
212             }\r
213         }\r
214     }\r
215 \r
216     PolicyNodeFilter filter;\r
217     SAMLConfig& samlConf = SAMLConfig::getConfig();\r
218     e = XMLHelper::getFirstChildElement(e, Policy);\r
219     while (e) {\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
226 \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
230 \r
231         // Process PolicyRule elements.\r
232         const DOMElement* rule = XMLHelper::getFirstChildElement(e, PolicyRule);\r
233         while (rule) {\r
234             string t(XMLHelper::getAttrString(rule, nullptr, _type));\r
235             if (!t.empty()) {\r
236                 try {\r
237                     rules.second.push_back(samlConf.SecurityPolicyRuleManager.newPlugin(t.c_str(), rule));\r
238                 }\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
241                 }\r
242             }\r
243             rule = XMLHelper::getNextSiblingElement(rule, PolicyRule);\r
244         }\r
245 \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
250             while (rule) {\r
251                 string t(XMLHelper::getAttrString(rule, nullptr, _type));\r
252                 if (!t.empty()) {\r
253                     try {\r
254                         rules.second.push_back(samlConf.SecurityPolicyRuleManager.newPlugin(t.c_str(), rule));\r
255                     }\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
258                     }\r
259                 }\r
260                 rule = XMLHelper::getNextSiblingElement(rule, Rule);\r
261             }\r
262 \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
266         }\r
267 \r
268         e = XMLHelper::getNextSiblingElement(e, Policy);\r
269     }\r
270 \r
271     if (m_defaultPolicy == m_policyMap.end())\r
272         throw ConfigurationException("XML SecurityPolicyProvider requires at least one Policy.");\r
273 }\r
274 \r
275 pair<bool,DOMElement*> XMLSecurityPolicyProvider::load(bool backup)\r
276 {\r
277     // Load from source using base class.\r
278     pair<bool,DOMElement*> raw = ReloadableXMLFile::load(backup);\r
279 \r
280     // If we own it, wrap it.\r
281     XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : nullptr);\r
282 \r
283     XMLSecurityPolicyProviderImpl* impl = new XMLSecurityPolicyProviderImpl(raw.second, m_log);\r
284 \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
287 \r
288     // Perform the swap inside a lock.\r
289     if (m_lock)\r
290         m_lock->wrlock();\r
291     SharedLock locker(m_lock, false);\r
292     delete m_impl;\r
293     m_impl = impl;\r
294 \r
295 \r
296     return make_pair(false,(DOMElement*)nullptr);\r
297 }\r
298 \r
299 pair<bool,DOMElement*> XMLSecurityPolicyProvider::background_load()\r
300 {\r
301     try {\r
302         return load(false);\r
303     }\r
304     catch (long& ex) {\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
308             return load(true);\r
309         throw;\r
310     }\r
311     catch (exception&) {\r
312         if (!m_loaded && !m_backing.empty())\r
313             return load(true);\r
314         throw;\r
315     }\r
316 }\r