]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - crypto/heimdal/lib/gssapi/mech/gss_add_cred.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / crypto / heimdal / lib / gssapi / mech / gss_add_cred.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: src/lib/libgssapi/gss_add_cred.c,v 1.1 2005/12/29 14:40:20 dfr Exp $
27  */
28
29 #include "mech_locl.h"
30
31 struct _gss_mechanism_cred *
32 _gss_copy_cred(struct _gss_mechanism_cred *mc)
33 {
34         struct _gss_mechanism_cred *new_mc;
35         gssapi_mech_interface m = mc->gmc_mech;
36         OM_uint32 major_status, minor_status;
37         gss_name_t name;
38         gss_cred_id_t cred;
39         OM_uint32 initiator_lifetime, acceptor_lifetime;
40         gss_cred_usage_t cred_usage;
41
42         major_status = m->gm_inquire_cred_by_mech(&minor_status,
43             mc->gmc_cred, mc->gmc_mech_oid,
44             &name, &initiator_lifetime, &acceptor_lifetime, &cred_usage);
45         if (major_status) {
46                 _gss_mg_error(m, major_status, minor_status);
47                 return (0);
48         }
49
50         major_status = m->gm_add_cred(&minor_status,
51             GSS_C_NO_CREDENTIAL, name, mc->gmc_mech_oid,
52             cred_usage, initiator_lifetime, acceptor_lifetime,
53             &cred, 0, 0, 0);
54         m->gm_release_name(&minor_status, &name);
55
56         if (major_status) {
57                 _gss_mg_error(m, major_status, minor_status);
58                 return (0);
59         }
60
61         new_mc = malloc(sizeof(struct _gss_mechanism_cred));
62         if (!new_mc) {
63                 m->gm_release_cred(&minor_status, &cred);
64                 return (0);
65         }
66         new_mc->gmc_mech = m;
67         new_mc->gmc_mech_oid = &m->gm_mech_oid;
68         new_mc->gmc_cred = cred;
69
70         return (new_mc);
71 }
72
73 GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
74 gss_add_cred(OM_uint32 *minor_status,
75     const gss_cred_id_t input_cred_handle,
76     const gss_name_t desired_name,
77     const gss_OID desired_mech,
78     gss_cred_usage_t cred_usage,
79     OM_uint32 initiator_time_req,
80     OM_uint32 acceptor_time_req,
81     gss_cred_id_t *output_cred_handle,
82     gss_OID_set *actual_mechs,
83     OM_uint32 *initiator_time_rec,
84     OM_uint32 *acceptor_time_rec)
85 {
86         OM_uint32 major_status;
87         gssapi_mech_interface m;
88         struct _gss_cred *cred = (struct _gss_cred *) input_cred_handle;
89         struct _gss_cred *new_cred;
90         gss_cred_id_t release_cred;
91         struct _gss_mechanism_cred *mc, *target_mc, *copy_mc;
92         struct _gss_mechanism_name *mn;
93         OM_uint32 junk;
94
95         *minor_status = 0;
96         *output_cred_handle = GSS_C_NO_CREDENTIAL;
97         if (initiator_time_rec)
98             *initiator_time_rec = 0;
99         if (acceptor_time_rec)
100             *acceptor_time_rec = 0;
101         if (actual_mechs)
102             *actual_mechs = GSS_C_NO_OID_SET;
103
104         new_cred = malloc(sizeof(struct _gss_cred));
105         if (!new_cred) {
106                 *minor_status = ENOMEM;
107                 return (GSS_S_FAILURE);
108         }
109         HEIM_SLIST_INIT(&new_cred->gc_mc);
110
111         /*
112          * We go through all the mc attached to the input_cred_handle
113          * and check the mechanism. If it matches, we call
114          * gss_add_cred for that mechanism, otherwise we copy the mc
115          * to new_cred.
116          */
117         target_mc = 0;
118         if (cred) {
119                 HEIM_SLIST_FOREACH(mc, &cred->gc_mc, gmc_link) {
120                         if (gss_oid_equal(mc->gmc_mech_oid, desired_mech)) {
121                                 target_mc = mc;
122                         }
123                         copy_mc = _gss_copy_cred(mc);
124                         if (!copy_mc) {
125                                 release_cred = (gss_cred_id_t)new_cred;
126                                 gss_release_cred(&junk, &release_cred);
127                                 *minor_status = ENOMEM;
128                                 return (GSS_S_FAILURE);
129                         }
130                         HEIM_SLIST_INSERT_HEAD(&new_cred->gc_mc, copy_mc, gmc_link);
131                 }
132         }
133
134         /*
135          * Figure out a suitable mn, if any.
136          */
137         if (desired_name) {
138                 major_status = _gss_find_mn(minor_status,
139                                             (struct _gss_name *) desired_name,
140                                             desired_mech,
141                                             &mn);
142                 if (major_status != GSS_S_COMPLETE) {
143                         free(new_cred);
144                         return major_status;
145                 }
146         } else {
147                 mn = 0;
148         }
149
150         m = __gss_get_mechanism(desired_mech);
151
152         mc = malloc(sizeof(struct _gss_mechanism_cred));
153         if (!mc) {
154                 release_cred = (gss_cred_id_t)new_cred;
155                 gss_release_cred(&junk, &release_cred);
156                 *minor_status = ENOMEM;
157                 return (GSS_S_FAILURE);
158         }
159         mc->gmc_mech = m;
160         mc->gmc_mech_oid = &m->gm_mech_oid;
161
162         major_status = m->gm_add_cred(minor_status,
163             target_mc ? target_mc->gmc_cred : GSS_C_NO_CREDENTIAL,
164             desired_name ? mn->gmn_name : GSS_C_NO_NAME,
165             desired_mech,
166             cred_usage,
167             initiator_time_req,
168             acceptor_time_req,
169             &mc->gmc_cred,
170             actual_mechs,
171             initiator_time_rec,
172             acceptor_time_rec);
173
174         if (major_status) {
175                 _gss_mg_error(m, major_status, *minor_status);
176                 release_cred = (gss_cred_id_t)new_cred;
177                 gss_release_cred(&junk, &release_cred);
178                 free(mc);
179                 return (major_status);
180         }
181         HEIM_SLIST_INSERT_HEAD(&new_cred->gc_mc, mc, gmc_link);
182         *output_cred_handle = (gss_cred_id_t) new_cred;
183
184         return (GSS_S_COMPLETE);
185 }
186