]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - lib/libgssapi/gss_accept_sec_context.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / lib / libgssapi / gss_accept_sec_context.c
1 /*-
2  * Copyright (c) 2005 Doug Rabson
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  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *      $FreeBSD$
27  */
28
29 #include <gssapi/gssapi.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <errno.h>
33
34 #include "mech_switch.h"
35 #include "context.h"
36 #include "cred.h"
37 #include "name.h"
38
39 OM_uint32 gss_accept_sec_context(OM_uint32 *minor_status,
40     gss_ctx_id_t *context_handle,
41     const gss_cred_id_t acceptor_cred_handle,
42     const gss_buffer_t input_token,
43     const gss_channel_bindings_t input_chan_bindings,
44     gss_name_t *src_name,
45     gss_OID *mech_type,
46     gss_buffer_t output_token,
47     OM_uint32 *ret_flags,
48     OM_uint32 *time_rec,
49     gss_cred_id_t *delegated_cred_handle)
50 {
51         OM_uint32 major_status, mech_ret_flags;
52         struct _gss_mech_switch *m;
53         struct _gss_context *ctx = (struct _gss_context *) *context_handle;
54         struct _gss_cred *cred = (struct _gss_cred *) acceptor_cred_handle;
55         struct _gss_mechanism_cred *mc;
56         gss_cred_id_t acceptor_mc, delegated_mc;
57         gss_name_t src_mn;
58         int allocated_ctx;
59
60         *minor_status = 0;
61         if (src_name) *src_name = 0;
62         if (mech_type) *mech_type = 0;
63         if (ret_flags) *ret_flags = 0;
64         if (time_rec) *time_rec = 0;
65         if (delegated_cred_handle) *delegated_cred_handle = 0;
66         output_token->length = 0;
67         output_token->value = 0;
68
69         /*
70          * If this is the first call (*context_handle is NULL), we must
71          * parse the input token to figure out the mechanism to use.
72          */
73         if (*context_handle == GSS_C_NO_CONTEXT) {
74                 unsigned char *p = input_token->value;
75                 size_t len = input_token->length;
76                 size_t a, b;
77                 gss_OID_desc mech_oid;
78
79                 /*
80                  * Token must start with [APPLICATION 0] SEQUENCE.
81                  */
82                 if (len == 0 || *p != 0x60)
83                         return (GSS_S_DEFECTIVE_TOKEN);
84                 p++;
85                 len--;
86
87                 /*
88                  * Decode the length and make sure it agrees with the
89                  * token length.
90                  */
91                 if (len == 0)
92                         return (GSS_S_DEFECTIVE_TOKEN);
93                 if ((*p & 0x80) == 0) {
94                         a = *p;
95                         p++;
96                         len--;
97                 } else {
98                         b = *p & 0x7f;
99                         p++;
100                         len--;
101                         if (len < b)
102                                 return (GSS_S_DEFECTIVE_TOKEN);
103                         a = 0;
104                         while (b) {
105                                 a = (a << 8) | *p;
106                                 p++;
107                                 len--;
108                                 b--;
109                         }
110                 }
111                 if (a != len)
112                         return (GSS_S_DEFECTIVE_TOKEN);
113
114                 /*
115                  * Decode the OID for the mechanism. Simplify life by
116                  * assuming that the OID length is less than 128 bytes.
117                  */
118                 if (len < 2 || *p != 0x06)
119                         return (GSS_S_DEFECTIVE_TOKEN);
120                 if ((p[1] & 0x80) || p[1] > (len - 2))
121                         return (GSS_S_DEFECTIVE_TOKEN);
122                 mech_oid.length = p[1];
123                 p += 2;
124                 len -= 2;
125                 mech_oid.elements = p;
126
127                 /*
128                  * Now that we have a mechanism, we can find the
129                  * implementation.
130                  */
131                 ctx = malloc(sizeof(struct _gss_context));
132                 if (!ctx) {
133                         *minor_status = ENOMEM;
134                         return (GSS_S_DEFECTIVE_TOKEN);
135                 }
136                 memset(ctx, 0, sizeof(struct _gss_context));
137                 m = ctx->gc_mech = _gss_find_mech_switch(&mech_oid);
138                 if (!m) {
139                         free(ctx);
140                         return (GSS_S_BAD_MECH);
141                 }
142                 allocated_ctx = 1;
143         } else {
144                 m = ctx->gc_mech;
145                 allocated_ctx = 0;
146         }
147
148         if (cred) {
149                 SLIST_FOREACH(mc, &cred->gc_mc, gmc_link)
150                         if (mc->gmc_mech == m)
151                                 break;
152                 if (!mc)
153                         return (GSS_S_BAD_MECH);
154                 acceptor_mc = mc->gmc_cred;
155         } else {
156                 acceptor_mc = GSS_C_NO_CREDENTIAL;
157         }
158         delegated_mc = GSS_C_NO_CREDENTIAL;
159         
160         major_status = m->gm_accept_sec_context(minor_status,
161             &ctx->gc_ctx,
162             acceptor_mc,
163             input_token,
164             input_chan_bindings,
165             &src_mn,
166             mech_type,
167             output_token,
168             &mech_ret_flags,
169             time_rec,
170             &delegated_mc);
171         if (major_status != GSS_S_COMPLETE &&
172             major_status != GSS_S_CONTINUE_NEEDED)
173                 return (major_status);
174
175         if (!src_name) {
176                 m->gm_release_name(minor_status, &src_mn);
177         } else {
178                 /*
179                  * Make a new name and mark it as an MN.
180                  */
181                 struct _gss_name *name = _gss_make_name(m, src_mn);
182
183                 if (!name) {
184                         m->gm_release_name(minor_status, &src_mn);
185                         return (GSS_S_FAILURE);
186                 }
187                 *src_name = (gss_name_t) name;
188         }
189
190         if (delegated_mc == GSS_C_NO_CREDENTIAL)
191                 mech_ret_flags &= ~GSS_C_DELEG_FLAG;
192
193         if (mech_ret_flags & GSS_C_DELEG_FLAG) {
194                 if (!delegated_cred_handle) {
195                         m->gm_release_cred(minor_status, &delegated_mc);
196                         mech_ret_flags &= ~GSS_C_DELEG_FLAG;
197                 } else {
198                         struct _gss_cred *cred;
199                         struct _gss_mechanism_cred *mc;
200
201                         cred = malloc(sizeof(struct _gss_cred));
202                         if (!cred) {
203                                 *minor_status = ENOMEM;
204                                 return (GSS_S_FAILURE);
205                         }
206                         SLIST_INIT(&cred->gc_mc);
207                         mc = malloc(sizeof(struct _gss_mechanism_cred));
208                         if (!mc) {
209                                 free(cred);
210                                 *minor_status = ENOMEM;
211                                 return (GSS_S_FAILURE);
212                         }
213                         m->gm_inquire_cred(minor_status, delegated_mc,
214                             0, 0, &cred->gc_usage, 0);
215                         mc->gmc_mech = m;
216                         mc->gmc_mech_oid = &m->gm_mech_oid;
217                         mc->gmc_cred = delegated_mc;
218                         SLIST_INSERT_HEAD(&cred->gc_mc, mc, gmc_link);
219
220                         *delegated_cred_handle = (gss_cred_id_t) cred;
221                 }
222         }
223
224         if (ret_flags)
225                 *ret_flags = mech_ret_flags;
226         *context_handle = (gss_ctx_id_t) ctx;
227         return (major_status);
228 }