2 * Copyright 2009-2010 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * SimpleAggregationAttributeResolver.cpp
20 * AttributeResolver based on SAML queries to third-party AA sources.
24 #include "exceptions.h"
25 #include "Application.h"
26 #include "ServiceProvider.h"
27 #include "SessionCache.h"
28 #include "attribute/NameIDAttribute.h"
29 #include "attribute/filtering/AttributeFilter.h"
30 #include "attribute/filtering/BasicFilteringContext.h"
31 #include "attribute/resolver/AttributeExtractor.h"
32 #include "attribute/resolver/AttributeResolver.h"
33 #include "attribute/resolver/ResolutionContext.h"
34 #include "binding/SOAPClient.h"
35 #include "metadata/MetadataProviderCriteria.h"
36 #include "security/SecurityPolicy.h"
37 #include "security/SecurityPolicyProvider.h"
38 #include "util/SPConstants.h"
40 #include <saml/exceptions.h>
41 #include <saml/SAMLConfig.h>
42 #include <saml/saml2/binding/SAML2SOAPClient.h>
43 #include <saml/saml2/core/Protocols.h>
44 #include <saml/saml2/metadata/Metadata.h>
45 #include <saml/saml2/metadata/MetadataCredentialCriteria.h>
46 #include <saml/saml2/metadata/MetadataProvider.h>
47 #include <xmltooling/XMLToolingConfig.h>
48 #include <xmltooling/security/TrustEngine.h>
49 #include <xmltooling/util/NDC.h>
50 #include <xmltooling/util/XMLHelper.h>
51 #include <xercesc/util/XMLUniDefs.hpp>
53 using namespace shibsp;
54 using namespace opensaml::saml2;
55 using namespace opensaml::saml2p;
56 using namespace opensaml::saml2md;
57 using namespace opensaml;
58 using namespace xmltooling;
63 class SHIBSP_DLLLOCAL SimpleAggregationContext : public ResolutionContext
66 SimpleAggregationContext(const Application& application, const Session& session)
71 m_class(XMLString::transcode(session.getAuthnContextClassRef())),
72 m_decl(XMLString::transcode(session.getAuthnContextDeclRef())),
73 m_inputTokens(nullptr),
74 m_inputAttributes(nullptr) {
77 SimpleAggregationContext(
78 const Application& application,
79 const NameID* nameid=nullptr,
80 const XMLCh* entityID=nullptr,
81 const XMLCh* authncontext_class=nullptr,
82 const XMLCh* authncontext_decl=nullptr,
83 const vector<const opensaml::Assertion*>* tokens=nullptr,
84 const vector<shibsp::Attribute*>* attributes=nullptr
85 ) : m_app(application),
88 m_entityid(entityID ? XMLString::transcode(entityID) : nullptr),
89 m_class(const_cast<XMLCh*>(authncontext_class)),
90 m_decl(const_cast<XMLCh*>(authncontext_decl)),
91 m_inputTokens(tokens),
92 m_inputAttributes(attributes) {
95 ~SimpleAggregationContext() {
96 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<shibsp::Attribute>());
97 for_each(m_assertions.begin(), m_assertions.end(), xmltooling::cleanup<opensaml::Assertion>());
99 XMLString::release(&m_class);
100 XMLString::release(&m_decl);
102 XMLString::release(&m_entityid);
105 const Application& getApplication() const {
108 const char* getEntityID() const {
109 return m_session ? m_session->getEntityID() : m_entityid;
111 const NameID* getNameID() const {
112 return m_session ? m_session->getNameID() : m_nameid;
114 const XMLCh* getClassRef() const {
117 const XMLCh* getDeclRef() const {
120 const Session* getSession() const {
123 const vector<shibsp::Attribute*>* getInputAttributes() const {
124 return m_inputAttributes;
126 const vector<const opensaml::Assertion*>* getInputTokens() const {
127 return m_inputTokens;
129 vector<shibsp::Attribute*>& getResolvedAttributes() {
132 vector<opensaml::Assertion*>& getResolvedAssertions() {
137 const Application& m_app;
138 const Session* m_session;
139 const NameID* m_nameid;
143 const vector<const opensaml::Assertion*>* m_inputTokens;
144 const vector<shibsp::Attribute*>* m_inputAttributes;
145 vector<shibsp::Attribute*> m_attributes;
146 vector<opensaml::Assertion*> m_assertions;
149 class SHIBSP_DLLLOCAL SimpleAggregationResolver : public AttributeResolver
152 SimpleAggregationResolver(const DOMElement* e);
153 ~SimpleAggregationResolver() {
156 for_each(m_designators.begin(), m_designators.end(), xmltooling::cleanup<saml2::Attribute>());
159 Lockable* lock() {return this;}
162 ResolutionContext* createResolutionContext(
163 const Application& application,
164 const EntityDescriptor* issuer,
165 const XMLCh* protocol,
166 const NameID* nameid=nullptr,
167 const XMLCh* authncontext_class=nullptr,
168 const XMLCh* authncontext_decl=nullptr,
169 const vector<const opensaml::Assertion*>* tokens=nullptr,
170 const vector<shibsp::Attribute*>* attributes=nullptr
172 return new SimpleAggregationContext(
173 application, nameid, (issuer ? issuer->getEntityID() : nullptr), authncontext_class, authncontext_decl, tokens, attributes
177 ResolutionContext* createResolutionContext(const Application& application, const Session& session) const {
178 return new SimpleAggregationContext(application,session);
181 void resolveAttributes(ResolutionContext& ctx) const;
183 void getAttributeIds(vector<string>& attributes) const {
184 // Nothing to do, only the extractor would actually generate them.
188 bool doQuery(SimpleAggregationContext& ctx, const char* entityID, const NameID* name) const;
193 vector<string> m_attributeIds;
195 MetadataProvider* m_metadata;
196 TrustEngine* m_trust;
197 vector<saml2::Attribute*> m_designators;
198 vector< pair<string,bool> > m_sources;
201 AttributeResolver* SHIBSP_DLLLOCAL SimpleAggregationResolverFactory(const DOMElement* const & e)
203 return new SimpleAggregationResolver(e);
206 static const XMLCh attributeId[] = UNICODE_LITERAL_11(a,t,t,r,i,b,u,t,e,I,d);
207 static const XMLCh Entity[] = UNICODE_LITERAL_6(E,n,t,i,t,y);
208 static const XMLCh EntityReference[] = UNICODE_LITERAL_15(E,n,t,i,t,y,R,e,f,e,r,e,n,c,e);
209 static const XMLCh format[] = UNICODE_LITERAL_6(f,o,r,m,a,t);
210 static const XMLCh _MetadataProvider[] = UNICODE_LITERAL_16(M,e,t,a,d,a,t,a,P,r,o,v,i,d,e,r);
211 static const XMLCh policyId[] = UNICODE_LITERAL_8(p,o,l,i,c,y,I,d);
212 static const XMLCh subjectMatch[] = UNICODE_LITERAL_12(s,u,b,j,e,c,t,M,a,t,c,h);
213 static const XMLCh _TrustEngine[] = UNICODE_LITERAL_11(T,r,u,s,t,E,n,g,i,n,e);
214 static const XMLCh _type[] = UNICODE_LITERAL_4(t,y,p,e);
217 SimpleAggregationResolver::SimpleAggregationResolver(const DOMElement* e)
218 : m_log(Category::getInstance(SHIBSP_LOGCAT".AttributeResolver.SimpleAggregation")),
219 m_policyId(XMLHelper::getAttrString(e, nullptr, policyId)),
220 m_subjectMatch(XMLHelper::getAttrBool(e, false, subjectMatch)),
221 m_metadata(nullptr), m_trust(nullptr)
224 xmltooling::NDC ndc("SimpleAggregationResolver");
227 const XMLCh* aid = e ? e->getAttributeNS(nullptr, attributeId) : nullptr;
229 char* dup = XMLString::transcode(aid);
232 while (start && *start) {
233 while (*start && isspace(*start))
237 pos = strchr(start,' ');
240 m_attributeIds.push_back(start);
241 start = pos ? pos+1 : nullptr;
243 XMLString::release(&dup);
245 aid = e->getAttributeNS(nullptr, format);
250 DOMElement* child = XMLHelper::getFirstChildElement(e, _MetadataProvider);
252 string t(XMLHelper::getAttrString(child, nullptr, _type));
254 throw ConfigurationException("MetadataProvider element missing type attribute.");
255 m_log.info("building MetadataProvider of type %s...", t.c_str());
256 auto_ptr<MetadataProvider> mp(SAMLConfig::getConfig().MetadataProviderManager.newPlugin(t.c_str(), child));
258 m_metadata = mp.release();
261 child = XMLHelper::getFirstChildElement(e, _TrustEngine);
264 string t(XMLHelper::getAttrString(child, nullptr, _type));
266 throw ConfigurationException("TrustEngine element missing type attribute.");
267 m_log.info("building TrustEngine of type %s...", t.c_str());
268 m_trust = XMLToolingConfig::getConfig().TrustEngineManager.newPlugin(t.c_str(), child);
276 child = XMLHelper::getFirstChildElement(e);
278 if (child->hasChildNodes() && XMLString::equals(child->getLocalName(), Entity)) {
279 aid = child->getFirstChild()->getNodeValue();
281 auto_ptr_char taid(aid);
282 m_sources.push_back(pair<string,bool>(taid.get(),true));
285 else if (child->hasChildNodes() && XMLString::equals(child->getLocalName(), EntityReference)) {
286 aid = child->getFirstChild()->getNodeValue();
288 auto_ptr_char taid(aid);
289 m_sources.push_back(pair<string,bool>(taid.get(),false));
292 else if (XMLHelper::isNodeNamed(child, samlconstants::SAML20_NS, saml2::Attribute::LOCAL_NAME)) {
294 auto_ptr<XMLObject> obj(saml2::AttributeBuilder::buildOneFromElement(child));
295 saml2::Attribute* down = dynamic_cast<saml2::Attribute*>(obj.get());
297 m_designators.push_back(down);
301 catch (exception& ex) {
302 m_log.error("exception loading attribute designator: %s", ex.what());
305 child = XMLHelper::getNextSiblingElement(child);
309 bool SimpleAggregationResolver::doQuery(SimpleAggregationContext& ctx, const char* entityID, const NameID* name) const
312 xmltooling::NDC ndc("doQuery");
314 const Application& application = ctx.getApplication();
315 MetadataProviderCriteria mc(application, entityID, &AttributeAuthorityDescriptor::ELEMENT_QNAME, samlconstants::SAML20P_NS);
316 Locker mlocker(m_metadata);
317 const AttributeAuthorityDescriptor* AA=nullptr;
318 pair<const EntityDescriptor*,const RoleDescriptor*> mdresult =
319 (m_metadata ? m_metadata : application.getMetadataProvider())->getEntityDescriptor(mc);
320 if (!mdresult.first) {
321 m_log.warn("unable to locate metadata for provider (%s)", entityID);
324 else if (!(AA=dynamic_cast<const AttributeAuthorityDescriptor*>(mdresult.second))) {
325 m_log.warn("no SAML 2 AttributeAuthority role found in metadata for (%s)", entityID);
329 const PropertySet* relyingParty = application.getRelyingParty(mdresult.first);
330 pair<bool,bool> signedAssertions = relyingParty->getBool("requireSignedAssertions");
331 pair<bool,const char*> encryption = relyingParty->getString("encryption");
333 // Locate policy key.
334 const char* policyId = m_policyId.empty() ? application.getString("policyId").second : m_policyId.c_str();
336 // Set up policy and SOAP client.
337 auto_ptr<SecurityPolicy> policy(
338 application.getServiceProvider().getSecurityPolicyProvider()->createSecurityPolicy(application, nullptr, policyId)
341 policy->setMetadataProvider(m_metadata);
343 policy->setTrustEngine(m_trust);
344 policy->getAudiences().push_back(relyingParty->getXMLString("entityID").second);
346 MetadataCredentialCriteria mcc(*AA);
347 shibsp::SOAPClient soaper(*policy.get());
349 auto_ptr_XMLCh binding(samlconstants::SAML20_BINDING_SOAP);
350 saml2p::StatusResponseType* srt=nullptr;
351 const vector<AttributeService*>& endpoints=AA->getAttributeServices();
352 for (vector<AttributeService*>::const_iterator ep=endpoints.begin(); !srt && ep!=endpoints.end(); ++ep) {
353 if (!XMLString::equals((*ep)->getBinding(),binding.get()) || !(*ep)->getLocation())
355 auto_ptr_char loc((*ep)->getLocation());
357 auto_ptr<saml2::Subject> subject(saml2::SubjectBuilder::buildSubject());
359 // Encrypt the NameID?
360 if (encryption.first && (!strcmp(encryption.second, "true") || !strcmp(encryption.second, "back"))) {
361 auto_ptr<EncryptedID> encrypted(EncryptedIDBuilder::buildEncryptedID());
364 *(policy->getMetadataProvider()),
367 relyingParty->getXMLString("encryptionAlg").second
369 subject->setEncryptedID(encrypted.release());
372 subject->setNameID(name->cloneNameID());
375 saml2p::AttributeQuery* query = saml2p::AttributeQueryBuilder::buildAttributeQuery();
376 query->setSubject(subject.release());
377 Issuer* iss = IssuerBuilder::buildIssuer();
378 iss->setName(relyingParty->getXMLString("entityID").second);
379 query->setIssuer(iss);
380 for (vector<saml2::Attribute*>::const_iterator ad = m_designators.begin(); ad!=m_designators.end(); ++ad)
381 query->getAttributes().push_back((*ad)->cloneAttribute());
383 SAML2SOAPClient client(soaper, false);
384 client.sendSAML(query, application.getId(), mcc, loc.get());
385 srt = client.receiveSAML();
387 catch (exception& ex) {
388 m_log.error("exception during SAML query to %s: %s", loc.get(), ex.what());
394 m_log.error("unable to obtain a SAML response from attribute authority (%s)", entityID);
398 auto_ptr<saml2p::StatusResponseType> wrapper(srt);
400 saml2p::Response* response = dynamic_cast<saml2p::Response*>(srt);
402 m_log.error("message was not a samlp:Response");
405 else if (!response->getStatus() || !response->getStatus()->getStatusCode() ||
406 !XMLString::equals(response->getStatus()->getStatusCode()->getValue(), saml2p::StatusCode::SUCCESS)) {
407 m_log.error("attribute authority (%s) returned a SAML error", entityID);
411 saml2::Assertion* newtoken = nullptr;
412 const vector<saml2::Assertion*>& assertions = const_cast<const saml2p::Response*>(response)->getAssertions();
413 if (assertions.empty()) {
414 // Check for encryption.
415 const vector<saml2::EncryptedAssertion*>& encassertions =
416 const_cast<const saml2p::Response*>(response)->getEncryptedAssertions();
417 if (encassertions.empty()) {
418 m_log.warn("response from attribute authority was empty");
421 else if (encassertions.size() > 1) {
422 m_log.warn("simple resolver only supports one assertion in the query response");
425 CredentialResolver* cr=application.getCredentialResolver();
427 m_log.warn("found encrypted assertion, but no CredentialResolver was available");
431 // Attempt to decrypt it.
433 Locker credlocker(cr);
434 auto_ptr<XMLObject> tokenwrapper(encassertions.front()->decrypt(*cr, relyingParty->getXMLString("entityID").second, &mcc));
435 newtoken = dynamic_cast<saml2::Assertion*>(tokenwrapper.get());
437 tokenwrapper.release();
438 if (m_log.isDebugEnabled())
439 m_log.debugStream() << "decrypted Assertion: " << *newtoken << logging::eol;
442 catch (exception& ex) {
443 m_log.error(ex.what());
446 // Free the Response now, so we know this is a stand-alone token later.
447 delete wrapper.release();
450 // Nothing decrypted, should already be logged.
455 if (assertions.size() > 1)
456 m_log.warn("simple resolver only supports one assertion in the query response");
457 newtoken = assertions.front();
460 if (!newtoken->getSignature() && signedAssertions.first && signedAssertions.second) {
461 m_log.error("assertion unsigned, rejecting it based on signedAssertions policy");
468 // We're going to insist that the assertion issuer is the same as the peer.
469 // Reset the policy's message bits and extract them from the assertion.
471 policy->setMessageID(newtoken->getID());
472 policy->setIssueInstant(newtoken->getIssueInstantEpoch());
473 policy->setIssuer(newtoken->getIssuer());
474 policy->evaluate(*newtoken);
476 // Now we can check the security status of the policy.
477 if (!policy->isAuthenticated())
478 throw SecurityPolicyException("Security of SAML 2.0 query result not established.");
480 if (m_subjectMatch) {
481 // Check for subject match.
482 bool ownedName = false;
483 NameID* respName = newtoken->getSubject() ? newtoken->getSubject()->getNameID() : nullptr;
485 // Check for encryption.
486 EncryptedID* encname = newtoken->getSubject() ? newtoken->getSubject()->getEncryptedID() : nullptr;
488 CredentialResolver* cr=application.getCredentialResolver();
490 m_log.warn("found EncryptedID, but no CredentialResolver was available");
492 Locker credlocker(cr);
493 auto_ptr<XMLObject> decryptedID(encname->decrypt(*cr, relyingParty->getXMLString("entityID").second, &mcc));
494 respName = dynamic_cast<NameID*>(decryptedID.get());
497 decryptedID.release();
498 if (m_log.isDebugEnabled())
499 m_log.debugStream() << "decrypted NameID: " << *respName << logging::eol;
505 auto_ptr<NameID> nameIDwrapper(ownedName ? respName : nullptr);
507 if (!respName || !XMLString::equals(respName->getName(), name->getName()) ||
508 !XMLString::equals(respName->getFormat(), name->getFormat()) ||
509 !XMLString::equals(respName->getNameQualifier(), name->getNameQualifier()) ||
510 !XMLString::equals(respName->getSPNameQualifier(), name->getSPNameQualifier())) {
512 m_log.warnStream() << "ignoring Assertion without strongly matching NameID in Subject: " <<
513 *respName << logging::eol;
515 m_log.warn("ignoring Assertion without NameID in Subject");
522 catch (exception& ex) {
523 m_log.error("assertion failed policy validation: %s", ex.what());
531 wrapper.release(); // detach blows away the Response
533 ctx.getResolvedAssertions().push_back(newtoken);
535 // Finally, extract and filter the result.
537 AttributeExtractor* extractor = application.getAttributeExtractor();
539 Locker extlocker(extractor);
540 extractor->extractAttributes(application, AA, *newtoken, ctx.getResolvedAttributes());
543 AttributeFilter* filter = application.getAttributeFilter();
545 BasicFilteringContext fc(application, ctx.getResolvedAttributes(), AA, ctx.getClassRef(), ctx.getDeclRef());
546 Locker filtlocker(filter);
547 filter->filterAttributes(fc, ctx.getResolvedAttributes());
550 catch (exception& ex) {
551 m_log.error("caught exception extracting/filtering attributes from query result: %s", ex.what());
552 for_each(ctx.getResolvedAttributes().begin(), ctx.getResolvedAttributes().end(), xmltooling::cleanup<shibsp::Attribute>());
553 ctx.getResolvedAttributes().clear();
559 void SimpleAggregationResolver::resolveAttributes(ResolutionContext& ctx) const
562 xmltooling::NDC ndc("resolveAttributes");
565 SimpleAggregationContext& qctx = dynamic_cast<SimpleAggregationContext&>(ctx);
567 // First we manufacture the appropriate NameID to use.
569 for (vector<string>::const_iterator a = m_attributeIds.begin(); !n && a != m_attributeIds.end(); ++a) {
570 const Attribute* attr=nullptr;
571 if (qctx.getSession()) {
572 // Input attributes should be available via multimap.
573 pair<multimap<string,const Attribute*>::const_iterator, multimap<string,const Attribute*>::const_iterator> range =
574 qctx.getSession()->getIndexedAttributes().equal_range(*a);
575 for (; !attr && range.first != range.second; ++range.first) {
576 if (range.first->second->valueCount() > 0)
577 attr = range.first->second;
580 else if (qctx.getInputAttributes()) {
581 // Have to loop over unindexed set.
582 const vector<Attribute*>* matches = qctx.getInputAttributes();
583 for (vector<Attribute*>::const_iterator match = matches->begin(); !attr && match != matches->end(); ++match) {
584 if (*a == (*match)->getId() && (*match)->valueCount() > 0)
590 m_log.debug("using input attribute (%s) as identifier for queries", attr->getId());
591 n = NameIDBuilder::buildNameID();
592 const NameIDAttribute* down = dynamic_cast<const NameIDAttribute*>(attr);
594 // We can create a NameID directly from the source material.
595 const NameIDAttribute::Value& v = down->getValues().front();
596 XMLCh* val = fromUTF8(v.m_Name.c_str());
599 if (!v.m_Format.empty()) {
600 val = fromUTF8(v.m_Format.c_str());
604 if (!v.m_NameQualifier.empty()) {
605 val = fromUTF8(v.m_NameQualifier.c_str());
606 n->setNameQualifier(val);
609 if (!v.m_SPNameQualifier.empty()) {
610 val = fromUTF8(v.m_SPNameQualifier.c_str());
611 n->setSPNameQualifier(val);
614 if (!v.m_SPProvidedID.empty()) {
615 val = fromUTF8(v.m_SPProvidedID.c_str());
616 n->setSPProvidedID(val);
621 // We have to mock up the NameID.
622 XMLCh* val = fromUTF8(attr->getSerializedValues().front().c_str());
625 if (!m_format.empty())
626 n->setFormat(m_format.c_str());
632 if (qctx.getNameID() && m_attributeIds.empty()) {
633 m_log.debug("using authenticated NameID as identifier for queries");
636 m_log.warn("unable to resolve attributes, no suitable query identifier found");
641 auto_ptr<NameID> wrapper(n);
645 // Put initial IdP into history to prevent extra query.
646 if (qctx.getEntityID())
647 history.insert(qctx.getEntityID());
649 // We have a master loop over all the possible sources of material.
650 for (vector< pair<string,bool> >::const_iterator source = m_sources.begin(); source != m_sources.end(); ++source) {
651 if (source->second) {
652 // A literal entityID to query.
653 if (history.count(source->first) == 0) {
654 m_log.debug("issuing SAML query to (%s)", source->first.c_str());
655 doQuery(qctx, source->first.c_str(), n ? n : qctx.getNameID());
656 history.insert(source->first);
659 m_log.debug("skipping previously queried attribute source (%s)", source->first.c_str());
663 m_log.debug("using attribute sources referenced in attribute (%s)", source->first.c_str());
664 if (qctx.getSession()) {
665 // Input attributes should be available via multimap.
666 pair<multimap<string,const Attribute*>::const_iterator, multimap<string,const Attribute*>::const_iterator> range =
667 qctx.getSession()->getIndexedAttributes().equal_range(source->first);
668 for (; range.first != range.second; ++range.first) {
669 const vector<string>& links = range.first->second->getSerializedValues();
670 for (vector<string>::const_iterator link = links.begin(); link != links.end(); ++link) {
671 if (history.count(*link) == 0) {
672 m_log.debug("issuing SAML query to (%s)", link->c_str());
673 doQuery(qctx, link->c_str(), n ? n : qctx.getNameID());
674 history.insert(*link);
677 m_log.debug("skipping previously queried attribute source (%s)", link->c_str());
682 else if (qctx.getInputAttributes()) {
683 // Have to loop over unindexed set.
684 const vector<Attribute*>* matches = qctx.getInputAttributes();
685 for (vector<Attribute*>::const_iterator match = matches->begin(); match != matches->end(); ++match) {
686 if (source->first == (*match)->getId()) {
687 const vector<string>& links = (*match)->getSerializedValues();
688 for (vector<string>::const_iterator link = links.begin(); link != links.end(); ++link) {
689 if (history.count(*link) == 0) {
690 m_log.debug("issuing SAML query to (%s)", link->c_str());
691 doQuery(qctx, link->c_str(), n ? n : qctx.getNameID());
692 history.insert(*link);
695 m_log.debug("skipping previously queried attribute source (%s)", link->c_str());