2 * Copyright 2001-2010 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 * MetadataGenerator.cpp
20 * Handler for generating "approximate" metadata based on SP configuration.
24 #include "Application.h"
25 #include "exceptions.h"
26 #include "ServiceProvider.h"
27 #include "SPRequest.h"
28 #include "handler/AbstractHandler.h"
29 #include "handler/RemotedHandler.h"
32 # include "attribute/resolver/AttributeExtractor.h"
33 # include "metadata/MetadataProviderCriteria.h"
34 # include <saml/exceptions.h>
35 # include <saml/SAMLConfig.h>
36 # include <saml/signature/ContentReference.h>
37 # include <saml/saml2/metadata/Metadata.h>
38 # include <saml/saml2/metadata/MetadataProvider.h>
39 # include <xmltooling/XMLToolingConfig.h>
40 # include <xmltooling/security/Credential.h>
41 # include <xmltooling/security/CredentialCriteria.h>
42 # include <xmltooling/security/SecurityHelper.h>
43 # include <xmltooling/signature/Signature.h>
44 # include <xmltooling/util/ParserPool.h>
45 # include <xmltooling/util/PathResolver.h>
46 # include <xercesc/framework/LocalFileInputSource.hpp>
47 # include <xercesc/framework/Wrapper4InputSource.hpp>
51 using namespace shibsp;
53 using namespace opensaml::saml2md;
54 using namespace opensaml;
55 using namespace xmlsignature;
57 using namespace xmltooling;
62 #if defined (_MSC_VER)
63 #pragma warning( push )
64 #pragma warning( disable : 4250 )
67 class SHIBSP_DLLLOCAL Blocker : public DOMNodeFilter
70 #ifdef SHIBSP_XERCESC_SHORT_ACCEPTNODE
75 acceptNode(const DOMNode* node) const {
80 static SHIBSP_DLLLOCAL Blocker g_Blocker;
82 class SHIBSP_API MetadataGenerator : public AbstractHandler, public RemotedHandler
85 MetadataGenerator(const DOMElement* e, const char* appId);
86 virtual ~MetadataGenerator() {
91 for_each(m_contacts.begin(), m_contacts.end(), xmltooling::cleanup<ContactPerson>());
92 for_each(m_reqAttrs.begin(), m_reqAttrs.end(), xmltooling::cleanup<RequestedAttribute>());
93 for_each(m_attrConsumers.begin(), m_attrConsumers.end(), xmltooling::cleanup<AttributeConsumingService>());
97 pair<bool,long> run(SPRequest& request, bool isHandler=true) const;
98 void receive(DDF& in, ostream& out);
101 pair<bool,long> processMessage(
102 const Application& application,
103 const char* handlerURL,
104 const char* entityID,
105 HTTPResponse& httpResponse
111 short m_http,m_https;
112 vector<string> m_bases;
115 EntityAttributes* m_entityAttrs;
116 vector<ContactPerson*> m_contacts;
117 vector<RequestedAttribute*> m_reqAttrs;
118 vector<AttributeConsumingService*> m_attrConsumers;
122 #if defined (_MSC_VER)
123 #pragma warning( pop )
126 Handler* SHIBSP_DLLLOCAL MetadataGeneratorFactory(const pair<const DOMElement*,const char*>& p)
128 return new MetadataGenerator(p.first, p.second);
133 MetadataGenerator::MetadataGenerator(const DOMElement* e, const char* appId)
134 : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".MetadataGenerator"), &g_Blocker)
136 ,m_http(0), m_https(0), m_uiinfo(nullptr), m_org(nullptr), m_entityAttrs(nullptr)
139 string address(appId);
140 address += getString("Location").second;
141 setAddress(address.c_str());
142 if (SPConfig::getConfig().isEnabled(SPConfig::InProcess)) {
143 pair<bool,const char*> acl = getString("acl");
145 string aclbuf=acl.second;
147 for (unsigned int i=0; i < aclbuf.length(); i++) {
148 if (aclbuf.at(i)==' ') {
149 m_acl.insert(aclbuf.substr(j, i-j));
153 m_acl.insert(aclbuf.substr(j, aclbuf.length()-j));
158 static XMLCh EndpointBase[] = UNICODE_LITERAL_12(E,n,d,p,o,i,n,t,B,a,s,e);
160 pair<bool,const char*> salt = getString("salt");
162 m_salt = salt.second;
164 pair<bool,bool> flag = getBool("http");
166 m_http = flag.second ? 1 : -1;
167 flag = getBool("https");
169 m_https = flag.second ? 1 : -1;
171 e = XMLHelper::getFirstChildElement(e);
173 if (XMLString::equals(e->getLocalName(), EndpointBase) && e->hasChildNodes()) {
174 auto_ptr_char base(e->getFirstChild()->getNodeValue());
175 if (base.get() && *base.get())
176 m_bases.push_back(base.get());
179 // Try and parse the object.
180 auto_ptr<XMLObject> child(XMLObjectBuilder::buildOneFromElement(const_cast<DOMElement*>(e)));
181 ContactPerson* cp = dynamic_cast<ContactPerson*>(child.get());
184 m_contacts.push_back(cp);
187 RequestedAttribute* req = dynamic_cast<RequestedAttribute*>(child.get());
190 m_reqAttrs.push_back(req);
193 AttributeConsumingService* acs = dynamic_cast<AttributeConsumingService*>(child.get());
196 m_attrConsumers.push_back(acs);
199 UIInfo* info = dynamic_cast<UIInfo*>(child.get());
206 m_log.warn("skipping duplicate UIInfo element");
210 Organization* org = dynamic_cast<Organization*>(child.get());
217 m_log.warn("skipping duplicate Organization element");
221 EntityAttributes* ea = dynamic_cast<EntityAttributes*>(child.get());
223 if (!m_entityAttrs) {
228 m_log.warn("skipping duplicate EntityAttributes element");
237 e = XMLHelper::getNextSiblingElement(e);
242 pair<bool,long> MetadataGenerator::run(SPRequest& request, bool isHandler) const
244 SPConfig& conf = SPConfig::getConfig();
245 if (conf.isEnabled(SPConfig::InProcess)) {
246 if (!m_acl.empty() && m_acl.count(request.getRemoteAddr()) == 0) {
247 m_log.error("request for metadata blocked from invalid address (%s)", request.getRemoteAddr().c_str());
248 istringstream msg("Metadata Request Blocked");
249 return make_pair(true,request.sendResponse(msg, HTTPResponse::XMLTOOLING_HTTP_STATUS_FORBIDDEN));
254 if (conf.isEnabled(SPConfig::OutOfProcess)) {
255 // When out of process, we run natively and directly process the message.
256 return processMessage(request.getApplication(), request.getHandlerURL(), request.getParameter("entityID"), request);
259 // When not out of process, we remote all the message processing.
260 DDF out,in = DDF(m_address.c_str());
261 in.addmember("application_id").string(request.getApplication().getId());
262 in.addmember("handler_url").string(request.getHandlerURL());
263 if (request.getParameter("entityID"))
264 in.addmember("entity_id").string(request.getParameter("entityID"));
265 DDFJanitor jin(in), jout(out);
267 out=request.getServiceProvider().getListenerService()->send(in);
268 return unwrap(request, out);
271 catch (exception& ex) {
272 m_log.error("error while processing request: %s", ex.what());
273 istringstream msg("Metadata Request Failed");
274 return make_pair(true,request.sendResponse(msg, HTTPResponse::XMLTOOLING_HTTP_STATUS_ERROR));
278 void MetadataGenerator::receive(DDF& in, ostream& out)
281 const char* aid=in["application_id"].string();
282 const char* hurl=in["handler_url"].string();
283 const Application* app=aid ? SPConfig::getConfig().getServiceProvider()->getApplication(aid) : nullptr;
285 // Something's horribly wrong.
286 m_log.error("couldn't find application (%s) for metadata request", aid ? aid : "(missing)");
287 throw ConfigurationException("Unable to locate application for metadata request, deleted?");
290 throw ConfigurationException("Missing handler_url parameter in remoted method call.");
293 // Wrap a response shim.
295 DDFJanitor jout(ret);
296 auto_ptr<HTTPResponse> resp(getResponse(ret));
298 // Since we're remoted, the result should either be a throw, a false/0 return,
299 // which we just return as an empty structure, or a response/redirect,
300 // which we capture in the facade and send back.
301 processMessage(*app, hurl, in["entity_id"].string(), *resp.get());
305 pair<bool,long> MetadataGenerator::processMessage(
306 const Application& application, const char* handlerURL, const char* entityID, HTTPResponse& httpResponse
310 m_log.debug("processing metadata request");
312 const PropertySet* relyingParty=nullptr;
314 MetadataProvider* m=application.getMetadataProvider();
316 MetadataProviderCriteria mc(application, entityID);
317 relyingParty = application.getRelyingParty(m->getEntityDescriptor(mc).first);
320 relyingParty = &application;
323 EntityDescriptor* entity;
324 pair<bool,const char*> prop = getString("template");
326 // Load a template to use for our metadata.
327 string templ(prop.second);
328 XMLToolingConfig::getConfig().getPathResolver()->resolve(templ, PathResolver::XMLTOOLING_CFG_FILE);
329 auto_ptr_XMLCh widenit(templ.c_str());
330 LocalFileInputSource src(widenit.get());
331 Wrapper4InputSource dsrc(&src,false);
332 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
333 XercesJanitor<DOMDocument> docjan(doc);
334 auto_ptr<XMLObject> xmlobj(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
336 entity = dynamic_cast<EntityDescriptor*>(xmlobj.get());
338 throw ConfigurationException("Template file ($1) did not contain an EntityDescriptor", params(1, templ.c_str()));
342 entity = EntityDescriptorBuilder::buildEntityDescriptor();
345 if (!entity->getID()) {
346 string hashinput = m_salt + relyingParty->getString("entityID").second;
347 string hashed = '_' + SecurityHelper::doHash("SHA1", hashinput.c_str(), hashinput.length());
348 auto_ptr_XMLCh widenit(hashed.c_str());
349 entity->setID(widenit.get());
352 auto_ptr<EntityDescriptor> wrapper(entity);
353 pair<bool,unsigned int> cache = getUnsignedInt("cacheDuration");
355 entity->setCacheDuration(cache.second);
357 cache = getUnsignedInt("validUntil");
359 entity->setValidUntil(time(nullptr) + cache.second);
360 entity->setEntityID(relyingParty->getXMLString("entityID").second);
362 if (m_org && !entity->getOrganization())
363 entity->setOrganization(m_org->cloneOrganization());
365 for (vector<ContactPerson*>::const_iterator cp = m_contacts.begin(); cp != m_contacts.end(); ++cp)
366 entity->getContactPersons().push_back((*cp)->cloneContactPerson());
369 if (!entity->getExtensions())
370 entity->setExtensions(ExtensionsBuilder::buildExtensions());
371 entity->getExtensions()->getUnknownXMLObjects().push_back(m_entityAttrs->cloneEntityAttributes());
374 SPSSODescriptor* role;
375 if (entity->getSPSSODescriptors().empty()) {
376 role = SPSSODescriptorBuilder::buildSPSSODescriptor();
377 entity->getSPSSODescriptors().push_back(role);
380 role = entity->getSPSSODescriptors().front();
384 if (!role->getExtensions())
385 role->setExtensions(ExtensionsBuilder::buildExtensions());
386 role->getExtensions()->getUnknownXMLObjects().push_back(m_uiinfo->cloneUIInfo());
389 for (vector<AttributeConsumingService*>::const_iterator acs = m_attrConsumers.begin(); acs != m_attrConsumers.end(); ++acs)
390 role->getAttributeConsumingServices().push_back((*acs)->cloneAttributeConsumingService());
392 if (!m_reqAttrs.empty()) {
394 const vector<AttributeConsumingService*>& svcs = const_cast<const SPSSODescriptor*>(role)->getAttributeConsumingServices();
395 for (vector<AttributeConsumingService*>::const_iterator s =svcs.begin(); s != svcs.end(); ++s) {
396 pair<bool,int> i = (*s)->getIndex();
397 if (i.first && index == i.second)
398 index = i.second + 1;
400 AttributeConsumingService* svc = AttributeConsumingServiceBuilder::buildAttributeConsumingService();
401 role->getAttributeConsumingServices().push_back(svc);
402 svc->setIndex(index);
403 ServiceName* sn = ServiceNameBuilder::buildServiceName();
404 svc->getServiceNames().push_back(sn);
405 sn->setName(entity->getEntityID());
406 static const XMLCh english[] = UNICODE_LITERAL_2(e,n);
407 sn->setLang(english);
408 for (vector<RequestedAttribute*>::const_iterator req = m_reqAttrs.begin(); req != m_reqAttrs.end(); ++req)
409 svc->getRequestedAttributes().push_back((*req)->cloneRequestedAttribute());
413 prop = relyingParty->getString("signing");
414 if (prop.first && (!strcmp(prop.second,"true") || !strcmp(prop.second,"front")))
415 role->AuthnRequestsSigned(true);
416 pair<bool,bool> flagprop = relyingParty->getBool("requireSignedAssertions");
417 if (flagprop.first && flagprop.second)
418 role->WantAssertionsSigned(true);
420 // Ask each handler to generate itself.
421 vector<const Handler*> handlers;
422 application.getHandlers(handlers);
423 for (vector<const Handler*>::const_iterator h = handlers.begin(); h != handlers.end(); ++h) {
424 if (m_bases.empty()) {
425 if (strncmp(handlerURL, "https", 5) == 0) {
427 (*h)->generateMetadata(*role, handlerURL);
429 string temp(handlerURL);
431 (*h)->generateMetadata(*role, temp.c_str());
436 (*h)->generateMetadata(*role, handlerURL);
438 string temp(handlerURL);
439 temp.insert(temp.begin() + 4, 's');
440 (*h)->generateMetadata(*role, temp.c_str());
445 for (vector<string>::const_iterator b = m_bases.begin(); b != m_bases.end(); ++b)
446 (*h)->generateMetadata(*role, b->c_str());
450 AttributeExtractor* extractor = application.getAttributeExtractor();
452 Locker extlocker(extractor);
453 extractor->generateMetadata(*role);
456 CredentialResolver* credResolver=application.getCredentialResolver();
458 Locker credLocker(credResolver);
459 CredentialCriteria cc;
460 prop = relyingParty->getString("keyName");
462 cc.getKeyNames().insert(prop.second);
463 vector<const Credential*> signingcreds,enccreds;
464 cc.setUsage(Credential::SIGNING_CREDENTIAL);
465 credResolver->resolve(signingcreds, &cc);
466 cc.setUsage(Credential::ENCRYPTION_CREDENTIAL);
467 credResolver->resolve(enccreds, &cc);
469 for (vector<const Credential*>::const_iterator c = signingcreds.begin(); c != signingcreds.end(); ++c) {
470 KeyInfo* kinfo = (*c)->getKeyInfo();
472 KeyDescriptor* kd = KeyDescriptorBuilder::buildKeyDescriptor();
473 kd->setKeyInfo(kinfo);
474 const XMLCh* use = KeyDescriptor::KEYTYPE_SIGNING;
475 for (vector<const Credential*>::iterator match = enccreds.begin(); match != enccreds.end(); ++match) {
478 enccreds.erase(match);
483 role->getKeyDescriptors().push_back(kd);
487 for (vector<const Credential*>::const_iterator c = enccreds.begin(); c != enccreds.end(); ++c) {
488 KeyInfo* kinfo = (*c)->getKeyInfo();
490 KeyDescriptor* kd = KeyDescriptorBuilder::buildKeyDescriptor();
491 kd->setUse(KeyDescriptor::KEYTYPE_ENCRYPTION);
492 kd->setKeyInfo(kinfo);
493 role->getKeyDescriptors().push_back(kd);
498 // Stream for response.
502 pair<bool,bool> flag = getBool("signing");
503 if (flag.first && flag.second) {
505 Locker credLocker(credResolver);
506 // Fill in criteria to use.
507 CredentialCriteria cc;
508 cc.setUsage(Credential::SIGNING_CREDENTIAL);
509 prop = getString("keyName");
511 cc.getKeyNames().insert(prop.second);
512 pair<bool,const XMLCh*> sigalg = getXMLString("signingAlg");
513 pair<bool,const XMLCh*> digalg = getXMLString("digestAlg");
515 cc.setXMLAlgorithm(sigalg.second);
516 const Credential* cred = credResolver->resolve(&cc);
518 throw XMLSecurityException("Unable to obtain signing credential to use.");
520 // Pretty-print it first and then read it back in.
522 XMLHelper::serialize(entity->marshall(), pretty, true);
523 DOMDocument* prettydoc = XMLToolingConfig::getConfig().getParser().parse(pretty);
524 auto_ptr<XMLObject> prettyentity(XMLObjectBuilder::buildOneFromElement(prettydoc->getDocumentElement(), true));
526 Signature* sig = SignatureBuilder::buildSignature();
527 dynamic_cast<EntityDescriptor*>(prettyentity.get())->setSignature(sig);
529 sig->setSignatureAlgorithm(sigalg.second);
531 opensaml::ContentReference* cr = dynamic_cast<opensaml::ContentReference*>(sig->getContentReference());
533 cr->setDigestAlgorithm(digalg.second);
536 // Sign while marshalling.
537 vector<Signature*> sigs(1,sig);
538 prettyentity->marshall(prettydoc,&sigs,cred);
542 throw FatalProfileException("Can't self-sign metadata, no credential resolver found.");
546 // Pretty-print it directly to client.
547 XMLHelper::serialize(entity->marshall(), s, true);
550 prop = getString("mimeType");
551 httpResponse.setContentType(prop.first ? prop.second : "application/samlmetadata+xml");
552 return make_pair(true, httpResponse.sendResponse(s));
554 return make_pair(false,0L);