2 * Copyright (c) 2011, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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
34 * Deserialise a context handle.
37 #include "gssapiP_eap.h"
39 #define UPDATE_REMAIN(n) do { \
44 #define CHECK_REMAIN(n) do { \
46 *minor = GSSEAP_TOK_TRUNC; \
47 return GSS_S_DEFECTIVE_TOKEN; \
52 gssEapImportPartialContext(OM_uint32 *minor,
58 unsigned char *p = *pBuf;
59 size_t remain = *pRemain;
61 size_t ctxLength, serverLen;
63 /* Length of partial RADIUS context */
65 ctxLength = load_uint32_be(p);
68 CHECK_REMAIN(ctxLength);
69 remain = ctxLength; /* check against partial context length */
71 /* Selected RADIUS server */
73 serverLen = load_uint32_be(p);
77 CHECK_REMAIN(serverLen);
79 ctx->acceptorCtx.radServer = GSSEAP_MALLOC(serverLen + 1);
80 if (ctx->acceptorCtx.radServer == NULL) {
84 memcpy(ctx->acceptorCtx.radServer, p, serverLen);
85 ctx->acceptorCtx.radServer[serverLen] = '\0';
87 UPDATE_REMAIN(serverLen);
90 /* RADIUS state blob */
92 buf.length = load_uint32_be(p);
95 if (buf.length != 0) {
96 CHECK_REMAIN(buf.length);
100 major = duplicateBuffer(minor, &buf, &ctx->acceptorCtx.state);
101 if (GSS_ERROR(major))
104 UPDATE_REMAIN(buf.length);
112 *pRemain -= 4 + ctxLength;
114 return GSS_S_COMPLETE;
118 importMechanismOid(OM_uint32 *minor,
119 unsigned char **pBuf,
124 unsigned char *p = *pBuf;
125 size_t remain = *pRemain;
128 oidBuf.length = load_uint32_be(p);
129 if (remain < 4 + oidBuf.length || oidBuf.length == 0) {
130 *minor = GSSEAP_TOK_TRUNC;
131 return GSS_S_DEFECTIVE_TOKEN;
134 oidBuf.elements = &p[4];
136 major = gssEapCanonicalizeOid(minor, &oidBuf, 0, pOid);
137 if (GSS_ERROR(major))
140 *pBuf += 4 + oidBuf.length;
141 *pRemain -= 4 + oidBuf.length;
144 return GSS_S_COMPLETE;
148 importKerberosKey(OM_uint32 *minor,
149 unsigned char **pBuf,
151 krb5_cksumtype *checksumType,
152 krb5_enctype *pEncryptionType,
155 unsigned char *p = *pBuf;
156 size_t remain = *pRemain;
157 OM_uint32 encryptionType;
162 *minor = GSSEAP_TOK_TRUNC;
163 return GSS_S_DEFECTIVE_TOKEN;
166 *checksumType = load_uint32_be(&p[0]);
167 encryptionType = load_uint32_be(&p[4]);
168 length = load_uint32_be(&p[8]);
170 if ((length != 0) != (encryptionType != ENCTYPE_NULL)) {
171 *minor = GSSEAP_BAD_CONTEXT_TOKEN;
172 return GSS_S_DEFECTIVE_TOKEN;
175 if (remain - 12 < length) {
176 *minor = GSSEAP_TOK_TRUNC;
177 return GSS_S_DEFECTIVE_TOKEN;
180 if (load_buffer(&p[12], length, &tmp) == NULL) {
182 return GSS_S_FAILURE;
185 KRB_KEY_TYPE(key) = encryptionType;
186 KRB_KEY_LENGTH(key) = tmp.length;
187 KRB_KEY_DATA(key) = (unsigned char *)tmp.value;
189 *pBuf += 12 + length;
190 *pRemain -= 12 + length;
191 *pEncryptionType = encryptionType;
194 return GSS_S_COMPLETE;
198 importName(OM_uint32 *minor,
199 unsigned char **pBuf,
204 unsigned char *p = *pBuf;
205 size_t remain = *pRemain;
209 *minor = GSSEAP_TOK_TRUNC;
210 return GSS_S_DEFECTIVE_TOKEN;
213 tmp.length = load_uint32_be(p);
214 if (tmp.length != 0) {
215 if (remain - 4 < tmp.length) {
216 *minor = GSSEAP_TOK_TRUNC;
217 return GSS_S_DEFECTIVE_TOKEN;
222 major = gssEapImportNameInternal(minor, &tmp, pName,
223 EXPORT_NAME_FLAG_COMPOSITE);
224 if (GSS_ERROR(major))
228 *pBuf += 4 + tmp.length;
229 *pRemain -= 4 + tmp.length;
232 return GSS_S_COMPLETE;
236 gssEapImportContext(OM_uint32 *minor,
241 unsigned char *p = (unsigned char *)token->value;
242 size_t remain = token->length;
245 *minor = GSSEAP_TOK_TRUNC;
246 return GSS_S_DEFECTIVE_TOKEN;
248 if (load_uint32_be(&p[0]) != EAP_EXPORT_CONTEXT_V1) {
249 *minor = GSSEAP_BAD_CONTEXT_TOKEN;
250 return GSS_S_DEFECTIVE_TOKEN;
252 ctx->state = load_uint32_be(&p[4]);
253 ctx->flags = load_uint32_be(&p[8]);
254 ctx->gssFlags = load_uint32_be(&p[12]);
259 if (GSSEAP_SM_STATE(ctx) < GSSEAP_STATE_INITIAL ||
260 GSSEAP_SM_STATE(ctx) > GSSEAP_STATE_ESTABLISHED)
261 return GSS_S_DEFECTIVE_TOKEN;
263 /* Only acceptor can export partial context tokens */
264 if (CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx))
265 return GSS_S_DEFECTIVE_TOKEN;
267 major = importMechanismOid(minor, &p, &remain, &ctx->mechanismUsed);
268 if (GSS_ERROR(major))
271 major = importKerberosKey(minor, &p, &remain,
273 &ctx->encryptionType,
275 if (GSS_ERROR(major))
278 major = importName(minor, &p, &remain, &ctx->initiatorName);
279 if (GSS_ERROR(major))
282 major = importName(minor, &p, &remain, &ctx->acceptorName);
283 if (GSS_ERROR(major))
286 /* Check that, if context is established, names are valid */
287 if (CTX_IS_ESTABLISHED(ctx) &&
288 (CTX_IS_INITIATOR(ctx) ? ctx->acceptorName == GSS_C_NO_NAME
289 : ctx->initiatorName == GSS_C_NO_NAME)) {
290 return GSS_S_DEFECTIVE_TOKEN;
293 if (remain < 24 + sequenceSize(ctx->seqState)) {
294 *minor = GSSEAP_TOK_TRUNC;
295 return GSS_S_DEFECTIVE_TOKEN;
297 ctx->expiryTime = (time_t)load_uint64_be(&p[0]);
298 ctx->sendSeq = load_uint64_be(&p[8]);
299 ctx->recvSeq = load_uint64_be(&p[16]);
303 major = sequenceInternalize(minor, &ctx->seqState, &p, &remain);
304 if (GSS_ERROR(major))
308 * The partial context should only be expected for unestablished
311 if (!CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx) &&
312 (ctx->flags & CTX_FLAG_KRB_REAUTH) == 0) {
313 major = gssEapImportPartialContext(minor, &p, &remain, ctx);
314 if (GSS_ERROR(major))
322 major = GSS_S_COMPLETE;
329 gss_import_sec_context(OM_uint32 *minor,
330 gss_buffer_t interprocess_token,
331 gss_ctx_id_t *context_handle)
333 OM_uint32 major, tmpMinor;
334 gss_ctx_id_t ctx = GSS_C_NO_CONTEXT;
336 *context_handle = GSS_C_NO_CONTEXT;
338 if (interprocess_token == GSS_C_NO_BUFFER ||
339 interprocess_token->length == 0) {
340 *minor = GSSEAP_TOK_TRUNC;
341 return GSS_S_DEFECTIVE_TOKEN;
344 major = gssEapAllocContext(minor, &ctx);
345 if (GSS_ERROR(major))
348 major = gssEapImportContext(minor, interprocess_token, ctx);
349 if (GSS_ERROR(major))
352 *context_handle = ctx;
355 if (GSS_ERROR(major))
356 gssEapReleaseContext(&tmpMinor, &ctx);