2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
22 * MetadataGenerator.cpp
24 * Handler for generating "approximate" metadata based on SP configuration.
28 #include "Application.h"
29 #include "exceptions.h"
30 #include "ServiceProvider.h"
31 #include "SPRequest.h"
32 #include "handler/AbstractHandler.h"
33 #include "handler/RemotedHandler.h"
36 # include "attribute/resolver/AttributeExtractor.h"
37 # include "metadata/MetadataProviderCriteria.h"
38 # include <saml/exceptions.h>
39 # include <saml/SAMLConfig.h>
40 # include <saml/signature/ContentReference.h>
41 # include <saml/saml2/metadata/Metadata.h>
42 # include <saml/saml2/metadata/MetadataProvider.h>
43 # include <xmltooling/XMLToolingConfig.h>
44 # include <xmltooling/security/Credential.h>
45 # include <xmltooling/security/CredentialCriteria.h>
46 # include <xmltooling/security/SecurityHelper.h>
47 # include <xmltooling/signature/Signature.h>
48 # include <xmltooling/util/ParserPool.h>
49 # include <xmltooling/util/PathResolver.h>
50 # include <xercesc/framework/LocalFileInputSource.hpp>
51 # include <xercesc/framework/Wrapper4InputSource.hpp>
55 using namespace shibsp;
57 using namespace opensaml::saml2md;
58 using namespace opensaml;
59 using namespace xmlsignature;
61 using namespace xmltooling;
66 #if defined (_MSC_VER)
67 #pragma warning( push )
68 #pragma warning( disable : 4250 )
71 class SHIBSP_DLLLOCAL Blocker : public DOMNodeFilter
74 #ifdef SHIBSP_XERCESC_SHORT_ACCEPTNODE
79 acceptNode(const DOMNode* node) const {
84 static SHIBSP_DLLLOCAL Blocker g_Blocker;
86 class SHIBSP_API MetadataGenerator : public AbstractHandler, public RemotedHandler
89 MetadataGenerator(const DOMElement* e, const char* appId);
90 virtual ~MetadataGenerator() {
95 for_each(m_contacts.begin(), m_contacts.end(), xmltooling::cleanup<ContactPerson>());
96 for_each(m_formats.begin(), m_formats.end(), xmltooling::cleanup<NameIDFormat>());
97 for_each(m_reqAttrs.begin(), m_reqAttrs.end(), xmltooling::cleanup<RequestedAttribute>());
98 for_each(m_attrConsumers.begin(), m_attrConsumers.end(), xmltooling::cleanup<AttributeConsumingService>());
102 pair<bool,long> run(SPRequest& request, bool isHandler=true) const;
103 void receive(DDF& in, ostream& out);
106 pair<bool,long> processMessage(
107 const Application& application,
108 const char* handlerURL,
109 const char* entityID,
110 HTTPResponse& httpResponse
116 short m_http,m_https;
117 vector<string> m_bases;
120 EntityAttributes* m_entityAttrs;
121 vector<ContactPerson*> m_contacts;
122 vector<NameIDFormat*> m_formats;
123 vector<RequestedAttribute*> m_reqAttrs;
124 vector<AttributeConsumingService*> m_attrConsumers;
128 #if defined (_MSC_VER)
129 #pragma warning( pop )
132 Handler* SHIBSP_DLLLOCAL MetadataGeneratorFactory(const pair<const DOMElement*,const char*>& p)
134 return new MetadataGenerator(p.first, p.second);
139 MetadataGenerator::MetadataGenerator(const DOMElement* e, const char* appId)
140 : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".MetadataGenerator"), &g_Blocker)
142 ,m_http(0), m_https(0), m_uiinfo(nullptr), m_org(nullptr), m_entityAttrs(nullptr)
145 string address(appId);
146 address += getString("Location").second;
147 setAddress(address.c_str());
148 if (SPConfig::getConfig().isEnabled(SPConfig::InProcess)) {
149 pair<bool,const char*> acl = getString("acl");
151 string aclbuf=acl.second;
153 for (unsigned int i=0; i < aclbuf.length(); i++) {
154 if (aclbuf.at(i)==' ') {
155 m_acl.insert(aclbuf.substr(j, i-j));
159 m_acl.insert(aclbuf.substr(j, aclbuf.length()-j));
164 static XMLCh EndpointBase[] = UNICODE_LITERAL_12(E,n,d,p,o,i,n,t,B,a,s,e);
166 pair<bool,const char*> salt = getString("salt");
168 m_salt = salt.second;
170 pair<bool,bool> flag = getBool("http");
172 m_http = flag.second ? 1 : -1;
173 flag = getBool("https");
175 m_https = flag.second ? 1 : -1;
177 e = XMLHelper::getFirstChildElement(e);
179 if (XMLString::equals(e->getLocalName(), EndpointBase) && e->hasChildNodes()) {
180 auto_ptr_char base(e->getFirstChild()->getNodeValue());
181 if (base.get() && *base.get())
182 m_bases.push_back(base.get());
185 // Try and parse the object.
186 auto_ptr<XMLObject> child(XMLObjectBuilder::buildOneFromElement(const_cast<DOMElement*>(e)));
187 ContactPerson* cp = dynamic_cast<ContactPerson*>(child.get());
190 m_contacts.push_back(cp);
193 NameIDFormat* nif = dynamic_cast<NameIDFormat*>(child.get());
196 m_formats.push_back(nif);
199 RequestedAttribute* req = dynamic_cast<RequestedAttribute*>(child.get());
202 m_reqAttrs.push_back(req);
205 AttributeConsumingService* acs = dynamic_cast<AttributeConsumingService*>(child.get());
208 m_attrConsumers.push_back(acs);
211 UIInfo* info = dynamic_cast<UIInfo*>(child.get());
218 m_log.warn("skipping duplicate UIInfo element");
222 Organization* org = dynamic_cast<Organization*>(child.get());
229 m_log.warn("skipping duplicate Organization element");
233 EntityAttributes* ea = dynamic_cast<EntityAttributes*>(child.get());
235 if (!m_entityAttrs) {
240 m_log.warn("skipping duplicate EntityAttributes element");
250 e = XMLHelper::getNextSiblingElement(e);
255 pair<bool,long> MetadataGenerator::run(SPRequest& request, bool isHandler) const
257 SPConfig& conf = SPConfig::getConfig();
258 if (conf.isEnabled(SPConfig::InProcess)) {
259 if (!m_acl.empty() && m_acl.count(request.getRemoteAddr()) == 0) {
260 m_log.error("request for metadata blocked from invalid address (%s)", request.getRemoteAddr().c_str());
261 istringstream msg("Metadata Request Blocked");
262 return make_pair(true,request.sendResponse(msg, HTTPResponse::XMLTOOLING_HTTP_STATUS_FORBIDDEN));
267 if (conf.isEnabled(SPConfig::OutOfProcess)) {
268 // When out of process, we run natively and directly process the message.
269 return processMessage(request.getApplication(), request.getHandlerURL(), request.getParameter("entityID"), request);
272 // When not out of process, we remote all the message processing.
273 DDF out,in = DDF(m_address.c_str());
274 in.addmember("application_id").string(request.getApplication().getId());
275 in.addmember("handler_url").string(request.getHandlerURL());
276 if (request.getParameter("entityID"))
277 in.addmember("entity_id").string(request.getParameter("entityID"));
278 DDFJanitor jin(in), jout(out);
280 out=request.getServiceProvider().getListenerService()->send(in);
281 return unwrap(request, out);
284 catch (exception& ex) {
285 m_log.error("error while processing request: %s", ex.what());
286 istringstream msg("Metadata Request Failed");
287 return make_pair(true,request.sendResponse(msg, HTTPResponse::XMLTOOLING_HTTP_STATUS_ERROR));
291 void MetadataGenerator::receive(DDF& in, ostream& out)
294 const char* aid=in["application_id"].string();
295 const char* hurl=in["handler_url"].string();
296 const Application* app=aid ? SPConfig::getConfig().getServiceProvider()->getApplication(aid) : nullptr;
298 // Something's horribly wrong.
299 m_log.error("couldn't find application (%s) for metadata request", aid ? aid : "(missing)");
300 throw ConfigurationException("Unable to locate application for metadata request, deleted?");
303 throw ConfigurationException("Missing handler_url parameter in remoted method call.");
306 // Wrap a response shim.
308 DDFJanitor jout(ret);
309 auto_ptr<HTTPResponse> resp(getResponse(ret));
311 // Since we're remoted, the result should either be a throw, a false/0 return,
312 // which we just return as an empty structure, or a response/redirect,
313 // which we capture in the facade and send back.
314 processMessage(*app, hurl, in["entity_id"].string(), *resp.get());
318 pair<bool,long> MetadataGenerator::processMessage(
319 const Application& application, const char* handlerURL, const char* entityID, HTTPResponse& httpResponse
323 m_log.debug("processing metadata request");
325 const PropertySet* relyingParty=nullptr;
327 MetadataProvider* m=application.getMetadataProvider();
329 MetadataProviderCriteria mc(application, entityID);
330 relyingParty = application.getRelyingParty(m->getEntityDescriptor(mc).first);
333 relyingParty = &application;
336 EntityDescriptor* entity;
337 pair<bool,const char*> prop = getString("template");
339 // Load a template to use for our metadata.
340 string templ(prop.second);
341 XMLToolingConfig::getConfig().getPathResolver()->resolve(templ, PathResolver::XMLTOOLING_CFG_FILE);
342 auto_ptr_XMLCh widenit(templ.c_str());
343 LocalFileInputSource src(widenit.get());
344 Wrapper4InputSource dsrc(&src,false);
345 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
346 XercesJanitor<DOMDocument> docjan(doc);
347 auto_ptr<XMLObject> xmlobj(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
349 entity = dynamic_cast<EntityDescriptor*>(xmlobj.get());
351 throw ConfigurationException("Template file ($1) did not contain an EntityDescriptor", params(1, templ.c_str()));
355 entity = EntityDescriptorBuilder::buildEntityDescriptor();
358 if (!entity->getID()) {
359 string hashinput = m_salt + relyingParty->getString("entityID").second;
360 string hashed = '_' + SecurityHelper::doHash("SHA1", hashinput.c_str(), hashinput.length());
361 auto_ptr_XMLCh widenit(hashed.c_str());
362 entity->setID(widenit.get());
365 auto_ptr<EntityDescriptor> wrapper(entity);
366 pair<bool,unsigned int> cache = getUnsignedInt("cacheDuration");
368 entity->setCacheDuration(cache.second);
370 cache = getUnsignedInt("validUntil");
372 entity->setValidUntil(time(nullptr) + cache.second);
373 entity->setEntityID(relyingParty->getXMLString("entityID").second);
375 if (m_org && !entity->getOrganization())
376 entity->setOrganization(m_org->cloneOrganization());
378 for (vector<ContactPerson*>::const_iterator cp = m_contacts.begin(); cp != m_contacts.end(); ++cp)
379 entity->getContactPersons().push_back((*cp)->cloneContactPerson());
382 if (!entity->getExtensions())
383 entity->setExtensions(ExtensionsBuilder::buildExtensions());
384 entity->getExtensions()->getUnknownXMLObjects().push_back(m_entityAttrs->cloneEntityAttributes());
387 SPSSODescriptor* role;
388 if (entity->getSPSSODescriptors().empty()) {
389 role = SPSSODescriptorBuilder::buildSPSSODescriptor();
390 entity->getSPSSODescriptors().push_back(role);
393 role = entity->getSPSSODescriptors().front();
396 for (vector<NameIDFormat*>::const_iterator nif = m_formats.begin(); nif != m_formats.end(); ++nif)
397 role->getNameIDFormats().push_back((*nif)->cloneNameIDFormat());
400 if (!role->getExtensions())
401 role->setExtensions(ExtensionsBuilder::buildExtensions());
402 role->getExtensions()->getUnknownXMLObjects().push_back(m_uiinfo->cloneUIInfo());
405 for (vector<AttributeConsumingService*>::const_iterator acs = m_attrConsumers.begin(); acs != m_attrConsumers.end(); ++acs)
406 role->getAttributeConsumingServices().push_back((*acs)->cloneAttributeConsumingService());
408 if (!m_reqAttrs.empty()) {
410 const vector<AttributeConsumingService*>& svcs = const_cast<const SPSSODescriptor*>(role)->getAttributeConsumingServices();
411 for (vector<AttributeConsumingService*>::const_iterator s =svcs.begin(); s != svcs.end(); ++s) {
412 pair<bool,int> i = (*s)->getIndex();
413 if (i.first && index == i.second)
414 index = i.second + 1;
416 AttributeConsumingService* svc = AttributeConsumingServiceBuilder::buildAttributeConsumingService();
417 role->getAttributeConsumingServices().push_back(svc);
418 svc->setIndex(index);
419 ServiceName* sn = ServiceNameBuilder::buildServiceName();
420 svc->getServiceNames().push_back(sn);
421 sn->setName(entity->getEntityID());
422 static const XMLCh english[] = UNICODE_LITERAL_2(e,n);
423 sn->setLang(english);
424 for (vector<RequestedAttribute*>::const_iterator req = m_reqAttrs.begin(); req != m_reqAttrs.end(); ++req)
425 svc->getRequestedAttributes().push_back((*req)->cloneRequestedAttribute());
429 prop = relyingParty->getString("signing");
430 if (prop.first && (!strcmp(prop.second,"true") || !strcmp(prop.second,"front")))
431 role->AuthnRequestsSigned(true);
432 pair<bool,bool> flagprop = relyingParty->getBool("requireSignedAssertions");
433 if (flagprop.first && flagprop.second)
434 role->WantAssertionsSigned(true);
436 // Ask each handler to generate itself.
437 vector<const Handler*> handlers;
438 application.getHandlers(handlers);
439 for (vector<const Handler*>::const_iterator h = handlers.begin(); h != handlers.end(); ++h) {
440 if (m_bases.empty()) {
441 if (strncmp(handlerURL, "https", 5) == 0) {
443 (*h)->generateMetadata(*role, handlerURL);
445 string temp(handlerURL);
447 (*h)->generateMetadata(*role, temp.c_str());
452 (*h)->generateMetadata(*role, handlerURL);
454 string temp(handlerURL);
455 temp.insert(temp.begin() + 4, 's');
456 (*h)->generateMetadata(*role, temp.c_str());
461 for (vector<string>::const_iterator b = m_bases.begin(); b != m_bases.end(); ++b)
462 (*h)->generateMetadata(*role, b->c_str());
466 AttributeExtractor* extractor = application.getAttributeExtractor();
468 Locker extlocker(extractor);
469 extractor->generateMetadata(*role);
472 CredentialResolver* credResolver=application.getCredentialResolver();
474 Locker credLocker(credResolver);
475 CredentialCriteria cc;
476 prop = relyingParty->getString("keyName");
478 cc.getKeyNames().insert(prop.second);
479 vector<const Credential*> signingcreds,enccreds;
480 cc.setUsage(Credential::SIGNING_CREDENTIAL);
481 credResolver->resolve(signingcreds, &cc);
482 cc.setUsage(Credential::ENCRYPTION_CREDENTIAL);
483 credResolver->resolve(enccreds, &cc);
485 for (vector<const Credential*>::const_iterator c = signingcreds.begin(); c != signingcreds.end(); ++c) {
486 KeyInfo* kinfo = (*c)->getKeyInfo();
488 KeyDescriptor* kd = KeyDescriptorBuilder::buildKeyDescriptor();
489 kd->setKeyInfo(kinfo);
490 const XMLCh* use = KeyDescriptor::KEYTYPE_SIGNING;
491 for (vector<const Credential*>::iterator match = enccreds.begin(); match != enccreds.end(); ++match) {
494 enccreds.erase(match);
499 role->getKeyDescriptors().push_back(kd);
503 for (vector<const Credential*>::const_iterator c = enccreds.begin(); c != enccreds.end(); ++c) {
504 KeyInfo* kinfo = (*c)->getKeyInfo();
506 KeyDescriptor* kd = KeyDescriptorBuilder::buildKeyDescriptor();
507 kd->setUse(KeyDescriptor::KEYTYPE_ENCRYPTION);
508 kd->setKeyInfo(kinfo);
509 role->getKeyDescriptors().push_back(kd);
514 // Stream for response.
518 pair<bool,bool> flag = getBool("signing");
519 if (flag.first && flag.second) {
521 Locker credLocker(credResolver);
522 // Fill in criteria to use.
523 CredentialCriteria cc;
524 cc.setUsage(Credential::SIGNING_CREDENTIAL);
525 prop = getString("keyName");
527 cc.getKeyNames().insert(prop.second);
528 pair<bool,const XMLCh*> sigalg = getXMLString("signingAlg");
529 pair<bool,const XMLCh*> digalg = getXMLString("digestAlg");
531 cc.setXMLAlgorithm(sigalg.second);
532 const Credential* cred = credResolver->resolve(&cc);
534 throw XMLSecurityException("Unable to obtain signing credential to use.");
536 // Pretty-print it first and then read it back in.
538 XMLHelper::serialize(entity->marshall(), pretty, true);
539 DOMDocument* prettydoc = XMLToolingConfig::getConfig().getParser().parse(pretty);
540 auto_ptr<XMLObject> prettyentity(XMLObjectBuilder::buildOneFromElement(prettydoc->getDocumentElement(), true));
542 Signature* sig = SignatureBuilder::buildSignature();
543 dynamic_cast<EntityDescriptor*>(prettyentity.get())->setSignature(sig);
545 sig->setSignatureAlgorithm(sigalg.second);
547 opensaml::ContentReference* cr = dynamic_cast<opensaml::ContentReference*>(sig->getContentReference());
549 cr->setDigestAlgorithm(digalg.second);
552 // Sign while marshalling.
553 vector<Signature*> sigs(1,sig);
554 prettyentity->marshall(prettydoc,&sigs,cred);
558 throw FatalProfileException("Can't self-sign metadata, no credential resolver found.");
562 // Pretty-print it directly to client.
563 XMLHelper::serialize(entity->marshall(), s, true);
566 prop = getString("mimeType");
567 httpResponse.setContentType(prop.first ? prop.second : "application/samlmetadata+xml");
568 return make_pair(true, httpResponse.sendResponse(s));
570 return make_pair(false,0L);