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