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"
35 #define UPDATE_REMAIN(n) do { \
40 #define CHECK_REMAIN(n) do { \
43 return GSS_S_DEFECTIVE_TOKEN; \
48 gssEapImportPartialContext(OM_uint32 *minor,
54 unsigned char *p = *pBuf;
55 size_t remain = *pRemain;
59 /* Selected RADIUS server */
61 serverLen = load_uint32_be(p);
65 CHECK_REMAIN(serverLen);
67 ctx->acceptorCtx.radServer = GSSEAP_MALLOC(serverLen + 1);
68 if (ctx->acceptorCtx.radServer == NULL) {
72 memcpy(ctx->acceptorCtx.radServer, p, serverLen);
73 ctx->acceptorCtx.radServer[serverLen] = '\0';
75 UPDATE_REMAIN(serverLen);
78 /* RADIUS state blob */
80 buf.length = load_uint32_be(p);
83 if (buf.length != 0) {
84 CHECK_REMAIN(buf.length);
88 major = duplicateBuffer(minor, &buf, &ctx->acceptorCtx.state);
92 UPDATE_REMAIN(buf.length);
98 return GSS_S_COMPLETE;
102 importMechanismOid(OM_uint32 *minor,
103 unsigned char **pBuf,
108 unsigned char *p = *pBuf;
109 size_t remain = *pRemain;
112 oidBuf.length = load_uint32_be(p);
113 if (remain < 4 + oidBuf.length || oidBuf.length == 0) {
115 return GSS_S_DEFECTIVE_TOKEN;
118 oidBuf.elements = &p[4];
120 if (!gssEapIsConcreteMechanismOid(&oidBuf)) {
121 return GSS_S_BAD_MECH;
124 if (!gssEapInternalizeOid(&oidBuf, pOid)) {
125 major = duplicateOid(minor, &oidBuf, pOid);
126 if (GSS_ERROR(major))
130 *pBuf += 4 + oidBuf.length;
131 *pRemain -= 4 + oidBuf.length;
134 return GSS_S_COMPLETE;
138 importKerberosKey(OM_uint32 *minor,
139 unsigned char **pBuf,
141 krb5_cksumtype *checksumType,
142 krb5_enctype *pEncryptionType,
145 unsigned char *p = *pBuf;
146 size_t remain = *pRemain;
147 OM_uint32 encryptionType;
153 return GSS_S_DEFECTIVE_TOKEN;
156 *checksumType = load_uint32_be(&p[0]);
157 encryptionType = load_uint32_be(&p[4]);
158 length = load_uint32_be(&p[8]);
160 if ((length != 0) != (encryptionType != ENCTYPE_NULL)) {
162 return GSS_S_DEFECTIVE_TOKEN;
165 if (remain - 12 < length) {
167 return GSS_S_DEFECTIVE_TOKEN;
170 if (load_buffer(&p[12], length, &tmp) == NULL) {
172 return GSS_S_FAILURE;
175 KRB_KEY_TYPE(key) = encryptionType;
176 KRB_KEY_LENGTH(key) = tmp.length;
177 KRB_KEY_DATA(key) = (unsigned char *)tmp.value;
179 *pBuf += 12 + length;
180 *pRemain -= 12 + length;
181 *pEncryptionType = encryptionType;
184 return GSS_S_COMPLETE;
188 importName(OM_uint32 *minor,
189 unsigned char **pBuf,
194 unsigned char *p = *pBuf;
195 size_t remain = *pRemain;
200 return GSS_S_DEFECTIVE_TOKEN;
203 tmp.length = load_uint32_be(p);
204 if (tmp.length != 0) {
205 if (remain - 4 < tmp.length) {
207 return GSS_S_DEFECTIVE_TOKEN;
212 major = gssEapImportNameInternal(minor, &tmp, pName,
213 EXPORT_NAME_FLAG_COMPOSITE);
214 if (GSS_ERROR(major))
218 *pBuf += 4 + tmp.length;
219 *pRemain -= 4 + tmp.length;
222 return GSS_S_COMPLETE;
226 gssEapImportContext(OM_uint32 *minor,
231 unsigned char *p = (unsigned char *)token->value;
232 size_t remain = token->length;
236 return GSS_S_DEFECTIVE_TOKEN;
238 if (load_uint32_be(&p[0]) != EAP_EXPORT_CONTEXT_V1) {
240 return GSS_S_DEFECTIVE_TOKEN;
242 ctx->state = load_uint32_be(&p[4]);
243 ctx->flags = load_uint32_be(&p[8]);
244 ctx->gssFlags = load_uint32_be(&p[12]);
249 if (ctx->state < EAP_STATE_IDENTITY ||
250 ctx->state > EAP_STATE_ESTABLISHED)
251 return GSS_S_DEFECTIVE_TOKEN;
253 /* Only acceptor can export partial context tokens */
254 if (CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx))
255 return GSS_S_DEFECTIVE_TOKEN;
257 major = importMechanismOid(minor, &p, &remain, &ctx->mechanismUsed);
258 if (GSS_ERROR(major))
261 major = importKerberosKey(minor, &p, &remain,
263 &ctx->encryptionType,
265 if (GSS_ERROR(major))
268 major = importName(minor, &p, &remain, &ctx->initiatorName);
269 if (GSS_ERROR(major))
272 major = importName(minor, &p, &remain, &ctx->acceptorName);
273 if (GSS_ERROR(major))
276 /* Check that, if context is established, names are valid */
277 if (CTX_IS_ESTABLISHED(ctx) &&
278 (CTX_IS_INITIATOR(ctx) ? ctx->acceptorName == GSS_C_NO_NAME
279 : ctx->initiatorName == GSS_C_NO_NAME)) {
280 return GSS_S_DEFECTIVE_TOKEN;
283 if (remain < 24 + sequenceSize(ctx->seqState)) {
285 return GSS_S_DEFECTIVE_TOKEN;
287 ctx->expiryTime = (time_t)load_uint64_be(&p[0]); /* XXX */
288 ctx->sendSeq = load_uint64_be(&p[8]);
289 ctx->recvSeq = load_uint64_be(&p[16]);
293 major = sequenceInternalize(minor, &ctx->seqState, &p, &remain);
294 if (GSS_ERROR(major))
298 * The partial context should only be expected for unestablished
301 if (!CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx)) {
302 assert((ctx->flags & CTX_FLAG_KRB_REAUTH_GSS) == 0);
304 major = gssEapImportPartialContext(minor, &p, &remain, ctx);
305 if (GSS_ERROR(major))
314 major = GSS_S_COMPLETE;
320 gss_import_sec_context(OM_uint32 *minor,
321 gss_buffer_t interprocess_token,
322 gss_ctx_id_t *context_handle)
324 OM_uint32 major, tmpMinor;
325 gss_ctx_id_t ctx = GSS_C_NO_CONTEXT;
328 *context_handle = GSS_C_NO_CONTEXT;
330 if (interprocess_token == GSS_C_NO_BUFFER ||
331 interprocess_token->length == 0)
332 return GSS_S_DEFECTIVE_TOKEN;
334 major = gssEapAllocContext(minor, &ctx);
335 if (GSS_ERROR(major))
338 major = gssEapImportContext(minor, interprocess_token, ctx);
339 if (GSS_ERROR(major))
342 *context_handle = ctx;
345 if (GSS_ERROR(major))
346 gssEapReleaseContext(&tmpMinor, &ctx);