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 { \
42 *minor = GSSEAP_TOK_TRUNC; \
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) {
114 *minor = GSSEAP_TOK_TRUNC;
115 return GSS_S_DEFECTIVE_TOKEN;
118 oidBuf.elements = &p[4];
120 if (!gssEapIsConcreteMechanismOid(&oidBuf)) {
121 *minor = GSSEAP_WRONG_MECH;
122 return GSS_S_BAD_MECH;
125 if (!gssEapInternalizeOid(&oidBuf, pOid)) {
126 major = duplicateOid(minor, &oidBuf, pOid);
127 if (GSS_ERROR(major))
131 *pBuf += 4 + oidBuf.length;
132 *pRemain -= 4 + oidBuf.length;
135 return GSS_S_COMPLETE;
139 importKerberosKey(OM_uint32 *minor,
140 unsigned char **pBuf,
142 krb5_cksumtype *checksumType,
143 krb5_enctype *pEncryptionType,
146 unsigned char *p = *pBuf;
147 size_t remain = *pRemain;
148 OM_uint32 encryptionType;
153 *minor = GSSEAP_TOK_TRUNC;
154 return GSS_S_DEFECTIVE_TOKEN;
157 *checksumType = load_uint32_be(&p[0]);
158 encryptionType = load_uint32_be(&p[4]);
159 length = load_uint32_be(&p[8]);
161 if ((length != 0) != (encryptionType != ENCTYPE_NULL)) {
162 *minor = GSSEAP_BAD_CONTEXT_TOKEN;
163 return GSS_S_DEFECTIVE_TOKEN;
166 if (remain - 12 < length) {
167 *minor = GSSEAP_TOK_TRUNC;
168 return GSS_S_DEFECTIVE_TOKEN;
171 if (load_buffer(&p[12], length, &tmp) == NULL) {
173 return GSS_S_FAILURE;
176 KRB_KEY_TYPE(key) = encryptionType;
177 KRB_KEY_LENGTH(key) = tmp.length;
178 KRB_KEY_DATA(key) = (unsigned char *)tmp.value;
180 *pBuf += 12 + length;
181 *pRemain -= 12 + length;
182 *pEncryptionType = encryptionType;
185 return GSS_S_COMPLETE;
189 importName(OM_uint32 *minor,
190 unsigned char **pBuf,
195 unsigned char *p = *pBuf;
196 size_t remain = *pRemain;
200 *minor = GSSEAP_TOK_TRUNC;
201 return GSS_S_DEFECTIVE_TOKEN;
204 tmp.length = load_uint32_be(p);
205 if (tmp.length != 0) {
206 if (remain - 4 < tmp.length) {
207 *minor = GSSEAP_TOK_TRUNC;
208 return GSS_S_DEFECTIVE_TOKEN;
213 major = gssEapImportNameInternal(minor, &tmp, pName,
214 EXPORT_NAME_FLAG_COMPOSITE);
215 if (GSS_ERROR(major))
219 *pBuf += 4 + tmp.length;
220 *pRemain -= 4 + tmp.length;
223 return GSS_S_COMPLETE;
227 gssEapImportContext(OM_uint32 *minor,
232 unsigned char *p = (unsigned char *)token->value;
233 size_t remain = token->length;
236 *minor = GSSEAP_TOK_TRUNC;
237 return GSS_S_DEFECTIVE_TOKEN;
239 if (load_uint32_be(&p[0]) != EAP_EXPORT_CONTEXT_V1) {
240 *minor = GSSEAP_BAD_CONTEXT_TOKEN;
241 return GSS_S_DEFECTIVE_TOKEN;
243 ctx->state = load_uint32_be(&p[4]);
244 ctx->flags = load_uint32_be(&p[8]);
245 ctx->gssFlags = load_uint32_be(&p[12]);
250 if (ctx->state < EAP_STATE_IDENTITY ||
251 ctx->state > EAP_STATE_ESTABLISHED)
252 return GSS_S_DEFECTIVE_TOKEN;
254 /* Only acceptor can export partial context tokens */
255 if (CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx))
256 return GSS_S_DEFECTIVE_TOKEN;
258 major = importMechanismOid(minor, &p, &remain, &ctx->mechanismUsed);
259 if (GSS_ERROR(major))
262 major = importKerberosKey(minor, &p, &remain,
264 &ctx->encryptionType,
266 if (GSS_ERROR(major))
269 major = importName(minor, &p, &remain, &ctx->initiatorName);
270 if (GSS_ERROR(major))
273 major = importName(minor, &p, &remain, &ctx->acceptorName);
274 if (GSS_ERROR(major))
277 /* Check that, if context is established, names are valid */
278 if (CTX_IS_ESTABLISHED(ctx) &&
279 (CTX_IS_INITIATOR(ctx) ? ctx->acceptorName == GSS_C_NO_NAME
280 : ctx->initiatorName == GSS_C_NO_NAME)) {
281 return GSS_S_DEFECTIVE_TOKEN;
284 if (remain < 24 + sequenceSize(ctx->seqState)) {
285 *minor = GSSEAP_TOK_TRUNC;
286 return GSS_S_DEFECTIVE_TOKEN;
288 ctx->expiryTime = (time_t)load_uint64_be(&p[0]); /* XXX */
289 ctx->sendSeq = load_uint64_be(&p[8]);
290 ctx->recvSeq = load_uint64_be(&p[16]);
294 major = sequenceInternalize(minor, &ctx->seqState, &p, &remain);
295 if (GSS_ERROR(major))
299 * The partial context should only be expected for unestablished
302 if (!CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx)) {
303 assert((ctx->flags & CTX_FLAG_KRB_REAUTH_GSS) == 0);
305 major = gssEapImportPartialContext(minor, &p, &remain, ctx);
306 if (GSS_ERROR(major))
315 major = GSS_S_COMPLETE;
321 gss_import_sec_context(OM_uint32 *minor,
322 gss_buffer_t interprocess_token,
323 gss_ctx_id_t *context_handle)
325 OM_uint32 major, tmpMinor;
326 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 *minor = GSSEAP_TOK_TRUNC;
333 return GSS_S_DEFECTIVE_TOKEN;
336 major = gssEapAllocContext(minor, &ctx);
337 if (GSS_ERROR(major))
340 major = gssEapImportContext(minor, interprocess_token, ctx);
341 if (GSS_ERROR(major))
344 *context_handle = ctx;
347 if (GSS_ERROR(major))
348 gssEapReleaseContext(&tmpMinor, &ctx);