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,
41 unsigned char *p = *pBuf;
42 size_t remain = *pRemain;
47 return GSS_S_DEFECTIVE_TOKEN;
50 buf.length = load_uint32_be(p);
52 if (buf.length != 0) {
54 return GSS_S_DEFECTIVE_TOKEN;
58 return GSS_S_COMPLETE;
62 importMechanismOid(OM_uint32 *minor,
68 unsigned char *p = *pBuf;
69 size_t remain = *pRemain;
72 oidBuf.length = load_uint32_be(p);
73 if (remain < 4 + oidBuf.length || oidBuf.length == 0) {
75 return GSS_S_DEFECTIVE_TOKEN;
78 oidBuf.elements = &p[4];
80 if (!gssEapIsConcreteMechanismOid(&oidBuf)) {
81 return GSS_S_BAD_MECH;
84 if (!gssEapInternalizeOid(&oidBuf, pOid)) {
85 major = duplicateOid(minor, &oidBuf, pOid);
90 *pBuf += 4 + oidBuf.length;
91 *pRemain -= 4 + oidBuf.length;
94 return GSS_S_COMPLETE;
98 importKerberosKey(OM_uint32 *minor,
101 krb5_cksumtype *checksumType,
102 krb5_enctype *pEncryptionType,
105 unsigned char *p = *pBuf;
106 size_t remain = *pRemain;
107 OM_uint32 encryptionType;
113 return GSS_S_DEFECTIVE_TOKEN;
116 *checksumType = load_uint32_be(&p[0]);
117 encryptionType = load_uint32_be(&p[4]);
118 length = load_uint32_be(&p[8]);
120 if ((length != 0) != (encryptionType != ENCTYPE_NULL)) {
122 return GSS_S_DEFECTIVE_TOKEN;
125 if (remain - 12 < length) {
127 return GSS_S_DEFECTIVE_TOKEN;
130 if (load_buffer(&p[12], length, &tmp) == NULL) {
132 return GSS_S_FAILURE;
135 KRB_KEY_TYPE(key) = encryptionType;
136 KRB_KEY_LENGTH(key) = tmp.length;
137 KRB_KEY_DATA(key) = (unsigned char *)tmp.value;
139 *pBuf += 12 + length;
140 *pRemain -= 12 + length;
141 *pEncryptionType = encryptionType;
144 return GSS_S_COMPLETE;
148 importName(OM_uint32 *minor,
149 unsigned char **pBuf,
154 unsigned char *p = *pBuf;
155 size_t remain = *pRemain;
160 return GSS_S_DEFECTIVE_TOKEN;
163 tmp.length = load_uint32_be(p);
164 if (tmp.length != 0) {
165 if (remain - 4 < tmp.length) {
167 return GSS_S_DEFECTIVE_TOKEN;
172 major = gssEapImportName(minor, &tmp, GSS_C_NT_EXPORT_NAME, pName);
173 if (GSS_ERROR(major))
177 *pBuf += 4 + tmp.length;
178 *pRemain -= 4 + tmp.length;
181 return GSS_S_COMPLETE;
185 gssEapImportContext(OM_uint32 *minor,
190 unsigned char *p = (unsigned char *)token->value;
191 size_t remain = token->length;
195 return GSS_S_DEFECTIVE_TOKEN;
197 if (load_uint32_be(&p[0]) != EAP_EXPORT_CONTEXT_V1) {
199 return GSS_S_DEFECTIVE_TOKEN;
201 ctx->state = load_uint32_be(&p[4]);
202 ctx->flags = load_uint32_be(&p[8]);
203 ctx->gssFlags = load_uint32_be(&p[12]);
208 if (ctx->state < EAP_STATE_AUTHENTICATE ||
209 ctx->state > EAP_STATE_ESTABLISHED)
210 return GSS_S_DEFECTIVE_TOKEN;
212 /* Only acceptor can export partial context tokens */
213 if (CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx))
214 return GSS_S_DEFECTIVE_TOKEN;
216 major = importMechanismOid(minor, &p, &remain, &ctx->mechanismUsed);
217 if (GSS_ERROR(major))
220 major = importKerberosKey(minor, &p, &remain,
222 &ctx->encryptionType,
224 if (GSS_ERROR(major))
227 major = importName(minor, &p, &remain, &ctx->initiatorName);
228 if (GSS_ERROR(major))
231 major = importName(minor, &p, &remain, &ctx->acceptorName);
232 if (GSS_ERROR(major))
235 /* Check that, if context is established, names are valid */
236 if (CTX_IS_ESTABLISHED(ctx) &&
237 (CTX_IS_INITIATOR(ctx) ? ctx->acceptorName == GSS_C_NO_NAME
238 : ctx->initiatorName == GSS_C_NO_NAME)) {
239 return GSS_S_DEFECTIVE_TOKEN;
242 if (remain < 24 + sequenceSize(ctx->seqState)) {
244 return GSS_S_DEFECTIVE_TOKEN;
246 ctx->expiryTime = (time_t)load_uint64_be(&p[0]); /* XXX */
247 ctx->sendSeq = load_uint64_be(&p[8]);
248 ctx->recvSeq = load_uint64_be(&p[16]);
252 *minor = sequenceInternalize(&ctx->seqState, &p, &remain);
254 return GSS_S_FAILURE;
257 * The partial context should only be expected for unestablished
260 if (!CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx)) {
261 major = gssEapImportPartialContext(minor, &p, &remain, ctx);
262 if (GSS_ERROR(major))
269 major = GSS_S_COMPLETE;
275 gss_import_sec_context(OM_uint32 *minor,
276 gss_buffer_t interprocess_token,
277 gss_ctx_id_t *context_handle)
279 OM_uint32 major, tmpMinor;
280 gss_ctx_id_t ctx = GSS_C_NO_CONTEXT;
282 *context_handle = GSS_C_NO_CONTEXT;
284 if (interprocess_token == GSS_C_NO_BUFFER ||
285 interprocess_token->length == 0)
286 return GSS_S_DEFECTIVE_TOKEN;
288 major = gssEapAllocContext(minor, &ctx);
289 if (GSS_ERROR(major))
292 major = gssEapImportContext(minor, interprocess_token, ctx);
293 if (GSS_ERROR(major))
296 *context_handle = ctx;
299 if (GSS_ERROR(major))
300 gssEapReleaseContext(&tmpMinor, &ctx);