Support GSS_C_NT_COMPOSITE_EXPORT
[mech_eap.orig] / init_sec_context.c
1 /*
2  * Copyright (c) 2010, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 #include "gssapiP_eap.h"
34
35 static OM_uint32
36 policyVariableToFlag(enum eapol_bool_var variable)
37 {
38     OM_uint32 flag = 0;
39
40     switch (variable) {
41     case EAPOL_eapSuccess:
42         flag = CTX_FLAG_EAP_SUCCESS;
43         break;
44     case EAPOL_eapRestart:
45         flag = CTX_FLAG_EAP_RESTART;
46         break;
47     case EAPOL_eapFail:
48         flag = CTX_FLAG_EAP_FAIL;
49         break;
50     case EAPOL_eapResp:
51         flag = CTX_FLAG_EAP_RESP;
52         break;
53     case EAPOL_eapNoResp:
54         flag = CTX_FLAG_EAP_NO_RESP;
55         break;
56     case EAPOL_eapReq:
57         flag = CTX_FLAG_EAP_REQ;
58         break;
59     case EAPOL_portEnabled:
60         flag = CTX_FLAG_EAP_PORT_ENABLED;
61         break;
62     case EAPOL_altAccept:
63         flag = CTX_FLAG_EAP_ALT_ACCEPT;
64         break;
65     case EAPOL_altReject:
66         flag = CTX_FLAG_EAP_ALT_REJECT;
67         break;
68     }
69
70     return flag;
71         
72 }
73
74 static struct eap_peer_config *
75 peerGetConfig(void *ctx)
76 {
77     gss_ctx_id_t gssCtx = (gss_ctx_id_t)ctx;
78
79     return &gssCtx->initiatorCtx.eapPeerConfig;
80 }
81
82 static Boolean
83 peerGetBool(void *data, enum eapol_bool_var variable)
84 {
85     gss_ctx_id_t ctx = data;
86     OM_uint32 flag;
87
88     if (ctx == GSS_C_NO_CONTEXT)
89         return FALSE;
90
91     flag = policyVariableToFlag(variable);
92
93     return ((ctx->flags & flag) != 0);
94 }
95
96 static void
97 peerSetBool(void *data, enum eapol_bool_var variable,
98             Boolean value)
99 {
100     gss_ctx_id_t ctx = data;
101     OM_uint32 flag;
102
103     if (ctx == GSS_C_NO_CONTEXT)
104         return;
105
106     flag = policyVariableToFlag(variable);
107
108     if (value)
109         ctx->flags |= flag;
110     else
111         ctx->flags &= ~(flag);
112 }
113
114 static unsigned int
115 peerGetInt(void *data, enum eapol_int_var variable)
116 {
117     gss_ctx_id_t ctx = data;
118
119     if (ctx == GSS_C_NO_CONTEXT)
120         return FALSE;
121
122     assert(CTX_IS_INITIATOR(ctx));
123
124     switch (variable) {
125     case EAPOL_idleWhile:
126         return ctx->initiatorCtx.idleWhile;
127         break;
128     }
129
130     return 0;
131 }
132
133 static void
134 peerSetInt(void *data, enum eapol_int_var variable,
135            unsigned int value)
136 {
137     gss_ctx_id_t ctx = data;
138
139     if (ctx == GSS_C_NO_CONTEXT)
140         return;
141
142     assert(CTX_IS_INITIATOR(ctx));
143
144     switch (variable) {
145     case EAPOL_idleWhile:
146         ctx->initiatorCtx.idleWhile = value;
147         break;
148     }
149 }
150
151 static struct wpabuf *
152 peerGetEapReqData(void *ctx)
153 {
154     gss_ctx_id_t gssCtx = (gss_ctx_id_t)ctx;
155
156     return &gssCtx->initiatorCtx.reqData;
157 }
158
159 static void
160 peerSetConfigBlob(void *ctx, struct wpa_config_blob *blob)
161 {
162 }
163
164 static const struct wpa_config_blob *
165 peerGetConfigBlob(void *ctx, const char *name)
166 {
167     return NULL;
168 }
169
170 static void
171 peerNotifyPending(void *ctx)
172 {
173 }
174
175 static struct eapol_callbacks gssEapPolicyCallbacks = {
176     peerGetConfig,
177     peerGetBool,
178     peerSetBool,
179     peerGetInt,
180     peerSetInt,
181     peerGetEapReqData,
182     peerSetConfigBlob,
183     peerGetConfigBlob,
184     peerNotifyPending,
185 };
186
187 extern int wpa_debug_level;
188
189 static OM_uint32
190 peerConfigInit(OM_uint32 *minor,
191                gss_cred_id_t cred,
192                gss_ctx_id_t ctx,
193                int loadConfig)
194 {
195     krb5_context krbContext;
196     struct eap_peer_config *eapPeerConfig = &ctx->initiatorCtx.eapPeerConfig;
197     krb5_error_code code;
198     char *identity;
199
200     GSSEAP_KRB_INIT(&krbContext);
201
202     if (loadConfig) {
203         eapPeerConfig->fragment_size = 1024;
204         wpa_debug_level = 0;
205     }
206
207     code = krb5_unparse_name(krbContext, cred->name->krbPrincipal, &identity);
208     if (code != 0) {
209         *minor = code;
210         return GSS_S_FAILURE;
211     }
212
213     eapPeerConfig->identity = (unsigned char *)identity;
214     eapPeerConfig->identity_len = strlen(identity);
215     eapPeerConfig->password = (unsigned char *)cred->password.value;
216     eapPeerConfig->password_len = cred->password.length;
217
218     return GSS_S_COMPLETE;
219 }
220
221 static OM_uint32
222 peerConfigFree(OM_uint32 *minor,
223                gss_ctx_id_t ctx)
224 {
225     krb5_context krbContext;
226     struct eap_peer_config *eapPeerConfig = &ctx->initiatorCtx.eapPeerConfig;
227
228     GSSEAP_KRB_INIT(&krbContext);
229
230     krb5_free_unparsed_name(krbContext, (char *)eapPeerConfig->identity);
231
232     return GSS_S_COMPLETE;
233 }
234
235 static OM_uint32
236 initReady(OM_uint32 *minor, gss_ctx_id_t ctx)
237 {
238     OM_uint32 major;
239     const unsigned char *key;
240     size_t keyLength;
241
242     /* Cache encryption type derived from selected mechanism OID */
243     major = gssEapOidToEnctype(minor, ctx->mechanismUsed, &ctx->encryptionType);
244     if (GSS_ERROR(major))
245         return major;
246
247     if (ctx->encryptionType != ENCTYPE_NULL &&
248         eap_key_available(ctx->initiatorCtx.eap)) {
249         key = eap_get_eapKeyData(ctx->initiatorCtx.eap, &keyLength);
250
251         major = gssEapDeriveRfc3961Key(minor, key, keyLength,
252                                        ctx->encryptionType, &ctx->rfc3961Key);
253         if (GSS_ERROR(major))
254             return major;
255
256         major = rfc3961ChecksumTypeForKey(minor, &ctx->rfc3961Key,
257                                            &ctx->checksumType);
258         if (GSS_ERROR(major))
259             return major;
260     } else {
261         /*
262          * draft-howlett-eap-gss says that integrity/confidentialty should
263          * always be advertised as available, but if we have no keying
264          * material it seems confusing to the caller to advertise this.
265          */
266         ctx->gssFlags &= ~(GSS_C_INTEG_FLAG | GSS_C_CONF_FLAG);
267     }
268
269     major = sequenceInit(minor,
270                          &ctx->seqState,
271                          ctx->recvSeq,
272                          ((ctx->gssFlags & GSS_C_REPLAY_FLAG) != 0),
273                          ((ctx->gssFlags & GSS_C_SEQUENCE_FLAG) != 0),
274                          TRUE);
275     if (GSS_ERROR(major))
276         return major;
277
278     return GSS_S_COMPLETE;
279 }
280
281 static OM_uint32
282 eapGssSmInitAuthenticate(OM_uint32 *minor,
283                          gss_cred_id_t cred,
284                          gss_ctx_id_t ctx,
285                          gss_name_t target,
286                          gss_OID mech,
287                          OM_uint32 reqFlags,
288                          OM_uint32 timeReq,
289                          gss_channel_bindings_t chanBindings,
290                          gss_buffer_t inputToken,
291                          gss_buffer_t outputToken)
292 {
293     OM_uint32 major;
294     OM_uint32 tmpMajor, tmpMinor;
295     time_t now;
296     int initialContextToken = 0, code;
297     gss_buffer_desc respBuf = GSS_C_EMPTY_BUFFER;
298
299     initialContextToken = (inputToken == GSS_C_NO_BUFFER ||
300                            inputToken->length == 0);
301
302     major = peerConfigInit(minor, cred, ctx, initialContextToken);
303     if (GSS_ERROR(major))
304         goto cleanup;
305
306     if (initialContextToken) {
307         struct eap_config eapConfig;
308
309         memset(&eapConfig, 0, sizeof(eapConfig));
310         ctx->flags |= CTX_FLAG_EAP_PORT_ENABLED;
311
312         ctx->initiatorCtx.eap = eap_peer_sm_init(ctx,
313                                                  &gssEapPolicyCallbacks,
314                                                  ctx,
315                                                  &eapConfig);
316
317         time(&now);
318         if (timeReq == 0 || timeReq == GSS_C_INDEFINITE)
319             ctx->expiryTime = 0;
320         else
321             ctx->expiryTime = now + timeReq;
322
323         major = gss_duplicate_name(minor, cred->name, &ctx->initiatorName);
324         if (GSS_ERROR(major))
325             goto cleanup;
326
327         major = gss_duplicate_name(minor, target, &ctx->acceptorName);
328         if (GSS_ERROR(major))
329             goto cleanup;
330
331         if (mech == GSS_C_NULL_OID || oidEqual(mech, GSS_EAP_MECHANISM)) {
332             major = gssEapDefaultMech(minor, &ctx->mechanismUsed);
333         } else if (gssEapIsConcreteMechanismOid(mech)) {
334             if (!gssEapInternalizeOid(mech, &ctx->mechanismUsed))
335                 major = duplicateOid(minor, mech, &ctx->mechanismUsed);
336         } else {
337             major = GSS_S_BAD_MECH;
338         }
339         if (GSS_ERROR(major))
340             goto cleanup;
341
342         /* If credentials were provided, check they're usable with this mech */
343         if (!gssEapCredAvailable(cred, ctx->mechanismUsed)) {
344             major = GSS_S_BAD_MECH;
345             goto cleanup;
346         }
347
348         respBuf.value = ""; /* emit empty inner token */
349         major = GSS_S_CONTINUE_NEEDED;
350         goto cleanup;
351     } else {
352         ctx->flags |= CTX_FLAG_EAP_REQ; /* we have a Request from the acceptor */
353     }
354
355     wpabuf_set(&ctx->initiatorCtx.reqData,
356                inputToken->value, inputToken->length);
357
358     major = GSS_S_CONTINUE_NEEDED;
359
360     code = eap_peer_sm_step(ctx->initiatorCtx.eap);
361     if (ctx->flags & CTX_FLAG_EAP_RESP) {
362         struct wpabuf *resp;
363
364         ctx->flags &= ~(CTX_FLAG_EAP_RESP);
365
366         resp = eap_get_eapRespData(ctx->initiatorCtx.eap);
367         if (resp != NULL) {
368             respBuf.length = wpabuf_len(resp);
369             respBuf.value = (void *)wpabuf_head(resp);
370         }
371     } else if (ctx->flags & CTX_FLAG_EAP_SUCCESS) {
372         major = initReady(minor, ctx);
373         if (GSS_ERROR(major))
374             goto cleanup;
375
376         ctx->flags &= ~(CTX_FLAG_EAP_SUCCESS);
377         major = GSS_S_CONTINUE_NEEDED;
378         ctx->state = EAP_STATE_GSS_CHANNEL_BINDINGS;
379     } else if ((ctx->flags & CTX_FLAG_EAP_FAIL) || code == 0) {
380         major = GSS_S_FAILURE;
381     }
382
383 cleanup:
384     if (respBuf.value != NULL) {
385         OM_uint32 tmpMajor;
386
387         assert(major == GSS_S_CONTINUE_NEEDED);
388
389         tmpMajor = duplicateBuffer(&tmpMinor, &respBuf, outputToken);
390         if (GSS_ERROR(tmpMajor)) {
391             major = tmpMajor;
392             *minor = tmpMinor;
393         }
394     }
395
396     wpabuf_set(&ctx->initiatorCtx.reqData, NULL, 0);
397     peerConfigFree(&tmpMinor, ctx);
398
399     return major;
400 }
401
402 #if 0
403 static OM_uint32
404 eapGssSmInitKeyTransport(OM_uint32 *minor,
405                          gss_cred_id_t cred,
406                          gss_ctx_id_t ctx,
407                          gss_name_t target,
408                          gss_OID mech,
409                          OM_uint32 reqFlags,
410                          OM_uint32 timeReq,
411                          gss_channel_bindings_t chanBindings,
412                          gss_buffer_t inputToken,
413                          gss_buffer_t outputToken)
414 {
415     GSSEAP_NOT_IMPLEMENTED;
416 }
417
418 static OM_uint32
419 eapGssSmInitSecureAssoc(OM_uint32 *minor,
420                         gss_cred_id_t cred,
421                         gss_ctx_id_t ctx,
422                         gss_name_t target,
423                         gss_OID mech,
424                         OM_uint32 reqFlags,
425                         OM_uint32 timeReq,
426                         gss_channel_bindings_t chanBindings,
427                         gss_buffer_t inputToken,
428                         gss_buffer_t outputToken)
429 {
430     GSSEAP_NOT_IMPLEMENTED;
431 }
432 #endif
433
434 static OM_uint32
435 eapGssSmInitGssChannelBindings(OM_uint32 *minor,
436                                gss_cred_id_t cred,
437                                gss_ctx_id_t ctx,
438                                gss_name_t target,
439                                gss_OID mech,
440                                OM_uint32 reqFlags,
441                                OM_uint32 timeReq,
442                                gss_channel_bindings_t chanBindings,
443                                gss_buffer_t inputToken,
444                                gss_buffer_t outputToken)
445 {
446     OM_uint32 major, tmpMinor;
447     gss_iov_buffer_desc iov[2];
448     gss_buffer_desc buf;
449
450     iov[0].type = GSS_IOV_BUFFER_TYPE_DATA;
451     iov[0].buffer.length = 0;
452     iov[0].buffer.value = NULL;
453
454     iov[1].type = GSS_IOV_BUFFER_TYPE_HEADER | GSS_IOV_BUFFER_FLAG_ALLOCATE;
455     iov[1].buffer.length = 0;
456     iov[1].buffer.value = NULL;
457
458     if (chanBindings != GSS_C_NO_CHANNEL_BINDINGS)
459         iov[0].buffer = chanBindings->application_data;
460
461     major = gssEapWrapOrGetMIC(minor, ctx, FALSE, FALSE, iov, 2,
462                                TOK_TYPE_GSS_CB);
463     if (GSS_ERROR(major))
464         goto cleanup;
465
466     /* Skip past token ID */
467     assert(iov[1].buffer.length > 2);
468     assert(load_uint16_be(iov[1].buffer.value) == TOK_TYPE_GSS_CB);
469
470     buf.length = iov[1].buffer.length - 2;
471     buf.value = (unsigned char *)iov[1].buffer.value + 2;
472
473     major = duplicateBuffer(minor, &buf, outputToken);
474     if (GSS_ERROR(major))
475         goto cleanup;
476
477     major = GSS_S_COMPLETE;
478     ctx->state = EAP_STATE_ESTABLISHED;
479
480 cleanup:
481     gssEapReleaseIov(iov, 2);
482
483     return major;
484 }
485
486 static OM_uint32
487 eapGssSmInitEstablished(OM_uint32 *minor,
488                         gss_cred_id_t cred,
489                         gss_ctx_id_t ctx,
490                         gss_name_t target,
491                         gss_OID mech,
492                         OM_uint32 reqFlags,
493                         OM_uint32 timeReq,
494                         gss_channel_bindings_t chanBindings,
495                         gss_buffer_t inputToken,
496                         gss_buffer_t outputToken)
497 {
498     /* Called with already established context */
499     *minor = EINVAL;
500     return GSS_S_BAD_STATUS;
501 }
502
503 static struct gss_eap_initiator_sm {
504     enum gss_eap_token_type inputTokenType;
505     enum gss_eap_token_type outputTokenType;
506     OM_uint32 (*processToken)(OM_uint32 *,
507                               gss_cred_id_t,
508                               gss_ctx_id_t,
509                               gss_name_t,
510                               gss_OID,
511                               OM_uint32,
512                               OM_uint32,
513                               gss_channel_bindings_t,
514                               gss_buffer_t,
515                               gss_buffer_t);
516 } eapGssInitiatorSm[] = {
517     { TOK_TYPE_EAP_REQ, TOK_TYPE_EAP_RESP,  eapGssSmInitAuthenticate        },
518 #if 0
519     { TOK_TYPE_EAP_REQ, TOK_TYPE_EAP_RESP,  eapGssSmInitKeyTransport        },
520     { TOK_TYPE_EAP_REQ, TOK_TYPE_EAP_RESP,  eapGssSmInitSecureAssoc         },
521 #endif
522     { TOK_TYPE_NONE,    TOK_TYPE_GSS_CB,    eapGssSmInitGssChannelBindings  },
523     { TOK_TYPE_NONE,    TOK_TYPE_NONE,      eapGssSmInitEstablished         },
524 };
525
526 OM_uint32
527 gss_init_sec_context(OM_uint32 *minor,
528                      gss_cred_id_t cred,
529                      gss_ctx_id_t *context_handle,
530                      gss_name_t target_name,
531                      gss_OID mech_type,
532                      OM_uint32 req_flags,
533                      OM_uint32 time_req,
534                      gss_channel_bindings_t input_chan_bindings,
535                      gss_buffer_t input_token,
536                      gss_OID *actual_mech_type,
537                      gss_buffer_t output_token,
538                      OM_uint32 *ret_flags,
539                      OM_uint32 *time_rec)
540 {
541     OM_uint32 major;
542     OM_uint32 tmpMajor, tmpMinor;
543     gss_ctx_id_t ctx = *context_handle;
544     struct gss_eap_initiator_sm *sm = NULL;
545     gss_buffer_desc innerInputToken;
546     gss_buffer_desc innerOutputToken = GSS_C_EMPTY_BUFFER;
547
548     *minor = 0;
549
550     output_token->length = 0;
551     output_token->value = NULL;
552
553     if (cred != GSS_C_NO_CREDENTIAL && !(cred->flags & CRED_FLAG_INITIATE)) {
554         return GSS_S_NO_CRED;
555     }
556
557     if (ctx == GSS_C_NO_CONTEXT) {
558         if (input_token != GSS_C_NO_BUFFER && input_token->length != 0) {
559             return GSS_S_DEFECTIVE_TOKEN;
560         }
561
562         major = gssEapAllocContext(minor, &ctx);
563         if (GSS_ERROR(major))
564             return major;
565
566         ctx->flags |= CTX_FLAG_INITIATOR;
567
568         *context_handle = ctx;
569     }
570
571     GSSEAP_MUTEX_LOCK(&ctx->mutex);
572
573     sm = &eapGssInitiatorSm[ctx->state];
574
575     if (input_token != GSS_C_NO_BUFFER) {
576         major = gssEapVerifyToken(minor, ctx, input_token,
577                                   sm->inputTokenType, &innerInputToken);
578         if (GSS_ERROR(major))
579             goto cleanup;
580     } else {
581         innerInputToken.length = 0;
582         innerInputToken.value = NULL;
583     }
584
585     /*
586      * Advance through state machine whilst empty tokens are emitted and
587      * the status is not GSS_S_COMPLETE or an error status.
588      */
589     do {
590         sm = &eapGssInitiatorSm[ctx->state];
591
592         major = (sm->processToken)(minor,
593                                    cred,
594                                    ctx,
595                                    target_name,
596                                    mech_type,
597                                    req_flags,
598                                    time_req,
599                                    input_chan_bindings,
600                                    &innerInputToken,
601                                    &innerOutputToken);
602         if (GSS_ERROR(major))
603             goto cleanup;
604     } while (major == GSS_S_CONTINUE_NEEDED && innerOutputToken.value == NULL);
605
606     if (actual_mech_type != NULL) {
607         if (!gssEapInternalizeOid(ctx->mechanismUsed, actual_mech_type))
608             duplicateOid(&tmpMinor, ctx->mechanismUsed, actual_mech_type);
609     }
610     if (innerOutputToken.value != NULL) {
611         tmpMajor = gssEapMakeToken(&tmpMinor, ctx, &innerOutputToken,
612                                    sm->outputTokenType, output_token);
613         if (GSS_ERROR(tmpMajor)) {
614             major = tmpMajor;
615             *minor = tmpMinor;
616             goto cleanup;
617         }
618     }
619     if (ret_flags != NULL)
620         *ret_flags = ctx->gssFlags;
621     if (time_rec != NULL)
622         gss_context_time(&tmpMinor, ctx, time_rec);
623
624     assert(ctx->state == EAP_STATE_ESTABLISHED || major == GSS_S_CONTINUE_NEEDED);
625
626 cleanup:
627     GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
628
629     if (GSS_ERROR(major))
630         gssEapReleaseContext(&tmpMinor, context_handle);
631
632     gss_release_buffer(&tmpMinor, &innerOutputToken);
633
634     return major;
635 }