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