Support for libradius
[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 RC_CONFIG_FILE      SYSCONFDIR "/radiusclient/radiusclient.conf"
36
37 /*
38  * Mark a context as ready for cryptographic operations
39  */
40 static OM_uint32
41 acceptReady(OM_uint32 *minor, gss_ctx_id_t ctx)
42 {
43     OM_uint32 major;
44     VALUE_PAIR *vp;
45     gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
46
47     /* Cache encryption type derived from selected mechanism OID */
48     major = gssEapOidToEnctype(minor, ctx->mechanismUsed,
49                                &ctx->encryptionType);
50     if (GSS_ERROR(major))
51         return major;
52
53     vp = rc_avpair_get(ctx->acceptorCtx.avps, PW_USER_NAME, 0);
54     if (vp != NULL) {
55         nameBuf.length = vp->lvalue;
56         nameBuf.value = vp->strvalue;
57     } else {
58         ctx->gssFlags |= GSS_C_ANON_FLAG;
59     }
60
61     major = gssEapImportName(minor, &nameBuf, GSS_C_NT_USER_NAME,
62                              &ctx->initiatorName);
63     if (GSS_ERROR(major))
64         return major;
65
66     vp = rc_avpair_get(ctx->acceptorCtx.avps, PW_MSCHAP2_SUCCESS, 0);
67     if (ctx->encryptionType != ENCTYPE_NULL && vp != NULL) {
68         major = gssEapDeriveRfc3961Key(minor,
69                                        (unsigned char *)vp->strvalue,
70                                        vp->lvalue,
71                                        ctx->encryptionType,
72                                        &ctx->rfc3961Key);
73         if (GSS_ERROR(major))
74             return major;
75
76         major = rfc3961ChecksumTypeForKey(minor, &ctx->rfc3961Key,
77                                            &ctx->checksumType);
78         if (GSS_ERROR(major))
79             return major;
80     } else {
81         /*
82          * draft-howlett-eap-gss says that integrity/confidentialty should
83          * always be advertised as available, but if we have no keying
84          * material it seems confusing to the caller to advertise this.
85          */
86         ctx->gssFlags &= ~(GSS_C_INTEG_FLAG | GSS_C_CONF_FLAG);
87     }
88
89     major = sequenceInit(minor,
90                          &ctx->seqState, ctx->recvSeq,
91                          ((ctx->gssFlags & GSS_C_REPLAY_FLAG) != 0),
92                          ((ctx->gssFlags & GSS_C_SEQUENCE_FLAG) != 0),
93                          TRUE);
94     if (GSS_ERROR(major))
95         return major;
96
97     return GSS_S_COMPLETE;
98 }
99
100 static OM_uint32
101 eapGssSmAcceptAuthenticate(OM_uint32 *minor,
102                            gss_ctx_id_t ctx,
103                            gss_cred_id_t cred,
104                            gss_buffer_t inputToken,
105                            gss_channel_bindings_t chanBindings,
106                            gss_buffer_t outputToken)
107 {
108     OM_uint32 major;
109     OM_uint32 service = PW_AUTHENTICATE_ONLY;
110     int code;
111     VALUE_PAIR *send = NULL;
112     VALUE_PAIR *received = NULL;
113     rc_handle *rh = ctx->acceptorCtx.radHandle;
114     char msgBuffer[4096];
115
116     if (rh == NULL) {
117         rh = ctx->acceptorCtx.radHandle = rc_read_config(RC_CONFIG_FILE);
118         if (rh == NULL) {
119             *minor = errno;
120             major = GSS_S_FAILURE;
121             goto cleanup;
122         }
123
124         if (rc_read_dictionary(rh, rc_conf_str(rh, "dictionary")) != 0) {
125             *minor = errno;
126             major = GSS_S_FAILURE;
127             goto cleanup;
128         }
129     }
130
131     if (ctx->acceptorName == GSS_C_NO_NAME &&
132         cred != GSS_C_NO_CREDENTIAL &&
133         cred->name != GSS_C_NO_NAME) {
134         major = gss_duplicate_name(minor, cred->name, &ctx->acceptorName);
135         if (GSS_ERROR(major))
136             goto cleanup;
137     }
138
139     if (rc_avpair_add(rh, &send, PW_EAP_MESSAGE,
140                       inputToken->value, inputToken->length, 0) == NULL) {
141         *minor = ENOMEM;
142         major = GSS_S_FAILURE;
143         goto cleanup;
144     }
145
146     if (rc_avpair_add(rh, &send, PW_SERVICE_TYPE, &service, -1, 0) == NULL) {
147         *minor = ENOMEM;
148         major = GSS_S_FAILURE;
149         goto cleanup;
150     }
151
152     code = rc_auth(rh, 0, send, &received, msgBuffer);
153     if (code != 0) {
154         *minor = errno;
155         major = GSS_S_UNAVAILABLE;
156         goto cleanup;
157     }
158
159     if (code == OK_RC || code == PW_ACCESS_CHALLENGE) {
160         VALUE_PAIR *eapResponse;
161         gss_buffer_desc eapBuf = GSS_C_EMPTY_BUFFER;
162
163         eapResponse = rc_avpair_get(received, PW_EAP_MESSAGE, 0);
164         if (eapResponse != NULL) {
165             eapBuf.length = eapResponse->lvalue;
166             eapBuf.value = eapResponse->strvalue;
167         }
168
169         major = duplicateBuffer(minor, &eapBuf, outputToken);
170         if (GSS_ERROR(major))
171             goto cleanup;
172
173         major = GSS_S_CONTINUE_NEEDED;
174     } else {
175         major = GSS_S_FAILURE;
176         goto cleanup;
177     }
178
179     if (code == OK_RC) {
180         ctx->acceptorCtx.avps = received;
181         received = NULL;
182
183         major = acceptReady(minor, ctx);
184         if (GSS_ERROR(major))
185             goto cleanup;
186
187         ctx->state = EAP_STATE_GSS_CHANNEL_BINDINGS;
188         major = GSS_S_CONTINUE_NEEDED;
189     }
190
191 cleanup:
192     if (received != NULL)
193         rc_avpair_free(received);
194
195     return major;
196 }
197
198 static OM_uint32
199 eapGssSmAcceptGssChannelBindings(OM_uint32 *minor,
200                                  gss_ctx_id_t ctx,
201                                  gss_cred_id_t cred,
202                                  gss_buffer_t inputToken,
203                                  gss_channel_bindings_t chanBindings,
204                                  gss_buffer_t outputToken)
205 {
206     OM_uint32 major, tmpMinor;
207     gss_iov_buffer_desc iov[2];
208
209     outputToken->length = 0;
210     outputToken->value = NULL;
211
212     if (chanBindings == GSS_C_NO_CHANNEL_BINDINGS) {
213         ctx->state = EAP_STATE_ESTABLISHED;
214         return GSS_S_COMPLETE;
215     }
216
217     if (inputToken->length < 14) {
218         return GSS_S_DEFECTIVE_TOKEN;
219     }
220
221     iov[0].type = GSS_IOV_BUFFER_TYPE_DATA;
222     iov[0].buffer.length = 0;
223     iov[0].buffer.value = NULL;
224
225     if (chanBindings != GSS_C_NO_CHANNEL_BINDINGS)
226         iov[0].buffer = chanBindings->application_data;
227
228     iov[1].type = GSS_IOV_BUFFER_TYPE_HEADER;
229     iov[1].buffer.length = 16;
230     iov[1].buffer.value = (unsigned char *)inputToken->value - 2;
231
232     assert(load_uint16_be(iov[1].buffer.value) == TOK_TYPE_GSS_CB);
233
234     iov[2].type = GSS_IOV_BUFFER_TYPE_TRAILER;
235     iov[2].buffer.length = inputToken->length - 14;
236     iov[2].buffer.value = (unsigned char *)inputToken->value + 14;
237
238     major = gssEapUnwrapOrVerifyMIC(minor, ctx, NULL, NULL,
239                                     iov, 3, TOK_TYPE_GSS_CB);
240     if (major == GSS_S_COMPLETE) {
241         ctx->state = EAP_STATE_ESTABLISHED;
242     }
243
244 #if 0
245     gss_release_buffer(&tmpMinor, &iov[0].buffer);
246 #endif
247
248     return major;
249 }
250
251 static OM_uint32
252 eapGssSmAcceptEstablished(OM_uint32 *minor,
253                           gss_ctx_id_t ctx,
254                           gss_cred_id_t cred,
255                           gss_buffer_t inputToken,
256                           gss_channel_bindings_t chanBindings,
257                           gss_buffer_t outputToken)
258 {
259     /* Called with already established context */
260     *minor = EINVAL;
261     return GSS_S_BAD_STATUS;
262 }
263
264 static struct gss_eap_acceptor_sm {
265     enum gss_eap_token_type inputTokenType;
266     enum gss_eap_token_type outputTokenType;
267     OM_uint32 (*processToken)(OM_uint32 *,
268                               gss_ctx_id_t,
269                               gss_cred_id_t,
270                               gss_buffer_t,
271                               gss_channel_bindings_t,
272                               gss_buffer_t);
273 } eapGssAcceptorSm[] = {
274     { TOK_TYPE_EAP_RESP,    TOK_TYPE_EAP_REQ,  eapGssSmAcceptAuthenticate       },
275 #if 0
276     { TOK_TYPE_EAP_RESP,    TOK_TYPE_EAP_REQ,  NULL                             },
277     { TOK_TYPE_EAP_RESP,    TOK_TYPE_EAP_REQ,  NULL                             },
278 #endif
279     { TOK_TYPE_GSS_CB,      TOK_TYPE_NONE,     eapGssSmAcceptGssChannelBindings },
280     { TOK_TYPE_NONE,        TOK_TYPE_NONE,     eapGssSmAcceptEstablished        },
281 };
282
283 OM_uint32
284 gss_accept_sec_context(OM_uint32 *minor,
285                        gss_ctx_id_t *context_handle,
286                        gss_cred_id_t cred,
287                        gss_buffer_t input_token,
288                        gss_channel_bindings_t input_chan_bindings,
289                        gss_name_t *src_name,
290                        gss_OID *mech_type,
291                        gss_buffer_t output_token,
292                        OM_uint32 *ret_flags,
293                        OM_uint32 *time_rec,
294                        gss_cred_id_t *delegated_cred_handle)
295 {
296     OM_uint32 major;
297     OM_uint32 tmpMajor, tmpMinor;
298     gss_ctx_id_t ctx = *context_handle;
299     struct gss_eap_acceptor_sm *sm = NULL;
300     gss_buffer_desc innerInputToken = GSS_C_EMPTY_BUFFER;
301     gss_buffer_desc innerOutputToken = GSS_C_EMPTY_BUFFER;
302
303     *minor = 0;
304
305     output_token->length = 0;
306     output_token->value = NULL;
307
308     if (cred != GSS_C_NO_CREDENTIAL && !(cred->flags & CRED_FLAG_ACCEPT)) {
309         return GSS_S_NO_CRED;
310     }
311
312     if (input_token == GSS_C_NO_BUFFER || input_token->length == 0) {
313         return GSS_S_DEFECTIVE_TOKEN;
314     }
315
316     if (ctx == GSS_C_NO_CONTEXT) {
317         major = gssEapAllocContext(minor, &ctx);
318         if (GSS_ERROR(major))
319             return major;
320
321         *context_handle = ctx;
322     }
323
324     GSSEAP_MUTEX_LOCK(&ctx->mutex);
325
326     sm = &eapGssAcceptorSm[ctx->state];
327
328     major = gssEapVerifyToken(minor, ctx, input_token,
329                               sm->inputTokenType, &innerInputToken);
330     if (GSS_ERROR(major))
331         goto cleanup;
332
333     /* If credentials were provided, check they're usable with this mech */
334     if (!gssEapCredAvailable(cred, ctx->mechanismUsed)) {
335         major = GSS_S_BAD_MECH;
336         goto cleanup;
337     }
338
339     do {
340         sm = &eapGssAcceptorSm[ctx->state];
341
342         major = (sm->processToken)(minor,
343                                    ctx,
344                                    cred,
345                                    &innerInputToken,
346                                    input_chan_bindings,
347                                    &innerOutputToken);
348         if (GSS_ERROR(major))
349             goto cleanup;
350     } while (major == GSS_S_CONTINUE_NEEDED && innerOutputToken.length == 0);
351
352     if (mech_type != NULL) {
353         if (!gssEapInternalizeOid(ctx->mechanismUsed, mech_type))
354             duplicateOid(&tmpMinor, ctx->mechanismUsed, mech_type);
355     }
356     if (innerOutputToken.length != 0) {
357         tmpMajor = gssEapMakeToken(&tmpMinor, ctx, &innerOutputToken,
358                                    sm->outputTokenType, output_token);
359         if (GSS_ERROR(tmpMajor)) {
360             major = tmpMajor;
361             *minor = tmpMinor;
362             goto cleanup;
363         }
364     }
365     if (ret_flags != NULL)
366         *ret_flags = ctx->gssFlags;
367     if (delegated_cred_handle != NULL)
368         *delegated_cred_handle = GSS_C_NO_CREDENTIAL;
369
370     if (major == GSS_S_COMPLETE) {
371         if (src_name != NULL && ctx->initiatorName != GSS_C_NO_NAME) {
372             major = gss_duplicate_name(&tmpMinor, ctx->initiatorName, src_name);
373             if (GSS_ERROR(major))
374                 goto cleanup;
375         }
376         if (time_rec != NULL)
377             gss_context_time(&tmpMinor, ctx, time_rec);
378     }
379
380     assert(ctx->state == EAP_STATE_ESTABLISHED || major == GSS_S_CONTINUE_NEEDED);
381
382 cleanup:
383     GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
384
385     if (GSS_ERROR(major))
386         gssEapReleaseContext(&tmpMinor, context_handle);
387
388     gss_release_buffer(&tmpMinor, &innerOutputToken);
389
390     return major;
391 }
392