don't expose unauthenticated initiator identity
[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 /*
36  * Mark a context as ready for cryptographic operations
37  */
38 static OM_uint32
39 acceptReady(OM_uint32 *minor, gss_ctx_id_t ctx, gss_cred_id_t cred)
40 {
41     OM_uint32 major, tmpMinor;
42     VALUE_PAIR *vp;
43     gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
44
45     /* Cache encryption type derived from selected mechanism OID */
46     major = gssEapOidToEnctype(minor, ctx->mechanismUsed,
47                                &ctx->encryptionType);
48     if (GSS_ERROR(major))
49         return major;
50
51     gssEapReleaseName(&tmpMinor, &ctx->initiatorName);
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     ctx->initiatorName->attrCtx = gssEapCreateAttrContext(cred, ctx);
67
68     vp = rc_avpair_get(ctx->acceptorCtx.avps,
69                        RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY,
70                        RADIUS_VENDOR_ID_MICROSOFT);
71     if (ctx->encryptionType != ENCTYPE_NULL && vp != NULL) {
72         major = gssEapDeriveRfc3961Key(minor,
73                                        (unsigned char *)vp->strvalue,
74                                        vp->lvalue,
75                                        ctx->encryptionType,
76                                        &ctx->rfc3961Key);
77         if (GSS_ERROR(major))
78             return major;
79
80         major = rfc3961ChecksumTypeForKey(minor, &ctx->rfc3961Key,
81                                            &ctx->checksumType);
82         if (GSS_ERROR(major))
83             return major;
84     } else {
85         /*
86          * draft-howlett-eap-gss says that integrity/confidentialty should
87          * always be advertised as available, but if we have no keying
88          * material it seems confusing to the caller to advertise this.
89          */
90         ctx->gssFlags &= ~(GSS_C_INTEG_FLAG | GSS_C_CONF_FLAG);
91         ctx->encryptionType = ENCTYPE_NULL;
92     }
93
94     major = sequenceInit(minor,
95                          &ctx->seqState, ctx->recvSeq,
96                          ((ctx->gssFlags & GSS_C_REPLAY_FLAG) != 0),
97                          ((ctx->gssFlags & GSS_C_SEQUENCE_FLAG) != 0),
98                          TRUE);
99     if (GSS_ERROR(major))
100         return major;
101
102     return GSS_S_COMPLETE;
103 }
104
105 static OM_uint32
106 eapGssSmAcceptIdentity(OM_uint32 *minor,
107                        gss_ctx_id_t ctx,
108                        gss_cred_id_t cred,
109                        gss_buffer_t inputToken,
110                        gss_channel_bindings_t chanBindings,
111                        gss_buffer_t outputToken)
112 {
113     OM_uint32 major;
114     union {
115         struct eap_hdr pdu;
116         unsigned char data[5];
117     } pkt;
118     gss_buffer_desc pktBuffer;
119
120     if (inputToken != GSS_C_NO_BUFFER && inputToken->length != 0)
121         return GSS_S_DEFECTIVE_TOKEN;
122
123     assert(ctx->acceptorCtx.radHandle == NULL);
124
125     major = gssEapRadiusAllocHandle(minor, cred, &ctx->acceptorCtx.radHandle);
126     if (GSS_ERROR(major))
127         return major;
128
129     assert(ctx->acceptorName == GSS_C_NO_NAME);
130
131     if (cred != GSS_C_NO_CREDENTIAL && cred->name != GSS_C_NO_NAME) {
132         major = gss_duplicate_name(minor, cred->name, &ctx->acceptorName);
133         if (GSS_ERROR(major))
134             return major;
135     }
136
137     pkt.pdu.code = EAP_CODE_REQUEST;
138     pkt.pdu.identifier = 0;
139     pkt.pdu.length = htons(sizeof(pkt.data));
140     pkt.data[4] = EAP_TYPE_IDENTITY;
141
142     pktBuffer.length = sizeof(pkt.data);
143     pktBuffer.value = pkt.data;
144
145     major = duplicateBuffer(minor, &pktBuffer, outputToken);
146     if (GSS_ERROR(major))
147         return major;
148
149     ctx->state = EAP_STATE_AUTHENTICATE;
150
151     return GSS_S_CONTINUE_NEEDED;
152 }
153
154 static OM_uint32
155 setAcceptorIdentity(OM_uint32 *minor,
156                     gss_ctx_id_t ctx,
157                     VALUE_PAIR **avps)
158 {
159     OM_uint32 major, tmpMinor;
160     gss_buffer_desc nameBuf;
161
162     /* Awaits further specification */
163     if (ctx->acceptorName == GSS_C_NO_NAME)
164         return GSS_S_COMPLETE;
165
166     major = gss_display_name(minor, ctx->acceptorName, &nameBuf, NULL);
167     if (GSS_ERROR(major))
168         return major;
169
170     major = addAvpFromBuffer(minor, ctx->acceptorCtx.radHandle, avps,
171                              RADIUS_VENDOR_ATTR_GSS_EAP_ACCEPTOR_SERVICE_NAME,
172                              RADIUS_VENDOR_ID_GSS_EAP,
173                              &nameBuf);
174     if (GSS_ERROR(major))
175         return major;
176
177     gss_release_buffer(&tmpMinor, &nameBuf);
178
179     return major;
180 }
181
182 static OM_uint32
183 eapGssSmAcceptAuthenticate(OM_uint32 *minor,
184                            gss_ctx_id_t ctx,
185                            gss_cred_id_t cred,
186                            gss_buffer_t inputToken,
187                            gss_channel_bindings_t chanBindings,
188                            gss_buffer_t outputToken)
189 {
190     OM_uint32 major, tmpMinor;
191     int code;
192     VALUE_PAIR *send = NULL;
193     VALUE_PAIR *received = NULL;
194     rc_handle *rh = ctx->acceptorCtx.radHandle;
195     char msgBuffer[4096];
196     struct eap_hdr *pdu;
197     unsigned char *pos;
198     gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
199
200     pdu = (struct eap_hdr *)inputToken->value;
201     pos = (unsigned char *)(pdu + 1);
202
203     if (inputToken->length > sizeof(*pdu) &&
204         pdu->code == EAP_CODE_RESPONSE &&
205         pos[0] == EAP_TYPE_IDENTITY) {
206         /*
207          * XXX TODO do we really need to set User-Name? FreeRADIUS does
208          * not appear to require it.
209          */
210         major = addAvpFromBuffer(minor, rh, &send, PW_USER_NAME, 0, &nameBuf);
211         if (GSS_ERROR(major))
212             goto cleanup;
213
214         major = setAcceptorIdentity(minor, ctx, &send);
215         if (GSS_ERROR(major))
216             goto cleanup;
217     }
218
219     major = addAvpFromBuffer(minor, rh, &send, PW_EAP_MESSAGE, 0, inputToken);
220     if (GSS_ERROR(major))
221         goto cleanup;
222
223     if (ctx->acceptorCtx.lastStatus == CHALLENGE_RC) {
224         major = addAvpFromBuffer(minor, rh, &send, PW_STATE, 0,
225                                  &ctx->acceptorCtx.state);
226         if (GSS_ERROR(major))
227             goto cleanup;
228
229         gss_release_buffer(&tmpMinor, &ctx->acceptorCtx.state);
230     }
231
232     code = rc_auth(rh, 0, send, &received, msgBuffer);
233     switch (code) {
234     case OK_RC:
235     case CHALLENGE_RC:
236         major = GSS_S_CONTINUE_NEEDED;
237         break;
238     case TIMEOUT_RC:
239         major = GSS_S_UNAVAILABLE;
240         break;
241     case REJECT_RC:
242         major = GSS_S_DEFECTIVE_CREDENTIAL;
243         break;
244     default:
245         major = GSS_S_FAILURE;
246         goto cleanup;
247     }
248
249     if (GSS_ERROR(major))
250         goto cleanup;
251
252     ctx->acceptorCtx.lastStatus = code;
253
254     major = getBufferFromAvps(minor, received, PW_EAP_MESSAGE, 0,
255                               outputToken, TRUE);
256     if ((major == GSS_S_UNAVAILABLE && code != OK_RC) ||
257         GSS_ERROR(major))
258         goto cleanup;
259
260     if (code == CHALLENGE_RC) {
261         major = getBufferFromAvps(minor, received, PW_STATE, 0,
262                                   &ctx->acceptorCtx.state, TRUE);
263         if (major != GSS_S_UNAVAILABLE && GSS_ERROR(major))
264             goto cleanup;
265     } else {
266         ctx->acceptorCtx.avps = received;
267         received = NULL;
268
269         major = acceptReady(minor, ctx, cred);
270         if (GSS_ERROR(major))
271             goto cleanup;
272
273         ctx->state = EAP_STATE_GSS_CHANNEL_BINDINGS;
274     }
275
276     major = GSS_S_CONTINUE_NEEDED;
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