Merge commit '2.5.0' into moonshot-packaging-fixes
[shibboleth/sp.git] / shibsp / attribute / resolver / impl / QueryAttributeResolver.cpp
index 2206b0a..a9e4dc4 100644 (file)
@@ -40,6 +40,8 @@
 #include "security/SecurityPolicyProvider.h"
 #include "util/SPConstants.h"
 
+#include <boost/iterator/indirect_iterator.hpp>
+#include <boost/ptr_container/ptr_vector.hpp>
 #include <saml/exceptions.h>
 #include <saml/saml1/binding/SAML1SOAPClient.h>
 #include <saml/saml1/core/Assertions.h>
@@ -63,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 {
@@ -71,7 +74,7 @@ namespace shibsp {
     {
     public:
         QueryContext(const Application& application, const Session& session)
-                : m_query(true), m_app(application), m_session(&session), m_metadata(nullptr), m_entity(nullptr), m_nameid(nullptr) {
+                : m_query(true), m_app(application), m_request(nullptr), m_session(&session), m_metadata(nullptr), m_entity(nullptr), m_nameid(nullptr) {
             m_protocol = XMLString::transcode(session.getProtocol());
             m_class = XMLString::transcode(session.getAuthnContextClassRef());
             m_decl = XMLString::transcode(session.getAuthnContextDeclRef());
@@ -79,13 +82,14 @@ namespace shibsp {
 
         QueryContext(
             const Application& application,
+            const GenericRequest* request,
             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
-            ) : m_query(true), m_app(application), m_session(nullptr), m_metadata(nullptr), m_entity(issuer),
+            ) : m_query(true), m_app(application), m_request(request), m_session(nullptr), m_metadata(nullptr), m_entity(issuer),
                 m_protocol(protocol), m_nameid(nameid), m_class(authncontext_class), m_decl(authncontext_decl) {
 
             if (tokens) {
@@ -123,6 +127,9 @@ namespace shibsp {
         const Application& getApplication() const {
             return m_app;
         }
+        const GenericRequest* getRequest() const {
+            return m_request;
+        }
         const EntityDescriptor* getEntityDescriptor() const {
             if (m_entity)
                 return m_entity;
@@ -160,6 +167,7 @@ namespace shibsp {
     private:
         bool m_query;
         const Application& m_app;
+        const GenericRequest* m_request;
         const Session* m_session;
         mutable MetadataProvider* m_metadata;
         mutable const EntityDescriptor* m_entity;
@@ -175,16 +183,28 @@ namespace shibsp {
     {
     public:
         QueryResolver(const DOMElement* e);
-        ~QueryResolver() {
-            for_each(m_SAML1Designators.begin(), m_SAML1Designators.end(), xmltooling::cleanup<AttributeDesignator>());
-            for_each(m_SAML2Designators.begin(), m_SAML2Designators.end(), xmltooling::cleanup<saml2::Attribute>());
-        }
+        ~QueryResolver() {}
 
         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);
+        }
+
         ResolutionContext* createResolutionContext(
             const Application& application,
+            const GenericRequest* request,
             const EntityDescriptor* issuer,
             const XMLCh* protocol,
             const NameID* nameid=nullptr,
@@ -193,7 +213,7 @@ namespace shibsp {
             const vector<const opensaml::Assertion*>* tokens=nullptr,
             const vector<shibsp::Attribute*>* attributes=nullptr
             ) const {
-            return new QueryContext(application,issuer,protocol,nameid,authncontext_class,authncontext_decl,tokens);
+            return new QueryContext(application, request, issuer, protocol, nameid, authncontext_class, authncontext_decl, tokens);
         }
 
         ResolutionContext* createResolutionContext(const Application& application, const Session& session) const {
@@ -213,8 +233,8 @@ namespace shibsp {
         Category& m_log;
         string m_policyId;
         bool m_subjectMatch;
-        vector<AttributeDesignator*> m_SAML1Designators;
-        vector<saml2::Attribute*> m_SAML2Designators;
+        ptr_vector<AttributeDesignator> m_SAML1Designators;
+        ptr_vector<saml2::Attribute> m_SAML2Designators;
         vector<string> m_exceptionId;
     };
 
@@ -289,20 +309,21 @@ void QueryResolver::SAML1Query(QueryContext& ctx) const
     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)
         );
     policy->getAudiences().push_back(relyingParty->getXMLString("entityID").second);
     MetadataCredentialCriteria mcc(*AA);
-    shibsp::SOAPClient soaper(*policy.get());
+    shibsp::SOAPClient soaper(*policy);
 
     auto_ptr_XMLCh binding(samlconstants::SAML1_BINDING_SOAP);
-    saml1p::Response* response=nullptr;
+    auto_ptr<saml1p::Response> response;
     const vector<AttributeService*>& endpoints=AA->getAttributeServices();
-    for (vector<AttributeService*>::const_iterator ep=endpoints.begin(); !response && 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());
+            !response.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 {
             NameIdentifier* nameid = NameIdentifierBuilder::buildNameIdentifier();
             nameid->setName(ctx.getNameID()->getName());
@@ -313,15 +334,18 @@ void QueryResolver::SAML1Query(QueryContext& ctx) const
             saml1p::AttributeQuery* query = saml1p::AttributeQueryBuilder::buildAttributeQuery();
             query->setSubject(subject);
             query->setResource(relyingParty->getXMLString("entityID").second);
-            for (vector<AttributeDesignator*>::const_iterator ad = m_SAML1Designators.begin(); ad!=m_SAML1Designators.end(); ++ad)
-                query->getAttributeDesignators().push_back((*ad)->cloneAttributeDesignator());
+            for (ptr_vector<AttributeDesignator>::const_iterator ad = m_SAML1Designators.begin(); ad != m_SAML1Designators.end(); ++ad) {
+                auto_ptr<AttributeDesignator> adwrapper(ad->cloneAttributeDesignator());
+                query->getAttributeDesignators().push_back(adwrapper.get());
+                adwrapper.release();
+            }
             Request* request = RequestBuilder::buildRequest();
             request->setAttributeQuery(query);
             request->setMinorVersion(version);
 
             SAML1SOAPClient client(soaper, false);
             client.sendSAML(request, application.getId(), mcc, loc.get());
-            response = client.receiveSAML();
+            response.reset(client.receiveSAML());
         }
         catch (exception& ex) {
             m_log.error("exception during SAML query to %s: %s", loc.get(), ex.what());
@@ -329,28 +353,25 @@ void QueryResolver::SAML1Query(QueryContext& ctx) const
         }
     }
 
-    if (!response) {
+    if (!response.get()) {
         m_log.error("unable to obtain a SAML response from attribute authority");
         throw BindingException("Unable to obtain a SAML response from attribute authority.");
     }
     else if (!response->getStatus() || !response->getStatus()->getStatusCode() || response->getStatus()->getStatusCode()->getValue()==nullptr ||
             *(response->getStatus()->getStatusCode()->getValue()) != saml1p::StatusCode::SUCCESS) {
-        delete response;
         m_log.error("attribute authority returned a SAML error");
         throw FatalProfileException("Attribute authority returned a SAML error.");
     }
 
-    const vector<saml1::Assertion*>& assertions = const_cast<const saml1p::Response*>(response)->getAssertions();
+    const vector<saml1::Assertion*>& assertions = const_cast<const saml1p::Response*>(response.get())->getAssertions();
     if (assertions.empty()) {
-        delete response;
         m_log.warn("response from attribute authority was empty");
         return;
     }
-    else if (assertions.size()>1) {
+    else if (assertions.size() > 1) {
         m_log.warn("simple resolver only supports one assertion in the query response");
     }
 
-    auto_ptr<saml1p::Response> wrapper(response);
     saml1::Assertion* newtoken = assertions.front();
 
     pair<bool,bool> signedAssertions = relyingParty->getBool("requireSignedAssertions");
@@ -378,7 +399,7 @@ void QueryResolver::SAML1Query(QueryContext& ctx) const
     }
 
     newtoken->detach();
-    wrapper.release();  // detach blows away the Response
+    response.release();  // detach blows away the Response
     ctx.getResolvedAssertions().push_back(newtoken);
 
     // Finally, extract and filter the result.
@@ -387,10 +408,11 @@ void QueryResolver::SAML1Query(QueryContext& ctx) const
         if (extractor) {
             Locker extlocker(extractor);
             const vector<saml1::AttributeStatement*>& statements = const_cast<const saml1::Assertion*>(newtoken)->getAttributeStatements();
-            for (vector<saml1::AttributeStatement*>::const_iterator s = statements.begin(); s!=statements.end(); ++s) {
+            for (indirect_iterator<vector<saml1::AttributeStatement*>::const_iterator> s = make_indirect_iterator(statements.begin());
+                    s != make_indirect_iterator(statements.end()); ++s) {
                 if (m_subjectMatch) {
                     // Check for subject match.
-                    const NameIdentifier* respName = (*s)->getSubject() ? (*s)->getSubject()->getNameIdentifier() : nullptr;
+                    const NameIdentifier* respName = s->getSubject() ? s->getSubject()->getNameIdentifier() : nullptr;
                     if (!respName || !XMLString::equals(respName->getName(), ctx.getNameID()->getName()) ||
                         !XMLString::equals(respName->getFormat(), ctx.getNameID()->getFormat()) ||
                         !XMLString::equals(respName->getNameQualifier(), ctx.getNameID()->getNameQualifier())) {
@@ -402,7 +424,7 @@ void QueryResolver::SAML1Query(QueryContext& ctx) const
                         continue;
                     }
                 }
-                extractor->extractAttributes(application, AA, *(*s), ctx.getResolvedAttributes());
+                extractor->extractAttributes(application, ctx.getRequest(), AA, *s, ctx.getResolvedAttributes());
             }
         }
 
@@ -443,20 +465,21 @@ void QueryResolver::SAML2Query(QueryContext& ctx) const
     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)
         );
     policy->getAudiences().push_back(relyingParty->getXMLString("entityID").second);
     MetadataCredentialCriteria mcc(*AA);
-    shibsp::SOAPClient soaper(*policy.get());
+    shibsp::SOAPClient soaper(*policy);
 
     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());
 
@@ -470,10 +493,13 @@ void QueryResolver::SAML2Query(QueryContext& ctx) const
                     false,
                     relyingParty->getXMLString("encryptionAlg").second
                     );
-                subject->setEncryptedID(encrypted.release());
+                subject->setEncryptedID(encrypted.get());
+                encrypted.release();
             }
             else {
-                subject->setNameID(ctx.getNameID()->cloneNameID());
+                auto_ptr<NameID> namewrapper(ctx.getNameID()->cloneNameID());
+                subject->setNameID(namewrapper.get());
+                namewrapper.release();
             }
 
             saml2p::AttributeQuery* query = saml2p::AttributeQueryBuilder::buildAttributeQuery();
@@ -481,12 +507,15 @@ void QueryResolver::SAML2Query(QueryContext& ctx) const
             Issuer* iss = IssuerBuilder::buildIssuer();
             iss->setName(relyingParty->getXMLString("entityID").second);
             query->setIssuer(iss);
-            for (vector<saml2::Attribute*>::const_iterator ad = m_SAML2Designators.begin(); ad!=m_SAML2Designators.end(); ++ad)
-                query->getAttributes().push_back((*ad)->cloneAttribute());
+            for (ptr_vector<saml2::Attribute>::const_iterator ad = m_SAML2Designators.begin(); ad != m_SAML2Designators.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) {
             m_log.error("exception during SAML query to %s: %s", loc.get(), ex.what());
@@ -494,14 +523,12 @@ void QueryResolver::SAML2Query(QueryContext& ctx) const
         }
     }
 
-    if (!srt) {
+    if (!srt.get()) {
         m_log.error("unable to obtain a SAML response from attribute authority");
         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");
         throw FatalProfileException("Attribute authority returned an unrecognized message.");
@@ -513,6 +540,7 @@ void QueryResolver::SAML2Query(QueryContext& ctx) const
     }
 
     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.
@@ -525,27 +553,36 @@ void QueryResolver::SAML2Query(QueryContext& ctx) const
             m_log.warn("simple resolver only supports one assertion in the query response");
         }
 
-        CredentialResolver* cr=application.getCredentialResolver();
+        CredentialResolver* cr = application.getCredentialResolver();
         if (!cr) {
             m_log.warn("found encrypted assertion, but no CredentialResolver was available");
             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;
-                // Free the Response now, so we know this is a stand-alone token later.
-                delete wrapper.release();
+                    m_log.debugStream() << "decrypted assertion: " << *newtoken << logging::eol;
             }
         }
         catch (exception& ex) {
-            m_log.error(ex.what());
+            m_log.error("failed to decrypt assertion: %s", ex.what());
             throw;
         }
     }
@@ -557,8 +594,6 @@ void QueryResolver::SAML2Query(QueryContext& ctx) const
 
     if (!newtoken->getSignature() && signedAssertions.first && signedAssertions.second) {
         m_log.error("assertion unsigned, rejecting it based on signedAssertions policy");
-        if (!wrapper.get())
-            delete newtoken;
         throw SecurityPolicyException("Rejected unsigned assertion based on local policy.");
     }
 
@@ -577,7 +612,7 @@ void QueryResolver::SAML2Query(QueryContext& ctx) const
 
         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.
@@ -591,8 +626,8 @@ void QueryResolver::SAML2Query(QueryContext& ctx) const
                         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;
                         }
@@ -600,8 +635,6 @@ void QueryResolver::SAML2Query(QueryContext& ctx) const
                 }
             }
 
-            auto_ptr<NameID> nameIDwrapper(ownedName ? respName : nullptr);
-
             if (!respName || !XMLString::equals(respName->getName(), ctx.getNameID()->getName()) ||
                 !XMLString::equals(respName->getFormat(), ctx.getNameID()->getFormat()) ||
                 !XMLString::equals(respName->getNameQualifier(), ctx.getNameID()->getNameQualifier()) ||
@@ -611,31 +644,30 @@ void QueryResolver::SAML2Query(QueryContext& ctx) const
                         *respName << logging::eol;
                 else
                     m_log.warn("ignoring Assertion without NameID in Subject");
-                if (!wrapper.get())
-                    delete newtoken;
                 return;
             }
         }
     }
     catch (exception& ex) {
         m_log.error("assertion failed policy validation: %s", ex.what());
-        if (!wrapper.get())
-            delete newtoken;
         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();
         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();
@@ -687,9 +719,10 @@ void QueryResolver::resolveAttributes(ResolutionContext& ctx) const
     catch (exception& ex) {
         // Already logged.
         if (!m_exceptionId.empty()) {
-            SimpleAttribute* attr = new SimpleAttribute(m_exceptionId);
+            auto_ptr<SimpleAttribute> attr(new SimpleAttribute(m_exceptionId));
             attr->getValues().push_back(XMLToolingConfig::getConfig().getURLEncoder()->encode(ex.what()));
-            qctx.getResolvedAttributes().push_back(attr);
+            qctx.getResolvedAttributes().push_back(attr.get());
+            attr.release();
         }
     }
 }