330d0ae5e455c6df6907f5ae61656de395858831
[mech_eap.git] / init_sec_context.c
1 /*
2  * Copyright (c) 2010, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 #include "gssapiP_eap.h"
34
35 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;
298
299     respBuf.length = 0;
300     respBuf.value = NULL;
301
302     initialContextToken = (inputToken == GSS_C_NO_BUFFER ||
303                            inputToken->length == 0);
304
305     major = peerConfigInit(minor, cred, ctx, initialContextToken);
306     if (GSS_ERROR(major))
307         goto cleanup;
308
309     if (initialContextToken) {
310         struct eap_config eapConfig;
311
312         memset(&eapConfig, 0, sizeof(eapConfig));
313         ctx->flags |= CTX_FLAG_EAP_PORT_ENABLED;
314
315         ctx->initiatorCtx.eap = eap_peer_sm_init(ctx,
316                                                  &gssEapPolicyCallbacks,
317                                                  ctx,
318                                                  &eapConfig);
319
320         time(&now);
321         if (timeReq == 0 || timeReq == GSS_C_INDEFINITE)
322             ctx->expiryTime = 0;
323         else
324             ctx->expiryTime = now + timeReq;
325
326         major = gss_duplicate_name(minor, cred->name, &ctx->initiatorName);
327         if (GSS_ERROR(major))
328             goto cleanup;
329
330         major = gss_duplicate_name(minor, target, &ctx->acceptorName);
331         if (GSS_ERROR(major))
332             goto cleanup;
333
334         if (mech == GSS_C_NULL_OID || oidEqual(mech, GSS_EAP_MECHANISM)) {
335             major = gssEapDefaultMech(minor, &ctx->mechanismUsed);
336         } else if (gssEapIsConcreteMechanismOid(mech)) {
337             if (!gssEapInternalizeOid(mech, &ctx->mechanismUsed))
338                 major = duplicateOid(minor, mech, &ctx->mechanismUsed);
339         } else {
340             major = GSS_S_BAD_MECH;
341         }
342         if (GSS_ERROR(major))
343             goto cleanup;
344
345         /* If credentials were provided, check they're usable with this mech */
346         if (!gssEapCredAvailable(cred, ctx->mechanismUsed)) {
347             major = GSS_S_BAD_MECH;
348             goto cleanup;
349         }
350
351         respBuf.value = ""; /* emit empty inner token */
352         major = GSS_S_CONTINUE_NEEDED;
353         goto cleanup;
354     } else {
355         ctx->flags |= CTX_FLAG_EAP_REQ; /* we have a Request from the acceptor */
356     }
357
358     wpabuf_set(&ctx->initiatorCtx.reqData,
359                inputToken->value, inputToken->length);
360
361     major = GSS_S_CONTINUE_NEEDED;
362
363     code = eap_peer_sm_step(ctx->initiatorCtx.eap);
364     if (ctx->flags & CTX_FLAG_EAP_RESP) {
365         struct wpabuf *resp;
366
367         ctx->flags &= ~(CTX_FLAG_EAP_RESP);
368
369         resp = eap_get_eapRespData(ctx->initiatorCtx.eap);
370         if (resp != NULL) {
371             respBuf.length = wpabuf_len(resp);
372             respBuf.value = (void *)wpabuf_head(resp);
373         }
374     } else if (ctx->flags & CTX_FLAG_EAP_SUCCESS) {
375         major = initReady(minor, ctx);
376         if (GSS_ERROR(major))
377             goto cleanup;
378
379         ctx->flags &= ~(CTX_FLAG_EAP_SUCCESS);
380         major = GSS_S_CONTINUE_NEEDED;
381         ctx->state = EAP_STATE_GSS_CHANNEL_BINDINGS;
382     } else if ((ctx->flags & CTX_FLAG_EAP_FAIL) || code == 0) {
383         major = GSS_S_FAILURE;
384     }
385
386 cleanup:
387     if (respBuf.value != NULL) {
388         OM_uint32 tmpMajor;
389
390         assert(major == GSS_S_CONTINUE_NEEDED);
391
392         tmpMajor = duplicateBuffer(&tmpMinor, &respBuf, outputToken);
393         if (GSS_ERROR(tmpMajor)) {
394             major = tmpMajor;
395             *minor = tmpMinor;
396         }
397     }
398
399     wpabuf_set(&ctx->initiatorCtx.reqData, NULL, 0);
400     peerConfigFree(&tmpMinor, ctx);
401
402     return major;
403 }
404
405 #if 0
406 static OM_uint32
407 eapGssSmInitKeyTransport(OM_uint32 *minor,
408                          gss_cred_id_t cred,
409                          gss_ctx_id_t ctx,
410                          gss_name_t target,
411                          gss_OID mech,
412                          OM_uint32 reqFlags,
413                          OM_uint32 timeReq,
414                          gss_channel_bindings_t chanBindings,
415                          gss_buffer_t inputToken,
416                          gss_buffer_t outputToken)
417 {
418     GSSEAP_NOT_IMPLEMENTED;
419 }
420
421 static OM_uint32
422 eapGssSmInitSecureAssoc(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 {
433     GSSEAP_NOT_IMPLEMENTED;
434 }
435 #endif
436
437 static OM_uint32
438 eapGssSmInitGssChannelBindings(OM_uint32 *minor,
439                                gss_cred_id_t cred,
440                                gss_ctx_id_t ctx,
441                                gss_name_t target,
442                                gss_OID mech,
443                                OM_uint32 reqFlags,
444                                OM_uint32 timeReq,
445                                gss_channel_bindings_t chanBindings,
446                                gss_buffer_t inputToken,
447                                gss_buffer_t outputToken)
448 {
449     OM_uint32 major, tmpMinor;
450     gss_iov_buffer_desc iov[2];
451     gss_buffer_desc buf;
452
453     iov[0].type = GSS_IOV_BUFFER_TYPE_DATA;
454     iov[0].buffer.length = 0;
455     iov[0].buffer.value = NULL;
456
457     iov[1].type = GSS_IOV_BUFFER_TYPE_HEADER | GSS_IOV_BUFFER_FLAG_ALLOCATE;
458     iov[1].buffer.length = 0;
459     iov[1].buffer.value = NULL;
460
461     if (chanBindings != GSS_C_NO_CHANNEL_BINDINGS)
462         iov[0].buffer = chanBindings->application_data;
463
464     major = gssEapWrapOrGetMIC(minor, ctx, FALSE, FALSE, iov, 2,
465                                TOK_TYPE_GSS_CB);
466     if (GSS_ERROR(major))
467         goto cleanup;
468
469     /* Skip past token ID */
470     assert(iov[1].buffer.length > 2);
471     assert(load_uint16_be(iov[1].buffer.value) == TOK_TYPE_GSS_CB);
472
473     buf.length = iov[1].buffer.length - 2;
474     buf.value = (unsigned char *)iov[1].buffer.value + 2;
475
476     major = duplicateBuffer(minor, &buf, outputToken);
477     if (GSS_ERROR(major))
478         goto cleanup;
479
480     major = GSS_S_COMPLETE;
481     ctx->state = EAP_STATE_ESTABLISHED;
482
483 cleanup:
484     gssEapReleaseIov(iov, 2);
485
486     return major;
487 }
488
489 static OM_uint32
490 eapGssSmInitEstablished(OM_uint32 *minor,
491                         gss_cred_id_t cred,
492                         gss_ctx_id_t ctx,
493                         gss_name_t target,
494                         gss_OID mech,
495                         OM_uint32 reqFlags,
496                         OM_uint32 timeReq,
497                         gss_channel_bindings_t chanBindings,
498                         gss_buffer_t inputToken,
499                         gss_buffer_t outputToken)
500 {
501     /* Called with already established context */
502     *minor = EINVAL;
503     return GSS_S_BAD_STATUS;
504 }
505
506 static struct eap_gss_initiator_sm {
507     enum gss_eap_token_type inputTokenType;
508     enum gss_eap_token_type outputTokenType;
509     OM_uint32 (*processToken)(OM_uint32 *,
510                               gss_cred_id_t,
511                               gss_ctx_id_t,
512                               gss_name_t,
513                               gss_OID,
514                               OM_uint32,
515                               OM_uint32,
516                               gss_channel_bindings_t,
517                               gss_buffer_t,
518                               gss_buffer_t);
519 } eapGssInitiatorSm[] = {
520     { TOK_TYPE_EAP_REQ, TOK_TYPE_EAP_RESP,  eapGssSmInitAuthenticate        },
521 #if 0
522     { TOK_TYPE_EAP_REQ, TOK_TYPE_EAP_RESP,  eapGssSmInitKeyTransport        },
523     { TOK_TYPE_EAP_REQ, TOK_TYPE_EAP_RESP,  eapGssSmInitSecureAssoc         },
524 #endif
525     { TOK_TYPE_NONE,    TOK_TYPE_GSS_CB,    eapGssSmInitGssChannelBindings  },
526     { TOK_TYPE_NONE,    TOK_TYPE_NONE,      eapGssSmInitEstablished         },
527 };
528
529 OM_uint32
530 gss_init_sec_context(OM_uint32 *minor,
531                      gss_cred_id_t cred,
532                      gss_ctx_id_t *context_handle,
533                      gss_name_t target_name,
534                      gss_OID mech_type,
535                      OM_uint32 req_flags,
536                      OM_uint32 time_req,
537                      gss_channel_bindings_t input_chan_bindings,
538                      gss_buffer_t input_token,
539                      gss_OID *actual_mech_type,
540                      gss_buffer_t output_token,
541                      OM_uint32 *ret_flags,
542                      OM_uint32 *time_rec)
543 {
544     OM_uint32 major;
545     OM_uint32 tmpMajor, tmpMinor;
546     gss_ctx_id_t ctx = *context_handle;
547     struct eap_gss_initiator_sm *sm = NULL;
548     gss_buffer_desc innerInputToken, innerOutputToken;
549
550     *minor = 0;
551
552     innerOutputToken.length = 0;
553     innerOutputToken.value = NULL;
554
555     output_token->length = 0;
556     output_token->value = NULL;
557
558     if (cred != GSS_C_NO_CREDENTIAL && !(cred->flags & CRED_FLAG_INITIATE)) {
559         return GSS_S_NO_CRED;
560     }
561
562     if (ctx == GSS_C_NO_CONTEXT) {
563         if (input_token != GSS_C_NO_BUFFER && input_token->length != 0) {
564             return GSS_S_DEFECTIVE_TOKEN;
565         }
566
567         major = gssEapAllocContext(minor, &ctx);
568         if (GSS_ERROR(major))
569             return major;
570
571         ctx->flags |= CTX_FLAG_INITIATOR;
572
573         *context_handle = ctx;
574     }
575
576     GSSEAP_MUTEX_LOCK(&ctx->mutex);
577
578     sm = &eapGssInitiatorSm[ctx->state];
579
580     if (input_token != GSS_C_NO_BUFFER) {
581         major = gssEapVerifyToken(minor, ctx, input_token,
582                                   sm->inputTokenType, &innerInputToken);
583         if (GSS_ERROR(major))
584             goto cleanup;
585     } else {
586         innerInputToken.length = 0;
587         innerInputToken.value = NULL;
588     }
589
590     /*
591      * Advance through state machine whilst empty tokens are emitted and
592      * the status is not GSS_S_COMPLETE or an error status.
593      */
594     do {
595         sm = &eapGssInitiatorSm[ctx->state];
596
597         major = (sm->processToken)(minor,
598                                    cred,
599                                    ctx,
600                                    target_name,
601                                    mech_type,
602                                    req_flags,
603                                    time_req,
604                                    input_chan_bindings,
605                                    &innerInputToken,
606                                    &innerOutputToken);
607         if (GSS_ERROR(major))
608             goto cleanup;
609     } while (major == GSS_S_CONTINUE_NEEDED && innerOutputToken.value == NULL);
610
611     if (actual_mech_type != NULL) {
612         if (!gssEapInternalizeOid(ctx->mechanismUsed, actual_mech_type))
613             duplicateOid(&tmpMinor, ctx->mechanismUsed, actual_mech_type);
614     }
615     if (innerOutputToken.value != NULL) {
616         tmpMajor = gssEapMakeToken(&tmpMinor, ctx, &innerOutputToken,
617                                    sm->outputTokenType, output_token);
618         if (GSS_ERROR(tmpMajor)) {
619             major = tmpMajor;
620             *minor = tmpMinor;
621             goto cleanup;
622         }
623     }
624     if (ret_flags != NULL)
625         *ret_flags = ctx->gssFlags;
626     if (time_rec != NULL)
627         gss_context_time(&tmpMinor, ctx, time_rec);
628
629     assert(ctx->state == EAP_STATE_ESTABLISHED || major == GSS_S_CONTINUE_NEEDED);
630
631 cleanup:
632     GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
633
634     if (GSS_ERROR(major))
635         gssEapReleaseContext(&tmpMinor, context_handle);
636
637     gss_release_buffer(&tmpMinor, &innerOutputToken);
638
639     return major;
640 }