cleanup radius code a bit
[mech_eap.git] / 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 if (ctx->initiatorName == GSS_C_NO_NAME) {
58         ctx->gssFlags |= GSS_C_ANON_FLAG;
59     }
60
61     if (nameBuf.length != 0 || ctx->initiatorName == GSS_C_NO_NAME) {
62         major = gssEapImportName(minor, &nameBuf, GSS_C_NT_USER_NAME,
63                                  &ctx->initiatorName);
64         if (GSS_ERROR(major))
65             return major;
66     }
67
68     vp = rc_avpair_get(ctx->acceptorCtx.avps, PW_MSCHAP2_SUCCESS, 0);
69     if (ctx->encryptionType != ENCTYPE_NULL && vp != NULL) {
70         major = gssEapDeriveRfc3961Key(minor,
71                                        (unsigned char *)vp->strvalue,
72                                        vp->lvalue,
73                                        ctx->encryptionType,
74                                        &ctx->rfc3961Key);
75         if (GSS_ERROR(major))
76             return major;
77
78         major = rfc3961ChecksumTypeForKey(minor, &ctx->rfc3961Key,
79                                            &ctx->checksumType);
80         if (GSS_ERROR(major))
81             return major;
82     } else {
83         /*
84          * draft-howlett-eap-gss says that integrity/confidentialty should
85          * always be advertised as available, but if we have no keying
86          * material it seems confusing to the caller to advertise this.
87          */
88         ctx->gssFlags &= ~(GSS_C_INTEG_FLAG | GSS_C_CONF_FLAG);
89     }
90
91     major = sequenceInit(minor,
92                          &ctx->seqState, ctx->recvSeq,
93                          ((ctx->gssFlags & GSS_C_REPLAY_FLAG) != 0),
94                          ((ctx->gssFlags & GSS_C_SEQUENCE_FLAG) != 0),
95                          TRUE);
96     if (GSS_ERROR(major))
97         return major;
98
99     return GSS_S_COMPLETE;
100 }
101
102 static OM_uint32
103 eapGssSmAcceptIdentity(OM_uint32 *minor,
104                        gss_ctx_id_t ctx,
105                        gss_cred_id_t cred,
106                        gss_buffer_t inputToken,
107                        gss_channel_bindings_t chanBindings,
108                        gss_buffer_t outputToken)
109 {
110     OM_uint32 major;
111     rc_handle *rh;
112     union {
113         struct eap_hdr pdu;
114         unsigned char data[5];
115     } pkt;
116     gss_buffer_desc pktBuffer;
117     char *config = RC_CONFIG_FILE;
118
119     if (inputToken != GSS_C_NO_BUFFER && inputToken->length != 0)
120         return GSS_S_DEFECTIVE_TOKEN;
121
122     assert(ctx->acceptorCtx.radHandle == NULL);
123
124     if (cred != GSS_C_NO_CREDENTIAL && cred->radiusConfigFile != NULL)
125         config = cred->radiusConfigFile;
126
127     rh = ctx->acceptorCtx.radHandle = rc_read_config(config);
128     if (rh == NULL) {
129         *minor = errno;
130         return GSS_S_FAILURE;
131     }
132
133     if (rc_read_dictionary(rh, rc_conf_str(rh, "dictionary")) != 0) {
134         *minor = errno;
135         return GSS_S_FAILURE;
136     }
137
138     if (ctx->acceptorName == GSS_C_NO_NAME &&
139         cred != GSS_C_NO_CREDENTIAL &&
140         cred->name != GSS_C_NO_NAME) {
141         major = gss_duplicate_name(minor, cred->name, &ctx->acceptorName);
142         if (GSS_ERROR(major))
143             return major;
144     }
145
146     pkt.pdu.code = EAP_CODE_REQUEST;
147     pkt.pdu.identifier = 0;
148     pkt.pdu.length = htons(sizeof(pkt.data));
149     pkt.data[4] = EAP_TYPE_IDENTITY;
150
151     pktBuffer.length = sizeof(pkt.data);
152     pktBuffer.value = pkt.data;
153
154     major = duplicateBuffer(minor, &pktBuffer, outputToken);
155     if (GSS_ERROR(major))
156         return major;
157
158     ctx->state = EAP_STATE_AUTHENTICATE;
159
160     return GSS_S_CONTINUE_NEEDED;
161 }
162
163 static OM_uint32
164 importInitiatorIdentity(OM_uint32 *minor,
165                         gss_ctx_id_t ctx,
166                         gss_buffer_t inputToken,
167                         gss_buffer_t nameBuf)
168 {
169     OM_uint32 major, tmpMinor;
170     struct eap_hdr *pdu = (struct eap_hdr *)inputToken->value;
171     unsigned char *pos = (unsigned char *)(pdu + 1);
172     gss_name_t name;
173
174     assert(pdu->code == EAP_CODE_RESPONSE);
175     assert(pos[0] == EAP_TYPE_IDENTITY);
176
177     nameBuf->value = pos + 1;
178     nameBuf->length = inputToken->length - sizeof(*pdu) - 1;
179
180     major = gssEapImportName(minor, nameBuf, GSS_C_NT_USER_NAME, &name);
181     if (GSS_ERROR(major))
182         return major;
183
184     gssEapReleaseName(&tmpMinor, &ctx->initiatorName);
185     ctx->initiatorName = name;
186
187     return GSS_S_COMPLETE;
188 }
189
190 static OM_uint32
191 eapGssSmAcceptAuthenticate(OM_uint32 *minor,
192                            gss_ctx_id_t ctx,
193                            gss_cred_id_t cred,
194                            gss_buffer_t inputToken,
195                            gss_channel_bindings_t chanBindings,
196                            gss_buffer_t outputToken)
197 {
198     OM_uint32 major, tmpMinor;
199     int code;
200     VALUE_PAIR *send = NULL;
201     VALUE_PAIR *received = NULL;
202     rc_handle *rh = ctx->acceptorCtx.radHandle;
203     char msgBuffer[4096];
204     struct eap_hdr *pdu;
205     unsigned char *pos;
206     gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
207
208     pdu = (struct eap_hdr *)inputToken->value;
209     pos = (unsigned char *)(pdu + 1);
210
211     if (inputToken->length > sizeof(*pdu) &&
212         pdu->code == EAP_CODE_RESPONSE &&
213         pos[0] == EAP_TYPE_IDENTITY) {
214         major = importInitiatorIdentity(minor, ctx, inputToken, &nameBuf);
215         if (GSS_ERROR(major))
216             goto cleanup;
217
218         major = addRadiusAttributeFromBuffer(minor, rh, &send,
219                                              PW_USER_NAME, &nameBuf);
220         if (GSS_ERROR(major))
221             goto cleanup;
222     }
223
224     major = addRadiusAttributeFromBuffer(minor, rh, &send, PW_EAP_MESSAGE,
225                                          inputToken);
226     if (GSS_ERROR(major))
227         goto cleanup;
228
229     if (ctx->acceptorCtx.lastStatus == PW_ACCESS_CHALLENGE) {
230         major = addRadiusAttributeFromBuffer(minor, rh, &send, PW_STATE,
231                                              &ctx->acceptorCtx.state);
232         if (GSS_ERROR(major))
233             goto cleanup;
234
235         gss_release_buffer(&tmpMinor, &ctx->acceptorCtx.state);
236     }
237
238     code = rc_auth(rh, 0, send, &received, msgBuffer);
239     if (code != 0) {
240         *minor = errno;
241         major = GSS_S_UNAVAILABLE;
242         goto cleanup;
243     }
244
245     ctx->acceptorCtx.lastStatus = code;
246
247     if (code == OK_RC || code == PW_ACCESS_CHALLENGE) {
248         major = getBufferFromRadiusAttributes(minor, received, PW_EAP_MESSAGE,
249                                               outputToken);
250         if (GSS_ERROR(major))
251             goto cleanup;
252
253         if (code == PW_ACCESS_CHALLENGE) {
254             major = getBufferFromRadiusAttributes(minor, received, PW_STATE,
255                                                   &ctx->acceptorCtx.state);
256             if (GSS_ERROR(major))
257                 goto cleanup;
258         }
259
260         major = GSS_S_CONTINUE_NEEDED;
261     } else {
262         major = GSS_S_FAILURE;
263         goto cleanup;
264     }
265
266     if (code == OK_RC) {
267         ctx->acceptorCtx.avps = received;
268         received = NULL;
269
270         major = acceptReady(minor, ctx);
271         if (GSS_ERROR(major))
272             goto cleanup;
273
274         ctx->state = EAP_STATE_GSS_CHANNEL_BINDINGS;
275         major = GSS_S_CONTINUE_NEEDED;
276     }
277
278 cleanup:
279     if (received != NULL)
280         rc_avpair_free(received);
281
282     return major;
283 }
284
285 static OM_uint32
286 eapGssSmAcceptGssChannelBindings(OM_uint32 *minor,
287                                  gss_ctx_id_t ctx,
288                                  gss_cred_id_t cred,
289                                  gss_buffer_t inputToken,
290                                  gss_channel_bindings_t chanBindings,
291                                  gss_buffer_t outputToken)
292 {
293     OM_uint32 major;
294     gss_iov_buffer_desc iov[2];
295
296     outputToken->length = 0;
297     outputToken->value = NULL;
298
299     if (chanBindings == GSS_C_NO_CHANNEL_BINDINGS) {
300         ctx->state = EAP_STATE_ESTABLISHED;
301         return GSS_S_COMPLETE;
302     }
303
304     if (inputToken->length < 14) {
305         return GSS_S_DEFECTIVE_TOKEN;
306     }
307
308     iov[0].type = GSS_IOV_BUFFER_TYPE_DATA;
309     iov[0].buffer.length = 0;
310     iov[0].buffer.value = NULL;
311
312     if (chanBindings != GSS_C_NO_CHANNEL_BINDINGS)
313         iov[0].buffer = chanBindings->application_data;
314
315     iov[1].type = GSS_IOV_BUFFER_TYPE_HEADER;
316     iov[1].buffer.length = 16;
317     iov[1].buffer.value = (unsigned char *)inputToken->value - 2;
318
319     assert(load_uint16_be(iov[1].buffer.value) == TOK_TYPE_GSS_CB);
320
321     iov[2].type = GSS_IOV_BUFFER_TYPE_TRAILER;
322     iov[2].buffer.length = inputToken->length - 14;
323     iov[2].buffer.value = (unsigned char *)inputToken->value + 14;
324
325     major = gssEapUnwrapOrVerifyMIC(minor, ctx, NULL, NULL,
326                                     iov, 3, TOK_TYPE_GSS_CB);
327     if (major == GSS_S_COMPLETE) {
328         ctx->state = EAP_STATE_ESTABLISHED;
329     }
330
331 #if 0
332     gss_release_buffer(&tmpMinor, &iov[0].buffer);
333 #endif
334
335     return major;
336 }
337
338 static OM_uint32
339 eapGssSmAcceptEstablished(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     /* Called with already established context */
347     *minor = EINVAL;
348     return GSS_S_BAD_STATUS;
349 }
350
351 static struct gss_eap_acceptor_sm {
352     enum gss_eap_token_type inputTokenType;
353     enum gss_eap_token_type outputTokenType;
354     OM_uint32 (*processToken)(OM_uint32 *,
355                               gss_ctx_id_t,
356                               gss_cred_id_t,
357                               gss_buffer_t,
358                               gss_channel_bindings_t,
359                               gss_buffer_t);
360 } eapGssAcceptorSm[] = {
361     { TOK_TYPE_EAP_RESP,    TOK_TYPE_EAP_REQ,  eapGssSmAcceptIdentity           },
362     { TOK_TYPE_EAP_RESP,    TOK_TYPE_EAP_REQ,  eapGssSmAcceptAuthenticate       },
363     { TOK_TYPE_GSS_CB,      TOK_TYPE_NONE,     eapGssSmAcceptGssChannelBindings },
364     { TOK_TYPE_NONE,        TOK_TYPE_NONE,     eapGssSmAcceptEstablished        },
365 };
366
367 OM_uint32
368 gss_accept_sec_context(OM_uint32 *minor,
369                        gss_ctx_id_t *context_handle,
370                        gss_cred_id_t cred,
371                        gss_buffer_t input_token,
372                        gss_channel_bindings_t input_chan_bindings,
373                        gss_name_t *src_name,
374                        gss_OID *mech_type,
375                        gss_buffer_t output_token,
376                        OM_uint32 *ret_flags,
377                        OM_uint32 *time_rec,
378                        gss_cred_id_t *delegated_cred_handle)
379 {
380     OM_uint32 major;
381     OM_uint32 tmpMajor, tmpMinor;
382     gss_ctx_id_t ctx = *context_handle;
383     struct gss_eap_acceptor_sm *sm = NULL;
384     gss_buffer_desc innerInputToken = GSS_C_EMPTY_BUFFER;
385     gss_buffer_desc innerOutputToken = GSS_C_EMPTY_BUFFER;
386
387     *minor = 0;
388
389     output_token->length = 0;
390     output_token->value = NULL;
391
392     if (cred != GSS_C_NO_CREDENTIAL && !(cred->flags & CRED_FLAG_ACCEPT)) {
393         return GSS_S_NO_CRED;
394     }
395
396     if (input_token == GSS_C_NO_BUFFER || input_token->length == 0) {
397         return GSS_S_DEFECTIVE_TOKEN;
398     }
399
400     if (ctx == GSS_C_NO_CONTEXT) {
401         major = gssEapAllocContext(minor, &ctx);
402         if (GSS_ERROR(major))
403             return major;
404
405         *context_handle = ctx;
406     }
407
408     GSSEAP_MUTEX_LOCK(&ctx->mutex);
409
410     sm = &eapGssAcceptorSm[ctx->state];
411
412     major = gssEapVerifyToken(minor, ctx, input_token,
413                               sm->inputTokenType, &innerInputToken);
414     if (GSS_ERROR(major))
415         goto cleanup;
416
417     /* If credentials were provided, check they're usable with this mech */
418     if (!gssEapCredAvailable(cred, ctx->mechanismUsed)) {
419         major = GSS_S_BAD_MECH;
420         goto cleanup;
421     }
422
423     do {
424         sm = &eapGssAcceptorSm[ctx->state];
425
426         major = (sm->processToken)(minor,
427                                    ctx,
428                                    cred,
429                                    &innerInputToken,
430                                    input_chan_bindings,
431                                    &innerOutputToken);
432         if (GSS_ERROR(major))
433             goto cleanup;
434     } while (major == GSS_S_CONTINUE_NEEDED && innerOutputToken.length == 0);
435
436     if (mech_type != NULL) {
437         if (!gssEapInternalizeOid(ctx->mechanismUsed, mech_type))
438             duplicateOid(&tmpMinor, ctx->mechanismUsed, mech_type);
439     }
440     if (innerOutputToken.length != 0) {
441         tmpMajor = gssEapMakeToken(&tmpMinor, ctx, &innerOutputToken,
442                                    sm->outputTokenType, output_token);
443         if (GSS_ERROR(tmpMajor)) {
444             major = tmpMajor;
445             *minor = tmpMinor;
446             goto cleanup;
447         }
448     }
449     if (ret_flags != NULL)
450         *ret_flags = ctx->gssFlags;
451     if (delegated_cred_handle != NULL)
452         *delegated_cred_handle = GSS_C_NO_CREDENTIAL;
453
454     if (major == GSS_S_COMPLETE) {
455         if (src_name != NULL && ctx->initiatorName != GSS_C_NO_NAME) {
456             major = gss_duplicate_name(&tmpMinor, ctx->initiatorName, src_name);
457             if (GSS_ERROR(major))
458                 goto cleanup;
459         }
460         if (time_rec != NULL)
461             gss_context_time(&tmpMinor, ctx, time_rec);
462     }
463
464     assert(ctx->state == EAP_STATE_ESTABLISHED || major == GSS_S_CONTINUE_NEEDED);
465
466 cleanup:
467     GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
468
469     if (GSS_ERROR(major))
470         gssEapReleaseContext(&tmpMinor, context_handle);
471
472     gss_release_buffer(&tmpMinor, &innerOutputToken);
473
474     return major;
475 }
476