d538556ce6e5ed2a1da51b4fec6d937a7d51485e
[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     eapPeerConfig->identity = NULL;
216     eapPeerConfig->identity_len = 0;
217     eapPeerConfig->password = NULL;
218     eapPeerConfig->password_len = 0;
219
220     if (cred == GSS_C_NO_CREDENTIAL)
221         return GSS_S_NO_CRED;
222
223     GSSEAP_KRB_INIT(&krbContext);
224
225     eapPeerConfig->fragment_size = 1024;
226     wpa_debug_level = 0;
227
228     code = krb5_unparse_name(krbContext, cred->name->krbPrincipal, &identity);
229     if (code != 0) {
230         *minor = code;
231         return GSS_S_FAILURE;
232     }
233
234     eapPeerConfig->identity = (unsigned char *)identity;
235     eapPeerConfig->identity_len = strlen(identity);
236     eapPeerConfig->password = (unsigned char *)cred->password.value;
237     eapPeerConfig->password_len = cred->password.length;
238
239     return GSS_S_COMPLETE;
240 }
241
242 static OM_uint32
243 peerConfigFree(OM_uint32 *minor,
244                gss_ctx_id_t ctx)
245 {
246     krb5_context krbContext;
247     struct eap_peer_config *eapPeerConfig = &ctx->initiatorCtx.eapPeerConfig;
248
249     GSSEAP_KRB_INIT(&krbContext);
250
251     krb5_free_unparsed_name(krbContext, (char *)eapPeerConfig->identity);
252
253     return GSS_S_COMPLETE;
254 }
255
256 static OM_uint32
257 initReady(OM_uint32 *minor, gss_ctx_id_t ctx)
258 {
259     OM_uint32 major;
260     const unsigned char *key;
261     size_t keyLength;
262     krb5_enctype encryptionType;
263     int gotKey = 0;
264
265     /* Cache encryption type derived from selected mechanism OID */
266     major = gssEapOidToEnctype(minor, ctx->mechanismUsed, &encryptionType);
267     if (GSS_ERROR(major))
268         return major;
269
270     if (encryptionType != ENCTYPE_NULL &&
271         eap_key_available(ctx->initiatorCtx.eap)) {
272         key = eap_get_eapKeyData(ctx->initiatorCtx.eap, &keyLength);
273
274         if (keyLength >= EAP_EMSK_LEN) {
275             major = gssEapDeriveRfc3961Key(minor,
276                                            &key[EAP_EMSK_LEN / 2],
277                                            EAP_EMSK_LEN / 2,
278                                            encryptionType,
279                                            &ctx->rfc3961Key);
280                if (GSS_ERROR(major))
281                    return major;
282
283             major = rfc3961ChecksumTypeForKey(minor, &ctx->rfc3961Key,
284                                               &ctx->checksumType);
285             if (GSS_ERROR(major))
286                 return major;
287             gotKey++;
288         }
289     }
290
291     if (gotKey) {
292         ctx->encryptionType = encryptionType;
293     } else {
294         /*
295          * draft-howlett-eap-gss says that integrity/confidentialty should
296          * always be advertised as available, but if we have no keying
297          * material it seems confusing to the caller to advertise this.
298          */
299         ctx->gssFlags &= ~(GSS_C_INTEG_FLAG | GSS_C_CONF_FLAG);
300     }
301
302     major = sequenceInit(minor,
303                          &ctx->seqState,
304                          ctx->recvSeq,
305                          ((ctx->gssFlags & GSS_C_REPLAY_FLAG) != 0),
306                          ((ctx->gssFlags & GSS_C_SEQUENCE_FLAG) != 0),
307                          TRUE);
308     if (GSS_ERROR(major))
309         return major;
310
311     return GSS_S_COMPLETE;
312 }
313
314 static OM_uint32
315 initBegin(OM_uint32 *minor,
316           gss_cred_id_t cred,
317           gss_ctx_id_t ctx,
318           gss_name_t target,
319           gss_OID mech,
320           OM_uint32 reqFlags,
321           OM_uint32 timeReq,
322           gss_channel_bindings_t chanBindings,
323           gss_buffer_t inputToken,
324           gss_buffer_t outputToken)
325 {
326     OM_uint32 major;
327
328     if (cred != GSS_C_NO_CREDENTIAL && cred->expiryTime)
329         ctx->expiryTime = cred->expiryTime;
330     else if (timeReq == 0 || timeReq == GSS_C_INDEFINITE)
331         ctx->expiryTime = 0;
332     else
333         ctx->expiryTime = time(NULL) + timeReq;
334
335     if (cred != GSS_C_NO_CREDENTIAL) {
336         major = gssEapDuplicateName(minor, cred->name, &ctx->initiatorName);
337         if (GSS_ERROR(major))
338             return major;
339     }
340
341     major = gssEapDuplicateName(minor, target, &ctx->acceptorName);
342     if (GSS_ERROR(major))
343         return major;
344
345     if (mech == GSS_C_NULL_OID || oidEqual(mech, GSS_EAP_MECHANISM)) {
346         major = gssEapDefaultMech(minor, &ctx->mechanismUsed);
347     } else if (gssEapIsConcreteMechanismOid(mech)) {
348         if (!gssEapInternalizeOid(mech, &ctx->mechanismUsed))
349             major = duplicateOid(minor, mech, &ctx->mechanismUsed);
350     } else {
351         major = GSS_S_BAD_MECH;
352     }
353     if (GSS_ERROR(major))
354         return major;
355
356     /* If credentials were provided, check they're usable with this mech */
357     if (!gssEapCredAvailable(cred, ctx->mechanismUsed))
358         return GSS_S_BAD_MECH;
359
360     return GSS_S_COMPLETE;
361 }
362
363 static OM_uint32
364 eapGssSmInitIdentity(OM_uint32 *minor,
365                      gss_cred_id_t cred,
366                      gss_ctx_id_t ctx,
367                      gss_name_t target,
368                      gss_OID mech,
369                      OM_uint32 reqFlags,
370                      OM_uint32 timeReq,
371                      gss_channel_bindings_t chanBindings,
372                      gss_buffer_t inputToken,
373                      gss_buffer_t outputToken)
374 {
375     OM_uint32 major;
376     int initialContextToken;
377
378     initialContextToken = (inputToken->length == 0);
379     if (!initialContextToken)
380         return GSS_S_DEFECTIVE_TOKEN;
381
382     major = initBegin(minor, cred, ctx, target, mech,
383                       reqFlags, timeReq, chanBindings,
384                       inputToken, outputToken);
385     if (GSS_ERROR(major))
386         return major;
387
388     ctx->state = EAP_STATE_AUTHENTICATE;
389
390     return GSS_S_CONTINUE_NEEDED;
391 }
392
393 static struct wpabuf emptyWpaBuffer;
394
395 static OM_uint32
396 eapGssSmInitAuthenticate(OM_uint32 *minor,
397                          gss_cred_id_t cred,
398                          gss_ctx_id_t ctx,
399                          gss_name_t target,
400                          gss_OID mech,
401                          OM_uint32 reqFlags,
402                          OM_uint32 timeReq,
403                          gss_channel_bindings_t chanBindings,
404                          gss_buffer_t inputToken,
405                          gss_buffer_t outputToken)
406 {
407     OM_uint32 major;
408     OM_uint32 tmpMinor;
409     int code;
410     struct wpabuf *resp = NULL;
411     int initialContextToken;
412
413     initialContextToken = (inputToken == GSS_C_NO_BUFFER ||
414                            inputToken->length == 0);
415
416     major = peerConfigInit(minor, cred, ctx);
417     if (GSS_ERROR(major))
418         goto cleanup;
419
420     if (ctx->initiatorCtx.eap == NULL) {
421         struct eap_config eapConfig;
422
423         memset(&eapConfig, 0, sizeof(eapConfig));
424
425         ctx->initiatorCtx.eap = eap_peer_sm_init(ctx,
426                                                  &gssEapPolicyCallbacks,
427                                                  ctx,
428                                                  &eapConfig);
429         if (ctx->initiatorCtx.eap == NULL) {
430             major = GSS_S_FAILURE;
431             goto cleanup;
432         }
433
434         ctx->flags |= CTX_FLAG_EAP_RESTART | CTX_FLAG_EAP_PORT_ENABLED;
435     }
436
437     ctx->flags |= CTX_FLAG_EAP_REQ; /* we have a Request from the acceptor */
438
439     wpabuf_set(&ctx->initiatorCtx.reqData,
440                inputToken->value, inputToken->length);
441
442     major = GSS_S_CONTINUE_NEEDED;
443
444     code = eap_peer_sm_step(ctx->initiatorCtx.eap);
445     if (ctx->flags & CTX_FLAG_EAP_RESP) {
446         ctx->flags &= ~(CTX_FLAG_EAP_RESP);
447
448         resp = eap_get_eapRespData(ctx->initiatorCtx.eap);
449     } else if (ctx->flags & CTX_FLAG_EAP_SUCCESS) {
450         major = initReady(minor, ctx);
451         if (GSS_ERROR(major))
452             goto cleanup;
453
454         ctx->flags &= ~(CTX_FLAG_EAP_SUCCESS);
455         major = GSS_S_CONTINUE_NEEDED;
456         ctx->state = EAP_STATE_EXTENSIONS_REQ;
457     } else if (ctx->flags & CTX_FLAG_EAP_FAIL) {
458         major = GSS_S_DEFECTIVE_CREDENTIAL;
459     } else if (code == 0 && initialContextToken) {
460         resp = &emptyWpaBuffer;
461         major = GSS_S_CONTINUE_NEEDED;
462     } else {
463         major = GSS_S_DEFECTIVE_TOKEN;
464     }
465
466 cleanup:
467     if (resp != NULL) {
468         OM_uint32 tmpMajor;
469         gss_buffer_desc respBuf;
470
471         assert(major == GSS_S_CONTINUE_NEEDED);
472
473         respBuf.length = wpabuf_len(resp);
474         respBuf.value = (void *)wpabuf_head(resp);
475
476         tmpMajor = duplicateBuffer(&tmpMinor, &respBuf, outputToken);
477         if (GSS_ERROR(tmpMajor)) {
478             major = tmpMajor;
479             *minor = tmpMinor;
480         }
481     }
482
483     wpabuf_set(&ctx->initiatorCtx.reqData, NULL, 0);
484     peerConfigFree(&tmpMinor, ctx);
485
486     return major;
487 }
488
489 static OM_uint32
490 initGssChannelBindings(OM_uint32 *minor,
491                        gss_ctx_id_t ctx,
492                        gss_channel_bindings_t chanBindings,
493                        gss_buffer_t outputToken)
494 {
495     OM_uint32 major;
496     gss_buffer_desc buffer = GSS_C_EMPTY_BUFFER;
497
498
499     if (chanBindings != GSS_C_NO_CHANNEL_BINDINGS)
500         buffer = chanBindings->application_data;
501
502     major = gssEapWrap(minor, ctx, TRUE, GSS_C_QOP_DEFAULT,
503                        &buffer, NULL, outputToken);
504     if (GSS_ERROR(major))
505         return major;                       
506
507     return GSS_S_CONTINUE_NEEDED;
508 }
509
510 static OM_uint32
511 eapGssSmInitExtensionsReq(OM_uint32 *minor,
512                           gss_cred_id_t cred,
513                           gss_ctx_id_t ctx,
514                           gss_name_t target,
515                           gss_OID mech,
516                           OM_uint32 reqFlags,
517                           OM_uint32 timeReq,
518                           gss_channel_bindings_t chanBindings,
519                           gss_buffer_t inputToken,
520                           gss_buffer_t outputToken)
521 {
522     OM_uint32 major, tmpMinor;
523     gss_buffer_desc cbToken = GSS_C_EMPTY_BUFFER;
524
525     major = initGssChannelBindings(minor, ctx, chanBindings, &cbToken);
526     if (GSS_ERROR(major))
527         return major;
528
529     ctx->state = EAP_STATE_EXTENSIONS_RESP;
530
531     major = duplicateBuffer(minor, &cbToken, outputToken);
532     if (GSS_ERROR(major)) {
533         gss_release_buffer(&tmpMinor, &cbToken);
534         return major;
535     }
536
537     gss_release_buffer(&tmpMinor, &cbToken);
538
539     return GSS_S_CONTINUE_NEEDED;
540 }
541
542 static OM_uint32
543 eapGssSmInitExtensionsResp(OM_uint32 *minor,
544                            gss_cred_id_t cred,
545                            gss_ctx_id_t ctx,
546                            gss_name_t target,
547                            gss_OID mech,
548                            OM_uint32 reqFlags,
549                            OM_uint32 timeReq,
550                            gss_channel_bindings_t chanBindings,
551                            gss_buffer_t inputToken,
552                            gss_buffer_t outputToken)
553 {
554 #ifdef GSSEAP_ENABLE_REAUTH
555     OM_uint32 major;
556
557     major = gssEapStoreReauthCreds(minor, ctx, cred, inputToken);
558     if (GSS_ERROR(major))
559         return major;
560 #endif
561
562     ctx->state = EAP_STATE_ESTABLISHED;
563
564     return GSS_S_COMPLETE;
565 }
566
567 static OM_uint32
568 eapGssSmInitEstablished(OM_uint32 *minor,
569                         gss_cred_id_t cred,
570                         gss_ctx_id_t ctx,
571                         gss_name_t target,
572                         gss_OID mech,
573                         OM_uint32 reqFlags,
574                         OM_uint32 timeReq,
575                         gss_channel_bindings_t chanBindings,
576                         gss_buffer_t inputToken,
577                         gss_buffer_t outputToken)
578 {
579     /* Called with already established context */
580     *minor = EINVAL;
581     return GSS_S_BAD_STATUS;
582 }
583
584 static struct gss_eap_initiator_sm {
585     enum gss_eap_token_type inputTokenType;
586     enum gss_eap_token_type outputTokenType;
587     OM_uint32 (*processToken)(OM_uint32 *,
588                               gss_cred_id_t,
589                               gss_ctx_id_t,
590                               gss_name_t,
591                               gss_OID,
592                               OM_uint32,
593                               OM_uint32,
594                               gss_channel_bindings_t,
595                               gss_buffer_t,
596                               gss_buffer_t);
597 } eapGssInitiatorSm[] = {
598     { TOK_TYPE_NONE,    TOK_TYPE_EAP_RESP,      eapGssSmInitIdentity            },
599     { TOK_TYPE_EAP_REQ, TOK_TYPE_EAP_RESP,      eapGssSmInitAuthenticate        },
600     { TOK_TYPE_NONE,    TOK_TYPE_EXT_REQ,       eapGssSmInitExtensionsReq       },
601     { TOK_TYPE_EXT_RESP,TOK_TYPE_NONE,          eapGssSmInitExtensionsResp      },
602     { TOK_TYPE_NONE,    TOK_TYPE_NONE,          eapGssSmInitEstablished         },
603 #ifdef GSSEAP_ENABLE_REAUTH
604     { TOK_TYPE_GSS_REAUTH, TOK_TYPE_GSS_REAUTH, eapGssSmInitGssReauth           },
605 #endif
606 };
607
608 OM_uint32
609 gss_init_sec_context(OM_uint32 *minor,
610                      gss_cred_id_t cred,
611                      gss_ctx_id_t *context_handle,
612                      gss_name_t target_name,
613                      gss_OID mech_type,
614                      OM_uint32 req_flags,
615                      OM_uint32 time_req,
616                      gss_channel_bindings_t input_chan_bindings,
617                      gss_buffer_t input_token,
618                      gss_OID *actual_mech_type,
619                      gss_buffer_t output_token,
620                      OM_uint32 *ret_flags,
621                      OM_uint32 *time_rec)
622 {
623     OM_uint32 major;
624     OM_uint32 tmpMajor, tmpMinor;
625     gss_ctx_id_t ctx = *context_handle;
626     struct gss_eap_initiator_sm *sm = NULL;
627     gss_buffer_desc innerInputToken;
628     gss_buffer_desc innerOutputToken = GSS_C_EMPTY_BUFFER;
629     enum gss_eap_token_type tokType;
630
631     *minor = 0;
632
633     output_token->length = 0;
634     output_token->value = NULL;
635
636     if (cred != GSS_C_NO_CREDENTIAL && !(cred->flags & CRED_FLAG_INITIATE)) {
637         return GSS_S_NO_CRED;
638     }
639
640     if (ctx == GSS_C_NO_CONTEXT) {
641         if (input_token != GSS_C_NO_BUFFER && input_token->length != 0) {
642             return GSS_S_DEFECTIVE_TOKEN;
643         }
644
645         major = gssEapAllocContext(minor, &ctx);
646         if (GSS_ERROR(major))
647             return major;
648
649         ctx->flags |= CTX_FLAG_INITIATOR;
650
651 #ifdef GSSEAP_ENABLE_REAUTH
652         if (canReauthP(cred))
653             ctx->state = EAP_STATE_KRB_REAUTH_GSS;
654 #endif
655
656         *context_handle = ctx;
657     }
658
659     GSSEAP_MUTEX_LOCK(&ctx->mutex);
660
661     sm = &eapGssInitiatorSm[ctx->state];
662
663     if (input_token != GSS_C_NO_BUFFER) {
664         major = gssEapVerifyToken(minor, ctx, input_token,
665                                   &tokType, &innerInputToken);
666         if (GSS_ERROR(major))
667             goto cleanup;
668
669         if (tokType != sm->inputTokenType) {
670             major = GSS_S_DEFECTIVE_TOKEN;
671             goto cleanup;
672         }
673     } else {
674         innerInputToken.length = 0;
675         innerInputToken.value = NULL;
676     }
677
678     /*
679      * Advance through state machine whilst empty tokens are emitted and
680      * the status is not GSS_S_COMPLETE or an error status.
681      */
682     do {
683         sm = &eapGssInitiatorSm[ctx->state];
684
685         major = (sm->processToken)(minor,
686                                    cred,
687                                    ctx,
688                                    target_name,
689                                    mech_type,
690                                    req_flags,
691                                    time_req,
692                                    input_chan_bindings,
693                                    &innerInputToken,
694                                    &innerOutputToken);
695         if (GSS_ERROR(major))
696             goto cleanup;
697     } while (major == GSS_S_CONTINUE_NEEDED && innerOutputToken.value == NULL);
698
699     if (actual_mech_type != NULL) {
700         if (!gssEapInternalizeOid(ctx->mechanismUsed, actual_mech_type))
701             duplicateOid(&tmpMinor, ctx->mechanismUsed, actual_mech_type);
702     }
703     if (innerOutputToken.value != NULL) {
704         tmpMajor = gssEapMakeToken(&tmpMinor, ctx, &innerOutputToken,
705                                    sm->outputTokenType, output_token);
706         if (GSS_ERROR(tmpMajor)) {
707             major = tmpMajor;
708             *minor = tmpMinor;
709             goto cleanup;
710         }
711     }
712     if (ret_flags != NULL)
713         *ret_flags = ctx->gssFlags;
714     if (time_rec != NULL)
715         gssEapContextTime(&tmpMinor, ctx, time_rec);
716
717     assert(ctx->state == EAP_STATE_ESTABLISHED || major == GSS_S_CONTINUE_NEEDED);
718
719 cleanup:
720     GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
721
722     if (GSS_ERROR(major))
723         gssEapReleaseContext(&tmpMinor, context_handle);
724
725     gss_release_buffer(&tmpMinor, &innerOutputToken);
726
727     return major;
728 }
729
730 #ifdef GSSEAP_ENABLE_REAUTH
731 static int
732 canReauthP(gss_cred_id_t cred)
733 {
734     return (cred != GSS_C_NO_CREDENTIAL &&
735             cred->krbCred != GSS_C_NO_CREDENTIAL &&
736             cred->expiryTime > time(NULL));
737 }
738
739 static OM_uint32
740 eapGssSmInitGssReauth(OM_uint32 *minor,
741                       gss_cred_id_t cred,
742                       gss_ctx_id_t ctx,
743                       gss_name_t target,
744                       gss_OID mech,
745                       OM_uint32 reqFlags,
746                       OM_uint32 timeReq,
747                       gss_channel_bindings_t chanBindings,
748                       gss_buffer_t inputToken,
749                       gss_buffer_t outputToken)
750 {
751     OM_uint32 major, tmpMinor;
752     gss_name_t mechTarget = GSS_C_NO_NAME;
753     gss_OID actualMech = GSS_C_NO_OID;
754     OM_uint32 gssFlags, timeRec;
755
756     assert(cred != GSS_C_NO_CREDENTIAL);
757
758     ctx->flags |= CTX_FLAG_KRB_REAUTH_GSS;
759
760     if (inputToken->length == 0) {
761         major = initBegin(minor, cred, ctx, target, mech,
762                           reqFlags, timeReq, chanBindings,
763                           inputToken, outputToken);
764         if (GSS_ERROR(major))
765             goto cleanup;
766     }
767
768     major = gssEapMechToGlueName(minor, target, &mechTarget);
769     if (GSS_ERROR(major))
770         goto cleanup;
771
772     major = gssInitSecContext(minor,
773                               cred->krbCred,
774                               &ctx->kerberosCtx,
775                               mechTarget,
776                               (gss_OID)gss_mech_krb5,
777                               reqFlags, /* | GSS_C_DCE_STYLE, */
778                               timeReq,
779                               chanBindings,
780                               inputToken,
781                               &actualMech,
782                               outputToken,
783                               &gssFlags,
784                               &timeRec);
785     if (GSS_ERROR(major))
786         goto cleanup;
787
788     ctx->gssFlags = gssFlags;
789
790     if (major == GSS_S_COMPLETE) {
791         major = gssEapReauthComplete(minor, ctx, cred, actualMech, timeRec);
792         if (GSS_ERROR(major))
793             goto cleanup;
794         ctx->state = EAP_STATE_ESTABLISHED;
795     }
796
797 cleanup:
798     gssReleaseName(&tmpMinor, &mechTarget);
799
800     return major;
801 }
802 #endif /* GSSEAP_ENABLE_REAUTH */
803
804