s4:torture: Adapt KDC canon test to Heimdal upstream changes
[samba.git] / source4 / heimdal / lib / gssapi / mech / gssspi_exchange_meta_data.c
1 /*-
2  * Copyright (c) 2005 Doug Rabson
3  * All rights reserved.
4  *
5  * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
6  * Portions Copyright (c) 2019 AuriStor, Inc. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include "mech_locl.h"
31
32 GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
33 gssspi_exchange_meta_data(
34     OM_uint32 *minor_status,
35     gss_const_OID input_mech_type,
36     gss_cred_id_t input_cred_handle,
37     gss_ctx_id_t *context_handle,
38     gss_const_name_t target_name,
39     OM_uint32 req_flags,
40     gss_const_buffer_t meta_data)
41 {
42     OM_uint32 major_status, junk;
43     gssapi_mech_interface m;
44     struct _gss_name *name = (struct _gss_name *) target_name;
45     struct _gss_mechanism_name *mn;
46     struct _gss_context *ctx = (struct _gss_context *) *context_handle;
47     gss_cred_id_t cred_handle;
48     int allocated_ctx;
49     gss_const_OID mech_type = input_mech_type;
50
51     *minor_status = 0;
52
53     if (mech_type == GSS_C_NO_OID)
54         return GSS_S_BAD_MECH;
55
56     if (ctx == NULL) {
57         ctx = calloc(1, sizeof(struct _gss_context));
58         if (ctx == NULL) {
59             *minor_status = ENOMEM;
60             return GSS_S_FAILURE;
61         }
62
63         m = ctx->gc_mech = __gss_get_mechanism(mech_type);
64         if (m == NULL) {
65             free(ctx);
66             return GSS_S_BAD_MECH;
67         }
68         allocated_ctx = 1;
69     } else {
70         m = ctx->gc_mech;
71         mech_type = &m->gm_mech_oid;
72         allocated_ctx = 0;
73     }
74
75     if (m->gm_exchange_meta_data == NULL) {
76         major_status = GSS_S_BAD_MECH;
77         goto cleanup;
78     }
79
80     major_status = _gss_find_mn(minor_status, name, mech_type, &mn);
81     if (major_status != GSS_S_COMPLETE)
82         goto cleanup;
83
84     if (m->gm_flags & GM_USE_MG_CRED)
85         cred_handle = input_cred_handle;
86     else
87         cred_handle = _gss_mg_find_mech_cred(input_cred_handle, mech_type);
88
89     if (input_cred_handle != GSS_C_NO_CREDENTIAL &&
90         cred_handle == NULL) {
91         major_status = GSS_S_NO_CRED;
92         goto cleanup;
93     }
94
95     /* note: mechanism is not obligated to allocate a context on success */
96     major_status = m->gm_exchange_meta_data(minor_status,
97             mech_type,
98             cred_handle,
99             &ctx->gc_ctx,
100             mn ? mn->gmn_name : GSS_C_NO_NAME,
101             req_flags,
102             meta_data);
103     if (major_status != GSS_S_COMPLETE)
104         _gss_mg_error(m, *minor_status);
105
106 cleanup:
107     if (allocated_ctx && major_status != GSS_S_COMPLETE)
108         gss_delete_sec_context(&junk, (gss_ctx_id_t *)&ctx, GSS_C_NO_BUFFER);
109
110     *context_handle = (gss_ctx_id_t) ctx;
111
112     _gss_mg_log(10, "gss-emd: return %d/%d", (int)major_status, (int)*minor_status);
113
114     return major_status;
115 }