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