more ISC work
[moonshot.git] / mech_eap / 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 eap_gss_initiator_sm eapGssSm[] = {
195     eapGssSmAuthenticate,
196     NULL,
197     NULL,
198     NULL,
199     NULL,
200 };
201
202 OM_uint32
203 gss_init_sec_context(OM_uint32 *minor,
204                      gss_cred_id_t cred,
205                      gss_ctx_id_t *pCtx,
206                      gss_name_t target_name,
207                      gss_OID mech_type,
208                      OM_uint32 req_flags,
209                      OM_uint32 time_req,
210                      gss_channel_bindings_t input_chan_bindings,
211                      gss_buffer_t input_token,
212                      gss_OID *actual_mech_type,
213                      gss_buffer_t output_token,
214                      OM_uint32 *ret_flags,
215                      OM_uint32 *time_rec)
216 {
217     OM_uint32 major, tmpMinor;
218     gss_ctx_id_t ctx = *pCtx;
219
220     *minor = 0;
221
222     output_token->length = 0;
223     output_token->value = NULL;
224
225     if (cred != GSS_C_NO_CREDENTIAL && !(cred->flags & CRED_FLAG_INITIATE)) {
226         major = GSS_S_NO_CRED;
227         goto cleanup;
228     }
229
230     if (ctx == GSS_C_NO_CONTEXT) {
231         if (input_token != GSS_C_NO_BUFFER && input_token->length != 0) {
232             major = GSS_S_DEFECTIVE_TOKEN;
233             goto cleanup;
234         }
235
236         major = gssEapAllocContext(minor, &ctx);
237         if (GSS_ERROR(major))
238             goto cleanup;
239
240         *pCtx = ctx;
241     }
242
243     for (; ctx->state != EAP_STATE_ESTABLISHED; ctx->state++) {
244         major = (eapGssSm[ctx->state])(minor,
245                                        cred,
246                                        ctx,
247                                        target_name,
248                                        mech_type,
249                                        req_flags,
250                                        time_req,
251                                        input_chan_bindings,
252                                        input_token,
253                                        output_token);
254         if (GSS_ERROR(major))
255             goto cleanup;
256         if (output_token->length != 0) {
257             assert(major == GSS_S_CONTINUE_NEEDED);
258             break;
259         }
260     }
261
262     if (actual_mech_type != NULL) {
263         if (!gssEapInternalizeOid(ctx->mechanismUsed, actual_mech_type))
264             duplicateOid(&tmpMinor, ctx->mechanismUsed, actual_mech_type);
265     }
266     if (ret_flags != NULL)
267         *ret_flags = ctx->gssFlags;
268     if (time_rec != NULL)
269         gss_context_time(&tmpMinor, ctx, time_rec);
270
271     assert(ctx->state == EAP_STATE_ESTABLISHED || output_token->length != 0);
272
273 cleanup:
274     if (GSS_ERROR(major))
275         gssEapReleaseContext(&tmpMinor, pCtx);
276
277     return major;
278 }