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