get GSS-EAP working again with TLV
[mech_eap.orig] / util_sm.c
1 /*
2  * Copyright (c) 2011, 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 /*
34  * Context establishment state machine.
35  */
36
37 #include "gssapiP_eap.h"
38
39 static const char *
40 gssEapStateToString(enum gss_eap_state state)
41 {
42     const char *s;
43
44     switch (state) {
45     case GSSEAP_STATE_INITIAL:
46         s = "INITIAL";
47         break;
48     case GSSEAP_STATE_AUTHENTICATE:
49         s = "AUTHENTICATE";
50         break;
51     case GSSEAP_STATE_INITIATOR_EXTS:
52         s = "INITIATOR_EXTS";
53         break;
54     case GSSEAP_STATE_ACCEPTOR_EXTS:
55         s = "ACCEPTOR_EXTS";
56         break;
57     case GSSEAP_STATE_ESTABLISHED:
58         s = "ESTABLISHED";
59         break;
60     default:
61         s = "INVALID";
62         break;
63     }
64
65     return s;
66 }
67
68 static OM_uint32
69 makeErrorToken(OM_uint32 *minor,
70                OM_uint32 majorStatus,
71                OM_uint32 minorStatus,
72                gss_buffer_set_t *outputToken)
73 {
74     unsigned char errorData[8];
75     gss_buffer_desc errorBuffer;
76
77     assert(GSS_ERROR(majorStatus));
78
79     /*
80      * Only return error codes that the initiator could have caused,
81      * to avoid information leakage.
82      */
83     if (IS_RADIUS_ERROR(minorStatus)) {
84         /* Squash RADIUS error codes */
85         minorStatus = GSSEAP_RADIUS_PROT_FAILURE;
86     } else if (!IS_WIRE_ERROR(minorStatus)) {
87         /* Don't return non-wire error codes */
88         return GSS_S_COMPLETE;
89     }
90
91     minorStatus -= ERROR_TABLE_BASE_eapg;
92
93     store_uint32_be(majorStatus, &errorData[0]);
94     store_uint32_be(minorStatus, &errorData[4]);
95
96     errorBuffer.length = sizeof(errorData);
97     errorBuffer.value = errorData;
98
99     return gss_add_buffer_set_member(minor, &errorBuffer, outputToken);
100 }
101
102 OM_uint32
103 gssEapSmStep(OM_uint32 *minor,
104              gss_cred_id_t cred,
105              gss_ctx_id_t ctx,
106              gss_name_t target,
107              gss_OID mech,
108              OM_uint32 reqFlags,
109              OM_uint32 timeReq,
110              gss_channel_bindings_t chanBindings,
111              gss_buffer_t inputToken,
112              gss_buffer_t outputToken,
113              struct gss_eap_sm *sm,
114              size_t smCount)
115 {
116     OM_uint32 major, tmpMajor, tmpMinor;
117     gss_buffer_desc unwrappedInputToken = GSS_C_EMPTY_BUFFER;
118     gss_buffer_desc unwrappedOutputToken = GSS_C_EMPTY_BUFFER;
119     gss_buffer_set_t innerInputTokens = GSS_C_NO_BUFFER_SET;
120     gss_buffer_set_t innerOutputTokens = GSS_C_NO_BUFFER_SET;
121     OM_uint32 *inputTokenTypes = NULL, *outputTokenTypes = NULL;
122     unsigned int smFlags = 0;
123     size_t i, j;
124     int initialContextToken = 0;
125
126     assert(smCount > 0);
127
128     *minor = 0;
129
130     outputToken->length = 0;
131     outputToken->value = NULL;
132
133     if (inputToken != GSS_C_NO_BUFFER && inputToken->length != 0) {
134         enum gss_eap_token_type tokType;
135
136         major = gssEapVerifyToken(minor, ctx, inputToken, &tokType,
137                                   &unwrappedInputToken);
138         if (GSS_ERROR(major))
139             goto cleanup;
140
141         if (tokType != TOK_TYPE_ESTABLISH_CONTEXT) {
142             major = GSS_S_DEFECTIVE_TOKEN;
143             *minor = GSSEAP_WRONG_TOK_ID;
144             goto cleanup;
145         }
146     } else if (!CTX_IS_INITIATOR(ctx) || ctx->state != GSSEAP_STATE_INITIAL) {
147         major = GSS_S_DEFECTIVE_TOKEN;
148         *minor = GSSEAP_WRONG_SIZE;
149         goto cleanup;
150     } else {
151         initialContextToken = 1;
152     }
153
154     if (ctx->state == GSSEAP_STATE_ESTABLISHED) {
155         major = GSS_S_BAD_STATUS;
156         *minor = GSSEAP_CONTEXT_ESTABLISHED;
157         goto cleanup;
158     }
159
160     assert(ctx->state < GSSEAP_STATE_ESTABLISHED);
161
162     major = gssEapDecodeInnerTokens(minor, &unwrappedInputToken,
163                                     &innerInputTokens, &inputTokenTypes);
164     if (GSS_ERROR(major))
165         goto cleanup;
166
167     assert(innerInputTokens != GSS_C_NO_BUFFER_SET);
168
169     major = gss_create_empty_buffer_set(minor, &innerOutputTokens);
170     if (GSS_ERROR(major))
171         goto cleanup;
172
173     assert(innerOutputTokens->count == 0);
174     assert(innerOutputTokens->elements == NULL);
175
176     innerOutputTokens->elements = (gss_buffer_desc *)GSSEAP_CALLOC(smCount,
177                                                                    sizeof(gss_buffer_desc));
178     if (innerOutputTokens->elements == NULL) {
179         major = GSS_S_FAILURE;
180         *minor = ENOMEM;
181         goto cleanup;
182     }
183
184     outputTokenTypes = (OM_uint32 *)GSSEAP_CALLOC(smCount, sizeof(OM_uint32));
185     if (outputTokenTypes == NULL) {
186         major = GSS_S_FAILURE;
187         *minor = ENOMEM;
188         goto cleanup;
189     }
190
191     /*
192      * Process all the tokens that are valid for the current state. If
193      * the processToken function returns GSS_S_COMPLETE, the state is
194      * advanced until there is a token to send or the ESTABLISHED state
195      * is reached.
196      */
197     do {
198         major = GSS_S_COMPLETE;
199
200         for (i = 0; i < smCount; i++) {
201             struct gss_eap_sm *smp = &sm[i];
202             int processToken = 0;
203             gss_buffer_t innerInputToken = GSS_C_NO_BUFFER;
204             OM_uint32 *inputTokenType = NULL;
205             gss_buffer_desc innerOutputToken = GSS_C_EMPTY_BUFFER;
206
207             if ((smp->validStates & ctx->state) == 0)
208                 continue;
209
210             if (smp->inputTokenType == ITOK_TYPE_NONE || initialContextToken) {
211                 processToken = 1;
212             } else if ((smFlags & SM_FLAG_TRANSITION) == 0) {
213                 for (j = 0; j < innerInputTokens->count; j++) {
214                     if ((inputTokenTypes[j] & ITOK_TYPE_MASK) == smp->inputTokenType) {
215                         processToken = 1;
216                         if (innerInputToken != GSS_C_NO_BUFFER) {
217                             major = GSS_S_DEFECTIVE_TOKEN;
218                             *minor = GSSEAP_DUPLICATE_ITOK;
219                             break;
220                         }
221                     }
222                     innerInputToken = &innerInputTokens->elements[j];
223                     inputTokenType = &inputTokenTypes[j];
224                 }
225             }
226
227 #ifdef GSSEAP_DEBUG
228             fprintf(stderr, "GSS-EAP: state %d processToken %d inputTokenType %08x "
229                     "innerInputToken %p innerOutputTokensCount %zd\n",
230                     ctx->state, processToken, smp->inputTokenType,
231                     innerInputToken, innerOutputTokens->count);
232 #endif
233
234             if (processToken) {
235                 smFlags = 0;
236
237                 major = smp->processToken(minor, cred, ctx, target, mech, reqFlags,
238                                          timeReq, chanBindings, innerInputToken,
239                                          &innerOutputToken, &smFlags);
240                 if (GSS_ERROR(major))
241                     break;
242
243                 if (inputTokenType != NULL)
244                     *inputTokenType |= ITOK_FLAG_VERIFIED;
245
246                 if (innerOutputToken.value != NULL) {
247                     innerOutputTokens->elements[innerOutputTokens->count] = innerOutputToken;
248                     assert(smp->outputTokenType != ITOK_TYPE_NONE);
249                     outputTokenTypes[innerOutputTokens->count] = smp->outputTokenType;
250                     if (smp->critical)
251                         outputTokenTypes[innerOutputTokens->count] |= ITOK_FLAG_CRITICAL;
252                     innerOutputTokens->count++;
253                 }
254                 if (smFlags & SM_FLAG_STOP_EVAL)
255                     break;
256             } else if (smp->required && smp->inputTokenType != ITOK_TYPE_NONE) {
257                 major = GSS_S_DEFECTIVE_TOKEN;
258                 *minor = GSSEAP_MISSING_REQUIRED_ITOK;
259                 break;
260             }
261         }
262
263         if (GSS_ERROR(major) || (smFlags & SM_FLAG_TRANSITION) == 0)
264             break;
265
266         assert(ctx->state < GSSEAP_STATE_ESTABLISHED);
267
268 #ifdef GSSEAP_DEBUG
269         fprintf(stderr, "GSS-EAP: state transition %s->%s\n",
270                 gssEapStateToString(ctx->state),
271                 gssEapStateToString(GSSEAP_STATE_NEXT(ctx->state)));
272 #endif
273
274         ctx->state = GSSEAP_STATE_NEXT(ctx->state);
275
276         if (innerOutputTokens->count != 0 || (smFlags & SM_FLAG_FORCE_SEND_TOKEN)) {
277             assert(major == GSS_S_CONTINUE_NEEDED || ctx->state == GSSEAP_STATE_ESTABLISHED);
278             break; /* send any tokens if we have them */
279         }
280     } while (ctx->state != GSSEAP_STATE_ESTABLISHED);
281
282     assert(innerOutputTokens->count <= smCount);
283
284     /* Check we understood all critical tokens */
285     if (!GSS_ERROR(major)) {
286         for (j = 0; j < innerInputTokens->count; j++) {
287             if ((inputTokenTypes[j] & ITOK_FLAG_CRITICAL) &&
288                 (inputTokenTypes[j] & ITOK_FLAG_VERIFIED) == 0) {
289                 major = GSS_S_UNAVAILABLE;
290                 *minor = GSSEAP_CRIT_ITOK_UNAVAILABLE;
291                 goto cleanup;
292             }
293         }
294     }
295
296     /* Emit an error token if we are the acceptor */
297     if (GSS_ERROR(major)) {
298         if (CTX_IS_INITIATOR(ctx))
299             goto cleanup; /* return error directly to caller */
300
301         /* replace any emitted tokens with error token */
302         gss_release_buffer_set(&tmpMinor, &innerOutputTokens);
303
304         tmpMajor = makeErrorToken(&tmpMinor, major, *minor, &innerOutputTokens);
305         if (GSS_ERROR(tmpMajor)) {
306             major = tmpMajor;
307             *minor = tmpMinor;
308             goto cleanup;
309         }
310
311         outputTokenTypes[0] = ITOK_TYPE_CONTEXT_ERR | ITOK_FLAG_CRITICAL;
312     }
313
314 #ifdef GSSEAP_DEBUG
315     for (i = 0; i < innerOutputTokens->count; i++) {
316         fprintf(stderr, "GSS-EAP: type %08x length %zd value %p\n",
317                 outputTokenTypes[i],
318                 innerOutputTokens->elements[i].length,
319                 innerOutputTokens->elements[i].value);
320     }
321 #endif
322
323     /* Format composite output token */
324     if (innerOutputTokens->count != 0 ||            /* inner tokens to send */
325         !CTX_IS_INITIATOR(ctx) ||                   /* any leg acceptor */
326         ctx->state != GSSEAP_STATE_ESTABLISHED) {   /* non-last leg initiator */
327         tmpMajor = gssEapEncodeInnerTokens(&tmpMinor, innerOutputTokens,
328                                            outputTokenTypes, &unwrappedOutputToken);
329         if (tmpMajor == GSS_S_COMPLETE) {
330             tmpMajor = gssEapMakeToken(&tmpMinor, ctx, &unwrappedOutputToken,
331                                        TOK_TYPE_ESTABLISH_CONTEXT, outputToken);
332             if (GSS_ERROR(tmpMajor)) {
333                 major = tmpMajor;
334                 *minor = tmpMinor;
335                 goto cleanup;
336             }
337         }
338     }
339
340     assert(GSS_ERROR(major) ||
341            (major == GSS_S_CONTINUE_NEEDED && (ctx->state > GSSEAP_STATE_INITIAL && ctx->state < GSSEAP_STATE_ESTABLISHED)) ||
342            (major == GSS_S_COMPLETE && ctx->state == GSSEAP_STATE_ESTABLISHED));
343
344 cleanup:
345     gss_release_buffer_set(&tmpMinor, &innerInputTokens);
346     gss_release_buffer_set(&tmpMinor, &innerOutputTokens);
347     if (inputTokenTypes != NULL)
348         GSSEAP_FREE(inputTokenTypes);
349     if (outputTokenTypes != NULL)
350     gss_release_buffer(&tmpMinor, &unwrappedOutputToken);
351         GSSEAP_FREE(outputTokenTypes);
352
353     return major;
354 }