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 <saml/saml1/core/Assertions.h>
62 #include <saml/saml2/core/Assertions.h>
63 #include <saml/saml2/metadata/Metadata.h>
64 #include <xercesc/util/XMLUniDefs.hpp>
65 #include <xmltooling/XMLToolingConfig.h>
66 #include <xmltooling/util/XMLHelper.h>
70 using namespace shibsp;
71 using namespace shibresolver;
72 using namespace opensaml::saml2md;
73 using namespace opensaml;
74 using namespace xmltooling::logging;
75 using namespace xmltooling;
76 using namespace xercesc;
80 gss_eap_shib_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *manager,
81 const gss_eap_attr_provider *ctx)
83 const gss_eap_shib_attr_provider *shib;
85 if (!gss_eap_attr_provider::initFromExistingContext(manager, ctx)) {
89 m_authenticated = false;
91 shib = static_cast<const gss_eap_shib_attr_provider *>(ctx);
93 m_attributes = duplicateAttributes(shib->getAttributes());
94 m_authenticated = shib->authenticated();
101 addRadiusAttribute(const gss_eap_attr_provider *provider,
102 const gss_buffer_t attribute,
105 const gss_eap_shib_attr_provider *shib;
106 const gss_eap_radius_attr_provider *radius;
107 int authenticated, complete, more = -1;
108 vector <string> attributeIds(1);
111 radius = static_cast<const gss_eap_radius_attr_provider *>(provider);
112 shib = static_cast<const gss_eap_shib_attr_provider *>(data);
114 assert(radius != NULL && shib != NULL);
116 string attributeName =
117 gss_eap_attr_ctx::composeAttributeName(ATTR_TYPE_RADIUS, attribute);
119 attributeIds.push_back(attributeName);
120 a = new SimpleAttribute(attributeIds);
125 gss_buffer_desc value = GSS_C_EMPTY_BUFFER;
128 if (!radius->getAttribute(attribute,
136 string attributeValue((char *)value.value, value.length);
137 a->getValues().push_back(attributeValue);
139 gss_release_buffer(&minor, &value);
142 shib->getAttributes().push_back(a);
148 gss_eap_shib_attr_provider::initFromGssContext(const gss_eap_attr_ctx *manager,
149 const gss_cred_id_t gssCred,
150 const gss_ctx_id_t gssCtx)
152 const gss_eap_saml_assertion_provider *saml;
153 const gss_eap_radius_attr_provider *radius;
154 gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
155 ShibbolethResolver *resolver = NULL;
158 if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
161 saml = static_cast<const gss_eap_saml_assertion_provider *>
162 (manager->getProvider(ATTR_TYPE_SAML_ASSERTION));
163 radius = static_cast<const gss_eap_radius_attr_provider *>
164 (manager->getProvider(ATTR_TYPE_RADIUS));
166 if (gssCred != GSS_C_NO_CREDENTIAL &&
167 gss_display_name(&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();
183 resolver->resolveAttributes(m_attributes);
185 gss_release_buffer(&minor, &nameBuf);
192 gss_eap_shib_attr_provider::~gss_eap_shib_attr_provider(void)
194 for_each(m_attributes.begin(),
196 xmltooling::cleanup<Attribute>())
201 gss_eap_shib_attr_provider::getAttributeIndex(const gss_buffer_t attr) const
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,
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);
230 ids.push_back(attrStr);
232 SimpleAttribute *a = new SimpleAttribute(ids);
234 if (value->length != 0) {
235 string valueStr((char *)value->value, value->length);
237 a->getValues().push_back(valueStr);
240 m_attributes.push_back(a);
241 m_authenticated = false;
245 gss_eap_shib_attr_provider::deleteAttribute(const gss_buffer_t attr)
249 i = getAttributeIndex(attr);
251 m_attributes.erase(m_attributes.begin() + i);
253 m_authenticated = false;
257 gss_eap_shib_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
260 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
261 a != m_attributes.end();
264 gss_buffer_desc attribute;
266 attribute.value = (void *)((*a)->getId());
267 attribute.length = strlen((char *)attribute.value);
269 if (!addAttribute(this, &attribute, data))
277 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr) const
279 const Attribute *ret = NULL;
281 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
282 a != m_attributes.end();
285 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
286 s != (*a)->getAliases().end();
288 if (attr->length == (*s).length() &&
289 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
302 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr,
306 gss_buffer_t display_value,
309 const Attribute *shibAttr = NULL;
311 int nvalues, i = *more;
315 shibAttr = getAttribute(attr);
316 if (shibAttr == NULL)
319 nvalues = shibAttr->valueCount();
323 else if (i >= nvalues)
326 buf.value = (void *)shibAttr->getString(*more);
327 buf.length = strlen((char *)buf.value);
329 if (buf.length != 0) {
331 duplicateBuffer(buf, value);
333 if (display_value != NULL)
334 duplicateBuffer(buf, display_value);
337 *authenticated = m_authenticated;
347 gss_eap_shib_attr_provider::mapToAny(int authenticated,
348 gss_buffer_t type_id) const
352 vector <Attribute *>v = duplicateAttributes(m_attributes);
354 output = (gss_any_t)new vector <Attribute *>(v);
360 gss_eap_shib_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
361 gss_any_t input) const
363 vector <Attribute *> *v = ((vector <Attribute *> *)input);
368 gss_eap_shib_attr_provider::exportToBuffer(gss_buffer_t buffer) const
374 buffer->value = NULL;
376 obj.addmember("version").integer(1);
377 obj.addmember("authenticated").integer(m_authenticated);
379 attrs = obj.addmember("attributes").list();
380 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
381 a != m_attributes.end(); ++a) {
382 DDF attr = (*a)->marshall();
388 string str = sink.str();
390 duplicateBuffer(str, buffer);
396 gss_eap_shib_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
397 const gss_buffer_t buffer)
399 if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer))
402 if (buffer->length == 0)
405 assert(m_authenticated == false);
406 assert(m_attributes.size() == 0);
409 string str((const char *)buffer->value, buffer->length);
410 istringstream source(str);
414 if (obj["version"].integer() != 1)
417 m_authenticated = (obj["authenticated"].integer() != 0);
419 DDF attrs = obj["attributes"];
420 DDF attr = attrs.first();
421 while (!attr.isnull()) {
422 Attribute *attribute = Attribute::unmarshall(attr);
423 m_attributes.push_back(attribute);
433 gss_eap_shib_attr_provider::init(void)
435 if (!ShibbolethResolver::init())
438 gss_eap_attr_ctx::registerProvider(ATTR_TYPE_LOCAL,
440 gss_eap_shib_attr_provider::createAttrContext);
446 gss_eap_shib_attr_provider::finalize(void)
448 ShibbolethResolver::term();
449 gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_LOCAL);
452 gss_eap_attr_provider *
453 gss_eap_shib_attr_provider::createAttrContext(void)
455 return new gss_eap_shib_attr_provider;
459 gss_eap_shib_attr_provider::duplicateAttribute(const Attribute *src)
461 Attribute *attribute;
463 DDF obj = src->marshall();
464 attribute = Attribute::unmarshall(obj);
471 gss_eap_shib_attr_provider::duplicateAttributes(const vector <Attribute *>src)
473 vector <Attribute *> dst;
475 for (vector<Attribute *>::const_iterator a = src.begin();
478 dst.push_back(duplicateAttribute(*a));