2 * Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * 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.
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.
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
34 #include "gssapi_locl.h"
38 static krb5_keytab gss_keytab
;
41 gsskrb5_register_acceptor_identity (char *identity
)
44 if(gss_keytab
!= NULL
) {
45 krb5_kt_close(gssapi_krb5_context
, gss_keytab
);
48 asprintf(&p
, "FILE:%s", identity
);
51 krb5_kt_resolve(gssapi_krb5_context
, p
, &gss_keytab
);
53 return GSS_S_COMPLETE
;
57 gss_accept_sec_context
58 (OM_uint32
* minor_status
,
59 gss_ctx_id_t
* context_handle
,
60 const gss_cred_id_t acceptor_cred_handle
,
61 const gss_buffer_t input_token_buffer
,
62 const gss_channel_bindings_t input_chan_bindings
,
63 gss_name_t
* src_name
,
65 gss_buffer_t output_token
,
66 OM_uint32
* ret_flags
,
68 gss_cred_id_t
* delegated_cred_handle
74 krb5_flags ap_options
;
76 krb5_ticket
*ticket
= NULL
;
77 krb5_keytab keytab
= NULL
;
83 krb5_data_zero (&fwd_data
);
84 output_token
->length
= 0;
85 output_token
->value
= NULL
;
87 if (*context_handle
== GSS_C_NO_CONTEXT
) {
88 *context_handle
= malloc(sizeof(**context_handle
));
89 if (*context_handle
== GSS_C_NO_CONTEXT
) {
90 *minor_status
= ENOMEM
;
95 (*context_handle
)->auth_context
= NULL
;
96 (*context_handle
)->source
= NULL
;
97 (*context_handle
)->target
= NULL
;
98 (*context_handle
)->flags
= 0;
99 (*context_handle
)->more_flags
= 0;
100 (*context_handle
)->ticket
= NULL
;
102 if (src_name
!= NULL
)
105 kret
= krb5_auth_con_init (gssapi_krb5_context
,
106 &(*context_handle
)->auth_context
);
109 *minor_status
= kret
;
110 gssapi_krb5_set_error_string ();
114 if (input_chan_bindings
!= GSS_C_NO_CHANNEL_BINDINGS
115 && input_chan_bindings
->application_data
.length
==
116 2 * sizeof((*context_handle
)->auth_context
->local_port
)
119 /* Port numbers are expected to be in application_data.value,
120 * initator's port first */
122 krb5_address initiator_addr
, acceptor_addr
;
124 memset(&initiator_addr
, 0, sizeof(initiator_addr
));
125 memset(&acceptor_addr
, 0, sizeof(acceptor_addr
));
127 (*context_handle
)->auth_context
->remote_port
=
128 *(int16_t *) input_chan_bindings
->application_data
.value
;
130 (*context_handle
)->auth_context
->local_port
=
131 *((int16_t *) input_chan_bindings
->application_data
.value
+ 1);
134 kret
= gss_address_to_krb5addr(input_chan_bindings
->acceptor_addrtype
,
135 &input_chan_bindings
->acceptor_address
,
136 (*context_handle
)->auth_context
->local_port
,
139 *minor_status
= kret
;
140 gssapi_krb5_set_error_string ();
141 ret
= GSS_S_BAD_BINDINGS
;
145 kret
= gss_address_to_krb5addr(input_chan_bindings
->initiator_addrtype
,
146 &input_chan_bindings
->initiator_address
,
147 (*context_handle
)->auth_context
->remote_port
,
150 krb5_free_address (gssapi_krb5_context
, &acceptor_addr
);
151 *minor_status
= kret
;
152 gssapi_krb5_set_error_string ();
153 ret
= GSS_S_BAD_BINDINGS
;
157 kret
= krb5_auth_con_setaddrs(gssapi_krb5_context
,
158 (*context_handle
)->auth_context
,
159 &acceptor_addr
, /* local address */
160 &initiator_addr
); /* remote address */
162 krb5_free_address (gssapi_krb5_context
, &initiator_addr
);
163 krb5_free_address (gssapi_krb5_context
, &acceptor_addr
);
166 free(input_chan_bindings
->application_data
.value
);
167 input_chan_bindings
->application_data
.value
= NULL
;
168 input_chan_bindings
->application_data
.length
= 0;
172 *minor_status
= kret
;
173 gssapi_krb5_set_error_string ();
174 ret
= GSS_S_BAD_BINDINGS
;
184 krb5_auth_con_getflags(gssapi_krb5_context
,
185 (*context_handle
)->auth_context
,
187 tmp
|= KRB5_AUTH_CONTEXT_DO_SEQUENCE
;
188 krb5_auth_con_setflags(gssapi_krb5_context
,
189 (*context_handle
)->auth_context
,
193 ret
= gssapi_krb5_decapsulate (input_token_buffer
,
201 if (acceptor_cred_handle
== GSS_C_NO_CREDENTIAL
) {
202 if (gss_keytab
!= NULL
) {
205 } else if (acceptor_cred_handle
->keytab
!= NULL
) {
206 keytab
= acceptor_cred_handle
->keytab
;
209 kret
= krb5_rd_req (gssapi_krb5_context
,
210 &(*context_handle
)->auth_context
,
212 (acceptor_cred_handle
== GSS_C_NO_CREDENTIAL
) ? NULL
213 : acceptor_cred_handle
->principal
,
219 *minor_status
= kret
;
220 gssapi_krb5_set_error_string ();
224 kret
= krb5_copy_principal (gssapi_krb5_context
,
226 &(*context_handle
)->source
);
229 *minor_status
= kret
;
230 gssapi_krb5_set_error_string ();
234 kret
= krb5_copy_principal (gssapi_krb5_context
,
236 &(*context_handle
)->target
);
239 *minor_status
= kret
;
240 gssapi_krb5_set_error_string ();
244 if (src_name
!= NULL
) {
245 kret
= krb5_copy_principal (gssapi_krb5_context
,
250 *minor_status
= kret
;
251 gssapi_krb5_set_error_string ();
257 krb5_authenticator authenticator
;
259 kret
= krb5_auth_con_getauthenticator(gssapi_krb5_context
,
260 (*context_handle
)->auth_context
,
264 *minor_status
= kret
;
265 gssapi_krb5_set_error_string ();
269 kret
= gssapi_krb5_verify_8003_checksum(input_chan_bindings
,
270 authenticator
->cksum
,
273 krb5_free_authenticator(gssapi_krb5_context
, &authenticator
);
276 *minor_status
= kret
;
277 gssapi_krb5_set_error_string ();
282 if (fwd_data
.length
> 0 && (flags
& GSS_C_DELEG_FLAG
)) {
286 if (delegated_cred_handle
== NULL
|| *delegated_cred_handle
== NULL
)
287 /* XXX Create a new delegated_cred_handle? */
288 kret
= krb5_cc_default (gssapi_krb5_context
, &ccache
);
291 if ((*delegated_cred_handle
)->ccache
== NULL
)
292 kret
= krb5_cc_gen_new (gssapi_krb5_context
,
294 &(*delegated_cred_handle
)->ccache
);
295 ccache
= (*delegated_cred_handle
)->ccache
;
299 flags
&= ~GSS_C_DELEG_FLAG
;
303 kret
= krb5_cc_initialize(gssapi_krb5_context
,
307 flags
&= ~GSS_C_DELEG_FLAG
;
311 kret
= krb5_rd_cred2(gssapi_krb5_context
,
312 (*context_handle
)->auth_context
,
316 flags
&= ~GSS_C_DELEG_FLAG
;
325 flags
|= GSS_C_TRANS_FLAG
;
329 (*context_handle
)->flags
= flags
;
330 (*context_handle
)->more_flags
|= OPEN
;
333 *mech_type
= GSS_KRB5_MECHANISM
;
336 *time_rec
= GSS_C_INDEFINITE
;
338 if(flags
& GSS_C_MUTUAL_FLAG
) {
341 kret
= krb5_mk_rep (gssapi_krb5_context
,
342 (*context_handle
)->auth_context
,
346 *minor_status
= kret
;
347 gssapi_krb5_set_error_string ();
350 ret
= gssapi_krb5_encapsulate (&outbuf
,
353 krb5_data_free (&outbuf
);
359 output_token
->length
= 0;
362 (*context_handle
)->ticket
= ticket
;
366 krb5_free_ticket (context
, ticket
);
369 return GSS_S_COMPLETE
;
372 if (fwd_data
.length
> 0)
375 krb5_free_ticket (gssapi_krb5_context
, ticket
);
376 krb5_auth_con_free (gssapi_krb5_context
,
377 (*context_handle
)->auth_context
);
378 if((*context_handle
)->source
)
379 krb5_free_principal (gssapi_krb5_context
,
380 (*context_handle
)->source
);
381 if((*context_handle
)->target
)
382 krb5_free_principal (gssapi_krb5_context
,
383 (*context_handle
)->target
);
384 free (*context_handle
);
385 if (src_name
!= NULL
) {
386 gss_release_name (&minor
, src_name
);
389 *context_handle
= GSS_C_NO_CONTEXT
;
390 *minor_status
= kret
;
391 return GSS_S_FAILURE
;