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