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;
79 static vector <Attribute *>
80 duplicateAttributes(const vector <Attribute *>src);
83 gss_eap_shib_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *source,
84 const gss_eap_attr_provider *ctx)
86 const gss_eap_shib_attr_provider *shib;
88 if (!gss_eap_attr_provider::initFromExistingContext(source, ctx))
91 shib = dynamic_cast<const gss_eap_shib_attr_provider *>(ctx);
93 m_attributes = duplicateAttributes(shib->m_attributes);
99 gss_eap_shib_attr_provider::initFromGssContext(const gss_eap_attr_ctx *source,
100 const gss_cred_id_t gssCred,
101 const gss_ctx_id_t gssCtx)
103 const gss_eap_saml_assertion_provider *saml;
104 const gss_eap_radius_attr_provider *radius;
105 gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
106 ShibbolethResolver *resolver = NULL;
109 if (!gss_eap_attr_provider::initFromGssContext(source, gssCred, gssCtx))
112 saml = dynamic_cast<const gss_eap_saml_assertion_provider *>
113 (source->getProvider(ATTR_TYPE_SAML_ASSERTION));
114 radius = dynamic_cast<const gss_eap_radius_attr_provider *>
115 (source->getProvider(ATTR_TYPE_RADIUS));
120 if (gssCred != GSS_C_NO_CREDENTIAL &&
121 gss_display_name(&minor, gssCred->name, &nameBuf, NULL) == GSS_S_COMPLETE)
122 resolver->setApplicationID((const char *)nameBuf.value);
124 if (saml != NULL && saml->getAssertion() != NULL)
125 resolver->addToken(saml->getAssertion());
127 /* TODO inject RADIUS attribute types */
129 resolver->resolveAttributes(m_attributes);
131 gss_release_buffer(&minor, &nameBuf);
138 gss_eap_shib_attr_provider::~gss_eap_shib_attr_provider(void)
140 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
144 gss_eap_shib_attr_provider::getAttributeIndex(const gss_buffer_t attr) const
148 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
149 a != m_attributes.end();
152 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
153 s != (*a)->getAliases().end();
155 if (attr->length == (*s).length() &&
156 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
166 gss_eap_shib_attr_provider::setAttribute(int complete,
167 const gss_buffer_t attr,
168 const gss_buffer_t value)
170 string attrStr((char *)attr->value, attr->length);
171 vector <string> ids(1);
173 ids.push_back(attrStr);
175 SimpleAttribute *a = new SimpleAttribute(ids);
177 if (value->length != 0) {
178 string valStr((char *)value->value, value->length);
180 a->getValues().push_back(valStr);
183 m_attributes.push_back(a);
187 gss_eap_shib_attr_provider::deleteAttribute(const gss_buffer_t attr)
191 i = getAttributeIndex(attr);
193 m_attributes.erase(m_attributes.begin() + i);
197 gss_eap_shib_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
200 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
201 a != m_attributes.end();
204 gss_buffer_desc attribute;
206 attribute.value = (void *)((*a)->getId());
207 attribute.length = strlen((char *)attribute.value);
209 if (!addAttribute(this, &attribute, data))
217 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr) const
219 const Attribute *ret = NULL;
221 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
222 a != m_attributes.end();
225 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
226 s != (*a)->getAliases().end();
228 if (attr->length == (*s).length() &&
229 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
242 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr,
246 gss_buffer_t display_value,
249 const Attribute *shibAttr = NULL;
252 shibAttr = getAttribute(attr);
253 if (shibAttr == NULL)
258 } else if (*more >= (int)shibAttr->valueCount()) {
263 buf.value = (void *)shibAttr->getString(*more);
264 buf.length = strlen((char *)buf.value);
266 duplicateBuffer(buf, value);
268 *authenticated = TRUE;
275 gss_eap_shib_attr_provider::mapToAny(int authenticated,
276 gss_buffer_t type_id) const
280 vector <Attribute *>v = duplicateAttributes(m_attributes);
282 output = (gss_any_t)new vector <Attribute *>(v);
288 gss_eap_shib_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
289 gss_any_t input) const
291 vector <Attribute *> *v = ((vector <Attribute *> *)input);
296 gss_eap_shib_attr_provider::marshall(gss_buffer_t buffer) const
301 gss_eap_shib_attr_provider::unmarshall(const gss_eap_attr_ctx *ctx,
302 const gss_buffer_t buffer)
308 gss_eap_shib_attr_provider::init(void)
310 return ShibbolethResolver::init();
314 gss_eap_shib_attr_provider::finalize(void)
316 ShibbolethResolver::term();
319 gss_eap_attr_provider *
320 gss_eap_shib_attr_provider::createAttrContext(void)
322 return new gss_eap_shib_attr_provider;
326 duplicateAttribute(const Attribute *src)
328 Attribute *attribute;
330 DDF obj = src->marshall();
331 attribute = Attribute::unmarshall(obj);
337 static vector <Attribute *>
338 duplicateAttributes(const vector <Attribute *>src)
340 vector <Attribute *> dst;
342 for (vector<Attribute *>::const_iterator a = src.begin();
345 dst.push_back(duplicateAttribute(*a));