Support for libradius
[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 static struct eap_peer_config *
74 peerGetConfig(void *ctx)
75 {
76     gss_ctx_id_t gssCtx = (gss_ctx_id_t)ctx;
77
78     return &gssCtx->initiatorCtx.eapPeerConfig;
79 }
80
81 static Boolean
82 peerGetBool(void *data, enum eapol_bool_var variable)
83 {
84     gss_ctx_id_t ctx = data;
85     OM_uint32 flag;
86
87     if (ctx == GSS_C_NO_CONTEXT)
88         return FALSE;
89
90     flag = policyVariableToFlag(variable);
91
92     return ((ctx->flags & flag) != 0);
93 }
94
95 static void
96 peerSetBool(void *data, enum eapol_bool_var variable,
97             Boolean value)
98 {
99     gss_ctx_id_t ctx = data;
100     OM_uint32 flag;
101
102     if (ctx == GSS_C_NO_CONTEXT)
103         return;
104
105     flag = policyVariableToFlag(variable);
106
107     if (value)
108         ctx->flags |= flag;
109     else
110         ctx->flags &= ~(flag);
111 }
112
113 static unsigned int
114 peerGetInt(void *data, enum eapol_int_var variable)
115 {
116     gss_ctx_id_t ctx = data;
117
118     if (ctx == GSS_C_NO_CONTEXT)
119         return FALSE;
120
121     assert(CTX_IS_INITIATOR(ctx));
122
123     switch (variable) {
124     case EAPOL_idleWhile:
125         return ctx->initiatorCtx.idleWhile;
126         break;
127     }
128
129     return 0;
130 }
131
132 static void
133 peerSetInt(void *data, enum eapol_int_var variable,
134            unsigned int value)
135 {
136     gss_ctx_id_t ctx = data;
137
138     if (ctx == GSS_C_NO_CONTEXT)
139         return;
140
141     assert(CTX_IS_INITIATOR(ctx));
142
143     switch (variable) {
144     case EAPOL_idleWhile:
145         ctx->initiatorCtx.idleWhile = value;
146         break;
147     }
148 }
149
150 static struct wpabuf *
151 peerGetEapReqData(void *ctx)
152 {
153     gss_ctx_id_t gssCtx = (gss_ctx_id_t)ctx;
154
155     return &gssCtx->initiatorCtx.reqData;
156 }
157
158 static void
159 peerSetConfigBlob(void *ctx, struct wpa_config_blob *blob)
160 {
161 }
162
163 static const struct wpa_config_blob *
164 peerGetConfigBlob(void *ctx, const char *name)
165 {
166     return NULL;
167 }
168
169 static void
170 peerNotifyPending(void *ctx)
171 {
172 }
173
174 static struct eapol_callbacks gssEapPolicyCallbacks = {
175     peerGetConfig,
176     peerGetBool,
177     peerSetBool,
178     peerGetInt,
179     peerSetInt,
180     peerGetEapReqData,
181     peerSetConfigBlob,
182     peerGetConfigBlob,
183     peerNotifyPending,
184 };
185
186 extern int wpa_debug_level;
187
188 static OM_uint32
189 peerConfigInit(OM_uint32 *minor,
190                gss_cred_id_t cred,
191                gss_ctx_id_t ctx,
192                int loadConfig)
193 {
194     krb5_context krbContext;
195     struct eap_peer_config *eapPeerConfig = &ctx->initiatorCtx.eapPeerConfig;
196     krb5_error_code code;
197     char *identity;
198
199     GSSEAP_KRB_INIT(&krbContext);
200
201     if (loadConfig) {
202         eapPeerConfig->fragment_size = 1024;
203         wpa_debug_level = 0;
204     }
205
206     code = krb5_unparse_name(krbContext, cred->name->krbPrincipal, &identity);
207     if (code != 0) {
208         *minor = code;
209         return GSS_S_FAILURE;
210     }
211
212     eapPeerConfig->identity = (unsigned char *)identity;
213     eapPeerConfig->identity_len = strlen(identity);
214     eapPeerConfig->password = (unsigned char *)cred->password.value;
215     eapPeerConfig->password_len = cred->password.length;
216
217     return GSS_S_COMPLETE;
218 }
219
220 static OM_uint32
221 peerConfigFree(OM_uint32 *minor,
222                gss_ctx_id_t ctx)
223 {
224     krb5_context krbContext;
225     struct eap_peer_config *eapPeerConfig = &ctx->initiatorCtx.eapPeerConfig;
226
227     GSSEAP_KRB_INIT(&krbContext);
228
229     krb5_free_unparsed_name(krbContext, (char *)eapPeerConfig->identity);
230
231     return GSS_S_COMPLETE;
232 }
233
234 static OM_uint32
235 initReady(OM_uint32 *minor, gss_ctx_id_t ctx)
236 {
237     OM_uint32 major;
238     const unsigned char *key;
239     size_t keyLength;
240
241     /* Cache encryption type derived from selected mechanism OID */
242     major = gssEapOidToEnctype(minor, ctx->mechanismUsed, &ctx->encryptionType);
243     if (GSS_ERROR(major))
244         return major;
245
246     if (ctx->encryptionType != ENCTYPE_NULL &&
247         eap_key_available(ctx->initiatorCtx.eap)) {
248         key = eap_get_eapKeyData(ctx->initiatorCtx.eap, &keyLength);
249
250         major = gssEapDeriveRfc3961Key(minor, key, keyLength,
251                                        ctx->encryptionType, &ctx->rfc3961Key);
252         if (GSS_ERROR(major))
253             return major;
254
255         major = rfc3961ChecksumTypeForKey(minor, &ctx->rfc3961Key,
256                                            &ctx->checksumType);
257         if (GSS_ERROR(major))
258             return major;
259     } else {
260         /*
261          * draft-howlett-eap-gss says that integrity/confidentialty should
262          * always be advertised as available, but if we have no keying
263          * material it seems confusing to the caller to advertise this.
264          */
265         ctx->gssFlags &= ~(GSS_C_INTEG_FLAG | GSS_C_CONF_FLAG);
266     }
267
268     major = sequenceInit(minor,
269                          &ctx->seqState,
270                          ctx->recvSeq,
271                          ((ctx->gssFlags & GSS_C_REPLAY_FLAG) != 0),
272                          ((ctx->gssFlags & GSS_C_SEQUENCE_FLAG) != 0),
273                          TRUE);
274     if (GSS_ERROR(major))
275         return major;
276
277     return GSS_S_COMPLETE;
278 }
279
280 static OM_uint32
281 eapGssSmInitAuthenticate(OM_uint32 *minor,
282                          gss_cred_id_t cred,
283                          gss_ctx_id_t ctx,
284                          gss_name_t target,
285                          gss_OID mech,
286                          OM_uint32 reqFlags,
287                          OM_uint32 timeReq,
288                          gss_channel_bindings_t chanBindings,
289                          gss_buffer_t inputToken,
290                          gss_buffer_t outputToken)
291 {
292     OM_uint32 major;
293     OM_uint32 tmpMajor, tmpMinor;
294     time_t now;
295     int initialContextToken = 0, code;
296     gss_buffer_desc respBuf = GSS_C_EMPTY_BUFFER;
297
298     initialContextToken = (inputToken == GSS_C_NO_BUFFER ||
299                            inputToken->length == 0);
300
301     major = peerConfigInit(minor, cred, ctx, initialContextToken);
302     if (GSS_ERROR(major))
303         goto cleanup;
304
305     if (initialContextToken) {
306         struct eap_config eapConfig;
307
308         memset(&eapConfig, 0, sizeof(eapConfig));
309         ctx->flags |= CTX_FLAG_EAP_PORT_ENABLED;
310
311         ctx->initiatorCtx.eap = eap_peer_sm_init(ctx,
312                                                  &gssEapPolicyCallbacks,
313                                                  ctx,
314                                                  &eapConfig);
315
316         time(&now);
317         if (timeReq == 0 || timeReq == GSS_C_INDEFINITE)
318             ctx->expiryTime = 0;
319         else
320             ctx->expiryTime = now + timeReq;
321
322         major = gss_duplicate_name(minor, cred->name, &ctx->initiatorName);
323         if (GSS_ERROR(major))
324             goto cleanup;
325
326         major = gss_duplicate_name(minor, target, &ctx->acceptorName);
327         if (GSS_ERROR(major))
328             goto cleanup;
329
330         if (mech == GSS_C_NULL_OID || oidEqual(mech, GSS_EAP_MECHANISM)) {
331             major = gssEapDefaultMech(minor, &ctx->mechanismUsed);
332         } else if (gssEapIsConcreteMechanismOid(mech)) {
333             if (!gssEapInternalizeOid(mech, &ctx->mechanismUsed))
334                 major = duplicateOid(minor, mech, &ctx->mechanismUsed);
335         } else {
336             major = GSS_S_BAD_MECH;
337         }
338         if (GSS_ERROR(major))
339             goto cleanup;
340
341         /* If credentials were provided, check they're usable with this mech */
342         if (!gssEapCredAvailable(cred, ctx->mechanismUsed)) {
343             major = GSS_S_BAD_MECH;
344             goto cleanup;
345         }
346
347         respBuf.value = ""; /* emit empty inner token */
348         major = GSS_S_CONTINUE_NEEDED;
349         goto cleanup;
350     } else {
351         ctx->flags |= CTX_FLAG_EAP_REQ; /* we have a Request from the acceptor */
352     }
353
354     wpabuf_set(&ctx->initiatorCtx.reqData,
355                inputToken->value, inputToken->length);
356
357     major = GSS_S_CONTINUE_NEEDED;
358
359     code = eap_peer_sm_step(ctx->initiatorCtx.eap);
360     if (ctx->flags & CTX_FLAG_EAP_RESP) {
361         struct wpabuf *resp;
362
363         ctx->flags &= ~(CTX_FLAG_EAP_RESP);
364
365         resp = eap_get_eapRespData(ctx->initiatorCtx.eap);
366         if (resp != NULL) {
367             respBuf.length = wpabuf_len(resp);
368             respBuf.value = (void *)wpabuf_head(resp);
369         }
370     } else if (ctx->flags & CTX_FLAG_EAP_SUCCESS) {
371         major = initReady(minor, ctx);
372         if (GSS_ERROR(major))
373             goto cleanup;
374
375         ctx->flags &= ~(CTX_FLAG_EAP_SUCCESS);
376         major = GSS_S_CONTINUE_NEEDED;
377         ctx->state = EAP_STATE_GSS_CHANNEL_BINDINGS;
378     } else if (ctx->flags & CTX_FLAG_EAP_FAIL) {
379         major = GSS_S_DEFECTIVE_CREDENTIAL;
380     } else if (code == 0) {
381         major = GSS_S_FAILURE;
382     }
383
384 cleanup:
385     if (respBuf.value != NULL) {
386         OM_uint32 tmpMajor;
387
388         assert(major == GSS_S_CONTINUE_NEEDED);
389
390         tmpMajor = duplicateBuffer(&tmpMinor, &respBuf, outputToken);
391         if (GSS_ERROR(tmpMajor)) {
392             major = tmpMajor;
393             *minor = tmpMinor;
394         }
395     }
396
397     wpabuf_set(&ctx->initiatorCtx.reqData, NULL, 0);
398     peerConfigFree(&tmpMinor, ctx);
399
400     return major;
401 }
402
403 #if 0
404 static OM_uint32
405 eapGssSmInitKeyTransport(OM_uint32 *minor,
406                          gss_cred_id_t cred,
407                          gss_ctx_id_t ctx,
408                          gss_name_t target,
409                          gss_OID mech,
410                          OM_uint32 reqFlags,
411                          OM_uint32 timeReq,
412                          gss_channel_bindings_t chanBindings,
413                          gss_buffer_t inputToken,
414                          gss_buffer_t outputToken)
415 {
416     GSSEAP_NOT_IMPLEMENTED;
417 }
418
419 static OM_uint32
420 eapGssSmInitSecureAssoc(OM_uint32 *minor,
421                         gss_cred_id_t cred,
422                         gss_ctx_id_t ctx,
423                         gss_name_t target,
424                         gss_OID mech,
425                         OM_uint32 reqFlags,
426                         OM_uint32 timeReq,
427                         gss_channel_bindings_t chanBindings,
428                         gss_buffer_t inputToken,
429                         gss_buffer_t outputToken)
430 {
431     GSSEAP_NOT_IMPLEMENTED;
432 }
433 #endif
434
435 static OM_uint32
436 eapGssSmInitGssChannelBindings(OM_uint32 *minor,
437                                gss_cred_id_t cred,
438                                gss_ctx_id_t ctx,
439                                gss_name_t target,
440                                gss_OID mech,
441                                OM_uint32 reqFlags,
442                                OM_uint32 timeReq,
443                                gss_channel_bindings_t chanBindings,
444                                gss_buffer_t inputToken,
445                                gss_buffer_t outputToken)
446 {
447     OM_uint32 major, tmpMinor;
448     gss_iov_buffer_desc iov[2];
449     gss_buffer_desc buf;
450
451     iov[0].type = GSS_IOV_BUFFER_TYPE_DATA;
452     iov[0].buffer.length = 0;
453     iov[0].buffer.value = NULL;
454
455     iov[1].type = GSS_IOV_BUFFER_TYPE_HEADER | GSS_IOV_BUFFER_FLAG_ALLOCATE;
456     iov[1].buffer.length = 0;
457     iov[1].buffer.value = NULL;
458
459     if (chanBindings != GSS_C_NO_CHANNEL_BINDINGS)
460         iov[0].buffer = chanBindings->application_data;
461
462     major = gssEapWrapOrGetMIC(minor, ctx, FALSE, FALSE, iov, 2,
463                                TOK_TYPE_GSS_CB);
464     if (GSS_ERROR(major))
465         goto cleanup;
466
467     /* Skip past token ID */
468     assert(iov[1].buffer.length > 2);
469     assert(load_uint16_be(iov[1].buffer.value) == TOK_TYPE_GSS_CB);
470
471     buf.length = iov[1].buffer.length - 2;
472     buf.value = (unsigned char *)iov[1].buffer.value + 2;
473
474     major = duplicateBuffer(minor, &buf, outputToken);
475     if (GSS_ERROR(major))
476         goto cleanup;
477
478     major = GSS_S_COMPLETE;
479     ctx->state = EAP_STATE_ESTABLISHED;
480
481 cleanup:
482     gssEapReleaseIov(iov, 2);
483
484     return major;
485 }
486
487 static OM_uint32
488 eapGssSmInitEstablished(OM_uint32 *minor,
489                         gss_cred_id_t cred,
490                         gss_ctx_id_t ctx,
491                         gss_name_t target,
492                         gss_OID mech,
493                         OM_uint32 reqFlags,
494                         OM_uint32 timeReq,
495                         gss_channel_bindings_t chanBindings,
496                         gss_buffer_t inputToken,
497                         gss_buffer_t outputToken)
498 {
499     /* Called with already established context */
500     *minor = EINVAL;
501     return GSS_S_BAD_STATUS;
502 }
503
504 static struct gss_eap_initiator_sm {
505     enum gss_eap_token_type inputTokenType;
506     enum gss_eap_token_type outputTokenType;
507     OM_uint32 (*processToken)(OM_uint32 *,
508                               gss_cred_id_t,
509                               gss_ctx_id_t,
510                               gss_name_t,
511                               gss_OID,
512                               OM_uint32,
513                               OM_uint32,
514                               gss_channel_bindings_t,
515                               gss_buffer_t,
516                               gss_buffer_t);
517 } eapGssInitiatorSm[] = {
518     { TOK_TYPE_EAP_REQ, TOK_TYPE_EAP_RESP,  eapGssSmInitAuthenticate        },
519 #if 0
520     { TOK_TYPE_EAP_REQ, TOK_TYPE_EAP_RESP,  eapGssSmInitKeyTransport        },
521     { TOK_TYPE_EAP_REQ, TOK_TYPE_EAP_RESP,  eapGssSmInitSecureAssoc         },
522 #endif
523     { TOK_TYPE_NONE,    TOK_TYPE_GSS_CB,    eapGssSmInitGssChannelBindings  },
524     { TOK_TYPE_NONE,    TOK_TYPE_NONE,      eapGssSmInitEstablished         },
525 };
526
527 OM_uint32
528 gss_init_sec_context(OM_uint32 *minor,
529                      gss_cred_id_t cred,
530                      gss_ctx_id_t *context_handle,
531                      gss_name_t target_name,
532                      gss_OID mech_type,
533                      OM_uint32 req_flags,
534                      OM_uint32 time_req,
535                      gss_channel_bindings_t input_chan_bindings,
536                      gss_buffer_t input_token,
537                      gss_OID *actual_mech_type,
538                      gss_buffer_t output_token,
539                      OM_uint32 *ret_flags,
540                      OM_uint32 *time_rec)
541 {
542     OM_uint32 major;
543     OM_uint32 tmpMajor, tmpMinor;
544     gss_ctx_id_t ctx = *context_handle;
545     struct gss_eap_initiator_sm *sm = NULL;
546     gss_buffer_desc innerInputToken;
547     gss_buffer_desc innerOutputToken = GSS_C_EMPTY_BUFFER;
548
549     *minor = 0;
550
551     output_token->length = 0;
552     output_token->value = NULL;
553
554     if (cred != GSS_C_NO_CREDENTIAL && !(cred->flags & CRED_FLAG_INITIATE)) {
555         return GSS_S_NO_CRED;
556     }
557
558     if (ctx == GSS_C_NO_CONTEXT) {
559         if (input_token != GSS_C_NO_BUFFER && input_token->length != 0) {
560             return GSS_S_DEFECTIVE_TOKEN;
561         }
562
563         major = gssEapAllocContext(minor, &ctx);
564         if (GSS_ERROR(major))
565             return major;
566
567         ctx->flags |= CTX_FLAG_INITIATOR;
568
569         *context_handle = ctx;
570     }
571
572     GSSEAP_MUTEX_LOCK(&ctx->mutex);
573
574     sm = &eapGssInitiatorSm[ctx->state];
575
576     if (input_token != GSS_C_NO_BUFFER) {
577         major = gssEapVerifyToken(minor, ctx, input_token,
578                                   sm->inputTokenType, &innerInputToken);
579         if (GSS_ERROR(major))
580             goto cleanup;
581     } else {
582         innerInputToken.length = 0;
583         innerInputToken.value = NULL;
584     }
585
586     /*
587      * Advance through state machine whilst empty tokens are emitted and
588      * the status is not GSS_S_COMPLETE or an error status.
589      */
590     do {
591         sm = &eapGssInitiatorSm[ctx->state];
592
593         major = (sm->processToken)(minor,
594                                    cred,
595                                    ctx,
596                                    target_name,
597                                    mech_type,
598                                    req_flags,
599                                    time_req,
600                                    input_chan_bindings,
601                                    &innerInputToken,
602                                    &innerOutputToken);
603         if (GSS_ERROR(major))
604             goto cleanup;
605     } while (major == GSS_S_CONTINUE_NEEDED && innerOutputToken.value == NULL);
606
607     if (actual_mech_type != NULL) {
608         if (!gssEapInternalizeOid(ctx->mechanismUsed, actual_mech_type))
609             duplicateOid(&tmpMinor, ctx->mechanismUsed, actual_mech_type);
610     }
611     if (innerOutputToken.value != NULL) {
612         tmpMajor = gssEapMakeToken(&tmpMinor, ctx, &innerOutputToken,
613                                    sm->outputTokenType, output_token);
614         if (GSS_ERROR(tmpMajor)) {
615             major = tmpMajor;
616             *minor = tmpMinor;
617             goto cleanup;
618         }
619     }
620     if (ret_flags != NULL)
621         *ret_flags = ctx->gssFlags;
622     if (time_rec != NULL)
623         gss_context_time(&tmpMinor, ctx, time_rec);
624
625     assert(ctx->state == EAP_STATE_ESTABLISHED || major == GSS_S_CONTINUE_NEEDED);
626
627 cleanup:
628     GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
629
630     if (GSS_ERROR(major))
631         gssEapReleaseContext(&tmpMinor, context_handle);
632
633     gss_release_buffer(&tmpMinor, &innerOutputToken);
634
635     return major;
636 }