2 * Copyright 2001-2005 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 /* XMLMetadata.cpp - a metadata implementation that uses an XML-based registry
29 #include <sys/types.h>
32 #include <log4cpp/Category.hh>
33 #include <xercesc/util/XMLChar.hpp>
34 #include <xsec/dsig/DSIGTransformC14n.hpp>
35 #include <xsec/dsig/DSIGReference.hpp>
36 #include <xsec/dsig/DSIGTransformList.hpp>
37 #include <xsec/enc/XSECCryptoException.hpp>
38 #include <xsec/enc/XSECKeyInfoResolverDefault.hpp>
39 #include <xsec/enc/OpenSSL/OpenSSLCryptoX509.hpp>
40 #include <xsec/framework/XSECException.hpp>
41 #include <xsec/framework/XSECProvider.hpp>
43 using namespace xmlproviders;
44 using namespace shibboleth;
46 using namespace log4cpp;
52 class XMLMetadataImpl : public ReloadableXMLFileImpl
55 class ContactPerson : public IContactPerson
58 ContactPerson(const DOMElement* e);
61 ContactType getType() const { return m_type; }
62 const char* getCompany() const { return m_company.get(); }
63 const char* getGivenName() const { return m_givenName.get(); }
64 const char* getSurName() const { return m_surName.get(); }
65 Iterator<string> getEmailAddresses() const { return m_emails; }
66 Iterator<string> getTelephoneNumbers() const { return m_phones; }
67 const DOMElement* getElement() const { return m_root; }
70 const DOMElement* m_root;
72 auto_ptr<char> m_givenName,m_surName,m_company;
73 vector<string> m_emails,m_phones;
76 class Organization : public IOrganization
79 Organization(const DOMElement* e);
82 const char* getName(const char* lang="en") const { return forLang(m_names,lang); }
83 const char* getDisplayName(const char* lang="en") const { return forLang(m_displays,lang); }
84 const char* getURL(const char* lang="en") const { return forLang(m_urls,lang); }
85 const DOMElement* getElement() const { return m_root; }
88 const char* forLang(const map<string,string>& m, const char* lang) const {
89 map<string,string>::const_iterator i=m.find(lang);
90 return (i==m.end()) ? NULL : i->second.c_str();
92 const DOMElement* m_root;
93 map<string,string> m_names,m_displays,m_urls;
96 class EntityDescriptor;
98 class EncryptionMethod : public XENCEncryptionMethod
101 EncryptionMethod(const DOMElement* e);
102 ~EncryptionMethod() {}
104 const XMLCh * getAlgorithm(void) const { return m_alg; }
105 const XMLCh * getDigestMethod(void) const { return m_digest; }
106 const XMLCh * getOAEPparams(void) const { return m_params; }
107 int getKeySize(void) const { return m_size; }
108 DOMElement* getElement(void) const { return const_cast<DOMElement*>(m_root); }
109 void setDigestMethod(const XMLCh * method) {throw exception();}
110 void setOAEPparams(const XMLCh * params) {throw exception();}
111 void setKeySize(int size) {throw exception();}
114 const DOMElement* m_root;
116 const XMLCh* m_digest;
117 const XMLCh* m_params;
121 class KeyDescriptor : public IKeyDescriptor
124 KeyDescriptor(const DOMElement* e);
127 KeyUse getUse() const { return m_use; }
128 DSIGKeyInfoList* getKeyInfo() const { return m_klist; }
129 saml::Iterator<const XENCEncryptionMethod*> getEncryptionMethods() const { return m_methods; }
130 const DOMElement* getElement() const { return m_root; }
133 const DOMElement* m_root;
135 mutable DSIGKeyInfoList* m_klist;
136 vector<const XENCEncryptionMethod*> m_methods;
139 class KeyAuthority : public IKeyAuthority
142 KeyAuthority(const DOMElement* e);
145 int getVerifyDepth() const { return m_depth; }
146 Iterator<DSIGKeyInfoList*> getKeyInfos() const { return m_klists; }
150 vector<DSIGKeyInfoList*> m_klists;
153 class Role : public virtual IRoleDescriptor
156 Role(const EntityDescriptor* provider, time_t validUntil, const DOMElement* e);
160 const IEntityDescriptor* getEntityDescriptor() const {return m_provider;}
161 Iterator<const XMLCh*> getProtocolSupportEnumeration() const {return m_protocolEnum;}
162 bool hasSupport(const XMLCh* protocol) const;
163 const char* getErrorURL() const {return (m_errorURL ? m_errorURL : m_provider->getErrorURL());}
164 bool isValid() const {return time(NULL) < m_validUntil;}
165 Iterator<const IKeyDescriptor*> getKeyDescriptors() const {return m_keys;}
166 const IOrganization* getOrganization() const {return m_org ? m_org : m_provider->getOrganization();}
167 Iterator<const IContactPerson*> getContactPersons() const
168 {return (m_contacts.empty() ? m_provider->getContactPersons() : m_contacts);}
169 const DOMElement* getElement() const {return m_root;}
172 vector<const XMLCh*> m_protocolEnum;
173 vector<const IKeyDescriptor*> m_keys;
176 const EntityDescriptor* m_provider;
177 const DOMElement* m_root;
178 XMLCh* m_protocolEnumCopy;
181 vector<const IContactPerson*> m_contacts;
185 class Endpoint : public virtual IEndpoint
188 Endpoint(const DOMElement* e) : m_root(e),
189 m_binding(e->getAttributeNS(NULL,L(Binding))),
190 m_location(e->getAttributeNS(NULL,L(Location))),
191 m_resploc(e->getAttributeNS(NULL,SHIB_L(ResponseLocation))) {}
192 Endpoint(const XMLCh* binding, const XMLCh* loc)
193 : m_root(NULL), m_binding(binding), m_location(loc), m_resploc(NULL) {}
196 const XMLCh* getBinding() const { return m_binding; }
197 const XMLCh* getLocation() const { return m_location; }
198 const XMLCh* getResponseLocation() const { return m_resploc; }
199 const DOMElement* getElement() const { return m_root; }
202 const DOMElement* m_root;
203 const XMLCh* m_binding;
204 const XMLCh* m_location;
205 const XMLCh* m_resploc;
208 class IndexedEndpoint : public Endpoint, public virtual IIndexedEndpoint
211 IndexedEndpoint(const DOMElement* e) : Endpoint(e), m_index(XMLString::parseInt(e->getAttributeNS(NULL,SHIB_L(index)))) {}
212 unsigned short getIndex() const {return m_index;}
215 unsigned short m_index;
218 class EndpointManager : public IEndpointManager
221 EndpointManager() : m_soft(NULL), m_hard(NULL) {}
223 for (vector<const IEndpoint*>::iterator i=m_endpoints.begin(); i!=m_endpoints.end(); i++)
224 delete const_cast<IEndpoint*>(*i);
226 saml::Iterator<const IEndpoint*> getEndpoints() const {return m_endpoints;}
227 const IEndpoint* getDefaultEndpoint() const {
228 if (m_hard) return m_hard;
229 if (m_soft) return m_soft;
230 if (!m_endpoints.empty()) return *(m_endpoints.begin());
233 const IEndpoint* getEndpointByIndex(unsigned short index) const {
234 for (vector<const IEndpoint*>::const_iterator i=m_endpoints.begin(); i!=m_endpoints.end(); i++) {
235 const IIndexedEndpoint* temp=dynamic_cast<const IIndexedEndpoint*>(*i);
236 if (temp && index==temp->getIndex())
241 const IEndpoint* getEndpointByBinding(const XMLCh* binding) const {
242 for (vector<const IEndpoint*>::const_iterator i=m_endpoints.begin(); i!=m_endpoints.end(); i++)
243 if (!XMLString::compareString(binding,(*i)->getBinding()))
247 void add(IEndpoint* e) {
248 m_endpoints.push_back(e);
249 if (!m_hard && e->getElement()) {
250 const XMLCh* v=e->getElement()->getAttributeNS(NULL,SHIB_L(isDefault));
251 if (v && (*v==chDigit_1 || *v==chLatin_t)) // explicit default
253 else if ((!v || !*v) && !m_soft) // implicit default
256 else if (!m_hard && !m_soft) {
257 // No default yet, so this one qualifies as an implicit.
263 vector<const IEndpoint*> m_endpoints;
264 const IEndpoint* m_soft; // Soft default (not explicit)
265 const IEndpoint* m_hard; // Hard default (explicit)
268 class SSORole : public Role, public virtual ISSODescriptor
271 SSORole(const EntityDescriptor* provider, time_t validUntil, const DOMElement* e);
273 const IEndpointManager* getArtifactResolutionServiceManager() const {return &m_artifact;}
274 const IEndpointManager* getSingleLogoutServiceManager() const {return &m_logout;}
275 const IEndpointManager* getManageNameIDServiceManager() const {return &m_nameid;}
276 saml::Iterator<const XMLCh*> getNameIDFormats() const {return m_formats;}
279 EndpointManager m_artifact,m_logout,m_nameid;
280 vector<const XMLCh*> m_formats;
283 class ScopedRole : public virtual IScopedRoleDescriptor
286 ScopedRole(const DOMElement* e);
287 saml::Iterator<std::pair<const XMLCh*,bool> > getScopes() const {return m_scopes;}
290 vector<pair<const XMLCh*,bool> > m_scopes;
293 class IDPRole : public SSORole, public ScopedRole, public virtual IIDPSSODescriptor
296 IDPRole(const EntityDescriptor* provider, time_t validUntil, const DOMElement* e);
298 bool getWantAuthnRequestsSigned() const {return m_wantAuthnRequestsSigned;}
299 const IEndpointManager* getSingleSignOnServiceManager() const {return &m_sso;}
300 const IEndpointManager* getNameIDMappingServiceManager() const {return &m_mapping;}
301 const IEndpointManager* getAssertionIDRequestServiceManager() const {return &m_idreq;}
302 saml::Iterator<const XMLCh*> getAttributeProfiles() const {return m_attrprofs;}
303 saml::Iterator<const saml::SAMLAttribute*> getAttributes() const {return m_attrs;}
306 EndpointManager m_sso,m_mapping,m_idreq;
307 vector<const XMLCh*> m_attrprofs;
308 vector<const SAMLAttribute*> m_attrs;
309 bool m_wantAuthnRequestsSigned;
310 const XMLCh* m_sourceId;
311 friend class EntityDescriptor;
314 class AARole : public Role, public ScopedRole, public virtual IAttributeAuthorityDescriptor
317 AARole(const EntityDescriptor* provider, time_t validUntil, const DOMElement* e);
319 const IEndpointManager* getAttributeServiceManager() const {return &m_query;}
320 const IEndpointManager* getAssertionIDRequestServiceManager() const {return &m_idreq;}
321 saml::Iterator<const XMLCh*> getNameIDFormats() const {return m_formats;}
322 saml::Iterator<const XMLCh*> getAttributeProfiles() const {return m_attrprofs;}
323 saml::Iterator<const saml::SAMLAttribute*> getAttributes() const {return m_attrs;}
326 EndpointManager m_query,m_idreq;
327 vector<const XMLCh*> m_formats,m_attrprofs;
328 vector<const SAMLAttribute*> m_attrs;
331 class EntityDescriptor : public IExtendedEntityDescriptor
336 XMLMetadataImpl* wrapper,
337 time_t validUntil=LONG_MAX,
338 const IEntitiesDescriptor* parent=NULL
343 const XMLCh* getId() const {return m_id;}
344 bool isValid() const {return time(NULL) < m_validUntil;}
345 Iterator<const IRoleDescriptor*> getRoleDescriptors() const {return m_roles;}
346 const IIDPSSODescriptor* getIDPSSODescriptor(const XMLCh* protocol) const;
347 const ISPSSODescriptor* getSPSSODescriptor(const XMLCh* protocol) const {return NULL;}
348 const IAuthnAuthorityDescriptor* getAuthnAuthorityDescriptor(const XMLCh* protocol) const {return NULL;}
349 const IAttributeAuthorityDescriptor* getAttributeAuthorityDescriptor(const XMLCh* protocol) const;
350 const IPDPDescriptor* getPDPDescriptor(const XMLCh* protocol) const {return NULL;}
351 const IAffiliationDescriptor* getAffiliationDescriptor() const {return NULL;}
352 const IOrganization* getOrganization() const {return m_org;}
353 Iterator<const IContactPerson*> getContactPersons() const {return m_contacts;}
354 Iterator<pair<const XMLCh*,const XMLCh*> > getAdditionalMetadataLocations() const {return m_locs;}
355 const IEntitiesDescriptor* getEntitiesDescriptor() const {return m_parent;}
356 Iterator<const IKeyAuthority*> getKeyAuthorities() const {return m_keyauths;}
357 const DOMElement* getElement() const {return m_root;}
360 const char* getErrorURL() const {return m_errorURL.get();}
361 time_t getValidUntil() const {return m_validUntil;}
363 const DOMElement* m_root;
364 const IEntitiesDescriptor* m_parent;
366 auto_ptr<char> m_errorURL;
367 IOrganization* m_org;
368 vector<const IContactPerson*> m_contacts;
369 vector<const IRoleDescriptor*> m_roles;
370 vector<pair<const XMLCh*,const XMLCh*> > m_locs;
371 vector<const IKeyAuthority*> m_keyauths;
375 class EntitiesDescriptor : public IExtendedEntitiesDescriptor
380 XMLMetadataImpl* wrapper,
381 time_t validUntil=LONG_MAX,
382 const IEntitiesDescriptor* parent=NULL
384 ~EntitiesDescriptor();
386 const XMLCh* getName() const {return m_name;}
387 bool isValid() const {return time(NULL) < m_validUntil;}
388 const IEntitiesDescriptor* getEntitiesDescriptor() const {return m_parent;}
389 Iterator<const IEntitiesDescriptor*> getEntitiesDescriptors() const {return m_groups;}
390 Iterator<const IEntityDescriptor*> getEntityDescriptors() const {return m_providers;}
391 Iterator<const IKeyAuthority*> getKeyAuthorities() const {return m_keyauths;}
392 const DOMElement* getElement() const {return m_root;}
395 time_t getValidUntil() const {return m_validUntil;}
397 const DOMElement* m_root;
398 const IEntitiesDescriptor* m_parent;
400 vector<const IEntitiesDescriptor*> m_groups;
401 vector<const IEntityDescriptor*> m_providers;
402 vector<const IKeyAuthority*> m_keyauths;
406 XMLMetadataImpl(const char* pathname, const XMLMetadata* wrapper)
407 : ReloadableXMLFileImpl(pathname), m_outer(wrapper), m_rootProvider(NULL), m_rootGroup(NULL) { init(); }
408 XMLMetadataImpl(const DOMElement* e, const XMLMetadata* wrapper)
409 : ReloadableXMLFileImpl(e), m_outer(wrapper), m_rootProvider(NULL), m_rootGroup(NULL) { init(); }
413 typedef multimap<string,const EntityDescriptor*> sitemap_t;
414 typedef multimap<string,const EntitiesDescriptor*> groupmap_t;
418 EntityDescriptor* m_rootProvider;
419 EntitiesDescriptor* m_rootGroup;
420 const XMLMetadata* m_outer;
423 class XMLMetadata : public IMetadata, public ReloadableXMLFile
426 XMLMetadata(const DOMElement* e);
427 ~XMLMetadata() {delete m_credResolver;}
429 const IEntityDescriptor* lookup(const char* providerId, bool strict=true) const;
430 const IEntityDescriptor* lookup(const XMLCh* providerId, bool strict=true) const;
431 const IEntityDescriptor* lookup(const saml::SAMLArtifact* artifact) const;
432 const IEntitiesDescriptor* lookupGroup(const char* name, bool strict=true) const;
433 const IEntitiesDescriptor* lookupGroup(const XMLCh* name, bool strict=true) const;
434 pair<const IEntitiesDescriptor*,const IEntityDescriptor*> getRoot() const;
436 bool verifySignature(DOMDocument* doc, const DOMElement* parent, bool failUnsigned) const;
439 virtual ReloadableXMLFileImpl* newImplementation(const char* pathname, bool first=true) const;
440 virtual ReloadableXMLFileImpl* newImplementation(const DOMElement* e, bool first=true) const;
443 bool m_exclusions,m_verify;
445 ICredResolver* m_credResolver;
449 IPlugIn* XMLMetadataFactory(const DOMElement* e)
451 auto_ptr<XMLMetadata> m(new XMLMetadata(e));
452 m->getImplementation();
456 ReloadableXMLFileImpl* XMLMetadata::newImplementation(const DOMElement* e, bool first) const
458 return new XMLMetadataImpl(e,this);
461 ReloadableXMLFileImpl* XMLMetadata::newImplementation(const char* pathname, bool first) const
463 return new XMLMetadataImpl(pathname,this);
466 XMLMetadataImpl::ContactPerson::ContactPerson(const DOMElement* e) : m_root(e)
468 const XMLCh* type=NULL;
470 // Old metadata or new?
471 if (saml::XML::isElementNamed(e,::XML::SHIB_NS,SHIB_L(Contact))) {
472 type=e->getAttributeNS(NULL,SHIB_L(Type));
473 auto_ptr<char> wrapper(toUTF8(e->getAttributeNS(NULL,SHIB_L(Name))));
475 if (e->hasAttributeNS(NULL,SHIB_L(Email))) {
476 auto_ptr<char> temp(toUTF8(e->getAttributeNS(NULL,SHIB_L(Email))));
478 m_emails.push_back(temp.get());
481 else if (saml::XML::isElementNamed(e,::XML::SAML2META_NS,SHIB_L(ContactPerson))) {
482 type=e->getAttributeNS(NULL,SHIB_L(contactType));
483 e=saml::XML::getFirstChildElement(e);
485 if (saml::XML::isElementNamed(e,::XML::SAML2META_NS,SHIB_L(Company)) && e->hasChildNodes()) {
486 auto_ptr<char> wrapper(toUTF8(e->getFirstChild()->getNodeValue()));
489 else if (saml::XML::isElementNamed(e,::XML::SAML2META_NS,SHIB_L(GivenName)) && e->hasChildNodes()) {
490 auto_ptr<char> wrapper(toUTF8(e->getFirstChild()->getNodeValue()));
493 else if (saml::XML::isElementNamed(e,::XML::SAML2META_NS,SHIB_L(SurName)) && e->hasChildNodes()) {
494 auto_ptr<char> wrapper(toUTF8(e->getFirstChild()->getNodeValue()));
497 else if (saml::XML::isElementNamed(e,::XML::SAML2META_NS,SHIB_L(EmailAddress)) && e->hasChildNodes()) {
498 auto_ptr<char> temp(toUTF8(e->getFirstChild()->getNodeValue()));
499 if (temp.get()) m_emails.push_back(temp.get());
501 else if (saml::XML::isElementNamed(e,::XML::SAML2META_NS,SHIB_L(TelephoneNumber)) && e->hasChildNodes()) {
502 auto_ptr<char> temp(toUTF8(e->getFirstChild()->getNodeValue()));
503 if (temp.get()) m_phones.push_back(temp.get());
505 e=saml::XML::getNextSiblingElement(e);
509 if (!XMLString::compareString(type,SHIB_L(technical)))
510 m_type=IContactPerson::technical;
511 else if (!XMLString::compareString(type,SHIB_L(support)))
512 m_type=IContactPerson::support;
513 else if (!XMLString::compareString(type,SHIB_L(administrative)))
514 m_type=IContactPerson::administrative;
515 else if (!XMLString::compareString(type,SHIB_L(billing)))
516 m_type=IContactPerson::billing;
517 else if (!XMLString::compareString(type,SHIB_L(other)))
518 m_type=IContactPerson::other;
521 XMLMetadataImpl::Organization::Organization(const DOMElement* e) : m_root(e)
524 e=saml::XML::getFirstChildElement(e);
526 if (saml::XML::isElementNamed(e,::XML::SAML2META_NS,SHIB_L(OrganizationName))) {
527 n=e->getFirstChild();
529 auto_ptr<char> name(toUTF8(n->getNodeValue()));
530 auto_ptr_char lang(e->getAttributeNS(saml::XML::XML_NS,L(lang)));
531 m_names[lang.get()]=name.get();
534 else if (saml::XML::isElementNamed(e,::XML::SAML2META_NS,SHIB_L(OrganizationDisplayName))) {
535 n=e->getFirstChild();
537 auto_ptr<char> display(toUTF8(n->getNodeValue()));
538 auto_ptr_char lang(e->getAttributeNS(saml::XML::XML_NS,L(lang)));
539 m_displays[lang.get()]=display.get();
542 else if (saml::XML::isElementNamed(e,::XML::SAML2META_NS,SHIB_L(OrganizationURL))) {
543 n=e->getFirstChild();
545 auto_ptr<char> url(toUTF8(n->getNodeValue()));
546 auto_ptr_char lang(e->getAttributeNS(saml::XML::XML_NS,L(lang)));
547 m_urls[lang.get()]=url.get();
550 e=saml::XML::getNextSiblingElement(e);
554 XMLMetadataImpl::EncryptionMethod::EncryptionMethod(const DOMElement* e) : m_root(e)
556 m_alg=e->getAttributeNS(NULL,SHIB_L(Algorithm));
557 e=saml::XML::getFirstChildElement(e);
559 if (saml::XML::isElementNamed(e,::XML::XMLENC_NS,SHIB_L(KeySize))) {
560 DOMNode* n=e->getFirstChild();
561 if (n) m_size=XMLString::parseInt(n->getNodeValue());
563 else if (saml::XML::isElementNamed(e,saml::XML::XMLSIG_NS,SHIB_L(DigestMethod))) {
564 DOMNode* n=e->getFirstChild();
565 if (n) m_digest=n->getNodeValue();
567 else if (saml::XML::isElementNamed(e,::XML::XMLENC_NS,SHIB_L(OAEParams))) {
568 DOMNode* n=e->getFirstChild();
569 if (n) m_params=n->getNodeValue();
571 e=saml::XML::getNextSiblingElement(e);
575 XMLMetadataImpl::KeyDescriptor::KeyDescriptor(const DOMElement* e) : m_root(e), m_use(unspecified), m_klist(NULL)
578 saml::NDC ndc("KeyDescriptor");
580 if (!XMLString::compareString(e->getAttributeNS(NULL,SHIB_L(use)),SHIB_L(encryption)))
582 else if (!XMLString::compareString(e->getAttributeNS(NULL,SHIB_L(use)),SHIB_L(signing)))
585 m_klist = new DSIGKeyInfoList(NULL);
587 // Process ds:KeyInfo
588 e=saml::XML::getFirstChildElement(e);
590 // We let XMLSec hack through anything it can. This should evolve over time, or we can
591 // plug in our own KeyResolver later...
593 if (!m_klist->loadListFromXML(const_cast<DOMElement*>(e)))
594 Category::getInstance(XMLPROVIDERS_LOGCAT".Metadata").warn(
595 "skipping ds:KeyInfo element containing unsupported children"
598 catch (XSECCryptoException& xe) {
599 Category::getInstance(XMLPROVIDERS_LOGCAT".Metadata").error("unable to process ds:KeyInfo element: %s",xe.getMsg());
602 // Check for encryption methods.
603 e=saml::XML::getNextSiblingElement(e);
604 while (e && saml::XML::isElementNamed(e,::XML::SAML2META_NS,SHIB_L(EncryptionMethod)))
605 m_methods.push_back(new EncryptionMethod(e));
608 XMLMetadataImpl::KeyDescriptor::~KeyDescriptor()
610 for_each(m_methods.begin(),m_methods.end(),cleanup<XENCEncryptionMethod>);
614 XMLMetadataImpl::KeyAuthority::KeyAuthority(const DOMElement* e) : m_depth(1)
617 saml::NDC ndc("KeyAuthority");
619 if (e->hasAttributeNS(NULL,SHIB_L(VerifyDepth)))
620 m_depth=XMLString::parseInt(e->getAttributeNS(NULL,SHIB_L(VerifyDepth)));
622 // Process ds:KeyInfo children
623 e=saml::XML::getFirstChildElement(e,saml::XML::XMLSIG_NS,L(KeyInfo));
625 auto_ptr<DSIGKeyInfoList> klist(new DSIGKeyInfoList(NULL));
627 // We let XMLSec hack through anything it can. This should evolve over time, or we can
628 // plug in our own KeyResolver later...
629 DOMElement* child=saml::XML::getFirstChildElement(e);
632 if (!klist->addXMLKeyInfo(child)) {
633 Category::getInstance(XMLPROVIDERS_LOGCAT".Metadata").warn(
634 "skipped unresolvable ds:KeyInfo child element");
637 catch (XSECCryptoException& xe) {
638 Category::getInstance(XMLPROVIDERS_LOGCAT".Metadata").error(
639 "unable to process ds:KeyInfo child element: %s",xe.getMsg());
641 child=saml::XML::getNextSiblingElement(child);
644 if (klist->getSize()>0)
645 m_klists.push_back(klist.release());
647 Category::getInstance(XMLPROVIDERS_LOGCAT".Metadata").warn(
648 "skipping ds:KeyInfo with no resolvable child elements");
649 e=saml::XML::getNextSiblingElement(e,saml::XML::XMLSIG_NS,L(KeyInfo));
653 XMLMetadataImpl::KeyAuthority::~KeyAuthority()
655 for_each(m_klists.begin(),m_klists.end(),cleanup<DSIGKeyInfoList>);
658 XMLMetadataImpl::Role::Role(const EntityDescriptor* provider, time_t validUntil, const DOMElement* e)
659 : m_provider(provider), m_errorURL(NULL), m_protocolEnumCopy(NULL), m_org(NULL), m_validUntil(validUntil), m_root(e)
661 // Check the root element namespace. If SAML2, assume it's the std schema.
662 if (e && !XMLString::compareString(e->getNamespaceURI(),::XML::SAML2META_NS)) {
664 if (e->hasAttributeNS(NULL,SHIB_L(validUntil))) {
665 SAMLDateTime exp(e->getAttributeNS(NULL,SHIB_L(validUntil)));
667 m_validUntil=min(m_validUntil,exp.getEpoch());
670 if (e->hasAttributeNS(NULL,SHIB_L(errorURL)))
671 m_errorURL=toUTF8(e->getAttributeNS(NULL,SHIB_L(errorURL)));
673 // Chop the protocol list into pieces...assume any whitespace can appear in between.
674 m_protocolEnumCopy=XMLString::replicate(e->getAttributeNS(NULL,SHIB_L(protocolSupportEnumeration)));
675 XMLCh* temp=m_protocolEnumCopy;
676 while (temp && *temp) {
678 while (*temp && !XMLChar1_1::isWhitespace(*temp)) temp++;
681 m_protocolEnum.push_back(start);
682 while (*temp && XMLChar1_1::isWhitespace(*temp)) temp++;
685 e=saml::XML::getFirstChildElement(m_root,::XML::SAML2META_NS,SHIB_L(KeyDescriptor));
687 m_keys.push_back(new KeyDescriptor(e));
688 e=saml::XML::getNextSiblingElement(e,::XML::SAML2META_NS,SHIB_L(KeyDescriptor));
691 e=saml::XML::getFirstChildElement(m_root,::XML::SAML2META_NS,SHIB_L(Organization));
693 m_org=new Organization(e);
695 e=saml::XML::getFirstChildElement(m_root,::XML::SAML2META_NS,SHIB_L(ContactPerson));
697 m_contacts.push_back(new ContactPerson(e));
698 e=saml::XML::getNextSiblingElement(e,::XML::SAML2META_NS,SHIB_L(ContactPerson));
703 XMLMetadataImpl::Role::~Role()
707 if (m_protocolEnumCopy) XMLString::release(&m_protocolEnumCopy);
708 for_each(m_keys.begin(),m_keys.end(),cleanup<IKeyDescriptor>);
709 for_each(m_contacts.begin(),m_contacts.end(),cleanup<IContactPerson>);
712 bool XMLMetadataImpl::Role::hasSupport(const XMLCh* protocol) const
714 Iterator<const XMLCh*> i(m_protocolEnum);
715 while (i.hasNext()) {
716 if (!XMLString::compareString(protocol,i.next()))
722 XMLMetadataImpl::SSORole::SSORole(const EntityDescriptor* provider, time_t validUntil, const DOMElement* e)
723 : Role(provider,validUntil,e)
725 // Check the root element namespace. If SAML2, assume it's the std schema.
726 if (!XMLString::compareString(e->getNamespaceURI(),::XML::SAML2META_NS)) {
728 DOMNodeList* nlist=e->getElementsByTagNameNS(::XML::SAML2META_NS,SHIB_L(ArtifactResolutionService));
729 for (i=0; nlist && i<nlist->getLength(); i++)
730 m_artifact.add(new IndexedEndpoint(static_cast<DOMElement*>(nlist->item(i))));
732 nlist=e->getElementsByTagNameNS(::XML::SAML2META_NS,SHIB_L(SingleLogoutService));
733 for (i=0; nlist && i<nlist->getLength(); i++)
734 m_logout.add(new Endpoint(static_cast<DOMElement*>(nlist->item(i))));
736 nlist=e->getElementsByTagNameNS(::XML::SAML2META_NS,SHIB_L(ManageNameIDService));
737 for (i=0; nlist && i<nlist->getLength(); i++)
738 m_nameid.add(new Endpoint(static_cast<DOMElement*>(nlist->item(i))));
740 nlist=e->getElementsByTagNameNS(::XML::SAML2META_NS,SHIB_L(NameIDFormat));
741 for (i=0; nlist && i<nlist->getLength(); i++) {
742 DOMNode* n=nlist->item(i)->getFirstChild();
743 if (n) m_formats.push_back(n->getNodeValue());
747 // For old style, we just do SAML 1.1 compatibility with Shib handles.
748 m_protocolEnum.push_back(saml::XML::SAML11_PROTOCOL_ENUM);
749 m_formats.push_back(shibboleth::Constants::SHIB_NAMEID_FORMAT_URI);
753 XMLMetadataImpl::ScopedRole::ScopedRole(const DOMElement* e)
755 // Check the root element namespace. If SAML2, assume it's the std schema.
756 DOMNodeList* nlist=NULL;
757 if (!XMLString::compareString(e->getNamespaceURI(),::XML::SAML2META_NS)) {
758 e=saml::XML::getFirstChildElement(e,::XML::SAML2META_NS,SHIB_L(Extensions));
759 if (e) nlist=e->getElementsByTagNameNS(::XML::SHIBMETA_NS,SHIB_L(Scope));
762 nlist=e->getElementsByTagNameNS(::XML::SHIB_NS,SHIB_L(Domain));
765 for (unsigned int i=0; nlist && i < nlist->getLength(); i++) {
766 const XMLCh* dom=(nlist->item(i)->hasChildNodes()) ? nlist->item(i)->getFirstChild()->getNodeValue() : NULL;
768 const XMLCh* regexp=static_cast<DOMElement*>(nlist->item(i))->getAttributeNS(NULL,SHIB_L(regexp));
770 pair<const XMLCh*,bool>(dom,(regexp && (*regexp==chLatin_t || *regexp==chDigit_1)))
776 XMLMetadataImpl::IDPRole::IDPRole(const EntityDescriptor* provider, time_t validUntil, const DOMElement* e)
777 : SSORole(provider,validUntil,e), ScopedRole(e), m_wantAuthnRequestsSigned(false), m_sourceId(NULL)
779 // Check the root element namespace. If SAML2, assume it's the std schema.
780 if (!XMLString::compareString(e->getNamespaceURI(),::XML::SAML2META_NS)) {
781 const XMLCh* flag=e->getAttributeNS(NULL,SHIB_L(WantAuthnRequestsSigned));
782 m_wantAuthnRequestsSigned=(flag && (*flag==chDigit_1 || *flag==chLatin_t));
784 // Check for SourceID extension.
785 DOMElement* ext=saml::XML::getFirstChildElement(e,::XML::SAML2META_NS,SHIB_L(Extensions));
787 ext=saml::XML::getFirstChildElement(ext,saml::XML::SAML_ARTIFACT_SOURCEID,SHIB_L(SourceID));
788 if (ext && ext->hasChildNodes())
789 m_sourceId=ext->getFirstChild()->getNodeValue();
793 DOMNodeList* nlist=e->getElementsByTagNameNS(::XML::SAML2META_NS,SHIB_L(SingleSignOnService));
794 for (i=0; nlist && i<nlist->getLength(); i++)
795 m_sso.add(new Endpoint(static_cast<DOMElement*>(nlist->item(i))));
797 nlist=e->getElementsByTagNameNS(::XML::SAML2META_NS,SHIB_L(NameIDMappingService));
798 for (i=0; nlist && i<nlist->getLength(); i++)
799 m_mapping.add(new Endpoint(static_cast<DOMElement*>(nlist->item(i))));
801 nlist=e->getElementsByTagNameNS(::XML::SAML2META_NS,SHIB_L(AssertionIDRequestService));
802 for (i=0; nlist && i<nlist->getLength(); i++)
803 m_idreq.add(new Endpoint(static_cast<DOMElement*>(nlist->item(i))));
805 nlist=e->getElementsByTagNameNS(::XML::SAML2META_NS,SHIB_L(AttributeProfile));
806 for (i=0; nlist && i<nlist->getLength(); i++) {
807 DOMNode* n=nlist->item(i)->getFirstChild();
808 if (n) m_attrprofs.push_back(n->getNodeValue());
811 nlist=e->getElementsByTagNameNS(::XML::SAML2ASSERT_NS,L(Attribute));
812 for (i=0; nlist && i<nlist->getLength(); i++) {
813 // For now, we need to convert these to plain SAML 1.1 attributes.
814 DOMElement* src=static_cast<DOMElement*>(nlist->item(i));
815 DOMElement* copy=e->getOwnerDocument()->createElementNS(saml::XML::SAML_NS,L(Attribute));
816 copy->setAttributeNS(NULL,L(AttributeName),src->getAttributeNS(NULL,SHIB_L(Name)));
817 copy->setAttributeNS(NULL,L(AttributeNamespace),src->getAttributeNS(NULL,SHIB_L(NameFormat)));
818 src=saml::XML::getFirstChildElement(src,::XML::SAML2ASSERT_NS,L(AttributeValue));
820 src=saml::XML::getNextSiblingElement(src,::XML::SAML2ASSERT_NS,L(AttributeValue));
821 DOMElement* val=e->getOwnerDocument()->createElementNS(saml::XML::SAML_NS,L(AttributeValue));
822 DOMNamedNodeMap* attrs = src->getAttributes();
823 for (unsigned int j=0; j<attrs->getLength(); j++)
824 val->setAttributeNodeNS(static_cast<DOMAttr*>(e->getOwnerDocument()->importNode(attrs->item(j),true)));
825 while (src->hasChildNodes())
826 val->appendChild(src->getFirstChild());
827 copy->appendChild(val);
829 m_attrs.push_back(SAMLAttribute::getInstance(copy));
833 m_protocolEnum.push_back(Constants::SHIB_NS);
834 m_attrprofs.push_back(Constants::SHIB_ATTRIBUTE_NAMESPACE_URI);
836 DOMNodeList* nlist=e->getElementsByTagNameNS(::XML::SHIB_NS,SHIB_L(HandleService));
837 for (i=0; nlist && i<nlist->getLength(); i++) {
838 // Manufacture an endpoint for the "Shib" binding.
840 new Endpoint(Constants::SHIB_AUTHNREQUEST_PROFILE_URI,static_cast<DOMElement*>(nlist->item(i))->getAttributeNS(NULL,L(Location)))
843 // We're going to "mock up" a KeyDescriptor that contains the specified Name as a ds:KeyName.
844 DOMElement* kd=e->getOwnerDocument()->createElementNS(::XML::SAML2META_NS,SHIB_L(KeyDescriptor));
845 DOMElement* ki=e->getOwnerDocument()->createElementNS(saml::XML::XMLSIG_NS,L(KeyInfo));
846 DOMElement* kn=e->getOwnerDocument()->createElementNS(saml::XML::XMLSIG_NS,SHIB_L(KeyName));
848 e->getOwnerDocument()->createTextNode(
849 static_cast<DOMElement*>(nlist->item(i))->getAttributeNS(NULL,SHIB_L(Name))
854 kd->setAttributeNS(NULL,SHIB_L(use),SHIB_L(signing));
855 m_keys.push_back(new KeyDescriptor(kd));
860 XMLMetadataImpl::IDPRole::~IDPRole()
862 for_each(m_attrs.begin(),m_attrs.end(),cleanup<SAMLAttribute>);
865 XMLMetadataImpl::AARole::AARole(const EntityDescriptor* provider, time_t validUntil, const DOMElement* e)
866 : Role(provider,validUntil,e), ScopedRole(e)
868 // Check the root element namespace. If SAML2, assume it's the std schema.
869 if (!XMLString::compareString(e->getNamespaceURI(),::XML::SAML2META_NS)) {
871 DOMNodeList* nlist=e->getElementsByTagNameNS(::XML::SAML2META_NS,SHIB_L(AttributeService));
872 for (i=0; nlist && i<nlist->getLength(); i++)
873 m_query.add(new Endpoint(static_cast<DOMElement*>(nlist->item(i))));
875 nlist=e->getElementsByTagNameNS(::XML::SAML2META_NS,SHIB_L(AssertionIDRequestService));
876 for (i=0; nlist && i<nlist->getLength(); i++)
877 m_idreq.add(new Endpoint(static_cast<DOMElement*>(nlist->item(i))));
879 nlist=e->getElementsByTagNameNS(::XML::SAML2META_NS,SHIB_L(NameIDFormat));
880 for (i=0; nlist && i<nlist->getLength(); i++) {
881 DOMNode* n=nlist->item(i)->getFirstChild();
882 if (n) m_formats.push_back(n->getNodeValue());
885 nlist=e->getElementsByTagNameNS(::XML::SAML2META_NS,SHIB_L(AttributeProfile));
886 for (i=0; nlist && i<nlist->getLength(); i++) {
887 DOMNode* n=nlist->item(i)->getFirstChild();
888 if (n) m_attrprofs.push_back(n->getNodeValue());
891 nlist=e->getElementsByTagNameNS(::XML::SAML2ASSERT_NS,L(Attribute));
892 for (i=0; nlist && i<nlist->getLength(); i++) {
893 // For now, we need to convert these to plain SAML 1.1 attributes.
894 DOMElement* src=static_cast<DOMElement*>(nlist->item(i));
895 DOMElement* copy=e->getOwnerDocument()->createElementNS(saml::XML::SAML_NS,L(Attribute));
896 copy->setAttributeNS(NULL,L(AttributeName),src->getAttributeNS(NULL,SHIB_L(Name)));
897 copy->setAttributeNS(NULL,L(AttributeNamespace),src->getAttributeNS(NULL,SHIB_L(NameFormat)));
898 src=saml::XML::getFirstChildElement(src,::XML::SAML2ASSERT_NS,L(AttributeValue));
900 src=saml::XML::getNextSiblingElement(src,::XML::SAML2ASSERT_NS,L(AttributeValue));
901 DOMElement* val=e->getOwnerDocument()->createElementNS(saml::XML::SAML_NS,L(AttributeValue));
902 DOMNamedNodeMap* attrs = src->getAttributes();
903 for (unsigned int j=0; j<attrs->getLength(); j++)
904 val->setAttributeNodeNS(static_cast<DOMAttr*>(e->getOwnerDocument()->importNode(attrs->item(j),true)));
905 while (src->hasChildNodes())
906 val->appendChild(src->getFirstChild());
907 copy->appendChild(val);
909 m_attrs.push_back(SAMLAttribute::getInstance(copy));
913 // For old style, we just do SAML 1.1 compatibility with Shib handles.
914 m_protocolEnum.push_back(saml::XML::SAML11_PROTOCOL_ENUM);
915 m_formats.push_back(Constants::SHIB_NAMEID_FORMAT_URI);
916 m_attrprofs.push_back(Constants::SHIB_ATTRIBUTE_NAMESPACE_URI);
918 DOMNodeList* nlist=e->getElementsByTagNameNS(::XML::SHIB_NS,SHIB_L(AttributeAuthority));
919 for (i=0; nlist && i<nlist->getLength(); i++) {
920 // Manufacture an endpoint for the SOAP binding.
924 static_cast<DOMElement*>(nlist->item(i))->getAttributeNS(NULL,L(Location))
928 // We're going to "mock up" a KeyDescriptor that contains the specified Name as a ds:KeyName.
929 DOMElement* kd=e->getOwnerDocument()->createElementNS(::XML::SAML2META_NS,SHIB_L(KeyDescriptor));
930 DOMElement* ki=e->getOwnerDocument()->createElementNS(saml::XML::XMLSIG_NS,L(KeyInfo));
931 DOMElement* kn=e->getOwnerDocument()->createElementNS(saml::XML::XMLSIG_NS,SHIB_L(KeyName));
933 e->getOwnerDocument()->createTextNode(
934 static_cast<DOMElement*>(nlist->item(i))->getAttributeNS(NULL,SHIB_L(Name))
939 m_keys.push_back(new KeyDescriptor(kd));
944 XMLMetadataImpl::AARole::~AARole()
946 for_each(m_attrs.begin(),m_attrs.end(),cleanup<SAMLAttribute>);
949 XMLMetadataImpl::EntityDescriptor::EntityDescriptor(
950 const DOMElement* e, XMLMetadataImpl* wrapper, time_t validUntil, const IEntitiesDescriptor* parent
951 ) : m_root(e), m_parent(parent), m_org(NULL), m_validUntil(validUntil)
953 // Check the root element namespace. If SAML2, assume it's the std schema.
954 if (!XMLString::compareString(e->getNamespaceURI(),::XML::SAML2META_NS)) {
955 m_id=e->getAttributeNS(NULL,SHIB_L(entityID));
957 if (e->hasAttributeNS(NULL,SHIB_L(validUntil))) {
958 SAMLDateTime exp(e->getAttributeNS(NULL,SHIB_L(validUntil)));
960 m_validUntil=min(validUntil,exp.getEpoch());
963 DOMElement* child=saml::XML::getFirstChildElement(e);
965 // Process the various kinds of children that we care about...
966 if (saml::XML::isElementNamed(child,::XML::SAML2META_NS,SHIB_L(Extensions))) {
967 DOMElement* ext = saml::XML::getFirstChildElement(child,::XML::SHIBMETA_NS,SHIB_L(KeyAuthority));
969 m_keyauths.push_back(new KeyAuthority(ext));
970 ext = saml::XML::getNextSiblingElement(ext,::XML::SHIBMETA_NS,SHIB_L(KeyAuthority));
973 else if (saml::XML::isElementNamed(child,::XML::SAML2META_NS,SHIB_L(ContactPerson))) {
974 m_contacts.push_back(new ContactPerson(child));
976 else if (saml::XML::isElementNamed(child,::XML::SAML2META_NS,SHIB_L(Organization))) {
977 m_org=new Organization(child);
979 else if (saml::XML::isElementNamed(child,::XML::SAML2META_NS,SHIB_L(AdditionalMetadataLocation))) {
980 DOMNode* loc=child->getFirstChild();
983 pair<const XMLCh*,const XMLCh*>(child->getAttributeNS(NULL,::XML::Literals::_namespace),loc->getNodeValue())
986 else if (saml::XML::isElementNamed(child,::XML::SAML2META_NS,SHIB_L(IDPSSODescriptor))) {
987 if (wrapper->m_outer->verifySignature(child->getOwnerDocument(),child,false))
988 m_roles.push_back(new IDPRole(this,m_validUntil,child));
990 else if (saml::XML::isElementNamed(child,::XML::SAML2META_NS,SHIB_L(AttributeAuthorityDescriptor))) {
991 if (wrapper->m_outer->verifySignature(child->getOwnerDocument(),child,false))
992 m_roles.push_back(new AARole(this,m_validUntil,child));
994 child = saml::XML::getNextSiblingElement(child);
998 m_id=e->getAttributeNS(NULL,SHIB_L(Name));
999 auto_ptr<char> wrapper(toUTF8(e->getAttributeNS(NULL,SHIB_L(ErrorURL))));
1002 bool idp=false,aa=false; // only want to build a role once
1003 DOMElement* child=saml::XML::getFirstChildElement(e);
1005 // Process the various kinds of OriginSite children that we care about...
1006 if (saml::XML::isElementNamed(child,::XML::SHIB_NS,SHIB_L(Contact))) {
1007 m_contacts.push_back(new ContactPerson(child));
1009 else if (saml::XML::isElementNamed(child,::XML::SHIB_NS,SHIB_L(HandleService)) && !idp) {
1010 // Create the IDP role if needed.
1011 m_roles.push_back(new IDPRole(this, m_validUntil, e));
1014 else if (saml::XML::isElementNamed(child,::XML::SHIB_NS,SHIB_L(AttributeAuthority)) && !aa) {
1015 // Create the AA role if needed.
1016 m_roles.push_back(new AARole(this, m_validUntil, e));
1019 child = saml::XML::getNextSiblingElement(child);
1023 auto_ptr_char id(m_id);
1024 wrapper->m_sites.insert(pair<const string,const EntityDescriptor*>(id.get(),this));
1026 // Look for an IdP role, and register the artifact source ID and endpoints.
1027 const IDPRole* idp=NULL;
1028 for (vector<const IRoleDescriptor*>::const_iterator r=m_roles.begin(); r!=m_roles.end(); r++) {
1029 if (idp=dynamic_cast<const IDPRole*>(*r)) {
1030 if (idp->m_sourceId) {
1031 auto_ptr_char sourceid(idp->m_sourceId);
1032 wrapper->m_sources.insert(pair<const string,const EntityDescriptor*>(sourceid.get(),this));
1035 string sourceid=SAMLArtifact::toHex(SAMLArtifactType0001::generateSourceId(id.get()));
1036 wrapper->m_sources.insert(pair<const string,const EntityDescriptor*>(sourceid,this));
1038 Iterator<const IEndpoint*> locs=idp->getArtifactResolutionServiceManager()->getEndpoints();
1039 while (locs.hasNext()) {
1040 auto_ptr_char loc(locs.next()->getLocation());
1041 wrapper->m_sources.insert(pair<const string,const EntityDescriptor*>(loc.get(),this));
1047 const IIDPSSODescriptor* XMLMetadataImpl::EntityDescriptor::getIDPSSODescriptor(const XMLCh* protocol) const
1049 const IIDPSSODescriptor* ret=NULL;
1050 for (vector<const IRoleDescriptor*>::const_iterator i=m_roles.begin(); i!=m_roles.end(); i++) {
1051 if ((*i)->hasSupport(protocol) && (*i)->isValid() && (ret=dynamic_cast<const IIDPSSODescriptor*>(*i)))
1057 const IAttributeAuthorityDescriptor* XMLMetadataImpl::EntityDescriptor::getAttributeAuthorityDescriptor(const XMLCh* protocol) const
1059 const IAttributeAuthorityDescriptor* ret=NULL;
1060 for (vector<const IRoleDescriptor*>::const_iterator i=m_roles.begin(); i!=m_roles.end(); i++) {
1061 if ((*i)->hasSupport(protocol) && (*i)->isValid() && (ret=dynamic_cast<const IAttributeAuthorityDescriptor*>(*i)))
1067 XMLMetadataImpl::EntityDescriptor::~EntityDescriptor()
1070 for_each(m_contacts.begin(),m_contacts.end(),cleanup<IContactPerson>);
1071 for_each(m_roles.begin(),m_roles.end(),cleanup<IRoleDescriptor>);
1072 for_each(m_keyauths.begin(),m_keyauths.end(),cleanup<IKeyAuthority>);
1075 XMLMetadataImpl::EntitiesDescriptor::EntitiesDescriptor(
1076 const DOMElement* e, XMLMetadataImpl* wrapper, time_t validUntil, const IEntitiesDescriptor* parent
1077 ) : m_root(e), m_name(e->getAttributeNS(NULL,SHIB_L(Name))), m_parent(parent), m_validUntil(validUntil)
1079 // Check the root element namespace. If SAML2, assume it's the std schema.
1080 if (!XMLString::compareString(e->getNamespaceURI(),::XML::SAML2META_NS)) {
1082 if (e->hasAttributeNS(NULL,SHIB_L(validUntil))) {
1083 SAMLDateTime exp(e->getAttributeNS(NULL,SHIB_L(validUntil)));
1084 exp.parseDateTime();
1085 m_validUntil=min(validUntil,exp.getEpoch());
1088 e=saml::XML::getFirstChildElement(e);
1090 if (saml::XML::isElementNamed(e,::XML::SAML2META_NS,SHIB_L(Extensions))) {
1091 DOMElement* ext = saml::XML::getFirstChildElement(e,::XML::SHIBMETA_NS,SHIB_L(KeyAuthority));
1093 m_keyauths.push_back(new KeyAuthority(ext));
1094 ext = saml::XML::getNextSiblingElement(ext,::XML::SHIBMETA_NS,SHIB_L(KeyAuthority));
1097 else if (saml::XML::isElementNamed(e,::XML::SAML2META_NS,SHIB_L(EntitiesDescriptor))) {
1098 if (wrapper->m_outer->verifySignature(e->getOwnerDocument(),e,false))
1099 m_groups.push_back(new EntitiesDescriptor(e,wrapper,m_validUntil,this));
1101 else if (saml::XML::isElementNamed(e,::XML::SAML2META_NS,SHIB_L(EntityDescriptor))) {
1102 if (wrapper->m_outer->verifySignature(e->getOwnerDocument(),e,false))
1103 m_providers.push_back(new EntityDescriptor(e,wrapper,m_validUntil,this));
1105 e=saml::XML::getNextSiblingElement(e);
1109 e=saml::XML::getFirstChildElement(e);
1111 if (saml::XML::isElementNamed(e,::XML::SHIB_NS,SHIB_L(SiteGroup))) {
1112 if (wrapper->m_outer->verifySignature(e->getOwnerDocument(),e,false))
1113 m_groups.push_back(new EntitiesDescriptor(e,wrapper,m_validUntil,this));
1115 else if (saml::XML::isElementNamed(e,::XML::SHIB_NS,SHIB_L(OriginSite)))
1116 m_providers.push_back(new EntityDescriptor(e,wrapper,m_validUntil,this));
1117 e=saml::XML::getNextSiblingElement(e);
1121 if (!saml::XML::isEmpty(m_name)) {
1122 auto_ptr_char n(m_name);
1123 wrapper->m_groups.insert(pair<const string,const EntitiesDescriptor*>(n.get(),this));
1129 XMLMetadataImpl::EntitiesDescriptor::~EntitiesDescriptor()
1131 for_each(m_providers.begin(),m_providers.end(),cleanup<IEntityDescriptor>);
1132 for_each(m_groups.begin(),m_groups.end(),cleanup<IEntitiesDescriptor>);
1133 for_each(m_keyauths.begin(),m_keyauths.end(),cleanup<IKeyAuthority>);
1136 void XMLMetadataImpl::init()
1141 Category& log=Category::getInstance(XMLPROVIDERS_LOGCAT".Metadata");
1145 if (saml::XML::isElementNamed(m_root,::XML::SAML2META_NS,SHIB_L(EntitiesDescriptor))) {
1146 if (m_outer->verifySignature(m_root->getOwnerDocument(),m_root,true))
1147 m_rootGroup=new EntitiesDescriptor(m_root,this);
1149 else if (saml::XML::isElementNamed(m_root,::XML::SAML2META_NS,SHIB_L(EntityDescriptor))) {
1150 if (m_outer->verifySignature(m_root->getOwnerDocument(),m_root,true))
1151 m_rootProvider=new EntityDescriptor(m_root,this);
1153 else if (saml::XML::isElementNamed(m_root,::XML::SHIB_NS,SHIB_L(SiteGroup))) {
1154 if (m_outer->verifySignature(m_root->getOwnerDocument(),m_root,true))
1155 m_rootGroup=new EntitiesDescriptor(m_root,this);
1157 else if (saml::XML::isElementNamed(m_root,::XML::SHIB_NS,SHIB_L(OriginSite))) {
1158 if (m_outer->verifySignature(m_root->getOwnerDocument(),m_root,true))
1159 m_rootProvider=new EntityDescriptor(m_root,this);
1162 log.error("Construction requires a valid SAML metadata file");
1163 throw MetadataException("Construction requires a valid SAML metadata file");
1166 catch (SAMLException& e)
1168 log.errorStream() << "Error while parsing SAML metadata: " << e.what() << CategoryStream::ENDLINE;
1169 this->~XMLMetadataImpl();
1175 log.error("Unexpected error while parsing SAML metadata");
1176 this->~XMLMetadataImpl();
1181 if (!m_rootGroup && !m_rootProvider) {
1182 log.error("Metadata file contained no valid information");
1183 throw MetadataException("Metadata file contained no valid information");
1187 XMLMetadataImpl::~XMLMetadataImpl()
1190 delete m_rootProvider;
1193 XMLMetadata::XMLMetadata(const DOMElement* e) : ReloadableXMLFile(e), m_exclusions(true), m_verify(false), m_credResolver(NULL)
1195 static const XMLCh uri[] = { chLatin_u, chLatin_r, chLatin_i, chNull };
1196 if (e->hasAttributeNS(NULL,uri)) {
1197 // First check for explicit enablement of entities.
1198 DOMNodeList* nlist=e->getElementsByTagName(SHIB_L(Include));
1199 for (unsigned int i=0; nlist && i<nlist->getLength(); i++) {
1200 if (nlist->item(i)->hasChildNodes()) {
1201 auto_ptr_char temp(nlist->item(i)->getFirstChild()->getNodeValue());
1203 m_set.insert(temp.get());
1208 // If there was no explicit enablement, build a set of exclusions.
1210 nlist=e->getElementsByTagName(SHIB_L(Exclude));
1211 for (unsigned int j=0; nlist && j<nlist->getLength(); j++) {
1212 if (nlist->item(j)->hasChildNodes()) {
1213 auto_ptr_char temp(nlist->item(j)->getFirstChild()->getNodeValue());
1215 m_set.insert(temp.get());
1221 const XMLCh* v=e->getAttributeNS(NULL,SHIB_L(verify));
1222 m_verify=(v && (*v==chLatin_t || *v==chDigit_1));
1225 DOMElement* r=saml::XML::getFirstChildElement(e,::XML::CREDS_NS,SHIB_L(FileResolver));
1227 cr_type="edu.internet2.middleware.shibboleth.common.Credentials.FileCredentialResolver";
1229 r=saml::XML::getFirstChildElement(e,::XML::CREDS_NS,SHIB_L(CustomResolver));
1231 auto_ptr_char c(r->getAttributeNS(NULL,SHIB_L(Class)));
1236 if (!cr_type.empty()) {
1238 IPlugIn* plugin=SAMLConfig::getConfig().getPlugMgr().newPlugin(cr_type.c_str(),r);
1239 ICredResolver* cr=dynamic_cast<ICredResolver*>(plugin);
1243 Category::getInstance(XMLPROVIDERS_LOGCAT".Metadata").error("plugin was not a credential resolver");
1245 throw UnsupportedExtensionException("plugin was not a credential resolver");
1248 catch (SAMLException& e) {
1249 Category::getInstance(XMLPROVIDERS_LOGCAT".Metadata").error("failed to instantiate credential resolver: %s", e.what());
1254 if (m_verify && !m_credResolver) {
1255 delete m_credResolver;
1256 throw MalformedException("Metadata provider told to verify signatures, but a verification key is not available.");
1260 bool XMLMetadata::verifySignature(DOMDocument* doc, const DOMElement* parent, bool failUnsigned) const
1266 saml::NDC ndc("verifySignature");
1268 Category& log=Category::getInstance(XMLPROVIDERS_LOGCAT".Metadata");
1270 DOMElement* sigNode=saml::XML::getFirstChildElement(parent,saml::XML::XMLSIG_NS,L(Signature));
1273 log.error("rejecting unsigned element");
1279 XSECCryptoX509* cert=NULL;
1280 Iterator<XSECCryptoX509*> certs=m_credResolver->getCertificates();
1281 if (certs.hasNext())
1284 log.error("unable to find any certificates to use in verifying signature");
1288 static const XMLCh ID[]={chLatin_I, chLatin_D, chNull};
1289 static const XMLCh null[]={chDoubleQuote, chDoubleQuote, chNull};
1291 // Load the signature.
1293 DSIGSignature* sig=NULL;
1295 sig=prov.newSignatureFromDOM(doc,sigNode);
1299 const XMLCh* URI=NULL;
1301 // Verify the signature coverage.
1302 DSIGReferenceList* refs=sig->getReferenceList();
1303 if (sig->getSignatureMethod()==SIGNATURE_RSA && refs && refs->getSize()==1) {
1304 DSIGReference* ref=refs->item(0);
1307 if (!URI || !*URI || (*URI==chPound &&
1308 !XMLString::compareString(&URI[1],static_cast<DOMElement*>(sigNode->getParentNode())->getAttributeNS(NULL,ID)))) {
1309 DSIGTransformList* tlist=ref->getTransforms();
1310 for (unsigned int i=0; tlist && i<tlist->getSize(); i++) {
1311 if (tlist->item(i)->getTransformType()==TRANSFORM_ENVELOPED_SIGNATURE)
1313 else if (tlist->item(i)->getTransformType()!=TRANSFORM_EXC_C14N &&
1314 tlist->item(i)->getTransformType()!=TRANSFORM_C14N) {
1324 auto_ptr_char temp((URI && *URI) ? URI : null);
1325 log.error("detected an invalid signature profile (Reference URI was %s)",temp.get());
1329 sig->setSigningKey(cert->clonePublicKey());
1330 if (!sig->verify()) {
1331 auto_ptr_char temp((URI && *URI) ? URI : null);
1332 log.error("detected an invalid signature value (Reference URI was %s)",temp.get());
1336 prov.releaseSignature(sig);
1338 catch(XSECException& e) {
1339 auto_ptr_char msg(e.getMsg());
1340 log.errorStream() << "caught XMLSec exception while verifying metadata signature: " << msg.get() << CategoryStream::ENDLINE;
1342 prov.releaseSignature(sig);
1345 catch(XSECCryptoException& e) {
1346 log.errorStream() << "caught XMLSecCrypto exception while verifying metadata signature: " << e.getMsg() << CategoryStream::ENDLINE;
1348 prov.releaseSignature(sig);
1353 prov.releaseSignature(sig);
1354 log.error("caught unknown exception while verifying metadata signature");
1360 const IEntityDescriptor* XMLMetadata::lookup(const char* providerId, bool strict) const
1362 if (strict && m_exclusions && m_set.find(providerId)!=m_set.end())
1364 else if (strict && !m_exclusions && m_set.find(providerId)==m_set.end())
1367 XMLMetadataImpl* impl=dynamic_cast<XMLMetadataImpl*>(getImplementation());
1368 pair<XMLMetadataImpl::sitemap_t::iterator,XMLMetadataImpl::sitemap_t::iterator> range=
1369 impl->m_sites.equal_range(providerId);
1371 time_t now=time(NULL);
1372 for (XMLMetadataImpl::sitemap_t::const_iterator i=range.first; i!=range.second; i++)
1373 if (now < i->second->getValidUntil())
1376 if (!strict && range.first!=range.second)
1377 return range.first->second;
1382 const IEntityDescriptor* XMLMetadata::lookup(const XMLCh* providerId, bool strict) const
1384 auto_ptr_char temp(providerId);
1385 return lookup(temp.get(),strict);
1388 const IEntityDescriptor* XMLMetadata::lookup(const SAMLArtifact* artifact) const
1390 time_t now=time(NULL);
1391 XMLMetadataImpl* impl=dynamic_cast<XMLMetadataImpl*>(getImplementation());
1392 pair<XMLMetadataImpl::sitemap_t::iterator,XMLMetadataImpl::sitemap_t::iterator> range;
1394 // Depends on type of artifact.
1395 const SAMLArtifactType0001* type1=dynamic_cast<const SAMLArtifactType0001*>(artifact);
1397 range=impl->m_sources.equal_range(SAMLArtifact::toHex(type1->getSourceID()));
1400 const SAMLArtifactType0002* type2=dynamic_cast<const SAMLArtifactType0002*>(artifact);
1402 range=impl->m_sources.equal_range(type2->getSourceLocation());
1408 // Check exclude list.
1409 if (range.first!=range.second) {
1410 auto_ptr_char id(range.first->second->getId());
1411 if (m_exclusions && m_set.find(id.get())!=m_set.end())
1413 else if (!m_exclusions && m_set.find(id.get())==m_set.end())
1416 for (XMLMetadataImpl::sitemap_t::iterator i=range.first; i!=range.second; i++)
1417 if (now < i->second->getValidUntil())
1424 const IEntitiesDescriptor* XMLMetadata::lookupGroup(const char* name, bool strict) const
1426 if (strict && m_exclusions && m_set.find(name)!=m_set.end())
1428 else if (strict && !m_exclusions && m_set.find(name)==m_set.end())
1431 XMLMetadataImpl* impl=dynamic_cast<XMLMetadataImpl*>(getImplementation());
1432 pair<XMLMetadataImpl::groupmap_t::iterator,XMLMetadataImpl::groupmap_t::iterator> range=
1433 impl->m_groups.equal_range(name);
1435 time_t now=time(NULL);
1436 for (XMLMetadataImpl::groupmap_t::iterator i=range.first; i!=range.second; i++)
1437 if (now < i->second->getValidUntil())
1440 if (!strict && range.first!=range.second)
1441 return range.first->second;
1446 const IEntitiesDescriptor* XMLMetadata::lookupGroup(const XMLCh* name, bool strict) const
1448 auto_ptr_char temp(name);
1449 return lookupGroup(temp.get(),strict);
1452 pair<const IEntitiesDescriptor*,const IEntityDescriptor*> XMLMetadata::getRoot() const
1454 XMLMetadataImpl* impl=dynamic_cast<XMLMetadataImpl*>(getImplementation());
1455 return pair<const IEntitiesDescriptor*,const IEntityDescriptor*>(impl->m_rootGroup,impl->m_rootProvider);