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;
48 return GSS_S_DEFECTIVE_TOKEN;
50 buf.length = load_uint32_be(p);
52 if (remain < buf.length) {
54 return GSS_S_DEFECTIVE_TOKEN;
59 major = duplicateBuffer(minor, &buf, &ctx->acceptorCtx.state);
63 *pBuf += 4 + buf.length;
64 *pRemain -= 4 + buf.length;
66 return GSS_S_COMPLETE;
70 importMechanismOid(OM_uint32 *minor,
76 unsigned char *p = *pBuf;
77 size_t remain = *pRemain;
80 oidBuf.length = load_uint32_be(p);
81 if (remain < 4 + oidBuf.length || oidBuf.length == 0) {
83 return GSS_S_DEFECTIVE_TOKEN;
86 oidBuf.elements = &p[4];
88 if (!gssEapIsConcreteMechanismOid(&oidBuf)) {
89 return GSS_S_BAD_MECH;
92 if (!gssEapInternalizeOid(&oidBuf, pOid)) {
93 major = duplicateOid(minor, &oidBuf, pOid);
98 *pBuf += 4 + oidBuf.length;
99 *pRemain -= 4 + oidBuf.length;
102 return GSS_S_COMPLETE;
106 importKerberosKey(OM_uint32 *minor,
107 unsigned char **pBuf,
109 krb5_cksumtype *checksumType,
110 krb5_enctype *pEncryptionType,
113 unsigned char *p = *pBuf;
114 size_t remain = *pRemain;
115 OM_uint32 encryptionType;
121 return GSS_S_DEFECTIVE_TOKEN;
124 *checksumType = load_uint32_be(&p[0]);
125 encryptionType = load_uint32_be(&p[4]);
126 length = load_uint32_be(&p[8]);
128 if ((length != 0) != (encryptionType != ENCTYPE_NULL)) {
130 return GSS_S_DEFECTIVE_TOKEN;
133 if (remain - 12 < length) {
135 return GSS_S_DEFECTIVE_TOKEN;
138 if (load_buffer(&p[12], length, &tmp) == NULL) {
140 return GSS_S_FAILURE;
143 KRB_KEY_TYPE(key) = encryptionType;
144 KRB_KEY_LENGTH(key) = tmp.length;
145 KRB_KEY_DATA(key) = (unsigned char *)tmp.value;
147 *pBuf += 12 + length;
148 *pRemain -= 12 + length;
149 *pEncryptionType = encryptionType;
152 return GSS_S_COMPLETE;
156 importName(OM_uint32 *minor,
157 unsigned char **pBuf,
162 unsigned char *p = *pBuf;
163 size_t remain = *pRemain;
168 return GSS_S_DEFECTIVE_TOKEN;
171 tmp.length = load_uint32_be(p);
172 if (tmp.length != 0) {
173 if (remain - 4 < tmp.length) {
175 return GSS_S_DEFECTIVE_TOKEN;
180 major = gssEapImportNameInternal(minor, &tmp, pName,
181 EXPORT_NAME_FLAG_COMPOSITE);
182 if (GSS_ERROR(major))
186 *pBuf += 4 + tmp.length;
187 *pRemain -= 4 + tmp.length;
190 return GSS_S_COMPLETE;
194 gssEapImportContext(OM_uint32 *minor,
199 unsigned char *p = (unsigned char *)token->value;
200 size_t remain = token->length;
204 return GSS_S_DEFECTIVE_TOKEN;
206 if (load_uint32_be(&p[0]) != EAP_EXPORT_CONTEXT_V1) {
208 return GSS_S_DEFECTIVE_TOKEN;
210 ctx->state = load_uint32_be(&p[4]);
211 ctx->flags = load_uint32_be(&p[8]);
212 ctx->gssFlags = load_uint32_be(&p[12]);
217 if (ctx->state < EAP_STATE_IDENTITY ||
218 ctx->state > EAP_STATE_ESTABLISHED)
219 return GSS_S_DEFECTIVE_TOKEN;
221 /* Only acceptor can export partial context tokens */
222 if (CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx))
223 return GSS_S_DEFECTIVE_TOKEN;
225 major = importMechanismOid(minor, &p, &remain, &ctx->mechanismUsed);
226 if (GSS_ERROR(major))
229 major = importKerberosKey(minor, &p, &remain,
231 &ctx->encryptionType,
233 if (GSS_ERROR(major))
236 major = importName(minor, &p, &remain, &ctx->initiatorName);
237 if (GSS_ERROR(major))
240 major = importName(minor, &p, &remain, &ctx->acceptorName);
241 if (GSS_ERROR(major))
244 /* Check that, if context is established, names are valid */
245 if (CTX_IS_ESTABLISHED(ctx) &&
246 (CTX_IS_INITIATOR(ctx) ? ctx->acceptorName == GSS_C_NO_NAME
247 : ctx->initiatorName == GSS_C_NO_NAME)) {
248 return GSS_S_DEFECTIVE_TOKEN;
251 if (remain < 24 + sequenceSize(ctx->seqState)) {
253 return GSS_S_DEFECTIVE_TOKEN;
255 ctx->expiryTime = (time_t)load_uint64_be(&p[0]); /* XXX */
256 ctx->sendSeq = load_uint64_be(&p[8]);
257 ctx->recvSeq = load_uint64_be(&p[16]);
261 major = sequenceInternalize(minor, &ctx->seqState, &p, &remain);
262 if (GSS_ERROR(major))
266 * The partial context should only be expected for unestablished
269 if (!CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx)) {
270 major = gssEapImportPartialContext(minor, &p, &remain, ctx);
271 if (GSS_ERROR(major))
280 major = GSS_S_COMPLETE;
286 gss_import_sec_context(OM_uint32 *minor,
287 gss_buffer_t interprocess_token,
288 gss_ctx_id_t *context_handle)
290 OM_uint32 major, tmpMinor;
291 gss_ctx_id_t ctx = GSS_C_NO_CONTEXT;
293 *context_handle = GSS_C_NO_CONTEXT;
295 if (interprocess_token == GSS_C_NO_BUFFER ||
296 interprocess_token->length == 0)
297 return GSS_S_DEFECTIVE_TOKEN;
299 major = gssEapAllocContext(minor, &ctx);
300 if (GSS_ERROR(major))
303 major = gssEapImportContext(minor, interprocess_token, ctx);
304 if (GSS_ERROR(major))
307 *context_handle = ctx;
310 if (GSS_ERROR(major))
311 gssEapReleaseContext(&tmpMinor, &ctx);