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