]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libgssapi/gss_accept_sec_context.c
This commit was generated by cvs2svn to compensate for changes in r162503,
[FreeBSD/FreeBSD.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 <errno.h>
32
33 #include "mech_switch.h"
34 #include "context.h"
35 #include "cred.h"
36 #include "name.h"
37
38 OM_uint32 gss_accept_sec_context(OM_uint32 *minor_status,
39     gss_ctx_id_t *context_handle,
40     const gss_cred_id_t acceptor_cred_handle,
41     const gss_buffer_t input_token,
42     const gss_channel_bindings_t input_chan_bindings,
43     gss_name_t *src_name,
44     gss_OID *mech_type,
45     gss_buffer_t output_token,
46     OM_uint32 *ret_flags,
47     OM_uint32 *time_rec,
48     gss_cred_id_t *delegated_cred_handle)
49 {
50         OM_uint32 major_status;
51         struct _gss_mech_switch *m;
52         struct _gss_context *ctx = (struct _gss_context *) *context_handle;
53         struct _gss_cred *cred = (struct _gss_cred *) acceptor_cred_handle;
54         struct _gss_mechanism_cred *mc;
55         gss_cred_id_t acceptor_mc, delegated_mc;
56         gss_name_t src_mn;
57         int allocated_ctx;
58
59         *minor_status = 0;
60         if (src_name) *src_name = 0;
61         if (mech_type) *mech_type = 0;
62         if (ret_flags) *ret_flags = 0;
63         if (time_rec) *time_rec = 0;
64         if (delegated_cred_handle) *delegated_cred_handle = 0;
65         output_token->length = 0;
66         output_token->value = 0;
67
68         /*
69          * If this is the first call (*context_handle is NULL), we must
70          * parse the input token to figure out the mechanism to use.
71          */
72         if (*context_handle == GSS_C_NO_CONTEXT) {
73                 unsigned char *p = input_token->value;
74                 size_t len = input_token->length;
75                 size_t a, b;
76                 gss_OID_desc mech_oid;
77
78                 /*
79                  * Token must start with [APPLICATION 0] SEQUENCE.
80                  */
81                 if (len == 0 || *p != 0x60)
82                         return (GSS_S_DEFECTIVE_TOKEN);
83                 p++;
84                 len--;
85
86                 /*
87                  * Decode the length and make sure it agrees with the
88                  * token length.
89                  */
90                 if (len == 0)
91                         return (GSS_S_DEFECTIVE_TOKEN);
92                 if ((*p & 0x80) == 0) {
93                         a = *p;
94                         p++;
95                         len--;
96                 } else {
97                         b = *p & 0x7f;
98                         p++;
99                         len--;
100                         if (len < b)
101                                 return (GSS_S_DEFECTIVE_TOKEN);
102                         a = 0;
103                         while (b) {
104                                 a = (a << 8) | *p;
105                                 p++;
106                                 len--;
107                                 b--;
108                         }
109                 }
110                 if (a != len)
111                         return (GSS_S_DEFECTIVE_TOKEN);
112
113                 /*
114                  * Decode the OID for the mechanism. Simplify life by
115                  * assuming that the OID length is less than 128 bytes.
116                  */
117                 if (len < 2 || *p != 0x06)
118                         return (GSS_S_DEFECTIVE_TOKEN);
119                 if ((p[1] & 0x80) || p[1] > (len - 2))
120                         return (GSS_S_DEFECTIVE_TOKEN);
121                 mech_oid.length = p[1];
122                 p += 2;
123                 len -= 2;
124                 mech_oid.elements = p;
125
126                 /*
127                  * Now that we have a mechanism, we can find the
128                  * implementation.
129                  */
130                 ctx = malloc(sizeof(struct _gss_context));
131                 if (!ctx) {
132                         *minor_status = ENOMEM;
133                         return (GSS_S_DEFECTIVE_TOKEN);
134                 }
135                 memset(ctx, 0, sizeof(struct _gss_context));
136                 m = ctx->gc_mech = _gss_find_mech_switch(&mech_oid);
137                 if (!m) {
138                         free(ctx);
139                         return (GSS_S_BAD_MECH);
140                 }
141                 allocated_ctx = 1;
142         } else {
143                 m = ctx->gc_mech;
144                 allocated_ctx = 0;
145         }
146
147         if (cred) {
148                 SLIST_FOREACH(mc, &cred->gc_mc, gmc_link)
149                         if (mc->gmc_mech == m)
150                                 break;
151                 if (!mc)
152                         return (GSS_S_BAD_MECH);
153                 acceptor_mc = mc->gmc_cred;
154         } else {
155                 acceptor_mc = GSS_C_NO_CREDENTIAL;
156         }
157         delegated_mc = GSS_C_NO_CREDENTIAL;
158         
159         major_status = m->gm_accept_sec_context(minor_status,
160             &ctx->gc_ctx,
161             acceptor_mc,
162             input_token,
163             input_chan_bindings,
164             &src_mn,
165             mech_type,
166             output_token,
167             ret_flags,
168             time_rec,
169             &delegated_mc);
170         if (major_status != GSS_S_COMPLETE &&
171             major_status != GSS_S_CONTINUE_NEEDED)
172                 return (major_status);
173
174         if (!src_name) {
175                 m->gm_release_name(minor_status, &src_mn);
176         } else {
177                 /*
178                  * Make a new name and mark it as an MN.
179                  */
180                 struct _gss_name *name = _gss_make_name(m, src_mn);
181
182                 if (!name) {
183                         m->gm_release_name(minor_status, &src_mn);
184                         return (GSS_S_FAILURE);
185                 }
186                 *src_name = (gss_name_t) name;
187         }
188
189         if (*ret_flags & GSS_C_DELEG_FLAG) {
190                 if (!delegated_cred_handle) {
191                         m->gm_release_cred(minor_status, &delegated_mc);
192                         *ret_flags &= ~GSS_C_DELEG_FLAG;
193                 } else {
194                         struct _gss_cred *cred;
195                         struct _gss_mechanism_cred *mc;
196
197                         cred = malloc(sizeof(struct _gss_cred));
198                         if (!cred) {
199                                 *minor_status = ENOMEM;
200                                 return (GSS_S_FAILURE);
201                         }
202                         mc = malloc(sizeof(struct _gss_mechanism_cred));
203                         if (!mc) {
204                                 free(cred);
205                                 *minor_status = ENOMEM;
206                                 return (GSS_S_FAILURE);
207                         }
208                         m->gm_inquire_cred(minor_status, delegated_mc,
209                             0, 0, &cred->gc_usage, 0);
210                         mc->gmc_mech = m;
211                         mc->gmc_mech_oid = &m->gm_mech_oid;
212                         mc->gmc_cred = delegated_mc;
213                         SLIST_INSERT_HEAD(&cred->gc_mc, mc, gmc_link);
214
215                         *delegated_cred_handle = (gss_cred_id_t) cred;
216                 }
217         }
218
219         *context_handle = (gss_ctx_id_t) ctx;
220         return (major_status);
221 }