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