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