initial TLV refactor
[mech_eap.orig] / 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 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     if (!gssEapIsConcreteMechanismOid(&oidBuf)) {
125         *minor = GSSEAP_WRONG_MECH;
126         return GSS_S_BAD_MECH;
127     }
128
129     if (!gssEapInternalizeOid(&oidBuf, pOid)) {
130         major = duplicateOid(minor, &oidBuf, pOid);
131         if (GSS_ERROR(major))
132             return major;
133     }
134
135     *pBuf    += 4 + oidBuf.length;
136     *pRemain -= 4 + oidBuf.length;
137
138     *minor = 0;
139     return GSS_S_COMPLETE;
140 }
141
142 static OM_uint32
143 importKerberosKey(OM_uint32 *minor,
144                   unsigned char **pBuf,
145                   size_t *pRemain,
146                   krb5_cksumtype *checksumType,
147                   krb5_enctype *pEncryptionType,
148                   krb5_keyblock *key)
149 {
150     unsigned char *p = *pBuf;
151     size_t remain = *pRemain;
152     OM_uint32 encryptionType;
153     OM_uint32 length;
154     gss_buffer_desc tmp;
155
156     if (remain < 12) {
157         *minor = GSSEAP_TOK_TRUNC;
158         return GSS_S_DEFECTIVE_TOKEN;
159     }
160
161     *checksumType  = load_uint32_be(&p[0]);
162     encryptionType = load_uint32_be(&p[4]);
163     length         = load_uint32_be(&p[8]);
164
165     if ((length != 0) != (encryptionType != ENCTYPE_NULL)) {
166         *minor = GSSEAP_BAD_CONTEXT_TOKEN;
167         return GSS_S_DEFECTIVE_TOKEN;
168     }
169
170     if (remain - 12 < length) {
171         *minor = GSSEAP_TOK_TRUNC;
172         return GSS_S_DEFECTIVE_TOKEN;
173     }
174
175     if (load_buffer(&p[12], length, &tmp) == NULL) {
176         *minor = ENOMEM;
177         return GSS_S_FAILURE;
178     }
179
180     KRB_KEY_TYPE(key)   = encryptionType;
181     KRB_KEY_LENGTH(key) = tmp.length;
182     KRB_KEY_DATA(key)   = (unsigned char *)tmp.value;
183
184     *pBuf    += 12 + length;
185     *pRemain -= 12 + length;
186     *pEncryptionType = encryptionType;
187
188     *minor = 0;
189     return GSS_S_COMPLETE;
190 }
191
192 static OM_uint32
193 importName(OM_uint32 *minor,
194            unsigned char **pBuf,
195            size_t *pRemain,
196            gss_name_t *pName)
197 {
198     OM_uint32 major;
199     unsigned char *p = *pBuf;
200     size_t remain = *pRemain;
201     gss_buffer_desc tmp;
202
203     if (remain < 4) {
204         *minor = GSSEAP_TOK_TRUNC;
205         return GSS_S_DEFECTIVE_TOKEN;
206     }
207
208     tmp.length = load_uint32_be(p);
209     if (tmp.length != 0) {
210         if (remain - 4 < tmp.length) {
211             *minor = GSSEAP_TOK_TRUNC;
212             return GSS_S_DEFECTIVE_TOKEN;
213         }
214
215         tmp.value = p + 4;
216
217         major = gssEapImportNameInternal(minor, &tmp, pName,
218                                          EXPORT_NAME_FLAG_COMPOSITE);
219         if (GSS_ERROR(major))
220             return major;
221     }
222
223     *pBuf    += 4 + tmp.length;
224     *pRemain -= 4 + tmp.length;
225
226     *minor = 0;
227     return GSS_S_COMPLETE;
228 }
229
230 static OM_uint32
231 gssEapImportContext(OM_uint32 *minor,
232                     gss_buffer_t token,
233                     gss_ctx_id_t ctx)
234 {
235     OM_uint32 major;
236     unsigned char *p = (unsigned char *)token->value;
237     size_t remain = token->length;
238
239     if (remain < 16) {
240         *minor = GSSEAP_TOK_TRUNC;
241         return GSS_S_DEFECTIVE_TOKEN;
242     }
243     if (load_uint32_be(&p[0]) != EAP_EXPORT_CONTEXT_V1) {
244         *minor = GSSEAP_BAD_CONTEXT_TOKEN;
245         return GSS_S_DEFECTIVE_TOKEN;
246     }
247     ctx->state      = load_uint32_be(&p[4]);
248     ctx->flags      = load_uint32_be(&p[8]);
249     ctx->gssFlags   = load_uint32_be(&p[12]);
250     p      += 16;
251     remain -= 16;
252
253     /* Validate state */
254     if (ctx->state < GSSEAP_STATE_INITIAL ||
255         ctx->state > GSSEAP_STATE_ESTABLISHED)
256         return GSS_S_DEFECTIVE_TOKEN;
257
258     /* Only acceptor can export partial context tokens */
259     if (CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx))
260         return GSS_S_DEFECTIVE_TOKEN;
261
262     major = importMechanismOid(minor, &p, &remain, &ctx->mechanismUsed);
263     if (GSS_ERROR(major))
264         return major;
265
266     major = importKerberosKey(minor, &p, &remain,
267                               &ctx->checksumType,
268                               &ctx->encryptionType,
269                               &ctx->rfc3961Key);
270     if (GSS_ERROR(major))
271         return major;
272
273     major = importName(minor, &p, &remain, &ctx->initiatorName);
274     if (GSS_ERROR(major))
275         return major;
276
277     major = importName(minor, &p, &remain, &ctx->acceptorName);
278     if (GSS_ERROR(major))
279         return major;
280
281     /* Check that, if context is established, names are valid */
282     if (CTX_IS_ESTABLISHED(ctx) &&
283         (CTX_IS_INITIATOR(ctx) ? ctx->acceptorName == GSS_C_NO_NAME
284                                : ctx->initiatorName == GSS_C_NO_NAME)) {
285         return GSS_S_DEFECTIVE_TOKEN;
286     }
287
288     if (remain < 24 + sequenceSize(ctx->seqState)) {
289         *minor = GSSEAP_TOK_TRUNC;
290         return GSS_S_DEFECTIVE_TOKEN;
291     }
292     ctx->expiryTime = (time_t)load_uint64_be(&p[0]);
293     ctx->sendSeq    = load_uint64_be(&p[8]);
294     ctx->recvSeq    = load_uint64_be(&p[16]);
295     p      += 24;
296     remain -= 24;
297
298     major = sequenceInternalize(minor, &ctx->seqState, &p, &remain);
299     if (GSS_ERROR(major))
300         return major;
301
302     /*
303      * The partial context should only be expected for unestablished
304      * acceptor contexts.
305      */
306     if (!CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx)) {
307         assert((ctx->flags & CTX_FLAG_KRB_REAUTH) == 0);
308
309         major = gssEapImportPartialContext(minor, &p, &remain, ctx);
310         if (GSS_ERROR(major))
311             return major;
312     }
313
314 #ifdef GSSEAP_DEBUG
315     assert(remain == 0);
316 #endif
317
318     major = GSS_S_COMPLETE;
319     *minor = 0;
320
321     return major;
322 }
323
324 OM_uint32
325 gss_import_sec_context(OM_uint32 *minor,
326                        gss_buffer_t interprocess_token,
327                        gss_ctx_id_t *context_handle)
328 {
329     OM_uint32 major, tmpMinor;
330     gss_ctx_id_t ctx = GSS_C_NO_CONTEXT;
331
332     *context_handle = GSS_C_NO_CONTEXT;
333
334     if (interprocess_token == GSS_C_NO_BUFFER ||
335         interprocess_token->length == 0) {
336         *minor = GSSEAP_TOK_TRUNC;
337         return GSS_S_DEFECTIVE_TOKEN;
338     }
339
340     major = gssEapAllocContext(minor, &ctx);
341     if (GSS_ERROR(major))
342         goto cleanup;
343
344     major = gssEapImportContext(minor, interprocess_token, ctx);
345     if (GSS_ERROR(major))
346         goto cleanup;
347
348     *context_handle = ctx;
349
350 cleanup:
351     if (GSS_ERROR(major))
352         gssEapReleaseContext(&tmpMinor, &ctx);
353
354     return major;
355 }