cleanup, various
[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_keyblock *key)
102 {
103     unsigned char *p = *pBuf;
104     size_t remain = *pRemain;
105     OM_uint32 encryptionType;
106     OM_uint32 length;
107     gss_buffer_desc tmp;
108
109     if (remain < 8) {
110         *minor = ERANGE;
111         return GSS_S_DEFECTIVE_TOKEN;
112     }
113
114     encryptionType = load_uint32_be(&p[0]);
115     length         = load_uint32_be(&p[4]);
116
117     if ((length != 0) != (encryptionType != ENCTYPE_NULL)) {
118         *minor = ERANGE;
119         return GSS_S_DEFECTIVE_TOKEN;
120     }
121
122     if (remain - 8 < length) {
123         *minor = ERANGE;
124         return GSS_S_DEFECTIVE_TOKEN;
125     }
126
127     if (load_buffer(&p[8], length, &tmp) == NULL) {
128         *minor = ENOMEM;
129         return GSS_S_FAILURE;
130     }
131
132     KRB_KEY_TYPE(key)   = encryptionType;
133     KRB_KEY_LENGTH(key) = tmp.length;
134     KRB_KEY_DATA(key)   = (unsigned char *)tmp.value;
135
136     *pBuf    += 8 + length;
137     *pRemain -= 8 + length;
138
139     *minor = 0;
140     return GSS_S_COMPLETE;
141 }
142
143 static OM_uint32
144 importName(OM_uint32 *minor,
145            unsigned char **pBuf,
146            size_t *pRemain,
147            gss_name_t *pName)
148 {
149     OM_uint32 major;
150     unsigned char *p = *pBuf;
151     size_t remain = *pRemain;
152     gss_buffer_desc tmp;
153
154     if (remain < 4) {
155         *minor = ERANGE;
156         return GSS_S_DEFECTIVE_TOKEN;
157     }
158
159     tmp.length = load_uint32_be(p);
160     if (tmp.length != 0) {
161         if (remain - 4 < tmp.length) {
162             *minor = ERANGE;
163             return GSS_S_DEFECTIVE_TOKEN;
164         }
165
166         tmp.value = p + 4;
167
168         major = gssEapImportName(minor, &tmp, GSS_C_NT_EXPORT_NAME, pName);
169         if (GSS_ERROR(major))
170             return major;
171     }
172
173     *pBuf    += 4 + tmp.length;
174     *pRemain -= 4 + tmp.length;
175
176     *minor = 0;
177     return GSS_S_COMPLETE;
178 }
179
180 static OM_uint32
181 gssEapImportContext(OM_uint32 *minor,
182                     gss_buffer_t token,
183                     gss_ctx_id_t ctx)
184 {
185     OM_uint32 major;
186     unsigned char *p = (unsigned char *)token->value;
187     size_t remain = token->length;
188
189     if (remain < 16) {
190         *minor = ERANGE;
191         return GSS_S_DEFECTIVE_TOKEN;
192     }
193     if (load_uint32_be(&p[0]) != EAP_EXPORT_CONTEXT_V1) {
194         *minor = EINVAL;
195         return GSS_S_DEFECTIVE_TOKEN;
196     }
197     ctx->state      = load_uint32_be(&p[4]);
198     ctx->flags      = load_uint32_be(&p[8]);
199     ctx->gssFlags   = load_uint32_be(&p[12]);
200     p      += 16;
201     remain -= 16;
202
203     /* Validate state */
204     if (ctx->state < EAP_STATE_AUTHENTICATE ||
205         ctx->state > EAP_STATE_ESTABLISHED)
206         return GSS_S_DEFECTIVE_TOKEN;
207
208     /* Only acceptor can export partial context tokens */
209     if (CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx))
210         return GSS_S_DEFECTIVE_TOKEN;
211
212     major = importMechanismOid(minor, &p, &remain, &ctx->mechanismUsed);
213     if (GSS_ERROR(major))
214         return major;
215
216     major = importKerberosKey(minor, &p, &remain, &ctx->rfc3961Key);
217     if (GSS_ERROR(major))
218         return major;
219
220     ctx->encryptionType = KRB_KEY_TYPE(&ctx->rfc3961Key);
221
222     major = importName(minor, &p, &remain, &ctx->initiatorName);
223     if (GSS_ERROR(major))
224         return major;
225
226     major = importName(minor, &p, &remain, &ctx->acceptorName);
227     if (GSS_ERROR(major))
228         return major;
229
230     /* Check that, if context is established, names are valid */
231     if (CTX_IS_ESTABLISHED(ctx) &&
232         (CTX_IS_INITIATOR(ctx) ? ctx->acceptorName == GSS_C_NO_NAME
233                                : ctx->initiatorName == GSS_C_NO_NAME)) {
234         return GSS_S_DEFECTIVE_TOKEN;
235     }
236
237     if (remain < 24 + sequenceSize(ctx->seqState)) {
238         *minor = ERANGE;
239         return GSS_S_DEFECTIVE_TOKEN;
240     }
241     ctx->expiryTime = (time_t)load_uint64_be(&p[0]); /* XXX */
242     ctx->sendSeq    = load_uint64_be(&p[8]);
243     ctx->recvSeq    = load_uint64_be(&p[16]);
244     p      += 24;
245     remain -= 24;
246
247     *minor = sequenceInternalize(&ctx->seqState, &p, &remain);
248     if (*minor != 0)
249         return GSS_S_FAILURE;
250
251     /*
252      * The partial context should only be expected for unestablished
253      * acceptor contexts.
254      */
255     if (!CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx)) {
256         major = gssEapImportPartialContext(minor, &p, &remain, ctx);
257         if (GSS_ERROR(major))
258             return major;
259     }
260
261     assert(remain == 0);
262
263     *minor = 0;
264     major = GSS_S_COMPLETE;
265
266     return major;
267 }
268
269 OM_uint32
270 gss_import_sec_context(OM_uint32 *minor,
271                        gss_buffer_t interprocess_token,
272                        gss_ctx_id_t *context_handle)
273 {
274     OM_uint32 major, tmpMinor;
275     gss_ctx_id_t ctx = GSS_C_NO_CONTEXT;
276
277     *context_handle = GSS_C_NO_CONTEXT;
278
279     if (interprocess_token == GSS_C_NO_BUFFER ||
280         interprocess_token->length == 0)
281         return GSS_S_DEFECTIVE_TOKEN;
282
283     major = gssEapAllocContext(minor, &ctx);
284     if (GSS_ERROR(major))
285         goto cleanup;
286
287     major = gssEapImportContext(minor, interprocess_token, ctx);
288     if (GSS_ERROR(major))
289         goto cleanup;
290
291     *context_handle = ctx;
292
293 cleanup:
294     if (GSS_ERROR(major))
295         gssEapReleaseContext(&tmpMinor, &ctx);
296
297     return major;
298 }