initial TLV refactor
[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 {
396     OM_uint32 major;
397     unsigned char *p;
398
399     if (inputToken->length < 8) {
400         *minor = GSSEAP_TOK_TRUNC;
401         return GSS_S_DEFECTIVE_TOKEN;
402     }
403
404     p = (unsigned char *)inputToken->value;
405
406     major = load_uint32_be(&p[0]);
407     *minor = ERROR_TABLE_BASE_eapg + load_uint32_be(&p[4]);
408
409     if (!GSS_ERROR(major) || !IS_WIRE_ERROR(*minor)) {
410         major = GSS_S_FAILURE;
411         *minor = GSSEAP_BAD_ERROR_TOKEN;
412     }
413
414     assert(GSS_ERROR(major));
415
416     return major;
417 }
418
419 #ifdef GSSEAP_ENABLE_REAUTH
420 static OM_uint32
421 eapGssSmInitGssReauth(OM_uint32 *minor,
422                       gss_cred_id_t cred,
423                       gss_ctx_id_t ctx,
424                       gss_name_t target,
425                       gss_OID mech,
426                       OM_uint32 reqFlags,
427                       OM_uint32 timeReq,
428                       gss_channel_bindings_t chanBindings,
429                       gss_buffer_t inputToken,
430                       gss_buffer_t outputToken)
431 {
432     OM_uint32 major, tmpMinor;
433     gss_name_t mechTarget = GSS_C_NO_NAME;
434     gss_OID actualMech = GSS_C_NO_OID;
435     OM_uint32 gssFlags, timeRec;
436
437     assert(cred != GSS_C_NO_CREDENTIAL);
438
439     if (ctx->state == GSSEAP_STATE_INITIAL) {
440         if (!gssEapCanReauthP(cred, target, timeReq))
441             return GSS_S_CONTINUE_NEEDED;
442
443         major = initBegin(minor, cred, ctx, target, mech,
444                           reqFlags, timeReq, chanBindings,
445                           inputToken);
446         if (GSS_ERROR(major))
447             goto cleanup;
448
449         ctx->flags |= CTX_FLAG_KRB_REAUTH;
450     } else if ((ctx->flags & CTX_FLAG_KRB_REAUTH) == 0) {
451         major = GSS_S_DEFECTIVE_TOKEN;
452         *minor = GSSEAP_WRONG_ITOK;
453         goto cleanup;
454     }
455
456     major = gssEapMechToGlueName(minor, target, &mechTarget);
457     if (GSS_ERROR(major))
458         goto cleanup;
459
460     major = gssInitSecContext(minor,
461                               cred->krbCred,
462                               &ctx->kerberosCtx,
463                               mechTarget,
464                               (gss_OID)gss_mech_krb5,
465                               reqFlags,
466                               timeReq,
467                               chanBindings,
468                               inputToken,
469                               &actualMech,
470                               outputToken,
471                               &gssFlags,
472                               &timeRec);
473     if (GSS_ERROR(major))
474         goto cleanup;
475
476     ctx->gssFlags = gssFlags;
477
478     if (major == GSS_S_COMPLETE) {
479         major = gssEapReauthComplete(minor, ctx, cred, actualMech, timeRec);
480         if (GSS_ERROR(major))
481             goto cleanup;
482         ctx->state = GSSEAP_STATE_NEGO_EXT; /* skip */
483     } else {
484         major = GSS_S_COMPLETE; /* advance state */
485     }
486
487 cleanup:
488     gssReleaseName(&tmpMinor, &mechTarget);
489
490     return major;
491 }
492 #endif /* GSSEAP_ENABLE_REAUTH */
493
494 static OM_uint32
495 eapGssSmInitIdentity(OM_uint32 *minor,
496                      gss_cred_id_t cred,
497                      gss_ctx_id_t ctx,
498                      gss_name_t target,
499                      gss_OID mech,
500                      OM_uint32 reqFlags,
501                      OM_uint32 timeReq,
502                      gss_channel_bindings_t chanBindings,
503                      gss_buffer_t inputToken,
504                      gss_buffer_t outputToken)
505 {
506     OM_uint32 major;
507     int initialContextToken;
508
509     assert((ctx->flags & CTX_FLAG_KRB_REAUTH) == 0);
510
511     initialContextToken = (inputToken->length == 0);
512     if (!initialContextToken) {
513         *minor = GSSEAP_WRONG_SIZE;
514         return GSS_S_DEFECTIVE_TOKEN;
515     }
516
517     major = initBegin(minor, cred, ctx, target, mech,
518                       reqFlags, timeReq, chanBindings,
519                       inputToken);
520     if (GSS_ERROR(major))
521         return major;
522
523     outputToken->length = 0;
524     outputToken->value = NULL;
525
526     *minor = 0;
527     return GSS_S_COMPLETE; /* advance state */
528 }
529
530 static struct wpabuf emptyWpaBuffer;
531
532 static OM_uint32
533 eapGssSmInitAuthenticate(OM_uint32 *minor,
534                          gss_cred_id_t cred,
535                          gss_ctx_id_t ctx,
536                          gss_name_t target,
537                          gss_OID mech,
538                          OM_uint32 reqFlags,
539                          OM_uint32 timeReq,
540                          gss_channel_bindings_t chanBindings,
541                          gss_buffer_t inputToken,
542                          gss_buffer_t outputToken)
543 {
544     OM_uint32 major;
545     OM_uint32 tmpMinor;
546     int code;
547     struct wpabuf *resp = NULL;
548     int initialContextToken;
549
550     *minor = 0;
551
552     initialContextToken = (inputToken == GSS_C_NO_BUFFER ||
553                            inputToken->length == 0);
554
555     major = peerConfigInit(minor, cred, ctx);
556     if (GSS_ERROR(major))
557         goto cleanup;
558
559     if (ctx->initiatorCtx.eap == NULL) {
560         struct eap_config eapConfig;
561
562         memset(&eapConfig, 0, sizeof(eapConfig));
563
564         ctx->initiatorCtx.eap = eap_peer_sm_init(ctx,
565                                                  &gssEapPolicyCallbacks,
566                                                  ctx,
567                                                  &eapConfig);
568         if (ctx->initiatorCtx.eap == NULL) {
569             major = GSS_S_FAILURE;
570             *minor = GSSEAP_PEER_SM_INIT_FAILURE;
571             goto cleanup;
572         }
573
574         ctx->flags |= CTX_FLAG_EAP_RESTART | CTX_FLAG_EAP_PORT_ENABLED;
575     }
576
577     ctx->flags |= CTX_FLAG_EAP_REQ; /* we have a Request from the acceptor */
578
579     wpabuf_set(&ctx->initiatorCtx.reqData,
580                inputToken->value, inputToken->length);
581
582     code = eap_peer_sm_step(ctx->initiatorCtx.eap);
583     if (ctx->flags & CTX_FLAG_EAP_RESP) {
584         ctx->flags &= ~(CTX_FLAG_EAP_RESP);
585
586         resp = eap_get_eapRespData(ctx->initiatorCtx.eap);
587         major = GSS_S_CONTINUE_NEEDED;
588     } else if (ctx->flags & CTX_FLAG_EAP_SUCCESS) {
589         major = initReady(minor, ctx, reqFlags);
590         if (GSS_ERROR(major))
591             goto cleanup;
592
593         ctx->flags &= ~(CTX_FLAG_EAP_SUCCESS);
594         major = GSS_S_COMPLETE; /* advance state */
595     } else if (ctx->flags & CTX_FLAG_EAP_FAIL) {
596         major = GSS_S_DEFECTIVE_CREDENTIAL;
597         *minor = GSSEAP_PEER_AUTH_FAILURE;
598     } else if (code == 0 && initialContextToken) {
599         major = GSS_S_CONTINUE_NEEDED;
600     } else {
601         major = GSS_S_DEFECTIVE_TOKEN;
602         *minor = GSSEAP_PEER_BAD_MESSAGE;
603     }
604
605 cleanup:
606     if (resp != NULL) {
607         OM_uint32 tmpMajor;
608         gss_buffer_desc respBuf;
609
610         assert(!GSS_ERROR(major));
611
612         respBuf.length = wpabuf_len(resp);
613         respBuf.value = (void *)wpabuf_head(resp);
614
615         tmpMajor = duplicateBuffer(&tmpMinor, &respBuf, outputToken);
616         if (GSS_ERROR(tmpMajor)) {
617             major = tmpMajor;
618             *minor = tmpMinor;
619         }
620     }
621
622     wpabuf_set(&ctx->initiatorCtx.reqData, NULL, 0);
623     peerConfigFree(&tmpMinor, ctx);
624
625     return major;
626 }
627
628 static OM_uint32
629 eapGssSmInitGssChannelBindings(OM_uint32 *minor,
630                                gss_cred_id_t cred,
631                                gss_ctx_id_t ctx,
632                                gss_name_t target,
633                                gss_OID mech,
634                                OM_uint32 reqFlags,
635                                OM_uint32 timeReq,
636                                gss_channel_bindings_t chanBindings,
637                                gss_buffer_t inputToken,
638                                gss_buffer_t outputToken)
639 {
640     OM_uint32 major;
641     gss_buffer_desc buffer = GSS_C_EMPTY_BUFFER;
642
643     if (chanBindings != GSS_C_NO_CHANNEL_BINDINGS)
644         buffer = chanBindings->application_data;
645
646     major = gssEapWrap(minor, ctx, TRUE, GSS_C_QOP_DEFAULT,
647                        &buffer, NULL, outputToken);
648     if (GSS_ERROR(major))
649         return major;
650
651     assert(outputToken->value != NULL);
652
653     *minor = 0;
654     return GSS_S_CONTINUE_NEEDED;
655 }
656
657 #ifdef GSSEAP_ENABLE_REAUTH
658 static OM_uint32
659 eapGssSmInitReauthCreds(OM_uint32 *minor,
660                         gss_cred_id_t cred,
661                         gss_ctx_id_t ctx,
662                         gss_name_t target,
663                         gss_OID mech,
664                         OM_uint32 reqFlags,
665                         OM_uint32 timeReq,
666                         gss_channel_bindings_t chanBindings,
667                         gss_buffer_t inputToken,
668                         gss_buffer_t outputToken)
669 {
670     OM_uint32 major;
671
672     major = gssEapStoreReauthCreds(minor, ctx, cred, inputToken);
673     if (GSS_ERROR(major))
674         return major;
675
676     *minor = 0;
677     return GSS_S_CONTINUE_NEEDED;
678 }
679 #endif /* GSSEAP_ENABLE_REAUTH */
680
681 static OM_uint32
682 eapGssSmInitNegoExtFinished(OM_uint32 *minor,
683                             gss_cred_id_t cred,
684                             gss_ctx_id_t ctx,
685                             gss_name_t target,
686                             gss_OID mech,
687                             OM_uint32 reqFlags,
688                             OM_uint32 timeReq,
689                             gss_channel_bindings_t chanBindings,
690                             gss_buffer_t inputToken,
691                             gss_buffer_t outputToken)
692 {
693     *minor = 0;
694     return GSS_S_COMPLETE; /* advance state */
695 }
696
697 static struct gss_eap_sm eapGssInitiatorSm[] = {
698     {
699         ITOK_TYPE_CONTEXT_ERR,
700         ITOK_TYPE_NONE,
701         GSSEAP_STATE_ALL,
702         1, /* critical */
703         0, /* required */
704         eapGssSmInitError,
705     },
706 #ifdef GSSEAP_ENABLE_REAUTH
707     {
708         ITOK_TYPE_REAUTH_RESP,
709         ITOK_TYPE_REAUTH_REQ,
710         GSSEAP_STATE_INITIAL | GSSEAP_STATE_AUTHENTICATE,
711         0, /* critical */
712         0, /* required */
713         eapGssSmInitGssReauth,
714     },
715 #endif
716     /* first-leg extensions go here, they should return GSS_S_CONTINUE_NEEDED */
717     {
718         ITOK_TYPE_NONE,
719         ITOK_TYPE_NONE,
720         GSSEAP_STATE_INITIAL,
721         1, /* critical */
722         1, /* required */
723         eapGssSmInitIdentity,
724     },
725     {
726         ITOK_TYPE_EAP_REQ,
727         ITOK_TYPE_EAP_RESP,
728         GSSEAP_STATE_AUTHENTICATE,
729         1, /* critical */
730         1, /* required */
731         eapGssSmInitAuthenticate,
732     },
733     {
734         ITOK_TYPE_NONE,
735         ITOK_TYPE_GSS_CHANNEL_BINDINGS,
736         GSSEAP_STATE_NEGO_EXT,
737         1, /* critical */
738         1, /* required */
739         eapGssSmInitGssChannelBindings,
740     },
741 #ifdef GSSEAP_ENABLE_REAUTH
742     {
743         ITOK_TYPE_REAUTH_CREDS,
744         ITOK_TYPE_NONE,
745         GSSEAP_STATE_NEGO_EXT,
746         0, /* critical */
747         0, /* required */
748         eapGssSmInitReauthCreds,
749     },
750 #endif
751     /* other extensions go here */
752     {
753         ITOK_TYPE_NONE,
754         ITOK_TYPE_NONE,
755         GSSEAP_STATE_NEGO_EXT,
756         1, /* critical */
757         1, /* required */
758         eapGssSmInitNegoExtFinished
759     }
760 };
761
762 OM_uint32
763 gss_init_sec_context(OM_uint32 *minor,
764                      gss_cred_id_t cred,
765                      gss_ctx_id_t *context_handle,
766                      gss_name_t target_name,
767                      gss_OID mech_type,
768                      OM_uint32 req_flags,
769                      OM_uint32 time_req,
770                      gss_channel_bindings_t input_chan_bindings,
771                      gss_buffer_t input_token,
772                      gss_OID *actual_mech_type,
773                      gss_buffer_t output_token,
774                      OM_uint32 *ret_flags,
775                      OM_uint32 *time_rec)
776 {
777     OM_uint32 major, tmpMinor;
778     gss_ctx_id_t ctx = *context_handle;
779     int initialContextToken = 0;
780
781     *minor = 0;
782
783     output_token->length = 0;
784     output_token->value = NULL;
785
786     if (ctx == GSS_C_NO_CONTEXT) {
787         if (input_token != GSS_C_NO_BUFFER && input_token->length != 0) {
788             *minor = GSSEAP_WRONG_SIZE;
789             return GSS_S_DEFECTIVE_TOKEN;
790         }
791
792         major = gssEapAllocContext(minor, &ctx);
793         if (GSS_ERROR(major))
794             return major;
795
796         ctx->flags |= CTX_FLAG_INITIATOR;
797
798         initialContextToken = 1;
799         *context_handle = ctx;
800     }
801
802     GSSEAP_MUTEX_LOCK(&ctx->mutex);
803
804     if (cred == GSS_C_NO_CREDENTIAL) {
805         if (ctx->defaultCred == GSS_C_NO_CREDENTIAL) {
806             major = gssEapAcquireCred(minor,
807                                       GSS_C_NO_NAME,
808                                       GSS_C_NO_BUFFER,
809                                       time_req,
810                                       GSS_C_NO_OID_SET,
811                                       GSS_C_INITIATE,
812                                       &ctx->defaultCred,
813                                       NULL,
814                                       NULL);
815             if (GSS_ERROR(major))
816                 goto cleanup;
817         }
818
819         cred = ctx->defaultCred;
820     }
821
822     GSSEAP_MUTEX_LOCK(&cred->mutex);
823
824
825     if ((cred->flags & CRED_FLAG_INITIATE) == 0) {
826         major = GSS_S_NO_CRED;
827         *minor = GSSEAP_CRED_USAGE_MISMATCH;
828         goto cleanup;
829     }
830
831     major = gssEapSmStep(minor,
832                          cred,
833                          ctx,
834                          target_name,
835                          mech_type,
836                          req_flags,
837                          time_req,
838                          input_chan_bindings,
839                          input_token,
840                          output_token,
841                          eapGssInitiatorSm,
842                          sizeof(eapGssInitiatorSm) / sizeof(eapGssInitiatorSm[0]));
843     if (GSS_ERROR(major))
844         goto cleanup;
845
846     if (actual_mech_type != NULL) {
847         if (!gssEapInternalizeOid(ctx->mechanismUsed, actual_mech_type))
848             duplicateOid(&tmpMinor, ctx->mechanismUsed, actual_mech_type);
849     }
850     if (ret_flags != NULL)
851         *ret_flags = ctx->gssFlags;
852     if (time_rec != NULL)
853         gssEapContextTime(&tmpMinor, ctx, time_rec);
854
855     assert(ctx->state == GSSEAP_STATE_ESTABLISHED || major == GSS_S_CONTINUE_NEEDED);
856
857 cleanup:
858     if (cred != GSS_C_NO_CREDENTIAL)
859         GSSEAP_MUTEX_UNLOCK(&cred->mutex);
860     GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
861
862     if (GSS_ERROR(major))
863         gssEapReleaseContext(&tmpMinor, context_handle);
864
865     return major;
866 }