6b5bd74f6bdc29cb0ad50ce8385272b4036517ab
[mech_eap.orig] / 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 gss_eap_shib_attr_provider::gss_eap_shib_attr_provider(void)
80 {
81     m_authenticated = false;
82 }
83
84 gss_eap_shib_attr_provider::~gss_eap_shib_attr_provider(void)
85 {
86     for_each(m_attributes.begin(),
87              m_attributes.end(),
88              xmltooling::cleanup<Attribute>())
89         ;
90 }
91
92 bool
93 gss_eap_shib_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *manager,
94                                                     const gss_eap_attr_provider *ctx)
95 {
96     const gss_eap_shib_attr_provider *shib;
97
98     if (!gss_eap_attr_provider::initFromExistingContext(manager, ctx)) {
99         return false;
100     }
101
102     m_authenticated = false;
103
104     shib = static_cast<const gss_eap_shib_attr_provider *>(ctx);
105     if (shib != NULL) {
106         m_attributes = duplicateAttributes(shib->getAttributes());
107         m_authenticated = shib->authenticated();
108     }
109
110     return true;
111 }
112
113 bool
114 addRadiusAttribute(const gss_eap_attr_provider *provider,
115                    const gss_buffer_t attribute,
116                    void *data)
117 {
118     const gss_eap_shib_attr_provider *shib;
119     const gss_eap_radius_attr_provider *radius;
120     int authenticated, complete, more = -1;
121     vector <string> attributeIds(1);
122     SimpleAttribute *a;
123
124     radius = static_cast<const gss_eap_radius_attr_provider *>(provider);
125     shib = static_cast<const gss_eap_shib_attr_provider *>(data);
126
127     assert(radius != NULL && shib != NULL);
128
129     string attributeName =
130         gss_eap_attr_ctx::composeAttributeName(ATTR_TYPE_RADIUS, attribute);
131
132     attributeIds.push_back(attributeName);
133     a = new SimpleAttribute(attributeIds);
134     if (a == NULL)
135         return false;
136
137     while (more != 0) {
138         gss_buffer_desc value = GSS_C_EMPTY_BUFFER;
139         OM_uint32 minor;
140
141         if (!radius->getAttribute(attribute,
142                                   &authenticated,
143                                   &complete,
144                                   &value,
145                                   NULL,
146                                   &more))
147             return false;
148
149         string attributeValue((char *)value.value, value.length);
150         a->getValues().push_back(attributeValue);
151
152         gss_release_buffer(&minor, &value);
153     }
154
155     shib->getAttributes().push_back(a);
156
157     return true;
158 }
159
160 bool
161 gss_eap_shib_attr_provider::initFromGssContext(const gss_eap_attr_ctx *manager,
162                                                const gss_cred_id_t gssCred,
163                                                const gss_ctx_id_t gssCtx)
164 {
165     const gss_eap_saml_assertion_provider *saml;
166     const gss_eap_radius_attr_provider *radius;
167     gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
168     ShibbolethResolver *resolver = NULL;
169     OM_uint32 minor;
170
171     if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
172         return false;
173
174     saml = static_cast<const gss_eap_saml_assertion_provider *>
175         (manager->getProvider(ATTR_TYPE_SAML_ASSERTION));
176     radius = static_cast<const gss_eap_radius_attr_provider *>
177         (manager->getProvider(ATTR_TYPE_RADIUS));
178
179     if (gssCred != GSS_C_NO_CREDENTIAL &&
180         gss_display_name(&minor, gssCred->name, &nameBuf, NULL) == GSS_S_COMPLETE)
181         resolver->setApplicationID((const char *)nameBuf.value);
182
183     m_authenticated = false;
184
185     if (radius != NULL) {
186         radius->getAttributeTypes(addRadiusAttribute, (void *)this);
187         m_authenticated = radius->authenticated();
188     }
189
190     if (saml != NULL && saml->getAssertion() != NULL) {
191         resolver->addToken(saml->getAssertion());
192         if (m_authenticated)
193             m_authenticated = saml->authenticated();
194     }
195
196     resolver->resolveAttributes(m_attributes);
197
198     gss_release_buffer(&minor, &nameBuf);
199
200     delete resolver;
201
202 #ifdef GSSEAP_DEBUG
203     gss_buffer_desc testattr = {
204         sizeof("urn:greet:greeting") - 1, (void *)"urn:greet:greeting" };
205     gss_buffer_desc testval =
206         { sizeof("Hello, GSS EAP.") - 1, (void *)"Hello, GSS EAP." };
207     setAttribute(true, &testattr, &testval);
208 #endif /* GSSEAP_DEBUG */
209
210     return true;
211 }
212
213 int
214 gss_eap_shib_attr_provider::getAttributeIndex(const gss_buffer_t attr) const
215 {
216     int i = 0;
217
218     for (vector<Attribute *>::const_iterator a = m_attributes.begin();
219          a != m_attributes.end();
220          ++a)
221     {
222         for (vector<string>::const_iterator s = (*a)->getAliases().begin();
223              s != (*a)->getAliases().end();
224              ++s) {
225             if (attr->length == (*s).length() &&
226                 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
227                 return i;
228             }
229         }
230     }
231
232     return -1;
233 }
234
235 void
236 gss_eap_shib_attr_provider::setAttribute(int complete,
237                                          const gss_buffer_t attr,
238                                          const gss_buffer_t value)
239 {
240     string attrStr((char *)attr->value, attr->length);
241     vector <string> ids(1, attrStr);
242     SimpleAttribute *a = new SimpleAttribute(ids);
243
244     if (value->length != 0) {
245         string valueStr((char *)value->value, value->length);
246
247         a->getValues().push_back(valueStr);
248     }
249
250     m_attributes.push_back(a);
251     m_authenticated = false;
252 }
253
254 void
255 gss_eap_shib_attr_provider::deleteAttribute(const gss_buffer_t attr)
256 {
257     int i;
258
259     i = getAttributeIndex(attr);
260     if (i >= 0)
261         m_attributes.erase(m_attributes.begin() + i);
262
263     m_authenticated = false;
264 }
265
266 bool
267 gss_eap_shib_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
268                                               void *data) const
269 {
270     for (vector<Attribute*>::const_iterator a = m_attributes.begin();
271         a != m_attributes.end();
272         ++a)
273     {
274         gss_buffer_desc attribute;
275
276         attribute.value = (void *)((*a)->getId());
277         attribute.length = strlen((char *)attribute.value);
278
279         if (!addAttribute(this, &attribute, data))
280             return false;
281     }
282
283     return true;
284 }
285
286 const Attribute *
287 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr) const
288 {
289     const Attribute *ret = NULL;
290
291     for (vector<Attribute *>::const_iterator a = m_attributes.begin();
292          a != m_attributes.end();
293          ++a)
294     {
295         for (vector<string>::const_iterator s = (*a)->getAliases().begin();
296              s != (*a)->getAliases().end();
297              ++s) {
298             if (attr->length == (*s).length() &&
299                 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
300                 ret = *a;
301                 break;
302             }
303         }
304         if (ret != NULL)
305             break;
306     }
307
308     return ret;
309 }
310
311 bool
312 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr,
313                                          int *authenticated,
314                                          int *complete,
315                                          gss_buffer_t value,
316                                          gss_buffer_t display_value,
317                                          int *more) const
318 {
319     const Attribute *shibAttr = NULL;
320     gss_buffer_desc buf;
321     int nvalues, i = *more;
322
323     *more = 0;
324
325     shibAttr = getAttribute(attr);
326     if (shibAttr == NULL)
327         return false;
328
329     nvalues = shibAttr->valueCount();
330
331     if (i == -1)
332         i = 0;
333     else if (i >= nvalues)
334         return false;
335
336     buf.value = (void *)shibAttr->getString(*more);
337     buf.length = strlen((char *)buf.value);
338
339     if (buf.length != 0) {
340         if (value != NULL)
341             duplicateBuffer(buf, value);
342
343         if (display_value != NULL)
344             duplicateBuffer(buf, display_value);
345     }
346
347     if (authenticated != NULL)
348         *authenticated = m_authenticated;
349     if (complete != NULL)
350         *complete = false;
351
352     if (nvalues > ++i)
353         *more = i;
354
355     return true;
356 }
357
358 gss_any_t
359 gss_eap_shib_attr_provider::mapToAny(int authenticated,
360                                      gss_buffer_t type_id) const
361 {
362     gss_any_t output;
363
364     vector <Attribute *>v = duplicateAttributes(m_attributes);
365
366     output = (gss_any_t)new vector <Attribute *>(v);
367
368     return output;
369 }
370
371 void
372 gss_eap_shib_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
373                                                   gss_any_t input) const
374 {
375     vector <Attribute *> *v = ((vector <Attribute *> *)input);
376     delete v;
377 }
378
379 void
380 gss_eap_shib_attr_provider::exportToBuffer(gss_buffer_t buffer) const
381 {
382     DDF obj(NULL);
383     DDF attrs(NULL);
384
385     buffer->length = 0;
386     buffer->value = NULL;
387
388     obj.addmember("version").integer(1);
389     obj.addmember("authenticated").integer(m_authenticated);
390
391     attrs = obj.addmember("attributes").list();
392     for (vector<Attribute*>::const_iterator a = m_attributes.begin();
393          a != m_attributes.end(); ++a) {
394         DDF attr = (*a)->marshall();
395         attrs.add(attr);
396     }
397
398     ostringstream sink;
399     sink << attrs;
400     string str = sink.str();
401
402     duplicateBuffer(str, buffer);
403
404     attrs.destroy();
405 }
406
407 bool
408 gss_eap_shib_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
409                                            const gss_buffer_t buffer)
410 {
411     if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer))
412         return false;
413
414     if (buffer->length == 0)
415         return true;
416
417     assert(m_authenticated == false);
418     assert(m_attributes.size() == 0);
419
420     DDF obj(NULL);
421     string str((const char *)buffer->value, buffer->length);
422     istringstream source(str);
423
424     source >> obj;
425
426     if (obj["version"].integer() != 1)
427         return false;
428
429     m_authenticated = (obj["authenticated"].integer() != 0);
430
431     DDF attrs = obj["attributes"];
432     DDF attr = attrs.first();
433     while (!attr.isnull()) {
434         Attribute *attribute = Attribute::unmarshall(attr);
435         m_attributes.push_back(attribute);
436         attr = attrs.next();
437     }
438
439     attrs.destroy();
440
441     return true;
442 }
443
444 bool
445 gss_eap_shib_attr_provider::init(void)
446 {
447 #if 1
448     SPConfig& conf=SPConfig::getConfig();
449     conf.setFeatures(
450         SPConfig::Metadata |
451         SPConfig::Trust |
452         SPConfig::AttributeResolution |
453         SPConfig::Credentials |
454         SPConfig::OutOfProcess
455         );
456     if (!conf.init())
457         return false;
458     if (!conf.instantiate()) {
459         conf.term();
460         return false;
461     }
462 #else
463     if (!ShibbolethResolver::init())
464         return false;
465 #endif
466
467     gss_eap_attr_ctx::registerProvider(ATTR_TYPE_LOCAL,
468                                        NULL,
469                                        gss_eap_shib_attr_provider::createAttrContext);
470
471     return true;
472 }
473
474 void
475 gss_eap_shib_attr_provider::finalize(void)
476 {
477     gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_LOCAL);
478     ShibbolethResolver::term();
479 }
480
481 gss_eap_attr_provider *
482 gss_eap_shib_attr_provider::createAttrContext(void)
483 {
484     return new gss_eap_shib_attr_provider;
485 }
486
487 Attribute *
488 gss_eap_shib_attr_provider::duplicateAttribute(const Attribute *src)
489 {
490     DDF obj = src->marshall();
491     Attribute *attribute = Attribute::unmarshall(obj);
492     obj.destroy();
493
494     return attribute;
495 }
496
497 vector <Attribute *>
498 gss_eap_shib_attr_provider::duplicateAttributes(const vector <Attribute *>src)
499 {
500     vector <Attribute *> dst;
501
502     for (vector<Attribute *>::const_iterator a = src.begin();
503          a != src.end();
504          ++a)
505         dst.push_back(duplicateAttribute(*a));
506
507     return dst;
508 }
509
510 OM_uint32
511 gssEapLocalAttrProviderInit(OM_uint32 *minor)
512 {
513     return gss_eap_shib_attr_provider::init()
514         ? GSS_S_COMPLETE : GSS_S_FAILURE;
515 }
516
517 OM_uint32
518 gssEapLocalAttrProviderFinalize(OM_uint32 *minor)
519 {
520     gss_eap_shib_attr_provider::finalize();
521     return GSS_S_COMPLETE;
522 }