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 importPartialRadiusContext(OM_uint32 *minor,
58 unsigned char *p = *pBuf;
59 size_t remain = *pRemain;
63 /* Selected RADIUS server */
65 serverLen = load_uint32_be(p);
69 CHECK_REMAIN(serverLen);
71 ctx->acceptorCtx.radServer = GSSEAP_MALLOC(serverLen + 1);
72 if (ctx->acceptorCtx.radServer == NULL) {
76 memcpy(ctx->acceptorCtx.radServer, p, serverLen);
77 ctx->acceptorCtx.radServer[serverLen] = '\0';
79 UPDATE_REMAIN(serverLen);
82 /* RADIUS state blob */
84 buf.length = load_uint32_be(p);
87 if (buf.length != 0) {
88 CHECK_REMAIN(buf.length);
92 major = duplicateBuffer(minor, &buf, &ctx->acceptorCtx.state);
96 UPDATE_REMAIN(buf.length);
102 return GSS_S_COMPLETE;
106 importMechanismOid(OM_uint32 *minor,
107 unsigned char **pBuf,
112 unsigned char *p = *pBuf;
113 size_t remain = *pRemain;
116 oidBuf.length = load_uint32_be(p);
117 if (remain < 4 + oidBuf.length || oidBuf.length == 0) {
118 *minor = GSSEAP_TOK_TRUNC;
119 return GSS_S_DEFECTIVE_TOKEN;
122 oidBuf.elements = &p[4];
124 if (!gssEapIsConcreteMechanismOid(&oidBuf)) {
125 *minor = GSSEAP_WRONG_MECH;
126 return GSS_S_BAD_MECH;
129 if (!gssEapInternalizeOid(&oidBuf, pOid)) {
130 major = duplicateOid(minor, &oidBuf, pOid);
131 if (GSS_ERROR(major))
135 *pBuf += 4 + oidBuf.length;
136 *pRemain -= 4 + oidBuf.length;
139 return GSS_S_COMPLETE;
143 importKerberosKey(OM_uint32 *minor,
144 unsigned char **pBuf,
146 krb5_cksumtype *checksumType,
147 krb5_enctype *pEncryptionType,
150 unsigned char *p = *pBuf;
151 size_t remain = *pRemain;
152 OM_uint32 encryptionType;
157 *minor = GSSEAP_TOK_TRUNC;
158 return GSS_S_DEFECTIVE_TOKEN;
161 *checksumType = load_uint32_be(&p[0]);
162 encryptionType = load_uint32_be(&p[4]);
163 length = load_uint32_be(&p[8]);
165 if ((length != 0) != (encryptionType != ENCTYPE_NULL)) {
166 *minor = GSSEAP_BAD_CONTEXT_TOKEN;
167 return GSS_S_DEFECTIVE_TOKEN;
170 if (remain - 12 < length) {
171 *minor = GSSEAP_TOK_TRUNC;
172 return GSS_S_DEFECTIVE_TOKEN;
175 if (load_buffer(&p[12], length, &tmp) == NULL) {
177 return GSS_S_FAILURE;
180 KRB_KEY_TYPE(key) = encryptionType;
181 KRB_KEY_LENGTH(key) = tmp.length;
182 KRB_KEY_DATA(key) = (unsigned char *)tmp.value;
184 *pBuf += 12 + length;
185 *pRemain -= 12 + length;
186 *pEncryptionType = encryptionType;
189 return GSS_S_COMPLETE;
193 importName(OM_uint32 *minor,
194 unsigned char **pBuf,
199 unsigned char *p = *pBuf;
200 size_t remain = *pRemain;
204 *minor = GSSEAP_TOK_TRUNC;
205 return GSS_S_DEFECTIVE_TOKEN;
208 tmp.length = load_uint32_be(p);
209 if (tmp.length != 0) {
210 if (remain - 4 < tmp.length) {
211 *minor = GSSEAP_TOK_TRUNC;
212 return GSS_S_DEFECTIVE_TOKEN;
217 major = gssEapImportNameInternal(minor, &tmp, pName,
218 EXPORT_NAME_FLAG_COMPOSITE);
219 if (GSS_ERROR(major))
223 *pBuf += 4 + tmp.length;
224 *pRemain -= 4 + tmp.length;
227 return GSS_S_COMPLETE;
231 importConversation(OM_uint32 *minor,
232 unsigned char **pBuf,
237 unsigned char *p = *pBuf;
238 size_t remain = *pRemain;
242 *minor = GSSEAP_TOK_TRUNC;
243 return GSS_S_DEFECTIVE_TOKEN;
246 tmp.length = load_uint32_be(p);
247 if (tmp.length == 0 ||
248 remain - 4 < tmp.length) {
249 *minor = GSSEAP_TOK_TRUNC;
250 return GSS_S_DEFECTIVE_TOKEN;
254 *minor = GSSEAP_BAD_TOK_HEADER;
255 return GSS_S_DEFECTIVE_TOKEN;
260 major = duplicateBuffer(minor, &tmp, &ctx->conversation);
261 if (GSS_ERROR(major))
264 *pBuf += 4 + tmp.length;
265 *pRemain -= 4 + tmp.length;
268 return GSS_S_COMPLETE;
272 gssEapImportContext(OM_uint32 *minor,
277 unsigned char *p = (unsigned char *)token->value;
278 size_t remain = token->length;
281 *minor = GSSEAP_TOK_TRUNC;
282 return GSS_S_DEFECTIVE_TOKEN;
284 if (load_uint32_be(&p[0]) != EAP_EXPORT_CONTEXT_V1) {
285 *minor = GSSEAP_BAD_CONTEXT_TOKEN;
286 return GSS_S_DEFECTIVE_TOKEN;
288 ctx->state = load_uint32_be(&p[4]);
289 ctx->flags = load_uint32_be(&p[8]);
290 ctx->gssFlags = load_uint32_be(&p[12]);
295 if (GSSEAP_SM_STATE(ctx) < GSSEAP_STATE_INITIAL ||
296 GSSEAP_SM_STATE(ctx) > GSSEAP_STATE_ESTABLISHED)
297 return GSS_S_DEFECTIVE_TOKEN;
299 /* Only acceptor can export partial context tokens */
300 if (CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx))
301 return GSS_S_DEFECTIVE_TOKEN;
303 major = importMechanismOid(minor, &p, &remain, &ctx->mechanismUsed);
304 if (GSS_ERROR(major))
307 major = importKerberosKey(minor, &p, &remain,
309 &ctx->encryptionType,
311 if (GSS_ERROR(major))
314 major = importName(minor, &p, &remain, &ctx->initiatorName);
315 if (GSS_ERROR(major))
318 major = importName(minor, &p, &remain, &ctx->acceptorName);
319 if (GSS_ERROR(major))
322 /* Check that, if context is established, names are valid */
323 if (CTX_IS_ESTABLISHED(ctx) &&
324 (CTX_IS_INITIATOR(ctx) ? ctx->acceptorName == GSS_C_NO_NAME
325 : ctx->initiatorName == GSS_C_NO_NAME)) {
326 return GSS_S_DEFECTIVE_TOKEN;
329 if (remain < 24 + sequenceSize(ctx->seqState)) {
330 *minor = GSSEAP_TOK_TRUNC;
331 return GSS_S_DEFECTIVE_TOKEN;
333 ctx->expiryTime = (time_t)load_uint64_be(&p[0]);
334 ctx->sendSeq = load_uint64_be(&p[8]);
335 ctx->recvSeq = load_uint64_be(&p[16]);
339 major = sequenceInternalize(minor, &ctx->seqState, &p, &remain);
340 if (GSS_ERROR(major))
344 * The partial context should only be expected for unestablished
347 if (!CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx)) {
348 major = importConversation(minor, &p, &remain, ctx);
349 if (GSS_ERROR(major))
352 if ((ctx->flags & CTX_FLAG_KRB_REAUTH) == 0) {
353 major = importPartialRadiusContext(minor, &p, &remain, ctx);
354 if (GSS_ERROR(major))
363 major = GSS_S_COMPLETE;
370 gss_import_sec_context(OM_uint32 *minor,
371 gss_buffer_t interprocess_token,
372 gss_ctx_id_t *context_handle)
374 OM_uint32 major, tmpMinor;
375 gss_ctx_id_t ctx = GSS_C_NO_CONTEXT;
377 *context_handle = GSS_C_NO_CONTEXT;
379 if (interprocess_token == GSS_C_NO_BUFFER ||
380 interprocess_token->length == 0) {
381 *minor = GSSEAP_TOK_TRUNC;
382 return GSS_S_DEFECTIVE_TOKEN;
385 major = gssEapAllocContext(minor, &ctx);
386 if (GSS_ERROR(major))
389 major = gssEapImportContext(minor, interprocess_token, ctx);
390 if (GSS_ERROR(major))
393 *context_handle = ctx;
396 if (GSS_ERROR(major))
397 gssEapReleaseContext(&tmpMinor, &ctx);