2 * Copyright (c) 2011, 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.
49 * Local attribute provider implementation.
52 #include <xmltooling/XMLObject.h>
54 #include <saml/saml2/core/Assertions.h>
56 #include <shibsp/exceptions.h>
57 #include <shibsp/attribute/SimpleAttribute.h>
58 #include <shibresolver/resolver.h>
62 #include "gssapiP_eap.h"
64 using namespace shibsp;
65 using namespace shibresolver;
66 using namespace opensaml::saml2md;
67 using namespace opensaml;
68 using namespace xmltooling;
71 gss_eap_shib_attr_provider::gss_eap_shib_attr_provider(void)
73 m_initialized = false;
74 m_authenticated = false;
77 gss_eap_shib_attr_provider::~gss_eap_shib_attr_provider(void)
79 for_each(m_attributes.begin(),
81 xmltooling::cleanup<Attribute>())
86 gss_eap_shib_attr_provider::initWithExistingContext(const gss_eap_attr_ctx *manager,
87 const gss_eap_attr_provider *ctx)
89 const gss_eap_shib_attr_provider *shib;
91 if (!gss_eap_attr_provider::initWithExistingContext(manager, ctx)) {
95 m_authenticated = false;
97 shib = static_cast<const gss_eap_shib_attr_provider *>(ctx);
99 m_attributes = duplicateAttributes(shib->getAttributes());
100 m_authenticated = shib->authenticated();
103 m_initialized = true;
109 exportMechSecContext(OM_uint32 *minor,
111 gss_buffer_t mechContext)
114 gss_buffer_desc exportedCtx;
117 assert(gssCtx->mechanismUsed != GSS_C_NO_OID);
119 major = gssEapExportSecContext(minor, gssCtx, &exportedCtx);
120 if (GSS_ERROR(major))
124 * gss_import_sec_context expects the exported security context token
125 * to be tagged with the mechanism OID; in Heimdal and MIT, this is
126 * done by the mechglue, so if we are subverting the mechglue we need
127 * to add it ourselves.
129 mechContext->length = 4 + gssCtx->mechanismUsed->length + exportedCtx.length;
130 mechContext->value = p = (unsigned char *)GSSEAP_MALLOC(mechContext->length);
131 if (mechContext->value == NULL) {
132 gss_release_buffer(minor, &exportedCtx);
133 throw std::bad_alloc();
136 p = store_oid(gssCtx->mechanismUsed, p);
137 memcpy(p, exportedCtx.value, exportedCtx.length);
139 gss_release_buffer(minor, &exportedCtx);
141 return GSS_S_COMPLETE;
145 gss_eap_shib_attr_provider::initWithGssContext(const gss_eap_attr_ctx *manager,
146 const gss_cred_id_t gssCred,
147 const gss_ctx_id_t gssCtx)
149 if (!gss_eap_attr_provider::initWithGssContext(manager, gssCred, gssCtx))
152 auto_ptr<ShibbolethResolver> resolver(ShibbolethResolver::create());
155 * For now, leave ApplicationID defaulted.
156 * Later on, we could allow this via config option to the mechanism
157 * or rely on an SPRequest interface to pass in a URI identifying the
161 gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
162 if (gssCred != GSS_C_NO_CREDENTIAL &&
163 gssEapDisplayName(&minor, gssCred->name, &nameBuf, NULL) == GSS_S_COMPLETE) {
164 resolver->setApplicationID((const char *)nameBuf.value);
165 gss_release_buffer(&minor, &nameBuf);
169 gss_buffer_desc mechContext = GSS_C_EMPTY_BUFFER;
170 OM_uint32 major, minor;
171 major = exportMechSecContext(&minor, gssCtx, &mechContext);
172 if (major == GSS_S_COMPLETE) {
173 resolver->addToken(&mechContext);
174 gss_release_buffer(&minor, &mechContext);
177 const gss_eap_saml_assertion_provider *saml;
178 saml = static_cast<const gss_eap_saml_assertion_provider *>
179 (m_manager->getProvider(ATTR_TYPE_SAML_ASSERTION));
180 if (saml != NULL && saml->getAssertion() != NULL) {
181 resolver->addToken(saml->getAssertion());
186 m_attributes = resolver->getResolvedAttributes();
187 resolver->getResolvedAttributes().clear();
188 } catch (exception &e) {
192 m_authenticated = true;
193 m_initialized = true;
199 gss_eap_shib_attr_provider::getAttributeIndex(const gss_buffer_t attr) const
203 assert(m_initialized);
205 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
206 a != m_attributes.end();
209 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
210 s != (*a)->getAliases().end();
212 if (attr->length == (*s).length() &&
213 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
223 gss_eap_shib_attr_provider::setAttribute(int complete GSSEAP_UNUSED,
224 const gss_buffer_t attr,
225 const gss_buffer_t value)
227 string attrStr((char *)attr->value, attr->length);
228 vector <string> ids(1, attrStr);
229 SimpleAttribute *a = new SimpleAttribute(ids);
231 assert(m_initialized);
233 if (value->length != 0) {
234 string valueStr((char *)value->value, value->length);
236 a->getValues().push_back(valueStr);
239 m_attributes.push_back(a);
240 m_authenticated = false;
246 gss_eap_shib_attr_provider::deleteAttribute(const gss_buffer_t attr)
250 assert(m_initialized);
252 i = getAttributeIndex(attr);
254 m_attributes.erase(m_attributes.begin() + i);
256 m_authenticated = false;
262 gss_eap_shib_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
265 assert(m_initialized);
267 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
268 a != m_attributes.end();
271 gss_buffer_desc attribute;
273 attribute.value = (void *)((*a)->getId());
274 attribute.length = strlen((char *)attribute.value);
276 if (!addAttribute(m_manager, this, &attribute, data))
284 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr) const
286 const Attribute *ret = NULL;
288 assert(m_initialized);
290 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
291 a != m_attributes.end();
294 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
295 s != (*a)->getAliases().end();
297 if (attr->length == (*s).length() &&
298 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
311 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr,
315 gss_buffer_t display_value,
318 const Attribute *shibAttr = NULL;
320 int nvalues, i = *more;
322 assert(m_initialized);
326 shibAttr = getAttribute(attr);
327 if (shibAttr == NULL)
330 nvalues = shibAttr->valueCount();
334 else if (i >= nvalues)
337 buf.value = (void *)shibAttr->getSerializedValues()[*more].c_str();
338 buf.length = strlen((char *)buf.value);
340 if (buf.length != 0) {
342 duplicateBuffer(buf, value);
344 if (display_value != NULL)
345 duplicateBuffer(buf, display_value);
348 if (authenticated != NULL)
349 *authenticated = m_authenticated;
350 if (complete != NULL)
360 gss_eap_shib_attr_provider::mapToAny(int authenticated,
361 gss_buffer_t type_id GSSEAP_UNUSED) const
365 assert(m_initialized);
367 if (authenticated && !m_authenticated)
368 return (gss_any_t)NULL;
370 vector <Attribute *>v = duplicateAttributes(m_attributes);
372 output = (gss_any_t)new vector <Attribute *>(v);
378 gss_eap_shib_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id GSSEAP_UNUSED,
379 gss_any_t input) const
381 assert(m_initialized);
383 vector <Attribute *> *v = ((vector <Attribute *> *)input);
388 gss_eap_shib_attr_provider::prefix(void) const
394 gss_eap_shib_attr_provider::name(void) const
400 gss_eap_shib_attr_provider::jsonRepresentation(void) const
404 if (m_initialized == false)
405 return obj; /* don't export incomplete context */
407 JSONObject jattrs = JSONObject::array();
409 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
410 a != m_attributes.end(); ++a) {
411 DDF attr = (*a)->marshall();
412 JSONObject jattr = JSONObject::ddf(attr);
413 jattrs.append(jattr);
416 obj.set("attributes", jattrs);
418 obj.set("authenticated", m_authenticated);
424 gss_eap_shib_attr_provider::initWithJsonObject(const gss_eap_attr_ctx *ctx,
427 if (!gss_eap_attr_provider::initWithJsonObject(ctx, obj))
430 assert(m_authenticated == false);
431 assert(m_attributes.size() == 0);
433 JSONObject jattrs = obj["attributes"];
434 size_t nelems = jattrs.size();
436 for (size_t i = 0; i < nelems; i++) {
437 JSONObject jattr = jattrs.get(i);
439 DDF attr = jattr.ddf();
440 Attribute *attribute = Attribute::unmarshall(attr);
441 m_attributes.push_back(attribute);
444 m_authenticated = obj["authenticated"].integer();
445 m_initialized = true;
451 gss_eap_shib_attr_provider::init(void)
453 if (!ShibbolethResolver::init())
456 gss_eap_attr_ctx::registerProvider(ATTR_TYPE_LOCAL, createAttrContext);
462 gss_eap_shib_attr_provider::finalize(void)
464 gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_LOCAL);
465 ShibbolethResolver::term();
469 gss_eap_shib_attr_provider::mapException(OM_uint32 *minor,
470 std::exception &e) const
472 if (typeid(e) == typeid(AttributeException))
473 *minor = GSSEAP_SHIB_ATTR_FAILURE;
474 else if (typeid(e) == typeid(AttributeExtractionException))
475 *minor = GSSEAP_SHIB_ATTR_EXTRACT_FAILURE;
476 else if (typeid(e) == typeid(AttributeFilteringException))
477 *minor = GSSEAP_SHIB_ATTR_FILTER_FAILURE;
478 else if (typeid(e) == typeid(AttributeResolutionException))
479 *minor = GSSEAP_SHIB_ATTR_RESOLVE_FAILURE;
480 else if (typeid(e) == typeid(ConfigurationException))
481 *minor = GSSEAP_SHIB_CONFIG_FAILURE;
482 else if (typeid(e) == typeid(ListenerException))
483 *minor = GSSEAP_SHIB_LISTENER_FAILURE;
485 return GSS_S_CONTINUE_NEEDED;
487 gssEapSaveStatusInfo(*minor, "%s", e.what());
489 return GSS_S_FAILURE;
492 gss_eap_attr_provider *
493 gss_eap_shib_attr_provider::createAttrContext(void)
495 return new gss_eap_shib_attr_provider;
499 gss_eap_shib_attr_provider::duplicateAttribute(const Attribute *src)
501 DDF obj = src->marshall();
502 Attribute *attribute = Attribute::unmarshall(obj);
509 gss_eap_shib_attr_provider::duplicateAttributes(const vector <Attribute *>src)
511 vector <Attribute *> dst;
513 for (vector<Attribute *>::const_iterator a = src.begin();
516 dst.push_back(duplicateAttribute(*a));
522 gssEapLocalAttrProviderInit(OM_uint32 *minor)
524 if (!gss_eap_shib_attr_provider::init()) {
525 *minor = GSSEAP_SHIB_INIT_FAILURE;
526 return GSS_S_FAILURE;
528 return GSS_S_COMPLETE;
532 gssEapLocalAttrProviderFinalize(OM_uint32 *minor)
534 gss_eap_shib_attr_provider::finalize();
537 return GSS_S_COMPLETE;