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