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