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; \
51 #ifdef GSSEAP_ENABLE_ACCEPTOR
53 gssEapImportPartialContext(OM_uint32 *minor,
59 unsigned char *p = *pBuf;
60 size_t remain = *pRemain;
62 size_t ctxLength, serverLen;
64 /* Length of partial RADIUS context */
66 ctxLength = load_uint32_be(p);
69 CHECK_REMAIN(ctxLength);
70 remain = ctxLength; /* check against partial context length */
72 /* Selected RADIUS server */
74 serverLen = load_uint32_be(p);
78 CHECK_REMAIN(serverLen);
80 ctx->acceptorCtx.radServer = GSSEAP_MALLOC(serverLen + 1);
81 if (ctx->acceptorCtx.radServer == NULL) {
85 memcpy(ctx->acceptorCtx.radServer, p, serverLen);
86 ctx->acceptorCtx.radServer[serverLen] = '\0';
88 UPDATE_REMAIN(serverLen);
91 /* RADIUS state blob */
93 buf.length = load_uint32_be(p);
96 if (buf.length != 0) {
97 CHECK_REMAIN(buf.length);
101 major = duplicateBuffer(minor, &buf, &ctx->acceptorCtx.state);
102 if (GSS_ERROR(major))
105 UPDATE_REMAIN(buf.length);
109 GSSEAP_ASSERT(remain == 0);
113 *pRemain -= 4 + ctxLength;
115 return GSS_S_COMPLETE;
117 #endif /* GSSEAP_ENABLE_ACCEPTOR */
120 importMechanismOid(OM_uint32 *minor,
121 unsigned char **pBuf,
126 unsigned char *p = *pBuf;
127 size_t remain = *pRemain;
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;
136 oidBuf.elements = &p[4];
138 major = gssEapCanonicalizeOid(minor, &oidBuf, 0, pOid);
139 if (GSS_ERROR(major))
142 *pBuf += 4 + oidBuf.length;
143 *pRemain -= 4 + oidBuf.length;
146 return GSS_S_COMPLETE;
150 importKerberosKey(OM_uint32 *minor,
151 unsigned char **pBuf,
153 krb5_cksumtype *checksumType,
154 krb5_enctype *pEncryptionType,
157 unsigned char *p = *pBuf;
158 size_t remain = *pRemain;
159 OM_uint32 encryptionType;
161 krb5_context krbContext;
163 krb5_error_code code;
165 GSSEAP_KRB_INIT(&krbContext);
170 *minor = GSSEAP_TOK_TRUNC;
171 return GSS_S_DEFECTIVE_TOKEN;
174 *checksumType = load_uint32_be(&p[0]);
175 encryptionType = load_uint32_be(&p[4]);
176 length = load_uint32_be(&p[8]);
178 if ((length != 0) != (encryptionType != ENCTYPE_NULL)) {
179 *minor = GSSEAP_BAD_CONTEXT_TOKEN;
180 return GSS_S_DEFECTIVE_TOKEN;
183 if (remain - 12 < length) {
184 *minor = GSSEAP_TOK_TRUNC;
185 return GSS_S_DEFECTIVE_TOKEN;
188 if (encryptionType != ENCTYPE_NULL) {
191 KRB_KEY_TYPE(&key) = encryptionType;
192 KRB_KEY_LENGTH(&key) = length;
193 KRB_KEY_DATA(&key) = &p[12];
195 code = krb5_copy_keyblock_contents(krbContext, &key, pKey);
198 return GSS_S_FAILURE;
202 *pBuf += 12 + length;
203 *pRemain -= 12 + length;
204 *pEncryptionType = encryptionType;
207 return GSS_S_COMPLETE;
211 importName(OM_uint32 *minor,
213 unsigned char **pBuf,
217 OM_uint32 major, tmpMinor;
218 unsigned char *p = *pBuf;
219 size_t remain = *pRemain;
223 *minor = GSSEAP_TOK_TRUNC;
224 return GSS_S_DEFECTIVE_TOKEN;
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;
236 major = gssEapImportNameInternal(minor, &tmp, pName,
237 EXPORT_NAME_FLAG_COMPOSITE);
238 if (GSS_ERROR(major))
241 if (mech != GSS_C_NO_OID) {
242 major = gssEapCanonicalizeOid(minor, mech, 0, &(*pName)->mechanismUsed);
243 if (GSS_ERROR(major)) {
244 gssEapReleaseName(&tmpMinor, pName);
250 *pBuf += 4 + tmp.length;
251 *pRemain -= 4 + tmp.length;
254 return GSS_S_COMPLETE;
258 gssEapImportContext(OM_uint32 *minor,
263 unsigned char *p = (unsigned char *)token->value;
264 size_t remain = token->length;
267 *minor = GSSEAP_TOK_TRUNC;
268 return GSS_S_DEFECTIVE_TOKEN;
270 if (load_uint32_be(&p[0]) != EAP_EXPORT_CONTEXT_V1) {
271 *minor = GSSEAP_BAD_CONTEXT_TOKEN;
272 return GSS_S_DEFECTIVE_TOKEN;
274 ctx->state = load_uint32_be(&p[4]);
275 ctx->flags = load_uint32_be(&p[8]);
276 ctx->gssFlags = load_uint32_be(&p[12]);
281 if (GSSEAP_SM_STATE(ctx) < GSSEAP_STATE_INITIAL ||
282 GSSEAP_SM_STATE(ctx) > GSSEAP_STATE_ESTABLISHED)
283 return GSS_S_DEFECTIVE_TOKEN;
285 /* Only acceptor can export partial context tokens */
286 if (CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx))
287 return GSS_S_DEFECTIVE_TOKEN;
289 major = importMechanismOid(minor, &p, &remain, &ctx->mechanismUsed);
290 if (GSS_ERROR(major))
293 major = importKerberosKey(minor, &p, &remain,
295 &ctx->encryptionType,
297 if (GSS_ERROR(major))
300 /* Initiator name OID matches the context mechanism, so it's not encoded */
301 major = importName(minor, ctx->mechanismUsed, &p, &remain, &ctx->initiatorName);
302 if (GSS_ERROR(major))
305 major = importName(minor, GSS_C_NO_OID, &p, &remain, &ctx->acceptorName);
306 if (GSS_ERROR(major))
309 /* Check that, if context is established, names are valid */
310 if (CTX_IS_ESTABLISHED(ctx) &&
311 (CTX_IS_INITIATOR(ctx) ? ctx->acceptorName == GSS_C_NO_NAME
312 : ctx->initiatorName == GSS_C_NO_NAME)) {
313 return GSS_S_DEFECTIVE_TOKEN;
316 if (remain < 24 + sequenceSize(ctx->seqState)) {
317 *minor = GSSEAP_TOK_TRUNC;
318 return GSS_S_DEFECTIVE_TOKEN;
320 ctx->expiryTime = (time_t)load_uint64_be(&p[0]);
321 ctx->sendSeq = load_uint64_be(&p[8]);
322 ctx->recvSeq = load_uint64_be(&p[16]);
326 major = sequenceInternalize(minor, &ctx->seqState, &p, &remain);
327 if (GSS_ERROR(major))
330 #ifdef GSSEAP_ENABLE_ACCEPTOR
332 * The partial context should only be expected for unestablished
335 if (!CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx) &&
336 (ctx->flags & CTX_FLAG_KRB_REAUTH) == 0) {
337 major = gssEapImportPartialContext(minor, &p, &remain, ctx);
338 if (GSS_ERROR(major))
343 GSSEAP_ASSERT(remain == 0);
345 #endif /* GSSEAP_ENABLE_ACCEPTOR */
347 major = GSS_S_COMPLETE;
353 OM_uint32 GSSAPI_CALLCONV
354 gss_import_sec_context(OM_uint32 *minor,
355 gss_buffer_t interprocess_token,
356 gss_ctx_id_t *context_handle)
358 OM_uint32 major, tmpMinor;
359 gss_ctx_id_t ctx = GSS_C_NO_CONTEXT;
361 *context_handle = GSS_C_NO_CONTEXT;
363 if (interprocess_token == GSS_C_NO_BUFFER ||
364 interprocess_token->length == 0) {
365 *minor = GSSEAP_TOK_TRUNC;
366 return GSS_S_DEFECTIVE_TOKEN;
369 major = gssEapAllocContext(minor, &ctx);
370 if (GSS_ERROR(major))
373 major = gssEapImportContext(minor, interprocess_token, ctx);
374 if (GSS_ERROR(major))
377 *context_handle = ctx;
380 if (GSS_ERROR(major))
381 gssEapReleaseContext(&tmpMinor, &ctx);