allow reauth code to use default credentials
[mech_eap.git] / util_reauth.c
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 <dlfcn.h>
36
37 /*
38  * Fast reauthentication support for EAP GSS.
39  */
40
41 krb5_error_code
42 krb5_encrypt_tkt_part(krb5_context, const krb5_keyblock *, krb5_ticket *);
43
44 krb5_error_code
45 encode_krb5_ticket(const krb5_ticket *rep, krb5_data **code);
46
47 static OM_uint32
48 gssDisplayName(OM_uint32 *minor,
49                gss_name_t name,
50                gss_buffer_t buffer,
51                gss_OID *name_type);
52
53 static OM_uint32
54 gssImportName(OM_uint32 *minor,
55               gss_buffer_t buffer,
56               gss_OID name_type,
57               gss_name_t *name);
58
59 static krb5_error_code
60 getAcceptorKey(krb5_context krbContext,
61                gss_ctx_id_t ctx,
62                gss_cred_id_t cred,
63                krb5_principal *princ,
64                krb5_keyblock *key)
65 {
66     krb5_error_code code;
67     krb5_keytab keytab = NULL;
68     krb5_keytab_entry ktent = { 0 };
69     krb5_kt_cursor cursor = NULL;
70
71     *princ = NULL;
72     memset(key, 0, sizeof(*key));
73
74     code = krb5_kt_default(krbContext, &keytab);
75     if (code != 0)
76         goto cleanup;
77
78     if (cred != GSS_C_NO_CREDENTIAL && cred->name != GSS_C_NO_NAME) {
79         code = krb5_kt_get_entry(krbContext, keytab,
80                                  cred->name->krbPrincipal, 0,
81                                  ctx->encryptionType, &ktent);
82         if (code != 0)
83             goto cleanup;
84     } else {
85         /*
86          * It's not clear that looking encrypting the ticket in the
87          * requested EAP enctype provides any value.
88          */
89         code = krb5_kt_start_seq_get(krbContext, keytab, &cursor);
90         if (code != 0)
91             goto cleanup;
92
93         while ((code = krb5_kt_next_entry(krbContext, keytab,
94                                           &ktent, &cursor)) == 0) {
95             if (ktent.key.enctype == ctx->encryptionType)
96                 break;
97             else
98                 krb5_free_keytab_entry_contents(krbContext, &ktent);
99         }
100     }
101
102     if (code == 0) {
103         *princ = ktent.principal;
104         *key = ktent.key;
105     }
106
107 cleanup:
108     if (cred == GSS_C_NO_CREDENTIAL || cred->name == GSS_C_NO_NAME)
109         krb5_kt_end_seq_get(krbContext, keytab, &cursor);
110     krb5_kt_close(krbContext, keytab);
111
112     if (code != 0)
113         krb5_free_keytab_entry_contents(krbContext, &ktent);
114
115     return code;
116 }
117
118 static OM_uint32
119 freezeAttrContext(OM_uint32 *minor,
120                   gss_name_t initiatorName,
121                   krb5_const_principal acceptorPrinc,
122                   krb5_keyblock *session,
123                   krb5_authdata ***authdata)
124 {
125     OM_uint32 major, tmpMinor;
126     krb5_error_code code;
127     gss_buffer_desc attrBuf = GSS_C_EMPTY_BUFFER;
128     krb5_authdata *authData[2], authDatum = { 0 };
129     krb5_context krbContext;
130
131     GSSEAP_KRB_INIT(&krbContext);
132
133     major = gssEapExportAttrContext(minor, initiatorName, &attrBuf);
134     if (GSS_ERROR(major))
135         return major;
136
137     authDatum.ad_type = KRB5_AUTHDATA_RADIUS_AVP;
138     authDatum.length = attrBuf.length;
139     authDatum.contents = attrBuf.value;
140     authData[0] = &authDatum;
141     authData[1] = NULL;
142
143     code = krb5_make_authdata_kdc_issued(krbContext, session, acceptorPrinc,
144                                          authData, authdata);
145     if (code != 0) {
146         major = GSS_S_FAILURE;
147         *minor = code;
148     } else {
149         major = GSS_S_COMPLETE;
150     }
151
152     gss_release_buffer(&tmpMinor, &attrBuf);
153
154     return major;
155 }
156
157 /*
158  * Fabricate a ticket to ourselves given a GSS EAP context.
159  */
160 OM_uint32
161 gssEapMakeReauthCreds(OM_uint32 *minor,
162                       gss_ctx_id_t ctx,
163                       gss_cred_id_t cred,
164                       gss_buffer_t credBuf)
165 {
166     OM_uint32 major = GSS_S_COMPLETE;
167     krb5_error_code code;
168     krb5_context krbContext = NULL;
169     krb5_ticket ticket = { 0 };
170     krb5_keyblock session = { 0 }, acceptorKey = { 0 };
171     krb5_enc_tkt_part enc_part = { 0 };
172     krb5_data *ticketData = NULL, *credsData = NULL;
173     krb5_creds creds = { 0 };
174     krb5_auth_context authContext = NULL;
175
176     credBuf->length = 0;
177     credBuf->value = NULL;
178
179     GSSEAP_KRB_INIT(&krbContext);
180
181     code = getAcceptorKey(krbContext, ctx, cred,
182                           &ticket.server, &acceptorKey);
183     if (code == KRB5_KT_NOTFOUND) {
184         gss_buffer_desc emptyToken = { 0, "" };
185
186         /*
187          * If we can't produce the KRB-CRED message, we need to
188          * return an empty (not NULL) token to the caller so we
189          * don't change the number of authentication legs.
190          */
191         return duplicateBuffer(minor, &emptyToken, credBuf);
192     } else if (code != 0)
193         goto cleanup;
194
195     enc_part.flags = TKT_FLG_INITIAL;
196
197     /*
198      * Generate a random session key to place in the ticket and
199      * sign the "KDC-Issued" authorization data element.
200      */
201     code = krb5_c_make_random_key(krbContext, ctx->encryptionType,
202                                   &session);
203     if (code != 0)
204         goto cleanup;
205
206     enc_part.session = &session;
207     enc_part.client = ctx->initiatorName->krbPrincipal;
208     enc_part.times.authtime = time(NULL);
209     enc_part.times.starttime = enc_part.times.authtime;
210     enc_part.times.endtime = ctx->expiryTime
211                              ? ctx->expiryTime
212                              : KRB5_INT32_MAX;
213     enc_part.times.renew_till = 0;
214
215     major = freezeAttrContext(minor, ctx->initiatorName, ticket.server,
216                               &session, &enc_part.authorization_data);
217     if (GSS_ERROR(major))
218         goto cleanup;
219
220     ticket.enc_part2 = &enc_part;
221
222     code = krb5_encrypt_tkt_part(krbContext, &acceptorKey, &ticket);
223     if (code != 0)
224         goto cleanup;
225
226     code = encode_krb5_ticket(&ticket, &ticketData);
227     if (code != 0)
228         goto cleanup;
229
230     creds.client = enc_part.client;
231     creds.server = ticket.server;
232     creds.keyblock = session;
233     creds.times = enc_part.times;
234     creds.ticket_flags = enc_part.flags;
235     creds.ticket = *ticketData;
236     creds.authdata = enc_part.authorization_data;
237
238     code = krb5_auth_con_init(krbContext, &authContext);
239     if (code != 0)
240         goto cleanup;
241
242     code = krb5_auth_con_setflags(krbContext, authContext, 0);
243     if (code != 0)
244         goto cleanup;
245
246     code = krb5_auth_con_setsendsubkey(krbContext, authContext,
247                                        &ctx->rfc3961Key);
248     if (code != 0)
249         goto cleanup;
250
251     code = krb5_mk_1cred(krbContext, authContext, &creds, &credsData, NULL);
252     if (code != 0)
253         goto cleanup;
254
255     krbDataToGssBuffer(credsData, credBuf);
256
257 cleanup:
258     if (ticket.enc_part.ciphertext.data != NULL)
259         GSSEAP_FREE(ticket.enc_part.ciphertext.data);
260     krb5_free_keyblock_contents(krbContext, &session);
261     krb5_free_keyblock_contents(krbContext, &acceptorKey);
262     krb5_free_data(krbContext, ticketData);
263     krb5_auth_con_free(krbContext, authContext);
264     krb5_free_authdata(krbContext, enc_part.authorization_data);
265     if (credsData != NULL)
266         GSSEAP_FREE(credsData);
267
268     if (major == GSS_S_COMPLETE) {
269         *minor = code;
270         major = code != 0 ? GSS_S_FAILURE : GSS_S_COMPLETE;
271     }
272
273     return major;
274 }
275
276 static int
277 isTicketGrantingServiceP(krb5_context krbContext,
278                          krb5_const_principal principal)
279 {
280     if (krb5_princ_size(krbContext, principal) == 2 &&
281         krb5_princ_component(krbContext, principal, 0)->length == 6 &&
282         memcmp(krb5_princ_component(krbContext,
283                                     principal, 0)->data, "krbtgt", 6) == 0)
284         return TRUE;
285
286     return FALSE;
287 }
288
289 static int
290 reauthUseCredsCache(krb5_context krbContext,
291                     krb5_principal principal)
292 {
293     int reauthUseCCache;
294
295     /* if reauth_use_ccache, use default credentials cache if ticket is for us */
296     krb5_appdefault_boolean(krbContext, "eap_gss",
297                             krb5_princ_realm(krbContext, principal),
298                             "reauth_use_ccache", 0, &reauthUseCCache);
299
300     return reauthUseCCache;
301 }
302
303 static OM_uint32
304 getReauthCredentials(OM_uint32 *minor,
305                      gss_cred_id_t cred,
306                      gss_name_t target,
307                      time_t now,
308                      OM_uint32 timeReq)
309 {
310     OM_uint32 major = GSS_S_CRED_UNAVAIL;
311     krb5_context krbContext = NULL;
312     krb5_error_code code;
313     krb5_ccache ccache = NULL;
314     krb5_creds match = { 0 };
315     krb5_creds creds = { 0 };
316
317     GSSEAP_KRB_INIT(&krbContext);
318
319     assert(cred != GSS_C_NO_CREDENTIAL);
320     assert(target != GSS_C_NO_NAME);
321
322     if (!reauthUseCredsCache(krbContext, cred->name->krbPrincipal))
323         goto cleanup;
324
325     match.client = cred->name->krbPrincipal;
326     match.server = target->krbPrincipal;
327     if (timeReq != 0 && timeReq != GSS_C_INDEFINITE)
328         match.times.endtime = now + timeReq;
329
330     code = krb5_cc_default(krbContext, &ccache);
331     if (code != 0)
332         goto cleanup;
333
334     code = krb5_cc_retrieve_cred(krbContext, ccache, 0, &match, &creds);
335     if (code != 0)
336         goto cleanup;
337
338     cred->flags |= CRED_FLAG_DEFAULT_CCACHE;
339     cred->krbCredCache = ccache;
340     ccache = NULL;
341
342     major = gss_krb5_import_cred(minor, cred->krbCredCache, NULL, NULL,
343                                  &cred->krbCred);
344
345 cleanup:
346     if (major == GSS_S_CRED_UNAVAIL)
347         *minor = code;
348
349     if (ccache != NULL)
350         krb5_cc_close(krbContext, ccache);
351     krb5_free_cred_contents(krbContext, &creds);
352
353     return major;
354 }
355
356 int
357 gssEapCanReauthP(gss_cred_id_t cred,
358                  gss_name_t target,
359                  OM_uint32 timeReq)
360 {
361     time_t now;
362
363     if (cred == GSS_C_NO_CREDENTIAL)
364         return FALSE;
365
366     now = time(NULL);
367
368     if (cred->krbCredCache != NULL &&
369         cred->expiryTime > time(NULL) + (timeReq ? timeReq : 0))
370         return TRUE;
371
372     if (cred->name != GSS_C_NO_NAME) {
373         OM_uint32 major, minor;
374
375         major = getReauthCredentials(&minor, cred, target, now, timeReq);
376
377         return !GSS_ERROR(major);
378     }
379
380     return FALSE;
381 }
382
383 /*
384  * Store re-authentication (Kerberos) credentials in a credential handle.
385  */
386 OM_uint32
387 gssEapStoreReauthCreds(OM_uint32 *minor,
388                        gss_ctx_id_t ctx,
389                        gss_cred_id_t cred,
390                        gss_buffer_t credBuf)
391 {
392     OM_uint32 major = GSS_S_COMPLETE;
393     krb5_error_code code;
394     krb5_context krbContext = NULL;
395     krb5_auth_context authContext = NULL;
396     krb5_data credData = { 0 };
397     krb5_creds **creds = NULL;
398     krb5_principal canonPrinc;
399     krb5_principal ccPrinc = NULL;
400     int i;
401
402     if (credBuf->length == 0 || cred == GSS_C_NO_CREDENTIAL)
403         return GSS_S_COMPLETE;
404
405     GSSEAP_KRB_INIT(&krbContext);
406
407     code = krb5_auth_con_init(krbContext, &authContext);
408     if (code != 0)
409         goto cleanup;
410
411     code = krb5_auth_con_setflags(krbContext, authContext, 0);
412     if (code != 0)
413         goto cleanup;
414
415     code = krb5_auth_con_setrecvsubkey(krbContext, authContext,
416                                        &ctx->rfc3961Key);
417     if (code != 0)
418         goto cleanup;
419
420     gssBufferToKrbData(credBuf, &credData);
421
422     code = krb5_rd_cred(krbContext, authContext, &credData, &creds, NULL);
423     if (code != 0)
424         goto cleanup;
425
426     if (creds == NULL || creds[0] == NULL)
427         goto cleanup;
428
429     code = krb5_copy_principal(krbContext, creds[0]->client, &canonPrinc);
430     if (code != 0)
431         goto cleanup;
432
433     krb5_free_principal(krbContext, cred->name->krbPrincipal);
434     cred->name->krbPrincipal = canonPrinc;
435
436     cred->expiryTime = creds[0]->times.endtime;
437
438     if (cred->krbCredCache == NULL) {
439         if (reauthUseCredsCache(krbContext, creds[0]->client) &&
440             krb5_cc_default(krbContext, &cred->krbCredCache) == 0)
441             cred->flags |= CRED_FLAG_DEFAULT_CCACHE;
442     } else {
443         /*
444          * If we already have an associated credentials cache, possibly from
445          * the last time we stored a reauthentication credential, then we
446          * need to clear it out and release the associated GSS credential.
447          */
448         if (cred->flags & CRED_FLAG_DEFAULT_CCACHE) {
449             krb5_cc_remove_cred(krbContext, cred->krbCredCache, 0, creds[0]);
450         } else {
451             krb5_cc_destroy(krbContext, cred->krbCredCache);
452             cred->krbCredCache = NULL;
453         }
454         gssReleaseCred(minor, &cred->krbCred);
455     }
456
457     if (cred->krbCredCache == NULL) {
458         code = krb5_cc_new_unique(krbContext, "MEMORY", NULL, &cred->krbCredCache);
459         if (code != 0)
460             goto cleanup;
461     }
462
463     if ((cred->flags & CRED_FLAG_DEFAULT_CCACHE) == 0 ||
464         krb5_cc_get_principal(krbContext, cred->krbCredCache, &ccPrinc) != 0) {
465         code = krb5_cc_initialize(krbContext, cred->krbCredCache,
466                                   creds[0]->client);
467         if (code != 0)
468             goto cleanup;
469     }
470
471     for (i = 0; creds[i] != NULL; i++) {
472         krb5_creds kcred = *(creds[i]);
473
474         /*
475          * Swap in the acceptor name the client asked for so
476          * get_credentials() works. We're making the assumption that
477          * any service tickets returned are for us. We'll need to
478          * reflect some more on whether that is a safe assumption.
479          */
480         if (!isTicketGrantingServiceP(krbContext, kcred.server))
481             kcred.server = ctx->acceptorName->krbPrincipal;
482
483         code = krb5_cc_store_cred(krbContext, cred->krbCredCache, &kcred);
484         if (code != 0)
485             goto cleanup;
486     }
487
488     major = gss_krb5_import_cred(minor, cred->krbCredCache, NULL, NULL,
489                                  &cred->krbCred);
490     if (GSS_ERROR(major))
491         goto cleanup;
492
493 cleanup:
494     *minor = code;
495
496     krb5_free_principal(krbContext, ccPrinc);
497     krb5_auth_con_free(krbContext, authContext);
498     if (creds != NULL) {
499         for (i = 0; creds[i] != NULL; i++)
500             krb5_free_creds(krbContext, creds[i]);
501         GSSEAP_FREE(creds);
502     }
503     if (major == GSS_S_COMPLETE)
504         major = *minor ? GSS_S_FAILURE : GSS_S_COMPLETE;
505
506     return major;
507 }
508
509 static gss_buffer_desc radiusAvpKrbAttr = {
510     sizeof("urn:authdata-radius-avp") - 1, "urn:authdata-radius-avp"
511 };
512
513 /*
514  * Unfortunately extracting an AD-KDCIssued authorization data element
515  * is pretty implementation-dependent. It's not possible to verify the
516  * signature ourselves because the ticket session key is not exposed
517  * outside GSS. In an ideal world, all AD-KDCIssued elements would be
518  * verified by the Kerberos library and authentication would fail if
519  * verification failed. We're not quite there yet and as a result have
520  * to go through some hoops to get this to work. The alternative would
521  * be to sign the authorization data with our long-term key, but it
522  * seems a pity to compromise the design because of current implementation
523  * limitations.
524  *
525  * (Specifically, the hoops involve a libkrb5 authorisation data plugin
526  * that exposes the verified and serialised attribute context through
527  * the Kerberos GSS mechanism's naming extensions API.)
528  */
529 static OM_uint32
530 defrostAttrContext(OM_uint32 *minor,
531                    gss_name_t glueName,
532                    gss_name_t mechName)
533 {
534     OM_uint32 major, tmpMinor;
535     gss_buffer_desc authData = GSS_C_EMPTY_BUFFER;
536     gss_buffer_desc authDataDisplay = GSS_C_EMPTY_BUFFER;
537     int more = -1;
538     int authenticated, complete;
539
540     major = gssGetNameAttribute(minor, glueName, &radiusAvpKrbAttr,
541                                 &authenticated, &complete,
542                                 &authData, &authDataDisplay, &more);
543     if (major == GSS_S_COMPLETE) {
544         if (authenticated == 0)
545             major = GSS_S_BAD_NAME;
546         else
547             major = gssEapImportAttrContext(minor, &authData, mechName);
548     } else if (major == GSS_S_UNAVAILABLE) {
549         major = GSS_S_COMPLETE;
550     }
551
552     gss_release_buffer(&tmpMinor, &authData);
553     gss_release_buffer(&tmpMinor, &authDataDisplay);
554
555     return major;
556 }
557
558 /*
559  * Convert a mechanism glue to an EAP mechanism name by displaying and
560  * importing it. This also handles the RADIUS attributes.
561  */
562 OM_uint32
563 gssEapGlueToMechName(OM_uint32 *minor,
564                      gss_name_t glueName,
565                      gss_name_t *pMechName)
566 {
567     OM_uint32 major, tmpMinor;
568     gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
569
570     *pMechName = GSS_C_NO_NAME;
571
572     major = gssDisplayName(minor, glueName, &nameBuf, NULL);
573     if (GSS_ERROR(major))
574         goto cleanup;
575
576     major = gssEapImportName(minor, &nameBuf, GSS_C_NT_USER_NAME,
577                              pMechName);
578     if (GSS_ERROR(major))
579         goto cleanup;
580
581     major = defrostAttrContext(minor, glueName, *pMechName);
582     if (GSS_ERROR(major))
583         goto cleanup;
584
585 cleanup:
586     if (GSS_ERROR(major)) {
587         gssReleaseName(&tmpMinor, pMechName);
588         *pMechName = GSS_C_NO_NAME;
589     }
590
591     gss_release_buffer(&tmpMinor, &nameBuf);
592
593     return major;
594 }
595
596 /*
597  * Convert an EAP mechanism name to a mechanism glue name by displaying
598  * and importing it.
599  */
600 OM_uint32
601 gssEapMechToGlueName(OM_uint32 *minor,
602                      gss_name_t mechName,
603                      gss_name_t *pGlueName)
604 {
605     OM_uint32 major, tmpMinor;
606     gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
607
608     *pGlueName = GSS_C_NO_NAME;
609
610     major = gssEapDisplayName(minor, mechName, &nameBuf, NULL);
611     if (GSS_ERROR(major))
612         goto cleanup;
613
614     major = gssImportName(minor, &nameBuf, GSS_C_NT_USER_NAME,
615                           pGlueName);
616     if (GSS_ERROR(major))
617         goto cleanup;
618
619 cleanup:
620     gss_release_buffer(&tmpMinor, &nameBuf);
621
622     return major;
623 }
624
625 /*
626  * Suck out the analgous elements of a Kerberos GSS context into an EAP
627  * one so that the application doesn't know the difference.
628  */
629 OM_uint32
630 gssEapReauthComplete(OM_uint32 *minor,
631                     gss_ctx_id_t ctx,
632                     gss_cred_id_t cred,
633                     const gss_OID mech,
634                     OM_uint32 timeRec)
635 {
636     OM_uint32 major, tmpMinor;
637     gss_buffer_set_t keyData = GSS_C_NO_BUFFER_SET;
638
639     if (!oidEqual(mech, gss_mech_krb5)) {
640         major = GSS_S_BAD_MECH;
641         goto cleanup;
642     }
643
644     /* Get the raw subsession key and encryption type*/
645     major = gssInquireSecContextByOid(minor, ctx->kerberosCtx,
646                                       GSS_C_INQ_SSPI_SESSION_KEY, &keyData);
647     if (GSS_ERROR(major))
648         goto cleanup;
649
650     {
651         gss_OID_desc oid;
652         int suffix;
653
654         oid.length = keyData->elements[1].length;
655         oid.elements = keyData->elements[1].value;
656
657         /* GSS_KRB5_SESSION_KEY_ENCTYPE_OID */
658         major = decomposeOid(minor,
659                              "\x2a\x86\x48\x86\xf7\x12\x01\x02\x02\x04",
660                              10, &oid, &suffix);
661         if (GSS_ERROR(major))
662             goto cleanup;
663
664         ctx->encryptionType = suffix;
665     }
666
667     {
668         krb5_context krbContext = NULL;
669         krb5_keyblock key;
670
671         GSSEAP_KRB_INIT(&krbContext);
672
673         KRB_KEY_LENGTH(&key) = keyData->elements[0].length;
674         KRB_KEY_DATA(&key)   = keyData->elements[0].value;
675         KRB_KEY_TYPE(&key)   = ctx->encryptionType;
676
677         *minor = krb5_copy_keyblock_contents(krbContext,
678                                              &key, &ctx->rfc3961Key);
679         if (*minor != 0) {
680             major = GSS_S_FAILURE;
681             goto cleanup;
682         }
683     }
684
685     major = rfc3961ChecksumTypeForKey(minor, &ctx->rfc3961Key,
686                                       &ctx->checksumType);
687     if (GSS_ERROR(major))
688         goto cleanup;
689
690     if (timeRec != GSS_C_INDEFINITE)
691         ctx->expiryTime = time(NULL) + timeRec;
692
693     /* Initialize our sequence state */
694     major = sequenceInit(minor,
695                          &ctx->seqState, ctx->recvSeq,
696                          ((ctx->gssFlags & GSS_C_REPLAY_FLAG) != 0),
697                          ((ctx->gssFlags & GSS_C_SEQUENCE_FLAG) != 0),
698                          TRUE);
699     if (GSS_ERROR(major))
700         goto cleanup;
701
702     major = GSS_S_COMPLETE;
703
704 cleanup:
705     gss_release_buffer_set(&tmpMinor, &keyData);
706
707     return major;
708 }
709
710 /*
711  * The remainder of this file consists of wrappers so we can call into the
712  * mechanism glue without calling ourselves.
713  */
714 static OM_uint32
715 (*gssInitSecContextNext)(OM_uint32 *,
716                          gss_cred_id_t,
717                          gss_ctx_id_t *,
718                          gss_name_t,
719                          gss_OID,
720                          OM_uint32,
721                          OM_uint32,
722                          gss_channel_bindings_t,
723                          gss_buffer_t,
724                          gss_OID *,
725                          gss_buffer_t,
726                          OM_uint32 *,
727                          OM_uint32 *);
728
729 static OM_uint32
730 (*gssAcceptSecContextNext)(OM_uint32 *,
731                            gss_ctx_id_t *,
732                            gss_cred_id_t,
733                            gss_buffer_t,
734                            gss_channel_bindings_t,
735                            gss_name_t *,
736                            gss_OID *,
737                            gss_buffer_t,
738                            OM_uint32 *,
739                            OM_uint32 *,
740                            gss_cred_id_t *);
741
742 static OM_uint32
743 (*gssReleaseCredNext)(OM_uint32 *, gss_cred_id_t *);
744
745 static OM_uint32
746 (*gssReleaseNameNext)(OM_uint32 *, gss_name_t *);
747
748 static OM_uint32
749 (*gssInquireSecContextByOidNext)(OM_uint32 *,
750                                  const gss_ctx_id_t,
751                                  const gss_OID,
752                                  gss_buffer_set_t *);
753
754 static OM_uint32
755 (*gssDeleteSecContextNext)(OM_uint32 *,
756                           gss_ctx_id_t *,
757                           gss_buffer_t);
758
759 static OM_uint32
760 (*gssDisplayNameNext)(OM_uint32 *,
761                       gss_name_t,
762                       gss_buffer_t,
763                       gss_OID *);
764
765 static OM_uint32
766 (*gssImportNameNext)(OM_uint32 *,
767                      gss_buffer_t,
768                      gss_OID,
769                      gss_name_t *);
770
771 static OM_uint32
772 (*gssStoreCredNext)(OM_uint32 *,
773                     const gss_cred_id_t,
774                     gss_cred_usage_t,
775                     const gss_OID,
776                     OM_uint32,
777                     OM_uint32,
778                     gss_OID_set *,
779                     gss_cred_usage_t *);
780
781 static OM_uint32
782 (*gssGetNameAttributeNext)(OM_uint32 *,
783                           gss_name_t,
784                           gss_buffer_t,
785                           int *,
786                           int *,
787                           gss_buffer_t,
788                           gss_buffer_t,
789                           int *);
790
791 #define NEXT_SYMBOL(local, global)  ((local) = dlsym(RTLD_NEXT, (global)))
792
793 OM_uint32
794 gssEapReauthInitialize(OM_uint32 *minor)
795 {
796     NEXT_SYMBOL(gssInitSecContextNext,         "gss_init_sec_context");
797     NEXT_SYMBOL(gssAcceptSecContextNext,       "gss_accept_sec_context");
798     NEXT_SYMBOL(gssReleaseCredNext,            "gss_release_cred");
799     NEXT_SYMBOL(gssReleaseNameNext,            "gss_release_name");
800     NEXT_SYMBOL(gssInquireSecContextByOidNext, "gss_inquire_sec_context_by_oid");
801     NEXT_SYMBOL(gssDeleteSecContextNext,       "gss_delete_sec_context");
802     NEXT_SYMBOL(gssDisplayNameNext,            "gss_display_name");
803     NEXT_SYMBOL(gssImportNameNext,             "gss_import_name");
804     NEXT_SYMBOL(gssStoreCredNext,              "gss_store_cred");
805     NEXT_SYMBOL(gssGetNameAttributeNext,       "gss_get_name_attribute");
806
807     return GSS_S_COMPLETE;
808 }
809
810 OM_uint32
811 gssInitSecContext(OM_uint32 *minor,
812                   gss_cred_id_t cred,
813                   gss_ctx_id_t *context_handle,
814                   gss_name_t target_name,
815                   gss_OID mech_type,
816                   OM_uint32 req_flags,
817                   OM_uint32 time_req,
818                   gss_channel_bindings_t input_chan_bindings,
819                   gss_buffer_t input_token,
820                   gss_OID *actual_mech_type,
821                   gss_buffer_t output_token,
822                   OM_uint32 *ret_flags,
823                   OM_uint32 *time_rec)
824 {
825     if (gssInitSecContextNext == NULL)
826         return GSS_S_UNAVAILABLE;
827
828     return gssInitSecContextNext(minor, cred, context_handle,
829                                  target_name, mech_type, req_flags,
830                                  time_req, input_chan_bindings,
831                                  input_token, actual_mech_type,
832                                  output_token, ret_flags, time_rec);
833 }
834
835 OM_uint32
836 gssAcceptSecContext(OM_uint32 *minor,
837                     gss_ctx_id_t *context_handle,
838                     gss_cred_id_t cred,
839                     gss_buffer_t input_token,
840                     gss_channel_bindings_t input_chan_bindings,
841                     gss_name_t *src_name,
842                     gss_OID *mech_type,
843                     gss_buffer_t output_token,
844                     OM_uint32 *ret_flags,
845                     OM_uint32 *time_rec,
846                     gss_cred_id_t *delegated_cred_handle)
847 {
848     if (gssAcceptSecContextNext == NULL)
849         return GSS_S_UNAVAILABLE;
850
851     return gssAcceptSecContextNext(minor, context_handle, cred,
852                                    input_token, input_chan_bindings,
853                                    src_name, mech_type, output_token,
854                                    ret_flags, time_rec, delegated_cred_handle);
855 }
856
857 OM_uint32
858 gssReleaseCred(OM_uint32 *minor,
859                gss_cred_id_t *cred_handle)
860 {
861     if (gssReleaseCredNext == NULL)
862         return GSS_S_UNAVAILABLE;
863
864     return gssReleaseCredNext(minor, cred_handle);
865 }
866
867 OM_uint32
868 gssReleaseName(OM_uint32 *minor,
869                gss_name_t *name)
870 {
871     if (gssReleaseName == NULL)
872         return GSS_S_UNAVAILABLE;
873
874     return gssReleaseNameNext(minor, name);
875 }
876
877 OM_uint32
878 gssDeleteSecContext(OM_uint32 *minor,
879                     gss_ctx_id_t *context_handle,
880                     gss_buffer_t output_token)
881 {
882     if (gssDeleteSecContextNext == NULL)
883         return GSS_S_UNAVAILABLE;
884
885     return gssDeleteSecContextNext(minor, context_handle, output_token);
886 }
887
888 static OM_uint32
889 gssDisplayName(OM_uint32 *minor,
890                gss_name_t name,
891                gss_buffer_t buffer,
892                gss_OID *name_type)
893 {
894     if (gssDisplayNameNext == NULL)
895         return GSS_S_UNAVAILABLE;
896
897     return gssDisplayNameNext(minor, name, buffer, name_type);
898 }
899
900 static OM_uint32
901 gssImportName(OM_uint32 *minor,
902               gss_buffer_t buffer,
903               gss_OID name_type,
904               gss_name_t *name)
905 {
906     if (gssImportNameNext == NULL)
907         return GSS_S_UNAVAILABLE;
908
909     return gssImportNameNext(minor, buffer, name_type, name);
910 }
911
912 OM_uint32
913 gssInquireSecContextByOid(OM_uint32 *minor,
914                           const gss_ctx_id_t context_handle,
915                           const gss_OID desired_object,
916                           gss_buffer_set_t *data_set)
917 {
918     if (gssInquireSecContextByOidNext == NULL)
919         return GSS_S_UNAVAILABLE;
920
921     return gssInquireSecContextByOidNext(minor, context_handle,
922                                          desired_object, data_set);
923 }
924
925 OM_uint32
926 gssStoreCred(OM_uint32 *minor,
927              const gss_cred_id_t input_cred_handle,
928              gss_cred_usage_t input_usage,
929              const gss_OID desired_mech,
930              OM_uint32 overwrite_cred,
931              OM_uint32 default_cred,
932              gss_OID_set *elements_stored,
933              gss_cred_usage_t *cred_usage_stored)
934 {
935     if (gssStoreCredNext == NULL)
936         return GSS_S_UNAVAILABLE;
937
938     return gssStoreCredNext(minor, input_cred_handle, input_usage,
939                             desired_mech, overwrite_cred, default_cred,
940                             elements_stored, cred_usage_stored);
941 }
942
943 OM_uint32
944 gssGetNameAttribute(OM_uint32 *minor,
945                     gss_name_t name,
946                     gss_buffer_t attr,
947                     int *authenticated,
948                     int *complete,
949                     gss_buffer_t value,
950                     gss_buffer_t display_value,
951                     int *more)
952 {
953     if (gssGetNameAttributeNext == NULL)
954         return GSS_S_UNAVAILABLE;
955
956     return gssGetNameAttributeNext(minor, name, attr, authenticated, complete,
957                                    value, display_value, more);
958 }