xref: /freebsd/crypto/heimdal/lib/gssapi/mech/gss_store_cred.c (revision 8d20be1e22095c27faf8fe8b2f0d089739cc742e)
1 /*
2  * Copyright (c) 2009 Kungliga Tekniska H�gskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
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  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include "mech_locl.h"
35 
36 GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
37 gss_store_cred(OM_uint32         *minor_status,
38 	       gss_cred_id_t     input_cred_handle,
39 	       gss_cred_usage_t  cred_usage,
40 	       const gss_OID     desired_mech,
41 	       OM_uint32         overwrite_cred,
42 	       OM_uint32         default_cred,
43 	       gss_OID_set       *elements_stored,
44 	       gss_cred_usage_t  *cred_usage_stored)
45 {
46     struct _gss_cred *cred = (struct _gss_cred *) input_cred_handle;
47     struct _gss_mechanism_cred *mc;
48     OM_uint32 maj, junk;
49 
50     if (minor_status == NULL)
51 	return GSS_S_FAILURE;
52     if (elements_stored)
53 	*elements_stored = NULL;
54     if (cred_usage_stored)
55 	*cred_usage_stored = 0;
56 
57     if (cred == NULL)
58 	return GSS_S_NO_CONTEXT;
59 
60     if (elements_stored) {
61 	maj = gss_create_empty_oid_set(minor_status, elements_stored);
62 	if (maj != GSS_S_COMPLETE)
63 	    return maj;
64     }
65 
66     HEIM_SLIST_FOREACH(mc, &cred->gc_mc, gmc_link) {
67 	gssapi_mech_interface m = mc->gmc_mech;
68 
69 	if (m == NULL || m->gm_store_cred == NULL)
70 	    continue;
71 
72 	if (desired_mech) {
73 	    maj = gss_oid_equal(&m->gm_mech_oid, desired_mech);
74 	    if (maj != 0)
75 		continue;
76 	}
77 
78 	maj = (m->gm_store_cred)(minor_status, mc->gmc_cred,
79 				 cred_usage, desired_mech, overwrite_cred,
80 				 default_cred, NULL, cred_usage_stored);
81 	if (maj != GSS_S_COMPLETE) {
82 	    gss_release_oid_set(&junk, elements_stored);
83 	    return maj;
84 	}
85 
86 	if (elements_stored) {
87 	    gss_add_oid_set_member(&junk,
88 				   &m->gm_mech_oid,
89 				   elements_stored);
90 	}
91 
92     }
93     return GSS_S_COMPLETE;
94 }
95