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