some cleanup work on marshalling
[mech_eap.git] / util_shib.cpp
1 /*
2  * Copyright (c) 2010, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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
30  * SUCH DAMAGE.
31  */
32 /*
33  * Copyright 2001-2009 Internet2
34  * 
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
38  *
39  *     http://www.apache.org/licenses/LICENSE-2.0
40  *
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.
46  */
47
48 #include "gssapiP_eap.h"
49
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>
60
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>
67
68 #include "resolver.h"
69
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;
77 using namespace std;
78
79 bool
80 gss_eap_shib_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *source,
81                                                     const gss_eap_attr_provider *ctx)
82 {
83     const gss_eap_shib_attr_provider *shib;
84
85     if (!gss_eap_attr_provider::initFromExistingContext(source, ctx))
86         return false;
87
88     shib = dynamic_cast<const gss_eap_shib_attr_provider *>(ctx);
89     if (shib != NULL)
90         m_attributes = duplicateAttributes(shib->getAttributes());
91
92     return true;
93 }
94
95 bool
96 addRadiusAttribute(const gss_eap_attr_provider *provider,
97                    const gss_buffer_t attribute,
98                    void *data)
99 {
100     const gss_eap_shib_attr_provider *shib;
101     const gss_eap_radius_attr_provider *radius;
102     int authenticated, complete, more = -1;
103     vector <string> attributeIds(1);
104     SimpleAttribute *a;
105
106     radius = dynamic_cast<const gss_eap_radius_attr_provider *>(provider);
107     shib = static_cast<const gss_eap_shib_attr_provider *>(data);
108
109     assert(radius != NULL && shib != NULL);
110
111     string attributeName =
112         gss_eap_attr_ctx::composeAttributeName(ATTR_TYPE_RADIUS, attribute);
113
114     attributeIds.push_back(attributeName);
115     a = new SimpleAttribute(attributeIds);
116     if (a == NULL)
117         return false;
118
119     while (more != 0) {
120         gss_buffer_desc value = GSS_C_EMPTY_BUFFER;
121         OM_uint32 minor;
122
123         if (!radius->getAttribute(attribute,
124                                   &authenticated,
125                                   &complete,
126                                   &value,
127                                   NULL,
128                                   &more))
129             return false;
130
131         string attributeValue((char *)value.value, value.length);
132         a->getValues().push_back(attributeValue);
133
134         gss_release_buffer(&minor, &value);
135     }
136
137     shib->getAttributes().push_back(a);
138
139     return true;
140 }
141
142 bool
143 gss_eap_shib_attr_provider::initFromGssContext(const gss_eap_attr_ctx *source,
144                                                const gss_cred_id_t gssCred,
145                                                const gss_ctx_id_t gssCtx)
146 {
147     const gss_eap_saml_assertion_provider *saml;
148     const gss_eap_radius_attr_provider *radius;
149     gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
150     ShibbolethResolver *resolver = NULL;
151     OM_uint32 minor;
152
153     if (!gss_eap_attr_provider::initFromGssContext(source, gssCred, gssCtx))
154         return false;
155
156     saml = dynamic_cast<const gss_eap_saml_assertion_provider *>
157         (source->getProvider(ATTR_TYPE_SAML_ASSERTION));
158     radius = dynamic_cast<const gss_eap_radius_attr_provider *>
159         (source->getProvider(ATTR_TYPE_RADIUS));
160
161     if (gssCred != GSS_C_NO_CREDENTIAL &&
162         gss_display_name(&minor, gssCred->name, &nameBuf, NULL) == GSS_S_COMPLETE)
163         resolver->setApplicationID((const char *)nameBuf.value);
164
165     if (saml != NULL && saml->getAssertion() != NULL)
166         resolver->addToken(saml->getAssertion());
167
168     if (radius != NULL)
169         radius->getAttributeTypes(addRadiusAttribute, (void *)this);
170
171     resolver->resolveAttributes(m_attributes);
172
173     gss_release_buffer(&minor, &nameBuf);
174
175     delete resolver;
176
177     return true;
178 }
179
180 gss_eap_shib_attr_provider::~gss_eap_shib_attr_provider(void)
181 {
182     for_each(m_attributes.begin(),
183              m_attributes.end(),
184              xmltooling::cleanup<Attribute>())
185         ;
186 }
187
188 int
189 gss_eap_shib_attr_provider::getAttributeIndex(const gss_buffer_t attr) const
190 {
191     int i = 0;
192
193     for (vector<Attribute *>::const_iterator a = m_attributes.begin();
194          a != m_attributes.end();
195          ++a)
196     {
197         for (vector<string>::const_iterator s = (*a)->getAliases().begin();
198              s != (*a)->getAliases().end();
199              ++s) {
200             if (attr->length == (*s).length() &&
201                 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
202                 return i;
203             }
204         }
205     }
206
207     return -1;
208 }
209
210 void
211 gss_eap_shib_attr_provider::setAttribute(int complete,
212                                          const gss_buffer_t attr,
213                                          const gss_buffer_t value)
214 {
215     string attrStr((char *)attr->value, attr->length);
216     vector <string> ids(1);
217
218     ids.push_back(attrStr);
219
220     SimpleAttribute *a = new SimpleAttribute(ids);
221
222     if (value->length != 0) {
223         string valueStr((char *)value->value, value->length);
224
225         a->getValues().push_back(valueStr);        
226     }
227
228     m_attributes.push_back(a);
229 }
230
231 void
232 gss_eap_shib_attr_provider::deleteAttribute(const gss_buffer_t attr)
233 {
234     int i;
235
236     i = getAttributeIndex(attr);
237     if (i >= 0)
238         m_attributes.erase(m_attributes.begin() + i);
239 }
240
241 bool
242 gss_eap_shib_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
243                                               void *data) const
244 {
245     for (vector<Attribute*>::const_iterator a = m_attributes.begin();
246         a != m_attributes.end();
247         ++a)
248     {
249         gss_buffer_desc attribute;
250
251         attribute.value = (void *)((*a)->getId());
252         attribute.length = strlen((char *)attribute.value);
253
254         if (!addAttribute(this, &attribute, data))
255             return false;
256     }
257
258     return true;
259 }
260
261 const Attribute *
262 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr) const
263 {
264     const Attribute *ret = NULL;
265
266     for (vector<Attribute *>::const_iterator a = m_attributes.begin();
267          a != m_attributes.end();
268          ++a)
269     {
270         for (vector<string>::const_iterator s = (*a)->getAliases().begin();
271              s != (*a)->getAliases().end();
272              ++s) {
273             if (attr->length == (*s).length() &&
274                 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
275                 ret = *a;
276                 break;
277             }
278         }
279         if (ret != NULL)
280             break;
281     }
282
283     return ret;
284 }
285
286 bool
287 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr,
288                                          int *authenticated,
289                                          int *complete,
290                                          gss_buffer_t value,
291                                          gss_buffer_t display_value,
292                                          int *more) const
293 {
294     const Attribute *shibAttr = NULL;
295     gss_buffer_desc buf;
296
297     shibAttr = getAttribute(attr);
298     if (shibAttr == NULL)
299         return false;
300
301     if (*more == -1) {
302         *more = 0;
303     } else if (*more >= (int)shibAttr->valueCount()) {
304         *more = 0;
305         return true;
306     }
307
308     buf.value = (void *)shibAttr->getString(*more);
309     buf.length = strlen((char *)buf.value);
310
311     duplicateBuffer(buf, value);
312  
313     *authenticated = TRUE;
314     *complete = FALSE;
315
316     return true;
317 }
318
319 gss_any_t
320 gss_eap_shib_attr_provider::mapToAny(int authenticated,
321                                      gss_buffer_t type_id) const
322 {
323     gss_any_t output;
324
325     vector <Attribute *>v = duplicateAttributes(m_attributes);
326
327     output = (gss_any_t)new vector <Attribute *>(v);
328
329     return output;
330 }
331
332 void
333 gss_eap_shib_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
334                                                   gss_any_t input) const
335 {
336     vector <Attribute *> *v = ((vector <Attribute *> *)input);
337     delete v;
338 }
339
340 void
341 gss_eap_shib_attr_provider::exportToBuffer(gss_buffer_t buffer) const
342 {
343     buffer->length = 0;
344     buffer->value = NULL;
345 }
346
347 bool
348 gss_eap_shib_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
349                                            const gss_buffer_t buffer)
350 {
351     return true;
352 }
353
354 bool
355 gss_eap_shib_attr_provider::init(void)
356 {
357     return ShibbolethResolver::init();
358 }
359
360 void
361 gss_eap_shib_attr_provider::finalize(void)
362 {
363     ShibbolethResolver::term();
364 }
365
366 gss_eap_attr_provider *
367 gss_eap_shib_attr_provider::createAttrContext(void)
368 {
369     return new gss_eap_shib_attr_provider;
370 }
371
372 Attribute *
373 gss_eap_shib_attr_provider::duplicateAttribute(const Attribute *src)
374 {
375     Attribute *attribute;
376
377     DDF obj = src->marshall();
378     attribute = Attribute::unmarshall(obj);
379     obj.destroy();
380
381     return attribute;
382 }
383
384 vector <Attribute *>
385 gss_eap_shib_attr_provider::duplicateAttributes(const vector <Attribute *>src)
386 {
387     vector <Attribute *> dst;
388
389     for (vector<Attribute *>::const_iterator a = src.begin();
390          a != src.end();
391          ++a)
392         dst.push_back(duplicateAttribute(*a));
393
394     return dst;
395 }