Fixes for Heimdal (macOS) builds from Stefan.
[mech_eap.git] / mech_eap / import_sec_context.c
1 /*
2  * Copyright (c) 2011, 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 /*
34  * Deserialise a context handle.
35  */
36
37 #include "gssapiP_eap.h"
38
39 #define UPDATE_REMAIN(n)    do {                \
40         p += (n);                               \
41         remain -= (n);                          \
42     } while (0)
43
44 #define CHECK_REMAIN(n)     do {                \
45         if (remain < (n)) {                     \
46             *minor = GSSEAP_TOK_TRUNC;          \
47             return GSS_S_DEFECTIVE_TOKEN;       \
48         }                                       \
49     } while (0)
50
51 #ifdef GSSEAP_ENABLE_ACCEPTOR
52 static OM_uint32
53 gssEapImportPartialContext(OM_uint32 *minor,
54                            unsigned char **pBuf,
55                            size_t *pRemain,
56                            gss_ctx_id_t ctx)
57 {
58     OM_uint32 major;
59     unsigned char *p = *pBuf;
60     size_t remain = *pRemain;
61     gss_buffer_desc buf;
62     size_t ctxLength, serverLen;
63
64     /* Length of partial RADIUS context */
65     CHECK_REMAIN(4);
66     ctxLength = load_uint32_be(p);
67     UPDATE_REMAIN(4);
68
69     CHECK_REMAIN(ctxLength);
70     remain = ctxLength; /* check against partial context length */
71
72     /* Selected RADIUS server */
73     CHECK_REMAIN(4);
74     serverLen = load_uint32_be(p);
75     UPDATE_REMAIN(4);
76
77     if (serverLen != 0) {
78         CHECK_REMAIN(serverLen);
79
80         ctx->acceptorCtx.radServer = GSSEAP_MALLOC(serverLen + 1);
81         if (ctx->acceptorCtx.radServer == NULL) {
82             *minor = ENOMEM;
83             return GSS_S_FAILURE;
84         }
85         memcpy(ctx->acceptorCtx.radServer, p, serverLen);
86         ctx->acceptorCtx.radServer[serverLen] = '\0';
87
88         UPDATE_REMAIN(serverLen);
89     }
90
91     /* RADIUS state blob */
92     CHECK_REMAIN(4);
93     buf.length = load_uint32_be(p);
94     UPDATE_REMAIN(4);
95
96     if (buf.length != 0) {
97         CHECK_REMAIN(buf.length);
98
99         buf.value = p;
100
101         major = duplicateBuffer(minor, &buf, &ctx->acceptorCtx.state);
102         if (GSS_ERROR(major))
103             return major;
104
105         UPDATE_REMAIN(buf.length);
106     }
107
108 #ifdef GSSEAP_DEBUG
109     GSSEAP_ASSERT(remain == 0);
110 #endif
111
112     *pBuf = p;
113     *pRemain -= 4 + ctxLength;
114
115     return GSS_S_COMPLETE;
116 }
117 #endif /* GSSEAP_ENABLE_ACCEPTOR */
118
119 static OM_uint32
120 importMechanismOid(OM_uint32 *minor,
121                    unsigned char **pBuf,
122                    size_t *pRemain,
123                    gss_OID *pOid)
124 {
125     OM_uint32 major;
126     unsigned char *p = *pBuf;
127     size_t remain = *pRemain;
128     gss_OID_desc oidBuf;
129
130     oidBuf.length = load_uint32_be(p);
131     if (remain < 4 + oidBuf.length || oidBuf.length == 0) {
132         *minor = GSSEAP_TOK_TRUNC;
133         return GSS_S_DEFECTIVE_TOKEN;
134     }
135
136     oidBuf.elements = &p[4];
137
138     major = gssEapCanonicalizeOid(minor, &oidBuf, 0, pOid);
139     if (GSS_ERROR(major))
140         return major;
141
142     *pBuf    += 4 + oidBuf.length;
143     *pRemain -= 4 + oidBuf.length;
144
145     *minor = 0;
146     return GSS_S_COMPLETE;
147 }
148
149 static OM_uint32
150 importKerberosKey(OM_uint32 *minor,
151                   unsigned char **pBuf,
152                   size_t *pRemain,
153                   krb5_cksumtype *checksumType,
154                   krb5_enctype *pEncryptionType,
155                   krb5_keyblock *pKey)
156 {
157     unsigned char *p = *pBuf;
158     size_t remain = *pRemain;
159     OM_uint32 encryptionType;
160     OM_uint32 length;
161     krb5_context krbContext;
162     krb5_keyblock key;
163     krb5_error_code code;
164
165     GSSEAP_KRB_INIT(&krbContext);
166
167     KRB_KEY_INIT(pKey);
168
169     if (remain < 12) {
170         *minor = GSSEAP_TOK_TRUNC;
171         return GSS_S_DEFECTIVE_TOKEN;
172     }
173
174     *checksumType  = load_uint32_be(&p[0]);
175     encryptionType = load_uint32_be(&p[4]);
176     length         = load_uint32_be(&p[8]);
177
178     if ((length != 0) != (encryptionType != ENCTYPE_NULL)) {
179         *minor = GSSEAP_BAD_CONTEXT_TOKEN;
180         return GSS_S_DEFECTIVE_TOKEN;
181     }
182
183     if (remain - 12 < length) {
184         *minor = GSSEAP_TOK_TRUNC;
185         return GSS_S_DEFECTIVE_TOKEN;
186     }
187
188     if (encryptionType != ENCTYPE_NULL) {
189         KRB_KEY_INIT(&key);
190
191         KRB_KEY_TYPE(&key)   = encryptionType;
192         KRB_KEY_LENGTH(&key) = length;
193         KRB_KEY_DATA(&key)   = &p[12];
194
195         code = krb5_copy_keyblock_contents(krbContext, &key, pKey);
196         if (code != 0) {
197             *minor = code;
198             return GSS_S_FAILURE;
199         }
200     }
201
202     *pBuf    += 12 + length;
203     *pRemain -= 12 + length;
204     *pEncryptionType = encryptionType;
205
206     *minor = 0;
207     return GSS_S_COMPLETE;
208 }
209
210 static OM_uint32
211 importName(OM_uint32 *minor,
212            gss_OID mech,
213            unsigned char **pBuf,
214            size_t *pRemain,
215            gss_name_t *pName)
216 {
217     OM_uint32 major, tmpMinor, flags;
218     unsigned char *p = *pBuf;
219     size_t remain = *pRemain;
220     gss_buffer_desc tmp;
221
222     if (remain < 4) {
223         *minor = GSSEAP_TOK_TRUNC;
224         return GSS_S_DEFECTIVE_TOKEN;
225     }
226
227     tmp.length = load_uint32_be(p);
228     if (tmp.length != 0) {
229         if (remain - 4 < tmp.length) {
230             *minor = GSSEAP_TOK_TRUNC;
231             return GSS_S_DEFECTIVE_TOKEN;
232         }
233
234         tmp.value = p + 4;
235
236         flags = EXPORT_NAME_FLAG_COMPOSITE;
237         if (mech == GSS_C_NO_OID)
238             flags |= EXPORT_NAME_FLAG_OID;
239
240         major = gssEapImportNameInternal(minor, &tmp, pName, flags);
241         if (GSS_ERROR(major))
242             return major;
243
244         if ((flags & EXPORT_NAME_FLAG_OID) == 0) {
245             major = gssEapCanonicalizeOid(minor, mech, 0, &(*pName)->mechanismUsed);
246             if (GSS_ERROR(major)) {
247                 gssEapReleaseName(&tmpMinor, pName);
248                 return major;
249             }
250         }
251     }
252
253     *pBuf    += 4 + tmp.length;
254     *pRemain -= 4 + tmp.length;
255
256     *minor = 0;
257     return GSS_S_COMPLETE;
258 }
259
260 OM_uint32
261 gssEapImportContext(OM_uint32 *minor,
262                     gss_buffer_t token,
263                     gss_ctx_id_t ctx)
264 {
265     OM_uint32 major;
266     unsigned char *p = (unsigned char *)token->value;
267     size_t remain = token->length;
268
269     if (remain < 16) {
270         *minor = GSSEAP_TOK_TRUNC;
271         return GSS_S_DEFECTIVE_TOKEN;
272     }
273     if (load_uint32_be(&p[0]) != EAP_EXPORT_CONTEXT_V1) {
274         *minor = GSSEAP_BAD_CONTEXT_TOKEN;
275         return GSS_S_DEFECTIVE_TOKEN;
276     }
277     ctx->state      = load_uint32_be(&p[4]);
278     ctx->flags      = load_uint32_be(&p[8]);
279     ctx->gssFlags   = load_uint32_be(&p[12]);
280     p      += 16;
281     remain -= 16;
282
283     /* Validate state */
284     if (GSSEAP_SM_STATE(ctx) < GSSEAP_STATE_INITIAL ||
285         GSSEAP_SM_STATE(ctx) > GSSEAP_STATE_ESTABLISHED)
286         return GSS_S_DEFECTIVE_TOKEN;
287
288     /* Only acceptor can export partial context tokens */
289     if (CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx))
290         return GSS_S_DEFECTIVE_TOKEN;
291
292     major = importMechanismOid(minor, &p, &remain, &ctx->mechanismUsed);
293     if (GSS_ERROR(major))
294         return major;
295
296     major = importKerberosKey(minor, &p, &remain,
297                               &ctx->checksumType,
298                               &ctx->encryptionType,
299                               &ctx->rfc3961Key);
300     if (GSS_ERROR(major))
301         return major;
302
303     /* Initiator name OID matches the context mechanism, so it's not encoded */
304     major = importName(minor, ctx->mechanismUsed, &p, &remain, &ctx->initiatorName);
305     if (GSS_ERROR(major))
306         return major;
307
308     major = importName(minor, GSS_C_NO_OID, &p, &remain, &ctx->acceptorName);
309     if (GSS_ERROR(major))
310         return major;
311
312     /* Check that, if context is established, names are valid */
313     if (CTX_IS_ESTABLISHED(ctx) &&
314         (CTX_IS_INITIATOR(ctx) ? ctx->acceptorName == GSS_C_NO_NAME
315                                : ctx->initiatorName == GSS_C_NO_NAME)) {
316         return GSS_S_DEFECTIVE_TOKEN;
317     }
318
319     if (remain < 24 + sequenceSize(ctx->seqState)) {
320         *minor = GSSEAP_TOK_TRUNC;
321         return GSS_S_DEFECTIVE_TOKEN;
322     }
323     ctx->expiryTime = (time_t)load_uint64_be(&p[0]);
324     ctx->sendSeq    = load_uint64_be(&p[8]);
325     ctx->recvSeq    = load_uint64_be(&p[16]);
326     p      += 24;
327     remain -= 24;
328
329     major = sequenceInternalize(minor, &ctx->seqState, &p, &remain);
330     if (GSS_ERROR(major))
331         return major;
332
333 #ifdef GSSEAP_ENABLE_ACCEPTOR
334     /*
335      * The partial context should only be expected for unestablished
336      * acceptor contexts.
337      */
338     if (!CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx) &&
339         (ctx->flags & CTX_FLAG_KRB_REAUTH) == 0) {
340         major = gssEapImportPartialContext(minor, &p, &remain, ctx);
341         if (GSS_ERROR(major))
342             return major;
343     }
344
345 #ifdef GSSEAP_DEBUG
346     GSSEAP_ASSERT(remain == 0);
347 #endif
348 #endif /* GSSEAP_ENABLE_ACCEPTOR */
349
350     major = GSS_S_COMPLETE;
351     *minor = 0;
352
353     return major;
354 }
355
356 OM_uint32 GSSAPI_CALLCONV
357 gss_import_sec_context(OM_uint32 *minor,
358                        gss_buffer_t interprocess_token,
359                        gss_ctx_id_t *context_handle)
360 {
361     OM_uint32 major, tmpMinor;
362     gss_ctx_id_t ctx = GSS_C_NO_CONTEXT;
363
364     *context_handle = GSS_C_NO_CONTEXT;
365
366     if (interprocess_token == GSS_C_NO_BUFFER ||
367         interprocess_token->length == 0) {
368         *minor = GSSEAP_TOK_TRUNC;
369         return GSS_S_DEFECTIVE_TOKEN;
370     }
371
372     major = gssEapAllocContext(minor, &ctx);
373     if (GSS_ERROR(major))
374         goto cleanup;
375
376     major = gssEapImportContext(minor, interprocess_token, ctx);
377     if (GSS_ERROR(major))
378         goto cleanup;
379
380     *context_handle = ctx;
381
382 cleanup:
383     if (GSS_ERROR(major))
384         gssEapReleaseContext(&tmpMinor, &ctx);
385
386     return major;
387 }