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