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