r23456: Update Samba4 to current lorikeet-heimdal.
[jelmer/samba4-debian.git] / source / heimdal / lib / gssapi / spnego / compat.c
1 /*
2  * Copyright (c) 2004, PADL Software Pty Ltd.
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 PADL Software 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 PADL SOFTWARE AND CONTRIBUTORS ``AS IS'' AND
21  * 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 PADL SOFTWARE 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 "spnego/spnego_locl.h"
34
35 RCSID("$Id: compat.c 19415 2006-12-18 17:52:26Z lha $");
36
37 /*
38  * Apparently Microsoft got the OID wrong, and used
39  * 1.2.840.48018.1.2.2 instead. We need both this and
40  * the correct Kerberos OID here in order to deal with
41  * this. Because this is manifest in SPNEGO only I'd
42  * prefer to deal with this here rather than inside the
43  * Kerberos mechanism.
44  */
45 gss_OID_desc _gss_spnego_mskrb_mechanism_oid_desc =
46         {9, (void *)"\x2a\x86\x48\x82\xf7\x12\x01\x02\x02"};
47
48 gss_OID_desc _gss_spnego_krb5_mechanism_oid_desc =
49         {9, (void *)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x02"};
50
51 /*
52  * Allocate a SPNEGO context handle
53  */
54 OM_uint32 _gss_spnego_alloc_sec_context (OM_uint32 * minor_status,
55                                          gss_ctx_id_t *context_handle)
56 {
57     gssspnego_ctx ctx;
58
59     ctx = calloc(1, sizeof(*ctx));
60     if (ctx == NULL) {
61         *minor_status = ENOMEM;
62         return GSS_S_FAILURE;
63     }
64
65     ctx->initiator_mech_types.len = 0;
66     ctx->initiator_mech_types.val = NULL;
67     ctx->preferred_mech_type = GSS_C_NO_OID;
68     ctx->negotiated_mech_type = GSS_C_NO_OID;
69     ctx->negotiated_ctx_id = GSS_C_NO_CONTEXT;
70
71     /*
72      * Cache these so we can return them before returning
73      * GSS_S_COMPLETE, even if the mechanism has itself
74      * completed earlier
75      */
76     ctx->mech_flags = 0;
77     ctx->mech_time_rec = 0;
78     ctx->mech_src_name = GSS_C_NO_NAME;
79     ctx->delegated_cred_id = GSS_C_NO_CREDENTIAL;
80
81     ctx->open = 0;
82     ctx->local = 0;
83     ctx->require_mic = 0;
84     ctx->verified_mic = 0;
85
86     HEIMDAL_MUTEX_init(&ctx->ctx_id_mutex);
87
88     *context_handle = (gss_ctx_id_t)ctx;
89
90     return GSS_S_COMPLETE;
91 }
92
93 /*
94  * Free a SPNEGO context handle. The caller must have acquired
95  * the lock before this is called.
96  */
97 OM_uint32 _gss_spnego_internal_delete_sec_context
98            (OM_uint32 *minor_status,
99             gss_ctx_id_t *context_handle,
100             gss_buffer_t output_token
101            )
102 {
103     gssspnego_ctx ctx;
104     OM_uint32 ret, minor;
105
106     *minor_status = 0;
107
108     if (context_handle == NULL) {
109         return GSS_S_NO_CONTEXT;
110     }
111
112     if (output_token != GSS_C_NO_BUFFER) {
113         output_token->length = 0;
114         output_token->value = NULL;
115     }
116
117     ctx = (gssspnego_ctx)*context_handle;
118     *context_handle = GSS_C_NO_CONTEXT;
119
120     if (ctx == NULL) {
121         return GSS_S_NO_CONTEXT;
122     }
123
124     if (ctx->initiator_mech_types.val != NULL)
125         free_MechTypeList(&ctx->initiator_mech_types);
126
127     _gss_spnego_release_cred(&minor, &ctx->delegated_cred_id);
128
129     gss_release_oid(&minor, &ctx->preferred_mech_type);
130     ctx->negotiated_mech_type = GSS_C_NO_OID;
131
132     gss_release_name(&minor, &ctx->mech_src_name);
133
134     if (ctx->negotiated_ctx_id != GSS_C_NO_CONTEXT) {
135         ret = gss_delete_sec_context(minor_status,
136                                      &ctx->negotiated_ctx_id,
137                                      output_token);
138         ctx->negotiated_ctx_id = GSS_C_NO_CONTEXT;
139     } else {
140         ret = GSS_S_COMPLETE;
141     }
142
143     HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
144     HEIMDAL_MUTEX_destroy(&ctx->ctx_id_mutex);
145
146     free(ctx);
147     *context_handle = NULL;
148
149     return ret;
150 }
151
152 /*
153  * For compatability with the Windows SPNEGO implementation, the
154  * default is to ignore the mechListMIC unless CFX is used and
155  * a non-preferred mechanism was negotiated
156  */
157
158 OM_uint32
159 _gss_spnego_require_mechlist_mic(OM_uint32 *minor_status,
160                                  gssspnego_ctx ctx,
161                                  int *require_mic)
162 {
163     gss_buffer_set_t buffer_set = GSS_C_NO_BUFFER_SET;
164     OM_uint32 minor;
165
166     *minor_status = 0;
167     *require_mic = 0;
168
169     if (ctx == NULL) {
170         return GSS_S_COMPLETE;
171     }
172
173     if (ctx->require_mic) {
174         /* Acceptor requested it: mandatory to honour */
175         *require_mic = 1;
176         return GSS_S_COMPLETE;
177     }
178
179     /*
180      * Check whether peer indicated implicit support for updated SPNEGO
181      * (eg. in the Kerberos case by using CFX)
182      */
183     if (gss_inquire_sec_context_by_oid(&minor, ctx->negotiated_ctx_id,
184                                        GSS_C_PEER_HAS_UPDATED_SPNEGO,
185                                        &buffer_set) == GSS_S_COMPLETE) {
186         *require_mic = 1;
187         gss_release_buffer_set(&minor, &buffer_set);
188     }
189
190     /* Safe-to-omit MIC rules follow */
191     if (*require_mic) {
192         if (gss_oid_equal(ctx->negotiated_mech_type, ctx->preferred_mech_type)) {
193             *require_mic = 0;
194         } else if (gss_oid_equal(ctx->negotiated_mech_type, &_gss_spnego_krb5_mechanism_oid_desc) &&
195                    gss_oid_equal(ctx->preferred_mech_type, &_gss_spnego_mskrb_mechanism_oid_desc)) {
196             *require_mic = 0;
197         }
198     }
199
200     return GSS_S_COMPLETE;
201 }
202
203 static int
204 add_mech_type(gss_OID mech_type,
205               int includeMSCompatOID,
206               MechTypeList *mechtypelist)
207 {
208     MechType mech;
209     int ret;
210
211     if (gss_oid_equal(mech_type, GSS_SPNEGO_MECHANISM))
212         return 0;
213
214     if (includeMSCompatOID &&
215         gss_oid_equal(mech_type, &_gss_spnego_krb5_mechanism_oid_desc)) {
216         ret = der_get_oid(_gss_spnego_mskrb_mechanism_oid_desc.elements,
217                           _gss_spnego_mskrb_mechanism_oid_desc.length,
218                           &mech,
219                           NULL);
220         if (ret)
221             return ret;
222         ret = add_MechTypeList(mechtypelist, &mech);
223         free_MechType(&mech);
224         if (ret)
225             return ret;
226     }
227     ret = der_get_oid(mech_type->elements, mech_type->length, &mech, NULL);
228     if (ret)
229         return ret;
230     ret = add_MechTypeList(mechtypelist, &mech);
231     free_MechType(&mech);
232     return ret;
233 }
234
235
236 OM_uint32
237 _gss_spnego_indicate_mechtypelist (OM_uint32 *minor_status,
238                                    gss_name_t target_name,
239                                    OM_uint32 (*func)(gss_name_t, gss_OID),
240                                    int includeMSCompatOID,
241                                    const gssspnego_cred cred_handle,
242                                    MechTypeList *mechtypelist,
243                                    gss_OID *preferred_mech)
244 {
245     gss_OID_set supported_mechs = GSS_C_NO_OID_SET;
246     gss_OID first_mech = GSS_C_NO_OID;
247     OM_uint32 ret;
248     int i;
249
250     mechtypelist->len = 0;
251     mechtypelist->val = NULL;
252
253     if (cred_handle != NULL) {
254         ret = gss_inquire_cred(minor_status,
255                                cred_handle->negotiated_cred_id,
256                                NULL,
257                                NULL,
258                                NULL,
259                                &supported_mechs);
260     } else {
261         ret = gss_indicate_mechs(minor_status, &supported_mechs);
262     }
263
264     if (ret != GSS_S_COMPLETE) {
265         return ret;
266     }
267
268     if (supported_mechs->count == 0) {
269         *minor_status = ENOENT;
270         gss_release_oid_set(minor_status, &supported_mechs);
271         return GSS_S_FAILURE;
272     }
273
274     ret = (*func)(target_name, GSS_KRB5_MECHANISM);
275     if (ret == GSS_S_COMPLETE) {
276         ret = add_mech_type(GSS_KRB5_MECHANISM,
277                             includeMSCompatOID,
278                             mechtypelist);
279         if (!GSS_ERROR(ret))
280             first_mech = GSS_KRB5_MECHANISM;
281     }
282     ret = GSS_S_COMPLETE;
283
284     for (i = 0; i < supported_mechs->count; i++) {
285         OM_uint32 subret;
286         if (gss_oid_equal(&supported_mechs->elements[i], GSS_SPNEGO_MECHANISM))
287             continue;
288         if (gss_oid_equal(&supported_mechs->elements[i], GSS_KRB5_MECHANISM))
289             continue;
290
291         subret = (*func)(target_name, &supported_mechs->elements[i]);
292         if (subret != GSS_S_COMPLETE)
293             continue;
294
295         ret = add_mech_type(&supported_mechs->elements[i],
296                             includeMSCompatOID,
297                             mechtypelist);
298         if (ret != 0) {
299             *minor_status = ret;
300             ret = GSS_S_FAILURE;
301             break;
302         }
303         if (first_mech == GSS_C_NO_OID)
304             first_mech = &supported_mechs->elements[i];
305     }
306
307     if (mechtypelist->len == 0) {
308         gss_release_oid_set(minor_status, &supported_mechs);
309         *minor_status = 0;
310         return GSS_S_BAD_MECH;
311     }
312
313     if (preferred_mech != NULL) {
314         ret = gss_duplicate_oid(minor_status, first_mech, preferred_mech);
315         if (ret != GSS_S_COMPLETE)
316             free_MechTypeList(mechtypelist);
317     }
318     gss_release_oid_set(minor_status, &supported_mechs);
319
320     return ret;
321 }