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