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