Refactor
[mech_eap.orig] / accept_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 eapGssSmAcceptAuthenticate(OM_uint32 *minor,
37                            gss_ctx_id_t ctx,
38                            gss_cred_id_t cred,
39                            gss_buffer_t inputToken,
40                            gss_channel_bindings_t chanBindings,
41                            gss_buffer_t outputToken)
42 {
43     OM_uint32 major, tmpMinor;
44
45 cleanup:
46     return major;
47 }
48
49
50 static struct eap_gss_acceptor_sm {
51     enum gss_eap_token_type inputTokenType;
52     enum gss_eap_token_type outputTokenType;
53     OM_uint32 (*processToken)(OM_uint32 *,
54                               gss_ctx_id_t,
55                               gss_cred_id_t,
56                               gss_buffer_t,
57                               gss_channel_bindings_t,
58                               gss_buffer_t);
59 } eapGssAcceptorSm[] = {
60     { TOK_TYPE_EAP_RESP, TOK_TYPE_EAP_REQ, NULL },
61 };
62
63 OM_uint32
64 gss_accept_sec_context(OM_uint32 *minor,
65                        gss_ctx_id_t *context_handle,
66                        gss_cred_id_t cred,
67                        gss_buffer_t input_token,
68                        gss_channel_bindings_t input_chan_bindings,
69                        gss_name_t *src_name,
70                        gss_OID *mech_type,
71                        gss_buffer_t output_token,
72                        OM_uint32 *ret_flags,
73                        OM_uint32 *time_rec,
74                        gss_cred_id_t *delegated_cred_handle)
75 {
76     OM_uint32 major, tmpMinor;
77     gss_ctx_id_t ctx = *context_handle;
78     struct eap_gss_acceptor_sm *sm = NULL;
79     gss_buffer_desc innerInputToken, innerOutputToken;
80     
81     *minor = 0;
82
83     innerOutputToken.length = 0;
84     innerOutputToken.value = NULL;
85
86     output_token->length = 0;
87     output_token->value = NULL;
88
89     if (cred != GSS_C_NO_CREDENTIAL && !(cred->flags & CRED_FLAG_ACCEPT)) {
90         return GSS_S_NO_CRED;
91     }
92
93     if (input_token == GSS_C_NO_BUFFER || input_token->length == 0) {
94         return GSS_S_DEFECTIVE_TOKEN;
95     }
96
97     if (ctx == GSS_C_NO_CONTEXT) {
98         major = gssEapAllocContext(minor, &ctx);
99         if (GSS_ERROR(major))
100             return major;
101
102         *context_handle = ctx;
103     }
104
105     GSSEAP_MUTEX_LOCK(&ctx->mutex);
106
107     sm = &eapGssAcceptorSm[ctx->state];
108
109     major = gssEapVerifyToken(minor, ctx, input_token,
110                               sm->inputTokenType, &innerInputToken);
111     if (GSS_ERROR(major))
112         goto cleanup;
113
114     major = (sm->processToken)(minor,
115                                ctx,
116                                cred,
117                                &innerInputToken,
118                                input_chan_bindings,
119                                &innerOutputToken);
120     if (GSS_ERROR(major))
121         goto cleanup;
122
123     if (src_name != NULL && ctx->initiatorName != GSS_C_NO_NAME) {
124         major = gss_duplicate_name(&minor, ctx->initiatorName, src_name);
125         if (GSS_ERROR(major))
126             goto cleanup;
127     }
128     if (mech_type != NULL) {
129         if (!gssEapInternalizeOid(ctx->mechanismUsed, mech_type))
130             duplicateOid(&tmpMinor, ctx->mechanismUsed, mech_type);
131     }
132     if (innerOutputToken.length != 0) {
133         major = gssEapMakeToken(minor, ctx, &innerOutputToken,
134                                 sm->outputTokenType, output_token);
135         if (GSS_ERROR(major))
136             goto cleanup;
137     }
138     if (ret_flags != NULL)
139         *ret_flags = ctx->gssFlags;
140     if (time_rec != NULL)
141         gss_context_time(&tmpMinor, ctx, time_rec);
142     if (delegated_cred_handle != NULL)
143         *delegated_cred_handle = GSS_C_NO_CREDENTIAL;
144
145     assert(ctx->state == EAP_STATE_ESTABLISHED || major == GSS_S_CONTINUE_NEEDED);
146
147 cleanup:
148     GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
149
150     if (GSS_ERROR(major))
151         gssEapReleaseContext(&tmpMinor, context_handle);
152
153     gss_release_buffer(&tmpMinor, &innerOutputToken);
154
155     return major;
156 }