Change krbCred member to reauthCred to better clarify purpose
[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 gssEapImportPartialContext(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 gssEapImportContext(OM_uint32 *minor,
237                     gss_buffer_t token,
238                     gss_ctx_id_t ctx)
239 {
240     OM_uint32 major;
241     unsigned char *p = (unsigned char *)token->value;
242     size_t remain = token->length;
243
244     if (remain < 16) {
245         *minor = GSSEAP_TOK_TRUNC;
246         return GSS_S_DEFECTIVE_TOKEN;
247     }
248     if (load_uint32_be(&p[0]) != EAP_EXPORT_CONTEXT_V1) {
249         *minor = GSSEAP_BAD_CONTEXT_TOKEN;
250         return GSS_S_DEFECTIVE_TOKEN;
251     }
252     ctx->state      = load_uint32_be(&p[4]);
253     ctx->flags      = load_uint32_be(&p[8]);
254     ctx->gssFlags   = load_uint32_be(&p[12]);
255     p      += 16;
256     remain -= 16;
257
258     /* Validate state */
259     if (GSSEAP_SM_STATE(ctx) < GSSEAP_STATE_INITIAL ||
260         GSSEAP_SM_STATE(ctx) > GSSEAP_STATE_ESTABLISHED)
261         return GSS_S_DEFECTIVE_TOKEN;
262
263     /* Only acceptor can export partial context tokens */
264     if (CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx))
265         return GSS_S_DEFECTIVE_TOKEN;
266
267     major = importMechanismOid(minor, &p, &remain, &ctx->mechanismUsed);
268     if (GSS_ERROR(major))
269         return major;
270
271     major = importKerberosKey(minor, &p, &remain,
272                               &ctx->checksumType,
273                               &ctx->encryptionType,
274                               &ctx->rfc3961Key);
275     if (GSS_ERROR(major))
276         return major;
277
278     major = importName(minor, &p, &remain, &ctx->initiatorName);
279     if (GSS_ERROR(major))
280         return major;
281
282     major = importName(minor, &p, &remain, &ctx->acceptorName);
283     if (GSS_ERROR(major))
284         return major;
285
286     /* Check that, if context is established, names are valid */
287     if (CTX_IS_ESTABLISHED(ctx) &&
288         (CTX_IS_INITIATOR(ctx) ? ctx->acceptorName == GSS_C_NO_NAME
289                                : ctx->initiatorName == GSS_C_NO_NAME)) {
290         return GSS_S_DEFECTIVE_TOKEN;
291     }
292
293     if (remain < 24 + sequenceSize(ctx->seqState)) {
294         *minor = GSSEAP_TOK_TRUNC;
295         return GSS_S_DEFECTIVE_TOKEN;
296     }
297     ctx->expiryTime = (time_t)load_uint64_be(&p[0]);
298     ctx->sendSeq    = load_uint64_be(&p[8]);
299     ctx->recvSeq    = load_uint64_be(&p[16]);
300     p      += 24;
301     remain -= 24;
302
303     major = sequenceInternalize(minor, &ctx->seqState, &p, &remain);
304     if (GSS_ERROR(major))
305         return major;
306
307     /*
308      * The partial context should only be expected for unestablished
309      * acceptor contexts.
310      */
311     if (!CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx) &&
312         (ctx->flags & CTX_FLAG_KRB_REAUTH) == 0) {
313         major = gssEapImportPartialContext(minor, &p, &remain, ctx);
314         if (GSS_ERROR(major))
315             return major;
316     }
317
318 #ifdef GSSEAP_DEBUG
319     assert(remain == 0);
320 #endif
321
322     major = GSS_S_COMPLETE;
323     *minor = 0;
324
325     return major;
326 }
327
328 OM_uint32
329 gss_import_sec_context(OM_uint32 *minor,
330                        gss_buffer_t interprocess_token,
331                        gss_ctx_id_t *context_handle)
332 {
333     OM_uint32 major, tmpMinor;
334     gss_ctx_id_t ctx = GSS_C_NO_CONTEXT;
335
336     *context_handle = GSS_C_NO_CONTEXT;
337
338     if (interprocess_token == GSS_C_NO_BUFFER ||
339         interprocess_token->length == 0) {
340         *minor = GSSEAP_TOK_TRUNC;
341         return GSS_S_DEFECTIVE_TOKEN;
342     }
343
344     major = gssEapAllocContext(minor, &ctx);
345     if (GSS_ERROR(major))
346         goto cleanup;
347
348     major = gssEapImportContext(minor, interprocess_token, ctx);
349     if (GSS_ERROR(major))
350         goto cleanup;
351
352     *context_handle = ctx;
353
354 cleanup:
355     if (GSS_ERROR(major))
356         gssEapReleaseContext(&tmpMinor, &ctx);
357
358     return major;
359 }