For initialContextToken, emit zero lengths inner token
[mech_eap.orig] / accept_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 #define EAP_MAX_METHODS 8
36
37 #define EAP_TTLS_AUTH_PAP 1
38 #define EAP_TTLS_AUTH_CHAP 2
39 #define EAP_TTLS_AUTH_MSCHAP 4
40 #define EAP_TTLS_AUTH_MSCHAPV2 8
41
42 #if 1
43 struct eap_user {
44         struct {
45                 int vendor;
46                 u32 method;
47         } methods[EAP_MAX_METHODS];
48         u8 *password;
49         size_t password_len;
50         int password_hash; /* whether password is hashed with
51                             * nt_password_hash() */
52         int phase2;
53         int force_version;
54         int ttls_auth; /* bitfield of
55                         * EAP_TTLS_AUTH_{PAP,CHAP,MSCHAP,MSCHAPV2} */
56 };
57
58 struct eap_eapol_interface {
59         /* Lower layer to full authenticator variables */
60         Boolean eapResp; /* shared with EAPOL Backend Authentication */
61         struct wpabuf *eapRespData;
62         Boolean portEnabled;
63         int retransWhile;
64         Boolean eapRestart; /* shared with EAPOL Authenticator PAE */
65         int eapSRTT;
66         int eapRTTVAR;
67
68         /* Full authenticator to lower layer variables */
69         Boolean eapReq; /* shared with EAPOL Backend Authentication */
70         Boolean eapNoReq; /* shared with EAPOL Backend Authentication */
71         Boolean eapSuccess;
72         Boolean eapFail;
73         Boolean eapTimeout;
74         struct wpabuf *eapReqData;
75         u8 *eapKeyData;
76         size_t eapKeyDataLen;
77         Boolean eapKeyAvailable; /* called keyAvailable in IEEE 802.1X-2004 */
78
79         /* AAA interface to full authenticator variables */
80         Boolean aaaEapReq;
81         Boolean aaaEapNoReq;
82         Boolean aaaSuccess;
83         Boolean aaaFail;
84         struct wpabuf *aaaEapReqData;
85         u8 *aaaEapKeyData;
86         size_t aaaEapKeyDataLen;
87         Boolean aaaEapKeyAvailable;
88         int aaaMethodTimeout;
89
90         /* Full authenticator to AAA interface variables */
91         Boolean aaaEapResp;
92         struct wpabuf *aaaEapRespData;
93         /* aaaIdentity -> eap_get_identity() */
94         Boolean aaaTimeout;
95 };
96
97 #define eapol_callbacks     SERVER_eapol_callbacks
98
99 struct eapol_callbacks {
100         int (*get_eap_user)(void *ctx, const u8 *identity, size_t identity_len,
101                             int phase2, struct eap_user *user);
102         const char * (*get_eap_req_id_text)(void *ctx, size_t *len);
103 };
104
105 #define eap_config          SERVER_eap_config
106
107 struct eap_config {
108         void *ssl_ctx;
109         void *msg_ctx;
110         void *eap_sim_db_priv;
111         Boolean backend_auth;
112         int eap_server;
113         u8 *pac_opaque_encr_key;
114         u8 *eap_fast_a_id;
115         size_t eap_fast_a_id_len;
116         char *eap_fast_a_id_info;
117         int eap_fast_prov;
118         int pac_key_lifetime;
119         int pac_key_refresh_time;
120         int eap_sim_aka_result_ind;
121         int tnc;
122         struct wps_context *wps;
123         const struct wpabuf *assoc_wps_ie;
124         const u8 *peer_addr;
125         int fragment_size;
126 };
127
128 struct eap_sm * eap_server_sm_init(void *eapol_ctx,
129                                    struct eapol_callbacks *eapol_cb,
130                                    struct eap_config *eap_conf);
131 void eap_server_sm_deinit(struct eap_sm *sm);
132 int eap_server_sm_step(struct eap_sm *sm);
133 void eap_sm_notify_cached(struct eap_sm *sm);
134 void eap_sm_pending_cb(struct eap_sm *sm);
135 int eap_sm_method_pending(struct eap_sm *sm);
136 const u8 * eap_get_identity(struct eap_sm *sm, size_t *len);
137 struct eap_eapol_interface * eap_get_interface(struct eap_sm *sm);
138
139 #include <eap_server/eap_i.h>
140
141 static OM_uint32
142 initTls(OM_uint32 *minor,
143         gss_ctx_id_t ctx)
144 {
145     struct tls_config tconf;
146     struct tls_connection_params tparams;
147
148     memset(&tconf, 0, sizeof(tconf));
149     ctx->acceptorCtx.tlsContext = tls_init(&tconf);
150     if (ctx->acceptorCtx.tlsContext == NULL)
151         return GSS_S_FAILURE;
152
153     memset(&tparams, 0, sizeof(tparams));
154     tparams.ca_cert = "ca.pem";
155     tparams.client_cert = "server.pem";
156     tparams.private_key = "server-key.pem";
157
158     if (tls_global_set_params(ctx->acceptorCtx.tlsContext, &tparams)) {
159         return GSS_S_FAILURE;
160     }
161
162     if (tls_global_set_verify(ctx->acceptorCtx.tlsContext, 0)) {
163         return GSS_S_FAILURE;
164     }
165
166     return GSS_S_COMPLETE;
167 }
168
169 static int
170 serverGetEapUser(void *ctx,
171                  const unsigned char *identity,
172                  size_t identityLength,
173                  int phase2,
174                  struct eap_user *user)
175 {
176     gss_ctx_id_t gssCtx = (gss_ctx_id_t)ctx;
177     OM_uint32 major, minor;
178     gss_buffer_desc buf;
179
180     memset(user, 0, sizeof(*user));
181
182     buf.length = identityLength;
183     buf.value = (void *)identity;
184
185     if (phase2 == 0) {
186         user->methods[0].vendor = EAP_VENDOR_IETF;
187         user->methods[0].method = EAP_TYPE_PEAP;
188         return 0;
189     }
190
191     major = gssEapImportName(&minor, &buf, GSS_C_NT_USER_NAME,
192                              &gssCtx->initiatorName);
193     if (GSS_ERROR(major))
194         return -1;
195
196     /*
197      * OK, obviously there is no real security here, this is simply
198      * for testing the token exchange; this code will be completely
199      * replaced with libradsec once that library is available.
200      */
201     user->methods[0].vendor = EAP_VENDOR_IETF;
202     user->methods[0].method = EAP_TYPE_MSCHAPV2;
203     user->password = (unsigned char *)strdup(" ");
204     user->password_len = 1;
205
206     return 0;
207 }
208
209 static const char *
210 serverGetEapReqIdText(void *ctx,
211                       size_t *len)
212 {
213     *len = 0;
214     return NULL;
215 }
216 #endif
217
218 static OM_uint32
219 completeAccept(OM_uint32 *minor, gss_ctx_id_t ctx)
220 {
221     OM_uint32 major;
222     krb5_context krbContext;
223
224     GSSEAP_KRB_INIT(&krbContext);
225
226     /* Cache encryption type derived from selected mechanism OID */
227     major = gssEapOidToEnctype(minor, ctx->mechanismUsed, &ctx->encryptionType);
228     if (GSS_ERROR(major))
229         return major;
230
231     if (ctx->encryptionType != ENCTYPE_NULL &&
232         ctx->acceptorCtx.eapPolInterface->eapKeyAvailable) {
233         major = gssEapDeriveRFC3961Key(minor,
234                                        ctx->acceptorCtx.eapPolInterface->eapKeyData,
235                                        ctx->acceptorCtx.eapPolInterface->eapKeyDataLen,
236                                        ctx->encryptionType,
237                                        &ctx->rfc3961Key);
238         if (GSS_ERROR(major))
239             return major;
240     } else {
241         /*
242          * draft-howlett-eap-gss says that integrity/confidentialty should
243          * always be advertised as available, but if we have no keying
244          * material it seems confusing to the caller to advertise this.
245          */
246         ctx->gssFlags &= ~(GSS_C_INTEG_FLAG | GSS_C_CONF_FLAG);
247     }
248
249     sequenceInit(&ctx->seqState, ctx->recvSeq,
250                  ((ctx->gssFlags & GSS_C_REPLAY_FLAG) != 0),
251                  ((ctx->gssFlags & GSS_C_SEQUENCE_FLAG) != 0),
252                  TRUE);
253
254     return GSS_S_COMPLETE;
255 }
256
257 static OM_uint32
258 eapGssSmAcceptAuthenticate(OM_uint32 *minor,
259                            gss_ctx_id_t ctx,
260                            gss_cred_id_t cred,
261                            gss_buffer_t inputToken,
262                            gss_channel_bindings_t chanBindings,
263                            gss_buffer_t outputToken)
264 {
265     OM_uint32 major;
266     OM_uint32 tmpMinor, tmpMajor;
267     int code;
268     struct wpabuf respData;
269     static struct eapol_callbacks cb = { serverGetEapUser, serverGetEapReqIdText };
270     if (ctx->acceptorCtx.eap == NULL) {
271         struct eap_config eapConfig;
272
273         major = initTls(minor, ctx);
274         if (GSS_ERROR(major))
275             goto cleanup;
276
277         memset(&eapConfig, 0, sizeof(eapConfig));
278         eapConfig.eap_server = 1;
279         eapConfig.ssl_ctx = ctx->acceptorCtx.tlsContext;
280
281         ctx->acceptorCtx.eap = eap_server_sm_init(ctx, &cb, &eapConfig);
282         if (ctx->acceptorCtx.eap == NULL) {
283             major = GSS_S_FAILURE;
284             goto cleanup;
285         }
286
287         ctx->acceptorCtx.eapPolInterface = eap_get_interface(ctx->acceptorCtx.eap);
288         ctx->acceptorCtx.eapPolInterface->portEnabled = TRUE;
289         ctx->acceptorCtx.eapPolInterface->eapRestart = TRUE;
290     }
291
292     if (ctx->acceptorName == GSS_C_NO_NAME &&
293         cred != GSS_C_NO_CREDENTIAL &&
294         cred->name != GSS_C_NO_NAME) {
295         major = gss_duplicate_name(minor, cred->name, &ctx->acceptorName);
296         if (GSS_ERROR(major))
297             goto cleanup;
298     }
299
300     wpabuf_set(&respData, inputToken->value, inputToken->length);
301     ctx->acceptorCtx.eapPolInterface->eapRespData = &respData;
302     ctx->acceptorCtx.eapPolInterface->eapResp = TRUE;
303
304     code = eap_server_sm_step(ctx->acceptorCtx.eap);
305
306     if (ctx->acceptorCtx.eapPolInterface->eapReq) {
307         ctx->acceptorCtx.eapPolInterface->eapReq = 0;
308         major = GSS_S_CONTINUE_NEEDED;
309     }
310
311     if (ctx->acceptorCtx.eapPolInterface->eapSuccess) {
312         ctx->acceptorCtx.eapPolInterface->eapSuccess = 0;
313         ctx->state = EAP_STATE_ESTABLISHED;
314         major = completeAccept(minor, ctx);
315     } else if (ctx->acceptorCtx.eapPolInterface->eapFail) {
316         ctx->acceptorCtx.eapPolInterface->eapFail = 0;
317         major = GSS_S_FAILURE;
318     } else if (code == 0) {
319         major = GSS_S_FAILURE;
320     }
321
322     if (ctx->acceptorCtx.eapPolInterface->eapReqData != NULL) {
323         gss_buffer_desc buf;
324
325         buf.length = wpabuf_len(ctx->acceptorCtx.eapPolInterface->eapReqData);
326         buf.value = (void *)wpabuf_head(ctx->acceptorCtx.eapPolInterface->eapReqData);
327
328         tmpMajor = duplicateBuffer(&tmpMinor, &buf, outputToken);
329         if (GSS_ERROR(tmpMajor)) {
330             major = tmpMajor;
331             *minor = tmpMinor;
332             goto cleanup;
333         }
334     }
335
336 cleanup:
337     ctx->acceptorCtx.eapPolInterface->eapRespData = NULL;
338
339     return major;
340 }
341
342 static OM_uint32
343 eapGssSmAcceptEstablished(OM_uint32 *minor,
344                           gss_ctx_id_t ctx,
345                           gss_cred_id_t cred,
346                           gss_buffer_t inputToken,
347                           gss_channel_bindings_t chanBindings,
348                           gss_buffer_t outputToken)
349 {
350     /* Called with already established context */
351     *minor = EINVAL;
352     return GSS_S_BAD_STATUS;
353 }
354
355 static struct eap_gss_acceptor_sm {
356     enum gss_eap_token_type inputTokenType;
357     enum gss_eap_token_type outputTokenType;
358     OM_uint32 (*processToken)(OM_uint32 *,
359                               gss_ctx_id_t,
360                               gss_cred_id_t,
361                               gss_buffer_t,
362                               gss_channel_bindings_t,
363                               gss_buffer_t);
364 } eapGssAcceptorSm[] = {
365     { TOK_TYPE_EAP_RESP,    TOK_TYPE_EAP_REQ,  eapGssSmAcceptAuthenticate   },
366     { TOK_TYPE_EAP_RESP,    TOK_TYPE_EAP_REQ,  NULL                         },
367     { TOK_TYPE_EAP_RESP,    TOK_TYPE_EAP_REQ,  NULL                         },
368     { TOK_TYPE_GSS_CB,      TOK_TYPE_NONE,     NULL                         },
369     { TOK_TYPE_NONE,        TOK_TYPE_NONE,     eapGssSmAcceptEstablished    },
370 };
371
372 OM_uint32
373 gss_accept_sec_context(OM_uint32 *minor,
374                        gss_ctx_id_t *context_handle,
375                        gss_cred_id_t cred,
376                        gss_buffer_t input_token,
377                        gss_channel_bindings_t input_chan_bindings,
378                        gss_name_t *src_name,
379                        gss_OID *mech_type,
380                        gss_buffer_t output_token,
381                        OM_uint32 *ret_flags,
382                        OM_uint32 *time_rec,
383                        gss_cred_id_t *delegated_cred_handle)
384 {
385     OM_uint32 major;
386     OM_uint32 tmpMajor, tmpMinor;
387     gss_ctx_id_t ctx = *context_handle;
388     struct eap_gss_acceptor_sm *sm = NULL;
389     gss_buffer_desc innerInputToken, innerOutputToken;
390
391     *minor = 0;
392
393     innerOutputToken.length = 0;
394     innerOutputToken.value = NULL;
395
396     output_token->length = 0;
397     output_token->value = NULL;
398
399     if (cred != GSS_C_NO_CREDENTIAL && !(cred->flags & CRED_FLAG_ACCEPT)) {
400         return GSS_S_NO_CRED;
401     }
402
403     if (input_token == GSS_C_NO_BUFFER || input_token->length == 0) {
404         return GSS_S_DEFECTIVE_TOKEN;
405     }
406
407     if (ctx == GSS_C_NO_CONTEXT) {
408         major = gssEapAllocContext(minor, &ctx);
409         if (GSS_ERROR(major))
410             return major;
411
412         *context_handle = ctx;
413     }
414
415     GSSEAP_MUTEX_LOCK(&ctx->mutex);
416
417     sm = &eapGssAcceptorSm[ctx->state];
418
419     major = gssEapVerifyToken(minor, ctx, input_token,
420                               sm->inputTokenType, &innerInputToken);
421     if (GSS_ERROR(major))
422         goto cleanup;
423
424     do {
425         major = (sm->processToken)(minor,
426                                    ctx,
427                                    cred,
428                                    &innerInputToken,
429                                    input_chan_bindings,
430                                    &innerOutputToken);
431         if (GSS_ERROR(major))
432             goto cleanup;
433     } while (major == GSS_S_CONTINUE_NEEDED && innerOutputToken.length == 0);
434
435     if (mech_type != NULL) {
436         if (!gssEapInternalizeOid(ctx->mechanismUsed, mech_type))
437             duplicateOid(&tmpMinor, ctx->mechanismUsed, mech_type);
438     }
439     if (innerOutputToken.length != 0) {
440         tmpMajor = gssEapMakeToken(&tmpMinor, ctx, &innerOutputToken,
441                                    sm->outputTokenType, output_token);
442         if (GSS_ERROR(tmpMajor)) {
443             major = tmpMajor;
444             *minor = tmpMinor;
445             goto cleanup;
446         }
447     }
448     if (ret_flags != NULL)
449         *ret_flags = ctx->gssFlags;
450     if (delegated_cred_handle != NULL)
451         *delegated_cred_handle = GSS_C_NO_CREDENTIAL;
452
453     if (major == GSS_S_COMPLETE) {
454         if (src_name != NULL && ctx->initiatorName != GSS_C_NO_NAME) {
455             major = gss_duplicate_name(&tmpMinor, ctx->initiatorName, src_name);
456             if (GSS_ERROR(major))
457                 goto cleanup;
458         }
459         if (time_rec != NULL)
460             gss_context_time(&tmpMinor, ctx, time_rec);
461     }
462
463     assert(ctx->state == EAP_STATE_ESTABLISHED || major == GSS_S_CONTINUE_NEEDED);
464
465 cleanup:
466     GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
467
468     if (GSS_ERROR(major))
469         gssEapReleaseContext(&tmpMinor, context_handle);
470
471     gss_release_buffer(&tmpMinor, &innerOutputToken);
472
473     return major;
474 }