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