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