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 "gssapiP_eap.h"
50 #include <shibsp/Application.h>
51 #include <shibsp/exceptions.h>
52 #include <shibsp/SPConfig.h>
53 #include <shibsp/ServiceProvider.h>
54 #include <shibsp/attribute/Attribute.h>
55 #include <shibsp/attribute/SimpleAttribute.h>
56 #include <shibsp/attribute/resolver/ResolutionContext.h>
57 #include <shibsp/handler/AssertionConsumerService.h>
58 #include <shibsp/metadata/MetadataProviderCriteria.h>
59 #include <shibsp/util/SPConstants.h>
61 #include <shibresolver/resolver.h>
63 using namespace shibsp;
64 using namespace shibresolver;
65 using namespace opensaml::saml2md;
66 using namespace opensaml;
67 using namespace xmltooling::logging;
68 using namespace xmltooling;
69 using namespace xercesc;
72 gss_eap_shib_attr_provider::gss_eap_shib_attr_provider(void)
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::initFromExistingContext(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::initFromExistingContext(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();
107 addRadiusAttribute(const gss_eap_attr_provider *provider,
108 const gss_buffer_t attribute,
111 const gss_eap_shib_attr_provider *shib;
112 const gss_eap_radius_attr_provider *radius;
113 int authenticated, complete, more = -1;
114 vector <string> attributeIds(1);
117 radius = static_cast<const gss_eap_radius_attr_provider *>(provider);
118 shib = static_cast<const gss_eap_shib_attr_provider *>(data);
120 assert(radius != NULL && shib != NULL);
122 string attributeName =
123 gss_eap_attr_ctx::composeAttributeName(ATTR_TYPE_RADIUS, attribute);
125 attributeIds.push_back(attributeName);
126 a = new SimpleAttribute(attributeIds);
131 gss_buffer_desc value = GSS_C_EMPTY_BUFFER;
134 if (!radius->getAttribute(attribute,
142 string attributeValue((char *)value.value, value.length);
143 a->getValues().push_back(attributeValue);
145 gss_release_buffer(&minor, &value);
148 shib->getAttributes().push_back(a);
154 gss_eap_shib_attr_provider::initFromGssContext(const gss_eap_attr_ctx *manager,
155 const gss_cred_id_t gssCred,
156 const gss_ctx_id_t gssCtx)
158 const gss_eap_saml_assertion_provider *saml;
159 const gss_eap_radius_attr_provider *radius;
160 gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
161 ShibbolethResolver *resolver;
164 if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
167 saml = static_cast<const gss_eap_saml_assertion_provider *>
168 (manager->getProvider(ATTR_TYPE_SAML_ASSERTION));
169 radius = static_cast<const gss_eap_radius_attr_provider *>
170 (manager->getProvider(ATTR_TYPE_RADIUS));
172 resolver = ShibbolethResolver::create();
174 if (gssCred != GSS_C_NO_CREDENTIAL &&
175 gss_display_name(&minor, gssCred->name, &nameBuf, NULL) == GSS_S_COMPLETE)
176 resolver->setApplicationID((const char *)nameBuf.value);
178 m_authenticated = false;
180 if (radius != NULL) {
181 radius->getAttributeTypes(addRadiusAttribute, (void *)this);
182 m_authenticated = radius->authenticated();
185 if (saml != NULL && saml->getAssertion() != NULL) {
186 resolver->addToken(saml->getAssertion());
188 m_authenticated = saml->authenticated();
193 m_attributes = resolver->getResolvedAttributes();
194 resolver->getResolvedAttributes().clear();
196 gss_release_buffer(&minor, &nameBuf);
201 gss_buffer_desc testattr = {
202 sizeof("urn:greet:greeting") - 1, (void *)"urn:greet:greeting" };
203 gss_buffer_desc testval =
204 { sizeof("Hello, GSS EAP.") - 1, (void *)"Hello, GSS EAP." };
205 setAttribute(true, &testattr, &testval);
206 #endif /* GSSEAP_DEBUG */
212 gss_eap_shib_attr_provider::getAttributeIndex(const gss_buffer_t attr) const
216 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
217 a != m_attributes.end();
220 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
221 s != (*a)->getAliases().end();
223 if (attr->length == (*s).length() &&
224 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
234 gss_eap_shib_attr_provider::setAttribute(int complete,
235 const gss_buffer_t attr,
236 const gss_buffer_t value)
238 string attrStr((char *)attr->value, attr->length);
239 vector <string> ids(1, attrStr);
240 SimpleAttribute *a = new SimpleAttribute(ids);
242 if (value->length != 0) {
243 string valueStr((char *)value->value, value->length);
245 a->getValues().push_back(valueStr);
248 m_attributes.push_back(a);
249 m_authenticated = false;
253 gss_eap_shib_attr_provider::deleteAttribute(const gss_buffer_t attr)
257 i = getAttributeIndex(attr);
259 m_attributes.erase(m_attributes.begin() + i);
261 m_authenticated = false;
265 gss_eap_shib_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
268 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
269 a != m_attributes.end();
272 gss_buffer_desc attribute;
274 attribute.value = (void *)((*a)->getId());
275 attribute.length = strlen((char *)attribute.value);
277 if (!addAttribute(this, &attribute, data))
285 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr) const
287 const Attribute *ret = NULL;
289 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
290 a != m_attributes.end();
293 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
294 s != (*a)->getAliases().end();
296 if (attr->length == (*s).length() &&
297 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
310 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr,
314 gss_buffer_t display_value,
317 const Attribute *shibAttr = NULL;
319 int nvalues, i = *more;
323 shibAttr = getAttribute(attr);
324 if (shibAttr == NULL)
327 nvalues = shibAttr->valueCount();
331 else if (i >= nvalues)
334 buf.value = (void *)shibAttr->getString(*more);
335 buf.length = strlen((char *)buf.value);
337 if (buf.length != 0) {
339 duplicateBuffer(buf, value);
341 if (display_value != NULL)
342 duplicateBuffer(buf, display_value);
345 if (authenticated != NULL)
346 *authenticated = m_authenticated;
347 if (complete != NULL)
357 gss_eap_shib_attr_provider::mapToAny(int authenticated,
358 gss_buffer_t type_id) const
362 vector <Attribute *>v = duplicateAttributes(m_attributes);
364 output = (gss_any_t)new vector <Attribute *>(v);
370 gss_eap_shib_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
371 gss_any_t input) const
373 vector <Attribute *> *v = ((vector <Attribute *> *)input);
378 gss_eap_shib_attr_provider::exportToBuffer(gss_buffer_t buffer) const
384 buffer->value = NULL;
386 obj.addmember("version").integer(1);
387 obj.addmember("authenticated").integer(m_authenticated);
389 attrs = obj.addmember("attributes").list();
390 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
391 a != m_attributes.end(); ++a) {
392 DDF attr = (*a)->marshall();
398 string str = sink.str();
400 duplicateBuffer(str, buffer);
406 gss_eap_shib_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
407 const gss_buffer_t buffer)
409 if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer))
412 if (buffer->length == 0)
415 assert(m_authenticated == false);
416 assert(m_attributes.size() == 0);
419 string str((const char *)buffer->value, buffer->length);
420 istringstream source(str);
424 if (obj["version"].integer() != 1)
427 m_authenticated = (obj["authenticated"].integer() != 0);
429 DDF attrs = obj["attributes"];
430 DDF attr = attrs.first();
431 while (!attr.isnull()) {
432 Attribute *attribute = Attribute::unmarshall(attr);
433 m_attributes.push_back(attribute);
443 gss_eap_shib_attr_provider::init(void)
446 SPConfig& conf=SPConfig::getConfig();
450 SPConfig::AttributeResolution |
451 SPConfig::Credentials |
452 SPConfig::OutOfProcess
456 if (!conf.instantiate()) {
461 if (!ShibbolethResolver::init())
465 gss_eap_attr_ctx::registerProvider(ATTR_TYPE_LOCAL,
467 gss_eap_shib_attr_provider::createAttrContext);
473 gss_eap_shib_attr_provider::finalize(void)
475 gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_LOCAL);
476 // ShibbolethResolver::term();
479 gss_eap_attr_provider *
480 gss_eap_shib_attr_provider::createAttrContext(void)
482 return new gss_eap_shib_attr_provider;
486 gss_eap_shib_attr_provider::duplicateAttribute(const Attribute *src)
488 DDF obj = src->marshall();
489 Attribute *attribute = Attribute::unmarshall(obj);
496 gss_eap_shib_attr_provider::duplicateAttributes(const vector <Attribute *>src)
498 vector <Attribute *> dst;
500 for (vector<Attribute *>::const_iterator a = src.begin();
503 dst.push_back(duplicateAttribute(*a));
509 gssEapLocalAttrProviderInit(OM_uint32 *minor)
511 return gss_eap_shib_attr_provider::init()
512 ? GSS_S_COMPLETE : GSS_S_FAILURE;
516 gssEapLocalAttrProviderFinalize(OM_uint32 *minor)
518 gss_eap_shib_attr_provider::finalize();
519 return GSS_S_COMPLETE;