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;
138 gss_buffer_desc assertion;
142 samlAllocAttrContext(OM_uint32 *minor,
143 struct eap_gss_saml_attr_ctx **pCtx)
145 struct eap_gss_saml_attr_ctx *ctx;
147 ctx = (struct eap_gss_saml_attr_ctx *)GSSEAP_CALLOC(1, sizeof(*ctx));
150 return GSS_S_FAILURE;
155 return GSS_S_COMPLETE;
159 samlImportAssertion(OM_uint32 *minor,
161 saml2::Assertion **pAssertion)
167 const XMLObjectBuilder *b;
170 string samlBuf((char *)buffer->value, buffer->length);
171 istringstream samlIn(samlBuf);
173 doc = XMLToolingConfig::getConfig().getParser().parse(samlIn);
174 b = XMLObjectBuilder::getDefaultBuilder();
175 elem = doc->getDocumentElement();
176 xobj = b->buildOneFromElement(elem, true);
178 *pAssertion = dynamic_cast<saml2::Assertion *>(xobj);
179 if (*pAssertion == NULL) {
180 /* TODO minor_status */
181 return GSS_S_BAD_NAME;
183 } catch (exception &e){
184 /* TODO minor_status */
185 return GSS_S_BAD_NAME;
189 return GSS_S_COMPLETE;
193 samlDuplicateAttrContext(OM_uint32 *minor,
194 const struct eap_gss_saml_attr_ctx *in,
195 struct eap_gss_saml_attr_ctx **out)
197 OM_uint32 major, tmpMinor;
198 struct eap_gss_saml_attr_ctx *ctx;
200 major = samlAllocAttrContext(minor, &ctx);
201 if (GSS_ERROR(major))
204 ctx->resCtx = new DummyContext(in->resCtx->getResolvedAttributes());
207 if (GSS_ERROR(major))
208 samlReleaseAttrContext(&tmpMinor, &ctx);
214 samlReleaseAttrContext(OM_uint32 *minor,
215 struct eap_gss_saml_attr_ctx **pCtx)
217 struct eap_gss_saml_attr_ctx *ctx = *pCtx;
226 return GSS_S_COMPLETE;
230 samlCreateAttrContext(OM_uint32 *minor,
232 gss_name_t acceptorName,
233 struct eap_gss_saml_attr_ctx **pCtx)
235 OM_uint32 major, tmpMinor;
236 struct eap_gss_saml_attr_ctx *ctx;
237 SPConfig &conf = SPConfig::getConfig();
239 const Application *app;
241 gss_buffer_desc nameBuf;
242 const XMLCh *issuer = NULL;
243 saml2::NameID *subjectName = NULL;
244 saml2::Assertion *assertion;
247 nameBuf.value = NULL;
249 conf.setFeatures(SPConfig::Metadata |
251 SPConfig::AttributeResolution |
252 SPConfig::Credentials |
253 SPConfig::OutOfProcess);
255 return GSS_S_FAILURE;
256 if (!conf.instantiate())
257 return GSS_S_FAILURE;
259 sp = conf.getServiceProvider();
262 major = gss_display_name(minor, acceptorName, &nameBuf, NULL);
263 if (GSS_ERROR(major))
266 app = sp->getApplication((const char *)nameBuf.value);
268 major = GSS_S_FAILURE;
272 major = samlAllocAttrContext(minor, &ctx);
273 if (GSS_ERROR(major))
276 major = samlImportAssertion(minor, buffer, &assertion);
277 if (GSS_ERROR(major))
280 if (assertion->getIssuer() != NULL)
281 issuer = assertion->getIssuer()->getName();
282 if (assertion->getSubject() != NULL)
283 subjectName = assertion->getSubject()->getNameID();
286 m = app->getMetadataProvider();
287 xmltooling::Locker mlocker(m);
288 MetadataProviderCriteria mc(*app, issuer,
289 &IDPSSODescriptor::ELEMENT_QNAME,
290 samlconstants::SAML20P_NS);
291 pair<const EntityDescriptor *, const RoleDescriptor *> site =
292 m->getEntityDescriptor(mc);
294 auto_ptr_char temp(issuer);
295 throw MetadataException("Unable to locate metadata for IdP ($1).",
296 params(1,temp.get()));
298 vector<const Assertion*> tokens(1, assertion);
299 GSSEAPResolver gssResolver(NULL, (const char *)nameBuf.value);
300 ctx->resCtx = gssResolver.resolveAttributes(*app, site.second,
301 samlconstants::SAML20P_NS,
302 NULL, subjectName, NULL,
304 } catch (exception &ex) {
305 major = GSS_S_BAD_NAME;
309 major = GSS_S_COMPLETE;
316 if (GSS_ERROR(major))
317 samlReleaseAttrContext(&tmpMinor, &ctx);
318 gss_release_buffer(&tmpMinor, &nameBuf);
324 samlGetAttributeTypes(OM_uint32 *minor,
325 const struct eap_gss_saml_attr_ctx *ctx,
327 OM_uint32 (*addAttribute)(OM_uint32 *, void *, gss_buffer_t))
329 OM_uint32 major = GSS_S_COMPLETE;
332 return GSS_S_COMPLETE;
334 for (vector<Attribute*>::const_iterator a = ctx->resCtx->getResolvedAttributes().begin();
335 a != ctx->resCtx->getResolvedAttributes().end();
338 gss_buffer_desc attribute;
340 attribute.value = (void *)((*a)->getId());
341 attribute.length = strlen((char *)attribute.value);
343 major = addAttribute(minor, data, &attribute);
344 if (GSS_ERROR(major))
352 samlGetAttribute(OM_uint32 *minor,
353 const struct eap_gss_saml_attr_ctx *ctx,
358 gss_buffer_t display_value,
362 Attribute *shibAttr = NULL;
366 return GSS_S_UNAVAILABLE;
368 for (vector<Attribute *>::const_iterator a = ctx->resCtx->getResolvedAttributes().begin();
369 a != ctx->resCtx->getResolvedAttributes().end();
371 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
372 s != (*a)->getAliases().end();
374 if (attr->length == strlen((*s).c_str()) &&
375 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
380 if (shibAttr != NULL)
384 if (shibAttr == NULL)
385 return GSS_S_UNAVAILABLE;
389 } else if (*more >= (int)shibAttr->valueCount()) {
391 return GSS_S_COMPLETE;
394 buf.value = (void *)shibAttr->getString(*more);
395 buf.length = strlen((char *)buf.value);
397 major = duplicateBuffer(minor, &buf, value);
398 if (GSS_ERROR(major))
401 *authenticated = TRUE;
404 return GSS_S_COMPLETE;
408 samlSetAttribute(OM_uint32 *minor,
409 struct eap_gss_saml_attr_ctx *ctx,
414 return GSS_S_UNAVAILABLE;
418 samlExportAttrContext(OM_uint32 *minor,
419 struct eap_gss_saml_attr_ctx *ctx,
422 GSSEAP_NOT_IMPLEMENTED;
426 samlImportAttrContext(OM_uint32 *minor,
428 struct eap_gss_saml_attr_ctx **ppCtx)
430 GSSEAP_NOT_IMPLEMENTED;