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