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 = gssEapImportNameInternal(minor, &tmp, pName,
173 EXPORT_NAME_FLAG_COMPOSITE);
174 if (GSS_ERROR(major))
178 *pBuf += 4 + tmp.length;
179 *pRemain -= 4 + tmp.length;
182 return GSS_S_COMPLETE;
186 gssEapImportContext(OM_uint32 *minor,
191 unsigned char *p = (unsigned char *)token->value;
192 size_t remain = token->length;
196 return GSS_S_DEFECTIVE_TOKEN;
198 if (load_uint32_be(&p[0]) != EAP_EXPORT_CONTEXT_V1) {
200 return GSS_S_DEFECTIVE_TOKEN;
202 ctx->state = load_uint32_be(&p[4]);
203 ctx->flags = load_uint32_be(&p[8]);
204 ctx->gssFlags = load_uint32_be(&p[12]);
209 if (ctx->state < EAP_STATE_AUTHENTICATE ||
210 ctx->state > EAP_STATE_ESTABLISHED)
211 return GSS_S_DEFECTIVE_TOKEN;
213 /* Only acceptor can export partial context tokens */
214 if (CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx))
215 return GSS_S_DEFECTIVE_TOKEN;
217 major = importMechanismOid(minor, &p, &remain, &ctx->mechanismUsed);
218 if (GSS_ERROR(major))
221 major = importKerberosKey(minor, &p, &remain,
223 &ctx->encryptionType,
225 if (GSS_ERROR(major))
228 major = importName(minor, &p, &remain, &ctx->initiatorName);
229 if (GSS_ERROR(major))
232 major = importName(minor, &p, &remain, &ctx->acceptorName);
233 if (GSS_ERROR(major))
236 /* Check that, if context is established, names are valid */
237 if (CTX_IS_ESTABLISHED(ctx) &&
238 (CTX_IS_INITIATOR(ctx) ? ctx->acceptorName == GSS_C_NO_NAME
239 : ctx->initiatorName == GSS_C_NO_NAME)) {
240 return GSS_S_DEFECTIVE_TOKEN;
243 if (remain < 24 + sequenceSize(ctx->seqState)) {
245 return GSS_S_DEFECTIVE_TOKEN;
247 ctx->expiryTime = (time_t)load_uint64_be(&p[0]); /* XXX */
248 ctx->sendSeq = load_uint64_be(&p[8]);
249 ctx->recvSeq = load_uint64_be(&p[16]);
253 major = sequenceInternalize(minor, &ctx->seqState, &p, &remain);
254 if (GSS_ERROR(major))
258 * The partial context should only be expected for unestablished
261 if (!CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx)) {
262 major = gssEapImportPartialContext(minor, &p, &remain, ctx);
263 if (GSS_ERROR(major))
272 major = GSS_S_COMPLETE;
278 gss_import_sec_context(OM_uint32 *minor,
279 gss_buffer_t interprocess_token,
280 gss_ctx_id_t *context_handle)
282 OM_uint32 major, tmpMinor;
283 gss_ctx_id_t ctx = GSS_C_NO_CONTEXT;
285 *context_handle = GSS_C_NO_CONTEXT;
287 if (interprocess_token == GSS_C_NO_BUFFER ||
288 interprocess_token->length == 0)
289 return GSS_S_DEFECTIVE_TOKEN;
291 major = gssEapAllocContext(minor, &ctx);
292 if (GSS_ERROR(major))
295 major = gssEapImportContext(minor, interprocess_token, ctx);
296 if (GSS_ERROR(major))
299 *context_handle = ctx;
302 if (GSS_ERROR(major))
303 gssEapReleaseContext(&tmpMinor, &ctx);