Checking wrong element when looking for attributes to ask for.
[shibboleth/cpp-sp.git] / shibsp / attribute / resolver / impl / QueryAttributeResolver.cpp
1 /*
2  *  Copyright 2001-2009 Internet2
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * QueryAttributeResolver.cpp
19  *
20  * AttributeResolver based on SAML queries.
21  */
22
23 #include "internal.h"
24 #include "Application.h"
25 #include "ServiceProvider.h"
26 #include "SessionCache.h"
27 #include "attribute/Attribute.h"
28 #include "attribute/filtering/AttributeFilter.h"
29 #include "attribute/filtering/BasicFilteringContext.h"
30 #include "attribute/resolver/AttributeExtractor.h"
31 #include "attribute/resolver/AttributeResolver.h"
32 #include "attribute/resolver/ResolutionContext.h"
33 #include "binding/SOAPClient.h"
34 #include "metadata/MetadataProviderCriteria.h"
35 #include "util/SPConstants.h"
36
37 #include <saml/exceptions.h>
38 #include <saml/binding/SecurityPolicy.h>
39 #include <saml/saml1/binding/SAML1SOAPClient.h>
40 #include <saml/saml1/core/Assertions.h>
41 #include <saml/saml1/core/Protocols.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/MetadataProvider.h>
46 #include <xmltooling/util/NDC.h>
47 #include <xmltooling/util/XMLHelper.h>
48 #include <xercesc/util/XMLUniDefs.hpp>
49
50 using namespace shibsp;
51 using namespace opensaml::saml1;
52 using namespace opensaml::saml1p;
53 using namespace opensaml::saml2;
54 using namespace opensaml::saml2p;
55 using namespace opensaml::saml2md;
56 using namespace opensaml;
57 using namespace xmltooling;
58 using namespace std;
59
60 namespace shibsp {
61
62     class SHIBSP_DLLLOCAL QueryContext : public ResolutionContext
63     {
64     public:
65         QueryContext(const Application& application, const Session& session)
66                 : m_query(true), m_app(application), m_session(&session), m_metadata(NULL), m_entity(NULL), m_nameid(NULL) {
67             m_protocol = XMLString::transcode(session.getProtocol());
68             m_class = XMLString::transcode(session.getAuthnContextClassRef());
69             m_decl = XMLString::transcode(session.getAuthnContextDeclRef());
70         }
71
72         QueryContext(
73             const Application& application,
74             const EntityDescriptor* issuer,
75             const XMLCh* protocol,
76             const NameID* nameid=NULL,
77             const XMLCh* authncontext_class=NULL,
78             const XMLCh* authncontext_decl=NULL,
79             const vector<const opensaml::Assertion*>* tokens=NULL
80             ) : m_query(true), m_app(application), m_session(NULL), m_metadata(NULL), m_entity(issuer),
81                 m_protocol(protocol), m_nameid(nameid), m_class(authncontext_class), m_decl(authncontext_decl) {
82
83             if (tokens) {
84                 for (vector<const opensaml::Assertion*>::const_iterator t = tokens->begin(); t!=tokens->end(); ++t) {
85                     const saml2::Assertion* token2 = dynamic_cast<const saml2::Assertion*>(*t);
86                     if (token2 && !token2->getAttributeStatements().empty()) {
87                         m_query = false;
88                     }
89                     else {
90                         const saml1::Assertion* token1 = dynamic_cast<const saml1::Assertion*>(*t);
91                         if (token1 && !token1->getAttributeStatements().empty()) {
92                             m_query = false;
93                         }
94                     }
95                 }
96             }
97         }
98
99         ~QueryContext() {
100             if (m_session) {
101                 XMLString::release((XMLCh**)&m_protocol);
102                 XMLString::release((XMLCh**)&m_class);
103                 XMLString::release((XMLCh**)&m_decl);
104             }
105             if (m_metadata)
106                 m_metadata->unlock();
107             for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<shibsp::Attribute>());
108             for_each(m_assertions.begin(), m_assertions.end(), xmltooling::cleanup<opensaml::Assertion>());
109         }
110
111         bool doQuery() const {
112             return m_query;
113         }
114
115         const Application& getApplication() const {
116             return m_app;
117         }
118         const EntityDescriptor* getEntityDescriptor() const {
119             if (m_entity)
120                 return m_entity;
121             if (m_session && m_session->getEntityID()) {
122                 m_metadata = m_app.getMetadataProvider(false);
123                 if (m_metadata) {
124                     m_metadata->lock();
125                     return m_entity = m_metadata->getEntityDescriptor(MetadataProviderCriteria(m_app, m_session->getEntityID())).first;
126                 }
127             }
128             return NULL;
129         }
130         const XMLCh* getProtocol() const {
131             return m_protocol;
132         }
133         const NameID* getNameID() const {
134             return m_session ? m_session->getNameID() : m_nameid;
135         }
136         const XMLCh* getClassRef() const {
137             return m_class;
138         }
139         const XMLCh* getDeclRef() const {
140             return m_decl;
141         }
142         const Session* getSession() const {
143             return m_session;
144         }
145         vector<shibsp::Attribute*>& getResolvedAttributes() {
146             return m_attributes;
147         }
148         vector<opensaml::Assertion*>& getResolvedAssertions() {
149             return m_assertions;
150         }
151
152     private:
153         bool m_query;
154         const Application& m_app;
155         const Session* m_session;
156         mutable MetadataProvider* m_metadata;
157         mutable const EntityDescriptor* m_entity;
158         const XMLCh* m_protocol;
159         const NameID* m_nameid;
160         const XMLCh* m_class;
161         const XMLCh* m_decl;
162         vector<shibsp::Attribute*> m_attributes;
163         vector<opensaml::Assertion*> m_assertions;
164     };
165
166     class SHIBSP_DLLLOCAL QueryResolver : public AttributeResolver
167     {
168     public:
169         QueryResolver(const DOMElement* e);
170         ~QueryResolver() {
171             for_each(m_SAML1Designators.begin(), m_SAML1Designators.end(), xmltooling::cleanup<AttributeDesignator>());
172             for_each(m_SAML2Designators.begin(), m_SAML2Designators.end(), xmltooling::cleanup<saml2::Attribute>());
173         }
174
175         Lockable* lock() {return this;}
176         void unlock() {}
177
178         ResolutionContext* createResolutionContext(
179             const Application& application,
180             const EntityDescriptor* issuer,
181             const XMLCh* protocol,
182             const NameID* nameid=NULL,
183             const XMLCh* authncontext_class=NULL,
184             const XMLCh* authncontext_decl=NULL,
185             const vector<const opensaml::Assertion*>* tokens=NULL,
186             const vector<shibsp::Attribute*>* attributes=NULL
187             ) const {
188             return new QueryContext(application,issuer,protocol,nameid,authncontext_class,authncontext_decl,tokens);
189         }
190
191         ResolutionContext* createResolutionContext(const Application& application, const Session& session) const {
192             return new QueryContext(application,session);
193         }
194
195         void resolveAttributes(ResolutionContext& ctx) const;
196
197         void getAttributeIds(vector<string>& attributes) const {
198             // Nothing to do, only the extractor would actually generate them.
199         }
200
201     private:
202         bool SAML1Query(QueryContext& ctx) const;
203         bool SAML2Query(QueryContext& ctx) const;
204
205         Category& m_log;
206         string m_policyId;
207         vector<AttributeDesignator*> m_SAML1Designators;
208         vector<saml2::Attribute*> m_SAML2Designators;
209     };
210
211     AttributeResolver* SHIBSP_DLLLOCAL QueryResolverFactory(const DOMElement* const & e)
212     {
213         return new QueryResolver(e);
214     }
215
216     static const XMLCh _policyId[] = UNICODE_LITERAL_8(p,o,l,i,c,y,I,d);
217 };
218
219 QueryResolver::QueryResolver(const DOMElement* e) : m_log(Category::getInstance(SHIBSP_LOGCAT".AttributeResolver.Query"))
220 {
221 #ifdef _DEBUG
222     xmltooling::NDC ndc("QueryResolver");
223 #endif
224
225     const XMLCh* pid = e ? e->getAttributeNS(NULL, _policyId) : NULL;
226     if (pid && *pid) {
227         auto_ptr_char temp(pid);
228         m_policyId = temp.get();
229     }
230
231     DOMElement* child = XMLHelper::getFirstChildElement(e);
232     while (child) {
233         try {
234             if (XMLHelper::isNodeNamed(child, samlconstants::SAML20_NS, saml2::Attribute::LOCAL_NAME)) {
235                 auto_ptr<XMLObject> obj(saml2::AttributeBuilder::buildOneFromElement(child));
236                 saml2::Attribute* down = dynamic_cast<saml2::Attribute*>(obj.get());
237                 if (down) {
238                     m_SAML2Designators.push_back(down);
239                     obj.release();
240                 }
241             }
242             else if (XMLHelper::isNodeNamed(child, samlconstants::SAML1P_NS, AttributeDesignator::LOCAL_NAME)) {
243                 auto_ptr<XMLObject> obj(AttributeDesignatorBuilder::buildOneFromElement(child));
244                 AttributeDesignator* down = dynamic_cast<AttributeDesignator*>(obj.get());
245                 if (down) {
246                     m_SAML1Designators.push_back(down);
247                     obj.release();
248                 }
249             }
250         }
251         catch (exception& ex) {
252             m_log.error("exception loading attribute designator: %s", ex.what());
253         }
254         child = XMLHelper::getNextSiblingElement(child);
255     }
256 }
257
258 bool QueryResolver::SAML1Query(QueryContext& ctx) const
259 {
260 #ifdef _DEBUG
261     xmltooling::NDC ndc("query");
262 #endif
263
264     int version = XMLString::equals(ctx.getProtocol(), samlconstants::SAML11_PROTOCOL_ENUM) ? 1 : 0;
265     const AttributeAuthorityDescriptor* AA =
266         find_if(ctx.getEntityDescriptor()->getAttributeAuthorityDescriptors(), isValidForProtocol(ctx.getProtocol()));
267     if (!AA) {
268         m_log.warn("no SAML 1.%d AttributeAuthority role found in metadata", version);
269         return false;
270     }
271
272     const Application& application = ctx.getApplication();
273     const PropertySet* relyingParty = application.getRelyingParty(ctx.getEntityDescriptor());
274
275     // Locate policy key.
276     const char* policyId = m_policyId.empty() ? application.getString("policyId").second : m_policyId.c_str();
277
278     // Access policy properties.
279     const PropertySet* settings = application.getServiceProvider().getPolicySettings(policyId);
280     pair<bool,bool> validate = settings->getBool("validate");
281
282     shibsp::SecurityPolicy policy(application, NULL, validate.first && validate.second, policyId);
283     policy.getAudiences().push_back(relyingParty->getXMLString("entityID").second);
284     MetadataCredentialCriteria mcc(*AA);
285     shibsp::SOAPClient soaper(policy);
286
287     auto_ptr_XMLCh binding(samlconstants::SAML1_BINDING_SOAP);
288     saml1p::Response* response=NULL;
289     const vector<AttributeService*>& endpoints=AA->getAttributeServices();
290     for (vector<AttributeService*>::const_iterator ep=endpoints.begin(); !response && ep!=endpoints.end(); ++ep) {
291         if (!XMLString::equals((*ep)->getBinding(),binding.get()) || !(*ep)->getLocation())
292             continue;
293         auto_ptr_char loc((*ep)->getLocation());
294         try {
295             NameIdentifier* nameid = NameIdentifierBuilder::buildNameIdentifier();
296             nameid->setName(ctx.getNameID()->getName());
297             nameid->setFormat(ctx.getNameID()->getFormat());
298             nameid->setNameQualifier(ctx.getNameID()->getNameQualifier());
299             saml1::Subject* subject = saml1::SubjectBuilder::buildSubject();
300             subject->setNameIdentifier(nameid);
301             saml1p::AttributeQuery* query = saml1p::AttributeQueryBuilder::buildAttributeQuery();
302             query->setSubject(subject);
303             query->setResource(relyingParty->getXMLString("entityID").second);
304             for (vector<AttributeDesignator*>::const_iterator ad = m_SAML1Designators.begin(); ad!=m_SAML1Designators.end(); ++ad)
305                 query->getAttributeDesignators().push_back((*ad)->cloneAttributeDesignator());
306             Request* request = RequestBuilder::buildRequest();
307             request->setAttributeQuery(query);
308             request->setMinorVersion(version);
309
310             SAML1SOAPClient client(soaper, false);
311             client.sendSAML(request, application.getId(), mcc, loc.get());
312             response = client.receiveSAML();
313         }
314         catch (exception& ex) {
315             m_log.error("exception during SAML query to %s: %s", loc.get(), ex.what());
316             soaper.reset();
317         }
318     }
319
320     if (!response) {
321         m_log.error("unable to obtain a SAML response from attribute authority");
322         return false;
323     }
324     else if (!response->getStatus() || !response->getStatus()->getStatusCode() || response->getStatus()->getStatusCode()->getValue()==NULL ||
325             *(response->getStatus()->getStatusCode()->getValue()) != saml1p::StatusCode::SUCCESS) {
326         delete response;
327         m_log.error("attribute authority returned a SAML error");
328         return true;
329     }
330
331     const vector<saml1::Assertion*>& assertions = const_cast<const saml1p::Response*>(response)->getAssertions();
332     if (assertions.empty()) {
333         delete response;
334         m_log.warn("response from attribute authority was empty");
335         return true;
336     }
337     else if (assertions.size()>1)
338         m_log.warn("simple resolver only supports one assertion in the query response");
339
340     auto_ptr<saml1p::Response> wrapper(response);
341     saml1::Assertion* newtoken = assertions.front();
342
343     pair<bool,bool> signedAssertions = relyingParty->getBool("requireSignedAssertions");
344     if (!newtoken->getSignature() && signedAssertions.first && signedAssertions.second) {
345         m_log.error("assertion unsigned, rejecting it based on signedAssertions policy");
346         return true;
347     }
348
349     try {
350         // We're going to insist that the assertion issuer is the same as the peer.
351         // Reset the policy's message bits and extract them from the assertion.
352         policy.reset(true);
353         policy.setMessageID(newtoken->getAssertionID());
354         policy.setIssueInstant(newtoken->getIssueInstantEpoch());
355         policy.setIssuer(newtoken->getIssuer());
356         policy.evaluate(*newtoken);
357
358         // Now we can check the security status of the policy.
359         if (!policy.isAuthenticated())
360             throw SecurityPolicyException("Security of SAML 1.x query result not established.");
361     }
362     catch (exception& ex) {
363         m_log.error("assertion failed policy validation: %s", ex.what());
364         return true;
365     }
366
367     newtoken->detach();
368     wrapper.release();
369     ctx.getResolvedAssertions().push_back(newtoken);
370
371     // Finally, extract and filter the result.
372     try {
373         AttributeExtractor* extractor = application.getAttributeExtractor();
374         if (extractor) {
375             Locker extlocker(extractor);
376             extractor->extractAttributes(application, AA, *newtoken, ctx.getResolvedAttributes());
377         }
378
379         AttributeFilter* filter = application.getAttributeFilter();
380         if (filter) {
381             BasicFilteringContext fc(application, ctx.getResolvedAttributes(), AA, ctx.getClassRef(), ctx.getDeclRef());
382             Locker filtlocker(filter);
383             filter->filterAttributes(fc, ctx.getResolvedAttributes());
384         }
385     }
386     catch (exception& ex) {
387         m_log.error("caught exception extracting/filtering attributes from query result: %s", ex.what());
388         for_each(ctx.getResolvedAttributes().begin(), ctx.getResolvedAttributes().end(), xmltooling::cleanup<shibsp::Attribute>());
389         ctx.getResolvedAttributes().clear();
390     }
391
392     return true;
393 }
394
395 bool QueryResolver::SAML2Query(QueryContext& ctx) const
396 {
397 #ifdef _DEBUG
398     xmltooling::NDC ndc("query");
399 #endif
400
401     const AttributeAuthorityDescriptor* AA =
402         find_if(ctx.getEntityDescriptor()->getAttributeAuthorityDescriptors(), isValidForProtocol(samlconstants::SAML20P_NS));
403     if (!AA) {
404         m_log.warn("no SAML 2 AttributeAuthority role found in metadata");
405         return false;
406     }
407
408     const Application& application = ctx.getApplication();
409     const PropertySet* relyingParty = application.getRelyingParty(ctx.getEntityDescriptor());
410
411     // Locate policy key.
412     const char* policyId = m_policyId.empty() ? application.getString("policyId").second : m_policyId.c_str();
413
414     // Access policy properties.
415     const PropertySet* settings = application.getServiceProvider().getPolicySettings(policyId);
416     pair<bool,bool> validate = settings->getBool("validate");
417
418     pair<bool,bool> signedAssertions = relyingParty->getBool("requireSignedAssertions");
419     pair<bool,const char*> encryption = relyingParty->getString("encryption");
420
421     shibsp::SecurityPolicy policy(application, NULL, validate.first && validate.second, policyId);
422     policy.getAudiences().push_back(relyingParty->getXMLString("entityID").second);
423     MetadataCredentialCriteria mcc(*AA);
424     shibsp::SOAPClient soaper(policy);
425
426     auto_ptr_XMLCh binding(samlconstants::SAML20_BINDING_SOAP);
427     saml2p::StatusResponseType* srt=NULL;
428     const vector<AttributeService*>& endpoints=AA->getAttributeServices();
429     for (vector<AttributeService*>::const_iterator ep=endpoints.begin(); !srt && ep!=endpoints.end(); ++ep) {
430         if (!XMLString::equals((*ep)->getBinding(),binding.get())  || !(*ep)->getLocation())
431             continue;
432         auto_ptr_char loc((*ep)->getLocation());
433         try {
434             auto_ptr<saml2::Subject> subject(saml2::SubjectBuilder::buildSubject());
435
436             // Encrypt the NameID?
437             if (encryption.first && (!strcmp(encryption.second, "true") || !strcmp(encryption.second, "back"))) {
438                 auto_ptr<EncryptedID> encrypted(EncryptedIDBuilder::buildEncryptedID());
439                 MetadataCredentialCriteria mcc(*AA);
440                 encrypted->encrypt(
441                     *ctx.getNameID(),
442                     *(application.getMetadataProvider()),
443                     mcc,
444                     false,
445                     relyingParty->getXMLString("encryptionAlg").second
446                     );
447                 subject->setEncryptedID(encrypted.release());
448             }
449             else {
450                 subject->setNameID(ctx.getNameID()->cloneNameID());
451             }
452
453             saml2p::AttributeQuery* query = saml2p::AttributeQueryBuilder::buildAttributeQuery();
454             query->setSubject(subject.release());
455             Issuer* iss = IssuerBuilder::buildIssuer();
456             iss->setName(relyingParty->getXMLString("entityID").second);
457             query->setIssuer(iss);
458             for (vector<saml2::Attribute*>::const_iterator ad = m_SAML2Designators.begin(); ad!=m_SAML2Designators.end(); ++ad)
459                 query->getAttributes().push_back((*ad)->cloneAttribute());
460
461             SAML2SOAPClient client(soaper, false);
462             client.sendSAML(query, application.getId(), mcc, loc.get());
463             srt = client.receiveSAML();
464         }
465         catch (exception& ex) {
466             m_log.error("exception during SAML query to %s: %s", loc.get(), ex.what());
467             soaper.reset();
468         }
469     }
470
471     if (!srt) {
472         m_log.error("unable to obtain a SAML response from attribute authority");
473         return false;
474     }
475     saml2p::Response* response = dynamic_cast<saml2p::Response*>(srt);
476     if (!response) {
477         delete srt;
478         m_log.error("message was not a samlp:Response");
479         return true;
480     }
481     else if (!response->getStatus() || !response->getStatus()->getStatusCode() ||
482             !XMLString::equals(response->getStatus()->getStatusCode()->getValue(), saml2p::StatusCode::SUCCESS)) {
483         delete srt;
484         m_log.error("attribute authority returned a SAML error");
485         return true;
486     }
487
488     const vector<saml2::Assertion*>& assertions = const_cast<const saml2p::Response*>(response)->getAssertions();
489     if (assertions.empty()) {
490         delete srt;
491         m_log.warn("response from attribute authority was empty");
492         return true;
493     }
494     else if (assertions.size()>1)
495         m_log.warn("simple resolver only supports one assertion in the query response");
496
497     auto_ptr<saml2p::StatusResponseType> wrapper(srt);
498     saml2::Assertion* newtoken = assertions.front();
499
500     if (!newtoken->getSignature() && signedAssertions.first && signedAssertions.second) {
501         m_log.error("assertion unsigned, rejecting it based on signedAssertions policy");
502         return true;
503     }
504
505     try {
506         // We're going to insist that the assertion issuer is the same as the peer.
507         // Reset the policy's message bits and extract them from the assertion.
508         policy.reset(true);
509         policy.setMessageID(newtoken->getID());
510         policy.setIssueInstant(newtoken->getIssueInstantEpoch());
511         policy.setIssuer(newtoken->getIssuer());
512         policy.evaluate(*newtoken);
513
514         // Now we can check the security status of the policy.
515         if (!policy.isAuthenticated())
516             throw SecurityPolicyException("Security of SAML 2.0 query result not established.");
517     }
518     catch (exception& ex) {
519         m_log.error("assertion failed policy validation: %s", ex.what());
520         return true;
521     }
522
523     newtoken->detach();
524     wrapper.release();
525     ctx.getResolvedAssertions().push_back(newtoken);
526
527     // Finally, extract and filter the result.
528     try {
529         AttributeExtractor* extractor = application.getAttributeExtractor();
530         if (extractor) {
531             Locker extlocker(extractor);
532             extractor->extractAttributes(application, AA, *newtoken, ctx.getResolvedAttributes());
533         }
534
535         AttributeFilter* filter = application.getAttributeFilter();
536         if (filter) {
537             BasicFilteringContext fc(application, ctx.getResolvedAttributes(), AA, ctx.getClassRef(), ctx.getDeclRef());
538             Locker filtlocker(filter);
539             filter->filterAttributes(fc, ctx.getResolvedAttributes());
540         }
541     }
542     catch (exception& ex) {
543         m_log.error("caught exception extracting/filtering attributes from query result: %s", ex.what());
544         for_each(ctx.getResolvedAttributes().begin(), ctx.getResolvedAttributes().end(), xmltooling::cleanup<shibsp::Attribute>());
545         ctx.getResolvedAttributes().clear();
546     }
547
548     return true;
549 }
550
551 void QueryResolver::resolveAttributes(ResolutionContext& ctx) const
552 {
553 #ifdef _DEBUG
554     xmltooling::NDC ndc("resolveAttributes");
555 #endif
556
557     QueryContext& qctx = dynamic_cast<QueryContext&>(ctx);
558     if (!qctx.doQuery()) {
559         m_log.debug("found AttributeStatement in input to new session, skipping query");
560         return;
561     }
562
563     if (qctx.getNameID() && qctx.getEntityDescriptor()) {
564         if (XMLString::equals(qctx.getProtocol(), samlconstants::SAML20P_NS)) {
565             m_log.debug("attempting SAML 2.0 attribute query");
566             SAML2Query(qctx);
567         }
568         else if (XMLString::equals(qctx.getProtocol(), samlconstants::SAML11_PROTOCOL_ENUM) ||
569                 XMLString::equals(qctx.getProtocol(), samlconstants::SAML10_PROTOCOL_ENUM)) {
570             m_log.debug("attempting SAML 1.x attribute query");
571             SAML1Query(qctx);
572         }
573         else
574             m_log.warn("SSO protocol does not allow for attribute query");
575     }
576     else
577         m_log.warn("can't attempt attribute query, either no NameID or no metadata to use");
578 }