2 * Copyright (c) 2011, 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.
49 * Local attribute provider implementation.
52 #include <xmltooling/XMLObject.h>
54 #include <saml/saml2/core/Assertions.h>
56 #include <shibsp/exceptions.h>
57 #include <shibsp/attribute/SimpleAttribute.h>
58 #include <shibsp/attribute/BinaryAttribute.h>
59 #include <shibsp/attribute/ScopedAttribute.h>
60 #include <shibresolver/resolver.h>
64 #include "gssapiP_eap.h"
66 using namespace shibsp;
67 using namespace shibresolver;
68 using namespace opensaml::saml2md;
69 using namespace opensaml;
70 using namespace xmltooling;
73 gss_eap_shib_attr_provider::gss_eap_shib_attr_provider(void)
75 m_initialized = false;
76 m_authenticated = false;
79 gss_eap_shib_attr_provider::~gss_eap_shib_attr_provider(void)
81 for_each(m_attributes.begin(),
83 xmltooling::cleanup<Attribute>())
88 gss_eap_shib_attr_provider::initWithExistingContext(const gss_eap_attr_ctx *manager,
89 const gss_eap_attr_provider *ctx)
91 const gss_eap_shib_attr_provider *shib;
93 if (!gss_eap_attr_provider::initWithExistingContext(manager, ctx)) {
97 m_authenticated = false;
99 shib = static_cast<const gss_eap_shib_attr_provider *>(ctx);
101 m_attributes = duplicateAttributes(shib->getAttributes());
102 m_authenticated = shib->authenticated();
105 m_initialized = true;
111 gss_eap_shib_attr_provider::initWithGssContext(const gss_eap_attr_ctx *manager,
112 const gss_cred_id_t gssCred,
113 const gss_ctx_id_t gssCtx)
115 if (!gss_eap_attr_provider::initWithGssContext(manager, gssCred, gssCtx))
118 auto_ptr<ShibbolethResolver> resolver(ShibbolethResolver::create());
121 * For now, leave ApplicationID defaulted.
122 * Later on, we could allow this via config option to the mechanism
123 * or rely on an SPRequest interface to pass in a URI identifying the
127 gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
128 if (gssCred != GSS_C_NO_CREDENTIAL &&
129 gssEapDisplayName(&minor, gssCred->name, &nameBuf, NULL) == GSS_S_COMPLETE) {
130 resolver->setApplicationID((const char *)nameBuf.value);
131 gss_release_buffer(&minor, &nameBuf);
135 gss_buffer_desc mechName = GSS_C_EMPTY_BUFFER;
136 OM_uint32 major, minor;
138 major = gssEapExportNameInternal(&minor, gssCtx->initiatorName, &mechName,
139 EXPORT_NAME_FLAG_OID |
140 EXPORT_NAME_FLAG_COMPOSITE);
141 if (major == GSS_S_COMPLETE) {
142 resolver->addToken(&mechName);
143 gss_release_buffer(&minor, &mechName);
146 const gss_eap_saml_assertion_provider *saml;
147 saml = static_cast<const gss_eap_saml_assertion_provider *>
148 (m_manager->getProvider(ATTR_TYPE_SAML_ASSERTION));
149 if (saml != NULL && saml->getAssertion() != NULL) {
150 resolver->addToken(saml->getAssertion());
155 m_attributes = resolver->getResolvedAttributes();
156 resolver->getResolvedAttributes().clear();
157 } catch (exception &e) {
161 m_authenticated = true;
162 m_initialized = true;
168 gss_eap_shib_attr_provider::getAttributeIndex(const gss_buffer_t attr) const
172 assert(m_initialized);
174 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
175 a != m_attributes.end();
178 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
179 s != (*a)->getAliases().end();
181 if (attr->length == (*s).length() &&
182 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
192 gss_eap_shib_attr_provider::setAttribute(int complete GSSEAP_UNUSED,
193 const gss_buffer_t attr,
194 const gss_buffer_t value)
196 string attrStr((char *)attr->value, attr->length);
197 vector <string> ids(1, attrStr);
198 BinaryAttribute *a = new BinaryAttribute(ids);
200 assert(m_initialized);
202 if (value->length != 0) {
203 string valueStr((char *)value->value, value->length);
205 a->getValues().push_back(valueStr);
208 m_attributes.push_back(a);
209 m_authenticated = false;
215 gss_eap_shib_attr_provider::deleteAttribute(const gss_buffer_t attr)
219 assert(m_initialized);
221 i = getAttributeIndex(attr);
223 m_attributes.erase(m_attributes.begin() + i);
225 m_authenticated = false;
231 gss_eap_shib_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
234 assert(m_initialized);
236 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
237 a != m_attributes.end();
240 gss_buffer_desc attribute;
242 attribute.value = (void *)((*a)->getId());
243 attribute.length = strlen((char *)attribute.value);
245 if (!addAttribute(m_manager, this, &attribute, data))
253 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr) const
255 const Attribute *ret = NULL;
257 assert(m_initialized);
259 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
260 a != m_attributes.end();
263 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
264 s != (*a)->getAliases().end();
266 if (attr->length == (*s).length() &&
267 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
280 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr,
284 gss_buffer_t display_value,
287 const Attribute *shibAttr = NULL;
288 const BinaryAttribute *binaryAttr;
289 gss_buffer_desc valueBuf = GSS_C_EMPTY_BUFFER;
290 gss_buffer_desc displayValueBuf = GSS_C_EMPTY_BUFFER;
291 int nvalues, i = *more;
293 assert(m_initialized);
297 shibAttr = getAttribute(attr);
298 if (shibAttr == NULL)
301 nvalues = shibAttr->valueCount();
308 binaryAttr = dynamic_cast<const BinaryAttribute *>(shibAttr);
309 if (binaryAttr != NULL) {
310 std::string str = binaryAttr->getValues()[*more];
312 valueBuf.value = (void *)str.data();
313 valueBuf.length = str.size();
315 std::string str = shibAttr->getSerializedValues()[*more];
317 valueBuf.value = (void *)str.c_str();
318 valueBuf.length = str.length();
320 const SimpleAttribute *simpleAttr =
321 dynamic_cast<const SimpleAttribute *>(shibAttr);
322 const ScopedAttribute *scopedAttr =
323 dynamic_cast<const ScopedAttribute *>(shibAttr);
324 if (simpleAttr != NULL || scopedAttr != NULL)
325 displayValueBuf = valueBuf;
328 if (authenticated != NULL)
329 *authenticated = m_authenticated;
330 if (complete != NULL)
333 duplicateBuffer(valueBuf, value);
334 if (display_value != NULL)
335 duplicateBuffer(displayValueBuf, display_value);
343 gss_eap_shib_attr_provider::mapToAny(int authenticated,
344 gss_buffer_t type_id GSSEAP_UNUSED) const
348 assert(m_initialized);
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 GSSEAP_UNUSED,
362 gss_any_t input) const
364 assert(m_initialized);
366 vector <Attribute *> *v = ((vector <Attribute *> *)input);
371 gss_eap_shib_attr_provider::prefix(void) const
377 gss_eap_shib_attr_provider::name(void) const
383 gss_eap_shib_attr_provider::jsonRepresentation(void) const
387 if (m_initialized == false)
388 return obj; /* don't export incomplete context */
390 JSONObject jattrs = JSONObject::array();
392 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
393 a != m_attributes.end(); ++a) {
394 DDF attr = (*a)->marshall();
395 JSONObject jattr = JSONObject::ddf(attr);
396 jattrs.append(jattr);
399 obj.set("attributes", jattrs);
401 obj.set("authenticated", m_authenticated);
407 gss_eap_shib_attr_provider::initWithJsonObject(const gss_eap_attr_ctx *ctx,
410 if (!gss_eap_attr_provider::initWithJsonObject(ctx, obj))
413 assert(m_authenticated == false);
414 assert(m_attributes.size() == 0);
416 JSONObject jattrs = obj["attributes"];
417 size_t nelems = jattrs.size();
419 for (size_t i = 0; i < nelems; i++) {
420 JSONObject jattr = jattrs.get(i);
422 DDF attr = jattr.ddf();
423 Attribute *attribute = Attribute::unmarshall(attr);
424 m_attributes.push_back(attribute);
427 m_authenticated = obj["authenticated"].integer();
428 m_initialized = true;
434 gss_eap_shib_attr_provider::init(void)
439 if (SPConfig::getConfig().getFeatures() == 0)
440 ret = ShibbolethResolver::init();
441 } catch (exception &e) {
445 gss_eap_attr_ctx::registerProvider(ATTR_TYPE_LOCAL, createAttrContext);
451 gss_eap_shib_attr_provider::finalize(void)
453 gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_LOCAL);
454 ShibbolethResolver::term();
458 gss_eap_shib_attr_provider::mapException(OM_uint32 *minor,
459 std::exception &e) const
461 if (typeid(e) == typeid(AttributeException))
462 *minor = GSSEAP_SHIB_ATTR_FAILURE;
463 else if (typeid(e) == typeid(AttributeExtractionException))
464 *minor = GSSEAP_SHIB_ATTR_EXTRACT_FAILURE;
465 else if (typeid(e) == typeid(AttributeFilteringException))
466 *minor = GSSEAP_SHIB_ATTR_FILTER_FAILURE;
467 else if (typeid(e) == typeid(AttributeResolutionException))
468 *minor = GSSEAP_SHIB_ATTR_RESOLVE_FAILURE;
469 else if (typeid(e) == typeid(ConfigurationException))
470 *minor = GSSEAP_SHIB_CONFIG_FAILURE;
471 else if (typeid(e) == typeid(ListenerException))
472 *minor = GSSEAP_SHIB_LISTENER_FAILURE;
474 return GSS_S_CONTINUE_NEEDED;
476 gssEapSaveStatusInfo(*minor, "%s", e.what());
478 return GSS_S_FAILURE;
481 gss_eap_attr_provider *
482 gss_eap_shib_attr_provider::createAttrContext(void)
484 return new gss_eap_shib_attr_provider;
488 gss_eap_shib_attr_provider::duplicateAttribute(const Attribute *src)
490 DDF obj = src->marshall();
491 Attribute *attribute = Attribute::unmarshall(obj);
498 gss_eap_shib_attr_provider::duplicateAttributes(const vector <Attribute *>src)
500 vector <Attribute *> dst;
502 for (vector<Attribute *>::const_iterator a = src.begin();
505 dst.push_back(duplicateAttribute(*a));
511 gssEapLocalAttrProviderInit(OM_uint32 *minor)
513 if (!gss_eap_shib_attr_provider::init()) {
514 *minor = GSSEAP_SHIB_INIT_FAILURE;
515 return GSS_S_FAILURE;
517 return GSS_S_COMPLETE;
521 gssEapLocalAttrProviderFinalize(OM_uint32 *minor)
523 gss_eap_shib_attr_provider::finalize();
526 return GSS_S_COMPLETE;