35a53819751195782de4b0e7cf396eb0231fdb08
[cyrus-sasl.git] / mech_eap / 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 static vector <Attribute *>
80 duplicateAttributes(const vector <Attribute *>src);
81
82 bool
83 gss_eap_shib_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *source,
84                                                     const gss_eap_attr_provider *ctx)
85 {
86     const gss_eap_shib_attr_provider *shib;
87
88     if (!gss_eap_attr_provider::initFromExistingContext(source, ctx))
89         return false;
90
91     shib = dynamic_cast<const gss_eap_shib_attr_provider *>(ctx);
92     if (shib != NULL)
93         m_attributes = duplicateAttributes(shib->m_attributes);
94
95     return true;
96 }
97
98 bool
99 gss_eap_shib_attr_provider::initFromGssContext(const gss_eap_attr_ctx *source,
100                                                const gss_cred_id_t gssCred,
101                                                const gss_ctx_id_t gssCtx)
102 {
103     const gss_eap_saml_assertion_provider *saml;
104     const gss_eap_radius_attr_provider *radius;
105     gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
106     ShibbolethResolver *resolver = NULL;
107     OM_uint32 minor;
108
109     if (!gss_eap_attr_provider::initFromGssContext(source, gssCred, gssCtx))
110         return false;
111
112     saml = dynamic_cast<const gss_eap_saml_assertion_provider *>
113         (source->getProvider(ATTR_TYPE_SAML_ASSERTION));
114     radius = dynamic_cast<const gss_eap_radius_attr_provider *>
115         (source->getProvider(ATTR_TYPE_RADIUS));
116
117     if (radius == NULL)
118         return true;
119
120     if (gssCred != GSS_C_NO_CREDENTIAL &&
121         gss_display_name(&minor, gssCred->name, &nameBuf, NULL) == GSS_S_COMPLETE)
122         resolver->setApplicationID((const char *)nameBuf.value);
123
124     if (saml != NULL && saml->getAssertion() != NULL)
125         resolver->addToken(saml->getAssertion());
126
127     /* TODO inject RADIUS attribute types */
128
129     resolver->resolveAttributes(m_attributes);
130
131     gss_release_buffer(&minor, &nameBuf);
132
133     delete resolver;
134
135     return true;
136 }
137
138 gss_eap_shib_attr_provider::~gss_eap_shib_attr_provider(void)
139 {
140     for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
141 }
142
143 int
144 gss_eap_shib_attr_provider::getAttributeIndex(const gss_buffer_t attr) const
145 {
146     int i = 0;
147
148     for (vector<Attribute *>::const_iterator a = m_attributes.begin();
149          a != m_attributes.end();
150          ++a)
151     {
152         for (vector<string>::const_iterator s = (*a)->getAliases().begin();
153              s != (*a)->getAliases().end();
154              ++s) {
155             if (attr->length == (*s).length() &&
156                 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
157                 return i;
158             }
159         }
160     }
161
162     return -1;
163 }
164
165 void
166 gss_eap_shib_attr_provider::setAttribute(int complete,
167                                          const gss_buffer_t attr,
168                                          const gss_buffer_t value)
169 {
170     string attrStr((char *)attr->value, attr->length);
171     vector <string> ids(1);
172
173     ids.push_back(attrStr);
174
175     SimpleAttribute *a = new SimpleAttribute(ids);
176
177     if (value->length != 0) {
178         string valStr((char *)value->value, value->length);
179
180         a->getValues().push_back(valStr);        
181     }
182
183     m_attributes.push_back(a);
184 }
185
186 void
187 gss_eap_shib_attr_provider::deleteAttribute(const gss_buffer_t attr)
188 {
189     int i;
190
191     i = getAttributeIndex(attr);
192     if (i >= 0)
193         m_attributes.erase(m_attributes.begin() + i);
194 }
195
196 bool
197 gss_eap_shib_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
198                                               void *data) const
199 {
200     for (vector<Attribute*>::const_iterator a = m_attributes.begin();
201         a != m_attributes.end();
202         ++a)
203     {
204         gss_buffer_desc attribute;
205
206         attribute.value = (void *)((*a)->getId());
207         attribute.length = strlen((char *)attribute.value);
208
209         if (!addAttribute(this, &attribute, data))
210             return false;
211     }
212
213     return true;
214 }
215
216 const Attribute *
217 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr) const
218 {
219     const Attribute *ret = NULL;
220
221     for (vector<Attribute *>::const_iterator a = m_attributes.begin();
222          a != m_attributes.end();
223          ++a)
224     {
225         for (vector<string>::const_iterator s = (*a)->getAliases().begin();
226              s != (*a)->getAliases().end();
227              ++s) {
228             if (attr->length == (*s).length() &&
229                 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
230                 ret = *a;
231                 break;
232             }
233         }
234         if (ret != NULL)
235             break;
236     }
237
238     return ret;
239 }
240
241 bool
242 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr,
243                                          int *authenticated,
244                                          int *complete,
245                                          gss_buffer_t value,
246                                          gss_buffer_t display_value,
247                                          int *more) const
248 {
249     const Attribute *shibAttr = NULL;
250     gss_buffer_desc buf;
251
252     shibAttr = getAttribute(attr);
253     if (shibAttr == NULL)
254         return false;
255
256     if (*more == -1) {
257         *more = 0;
258     } else if (*more >= (int)shibAttr->valueCount()) {
259         *more = 0;
260         return true;
261     }
262
263     buf.value = (void *)shibAttr->getString(*more);
264     buf.length = strlen((char *)buf.value);
265
266     duplicateBuffer(buf, value);
267  
268     *authenticated = TRUE;
269     *complete = FALSE;
270
271     return true;
272 }
273
274 gss_any_t
275 gss_eap_shib_attr_provider::mapToAny(int authenticated,
276                                      gss_buffer_t type_id) const
277 {
278     gss_any_t output;
279
280     vector <Attribute *>v = duplicateAttributes(m_attributes);
281
282     output = (gss_any_t)new vector <Attribute *>(v);
283
284     return output;
285 }
286
287 void
288 gss_eap_shib_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
289                                                   gss_any_t input) const
290 {
291     vector <Attribute *> *v = ((vector <Attribute *> *)input);
292     delete v;
293 }
294
295 void
296 gss_eap_shib_attr_provider::marshall(gss_buffer_t buffer) const
297 {
298 }
299
300 bool
301 gss_eap_shib_attr_provider::unmarshall(const gss_eap_attr_ctx *ctx,
302                                        const gss_buffer_t buffer)
303 {
304     return false;
305 }
306
307 bool
308 gss_eap_shib_attr_provider::init(void)
309 {
310     return ShibbolethResolver::init();
311 }
312
313 void
314 gss_eap_shib_attr_provider::finalize(void)
315 {
316     ShibbolethResolver::term();
317 }
318
319 gss_eap_attr_provider *
320 gss_eap_shib_attr_provider::createAttrContext(void)
321 {
322     return new gss_eap_shib_attr_provider;
323 }
324
325 static Attribute *
326 duplicateAttribute(const Attribute *src)
327 {
328     Attribute *attribute;
329
330     DDF obj = src->marshall();
331     attribute = Attribute::unmarshall(obj);
332     obj.destroy();
333
334     return attribute;
335 }
336
337 static vector <Attribute *>
338 duplicateAttributes(const vector <Attribute *>src)
339 {
340     vector <Attribute *> dst;
341
342     for (vector<Attribute *>::const_iterator a = src.begin();
343          a != src.end();
344          ++a)
345         dst.push_back(duplicateAttribute(*a));
346
347     return dst;
348 }