cleanup key derivation on acceptor
[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;
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     vp = rc_avpair_get(ctx->acceptorCtx.avps, PW_USER_NAME, 0);
52     if (vp != NULL) {
53         nameBuf.length = vp->lvalue;
54         nameBuf.value = vp->strvalue;
55     } else if (ctx->initiatorName == GSS_C_NO_NAME) {
56         ctx->gssFlags |= GSS_C_ANON_FLAG;
57     }
58
59     if (nameBuf.length != 0 || ctx->initiatorName == GSS_C_NO_NAME) {
60         major = gssEapImportName(minor, &nameBuf, GSS_C_NT_USER_NAME,
61                                  &ctx->initiatorName);
62         if (GSS_ERROR(major))
63             return major;
64     }
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     if (ctx->acceptorName == GSS_C_NO_NAME &&
130         cred != GSS_C_NO_CREDENTIAL &&
131         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 importInitiatorIdentity(OM_uint32 *minor,
156                         gss_ctx_id_t ctx,
157                         gss_buffer_t inputToken,
158                         gss_buffer_t nameBuf)
159 {
160     OM_uint32 major, tmpMinor;
161     struct eap_hdr *pdu = (struct eap_hdr *)inputToken->value;
162     unsigned char *pos = (unsigned char *)(pdu + 1);
163     gss_name_t name;
164
165     assert(pdu->code == EAP_CODE_RESPONSE);
166     assert(pos[0] == EAP_TYPE_IDENTITY);
167
168     nameBuf->value = pos + 1;
169     nameBuf->length = inputToken->length - sizeof(*pdu) - 1;
170
171     major = gssEapImportName(minor, nameBuf, GSS_C_NT_USER_NAME, &name);
172     if (GSS_ERROR(major))
173         return major;
174
175     gssEapReleaseName(&tmpMinor, &ctx->initiatorName);
176     ctx->initiatorName = name;
177
178     return GSS_S_COMPLETE;
179 }
180
181 static OM_uint32
182 eapGssSmAcceptAuthenticate(OM_uint32 *minor,
183                            gss_ctx_id_t ctx,
184                            gss_cred_id_t cred,
185                            gss_buffer_t inputToken,
186                            gss_channel_bindings_t chanBindings,
187                            gss_buffer_t outputToken)
188 {
189     OM_uint32 major, tmpMinor;
190     int code;
191     VALUE_PAIR *send = NULL;
192     VALUE_PAIR *received = NULL;
193     rc_handle *rh = ctx->acceptorCtx.radHandle;
194     char msgBuffer[4096];
195     struct eap_hdr *pdu;
196     unsigned char *pos;
197     gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
198
199     pdu = (struct eap_hdr *)inputToken->value;
200     pos = (unsigned char *)(pdu + 1);
201
202     if (inputToken->length > sizeof(*pdu) &&
203         pdu->code == EAP_CODE_RESPONSE &&
204         pos[0] == EAP_TYPE_IDENTITY) {
205         major = importInitiatorIdentity(minor, ctx, inputToken, &nameBuf);
206         if (GSS_ERROR(major))
207             goto cleanup;
208
209         major = addAvpFromBuffer(minor, rh, &send, PW_USER_NAME, &nameBuf);
210         if (GSS_ERROR(major))
211             goto cleanup;
212     }
213
214     major = addAvpFromBuffer(minor, rh, &send, PW_EAP_MESSAGE, inputToken);
215     if (GSS_ERROR(major))
216         goto cleanup;
217
218     if (ctx->acceptorCtx.lastStatus == CHALLENGE_RC) {
219         major = addAvpFromBuffer(minor, rh, &send, PW_STATE,
220                                  &ctx->acceptorCtx.state);
221         if (GSS_ERROR(major))
222             goto cleanup;
223
224         gss_release_buffer(&tmpMinor, &ctx->acceptorCtx.state);
225     }
226
227     code = rc_auth(rh, 0, send, &received, msgBuffer);
228     switch (code) {
229     case OK_RC:
230     case CHALLENGE_RC:
231         major = GSS_S_CONTINUE_NEEDED;
232         break;
233     case TIMEOUT_RC:
234         major = GSS_S_UNAVAILABLE;
235         break;
236     case REJECT_RC:
237         major = GSS_S_DEFECTIVE_CREDENTIAL;
238         break;
239     default:
240         major = GSS_S_FAILURE;
241         goto cleanup;
242     }
243
244     if (GSS_ERROR(major))
245         goto cleanup;
246
247     ctx->acceptorCtx.lastStatus = code;
248
249     major = getBufferFromAvps(minor, received, PW_EAP_MESSAGE,
250                               outputToken, TRUE);
251     if ((major == GSS_S_UNAVAILABLE && code != OK_RC) ||
252         GSS_ERROR(major))
253         goto cleanup;
254
255     if (code == CHALLENGE_RC) {
256         major = getBufferFromAvps(minor, received, PW_STATE,
257                                   &ctx->acceptorCtx.state, TRUE);
258         if (major != GSS_S_UNAVAILABLE && GSS_ERROR(major))
259             goto cleanup;
260     } else {
261         ctx->acceptorCtx.avps = received;
262         received = NULL;
263
264         major = acceptReady(minor, ctx, cred);
265         if (GSS_ERROR(major))
266             goto cleanup;
267
268         ctx->state = EAP_STATE_GSS_CHANNEL_BINDINGS;
269     }
270
271     major = GSS_S_CONTINUE_NEEDED;
272
273 cleanup:
274     if (received != NULL)
275         rc_avpair_free(received);
276
277     return major;
278 }
279
280 static OM_uint32
281 eapGssSmAcceptGssChannelBindings(OM_uint32 *minor,
282                                  gss_ctx_id_t ctx,
283                                  gss_cred_id_t cred,
284                                  gss_buffer_t inputToken,
285                                  gss_channel_bindings_t chanBindings,
286                                  gss_buffer_t outputToken)
287 {
288     OM_uint32 major;
289     gss_iov_buffer_desc iov[2];
290
291     outputToken->length = 0;
292     outputToken->value = NULL;
293
294     if (chanBindings == GSS_C_NO_CHANNEL_BINDINGS) {
295         ctx->state = EAP_STATE_ESTABLISHED;
296         return GSS_S_COMPLETE;
297     }
298
299     if (inputToken->length < 14) {
300         return GSS_S_DEFECTIVE_TOKEN;
301     }
302
303     iov[0].type = GSS_IOV_BUFFER_TYPE_DATA;
304     iov[0].buffer.length = 0;
305     iov[0].buffer.value = NULL;
306
307     if (chanBindings != GSS_C_NO_CHANNEL_BINDINGS)
308         iov[0].buffer = chanBindings->application_data;
309
310     iov[1].type = GSS_IOV_BUFFER_TYPE_HEADER;
311     iov[1].buffer.length = 16;
312     iov[1].buffer.value = (unsigned char *)inputToken->value - 2;
313
314     assert(load_uint16_be(iov[1].buffer.value) == TOK_TYPE_GSS_CB);
315
316     iov[2].type = GSS_IOV_BUFFER_TYPE_TRAILER;
317     iov[2].buffer.length = inputToken->length - 14;
318     iov[2].buffer.value = (unsigned char *)inputToken->value + 14;
319
320     major = gssEapUnwrapOrVerifyMIC(minor, ctx, NULL, NULL,
321                                     iov, 3, TOK_TYPE_GSS_CB);
322     if (major == GSS_S_COMPLETE) {
323         ctx->state = EAP_STATE_ESTABLISHED;
324     }
325
326 #if 0
327     gss_release_buffer(&tmpMinor, &iov[0].buffer);
328 #endif
329
330     return major;
331 }
332
333 static OM_uint32
334 eapGssSmAcceptEstablished(OM_uint32 *minor,
335                           gss_ctx_id_t ctx,
336                           gss_cred_id_t cred,
337                           gss_buffer_t inputToken,
338                           gss_channel_bindings_t chanBindings,
339                           gss_buffer_t outputToken)
340 {
341     /* Called with already established context */
342     *minor = EINVAL;
343     return GSS_S_BAD_STATUS;
344 }
345
346 static struct gss_eap_acceptor_sm {
347     enum gss_eap_token_type inputTokenType;
348     enum gss_eap_token_type outputTokenType;
349     OM_uint32 (*processToken)(OM_uint32 *,
350                               gss_ctx_id_t,
351                               gss_cred_id_t,
352                               gss_buffer_t,
353                               gss_channel_bindings_t,
354                               gss_buffer_t);
355 } eapGssAcceptorSm[] = {
356     { TOK_TYPE_EAP_RESP,    TOK_TYPE_EAP_REQ,  eapGssSmAcceptIdentity           },
357     { TOK_TYPE_EAP_RESP,    TOK_TYPE_EAP_REQ,  eapGssSmAcceptAuthenticate       },
358     { TOK_TYPE_GSS_CB,      TOK_TYPE_NONE,     eapGssSmAcceptGssChannelBindings },
359     { TOK_TYPE_NONE,        TOK_TYPE_NONE,     eapGssSmAcceptEstablished        },
360 };
361
362 OM_uint32
363 gss_accept_sec_context(OM_uint32 *minor,
364                        gss_ctx_id_t *context_handle,
365                        gss_cred_id_t cred,
366                        gss_buffer_t input_token,
367                        gss_channel_bindings_t input_chan_bindings,
368                        gss_name_t *src_name,
369                        gss_OID *mech_type,
370                        gss_buffer_t output_token,
371                        OM_uint32 *ret_flags,
372                        OM_uint32 *time_rec,
373                        gss_cred_id_t *delegated_cred_handle)
374 {
375     OM_uint32 major;
376     OM_uint32 tmpMajor, tmpMinor;
377     gss_ctx_id_t ctx = *context_handle;
378     struct gss_eap_acceptor_sm *sm = NULL;
379     gss_buffer_desc innerInputToken = GSS_C_EMPTY_BUFFER;
380     gss_buffer_desc innerOutputToken = GSS_C_EMPTY_BUFFER;
381
382     *minor = 0;
383
384     output_token->length = 0;
385     output_token->value = NULL;
386
387     if (cred != GSS_C_NO_CREDENTIAL && !(cred->flags & CRED_FLAG_ACCEPT)) {
388         return GSS_S_NO_CRED;
389     }
390
391     if (input_token == GSS_C_NO_BUFFER || input_token->length == 0) {
392         return GSS_S_DEFECTIVE_TOKEN;
393     }
394
395     if (ctx == GSS_C_NO_CONTEXT) {
396         major = gssEapAllocContext(minor, &ctx);
397         if (GSS_ERROR(major))
398             return major;
399
400         *context_handle = ctx;
401     }
402
403     GSSEAP_MUTEX_LOCK(&ctx->mutex);
404
405     sm = &eapGssAcceptorSm[ctx->state];
406
407     major = gssEapVerifyToken(minor, ctx, input_token,
408                               sm->inputTokenType, &innerInputToken);
409     if (GSS_ERROR(major))
410         goto cleanup;
411
412     /* If credentials were provided, check they're usable with this mech */
413     if (!gssEapCredAvailable(cred, ctx->mechanismUsed)) {
414         major = GSS_S_BAD_MECH;
415         goto cleanup;
416     }
417
418     do {
419         sm = &eapGssAcceptorSm[ctx->state];
420
421         major = (sm->processToken)(minor,
422                                    ctx,
423                                    cred,
424                                    &innerInputToken,
425                                    input_chan_bindings,
426                                    &innerOutputToken);
427         if (GSS_ERROR(major))
428             goto cleanup;
429     } while (major == GSS_S_CONTINUE_NEEDED && innerOutputToken.length == 0);
430
431     if (mech_type != NULL) {
432         if (!gssEapInternalizeOid(ctx->mechanismUsed, mech_type))
433             duplicateOid(&tmpMinor, ctx->mechanismUsed, mech_type);
434     }
435     if (innerOutputToken.length != 0) {
436         tmpMajor = gssEapMakeToken(&tmpMinor, ctx, &innerOutputToken,
437                                    sm->outputTokenType, output_token);
438         if (GSS_ERROR(tmpMajor)) {
439             major = tmpMajor;
440             *minor = tmpMinor;
441             goto cleanup;
442         }
443     }
444     if (ret_flags != NULL)
445         *ret_flags = ctx->gssFlags;
446     if (delegated_cred_handle != NULL)
447         *delegated_cred_handle = GSS_C_NO_CREDENTIAL;
448
449     if (major == GSS_S_COMPLETE) {
450         if (src_name != NULL && ctx->initiatorName != GSS_C_NO_NAME) {
451             major = gss_duplicate_name(&tmpMinor, ctx->initiatorName, src_name);
452             if (GSS_ERROR(major))
453                 goto cleanup;
454         }
455         if (time_rec != NULL)
456             gss_context_time(&tmpMinor, ctx, time_rec);
457     }
458
459     assert(ctx->state == EAP_STATE_ESTABLISHED || major == GSS_S_CONTINUE_NEEDED);
460
461 cleanup:
462     GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
463
464     if (GSS_ERROR(major))
465         gssEapReleaseContext(&tmpMinor, context_handle);
466
467     gss_release_buffer(&tmpMinor, &innerOutputToken);
468
469     return major;
470 }
471