cleanup
[mech_eap.git] / util_saml.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 #include "gssapiP_eap.h"
34
35 #include <sstream>
36
37 #include <xercesc/util/XMLUniDefs.hpp>
38 #include <xmltooling/XMLToolingConfig.h> 
39 #include <xmltooling/util/XMLHelper.h>
40
41 #include <saml/saml1/core/Assertions.h> 
42 #include <saml/saml2/core/Assertions.h>
43 #include <saml/saml2/metadata/Metadata.h>
44
45 using namespace xmltooling;
46 using namespace opensaml::saml2md;
47 using namespace opensaml;
48 using namespace xercesc;
49 using namespace std;
50
51 class auto_ptr_gss_buffer {
52     MAKE_NONCOPYABLE(auto_ptr_gss_buffer);
53     public:
54         auto_ptr_gss_buffer() : m_buf(NULL) {
55         }
56         auto_ptr_gss_buffer(const gss_buffer_t src) {
57             m_buf = new XMLCh[src->length + 1];
58             XMLString::transcode((const char *)src->value, m_buf, src->length);
59         }
60         ~auto_ptr_gss_buffer() {
61             xercesc::XMLString::release(&m_buf);
62         }
63         const XMLCh* get() const {
64             return m_buf;
65         }
66         XMLCh* release() {
67             XMLCh *temp = m_buf; m_buf = NULL; return temp;
68         }
69     private:
70         XMLCh *m_buf;
71 };
72
73 /*
74  * gss_eap_saml_assertion_provider is for retrieving the underlying
75  * assertion.
76  */
77 gss_eap_saml_assertion_provider::gss_eap_saml_assertion_provider(const gss_eap_attr_ctx *
78 ctx)
79     : gss_eap_attr_provider(ctx)
80 {
81     /* Then we may be creating from an existing attribute context */
82     gss_eap_saml_assertion_provider *saml;
83
84     saml = dynamic_cast<gss_eap_saml_assertion_provider *>
85         (ctx->getProvider(ATTR_TYPE_SAML_ASSERTION));
86     if (saml != NULL)
87         setAssertion(saml->getAssertion());
88 }
89
90 gss_eap_saml_assertion_provider::gss_eap_saml_assertion_provider(const gss_eap_attr_ctx *ctx,
91                                                                  gss_cred_id_t gssCred,
92                                                                  gss_ctx_id_t gssCtx)
93     : gss_eap_attr_provider(ctx)
94 {
95     gss_eap_radius_attr_provider *radius;
96     gss_buffer_desc value = GSS_C_EMPTY_BUFFER;
97     int authenticated, complete, more = -1;
98     OM_uint32 minor;
99
100     radius = dynamic_cast<gss_eap_radius_attr_provider *>
101         (ctx->getProvider(ATTR_TYPE_RADIUS));
102     if (radius != NULL &&
103         radius->getAttribute(512, &authenticated, &complete,
104                              &value, NULL, &more)) {
105         m_assertion = parseAssertion(&value);
106         gss_release_buffer(&minor, &value);
107     }
108 }
109
110 gss_eap_saml_assertion_provider::~gss_eap_saml_assertion_provider(void)
111 {
112     delete m_assertion;
113 }
114
115 void
116 gss_eap_saml_assertion_provider::setAssertion(const saml2::Assertion *assertion)
117 {
118
119     delete m_assertion;
120     m_assertion = dynamic_cast<saml2::Assertion*>(assertion->clone());
121 }
122
123 saml2::Assertion *
124 gss_eap_saml_assertion_provider::parseAssertion(const gss_buffer_t buffer)
125 {
126     string str((char *)buffer->value, buffer->length);
127     istringstream istream(str);
128     DOMDocument *doc;
129     const XMLObjectBuilder *b;
130
131     doc = XMLToolingConfig::getConfig().getParser().parse(istream);
132     b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
133
134     return dynamic_cast<saml2::Assertion *>(b->buildFromDocument(doc));
135 }
136
137 bool
138 gss_eap_saml_assertion_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute, void *data) const
139 {
140     return addAttribute(this, GSS_C_NO_BUFFER, data);
141 }
142
143 void
144 gss_eap_saml_assertion_provider::setAttribute(int complete,
145                                               const gss_buffer_t attr,
146                                               const gss_buffer_t value)
147 {
148     saml2::Assertion *assertion = parseAssertion(value);
149
150     m_assertion = assertion;
151 }
152
153 void
154 gss_eap_saml_assertion_provider::deleteAttribute(const gss_buffer_t value)
155 {
156     delete m_assertion;
157     m_assertion = NULL;
158 }
159
160 bool
161 gss_eap_saml_assertion_provider::getAttribute(const gss_buffer_t attr,
162                                               int *authenticated,
163                                               int *complete,
164                                               gss_buffer_t value,
165                                               gss_buffer_t display_value,
166                                               int *more) const
167 {
168     string str;
169
170     if (attr->length != 0 || m_assertion == NULL)
171         return false;
172
173     if (*more == -1)
174         *more = 0;
175
176     if (*more == 0) {
177         *authenticated = true;
178         *complete = false;
179
180         XMLHelper::serialize(m_assertion->marshall((DOMDocument *)NULL), str);
181
182         duplicateBuffer(str, value);
183     }
184
185     return true;
186 }
187
188 gss_any_t
189 gss_eap_saml_assertion_provider::mapToAny(int authenticated,
190                                           gss_buffer_t type_id) const
191 {
192     return (gss_any_t)m_assertion;
193 }
194
195 void
196 gss_eap_saml_assertion_provider::releaseAnyNameMapping(gss_buffer_t type_id,
197                                                        gss_any_t input) const
198 {
199     delete ((saml2::Assertion *)input);
200 }
201
202 void
203 gss_eap_saml_assertion_provider::marshall(gss_buffer_t buffer) const
204 {
205     ostringstream sink;
206     string str;
207
208     buffer->length = 0;
209     buffer->value = NULL;
210
211     if (m_assertion == NULL)
212         return;
213
214     sink << *m_assertion;
215     str = sink.str();
216
217     duplicateBuffer(str, buffer);
218 }
219
220 bool
221 gss_eap_saml_assertion_provider::unmarshall(const gss_eap_attr_ctx *ctx,
222                                             const gss_buffer_t buffer)
223 {
224     assert(m_assertion == NULL);
225
226     m_assertion = parseAssertion(buffer);
227
228     return (m_assertion != NULL);
229 }
230
231 bool
232 gss_eap_saml_assertion_provider::init(void)
233 {
234     return true;
235 }
236
237 void
238 gss_eap_saml_assertion_provider::finalize(void)
239 {
240 }
241
242 gss_eap_attr_provider *
243 gss_eap_saml_assertion_provider::createAttrContext(const gss_eap_attr_ctx *ctx,
244                                                    gss_cred_id_t gssCred,
245                                                    gss_ctx_id_t gssCtx)
246 {
247     return new gss_eap_saml_assertion_provider(ctx, gssCred, gssCtx);
248 }
249
250 /*
251  * gss_eap_saml_attr_provider is for retrieving the underlying attributes.
252  */
253 const saml2::Assertion *
254 gss_eap_saml_attr_provider::getAssertion(void) const
255 {
256     gss_eap_saml_assertion_provider *saml;
257     
258     saml = dynamic_cast<gss_eap_saml_assertion_provider *>(m_source->getProvider(ATTR_TYPE_SAML_ASSERTION));
259     assert(saml != NULL);
260
261     return saml->getAssertion();
262 }
263
264 gss_eap_saml_attr_provider::gss_eap_saml_attr_provider(const gss_eap_attr_ctx *ctx,
265                                                        gss_cred_id_t gssCred,
266                                                        gss_ctx_id_t gssCtx)
267     : gss_eap_attr_provider(ctx, gssCred, gssCtx)
268 {
269     /* Nothing to do, we're just a wrapper around the assertion provider. */
270 }
271
272 gss_eap_saml_attr_provider::~gss_eap_saml_attr_provider(void)
273 {
274     /* Nothing to do, we're just a wrapper around the assertion provider. */
275 }
276
277 bool
278 gss_eap_saml_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
279                                               void *data) const
280 {
281     const saml2::Assertion *assertion = getAssertion();
282
283     if (assertion == NULL)
284         return true;
285
286     const vector<saml2::Attribute*>& attrs2 =
287         const_cast<const saml2::AttributeStatement*>(assertion->getAttributeStatements().front())->getAttributes();
288     for (vector<saml2::Attribute*>::const_iterator a = attrs2.begin();
289         a != attrs2.end();
290         ++a)
291     {
292         gss_buffer_desc attribute;
293
294         attribute.value = (void *)toUTF8((*a)->getName(), true);
295         attribute.length = strlen((char *)attribute.value);
296
297         if (!addAttribute(this, &attribute, data))
298             return false;
299
300         delete (char *)attribute.value;
301     }
302
303     return true;
304 }
305
306 void
307 gss_eap_saml_attr_provider::setAttribute(int complete,
308                                          const gss_buffer_t attr,
309                                          const gss_buffer_t value)
310 {
311 }
312
313 void
314 gss_eap_saml_attr_provider::deleteAttribute(const gss_buffer_t value)
315 {
316 }
317
318 const saml2::Attribute *
319 gss_eap_saml_attr_provider::getAttribute(const gss_buffer_t attr) const
320 {
321     const saml2::Assertion *assertion = getAssertion();
322     saml2::AttributeStatement *statement;
323
324     if (assertion == NULL)
325         return NULL;
326
327     if (assertion->getAttributeStatements().size() == 0)
328         return NULL;
329
330     statement = assertion->getAttributeStatements().front();
331
332     auto_ptr_gss_buffer attrname(attr);
333
334     const vector<saml2::Attribute*>& attrs2 =
335         const_cast<const saml2::AttributeStatement*>(statement)->getAttributes();
336
337     for (vector<saml2::Attribute*>::const_iterator a = attrs2.begin();
338         a != attrs2.end();
339         ++a) {
340         if (XMLString::equals((*a)->getName(), attrname.get()))
341             return *a;
342     }
343
344     return NULL;
345 }
346
347 bool
348 gss_eap_saml_attr_provider::getAttribute(const gss_buffer_t attr,
349                                          int *authenticated,
350                                          int *complete,
351                                          gss_buffer_t value,
352                                          gss_buffer_t display_value,
353                                          int *more) const
354 {
355     const saml2::Attribute *a;
356     const saml2::AttributeValue *av;
357     int nvalues, i = *more;
358
359     *more = 0;
360
361     a = getAttribute(attr);
362     if (a == NULL)
363         return false;
364
365     nvalues = a->getAttributeValues().size();
366
367     if (i == -1)
368         i = 0;
369     else if (i >= nvalues)
370         return false;
371     av = dynamic_cast<const saml2::AttributeValue *>(a->getAttributeValues().at(i)
372 );
373     if (av == NULL)
374         return false;
375
376     *authenticated = TRUE;
377     *complete = FALSE;
378
379     value->value = toUTF8(av->getTextContent(), true);
380     value->length = strlen((char *)value->value);
381
382     if (nvalues > ++i)
383         *more = i;
384
385     return true;
386 }
387
388 gss_any_t
389 gss_eap_saml_attr_provider::mapToAny(int authenticated,
390                                           gss_buffer_t type_id) const
391 {
392     return (gss_any_t)0;
393 }
394
395 void
396 gss_eap_saml_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
397                                                   gss_any_t input) const
398 {
399 }
400
401 void
402 gss_eap_saml_attr_provider::marshall(gss_buffer_t buffer) const
403 {
404 }
405
406 bool
407 gss_eap_saml_attr_provider::unmarshall(const gss_eap_attr_ctx *ctx,
408                                        const gss_buffer_t buffer)
409 {
410     return false;
411 }
412
413 bool
414 gss_eap_saml_attr_provider::init(void)
415 {
416     return true;
417 }
418
419 void
420 gss_eap_saml_attr_provider::finalize(void)
421 {
422 }
423
424 gss_eap_attr_provider *
425 gss_eap_saml_attr_provider::createAttrContext(const gss_eap_attr_ctx *ctx,
426                                               gss_cred_id_t gssCred,
427                                               gss_ctx_id_t gssCtx)
428 {
429     if (gssCtx != GSS_C_NO_CONTEXT)
430         return new gss_eap_saml_attr_provider(ctx, gssCred, gssCtx);
431     else
432         return new gss_eap_saml_attr_provider(ctx);
433 }