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 (manager->getProvider(ATTR_TYPE_SAML_ASSERTION));
161 radius = static_cast<const gss_eap_radius_attr_provider *>
162 (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;
237 gss_eap_shib_attr_provider::deleteAttribute(const gss_buffer_t attr)
241 i = getAttributeIndex(attr);
243 m_attributes.erase(m_attributes.begin() + i);
245 m_authenticated = false;
249 gss_eap_shib_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
252 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
253 a != m_attributes.end();
256 gss_buffer_desc attribute;
258 attribute.value = (void *)((*a)->getId());
259 attribute.length = strlen((char *)attribute.value);
261 if (!addAttribute(this, &attribute, data))
269 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr) const
271 const Attribute *ret = NULL;
273 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
274 a != m_attributes.end();
277 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
278 s != (*a)->getAliases().end();
280 if (attr->length == (*s).length() &&
281 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
294 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr,
298 gss_buffer_t display_value,
301 const Attribute *shibAttr = NULL;
303 int nvalues, i = *more;
307 shibAttr = getAttribute(attr);
308 if (shibAttr == NULL)
311 nvalues = shibAttr->valueCount();
315 else if (i >= nvalues)
318 buf.value = (void *)shibAttr->getString(*more);
319 buf.length = strlen((char *)buf.value);
321 if (buf.length != 0) {
323 duplicateBuffer(buf, value);
325 if (display_value != NULL)
326 duplicateBuffer(buf, display_value);
329 if (authenticated != NULL)
330 *authenticated = m_authenticated;
331 if (complete != NULL)
341 gss_eap_shib_attr_provider::mapToAny(int authenticated,
342 gss_buffer_t type_id) const
346 if (authenticated && !m_authenticated)
347 return (gss_any_t)NULL;
349 vector <Attribute *>v = duplicateAttributes(m_attributes);
351 output = (gss_any_t)new vector <Attribute *>(v);
357 gss_eap_shib_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
358 gss_any_t input) const
360 vector <Attribute *> *v = ((vector <Attribute *> *)input);
365 gss_eap_shib_attr_provider::exportToBuffer(gss_buffer_t buffer) const
371 buffer->value = NULL;
373 obj.addmember("version").integer(1);
374 obj.addmember("authenticated").integer(m_authenticated);
376 attrs = obj.addmember("attributes").list();
377 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
378 a != m_attributes.end(); ++a) {
379 DDF attr = (*a)->marshall();
385 string str = sink.str();
387 duplicateBuffer(str, buffer);
393 gss_eap_shib_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
394 const gss_buffer_t buffer)
396 if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer))
399 if (buffer->length == 0)
402 assert(m_authenticated == false);
403 assert(m_attributes.size() == 0);
406 string str((const char *)buffer->value, buffer->length);
407 istringstream source(str);
411 if (obj["version"].integer() != 1)
414 m_authenticated = (obj["authenticated"].integer() != 0);
416 DDF attrs = obj["attributes"];
417 DDF attr = attrs.first();
418 while (!attr.isnull()) {
419 Attribute *attribute = Attribute::unmarshall(attr);
420 m_attributes.push_back(attribute);
430 gss_eap_shib_attr_provider::init(void)
432 if (!ShibbolethResolver::init())
435 gss_eap_attr_ctx::registerProvider(ATTR_TYPE_LOCAL,
437 gss_eap_shib_attr_provider::createAttrContext);
443 gss_eap_shib_attr_provider::finalize(void)
445 gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_LOCAL);
446 ShibbolethResolver::term();
449 gss_eap_attr_provider *
450 gss_eap_shib_attr_provider::createAttrContext(void)
452 return new gss_eap_shib_attr_provider;
456 gss_eap_shib_attr_provider::duplicateAttribute(const Attribute *src)
458 DDF obj = src->marshall();
459 Attribute *attribute = Attribute::unmarshall(obj);
466 gss_eap_shib_attr_provider::duplicateAttributes(const vector <Attribute *>src)
468 vector <Attribute *> dst;
470 for (vector<Attribute *>::const_iterator a = src.begin();
473 dst.push_back(duplicateAttribute(*a));
479 gssEapLocalAttrProviderInit(OM_uint32 *minor)
481 return gss_eap_shib_attr_provider::init()
482 ? GSS_S_COMPLETE : GSS_S_FAILURE;
486 gssEapLocalAttrProviderFinalize(OM_uint32 *minor)
488 gss_eap_shib_attr_provider::finalize();
489 return GSS_S_COMPLETE;