some more fixes to almost get things working
[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, 0x01370010, 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 = addAvpFromBuffer(minor, rh, &send, PW_USER_NAME, &nameBuf);
219         if (GSS_ERROR(major))
220             goto cleanup;
221     }
222
223     major = addAvpFromBuffer(minor, rh, &send, PW_EAP_MESSAGE, inputToken);
224     if (GSS_ERROR(major))
225         goto cleanup;
226
227     if (ctx->acceptorCtx.lastStatus == CHALLENGE_RC) {
228         major = addAvpFromBuffer(minor, rh, &send, PW_STATE,
229                                  &ctx->acceptorCtx.state);
230         if (GSS_ERROR(major))
231             goto cleanup;
232
233         gss_release_buffer(&tmpMinor, &ctx->acceptorCtx.state);
234     }
235
236     code = rc_auth(rh, 0, send, &received, msgBuffer);
237     switch (code) {
238     case OK_RC:
239     case CHALLENGE_RC:
240         major = GSS_S_CONTINUE_NEEDED;
241         break;
242     case TIMEOUT_RC:
243         major = GSS_S_UNAVAILABLE;
244         break;
245     case REJECT_RC:
246         major = GSS_S_DEFECTIVE_CREDENTIAL;
247         break;
248     default:
249         major = GSS_S_FAILURE;
250         goto cleanup;
251     }
252
253     if (GSS_ERROR(major))
254         goto cleanup;
255
256     ctx->acceptorCtx.lastStatus = code;
257
258     major = getBufferFromAvps(minor, received, PW_EAP_MESSAGE, outputToken);
259     if (GSS_ERROR(major))
260         goto cleanup;
261
262     if (code == CHALLENGE_RC) {
263         major = getBufferFromAvps(minor, received, PW_STATE,
264                                   &ctx->acceptorCtx.state);
265         if (GSS_ERROR(major))
266             goto cleanup;
267     } else {
268         ctx->acceptorCtx.avps = received;
269         received = NULL;
270
271         major = acceptReady(minor, ctx);
272         if (GSS_ERROR(major))
273             goto cleanup;
274
275         ctx->state = EAP_STATE_GSS_CHANNEL_BINDINGS;
276     }
277
278     major = GSS_S_CONTINUE_NEEDED;
279
280 cleanup:
281     if (received != NULL)
282         rc_avpair_free(received);
283
284     return major;
285 }
286
287 static OM_uint32
288 eapGssSmAcceptGssChannelBindings(OM_uint32 *minor,
289                                  gss_ctx_id_t ctx,
290                                  gss_cred_id_t cred,
291                                  gss_buffer_t inputToken,
292                                  gss_channel_bindings_t chanBindings,
293                                  gss_buffer_t outputToken)
294 {
295     OM_uint32 major;
296     gss_iov_buffer_desc iov[2];
297
298     outputToken->length = 0;
299     outputToken->value = NULL;
300
301     if (chanBindings == GSS_C_NO_CHANNEL_BINDINGS) {
302         ctx->state = EAP_STATE_ESTABLISHED;
303         return GSS_S_COMPLETE;
304     }
305
306     if (inputToken->length < 14) {
307         return GSS_S_DEFECTIVE_TOKEN;
308     }
309
310     iov[0].type = GSS_IOV_BUFFER_TYPE_DATA;
311     iov[0].buffer.length = 0;
312     iov[0].buffer.value = NULL;
313
314     if (chanBindings != GSS_C_NO_CHANNEL_BINDINGS)
315         iov[0].buffer = chanBindings->application_data;
316
317     iov[1].type = GSS_IOV_BUFFER_TYPE_HEADER;
318     iov[1].buffer.length = 16;
319     iov[1].buffer.value = (unsigned char *)inputToken->value - 2;
320
321     assert(load_uint16_be(iov[1].buffer.value) == TOK_TYPE_GSS_CB);
322
323     iov[2].type = GSS_IOV_BUFFER_TYPE_TRAILER;
324     iov[2].buffer.length = inputToken->length - 14;
325     iov[2].buffer.value = (unsigned char *)inputToken->value + 14;
326
327     major = gssEapUnwrapOrVerifyMIC(minor, ctx, NULL, NULL,
328                                     iov, 3, TOK_TYPE_GSS_CB);
329     if (major == GSS_S_COMPLETE) {
330         ctx->state = EAP_STATE_ESTABLISHED;
331     }
332
333 #if 0
334     gss_release_buffer(&tmpMinor, &iov[0].buffer);
335 #endif
336
337     return major;
338 }
339
340 static OM_uint32
341 eapGssSmAcceptEstablished(OM_uint32 *minor,
342                           gss_ctx_id_t ctx,
343                           gss_cred_id_t cred,
344                           gss_buffer_t inputToken,
345                           gss_channel_bindings_t chanBindings,
346                           gss_buffer_t outputToken)
347 {
348     /* Called with already established context */
349     *minor = EINVAL;
350     return GSS_S_BAD_STATUS;
351 }
352
353 static struct gss_eap_acceptor_sm {
354     enum gss_eap_token_type inputTokenType;
355     enum gss_eap_token_type outputTokenType;
356     OM_uint32 (*processToken)(OM_uint32 *,
357                               gss_ctx_id_t,
358                               gss_cred_id_t,
359                               gss_buffer_t,
360                               gss_channel_bindings_t,
361                               gss_buffer_t);
362 } eapGssAcceptorSm[] = {
363     { TOK_TYPE_EAP_RESP,    TOK_TYPE_EAP_REQ,  eapGssSmAcceptIdentity           },
364     { TOK_TYPE_EAP_RESP,    TOK_TYPE_EAP_REQ,  eapGssSmAcceptAuthenticate       },
365     { TOK_TYPE_GSS_CB,      TOK_TYPE_NONE,     eapGssSmAcceptGssChannelBindings },
366     { TOK_TYPE_NONE,        TOK_TYPE_NONE,     eapGssSmAcceptEstablished        },
367 };
368
369 OM_uint32
370 gss_accept_sec_context(OM_uint32 *minor,
371                        gss_ctx_id_t *context_handle,
372                        gss_cred_id_t cred,
373                        gss_buffer_t input_token,
374                        gss_channel_bindings_t input_chan_bindings,
375                        gss_name_t *src_name,
376                        gss_OID *mech_type,
377                        gss_buffer_t output_token,
378                        OM_uint32 *ret_flags,
379                        OM_uint32 *time_rec,
380                        gss_cred_id_t *delegated_cred_handle)
381 {
382     OM_uint32 major;
383     OM_uint32 tmpMajor, tmpMinor;
384     gss_ctx_id_t ctx = *context_handle;
385     struct gss_eap_acceptor_sm *sm = NULL;
386     gss_buffer_desc innerInputToken = GSS_C_EMPTY_BUFFER;
387     gss_buffer_desc innerOutputToken = GSS_C_EMPTY_BUFFER;
388
389     *minor = 0;
390
391     output_token->length = 0;
392     output_token->value = NULL;
393
394     if (cred != GSS_C_NO_CREDENTIAL && !(cred->flags & CRED_FLAG_ACCEPT)) {
395         return GSS_S_NO_CRED;
396     }
397
398     if (input_token == GSS_C_NO_BUFFER || input_token->length == 0) {
399         return GSS_S_DEFECTIVE_TOKEN;
400     }
401
402     if (ctx == GSS_C_NO_CONTEXT) {
403         major = gssEapAllocContext(minor, &ctx);
404         if (GSS_ERROR(major))
405             return major;
406
407         *context_handle = ctx;
408     }
409
410     GSSEAP_MUTEX_LOCK(&ctx->mutex);
411
412     sm = &eapGssAcceptorSm[ctx->state];
413
414     major = gssEapVerifyToken(minor, ctx, input_token,
415                               sm->inputTokenType, &innerInputToken);
416     if (GSS_ERROR(major))
417         goto cleanup;
418
419     /* If credentials were provided, check they're usable with this mech */
420     if (!gssEapCredAvailable(cred, ctx->mechanismUsed)) {
421         major = GSS_S_BAD_MECH;
422         goto cleanup;
423     }
424
425     do {
426         sm = &eapGssAcceptorSm[ctx->state];
427
428         major = (sm->processToken)(minor,
429                                    ctx,
430                                    cred,
431                                    &innerInputToken,
432                                    input_chan_bindings,
433                                    &innerOutputToken);
434         if (GSS_ERROR(major))
435             goto cleanup;
436     } while (major == GSS_S_CONTINUE_NEEDED && innerOutputToken.length == 0);
437
438     if (mech_type != NULL) {
439         if (!gssEapInternalizeOid(ctx->mechanismUsed, mech_type))
440             duplicateOid(&tmpMinor, ctx->mechanismUsed, mech_type);
441     }
442     if (innerOutputToken.length != 0) {
443         tmpMajor = gssEapMakeToken(&tmpMinor, ctx, &innerOutputToken,
444                                    sm->outputTokenType, output_token);
445         if (GSS_ERROR(tmpMajor)) {
446             major = tmpMajor;
447             *minor = tmpMinor;
448             goto cleanup;
449         }
450     }
451     if (ret_flags != NULL)
452         *ret_flags = ctx->gssFlags;
453     if (delegated_cred_handle != NULL)
454         *delegated_cred_handle = GSS_C_NO_CREDENTIAL;
455
456     if (major == GSS_S_COMPLETE) {
457         if (src_name != NULL && ctx->initiatorName != GSS_C_NO_NAME) {
458             major = gss_duplicate_name(&tmpMinor, ctx->initiatorName, src_name);
459             if (GSS_ERROR(major))
460                 goto cleanup;
461         }
462         if (time_rec != NULL)
463             gss_context_time(&tmpMinor, ctx, time_rec);
464     }
465
466     assert(ctx->state == EAP_STATE_ESTABLISHED || major == GSS_S_CONTINUE_NEEDED);
467
468 cleanup:
469     GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
470
471     if (GSS_ERROR(major))
472         gssEapReleaseContext(&tmpMinor, context_handle);
473
474     gss_release_buffer(&tmpMinor, &innerOutputToken);
475
476     return major;
477 }
478