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 gssEapExportPartialContext(OM_uint32 *minor,
40 OM_uint32 major, tmpMinor;
41 size_t length, serverLen = 0;
43 char serverBuf[MAXHOSTNAMELEN];
45 if (ctx->acceptorCtx.radConn != NULL &&
46 rs_conn_get_current_server(ctx->acceptorCtx.radConn,
47 serverBuf, sizeof(serverBuf)) == 0)
48 serverLen = strlen(serverBuf);
50 length = 4 + serverLen + 4 + ctx->acceptorCtx.state.length;
52 token->value = GSSEAP_MALLOC(length);
53 if (token->value == NULL) {
55 major = GSS_S_FAILURE;
58 token->length = length;
60 p = (unsigned char *)token->value;
62 store_uint32_be(serverLen, p);
65 memcpy(p, serverBuf, serverLen);
69 store_uint32_be(ctx->acceptorCtx.state.length, p);
71 if (ctx->acceptorCtx.state.length != 0) {
72 memcpy(p, ctx->acceptorCtx.state.value,
73 ctx->acceptorCtx.state.length);
74 p += ctx->acceptorCtx.state.length;
77 assert(p == (unsigned char *)token->value + token->length);
81 gss_release_buffer(&tmpMinor, token);
87 gssEapExportSecContext(OM_uint32 *minor,
91 OM_uint32 major, tmpMinor;
93 gss_buffer_desc initiatorName = GSS_C_EMPTY_BUFFER;
94 gss_buffer_desc acceptorName = GSS_C_EMPTY_BUFFER;
95 gss_buffer_desc partialCtx = GSS_C_EMPTY_BUFFER;
99 if ((CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx)) ||
100 ctx->mechanismUsed == GSS_C_NO_OID)
101 return GSS_S_NO_CONTEXT;
103 key.length = KRB_KEY_LENGTH(&ctx->rfc3961Key);
104 key.value = KRB_KEY_DATA(&ctx->rfc3961Key);
106 if (ctx->initiatorName != GSS_C_NO_NAME) {
107 major = gssEapExportNameInternal(minor, ctx->initiatorName,
109 EXPORT_NAME_FLAG_COMPOSITE);
110 if (GSS_ERROR(major))
113 if (ctx->acceptorName != GSS_C_NO_NAME) {
114 major = gssEapExportNameInternal(minor, ctx->acceptorName,
116 EXPORT_NAME_FLAG_COMPOSITE);
117 if (GSS_ERROR(major))
122 * The partial context is only transmitted for unestablished acceptor
125 if (!CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx)) {
126 assert((ctx->flags & CTX_FLAG_KRB_REAUTH_GSS) == 0);
128 major = gssEapExportPartialContext(minor, ctx, &partialCtx);
129 if (GSS_ERROR(major))
133 length = 16; /* version, state, flags, */
134 length += 4 + ctx->mechanismUsed->length; /* mechanismUsed */
135 length += 12 + key.length; /* rfc3961Key.value */
136 length += 4 + initiatorName.length; /* initiatorName.value */
137 length += 4 + acceptorName.length; /* acceptorName.value */
138 length += 24 + sequenceSize(ctx->seqState); /* seqState */
140 if (partialCtx.value != NULL)
141 length += 4 + partialCtx.length; /* partialCtx.value */
143 token->value = GSSEAP_MALLOC(length);
144 if (token->value == NULL) {
146 major = GSS_S_FAILURE;
149 token->length = length;
151 p = (unsigned char *)token->value;
153 store_uint32_be(EAP_EXPORT_CONTEXT_V1, &p[0]); /* version */
154 store_uint32_be(ctx->state, &p[4]);
155 store_uint32_be(ctx->flags, &p[8]);
156 store_uint32_be(ctx->gssFlags, &p[12]);
157 p = store_oid(ctx->mechanismUsed, &p[16]);
159 store_uint32_be(ctx->checksumType, &p[0]);
160 store_uint32_be(ctx->encryptionType, &p[4]);
161 p = store_buffer(&key, &p[8], FALSE);
163 p = store_buffer(&initiatorName, p, FALSE);
164 p = store_buffer(&acceptorName, p, FALSE);
166 store_uint64_be(ctx->expiryTime, &p[0]);
167 store_uint64_be(ctx->sendSeq, &p[8]);
168 store_uint64_be(ctx->recvSeq, &p[16]);
171 major = sequenceExternalize(minor, ctx->seqState, &p, &length);
172 if (GSS_ERROR(major))
175 if (partialCtx.value != NULL)
176 p = store_buffer(&partialCtx, p, FALSE);
178 assert(p == (unsigned char *)token->value + token->length);
180 major = GSS_S_COMPLETE;
184 if (GSS_ERROR(major))
185 gss_release_buffer(&tmpMinor, token);
186 gss_release_buffer(&tmpMinor, &initiatorName);
187 gss_release_buffer(&tmpMinor, &acceptorName);
188 gss_release_buffer(&tmpMinor, &partialCtx);
194 gss_export_sec_context(OM_uint32 *minor,
195 gss_ctx_id_t *context_handle,
196 gss_buffer_t interprocess_token)
198 OM_uint32 major, tmpMinor;
199 gss_ctx_id_t ctx = *context_handle;
201 interprocess_token->length = 0;
202 interprocess_token->value = NULL;
204 if (ctx == GSS_C_NO_CONTEXT) {
206 return GSS_S_NO_CONTEXT;
211 GSSEAP_MUTEX_LOCK(&ctx->mutex);
213 major = gssEapExportSecContext(minor, ctx, interprocess_token);
214 if (GSS_ERROR(major)) {
215 GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
219 *context_handle = GSS_C_NO_CONTEXT;
221 GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
223 gssEapReleaseContext(&tmpMinor, &ctx);
225 return GSS_S_COMPLETE;