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/resolver/ResolutionContext.h>
58 #include <shibsp/handler/AssertionConsumerService.h>
59 #include <shibsp/metadata/MetadataProviderCriteria.h>
60 #include <shibsp/util/SPConstants.h>
62 #include <saml/saml1/core/Assertions.h>
63 #include <saml/saml2/core/Assertions.h>
64 #include <saml/saml2/metadata/Metadata.h>
65 #include <xercesc/util/XMLUniDefs.hpp>
66 #include <xmltooling/XMLToolingConfig.h>
67 #include <xmltooling/util/XMLHelper.h>
69 using namespace shibsp;
70 using namespace opensaml::saml2md;
71 using namespace opensaml;
72 using namespace xmltooling::logging;
73 using namespace xmltooling;
74 using namespace xercesc;
77 class GSSEAPResolver : public shibsp::AssertionConsumerService
80 GSSEAPResolver(const DOMElement *e, const char *appId)
81 : shibsp::AssertionConsumerService(e, appId, Category::getInstance(SHIBSP_LOGCAT".GSSEAPResolver")) {
83 virtual ~GSSEAPResolver() {}
85 ResolutionContext* resolveAttributes (
86 const Application& application,
87 const RoleDescriptor* issuer,
88 const XMLCh* protocol,
89 const saml1::NameIdentifier* v1nameid,
90 const saml2::NameID* nameid,
91 const XMLCh* authncontext_class,
92 const XMLCh* authncontext_decl,
93 const vector<const Assertion*>* tokens
95 return shibsp::AssertionConsumerService::resolveAttributes(
96 application, issuer, protocol, v1nameid,
97 nameid, authncontext_class, authncontext_decl, tokens
102 void implementProtocol(
103 const Application& application,
104 const HTTPRequest& httpRequest,
105 HTTPResponse& httpResponse,
106 SecurityPolicy& policy,
107 const PropertySet* settings,
108 const XMLObject& xmlObject
110 throw FatalProfileException("Should never be called.");
114 class SHIBSP_DLLLOCAL DummyContext : public ResolutionContext
117 DummyContext(const vector<Attribute*>& attributes) : m_attributes(attributes) {
120 virtual ~DummyContext() {
121 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
124 vector<Attribute*>& getResolvedAttributes() {
127 vector<Assertion*>& getResolvedAssertions() {
132 vector<Attribute*> m_attributes;
133 static vector<Assertion*> m_tokens; // never any tokens, so just share an empty vector
136 struct eap_gss_saml_attr_ctx {
137 ResolutionContext *resCtx;
141 samlAllocAttrContext(OM_uint32 *minor,
142 struct eap_gss_saml_attr_ctx **pCtx)
144 struct eap_gss_saml_attr_ctx *ctx;
146 ctx = (struct eap_gss_saml_attr_ctx *)GSSEAP_CALLOC(1, sizeof(*ctx));
149 return GSS_S_FAILURE;
154 return GSS_S_COMPLETE;
158 samlImportAssertion(OM_uint32 *minor,
160 saml2::Assertion **pAssertion)
166 const XMLObjectBuilder *b;
169 string samlBuf((char *)buffer->value, buffer->length);
170 istringstream samlIn(samlBuf);
172 doc = XMLToolingConfig::getConfig().getParser().parse(samlIn);
173 b = XMLObjectBuilder::getDefaultBuilder();
174 elem = doc->getDocumentElement();
175 xobj = b->buildOneFromElement(elem, true);
177 *pAssertion = dynamic_cast<saml2::Assertion *>(xobj);
178 if (*pAssertion == NULL) {
179 /* TODO minor_status */
180 return GSS_S_BAD_NAME;
182 } catch (exception &e){
183 /* TODO minor_status */
184 return GSS_S_BAD_NAME;
188 return GSS_S_COMPLETE;
192 samlDuplicateAttrContext(OM_uint32 *minor,
193 const struct eap_gss_saml_attr_ctx *in,
194 struct eap_gss_saml_attr_ctx **out)
196 OM_uint32 major, tmpMinor;
197 struct eap_gss_saml_attr_ctx *ctx;
199 major = samlAllocAttrContext(minor, &ctx);
200 if (GSS_ERROR(major))
203 ctx->resCtx = new DummyContext(in->resCtx->getResolvedAttributes());
206 if (GSS_ERROR(major))
207 samlReleaseAttrContext(&tmpMinor, &ctx);
213 samlReleaseAttrContext(OM_uint32 *minor,
214 struct eap_gss_saml_attr_ctx **pCtx)
216 struct eap_gss_saml_attr_ctx *ctx = *pCtx;
225 return GSS_S_COMPLETE;
229 samlCreateAttrContext(OM_uint32 *minor,
231 gss_name_t acceptorName,
232 struct eap_gss_saml_attr_ctx **pCtx)
234 OM_uint32 major, tmpMinor;
235 struct eap_gss_saml_attr_ctx *ctx;
236 SPConfig &conf = SPConfig::getConfig();
238 const Application *app;
240 gss_buffer_desc nameBuf;
241 const XMLCh *issuer = NULL;
242 saml2::NameID *subjectName = NULL;
243 saml2::Assertion *assertion;
246 nameBuf.value = NULL;
248 conf.setFeatures(SPConfig::Metadata |
250 SPConfig::AttributeResolution |
251 SPConfig::Credentials |
252 SPConfig::OutOfProcess);
254 return GSS_S_FAILURE;
255 if (!conf.instantiate())
256 return GSS_S_FAILURE;
258 sp = conf.getServiceProvider();
261 major = gss_display_name(minor, acceptorName, &nameBuf, NULL);
262 if (GSS_ERROR(major))
265 app = sp->getApplication((const char *)nameBuf.value);
267 major = GSS_S_FAILURE;
271 major = samlAllocAttrContext(minor, &ctx);
272 if (GSS_ERROR(major))
275 major = samlImportAssertion(minor, buffer, &assertion);
276 if (GSS_ERROR(major))
279 if (assertion->getIssuer() != NULL)
280 issuer = assertion->getIssuer()->getName();
281 if (assertion->getSubject() != NULL)
282 subjectName = assertion->getSubject()->getNameID();
285 m = app->getMetadataProvider();
286 xmltooling::Locker mlocker(m);
287 MetadataProviderCriteria mc(*app, issuer,
288 &IDPSSODescriptor::ELEMENT_QNAME,
289 samlconstants::SAML20P_NS);
290 pair<const EntityDescriptor *, const RoleDescriptor *> site =
291 m->getEntityDescriptor(mc);
293 auto_ptr_char temp(issuer);
294 throw MetadataException("Unable to locate metadata for IdP ($1).",
295 params(1,temp.get()));
297 vector<const Assertion*> tokens(1, assertion);
298 GSSEAPResolver gssResolver(NULL, (const char *)nameBuf.value);
299 ctx->resCtx = gssResolver.resolveAttributes(*app, site.second,
300 samlconstants::SAML20P_NS,
301 NULL, subjectName, NULL,
303 } catch (exception &ex) {
304 major = GSS_S_BAD_NAME;
308 major = GSS_S_COMPLETE;
315 if (GSS_ERROR(major))
316 samlReleaseAttrContext(&tmpMinor, &ctx);
317 gss_release_buffer(&tmpMinor, &nameBuf);
323 samlGetAttributeTypes(OM_uint32 *minor,
324 const struct eap_gss_saml_attr_ctx *ctx,
326 OM_uint32 (*addAttribute)(OM_uint32 *, void *, gss_buffer_t))
328 OM_uint32 major = GSS_S_COMPLETE;
331 return GSS_S_COMPLETE;
333 for (vector<Attribute*>::const_iterator a = ctx->resCtx->getResolvedAttributes().begin();
334 a != ctx->resCtx->getResolvedAttributes().end();
337 gss_buffer_desc attribute;
339 attribute.value = (void *)((*a)->getId());
340 attribute.length = strlen((char *)attribute.value);
342 major = addAttribute(minor, data, &attribute);
343 if (GSS_ERROR(major))
351 * SAML implementation of gss_get_name_attribute
354 samlGetAttribute(OM_uint32 *minor,
355 const struct eap_gss_saml_attr_ctx *ctx,
360 gss_buffer_t display_value,
364 Attribute *shibAttr = NULL;
368 return GSS_S_UNAVAILABLE;
370 for (vector<Attribute *>::const_iterator a = ctx->resCtx->getResolvedAttributes().begin();
371 a != ctx->resCtx->getResolvedAttributes().end();
373 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
374 s != (*a)->getAliases().end();
376 if (attr->length == strlen((*s).c_str()) &&
377 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
382 if (shibAttr != NULL)
386 if (shibAttr == NULL)
387 return GSS_S_UNAVAILABLE;
391 } else if (*more >= (int)shibAttr->valueCount()) {
393 return GSS_S_COMPLETE;
396 buf.value = (void *)shibAttr->getString(*more);
397 buf.length = strlen((char *)buf.value);
399 major = duplicateBuffer(minor, &buf, value);
400 if (GSS_ERROR(major))
403 *authenticated = TRUE;
406 return GSS_S_COMPLETE;
410 * No plans to support gss_set_name_attribute at this time.
413 samlSetAttribute(OM_uint32 *minor,
414 struct eap_gss_saml_attr_ctx *ctx,
419 return GSS_S_UNAVAILABLE;
423 * In order to implement gss_export_name and gss_export_sec_context,
424 * we need to serialise a resolved attribute context to a buffer.
427 samlExportAttrContext(OM_uint32 *minor,
428 struct eap_gss_saml_attr_ctx *ctx,
431 GSSEAP_NOT_IMPLEMENTED;
435 * In order to implement gss_import_name and gss_import_sec_context,
436 * we need to deserialise a resolved attribute context from a buffer.
439 samlImportAttrContext(OM_uint32 *minor,
441 struct eap_gss_saml_attr_ctx **ppCtx)
443 GSSEAP_NOT_IMPLEMENTED;