make reauth support conditionaly compilable
[mech_eap.git] / init_sec_context.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 #ifdef GSSEAP_ENABLE_REAUTH
36 static int
37 canReauthP(gss_cred_id_t cred);
38
39 static OM_uint32
40 eapGssSmInitGssReauth(OM_uint32 *minor,
41                       gss_cred_id_t cred,
42                       gss_ctx_id_t ctx,
43                       gss_name_t target,
44                       gss_OID mech,
45                       OM_uint32 reqFlags,
46                       OM_uint32 timeReq,
47                       gss_channel_bindings_t chanBindings,
48                       gss_buffer_t inputToken,
49                       gss_buffer_t outputToken);
50 #endif
51
52 static OM_uint32
53 policyVariableToFlag(enum eapol_bool_var variable)
54 {
55     OM_uint32 flag = 0;
56
57     switch (variable) {
58     case EAPOL_eapSuccess:
59         flag = CTX_FLAG_EAP_SUCCESS;
60         break;
61     case EAPOL_eapRestart:
62         flag = CTX_FLAG_EAP_RESTART;
63         break;
64     case EAPOL_eapFail:
65         flag = CTX_FLAG_EAP_FAIL;
66         break;
67     case EAPOL_eapResp:
68         flag = CTX_FLAG_EAP_RESP;
69         break;
70     case EAPOL_eapNoResp:
71         flag = CTX_FLAG_EAP_NO_RESP;
72         break;
73     case EAPOL_eapReq:
74         flag = CTX_FLAG_EAP_REQ;
75         break;
76     case EAPOL_portEnabled:
77         flag = CTX_FLAG_EAP_PORT_ENABLED;
78         break;
79     case EAPOL_altAccept:
80         flag = CTX_FLAG_EAP_ALT_ACCEPT;
81         break;
82     case EAPOL_altReject:
83         flag = CTX_FLAG_EAP_ALT_REJECT;
84         break;
85     }
86
87     return flag;
88 }
89
90 static struct eap_peer_config *
91 peerGetConfig(void *ctx)
92 {
93     gss_ctx_id_t gssCtx = (gss_ctx_id_t)ctx;
94
95     return &gssCtx->initiatorCtx.eapPeerConfig;
96 }
97
98 static Boolean
99 peerGetBool(void *data, enum eapol_bool_var variable)
100 {
101     gss_ctx_id_t ctx = data;
102     OM_uint32 flag;
103
104     if (ctx == GSS_C_NO_CONTEXT)
105         return FALSE;
106
107     flag = policyVariableToFlag(variable);
108
109     return ((ctx->flags & flag) != 0);
110 }
111
112 static void
113 peerSetBool(void *data, enum eapol_bool_var variable,
114             Boolean value)
115 {
116     gss_ctx_id_t ctx = data;
117     OM_uint32 flag;
118
119     if (ctx == GSS_C_NO_CONTEXT)
120         return;
121
122     flag = policyVariableToFlag(variable);
123
124     if (value)
125         ctx->flags |= flag;
126     else
127         ctx->flags &= ~(flag);
128 }
129
130 static unsigned int
131 peerGetInt(void *data, enum eapol_int_var variable)
132 {
133     gss_ctx_id_t ctx = data;
134
135     if (ctx == GSS_C_NO_CONTEXT)
136         return FALSE;
137
138     assert(CTX_IS_INITIATOR(ctx));
139
140     switch (variable) {
141     case EAPOL_idleWhile:
142         return ctx->initiatorCtx.idleWhile;
143         break;
144     }
145
146     return 0;
147 }
148
149 static void
150 peerSetInt(void *data, enum eapol_int_var variable,
151            unsigned int value)
152 {
153     gss_ctx_id_t ctx = data;
154
155     if (ctx == GSS_C_NO_CONTEXT)
156         return;
157
158     assert(CTX_IS_INITIATOR(ctx));
159
160     switch (variable) {
161     case EAPOL_idleWhile:
162         ctx->initiatorCtx.idleWhile = value;
163         break;
164     }
165 }
166
167 static struct wpabuf *
168 peerGetEapReqData(void *ctx)
169 {
170     gss_ctx_id_t gssCtx = (gss_ctx_id_t)ctx;
171
172     return &gssCtx->initiatorCtx.reqData;
173 }
174
175 static void
176 peerSetConfigBlob(void *ctx, struct wpa_config_blob *blob)
177 {
178 }
179
180 static const struct wpa_config_blob *
181 peerGetConfigBlob(void *ctx, const char *name)
182 {
183     return NULL;
184 }
185
186 static void
187 peerNotifyPending(void *ctx)
188 {
189 }
190
191 static struct eapol_callbacks gssEapPolicyCallbacks = {
192     peerGetConfig,
193     peerGetBool,
194     peerSetBool,
195     peerGetInt,
196     peerSetInt,
197     peerGetEapReqData,
198     peerSetConfigBlob,
199     peerGetConfigBlob,
200     peerNotifyPending,
201 };
202
203 extern int wpa_debug_level;
204
205 static OM_uint32
206 peerConfigInit(OM_uint32 *minor,
207                gss_cred_id_t cred,
208                gss_ctx_id_t ctx)
209 {
210     krb5_context krbContext;
211     struct eap_peer_config *eapPeerConfig = &ctx->initiatorCtx.eapPeerConfig;
212     krb5_error_code code;
213     char *identity;
214
215     GSSEAP_KRB_INIT(&krbContext);
216
217     eapPeerConfig->fragment_size = 1024;
218     wpa_debug_level = 0;
219
220     code = krb5_unparse_name(krbContext, cred->name->krbPrincipal, &identity);
221     if (code != 0) {
222         *minor = code;
223         return GSS_S_FAILURE;
224     }
225
226     eapPeerConfig->identity = (unsigned char *)identity;
227     eapPeerConfig->identity_len = strlen(identity);
228     eapPeerConfig->password = (unsigned char *)cred->password.value;
229     eapPeerConfig->password_len = cred->password.length;
230
231     return GSS_S_COMPLETE;
232 }
233
234 static OM_uint32
235 peerConfigFree(OM_uint32 *minor,
236                gss_ctx_id_t ctx)
237 {
238     krb5_context krbContext;
239     struct eap_peer_config *eapPeerConfig = &ctx->initiatorCtx.eapPeerConfig;
240
241     GSSEAP_KRB_INIT(&krbContext);
242
243     krb5_free_unparsed_name(krbContext, (char *)eapPeerConfig->identity);
244
245     return GSS_S_COMPLETE;
246 }
247
248 static OM_uint32
249 initReady(OM_uint32 *minor, gss_ctx_id_t ctx)
250 {
251     OM_uint32 major;
252     const unsigned char *key;
253     size_t keyLength;
254     krb5_enctype encryptionType;
255     int gotKey = 0;
256
257     /* Cache encryption type derived from selected mechanism OID */
258     major = gssEapOidToEnctype(minor, ctx->mechanismUsed, &encryptionType);
259     if (GSS_ERROR(major))
260         return major;
261
262     if (encryptionType != ENCTYPE_NULL &&
263         eap_key_available(ctx->initiatorCtx.eap)) {
264         key = eap_get_eapKeyData(ctx->initiatorCtx.eap, &keyLength);
265
266         if (keyLength >= EAP_EMSK_LEN) {
267             major = gssEapDeriveRfc3961Key(minor,
268                                            &key[EAP_EMSK_LEN / 2],
269                                            EAP_EMSK_LEN / 2,
270                                            encryptionType,
271                                            &ctx->rfc3961Key);
272                if (GSS_ERROR(major))
273                    return major;
274
275             major = rfc3961ChecksumTypeForKey(minor, &ctx->rfc3961Key,
276                                               &ctx->checksumType);
277             if (GSS_ERROR(major))
278                 return major;
279             gotKey++;
280         }
281     }
282
283     if (gotKey) {
284         ctx->encryptionType = encryptionType;
285     } else {
286         /*
287          * draft-howlett-eap-gss says that integrity/confidentialty should
288          * always be advertised as available, but if we have no keying
289          * material it seems confusing to the caller to advertise this.
290          */
291         ctx->gssFlags &= ~(GSS_C_INTEG_FLAG | GSS_C_CONF_FLAG);
292     }
293
294     major = sequenceInit(minor,
295                          &ctx->seqState,
296                          ctx->recvSeq,
297                          ((ctx->gssFlags & GSS_C_REPLAY_FLAG) != 0),
298                          ((ctx->gssFlags & GSS_C_SEQUENCE_FLAG) != 0),
299                          TRUE);
300     if (GSS_ERROR(major))
301         return major;
302
303     return GSS_S_COMPLETE;
304 }
305
306 static OM_uint32
307 initBegin(OM_uint32 *minor,
308           gss_cred_id_t cred,
309           gss_ctx_id_t ctx,
310           gss_name_t target,
311           gss_OID mech,
312           OM_uint32 reqFlags,
313           OM_uint32 timeReq,
314           gss_channel_bindings_t chanBindings,
315           gss_buffer_t inputToken,
316           gss_buffer_t outputToken)
317 {
318     OM_uint32 major;
319
320     if (cred != GSS_C_NO_CREDENTIAL && cred->expiryTime)
321         ctx->expiryTime = cred->expiryTime;
322     else if (timeReq == 0 || timeReq == GSS_C_INDEFINITE)
323         ctx->expiryTime = 0;
324     else
325         ctx->expiryTime = time(NULL) + timeReq;
326
327     if (cred != GSS_C_NO_CREDENTIAL) {
328         major = gssEapDuplicateName(minor, cred->name, &ctx->initiatorName);
329         if (GSS_ERROR(major))
330             return major;
331     }
332
333     major = gssEapDuplicateName(minor, target, &ctx->acceptorName);
334     if (GSS_ERROR(major))
335         return major;
336
337     if (mech == GSS_C_NULL_OID || oidEqual(mech, GSS_EAP_MECHANISM)) {
338         major = gssEapDefaultMech(minor, &ctx->mechanismUsed);
339     } else if (gssEapIsConcreteMechanismOid(mech)) {
340         if (!gssEapInternalizeOid(mech, &ctx->mechanismUsed))
341             major = duplicateOid(minor, mech, &ctx->mechanismUsed);
342     } else {
343         major = GSS_S_BAD_MECH;
344     }
345     if (GSS_ERROR(major))
346         return major;
347
348     /* If credentials were provided, check they're usable with this mech */
349     if (!gssEapCredAvailable(cred, ctx->mechanismUsed))
350         return GSS_S_BAD_MECH;
351
352     return GSS_S_COMPLETE;
353 }
354
355 static OM_uint32
356 eapGssSmInitIdentity(OM_uint32 *minor,
357                      gss_cred_id_t cred,
358                      gss_ctx_id_t ctx,
359                      gss_name_t target,
360                      gss_OID mech,
361                      OM_uint32 reqFlags,
362                      OM_uint32 timeReq,
363                      gss_channel_bindings_t chanBindings,
364                      gss_buffer_t inputToken,
365                      gss_buffer_t outputToken)
366 {
367     OM_uint32 major;
368     int initialContextToken;
369
370     initialContextToken = (inputToken->length == 0);
371     if (!initialContextToken)
372         return GSS_S_DEFECTIVE_TOKEN;
373
374     major = initBegin(minor, cred, ctx, target, mech,
375                       reqFlags, timeReq, chanBindings,
376                       inputToken, outputToken);
377     if (GSS_ERROR(major))
378         return major;
379
380     ctx->state = EAP_STATE_AUTHENTICATE;
381
382     return GSS_S_CONTINUE_NEEDED;
383 }
384
385 static struct wpabuf emptyWpaBuffer;
386
387 static OM_uint32
388 eapGssSmInitAuthenticate(OM_uint32 *minor,
389                          gss_cred_id_t cred,
390                          gss_ctx_id_t ctx,
391                          gss_name_t target,
392                          gss_OID mech,
393                          OM_uint32 reqFlags,
394                          OM_uint32 timeReq,
395                          gss_channel_bindings_t chanBindings,
396                          gss_buffer_t inputToken,
397                          gss_buffer_t outputToken)
398 {
399     OM_uint32 major;
400     OM_uint32 tmpMinor;
401     int code;
402     struct wpabuf *resp = NULL;
403     int initialContextToken;
404
405     initialContextToken = (inputToken == GSS_C_NO_BUFFER ||
406                            inputToken->length == 0);
407
408     major = peerConfigInit(minor, cred, ctx);
409     if (GSS_ERROR(major))
410         goto cleanup;
411
412     if (ctx->initiatorCtx.eap == NULL) {
413         struct eap_config eapConfig;
414
415         memset(&eapConfig, 0, sizeof(eapConfig));
416
417         ctx->initiatorCtx.eap = eap_peer_sm_init(ctx,
418                                                  &gssEapPolicyCallbacks,
419                                                  ctx,
420                                                  &eapConfig);
421         if (ctx->initiatorCtx.eap == NULL) {
422             major = GSS_S_FAILURE;
423             goto cleanup;
424         }
425
426         ctx->flags |= CTX_FLAG_EAP_RESTART | CTX_FLAG_EAP_PORT_ENABLED;
427     }
428
429     ctx->flags |= CTX_FLAG_EAP_REQ; /* we have a Request from the acceptor */
430
431     wpabuf_set(&ctx->initiatorCtx.reqData,
432                inputToken->value, inputToken->length);
433
434     major = GSS_S_CONTINUE_NEEDED;
435
436     code = eap_peer_sm_step(ctx->initiatorCtx.eap);
437     if (ctx->flags & CTX_FLAG_EAP_RESP) {
438         ctx->flags &= ~(CTX_FLAG_EAP_RESP);
439
440         resp = eap_get_eapRespData(ctx->initiatorCtx.eap);
441     } else if (ctx->flags & CTX_FLAG_EAP_SUCCESS) {
442         major = initReady(minor, ctx);
443         if (GSS_ERROR(major))
444             goto cleanup;
445
446         ctx->flags &= ~(CTX_FLAG_EAP_SUCCESS);
447         major = GSS_S_CONTINUE_NEEDED;
448         ctx->state = EAP_STATE_EXTENSIONS_REQ;
449     } else if (ctx->flags & CTX_FLAG_EAP_FAIL) {
450         major = GSS_S_DEFECTIVE_CREDENTIAL;
451     } else if (code == 0 && initialContextToken) {
452         resp = &emptyWpaBuffer;
453         major = GSS_S_CONTINUE_NEEDED;
454     } else {
455         major = GSS_S_DEFECTIVE_TOKEN;
456     }
457
458 cleanup:
459     if (resp != NULL) {
460         OM_uint32 tmpMajor;
461         gss_buffer_desc respBuf;
462
463         assert(major == GSS_S_CONTINUE_NEEDED);
464
465         respBuf.length = wpabuf_len(resp);
466         respBuf.value = (void *)wpabuf_head(resp);
467
468         tmpMajor = duplicateBuffer(&tmpMinor, &respBuf, outputToken);
469         if (GSS_ERROR(tmpMajor)) {
470             major = tmpMajor;
471             *minor = tmpMinor;
472         }
473     }
474
475     wpabuf_set(&ctx->initiatorCtx.reqData, NULL, 0);
476     peerConfigFree(&tmpMinor, ctx);
477
478     return major;
479 }
480
481 static OM_uint32
482 initGssChannelBindings(OM_uint32 *minor,
483                        gss_ctx_id_t ctx,
484                        gss_channel_bindings_t chanBindings,
485                        gss_buffer_t outputToken)
486 {
487     OM_uint32 major;
488     gss_buffer_desc buffer = GSS_C_EMPTY_BUFFER;
489
490
491     if (chanBindings != GSS_C_NO_CHANNEL_BINDINGS)
492         buffer = chanBindings->application_data;
493
494     major = gssEapWrap(minor, ctx, TRUE, GSS_C_QOP_DEFAULT,
495                        &buffer, NULL, outputToken);
496     if (GSS_ERROR(major))
497         return major;                       
498
499     return GSS_S_CONTINUE_NEEDED;
500 }
501
502 static OM_uint32
503 eapGssSmInitExtensionsReq(OM_uint32 *minor,
504                           gss_cred_id_t cred,
505                           gss_ctx_id_t ctx,
506                           gss_name_t target,
507                           gss_OID mech,
508                           OM_uint32 reqFlags,
509                           OM_uint32 timeReq,
510                           gss_channel_bindings_t chanBindings,
511                           gss_buffer_t inputToken,
512                           gss_buffer_t outputToken)
513 {
514     OM_uint32 major, tmpMinor;
515     gss_buffer_desc cbToken = GSS_C_EMPTY_BUFFER;
516
517     major = initGssChannelBindings(minor, ctx, chanBindings, &cbToken);
518     if (GSS_ERROR(major))
519         return major;
520
521     ctx->state = EAP_STATE_EXTENSIONS_RESP;
522
523     major = duplicateBuffer(minor, &cbToken, outputToken);
524     if (GSS_ERROR(major)) {
525         gss_release_buffer(&tmpMinor, &cbToken);
526         return major;
527     }
528
529     gss_release_buffer(&tmpMinor, &cbToken);
530
531     return GSS_S_CONTINUE_NEEDED;
532 }
533
534 static OM_uint32
535 eapGssSmInitExtensionsResp(OM_uint32 *minor,
536                            gss_cred_id_t cred,
537                            gss_ctx_id_t ctx,
538                            gss_name_t target,
539                            gss_OID mech,
540                            OM_uint32 reqFlags,
541                            OM_uint32 timeReq,
542                            gss_channel_bindings_t chanBindings,
543                            gss_buffer_t inputToken,
544                            gss_buffer_t outputToken)
545 {
546 #ifdef GSSEAP_ENABLE_REAUTH
547     OM_uint32 major;
548
549     major = gssEapStoreReauthCreds(minor, ctx, cred, inputToken);
550     if (GSS_ERROR(major))
551         return major;
552 #endif
553
554     ctx->state = EAP_STATE_ESTABLISHED;
555
556     return GSS_S_COMPLETE;
557 }
558
559 static OM_uint32
560 eapGssSmInitEstablished(OM_uint32 *minor,
561                         gss_cred_id_t cred,
562                         gss_ctx_id_t ctx,
563                         gss_name_t target,
564                         gss_OID mech,
565                         OM_uint32 reqFlags,
566                         OM_uint32 timeReq,
567                         gss_channel_bindings_t chanBindings,
568                         gss_buffer_t inputToken,
569                         gss_buffer_t outputToken)
570 {
571     /* Called with already established context */
572     *minor = EINVAL;
573     return GSS_S_BAD_STATUS;
574 }
575
576 static struct gss_eap_initiator_sm {
577     enum gss_eap_token_type inputTokenType;
578     enum gss_eap_token_type outputTokenType;
579     OM_uint32 (*processToken)(OM_uint32 *,
580                               gss_cred_id_t,
581                               gss_ctx_id_t,
582                               gss_name_t,
583                               gss_OID,
584                               OM_uint32,
585                               OM_uint32,
586                               gss_channel_bindings_t,
587                               gss_buffer_t,
588                               gss_buffer_t);
589 } eapGssInitiatorSm[] = {
590     { TOK_TYPE_NONE,    TOK_TYPE_EAP_RESP,      eapGssSmInitIdentity            },
591     { TOK_TYPE_EAP_REQ, TOK_TYPE_EAP_RESP,      eapGssSmInitAuthenticate        },
592     { TOK_TYPE_NONE,    TOK_TYPE_EXT_REQ,       eapGssSmInitExtensionsReq       },
593     { TOK_TYPE_EXT_RESP,TOK_TYPE_NONE,          eapGssSmInitExtensionsResp      },
594     { TOK_TYPE_NONE,    TOK_TYPE_NONE,          eapGssSmInitEstablished         },
595 #ifdef GSSEAP_ENABLE_REAUTH
596     { TOK_TYPE_GSS_REAUTH, TOK_TYPE_GSS_REAUTH, eapGssSmInitGssReauth           },
597 #endif
598 };
599
600 OM_uint32
601 gss_init_sec_context(OM_uint32 *minor,
602                      gss_cred_id_t cred,
603                      gss_ctx_id_t *context_handle,
604                      gss_name_t target_name,
605                      gss_OID mech_type,
606                      OM_uint32 req_flags,
607                      OM_uint32 time_req,
608                      gss_channel_bindings_t input_chan_bindings,
609                      gss_buffer_t input_token,
610                      gss_OID *actual_mech_type,
611                      gss_buffer_t output_token,
612                      OM_uint32 *ret_flags,
613                      OM_uint32 *time_rec)
614 {
615     OM_uint32 major;
616     OM_uint32 tmpMajor, tmpMinor;
617     gss_ctx_id_t ctx = *context_handle;
618     struct gss_eap_initiator_sm *sm = NULL;
619     gss_buffer_desc innerInputToken;
620     gss_buffer_desc innerOutputToken = GSS_C_EMPTY_BUFFER;
621     enum gss_eap_token_type tokType;
622
623     *minor = 0;
624
625     output_token->length = 0;
626     output_token->value = NULL;
627
628     if (cred != GSS_C_NO_CREDENTIAL && !(cred->flags & CRED_FLAG_INITIATE)) {
629         return GSS_S_NO_CRED;
630     }
631
632     if (ctx == GSS_C_NO_CONTEXT) {
633         if (input_token != GSS_C_NO_BUFFER && input_token->length != 0) {
634             return GSS_S_DEFECTIVE_TOKEN;
635         }
636
637         major = gssEapAllocContext(minor, &ctx);
638         if (GSS_ERROR(major))
639             return major;
640
641         ctx->flags |= CTX_FLAG_INITIATOR;
642
643 #ifdef GSSEAP_ENABLE_REAUTH
644         if (canReauthP(cred))
645             ctx->state = EAP_STATE_KRB_REAUTH_GSS;
646 #endif
647
648         *context_handle = ctx;
649     }
650
651     GSSEAP_MUTEX_LOCK(&ctx->mutex);
652
653     sm = &eapGssInitiatorSm[ctx->state];
654
655     if (input_token != GSS_C_NO_BUFFER) {
656         major = gssEapVerifyToken(minor, ctx, input_token,
657                                   &tokType, &innerInputToken);
658         if (GSS_ERROR(major))
659             goto cleanup;
660
661         if (tokType != sm->inputTokenType) {
662             major = GSS_S_DEFECTIVE_TOKEN;
663             goto cleanup;
664         }
665     } else {
666         innerInputToken.length = 0;
667         innerInputToken.value = NULL;
668     }
669
670     /*
671      * Advance through state machine whilst empty tokens are emitted and
672      * the status is not GSS_S_COMPLETE or an error status.
673      */
674     do {
675         sm = &eapGssInitiatorSm[ctx->state];
676
677         major = (sm->processToken)(minor,
678                                    cred,
679                                    ctx,
680                                    target_name,
681                                    mech_type,
682                                    req_flags,
683                                    time_req,
684                                    input_chan_bindings,
685                                    &innerInputToken,
686                                    &innerOutputToken);
687         if (GSS_ERROR(major))
688             goto cleanup;
689     } while (major == GSS_S_CONTINUE_NEEDED && innerOutputToken.value == NULL);
690
691     if (actual_mech_type != NULL) {
692         if (!gssEapInternalizeOid(ctx->mechanismUsed, actual_mech_type))
693             duplicateOid(&tmpMinor, ctx->mechanismUsed, actual_mech_type);
694     }
695     if (innerOutputToken.value != NULL) {
696         tmpMajor = gssEapMakeToken(&tmpMinor, ctx, &innerOutputToken,
697                                    sm->outputTokenType, output_token);
698         if (GSS_ERROR(tmpMajor)) {
699             major = tmpMajor;
700             *minor = tmpMinor;
701             goto cleanup;
702         }
703     }
704     if (ret_flags != NULL)
705         *ret_flags = ctx->gssFlags;
706     if (time_rec != NULL)
707         gssEapContextTime(&tmpMinor, ctx, time_rec);
708
709     assert(ctx->state == EAP_STATE_ESTABLISHED || major == GSS_S_CONTINUE_NEEDED);
710
711 cleanup:
712     GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
713
714     if (GSS_ERROR(major))
715         gssEapReleaseContext(&tmpMinor, context_handle);
716
717     gss_release_buffer(&tmpMinor, &innerOutputToken);
718
719     return major;
720 }
721
722 #ifdef GSSEAP_ENABLE_REAUTH
723 static int
724 canReauthP(gss_cred_id_t cred)
725 {
726     return (cred != GSS_C_NO_CREDENTIAL &&
727             cred->krbCred != GSS_C_NO_CREDENTIAL &&
728             cred->expiryTime > time(NULL));
729 }
730
731 static OM_uint32
732 eapGssSmInitGssReauth(OM_uint32 *minor,
733                       gss_cred_id_t cred,
734                       gss_ctx_id_t ctx,
735                       gss_name_t target,
736                       gss_OID mech,
737                       OM_uint32 reqFlags,
738                       OM_uint32 timeReq,
739                       gss_channel_bindings_t chanBindings,
740                       gss_buffer_t inputToken,
741                       gss_buffer_t outputToken)
742 {
743     OM_uint32 major, tmpMinor;
744     gss_name_t mechTarget = GSS_C_NO_NAME;
745     gss_OID actualMech = GSS_C_NO_OID;
746     OM_uint32 gssFlags, timeRec;
747
748     assert(cred != GSS_C_NO_CREDENTIAL);
749
750     ctx->flags |= CTX_FLAG_KRB_REAUTH_GSS;
751
752     if (inputToken->length == 0) {
753         major = initBegin(minor, cred, ctx, target, mech,
754                           reqFlags, timeReq, chanBindings,
755                           inputToken, outputToken);
756         if (GSS_ERROR(major))
757             goto cleanup;
758     }
759
760     major = gssEapMechToGlueName(minor, target, &mechTarget);
761     if (GSS_ERROR(major))
762         goto cleanup;
763
764     major = gssInitSecContext(minor,
765                               cred->krbCred,
766                               &ctx->kerberosCtx,
767                               mechTarget,
768                               (gss_OID)gss_mech_krb5,
769                               reqFlags, /* | GSS_C_DCE_STYLE, */
770                               timeReq,
771                               chanBindings,
772                               inputToken,
773                               &actualMech,
774                               outputToken,
775                               &gssFlags,
776                               &timeRec);
777     if (GSS_ERROR(major))
778         goto cleanup;
779
780     ctx->gssFlags = gssFlags;
781
782     if (major == GSS_S_COMPLETE) {
783         major = gssEapReauthComplete(minor, ctx, cred, actualMech, timeRec);
784         if (GSS_ERROR(major))
785             goto cleanup;
786         ctx->state = EAP_STATE_ESTABLISHED;
787     }
788
789 cleanup:
790     gssReleaseName(&tmpMinor, &mechTarget);
791
792     return major;
793 }
794 #endif /* GSSEAP_ENABLE_REAUTH */
795
796