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