1 /* $NetBSD: gss_cred.c,v 1.1.1.2 2014/04/24 12:45:29 pettai Exp $ */
4 * Copyright (c) 2009 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of KTH nor the names of its contributors may be
20 * used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
24 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include "mech_locl.h"
37 #include <krb5/krb5.h>
40 * format: any number of:
42 * mech-data: char * (not alligned)
44 * cred-data char * (not alligned)
47 GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
48 gss_export_cred(OM_uint32
* minor_status
,
49 gss_cred_id_t cred_handle
,
52 struct _gss_cred
*cred
= (struct _gss_cred
*)cred_handle
;
53 struct _gss_mechanism_cred
*mc
;
54 gss_buffer_desc buffer
;
60 _mg_buffer_zero(token
);
67 HEIM_SLIST_FOREACH(mc
, &cred
->gc_mc
, gmc_link
) {
68 if (mc
->gmc_mech
->gm_export_cred
== NULL
) {
74 sp
= krb5_storage_emem();
76 *minor_status
= ENOMEM
;
80 HEIM_SLIST_FOREACH(mc
, &cred
->gc_mc
, gmc_link
) {
82 major
= mc
->gmc_mech
->gm_export_cred(minor_status
,
83 mc
->gmc_cred
, &buffer
);
85 krb5_storage_free(sp
);
89 ret
= krb5_storage_write(sp
, buffer
.value
, buffer
.length
);
90 if (ret
< 0 || (size_t)ret
!= buffer
.length
) {
91 gss_release_buffer(minor_status
, &buffer
);
92 krb5_storage_free(sp
);
93 *minor_status
= EINVAL
;
96 gss_release_buffer(minor_status
, &buffer
);
99 ret
= krb5_storage_to_data(sp
, &data
);
100 krb5_storage_free(sp
);
103 return GSS_S_FAILURE
;
106 token
->value
= data
.data
;
107 token
->length
= data
.length
;
109 return GSS_S_COMPLETE
;
112 GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
113 gss_import_cred(OM_uint32
* minor_status
,
115 gss_cred_id_t
* cred_handle
)
117 gssapi_mech_interface m
;
119 struct _gss_cred
*cred
;
120 krb5_storage
*sp
= NULL
;
121 OM_uint32 major
, junk
;
124 *cred_handle
= GSS_C_NO_CREDENTIAL
;
126 if (token
->length
== 0) {
127 *minor_status
= ENOMEM
;
128 return GSS_S_FAILURE
;
131 sp
= krb5_storage_from_readonly_mem(token
->value
, token
->length
);
133 *minor_status
= ENOMEM
;
134 return GSS_S_FAILURE
;
137 cred
= calloc(1, sizeof(struct _gss_cred
));
139 krb5_storage_free(sp
);
140 *minor_status
= ENOMEM
;
141 return GSS_S_FAILURE
;
143 HEIM_SLIST_INIT(&cred
->gc_mc
);
145 *cred_handle
= (gss_cred_id_t
)cred
;
148 struct _gss_mechanism_cred
*mc
;
149 gss_buffer_desc buffer
;
153 ret
= krb5_ret_data(sp
, &data
);
154 if (ret
== HEIM_ERR_EOF
) {
158 major
= GSS_S_FAILURE
;
161 oid
.elements
= data
.data
;
162 oid
.length
= data
.length
;
164 m
= __gss_get_mechanism(&oid
);
165 krb5_data_free(&data
);
168 major
= GSS_S_BAD_MECH
;
172 if (m
->gm_import_cred
== NULL
) {
174 major
= GSS_S_BAD_MECH
;
178 ret
= krb5_ret_data(sp
, &data
);
181 major
= GSS_S_FAILURE
;
185 buffer
.value
= data
.data
;
186 buffer
.length
= data
.length
;
188 major
= m
->gm_import_cred(minor_status
,
190 krb5_data_free(&data
);
195 mc
= malloc(sizeof(struct _gss_mechanism_cred
));
197 *minor_status
= EINVAL
;
198 major
= GSS_S_FAILURE
;
203 mc
->gmc_mech_oid
= &m
->gm_mech_oid
;
204 mc
->gmc_cred
= mcred
;
206 HEIM_SLIST_INSERT_HEAD(&cred
->gc_mc
, mc
, gmc_link
);
208 krb5_storage_free(sp
);
211 if (HEIM_SLIST_EMPTY(&cred
->gc_mc
)) {
212 major
= GSS_S_NO_CRED
;
216 return GSS_S_COMPLETE
;
220 krb5_storage_free(sp
);
222 gss_release_cred(&junk
, cred_handle
);