import radius state
[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 /*
52  * gss_eap_saml_assertion_provider is for retrieving the underlying
53  * assertion.
54  */
55 gss_eap_saml_assertion_provider::gss_eap_saml_assertion_provider(void)
56 {
57     m_assertion = NULL;
58     m_authenticated = false;
59 }
60
61 gss_eap_saml_assertion_provider::~gss_eap_saml_assertion_provider(void)
62 {
63     delete m_assertion;
64 }
65
66 bool
67 gss_eap_saml_assertion_provider::initFromExistingContext(const gss_eap_attr_ctx *manager,
68                                                          const gss_eap_attr_provider *ctx)
69 {
70     /* Then we may be creating from an existing attribute context */
71     const gss_eap_saml_assertion_provider *saml;
72
73     assert(m_assertion == NULL);
74
75     if (!gss_eap_attr_provider::initFromExistingContext(manager, ctx))
76         return false;
77
78     saml = static_cast<const gss_eap_saml_assertion_provider *>(ctx);
79     setAssertion(saml->getAssertion(), saml->authenticated());
80
81     return true;
82 }
83
84 bool
85 gss_eap_saml_assertion_provider::initFromGssContext(const gss_eap_attr_ctx *manager,
86                                                     const gss_cred_id_t gssCred,
87                                                     const gss_ctx_id_t gssCtx)
88 {
89     const gss_eap_radius_attr_provider *radius;
90     gss_buffer_desc value = GSS_C_EMPTY_BUFFER;
91     int authenticated, complete, more = -1;
92     OM_uint32 minor;
93
94     assert(m_assertion == NULL);
95
96     if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
97         return false;
98
99     radius = static_cast<const gss_eap_radius_attr_provider *>
100         (m_manager->getProvider(ATTR_TYPE_RADIUS));
101     if (radius != NULL &&
102         radius->getAttribute(PW_SAML_ASSERTION, &authenticated, &complete,
103                              &value, NULL, &more)) {
104         setAssertion(&value, authenticated);
105         gss_release_buffer(&minor, &value);
106     } else {
107         m_assertion = NULL;
108     }
109
110     return true;
111 }
112
113 void
114 gss_eap_saml_assertion_provider::setAssertion(const saml2::Assertion *assertion,
115                                               bool authenticated)
116 {
117
118     delete m_assertion;
119
120     if (assertion != NULL) {
121         m_assertion = dynamic_cast<saml2::Assertion *>(assertion->clone());
122         m_authenticated = authenticated;
123     } else {
124         m_assertion = NULL;
125         m_authenticated = false;
126     }
127 }
128
129 void
130 gss_eap_saml_assertion_provider::setAssertion(const gss_buffer_t buffer,
131                                               bool authenticated)
132 {
133     delete m_assertion;
134
135     m_assertion = parseAssertion(buffer);
136     m_authenticated = (m_assertion != NULL && authenticated);
137 }
138
139 saml2::Assertion *
140 gss_eap_saml_assertion_provider::parseAssertion(const gss_buffer_t buffer)
141 {
142     string str((char *)buffer->value, buffer->length);
143     istringstream istream(str);
144     DOMDocument *doc;
145     const XMLObjectBuilder *b;
146
147     doc = XMLToolingConfig::getConfig().getParser().parse(istream);
148     b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
149
150     return dynamic_cast<saml2::Assertion *>(b->buildFromDocument(doc));
151 }
152
153 bool
154 gss_eap_saml_assertion_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
155                                                    void *data) const
156 {
157     bool ret;
158
159     /* just add the prefix */
160     if (m_assertion != NULL)
161         ret = addAttribute(this, GSS_C_NO_BUFFER, data);
162     else
163         ret = true;
164
165     return ret;
166 }
167
168 void
169 gss_eap_saml_assertion_provider::setAttribute(int complete,
170                                               const gss_buffer_t attr,
171                                               const gss_buffer_t value)
172 {
173     if (attr == GSS_C_NO_BUFFER || attr->length == 0) {
174         setAssertion(value);
175     }
176 }
177
178 void
179 gss_eap_saml_assertion_provider::deleteAttribute(const gss_buffer_t value)
180 {
181     delete m_assertion;
182     m_assertion = NULL;
183     m_authenticated = false;
184 }
185
186 time_t
187 gss_eap_saml_assertion_provider::getExpiryTime(void) const
188 {
189     saml2::Conditions *conditions;
190     time_t expiryTime = 0;
191
192     if (m_assertion == NULL)
193         return 0;
194
195     conditions = m_assertion->getConditions();
196
197     if (conditions != NULL && conditions->getNotOnOrAfter() != NULL)
198         expiryTime = conditions->getNotOnOrAfter()->getEpoch();
199
200     return expiryTime;
201 }
202
203 bool
204 gss_eap_saml_assertion_provider::getAttribute(const gss_buffer_t attr,
205                                               int *authenticated,
206                                               int *complete,
207                                               gss_buffer_t value,
208                                               gss_buffer_t display_value,
209                                               int *more) const
210 {
211     string str;
212
213     if (attr != GSS_C_NO_BUFFER || attr->length != 0)
214         return false;
215
216     if (m_assertion == NULL)
217         return false;
218
219     if (*more != -1)
220         return false;
221
222     if (authenticated != NULL)
223         *authenticated = m_authenticated;
224     if (complete != NULL)
225         *complete = true;
226
227     XMLHelper::serialize(m_assertion->marshall((DOMDocument *)NULL), str);
228
229     duplicateBuffer(str, value);
230     *more = 0;
231
232     return true;
233 }
234
235 gss_any_t
236 gss_eap_saml_assertion_provider::mapToAny(int authenticated,
237                                           gss_buffer_t type_id) const
238 {
239     if (authenticated && !m_authenticated)
240         return (gss_any_t)NULL;
241
242     return (gss_any_t)m_assertion;
243 }
244
245 void
246 gss_eap_saml_assertion_provider::releaseAnyNameMapping(gss_buffer_t type_id,
247                                                        gss_any_t input) const
248 {
249     delete ((saml2::Assertion *)input);
250 }
251
252 void
253 gss_eap_saml_assertion_provider::exportToBuffer(gss_buffer_t buffer) const
254 {
255     ostringstream sink;
256     string str;
257
258     buffer->length = 0;
259     buffer->value = NULL;
260
261     if (m_assertion == NULL)
262         return;
263
264     sink << *m_assertion;
265     str = sink.str();
266
267     duplicateBuffer(str, buffer);
268 }
269
270 bool
271 gss_eap_saml_assertion_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
272                                                 const gss_buffer_t buffer)
273 {
274     if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer))
275         return false;
276
277     if (buffer->length == 0)
278         return true;
279
280     assert(m_assertion == NULL);
281
282     setAssertion(buffer);
283     /* TODO XXX how to propagate authenticated flag? */
284
285     return true;
286 }
287
288 bool
289 gss_eap_saml_assertion_provider::init(void)
290 {
291     gss_eap_attr_ctx::registerProvider(ATTR_TYPE_SAML_ASSERTION,
292                                        "urn:ietf:params:gss-eap:saml-aaa-assertion",
293                                        gss_eap_saml_assertion_provider::createAttrContext);
294     return true;
295 }
296
297 void
298 gss_eap_saml_assertion_provider::finalize(void)
299 {
300     gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_SAML_ASSERTION);
301 }
302
303 gss_eap_attr_provider *
304 gss_eap_saml_assertion_provider::createAttrContext(void)
305 {
306     return new gss_eap_saml_assertion_provider;
307 }
308
309 /*
310  * gss_eap_saml_attr_provider is for retrieving the underlying attributes.
311  */
312 bool
313 gss_eap_saml_attr_provider::getAssertion(int *authenticated,
314                                          const saml2::Assertion **pAssertion) const
315 {
316     const gss_eap_saml_assertion_provider *saml;
317
318     if (authenticated != NULL)
319         *authenticated = false;
320     if (pAssertion != NULL)
321         *pAssertion = NULL;
322
323     saml = static_cast<const gss_eap_saml_assertion_provider *>
324         (m_manager->getProvider(ATTR_TYPE_SAML_ASSERTION));
325     if (saml == NULL)
326         return false;
327
328     if (authenticated != NULL)
329         *authenticated = saml->authenticated();
330     if (pAssertion != NULL)
331         *pAssertion = saml->getAssertion();
332
333     return (saml->getAssertion() != NULL);
334 }
335
336 bool
337 gss_eap_saml_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
338                                               void *data) const
339 {
340     const saml2::Assertion *assertion;
341     bool ret = true;
342     int authenticated;
343
344     if (!getAssertion(&authenticated, &assertion))
345         return true;
346
347     /*
348      * Note: the first prefix is added by the attribute provider manager
349      *
350      * From draft-hartman-gss-eap-naming-00:
351      *
352      *   Each attribute carried in the assertion SHOULD also be a GSS name
353      *   attribute.  The name of this attribute has three parts, all separated
354      *   by an ASCII space character.  The first part is
355      *   urn:ietf:params:gss-eap:saml-attr.  The second part is the URI for
356      *   the SAML attribute name format.  The final part is the name of the
357      *   SAML attribute.  If the mechanism performs an additional attribute
358      *   query, the retrieved attributes SHOULD be GSS-API name attributes
359      *   using the same name syntax.
360      */
361     const vector<saml2::Attribute*>& attrs2 =
362         const_cast<const saml2::AttributeStatement*>(assertion->getAttributeStatements().front())->getAttributes();
363     for (vector<saml2::Attribute*>::const_iterator a = attrs2.begin();
364         a != attrs2.end();
365         ++a)
366     {
367         const XMLCh *attributeName = (*a)->getName();
368         const XMLCh *attributeNameFormat = (*a)->getNameFormat();
369         XMLCh *qualifiedName;
370         XMLCh space[2] = { ' ', 0 };
371         gss_buffer_desc utf8;
372
373         qualifiedName = new XMLCh[XMLString::stringLen(attributeName) + 1 +
374                                   XMLString::stringLen(attributeNameFormat) + 1];
375         XMLString::copyString(qualifiedName, attributeName);
376         XMLString::catString(qualifiedName, space);
377         XMLString::catString(qualifiedName, attributeNameFormat);
378
379         utf8.value = (void *)toUTF8(qualifiedName);
380         utf8.length = strlen((char *)utf8.value);
381
382         ret = addAttribute(this, &utf8, data);
383
384         delete qualifiedName;
385
386         if (!ret)
387             break;
388     }
389
390     return ret;
391 }
392
393 void
394 gss_eap_saml_attr_provider::setAttribute(int complete,
395                                          const gss_buffer_t attr,
396                                          const gss_buffer_t value)
397 {
398 }
399
400 void
401 gss_eap_saml_attr_provider::deleteAttribute(const gss_buffer_t value)
402 {
403 }
404
405 static BaseRefVectorOf<XMLCh> *
406 decomposeAttributeName(const gss_buffer_t attr)
407 {
408     XMLCh *qualifiedAttr = new XMLCh[attr->length + 1];
409     XMLString::transcode((const char *)attr->value, qualifiedAttr, attr->length);
410
411     BaseRefVectorOf<XMLCh> *components = XMLString::tokenizeString(qualifiedAttr);
412
413     delete qualifiedAttr;
414
415     return components;
416 }
417
418 bool
419 gss_eap_saml_attr_provider::getAttribute(const gss_buffer_t attr,
420                                          int *authenticated,
421                                          int *complete,
422                                          const saml2::Attribute **pAttribute) const
423 {
424     const saml2::Assertion *assertion;
425
426     if (authenticated != NULL)
427         *authenticated = false;
428     if (complete != NULL)
429         *complete = true;
430     *pAttribute = NULL;
431
432     if (!getAssertion(authenticated, &assertion) ||
433         assertion->getAttributeStatements().size() == 0)
434         return false;
435
436     /* Check the attribute name consists of name format | whsp | name */
437     BaseRefVectorOf<XMLCh> *components = decomposeAttributeName(attr);
438     if (components == NULL || components->size() != 2) {
439         delete components;
440         return false;
441     }
442
443     /* For each attribute statement, look for an attribute match */
444     const vector <saml2::AttributeStatement *>&statements =
445         assertion->getAttributeStatements();
446     const saml2::Attribute *ret = NULL;
447
448     for (vector<saml2::AttributeStatement *>::const_iterator s = statements.begin();
449         s != statements.end();
450         ++s) {
451         const vector<saml2::Attribute*>& attrs =
452             const_cast<const saml2::AttributeStatement*>(*s)->getAttributes();
453
454         for (vector<saml2::Attribute*>::const_iterator a = attrs.begin(); a != attrs.end(); ++a) {
455             if (XMLString::equals((*a)->getNameFormat(), components->elementAt(0)) &&
456                 XMLString::equals((*a)->getName(), components->elementAt(1))) {
457                 ret = *a;
458                 break;
459             }
460         }
461
462         if (ret != NULL)
463             break;
464     }
465
466     delete components;
467
468     *pAttribute = ret;
469
470     return (ret != NULL);
471 }
472
473 bool
474 gss_eap_saml_attr_provider::getAttribute(const gss_buffer_t attr,
475                                          int *authenticated,
476                                          int *complete,
477                                          gss_buffer_t value,
478                                          gss_buffer_t display_value,
479                                          int *more) const
480 {
481     const saml2::Attribute *a;
482     const saml2::AttributeValue *av;
483     int nvalues, i = *more;
484
485     *more = 0;
486
487     if (!getAttribute(attr, authenticated, complete, &a))
488         return false;
489
490     nvalues = a->getAttributeValues().size();
491
492     if (i == -1)
493         i = 0;
494     else if (i >= nvalues)
495         return false;
496     av = dynamic_cast<const saml2::AttributeValue *>(a->getAttributeValues().at(i)
497 );
498     if (av != NULL) {
499         if (value != NULL) {
500             value->value = toUTF8(av->getTextContent(), true);
501             value->length = strlen((char *)value->value);
502         }
503         if (display_value != NULL) {
504             display_value->value = toUTF8(av->getTextContent(), true);
505             display_value->length = strlen((char *)value->value);
506         }
507     }
508
509     if (nvalues > ++i)
510         *more = i;
511
512     return true;
513 }
514
515 gss_any_t
516 gss_eap_saml_attr_provider::mapToAny(int authenticated,
517                                      gss_buffer_t type_id) const
518 {
519     return (gss_any_t)NULL;
520 }
521
522 void
523 gss_eap_saml_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
524                                                   gss_any_t input) const
525 {
526 }
527
528 void
529 gss_eap_saml_attr_provider::exportToBuffer(gss_buffer_t buffer) const
530 {
531     buffer->length = 0;
532     buffer->value = NULL;
533 }
534
535 bool
536 gss_eap_saml_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
537                                            const gss_buffer_t buffer)
538 {
539     return gss_eap_attr_provider::initFromBuffer(ctx, buffer);
540 }
541
542 bool
543 gss_eap_saml_attr_provider::init(void)
544 {
545     gss_eap_attr_ctx::registerProvider(ATTR_TYPE_SAML,
546                                        "urn:ietf:params:gss-eap:saml-attr",
547                                        gss_eap_saml_attr_provider::createAttrContext);
548     return true;
549 }
550
551 void
552 gss_eap_saml_attr_provider::finalize(void)
553 {
554     gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_SAML);
555 }
556
557 gss_eap_attr_provider *
558 gss_eap_saml_attr_provider::createAttrContext(void)
559 {
560     return new gss_eap_saml_attr_provider;
561 }
562
563 OM_uint32
564 gssEapSamlAttrProvidersInit(OM_uint32 *minor)
565 {
566     if (gss_eap_saml_assertion_provider::init() &&
567         gss_eap_saml_attr_provider::init())
568         return GSS_S_COMPLETE;
569
570     return GSS_S_FAILURE;
571 }
572
573 OM_uint32
574 gssEapSamlAttrProvidersFinalize(OM_uint32 *minor)
575 {
576     gss_eap_saml_attr_provider::finalize();
577     gss_eap_saml_assertion_provider::finalize();
578     return GSS_S_COMPLETE;
579 }