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;
166 gss_eap_attrid attrid(VENDORPEC_UKERNA, PW_SAML_AAA_ASSERTION);
168 radius = static_cast<const gss_eap_radius_attr_provider *>
169 (m_manager->getProvider(ATTR_TYPE_RADIUS));
170 if (radius != NULL &&
171 radius->getFragmentedAttribute(attrid, &authenticated, &complete, &value)) {
172 string str((char *)value.value, value.length);
173 istringstream istream(str);
174 DOMDocument *doc = XMLToolingConfig::getConfig().getParser().parse(istream);
175 const XMLObjectBuilder *b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
176 resolver->addToken(b->buildFromDocument(doc));
177 gss_release_buffer(&minor, &value);
179 #endif /* HAVE_OPENSAML */
183 m_attributes = resolver->getResolvedAttributes();
184 resolver->getResolvedAttributes().clear();
185 } catch (exception &e) {
189 m_authenticated = true;
190 m_initialized = true;
196 gss_eap_shib_attr_provider::getAttributeIndex(const gss_buffer_t attr) const
200 GSSEAP_ASSERT(m_initialized);
202 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
203 a != m_attributes.end();
206 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
207 s != (*a)->getAliases().end();
209 if (attr->length == (*s).length() &&
210 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
220 gss_eap_shib_attr_provider::setAttribute(int complete GSSEAP_UNUSED,
221 const gss_buffer_t attr,
222 const gss_buffer_t value)
224 string attrStr((char *)attr->value, attr->length);
225 vector <string> ids(1, attrStr);
226 BinaryAttribute *a = new BinaryAttribute(ids);
228 GSSEAP_ASSERT(m_initialized);
230 if (value->length != 0) {
231 string valueStr((char *)value->value, value->length);
233 a->getValues().push_back(valueStr);
236 m_attributes.push_back(a);
237 m_authenticated = false;
243 gss_eap_shib_attr_provider::deleteAttribute(const gss_buffer_t attr)
247 GSSEAP_ASSERT(m_initialized);
249 i = getAttributeIndex(attr);
251 m_attributes.erase(m_attributes.begin() + i);
253 m_authenticated = false;
259 gss_eap_shib_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
262 GSSEAP_ASSERT(m_initialized);
264 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
265 a != m_attributes.end();
268 gss_buffer_desc attribute;
270 attribute.value = (void *)((*a)->getId());
271 attribute.length = strlen((char *)attribute.value);
273 if (!addAttribute(m_manager, this, &attribute, data))
281 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr) const
283 const Attribute *ret = NULL;
285 GSSEAP_ASSERT(m_initialized);
287 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
288 a != m_attributes.end();
291 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
292 s != (*a)->getAliases().end();
294 if (attr->length == (*s).length() &&
295 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
308 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr,
312 gss_buffer_t display_value,
315 const Attribute *shibAttr = NULL;
316 const BinaryAttribute *binaryAttr;
317 gss_buffer_desc valueBuf = GSS_C_EMPTY_BUFFER;
318 gss_buffer_desc displayValueBuf = GSS_C_EMPTY_BUFFER;
319 int nvalues, i = *more;
321 GSSEAP_ASSERT(m_initialized);
325 shibAttr = getAttribute(attr);
326 if (shibAttr == NULL)
329 nvalues = shibAttr->valueCount();
336 binaryAttr = dynamic_cast<const BinaryAttribute *>(shibAttr);
337 if (binaryAttr != NULL) {
338 std::string str = binaryAttr->getValues()[*more];
340 valueBuf.value = (void *)str.data();
341 valueBuf.length = str.size();
343 std::string str = shibAttr->getSerializedValues()[*more];
345 valueBuf.value = (void *)str.c_str();
346 valueBuf.length = str.length();
348 const SimpleAttribute *simpleAttr =
349 dynamic_cast<const SimpleAttribute *>(shibAttr);
350 const ScopedAttribute *scopedAttr =
351 dynamic_cast<const ScopedAttribute *>(shibAttr);
352 if (simpleAttr != NULL || scopedAttr != NULL)
353 displayValueBuf = valueBuf;
356 if (authenticated != NULL)
357 *authenticated = m_authenticated;
358 if (complete != NULL)
361 duplicateBuffer(valueBuf, value);
362 if (display_value != NULL)
363 duplicateBuffer(displayValueBuf, display_value);
371 gss_eap_shib_attr_provider::mapToAny(int authenticated,
372 gss_buffer_t type_id GSSEAP_UNUSED) const
376 GSSEAP_ASSERT(m_initialized);
378 if (authenticated && !m_authenticated)
379 return (gss_any_t)NULL;
381 vector <Attribute *>v = duplicateAttributes(m_attributes);
383 output = (gss_any_t)new vector <Attribute *>(v);
389 gss_eap_shib_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id GSSEAP_UNUSED,
390 gss_any_t input) const
392 GSSEAP_ASSERT(m_initialized);
394 vector <Attribute *> *v = ((vector <Attribute *> *)input);
399 gss_eap_shib_attr_provider::prefix(void) const
405 gss_eap_shib_attr_provider::name(void) const
411 gss_eap_shib_attr_provider::jsonRepresentation(void) const
415 if (m_initialized == false)
416 return obj; /* don't export incomplete context */
418 JSONObject jattrs = JSONObject::array();
420 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
421 a != m_attributes.end(); ++a) {
422 DDF attr = (*a)->marshall();
423 JSONObject jattr = JSONObject::ddf(attr);
424 jattrs.append(jattr);
427 obj.set("attributes", jattrs);
429 obj.set("authenticated", m_authenticated);
435 gss_eap_shib_attr_provider::initWithJsonObject(const gss_eap_attr_ctx *ctx,
438 if (!gss_eap_attr_provider::initWithJsonObject(ctx, obj))
441 GSSEAP_ASSERT(m_authenticated == false);
442 GSSEAP_ASSERT(m_attributes.size() == 0);
444 JSONObject jattrs = obj["attributes"];
445 size_t nelems = jattrs.size();
447 for (size_t i = 0; i < nelems; i++) {
448 JSONObject jattr = jattrs.get(i);
450 DDF attr = jattr.ddf();
451 Attribute *attribute = Attribute::unmarshall(attr);
452 m_attributes.push_back(attribute);
455 m_authenticated = obj["authenticated"].integer();
456 m_initialized = true;
462 gss_eap_shib_attr_provider::init(void)
467 ret = ShibbolethResolver::init();
468 } catch (exception &e) {
472 gss_eap_attr_ctx::registerProvider(ATTR_TYPE_LOCAL, createAttrContext);
478 gss_eap_shib_attr_provider::finalize(void)
480 gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_LOCAL);
481 ShibbolethResolver::term();
485 gss_eap_shib_attr_provider::mapException(OM_uint32 *minor,
486 std::exception &e) const
488 if (typeid(e) == typeid(AttributeException))
489 *minor = GSSEAP_SHIB_ATTR_FAILURE;
490 else if (typeid(e) == typeid(AttributeExtractionException))
491 *minor = GSSEAP_SHIB_ATTR_EXTRACT_FAILURE;
492 else if (typeid(e) == typeid(AttributeFilteringException))
493 *minor = GSSEAP_SHIB_ATTR_FILTER_FAILURE;
494 else if (typeid(e) == typeid(AttributeResolutionException))
495 *minor = GSSEAP_SHIB_ATTR_RESOLVE_FAILURE;
496 else if (typeid(e) == typeid(ConfigurationException))
497 *minor = GSSEAP_SHIB_CONFIG_FAILURE;
498 else if (typeid(e) == typeid(ListenerException))
499 *minor = GSSEAP_SHIB_LISTENER_FAILURE;
501 return GSS_S_CONTINUE_NEEDED;
503 gssEapSaveStatusInfo(*minor, "%s", e.what());
505 return GSS_S_FAILURE;
508 gss_eap_attr_provider *
509 gss_eap_shib_attr_provider::createAttrContext(void)
511 return new gss_eap_shib_attr_provider;
515 gss_eap_shib_attr_provider::duplicateAttribute(const Attribute *src)
517 DDF obj = src->marshall();
518 Attribute *attribute = Attribute::unmarshall(obj);
525 gss_eap_shib_attr_provider::duplicateAttributes(const vector <Attribute *>src)
527 vector <Attribute *> dst;
529 for (vector<Attribute *>::const_iterator a = src.begin();
532 dst.push_back(duplicateAttribute(*a));
538 gssEapLocalAttrProviderInit(OM_uint32 *minor)
540 if (!gss_eap_shib_attr_provider::init()) {
541 *minor = GSSEAP_SHIB_INIT_FAILURE;
542 return GSS_S_FAILURE;
544 return GSS_S_COMPLETE;
548 gssEapLocalAttrProviderFinalize(OM_uint32 *minor)
550 gss_eap_shib_attr_provider::finalize();
553 return GSS_S_COMPLETE;