2 * The Shibboleth License, Version 1.
4 * University Corporation for Advanced Internet Development, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
14 * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution, if any, must include
17 * the following acknowledgment: "This product includes software developed by
18 * the University Corporation for Advanced Internet Development
19 * <http://www.ucaid.edu>Internet2 Project. Alternately, this acknowledegement
20 * may appear in the software itself, if and wherever such third-party
21 * acknowledgments normally appear.
23 * Neither the name of Shibboleth nor the names of its contributors, nor
24 * Internet2, nor the University Corporation for Advanced Internet Development,
25 * Inc., nor UCAID may be used to endorse or promote products derived from this
26 * software without specific prior written permission. For written permission,
27 * please contact shibboleth@shibboleth.org
29 * Products derived from this software may not be called Shibboleth, Internet2,
30 * UCAID, or the University Corporation for Advanced Internet Development, nor
31 * may Shibboleth appear in their name, without prior written permission of the
32 * University Corporation for Advanced Internet Development.
35 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
36 * AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
38 * PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK
39 * OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE.
40 * IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY
41 * CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY DIRECT,
42 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
43 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
44 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
47 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50 /* XMLMetadata.cpp - a metadata implementation that uses an XML-based registry
60 #include <sys/types.h>
63 #include <log4cpp/Category.hh>
64 #include <xercesc/util/XMLChar.hpp>
65 #include <xsec/enc/XSECCryptoException.hpp>
66 #include <xsec/enc/XSECKeyInfoResolverDefault.hpp>
67 #include <xsec/enc/OpenSSL/OpenSSLCryptoX509.hpp>
69 using namespace shibboleth;
71 using namespace log4cpp;
76 class XMLMetadataImpl : public ReloadableXMLFileImpl
79 class ContactPerson : public IContactPerson
82 ContactPerson(const DOMElement* e);
85 ContactType getType() const { return m_type; }
86 const char* getCompany() const { return m_company.get(); }
87 const char* getGivenName() const { return m_givenName.get(); }
88 const char* getSurName() const { return m_surName.get(); }
89 Iterator<string> getEmailAddresses() const { return m_emails; }
90 Iterator<string> getTelephoneNumbers() const { return m_phones; }
91 const DOMElement* getElement() const { return m_root; }
94 const DOMElement* m_root;
96 auto_ptr<char> m_givenName,m_surName,m_company;
97 vector<string> m_emails,m_phones;
100 class Organization : public IOrganization
103 Organization(const DOMElement* e);
106 const char* getName(const char* lang="en") const { return forLang(m_names,lang); }
107 const char* getDisplayName(const char* lang="en") const { return forLang(m_displays,lang); }
108 const char* getURL(const char* lang="en") const { return forLang(m_urls,lang); }
109 const DOMElement* getElement() const { return m_root; }
112 const char* forLang(const map<string,string>& m, const char* lang) const {
113 map<string,string>::const_iterator i=m.find(lang);
114 return (i==m.end()) ? NULL : i->second.c_str();
116 const DOMElement* m_root;
117 map<string,string> m_names,m_displays,m_urls;
120 class EntityDescriptor;
122 class EncryptionMethod : public XENCEncryptionMethod
125 EncryptionMethod(const DOMElement* e);
126 ~EncryptionMethod() {}
128 const XMLCh * getAlgorithm(void) const { return m_alg; }
129 const XMLCh * getDigestMethod(void) const { return m_digest; }
130 const XMLCh * getOAEPparams(void) const { return m_params; }
131 int getKeySize(void) const { return m_size; }
132 DOMElement* getElement(void) const { return const_cast<DOMElement*>(m_root); }
133 void setDigestMethod(const XMLCh * method) {throw exception();}
134 void setOAEPparams(const XMLCh * params) {throw exception();}
135 void setKeySize(int size) {throw exception();}
138 const DOMElement* m_root;
140 const XMLCh* m_digest;
141 const XMLCh* m_params;
145 class KeyDescriptor : public IKeyDescriptor
148 KeyDescriptor(const DOMElement* e);
151 KeyUse getUse() const { return m_use; }
152 DSIGKeyInfoList* getKeyInfo() const { return m_klist; }
153 saml::Iterator<const XENCEncryptionMethod*> getEncryptionMethods() const { return m_methods; }
154 const DOMElement* getElement() const { return m_root; }
157 const DOMElement* m_root;
159 mutable DSIGKeyInfoList* m_klist;
160 vector<const XENCEncryptionMethod*> m_methods;
163 class KeyAuthority : public IKeyAuthority
166 KeyAuthority(const DOMElement* e);
169 int getVerifyDepth() const { return m_depth; }
170 Iterator<DSIGKeyInfoList*> getKeyInfos() const { return m_klists; }
174 vector<DSIGKeyInfoList*> m_klists;
177 class Role : public virtual IRoleDescriptor
180 Role(const EntityDescriptor* provider, time_t validUntil, const DOMElement* e);
184 const IEntityDescriptor* getEntityDescriptor() const {return m_provider;}
185 Iterator<const XMLCh*> getProtocolSupportEnumeration() const {return m_protocolEnum;}
186 bool hasSupport(const XMLCh* protocol) const;
187 const char* getErrorURL() const {return (m_errorURL ? m_errorURL : m_provider->getErrorURL());}
188 bool isValid() const {return time(NULL) < m_validUntil;}
189 Iterator<const IKeyDescriptor*> getKeyDescriptors() const {return m_keys;}
190 const IOrganization* getOrganization() const {return m_org ? m_org : m_provider->getOrganization();}
191 Iterator<const IContactPerson*> getContactPersons() const
192 {return (m_contacts.empty() ? m_provider->getContactPersons() : m_contacts);}
193 const DOMElement* getElement() const {return m_root;}
196 vector<const XMLCh*> m_protocolEnum;
197 vector<const IKeyDescriptor*> m_keys;
200 const EntityDescriptor* m_provider;
201 const DOMElement* m_root;
202 XMLCh* m_protocolEnumCopy;
205 vector<const IContactPerson*> m_contacts;
209 class Endpoint : public virtual IEndpoint
212 Endpoint(const DOMElement* e) : m_root(e),
213 m_binding(e->getAttributeNS(NULL,L(Binding))),
214 m_location(e->getAttributeNS(NULL,SHIB_L(Location))),
215 m_resploc(e->getAttributeNS(NULL,SHIB_L(ResponseLocation))) {}
216 Endpoint(const XMLCh* binding, const XMLCh* loc)
217 : m_root(NULL), m_binding(binding), m_location(loc), m_resploc(NULL) {}
220 const XMLCh* getBinding() const { return m_binding; }
221 const XMLCh* getLocation() const { return m_location; }
222 const XMLCh* getResponseLocation() const { return m_resploc; }
223 const DOMElement* getElement() const { return m_root; }
226 const DOMElement* m_root;
227 const XMLCh* m_binding;
228 const XMLCh* m_location;
229 const XMLCh* m_resploc;
232 class IndexedEndpoint : public Endpoint, public virtual IIndexedEndpoint
235 IndexedEndpoint(const DOMElement* e) : Endpoint(e), m_index(XMLString::parseInt(e->getAttributeNS(NULL,SHIB_L(index)))) {}
236 unsigned short getIndex() const {return m_index;}
239 unsigned short m_index;
242 class EndpointManager : public IEndpointManager
245 EndpointManager() : m_soft(NULL), m_hard(NULL) {}
247 for (vector<const IEndpoint*>::iterator i=m_endpoints.begin(); i!=m_endpoints.end(); i++)
248 delete const_cast<IEndpoint*>(*i);
250 saml::Iterator<const IEndpoint*> getEndpoints() const {return m_endpoints;}
251 const IEndpoint* getDefaultEndpoint() const {
252 if (m_hard) return m_hard;
253 if (m_soft) return m_soft;
254 if (!m_endpoints.empty()) return *(m_endpoints.begin());
257 const IEndpoint* getEndpointByIndex(unsigned short index) const {
258 for (vector<const IEndpoint*>::const_iterator i=m_endpoints.begin(); i!=m_endpoints.end(); i++) {
259 const IIndexedEndpoint* temp=dynamic_cast<const IIndexedEndpoint*>(*i);
260 if (temp && index==temp->getIndex())
265 const IEndpoint* getEndpointByBinding(const XMLCh* binding) const {
266 for (vector<const IEndpoint*>::const_iterator i=m_endpoints.begin(); i!=m_endpoints.end(); i++)
267 if (!XMLString::compareString(binding,(*i)->getBinding()))
271 void add(IEndpoint* e) {
272 m_endpoints.push_back(e);
273 if (!m_hard && e->getElement()) {
274 const XMLCh* v=e->getElement()->getAttributeNS(NULL,SHIB_L(isDefault));
275 if (v && (*v==chDigit_1 || *v==chLatin_t)) // explicit default
277 else if ((!v || !*v) && !m_soft) // implicit default
280 else if (!m_hard && !m_soft) {
281 // No default yet, so this one qualifies as an implicit.
287 vector<const IEndpoint*> m_endpoints;
288 const IEndpoint* m_soft; // Soft default (not explicit)
289 const IEndpoint* m_hard; // Hard default (explicit)
292 class SSORole : public Role, public virtual ISSODescriptor
295 SSORole(const EntityDescriptor* provider, time_t validUntil, const DOMElement* e);
297 const IEndpointManager* getArtifactResolutionServiceManager() const {return &m_artifact;}
298 const IEndpointManager* getSingleLogoutServiceManager() const {return &m_logout;}
299 const IEndpointManager* getManageNameIDServiceManager() const {return &m_nameid;}
300 saml::Iterator<const XMLCh*> getNameIDFormats() const {return m_formats;}
303 EndpointManager m_artifact,m_logout,m_nameid;
304 vector<const XMLCh*> m_formats;
307 class ScopedRole : public virtual IScopedRoleDescriptor
310 ScopedRole(const DOMElement* e);
311 saml::Iterator<std::pair<const XMLCh*,bool> > getScopes() const {return m_scopes;}
314 vector<pair<const XMLCh*,bool> > m_scopes;
317 class IDPRole : public SSORole, public ScopedRole, public virtual IIDPSSODescriptor
320 IDPRole(const EntityDescriptor* provider, time_t validUntil, const DOMElement* e);
322 bool getWantAuthnRequestsSigned() const {return m_wantAuthnRequestsSigned;}
323 const IEndpointManager* getSingleSignOnServiceManager() const {return &m_sso;}
324 const IEndpointManager* getNameIDMappingServiceManager() const {return &m_mapping;}
325 const IEndpointManager* getAssertionIDRequestServiceManager() const {return &m_idreq;}
326 saml::Iterator<const XMLCh*> getAttributeProfiles() const {return m_attrprofs;}
327 saml::Iterator<const saml::SAMLAttribute*> getAttributes() const {return m_attrs;}
330 EndpointManager m_sso,m_mapping,m_idreq;
331 vector<const XMLCh*> m_attrprofs;
332 vector<const SAMLAttribute*> m_attrs;
333 bool m_wantAuthnRequestsSigned;
334 const XMLCh* m_sourceId;
335 friend class EntityDescriptor;
338 class AARole : public Role, public ScopedRole, public virtual IAttributeAuthorityDescriptor
341 AARole(const EntityDescriptor* provider, time_t validUntil, const DOMElement* e);
343 const IEndpointManager* getAttributeServiceManager() const {return &m_query;}
344 const IEndpointManager* getAssertionIDRequestServiceManager() const {return &m_idreq;}
345 saml::Iterator<const XMLCh*> getNameIDFormats() const {return m_formats;}
346 saml::Iterator<const XMLCh*> getAttributeProfiles() const {return m_attrprofs;}
347 saml::Iterator<const saml::SAMLAttribute*> getAttributes() const {return m_attrs;}
350 EndpointManager m_query,m_idreq;
351 vector<const XMLCh*> m_formats,m_attrprofs;
352 vector<const SAMLAttribute*> m_attrs;
355 class EntityDescriptor : public IExtendedEntityDescriptor
360 XMLMetadataImpl* wrapper,
361 time_t validUntil=LONG_MAX,
362 const IEntitiesDescriptor* parent=NULL
367 const XMLCh* getId() const {return m_id;}
368 bool isValid() const {return time(NULL) < m_validUntil;}
369 Iterator<const IRoleDescriptor*> getRoleDescriptors() const {return m_roles;}
370 const IIDPSSODescriptor* getIDPSSODescriptor(const XMLCh* protocol) const;
371 const ISPSSODescriptor* getSPSSODescriptor(const XMLCh* protocol) const {return NULL;}
372 const IAuthnAuthorityDescriptor* getAuthnAuthorityDescriptor(const XMLCh* protocol) const {return NULL;}
373 const IAttributeAuthorityDescriptor* getAttributeAuthorityDescriptor(const XMLCh* protocol) const;
374 const IPDPDescriptor* getPDPDescriptor(const XMLCh* protocol) const {return NULL;}
375 const IAffiliationDescriptor* getAffiliationDescriptor() const {return NULL;}
376 const IOrganization* getOrganization() const {return m_org;}
377 Iterator<const IContactPerson*> getContactPersons() const {return m_contacts;}
378 Iterator<pair<const XMLCh*,const XMLCh*> > getAdditionalMetadataLocations() const {return m_locs;}
379 const IEntitiesDescriptor* getEntitiesDescriptor() const {return m_parent;}
380 Iterator<const IKeyAuthority*> getKeyAuthorities() const {return m_keyauths;}
381 const DOMElement* getElement() const {return m_root;}
384 const char* getErrorURL() const {return m_errorURL.get();}
385 time_t getValidUntil() const {return m_validUntil;}
387 const DOMElement* m_root;
388 const IEntitiesDescriptor* m_parent;
390 auto_ptr<char> m_errorURL;
391 IOrganization* m_org;
392 vector<const IContactPerson*> m_contacts;
393 vector<const IRoleDescriptor*> m_roles;
394 vector<pair<const XMLCh*,const XMLCh*> > m_locs;
395 vector<const IKeyAuthority*> m_keyauths;
399 class EntitiesDescriptor : public IExtendedEntitiesDescriptor
404 XMLMetadataImpl* wrapper,
405 time_t validUntil=LONG_MAX,
406 const IEntitiesDescriptor* parent=NULL
408 ~EntitiesDescriptor();
410 const XMLCh* getName() const {return m_name;}
411 bool isValid() const {return time(NULL) < m_validUntil;}
412 const IEntitiesDescriptor* getEntitiesDescriptor() const {return m_parent;}
413 Iterator<const IEntitiesDescriptor*> getEntitiesDescriptors() const {return m_groups;}
414 Iterator<const IEntityDescriptor*> getEntityDescriptors() const {return m_providers;}
415 Iterator<const IKeyAuthority*> getKeyAuthorities() const {return m_keyauths;}
416 const DOMElement* getElement() const {return m_root;}
419 const DOMElement* m_root;
420 const IEntitiesDescriptor* m_parent;
422 vector<const IEntitiesDescriptor*> m_groups;
423 vector<const IEntityDescriptor*> m_providers;
424 vector<const IKeyAuthority*> m_keyauths;
428 XMLMetadataImpl(const char* pathname) : ReloadableXMLFileImpl(pathname), m_rootProvider(NULL), m_rootGroup(NULL) { init(); }
429 XMLMetadataImpl(const DOMElement* e) : ReloadableXMLFileImpl(e), m_rootProvider(NULL), m_rootGroup(NULL) { init(); }
433 typedef multimap<string,const EntityDescriptor*> sitemap_t;
436 EntityDescriptor* m_rootProvider;
437 EntitiesDescriptor* m_rootGroup;
440 class XMLMetadata : public IMetadata, public ReloadableXMLFile
443 XMLMetadata(const DOMElement* e) : ReloadableXMLFile(e), m_exclusions(true) {
444 static const XMLCh uri[] = { chLatin_u, chLatin_r, chLatin_i, chNull };
445 if (e->hasAttributeNS(NULL,uri)) {
446 // First check for explicit enablement of entities.
447 DOMNodeList* nlist=e->getElementsByTagName(SHIB_L(Include));
448 for (int i=0; nlist && i<nlist->getLength(); i++) {
449 if (nlist->item(i)->hasChildNodes()) {
450 auto_ptr_char temp(nlist->item(i)->getFirstChild()->getNodeValue());
452 m_set.insert(temp.get());
457 // If there was no explicit enablement, build a set of exclusions.
459 nlist=e->getElementsByTagName(SHIB_L(Exclude));
460 for (int j=0; nlist && j<nlist->getLength(); j++) {
461 if (nlist->item(j)->hasChildNodes()) {
462 auto_ptr_char temp(nlist->item(j)->getFirstChild()->getNodeValue());
464 m_set.insert(temp.get());
472 const IEntityDescriptor* lookup(const char* providerId, bool strict=true) const;
473 const IEntityDescriptor* lookup(const XMLCh* providerId, bool strict=true) const;
474 const IEntityDescriptor* lookup(const saml::SAMLArtifact* artifact) const;
477 virtual ReloadableXMLFileImpl* newImplementation(const char* pathname, bool first=true) const;
478 virtual ReloadableXMLFileImpl* newImplementation(const DOMElement* e, bool first=true) const;
486 IPlugIn* XMLMetadataFactory(const DOMElement* e)
488 auto_ptr<XMLMetadata> m(new XMLMetadata(e));
489 m->getImplementation();
493 ReloadableXMLFileImpl* XMLMetadata::newImplementation(const DOMElement* e, bool first) const
495 return new XMLMetadataImpl(e);
498 ReloadableXMLFileImpl* XMLMetadata::newImplementation(const char* pathname, bool first) const
500 return new XMLMetadataImpl(pathname);
503 XMLMetadataImpl::ContactPerson::ContactPerson(const DOMElement* e) : m_root(e)
505 const XMLCh* type=NULL;
507 // Old metadata or new?
508 if (saml::XML::isElementNamed(e,::XML::SHIB_NS,SHIB_L(Contact))) {
509 type=e->getAttributeNS(NULL,SHIB_L(Type));
510 m_surName=auto_ptr<char>(toUTF8(e->getAttributeNS(NULL,SHIB_L(Name))));
511 if (e->hasAttributeNS(NULL,SHIB_L(Email))) {
512 auto_ptr<char> temp(toUTF8(e->getAttributeNS(NULL,SHIB_L(Email))));
514 m_emails.push_back(temp.get());
517 else if (saml::XML::isElementNamed(e,::XML::SAML2META_NS,SHIB_L(ContactPerson))) {
518 type=e->getAttributeNS(NULL,SHIB_L(contactType));
520 e=saml::XML::getFirstChildElement(e);
522 if (saml::XML::isElementNamed(e,::XML::SAML2META_NS,SHIB_L(Company))) {
523 n=e->getFirstChild();
524 if (n) m_company=auto_ptr<char>(toUTF8(n->getNodeValue()));
526 else if (saml::XML::isElementNamed(e,::XML::SAML2META_NS,SHIB_L(GivenName))) {
527 n=e->getFirstChild();
528 if (n) m_givenName=auto_ptr<char>(toUTF8(n->getNodeValue()));
530 else if (saml::XML::isElementNamed(e,::XML::SAML2META_NS,SHIB_L(SurName))) {
531 n=e->getFirstChild();
532 if (n) m_surName=auto_ptr<char>(toUTF8(n->getNodeValue()));
534 else if (saml::XML::isElementNamed(e,::XML::SAML2META_NS,SHIB_L(EmailAddress))) {
535 n=e->getFirstChild();
537 auto_ptr<char> temp(toUTF8(n->getNodeValue()));
538 if (temp.get()) m_emails.push_back(temp.get());
541 else if (saml::XML::isElementNamed(e,::XML::SAML2META_NS,SHIB_L(TelephoneNumber))) {
542 n=e->getFirstChild();
544 auto_ptr<char> temp(toUTF8(n->getNodeValue()));
545 if (temp.get()) m_phones.push_back(temp.get());
548 e=saml::XML::getNextSiblingElement(e);
552 if (!XMLString::compareString(type,SHIB_L(technical)))
553 m_type=IContactPerson::technical;
554 else if (!XMLString::compareString(type,SHIB_L(support)))
555 m_type=IContactPerson::support;
556 else if (!XMLString::compareString(type,SHIB_L(administrative)))
557 m_type=IContactPerson::administrative;
558 else if (!XMLString::compareString(type,SHIB_L(billing)))
559 m_type=IContactPerson::billing;
560 else if (!XMLString::compareString(type,SHIB_L(other)))
561 m_type=IContactPerson::other;
564 XMLMetadataImpl::Organization::Organization(const DOMElement* e) : m_root(e)
567 e=saml::XML::getFirstChildElement(e);
569 if (saml::XML::isElementNamed(e,::XML::SAML2META_NS,SHIB_L(OrganizationName))) {
570 n=e->getFirstChild();
572 auto_ptr<char> name(toUTF8(n->getNodeValue()));
573 auto_ptr_char lang(e->getAttributeNS(saml::XML::XML_NS,L(lang)));
574 m_names[lang.get()]=name.get();
577 else if (saml::XML::isElementNamed(e,::XML::SAML2META_NS,SHIB_L(OrganizationDisplayName))) {
578 n=e->getFirstChild();
580 auto_ptr<char> display(toUTF8(n->getNodeValue()));
581 auto_ptr_char lang(e->getAttributeNS(saml::XML::XML_NS,L(lang)));
582 m_displays[lang.get()]=display.get();
585 else if (saml::XML::isElementNamed(e,::XML::SAML2META_NS,SHIB_L(OrganizationURL))) {
586 n=e->getFirstChild();
588 auto_ptr<char> url(toUTF8(n->getNodeValue()));
589 auto_ptr_char lang(e->getAttributeNS(saml::XML::XML_NS,L(lang)));
590 m_urls[lang.get()]=url.get();
593 e=saml::XML::getNextSiblingElement(e);
597 XMLMetadataImpl::EncryptionMethod::EncryptionMethod(const DOMElement* e) : m_root(e)
599 m_alg=e->getAttributeNS(NULL,SHIB_L(Algorithm));
600 e=saml::XML::getFirstChildElement(e);
602 if (saml::XML::isElementNamed(e,::XML::XMLENC_NS,SHIB_L(KeySize))) {
603 DOMNode* n=e->getFirstChild();
604 if (n) m_size=XMLString::parseInt(n->getNodeValue());
606 else if (saml::XML::isElementNamed(e,saml::XML::XMLSIG_NS,SHIB_L(DigestMethod))) {
607 DOMNode* n=e->getFirstChild();
608 if (n) m_digest=n->getNodeValue();
610 else if (saml::XML::isElementNamed(e,::XML::XMLENC_NS,SHIB_L(OAEParams))) {
611 DOMNode* n=e->getFirstChild();
612 if (n) m_params=n->getNodeValue();
614 e=saml::XML::getNextSiblingElement(e);
618 XMLMetadataImpl::KeyDescriptor::KeyDescriptor(const DOMElement* e) : m_root(e), m_use(unspecified), m_klist(NULL)
621 saml::NDC ndc("KeyDescriptor");
623 if (!XMLString::compareString(e->getAttributeNS(NULL,SHIB_L(use)),SHIB_L(encryption)))
625 else if (!XMLString::compareString(e->getAttributeNS(NULL,SHIB_L(use)),SHIB_L(signing)))
628 m_klist = new DSIGKeyInfoList(NULL);
630 // Process ds:KeyInfo
631 e=saml::XML::getFirstChildElement(e);
633 // We let XMLSec hack through anything it can. This should evolve over time, or we can
634 // plug in our own KeyResolver later...
635 DOMElement* child=saml::XML::getFirstChildElement(e);
638 if (!m_klist->addXMLKeyInfo(child)) {
639 Category::getInstance(XMLPROVIDERS_LOGCAT".Metadata").warn(
640 "skipped unsupported ds:KeyInfo child element");
643 catch (XSECCryptoException& xe) {
644 Category::getInstance(XMLPROVIDERS_LOGCAT".Metadata").error(
645 "unable to process ds:KeyInfo child element: %s",xe.getMsg());
647 child=saml::XML::getNextSiblingElement(child);
650 // Check for encryption methods.
651 e=saml::XML::getNextSiblingElement(e);
652 while (e && saml::XML::isElementNamed(e,::XML::SAML2META_NS,SHIB_L(EncryptionMethod)))
653 m_methods.push_back(new EncryptionMethod(e));
656 XMLMetadataImpl::KeyDescriptor::~KeyDescriptor()
658 for (vector<const XENCEncryptionMethod*>::iterator i=m_methods.begin(); i!=m_methods.end(); i++)
659 delete const_cast<XENCEncryptionMethod*>(*i);
663 XMLMetadataImpl::KeyAuthority::KeyAuthority(const DOMElement* e) : m_depth(1)
666 saml::NDC ndc("KeyAuthority");
668 if (e->hasAttributeNS(NULL,SHIB_L(VerifyDepth)))
669 m_depth=XMLString::parseInt(e->getAttributeNS(NULL,SHIB_L(VerifyDepth)));
671 // Process ds:KeyInfo children
672 e=saml::XML::getFirstChildElement(e,saml::XML::XMLSIG_NS,L(KeyInfo));
674 auto_ptr<DSIGKeyInfoList> klist(new DSIGKeyInfoList(NULL));
676 // We let XMLSec hack through anything it can. This should evolve over time, or we can
677 // plug in our own KeyResolver later...
678 DOMElement* child=saml::XML::getFirstChildElement(e);
681 if (!klist->addXMLKeyInfo(child)) {
682 Category::getInstance(XMLPROVIDERS_LOGCAT".Metadata").warn(
683 "skipped unresolvable ds:KeyInfo child element");
686 catch (XSECCryptoException& xe) {
687 Category::getInstance(XMLPROVIDERS_LOGCAT".Metadata").error(
688 "unable to process ds:KeyInfo child element: %s",xe.getMsg());
690 child=saml::XML::getNextSiblingElement(child);
693 if (klist->getSize()>0)
694 m_klists.push_back(klist.release());
696 Category::getInstance(XMLPROVIDERS_LOGCAT".Metadata").warn(
697 "skipping ds:KeyInfo with no resolvable child elements");
698 e=saml::XML::getNextSiblingElement(e,saml::XML::XMLSIG_NS,L(KeyInfo));
702 XMLMetadataImpl::KeyAuthority::~KeyAuthority()
704 for (vector<DSIGKeyInfoList*>::iterator i=m_klists.begin(); i!=m_klists.end(); i++)
708 XMLMetadataImpl::Role::Role(const EntityDescriptor* provider, time_t validUntil, const DOMElement* e)
709 : m_provider(provider), m_errorURL(NULL), m_protocolEnumCopy(NULL), m_org(NULL), m_validUntil(validUntil), m_root(e)
711 // Check the root element namespace. If SAML2, assume it's the std schema.
712 if (e && !XMLString::compareString(e->getNamespaceURI(),::XML::SAML2META_NS)) {
714 if (e->hasAttributeNS(NULL,SHIB_L(validUntil))) {
715 SAMLDateTime exp(e->getAttributeNS(NULL,SHIB_L(validUntil)));
717 m_validUntil=min(m_validUntil,exp.getEpoch());
720 if (e->hasAttributeNS(NULL,SHIB_L(errorURL)))
721 m_errorURL=toUTF8(e->getAttributeNS(NULL,SHIB_L(errorURL)));
723 // Chop the protocol list into pieces...assume any whitespace can appear in between.
724 m_protocolEnumCopy=XMLString::replicate(e->getAttributeNS(NULL,SHIB_L(protocolSupportEnumeration)));
725 XMLCh* temp=m_protocolEnumCopy;
726 while (temp && *temp) {
728 while (*temp && !XMLChar1_1::isWhitespace(*temp)) temp++;
731 m_protocolEnum.push_back(start);
732 while (*temp && XMLChar1_1::isWhitespace(*temp)) temp++;
735 e=saml::XML::getFirstChildElement(m_root,::XML::SAML2META_NS,SHIB_L(KeyDescriptor));
737 m_keys.push_back(new KeyDescriptor(e));
738 e=saml::XML::getNextSiblingElement(e,::XML::SAML2META_NS,SHIB_L(KeyDescriptor));
741 e=saml::XML::getFirstChildElement(m_root,::XML::SAML2META_NS,SHIB_L(Organization));
743 m_org=new Organization(e);
745 e=saml::XML::getFirstChildElement(m_root,::XML::SAML2META_NS,SHIB_L(ContactPerson));
747 m_contacts.push_back(new ContactPerson(e));
748 e=saml::XML::getNextSiblingElement(e,::XML::SAML2META_NS,SHIB_L(ContactPerson));
753 XMLMetadataImpl::Role::~Role()
757 if (m_protocolEnumCopy) XMLString::release(&m_protocolEnumCopy);
758 for (vector<const IKeyDescriptor*>::iterator i=m_keys.begin(); i!=m_keys.end(); i++)
759 delete const_cast<IKeyDescriptor*>(*i);
760 for (vector<const IContactPerson*>::iterator j=m_contacts.begin(); j!=m_contacts.end(); j++)
761 delete const_cast<IContactPerson*>(*j);
764 bool XMLMetadataImpl::Role::hasSupport(const XMLCh* protocol) const
766 Iterator<const XMLCh*> i(m_protocolEnum);
767 while (i.hasNext()) {
768 if (!XMLString::compareString(protocol,i.next()))
774 XMLMetadataImpl::SSORole::SSORole(const EntityDescriptor* provider, time_t validUntil, const DOMElement* e)
775 : Role(provider,validUntil,e)
777 // Check the root element namespace. If SAML2, assume it's the std schema.
778 if (!XMLString::compareString(e->getNamespaceURI(),::XML::SAML2META_NS)) {
780 DOMNodeList* nlist=e->getElementsByTagNameNS(::XML::SAML2META_NS,SHIB_L(ArtifactResolutionService));
781 for (i=0; nlist && i<nlist->getLength(); i++)
782 m_artifact.add(new IndexedEndpoint(static_cast<DOMElement*>(nlist->item(i))));
784 nlist=e->getElementsByTagNameNS(::XML::SAML2META_NS,SHIB_L(SingleLogoutService));
785 for (i=0; nlist && i<nlist->getLength(); i++)
786 m_logout.add(new Endpoint(static_cast<DOMElement*>(nlist->item(i))));
788 nlist=e->getElementsByTagNameNS(::XML::SAML2META_NS,SHIB_L(ManageNameIDService));
789 for (i=0; nlist && i<nlist->getLength(); i++)
790 m_nameid.add(new Endpoint(static_cast<DOMElement*>(nlist->item(i))));
792 nlist=e->getElementsByTagNameNS(::XML::SAML2META_NS,SHIB_L(NameIDFormat));
793 for (i=0; nlist && i<nlist->getLength(); i++) {
794 DOMNode* n=nlist->item(i)->getFirstChild();
795 if (n) m_formats.push_back(n->getNodeValue());
799 // For old style, we just do SAML 1.1 compatibility with Shib handles.
800 m_protocolEnum.push_back(saml::XML::SAML11_PROTOCOL_ENUM);
801 m_formats.push_back(shibboleth::Constants::SHIB_NAMEID_FORMAT_URI);
805 XMLMetadataImpl::ScopedRole::ScopedRole(const DOMElement* e)
807 // Check the root element namespace. If SAML2, assume it's the std schema.
808 DOMNodeList* nlist=NULL;
809 if (!XMLString::compareString(e->getNamespaceURI(),::XML::SAML2META_NS)) {
810 e=saml::XML::getFirstChildElement(e,::XML::SAML2META_NS,SHIB_L(Extensions));
811 nlist=e->getElementsByTagNameNS(::XML::SHIBMETA_NS,SHIB_L(Scope));
814 nlist=e->getElementsByTagNameNS(::XML::SHIB_NS,SHIB_L(Domain));
817 for (int i=0; nlist && i < nlist->getLength(); i++) {
818 const XMLCh* dom=(nlist->item(i)->hasChildNodes()) ? nlist->item(i)->getFirstChild()->getNodeValue() : NULL;
820 const XMLCh* regexp=static_cast<DOMElement*>(nlist->item(i))->getAttributeNS(NULL,SHIB_L(regexp));
822 pair<const XMLCh*,bool>(dom,(regexp && (*regexp==chLatin_t || *regexp==chDigit_1)))
828 XMLMetadataImpl::IDPRole::IDPRole(const EntityDescriptor* provider, time_t validUntil, const DOMElement* e)
829 : SSORole(provider,validUntil,e), ScopedRole(e), m_wantAuthnRequestsSigned(false), m_sourceId(NULL)
831 // Check the root element namespace. If SAML2, assume it's the std schema.
832 if (!XMLString::compareString(e->getNamespaceURI(),::XML::SAML2META_NS)) {
833 const XMLCh* flag=e->getAttributeNS(NULL,SHIB_L(WantAuthnRequestsSigned));
834 m_wantAuthnRequestsSigned=(flag && (*flag==chDigit_1 || *flag==chLatin_t));
836 // Check for SourceID extension.
837 DOMElement* ext=saml::XML::getFirstChildElement(e,::XML::SAML2META_NS,SHIB_L(Extensions));
839 ext=saml::XML::getFirstChildElement(ext,saml::XML::SAML_ARTIFACT_SOURCEID,SHIB_L(SourceID));
840 if (ext && ext->hasChildNodes())
841 m_sourceId=ext->getFirstChild()->getNodeValue();
845 DOMNodeList* nlist=e->getElementsByTagNameNS(::XML::SAML2META_NS,SHIB_L(SingleSignOnService));
846 for (i=0; nlist && i<nlist->getLength(); i++)
847 m_sso.add(new Endpoint(static_cast<DOMElement*>(nlist->item(i))));
849 nlist=e->getElementsByTagNameNS(::XML::SAML2META_NS,SHIB_L(NameIDMappingService));
850 for (i=0; nlist && i<nlist->getLength(); i++)
851 m_mapping.add(new Endpoint(static_cast<DOMElement*>(nlist->item(i))));
853 nlist=e->getElementsByTagNameNS(::XML::SAML2META_NS,SHIB_L(AssertionIDRequestService));
854 for (i=0; nlist && i<nlist->getLength(); i++)
855 m_idreq.add(new Endpoint(static_cast<DOMElement*>(nlist->item(i))));
857 nlist=e->getElementsByTagNameNS(::XML::SAML2META_NS,SHIB_L(AttributeProfile));
858 for (i=0; nlist && i<nlist->getLength(); i++) {
859 DOMNode* n=nlist->item(i)->getFirstChild();
860 if (n) m_attrprofs.push_back(n->getNodeValue());
863 nlist=e->getElementsByTagNameNS(::XML::SAML2ASSERT_NS,L(Attribute));
864 for (i=0; nlist && i<nlist->getLength(); i++) {
865 // For now, we need to convert these to plain SAML 1.1 attributes.
866 DOMElement* src=static_cast<DOMElement*>(nlist->item(i));
867 DOMElement* copy=e->getOwnerDocument()->createElementNS(saml::XML::SAML_NS,L(Attribute));
868 copy->setAttributeNS(NULL,L(AttributeName),src->getAttributeNS(NULL,SHIB_L(Name)));
869 copy->setAttributeNS(NULL,L(AttributeNamespace),src->getAttributeNS(NULL,SHIB_L(NameFormat)));
870 src=saml::XML::getFirstChildElement(src,::XML::SAML2ASSERT_NS,L(AttributeValue));
872 src=saml::XML::getNextSiblingElement(src,::XML::SAML2ASSERT_NS,L(AttributeValue));
873 DOMElement* val=e->getOwnerDocument()->createElementNS(saml::XML::SAML_NS,L(AttributeValue));
874 DOMNamedNodeMap* attrs = src->getAttributes();
875 for (int j=0; j<attrs->getLength(); j++)
876 val->setAttributeNodeNS(static_cast<DOMAttr*>(e->getOwnerDocument()->importNode(attrs->item(j),true)));
877 while (src->hasChildNodes())
878 val->appendChild(src->getFirstChild());
879 copy->appendChild(val);
881 m_attrs.push_back(SAMLAttribute::getInstance(copy));
885 m_attrprofs.push_back(Constants::SHIB_ATTRIBUTE_NAMESPACE_URI);
887 DOMNodeList* nlist=e->getElementsByTagNameNS(::XML::SHIB_NS,SHIB_L(HandleService));
888 for (i=0; nlist && i<nlist->getLength(); i++) {
889 // Manufacture an endpoint for the "Shib" binding.
891 new Endpoint(Constants::SHIB_AUTHNREQUEST_PROFILE_URI,static_cast<DOMElement*>(nlist->item(i))->getAttributeNS(NULL,SHIB_L(Location)))
894 // We're going to "mock up" a KeyDescriptor that contains the specified Name as a ds:KeyName.
895 DOMElement* kd=e->getOwnerDocument()->createElementNS(::XML::SAML2META_NS,SHIB_L(KeyDescriptor));
896 DOMElement* ki=e->getOwnerDocument()->createElementNS(saml::XML::XMLSIG_NS,L(KeyInfo));
897 DOMElement* kn=e->getOwnerDocument()->createElementNS(saml::XML::XMLSIG_NS,SHIB_L(KeyName));
899 e->getOwnerDocument()->createTextNode(
900 static_cast<DOMElement*>(nlist->item(i))->getAttributeNS(NULL,SHIB_L(Name))
905 kd->setAttributeNS(NULL,SHIB_L(use),SHIB_L(signing));
906 m_keys.push_back(new KeyDescriptor(kd));
911 XMLMetadataImpl::IDPRole::~IDPRole()
913 for (vector<const SAMLAttribute*>::iterator i=m_attrs.begin(); i!=m_attrs.end(); i++)
914 delete const_cast<SAMLAttribute*>(*i);
917 XMLMetadataImpl::AARole::AARole(const EntityDescriptor* provider, time_t validUntil, const DOMElement* e)
918 : Role(provider,validUntil,e), ScopedRole(e)
920 // Check the root element namespace. If SAML2, assume it's the std schema.
921 if (!XMLString::compareString(e->getNamespaceURI(),::XML::SAML2META_NS)) {
923 DOMNodeList* nlist=e->getElementsByTagNameNS(::XML::SAML2META_NS,SHIB_L(AttributeService));
924 for (i=0; nlist && i<nlist->getLength(); i++)
925 m_query.add(new Endpoint(static_cast<DOMElement*>(nlist->item(i))));
927 nlist=e->getElementsByTagNameNS(::XML::SAML2META_NS,SHIB_L(AssertionIDRequestService));
928 for (i=0; nlist && i<nlist->getLength(); i++)
929 m_idreq.add(new Endpoint(static_cast<DOMElement*>(nlist->item(i))));
931 nlist=e->getElementsByTagNameNS(::XML::SAML2META_NS,SHIB_L(NameIDFormat));
932 for (i=0; nlist && i<nlist->getLength(); i++) {
933 DOMNode* n=nlist->item(i)->getFirstChild();
934 if (n) m_formats.push_back(n->getNodeValue());
937 nlist=e->getElementsByTagNameNS(::XML::SAML2META_NS,SHIB_L(AttributeProfile));
938 for (i=0; nlist && i<nlist->getLength(); i++) {
939 DOMNode* n=nlist->item(i)->getFirstChild();
940 if (n) m_attrprofs.push_back(n->getNodeValue());
943 nlist=e->getElementsByTagNameNS(::XML::SAML2ASSERT_NS,L(Attribute));
944 for (i=0; nlist && i<nlist->getLength(); i++) {
945 // For now, we need to convert these to plain SAML 1.1 attributes.
946 DOMElement* src=static_cast<DOMElement*>(nlist->item(i));
947 DOMElement* copy=e->getOwnerDocument()->createElementNS(saml::XML::SAML_NS,L(Attribute));
948 copy->setAttributeNS(NULL,L(AttributeName),src->getAttributeNS(NULL,SHIB_L(Name)));
949 copy->setAttributeNS(NULL,L(AttributeNamespace),src->getAttributeNS(NULL,SHIB_L(NameFormat)));
950 src=saml::XML::getFirstChildElement(src,::XML::SAML2ASSERT_NS,L(AttributeValue));
952 src=saml::XML::getNextSiblingElement(src,::XML::SAML2ASSERT_NS,L(AttributeValue));
953 DOMElement* val=e->getOwnerDocument()->createElementNS(saml::XML::SAML_NS,L(AttributeValue));
954 DOMNamedNodeMap* attrs = src->getAttributes();
955 for (int j=0; j<attrs->getLength(); j++)
956 val->setAttributeNodeNS(static_cast<DOMAttr*>(e->getOwnerDocument()->importNode(attrs->item(j),true)));
957 while (src->hasChildNodes())
958 val->appendChild(src->getFirstChild());
959 copy->appendChild(val);
961 m_attrs.push_back(SAMLAttribute::getInstance(copy));
965 // For old style, we just do SAML 1.1 compatibility with Shib handles.
966 m_protocolEnum.push_back(saml::XML::SAML11_PROTOCOL_ENUM);
967 m_formats.push_back(Constants::SHIB_NAMEID_FORMAT_URI);
968 m_attrprofs.push_back(Constants::SHIB_ATTRIBUTE_NAMESPACE_URI);
970 DOMNodeList* nlist=e->getElementsByTagNameNS(::XML::SHIB_NS,SHIB_L(AttributeAuthority));
971 for (i=0; nlist && i<nlist->getLength(); i++) {
972 // Manufacture an endpoint for the SOAP binding.
976 static_cast<DOMElement*>(nlist->item(i))->getAttributeNS(NULL,SHIB_L(Location))
980 // We're going to "mock up" a KeyDescriptor that contains the specified Name as a ds:KeyName.
981 DOMElement* kd=e->getOwnerDocument()->createElementNS(::XML::SAML2META_NS,SHIB_L(KeyDescriptor));
982 DOMElement* ki=e->getOwnerDocument()->createElementNS(saml::XML::XMLSIG_NS,L(KeyInfo));
983 DOMElement* kn=e->getOwnerDocument()->createElementNS(saml::XML::XMLSIG_NS,SHIB_L(KeyName));
985 e->getOwnerDocument()->createTextNode(
986 static_cast<DOMElement*>(nlist->item(i))->getAttributeNS(NULL,SHIB_L(Name))
991 m_keys.push_back(new KeyDescriptor(kd));
996 XMLMetadataImpl::AARole::~AARole()
998 for (vector<const SAMLAttribute*>::iterator i=m_attrs.begin(); i!=m_attrs.end(); i++)
999 delete const_cast<SAMLAttribute*>(*i);
1002 XMLMetadataImpl::EntityDescriptor::EntityDescriptor(
1003 const DOMElement* e, XMLMetadataImpl* wrapper, time_t validUntil, const IEntitiesDescriptor* parent
1004 ) : m_root(e), m_parent(parent), m_org(NULL), m_validUntil(validUntil)
1006 // Check the root element namespace. If SAML2, assume it's the std schema.
1007 if (!XMLString::compareString(e->getNamespaceURI(),::XML::SAML2META_NS)) {
1008 m_id=e->getAttributeNS(NULL,SHIB_L(entityID));
1010 if (e->hasAttributeNS(NULL,SHIB_L(validUntil))) {
1011 SAMLDateTime exp(e->getAttributeNS(NULL,SHIB_L(validUntil)));
1012 exp.parseDateTime();
1013 m_validUntil=min(validUntil,exp.getEpoch());
1016 DOMElement* child=saml::XML::getFirstChildElement(e);
1018 // Process the various kinds of children that we care about...
1019 if (saml::XML::isElementNamed(e,::XML::SAML2META_NS,SHIB_L(Extensions))) {
1020 DOMElement* ext = saml::XML::getFirstChildElement(child,::XML::SHIBMETA_NS,SHIB_L(KeyAuthority));
1022 m_keyauths.push_back(new KeyAuthority(ext));
1023 ext = saml::XML::getNextSiblingElement(ext,::XML::SHIBMETA_NS,SHIB_L(KeyAuthority));
1026 else if (saml::XML::isElementNamed(child,::XML::SAML2META_NS,SHIB_L(ContactPerson))) {
1027 m_contacts.push_back(new ContactPerson(child));
1029 else if (saml::XML::isElementNamed(child,::XML::SAML2META_NS,SHIB_L(Organization))) {
1030 m_org=new Organization(child);
1032 else if (saml::XML::isElementNamed(child,::XML::SAML2META_NS,SHIB_L(AdditionalMetadataLocation))) {
1033 DOMNode* loc=child->getFirstChild();
1036 pair<const XMLCh*,const XMLCh*>(child->getAttributeNS(NULL,::XML::Literals::_namespace),loc->getNodeValue())
1039 else if (saml::XML::isElementNamed(child,::XML::SAML2META_NS,SHIB_L(IDPSSODescriptor))) {
1040 m_roles.push_back(new IDPRole(this,m_validUntil,child));
1042 else if (saml::XML::isElementNamed(child,::XML::SAML2META_NS,SHIB_L(AttributeAuthorityDescriptor))) {
1043 m_roles.push_back(new AARole(this,m_validUntil,child));
1045 child = saml::XML::getNextSiblingElement(child);
1049 m_id=e->getAttributeNS(NULL,SHIB_L(Name));
1050 m_errorURL=auto_ptr<char>(toUTF8(e->getAttributeNS(NULL,SHIB_L(ErrorURL))));
1052 bool idp=false,aa=false; // only want to build a role once
1053 DOMElement* child=saml::XML::getFirstChildElement(e);
1055 // Process the various kinds of OriginSite children that we care about...
1056 if (saml::XML::isElementNamed(child,::XML::SHIB_NS,SHIB_L(Contact))) {
1057 m_contacts.push_back(new ContactPerson(child));
1059 else if (saml::XML::isElementNamed(child,::XML::SHIB_NS,SHIB_L(HandleService)) && !idp) {
1060 // Create the IDP role if needed.
1061 m_roles.push_back(new IDPRole(this, m_validUntil, e));
1064 else if (saml::XML::isElementNamed(child,::XML::SHIB_NS,SHIB_L(AttributeAuthority)) && !aa) {
1065 // Create the AA role if needed.
1066 m_roles.push_back(new AARole(this, m_validUntil, e));
1069 child = saml::XML::getNextSiblingElement(child);
1073 auto_ptr_char id(m_id);
1074 wrapper->m_sites.insert(pair<string,const EntityDescriptor*>(id.get(),this));
1076 // Look for an IdP role, and register the artifact source ID and endpoints.
1077 const IDPRole* idp=NULL;
1078 for (vector<const IRoleDescriptor*>::const_iterator r=m_roles.begin(); r!=m_roles.end(); r++) {
1079 if (idp=dynamic_cast<const IDPRole*>(*r)) {
1080 if (idp->m_sourceId) {
1081 auto_ptr_char sourceid(idp->m_sourceId);
1082 wrapper->m_sources.insert(pair<string,const EntityDescriptor*>(sourceid.get(),this));
1085 string sourceid=SAMLArtifact::toHex(SAMLArtifactType0001::generateSourceId(id.get()));
1086 Category::getInstance(XMLPROVIDERS_LOGCAT".Metadata").debug(
1087 "generated artifact SourceID (%s) for entity (%s)",sourceid.c_str(),id.get()
1089 wrapper->m_sources.insert(pair<string,const EntityDescriptor*>(sourceid,this));
1091 Iterator<const IEndpoint*> locs=idp->getArtifactResolutionServiceManager()->getEndpoints();
1092 while (locs.hasNext()) {
1093 auto_ptr_char loc(locs.next()->getLocation());
1094 wrapper->m_sources.insert(pair<string,const EntityDescriptor*>(loc.get(),this));
1100 const IIDPSSODescriptor* XMLMetadataImpl::EntityDescriptor::getIDPSSODescriptor(const XMLCh* protocol) const
1102 const IIDPSSODescriptor* ret=NULL;
1103 for (vector<const IRoleDescriptor*>::const_iterator i=m_roles.begin(); i!=m_roles.end(); i++) {
1104 if ((*i)->hasSupport(protocol) && (*i)->isValid() && (ret=dynamic_cast<const IIDPSSODescriptor*>(*i)))
1110 const IAttributeAuthorityDescriptor* XMLMetadataImpl::EntityDescriptor::getAttributeAuthorityDescriptor(const XMLCh* protocol) const
1112 const IAttributeAuthorityDescriptor* ret=NULL;
1113 for (vector<const IRoleDescriptor*>::const_iterator i=m_roles.begin(); i!=m_roles.end(); i++) {
1114 if ((*i)->hasSupport(protocol) && (*i)->isValid() && (ret=dynamic_cast<const IAttributeAuthorityDescriptor*>(*i)))
1120 XMLMetadataImpl::EntityDescriptor::~EntityDescriptor()
1123 for (vector<const IContactPerson*>::iterator i=m_contacts.begin(); i!=m_contacts.end(); i++)
1124 delete const_cast<IContactPerson*>(*i);
1125 for (vector<const IRoleDescriptor*>::iterator j=m_roles.begin(); j!=m_roles.end(); j++)
1126 delete const_cast<IRoleDescriptor*>(*j);
1127 for (vector<const IKeyAuthority*>::iterator k=m_keyauths.begin(); k!=m_keyauths.end(); k++)
1128 delete const_cast<IKeyAuthority*>(*k);
1131 XMLMetadataImpl::EntitiesDescriptor::EntitiesDescriptor(
1132 const DOMElement* e, XMLMetadataImpl* wrapper, time_t validUntil, const IEntitiesDescriptor* parent
1133 ) : m_root(e), m_name(e->getAttributeNS(NULL,SHIB_L(Name))), m_parent(parent), m_validUntil(validUntil)
1135 // Check the root element namespace. If SAML2, assume it's the std schema.
1136 if (!XMLString::compareString(e->getNamespaceURI(),::XML::SAML2META_NS)) {
1138 if (e->hasAttributeNS(NULL,SHIB_L(validUntil))) {
1139 SAMLDateTime exp(e->getAttributeNS(NULL,SHIB_L(validUntil)));
1140 exp.parseDateTime();
1141 m_validUntil=min(validUntil,exp.getEpoch());
1144 e=saml::XML::getFirstChildElement(e);
1146 if (saml::XML::isElementNamed(e,::XML::SAML2META_NS,SHIB_L(Extensions))) {
1147 DOMElement* ext = saml::XML::getFirstChildElement(e,::XML::SHIBMETA_NS,SHIB_L(KeyAuthority));
1149 m_keyauths.push_back(new KeyAuthority(ext));
1150 ext = saml::XML::getNextSiblingElement(ext,::XML::SHIBMETA_NS,SHIB_L(KeyAuthority));
1153 else if (saml::XML::isElementNamed(e,::XML::SAML2META_NS,SHIB_L(EntitiesDescriptor)))
1154 m_groups.push_back(new EntitiesDescriptor(e,wrapper,m_validUntil,this));
1155 else if (saml::XML::isElementNamed(e,::XML::SAML2META_NS,SHIB_L(EntityDescriptor)))
1156 m_providers.push_back(new EntityDescriptor(e,wrapper,m_validUntil,this));
1157 e=saml::XML::getNextSiblingElement(e);
1161 e=saml::XML::getFirstChildElement(e);
1163 if (saml::XML::isElementNamed(e,::XML::SHIB_NS,SHIB_L(SiteGroup)))
1164 m_groups.push_back(new EntitiesDescriptor(e,wrapper,m_validUntil,this));
1165 else if (saml::XML::isElementNamed(e,::XML::SHIB_NS,SHIB_L(OriginSite)))
1166 m_providers.push_back(new EntityDescriptor(e,wrapper,m_validUntil,this));
1167 e=saml::XML::getNextSiblingElement(e);
1172 XMLMetadataImpl::EntitiesDescriptor::~EntitiesDescriptor()
1174 for (vector<const IEntityDescriptor*>::iterator i=m_providers.begin(); i!=m_providers.end(); i++)
1175 delete const_cast<IEntityDescriptor*>(*i);
1176 for (vector<const IEntitiesDescriptor*>::iterator j=m_groups.begin(); j!=m_groups.end(); j++)
1177 delete const_cast<IEntitiesDescriptor*>(*j);
1178 for (vector<const IKeyAuthority*>::iterator k=m_keyauths.begin(); k!=m_keyauths.end(); k++)
1179 delete const_cast<IKeyAuthority*>(*k);
1182 void XMLMetadataImpl::init()
1187 Category& log=Category::getInstance(XMLPROVIDERS_LOGCAT".Metadata");
1191 if (saml::XML::isElementNamed(m_root,::XML::SAML2META_NS,SHIB_L(EntitiesDescriptor)))
1192 m_rootGroup=new EntitiesDescriptor(m_root,this);
1193 else if (saml::XML::isElementNamed(m_root,::XML::SAML2META_NS,SHIB_L(EntityDescriptor)))
1194 m_rootProvider=new EntityDescriptor(m_root,this);
1195 else if (saml::XML::isElementNamed(m_root,::XML::SHIB_NS,SHIB_L(SiteGroup)))
1196 m_rootGroup=new EntitiesDescriptor(m_root,this);
1197 else if (saml::XML::isElementNamed(m_root,::XML::SHIB_NS,SHIB_L(OriginSite)))
1198 m_rootProvider=new EntityDescriptor(m_root,this);
1200 log.error("Construction requires a valid SAML metadata file");
1201 throw MetadataException("Construction requires a valid SAML metadata file");
1204 catch (SAMLException& e)
1206 log.errorStream() << "Error while parsing SAML metadata: " << e.what() << CategoryStream::ENDLINE;
1207 this->~XMLMetadataImpl();
1213 log.error("Unexpected error while parsing SAML metadata");
1214 this->~XMLMetadataImpl();
1220 XMLMetadataImpl::~XMLMetadataImpl()
1223 delete m_rootProvider;
1226 const IEntityDescriptor* XMLMetadata::lookup(const char* providerId, bool strict) const
1228 if (strict && m_exclusions && m_set.find(providerId)!=m_set.end())
1230 else if (strict && !m_exclusions && m_set.find(providerId)==m_set.end())
1233 XMLMetadataImpl* impl=dynamic_cast<XMLMetadataImpl*>(getImplementation());
1234 pair<XMLMetadataImpl::sitemap_t::const_iterator,XMLMetadataImpl::sitemap_t::const_iterator> range=
1235 impl->m_sites.equal_range(providerId);
1237 time_t now=time(NULL);
1238 for (XMLMetadataImpl::sitemap_t::const_iterator i=range.first; i!=range.second; i++)
1239 if (now < i->second->getValidUntil())
1242 if (!strict && range.first!=range.second)
1243 return range.first->second;
1248 const IEntityDescriptor* XMLMetadata::lookup(const XMLCh* providerId, bool strict) const
1250 auto_ptr_char temp(providerId);
1251 return lookup(temp.get(),strict);
1254 const IEntityDescriptor* XMLMetadata::lookup(const SAMLArtifact* artifact) const
1256 time_t now=time(NULL);
1257 XMLMetadataImpl* impl=dynamic_cast<XMLMetadataImpl*>(getImplementation());
1258 pair<XMLMetadataImpl::sitemap_t::const_iterator,XMLMetadataImpl::sitemap_t::const_iterator> range;
1260 // Depends on type of artifact.
1261 const SAMLArtifactType0001* type1=dynamic_cast<const SAMLArtifactType0001*>(artifact);
1263 range=impl->m_sources.equal_range(SAMLArtifact::toHex(type1->getSourceID()));
1266 const SAMLArtifactType0002* type2=dynamic_cast<const SAMLArtifactType0002*>(artifact);
1268 range=impl->m_sources.equal_range(type2->getSourceLocation());
1274 // Check exclude list.
1275 if (range.first!=range.second) {
1276 auto_ptr_char id(range.first->second->getId());
1277 if (m_exclusions && m_set.find(id.get())!=m_set.end())
1279 else if (!m_exclusions && m_set.find(id.get())==m_set.end())
1282 for (XMLMetadataImpl::sitemap_t::const_iterator i=range.first; i!=range.second; i++)
1283 if (now < i->second->getValidUntil())