add export_sec_context variant that does not reenter local attribute path
[mech_eap.git] / 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 addRadiusAttribute(const gss_eap_attr_ctx *manager,
107                    const gss_eap_attr_provider *provider,
108                    const gss_buffer_t attribute,
109                    void *data)
110 {
111     const gss_eap_radius_attr_provider *radius;
112     const gss_eap_shib_attr_provider *shib;
113     int authenticated, complete, more = -1;
114     vector <string> attributeIds(1);
115     SimpleAttribute *a;
116
117     radius = static_cast<const gss_eap_radius_attr_provider *>(provider);
118     shib = static_cast<const gss_eap_shib_attr_provider *>(data);
119
120     assert(radius != NULL && shib != NULL);
121
122     string attributeName =
123         manager->composeAttributeName(ATTR_TYPE_RADIUS, attribute);
124
125     attributeIds.push_back(attributeName);
126     a = new SimpleAttribute(attributeIds);
127     if (a == NULL)
128         return false;
129
130     while (more != 0) {
131         gss_buffer_desc value = GSS_C_EMPTY_BUFFER;
132         OM_uint32 minor;
133
134         if (!radius->getAttribute(attribute,
135                                   &authenticated,
136                                   &complete,
137                                   &value,
138                                   NULL,
139                                   &more))
140             return false;
141
142         string attributeValue((char *)value.value, value.length);
143         a->getValues().push_back(attributeValue);
144
145         gss_release_buffer(&minor, &value);
146     }
147
148     shib->getAttributes().push_back(a);
149
150     return true;
151 }
152
153 bool
154 gss_eap_shib_attr_provider::initFromGssContext(const gss_eap_attr_ctx *manager,
155                                                const gss_cred_id_t gssCred,
156                                                const gss_ctx_id_t gssCtx)
157 {
158     const gss_eap_saml_assertion_provider *saml;
159     const gss_eap_radius_attr_provider *radius;
160     gss_buffer_desc exportedCtx = GSS_C_EMPTY_BUFFER;
161     OM_uint32 major, minor;
162
163 #if 0
164     gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
165 #endif
166     if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
167         return false;
168
169     saml = static_cast<const gss_eap_saml_assertion_provider *>
170         (m_manager->getProvider(ATTR_TYPE_SAML_ASSERTION));
171     radius = static_cast<const gss_eap_radius_attr_provider *>
172         (m_manager->getProvider(ATTR_TYPE_RADIUS));
173
174     auto_ptr<ShibbolethResolver> resolver(ShibbolethResolver::create());
175
176     /*
177      * For now, leave ApplicationID defaulted.
178      * Later on, we could allow this via config option to the mechanism
179      * or rely on an SPRequest interface to pass in a URI identifying the
180      * acceptor.
181      */
182 #if 0
183     if (gssCred != GSS_C_NO_CREDENTIAL &&
184         gssEapDisplayName(&minor, gssCred->name, &nameBuf, NULL) == GSS_S_COMPLETE) {
185         resolver->setApplicationID((const char *)nameBuf.value);
186         gss_release_buffer(&minor, &nameBuf);
187     }
188 #endif
189
190     m_authenticated = false;
191
192     major = gssEapExportSecContext(&minor, gssCtx, &exportedCtx,
193                                    EXPORT_CTX_FLAG_DISABLE_LOCAL_ATTRS);
194     if (major == GSS_S_COMPLETE) {
195         gss_release_buffer(&minor, &exportedCtx);
196     }
197
198     if (radius != NULL) {
199         radius->getAttributeTypes(addRadiusAttribute, (void *)this);
200         m_authenticated = radius->authenticated();
201     }
202
203     if (saml != NULL && saml->getAssertion() != NULL) {
204         resolver->addToken(saml->getAssertion());
205         if (m_authenticated)
206             m_authenticated = saml->authenticated();
207     }
208
209     try {
210         resolver->resolve();
211         m_attributes = resolver->getResolvedAttributes();
212         resolver->getResolvedAttributes().clear();
213     } catch (exception &e) {
214 #if 0
215         fprintf(stderr, "%s", e.what());
216 #endif
217     }
218
219     return true;
220 }
221
222 ssize_t
223 gss_eap_shib_attr_provider::getAttributeIndex(const gss_buffer_t attr) const
224 {
225     int i = 0;
226
227     for (vector<Attribute *>::const_iterator a = m_attributes.begin();
228          a != m_attributes.end();
229          ++a)
230     {
231         for (vector<string>::const_iterator s = (*a)->getAliases().begin();
232              s != (*a)->getAliases().end();
233              ++s) {
234             if (attr->length == (*s).length() &&
235                 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
236                 return i;
237             }
238         }
239     }
240
241     return -1;
242 }
243
244 bool
245 gss_eap_shib_attr_provider::setAttribute(int complete GSSEAP_UNUSED,
246                                          const gss_buffer_t attr,
247                                          const gss_buffer_t value)
248 {
249     string attrStr((char *)attr->value, attr->length);
250     vector <string> ids(1, attrStr);
251     SimpleAttribute *a = new SimpleAttribute(ids);
252
253     if (value->length != 0) {
254         string valueStr((char *)value->value, value->length);
255
256         a->getValues().push_back(valueStr);
257     }
258
259     m_attributes.push_back(a);
260     m_authenticated = false;
261
262     return true;
263 }
264
265 bool
266 gss_eap_shib_attr_provider::deleteAttribute(const gss_buffer_t attr)
267 {
268     int i;
269
270     i = getAttributeIndex(attr);
271     if (i >= 0)
272         m_attributes.erase(m_attributes.begin() + i);
273
274     m_authenticated = false;
275
276     return true;
277 }
278
279 bool
280 gss_eap_shib_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
281                                               void *data) const
282 {
283     for (vector<Attribute*>::const_iterator a = m_attributes.begin();
284         a != m_attributes.end();
285         ++a)
286     {
287         gss_buffer_desc attribute;
288
289         attribute.value = (void *)((*a)->getId());
290         attribute.length = strlen((char *)attribute.value);
291
292         if (!addAttribute(m_manager, this, &attribute, data))
293             return false;
294     }
295
296     return true;
297 }
298
299 const Attribute *
300 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr) const
301 {
302     const Attribute *ret = NULL;
303
304     for (vector<Attribute *>::const_iterator a = m_attributes.begin();
305          a != m_attributes.end();
306          ++a)
307     {
308         for (vector<string>::const_iterator s = (*a)->getAliases().begin();
309              s != (*a)->getAliases().end();
310              ++s) {
311             if (attr->length == (*s).length() &&
312                 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
313                 ret = *a;
314                 break;
315             }
316         }
317         if (ret != NULL)
318             break;
319     }
320
321     return ret;
322 }
323
324 bool
325 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr,
326                                          int *authenticated,
327                                          int *complete,
328                                          gss_buffer_t value,
329                                          gss_buffer_t display_value,
330                                          int *more) const
331 {
332     const Attribute *shibAttr = NULL;
333     gss_buffer_desc buf;
334     int nvalues, i = *more;
335
336     *more = 0;
337
338     shibAttr = getAttribute(attr);
339     if (shibAttr == NULL)
340         return false;
341
342     nvalues = shibAttr->valueCount();
343
344     if (i == -1)
345         i = 0;
346     else if (i >= nvalues)
347         return false;
348
349     buf.value = (void *)shibAttr->getSerializedValues()[*more].c_str();
350     buf.length = strlen((char *)buf.value);
351
352     if (buf.length != 0) {
353         if (value != NULL)
354             duplicateBuffer(buf, value);
355
356         if (display_value != NULL)
357             duplicateBuffer(buf, display_value);
358     }
359
360     if (authenticated != NULL)
361         *authenticated = m_authenticated;
362     if (complete != NULL)
363         *complete = false;
364
365     if (nvalues > ++i)
366         *more = i;
367
368     return true;
369 }
370
371 gss_any_t
372 gss_eap_shib_attr_provider::mapToAny(int authenticated,
373                                      gss_buffer_t type_id GSSEAP_UNUSED) const
374 {
375     gss_any_t output;
376
377     if (authenticated && !m_authenticated)
378         return (gss_any_t)NULL;
379
380     vector <Attribute *>v = duplicateAttributes(m_attributes);
381
382     output = (gss_any_t)new vector <Attribute *>(v);
383
384     return output;
385 }
386
387 void
388 gss_eap_shib_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id GSSEAP_UNUSED,
389                                                   gss_any_t input) const
390 {
391     vector <Attribute *> *v = ((vector <Attribute *> *)input);
392     delete v;
393 }
394
395 const char *
396 gss_eap_shib_attr_provider::prefix(void) const
397 {
398     return NULL;
399 }
400
401 const char *
402 gss_eap_shib_attr_provider::name(void) const
403 {
404     return "local";
405 }
406
407 JSONObject
408 gss_eap_shib_attr_provider::jsonRepresentation(void) const
409 {
410     JSONObject obj;
411
412     obj.set("authenticated", m_authenticated);
413
414     JSONObject attrs = JSONObject::array();
415
416     for (vector<Attribute*>::const_iterator a = m_attributes.begin();
417          a != m_attributes.end(); ++a) {
418         DDF attr = (*a)->marshall();
419         JSONObject jobj(attr);
420         attrs.append(jobj);
421     }
422
423     obj.set("attributes", attrs);
424
425     return obj;
426 }
427
428 bool
429 gss_eap_shib_attr_provider::initWithJsonObject(const gss_eap_attr_ctx *ctx,
430                                                JSONObject &obj)
431 {
432     if (!gss_eap_attr_provider::initWithJsonObject(ctx, obj))
433         return false;
434
435     assert(m_authenticated == false);
436     assert(m_attributes.size() == 0);
437
438     m_authenticated = obj["authenticated"].integer();
439
440     JSONObject attrs = obj["attributes"];
441     size_t nelems = attrs.size();
442
443     for (size_t i = 0; i < nelems; i++) {
444         DDF attr = attrs.get(i).ddf();
445         Attribute *attribute = Attribute::unmarshall(attr);
446         m_attributes.push_back(attribute);
447     }
448
449     return true;
450 }
451
452 bool
453 gss_eap_shib_attr_provider::init(void)
454 {
455     if (!ShibbolethResolver::init())
456         return false;
457
458     gss_eap_attr_ctx::registerProvider(ATTR_TYPE_LOCAL, createAttrContext);
459
460     return true;
461 }
462
463 void
464 gss_eap_shib_attr_provider::finalize(void)
465 {
466     gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_LOCAL);
467     ShibbolethResolver::term();
468 }
469
470 OM_uint32
471 gss_eap_shib_attr_provider::mapException(OM_uint32 *minor,
472                                          std::exception &e) const
473 {
474     if (typeid(e) == typeid(AttributeException))
475         *minor = GSSEAP_SHIB_ATTR_FAILURE;
476     else if (typeid(e) == typeid(AttributeExtractionException))
477         *minor = GSSEAP_SHIB_ATTR_EXTRACT_FAILURE;
478     else if (typeid(e) == typeid(AttributeFilteringException))
479         *minor = GSSEAP_SHIB_ATTR_FILTER_FAILURE;
480     else if (typeid(e) == typeid(AttributeResolutionException))
481         *minor = GSSEAP_SHIB_ATTR_RESOLVE_FAILURE;
482     else if (typeid(e) == typeid(ConfigurationException))
483         *minor = GSSEAP_SHIB_CONFIG_FAILURE;
484     else if (typeid(e) == typeid(ListenerException))
485         *minor = GSSEAP_SHIB_LISTENER_FAILURE;
486     else
487         return GSS_S_CONTINUE_NEEDED;
488
489     return GSS_S_FAILURE;
490 }
491
492 gss_eap_attr_provider *
493 gss_eap_shib_attr_provider::createAttrContext(void)
494 {
495     return new gss_eap_shib_attr_provider;
496 }
497
498 Attribute *
499 gss_eap_shib_attr_provider::duplicateAttribute(const Attribute *src)
500 {
501     DDF obj = src->marshall();
502     Attribute *attribute = Attribute::unmarshall(obj);
503     obj.destroy();
504
505     return attribute;
506 }
507
508 vector <Attribute *>
509 gss_eap_shib_attr_provider::duplicateAttributes(const vector <Attribute *>src)
510 {
511     vector <Attribute *> dst;
512
513     for (vector<Attribute *>::const_iterator a = src.begin();
514          a != src.end();
515          ++a)
516         dst.push_back(duplicateAttribute(*a));
517
518     return dst;
519 }
520
521 OM_uint32
522 gssEapLocalAttrProviderInit(OM_uint32 *minor)
523 {
524     if (!gss_eap_shib_attr_provider::init()) {
525         *minor = GSSEAP_SHIB_INIT_FAILURE;
526         return GSS_S_FAILURE;
527     }
528     return GSS_S_COMPLETE;
529 }
530
531 OM_uint32
532 gssEapLocalAttrProviderFinalize(OM_uint32 *minor)
533 {
534     gss_eap_shib_attr_provider::finalize();
535
536     *minor = 0;
537     return GSS_S_COMPLETE;
538 }