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/exceptions.h>
51 #include <shibsp/attribute/SimpleAttribute.h>
52 #include <shibsp/handler/AssertionConsumerService.h>
54 #include <shibresolver/resolver.h>
56 using namespace shibsp;
57 using namespace shibresolver;
58 using namespace opensaml::saml2md;
59 using namespace opensaml;
60 using namespace xmltooling::logging;
61 using namespace xmltooling;
62 using namespace xercesc;
65 gss_eap_shib_attr_provider::gss_eap_shib_attr_provider(void)
67 m_authenticated = false;
70 gss_eap_shib_attr_provider::~gss_eap_shib_attr_provider(void)
72 for_each(m_attributes.begin(),
74 xmltooling::cleanup<Attribute>())
79 gss_eap_shib_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *manager,
80 const gss_eap_attr_provider *ctx)
82 const gss_eap_shib_attr_provider *shib;
84 if (!gss_eap_attr_provider::initFromExistingContext(manager, ctx)) {
88 m_authenticated = false;
90 shib = static_cast<const gss_eap_shib_attr_provider *>(ctx);
92 m_attributes = duplicateAttributes(shib->getAttributes());
93 m_authenticated = shib->authenticated();
100 addRadiusAttribute(const gss_eap_attr_provider *provider,
101 const gss_buffer_t attribute,
104 const gss_eap_shib_attr_provider *shib;
105 const gss_eap_radius_attr_provider *radius;
106 int authenticated, complete, more = -1;
107 vector <string> attributeIds(1);
110 radius = static_cast<const gss_eap_radius_attr_provider *>(provider);
111 shib = static_cast<const gss_eap_shib_attr_provider *>(data);
113 assert(radius != NULL && shib != NULL);
115 string attributeName =
116 gss_eap_attr_ctx::composeAttributeName(ATTR_TYPE_RADIUS, attribute);
118 attributeIds.push_back(attributeName);
119 a = new SimpleAttribute(attributeIds);
124 gss_buffer_desc value = GSS_C_EMPTY_BUFFER;
127 if (!radius->getAttribute(attribute,
135 string attributeValue((char *)value.value, value.length);
136 a->getValues().push_back(attributeValue);
138 gss_release_buffer(&minor, &value);
141 shib->getAttributes().push_back(a);
147 gss_eap_shib_attr_provider::initFromGssContext(const gss_eap_attr_ctx *manager,
148 const gss_cred_id_t gssCred,
149 const gss_ctx_id_t gssCtx)
151 const gss_eap_saml_assertion_provider *saml;
152 const gss_eap_radius_attr_provider *radius;
153 gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
154 ShibbolethResolver *resolver;
157 if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
160 saml = static_cast<const gss_eap_saml_assertion_provider *>
161 (manager->getProvider(ATTR_TYPE_SAML_ASSERTION));
162 radius = static_cast<const gss_eap_radius_attr_provider *>
163 (manager->getProvider(ATTR_TYPE_RADIUS));
165 resolver = ShibbolethResolver::create();
167 if (gssCred != GSS_C_NO_CREDENTIAL &&
168 gss_display_name(&minor, gssCred->name, &nameBuf, NULL) == GSS_S_COMPLETE)
169 resolver->setApplicationID((const char *)nameBuf.value);
171 m_authenticated = false;
173 if (radius != NULL) {
174 radius->getAttributeTypes(addRadiusAttribute, (void *)this);
175 m_authenticated = radius->authenticated();
178 if (saml != NULL && saml->getAssertion() != NULL) {
179 resolver->addToken(saml->getAssertion());
181 m_authenticated = saml->authenticated();
186 m_attributes = resolver->getResolvedAttributes();
187 resolver->getResolvedAttributes().clear();
189 gss_release_buffer(&minor, &nameBuf);
194 gss_buffer_desc testattr = {
195 sizeof("urn:greet:greeting") - 1, (void *)"urn:greet:greeting" };
196 gss_buffer_desc testval =
197 { sizeof("Hello, GSS EAP.") - 1, (void *)"Hello, GSS EAP." };
198 setAttribute(true, &testattr, &testval);
199 #endif /* GSSEAP_DEBUG */
205 gss_eap_shib_attr_provider::getAttributeIndex(const gss_buffer_t attr) const
209 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
210 a != m_attributes.end();
213 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
214 s != (*a)->getAliases().end();
216 if (attr->length == (*s).length() &&
217 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
227 gss_eap_shib_attr_provider::setAttribute(int complete,
228 const gss_buffer_t attr,
229 const gss_buffer_t value)
231 string attrStr((char *)attr->value, attr->length);
232 vector <string> ids(1, attrStr);
233 SimpleAttribute *a = new SimpleAttribute(ids);
235 if (value->length != 0) {
236 string valueStr((char *)value->value, value->length);
238 a->getValues().push_back(valueStr);
241 m_attributes.push_back(a);
242 m_authenticated = false;
246 gss_eap_shib_attr_provider::deleteAttribute(const gss_buffer_t attr)
250 i = getAttributeIndex(attr);
252 m_attributes.erase(m_attributes.begin() + i);
254 m_authenticated = false;
258 gss_eap_shib_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
261 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
262 a != m_attributes.end();
265 gss_buffer_desc attribute;
267 attribute.value = (void *)((*a)->getId());
268 attribute.length = strlen((char *)attribute.value);
270 if (!addAttribute(this, &attribute, data))
278 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr) const
280 const Attribute *ret = NULL;
282 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
283 a != m_attributes.end();
286 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
287 s != (*a)->getAliases().end();
289 if (attr->length == (*s).length() &&
290 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
303 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr,
307 gss_buffer_t display_value,
310 const Attribute *shibAttr = NULL;
312 int nvalues, i = *more;
316 shibAttr = getAttribute(attr);
317 if (shibAttr == NULL)
320 nvalues = shibAttr->valueCount();
324 else if (i >= nvalues)
327 buf.value = (void *)shibAttr->getString(*more);
328 buf.length = strlen((char *)buf.value);
330 if (buf.length != 0) {
332 duplicateBuffer(buf, value);
334 if (display_value != NULL)
335 duplicateBuffer(buf, display_value);
338 if (authenticated != NULL)
339 *authenticated = m_authenticated;
340 if (complete != NULL)
350 gss_eap_shib_attr_provider::mapToAny(int authenticated,
351 gss_buffer_t type_id) const
355 vector <Attribute *>v = duplicateAttributes(m_attributes);
357 output = (gss_any_t)new vector <Attribute *>(v);
363 gss_eap_shib_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
364 gss_any_t input) const
366 vector <Attribute *> *v = ((vector <Attribute *> *)input);
371 gss_eap_shib_attr_provider::exportToBuffer(gss_buffer_t buffer) const
377 buffer->value = NULL;
379 obj.addmember("version").integer(1);
380 obj.addmember("authenticated").integer(m_authenticated);
382 attrs = obj.addmember("attributes").list();
383 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
384 a != m_attributes.end(); ++a) {
385 DDF attr = (*a)->marshall();
391 string str = sink.str();
393 duplicateBuffer(str, buffer);
399 gss_eap_shib_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
400 const gss_buffer_t buffer)
402 if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer))
405 if (buffer->length == 0)
408 assert(m_authenticated == false);
409 assert(m_attributes.size() == 0);
412 string str((const char *)buffer->value, buffer->length);
413 istringstream source(str);
417 if (obj["version"].integer() != 1)
420 m_authenticated = (obj["authenticated"].integer() != 0);
422 DDF attrs = obj["attributes"];
423 DDF attr = attrs.first();
424 while (!attr.isnull()) {
425 Attribute *attribute = Attribute::unmarshall(attr);
426 m_attributes.push_back(attribute);
436 gss_eap_shib_attr_provider::init(void)
439 SPConfig& conf=SPConfig::getConfig();
443 SPConfig::AttributeResolution |
444 SPConfig::Credentials |
445 SPConfig::OutOfProcess
449 if (!conf.instantiate()) {
454 if (!ShibbolethResolver::init())
458 gss_eap_attr_ctx::registerProvider(ATTR_TYPE_LOCAL,
460 gss_eap_shib_attr_provider::createAttrContext);
466 gss_eap_shib_attr_provider::finalize(void)
468 gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_LOCAL);
469 // ShibbolethResolver::term();
472 gss_eap_attr_provider *
473 gss_eap_shib_attr_provider::createAttrContext(void)
475 return new gss_eap_shib_attr_provider;
479 gss_eap_shib_attr_provider::duplicateAttribute(const Attribute *src)
481 DDF obj = src->marshall();
482 Attribute *attribute = Attribute::unmarshall(obj);
489 gss_eap_shib_attr_provider::duplicateAttributes(const vector <Attribute *>src)
491 vector <Attribute *> dst;
493 for (vector<Attribute *>::const_iterator a = src.begin();
496 dst.push_back(duplicateAttribute(*a));
502 gssEapLocalAttrProviderInit(OM_uint32 *minor)
504 return gss_eap_shib_attr_provider::init()
505 ? GSS_S_COMPLETE : GSS_S_FAILURE;
509 gssEapLocalAttrProviderFinalize(OM_uint32 *minor)
511 gss_eap_shib_attr_provider::finalize();
512 return GSS_S_COMPLETE;