57298425b78fc4b3235460991fc6b6eff8d680d7
[mech_eap.git] / 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     GSSEAP_NOT_IMPLEMENTED;
155 }
156
157 static eap_gss_initiator_sm eapGssSm[] = {
158     eapGssSmAuthenticate,
159     NULL,
160     NULL,
161     NULL,
162     NULL,
163 };
164
165 OM_uint32
166 gss_init_sec_context(OM_uint32 *minor,
167                      gss_cred_id_t cred,
168                      gss_ctx_id_t *pCtx,
169                      gss_name_t target_name,
170                      gss_OID mech_type,
171                      OM_uint32 req_flags,
172                      OM_uint32 time_req,
173                      gss_channel_bindings_t input_chan_bindings,
174                      gss_buffer_t input_token,
175                      gss_OID *actual_mech_type,
176                      gss_buffer_t output_token,
177                      OM_uint32 *ret_flags,
178                      OM_uint32 *time_rec)
179 {
180     OM_uint32 major, tmpMinor;
181     gss_ctx_id_t ctx = *pCtx;
182
183     *minor = 0;
184
185     output_token->length = 0;
186     output_token->value = NULL;
187
188     if (cred != GSS_C_NO_CREDENTIAL && !(cred->flags & CRED_FLAG_INITIATE)) {
189         major = GSS_S_NO_CRED;
190         goto cleanup;
191     }
192
193     if (ctx == GSS_C_NO_CONTEXT) {
194         if (input_token != GSS_C_NO_BUFFER && input_token->length != 0) {
195             major = GSS_S_DEFECTIVE_TOKEN;
196             goto cleanup;
197         }
198
199         major = gssEapAllocContext(minor, &ctx);
200         if (GSS_ERROR(major))
201             goto cleanup;
202     }
203
204     for (; ctx->state != EAP_STATE_ESTABLISHED; ctx->state++) {
205         major = (eapGssSm[ctx->state])(minor,
206                                        cred,
207                                        ctx,
208                                        target_name,
209                                        mech_type,
210                                        req_flags,
211                                        time_req,
212                                        input_chan_bindings,
213                                        input_token,
214                                        output_token);
215         if (GSS_ERROR(major))
216             goto cleanup;
217         if (output_token->length != 0) {
218             assert(major == GSS_S_CONTINUE_NEEDED);
219             break;
220         }
221     }
222
223     if (actual_mech_type != NULL)
224         *actual_mech_type = ctx->mechanismUsed;
225     if (ret_flags != NULL)
226         *ret_flags = ctx->gssFlags;
227     if (time_rec != NULL)
228         gss_context_time(&tmpMinor, ctx, time_rec);
229
230     assert(ctx->state == EAP_STATE_ESTABLISHED || output_token->length != 0);
231
232 cleanup:
233     if (GSS_ERROR(major))
234         gssEapReleaseContext(&tmpMinor, &ctx);
235
236     return major;
237 }