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