cache the checksumtype and use MIT private API
[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 = gssEapImportName(minor, &tmp, GSS_C_NT_EXPORT_NAME, pName);
173         if (GSS_ERROR(major))
174             return major;
175     }
176
177     *pBuf    += 4 + tmp.length;
178     *pRemain -= 4 + tmp.length;
179
180     *minor = 0;
181     return GSS_S_COMPLETE;
182 }
183
184 static OM_uint32
185 gssEapImportContext(OM_uint32 *minor,
186                     gss_buffer_t token,
187                     gss_ctx_id_t ctx)
188 {
189     OM_uint32 major;
190     unsigned char *p = (unsigned char *)token->value;
191     size_t remain = token->length;
192
193     if (remain < 16) {
194         *minor = ERANGE;
195         return GSS_S_DEFECTIVE_TOKEN;
196     }
197     if (load_uint32_be(&p[0]) != EAP_EXPORT_CONTEXT_V1) {
198         *minor = EINVAL;
199         return GSS_S_DEFECTIVE_TOKEN;
200     }
201     ctx->state      = load_uint32_be(&p[4]);
202     ctx->flags      = load_uint32_be(&p[8]);
203     ctx->gssFlags   = load_uint32_be(&p[12]);
204     p      += 16;
205     remain -= 16;
206
207     /* Validate state */
208     if (ctx->state < EAP_STATE_AUTHENTICATE ||
209         ctx->state > EAP_STATE_ESTABLISHED)
210         return GSS_S_DEFECTIVE_TOKEN;
211
212     /* Only acceptor can export partial context tokens */
213     if (CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx))
214         return GSS_S_DEFECTIVE_TOKEN;
215
216     major = importMechanismOid(minor, &p, &remain, &ctx->mechanismUsed);
217     if (GSS_ERROR(major))
218         return major;
219
220     major = importKerberosKey(minor, &p, &remain,
221                               &ctx->checksumType,
222                               &ctx->encryptionType,
223                               &ctx->rfc3961Key);
224     if (GSS_ERROR(major))
225         return major;
226
227     major = importName(minor, &p, &remain, &ctx->initiatorName);
228     if (GSS_ERROR(major))
229         return major;
230
231     major = importName(minor, &p, &remain, &ctx->acceptorName);
232     if (GSS_ERROR(major))
233         return major;
234
235     /* Check that, if context is established, names are valid */
236     if (CTX_IS_ESTABLISHED(ctx) &&
237         (CTX_IS_INITIATOR(ctx) ? ctx->acceptorName == GSS_C_NO_NAME
238                                : ctx->initiatorName == GSS_C_NO_NAME)) {
239         return GSS_S_DEFECTIVE_TOKEN;
240     }
241
242     if (remain < 24 + sequenceSize(ctx->seqState)) {
243         *minor = ERANGE;
244         return GSS_S_DEFECTIVE_TOKEN;
245     }
246     ctx->expiryTime = (time_t)load_uint64_be(&p[0]); /* XXX */
247     ctx->sendSeq    = load_uint64_be(&p[8]);
248     ctx->recvSeq    = load_uint64_be(&p[16]);
249     p      += 24;
250     remain -= 24;
251
252     *minor = sequenceInternalize(&ctx->seqState, &p, &remain);
253     if (*minor != 0)
254         return GSS_S_FAILURE;
255
256     /*
257      * The partial context should only be expected for unestablished
258      * acceptor contexts.
259      */
260     if (!CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx)) {
261         major = gssEapImportPartialContext(minor, &p, &remain, ctx);
262         if (GSS_ERROR(major))
263             return major;
264     }
265
266     assert(remain == 0);
267
268     *minor = 0;
269     major = GSS_S_COMPLETE;
270
271     return major;
272 }
273
274 OM_uint32
275 gss_import_sec_context(OM_uint32 *minor,
276                        gss_buffer_t interprocess_token,
277                        gss_ctx_id_t *context_handle)
278 {
279     OM_uint32 major, tmpMinor;
280     gss_ctx_id_t ctx = GSS_C_NO_CONTEXT;
281
282     *context_handle = GSS_C_NO_CONTEXT;
283
284     if (interprocess_token == GSS_C_NO_BUFFER ||
285         interprocess_token->length == 0)
286         return GSS_S_DEFECTIVE_TOKEN;
287
288     major = gssEapAllocContext(minor, &ctx);
289     if (GSS_ERROR(major))
290         goto cleanup;
291
292     major = gssEapImportContext(minor, interprocess_token, ctx);
293     if (GSS_ERROR(major))
294         goto cleanup;
295
296     *context_handle = ctx;
297
298 cleanup:
299     if (GSS_ERROR(major))
300         gssEapReleaseContext(&tmpMinor, &ctx);
301
302     return major;
303 }