fe54b119138fd71e5872fe261baeaaccacddf375
[mech_eap.orig] / mech_eap / accept_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 acceptor (server). These functions
35  * wrap around libradsec and (thus) talk to a RADIUS server or proxy.
36  */
37
38 #include "gssapiP_eap.h"
39
40 #ifdef GSSEAP_ENABLE_REAUTH
41 static OM_uint32
42 eapGssSmAcceptGssReauth(OM_uint32 *minor,
43                         gss_cred_id_t cred,
44                         gss_ctx_id_t ctx,
45                         gss_name_t target,
46                         gss_OID mech,
47                         OM_uint32 reqFlags,
48                         OM_uint32 timeReq,
49                         gss_channel_bindings_t chanBindings,
50                         gss_buffer_t inputToken,
51                         gss_buffer_t outputToken,
52                         OM_uint32 *smFlags);
53 #endif
54
55 /*
56  * Mark an acceptor context as ready for cryptographic operations
57  */
58 static OM_uint32
59 acceptReadyEap(OM_uint32 *minor, gss_ctx_id_t ctx, gss_cred_id_t cred)
60 {
61     OM_uint32 major, tmpMinor;
62     VALUE_PAIR *vp;
63     gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
64
65     /* Cache encryption type derived from selected mechanism OID */
66     major = gssEapOidToEnctype(minor, ctx->mechanismUsed,
67                                &ctx->encryptionType);
68     if (GSS_ERROR(major))
69         return major;
70
71     gssEapReleaseName(&tmpMinor, &ctx->initiatorName);
72
73     major = gssEapRadiusGetRawAvp(minor, ctx->acceptorCtx.vps,
74                                   PW_USER_NAME, 0, &vp);
75     if (major == GSS_S_COMPLETE && vp->length) {
76         nameBuf.length = vp->length;
77         nameBuf.value = vp->vp_strvalue;
78     } else {
79         ctx->gssFlags |= GSS_C_ANON_FLAG;
80     }
81
82     major = gssEapImportName(minor, &nameBuf,
83                              (ctx->gssFlags & GSS_C_ANON_FLAG) ?
84                                 GSS_C_NT_ANONYMOUS : GSS_C_NT_USER_NAME,
85                              ctx->mechanismUsed,
86                              &ctx->initiatorName);
87     if (GSS_ERROR(major))
88         return major;
89
90     major = gssEapRadiusGetRawAvp(minor, ctx->acceptorCtx.vps,
91                                   PW_MS_MPPE_SEND_KEY, VENDORPEC_MS, &vp);
92     if (GSS_ERROR(major)) {
93         *minor = GSSEAP_KEY_UNAVAILABLE;
94         return GSS_S_UNAVAILABLE;
95     }
96
97     major = gssEapDeriveRfc3961Key(minor,
98                                    vp->vp_octets,
99                                    vp->length,
100                                    ctx->encryptionType,
101                                    &ctx->rfc3961Key);
102     if (GSS_ERROR(major))
103         return major;
104
105     major = rfc3961ChecksumTypeForKey(minor, &ctx->rfc3961Key,
106                                        &ctx->checksumType);
107     if (GSS_ERROR(major))
108         return major;
109
110     major = sequenceInit(minor,
111                          &ctx->seqState, ctx->recvSeq,
112                          ((ctx->gssFlags & GSS_C_REPLAY_FLAG) != 0),
113                          ((ctx->gssFlags & GSS_C_SEQUENCE_FLAG) != 0),
114                          TRUE);
115     if (GSS_ERROR(major))
116         return major;
117
118     major = gssEapCreateAttrContext(minor, cred, ctx,
119                                     &ctx->initiatorName->attrCtx,
120                                     &ctx->expiryTime);
121     if (GSS_ERROR(major))
122         return major;
123
124     if (ctx->expiryTime != 0 && ctx->expiryTime < time(NULL)) {
125         *minor = GSSEAP_CRED_EXPIRED;
126         return GSS_S_CREDENTIALS_EXPIRED;
127     }
128
129     *minor = 0;
130     return GSS_S_COMPLETE;
131 }
132
133 static OM_uint32
134 eapGssSmAcceptAcceptorName(OM_uint32 *minor,
135                            gss_cred_id_t cred GSSEAP_UNUSED,
136                            gss_ctx_id_t ctx,
137                            gss_name_t target GSSEAP_UNUSED,
138                            gss_OID mech GSSEAP_UNUSED,
139                            OM_uint32 reqFlags GSSEAP_UNUSED,
140                            OM_uint32 timeReq GSSEAP_UNUSED,
141                            gss_channel_bindings_t chanBindings GSSEAP_UNUSED,
142                            gss_buffer_t inputToken GSSEAP_UNUSED,
143                            gss_buffer_t outputToken,
144                            OM_uint32 *smFlags GSSEAP_UNUSED)
145 {
146     OM_uint32 major;
147
148     /* XXX TODO import and validate name from inputToken */
149
150     if (ctx->acceptorName != GSS_C_NO_NAME) {
151         /* Send desired target name to acceptor */
152         major = gssEapDisplayName(minor, ctx->acceptorName,
153                                   outputToken, NULL);
154         if (GSS_ERROR(major))
155             return major;
156     }
157
158     return GSS_S_CONTINUE_NEEDED;
159 }
160
161 #ifdef GSSEAP_DEBUG
162 static OM_uint32
163 eapGssSmAcceptVendorInfo(OM_uint32 *minor,
164                          gss_cred_id_t cred GSSEAP_UNUSED,
165                          gss_ctx_id_t ctx GSSEAP_UNUSED,
166                          gss_name_t target GSSEAP_UNUSED,
167                          gss_OID mech GSSEAP_UNUSED,
168                          OM_uint32 reqFlags GSSEAP_UNUSED,
169                          OM_uint32 timeReq GSSEAP_UNUSED,
170                          gss_channel_bindings_t chanBindings GSSEAP_UNUSED,
171                          gss_buffer_t inputToken,
172                          gss_buffer_t outputToken GSSEAP_UNUSED,
173                          OM_uint32 *smFlags GSSEAP_UNUSED)
174 {
175     fprintf(stderr, "GSS-EAP: vendor: %.*s\n",
176             (int)inputToken->length, (char *)inputToken->value);
177
178     *minor = 0;
179     return GSS_S_CONTINUE_NEEDED;
180 }
181 #endif
182
183
184 /*
185  * Emit a identity EAP request to force the initiator (peer) to identify
186  * itself.
187  */
188 static OM_uint32
189 eapGssSmAcceptIdentity(OM_uint32 *minor,
190                        gss_cred_id_t cred,
191                        gss_ctx_id_t ctx,
192                        gss_name_t target GSSEAP_UNUSED,
193                        gss_OID mech GSSEAP_UNUSED,
194                        OM_uint32 reqFlags GSSEAP_UNUSED,
195                        OM_uint32 timeReq GSSEAP_UNUSED,
196                        gss_channel_bindings_t chanBindings GSSEAP_UNUSED,
197                        gss_buffer_t inputToken,
198                        gss_buffer_t outputToken,
199                        OM_uint32 *smFlags)
200 {
201     OM_uint32 major;
202     struct wpabuf *reqData;
203     gss_buffer_desc pktBuffer;
204
205     if (!gssEapCredAvailable(cred, ctx->mechanismUsed)) {
206         *minor = GSSEAP_CRED_MECH_MISMATCH;
207         return GSS_S_BAD_MECH;
208     }
209
210     if (inputToken != GSS_C_NO_BUFFER && inputToken->length != 0) {
211         *minor = GSSEAP_WRONG_SIZE;
212         return GSS_S_DEFECTIVE_TOKEN;
213     }
214
215     reqData = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY, 0,
216                             EAP_CODE_REQUEST, 0);
217     if (reqData == NULL) {
218         *minor = ENOMEM;
219         return GSS_S_FAILURE;
220     }
221
222     pktBuffer.length = wpabuf_len(reqData);
223     pktBuffer.value = (void *)wpabuf_head(reqData);
224
225     major = duplicateBuffer(minor, &pktBuffer, outputToken);
226     if (GSS_ERROR(major))
227         return major;
228
229     wpabuf_free(reqData);
230
231     GSSEAP_SM_TRANSITION_NEXT(ctx);
232
233     *minor = 0;
234     *smFlags |= SM_FLAG_OUTPUT_TOKEN_CRITICAL;
235
236     return GSS_S_CONTINUE_NEEDED;
237 }
238
239 /*
240  * Returns TRUE if the input token contains an EAP identity response.
241  */
242 static int
243 isIdentityResponseP(gss_buffer_t inputToken)
244 {
245     struct wpabuf respData;
246
247     wpabuf_set(&respData, inputToken->value, inputToken->length);
248
249     return (eap_get_type(&respData) == EAP_TYPE_IDENTITY);
250 }
251
252 /*
253  * Save the asserted initiator identity from the EAP identity response.
254  */
255 static OM_uint32
256 importInitiatorIdentity(OM_uint32 *minor,
257                         gss_ctx_id_t ctx,
258                         gss_buffer_t inputToken)
259 {
260     OM_uint32 tmpMinor;
261     struct wpabuf respData;
262     const unsigned char *pos;
263     size_t len;
264     gss_buffer_desc nameBuf;
265
266     wpabuf_set(&respData, inputToken->value, inputToken->length);
267
268     pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY,
269                            &respData, &len);
270     if (pos == NULL) {
271         *minor = GSSEAP_PEER_BAD_MESSAGE;
272         return GSS_S_DEFECTIVE_TOKEN;
273     }
274
275     nameBuf.value = (void *)pos;
276     nameBuf.length = len;
277
278     gssEapReleaseName(&tmpMinor, &ctx->initiatorName);
279
280     return gssEapImportName(minor, &nameBuf, GSS_C_NT_USER_NAME,
281                             ctx->mechanismUsed, &ctx->initiatorName);
282 }
283
284 /*
285  * Pass the asserted initiator identity to the authentication server.
286  */
287 static OM_uint32
288 setInitiatorIdentity(OM_uint32 *minor,
289                      gss_ctx_id_t ctx,
290                      VALUE_PAIR **vps)
291 {
292     OM_uint32 major, tmpMinor;
293     gss_buffer_desc nameBuf;
294
295     /*
296      * We should have got an EAP identity response, but if we didn't, then
297      * we will just avoid sending User-Name. Note that radsecproxy requires
298      * User-Name to be sent on every request (presumably so it can remain
299      * stateless).
300      */
301     if (ctx->initiatorName != GSS_C_NO_NAME) {
302         major = gssEapDisplayName(minor, ctx->initiatorName, &nameBuf, NULL);
303         if (GSS_ERROR(major))
304             return major;
305
306         major = gssEapRadiusAddAvp(minor, vps, PW_USER_NAME, 0, &nameBuf);
307         if (GSS_ERROR(major))
308             return major;
309
310         gss_release_buffer(&tmpMinor, &nameBuf);
311     }
312
313     *minor = 0;
314     return GSS_S_COMPLETE;
315 }
316
317 /*
318  * Pass the asserted acceptor identity to the authentication server.
319  */
320 static OM_uint32
321 setAcceptorIdentity(OM_uint32 *minor,
322                     gss_ctx_id_t ctx,
323                     VALUE_PAIR **vps)
324 {
325     OM_uint32 major;
326     gss_buffer_desc nameBuf;
327     krb5_context krbContext = NULL;
328     krb5_principal krbPrinc;
329     struct rs_context *rc = ctx->acceptorCtx.radContext;
330
331     assert(rc != NULL);
332
333     if (ctx->acceptorName == GSS_C_NO_NAME) {
334         *minor = 0;
335         return GSS_S_COMPLETE;
336     }
337
338     if ((ctx->acceptorName->flags & NAME_FLAG_SERVICE) == 0) {
339         *minor = GSSEAP_BAD_SERVICE_NAME;
340         return GSS_S_BAD_NAME;
341     }
342
343     GSSEAP_KRB_INIT(&krbContext);
344
345     krbPrinc = ctx->acceptorName->krbPrincipal;
346     assert(krbPrinc != NULL);
347     assert(KRB_PRINC_LENGTH(krbPrinc) >= 2);
348
349     /* Acceptor-Service-Name */
350     krbPrincComponentToGssBuffer(krbPrinc, 0, &nameBuf);
351
352     major = gssEapRadiusAddAvp(minor, vps,
353                                PW_GSS_ACCEPTOR_SERVICE_NAME,
354                                VENDORPEC_UKERNA,
355                                &nameBuf);
356     if (GSS_ERROR(major))
357         return major;
358
359     /* Acceptor-Host-Name */
360     krbPrincComponentToGssBuffer(krbPrinc, 1, &nameBuf);
361
362     major = gssEapRadiusAddAvp(minor, vps,
363                                PW_GSS_ACCEPTOR_HOST_NAME,
364                                VENDORPEC_UKERNA,
365                                &nameBuf);
366     if (GSS_ERROR(major))
367         return major;
368
369     if (KRB_PRINC_LENGTH(krbPrinc) > 2) {
370         /* Acceptor-Service-Specific */
371         krb5_principal_data ssiPrinc = *krbPrinc;
372         char *ssi;
373
374         KRB_PRINC_LENGTH(&ssiPrinc) -= 2;
375         KRB_PRINC_NAME(&ssiPrinc) += 2;
376
377         *minor = krb5_unparse_name_flags(krbContext, &ssiPrinc,
378                                          KRB5_PRINCIPAL_UNPARSE_NO_REALM, &ssi);
379         if (*minor != 0)
380             return GSS_S_FAILURE;
381
382         nameBuf.value = ssi;
383         nameBuf.length = strlen(ssi);
384
385         major = gssEapRadiusAddAvp(minor, vps,
386                                    PW_GSS_ACCEPTOR_SERVICE_SPECIFIC,
387                                    VENDORPEC_UKERNA,
388                                    &nameBuf);
389
390         if (GSS_ERROR(major)) {
391             krb5_free_unparsed_name(krbContext, ssi);
392             return major;
393         }
394         krb5_free_unparsed_name(krbContext, ssi);
395     }
396
397     krbPrincRealmToGssBuffer(krbPrinc, &nameBuf);
398     if (nameBuf.length != 0) {
399         /* Acceptor-Realm-Name */
400         major = gssEapRadiusAddAvp(minor, vps,
401                                    PW_GSS_ACCEPTOR_REALM_NAME,
402                                    VENDORPEC_UKERNA,
403                                    &nameBuf);
404         if (GSS_ERROR(major))
405             return major;
406     }
407
408     *minor = 0;
409     return GSS_S_COMPLETE;
410 }
411
412 /*
413  * Allocate a RadSec handle
414  */
415 static OM_uint32
416 createRadiusHandle(OM_uint32 *minor,
417                    gss_cred_id_t cred,
418                    gss_ctx_id_t ctx)
419 {
420     struct gss_eap_acceptor_ctx *actx = &ctx->acceptorCtx;
421     const char *configFile = RS_CONFIG_FILE;
422     const char *configStanza = "gss-eap";
423     struct rs_alloc_scheme ralloc;
424     struct rs_error *err;
425
426     assert(actx->radContext == NULL);
427     assert(actx->radConn == NULL);
428
429     if (rs_context_create(&actx->radContext) != 0) {
430         *minor = GSSEAP_RADSEC_CONTEXT_FAILURE;
431         return GSS_S_FAILURE;
432     }
433
434     if (cred->radiusConfigFile != NULL)
435         configFile = cred->radiusConfigFile;
436     if (cred->radiusConfigStanza != NULL)
437         configStanza = cred->radiusConfigStanza;
438
439     ralloc.calloc  = GSSEAP_CALLOC;
440     ralloc.malloc  = GSSEAP_MALLOC;
441     ralloc.free    = GSSEAP_FREE;
442     ralloc.realloc = GSSEAP_REALLOC;
443
444     rs_context_set_alloc_scheme(actx->radContext, &ralloc);
445
446     if (rs_context_read_config(actx->radContext, configFile) != 0) {
447         err = rs_err_ctx_pop(actx->radContext);
448         goto fail;
449     }
450
451     if (rs_context_init_freeradius_dict(actx->radContext, NULL) != 0) {
452         err = rs_err_ctx_pop(actx->radContext);
453         goto fail;
454     }
455
456     if (rs_conn_create(actx->radContext, &actx->radConn, configStanza) != 0) {
457         err = rs_err_conn_pop(actx->radConn);
458         goto fail;
459     }
460
461     if (actx->radServer != NULL) {
462         if (rs_conn_select_peer(actx->radConn, actx->radServer) != 0) {
463             err = rs_err_conn_pop(actx->radConn);
464             goto fail;
465         }
466     }
467
468     *minor = 0;
469     return GSS_S_COMPLETE;
470
471 fail:
472     return gssEapRadiusMapError(minor, err);
473 }
474
475 /*
476  * Process a EAP response from the initiator.
477  */
478 static OM_uint32
479 eapGssSmAcceptAuthenticate(OM_uint32 *minor,
480                            gss_cred_id_t cred,
481                            gss_ctx_id_t ctx,
482                            gss_name_t target GSSEAP_UNUSED,
483                            gss_OID mech GSSEAP_UNUSED,
484                            OM_uint32 reqFlags GSSEAP_UNUSED,
485                            OM_uint32 timeReq GSSEAP_UNUSED,
486                            gss_channel_bindings_t chanBindings GSSEAP_UNUSED,
487                            gss_buffer_t inputToken,
488                            gss_buffer_t outputToken,
489                            OM_uint32 *smFlags)
490 {
491     OM_uint32 major, tmpMinor;
492     struct rs_connection *rconn;
493     struct rs_request *request = NULL;
494     struct rs_packet *req = NULL, *resp = NULL;
495     struct radius_packet *frreq, *frresp;
496
497     if (ctx->acceptorCtx.radContext == NULL) {
498         /* May be NULL from an imported partial context */
499         major = createRadiusHandle(minor, cred, ctx);
500         if (GSS_ERROR(major))
501             goto cleanup;
502     }
503
504     if (isIdentityResponseP(inputToken)) {
505         major = importInitiatorIdentity(minor, ctx, inputToken);
506         if (GSS_ERROR(major))
507             return major;
508     }
509
510     rconn = ctx->acceptorCtx.radConn;
511
512     if (rs_packet_create_authn_request(rconn, &req, NULL, NULL) != 0) {
513         major = gssEapRadiusMapError(minor, rs_err_conn_pop(rconn));
514         goto cleanup;
515     }
516     frreq = rs_packet_frpkt(req);
517
518     major = setInitiatorIdentity(minor, ctx, &frreq->vps);
519     if (GSS_ERROR(major))
520         goto cleanup;
521
522     major = setAcceptorIdentity(minor, ctx, &frreq->vps);
523     if (GSS_ERROR(major))
524         goto cleanup;
525
526     major = gssEapRadiusAddAvp(minor, &frreq->vps,
527                                PW_EAP_MESSAGE, 0, inputToken);
528     if (GSS_ERROR(major))
529         goto cleanup;
530
531     if (ctx->acceptorCtx.state.length != 0) {
532         major = gssEapRadiusAddAvp(minor, &frreq->vps, PW_STATE, 0,
533                                    &ctx->acceptorCtx.state);
534         if (GSS_ERROR(major))
535             goto cleanup;
536
537         gss_release_buffer(&tmpMinor, &ctx->acceptorCtx.state);
538     }
539
540     if (rs_request_create(rconn, &request) != 0) {
541         major = gssEapRadiusMapError(minor, rs_err_conn_pop(rconn));
542         goto cleanup;
543     }
544
545     rs_request_add_reqpkt(request, req);
546     req = NULL;
547
548     if (rs_request_send(request, &resp) != 0) {
549         major = gssEapRadiusMapError(minor, rs_err_conn_pop(rconn));
550         goto cleanup;
551     }
552
553     assert(resp != NULL);
554
555     frresp = rs_packet_frpkt(resp);
556     switch (frresp->code) {
557     case PW_ACCESS_CHALLENGE:
558     case PW_AUTHENTICATION_ACK:
559         break;
560     case PW_AUTHENTICATION_REJECT:
561         *minor = GSSEAP_RADIUS_AUTH_FAILURE;
562         major = GSS_S_DEFECTIVE_CREDENTIAL;
563         goto cleanup;
564         break;
565     default:
566         *minor = GSSEAP_UNKNOWN_RADIUS_CODE;
567         major = GSS_S_FAILURE;
568         goto cleanup;
569         break;
570     }
571
572     major = gssEapRadiusGetAvp(minor, frresp->vps, PW_EAP_MESSAGE, 0,
573                                outputToken, TRUE);
574     if (major == GSS_S_UNAVAILABLE && frresp->code == PW_ACCESS_CHALLENGE) {
575         *minor = GSSEAP_MISSING_EAP_REQUEST;
576         major = GSS_S_DEFECTIVE_TOKEN;
577         goto cleanup;
578     } else if (GSS_ERROR(major))
579         goto cleanup;
580
581     if (frresp->code == PW_ACCESS_CHALLENGE) {
582         major = gssEapRadiusGetAvp(minor, frresp->vps, PW_STATE, 0,
583                                    &ctx->acceptorCtx.state, TRUE);
584         if (GSS_ERROR(major) && *minor != GSSEAP_NO_SUCH_ATTR)
585             goto cleanup;
586     } else {
587         ctx->acceptorCtx.vps = frresp->vps;
588         frresp->vps = NULL;
589
590         major = acceptReadyEap(minor, ctx, cred);
591         if (GSS_ERROR(major))
592             goto cleanup;
593
594         GSSEAP_SM_TRANSITION_NEXT(ctx);
595     }
596
597     major = GSS_S_CONTINUE_NEEDED;
598     *minor = 0;
599     *smFlags |= SM_FLAG_OUTPUT_TOKEN_CRITICAL;
600
601 cleanup:
602     if (request != NULL)
603         rs_request_destroy(request);
604     if (req != NULL)
605         rs_packet_destroy(req);
606     if (resp != NULL)
607         rs_packet_destroy(resp);
608     if (GSSEAP_SM_STATE(ctx) == GSSEAP_STATE_INITIATOR_EXTS) {
609         assert(major == GSS_S_CONTINUE_NEEDED);
610
611         rs_conn_destroy(ctx->acceptorCtx.radConn);
612         ctx->acceptorCtx.radConn = NULL;
613     }
614
615     return major;
616 }
617
618 static OM_uint32
619 eapGssSmAcceptGssFlags(OM_uint32 *minor,
620                        gss_cred_id_t cred GSSEAP_UNUSED,
621                        gss_ctx_id_t ctx,
622                        gss_name_t target GSSEAP_UNUSED,
623                        gss_OID mech GSSEAP_UNUSED,
624                        OM_uint32 reqFlags GSSEAP_UNUSED,
625                        OM_uint32 timeReq GSSEAP_UNUSED,
626                        gss_channel_bindings_t chanBindings GSSEAP_UNUSED,
627                        gss_buffer_t inputToken,
628                        gss_buffer_t outputToken GSSEAP_UNUSED,
629                        OM_uint32 *smFlags GSSEAP_UNUSED)
630 {
631     unsigned char *p;
632     OM_uint32 initiatorGssFlags;
633
634     assert((ctx->flags & CTX_FLAG_KRB_REAUTH) == 0);
635
636     if (inputToken->length < 4) {
637         *minor = GSSEAP_TOK_TRUNC;
638         return GSS_S_DEFECTIVE_TOKEN;
639     }
640
641     /* allow flags to grow for future expansion */
642     p = (unsigned char *)inputToken->value + inputToken->length - 4;
643
644     initiatorGssFlags = load_uint32_be(p);
645     initiatorGssFlags &= GSSEAP_WIRE_FLAGS_MASK;
646
647     ctx->gssFlags |= initiatorGssFlags;
648
649     return GSS_S_CONTINUE_NEEDED;
650 }
651
652 static OM_uint32
653 eapGssSmAcceptGssChannelBindings(OM_uint32 *minor,
654                                  gss_cred_id_t cred GSSEAP_UNUSED,
655                                  gss_ctx_id_t ctx,
656                                  gss_name_t target GSSEAP_UNUSED,
657                                  gss_OID mech GSSEAP_UNUSED,
658                                  OM_uint32 reqFlags GSSEAP_UNUSED,
659                                  OM_uint32 timeReq GSSEAP_UNUSED,
660                                  gss_channel_bindings_t chanBindings,
661                                  gss_buffer_t inputToken,
662                                  gss_buffer_t outputToken GSSEAP_UNUSED,
663                                  OM_uint32 *smFlags GSSEAP_UNUSED)
664 {
665     OM_uint32 major, tmpMinor;
666     gss_iov_buffer_desc iov[2];
667
668     iov[0].type = GSS_IOV_BUFFER_TYPE_DATA | GSS_IOV_BUFFER_FLAG_ALLOCATE;
669     iov[0].buffer.length = 0;
670     iov[0].buffer.value = NULL;
671
672     iov[1].type = GSS_IOV_BUFFER_TYPE_STREAM;
673     iov[1].buffer = *inputToken;
674
675     major = gssEapUnwrapOrVerifyMIC(minor, ctx, NULL, NULL,
676                                     iov, 2, TOK_TYPE_WRAP);
677     if (GSS_ERROR(major))
678         return major;
679
680     if (chanBindings != GSS_C_NO_CHANNEL_BINDINGS &&
681         !bufferEqual(&iov[0].buffer, &chanBindings->application_data)) {
682         major = GSS_S_BAD_BINDINGS;
683         *minor = GSSEAP_BINDINGS_MISMATCH;
684     } else {
685         major = GSS_S_CONTINUE_NEEDED;
686         *minor = 0;
687     }
688
689     gss_release_buffer(&tmpMinor, &iov[0].buffer);
690
691     return major;
692 }
693
694 #ifdef GSSEAP_ENABLE_REAUTH
695 static OM_uint32
696 eapGssSmAcceptReauthCreds(OM_uint32 *minor,
697                           gss_cred_id_t cred,
698                           gss_ctx_id_t ctx,
699                           gss_name_t target GSSEAP_UNUSED,
700                           gss_OID mech GSSEAP_UNUSED,
701                           OM_uint32 reqFlags GSSEAP_UNUSED,
702                           OM_uint32 timeReq GSSEAP_UNUSED,
703                           gss_channel_bindings_t chanBindings GSSEAP_UNUSED,
704                           gss_buffer_t inputToken GSSEAP_UNUSED,
705                           gss_buffer_t outputToken,
706                           OM_uint32 *smFlags GSSEAP_UNUSED)
707 {
708     OM_uint32 major;
709
710     /*
711      * If we're built with fast reauthentication enabled, then
712      * fabricate a ticket from the initiator to ourselves.
713      */
714     major = gssEapMakeReauthCreds(minor, ctx, cred, outputToken);
715     if (major == GSS_S_UNAVAILABLE)
716         major = GSS_S_COMPLETE;
717     if (major == GSS_S_COMPLETE)
718         major = GSS_S_CONTINUE_NEEDED;
719
720     return major;
721 }
722 #endif
723
724 static OM_uint32
725 eapGssSmAcceptCompleteInitiatorExts(OM_uint32 *minor,
726                                     gss_cred_id_t cred GSSEAP_UNUSED,
727                                     gss_ctx_id_t ctx,
728                                     gss_name_t target GSSEAP_UNUSED,
729                                     gss_OID mech GSSEAP_UNUSED,
730                                     OM_uint32 reqFlags GSSEAP_UNUSED,
731                                     OM_uint32 timeReq GSSEAP_UNUSED,
732                                     gss_channel_bindings_t chanBindings GSSEAP_UNUSED,
733                                     gss_buffer_t inputToken GSSEAP_UNUSED,
734                                     gss_buffer_t outputToken GSSEAP_UNUSED,
735                                     OM_uint32 *smFlags GSSEAP_UNUSED)
736 {
737     GSSEAP_SM_TRANSITION_NEXT(ctx);
738
739     *minor = 0;
740
741     return GSS_S_CONTINUE_NEEDED;
742 }
743
744 static OM_uint32
745 eapGssSmAcceptCompleteAcceptorExts(OM_uint32 *minor,
746                                    gss_cred_id_t cred GSSEAP_UNUSED,
747                                    gss_ctx_id_t ctx,
748                                    gss_name_t target GSSEAP_UNUSED,
749                                    gss_OID mech GSSEAP_UNUSED,
750                                    OM_uint32 reqFlags GSSEAP_UNUSED,
751                                    OM_uint32 timeReq GSSEAP_UNUSED,
752                                    gss_channel_bindings_t chanBindings GSSEAP_UNUSED,
753                                    gss_buffer_t inputToken GSSEAP_UNUSED,
754                                    gss_buffer_t outputToken GSSEAP_UNUSED,
755                                    OM_uint32 *smFlags)
756 {
757     GSSEAP_SM_TRANSITION(ctx, GSSEAP_STATE_ESTABLISHED);
758
759     *minor = 0;
760     *smFlags |= SM_FLAG_FORCE_SEND_TOKEN;
761
762     return GSS_S_COMPLETE;
763 }
764
765 static struct gss_eap_sm eapGssAcceptorSm[] = {
766     {
767         ITOK_TYPE_ACCEPTOR_NAME_REQ,
768         ITOK_TYPE_ACCEPTOR_NAME_RESP,
769         GSSEAP_STATE_INITIAL,
770         0,
771         eapGssSmAcceptAcceptorName
772     },
773 #ifdef GSSEAP_DEBUG
774     {
775         ITOK_TYPE_VENDOR_INFO,
776         ITOK_TYPE_NONE,
777         GSSEAP_STATE_INITIAL,
778         0,
779         eapGssSmAcceptVendorInfo,
780     },
781 #endif
782 #ifdef GSSEAP_ENABLE_REAUTH
783     {
784         ITOK_TYPE_REAUTH_REQ,
785         ITOK_TYPE_REAUTH_RESP,
786         GSSEAP_STATE_INITIAL,
787         0,
788         eapGssSmAcceptGssReauth,
789     },
790 #endif
791     {
792         ITOK_TYPE_NONE,
793         ITOK_TYPE_EAP_REQ,
794         GSSEAP_STATE_INITIAL,
795         SM_ITOK_FLAG_REQUIRED,
796         eapGssSmAcceptIdentity,
797     },
798     {
799         ITOK_TYPE_EAP_RESP,
800         ITOK_TYPE_EAP_REQ,
801         GSSEAP_STATE_AUTHENTICATE,
802         SM_ITOK_FLAG_REQUIRED,
803         eapGssSmAcceptAuthenticate
804     },
805     {
806         ITOK_TYPE_GSS_FLAGS,
807         ITOK_TYPE_NONE,
808         GSSEAP_STATE_INITIATOR_EXTS,
809         0,
810         eapGssSmAcceptGssFlags
811     },
812     {
813         ITOK_TYPE_GSS_CHANNEL_BINDINGS,
814         ITOK_TYPE_NONE,
815         GSSEAP_STATE_INITIATOR_EXTS,
816         SM_ITOK_FLAG_REQUIRED,
817         eapGssSmAcceptGssChannelBindings,
818     },
819     {
820         ITOK_TYPE_NONE,
821         ITOK_TYPE_NONE,
822         GSSEAP_STATE_INITIATOR_EXTS,
823         0,
824         eapGssSmAcceptCompleteInitiatorExts,
825     },
826 #ifdef GSSEAP_ENABLE_REAUTH
827     {
828         ITOK_TYPE_NONE,
829         ITOK_TYPE_REAUTH_CREDS,
830         GSSEAP_STATE_ACCEPTOR_EXTS,
831         0,
832         eapGssSmAcceptReauthCreds,
833     },
834 #endif
835     {
836         ITOK_TYPE_NONE,
837         ITOK_TYPE_NONE,
838         GSSEAP_STATE_ACCEPTOR_EXTS,
839         0,
840         eapGssSmAcceptCompleteAcceptorExts
841     },
842 };
843
844 OM_uint32
845 gss_accept_sec_context(OM_uint32 *minor,
846                        gss_ctx_id_t *context_handle,
847                        gss_cred_id_t cred,
848                        gss_buffer_t input_token,
849                        gss_channel_bindings_t input_chan_bindings,
850                        gss_name_t *src_name,
851                        gss_OID *mech_type,
852                        gss_buffer_t output_token,
853                        OM_uint32 *ret_flags,
854                        OM_uint32 *time_rec,
855                        gss_cred_id_t *delegated_cred_handle)
856 {
857     OM_uint32 major, tmpMinor;
858     gss_ctx_id_t ctx = *context_handle;
859
860     *minor = 0;
861
862     output_token->length = 0;
863     output_token->value = NULL;
864
865     if (src_name != NULL)
866         *src_name = GSS_C_NO_NAME;
867
868     if (input_token == GSS_C_NO_BUFFER || input_token->length == 0) {
869         *minor = GSSEAP_TOK_TRUNC;
870         return GSS_S_DEFECTIVE_TOKEN;
871     }
872
873     if (ctx == GSS_C_NO_CONTEXT) {
874         major = gssEapAllocContext(minor, &ctx);
875         if (GSS_ERROR(major))
876             return major;
877
878         *context_handle = ctx;
879     }
880
881     GSSEAP_MUTEX_LOCK(&ctx->mutex);
882
883     if (cred == GSS_C_NO_CREDENTIAL) {
884         if (ctx->defaultCred == GSS_C_NO_CREDENTIAL) {
885             major = gssEapAcquireCred(minor,
886                                       GSS_C_NO_NAME,
887                                       GSS_C_NO_BUFFER,
888                                       GSS_C_INDEFINITE,
889                                       GSS_C_NO_OID_SET,
890                                       GSS_C_ACCEPT,
891                                       &ctx->defaultCred,
892                                       NULL,
893                                       NULL);
894             if (GSS_ERROR(major))
895                 goto cleanup;
896         }
897
898         cred = ctx->defaultCred;
899     }
900
901     GSSEAP_MUTEX_LOCK(&cred->mutex);
902
903     if (cred->name != GSS_C_NO_NAME) {
904         major = gssEapDuplicateName(minor, cred->name, &ctx->acceptorName);
905         if (GSS_ERROR(major))
906             goto cleanup;
907     }
908
909     major = gssEapSmStep(minor,
910                          cred,
911                          ctx,
912                          GSS_C_NO_NAME,
913                          GSS_C_NO_OID,
914                          0,
915                          GSS_C_INDEFINITE,
916                          input_chan_bindings,
917                          input_token,
918                          output_token,
919                          eapGssAcceptorSm,
920                          sizeof(eapGssAcceptorSm) / sizeof(eapGssAcceptorSm[0]));
921     if (GSS_ERROR(major))
922         goto cleanup;
923
924     if (mech_type != NULL) {
925         OM_uint32 tmpMajor;
926
927         tmpMajor = gssEapCanonicalizeOid(&tmpMinor, ctx->mechanismUsed, 0, mech_type);
928         if (GSS_ERROR(tmpMajor)) {
929             major = tmpMajor;
930             *minor = tmpMinor;
931             goto cleanup;
932         }
933     }
934     if (ret_flags != NULL)
935         *ret_flags = ctx->gssFlags;
936     if (delegated_cred_handle != NULL)
937         *delegated_cred_handle = GSS_C_NO_CREDENTIAL;
938
939     if (major == GSS_S_COMPLETE) {
940         if (src_name != NULL && ctx->initiatorName != GSS_C_NO_NAME) {
941             major = gssEapDuplicateName(&tmpMinor, ctx->initiatorName, src_name);
942             if (GSS_ERROR(major))
943                 goto cleanup;
944         }
945         if (time_rec != NULL) {
946             major = gssEapContextTime(&tmpMinor, ctx, time_rec);
947             if (GSS_ERROR(major))
948                 goto cleanup;
949         }
950     }
951
952     assert(CTX_IS_ESTABLISHED(ctx) || major == GSS_S_CONTINUE_NEEDED);
953
954 cleanup:
955     if (cred != GSS_C_NO_CREDENTIAL)
956         GSSEAP_MUTEX_UNLOCK(&cred->mutex);
957     GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
958
959     if (GSS_ERROR(major))
960         gssEapReleaseContext(&tmpMinor, context_handle);
961
962     return major;
963 }
964
965 #ifdef GSSEAP_ENABLE_REAUTH
966 static OM_uint32
967 acceptReadyKrb(OM_uint32 *minor,
968                gss_ctx_id_t ctx,
969                gss_cred_id_t cred,
970                const gss_name_t initiator,
971                const gss_OID mech,
972                OM_uint32 timeRec)
973 {
974     OM_uint32 major;
975
976     major = gssEapGlueToMechName(minor, ctx, initiator, &ctx->initiatorName);
977     if (GSS_ERROR(major))
978         return major;
979
980     major = gssEapReauthComplete(minor, ctx, cred, mech, timeRec);
981     if (GSS_ERROR(major))
982         return major;
983
984     *minor = 0;
985     return GSS_S_COMPLETE;
986 }
987
988 static OM_uint32
989 eapGssSmAcceptGssReauth(OM_uint32 *minor,
990                         gss_cred_id_t cred,
991                         gss_ctx_id_t ctx,
992                         gss_name_t target GSSEAP_UNUSED,
993                         gss_OID mech,
994                         OM_uint32 reqFlags GSSEAP_UNUSED,
995                         OM_uint32 timeReq GSSEAP_UNUSED,
996                         gss_channel_bindings_t chanBindings,
997                         gss_buffer_t inputToken,
998                         gss_buffer_t outputToken,
999                         OM_uint32 *smFlags)
1000 {
1001     OM_uint32 major, tmpMinor;
1002     gss_name_t krbInitiator = GSS_C_NO_NAME;
1003     OM_uint32 gssFlags, timeRec = GSS_C_INDEFINITE;
1004
1005     /*
1006      * If we're built with fast reauthentication support, it's valid
1007      * for an initiator to send a GSS reauthentication token as its
1008      * initial context token, causing us to short-circuit the state
1009      * machine and process Kerberos GSS messages instead.
1010      */
1011
1012     ctx->flags |= CTX_FLAG_KRB_REAUTH;
1013
1014     major = gssAcceptSecContext(minor,
1015                                 &ctx->reauthCtx,
1016                                 cred->reauthCred,
1017                                 inputToken,
1018                                 chanBindings,
1019                                 &krbInitiator,
1020                                 &mech,
1021                                 outputToken,
1022                                 &gssFlags,
1023                                 &timeRec,
1024                                 NULL);
1025     if (major == GSS_S_COMPLETE) {
1026         major = acceptReadyKrb(minor, ctx, cred,
1027                                krbInitiator, mech, timeRec);
1028         if (major == GSS_S_COMPLETE) {
1029             GSSEAP_SM_TRANSITION(ctx, GSSEAP_STATE_ESTABLISHED);
1030         }
1031         ctx->gssFlags = gssFlags;
1032     } else if (GSS_ERROR(major) &&
1033         (*smFlags & SM_FLAG_INPUT_TOKEN_CRITICAL) == 0) {
1034         /* pretend reauthentication attempt never happened */
1035         gssDeleteSecContext(&tmpMinor, &ctx->reauthCtx, GSS_C_NO_BUFFER);
1036         ctx->flags &= ~(CTX_FLAG_KRB_REAUTH);
1037         GSSEAP_SM_TRANSITION(ctx, GSSEAP_STATE_INITIAL);
1038         major = GSS_S_CONTINUE_NEEDED;
1039     }
1040
1041     gssReleaseName(&tmpMinor, &krbInitiator);
1042
1043     return major;
1044 }
1045 #endif /* GSSEAP_ENABLE_REAUTH */