Conditionalized Acceptor codepaths and modules.
[moonshot.git] / moonshot / 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 #ifdef GSSEAP_ENABLE_ACCEPTOR
40 static OM_uint32
41 gssEapExportPartialContext(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     if (ctx->acceptorCtx.radConn != NULL) {
50         if (rs_conn_get_current_peer(ctx->acceptorCtx.radConn,
51                                      serverBuf, sizeof(serverBuf)) != 0) {
52 #if 0
53             return gssEapRadiusMapError(minor,
54                                         rs_err_conn_pop(ctx->acceptorCtx.radConn));
55 #else
56             serverBuf[0] = '\0'; /* not implemented yet */
57 #endif
58         }
59         serverLen = strlen(serverBuf);
60     }
61     length = 4 + serverLen + 4 + ctx->acceptorCtx.state.length;
62
63     token->value = GSSEAP_MALLOC(length);
64     if (token->value == NULL) {
65         major = GSS_S_FAILURE;
66         *minor = ENOMEM;
67         goto cleanup;
68     }
69     token->length = length;
70
71     p = (unsigned char *)token->value;
72
73     store_uint32_be(serverLen, p);
74     p += 4;
75     if (serverLen != 0) {
76         memcpy(p, serverBuf, serverLen);
77         p += serverLen;
78     }
79
80     store_uint32_be(ctx->acceptorCtx.state.length, p);
81     p += 4;
82     if (ctx->acceptorCtx.state.length != 0) {
83         memcpy(p, ctx->acceptorCtx.state.value,
84                ctx->acceptorCtx.state.length);
85         p += ctx->acceptorCtx.state.length;
86     }
87
88     assert(p == (unsigned char *)token->value + token->length);
89
90     major = GSS_S_COMPLETE;
91     *minor = 0;
92
93 cleanup:
94     if (GSS_ERROR(major))
95         gss_release_buffer(&tmpMinor, token);
96
97     return major;
98 }
99 #endif /* GSSEAP_ENABLE_ACCEPTOR */
100
101 OM_uint32
102 gssEapExportSecContext(OM_uint32 *minor,
103                        gss_ctx_id_t ctx,
104                        gss_buffer_t token)
105 {
106     OM_uint32 major, tmpMinor;
107     size_t length;
108     gss_buffer_desc initiatorName = GSS_C_EMPTY_BUFFER;
109     gss_buffer_desc acceptorName = GSS_C_EMPTY_BUFFER;
110     gss_buffer_desc partialCtx = GSS_C_EMPTY_BUFFER;
111     gss_buffer_desc key;
112     unsigned char *p;
113
114     if ((CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx)) ||
115         ctx->mechanismUsed == GSS_C_NO_OID) {
116         *minor = GSSEAP_CONTEXT_INCOMPLETE;
117         return GSS_S_NO_CONTEXT;
118     }
119
120     key.length = KRB_KEY_LENGTH(&ctx->rfc3961Key);
121     key.value  = KRB_KEY_DATA(&ctx->rfc3961Key);
122
123     if (ctx->initiatorName != GSS_C_NO_NAME) {
124         major = gssEapExportNameInternal(minor, ctx->initiatorName,
125                                          &initiatorName,
126                                          EXPORT_NAME_FLAG_COMPOSITE);
127         if (GSS_ERROR(major))
128             goto cleanup;
129     }
130
131     if (ctx->acceptorName != GSS_C_NO_NAME) {
132         major = gssEapExportNameInternal(minor, ctx->acceptorName,
133                                          &acceptorName,
134                                          EXPORT_NAME_FLAG_COMPOSITE);
135         if (GSS_ERROR(major))
136             goto cleanup;
137     }
138 #ifdef GSSEAP_ENABLE_ACCEPTOR
139     /*
140      * The partial context is only transmitted for unestablished acceptor
141      * contexts.
142      */
143     if (!CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx) &&
144         (ctx->flags & CTX_FLAG_KRB_REAUTH) == 0) {
145         major = gssEapExportPartialContext(minor, ctx, &partialCtx);
146         if (GSS_ERROR(major))
147             goto cleanup;
148     }
149 #endif
150
151     length  = 16;                               /* version, state, flags, */
152     length += 4 + ctx->mechanismUsed->length;   /* mechanismUsed */
153     length += 12 + key.length;                  /* rfc3961Key.value */
154     length += 4 + initiatorName.length;         /* initiatorName.value */
155     length += 4 + acceptorName.length;          /* acceptorName.value */
156     length += 24 + sequenceSize(ctx->seqState); /* seqState */
157
158     if (partialCtx.value != NULL)
159         length += 4 + partialCtx.length;        /* partialCtx.value */
160
161     token->value = GSSEAP_MALLOC(length);
162     if (token->value == NULL) {
163         major = GSS_S_FAILURE;
164         *minor = ENOMEM;
165         goto cleanup;
166     }
167     token->length = length;
168
169     p = (unsigned char *)token->value;
170
171     store_uint32_be(EAP_EXPORT_CONTEXT_V1, &p[0]);        /* version */
172     store_uint32_be(GSSEAP_SM_STATE(ctx),  &p[4]);
173     store_uint32_be(ctx->flags,            &p[8]);
174     store_uint32_be(ctx->gssFlags,         &p[12]);
175     p = store_oid(ctx->mechanismUsed,      &p[16]);
176
177     store_uint32_be(ctx->checksumType,     &p[0]);
178     store_uint32_be(ctx->encryptionType,   &p[4]);
179     p = store_buffer(&key,                 &p[8], FALSE);
180
181     p = store_buffer(&initiatorName,       p, FALSE);
182     p = store_buffer(&acceptorName,        p, FALSE);
183
184     store_uint64_be(ctx->expiryTime,       &p[0]);
185     store_uint64_be(ctx->sendSeq,          &p[8]);
186     store_uint64_be(ctx->recvSeq,          &p[16]);
187     p += 24;
188
189     major = sequenceExternalize(minor, ctx->seqState, &p, &length);
190     if (GSS_ERROR(major))
191         goto cleanup;
192
193     if (partialCtx.value != NULL)
194         p = store_buffer(&partialCtx, p, FALSE);
195
196     assert(p == (unsigned char *)token->value + token->length);
197
198     major = GSS_S_COMPLETE;
199     *minor = 0;
200
201 cleanup:
202     if (GSS_ERROR(major))
203         gss_release_buffer(&tmpMinor, token);
204     gss_release_buffer(&tmpMinor, &initiatorName);
205     gss_release_buffer(&tmpMinor, &acceptorName);
206     gss_release_buffer(&tmpMinor, &partialCtx);
207
208     return major;
209 }
210
211 OM_uint32
212 gss_export_sec_context(OM_uint32 *minor,
213                        gss_ctx_id_t *context_handle,
214                        gss_buffer_t interprocess_token)
215 {
216     OM_uint32 major, tmpMinor;
217     gss_ctx_id_t ctx = *context_handle;
218
219     interprocess_token->length = 0;
220     interprocess_token->value = NULL;
221
222     if (ctx == GSS_C_NO_CONTEXT) {
223         *minor = EINVAL;
224         return GSS_S_CALL_INACCESSIBLE_READ | GSS_S_NO_CONTEXT;
225     }
226
227     *minor = 0;
228
229     GSSEAP_MUTEX_LOCK(&ctx->mutex);
230
231     major = gssEapExportSecContext(minor, ctx, interprocess_token);
232     if (GSS_ERROR(major)) {
233         GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
234         return major;
235     }
236
237     *context_handle = GSS_C_NO_CONTEXT;
238
239     GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
240
241     gssEapReleaseContext(&tmpMinor, &ctx);
242
243     return GSS_S_COMPLETE;
244 }