5075ed8d1dbca1b0fe386606c107ea37683ee17a
[mech_eap.orig] / init_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 policyVariableToFlag(enum eapol_bool_var variable)
37 {
38     OM_uint32 flag = 0;
39
40     switch (variable) {
41     case EAPOL_eapSuccess:
42         flag = CTX_FLAG_EAP_SUCCESS;
43         break;
44     case EAPOL_eapRestart:
45         flag = CTX_FLAG_EAP_RESTART;
46         break;
47     case EAPOL_eapFail:
48         flag = CTX_FLAG_EAP_FAIL;
49         break;
50     case EAPOL_eapResp:
51         flag = CTX_FLAG_EAP_RESP;
52         break;
53     case EAPOL_eapNoResp:
54         flag = CTX_FLAG_EAP_NO_RESP;
55         break;
56     case EAPOL_eapReq:
57         flag = CTX_FLAG_EAP_REQ;
58         break;
59     case EAPOL_portEnabled:
60         flag = CTX_FLAG_EAP_PORT_ENABLED;
61         break;
62     case EAPOL_altAccept:
63         flag = CTX_FLAG_EAP_ALT_ACCEPT;
64         break;
65     case EAPOL_altReject:
66         flag = CTX_FLAG_EAP_ALT_REJECT;
67         break;
68     }
69
70     return flag;
71 }
72
73 static Boolean
74 peerGetBool(void *data, enum eapol_bool_var variable)
75 {
76     gss_ctx_id_t ctx = data;
77     OM_uint32 flag;
78
79     if (ctx == GSS_C_NO_CONTEXT)
80         return FALSE;
81
82     flag = policyVariableToFlag(variable);
83
84     return ((ctx->flags & flag) != 0);
85 }
86
87 static void
88 peerSetBool(void *data, enum eapol_bool_var variable,
89             Boolean value)
90 {
91     gss_ctx_id_t ctx = data;
92     OM_uint32 flag;
93
94     if (ctx == GSS_C_NO_CONTEXT)
95         return;
96
97     flag = policyVariableToFlag(variable);
98
99     if (value)
100         ctx->flags |= flag;
101     else
102         ctx->flags &= ~(flag);
103 }
104
105 static int
106 peerGetInt(void *data, enum eapol_int_var variable)
107 {
108     gss_ctx_id_t ctx = data;
109
110     if (ctx == GSS_C_NO_CONTEXT)
111         return FALSE;
112
113     assert(CTX_IS_INITIATOR(ctx));
114
115     switch (variable) {
116     case EAPOL_idleWhile:
117         return ctx->initiatorCtx.idleWhile;
118         break;
119     }
120
121     return 0;
122 }
123
124 static void
125 peerSetInt(void *data, enum eapol_int_var variable,
126            unsigned int value)
127 {
128     gss_ctx_id_t ctx = data;
129
130     if (ctx == GSS_C_NO_CONTEXT)
131         return;
132
133     assert(CTX_IS_INITIATOR(ctx));
134
135     switch (variable) {
136     case EAPOL_idleWhile:
137         ctx->initiatorCtx.idleWhile = value;
138         break;
139     }
140 }
141
142 static OM_uint32
143 eapGssSmAuthenticate(OM_uint32 *minor,
144                      gss_cred_id_t cred,
145                      gss_ctx_id_t ctx,
146                      gss_name_t target_name,
147                      gss_OID mech_type,
148                      OM_uint32 req_flags,
149                      OM_uint32 time_req,
150                      gss_channel_bindings_t input_chan_bindings,
151                      gss_buffer_t input_token,
152                      gss_buffer_t output_token)
153 {
154     OM_uint32 major, tmpMinor;
155     time_t now;
156
157     if (input_token == GSS_C_NO_BUFFER || input_token->length == 0) {
158         /* first time */
159         req_flags &= GSS_C_TRANS_FLAG | GSS_C_REPLAY_FLAG | GSS_C_DCE_STYLE;
160         ctx->gssFlags |= req_flags;
161
162         time(&now);
163
164         if (time_req == 0 || time_req == GSS_C_INDEFINITE)
165             ctx->expiryTime = 0;
166         else
167             ctx->expiryTime = now + time_req;
168
169         major = gss_duplicate_name(minor, cred->name, &ctx->initiatorName);
170         if (GSS_ERROR(major))
171             goto cleanup;
172
173         major = gss_duplicate_name(minor, target_name, &ctx->acceptorName);
174         if (GSS_ERROR(major))
175             goto cleanup;
176
177         if (mech_type == GSS_C_NULL_OID ||
178             oidEqual(mech_type, GSS_EAP_MECHANISM)) {
179             major = gssEapDefaultMech(minor, &ctx->mechanismUsed);
180         } else if (gssEapIsMechanismOid(mech_type)) {
181             if (!gssEapInternalizeOid(mech_type, &ctx->mechanismUsed))
182                 major = duplicateOid(minor, mech_type, &ctx->mechanismUsed);
183         } else {
184             major = GSS_S_BAD_MECH;
185         }
186         if (GSS_ERROR(major))
187             goto cleanup;
188     }
189
190 cleanup:
191     return major;
192 }
193
194 static struct eap_gss_initiator_sm {
195     enum gss_eap_token_type inputTokenType;
196     enum gss_eap_token_type outputTokenType;
197     OM_uint32 (*processToken)(OM_uint32 *,
198                               gss_cred_id_t,
199                               gss_ctx_id_t,
200                               gss_name_t,
201                               gss_OID,
202                               OM_uint32,
203                               OM_uint32,
204                               gss_channel_bindings_t,
205                               gss_buffer_t,
206                               gss_buffer_t);
207 } eapGssInitiatorSm[] = {
208     { TOK_TYPE_EAP_REQ, TOK_TYPE_EAP_RESP, eapGssSmAuthenticate },
209 };
210
211 OM_uint32
212 gss_init_sec_context(OM_uint32 *minor,
213                      gss_cred_id_t cred,
214                      gss_ctx_id_t *pCtx,
215                      gss_name_t target_name,
216                      gss_OID mech_type,
217                      OM_uint32 req_flags,
218                      OM_uint32 time_req,
219                      gss_channel_bindings_t input_chan_bindings,
220                      gss_buffer_t input_token,
221                      gss_OID *actual_mech_type,
222                      gss_buffer_t output_token,
223                      OM_uint32 *ret_flags,
224                      OM_uint32 *time_rec)
225 {
226     OM_uint32 major, tmpMinor;
227     gss_ctx_id_t ctx = *pCtx;
228     struct eap_gss_initiator_sm *sm = NULL;
229     gss_buffer_desc innerInputToken, innerOutputToken;
230
231     *minor = 0;
232
233     innerOutputToken.length = 0;
234     innerOutputToken.value = NULL;
235
236     output_token->length = 0;
237     output_token->value = NULL;
238
239     if (cred != GSS_C_NO_CREDENTIAL && !(cred->flags & CRED_FLAG_INITIATE)) {
240         major = GSS_S_NO_CRED;
241         goto cleanup;
242     }
243
244     if (ctx == GSS_C_NO_CONTEXT) {
245         if (input_token != GSS_C_NO_BUFFER && input_token->length != 0) {
246             major = GSS_S_DEFECTIVE_TOKEN;
247             goto cleanup;
248         }
249
250         major = gssEapAllocContext(minor, &ctx);
251         if (GSS_ERROR(major))
252             goto cleanup;
253
254         *pCtx = ctx;
255     }
256
257     sm = &eapGssInitiatorSm[ctx->state];
258
259     if (input_token != GSS_C_NO_BUFFER) {
260         major = gssEapVerifyToken(minor, ctx, input_token,
261                                   sm->inputTokenType, &innerInputToken);
262         if (GSS_ERROR(major))
263             goto cleanup;
264     } else {
265         innerInputToken.length = 0;
266         innerInputToken.value = NULL;
267     }
268
269     major = (sm->processToken)(minor,
270                                cred,
271                                ctx,
272                                target_name,
273                                mech_type,
274                                req_flags,
275                                time_req,
276                                input_chan_bindings,
277                                input_token,
278                                output_token);
279     if (GSS_ERROR(major))
280         goto cleanup;
281
282     if (output_token->length != 0) {
283         major = gssEapMakeToken(minor, ctx, &innerOutputToken,
284                                 sm->outputTokenType, output_token);
285         if (GSS_ERROR(major))
286             goto cleanup;
287     }
288
289     if (actual_mech_type != NULL) {
290         if (!gssEapInternalizeOid(ctx->mechanismUsed, actual_mech_type))
291             duplicateOid(&tmpMinor, ctx->mechanismUsed, actual_mech_type);
292     }
293     if (ret_flags != NULL)
294         *ret_flags = ctx->gssFlags;
295     if (time_rec != NULL)
296         gss_context_time(&tmpMinor, ctx, time_rec);
297
298     assert(ctx->state == EAP_STATE_ESTABLISHED || output_token->length != 0);
299
300 cleanup:
301     if (GSS_ERROR(major))
302         gssEapReleaseContext(&tmpMinor, pCtx);
303
304     gss_release_buffer(&tmpMinor, &innerOutputToken);
305
306     return major;
307 }