https://issues.shibboleth.net/jira/browse/SSPCPP-624
[shibboleth/cpp-sp.git] / shibsp / attribute / resolver / impl / SimpleAggregationAttributeResolver.cpp
index 2d27a30..3db18e8 100644 (file)
@@ -1,17 +1,21 @@
-/*
- *  Copyright 2009-2010 Internet2
+/**
+ * Licensed to the University Corporation for Advanced Internet
+ * Development, Inc. (UCAID) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for
+ * additional information regarding copyright ownership.
  *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+ * UCAID licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the
+ * License at
  *
- *     http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
  *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific
+ * language governing permissions and limitations under the License.
  */
 
 /**
@@ -26,6 +30,7 @@
 #include "ServiceProvider.h"
 #include "SessionCache.h"
 #include "attribute/NameIDAttribute.h"
+#include "attribute/SimpleAttribute.h"
 #include "attribute/filtering/AttributeFilter.h"
 #include "attribute/filtering/BasicFilteringContext.h"
 #include "attribute/resolver/AttributeExtractor.h"
@@ -37,6 +42,9 @@
 #include "security/SecurityPolicyProvider.h"
 #include "util/SPConstants.h"
 
+#include <boost/algorithm/string.hpp>
+#include <boost/iterator/indirect_iterator.hpp>
+#include <boost/ptr_container/ptr_vector.hpp>
 #include <saml/exceptions.h>
 #include <saml/SAMLConfig.h>
 #include <saml/saml2/binding/SAML2SOAPClient.h>
@@ -47,6 +55,7 @@
 #include <xmltooling/XMLToolingConfig.h>
 #include <xmltooling/security/TrustEngine.h>
 #include <xmltooling/util/NDC.h>
+#include <xmltooling/util/URLEncoder.h>
 #include <xmltooling/util/XMLHelper.h>
 #include <xercesc/util/XMLUniDefs.hpp>
 
@@ -56,6 +65,7 @@ using namespace opensaml::saml2p;
 using namespace opensaml::saml2md;
 using namespace opensaml;
 using namespace xmltooling;
+using namespace boost;
 using namespace std;
 
 namespace shibsp {
@@ -65,17 +75,18 @@ namespace shibsp {
     public:
         SimpleAggregationContext(const Application& application, const Session& session)
             : m_app(application),
+              m_request(nullptr),
               m_session(&session),
               m_nameid(nullptr),
-              m_entityid(nullptr),
-              m_class(XMLString::transcode(session.getAuthnContextClassRef())),
-              m_decl(XMLString::transcode(session.getAuthnContextDeclRef())),
+              m_class(session.getAuthnContextClassRef()),
+              m_decl(session.getAuthnContextDeclRef()),
               m_inputTokens(nullptr),
               m_inputAttributes(nullptr) {
         }
 
         SimpleAggregationContext(
             const Application& application,
+            const GenericRequest* request=nullptr,
             const NameID* nameid=nullptr,
             const XMLCh* entityID=nullptr,
             const XMLCh* authncontext_class=nullptr,
@@ -83,11 +94,12 @@ namespace shibsp {
             const vector<const opensaml::Assertion*>* tokens=nullptr,
             const vector<shibsp::Attribute*>* attributes=nullptr
             ) : m_app(application),
+                m_request(request),
                 m_session(nullptr),
                 m_nameid(nameid),
-                m_entityid(entityID ? XMLString::transcode(entityID) : nullptr),
-                m_class(const_cast<XMLCh*>(authncontext_class)),
-                m_decl(const_cast<XMLCh*>(authncontext_decl)),
+                m_entityid(entityID),
+                m_class(authncontext_class),
+                m_decl(authncontext_decl),
                 m_inputTokens(tokens),
                 m_inputAttributes(attributes) {
         }
@@ -95,27 +107,25 @@ namespace shibsp {
         ~SimpleAggregationContext() {
             for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<shibsp::Attribute>());
             for_each(m_assertions.begin(), m_assertions.end(), xmltooling::cleanup<opensaml::Assertion>());
-            if (m_session) {
-                XMLString::release(&m_class);
-                XMLString::release(&m_decl);
-            }
-            XMLString::release(&m_entityid);
         }
 
         const Application& getApplication() const {
             return m_app;
         }
+        const GenericRequest* getRequest() const {
+            return m_request;
+        }
         const char* getEntityID() const {
-            return m_session ? m_session->getEntityID() : m_entityid;
+            return m_session ? m_session->getEntityID() : m_entityid.get();
         }
         const NameID* getNameID() const {
             return m_session ? m_session->getNameID() : m_nameid;
         }
         const XMLCh* getClassRef() const {
-            return m_class;
+            return m_class.get();
         }
         const XMLCh* getDeclRef() const {
-            return m_decl;
+            return m_decl.get();
         }
         const Session* getSession() const {
             return m_session;
@@ -135,11 +145,12 @@ namespace shibsp {
 
     private:
         const Application& m_app;
+        const GenericRequest* m_request;
         const Session* m_session;
         const NameID* m_nameid;
-        char* m_entityid;
-        XMLCh* m_class;
-        XMLCh* m_decl;
+        auto_ptr_char m_entityid;
+        auto_ptr_XMLCh m_class;
+        auto_ptr_XMLCh m_decl;
         const vector<const opensaml::Assertion*>* m_inputTokens;
         const vector<shibsp::Attribute*>* m_inputAttributes;
         vector<shibsp::Attribute*> m_attributes;
@@ -150,17 +161,28 @@ namespace shibsp {
     {
     public:
         SimpleAggregationResolver(const DOMElement* e);
-        ~SimpleAggregationResolver() {
-            delete m_trust;
-            delete m_metadata;
-            for_each(m_designators.begin(), m_designators.end(), xmltooling::cleanup<saml2::Attribute>());
-        }
+        ~SimpleAggregationResolver() {}
 
         Lockable* lock() {return this;}
         void unlock() {}
 
+        // deprecated method
+        ResolutionContext* createResolutionContext(
+            const Application& application,
+            const EntityDescriptor* issuer,
+            const XMLCh* protocol,
+            const NameID* nameid=nullptr,
+            const XMLCh* authncontext_class=nullptr,
+            const XMLCh* authncontext_decl=nullptr,
+            const vector<const opensaml::Assertion*>* tokens=nullptr,
+            const vector<shibsp::Attribute*>* attributes=nullptr
+            ) const {
+            return createResolutionContext(application, nullptr, issuer, protocol, nameid, authncontext_class, authncontext_decl, tokens, attributes);
+        }
+
         ResolutionContext* createResolutionContext(
             const Application& application,
+            const GenericRequest* request,
             const EntityDescriptor* issuer,
             const XMLCh* protocol,
             const NameID* nameid=nullptr,
@@ -170,7 +192,7 @@ namespace shibsp {
             const vector<shibsp::Attribute*>* attributes=nullptr
             ) const {
             return new SimpleAggregationContext(
-                application, nameid, (issuer ? issuer->getEntityID() : nullptr), authncontext_class, authncontext_decl, tokens, attributes
+                application, request, nameid, (issuer ? issuer->getEntityID() : nullptr), authncontext_class, authncontext_decl, tokens, attributes
                 );
         }
 
@@ -181,21 +203,25 @@ namespace shibsp {
         void resolveAttributes(ResolutionContext& ctx) const;
 
         void getAttributeIds(vector<string>& attributes) const {
-            // Nothing to do, only the extractor would actually generate them.
+            if (m_extractor)
+                m_extractor->getAttributeIds(attributes);
         }
 
     private:
-        bool doQuery(SimpleAggregationContext& ctx, const char* entityID, const NameID* name) const;
+        void doQuery(SimpleAggregationContext& ctx, const char* entityID, const NameID* name) const;
 
         Category& m_log;
         string m_policyId;
         bool m_subjectMatch;
         vector<string> m_attributeIds;
         xstring m_format;
-        MetadataProvider* m_metadata;
-        TrustEngine* m_trust;
-        vector<saml2::Attribute*> m_designators;
+        scoped_ptr<MetadataProvider> m_metadata;
+        scoped_ptr<TrustEngine> m_trust;
+        scoped_ptr<AttributeExtractor> m_extractor;
+        scoped_ptr<AttributeFilter> m_filter;
+        ptr_vector<saml2::Attribute> m_designators;
         vector< pair<string,bool> > m_sources;
+        vector<string> m_exceptionId;
     };
 
     AttributeResolver* SHIBSP_DLLLOCAL SimpleAggregationResolverFactory(const DOMElement* const & e)
@@ -203,9 +229,12 @@ namespace shibsp {
         return new SimpleAggregationResolver(e);
     }
 
+    static const XMLCh _AttributeExtractor[] =  UNICODE_LITERAL_18(A,t,t,r,i,b,u,t,e,E,x,t,r,a,c,t,o,r);
+    static const XMLCh _AttributeFilter[] =     UNICODE_LITERAL_15(A,t,t,r,i,b,u,t,e,F,i,l,t,e,r);
     static const XMLCh attributeId[] =          UNICODE_LITERAL_11(a,t,t,r,i,b,u,t,e,I,d);
     static const XMLCh Entity[] =               UNICODE_LITERAL_6(E,n,t,i,t,y);
     static const XMLCh EntityReference[] =      UNICODE_LITERAL_15(E,n,t,i,t,y,R,e,f,e,r,e,n,c,e);
+    static const XMLCh exceptionId[] =          UNICODE_LITERAL_11(e,x,c,e,p,t,i,o,n,I,d);
     static const XMLCh format[] =               UNICODE_LITERAL_6(f,o,r,m,a,t);
     static const XMLCh _MetadataProvider[] =    UNICODE_LITERAL_16(M,e,t,a,d,a,t,a,P,r,o,v,i,d,e,r);
     static const XMLCh policyId[] =             UNICODE_LITERAL_8(p,o,l,i,c,y,I,d);
@@ -217,8 +246,7 @@ namespace shibsp {
 SimpleAggregationResolver::SimpleAggregationResolver(const DOMElement* e)
     : m_log(Category::getInstance(SHIBSP_LOGCAT".AttributeResolver.SimpleAggregation")),
         m_policyId(XMLHelper::getAttrString(e, nullptr, policyId)),
-        m_subjectMatch(XMLHelper::getAttrBool(e, false, subjectMatch)),
-        m_metadata(nullptr), m_trust(nullptr)
+        m_subjectMatch(XMLHelper::getAttrBool(e, false, subjectMatch))
 {
 #ifdef _DEBUG
     xmltooling::NDC ndc("SimpleAggregationResolver");
@@ -226,51 +254,55 @@ SimpleAggregationResolver::SimpleAggregationResolver(const DOMElement* e)
 
     const XMLCh* aid = e ? e->getAttributeNS(nullptr, attributeId) : nullptr;
     if (aid && *aid) {
-        char* dup = XMLString::transcode(aid);
-        char* pos;
-        char* start = dup;
-        while (start && *start) {
-            while (*start && isspace(*start))
-                start++;
-            if (!*start)
-                break;
-            pos = strchr(start,' ');
-            if (pos)
-                *pos=0;
-            m_attributeIds.push_back(start);
-            start = pos ? pos+1 : nullptr;
-        }
-        XMLString::release(&dup);
+        auto_ptr_char dup(aid);
+        string sdup(dup.get());
+        trim(sdup);
+        split(m_attributeIds, sdup, is_space(), algorithm::token_compress_on);
 
         aid = e->getAttributeNS(nullptr, format);
         if (aid && *aid)
             m_format = aid;
     }
 
+    string exid(XMLHelper::getAttrString(e, nullptr, exceptionId));
+    if (!exid.empty())
+        m_exceptionId.push_back(exid);
+
     DOMElement* child = XMLHelper::getFirstChildElement(e, _MetadataProvider);
     if (child) {
         string t(XMLHelper::getAttrString(child, nullptr, _type));
         if (t.empty())
             throw ConfigurationException("MetadataProvider element missing type attribute.");
         m_log.info("building MetadataProvider of type %s...", t.c_str());
-        auto_ptr<MetadataProvider> mp(SAMLConfig::getConfig().MetadataProviderManager.newPlugin(t.c_str(), child));
-        mp->init();
-        m_metadata = mp.release();
+        m_metadata.reset(SAMLConfig::getConfig().MetadataProviderManager.newPlugin(t.c_str(), child));
+        m_metadata->init();
     }
 
     child = XMLHelper::getFirstChildElement(e,  _TrustEngine);
     if (child) {
-        try {
-            string t(XMLHelper::getAttrString(child, nullptr, _type));
-            if (t.empty())
-                throw ConfigurationException("TrustEngine element missing type attribute.");
-            m_log.info("building TrustEngine of type %s...", t.c_str());
-            m_trust = XMLToolingConfig::getConfig().TrustEngineManager.newPlugin(t.c_str(), child);
-        }
-        catch (exception&) {
-            delete m_metadata;
-            throw;
-        }
+        string t(XMLHelper::getAttrString(child, nullptr, _type));
+        if (t.empty())
+            throw ConfigurationException("TrustEngine element missing type attribute.");
+        m_log.info("building TrustEngine of type %s...", t.c_str());
+        m_trust.reset(XMLToolingConfig::getConfig().TrustEngineManager.newPlugin(t.c_str(), child));
+    }
+
+    child = XMLHelper::getFirstChildElement(e,  _AttributeExtractor);
+    if (child) {
+        string t(XMLHelper::getAttrString(child, nullptr, _type));
+        if (t.empty())
+            throw ConfigurationException("AttributeExtractor element missing type attribute.");
+        m_log.info("building AttributeExtractor of type %s...", t.c_str());
+        m_extractor.reset(SPConfig::getConfig().AttributeExtractorManager.newPlugin(t.c_str(), child));
+    }
+
+    child = XMLHelper::getFirstChildElement(e,  _AttributeFilter);
+    if (child) {
+        string t(XMLHelper::getAttrString(child, nullptr, _type));
+        if (t.empty())
+            throw ConfigurationException("AttributeFilter element missing type attribute.");
+        m_log.info("building AttributeFilter of type %s...", t.c_str());
+        m_filter.reset(SPConfig::getConfig().AttributeFilterManager.newPlugin(t.c_str(), child));
     }
 
     child = XMLHelper::getFirstChildElement(e);
@@ -298,7 +330,7 @@ SimpleAggregationResolver::SimpleAggregationResolver(const DOMElement* e)
                     obj.release();
                 }
             }
-            catch (exception& ex) {
+            catch (std::exception& ex) {
                 m_log.error("exception loading attribute designator: %s", ex.what());
             }
         }
@@ -306,24 +338,24 @@ SimpleAggregationResolver::SimpleAggregationResolver(const DOMElement* e)
     }
 }
 
-bool SimpleAggregationResolver::doQuery(SimpleAggregationContext& ctx, const char* entityID, const NameID* name) const
+void SimpleAggregationResolver::doQuery(SimpleAggregationContext& ctx, const char* entityID, const NameID* name) const
 {
 #ifdef _DEBUG
     xmltooling::NDC ndc("doQuery");
 #endif
     const Application& application = ctx.getApplication();
     MetadataProviderCriteria mc(application, entityID, &AttributeAuthorityDescriptor::ELEMENT_QNAME, samlconstants::SAML20P_NS);
-    Locker mlocker(m_metadata);
+    Locker mlocker(m_metadata.get());
     const AttributeAuthorityDescriptor* AA=nullptr;
     pair<const EntityDescriptor*,const RoleDescriptor*> mdresult =
-        (m_metadata ? m_metadata : application.getMetadataProvider())->getEntityDescriptor(mc);
+        (m_metadata ? m_metadata.get() : application.getMetadataProvider())->getEntityDescriptor(mc);
     if (!mdresult.first) {
         m_log.warn("unable to locate metadata for provider (%s)", entityID);
-        return false;
+        return;
     }
     else if (!(AA=dynamic_cast<const AttributeAuthorityDescriptor*>(mdresult.second))) {
         m_log.warn("no SAML 2 AttributeAuthority role found in metadata for (%s)", entityID);
-        return false;
+        return;
     }
 
     const PropertySet* relyingParty = application.getRelyingParty(mdresult.first);
@@ -334,25 +366,26 @@ bool SimpleAggregationResolver::doQuery(SimpleAggregationContext& ctx, const cha
     const char* policyId = m_policyId.empty() ? application.getString("policyId").second : m_policyId.c_str();
 
     // Set up policy and SOAP client.
-    auto_ptr<SecurityPolicy> policy(
+    scoped_ptr<SecurityPolicy> policy(
         application.getServiceProvider().getSecurityPolicyProvider()->createSecurityPolicy(application, nullptr, policyId)
         );
     if (m_metadata)
-        policy->setMetadataProvider(m_metadata);
+        policy->setMetadataProvider(m_metadata.get());
     if (m_trust)
-        policy->setTrustEngine(m_trust);
+        policy->setTrustEngine(m_trust.get());
     policy->getAudiences().push_back(relyingParty->getXMLString("entityID").second);
 
     MetadataCredentialCriteria mcc(*AA);
     shibsp::SOAPClient soaper(*policy.get());
 
     auto_ptr_XMLCh binding(samlconstants::SAML20_BINDING_SOAP);
-    saml2p::StatusResponseType* srt=nullptr;
+    auto_ptr<saml2p::StatusResponseType> srt;
     const vector<AttributeService*>& endpoints=AA->getAttributeServices();
-    for (vector<AttributeService*>::const_iterator ep=endpoints.begin(); !srt && ep!=endpoints.end(); ++ep) {
-        if (!XMLString::equals((*ep)->getBinding(),binding.get())  || !(*ep)->getLocation())
+    for (indirect_iterator<vector<AttributeService*>::const_iterator> ep = make_indirect_iterator(endpoints.begin());
+            !srt.get() && ep != make_indirect_iterator(endpoints.end()); ++ep) {
+        if (!XMLString::equals(ep->getBinding(), binding.get())  || !ep->getLocation())
             continue;
-        auto_ptr_char loc((*ep)->getLocation());
+        auto_ptr_char loc(ep->getLocation());
         try {
             auto_ptr<saml2::Subject> subject(saml2::SubjectBuilder::buildSubject());
 
@@ -366,7 +399,8 @@ bool SimpleAggregationResolver::doQuery(SimpleAggregationContext& ctx, const cha
                     false,
                     relyingParty->getXMLString("encryptionAlg").second
                     );
-                subject->setEncryptedID(encrypted.release());
+                subject->setEncryptedID(encrypted.get());
+                encrypted.release();
             }
             else {
                 subject->setNameID(name->cloneNameID());
@@ -377,38 +411,40 @@ bool SimpleAggregationResolver::doQuery(SimpleAggregationContext& ctx, const cha
             Issuer* iss = IssuerBuilder::buildIssuer();
             iss->setName(relyingParty->getXMLString("entityID").second);
             query->setIssuer(iss);
-            for (vector<saml2::Attribute*>::const_iterator ad = m_designators.begin(); ad!=m_designators.end(); ++ad)
-                query->getAttributes().push_back((*ad)->cloneAttribute());
+            for (ptr_vector<saml2::Attribute>::const_iterator ad = m_designators.begin(); ad != m_designators.end(); ++ad) {
+                auto_ptr<saml2::Attribute> adwrapper(ad->cloneAttribute());
+                query->getAttributes().push_back(adwrapper.get());
+                adwrapper.release();
+            }
 
             SAML2SOAPClient client(soaper, false);
             client.sendSAML(query, application.getId(), mcc, loc.get());
-            srt = client.receiveSAML();
+            srt.reset(client.receiveSAML());
         }
-        catch (exception& ex) {
+        catch (std::exception& ex) {
             m_log.error("exception during SAML query to %s: %s", loc.get(), ex.what());
             soaper.reset();
         }
     }
 
-    if (!srt) {
+    if (!srt.get()) {
         m_log.error("unable to obtain a SAML response from attribute authority (%s)", entityID);
-        return false;
+        throw BindingException("Unable to obtain a SAML response from attribute authority.");
     }
 
-    auto_ptr<saml2p::StatusResponseType> wrapper(srt);
-
-    saml2p::Response* response = dynamic_cast<saml2p::Response*>(srt);
+    saml2p::Response* response = dynamic_cast<saml2p::Response*>(srt.get());
     if (!response) {
         m_log.error("message was not a samlp:Response");
-        return true;
+        throw FatalProfileException("Attribute authority returned an unrecognized message.");
     }
     else if (!response->getStatus() || !response->getStatus()->getStatusCode() ||
             !XMLString::equals(response->getStatus()->getStatusCode()->getValue(), saml2p::StatusCode::SUCCESS)) {
         m_log.error("attribute authority (%s) returned a SAML error", entityID);
-        return true;
+        throw FatalProfileException("Attribute authority returned a SAML error.");
     }
 
     saml2::Assertion* newtoken = nullptr;
+    auto_ptr<saml2::Assertion> newtokenwrapper;
     const vector<saml2::Assertion*>& assertions = const_cast<const saml2p::Response*>(response)->getAssertions();
     if (assertions.empty()) {
         // Check for encryption.
@@ -416,7 +452,7 @@ bool SimpleAggregationResolver::doQuery(SimpleAggregationContext& ctx, const cha
             const_cast<const saml2p::Response*>(response)->getEncryptedAssertions();
         if (encassertions.empty()) {
             m_log.warn("response from attribute authority was empty");
-            return true;
+            return;
         }
         else if (encassertions.size() > 1) {
             m_log.warn("simple resolver only supports one assertion in the query response");
@@ -425,30 +461,34 @@ bool SimpleAggregationResolver::doQuery(SimpleAggregationContext& ctx, const cha
         CredentialResolver* cr=application.getCredentialResolver();
         if (!cr) {
             m_log.warn("found encrypted assertion, but no CredentialResolver was available");
-            return true;
+            throw FatalProfileException("Assertion was encrypted, but no decryption credentials are available.");
         }
 
+        // With this flag on, we block unauthenticated ciphertext when decrypting,
+        // unless the protocol was authenticated.
+        pair<bool,bool> authenticatedCipher = application.getBool("requireAuthenticatedEncryption");
+        if (policy->isAuthenticated())
+            authenticatedCipher.second = false;
+
         // Attempt to decrypt it.
         try {
             Locker credlocker(cr);
-            auto_ptr<XMLObject> tokenwrapper(encassertions.front()->decrypt(*cr, relyingParty->getXMLString("entityID").second, &mcc));
+            auto_ptr<XMLObject> tokenwrapper(
+                encassertions.front()->decrypt(
+                    *cr, relyingParty->getXMLString("entityID").second, &mcc, authenticatedCipher.first && authenticatedCipher.second
+                    )
+                );
             newtoken = dynamic_cast<saml2::Assertion*>(tokenwrapper.get());
             if (newtoken) {
                 tokenwrapper.release();
+                newtokenwrapper.reset(newtoken);
                 if (m_log.isDebugEnabled())
-                    m_log.debugStream() << "decrypted Assertion: " << *newtoken << logging::eol;
+                    m_log.debugStream() << "decrypted assertion: " << *newtoken << logging::eol;
             }
         }
-        catch (exception& ex) {
-            m_log.error(ex.what());
-        }
-        if (newtoken) {
-            // Free the Response now, so we know this is a stand-alone token later.
-            delete wrapper.release();
-        }
-        else {
-            // Nothing decrypted, should already be logged.
-            return true;
+        catch (std::exception& ex) {
+            m_log.error("failed to decrypt assertion: %s", ex.what());
+            throw;
         }
     }
     else {
@@ -459,9 +499,7 @@ bool SimpleAggregationResolver::doQuery(SimpleAggregationContext& ctx, const cha
 
     if (!newtoken->getSignature() && signedAssertions.first && signedAssertions.second) {
         m_log.error("assertion unsigned, rejecting it based on signedAssertions policy");
-        if (!wrapper.get())
-            delete newtoken;
-        return true;
+        throw SecurityPolicyException("Rejected unsigned assertion based on local policy.");
     }
 
     try {
@@ -479,7 +517,7 @@ bool SimpleAggregationResolver::doQuery(SimpleAggregationContext& ctx, const cha
 
         if (m_subjectMatch) {
             // Check for subject match.
-            bool ownedName = false;
+            auto_ptr<NameID> nameIDwrapper;
             NameID* respName = newtoken->getSubject() ? newtoken->getSubject()->getNameID() : nullptr;
             if (!respName) {
                 // Check for encryption.
@@ -493,8 +531,8 @@ bool SimpleAggregationResolver::doQuery(SimpleAggregationContext& ctx, const cha
                         auto_ptr<XMLObject> decryptedID(encname->decrypt(*cr, relyingParty->getXMLString("entityID").second, &mcc));
                         respName = dynamic_cast<NameID*>(decryptedID.get());
                         if (respName) {
-                            ownedName = true;
                             decryptedID.release();
+                            nameIDwrapper.reset(respName);
                             if (m_log.isDebugEnabled())
                                 m_log.debugStream() << "decrypted NameID: " << *respName << logging::eol;
                         }
@@ -502,8 +540,6 @@ bool SimpleAggregationResolver::doQuery(SimpleAggregationContext& ctx, const cha
                 }
             }
 
-            auto_ptr<NameID> nameIDwrapper(ownedName ? respName : nullptr);
-
             if (!respName || !XMLString::equals(respName->getName(), name->getName()) ||
                 !XMLString::equals(respName->getFormat(), name->getFormat()) ||
                 !XMLString::equals(respName->getNameQualifier(), name->getNameQualifier()) ||
@@ -513,47 +549,45 @@ bool SimpleAggregationResolver::doQuery(SimpleAggregationContext& ctx, const cha
                         *respName << logging::eol;
                 else
                     m_log.warn("ignoring Assertion without NameID in Subject");
-                if (!wrapper.get())
-                    delete newtoken;
-                return true;
+                return;
             }
         }
     }
-    catch (exception& ex) {
+    catch (std::exception& ex) {
         m_log.error("assertion failed policy validation: %s", ex.what());
-        if (!wrapper.get())
-            delete newtoken;
-        return true;
+        throw;
     }
 
-    if (wrapper.get()) {
+    // If the token's embedded, detach it.
+    if (!newtokenwrapper.get()) {
         newtoken->detach();
-        wrapper.release();  // detach blows away the Response
+        srt.release();  // detach blows away the Response, so avoid a double free
+        newtokenwrapper.reset(newtoken);
     }
     ctx.getResolvedAssertions().push_back(newtoken);
+    newtokenwrapper.release();
 
     // Finally, extract and filter the result.
     try {
-        AttributeExtractor* extractor = application.getAttributeExtractor();
+        AttributeExtractor* extractor = m_extractor ? m_extractor.get() : application.getAttributeExtractor();
         if (extractor) {
             Locker extlocker(extractor);
-            extractor->extractAttributes(application, AA, *newtoken, ctx.getResolvedAttributes());
+            extractor->extractAttributes(application, ctx.getRequest(), AA, *newtoken, ctx.getResolvedAttributes());
         }
 
-        AttributeFilter* filter = application.getAttributeFilter();
+        AttributeFilter* filter = m_filter ? m_filter.get() : application.getAttributeFilter();
         if (filter) {
             BasicFilteringContext fc(application, ctx.getResolvedAttributes(), AA, ctx.getClassRef(), ctx.getDeclRef());
             Locker filtlocker(filter);
             filter->filterAttributes(fc, ctx.getResolvedAttributes());
         }
     }
-    catch (exception& ex) {
+    catch (std::exception& ex) {
         m_log.error("caught exception extracting/filtering attributes from query result: %s", ex.what());
         for_each(ctx.getResolvedAttributes().begin(), ctx.getResolvedAttributes().end(), xmltooling::cleanup<shibsp::Attribute>());
         ctx.getResolvedAttributes().clear();
+        throw;
     }
-
-    return true;
 }
 
 void SimpleAggregationResolver::resolveAttributes(ResolutionContext& ctx) const
@@ -565,8 +599,8 @@ void SimpleAggregationResolver::resolveAttributes(ResolutionContext& ctx) const
     SimpleAggregationContext& qctx = dynamic_cast<SimpleAggregationContext&>(ctx);
 
     // First we manufacture the appropriate NameID to use.
-    NameID* n=nullptr;
-    for (vector<string>::const_iterator a = m_attributeIds.begin(); !n && a != m_attributeIds.end(); ++a) {
+    scoped_ptr<NameID> n;
+    for (vector<string>::const_iterator a = m_attributeIds.begin(); !n.get() && a != m_attributeIds.end(); ++a) {
         const Attribute* attr=nullptr;
         if (qctx.getSession()) {
             // Input attributes should be available via multimap.
@@ -580,48 +614,44 @@ void SimpleAggregationResolver::resolveAttributes(ResolutionContext& ctx) const
         else if (qctx.getInputAttributes()) {
             // Have to loop over unindexed set.
             const vector<Attribute*>* matches = qctx.getInputAttributes();
-            for (vector<Attribute*>::const_iterator match = matches->begin(); !attr && match != matches->end(); ++match) {
-                if (*a == (*match)->getId() && (*match)->valueCount() > 0)
-                    attr = *match;
+            for (indirect_iterator<vector<Attribute*>::const_iterator> match = make_indirect_iterator(matches->begin());
+                    !attr && match != make_indirect_iterator(matches->end()); ++match) {
+                if (*a == match->getId() && match->valueCount() > 0)
+                    attr = &(*match);
             }
         }
 
         if (attr) {
             m_log.debug("using input attribute (%s) as identifier for queries", attr->getId());
-            n = NameIDBuilder::buildNameID();
+            n.reset(NameIDBuilder::buildNameID());
             const NameIDAttribute* down = dynamic_cast<const NameIDAttribute*>(attr);
             if (down) {
                 // We can create a NameID directly from the source material.
                 const NameIDAttribute::Value& v = down->getValues().front();
-                XMLCh* val = fromUTF8(v.m_Name.c_str());
-                n->setName(val);
-                delete[] val;
+                auto_arrayptr<XMLCh> val(fromUTF8(v.m_Name.c_str()));
+                n->setName(val.get());
+
                 if (!v.m_Format.empty()) {
-                    val = fromUTF8(v.m_Format.c_str());
-                    n->setFormat(val);
-                    delete[] val;
+                    auto_arrayptr<XMLCh> format(fromUTF8(v.m_Format.c_str()));
+                    n->setFormat(format.get());
                 }
                 if (!v.m_NameQualifier.empty()) {
-                    val = fromUTF8(v.m_NameQualifier.c_str());
-                    n->setNameQualifier(val);
-                    delete[] val;
+                    auto_arrayptr<XMLCh> nq(fromUTF8(v.m_NameQualifier.c_str()));
+                    n->setNameQualifier(nq.get());
                 }
                 if (!v.m_SPNameQualifier.empty()) {
-                    val = fromUTF8(v.m_SPNameQualifier.c_str());
-                    n->setSPNameQualifier(val);
-                    delete[] val;
+                    auto_arrayptr<XMLCh> spnq(fromUTF8(v.m_SPNameQualifier.c_str()));
+                    n->setSPNameQualifier(spnq.get());
                 }
                 if (!v.m_SPProvidedID.empty()) {
-                    val = fromUTF8(v.m_SPProvidedID.c_str());
-                    n->setSPProvidedID(val);
-                    delete[] val;
+                    auto_arrayptr<XMLCh> sppid(fromUTF8(v.m_SPProvidedID.c_str()));
+                    n->setSPProvidedID(sppid.get());
                 }
             }
             else {
                 // We have to mock up the NameID.
-                XMLCh* val = fromUTF8(attr->getSerializedValues().front().c_str());
-                n->setName(val);
-                delete[] val;
+                auto_arrayptr<XMLCh> val(fromUTF8(attr->getSerializedValues().front().c_str()));
+                n->setName(val.get());
                 if (!m_format.empty())
                     n->setFormat(m_format.c_str());
             }
@@ -638,21 +668,30 @@ void SimpleAggregationResolver::resolveAttributes(ResolutionContext& ctx) const
         }
     }
 
-    auto_ptr<NameID> wrapper(n);
-
     set<string> history;
 
     // Put initial IdP into history to prevent extra query.
     if (qctx.getEntityID())
         history.insert(qctx.getEntityID());
 
+    // Prepare to track exceptions.
+    auto_ptr<SimpleAttribute> exceptAttr;
+    if (!m_exceptionId.empty())
+        exceptAttr.reset(new SimpleAttribute(m_exceptionId));
+
     // We have a master loop over all the possible sources of material.
     for (vector< pair<string,bool> >::const_iterator source = m_sources.begin(); source != m_sources.end(); ++source) {
         if (source->second) {
             // A literal entityID to query.
             if (history.count(source->first) == 0) {
                 m_log.debug("issuing SAML query to (%s)", source->first.c_str());
-                doQuery(qctx, source->first.c_str(), n ? n : qctx.getNameID());
+                try {
+                    doQuery(qctx, source->first.c_str(), n ? n.get() : qctx.getNameID());
+                }
+                catch (std::exception& ex) {
+                    if (exceptAttr.get())
+                        exceptAttr->getValues().push_back(XMLToolingConfig::getConfig().getURLEncoder()->encode(ex.what()));
+                }
                 history.insert(source->first);
             }
             else {
@@ -670,7 +709,13 @@ void SimpleAggregationResolver::resolveAttributes(ResolutionContext& ctx) const
                     for (vector<string>::const_iterator link = links.begin(); link != links.end(); ++link) {
                         if (history.count(*link) == 0) {
                             m_log.debug("issuing SAML query to (%s)", link->c_str());
-                            doQuery(qctx, link->c_str(), n ? n : qctx.getNameID());
+                            try {
+                                doQuery(qctx, link->c_str(), n ? n.get() : qctx.getNameID());
+                            }
+                            catch (std::exception& ex) {
+                                if (exceptAttr.get())
+                                    exceptAttr->getValues().push_back(XMLToolingConfig::getConfig().getURLEncoder()->encode(ex.what()));
+                            }
                             history.insert(*link);
                         }
                         else {
@@ -682,13 +727,20 @@ void SimpleAggregationResolver::resolveAttributes(ResolutionContext& ctx) const
             else if (qctx.getInputAttributes()) {
                 // Have to loop over unindexed set.
                 const vector<Attribute*>* matches = qctx.getInputAttributes();
-                for (vector<Attribute*>::const_iterator match = matches->begin(); match != matches->end(); ++match) {
-                    if (source->first == (*match)->getId()) {
-                        const vector<string>& links = (*match)->getSerializedValues();
+                for (indirect_iterator<vector<Attribute*>::const_iterator> match = make_indirect_iterator(matches->begin());
+                        match != make_indirect_iterator(matches->end()); ++match) {
+                    if (source->first == match->getId()) {
+                        const vector<string>& links = match->getSerializedValues();
                         for (vector<string>::const_iterator link = links.begin(); link != links.end(); ++link) {
                             if (history.count(*link) == 0) {
                                 m_log.debug("issuing SAML query to (%s)", link->c_str());
-                                doQuery(qctx, link->c_str(), n ? n : qctx.getNameID());
+                                try {
+                                    doQuery(qctx, link->c_str(), n ? n.get() : qctx.getNameID());
+                                }
+                                catch (std::exception& ex) {
+                                    if (exceptAttr.get())
+                                        exceptAttr->getValues().push_back(XMLToolingConfig::getConfig().getURLEncoder()->encode(ex.what()));
+                                }
                                 history.insert(*link);
                             }
                             else {
@@ -700,4 +752,9 @@ void SimpleAggregationResolver::resolveAttributes(ResolutionContext& ctx) const
             }
         }
     }
+
+    if (exceptAttr.get()) {
+        qctx.getResolvedAttributes().push_back(exceptAttr.get());
+        exceptAttr.release();
+    }
 }