Merge branch 'master' into tlv-mic
[moonshot.git] / mech_eap / import_sec_context.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  * Deserialise a context handle.
35  */
36
37 #include "gssapiP_eap.h"
38
39 #define UPDATE_REMAIN(n)    do {                \
40         p += (n);                               \
41         remain -= (n);                          \
42     } while (0)
43
44 #define CHECK_REMAIN(n)     do {                \
45         if (remain < (n)) {                     \
46             *minor = GSSEAP_TOK_TRUNC;          \
47             return GSS_S_DEFECTIVE_TOKEN;       \
48         }                                       \
49     } while (0)
50
51 static OM_uint32
52 importPartialRadiusContext(OM_uint32 *minor,
53                            unsigned char **pBuf,
54                            size_t *pRemain,
55                            gss_ctx_id_t ctx)
56 {
57     OM_uint32 major;
58     unsigned char *p = *pBuf;
59     size_t remain = *pRemain;
60     gss_buffer_desc buf;
61     size_t serverLen;
62
63     /* Selected RADIUS server */
64     CHECK_REMAIN(4);
65     serverLen = load_uint32_be(p);
66     UPDATE_REMAIN(4);
67
68     if (serverLen != 0) {
69         CHECK_REMAIN(serverLen);
70
71         ctx->acceptorCtx.radServer = GSSEAP_MALLOC(serverLen + 1);
72         if (ctx->acceptorCtx.radServer == NULL) {
73             *minor = ENOMEM;
74             return GSS_S_FAILURE;
75         }
76         memcpy(ctx->acceptorCtx.radServer, p, serverLen);
77         ctx->acceptorCtx.radServer[serverLen] = '\0';
78
79         UPDATE_REMAIN(serverLen);
80     }
81
82     /* RADIUS state blob */
83     CHECK_REMAIN(4);
84     buf.length = load_uint32_be(p);
85     UPDATE_REMAIN(4);
86
87     if (buf.length != 0) {
88         CHECK_REMAIN(buf.length);
89
90         buf.value = p;
91
92         major = duplicateBuffer(minor, &buf, &ctx->acceptorCtx.state);
93         if (GSS_ERROR(major))
94             return major;
95
96         UPDATE_REMAIN(buf.length);
97     }
98
99     *pBuf = p;
100     *pRemain = remain;
101
102     return GSS_S_COMPLETE;
103 }
104
105 static OM_uint32
106 importMechanismOid(OM_uint32 *minor,
107                    unsigned char **pBuf,
108                    size_t *pRemain,
109                    gss_OID *pOid)
110 {
111     OM_uint32 major;
112     unsigned char *p = *pBuf;
113     size_t remain = *pRemain;
114     gss_OID_desc oidBuf;
115
116     oidBuf.length = load_uint32_be(p);
117     if (remain < 4 + oidBuf.length || oidBuf.length == 0) {
118         *minor = GSSEAP_TOK_TRUNC;
119         return GSS_S_DEFECTIVE_TOKEN;
120     }
121
122     oidBuf.elements = &p[4];
123
124     major = gssEapCanonicalizeOid(minor, &oidBuf, 0, pOid);
125     if (GSS_ERROR(major))
126         return major;
127
128     *pBuf    += 4 + oidBuf.length;
129     *pRemain -= 4 + oidBuf.length;
130
131     *minor = 0;
132     return GSS_S_COMPLETE;
133 }
134
135 static OM_uint32
136 importKerberosKey(OM_uint32 *minor,
137                   unsigned char **pBuf,
138                   size_t *pRemain,
139                   krb5_cksumtype *checksumType,
140                   krb5_enctype *pEncryptionType,
141                   krb5_keyblock *key)
142 {
143     unsigned char *p = *pBuf;
144     size_t remain = *pRemain;
145     OM_uint32 encryptionType;
146     OM_uint32 length;
147     gss_buffer_desc tmp;
148
149     if (remain < 12) {
150         *minor = GSSEAP_TOK_TRUNC;
151         return GSS_S_DEFECTIVE_TOKEN;
152     }
153
154     *checksumType  = load_uint32_be(&p[0]);
155     encryptionType = load_uint32_be(&p[4]);
156     length         = load_uint32_be(&p[8]);
157
158     if ((length != 0) != (encryptionType != ENCTYPE_NULL)) {
159         *minor = GSSEAP_BAD_CONTEXT_TOKEN;
160         return GSS_S_DEFECTIVE_TOKEN;
161     }
162
163     if (remain - 12 < length) {
164         *minor = GSSEAP_TOK_TRUNC;
165         return GSS_S_DEFECTIVE_TOKEN;
166     }
167
168     if (load_buffer(&p[12], length, &tmp) == NULL) {
169         *minor = ENOMEM;
170         return GSS_S_FAILURE;
171     }
172
173     KRB_KEY_TYPE(key)   = encryptionType;
174     KRB_KEY_LENGTH(key) = tmp.length;
175     KRB_KEY_DATA(key)   = (unsigned char *)tmp.value;
176
177     *pBuf    += 12 + length;
178     *pRemain -= 12 + length;
179     *pEncryptionType = encryptionType;
180
181     *minor = 0;
182     return GSS_S_COMPLETE;
183 }
184
185 static OM_uint32
186 importName(OM_uint32 *minor,
187            unsigned char **pBuf,
188            size_t *pRemain,
189            gss_name_t *pName)
190 {
191     OM_uint32 major;
192     unsigned char *p = *pBuf;
193     size_t remain = *pRemain;
194     gss_buffer_desc tmp;
195
196     if (remain < 4) {
197         *minor = GSSEAP_TOK_TRUNC;
198         return GSS_S_DEFECTIVE_TOKEN;
199     }
200
201     tmp.length = load_uint32_be(p);
202     if (tmp.length != 0) {
203         if (remain - 4 < tmp.length) {
204             *minor = GSSEAP_TOK_TRUNC;
205             return GSS_S_DEFECTIVE_TOKEN;
206         }
207
208         tmp.value = p + 4;
209
210         major = gssEapImportNameInternal(minor, &tmp, pName,
211                                          EXPORT_NAME_FLAG_COMPOSITE);
212         if (GSS_ERROR(major))
213             return major;
214     }
215
216     *pBuf    += 4 + tmp.length;
217     *pRemain -= 4 + tmp.length;
218
219     *minor = 0;
220     return GSS_S_COMPLETE;
221 }
222
223 static OM_uint32
224 importConversation(OM_uint32 *minor,
225                    unsigned char **pBuf,
226                    size_t *pRemain,
227                    gss_ctx_id_t ctx)
228 {
229     OM_uint32 major;
230     unsigned char *p = *pBuf;
231     size_t remain = *pRemain;
232     gss_buffer_desc tmp;
233
234     if (remain < 4) {
235         *minor = GSSEAP_TOK_TRUNC;
236         return GSS_S_DEFECTIVE_TOKEN;
237     }
238
239     tmp.length = load_uint32_be(p);
240     if (tmp.length == 0 ||
241         remain - 4 < tmp.length) {
242         *minor = GSSEAP_TOK_TRUNC;
243         return GSS_S_DEFECTIVE_TOKEN;
244     }
245
246     if (p[4] != 0x06) {
247         *minor = GSSEAP_BAD_TOK_HEADER;
248         return GSS_S_DEFECTIVE_TOKEN;
249     }
250
251     tmp.value = p + 4;
252
253     major = duplicateBuffer(minor, &tmp, &ctx->conversation);
254     if (GSS_ERROR(major))
255         return major;
256
257     *pBuf    += 4 + tmp.length;
258     *pRemain -= 4 + tmp.length;
259
260     *minor = 0;
261     return GSS_S_COMPLETE;
262 }
263
264 static OM_uint32
265 gssEapImportContext(OM_uint32 *minor,
266                     gss_buffer_t token,
267                     gss_ctx_id_t ctx)
268 {
269     OM_uint32 major;
270     unsigned char *p = (unsigned char *)token->value;
271     size_t remain = token->length;
272
273     if (remain < 16) {
274         *minor = GSSEAP_TOK_TRUNC;
275         return GSS_S_DEFECTIVE_TOKEN;
276     }
277     if (load_uint32_be(&p[0]) != EAP_EXPORT_CONTEXT_V1) {
278         *minor = GSSEAP_BAD_CONTEXT_TOKEN;
279         return GSS_S_DEFECTIVE_TOKEN;
280     }
281     ctx->state      = load_uint32_be(&p[4]);
282     ctx->flags      = load_uint32_be(&p[8]);
283     ctx->gssFlags   = load_uint32_be(&p[12]);
284     p      += 16;
285     remain -= 16;
286
287     /* Validate state */
288     if (GSSEAP_SM_STATE(ctx) < GSSEAP_STATE_INITIAL ||
289         GSSEAP_SM_STATE(ctx) > GSSEAP_STATE_ESTABLISHED)
290         return GSS_S_DEFECTIVE_TOKEN;
291
292     /* Only acceptor can export partial context tokens */
293     if (CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx))
294         return GSS_S_DEFECTIVE_TOKEN;
295
296     major = importMechanismOid(minor, &p, &remain, &ctx->mechanismUsed);
297     if (GSS_ERROR(major))
298         return major;
299
300     major = importKerberosKey(minor, &p, &remain,
301                               &ctx->checksumType,
302                               &ctx->encryptionType,
303                               &ctx->rfc3961Key);
304     if (GSS_ERROR(major))
305         return major;
306
307     major = importName(minor, &p, &remain, &ctx->initiatorName);
308     if (GSS_ERROR(major))
309         return major;
310
311     major = importName(minor, &p, &remain, &ctx->acceptorName);
312     if (GSS_ERROR(major))
313         return major;
314
315     /* Check that, if context is established, names are valid */
316     if (CTX_IS_ESTABLISHED(ctx) &&
317         (CTX_IS_INITIATOR(ctx) ? ctx->acceptorName == GSS_C_NO_NAME
318                                : ctx->initiatorName == GSS_C_NO_NAME)) {
319         return GSS_S_DEFECTIVE_TOKEN;
320     }
321
322     if (remain < 24 + sequenceSize(ctx->seqState)) {
323         *minor = GSSEAP_TOK_TRUNC;
324         return GSS_S_DEFECTIVE_TOKEN;
325     }
326     ctx->expiryTime = (time_t)load_uint64_be(&p[0]);
327     ctx->sendSeq    = load_uint64_be(&p[8]);
328     ctx->recvSeq    = load_uint64_be(&p[16]);
329     p      += 24;
330     remain -= 24;
331
332     major = sequenceInternalize(minor, &ctx->seqState, &p, &remain);
333     if (GSS_ERROR(major))
334         return major;
335
336     /*
337      * The partial context should only be expected for unestablished
338      * acceptor contexts.
339      */
340     if (!CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx)) {
341         major = importConversation(minor, &p, &remain, ctx);
342         if (GSS_ERROR(major))
343             return major;
344
345         if ((ctx->flags & CTX_FLAG_KRB_REAUTH) == 0) {
346             major = importPartialRadiusContext(minor, &p, &remain, ctx);
347             if (GSS_ERROR(major))
348                 return major;
349         }
350     }
351
352 #ifdef GSSEAP_DEBUG
353     assert(remain == 0);
354 #endif
355
356     major = GSS_S_COMPLETE;
357     *minor = 0;
358
359     return major;
360 }
361
362 OM_uint32
363 gss_import_sec_context(OM_uint32 *minor,
364                        gss_buffer_t interprocess_token,
365                        gss_ctx_id_t *context_handle)
366 {
367     OM_uint32 major, tmpMinor;
368     gss_ctx_id_t ctx = GSS_C_NO_CONTEXT;
369
370     *context_handle = GSS_C_NO_CONTEXT;
371
372     if (interprocess_token == GSS_C_NO_BUFFER ||
373         interprocess_token->length == 0) {
374         *minor = GSSEAP_TOK_TRUNC;
375         return GSS_S_DEFECTIVE_TOKEN;
376     }
377
378     major = gssEapAllocContext(minor, &ctx);
379     if (GSS_ERROR(major))
380         goto cleanup;
381
382     major = gssEapImportContext(minor, interprocess_token, ctx);
383     if (GSS_ERROR(major))
384         goto cleanup;
385
386     *context_handle = ctx;
387
388 cleanup:
389     if (GSS_ERROR(major))
390         gssEapReleaseContext(&tmpMinor, &ctx);
391
392     return major;
393 }