2 * Copyright (c) 2010, 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
33 #include "gssapiP_eap.h"
36 gssEapImportPartialContext(OM_uint32 *minor,
42 unsigned char *p = *pBuf;
43 size_t remain = *pRemain;
46 /* XXX we also need to deserialise the current server name */
50 return GSS_S_DEFECTIVE_TOKEN;
52 buf.length = load_uint32_be(p);
54 if (remain < buf.length) {
56 return GSS_S_DEFECTIVE_TOKEN;
61 major = duplicateBuffer(minor, &buf, &ctx->acceptorCtx.state);
65 *pBuf += 4 + buf.length;
66 *pRemain -= 4 + buf.length;
68 return GSS_S_COMPLETE;
72 importMechanismOid(OM_uint32 *minor,
78 unsigned char *p = *pBuf;
79 size_t remain = *pRemain;
82 oidBuf.length = load_uint32_be(p);
83 if (remain < 4 + oidBuf.length || oidBuf.length == 0) {
85 return GSS_S_DEFECTIVE_TOKEN;
88 oidBuf.elements = &p[4];
90 if (!gssEapIsConcreteMechanismOid(&oidBuf)) {
91 return GSS_S_BAD_MECH;
94 if (!gssEapInternalizeOid(&oidBuf, pOid)) {
95 major = duplicateOid(minor, &oidBuf, pOid);
100 *pBuf += 4 + oidBuf.length;
101 *pRemain -= 4 + oidBuf.length;
104 return GSS_S_COMPLETE;
108 importKerberosKey(OM_uint32 *minor,
109 unsigned char **pBuf,
111 krb5_cksumtype *checksumType,
112 krb5_enctype *pEncryptionType,
115 unsigned char *p = *pBuf;
116 size_t remain = *pRemain;
117 OM_uint32 encryptionType;
123 return GSS_S_DEFECTIVE_TOKEN;
126 *checksumType = load_uint32_be(&p[0]);
127 encryptionType = load_uint32_be(&p[4]);
128 length = load_uint32_be(&p[8]);
130 if ((length != 0) != (encryptionType != ENCTYPE_NULL)) {
132 return GSS_S_DEFECTIVE_TOKEN;
135 if (remain - 12 < length) {
137 return GSS_S_DEFECTIVE_TOKEN;
140 if (load_buffer(&p[12], length, &tmp) == NULL) {
142 return GSS_S_FAILURE;
145 KRB_KEY_TYPE(key) = encryptionType;
146 KRB_KEY_LENGTH(key) = tmp.length;
147 KRB_KEY_DATA(key) = (unsigned char *)tmp.value;
149 *pBuf += 12 + length;
150 *pRemain -= 12 + length;
151 *pEncryptionType = encryptionType;
154 return GSS_S_COMPLETE;
158 importName(OM_uint32 *minor,
159 unsigned char **pBuf,
164 unsigned char *p = *pBuf;
165 size_t remain = *pRemain;
170 return GSS_S_DEFECTIVE_TOKEN;
173 tmp.length = load_uint32_be(p);
174 if (tmp.length != 0) {
175 if (remain - 4 < tmp.length) {
177 return GSS_S_DEFECTIVE_TOKEN;
182 major = gssEapImportNameInternal(minor, &tmp, pName,
183 EXPORT_NAME_FLAG_COMPOSITE);
184 if (GSS_ERROR(major))
188 *pBuf += 4 + tmp.length;
189 *pRemain -= 4 + tmp.length;
192 return GSS_S_COMPLETE;
196 gssEapImportContext(OM_uint32 *minor,
201 unsigned char *p = (unsigned char *)token->value;
202 size_t remain = token->length;
206 return GSS_S_DEFECTIVE_TOKEN;
208 if (load_uint32_be(&p[0]) != EAP_EXPORT_CONTEXT_V1) {
210 return GSS_S_DEFECTIVE_TOKEN;
212 ctx->state = load_uint32_be(&p[4]);
213 ctx->flags = load_uint32_be(&p[8]);
214 ctx->gssFlags = load_uint32_be(&p[12]);
219 if (ctx->state < EAP_STATE_IDENTITY ||
220 ctx->state > EAP_STATE_ESTABLISHED)
221 return GSS_S_DEFECTIVE_TOKEN;
223 /* Only acceptor can export partial context tokens */
224 if (CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx))
225 return GSS_S_DEFECTIVE_TOKEN;
227 major = importMechanismOid(minor, &p, &remain, &ctx->mechanismUsed);
228 if (GSS_ERROR(major))
231 major = importKerberosKey(minor, &p, &remain,
233 &ctx->encryptionType,
235 if (GSS_ERROR(major))
238 major = importName(minor, &p, &remain, &ctx->initiatorName);
239 if (GSS_ERROR(major))
242 major = importName(minor, &p, &remain, &ctx->acceptorName);
243 if (GSS_ERROR(major))
246 /* Check that, if context is established, names are valid */
247 if (CTX_IS_ESTABLISHED(ctx) &&
248 (CTX_IS_INITIATOR(ctx) ? ctx->acceptorName == GSS_C_NO_NAME
249 : ctx->initiatorName == GSS_C_NO_NAME)) {
250 return GSS_S_DEFECTIVE_TOKEN;
253 if (remain < 24 + sequenceSize(ctx->seqState)) {
255 return GSS_S_DEFECTIVE_TOKEN;
257 ctx->expiryTime = (time_t)load_uint64_be(&p[0]); /* XXX */
258 ctx->sendSeq = load_uint64_be(&p[8]);
259 ctx->recvSeq = load_uint64_be(&p[16]);
263 major = sequenceInternalize(minor, &ctx->seqState, &p, &remain);
264 if (GSS_ERROR(major))
268 * The partial context should only be expected for unestablished
271 if (!CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx)) {
272 assert((ctx->flags & CTX_FLAG_KRB_REAUTH_GSS) == 0);
274 major = gssEapImportPartialContext(minor, &p, &remain, ctx);
275 if (GSS_ERROR(major))
284 major = GSS_S_COMPLETE;
290 gss_import_sec_context(OM_uint32 *minor,
291 gss_buffer_t interprocess_token,
292 gss_ctx_id_t *context_handle)
294 OM_uint32 major, tmpMinor;
295 gss_ctx_id_t ctx = GSS_C_NO_CONTEXT;
297 *context_handle = GSS_C_NO_CONTEXT;
299 if (interprocess_token == GSS_C_NO_BUFFER ||
300 interprocess_token->length == 0)
301 return GSS_S_DEFECTIVE_TOKEN;
303 major = gssEapAllocContext(minor, &ctx);
304 if (GSS_ERROR(major))
307 major = gssEapImportContext(minor, interprocess_token, ctx);
308 if (GSS_ERROR(major))
311 *context_handle = ctx;
314 if (GSS_ERROR(major))
315 gssEapReleaseContext(&tmpMinor, &ctx);