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 "gssapiP_eap.h"
54 #include <xmltooling/XMLObject.h>
56 #include <xmltooling/XMLToolingConfig.h>
57 #include <xmltooling/util/ParserPool.h>
60 #include <saml/saml2/core/Assertions.h>
62 #include <shibsp/exceptions.h>
63 #include <shibsp/attribute/SimpleAttribute.h>
64 #include <shibsp/attribute/BinaryAttribute.h>
65 #include <shibsp/attribute/ScopedAttribute.h>
66 #include <shibresolver/resolver.h>
70 using namespace shibsp;
71 using namespace shibresolver;
72 using namespace xmltooling;
75 using namespace opensaml::saml2md;
76 using namespace opensaml;
78 using namespace xercesc;
81 gss_eap_shib_attr_provider::gss_eap_shib_attr_provider(void)
83 m_initialized = false;
84 m_authenticated = false;
87 gss_eap_shib_attr_provider::~gss_eap_shib_attr_provider(void)
89 for_each(m_attributes.begin(),
91 xmltooling::cleanup<Attribute>())
96 gss_eap_shib_attr_provider::initWithExistingContext(const gss_eap_attr_ctx *manager,
97 const gss_eap_attr_provider *ctx)
99 const gss_eap_shib_attr_provider *shib;
101 if (!gss_eap_attr_provider::initWithExistingContext(manager, ctx)) {
105 m_authenticated = false;
107 shib = static_cast<const gss_eap_shib_attr_provider *>(ctx);
109 m_attributes = duplicateAttributes(shib->getAttributes());
110 m_authenticated = shib->authenticated();
113 m_initialized = true;
119 gss_eap_shib_attr_provider::initWithGssContext(const gss_eap_attr_ctx *manager,
120 const gss_cred_id_t gssCred,
121 const gss_ctx_id_t gssCtx)
123 if (!gss_eap_attr_provider::initWithGssContext(manager, gssCred, gssCtx))
126 auto_ptr<ShibbolethResolver> resolver(ShibbolethResolver::create());
129 * For now, leave ApplicationID defaulted.
130 * Later on, we could allow this via config option to the mechanism
131 * or rely on an SPRequest interface to pass in a URI identifying the
135 gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
136 if (gssCred != GSS_C_NO_CREDENTIAL &&
137 gssEapDisplayName(&minor, gssCred->name, &nameBuf, NULL) == GSS_S_COMPLETE) {
138 resolver->setApplicationID((const char *)nameBuf.value);
139 gss_release_buffer(&minor, &nameBuf);
143 gss_buffer_desc mechName = GSS_C_EMPTY_BUFFER;
144 OM_uint32 major, minor;
146 major = gssEapExportNameInternal(&minor, gssCtx->initiatorName, &mechName,
147 EXPORT_NAME_FLAG_OID |
148 EXPORT_NAME_FLAG_COMPOSITE);
149 if (major == GSS_S_COMPLETE) {
150 resolver->addToken(&mechName);
151 gss_release_buffer(&minor, &mechName);
155 const gss_eap_saml_assertion_provider *saml;
156 saml = static_cast<const gss_eap_saml_assertion_provider *>
157 (m_manager->getProvider(ATTR_TYPE_SAML_ASSERTION));
158 if (saml != NULL && saml->getAssertion() != NULL) {
159 resolver->addToken(saml->getAssertion());
162 /* If no OpenSAML, parse the XML assertion explicitly */
163 const gss_eap_radius_attr_provider *radius;
164 int authenticated, complete;
165 gss_buffer_desc value = GSS_C_EMPTY_BUFFER;
167 radius = static_cast<const gss_eap_radius_attr_provider *>
168 (m_manager->getProvider(ATTR_TYPE_RADIUS));
169 if (radius != NULL &&
170 radius->getFragmentedAttribute(PW_SAML_AAA_ASSERTION,
172 &authenticated, &complete, &value)) {
173 string str((char *)value.value, value.length);
174 istringstream istream(str);
175 DOMDocument *doc = XMLToolingConfig::getConfig().getParser().parse(istream);
176 const XMLObjectBuilder *b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
177 resolver->addToken(b->buildFromDocument(doc));
178 gss_release_buffer(&minor, &value);
180 #endif /* HAVE_OPENSAML */
184 m_attributes = resolver->getResolvedAttributes();
185 resolver->getResolvedAttributes().clear();
186 } catch (exception &e) {
190 m_authenticated = true;
191 m_initialized = true;
197 gss_eap_shib_attr_provider::getAttributeIndex(const gss_buffer_t attr) const
201 GSSEAP_ASSERT(m_initialized);
203 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
204 a != m_attributes.end();
207 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
208 s != (*a)->getAliases().end();
210 if (attr->length == (*s).length() &&
211 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
221 gss_eap_shib_attr_provider::setAttribute(int complete GSSEAP_UNUSED,
222 const gss_buffer_t attr,
223 const gss_buffer_t value)
225 string attrStr((char *)attr->value, attr->length);
226 vector <string> ids(1, attrStr);
227 BinaryAttribute *a = new BinaryAttribute(ids);
229 GSSEAP_ASSERT(m_initialized);
231 if (value->length != 0) {
232 string valueStr((char *)value->value, value->length);
234 a->getValues().push_back(valueStr);
237 m_attributes.push_back(a);
238 m_authenticated = false;
244 gss_eap_shib_attr_provider::deleteAttribute(const gss_buffer_t attr)
248 GSSEAP_ASSERT(m_initialized);
250 i = getAttributeIndex(attr);
252 m_attributes.erase(m_attributes.begin() + i);
254 m_authenticated = false;
260 gss_eap_shib_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
263 GSSEAP_ASSERT(m_initialized);
265 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
266 a != m_attributes.end();
269 gss_buffer_desc attribute;
271 attribute.value = (void *)((*a)->getId());
272 attribute.length = strlen((char *)attribute.value);
274 if (!addAttribute(m_manager, this, &attribute, data))
282 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr) const
284 const Attribute *ret = NULL;
286 GSSEAP_ASSERT(m_initialized);
288 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
289 a != m_attributes.end();
292 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
293 s != (*a)->getAliases().end();
295 if (attr->length == (*s).length() &&
296 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
309 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr,
313 gss_buffer_t display_value,
316 const Attribute *shibAttr = NULL;
317 const BinaryAttribute *binaryAttr;
318 gss_buffer_desc valueBuf = GSS_C_EMPTY_BUFFER;
319 gss_buffer_desc displayValueBuf = GSS_C_EMPTY_BUFFER;
320 int nvalues, i = *more;
322 GSSEAP_ASSERT(m_initialized);
326 shibAttr = getAttribute(attr);
327 if (shibAttr == NULL)
330 nvalues = shibAttr->valueCount();
337 binaryAttr = dynamic_cast<const BinaryAttribute *>(shibAttr);
338 if (binaryAttr != NULL) {
339 std::string str = binaryAttr->getValues()[*more];
341 valueBuf.value = (void *)str.data();
342 valueBuf.length = str.size();
344 std::string str = shibAttr->getSerializedValues()[*more];
346 valueBuf.value = (void *)str.c_str();
347 valueBuf.length = str.length();
349 const SimpleAttribute *simpleAttr =
350 dynamic_cast<const SimpleAttribute *>(shibAttr);
351 const ScopedAttribute *scopedAttr =
352 dynamic_cast<const ScopedAttribute *>(shibAttr);
353 if (simpleAttr != NULL || scopedAttr != NULL)
354 displayValueBuf = valueBuf;
357 if (authenticated != NULL)
358 *authenticated = m_authenticated;
359 if (complete != NULL)
362 duplicateBuffer(valueBuf, value);
363 if (display_value != NULL)
364 duplicateBuffer(displayValueBuf, display_value);
372 gss_eap_shib_attr_provider::mapToAny(int authenticated,
373 gss_buffer_t type_id GSSEAP_UNUSED) const
377 GSSEAP_ASSERT(m_initialized);
379 if (authenticated && !m_authenticated)
380 return (gss_any_t)NULL;
382 vector <Attribute *>v = duplicateAttributes(m_attributes);
384 output = (gss_any_t)new vector <Attribute *>(v);
390 gss_eap_shib_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id GSSEAP_UNUSED,
391 gss_any_t input) const
393 GSSEAP_ASSERT(m_initialized);
395 vector <Attribute *> *v = ((vector <Attribute *> *)input);
400 gss_eap_shib_attr_provider::prefix(void) const
406 gss_eap_shib_attr_provider::name(void) const
412 gss_eap_shib_attr_provider::jsonRepresentation(void) const
416 if (m_initialized == false)
417 return obj; /* don't export incomplete context */
419 JSONObject jattrs = JSONObject::array();
421 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
422 a != m_attributes.end(); ++a) {
423 DDF attr = (*a)->marshall();
424 JSONObject jattr = JSONObject::ddf(attr);
425 jattrs.append(jattr);
428 obj.set("attributes", jattrs);
430 obj.set("authenticated", m_authenticated);
436 gss_eap_shib_attr_provider::initWithJsonObject(const gss_eap_attr_ctx *ctx,
439 if (!gss_eap_attr_provider::initWithJsonObject(ctx, obj))
442 GSSEAP_ASSERT(m_authenticated == false);
443 GSSEAP_ASSERT(m_attributes.size() == 0);
445 JSONObject jattrs = obj["attributes"];
446 size_t nelems = jattrs.size();
448 for (size_t i = 0; i < nelems; i++) {
449 JSONObject jattr = jattrs.get(i);
451 DDF attr = jattr.ddf();
452 Attribute *attribute = Attribute::unmarshall(attr);
453 m_attributes.push_back(attribute);
456 m_authenticated = obj["authenticated"].integer();
457 m_initialized = true;
463 gss_eap_shib_attr_provider::init(void)
468 ret = ShibbolethResolver::init();
469 } catch (exception &e) {
473 gss_eap_attr_ctx::registerProvider(ATTR_TYPE_LOCAL, createAttrContext);
479 gss_eap_shib_attr_provider::finalize(void)
481 gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_LOCAL);
482 ShibbolethResolver::term();
486 gss_eap_shib_attr_provider::mapException(OM_uint32 *minor,
487 std::exception &e) const
489 if (typeid(e) == typeid(AttributeException))
490 *minor = GSSEAP_SHIB_ATTR_FAILURE;
491 else if (typeid(e) == typeid(AttributeExtractionException))
492 *minor = GSSEAP_SHIB_ATTR_EXTRACT_FAILURE;
493 else if (typeid(e) == typeid(AttributeFilteringException))
494 *minor = GSSEAP_SHIB_ATTR_FILTER_FAILURE;
495 else if (typeid(e) == typeid(AttributeResolutionException))
496 *minor = GSSEAP_SHIB_ATTR_RESOLVE_FAILURE;
497 else if (typeid(e) == typeid(ConfigurationException))
498 *minor = GSSEAP_SHIB_CONFIG_FAILURE;
499 else if (typeid(e) == typeid(ListenerException))
500 *minor = GSSEAP_SHIB_LISTENER_FAILURE;
502 return GSS_S_CONTINUE_NEEDED;
504 gssEapSaveStatusInfo(*minor, "%s", e.what());
506 return GSS_S_FAILURE;
509 gss_eap_attr_provider *
510 gss_eap_shib_attr_provider::createAttrContext(void)
512 return new gss_eap_shib_attr_provider;
516 gss_eap_shib_attr_provider::duplicateAttribute(const Attribute *src)
518 DDF obj = src->marshall();
519 Attribute *attribute = Attribute::unmarshall(obj);
526 gss_eap_shib_attr_provider::duplicateAttributes(const vector <Attribute *>src)
528 vector <Attribute *> dst;
530 for (vector<Attribute *>::const_iterator a = src.begin();
533 dst.push_back(duplicateAttribute(*a));
539 gssEapLocalAttrProviderInit(OM_uint32 *minor)
541 if (!gss_eap_shib_attr_provider::init()) {
542 *minor = GSSEAP_SHIB_INIT_FAILURE;
543 return GSS_S_FAILURE;
545 return GSS_S_COMPLETE;
549 gssEapLocalAttrProviderFinalize(OM_uint32 *minor)
551 gss_eap_shib_attr_provider::finalize();
554 return GSS_S_COMPLETE;