2 * Copyright (c) 2010, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Copyright 2001-2009 Internet2
35 * Licensed under the Apache License, Version 2.0 (the "License");
36 * you may not use this file except in compliance with the License.
37 * You may obtain a copy of the License at
39 * http://www.apache.org/licenses/LICENSE-2.0
41 * Unless required by applicable law or agreed to in writing, software
42 * distributed under the License is distributed on an "AS IS" BASIS,
43 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
44 * See the License for the specific language governing permissions and
45 * limitations under the License.
48 #include <gssapi/gssapi.h>
49 #include <gssapi/gssapi_ext.h>
52 #include <shibsp/Application.h>
53 #include <shibsp/exceptions.h>
54 #include <shibsp/SPConfig.h>
55 #include <shibsp/ServiceProvider.h>
56 #include <shibsp/attribute/Attribute.h>
57 #include <shibsp/attribute/SimpleAttribute.h>
58 #include <shibsp/attribute/resolver/ResolutionContext.h>
59 #include <shibsp/handler/AssertionConsumerService.h>
60 #include <shibsp/metadata/MetadataProviderCriteria.h>
61 #include <shibsp/util/SPConstants.h>
63 #include <saml/saml1/core/Assertions.h>
64 #include <saml/saml2/core/Assertions.h>
65 #include <saml/saml2/metadata/Metadata.h>
66 #include <xercesc/util/XMLUniDefs.hpp>
67 #include <xmltooling/XMLToolingConfig.h>
68 #include <xmltooling/util/XMLHelper.h>
70 using namespace shibsp;
71 using namespace opensaml::saml2md;
72 using namespace opensaml;
73 using namespace xmltooling::logging;
74 using namespace xmltooling;
75 using namespace xercesc;
78 class GSSEAPResolver : public shibsp::AssertionConsumerService
81 GSSEAPResolver(const DOMElement *e, const char *appId)
82 : shibsp::AssertionConsumerService(e, appId, Category::getInstance(SHIBSP_LOGCAT".GSSEAPResolver")) {
84 virtual ~GSSEAPResolver() {}
86 ResolutionContext* resolveAttributes (
87 const Application& application,
88 const RoleDescriptor* issuer,
89 const XMLCh* protocol,
90 const saml1::NameIdentifier* v1nameid,
91 const saml2::NameID* nameid,
92 const XMLCh* authncontext_class,
93 const XMLCh* authncontext_decl,
94 const vector<const Assertion*>* tokens
96 return shibsp::AssertionConsumerService::resolveAttributes(
97 application, issuer, protocol, v1nameid,
98 nameid, authncontext_class, authncontext_decl, tokens
103 void implementProtocol(
104 const Application& application,
105 const HTTPRequest& httpRequest,
106 HTTPResponse& httpResponse,
107 SecurityPolicy& policy,
108 const PropertySet* settings,
109 const XMLObject& xmlObject
111 throw FatalProfileException("Should never be called.");
115 struct eap_gss_saml_attr_ctx {
117 eap_gss_saml_attr_ctx();
118 eap_gss_saml_attr_ctx(const gss_buffer_t buffer);
119 eap_gss_saml_attr_ctx(const Assertion *assertion);
121 eap_gss_saml_attr_ctx(const vector<Attribute*>& attributes,
122 const Assertion *assertion);
124 eap_gss_saml_attr_ctx(const eap_gss_saml_attr_ctx &ctx) {
125 eap_gss_saml_attr_ctx(ctx.m_attributes, ctx.m_assertion);
128 ~eap_gss_saml_attr_ctx();
130 const vector <Attribute *> getAttributes(void) const {
134 void addAttribute(Attribute *attr, bool copy = true);
135 void setAttributes(const vector<Attribute*> attributes);
137 void setAttribute(int complete,
138 const gss_buffer_t attr,
139 const gss_buffer_t value);
140 void deleteAttribute(const gss_buffer_t attr);
142 int getAttributeIndex(const gss_buffer_t attr) const;
143 const Attribute *getAttribute(const gss_buffer_t attr) const;
145 bool getAttribute(const gss_buffer_t attr,
149 gss_buffer_t display_value,
152 const Assertion *getAssertion(void) const {
156 bool getAssertion(gss_buffer_t buffer);
158 DDF marshall() const;
159 static eap_gss_saml_attr_ctx *unmarshall(DDF &in);
161 bool marshall(gss_buffer_t buffer);
162 static eap_gss_saml_attr_ctx *unmarshall(const gss_buffer_t buffer);
165 mutable vector<Attribute*> m_attributes;
166 mutable Assertion *m_assertion;
168 bool parseAssertion(const gss_buffer_t buffer);
171 eap_gss_saml_attr_ctx::eap_gss_saml_attr_ctx(const vector<Attribute*>& attributes,
172 const Assertion *assertion)
174 m_assertion = dynamic_cast<saml2::Assertion *>(assertion->clone());
175 setAttributes(attributes);
178 eap_gss_saml_attr_ctx::~eap_gss_saml_attr_ctx()
180 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
184 eap_gss_saml_attr_ctx::eap_gss_saml_attr_ctx(const gss_buffer_t buffer)
186 parseAssertion(buffer);
190 eap_gss_saml_attr_ctx::parseAssertion(const gss_buffer_t buffer)
193 const XMLObjectBuilder *b;
196 string str((char *)buffer->value, buffer->length);
197 istringstream istream(str);
199 doc = XMLToolingConfig::getConfig().getParser().parse(istream);
200 b = XMLObjectBuilder::getDefaultBuilder();
201 elem = doc->getDocumentElement();
202 xobj = b->buildOneFromElement(elem, true);
204 m_assertion = dynamic_cast<saml2::Assertion *>(xobj);
206 return (m_assertion != NULL);
210 duplicateBuffer(string &str, gss_buffer_t buffer)
215 tmp.length = str.length();
216 tmp.value = (char *)str.c_str();
218 if (GSS_ERROR(duplicateBuffer(&minor, &tmp, buffer)))
225 eap_gss_saml_attr_ctx::marshall() const
231 obj.addmember("version").integer(1);
233 attrs = obj.addmember("attributes").list();
234 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
235 a != m_attributes.end(); ++a) {
236 DDF attr = (*a)->marshall();
241 sink << *m_assertion;
242 assertion = obj.addmember("assertion").string(sink.str().c_str());
247 eap_gss_saml_attr_ctx *
248 eap_gss_saml_attr_ctx::unmarshall(DDF &obj)
250 eap_gss_saml_attr_ctx *ctx = new eap_gss_saml_attr_ctx();
252 DDF version = obj["version"];
253 if (version.integer() != 1)
256 DDF assertion = obj["assertion"];
257 gss_buffer_desc buffer;
259 if (!assertion.isnull()) {
260 buffer.length = assertion.strlen();
261 buffer.value = (void *)assertion.string();
266 if (buffer.length != 0)
267 ctx->parseAssertion(&buffer);
269 DDF attrs = obj["attributes"];
270 DDF attr = attrs.first();
271 while (!attr.isnull()) {
272 Attribute *attribute = Attribute::unmarshall(attr);
273 ctx->addAttribute(attribute, false);
281 eap_gss_saml_attr_ctx::marshall(gss_buffer_t buffer)
291 return duplicateBuffer(str, buffer);
294 eap_gss_saml_attr_ctx *
295 eap_gss_saml_attr_ctx::unmarshall(const gss_buffer_t buffer)
298 string str((const char *)buffer->value, buffer->length);
299 istringstream source(str);
302 return unmarshall(obj);
306 eap_gss_saml_attr_ctx::getAssertion(gss_buffer_t buffer)
310 buffer->value = NULL;
313 XMLHelper::serialize(m_assertion->marshall((DOMDocument *)NULL), str);
315 return duplicateBuffer(str, buffer);
319 eap_gss_saml_attr_ctx::addAttribute(Attribute *attribute, bool copy)
323 DDF obj = attribute->marshall();
324 a = Attribute::unmarshall(obj);
328 m_attributes.push_back(a);
332 eap_gss_saml_attr_ctx::setAttributes(const vector<Attribute*> attributes)
334 for (vector<Attribute *>::const_iterator a = attributes.begin();
335 a != attributes.end();
341 eap_gss_saml_attr_ctx::getAttributeIndex(const gss_buffer_t attr) const
345 for (vector<Attribute *>::const_iterator a = getAttributes().begin();
346 a != getAttributes().end();
349 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
350 s != (*a)->getAliases().end();
352 if (attr->length == (*s).length() &&
353 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
363 eap_gss_saml_attr_ctx::getAttribute(const gss_buffer_t attr) const
365 const Attribute *ret = NULL;
367 for (vector<Attribute *>::const_iterator a = getAttributes().begin();
368 a != getAttributes().end();
371 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
372 s != (*a)->getAliases().end();
374 if (attr->length == (*s).length() &&
375 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
388 eap_gss_saml_attr_ctx::getAttribute(const gss_buffer_t attr,
392 gss_buffer_t display_value,
395 OM_uint32 major, minor;
396 const Attribute *shibAttr = NULL;
399 shibAttr = getAttribute(attr);
400 if (shibAttr == NULL)
405 } else if (*more >= (int)shibAttr->valueCount()) {
410 buf.value = (void *)shibAttr->getString(*more);
411 buf.length = strlen((char *)buf.value);
413 major = duplicateBuffer(&minor, &buf, value);
414 if (GSS_ERROR(major))
417 *authenticated = TRUE;
424 eap_gss_saml_attr_ctx::setAttribute(int complete,
425 const gss_buffer_t attr,
426 const gss_buffer_t value)
428 string attrStr((char *)attr->value, attr->length);
429 vector <string> ids(1);
432 ids.push_back(attrStr);
434 a = new SimpleAttribute(ids);
436 if (value->length != 0) {
437 string valStr((char *)value->value, value->length);
439 a->getValues().push_back(valStr);
442 m_attributes.push_back(a);
446 eap_gss_saml_attr_ctx::deleteAttribute(const gss_buffer_t attr)
450 i = getAttributeIndex(attr);
452 m_attributes.erase(m_attributes.begin() + i);
456 samlReleaseAttrContext(OM_uint32 *minor,
457 struct eap_gss_saml_attr_ctx **pCtx)
459 eap_gss_saml_attr_ctx *ctx = *pCtx;
467 return GSS_S_COMPLETE;
471 samlCreateAttrContext(OM_uint32 *minor,
473 gss_name_t acceptorName,
474 struct eap_gss_saml_attr_ctx **pCtx)
476 OM_uint32 major, tmpMinor;
477 eap_gss_saml_attr_ctx *ctx = NULL;
478 SPConfig &conf = SPConfig::getConfig();
480 const Application *app;
482 gss_buffer_desc nameBuf;
483 const XMLCh *issuer = NULL;
484 saml2::NameID *subjectName = NULL;
485 saml2::Assertion *assertion;
486 ResolutionContext *resolverContext;
489 nameBuf.value = NULL;
491 conf.setFeatures(SPConfig::Metadata |
493 SPConfig::AttributeResolution |
494 SPConfig::Credentials |
495 SPConfig::OutOfProcess);
497 return GSS_S_FAILURE;
498 if (!conf.instantiate())
499 return GSS_S_FAILURE;
501 sp = conf.getServiceProvider();
504 major = gss_display_name(minor, acceptorName, &nameBuf, NULL);
505 if (GSS_ERROR(major))
508 app = sp->getApplication((const char *)nameBuf.value);
510 major = GSS_S_FAILURE;
515 ctx = new eap_gss_saml_attr_ctx(buffer);
517 if (assertion->getIssuer() != NULL)
518 issuer = assertion->getIssuer()->getName();
519 if (assertion->getSubject() != NULL)
520 subjectName = assertion->getSubject()->getNameID();
522 m = app->getMetadataProvider();
523 xmltooling::Locker mlocker(m);
524 MetadataProviderCriteria mc(*app, issuer,
525 &IDPSSODescriptor::ELEMENT_QNAME,
526 samlconstants::SAML20P_NS);
527 pair<const EntityDescriptor *, const RoleDescriptor *> site =
528 m->getEntityDescriptor(mc);
530 auto_ptr_char temp(issuer);
531 throw MetadataException("Unable to locate metadata for IdP ($1).",
532 params(1,temp.get()));
534 vector<const Assertion*> tokens(1, assertion);
535 GSSEAPResolver gssResolver(NULL, (const char *)nameBuf.value);
536 resolverContext = gssResolver.resolveAttributes(*app, site.second,
537 samlconstants::SAML20P_NS,
538 NULL, subjectName, NULL,
540 ctx->setAttributes(resolverContext->getResolvedAttributes());
541 } catch (exception &ex) {
542 major = GSS_S_BAD_NAME;
546 major = GSS_S_COMPLETE;
553 if (GSS_ERROR(major))
555 gss_release_buffer(&tmpMinor, &nameBuf);
561 samlGetAttributeTypes(OM_uint32 *minor,
562 const struct eap_gss_saml_attr_ctx *ctx,
564 OM_uint32 (*addAttribute)(OM_uint32 *, void *, gss_buffer_t))
566 OM_uint32 major = GSS_S_COMPLETE;
569 return GSS_S_COMPLETE;
571 for (vector<Attribute*>::const_iterator a = ctx->getAttributes().begin();
572 a != ctx->getAttributes().end();
575 gss_buffer_desc attribute;
577 attribute.value = (void *)((*a)->getId());
578 attribute.length = strlen((char *)attribute.value);
580 major = addAttribute(minor, data, &attribute);
581 if (GSS_ERROR(major))
589 * SAML implementation of gss_get_name_attribute
592 samlGetAttribute(OM_uint32 *minor,
593 struct eap_gss_saml_attr_ctx *ctx,
598 gss_buffer_t display_value,
602 return GSS_S_UNAVAILABLE;
604 if (!ctx->getAttribute(attr, authenticated, complete,
605 value, display_value, more))
606 return GSS_S_UNAVAILABLE;
608 return GSS_S_COMPLETE;
612 samlSetAttribute(OM_uint32 *minor,
613 struct eap_gss_saml_attr_ctx *ctx,
619 ctx->setAttribute(complete, attr, value);
620 } catch (exception &e) {
621 return GSS_S_FAILURE;
624 return GSS_S_COMPLETE;
628 samlDeleteAttribute(OM_uint32 *minor,
629 struct eap_gss_saml_attr_ctx *ctx,
633 ctx->deleteAttribute(attr);
634 } catch (exception &e) {
635 return GSS_S_FAILURE;
638 return GSS_S_COMPLETE;
642 * In order to implement gss_export_name and gss_export_sec_context,
643 * we need to serialise a resolved attribute context to a buffer.
646 samlExportAttrContext(OM_uint32 *minor,
647 struct eap_gss_saml_attr_ctx *ctx,
651 if (!ctx->marshall(buffer))
652 return GSS_S_FAILURE;
653 } catch (exception &e) {
654 return GSS_S_FAILURE;
657 return GSS_S_COMPLETE;
661 * In order to implement gss_import_name and gss_import_sec_context,
662 * we need to deserialise a resolved attribute context from a buffer.
665 samlImportAttrContext(OM_uint32 *minor,
667 struct eap_gss_saml_attr_ctx **pCtx)
670 *pCtx = eap_gss_saml_attr_ctx::unmarshall(buffer);
671 } catch (exception &e) {
672 return GSS_S_FAILURE;
675 return GSS_S_COMPLETE;
679 samlGetAssertion(OM_uint32 *minor,
680 struct eap_gss_saml_attr_ctx *ctx,
681 gss_buffer_t assertion)
684 ctx->getAssertion(assertion);
685 } catch (exception &e) {
686 return GSS_S_FAILURE;
689 return GSS_S_COMPLETE;
693 samlDuplicateAttrContext(OM_uint32 *minor,
694 const struct eap_gss_saml_attr_ctx *in,
695 struct eap_gss_saml_attr_ctx **out)
698 *out = new eap_gss_saml_attr_ctx(*in);
699 } catch (exception &e) {
700 return GSS_S_FAILURE;
703 return GSS_S_COMPLETE;