gssapi: defensively initialize *out in gss_duplicate_cred()
[heimdal.git] / lib / gssapi / mech / gss_duplicate_cred.c
blob19c4c197455ebdbc39e6ecf0f91dcac72ee01b7a
1 /*-
2 * Copyright (c) 2005 Doug Rabson
3 * Copyright (c) 2018 Kungliga Tekniska Högskolan
4 * (Royal Institute of Technology, Stockholm, Sweden).
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
28 * $FreeBSD: src/lib/libgssapi/gss_add_cred.c,v 1.1 2005/12/29 14:40:20 dfr Exp $
31 #include "mech_locl.h"
33 static OM_uint32
34 copy_cred_element(OM_uint32 *minor_status,
35 struct _gss_mechanism_cred *mc,
36 struct _gss_mechanism_cred **out)
38 gssapi_mech_interface m = mc->gmc_mech;
39 OM_uint32 major_status, tmp;
40 struct _gss_mechanism_cred *new_mc;
41 OM_uint32 initiator_lifetime, acceptor_lifetime;
42 gss_cred_usage_t cred_usage;
43 gss_cred_id_t dup_cred = GSS_C_NO_CREDENTIAL;
45 *out = NULL;
47 if (m->gm_duplicate_cred) {
48 major_status = m->gm_duplicate_cred(minor_status,
49 mc->gmc_cred, &dup_cred);
50 } else if (m->gm_import_cred && m->gm_export_cred) {
51 gss_buffer_desc export;
53 major_status = m->gm_export_cred(minor_status, mc->gmc_cred, &export);
54 if (major_status == GSS_S_COMPLETE) {
55 major_status = m->gm_import_cred(minor_status, &export, &dup_cred);
56 gss_release_buffer(&tmp, &export);
58 } else {
59 struct _gss_mechanism_name mn;
61 mn.gmn_mech = m;
62 mn.gmn_mech_oid = mc->gmc_mech_oid;
63 mn.gmn_name = GSS_C_NO_NAME;
65 /* This path won't work for ephemeral creds or cred stores */
66 major_status = m->gm_inquire_cred_by_mech(minor_status, mc->gmc_cred,
67 mc->gmc_mech_oid, &mn.gmn_name,
68 &initiator_lifetime,
69 &acceptor_lifetime, &cred_usage);
70 if (major_status == GSS_S_COMPLETE) {
71 major_status = _gss_mg_add_mech_cred(minor_status,
73 NULL, /* mc */
74 &mn,
75 cred_usage,
76 initiator_lifetime,
77 acceptor_lifetime,
78 GSS_C_NO_CRED_STORE,
79 &new_mc,
80 NULL,
81 NULL);
82 m->gm_release_name(&tmp, &mn.gmn_name);
86 if (major_status == GSS_S_COMPLETE) {
87 new_mc = calloc(1, sizeof(*new_mc));
88 if (new_mc == NULL) {
89 *minor_status = ENOMEM;
90 m->gm_release_cred(&tmp, &dup_cred);
91 return GSS_S_FAILURE;
94 new_mc->gmc_mech = m;
95 new_mc->gmc_mech_oid = mc->gmc_mech_oid;
96 new_mc->gmc_cred = dup_cred;
98 *out = new_mc;
99 } else
100 _gss_mg_error(m, *minor_status);
102 return major_status;
105 GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
106 gss_duplicate_cred(OM_uint32 *minor_status,
107 gss_const_cred_id_t input_cred_handle,
108 gss_cred_id_t *output_cred_handle)
110 struct _gss_mechanism_cred *mc;
111 struct _gss_cred *new_cred;
112 struct _gss_cred *cred = (struct _gss_cred *)input_cred_handle;
113 OM_uint32 major_status, junk;
115 if (input_cred_handle == GSS_C_NO_CREDENTIAL) {
117 * "Copy" the default credential by acquiring a cred handle for the
118 * default credential's name, GSS_C_NO_NAME.
120 return gss_acquire_cred(minor_status, GSS_C_NO_NAME, GSS_C_INDEFINITE,
121 GSS_C_NO_OID_SET, GSS_C_BOTH,
122 output_cred_handle, NULL, NULL);
125 *output_cred_handle = GSS_C_NO_CREDENTIAL;
126 new_cred = _gss_mg_alloc_cred();
127 if (!new_cred) {
128 *minor_status = ENOMEM;
129 return GSS_S_FAILURE;
132 *minor_status = 0;
133 major_status = GSS_S_NO_CRED;
135 HEIM_SLIST_FOREACH(mc, &cred->gc_mc, gmc_link) {
136 struct _gss_mechanism_cred *copy_mc;
138 major_status = copy_cred_element(minor_status, mc, &copy_mc);
139 if (major_status != GSS_S_COMPLETE)
140 break;
142 HEIM_SLIST_INSERT_HEAD(&new_cred->gc_mc, copy_mc, gmc_link);
145 if (major_status != GSS_S_COMPLETE) {
146 gss_cred_id_t release_cred = (gss_cred_id_t)new_cred;
147 gss_release_cred(&junk, &release_cred);
148 new_cred = NULL;
151 *output_cred_handle = (gss_cred_id_t)new_cred;
152 return major_status;