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