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 void 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<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 mapException(OM_uint32 *minor, exception &e)
192 return GSS_S_FAILURE;
196 eap_gss_saml_attr_ctx::parseAssertion(const gss_buffer_t buffer)
199 const XMLObjectBuilder *b;
202 string str((char *)buffer->value, buffer->length);
203 istringstream istream(str);
205 doc = XMLToolingConfig::getConfig().getParser().parse(istream);
206 b = XMLObjectBuilder::getDefaultBuilder();
207 elem = doc->getDocumentElement();
208 xobj = b->buildOneFromElement(elem, true);
210 m_assertion = dynamic_cast<Assertion *>(xobj);
212 return (m_assertion != NULL);
216 duplicateBuffer(gss_buffer_desc &src, gss_buffer_t dst)
220 if (GSS_ERROR(duplicateBuffer(&minor, &src, dst)))
221 throw new bad_alloc();
225 duplicateBuffer(string &str, gss_buffer_t buffer)
229 tmp.length = str.length();
230 tmp.value = (char *)str.c_str();
232 duplicateBuffer(tmp, buffer);
236 eap_gss_saml_attr_ctx::marshall() const
242 obj.addmember("version").integer(1);
244 attrs = obj.addmember("attributes").list();
245 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
246 a != m_attributes.end(); ++a) {
247 DDF attr = (*a)->marshall();
252 sink << *m_assertion;
253 assertion = obj.addmember("assertion").string(sink.str().c_str());
258 eap_gss_saml_attr_ctx *
259 eap_gss_saml_attr_ctx::unmarshall(DDF &obj)
261 eap_gss_saml_attr_ctx *ctx = new eap_gss_saml_attr_ctx();
263 DDF version = obj["version"];
264 if (version.integer() != 1)
267 DDF assertion = obj["assertion"];
268 gss_buffer_desc buffer;
270 if (!assertion.isnull()) {
271 buffer.length = assertion.strlen();
272 buffer.value = (void *)assertion.string();
277 if (buffer.length != 0)
278 ctx->parseAssertion(&buffer);
280 DDF attrs = obj["attributes"];
281 DDF attr = attrs.first();
282 while (!attr.isnull()) {
283 Attribute *attribute = Attribute::unmarshall(attr);
284 ctx->addAttribute(attribute, false);
292 eap_gss_saml_attr_ctx::marshall(gss_buffer_t buffer)
302 duplicateBuffer(str, buffer);
305 eap_gss_saml_attr_ctx *
306 eap_gss_saml_attr_ctx::unmarshall(const gss_buffer_t buffer)
309 string str((const char *)buffer->value, buffer->length);
310 istringstream source(str);
313 return unmarshall(obj);
317 eap_gss_saml_attr_ctx::getAssertion(gss_buffer_t buffer)
321 if (m_assertion == NULL)
324 buffer->value = NULL;
327 XMLHelper::serialize(m_assertion->marshall((DOMDocument *)NULL), str);
329 duplicateBuffer(str, buffer);
335 duplicateAttribute(const Attribute *src)
337 DDF obj = src->marshall();
338 return Attribute::unmarshall(obj);
341 static vector <Attribute *>
342 duplicateAttributes(const vector <Attribute *>src)
344 vector <Attribute *> dst;
346 for (vector<Attribute *>::const_iterator a = src.begin();
349 dst.push_back(duplicateAttribute(*a));
355 eap_gss_saml_attr_ctx::addAttribute(Attribute *attribute, bool copy)
359 a = copy ? duplicateAttribute(attribute) : attribute;
361 m_attributes.push_back(a);
365 eap_gss_saml_attr_ctx::setAttributes(const vector<Attribute*> attributes)
367 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
368 m_attributes = duplicateAttributes(attributes);
372 eap_gss_saml_attr_ctx::getAttributeIndex(const gss_buffer_t attr) const
376 for (vector<Attribute *>::const_iterator a = getAttributes().begin();
377 a != getAttributes().end();
380 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
381 s != (*a)->getAliases().end();
383 if (attr->length == (*s).length() &&
384 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
394 eap_gss_saml_attr_ctx::getAttribute(const gss_buffer_t attr) const
396 const Attribute *ret = NULL;
398 for (vector<Attribute *>::const_iterator a = getAttributes().begin();
399 a != getAttributes().end();
402 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
403 s != (*a)->getAliases().end();
405 if (attr->length == (*s).length() &&
406 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
419 eap_gss_saml_attr_ctx::getAttribute(const gss_buffer_t attr,
423 gss_buffer_t display_value,
426 const Attribute *shibAttr = NULL;
429 shibAttr = getAttribute(attr);
430 if (shibAttr == NULL)
435 } else if (*more >= (int)shibAttr->valueCount()) {
440 buf.value = (void *)shibAttr->getString(*more);
441 buf.length = strlen((char *)buf.value);
443 duplicateBuffer(buf, value);
445 *authenticated = TRUE;
452 eap_gss_saml_attr_ctx::setAttribute(int complete,
453 const gss_buffer_t attr,
454 const gss_buffer_t value)
456 string attrStr((char *)attr->value, attr->length);
457 vector <string> ids(1);
460 ids.push_back(attrStr);
462 a = new SimpleAttribute(ids);
464 if (value->length != 0) {
465 string valStr((char *)value->value, value->length);
467 a->getValues().push_back(valStr);
470 m_attributes.push_back(a);
474 eap_gss_saml_attr_ctx::deleteAttribute(const gss_buffer_t attr)
478 i = getAttributeIndex(attr);
480 m_attributes.erase(m_attributes.begin() + i);
484 samlReleaseAttrContext(OM_uint32 *minor,
485 struct eap_gss_saml_attr_ctx **pCtx)
487 eap_gss_saml_attr_ctx *ctx = *pCtx;
495 return GSS_S_COMPLETE;
499 samlCreateAttrContext(OM_uint32 *minor,
501 gss_name_t acceptorName,
502 struct eap_gss_saml_attr_ctx **pCtx)
504 OM_uint32 major, tmpMinor;
505 eap_gss_saml_attr_ctx *ctx = NULL;
506 SPConfig &conf = SPConfig::getConfig();
508 const Application *app;
510 gss_buffer_desc nameBuf;
511 const XMLCh *issuer = NULL;
512 saml2::NameID *subjectName = NULL;
513 saml2::Assertion *assertion;
514 ResolutionContext *resolverContext;
517 nameBuf.value = NULL;
519 conf.setFeatures(SPConfig::Metadata |
521 SPConfig::AttributeResolution |
522 SPConfig::Credentials |
523 SPConfig::OutOfProcess);
525 return GSS_S_FAILURE;
526 if (!conf.instantiate())
527 return GSS_S_FAILURE;
529 sp = conf.getServiceProvider();
532 major = gss_display_name(minor, acceptorName, &nameBuf, NULL);
533 if (GSS_ERROR(major))
536 app = sp->getApplication((const char *)nameBuf.value);
538 major = GSS_S_FAILURE;
543 ctx = new eap_gss_saml_attr_ctx(buffer);
545 if (assertion->getIssuer() != NULL)
546 issuer = assertion->getIssuer()->getName();
547 if (assertion->getSubject() != NULL)
548 subjectName = assertion->getSubject()->getNameID();
550 m = app->getMetadataProvider();
551 xmltooling::Locker mlocker(m);
552 MetadataProviderCriteria mc(*app, issuer,
553 &IDPSSODescriptor::ELEMENT_QNAME,
554 samlconstants::SAML20P_NS);
555 pair<const EntityDescriptor *, const RoleDescriptor *> site =
556 m->getEntityDescriptor(mc);
558 auto_ptr_char temp(issuer);
559 throw MetadataException("Unable to locate metadata for IdP ($1).",
560 params(1,temp.get()));
562 vector<const Assertion*> tokens(1, assertion);
563 GSSEAPResolver gssResolver(NULL, (const char *)nameBuf.value);
564 resolverContext = gssResolver.resolveAttributes(*app, site.second,
565 samlconstants::SAML20P_NS,
566 NULL, subjectName, NULL,
568 ctx->setAttributes(resolverContext->getResolvedAttributes());
569 } catch (exception &ex) {
570 major = mapException(minor, ex);
574 major = GSS_S_COMPLETE;
581 if (GSS_ERROR(major))
583 gss_release_buffer(&tmpMinor, &nameBuf);
589 samlGetAttributeTypes(OM_uint32 *minor,
590 const struct eap_gss_saml_attr_ctx *ctx,
592 OM_uint32 (*addAttribute)(OM_uint32 *, void *, gss_buffer_t))
594 OM_uint32 major = GSS_S_COMPLETE;
597 return GSS_S_COMPLETE;
599 for (vector<Attribute*>::const_iterator a = ctx->getAttributes().begin();
600 a != ctx->getAttributes().end();
603 gss_buffer_desc attribute;
605 attribute.value = (void *)((*a)->getId());
606 attribute.length = strlen((char *)attribute.value);
608 major = addAttribute(minor, data, &attribute);
609 if (GSS_ERROR(major))
617 * SAML implementation of gss_get_name_attribute
620 samlGetAttribute(OM_uint32 *minor,
621 struct eap_gss_saml_attr_ctx *ctx,
626 gss_buffer_t display_value,
630 return GSS_S_UNAVAILABLE;
632 if (!ctx->getAttribute(attr, authenticated, complete,
633 value, display_value, more))
634 return GSS_S_UNAVAILABLE;
636 return GSS_S_COMPLETE;
640 samlSetAttribute(OM_uint32 *minor,
641 struct eap_gss_saml_attr_ctx *ctx,
647 ctx->setAttribute(complete, attr, value);
648 } catch (exception &e) {
649 return mapException(minor, e);
652 return GSS_S_COMPLETE;
656 samlDeleteAttribute(OM_uint32 *minor,
657 struct eap_gss_saml_attr_ctx *ctx,
661 ctx->deleteAttribute(attr);
662 } catch (exception &e) {
663 return mapException(minor, e);
666 return GSS_S_COMPLETE;
670 * In order to implement gss_export_name and gss_export_sec_context,
671 * we need to serialise a resolved attribute context to a buffer.
674 samlExportAttrContext(OM_uint32 *minor,
675 struct eap_gss_saml_attr_ctx *ctx,
679 ctx->marshall(buffer);
680 } catch (exception &e) {
681 return mapException(minor, e);
684 return GSS_S_COMPLETE;
688 * In order to implement gss_import_name and gss_import_sec_context,
689 * we need to deserialise a resolved attribute context from a buffer.
692 samlImportAttrContext(OM_uint32 *minor,
694 struct eap_gss_saml_attr_ctx **pCtx)
697 *pCtx = eap_gss_saml_attr_ctx::unmarshall(buffer);
698 } catch (exception &e) {
699 return mapException(minor, e);
702 return GSS_S_COMPLETE;
706 samlGetAssertion(OM_uint32 *minor,
707 struct eap_gss_saml_attr_ctx *ctx,
708 gss_buffer_t assertion)
711 ctx->getAssertion(assertion);
712 } catch (exception &e) {
713 return mapException(minor, e);
716 return GSS_S_COMPLETE;
720 samlDuplicateAttrContext(OM_uint32 *minor,
721 const struct eap_gss_saml_attr_ctx *in,
722 struct eap_gss_saml_attr_ctx **out)
725 *out = new eap_gss_saml_attr_ctx(*in);
726 } catch (exception &e) {
727 return mapException(minor, e);
730 return GSS_S_COMPLETE;
734 samlMapNametoAny(OM_uint32 *minor,
735 const struct eap_gss_saml_attr_ctx *ctx,
737 gss_buffer_t type_id,
740 if (bufferEqualString(type_id, "shibsp::Attribute")) {
741 vector <Attribute *>v = duplicateAttributes(ctx->getAttributes());
743 *output = (gss_any_t)new vector <Attribute *>(v);
744 } else if (bufferEqualString(type_id, "opensaml::Assertion")) {
745 *output = (gss_any_t)ctx->getAssertion()->clone();
747 *output = (gss_any_t)NULL;
748 return GSS_S_UNAVAILABLE;
751 return GSS_S_COMPLETE;
755 samlReleaseAnyNameMapping(OM_uint32 *minor,
756 const struct eap_gss_saml_attr_ctx *ctx,
757 gss_buffer_t type_id,
760 if (bufferEqualString(type_id, "vector<shibsp::Attribute>")) {
761 vector <Attribute *> *v = ((vector <Attribute *> *)*input);
763 } else if (bufferEqualString(type_id, "opensaml::Assertion")) {
764 delete (Assertion *)*input;
766 return GSS_S_UNAVAILABLE;
769 *input = (gss_any_t)NULL;
770 return GSS_S_COMPLETE;