Merge branch 'master' into tlv-mic
[moonshot.git] / mech_eap / export_sec_context.c
1 /*
2  * Copyright (c) 2011, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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
30  * SUCH DAMAGE.
31  */
32
33 /*
34  * Serialise a security context. On the acceptor, this may be partially
35  * established.
36  */
37
38 #include "gssapiP_eap.h"
39
40 static OM_uint32
41 exportPartialRadiusContext(OM_uint32 *minor,
42                            gss_ctx_id_t ctx,
43                            gss_buffer_t token)
44 {
45     OM_uint32 major, tmpMinor;
46     size_t length, serverLen = 0;
47     unsigned char *p;
48     char serverBuf[MAXHOSTNAMELEN];
49
50     if (ctx->acceptorCtx.radConn != NULL) {
51         if (rs_conn_get_current_peer(ctx->acceptorCtx.radConn,
52                                      serverBuf, sizeof(serverBuf)) != 0) {
53             return gssEapRadiusMapError(minor,
54                                         rs_err_conn_pop(ctx->acceptorCtx.radConn));
55         }
56         serverLen = strlen(serverBuf);
57     }
58
59     length = 4 + serverLen + 4 + ctx->acceptorCtx.state.length;
60
61     token->value = GSSEAP_MALLOC(length);
62     if (token->value == NULL) {
63         major = GSS_S_FAILURE;
64         *minor = ENOMEM;
65         goto cleanup;
66     }
67     token->length = length;
68
69     p = (unsigned char *)token->value;
70
71     store_uint32_be(serverLen, p);
72     p += 4;
73     if (serverLen != 0) {
74         memcpy(p, serverBuf, serverLen);
75         p += serverLen;
76     }
77
78     store_uint32_be(ctx->acceptorCtx.state.length, p);
79     p += 4;
80     if (ctx->acceptorCtx.state.length != 0) {
81         memcpy(p, ctx->acceptorCtx.state.value,
82                ctx->acceptorCtx.state.length);
83         p += ctx->acceptorCtx.state.length;
84     }
85
86     assert(p == (unsigned char *)token->value + token->length);
87
88     major = GSS_S_COMPLETE;
89     *minor = 0;
90
91 cleanup:
92     if (GSS_ERROR(major))
93         gss_release_buffer(&tmpMinor, token);
94
95     return major;
96 }
97
98 static OM_uint32
99 gssEapExportSecContext(OM_uint32 *minor,
100                        gss_ctx_id_t ctx,
101                        gss_buffer_t token)
102 {
103     OM_uint32 major, tmpMinor;
104     size_t length;
105     gss_buffer_desc initiatorName = GSS_C_EMPTY_BUFFER;
106     gss_buffer_desc acceptorName = GSS_C_EMPTY_BUFFER;
107     gss_buffer_desc partialCtx = GSS_C_EMPTY_BUFFER;
108     gss_buffer_desc key;
109     unsigned char *p;
110
111     if ((CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx)) ||
112         ctx->mechanismUsed == GSS_C_NO_OID) {
113         *minor = GSSEAP_CONTEXT_INCOMPLETE;
114         return GSS_S_NO_CONTEXT;
115     }
116
117     key.length = KRB_KEY_LENGTH(&ctx->rfc3961Key);
118     key.value  = KRB_KEY_DATA(&ctx->rfc3961Key);
119
120     if (ctx->initiatorName != GSS_C_NO_NAME) {
121         major = gssEapExportNameInternal(minor, ctx->initiatorName,
122                                          &initiatorName,
123                                          EXPORT_NAME_FLAG_COMPOSITE);
124         if (GSS_ERROR(major))
125             goto cleanup;
126     }
127     if (ctx->acceptorName != GSS_C_NO_NAME) {
128         major = gssEapExportNameInternal(minor, ctx->acceptorName,
129                                          &acceptorName,
130                                          EXPORT_NAME_FLAG_COMPOSITE);
131         if (GSS_ERROR(major))
132             goto cleanup;
133     }
134
135     /*
136      * The partial context is only transmitted for unestablished acceptor
137      * contexts.
138      */
139     if (!CTX_IS_INITIATOR(ctx) &&
140         !CTX_IS_ESTABLISHED(ctx) &&
141         ((ctx->flags & CTX_FLAG_KRB_REAUTH) == 0)) {
142         major = exportPartialRadiusContext(minor, ctx, &partialCtx);
143         if (GSS_ERROR(major))
144             goto cleanup;
145     }
146
147     length  = 16;                               /* version, state, flags, */
148     length += 4 + ctx->mechanismUsed->length;   /* mechanismUsed */
149     length += 12 + key.length;                  /* rfc3961Key.value */
150     length += 4 + initiatorName.length;         /* initiatorName.value */
151     length += 4 + acceptorName.length;          /* acceptorName.value */
152     length += 24 + sequenceSize(ctx->seqState); /* seqState */
153
154     if (!CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx))
155         length += 4 + ctx->conversation.length;
156
157     if (partialCtx.value != NULL)
158         length += 4 + partialCtx.length;        /* partialCtx.value */
159
160     token->value = GSSEAP_MALLOC(length);
161     if (token->value == NULL) {
162         major = GSS_S_FAILURE;
163         *minor = ENOMEM;
164         goto cleanup;
165     }
166     token->length = length;
167
168     p = (unsigned char *)token->value;
169
170     store_uint32_be(EAP_EXPORT_CONTEXT_V1, &p[0]);        /* version */
171     store_uint32_be(GSSEAP_SM_STATE(ctx),  &p[4]);
172     store_uint32_be(ctx->flags,            &p[8]);
173     store_uint32_be(ctx->gssFlags,         &p[12]);
174     p = store_oid(ctx->mechanismUsed,      &p[16]);
175
176     store_uint32_be(ctx->checksumType,     &p[0]);
177     store_uint32_be(ctx->encryptionType,   &p[4]);
178     p = store_buffer(&key,                 &p[8], FALSE);
179
180     p = store_buffer(&initiatorName,       p, FALSE);
181     p = store_buffer(&acceptorName,        p, FALSE);
182
183     store_uint64_be(ctx->expiryTime,       &p[0]);
184     store_uint64_be(ctx->sendSeq,          &p[8]);
185     store_uint64_be(ctx->recvSeq,          &p[16]);
186     p += 24;
187
188     major = sequenceExternalize(minor, ctx->seqState, &p, &length);
189     if (GSS_ERROR(major))
190         goto cleanup;
191
192     if (!CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx))
193         p = store_buffer(&ctx->conversation, &p, FALSE);
194
195     if (partialCtx.value != NULL)
196         p = store_buffer(&partialCtx, p, FALSE);
197
198     assert(p == (unsigned char *)token->value + token->length);
199
200     major = GSS_S_COMPLETE;
201     *minor = 0;
202
203 cleanup:
204     if (GSS_ERROR(major))
205         gss_release_buffer(&tmpMinor, token);
206     gss_release_buffer(&tmpMinor, &initiatorName);
207     gss_release_buffer(&tmpMinor, &acceptorName);
208     gss_release_buffer(&tmpMinor, &partialCtx);
209
210     return major;
211 }
212
213 OM_uint32
214 gss_export_sec_context(OM_uint32 *minor,
215                        gss_ctx_id_t *context_handle,
216                        gss_buffer_t interprocess_token)
217 {
218     OM_uint32 major, tmpMinor;
219     gss_ctx_id_t ctx = *context_handle;
220
221     interprocess_token->length = 0;
222     interprocess_token->value = NULL;
223
224     if (ctx == GSS_C_NO_CONTEXT) {
225         *minor = EINVAL;
226         return GSS_S_CALL_INACCESSIBLE_READ | GSS_S_NO_CONTEXT;
227     }
228
229     *minor = 0;
230
231     GSSEAP_MUTEX_LOCK(&ctx->mutex);
232
233     major = gssEapExportSecContext(minor, ctx, interprocess_token);
234     if (GSS_ERROR(major)) {
235         GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
236         return major;
237     }
238
239     *context_handle = GSS_C_NO_CONTEXT;
240
241     GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
242
243     gssEapReleaseContext(&tmpMinor, &ctx);
244
245     return GSS_S_COMPLETE;
246 }