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);
82 gss_eap_shib_attr_provider::gss_eap_shib_attr_provider(const gss_eap_attr_ctx *ctx,
83 gss_cred_id_t gssCred,
85 : gss_eap_attr_provider(ctx, gssCred, gssCtx)
87 if (gssCtx == GSS_C_NO_CONTEXT) {
88 gss_eap_shib_attr_provider *shib;
90 shib = dynamic_cast<gss_eap_shib_attr_provider *>
91 (ctx->getProvider(ATTR_TYPE_LOCAL));
93 m_attributes = duplicateAttributes(shib->m_attributes);
95 gss_eap_saml_assertion_provider *saml;
96 gss_eap_radius_attr_provider *radius;
97 gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
98 ShibbolethResolver *resolver = NULL;
101 saml = dynamic_cast<gss_eap_saml_assertion_provider *>
102 (ctx->getProvider(ATTR_TYPE_SAML_ASSERTION));
103 radius = dynamic_cast<gss_eap_radius_attr_provider *>
104 (ctx->getProvider(ATTR_TYPE_RADIUS));
109 if (gssCred != GSS_C_NO_CREDENTIAL &&
110 gss_display_name(&minor, gssCred->name, &nameBuf, NULL) == GSS_S_COMPLETE)
111 resolver->setApplicationID((const char *)nameBuf.value);
113 if (saml != NULL && saml->getAssertion() != NULL)
114 resolver->addToken(saml->getAssertion());
116 /* TODO inject RADIUS attribute types */
118 resolver->resolveAttributes(m_attributes);
120 gss_release_buffer(&minor, &nameBuf);
126 gss_eap_shib_attr_provider::~gss_eap_shib_attr_provider(void)
128 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
132 gss_eap_shib_attr_provider::getAttributeIndex(const gss_buffer_t attr) const
136 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
137 a != m_attributes.end();
140 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
141 s != (*a)->getAliases().end();
143 if (attr->length == (*s).length() &&
144 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
154 gss_eap_shib_attr_provider::setAttribute(int complete,
155 const gss_buffer_t attr,
156 const gss_buffer_t value)
158 string attrStr((char *)attr->value, attr->length);
159 vector <string> ids(1);
161 ids.push_back(attrStr);
163 SimpleAttribute *a = new SimpleAttribute(ids);
165 if (value->length != 0) {
166 string valStr((char *)value->value, value->length);
168 a->getValues().push_back(valStr);
171 m_attributes.push_back(a);
175 gss_eap_shib_attr_provider::deleteAttribute(const gss_buffer_t attr)
179 i = getAttributeIndex(attr);
181 m_attributes.erase(m_attributes.begin() + i);
185 gss_eap_shib_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
188 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
189 a != m_attributes.end();
192 gss_buffer_desc attribute;
194 attribute.value = (void *)((*a)->getId());
195 attribute.length = strlen((char *)attribute.value);
197 if (!addAttribute(this, &attribute, data))
205 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr) const
207 const Attribute *ret = NULL;
209 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
210 a != m_attributes.end();
213 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
214 s != (*a)->getAliases().end();
216 if (attr->length == (*s).length() &&
217 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
230 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr,
234 gss_buffer_t display_value,
237 const Attribute *shibAttr = NULL;
240 shibAttr = getAttribute(attr);
241 if (shibAttr == NULL)
246 } else if (*more >= (int)shibAttr->valueCount()) {
251 buf.value = (void *)shibAttr->getString(*more);
252 buf.length = strlen((char *)buf.value);
254 duplicateBuffer(buf, value);
256 *authenticated = TRUE;
263 gss_eap_shib_attr_provider::mapToAny(int authenticated,
264 gss_buffer_t type_id) const
268 vector <Attribute *>v = duplicateAttributes(m_attributes);
270 output = (gss_any_t)new vector <Attribute *>(v);
276 gss_eap_shib_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
277 gss_any_t input) const
279 vector <Attribute *> *v = ((vector <Attribute *> *)input);
284 gss_eap_shib_attr_provider::marshall(gss_buffer_t buffer) const
288 gss_eap_attr_provider *
289 gss_eap_shib_attr_provider::unmarshall(const gss_eap_attr_ctx *ctx,
290 const gss_buffer_t buffer)
296 gss_eap_shib_attr_provider::init(void)
298 return ShibbolethResolver::init();
302 gss_eap_shib_attr_provider::finalize(void)
304 ShibbolethResolver::term();
307 gss_eap_attr_provider *
308 gss_eap_shib_attr_provider::createAttrContext(const gss_eap_attr_ctx *ctx,
309 gss_cred_id_t gssCred,
312 return new gss_eap_shib_attr_provider(ctx, gssCred, gssCtx);
316 duplicateAttribute(const Attribute *src)
318 Attribute *attribute;
320 DDF obj = src->marshall();
321 attribute = Attribute::unmarshall(obj);
327 static vector <Attribute *>
328 duplicateAttributes(const vector <Attribute *>src)
330 vector <Attribute *> dst;
332 for (vector<Attribute *>::const_iterator a = src.begin();
335 dst.push_back(duplicateAttribute(*a));