2 * Copyright 2001-2007 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * QueryAttributeResolver.cpp
20 * AttributeResolver based on SAML queries.
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"
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>
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;
64 class SHIBSP_DLLLOCAL QueryContext : public ResolutionContext
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());
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) {
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()) {
93 const saml1::Assertion* token1 = dynamic_cast<const saml1::Assertion*>(*t);
94 if (token1 && !token1->getAttributeStatements().empty()) {
104 XMLString::release((XMLCh**)&m_protocol);
105 XMLString::release((XMLCh**)&m_class);
106 XMLString::release((XMLCh**)&m_decl);
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>());
114 bool doQuery() const {
118 const Application& getApplication() const {
121 const EntityDescriptor* getEntityDescriptor() const {
124 if (m_session && m_session->getEntityID()) {
125 m_metadata = m_app.getMetadataProvider(false);
128 return m_entity = m_metadata->getEntityDescriptor(MetadataProviderCriteria(m_app, m_session->getEntityID())).first;
133 const XMLCh* getProtocol() const {
136 const NameID* getNameID() const {
137 return m_session ? m_session->getNameID() : m_nameid;
139 const XMLCh* getClassRef() const {
142 const XMLCh* getDeclRef() const {
145 const Session* getSession() const {
148 vector<shibsp::Attribute*>& getResolvedAttributes() {
151 vector<opensaml::Assertion*>& getResolvedAssertions() {
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;
165 vector<shibsp::Attribute*> m_attributes;
166 vector<opensaml::Assertion*> m_assertions;
169 class SHIBSP_DLLLOCAL QueryResolver : public AttributeResolver
172 QueryResolver(const DOMElement* e);
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>());
178 Lockable* lock() {return this;}
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
191 return new QueryContext(application,issuer,protocol,nameid,authncontext_class,authncontext_decl,tokens,attributes);
194 ResolutionContext* createResolutionContext(const Application& application, const Session& session) const {
195 return new QueryContext(application,session);
198 void resolveAttributes(ResolutionContext& ctx) const;
200 void getAttributeIds(vector<string>& attributes) const {
201 // Nothing to do, only the extractor would actually generate them.
205 bool SAML1Query(QueryContext& ctx) const;
206 bool SAML2Query(QueryContext& ctx) const;
209 vector<AttributeDesignator*> m_SAML1Designators;
210 vector<saml2::Attribute*> m_SAML2Designators;
213 AttributeResolver* SHIBSP_DLLLOCAL QueryResolverFactory(const DOMElement* const & e)
215 return new QueryResolver(e);
220 QueryResolver::QueryResolver(const DOMElement* e) : m_log(Category::getInstance(SHIBSP_LOGCAT".AttributeResolver.Query"))
223 xmltooling::NDC ndc("QueryResolver");
226 DOMElement* child = XMLHelper::getFirstChildElement(e);
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());
233 m_SAML2Designators.push_back(down);
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());
241 m_SAML1Designators.push_back(down);
246 catch (exception& ex) {
247 m_log.error("exception loading attribute designator: %s", ex.what());
249 child = XMLHelper::getNextSiblingElement(child);
253 bool QueryResolver::SAML1Query(QueryContext& ctx) const
256 xmltooling::NDC ndc("query");
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()));
263 m_log.warn("no SAML 1.%d AttributeAuthority role found in metadata", version);
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);
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()) || !(*ep)->getLocation())
279 auto_ptr_char loc((*ep)->getLocation());
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);
296 SAML1SOAPClient client(soaper, false);
297 client.sendSAML(request, application.getId(), mcc, loc.get());
298 response = client.receiveSAML();
300 catch (exception& ex) {
301 m_log.error("exception during SAML query to %s: %s", loc.get(), ex.what());
307 m_log.error("unable to obtain a SAML response from attribute authority");
310 else if (!response->getStatus() || !response->getStatus()->getStatusCode() || response->getStatus()->getStatusCode()->getValue()==NULL ||
311 *(response->getStatus()->getStatusCode()->getValue()) != saml1p::StatusCode::SUCCESS) {
313 m_log.error("attribute authority returned a SAML error");
317 const vector<saml1::Assertion*>& assertions = const_cast<const saml1p::Response*>(response)->getAssertions();
318 if (assertions.empty()) {
320 m_log.warn("response from attribute authority was empty");
323 else if (assertions.size()>1)
324 m_log.warn("simple resolver only supports one assertion in the query response");
326 auto_ptr<saml1p::Response> wrapper(response);
327 saml1::Assertion* newtoken = assertions.front();
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");
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.
339 policy.setMessageID(newtoken->getAssertionID());
340 policy.setIssueInstant(newtoken->getIssueInstantEpoch());
341 policy.setIssuer(newtoken->getIssuer());
342 policy.evaluate(*newtoken);
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.");
348 // Lastly, check it over.
349 saml1::AssertionValidator tokval(relyingParty->getXMLString("entityID").second, application.getAudiences(), time(NULL));
350 tokval.validateAssertion(*newtoken);
352 catch (exception& ex) {
353 m_log.error("assertion failed policy/validation: %s", ex.what());
359 ctx.getResolvedAssertions().push_back(newtoken);
361 // Finally, extract and filter the result.
363 AttributeExtractor* extractor = application.getAttributeExtractor();
365 Locker extlocker(extractor);
366 extractor->extractAttributes(application, AA, *newtoken, ctx.getResolvedAttributes());
369 AttributeFilter* filter = application.getAttributeFilter();
371 BasicFilteringContext fc(application, ctx.getResolvedAttributes(), AA, ctx.getClassRef(), ctx.getDeclRef());
372 Locker filtlocker(filter);
373 filter->filterAttributes(fc, ctx.getResolvedAttributes());
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();
385 bool QueryResolver::SAML2Query(QueryContext& ctx) const
388 xmltooling::NDC ndc("query");
391 const AttributeAuthorityDescriptor* AA =
392 find_if(ctx.getEntityDescriptor()->getAttributeAuthorityDescriptors(), isValidForProtocol(samlconstants::SAML20P_NS));
394 m_log.warn("no SAML 2 AttributeAuthority role found in metadata");
398 const Application& application = ctx.getApplication();
399 shibsp::SecurityPolicy policy(application);
400 MetadataCredentialCriteria mcc(*AA);
401 shibsp::SOAPClient soaper(policy);
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");
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()) || !(*ep)->getLocation())
413 auto_ptr_char loc((*ep)->getLocation());
415 auto_ptr<saml2::Subject> subject(saml2::SubjectBuilder::buildSubject());
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);
423 *(application.getMetadataProvider()),
426 relyingParty->getXMLString("encryptionAlg").second
428 subject->setEncryptedID(encrypted.release());
431 subject->setNameID(ctx.getNameID()->cloneNameID());
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());
442 SAML2SOAPClient client(soaper, false);
443 client.sendSAML(query, application.getId(), mcc, loc.get());
444 srt = client.receiveSAML();
446 catch (exception& ex) {
447 m_log.error("exception during SAML query to %s: %s", loc.get(), ex.what());
453 m_log.error("unable to obtain a SAML response from attribute authority");
456 saml2p::Response* response = dynamic_cast<saml2p::Response*>(srt);
459 m_log.error("message was not a samlp:Response");
462 else if (!response->getStatus() || !response->getStatus()->getStatusCode() ||
463 !XMLString::equals(response->getStatus()->getStatusCode()->getValue(), saml2p::StatusCode::SUCCESS)) {
465 m_log.error("attribute authority returned a SAML error");
469 const vector<saml2::Assertion*>& assertions = const_cast<const saml2p::Response*>(response)->getAssertions();
470 if (assertions.empty()) {
472 m_log.warn("response from attribute authority was empty");
475 else if (assertions.size()>1)
476 m_log.warn("simple resolver only supports one assertion in the query response");
478 auto_ptr<saml2p::StatusResponseType> wrapper(srt);
479 saml2::Assertion* newtoken = assertions.front();
481 if (!newtoken->getSignature() && signedAssertions.first && signedAssertions.second) {
482 m_log.error("assertion unsigned, rejecting it based on signedAssertions policy");
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.
490 policy.setMessageID(newtoken->getID());
491 policy.setIssueInstant(newtoken->getIssueInstantEpoch());
492 policy.setIssuer(newtoken->getIssuer());
493 policy.evaluate(*newtoken);
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.");
499 // Lastly, check it over.
500 saml2::AssertionValidator tokval(relyingParty->getXMLString("entityID").second, application.getAudiences(), time(NULL));
501 tokval.validateAssertion(*newtoken);
503 catch (exception& ex) {
504 m_log.error("assertion failed policy/validation: %s", ex.what());
510 ctx.getResolvedAssertions().push_back(newtoken);
512 // Finally, extract and filter the result.
514 AttributeExtractor* extractor = application.getAttributeExtractor();
516 Locker extlocker(extractor);
517 extractor->extractAttributes(application, AA, *newtoken, ctx.getResolvedAttributes());
520 AttributeFilter* filter = application.getAttributeFilter();
522 BasicFilteringContext fc(application, ctx.getResolvedAttributes(), AA, ctx.getClassRef(), ctx.getDeclRef());
523 Locker filtlocker(filter);
524 filter->filterAttributes(fc, ctx.getResolvedAttributes());
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();
536 void QueryResolver::resolveAttributes(ResolutionContext& ctx) const
539 xmltooling::NDC ndc("resolveAttributes");
542 QueryContext& qctx = dynamic_cast<QueryContext&>(ctx);
543 if (!qctx.doQuery()) {
544 m_log.debug("found AttributeStatement in input to new session, skipping query");
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");
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");
559 m_log.warn("SSO protocol does not allow for attribute query");
562 m_log.warn("can't attempt attribute query, either no NameID or no metadata to use");