cleanup
[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     /* XXX we also need to serialise the current server name */
41     return duplicateBuffer(minor, &ctx->acceptorCtx.state, token);
42 }
43
44 static OM_uint32
45 gssEapExportSecContext(OM_uint32 *minor,
46                        gss_ctx_id_t ctx,
47                        gss_buffer_t token)
48 {
49     OM_uint32 major, tmpMinor;
50     size_t length;
51     gss_buffer_desc initiatorName = GSS_C_EMPTY_BUFFER;
52     gss_buffer_desc acceptorName = GSS_C_EMPTY_BUFFER;
53     gss_buffer_desc partialCtx = GSS_C_EMPTY_BUFFER;
54     gss_buffer_desc key;
55     unsigned char *p;
56
57     if ((CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx)) ||
58         ctx->mechanismUsed == GSS_C_NO_OID)
59         return GSS_S_NO_CONTEXT;
60
61     key.length = KRB_KEY_LENGTH(&ctx->rfc3961Key);
62     key.value  = KRB_KEY_DATA(&ctx->rfc3961Key);
63
64     if (ctx->initiatorName != GSS_C_NO_NAME) {
65         major = gssEapExportNameInternal(minor, ctx->initiatorName,
66                                          &initiatorName,
67                                          EXPORT_NAME_FLAG_COMPOSITE);
68         if (GSS_ERROR(major))
69             goto cleanup;
70     }
71     if (ctx->acceptorName != GSS_C_NO_NAME) {
72         major = gssEapExportNameInternal(minor, ctx->acceptorName,
73                                          &acceptorName,
74                                          EXPORT_NAME_FLAG_COMPOSITE);
75         if (GSS_ERROR(major))
76             goto cleanup;
77     }
78
79     /*
80      * The partial context is only transmitted for unestablished acceptor
81      * contexts.
82      */
83     if (!CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx)) {
84         assert((ctx->flags & CTX_FLAG_KRB_REAUTH_GSS) == 0);
85
86         major = gssEapExportPartialContext(minor, ctx, &partialCtx);
87         if (GSS_ERROR(major))
88             goto cleanup;
89     }
90
91     length  = 16;                               /* version, state, flags, */
92     length += 4 + ctx->mechanismUsed->length;   /* mechanismUsed */
93     length += 12 + key.length;                  /* rfc3961Key.value */
94     length += 4 + initiatorName.length;         /* initiatorName.value */
95     length += 4 + acceptorName.length;          /* acceptorName.value */
96     length += 24 + sequenceSize(ctx->seqState); /* seqState */
97
98     if (partialCtx.value != NULL)
99         length += 4 + partialCtx.length;        /* partialCtx.value */
100
101     token->value = GSSEAP_MALLOC(length);
102     if (token->value == NULL) {
103         *minor = ENOMEM;
104         major = GSS_S_FAILURE;
105         goto cleanup;
106     }
107     token->length = length;
108
109     p = (unsigned char *)token->value;
110
111     store_uint32_be(EAP_EXPORT_CONTEXT_V1, &p[0]);        /* version */
112     store_uint32_be(ctx->state,            &p[4]);
113     store_uint32_be(ctx->flags,            &p[8]);
114     store_uint32_be(ctx->gssFlags,         &p[12]);
115     p = store_oid(ctx->mechanismUsed,      &p[16]);
116
117     store_uint32_be(ctx->checksumType,     &p[0]);
118     store_uint32_be(ctx->encryptionType,   &p[4]);
119     p = store_buffer(&key,                 &p[8], FALSE);
120
121     p = store_buffer(&initiatorName,       p, FALSE);
122     p = store_buffer(&acceptorName,        p, FALSE);
123
124     store_uint64_be(ctx->expiryTime,       &p[0]);
125     store_uint64_be(ctx->sendSeq,          &p[8]);
126     store_uint64_be(ctx->recvSeq,          &p[16]);
127     p += 24;
128
129     major = sequenceExternalize(minor, ctx->seqState, &p, &length);
130     if (GSS_ERROR(major))
131         goto cleanup;
132
133     if (partialCtx.value != NULL)
134         p = store_buffer(&partialCtx, p, FALSE);
135
136     assert(p == (unsigned char *)token->value + token->length);
137
138     major = GSS_S_COMPLETE;
139     *minor = 0;
140
141 cleanup:
142     if (GSS_ERROR(major))
143         gss_release_buffer(&tmpMinor, token);
144     gss_release_buffer(&tmpMinor, &initiatorName);
145     gss_release_buffer(&tmpMinor, &acceptorName);
146     gss_release_buffer(&tmpMinor, &partialCtx);
147
148     return major;
149 }
150
151 OM_uint32
152 gss_export_sec_context(OM_uint32 *minor,
153                        gss_ctx_id_t *context_handle,
154                        gss_buffer_t interprocess_token)
155 {
156     OM_uint32 major, tmpMinor;
157     gss_ctx_id_t ctx = *context_handle;
158
159     interprocess_token->length = 0;
160     interprocess_token->value = NULL;
161
162     if (ctx == GSS_C_NO_CONTEXT)
163         return GSS_S_NO_CONTEXT;
164
165     GSSEAP_MUTEX_LOCK(&ctx->mutex);
166
167     major = gssEapExportSecContext(minor, ctx, interprocess_token);
168     if (GSS_ERROR(major)) {
169         GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
170         return major;
171     }
172
173     *context_handle = GSS_C_NO_CONTEXT;
174
175     GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
176
177     gssEapReleaseContext(&tmpMinor, &ctx);
178
179     return GSS_S_COMPLETE;
180 }