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.
24 * Helper class for SAMLArtifact mapping and retrieval.
28 #include "exceptions.h"
29 #include "binding/ArtifactMap.h"
30 #include "binding/SAMLArtifact.h"
33 #include <xercesc/util/XMLUniDefs.hpp>
34 #include <xmltooling/logging.h>
35 #include <xmltooling/XMLObjectBuilder.h>
36 #include <xmltooling/XMLToolingConfig.h>
37 #include <xmltooling/security/SecurityHelper.h>
38 #include <xmltooling/util/NDC.h>
39 #include <xmltooling/util/ParserPool.h>
40 #include <xmltooling/util/StorageService.h>
41 #include <xmltooling/util/XMLHelper.h>
42 #include <xmltooling/util/Threads.h>
44 using namespace opensaml;
45 using namespace xmltooling::logging;
46 using namespace xmltooling;
50 // In-memory storage of mappings instead of using storage API.
51 class SAML_DLLLOCAL ArtifactMappings
54 ArtifactMappings() : m_lock(Mutex::create()) {}
57 for (map<string,Mapping>::iterator i=m_artMap.begin(); i!=m_artMap.end(); ++i)
58 delete i->second.m_xml;
60 void storeContent(XMLObject* content, const SAMLArtifact* artifact, const char* relyingParty, int TTL);
61 XMLObject* retrieveContent(const SAMLArtifact* artifact, const char* relyingParty);
62 string getRelyingParty(const SAMLArtifact* artifact);
65 struct SAML_DLLLOCAL Mapping {
66 Mapping() : m_xml(nullptr), m_expires(0) {}
72 void removeMapping(const map<string,Mapping>::iterator& i);
75 map<string,Mapping> m_artMap;
76 multimap<time_t,string> m_expMap;
79 static const XMLCh artifactTTL[] = UNICODE_LITERAL_11(a,r,t,i,f,a,c,t,T,T,L);
80 static const XMLCh context[] = UNICODE_LITERAL_7(c,o,n,t,e,x,t);
81 static const XMLCh Mapping[] = UNICODE_LITERAL_7(M,a,p,p,i,n,g);
82 static const XMLCh _relyingParty[] = UNICODE_LITERAL_12(r,e,l,y,i,n,g,P,a,r,t,y);
85 void ArtifactMappings::removeMapping(const map<string,Mapping>::iterator& i)
87 // Update secondary map.
88 pair<multimap<time_t,string>::iterator,multimap<time_t,string>::iterator> range =
89 m_expMap.equal_range(i->second.m_expires);
90 for (; range.first != range.second; ++range.first) {
91 if (range.first->second == i->first) {
92 m_expMap.erase(range.first);
96 delete i->second.m_xml;
100 void ArtifactMappings::storeContent(XMLObject* content, const SAMLArtifact* artifact, const char* relyingParty, int TTL)
102 Lock wrapper(m_lock);
104 // Garbage collect any expired artifacts.
105 time_t now=time(nullptr);
106 multimap<time_t,string>::iterator stop=m_expMap.upper_bound(now);
107 for (multimap<time_t,string>::iterator i=m_expMap.begin(); i!=stop; m_expMap.erase(i++)) {
108 delete m_artMap[i->second].m_xml;
109 m_artMap.erase(i->second);
112 // Key is the hexed handle.
113 string hexed = SAMLArtifact::toHex(artifact->getMessageHandle());
114 Mapping& m = m_artMap[hexed];
117 m.m_relying = relyingParty;
118 m.m_expires = now + TTL;
119 m_expMap.insert(pair<const time_t,string>(m.m_expires,hexed));
122 XMLObject* ArtifactMappings::retrieveContent(const SAMLArtifact* artifact, const char* relyingParty)
124 Category& log=Category::getInstance(SAML_LOGCAT".ArtifactMap");
125 Lock wrapper(m_lock);
127 map<string,Mapping>::iterator i=m_artMap.find(SAMLArtifact::toHex(artifact->getMessageHandle()));
128 if (i==m_artMap.end())
129 throw BindingException("Requested artifact not in map or may have expired.");
131 if (!(i->second.m_relying.empty())) {
132 if (!relyingParty || i->second.m_relying != relyingParty) {
134 "request from (%s) for artifact issued to (%s)",
135 relyingParty ? relyingParty : "unknown", i->second.m_relying.c_str()
138 throw BindingException("Unauthorized artifact mapping request.");
142 if (time(nullptr) >= i->second.m_expires) {
144 throw BindingException("Requested artifact has expired.");
147 log.debug("resolved artifact for (%s)", relyingParty ? relyingParty : "unknown");
148 XMLObject* ret = i->second.m_xml;
149 i->second.m_xml = nullptr; // clear member so it doesn't get deleted
154 string ArtifactMappings::getRelyingParty(const SAMLArtifact* artifact)
156 map<string,Mapping>::iterator i=m_artMap.find(SAMLArtifact::toHex(artifact->getMessageHandle()));
157 if (i==m_artMap.end())
158 throw BindingException("Requested artifact not in map or may have expired.");
159 return i->second.m_relying;
162 ArtifactMap::ArtifactMap(xmltooling::StorageService* storage, const char* context, unsigned int artifactTTL)
163 : m_storage(storage), m_context((context && *context) ? context : "opensaml::ArtifactMap"), m_mappings(nullptr), m_artifactTTL(artifactTTL)
166 m_mappings = new ArtifactMappings();
169 ArtifactMap::ArtifactMap(const DOMElement* e, xmltooling::StorageService* storage)
170 : m_storage(storage), m_mappings(nullptr), m_artifactTTL(180)
173 auto_ptr_char c(e->getAttributeNS(nullptr, context));
174 if (c.get() && *c.get()) {
176 if (storage && m_context.length() > m_storage->getCapabilities().getContextSize()) {
177 throw IOException("ArtifactMap context length exceeds capacity of storage service.");
181 m_context = "opensaml::ArtifactMap";
184 const XMLCh* TTL = e->getAttributeNS(nullptr, artifactTTL);
186 m_artifactTTL = XMLString::parseInt(TTL);
193 m_mappings = new ArtifactMappings();
196 ArtifactMap::~ArtifactMap()
201 void ArtifactMap::storeContent(XMLObject* content, const SAMLArtifact* artifact, const char* relyingParty)
203 if (content->getParent())
204 throw BindingException("Cannot store artifact mapping for XML content with parent.");
206 return m_mappings->storeContent(content, artifact, relyingParty, m_artifactTTL);
208 // Marshall with defaulted document, to reuse existing DOM and/or create a bound Document.
209 DOMElement* root = content->marshall();
211 // Build a DOM with the same document to store the relyingParty mapping.
213 auto_ptr_XMLCh temp(relyingParty);
214 root = root->getOwnerDocument()->createElementNS(nullptr,Mapping);
215 root->setAttributeNS(nullptr,_relyingParty,temp.get());
216 root->appendChild(content->getDOM());
219 // Serialize the root element, whatever it is, for storage.
221 XMLHelper::serialize(root, xmlbuf);
223 // Use hex form of message handler as storage key unless it's too big.
224 string key = artifact->getMessageHandle();
225 if (key.length() > m_storage->getCapabilities().getKeySize())
226 key = SecurityHelper::doHash("SHA1", key.data(), key.length());
228 key = SAMLArtifact::toHex(key);
230 if (!m_storage->createText(
234 time(nullptr) + m_artifactTTL
236 throw IOException("Attempt to insert duplicate artifact into map.");
239 // Cleanup by destroying XML.
243 XMLObject* ArtifactMap::retrieveContent(const SAMLArtifact* artifact, const char* relyingParty)
246 xmltooling::NDC ndc("retrieveContent");
248 Category& log=Category::getInstance(SAML_LOGCAT".ArtifactMap");
251 return m_mappings->retrieveContent(artifact, relyingParty);
253 // Use hex form of message handler as storage key unless it's too big.
254 string key = artifact->getMessageHandle();
255 if (key.length() > m_storage->getCapabilities().getKeySize())
256 key = SecurityHelper::doHash("SHA1", key.data(), key.length());
258 key = SAMLArtifact::toHex(key);
260 // Read the mapping and then delete it.
262 if (!m_storage->readText(m_context.c_str(), key.c_str(), &xmlbuf))
263 throw BindingException("Artifact not found in mapping database.");
264 m_storage->deleteText(m_context.c_str(), key.c_str());
266 // Parse the data back into XML.
267 istringstream is(xmlbuf);
268 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(is);
269 XercesJanitor<DOMDocument> janitor(doc);
271 // Check the root element.
272 DOMElement* messageRoot = doc->getDocumentElement();
273 if (XMLHelper::isNodeNamed(messageRoot, nullptr, Mapping)) {
274 auto_ptr_char temp(messageRoot->getAttributeNS(nullptr,_relyingParty));
275 if (!relyingParty || strcmp(temp.get(),relyingParty)) {
276 log.warn("request from (%s) for artifact issued to (%s)", relyingParty ? relyingParty : "unknown", temp.get());
277 throw BindingException("Unauthorized artifact mapping request.");
279 messageRoot = XMLHelper::getFirstChildElement(messageRoot);
283 XMLObject* xmlObject = XMLObjectBuilder::buildOneFromElement(messageRoot, true); // bind document
286 log.debug("resolved artifact for (%s)", relyingParty ? relyingParty : "unknown");
290 string ArtifactMap::getRelyingParty(const SAMLArtifact* artifact)
293 return m_mappings->getRelyingParty(artifact);
295 // Use hex form of message handler as storage key unless it's too big.
296 string key = artifact->getMessageHandle();
297 if (key.length() > m_storage->getCapabilities().getKeySize())
298 key = SecurityHelper::doHash("SHA1", key.data(), key.length());
300 key = SAMLArtifact::toHex(key);
303 if (!m_storage->readText(m_context.c_str(), key.c_str(), &xmlbuf))
304 throw BindingException("Artifact not found in mapping database.");
306 // Parse the data back into XML.
307 istringstream is(xmlbuf);
308 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(is);
309 XercesJanitor<DOMDocument> janitor(doc);
311 // Check the root element.
312 DOMElement* messageRoot = doc->getDocumentElement();
313 if (XMLHelper::isNodeNamed(messageRoot, nullptr, Mapping)) {
314 auto_ptr_char temp(messageRoot->getAttributeNS(nullptr,_relyingParty));