aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/authdata/saml_client/saml_authdata.cpp
blob: 2264af2af97a4aa4e4527a3f11631b2e9ccd76bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
/*
 * plugins/authdata/saml_client/saml_authdata.cpp
 *
 * Copyright 2009 by the Massachusetts Institute of Technology.
 *
 * Export of this software from the United States of America may
 *   require a specific license from the United States Government.
 *   It is the responsibility of any person or organization contemplating
 *   export to obtain such a license before exporting.
 *
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  Furthermore if you modify this software you must label
 * your software as modified software and not distribute it in such a
 * fashion that it might be confused with the original M.I.T. software.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 *
 *
 * Sample authorization data plugin
 */

#include <string.h>
#include <errno.h>

extern "C" {
#include "k5-int.h"
#include <krb5/authdata_plugin.h>
}

#include <saml/SAMLConfig.h>
#include <saml/saml2/metadata/Metadata.h>
#include <saml/saml2/metadata/MetadataProvider.h>
#include <saml/saml2/metadata/MetadataCredentialCriteria.h>
#include <saml/signature/SignatureProfileValidator.h>
#include <saml/util/SAMLConstants.h>
#include <xmltooling/logging.h>
#include <xmltooling/XMLToolingConfig.h>
#include <xmltooling/security/SignatureTrustEngine.h>
#include <xmltooling/security/OpenSSLCredential.h>
#include <xmltooling/signature/Signature.h>
#include <xmltooling/signature/SignatureValidator.h>
#include <xmltooling/util/XMLHelper.h>
#include <xsec/enc/XSECCryptoKeyHMAC.hpp>
#include <xsec/enc/OpenSSL/OpenSSLCryptoKeyHMAC.hpp>
#include <xsec/enc/XSECCryptoException.hpp>
#include <xsec/framework/XSECException.hpp>

using namespace xmlsignature;
using namespace xmlconstants;
using namespace xmltooling::logging;
using namespace xmltooling;
using namespace samlconstants;
using namespace opensaml::saml2md;
using namespace opensaml::saml2;
using namespace opensaml;
using namespace xercesc;
using namespace std;

struct saml_context {
    saml2::Assertion *assertion;
    krb5_boolean verified;
};

extern "C" {
static krb5_error_code
saml_init(krb5_context kcontext, void **plugin_context);

static void
saml_flags(krb5_context kcontext,
           void *plugin_context,
           krb5_authdatatype ad_type,
           krb5_flags *flags);

static void
saml_fini(krb5_context kcontext, void *plugin_context);

static krb5_error_code
saml_request_init(krb5_context kcontext,
                  krb5_authdata_context context,
                  void *plugin_context,
                  void **request_context);

static krb5_error_code
saml_export_authdata(krb5_context kcontext,
                     krb5_authdata_context context,
                     void *plugin_context,
                     void *request_context,
                     krb5_flags usage,
                     krb5_authdata ***out_authdata);

static krb5_error_code
saml_import_authdata(krb5_context kcontext,
                     krb5_authdata_context context,
                     void *plugin_context,
                     void *request_context,
                     krb5_authdata **authdata,
                     krb5_boolean kdc_issued_flag,
                     krb5_const_principal issuer);

static void
saml_request_fini(krb5_context kcontext,
                  krb5_authdata_context context,
                  void *plugin_context,
                  void *request_context);

static krb5_error_code
saml_get_attribute_types(krb5_context kcontext,
                         krb5_authdata_context context,
                         void *plugin_context,
                         void *request_context,
                         krb5_data **out_attrs);

static krb5_error_code
saml_get_attribute(krb5_context kcontext,
                   krb5_authdata_context context,
                   void *plugin_context,
                   void *request_context,
                   const krb5_data *attribute,
                   krb5_boolean *authenticated,
                   krb5_boolean *complete,
                   krb5_data *value,
                   krb5_data *display_value,
                   int *more);

static krb5_error_code
saml_delete_attribute(krb5_context kcontext,
                      krb5_authdata_context context,
                      void *plugin_context,
                      void *request_context,
                      const krb5_data *attribute);

static krb5_error_code
saml_size(krb5_context kcontext,
          krb5_authdata_context context,
          void *plugin_context,
          void *request_context,
          size_t *sizep);

static krb5_error_code
saml_externalize(krb5_context kcontext,
                 krb5_authdata_context context,
                 void *plugin_context,
                 void *request_context,
                 krb5_octet **buffer,
                 size_t *lenremain);

static krb5_error_code
saml_internalize(krb5_context kcontext,
                 krb5_authdata_context context,
                 void *plugin_context,
                 void *request_context,
                 krb5_octet **buffer,
                 size_t *lenremain);

static krb5_error_code
saml_verify_authdata(krb5_context kcontext,
                     krb5_authdata_context context,
                     void *plugin_context,
                     void *request_context,
                     const krb5_auth_context *auth_context,
                     const krb5_keyblock *key,
                     const krb5_ap_req *req);

static krb5_error_code
saml_copy(krb5_context kcontext,
          krb5_authdata_context context,
          void *plugin_context,
          void *request_context,
          void *dst_plugin_context,
          void *dst_request_context);

static void saml_library_init(void) __attribute__((__constructor__));
static void saml_library_fini(void) __attribute__((__destructor__));
}

static void saml_library_init(void)
{
    SAMLConfig &config = SAMLConfig::getConfig();
    XMLToolingConfig& xmlconf = XMLToolingConfig::getConfig();

    if (getenv("SAML_DEBUG"))
        xmlconf.log_config("DEBUG");
    else
        xmlconf.log_config();

    config.init();
}

static void saml_library_fini(void)
{
    SAMLConfig &config = SAMLConfig::getConfig();
    config.term();
}

static krb5_error_code
saml_init(krb5_context kcontext, void **plugin_context)
{
    return 0;
}

static void
saml_flags(krb5_context kcontext,
           void *plugin_context,
           krb5_authdatatype ad_type,
           krb5_flags *flags)
{
    *flags = AD_USAGE_TGS_REQ;
}

static void
saml_fini(krb5_context kcontext, void *plugin_context)
{
}

static krb5_error_code
saml_request_init(krb5_context kcontext,
                  krb5_authdata_context context,
                  void *plugin_context,
                  void **request_context)
{
    struct saml_context *sc;

    sc = (struct saml_context *)calloc(1, sizeof(*sc));
    if (sc == NULL)
        return ENOMEM;

    sc->verified = FALSE;

    *request_context = sc;

    return 0;
}

static krb5_error_code
saml_export_authdata(krb5_context kcontext,
                     krb5_authdata_context context,
                     void *plugin_context,
                     void *request_context,
                     krb5_flags usage,
                     krb5_authdata ***out_authdata)
{
    struct saml_context *sc = (struct saml_context *)request_context;
    krb5_authdata *data[2];
    krb5_authdata datum;
    string buf;

    if (sc->assertion == NULL)
        return 0;

    try {
        XMLHelper::serialize(sc->assertion->marshall((DOMDocument *)NULL), buf);
    } catch (XMLToolingException &e) {
        return ASN1_PARSE_ERROR;
    }

    datum.ad_type = KRB5_AUTHDATA_SAML;
    datum.length = buf.length();
    datum.contents = (krb5_octet *)buf.c_str();

    data[0] = &datum;
    data[1] = NULL;

    return krb5_copy_authdata(kcontext, data, out_authdata);
}

static krb5_error_code
saml_import_authdata(krb5_context kcontext,
                     krb5_authdata_context context,
                     void *plugin_context,
                     void *request_context,
                     krb5_authdata **authdata,
                     krb5_boolean kdc_issued_flag,
                     krb5_const_principal issuer)
{
    krb5_error_code code = 0;
    struct saml_context *sc = (struct saml_context *)request_context;
    string samlbuf((char *)authdata[0]->contents, authdata[0]->length);
    istringstream samlin(samlbuf);
    DOMDocument *doc = NULL;
    DOMElement *elem = NULL;
    const XMLObjectBuilder *b;
    XMLObject *xobj;

    assert(sc->verified == FALSE);

    try {
        doc = XMLToolingConfig::getConfig().getParser().parse(samlin);
        b = XMLObjectBuilder::getDefaultBuilder();
        elem = doc->getDocumentElement();
        xobj = b->buildOneFromElement(elem, true);
#if 0
        sc->assertion = dynamic_cast<saml2::Assertion*>(xobj);
        if (sc->assertion == NULL) {
            fprintf(stderr, "%s\n", typeid(xobj).name());
            delete xobj;
            code = ASN1_PARSE_ERROR;
        }
#else
        sc->assertion = (saml2::Assertion*)((void *)xobj);
#endif
    } catch (XMLToolingException &e) {
        code = ASN1_PARSE_ERROR; /* XXX */
    }

    return code;
}

static krb5_boolean
saml_compare_subject(krb5_context kcontext,
                     Subject *subject,
                     krb5_const_principal principal)
{
    NameID *nameID = subject->getNameID();
    char *sname;
    krb5_error_code ret;
    krb5_principal sprinc;

    if (nameID == NULL)
        return FALSE;

    if (!XMLString::equals(nameID->getFormat(), NameIDType::KERBEROS))
        return FALSE;

    if (!nameID->getName())
        return FALSE;

    sname = toUTF8(nameID->getName());
    if (sname == NULL)
        return FALSE;

    if (krb5_parse_name(kcontext, sname, &sprinc) == 0)
        ret = krb5_principal_compare(kcontext, sprinc, principal);
    else
        ret = FALSE;

    delete sname;

    return ret;
}

static krb5_timestamp
saml_get_authtime(krb5_context kcontext,
                  saml2::Assertion *assertion)
{
    krb5_timestamp ktime = 0;
    const AuthnStatement *statement;

    if (assertion->getAuthnStatements().size() == 1) {
        statement = assertion->getAuthnStatements().front();
        ktime = statement->getAuthnInstant()->getEpoch(false);
    }

    return ktime;
}

static krb5_error_code
saml_verify_authdata(krb5_context kcontext,
                     krb5_authdata_context context,
                     void *plugin_context,
                     void *request_context,
                     const krb5_auth_context *auth_context,
                     const krb5_keyblock *key,
                     const krb5_ap_req *req)
{
    krb5_error_code code;
    struct saml_context *sc = (struct saml_context *)request_context;
    krb5_keyblock *skey = req->ticket->enc_part2->session;

    if (sc->assertion == NULL)
        return EINVAL;

    try {
        OpenSSLCryptoKeyHMAC xkey;
        Signature *signature = sc->assertion->getSignature();
        DSIGSignature *dsig = signature->getXMLSignature();

        if (dsig == NULL)
            return KRB5KRB_AP_ERR_BAD_INTEGRITY;

        xkey.setKey(skey->contents, skey->length);

        dsig->setSigningKey(xkey.clone());
        if (dsig->verify())
            code = 0;
        else
            code = KRB5KRB_AP_ERR_BAD_INTEGRITY;
    } catch (XSECException &e) {
        code = KRB5KRB_AP_ERR_BAD_INTEGRITY;
    } catch (XSECCryptoException &e) {
        code = KRB5KRB_AP_ERR_BAD_INTEGRITY;
    }

    if (code == 0) {
        krb5_timestamp authtime;

        authtime = req->ticket->enc_part2->times.authtime;

        if (saml_get_authtime(kcontext, sc->assertion) != authtime ||
            saml_compare_subject(kcontext, sc->assertion->getSubject(),
                                 req->ticket->enc_part2->client) == FALSE)
            code = KRB5KRB_AP_WRONG_PRINC;
    }

    sc->verified = (code == 0);

    return 0;
}

static void
saml_request_fini(krb5_context kcontext,
                  krb5_authdata_context context,
                  void *plugin_context,
                  void *request_context)
{
    struct saml_context *sc = (struct saml_context *)request_context;

    if (sc != NULL) {
        delete sc->assertion;
        free(sc);
    }
}

static krb5_error_code
saml_get_attribute_types(krb5_context kcontext,
                         krb5_authdata_context context,
                         void *plugin_context,
                         void *request_context,
                         krb5_data **out_attrs)
{
    krb5_error_code code;
    struct saml_context *sc = (struct saml_context *)request_context;
    saml2::Assertion *token2 = sc->assertion;
    size_t nattrs, i = 0;
    krb5_data *attrs;

    if (token2 == NULL)
        return EINVAL;

    if (token2->getAttributeStatements().size() == 0)
        return ENOENT;

    nattrs = token2->getAttributeStatements().front()->getAttributes().size();

    attrs = (krb5_data *)k5alloc((nattrs + 1) * sizeof(krb5_data), &code);
    if (code != 0)
        return code;

    const vector<saml2::Attribute*>& attrs2 =
        const_cast<const saml2::AttributeStatement*>(token2->getAttributeStatements().front())->getAttributes();
    for (vector<saml2::Attribute*>::const_iterator a = attrs2.begin();
        a != attrs2.end();
        ++a)
    {
        krb5_data *d = &attrs[i++];

        d->magic = KV5M_DATA;
        d->data = toUTF8((*a)->getName(), true);
        d->length = strlen(d->data);
    }
    assert(i == nattrs);
    attrs[i].data = NULL;
    attrs[i].length = 0;

    *out_attrs = attrs;

    return 0;
}

static const saml2::Attribute *
saml_get_attribute_object(krb5_context context,
                          struct saml_context *sc,
                          const krb5_data *name)
{
    saml2::Assertion *token2 = sc->assertion;
    auto_ptr_XMLCh desiredName(name->data, name->length);

    if (token2->getAttributeStatements().size() == 0)
        return NULL;

    const vector<saml2::Attribute*>& attrs2 =
        const_cast<const saml2::AttributeStatement*>(token2->getAttributeStatements().front())->getAttributes();

    for (vector<saml2::Attribute*>::const_iterator a = attrs2.begin();
        a != attrs2.end();
        ++a) {
        if (XMLString::equals((*a)->getName(), desiredName.get()))
            return (*a);
    }

    return NULL;
}

static int
saml_get_attribute_index(krb5_context context,
                         AttributeStatement *statement,
                         const krb5_data *name)
{
    auto_ptr_XMLCh desiredName(name->data, name->length);
    int index = 0;

    const vector<saml2::Attribute*>& attrs2 =
        const_cast<const saml2::AttributeStatement*>(statement)->getAttributes();

    for (vector<saml2::Attribute*>::const_iterator a = attrs2.begin();
        a != attrs2.end();
        ++a) {
        if (XMLString::equals((*a)->getName(), desiredName.get()))
            return index;
        ++index;
    }

    return -1;
}

static krb5_error_code
saml_get_attribute_value(krb5_context context,
                         struct saml_context *sc,
                         const Attribute *attr,
                         krb5_boolean *authenticated,
                         krb5_boolean *complete,
                         krb5_data *value,
                         krb5_data *display_value,
                         int *more)
{
    const AttributeValue *av;
    int nvalues = attr->getAttributeValues().size();

    /*
     * On the first call, *more is -1. If there are more values,
     * it is set to the index of the next value to return. On
     * the last value, it is set to zero.
     */
    if (*more == -1)
        *more = 0;
    else if (*more >= nvalues) {
        *more = 0;
        return ENOENT;
    }

//    av = dynamic_cast<const AttributeValue *>(attr->getAttributeValues().at(*more));
    av = (const AttributeValue *)((void *)attr->getAttributeValues().at(*more));
    if (av == NULL) {
        *more = 0;
        return ENOENT;
    }

    *authenticated = sc->verified; /* XXX */
    *complete = TRUE; /* XXX */

    value->data = toUTF8(av->getTextContent(), true);
    value->length = strlen(value->data);

    display_value->data = toUTF8(av->getTextContent(), true);
    display_value->length = strlen(display_value->data);

    if (nvalues > *more + 1)
        (*more)++;
    else
        *more = 0;

    return 0;
}

static krb5_error_code
saml_get_attribute(krb5_context kcontext,
                   krb5_authdata_context context,
                   void *plugin_context,
                   void *request_context,
                   const krb5_data *attribute,
                   krb5_boolean *authenticated,
                   krb5_boolean *complete,
                   krb5_data *value,
                   krb5_data *display_value,
                   int *more)
{
    krb5_error_code code;
    struct saml_context *sc = (struct saml_context *)request_context;
    const Attribute *attr;

    if (sc->assertion == NULL)
        return EINVAL;

    attr = saml_get_attribute_object(kcontext, sc, attribute);
    if (attr == NULL) {
        assert(0);
        return ENOENT;
    }

    code = saml_get_attribute_value(kcontext, sc, attr,
                                    authenticated, complete,
                                    value, display_value, more);

    return code;
}

static krb5_error_code
saml_set_attribute(krb5_context kcontext,
                   krb5_authdata_context context,
                   void *plugin_context,
                   void *request_context,
                   krb5_boolean complete,
                   const krb5_data *attribute,
                   const krb5_data *value)
{
    struct saml_context *sc = (struct saml_context *)request_context;
    Attribute *attr;
    AttributeValue *attrValue;
    auto_ptr_XMLCh canonicalName(attribute->data, attribute->length);
    auto_ptr_XMLCh valueString(value->data, value->length);

    attr = AttributeBuilder::buildAttribute();
    attr->setNameFormat(Attribute::URI_REFERENCE);
    attr->setName(canonicalName.get());

    attrValue = AttributeValueBuilder::buildAttributeValue();
    attrValue->setTextContent(valueString.get());

    attr->getAttributeValues().push_back(attrValue);

    if (sc->assertion == NULL) {
        /* XXX this is probably not what we want to be doing */
        AttributeStatement *attrStatement;

        attrStatement = AttributeStatementBuilder::buildAttributeStatement();
        attrStatement->getAttributes().push_back(attr);

        sc->assertion = AssertionBuilder::buildAssertion();
        sc->assertion->getAttributeStatements().push_back(attrStatement);
    }

    return 0;
}

static krb5_error_code
saml_delete_attribute(krb5_context kcontext,
                      krb5_authdata_context context,
                      void *plugin_context,
                      void *request_context,
                      const krb5_data *attribute)
{
    struct saml_context *sc = (struct saml_context *)request_context;
    saml2::Assertion *token2 = sc->assertion;
    auto_ptr_XMLCh desiredName(attribute->data, attribute->length);
    saml2::AttributeStatement *attrStatement;
    int index;

    if (token2 == NULL)
        return ENOENT;

    if (token2->getAttributeStatements().size() == 0)
        return ENOENT;

    attrStatement = (saml2::AttributeStatement *)token2->getAttributeStatements().front();
    index = saml_get_attribute_index(kcontext, attrStatement, attribute);
    if (index == -1)
        return ENOENT;

    attrStatement->getAttributes().erase(
        attrStatement->getAttributes().begin() + index);

    sc->verified = FALSE;

    return 0;
}

static krb5_error_code
saml_size(krb5_context kcontext,
           krb5_authdata_context context,
           void *plugin_context,
           void *request_context,
           size_t *sizep)
{
    struct saml_context *sc = (struct saml_context *)request_context;
    string buf;

    try {
        XMLHelper::serialize(sc->assertion->marshall((DOMDocument *)NULL), buf);
    } catch (XMLToolingException &e) {
        return ASN1_PARSE_ERROR;
    }

    *sizep += sizeof(krb5_int32) + buf.length() + sizeof(krb5_int32);

    return 0;
}

static krb5_error_code
saml_externalize(krb5_context kcontext,
                 krb5_authdata_context context,
                 void *plugin_context,
                 void *request_context,
                 krb5_octet **buffer,
                 size_t *lenremain)
{
    struct saml_context *sc = (struct saml_context *)request_context;
    string buf;

    try {
        XMLHelper::serialize(sc->assertion->marshall((DOMDocument *)NULL), buf);
    } catch (XMLToolingException &e) {
        return ASN1_PARSE_ERROR;
    }

    if (*lenremain < sizeof(krb5_int32) + buf.length() + sizeof(krb5_int32))
        return ENOMEM;

    krb5_ser_pack_int32(buf.length(), buffer, lenremain);
    krb5_ser_pack_bytes((krb5_octet *)buf.c_str(), buf.length(),
                        buffer, lenremain);
    krb5_ser_pack_int32((krb5_int32)sc->verified, buffer, lenremain);

    return 0;
}

static krb5_error_code
saml_internalize(krb5_context kcontext,
                 krb5_authdata_context context,
                 void *plugin_context,
                 void *request_context,
                 krb5_octet **buffer,
                 size_t *lenremain)
{
    struct saml_context *sc = (struct saml_context *)request_context;
    krb5_error_code code;
    krb5_int32 length;
    krb5_octet *contents = NULL;
    krb5_int32 verified;
    krb5_octet *bp;
    size_t remain;

    bp = *buffer;
    remain = *lenremain;

    code = krb5_ser_unpack_int32(&length, &bp, &remain);
    if (code != 0)
        return code;

    if (length != 0) {
        krb5_authdata ad_datum, *ad_data[2];

        ad_datum.contents = bp;
        ad_datum.length = length;

        ad_data[0] = &ad_datum;
        ad_data[1] = NULL;

        if (remain < (size_t)length)
            return ENOMEM;

        code = saml_import_authdata(kcontext, context,
                                    plugin_context, request_context,
                                    ad_data, FALSE, NULL);
        if (code != 0)
            return code;

        bp += length;
        remain -= length;
    }

    /* Verified */
    code = krb5_ser_unpack_int32(&verified, &bp, &remain);
    if (code != 0) {
        free(contents);
        return code;
    }

    sc->verified = (verified != 0);

    *buffer = bp;
    *lenremain = remain;

    return 0;
}

static krb5_error_code
saml_copy(krb5_context kcontext,
          krb5_authdata_context context,
          void *plugin_context,
          void *request_context,
          void *dst_plugin_context,
          void *dst_request_context)
{
    struct saml_context *src = (struct saml_context *)request_context;
    struct saml_context *dst = (struct saml_context *)dst_request_context;

    if (src->assertion != NULL)
        dst->assertion = (saml2::Assertion *)((void *)src->assertion->clone());
    dst->verified = src->verified;

    assert(dst->assertion != NULL);

    return 0;
}

static krb5_authdatatype saml_ad_types[] = { KRB5_AUTHDATA_SAML, 0 };

krb5plugin_authdata_client_ftable_v0 authdata_client_0 = {
    "saml",
    saml_ad_types,
    saml_init,
    saml_fini,
    saml_flags,
    saml_request_init,
    saml_request_fini,
    saml_get_attribute_types,
    saml_get_attribute,
    saml_set_attribute,
    saml_delete_attribute,
    saml_export_authdata,
    saml_import_authdata,
    NULL,
    NULL,
    saml_verify_authdata,
    saml_size,
    saml_externalize,
    saml_internalize,
    saml_copy
};