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 <shibsp/exceptions.h>
49 #include <shibsp/attribute/SimpleAttribute.h>
51 #include <shibresolver/resolver.h>
55 #include "gssapiP_eap.h"
57 using namespace shibsp;
58 using namespace shibresolver;
59 using namespace opensaml::saml2md;
60 using namespace opensaml;
61 using namespace xmltooling;
64 gss_eap_shib_attr_provider::gss_eap_shib_attr_provider(void)
66 m_authenticated = false;
69 gss_eap_shib_attr_provider::~gss_eap_shib_attr_provider(void)
71 for_each(m_attributes.begin(),
73 xmltooling::cleanup<Attribute>())
78 gss_eap_shib_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *manager,
79 const gss_eap_attr_provider *ctx)
81 const gss_eap_shib_attr_provider *shib;
83 if (!gss_eap_attr_provider::initFromExistingContext(manager, ctx)) {
87 m_authenticated = false;
89 shib = static_cast<const gss_eap_shib_attr_provider *>(ctx);
91 m_attributes = duplicateAttributes(shib->getAttributes());
92 m_authenticated = shib->authenticated();
99 addRadiusAttribute(const gss_eap_attr_provider *provider,
100 const gss_buffer_t attribute,
103 const gss_eap_shib_attr_provider *shib;
104 const gss_eap_radius_attr_provider *radius;
105 int authenticated, complete, more = -1;
106 vector <string> attributeIds(1);
109 radius = static_cast<const gss_eap_radius_attr_provider *>(provider);
110 shib = static_cast<const gss_eap_shib_attr_provider *>(data);
112 assert(radius != NULL && shib != NULL);
114 string attributeName =
115 gss_eap_attr_ctx::composeAttributeName(ATTR_TYPE_RADIUS, attribute);
117 attributeIds.push_back(attributeName);
118 a = new SimpleAttribute(attributeIds);
123 gss_buffer_desc value = GSS_C_EMPTY_BUFFER;
126 if (!radius->getAttribute(attribute,
134 string attributeValue((char *)value.value, value.length);
135 a->getValues().push_back(attributeValue);
137 gss_release_buffer(&minor, &value);
140 shib->getAttributes().push_back(a);
146 gss_eap_shib_attr_provider::initFromGssContext(const gss_eap_attr_ctx *manager,
147 const gss_cred_id_t gssCred,
148 const gss_ctx_id_t gssCtx)
150 const gss_eap_saml_assertion_provider *saml;
151 const gss_eap_radius_attr_provider *radius;
152 gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
153 ShibbolethResolver *resolver;
156 if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
159 saml = static_cast<const gss_eap_saml_assertion_provider *>
160 (m_manager->getProvider(ATTR_TYPE_SAML_ASSERTION));
161 radius = static_cast<const gss_eap_radius_attr_provider *>
162 (m_manager->getProvider(ATTR_TYPE_RADIUS));
164 resolver = ShibbolethResolver::create();
166 if (gssCred != GSS_C_NO_CREDENTIAL &&
167 gssEapDisplayName(&minor, gssCred->name, &nameBuf, NULL) == GSS_S_COMPLETE)
168 resolver->setApplicationID((const char *)nameBuf.value);
170 m_authenticated = false;
172 if (radius != NULL) {
173 radius->getAttributeTypes(addRadiusAttribute, (void *)this);
174 m_authenticated = radius->authenticated();
177 if (saml != NULL && saml->getAssertion() != NULL) {
178 resolver->addToken(saml->getAssertion());
180 m_authenticated = saml->authenticated();
185 m_attributes = resolver->getResolvedAttributes();
186 resolver->getResolvedAttributes().clear();
188 gss_release_buffer(&minor, &nameBuf);
196 gss_eap_shib_attr_provider::getAttributeIndex(const gss_buffer_t attr) const
200 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
201 a != m_attributes.end();
204 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
205 s != (*a)->getAliases().end();
207 if (attr->length == (*s).length() &&
208 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
218 gss_eap_shib_attr_provider::setAttribute(int complete,
219 const gss_buffer_t attr,
220 const gss_buffer_t value)
222 string attrStr((char *)attr->value, attr->length);
223 vector <string> ids(1, attrStr);
224 SimpleAttribute *a = new SimpleAttribute(ids);
226 if (value->length != 0) {
227 string valueStr((char *)value->value, value->length);
229 a->getValues().push_back(valueStr);
232 m_attributes.push_back(a);
233 m_authenticated = false;
239 gss_eap_shib_attr_provider::deleteAttribute(const gss_buffer_t attr)
243 i = getAttributeIndex(attr);
245 m_attributes.erase(m_attributes.begin() + i);
247 m_authenticated = false;
253 gss_eap_shib_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
256 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
257 a != m_attributes.end();
260 gss_buffer_desc attribute;
262 attribute.value = (void *)((*a)->getId());
263 attribute.length = strlen((char *)attribute.value);
265 if (!addAttribute(this, &attribute, data))
273 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr) const
275 const Attribute *ret = NULL;
277 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
278 a != m_attributes.end();
281 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
282 s != (*a)->getAliases().end();
284 if (attr->length == (*s).length() &&
285 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
298 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr,
302 gss_buffer_t display_value,
305 const Attribute *shibAttr = NULL;
307 int nvalues, i = *more;
311 shibAttr = getAttribute(attr);
312 if (shibAttr == NULL)
315 nvalues = shibAttr->valueCount();
319 else if (i >= nvalues)
322 buf.value = (void *)shibAttr->getString(*more);
323 buf.length = strlen((char *)buf.value);
325 if (buf.length != 0) {
327 duplicateBuffer(buf, value);
329 if (display_value != NULL)
330 duplicateBuffer(buf, display_value);
333 if (authenticated != NULL)
334 *authenticated = m_authenticated;
335 if (complete != NULL)
345 gss_eap_shib_attr_provider::mapToAny(int authenticated,
346 gss_buffer_t type_id) const
350 if (authenticated && !m_authenticated)
351 return (gss_any_t)NULL;
353 vector <Attribute *>v = duplicateAttributes(m_attributes);
355 output = (gss_any_t)new vector <Attribute *>(v);
361 gss_eap_shib_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
362 gss_any_t input) const
364 vector <Attribute *> *v = ((vector <Attribute *> *)input);
369 gss_eap_shib_attr_provider::exportToBuffer(gss_buffer_t buffer) const
375 buffer->value = NULL;
377 obj.addmember("version").integer(1);
378 obj.addmember("authenticated").integer(m_authenticated);
380 attrs = obj.addmember("attributes").list();
381 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
382 a != m_attributes.end(); ++a) {
383 DDF attr = (*a)->marshall();
389 string str = sink.str();
391 duplicateBuffer(str, buffer);
397 gss_eap_shib_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
398 const gss_buffer_t buffer)
400 if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer))
403 if (buffer->length == 0)
406 assert(m_authenticated == false);
407 assert(m_attributes.size() == 0);
410 string str((const char *)buffer->value, buffer->length);
411 istringstream source(str);
415 if (obj["version"].integer() != 1)
418 m_authenticated = (obj["authenticated"].integer() != 0);
420 DDF attrs = obj["attributes"];
421 DDF attr = attrs.first();
422 while (!attr.isnull()) {
423 Attribute *attribute = Attribute::unmarshall(attr);
424 m_attributes.push_back(attribute);
434 gss_eap_shib_attr_provider::init(void)
436 if (!ShibbolethResolver::init())
439 gss_eap_attr_ctx::registerProvider(ATTR_TYPE_LOCAL,
441 gss_eap_shib_attr_provider::createAttrContext);
447 gss_eap_shib_attr_provider::finalize(void)
449 gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_LOCAL);
450 ShibbolethResolver::term();
453 gss_eap_attr_provider *
454 gss_eap_shib_attr_provider::createAttrContext(void)
456 return new gss_eap_shib_attr_provider;
460 gss_eap_shib_attr_provider::duplicateAttribute(const Attribute *src)
462 DDF obj = src->marshall();
463 Attribute *attribute = Attribute::unmarshall(obj);
470 gss_eap_shib_attr_provider::duplicateAttributes(const vector <Attribute *>src)
472 vector <Attribute *> dst;
474 for (vector<Attribute *>::const_iterator a = src.begin();
477 dst.push_back(duplicateAttribute(*a));
483 gssEapLocalAttrProviderInit(OM_uint32 *minor)
485 if (!gss_eap_shib_attr_provider::init()) {
486 *minor = GSSEAP_SHIB_INIT_FAILURE;
487 return GSS_S_FAILURE;
489 return GSS_S_COMPLETE;
493 gssEapLocalAttrProviderFinalize(OM_uint32 *minor)
495 gss_eap_shib_attr_provider::finalize();
496 return GSS_S_COMPLETE;