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